This repository was archived by the owner on Jun 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathminica.c
492 lines (403 loc) · 14.8 KB
/
minica.c
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <pthread.h>
#ifdef __LINUX__
#include <alloca.h>
#endif
#include <mbedtls/x509_crt.h>
#include <mbedtls/x509_csr.h>
#include <mbedtls/x509.h>
#include <mbedtls/entropy.h>
#include <mbedtls/ctr_drbg.h>
#include <mbedtls/error.h>
#include "ssl.h"
#include "minica.h"
#include "main.h"
#include "core.h"
#include "utils.h"
static pthread_mutex_t certificate_mutex;
/*
*
* Internal CA for proxenet, to generate on-the-fly valid certificates
*
*/
/**
* Print certificate serial.
*
*/
static void print_cert_serial(proxenet_ssl_buf_t serial)
{
size_t i, l;
char *m;
unsigned char *s;
s = serial.p;
l = serial.len;
m = alloca(l*3 + 1);
proxenet_xzero(m, l*3 + 1);
for(i=0; i<l; i++) sprintf(&m[i*3], "%.2x:", s[i]);
xlog(LOG_INFO, "Serial is '%s' (len=%u)\n", m, l);
return;
}
/**
* Get the serial from the certificate of the server
*
* @return 0 if success, -1 otherwise
*/
int proxenet_get_cert_serial(ssl_atom_t* ssl, proxenet_ssl_buf_t *serial)
{
proxenet_ssl_context_t* ctx;
const mbedtls_x509_crt *crt;
ctx = &ssl->context;
crt = mbedtls_ssl_get_peer_cert(ctx);
if (!crt){
xlog(LOG_ERROR, "%s\n", "Failed to get peer certificate");
return -1;
}
*serial = crt->serial;
if (cfg->verbose)
print_cert_serial(*serial);
return 0;
}
/**
*
*/
static int ca_init_prng(mbedtls_entropy_context *entropy, mbedtls_ctr_drbg_context *ctr_drbg)
{
int retcode;
mbedtls_entropy_init( entropy );
mbedtls_ctr_drbg_init( ctr_drbg );
retcode = mbedtls_ctr_drbg_seed(ctr_drbg, mbedtls_entropy_func,
entropy, (unsigned char*)PROGNAME,
strlen(PROGNAME));
if( retcode < 0 ){
xlog(LOG_ERROR, "mbedtls_ctr_drbg_seed() returned %d\n", retcode);
return -1;
}
return 0;
}
/**
*
*/
static int ca_release_prng(mbedtls_entropy_context *entropy, mbedtls_ctr_drbg_context *ctr_drbg)
{
mbedtls_ctr_drbg_free(ctr_drbg);
mbedtls_entropy_free(entropy);
return 0;
}
/**
* Generate a temporary certificate signature request
*
* @return 0 if successful, -1 otherwise
*/
static int ca_generate_csr(mbedtls_ctr_drbg_context *ctr_drbg, char* hostname, unsigned char* csrbuf, size_t csrbuf_len)
{
int retcode;
char subj_name[256] = {0, };
mbedtls_x509write_csr csr;
mbedtls_pk_context key;
/* init structures */
mbedtls_x509write_csr_init( &csr );
mbedtls_x509write_csr_set_md_alg( &csr, MBEDTLS_MD_SHA1 );
/* set the key */
mbedtls_pk_init( &key );
#ifdef DEBUG
xlog(LOG_DEBUG, "cert=%s pwd=%s\n", cfg->certskey, cfg->certskey_pwd);
#endif
retcode = mbedtls_pk_parse_keyfile(&key, cfg->certskey, cfg->certskey_pwd);
if(retcode < 0) {
xlog(LOG_ERROR, "pk_parse_keyfile() returned %#x\n", retcode);
retcode = -1;
goto free_all;
}
mbedtls_x509write_csr_set_key( &csr, &key );
/* set the subject name */
proxenet_xsnprintf(subj_name, sizeof(subj_name), PROXENET_CERT_SUBJECT, hostname);
retcode = mbedtls_x509write_csr_set_subject_name( &csr, subj_name );
if( retcode < 0 ) {
xlog(LOG_ERROR, "x509write_csr_set_subject_name() returned %d\n", retcode);
retcode = -1;
goto free_all;
}
/* write the csr */
retcode = mbedtls_x509write_csr_pem(&csr, csrbuf, csrbuf_len, mbedtls_ctr_drbg_random, ctr_drbg);
if (retcode < 0 ){
xlog(LOG_ERROR, "x509write_csr_pem() returned %d\n", retcode);
retcode = -1;
goto free_all;
}
if (cfg->verbose)
xlog(LOG_INFO, "Generated CSR for '%s'\n", hostname);
free_all:
mbedtls_pk_free( &key );
mbedtls_x509write_csr_free( &csr );
return retcode;
}
/**
* Generate a CRT for CSR given in input.
*
* @return 0 if successful, -1 otherwise
*/
static int ca_generate_crt(mbedtls_ctr_drbg_context *ctr_drbg, unsigned char* csrbuf, size_t csrbuf_len, unsigned char* crtbuf, size_t crtbuf_len)
{
int retcode = -1;
char errbuf[256] = {0, };
char subject_name[256] = {0, };
char issuer_name[256] = {0, };
char serial_str[32] = {0, };
mbedtls_x509_csr csr;
mbedtls_x509_crt issuer_crt;
mbedtls_pk_context issuer_key;
mbedtls_x509write_cert crt;
mbedtls_mpi serial;
#ifdef DEBUG
xlog(LOG_DEBUG, "%s\n", "CRT init structs");
#endif
/* init structs */
mbedtls_x509write_crt_init( &crt );
mbedtls_x509write_crt_set_md_alg( &crt, MBEDTLS_MD_SHA1 );
mbedtls_x509_csr_init( &csr );
mbedtls_x509_crt_init( &issuer_crt );
mbedtls_pk_init( &issuer_key );
mbedtls_mpi_init( &serial );
#ifdef DEBUG
xlog(LOG_DEBUG, "%s\n", "CRT load cert & key");
#endif
proxenet_xsnprintf(serial_str, sizeof(serial_str), "%u", ++serial_base);
retcode = mbedtls_mpi_read_string(&serial, 10, serial_str);
if(retcode < 0) {
mbedtls_strerror(retcode, errbuf, sizeof(errbuf));
xlog(LOG_ERROR, "mpi_read_string() returned -0x%02x - %s\n", -retcode, errbuf);
goto exit;
}
/* load proxenet CA certificate */
retcode = mbedtls_x509_crt_parse_file(&issuer_crt, cfg->cafile);
if(retcode < 0) {
mbedtls_strerror(retcode, errbuf, sizeof(errbuf));
xlog(LOG_ERROR, "x509_crt_parse_file() returned -0x%02x - %s\n", -retcode, errbuf);
goto exit;
}
/* load proxenet CA key */
retcode = mbedtls_pk_parse_keyfile(&issuer_key, cfg->keyfile, cfg->keyfile_pwd);
if(retcode < 0){
mbedtls_strerror(retcode, errbuf, sizeof(errbuf));
xlog(LOG_ERROR, "pk_parse_keyfile() returned -0x%02x - %s\n", -retcode, errbuf);
goto exit;
}
/* get proxenet CA CN field */
retcode = mbedtls_x509_dn_gets(issuer_name, sizeof(issuer_name), &issuer_crt.subject);
if(retcode < 0) {
mbedtls_strerror(retcode, errbuf, sizeof(errbuf));
xlog(LOG_ERROR, "x509_dn_gets() returned -0x%02x - %s\n", -retcode, errbuf);
goto exit;
}
/* parse CSR */
retcode = mbedtls_x509_csr_parse(&csr, csrbuf, csrbuf_len);
if(retcode < 0) {
mbedtls_strerror(retcode, errbuf, sizeof(errbuf));
xlog(LOG_ERROR, "x509_csr_parse() returned -0x%02x - %s\n", -retcode, errbuf );
goto exit;
}
/* load CSR subject name */
retcode = mbedtls_x509_dn_gets(subject_name, sizeof(subject_name), &csr.subject);
if(retcode < 0) {
mbedtls_strerror(retcode, errbuf, sizeof(errbuf));
xlog(LOG_ERROR, "x509_csr_parse() returned -0x%02x - %s\n", -retcode, errbuf );
goto exit;
}
#ifdef DEBUG
xlog(LOG_DEBUG, "%s\n", "CRT fill new crt fields");
#endif
/* apply settings */
mbedtls_x509write_crt_set_subject_key(&crt, &csr.pk);
mbedtls_x509write_crt_set_issuer_key(&crt, &issuer_key);
retcode = mbedtls_x509write_crt_set_subject_name(&crt, subject_name);
if(retcode < 0) {
mbedtls_strerror(retcode, errbuf, sizeof(errbuf));
xlog(LOG_ERROR, "x509write_crt_set_subject_name() returned -0x%02x - %s\n", -retcode, errbuf );
goto exit;
}
retcode = mbedtls_x509write_crt_set_issuer_name(&crt, issuer_name);
if(retcode < 0) {
mbedtls_strerror(retcode, errbuf, sizeof(errbuf));
xlog(LOG_ERROR, "x509write_crt_set_issuer_name() returned -0x%02x - %s\n", -retcode, errbuf );
goto exit;
}
retcode = mbedtls_x509write_crt_set_serial(&crt, &serial);
if(retcode < 0) {
mbedtls_strerror(retcode, errbuf, sizeof(errbuf));
xlog(LOG_ERROR, "x509write_crt_set_serial() returned -0x%02x - %s\n", -retcode, errbuf );
goto exit;
}
retcode = mbedtls_x509write_crt_set_validity(&crt, PROXENET_CERT_NOT_BEFORE, PROXENET_CERT_NOT_AFTER);
if(retcode < 0) {
mbedtls_strerror(retcode, errbuf, sizeof(errbuf));
xlog(LOG_ERROR, "x509write_crt_set_validity() returned -0x%02x - %s\n", -retcode, errbuf );
goto exit;
}
retcode = mbedtls_x509write_crt_set_basic_constraints(&crt, false, 0);
if(retcode < 0) {
mbedtls_strerror(retcode, errbuf, sizeof(errbuf));
xlog(LOG_ERROR, "x509write_crt_set_basic_constraints() returned -0x%02x - %s\n", -retcode, errbuf );
goto exit;
}
retcode = mbedtls_x509write_crt_set_subject_key_identifier( &crt );
if(retcode < 0) {
mbedtls_strerror(retcode, errbuf, sizeof(errbuf));
xlog(LOG_ERROR, "x509write_crt_set_subject_key_identifier() returned -0x%02x - %s\n", -retcode, errbuf );
goto exit;
}
retcode = mbedtls_x509write_crt_set_authority_key_identifier( &crt );
if(retcode < 0) {
mbedtls_strerror(retcode, errbuf, sizeof(errbuf));
xlog(LOG_ERROR, "x509write_crt_set_authority_key_identifier() returned -0x%02x - %s\n", -retcode, errbuf );
goto exit;
}
retcode = mbedtls_x509write_crt_set_ns_cert_type( &crt, MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER );
if(retcode < 0) {
mbedtls_strerror(retcode, errbuf, sizeof(errbuf));
xlog(LOG_ERROR, "x509write_crt_set_ns_cert_type() returned -0x%02x - %s\n", -retcode, errbuf );
goto exit;
}
/* write CRT in buffer */
retcode = mbedtls_x509write_crt_pem(&crt, crtbuf, crtbuf_len, mbedtls_ctr_drbg_random, ctr_drbg);
if( retcode < 0 ){
xlog(LOG_ERROR, "x509write_crt_pem() failed: %d\n", retcode);
return retcode;
}
/* free structs */
exit:
mbedtls_x509write_crt_free( &crt );
mbedtls_pk_free( &issuer_key );
mbedtls_mpi_free( &serial );
return retcode;
}
/**
* Generate the CRT file for the hostname. Stores it in keys/certs.
*
* This whole function (and sub-functions) is thread-safe (mutex).
*
* @return 0 if successful, -1 otherwise
*/
static int create_crt(char* hostname, char* crtpath)
{
int retcode, fd;
unsigned char csrbuf[4096]={0, };
unsigned char crtbuf[4096]={0, };
ssize_t n;
mbedtls_entropy_context entropy;
mbedtls_ctr_drbg_context ctr_drbg;
/* init prng */
retcode = ca_init_prng(&entropy, &ctr_drbg);
if(retcode<0)
goto exit;
/* generate csr w/ privkey static */
retcode = ca_generate_csr(&ctr_drbg, hostname, csrbuf, sizeof(csrbuf));
if(retcode<0)
goto exit;
#ifdef DEBUG_SSL
xlog(LOG_DEBUG, "CSR for '%s':\n%s\n", hostname, csrbuf);
#endif
/* sign csr w/ proxenet root crt (in `keys/`) */
#ifdef DEBUG_SSL
xlog(LOG_DEBUG, "Signing CSR for '%s' with '%s'(key='%s')\n", hostname, cfg->cafile, cfg->keyfile);
#endif
retcode = ca_generate_crt(&ctr_drbg, csrbuf, sizeof(csrbuf), crtbuf, sizeof(crtbuf));
if(retcode<0)
goto exit;
#ifdef DEBUG_SSL
xlog(LOG_DEBUG, "CRT signed for '%s':\n%s\n", hostname, crtbuf);
#endif
/* write CRT on FS */
#ifdef DEBUG
xlog(LOG_DEBUG, "Writing CRT signed in '%s'\n", crtpath);
#endif
fd = open(crtpath, O_WRONLY|O_CREAT|O_SYNC, S_IRUSR|S_IWUSR);
if(fd<0){
xlog(LOG_ERROR, "CRT open() failed: %s\n", strerror(errno));
retcode = -1;
goto exit;
}
n = write(fd, crtbuf, sizeof(csrbuf));
if(n<0){
xlog(LOG_ERROR, "CRT write() failed: %s\n", strerror(errno));
retcode = -1;
goto exit;
}
close(fd);
if(cfg->verbose)
xlog(LOG_INFO, "New CRT '%s'\n", crtpath);
/* delete csr & free rsrc */
retcode = 0;
exit:
ca_release_prng(&entropy, &ctr_drbg);
return retcode;
}
/**
* Lookup for a valid CRT file in cert dir.
*
* If found, the absolute path is stored in *crtpath. Its content *must* be free-ed by caller
* If not, *crtpath is NULL.
*
* @return 0 if successful, -1 otherwise
*/
int proxenet_lookup_crt(char* hostname, char** crtpath)
{
int retcode, n;
char path[PATH_MAX];
char *crt_realpath;
n = proxenet_xsnprintf(path, PATH_MAX, "%s/%s.crt", cfg->certsdir, hostname);
if (n<0){
*crtpath = NULL;
return -1;
}
#ifdef DEBUG
xlog(LOG_DEBUG, "Solving '%s'\n", path);
#endif
crt_realpath = proxenet_xstrdup2(path);
/* Was the certificate already generated? */
if (is_readable_file(crt_realpath)){
#ifdef DEBUG
xlog(LOG_DEBUG, "Certificate hit: hostname='%s' path='%s'\n", hostname, crt_realpath);
#endif
*crtpath = crt_realpath;
return 0;
}
/* if not, the certificate will be generated here after */
/* critical section to avoid race conditions on crt creation */
#ifdef DEBUG
xlog(LOG_DEBUG, "Acquiring cert_lock at %p\n", &certificate_mutex);
#endif
pthread_mutex_lock(&certificate_mutex);
/* To avoid TOC-TOU race, we need to re-check time if the certificate path */
if (is_readable_file(crt_realpath)){
#ifdef DEBUG
xlog(LOG_DEBUG, "Certificate hit (second check): hostname='%s' path='%s'\n", hostname, crt_realpath);
#endif
*crtpath = crt_realpath;
pthread_mutex_unlock(&certificate_mutex);
return 0;
}
/* if not, create && release lock && returns */
retcode = create_crt(hostname, crt_realpath);
if (retcode < 0){
xlog(LOG_ERROR, "create_crt(hostname='%s', crt='%s') has failed\n",
hostname, crt_realpath);
*crtpath = NULL;
pthread_mutex_unlock(&certificate_mutex);
return -1;
}
*crtpath = crt_realpath;
pthread_mutex_unlock(&certificate_mutex);
#ifdef DEBUG
xlog(LOG_DEBUG, "Certificate created: hostname='%s' path='%s'\n", hostname, crt_realpath);
#endif
/* end of critical section */
return 0;
}