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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
|
/* $OpenBSD: tls_config.c,v 1.36 2017/01/31 16:18:57 beck Exp $ */
/*
* Copyright (c) 2014 Joel Sing <jsing@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/stat.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <tls.h>
#include "tls_internal.h"
static int
set_string(const char **dest, const char *src)
{
free((char *)*dest);
*dest = NULL;
if (src != NULL)
if ((*dest = strdup(src)) == NULL)
return -1;
return 0;
}
static void *
memdup(const void *in, size_t len)
{
void *out;
if ((out = malloc(len)) == NULL)
return NULL;
memcpy(out, in, len);
return out;
}
static int
set_mem(char **dest, size_t *destlen, const void *src, size_t srclen)
{
free(*dest);
*dest = NULL;
*destlen = 0;
if (src != NULL)
if ((*dest = memdup(src, srclen)) == NULL)
return -1;
*destlen = srclen;
return 0;
}
static struct tls_keypair *
tls_keypair_new(void)
{
return calloc(1, sizeof(struct tls_keypair));
}
static int
tls_keypair_set_cert_file(struct tls_keypair *keypair, struct tls_error *error,
const char *cert_file)
{
return tls_config_load_file(error, "certificate", cert_file,
&keypair->cert_mem, &keypair->cert_len);
}
static int
tls_keypair_set_cert_mem(struct tls_keypair *keypair, const uint8_t *cert,
size_t len)
{
return set_mem(&keypair->cert_mem, &keypair->cert_len, cert, len);
}
static int
tls_keypair_set_key_file(struct tls_keypair *keypair, struct tls_error *error,
const char *key_file)
{
if (keypair->key_mem != NULL)
explicit_bzero(keypair->key_mem, keypair->key_len);
return tls_config_load_file(error, "key", key_file,
&keypair->key_mem, &keypair->key_len);
}
static int
tls_keypair_set_key_mem(struct tls_keypair *keypair, const uint8_t *key,
size_t len)
{
if (keypair->key_mem != NULL)
explicit_bzero(keypair->key_mem, keypair->key_len);
return set_mem(&keypair->key_mem, &keypair->key_len, key, len);
}
static int
tls_keypair_set_ocsp_staple_file(struct tls_keypair *keypair,
struct tls_error *error, const char *ocsp_file)
{
return tls_config_load_file(error, "ocsp", ocsp_file,
&keypair->ocsp_staple, &keypair->ocsp_staple_len);
}
static int
tls_keypair_set_ocsp_staple_mem(struct tls_keypair *keypair,
const uint8_t *staple, size_t len)
{
return set_mem(&keypair->ocsp_staple, &keypair->ocsp_staple_len, staple,
len);
}
static void
tls_keypair_clear(struct tls_keypair *keypair)
{
tls_keypair_set_cert_mem(keypair, NULL, 0);
tls_keypair_set_key_mem(keypair, NULL, 0);
}
static void
tls_keypair_free(struct tls_keypair *keypair)
{
if (keypair == NULL)
return;
tls_keypair_clear(keypair);
free(keypair->cert_mem);
free(keypair->key_mem);
free(keypair->ocsp_staple);
free(keypair);
}
int
tls_config_load_file(struct tls_error *error, const char *filetype,
const char *filename, char **buf, size_t *len)
{
struct stat st;
int fd = -1;
ssize_t n;
free(*buf);
*buf = NULL;
*len = 0;
if ((fd = open(filename, O_RDONLY)) == -1) {
tls_error_set(error, "failed to open %s file '%s'",
filetype, filename);
goto fail;
}
if (fstat(fd, &st) != 0) {
tls_error_set(error, "failed to stat %s file '%s'",
filetype, filename);
goto fail;
}
if (st.st_size < 0)
goto fail;
*len = (size_t)st.st_size;
if ((*buf = malloc(*len)) == NULL) {
tls_error_set(error, "failed to allocate buffer for "
"%s file", filetype);
goto fail;
}
n = read(fd, *buf, *len);
if (n < 0 || (size_t)n != *len) {
tls_error_set(error, "failed to read %s file '%s'",
filetype, filename);
goto fail;
}
close(fd);
return 0;
fail:
if (fd != -1)
close(fd);
if (*buf != NULL)
explicit_bzero(*buf, *len);
free(*buf);
*buf = NULL;
*len = 0;
return -1;
}
struct tls_config *
tls_config_new(void)
{
struct tls_config *config;
unsigned char sid[TLS_MAX_SESSION_ID_LENGTH];
if ((config = calloc(1, sizeof(*config))) == NULL)
return (NULL);
if ((config->keypair = tls_keypair_new()) == NULL)
goto err;
/*
* Default configuration.
*/
if (tls_config_set_dheparams(config, "none") != 0)
goto err;
if (tls_config_set_ecdhecurve(config, "auto") != 0)
goto err;
if (tls_config_set_ciphers(config, "secure") != 0)
goto err;
if (tls_config_set_protocols(config, TLS_PROTOCOLS_DEFAULT) != 0)
goto err;
if (tls_config_set_verify_depth(config, 6) != 0)
goto err;
/*
* Set session ID context to a random value. For the simple case
* of a single process server this is good enough. For multiprocess
* servers the session ID needs to be set by the caller.
*/
arc4random_buf(sid, sizeof(sid));
if (tls_config_set_session_id(config, sid, sizeof(sid)) != 0)
goto err;
config->ticket_keyrev = arc4random();
config->ticket_autorekey = 1;
tls_config_prefer_ciphers_server(config);
tls_config_verify(config);
return (config);
err:
tls_config_free(config);
return (NULL);
}
void
tls_config_free(struct tls_config *config)
{
struct tls_keypair *kp, *nkp;
if (config == NULL)
return;
for (kp = config->keypair; kp != NULL; kp = nkp) {
nkp = kp->next;
tls_keypair_free(kp);
}
free(config->error.msg);
free(config->alpn);
free((char *)config->ca_mem);
free((char *)config->ca_path);
free((char *)config->ciphers);
free(config);
}
static void
tls_config_keypair_add(struct tls_config *config, struct tls_keypair *keypair)
{
struct tls_keypair *kp;
kp = config->keypair;
while (kp->next != NULL)
kp = kp->next;
kp->next = keypair;
}
const char *
tls_config_error(struct tls_config *config)
{
return config->error.msg;
}
void
tls_config_clear_keys(struct tls_config *config)
{
struct tls_keypair *kp;
for (kp = config->keypair; kp != NULL; kp = kp->next)
tls_keypair_clear(kp);
tls_config_set_ca_mem(config, NULL, 0);
}
int
tls_config_parse_protocols(uint32_t *protocols, const char *protostr)
{
uint32_t proto, protos = 0;
char *s, *p, *q;
int negate;
if ((s = strdup(protostr)) == NULL)
return (-1);
q = s;
while ((p = strsep(&q, ",:")) != NULL) {
while (*p == ' ' || *p == '\t')
p++;
negate = 0;
if (*p == '!') {
negate = 1;
p++;
}
if (negate && protos == 0)
protos = TLS_PROTOCOLS_ALL;
proto = 0;
if (strcasecmp(p, "all") == 0 ||
strcasecmp(p, "legacy") == 0)
proto = TLS_PROTOCOLS_ALL;
else if (strcasecmp(p, "default") == 0 ||
strcasecmp(p, "secure") == 0)
proto = TLS_PROTOCOLS_DEFAULT;
if (strcasecmp(p, "tlsv1") == 0)
proto = TLS_PROTOCOL_TLSv1;
else if (strcasecmp(p, "tlsv1.0") == 0)
proto = TLS_PROTOCOL_TLSv1_0;
else if (strcasecmp(p, "tlsv1.1") == 0)
proto = TLS_PROTOCOL_TLSv1_1;
else if (strcasecmp(p, "tlsv1.2") == 0)
proto = TLS_PROTOCOL_TLSv1_2;
if (proto == 0) {
free(s);
return (-1);
}
if (negate)
protos &= ~proto;
else
protos |= proto;
}
*protocols = protos;
free(s);
return (0);
}
static int
tls_config_parse_alpn(struct tls_config *config, const char *alpn,
char **alpn_data, size_t *alpn_len)
{
size_t buf_len, i, len;
char *buf = NULL;
char *s = NULL;
char *p, *q;
free(*alpn_data);
*alpn_data = NULL;
*alpn_len = 0;
if ((buf_len = strlen(alpn) + 1) > 65535) {
tls_config_set_errorx(config, "alpn too large");
goto err;
}
if ((buf = malloc(buf_len)) == NULL) {
tls_config_set_errorx(config, "out of memory");
goto err;
}
if ((s = strdup(alpn)) == NULL) {
tls_config_set_errorx(config, "out of memory");
goto err;
}
i = 0;
q = s;
while ((p = strsep(&q, ",")) != NULL) {
if ((len = strlen(p)) == 0) {
tls_config_set_errorx(config,
"alpn protocol with zero length");
goto err;
}
if (len > 255) {
tls_config_set_errorx(config,
"alpn protocol too long");
goto err;
}
buf[i++] = len & 0xff;
memcpy(&buf[i], p, len);
i += len;
}
free(s);
*alpn_data = buf;
*alpn_len = buf_len;
return (0);
err:
free(buf);
free(s);
return (-1);
}
int
tls_config_set_alpn(struct tls_config *config, const char *alpn)
{
return tls_config_parse_alpn(config, alpn, &config->alpn,
&config->alpn_len);
}
static int
tls_config_add_keypair_file_internal(struct tls_config *config,
const char *cert_file, const char *key_file, const char *ocsp_file)
{
struct tls_keypair *keypair;
if ((keypair = tls_keypair_new()) == NULL)
return (-1);
if (tls_keypair_set_cert_file(keypair, &config->error, cert_file) != 0)
goto err;
if (tls_keypair_set_key_file(keypair, &config->error, key_file) != 0)
goto err;
if (ocsp_file != NULL &&
tls_keypair_set_ocsp_staple_file(keypair, &config->error,
ocsp_file) != 0)
goto err;
tls_config_keypair_add(config, keypair);
return (0);
err:
tls_keypair_free(keypair);
return (-1);
}
static int
tls_config_add_keypair_mem_internal(struct tls_config *config, const uint8_t *cert,
size_t cert_len, const uint8_t *key, size_t key_len,
const uint8_t *staple, size_t staple_len)
{
struct tls_keypair *keypair;
if ((keypair = tls_keypair_new()) == NULL)
return (-1);
if (tls_keypair_set_cert_mem(keypair, cert, cert_len) != 0)
goto err;
if (tls_keypair_set_key_mem(keypair, key, key_len) != 0)
goto err;
if (staple != NULL &&
tls_keypair_set_ocsp_staple_mem(keypair, staple, staple_len) != 0)
goto err;
tls_config_keypair_add(config, keypair);
return (0);
err:
tls_keypair_free(keypair);
return (-1);
}
int
tls_config_add_keypair_mem(struct tls_config *config, const uint8_t *cert,
size_t cert_len, const uint8_t *key, size_t key_len)
{
return tls_config_add_keypair_mem_internal(config, cert, cert_len, key,
key_len, NULL, 0);
}
int
tls_config_add_keypair_file(struct tls_config *config,
const char *cert_file, const char *key_file)
{
return tls_config_add_keypair_file_internal(config, cert_file,
key_file, NULL);
}
int
tls_config_add_keypair_ocsp_mem(struct tls_config *config, const uint8_t *cert,
size_t cert_len, const uint8_t *key, size_t key_len, const uint8_t *staple,
size_t staple_len)
{
return tls_config_add_keypair_mem_internal(config, cert, cert_len, key,
key_len, staple, staple_len);
}
int
tls_config_add_keypair_ocsp_file(struct tls_config *config,
const char *cert_file, const char *key_file, const char *ocsp_file)
{
return tls_config_add_keypair_file_internal(config, cert_file,
key_file, ocsp_file);
}
int
tls_config_set_ca_file(struct tls_config *config, const char *ca_file)
{
return tls_config_load_file(&config->error, "CA", ca_file,
&config->ca_mem, &config->ca_len);
}
int
tls_config_set_ca_path(struct tls_config *config, const char *ca_path)
{
return set_string(&config->ca_path, ca_path);
}
int
tls_config_set_ca_mem(struct tls_config *config, const uint8_t *ca, size_t len)
{
return set_mem(&config->ca_mem, &config->ca_len, ca, len);
}
int
tls_config_set_cert_file(struct tls_config *config, const char *cert_file)
{
return tls_keypair_set_cert_file(config->keypair, &config->error,
cert_file);
}
int
tls_config_set_cert_mem(struct tls_config *config, const uint8_t *cert,
size_t len)
{
return tls_keypair_set_cert_mem(config->keypair, cert, len);
}
int
tls_config_set_ciphers(struct tls_config *config, const char *ciphers)
{
SSL_CTX *ssl_ctx = NULL;
if (ciphers == NULL ||
strcasecmp(ciphers, "default") == 0 ||
strcasecmp(ciphers, "secure") == 0)
ciphers = TLS_CIPHERS_DEFAULT;
else if (strcasecmp(ciphers, "compat") == 0)
ciphers = TLS_CIPHERS_COMPAT;
else if (strcasecmp(ciphers, "legacy") == 0)
ciphers = TLS_CIPHERS_LEGACY;
else if (strcasecmp(ciphers, "all") == 0 ||
strcasecmp(ciphers, "insecure") == 0)
ciphers = TLS_CIPHERS_ALL;
if ((ssl_ctx = SSL_CTX_new(SSLv23_method())) == NULL) {
tls_config_set_errorx(config, "out of memory");
goto fail;
}
if (SSL_CTX_set_cipher_list(ssl_ctx, ciphers) != 1) {
tls_config_set_errorx(config, "no ciphers for '%s'", ciphers);
goto fail;
}
SSL_CTX_free(ssl_ctx);
return set_string(&config->ciphers, ciphers);
fail:
SSL_CTX_free(ssl_ctx);
return -1;
}
int
tls_config_set_dheparams(struct tls_config *config, const char *params)
{
int keylen;
if (params == NULL || strcasecmp(params, "none") == 0)
keylen = 0;
else if (strcasecmp(params, "auto") == 0)
keylen = -1;
else if (strcasecmp(params, "legacy") == 0)
keylen = 1024;
else {
tls_config_set_errorx(config, "invalid dhe param '%s'", params);
return (-1);
}
config->dheparams = keylen;
return (0);
}
int
tls_config_set_ecdhecurve(struct tls_config *config, const char *name)
{
int nid;
if (name == NULL || strcasecmp(name, "none") == 0)
nid = NID_undef;
else if (strcasecmp(name, "auto") == 0)
nid = -1;
else if ((nid = OBJ_txt2nid(name)) == NID_undef) {
tls_config_set_errorx(config, "invalid ecdhe curve '%s'", name);
return (-1);
}
config->ecdhecurve = nid;
return (0);
}
int
tls_config_set_key_file(struct tls_config *config, const char *key_file)
{
return tls_keypair_set_key_file(config->keypair, &config->error,
key_file);
}
int
tls_config_set_key_mem(struct tls_config *config, const uint8_t *key,
size_t len)
{
return tls_keypair_set_key_mem(config->keypair, key, len);
}
static int
tls_config_set_keypair_file_internal(struct tls_config *config,
const char *cert_file, const char *key_file, const char *ocsp_file)
{
if (tls_config_set_cert_file(config, cert_file) != 0)
return (-1);
if (tls_config_set_key_file(config, key_file) != 0)
return (-1);
if (tls_config_set_key_file(config, key_file) != 0)
return (-1);
if (ocsp_file != NULL &&
tls_config_set_ocsp_staple_file(config, ocsp_file) != 0)
return (-1);
return (0);
}
static int
tls_config_set_keypair_mem_internal(struct tls_config *config, const uint8_t *cert,
size_t cert_len, const uint8_t *key, size_t key_len,
const uint8_t *staple, size_t staple_len)
{
if (tls_config_set_cert_mem(config, cert, cert_len) != 0)
return (-1);
if (tls_config_set_key_mem(config, key, key_len) != 0)
return (-1);
if ((staple != NULL) &&
(tls_config_set_ocsp_staple_mem(config, staple, staple_len) != 0))
return (-1);
return (0);
}
int
tls_config_set_keypair_file(struct tls_config *config,
const char *cert_file, const char *key_file)
{
return tls_config_set_keypair_file_internal(config, cert_file, key_file,
NULL);
}
int
tls_config_set_keypair_mem(struct tls_config *config, const uint8_t *cert,
size_t cert_len, const uint8_t *key, size_t key_len)
{
return tls_config_set_keypair_mem_internal(config, cert, cert_len,
key, key_len, NULL, 0);
}
int
tls_config_set_keypair_ocsp_file(struct tls_config *config,
const char *cert_file, const char *key_file, const char *ocsp_file)
{
return tls_config_set_keypair_file_internal(config, cert_file, key_file,
ocsp_file);
}
int
tls_config_set_keypair_ocsp_mem(struct tls_config *config, const uint8_t *cert,
size_t cert_len, const uint8_t *key, size_t key_len,
const uint8_t *staple, size_t staple_len)
{
return tls_config_set_keypair_mem_internal(config, cert, cert_len,
key, key_len, staple, staple_len);
}
int
tls_config_set_protocols(struct tls_config *config, uint32_t protocols)
{
config->protocols = protocols;
return (0);
}
int
tls_config_set_verify_depth(struct tls_config *config, int verify_depth)
{
config->verify_depth = verify_depth;
return (0);
}
void
tls_config_prefer_ciphers_client(struct tls_config *config)
{
config->ciphers_server = 0;
}
void
tls_config_prefer_ciphers_server(struct tls_config *config)
{
config->ciphers_server = 1;
}
void
tls_config_insecure_noverifycert(struct tls_config *config)
{
config->verify_cert = 0;
}
void
tls_config_insecure_noverifyname(struct tls_config *config)
{
config->verify_name = 0;
}
void
tls_config_insecure_noverifytime(struct tls_config *config)
{
config->verify_time = 0;
}
void
tls_config_verify(struct tls_config *config)
{
config->verify_cert = 1;
config->verify_name = 1;
config->verify_time = 1;
}
void
tls_config_ocsp_require_stapling(struct tls_config *config)
{
config->ocsp_require_stapling = 1;
}
void
tls_config_verify_client(struct tls_config *config)
{
config->verify_client = 1;
}
void
tls_config_verify_client_optional(struct tls_config *config)
{
config->verify_client = 2;
}
int
tls_config_set_ocsp_staple_file(struct tls_config *config, const char *staple_file)
{
return tls_keypair_set_ocsp_staple_file(config->keypair, &config->error,
staple_file);
}
int
tls_config_set_ocsp_staple_mem(struct tls_config *config, const uint8_t *staple,
size_t len)
{
return tls_keypair_set_ocsp_staple_mem(config->keypair, staple, len);
}
int
tls_config_set_session_id(struct tls_config *config,
const unsigned char *session_id, size_t len)
{
if (len > TLS_MAX_SESSION_ID_LENGTH) {
tls_config_set_errorx(config, "session ID too large");
return (-1);
}
memset(config->session_id, 0, sizeof(config->session_id));
memcpy(config->session_id, session_id, len);
return (0);
}
int
tls_config_set_session_lifetime(struct tls_config *config, int lifetime)
{
if (lifetime > TLS_MAX_SESSION_TIMEOUT) {
tls_config_set_errorx(config, "session lifetime too large");
return (-1);
}
if (lifetime != 0 && lifetime < TLS_MIN_SESSION_TIMEOUT) {
tls_config_set_errorx(config, "session lifetime too small");
return (-1);
}
config->session_lifetime = lifetime;
return (0);
}
int
tls_config_add_ticket_key(struct tls_config *config, uint32_t keyrev,
unsigned char *key, size_t keylen)
{
struct tls_ticket_key newkey;
int i;
if (TLS_TICKET_KEY_SIZE != keylen ||
sizeof(newkey.aes_key) + sizeof(newkey.hmac_key) > keylen) {
tls_config_set_errorx(config,
"wrong amount of ticket key data");
return (-1);
}
keyrev = htonl(keyrev);
memset(&newkey, 0, sizeof(newkey));
memcpy(newkey.key_name, &keyrev, sizeof(keyrev));
memcpy(newkey.aes_key, key, sizeof(newkey.aes_key));
memcpy(newkey.hmac_key, key + sizeof(newkey.aes_key),
sizeof(newkey.hmac_key));
newkey.time = time(NULL);
for (i = 0; i < TLS_NUM_TICKETS; i++) {
struct tls_ticket_key *tk = &config->ticket_keys[i];
if (memcmp(newkey.key_name, tk->key_name,
sizeof(tk->key_name)) != 0)
continue;
/* allow re-entry of most recent key */
if (i == 0 && memcmp(newkey.aes_key, tk->aes_key,
sizeof(tk->aes_key)) == 0 && memcmp(newkey.hmac_key,
tk->hmac_key, sizeof(tk->hmac_key)) == 0)
return (0);
tls_config_set_errorx(config, "ticket key already present");
return (-1);
}
memmove(&config->ticket_keys[1], &config->ticket_keys[0],
sizeof(config->ticket_keys) - sizeof(config->ticket_keys[0]));
config->ticket_keys[0] = newkey;
config->ticket_autorekey = 0;
return (0);
}
int
tls_config_ticket_autorekey(struct tls_config *config)
{
unsigned char key[TLS_TICKET_KEY_SIZE];
int rv;
arc4random_buf(key, sizeof(key));
rv = tls_config_add_ticket_key(config, config->ticket_keyrev++, key,
sizeof(key));
config->ticket_autorekey = 1;
return (rv);
}
|