-
Notifications
You must be signed in to change notification settings - Fork 7
/
tls_helpers.cpp
174 lines (138 loc) · 4.9 KB
/
tls_helpers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#pragma once
#include "shared.cpp"
#if HAVE_TLS
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/x509.h>
#include <openssl/x509v3.h>
#include <openssl/rand.h>
bool cookie_init = 0;
unsigned char cookie_secret[16];
static void print_cert_hash(X509* cert)
{
unsigned char hash[160 / 8];
unsigned int hash_size = sizeof(hash);
if (X509_digest(cert, EVP_sha1(), hash, &hash_size) != 1) return;
for (unsigned int i = 0; i < hash_size; i++)
{
if (i % 2 == 0 && i != 0) printf(":");
printf("%02x", hash[i]);
}
}
int generate_cookie(SSL *ssl, unsigned char *cookie, unsigned int *cookie_len)
{
unsigned char *buffer, result[EVP_MAX_MD_SIZE];
unsigned int length = 0, result_len;
struct sockaddr_in peer;
if (!cookie_init)
{
if (!RAND_bytes(cookie_secret, sizeof(cookie_secret)))
{
fprintf(stderr, "Error generating cookie secret.\n");
return 0;
}
cookie_init = 1;
}
BIO_dgram_get_peer(SSL_get_rbio(ssl), &peer);
length = sizeof(struct in_addr) + sizeof(in_port_t);
buffer = (unsigned char*)OPENSSL_malloc(length);
memcpy(buffer, &peer.sin_port, sizeof(in_port_t));
memcpy(buffer + sizeof(peer.sin_port), &peer.sin_addr, sizeof(struct in_addr));
HMAC(EVP_sha1(), (const void*)cookie_secret, sizeof(cookie_secret), (const unsigned char*)buffer, length, result, &result_len);
OPENSSL_free(buffer);
memcpy(cookie, result, result_len);
*cookie_len = result_len;
return 1;
}
int verify_cookie(SSL *ssl, const unsigned char *cookie, unsigned int cookie_len)
{
unsigned char *buffer, result[EVP_MAX_MD_SIZE];
unsigned int length = 0, result_len;
struct sockaddr_in peer;
if (!cookie_init)
{
fprintf(stderr, "Verification called before cookie secret is initialized.\n");
return 0;
}
BIO_dgram_get_peer(SSL_get_rbio(ssl), &peer);
length = sizeof(struct in_addr) + sizeof(in_port_t);
buffer = (unsigned char*)OPENSSL_malloc(length);
memcpy(buffer, &peer.sin_port, sizeof(in_port_t));
memcpy(buffer + sizeof(peer.sin_port), &peer.sin_addr, sizeof(struct in_addr));
HMAC(EVP_sha1(), (const void*)cookie_secret, sizeof(cookie_secret), (const unsigned char*)buffer, length, result, &result_len);
OPENSSL_free(buffer);
if (cookie_len == result_len && memcmp(result, cookie, result_len) == 0)
{
return 1;
}
return 0;
}
static int ssl_gen_cert(const char *cn, X509 **cert, EVP_PKEY **key)
{
int res, ret = 0;
RSA *rsa;
*key = EVP_PKEY_new();
do {
BIGNUM *bne = BN_new();
ret = BN_set_word(bne, RSA_F4);
if (ret != 1) goto cleanup;
rsa = RSA_new();
ret = RSA_generate_key_ex(rsa, 2048, bne, NULL);
if (ret != 1) goto cleanup;
res = RSA_check_key(rsa);
} while (res == 0);
if (res == -1) return EXIT_FAILURE;
if (EVP_PKEY_assign_RSA(*key, rsa) == 0)
{
RSA_free(rsa);
goto cleanup;
}
*cert = X509_new();
if (X509_set_version(*cert, 2) == 0) goto cleanup;
if (X509_NAME_add_entry_by_txt(X509_get_subject_name(*cert), "commonName", MBSTRING_ASC, (unsigned char*)cn, -1, -1, 0) == 0) goto cleanup;
ASN1_INTEGER_set(X509_get_serialNumber(*cert), rand() & 0x7FFFFFFF);
char dnsName[128];
res = snprintf(dnsName, sizeof(dnsName), "DNS:%s", cn);
if (res < 0) goto cleanup;
X509V3_CTX ctx;
X509V3_set_ctx(&ctx, *cert, *cert, NULL, NULL, 0);
X509_EXTENSION *ext;
ext = X509V3_EXT_conf(NULL, &ctx, "subjectAltName", dnsName);
if (ext == NULL) goto cleanup;
if (X509_add_ext(*cert, ext, -1) == 0) goto cleanup;
#if (OPENSSL_VERSION_NUMBER >= 0x10100000L) && !defined LIBRESSL_VERSION_NUMBER
{
ASN1_TIME *tb = NULL, *ta = NULL;
if (X509_set_issuer_name(*cert, X509_get_subject_name(*cert)) == 0
|| (tb = ASN1_STRING_dup(X509_get0_notBefore(*cert))) == 0
|| X509_gmtime_adj(tb, 0) == 0
|| X509_set1_notBefore(*cert, tb) == 0
|| (ta = ASN1_STRING_dup(X509_get0_notAfter(*cert))) == 0
|| X509_gmtime_adj(ta, 60) == 0
|| X509_set1_notAfter(*cert, ta) == 0
|| X509_set_pubkey(*cert, *key) == 0)
{
ASN1_STRING_free(tb);
ASN1_STRING_free(ta);
goto cleanup;
}
ASN1_STRING_free(tb);
ASN1_STRING_free(ta);
}
#else
if (X509_set_issuer_name(*cert, X509_get_subject_name(*cert)) == 0
|| X509_gmtime_adj(X509_get_notBefore(*cert), 0) == 0
|| X509_gmtime_adj(X509_get_notAfter(*cert), 60 * 60 * 24 * 365) == 0
|| X509_set_pubkey(*cert, *key) == 0)
{
goto cleanup;
}
#endif
if (X509_sign(*cert, *key, EVP_sha1()) == 0) goto cleanup;
return EXIT_SUCCESS;
cleanup:
if (*cert != NULL) X509_free(*cert);
if (*key != NULL) EVP_PKEY_free(*key);
return EXIT_FAILURE;
}
#endif