Skip to content

Instantly share code, notes, and snippets.

Created May 27, 2019 13:48
Show Gist options
  • Save lava/94b4aff70ca5f25929603fc754ab5b33 to your computer and use it in GitHub Desktop.
Save lava/94b4aff70ca5f25929603fc754ab5b33 to your computer and use it in GitHub Desktop.
Shitty TLS server that tries to proivde a zero-length certificate.
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
// Implementation of a TLS "server" that sends an empty server
// certificate to clients that try to connect.
#define EXIT_FAILURE 1
int create_socket(int port)
int s;
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
s = socket(AF_INET, SOCK_STREAM, 0);
if (s < 0) {
perror("Unable to create socket");
if (bind(s, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
perror("Unable to bind");
if (listen(s, 1) < 0) {
perror("Unable to listen");
return s;
// Raw bytes from
const unsigned char ServerHello[] = {
0x03, 0x03,
0x00, 0x31,
0x00, 0x00, 0x2d,
0x03, 0x03,
0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
//0xc0, 0x13, // Cipher suite: TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA (0xc013)
0x00, 0x33, // Cipher Suite: TLS_DHE_RSA_WITH_AES_128_CBC_SHA (0x0033)
0x00, 0x05,
0xff, 0x01, 0x00, 0x01, 0x00,
// This is the only one that was heavily modified from the original, replacing the certificate
// by a zero-length certificate.
const unsigned char ServerCertificate[] = {
// Record Header
0x16, // Message type 0x16 (handshake record)
0x03, 0x03, // Protocol version 3,3
0x00, 0x0a, // Remaining bytes of handshake message
// Handshake Header
0x0b, // handshake message type 0x0b (certificate)
0x00, 0x00, 0x06, // Remaining bytes of certificate message
0x00, 0x00, 0x03, // Length of all certificate data below
0x00, 0x00, 0x00, // Length of first certificate
const unsigned char ServerKeyExchange[] = {
0x03, 0x03,
0x01, 0x2c,
0x00, 0x01, 0x28,
0x03, 0x00, 0x1d,
0x9f, 0xd7, 0xad, 0x6d, 0xcf, 0xf4, 0x29, 0x8d, 0xd3, 0xf9, 0x6d, 0x5b, 0x1b, 0x2a, 0xf9, 0x10, 0xa0, 0x53, 0x5b, 0x14, 0x88, 0xd7, 0xf8, 0xfa, 0xbb, 0x34, 0x9a, 0x98, 0x28, 0x80, 0xb6, 0x15,
0x04, 0x01,
0x01, 0x00,
0x04, 0x02, 0xb6, 0x61, 0xf7, 0xc1, 0x91, 0xee, 0x59, 0xbe, 0x45, 0x37, 0x66, 0x39, 0xbd, 0xc3, 0xd4, 0xbb, 0x81, 0xe1, 0x15, 0xca, 0x73, 0xc8, 0x34, 0x8b, 0x52, 0x5b, 0x0d, 0x23, 0x38, 0xaa, 0x14, 0x46, 0x67, 0xed, 0x94, 0x31, 0x02, 0x14, 0x12, 0xcd, 0x9b, 0x84, 0x4c, 0xba, 0x29, 0x93, 0x4a, 0xaa, 0xcc, 0xe8, 0x73, 0x41, 0x4e, 0xc1, 0x1c, 0xb0, 0x2e, 0x27, 0x2d, 0x0a, 0xd8, 0x1f, 0x76, 0x7d, 0x33, 0x07, 0x67, 0x21, 0xf1, 0x3b, 0xf3, 0x60, 0x20, 0xcf, 0x0b, 0x1f, 0xd0, 0xec, 0xb0, 0x78, 0xde, 0x11, 0x28, 0xbe, 0xba, 0x09, 0x49, 0xeb, 0xec, 0xe1, 0xa1, 0xf9, 0x6e, 0x20, 0x9d, 0xc3, 0x6e, 0x4f, 0xff, 0xd3, 0x6b, 0x67, 0x3a, 0x7d, 0xdc, 0x15, 0x97, 0xad, 0x44, 0x08, 0xe4, 0x85, 0xc4, 0xad, 0xb2, 0xc8, 0x73, 0x84, 0x12, 0x49, 0x37, 0x25, 0x23, 0x80, 0x9e, 0x43, 0x12, 0xd0, 0xc7, 0xb3, 0x52, 0x2e, 0xf9, 0x83, 0xca, 0xc1, 0xe0, 0x39, 0x35, 0xff, 0x13, 0xa8, 0xe9, 0x6b, 0xa6, 0x81, 0xa6, 0x2e, 0x40, 0xd3, 0xe7, 0x0a, 0x7f, 0xf3, 0x58, 0x66, 0xd3, 0xd9, 0x99, 0x3f, 0x9e, 0x26, 0xa6, 0x34, 0xc8, 0x1b, 0x4e, 0x71, 0x38, 0x0f, 0xcd, 0xd6, 0xf4, 0xe8, 0x35, 0xf7, 0x5a, 0x64, 0x09, 0xc7, 0xdc, 0x2c, 0x07, 0x41, 0x0e, 0x6f, 0x87, 0x85, 0x8c, 0x7b, 0x94, 0xc0, 0x1c, 0x2e, 0x32, 0xf2, 0x91, 0x76, 0x9e, 0xac, 0xca, 0x71, 0x64, 0x3b, 0x8b, 0x98, 0xa9, 0x63, 0xdf, 0x0a, 0x32, 0x9b, 0xea, 0x4e, 0xd6, 0x39, 0x7e, 0x8c, 0xd0, 0x1a, 0x11, 0x0a, 0xb3, 0x61, 0xac, 0x5b, 0xad, 0x1c, 0xcd, 0x84, 0x0a, 0x6c, 0x8a, 0x6e, 0xaa, 0x00, 0x1a, 0x9d, 0x7d, 0x87, 0xdc, 0x33, 0x18, 0x64, 0x35, 0x71, 0x22, 0x6c, 0x4d, 0xd2, 0xc2, 0xac, 0x41, 0xfb,
const unsigned char ServerHelloDone[] = {
0x03, 0x03,
0x00, 0x04,
0x0e, 0x00, 0x00, 0x00,
int main() {
int sock = create_socket(4447);
while(1) {
struct sockaddr_in addr;
uint len = sizeof(addr);
int client = accept(sock, (struct sockaddr*)&addr, &len);
if (client < 0) {
perror("Unable to accept");
char* incoming = new char[1024];
ssize_t n = recv(client, incoming, 1024, 0);
//printf("Received %ld bytes\n", tls_hdr_size);
write(client, ServerHello, sizeof(ServerHello));
write(client, ServerCertificate, sizeof(ServerCertificate));
write(client, ServerKeyExchange, sizeof(ServerKeyExchange));
write(client, ServerHelloDone, sizeof(ServerHelloDone));
read(client, incoming, 1024);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment