1
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
57
58
59
60
61
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
120
121
122
123
124
125
126
127
132
133
134
139
140
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
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
206
207
213
219
220
221
222
223
224
225
226
232
233
234
235
236
237
238
239
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
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
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
373
374
375
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
398
399
400
405
406
407
413
414
415
416
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
457
458
462
463
464
465
466
471
472
473
474
475
480
481
482
487
488
489
494
495
496
497
498
499
500
501
502
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
559
560
561
562
563
564
565
566
567
568
569
570
571
572
578
579
583
584
585
586
587
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
624
625
626
627
628
629
634
635
636
637
638
639
640
641
642
647
648
649
650
651
652
653
654
655
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
691
692
693
694
701
702
713
714
715
716
717
718
719
720
721
722
723
724
725
726
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
754
755
760
761
762
763
768
772
773
774
775
776
777
783
784
789
790
791
792
793
794
799
800
801
802
803
809
810
811
812
817
818
819
820
821
822
827
828
829
830
831
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
860
861
862
863
864
865
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
899
900
905
906
907
908
909
914
918
919
920
925
926
927
932
933
934
935
936
937
942
943
944
949
950
951
956
957
958
959
960
961
966
967
973
974
975
976
977
982
983
984
985
986
987
988
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1017
1018
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1086
1087
1088
1093
1094
1095
1096
1097
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1149
1150
1151
1152
1153
1154
1155
1160
1161
1162
1166
1167
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1205
1206
1207
1208
1209
1210
1211
1212
/* ... */
#include "includes.h"
#include "common.h"
#include "base64.h"
#include "crypto/crypto.h"
#include "crypto/sha1.h"
#include "pkcs5.h"
#include "pkcs8.h"
#include "x509v3.h"
#include "tlsv1_cred.h"9 includes
struct tlsv1_credentials * tlsv1_cred_alloc(void)
{
struct tlsv1_credentials *cred;
cred = os_zalloc(sizeof(*cred));
return cred;
}{ ... }
void tlsv1_cred_free(struct tlsv1_credentials *cred)
{
if (cred == NULL)
return;
x509_certificate_chain_free(cred->trusted_certs);
x509_certificate_chain_free(cred->cert);
crypto_private_key_free(cred->key);
os_free(cred->dh_p);
os_free(cred->dh_g);
os_free(cred->ocsp_stapling_response);
os_free(cred->ocsp_stapling_response_multi);
os_free(cred);
}{ ... }
static int tlsv1_add_cert_der(struct x509_certificate **chain,
const u8 *buf, size_t len)
{
struct x509_certificate *cert, *p;
char name[128];
cert = x509_certificate_parse(buf, len);
if (cert == NULL) {
wpa_printf(MSG_INFO, "TLSv1: %s - failed to parse certificate",
__func__);
return -1;
}{...}
p = *chain;
while (p && p->next)
p = p->next;
if (p && x509_name_compare(&cert->subject, &p->issuer) == 0) {
/* ... */
p->next = cert;
}{...} else {
cert->next = *chain;
*chain = cert;
}{...}
x509_name_string(&cert->subject, name, sizeof(name));
wpa_printf(MSG_DEBUG, "TLSv1: Added certificate: %s", name);
return 0;
}{ ... }
static const char *pem_cert_begin = "-----BEGIN CERTIFICATE-----";
static const char *pem_cert_end = "-----END CERTIFICATE-----";
static const char *pem_key_begin = "-----BEGIN RSA PRIVATE KEY-----";
static const char *pem_key_end = "-----END RSA PRIVATE KEY-----";
static const char *pem_key2_begin = "-----BEGIN PRIVATE KEY-----";
static const char *pem_key2_end = "-----END PRIVATE KEY-----";
static const char *pem_key_enc_begin = "-----BEGIN ENCRYPTED PRIVATE KEY-----";
static const char *pem_key_enc_end = "-----END ENCRYPTED PRIVATE KEY-----";
static const u8 * search_tag(const char *tag, const u8 *buf, size_t len)
{
size_t i, plen;
plen = os_strlen(tag);
if (len < plen)
return NULL;
for (i = 0; i < len - plen; i++) {
if (os_memcmp(buf + i, tag, plen) == 0)
return buf + i;
}{...}
return NULL;
}{ ... }
static int tlsv1_add_cert(struct x509_certificate **chain,
const u8 *buf, size_t len)
{
const u8 *pos, *end;
unsigned char *der;
size_t der_len;
pos = search_tag(pem_cert_begin, buf, len);
if (!pos) {
wpa_printf(MSG_DEBUG, "TLSv1: No PEM certificate tag found - "
"assume DER format");
return tlsv1_add_cert_der(chain, buf, len);
}{...}
wpa_printf(MSG_DEBUG, "TLSv1: Converting PEM format certificate into "
"DER format");
while (pos) {
pos += os_strlen(pem_cert_begin);
end = search_tag(pem_cert_end, pos, buf + len - pos);
if (end == NULL) {
wpa_printf(MSG_INFO, "TLSv1: Could not find PEM "
"certificate end tag (%s)", pem_cert_end);
return -1;
}{...}
der = base64_decode((const char *) pos, end - pos, &der_len);
if (der == NULL) {
wpa_printf(MSG_INFO, "TLSv1: Could not decode PEM "
"certificate");
return -1;
}{...}
if (tlsv1_add_cert_der(chain, der, der_len) < 0) {
wpa_printf(MSG_INFO, "TLSv1: Failed to parse PEM "
"certificate after DER conversion");
os_free(der);
return -1;
}{...}
os_free(der);
end += os_strlen(pem_cert_end);
pos = search_tag(pem_cert_begin, end, buf + len - end);
}{...}
return 0;
}{ ... }
static int tlsv1_set_cert_chain(struct x509_certificate **chain,
const char *cert, const u8 *cert_blob,
size_t cert_blob_len)
{
if (cert_blob)
return tlsv1_add_cert(chain, cert_blob, cert_blob_len);
if (cert) {
u8 *buf;
size_t len;
int ret;
buf = (u8 *) os_readfile(cert, &len);
if (buf == NULL) {
wpa_printf(MSG_INFO, "TLSv1: Failed to read '%s'",
cert);
return -1;
}{...}
ret = tlsv1_add_cert(chain, buf, len);
os_free(buf);
return ret;
}{...}
return 0;
}{ ... }
/* ... */
int tlsv1_set_ca_cert(struct tlsv1_credentials *cred, const char *cert,
const u8 *cert_blob, size_t cert_blob_len,
const char *path)
{
if (cert && os_strncmp(cert, "hash://", 7) == 0) {
const char *pos = cert + 7;
if (os_strncmp(pos, "server/sha256/", 14) != 0) {
wpa_printf(MSG_DEBUG,
"TLSv1: Unsupported ca_cert hash value '%s'",
cert);
return -1;
}{...}
pos += 14;
if (os_strlen(pos) != 32 * 2) {
wpa_printf(MSG_DEBUG,
"TLSv1: Unexpected SHA256 hash length in ca_cert '%s'",
cert);
return -1;
}{...}
if (hexstr2bin(pos, cred->srv_cert_hash, 32) < 0) {
wpa_printf(MSG_DEBUG,
"TLSv1: Invalid SHA256 hash value in ca_cert '%s'",
cert);
return -1;
}{...}
cred->server_cert_only = 1;
cred->ca_cert_verify = 0;
wpa_printf(MSG_DEBUG,
"TLSv1: Checking only server certificate match");
return 0;
}{...}
if (cert && os_strncmp(cert, "probe://", 8) == 0) {
cred->cert_probe = 1;
cred->ca_cert_verify = 0;
wpa_printf(MSG_DEBUG, "TLSv1: Only probe server certificate");
return 0;
}{...}
cred->ca_cert_verify = cert || cert_blob || path;
if (tlsv1_set_cert_chain(&cred->trusted_certs, cert,
cert_blob, cert_blob_len) < 0)
return -1;
if (path) {
wpa_printf(MSG_INFO, "TLSv1: Use of CA certificate directory "
"not yet supported");
return -1;
}{...}
return 0;
}{ ... }
/* ... */
int tlsv1_set_cert(struct tlsv1_credentials *cred, const char *cert,
const u8 *cert_blob, size_t cert_blob_len)
{
return tlsv1_set_cert_chain(&cred->cert, cert,
cert_blob, cert_blob_len);
}{ ... }
static struct crypto_private_key * tlsv1_set_key_pem(const u8 *key, size_t len)
{
const u8 *pos, *end;
unsigned char *der;
size_t der_len;
struct crypto_private_key *pkey;
pos = search_tag(pem_key_begin, key, len);
if (!pos) {
pos = search_tag(pem_key2_begin, key, len);
if (!pos)
return NULL;
pos += os_strlen(pem_key2_begin);
end = search_tag(pem_key2_end, pos, key + len - pos);
if (!end)
return NULL;
}{...} else {
const u8 *pos2;
pos += os_strlen(pem_key_begin);
end = search_tag(pem_key_end, pos, key + len - pos);
if (!end)
return NULL;
pos2 = search_tag("Proc-Type: 4,ENCRYPTED", pos, end - pos);
if (pos2) {
wpa_printf(MSG_DEBUG, "TLSv1: Unsupported private key "
"format (Proc-Type/DEK-Info)");
return NULL;
}{...}
}{...}
der = base64_decode((const char *) pos, end - pos, &der_len);
if (!der)
return NULL;
pkey = crypto_private_key_import(der, der_len, NULL);
os_free(der);
return pkey;
}{ ... }
static struct crypto_private_key * tlsv1_set_key_enc_pem(const u8 *key,
size_t len,
const char *passwd)
{
const u8 *pos, *end;
unsigned char *der;
size_t der_len;
struct crypto_private_key *pkey;
if (passwd == NULL)
return NULL;
pos = search_tag(pem_key_enc_begin, key, len);
if (!pos)
return NULL;
pos += os_strlen(pem_key_enc_begin);
end = search_tag(pem_key_enc_end, pos, key + len - pos);
if (!end)
return NULL;
der = base64_decode((const char *) pos, end - pos, &der_len);
if (!der)
return NULL;
pkey = crypto_private_key_import(der, der_len, passwd);
os_free(der);
return pkey;
}{ ... }
#ifdef PKCS12_FUNCS
static int oid_is_rsadsi(struct asn1_oid *oid)
{
return oid->len >= 4 &&
oid->oid[0] == 1 &&
oid->oid[1] == 2 &&
oid->oid[2] == 840 &&
oid->oid[3] == 113549 ;
}{...}
static int pkcs12_is_bagtype_oid(struct asn1_oid *oid, unsigned long type)
{
return oid->len == 9 &&
oid_is_rsadsi(oid) &&
oid->oid[4] == 1 &&
oid->oid[5] == 12 &&
oid->oid[6] == 10 &&
oid->oid[7] == 1 &&
oid->oid[8] == type;
}{...}
static int is_oid_pkcs7(struct asn1_oid *oid)
{
return oid->len == 7 &&
oid->oid[0] == 1 &&
oid->oid[1] == 2 &&
oid->oid[2] == 840 &&
oid->oid[3] == 113549 &&
oid->oid[4] == 1 &&
oid->oid[5] == 7 ;
}{...}
static int is_oid_pkcs7_data(struct asn1_oid *oid)
{
return is_oid_pkcs7(oid) && oid->oid[6] == 1 ;
}{...}
static int is_oid_pkcs7_enc_data(struct asn1_oid *oid)
{
return is_oid_pkcs7(oid) && oid->oid[6] == 6 ;
}{...}
static int is_oid_pkcs9(struct asn1_oid *oid)
{
return oid->len >= 6 &&
oid->oid[0] == 1 &&
oid->oid[1] == 2 &&
oid->oid[2] == 840 &&
oid->oid[3] == 113549 &&
oid->oid[4] == 1 &&
oid->oid[5] == 9 ;
}{...}
static int is_oid_pkcs9_friendly_name(struct asn1_oid *oid)
{
return oid->len == 7 && is_oid_pkcs9(oid) &&
oid->oid[6] == 20;
}{...}
static int is_oid_pkcs9_local_key_id(struct asn1_oid *oid)
{
return oid->len == 7 && is_oid_pkcs9(oid) &&
oid->oid[6] == 21;
}{...}
static int is_oid_pkcs9_x509_cert(struct asn1_oid *oid)
{
return oid->len == 8 && is_oid_pkcs9(oid) &&
oid->oid[6] == 22 &&
oid->oid[7] == 1 ;
}{...}
static int pkcs12_keybag(struct tlsv1_credentials *cred,
const u8 *buf, size_t len)
{
return 0;
}{...}
static int pkcs12_pkcs8_keybag(struct tlsv1_credentials *cred,
const u8 *buf, size_t len,
const char *passwd)
{
struct crypto_private_key *key;
key = pkcs8_enc_key_import(buf, len, passwd);
if (!key)
return -1;
wpa_printf(MSG_DEBUG,
"PKCS #12: Successfully decrypted PKCS8ShroudedKeyBag");
crypto_private_key_free(cred->key);
cred->key = key;
return 0;
}{...}
static int pkcs12_certbag(struct tlsv1_credentials *cred,
const u8 *buf, size_t len)
{
struct asn1_hdr hdr;
struct asn1_oid oid;
char obuf[80];
const u8 *pos, *end;
/* ... */
if (asn1_get_next(buf, len, &hdr) < 0 || !asn1_is_sequence(&hdr)) {
asn1_unexpected(&hdr, "PKCS #12: Expected SEQUENCE (CertBag)");
return -1;
}{...}
pos = hdr.payload;
end = hdr.payload + hdr.length;
if (asn1_get_oid(pos, end - pos, &oid, &pos)) {
wpa_printf(MSG_DEBUG,
"PKCS #12: Failed to parse OID (certId)");
return -1;
}{...}
asn1_oid_to_str(&oid, obuf, sizeof(obuf));
wpa_printf(MSG_DEBUG, "PKCS #12: certId %s", obuf);
if (!is_oid_pkcs9_x509_cert(&oid)) {
wpa_printf(MSG_DEBUG,
"PKCS #12: Ignored unsupported certificate type (certId %s)",
obuf);
}{...}
if (asn1_get_next(pos, end - pos, &hdr) < 0 || !hdr.constructed ||
!asn1_is_cs_tag(&hdr, 0)) {
asn1_unexpected(&hdr,
"PKCS #12: Expected [0] EXPLICIT (certValue)");
return -1;
}{...}
if (asn1_get_next(hdr.payload, hdr.length, &hdr) < 0 ||
!asn1_is_octetstring(&hdr)) {
asn1_unexpected(&hdr,
"PKCS #12: Expected OCTET STRING (x509Certificate)");
return -1;
}{...}
wpa_hexdump(MSG_DEBUG, "PKCS #12: x509Certificate",
hdr.payload, hdr.length);
if (cred->cert) {
struct x509_certificate *cert;
wpa_printf(MSG_DEBUG, "PKCS #12: Ignore extra certificate");
cert = x509_certificate_parse(hdr.payload, hdr.length);
if (!cert) {
wpa_printf(MSG_DEBUG,
"PKCS #12: Failed to parse x509Certificate");
return 0;
}{...}
x509_certificate_chain_free(cert);
return 0;
}{...}
return tlsv1_set_cert(cred, NULL, hdr.payload, hdr.length);
}{...}
static int pkcs12_parse_attr_friendly_name(const u8 *pos, const u8 *end)
{
struct asn1_hdr hdr;
/* ... */
if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
!asn1_is_bmpstring(&hdr)) {
asn1_unexpected(&hdr,
"PKCS #12: Expected BMPSTRING (friendlyName)");
return 0;
}{...}
wpa_hexdump_ascii(MSG_DEBUG, "PKCS #12: friendlyName",
hdr.payload, hdr.length);
return 0;
}{...}
static int pkcs12_parse_attr_local_key_id(const u8 *pos, const u8 *end)
{
struct asn1_hdr hdr;
/* ... */
if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
!asn1_is_octetstring(&hdr)) {
asn1_unexpected(&hdr,
"PKCS #12: Expected OCTET STRING (localKeyID)");
return -1;
}{...}
wpa_hexdump_key(MSG_DEBUG, "PKCS #12: localKeyID",
hdr.payload, hdr.length);
return 0;
}{...}
static int pkcs12_parse_attr(const u8 *pos, size_t len)
{
const u8 *end = pos + len;
struct asn1_hdr hdr;
struct asn1_oid a_oid;
char obuf[80];
/* ... */
if (asn1_get_oid(pos, end - pos, &a_oid, &pos)) {
wpa_printf(MSG_DEBUG, "PKCS #12: Failed to parse OID (attrId)");
return -1;
}{...}
asn1_oid_to_str(&a_oid, obuf, sizeof(obuf));
wpa_printf(MSG_DEBUG, "PKCS #12: attrId %s", obuf);
if (asn1_get_next(pos, end - pos, &hdr) < 0 || !asn1_is_set(&hdr)) {
asn1_unexpected(&hdr, "PKCS #12: Expected SET (attrValues)");
return -1;
}{...}
wpa_hexdump_key(MSG_MSGDUMP, "PKCS #12: attrValues",
hdr.payload, hdr.length);
pos = hdr.payload;
end = hdr.payload + hdr.length;
if (is_oid_pkcs9_friendly_name(&a_oid))
return pkcs12_parse_attr_friendly_name(pos, end);
if (is_oid_pkcs9_local_key_id(&a_oid))
return pkcs12_parse_attr_local_key_id(pos, end);
wpa_printf(MSG_DEBUG, "PKCS #12: Ignore unknown attribute");
return 0;
}{...}
static int pkcs12_safebag(struct tlsv1_credentials *cred,
const u8 *buf, size_t len, const char *passwd)
{
struct asn1_hdr hdr;
struct asn1_oid oid;
char obuf[80];
const u8 *pos = buf, *end = buf + len;
const u8 *value;
size_t value_len;
wpa_hexdump_key(MSG_MSGDUMP, "PKCS #12: SafeBag", buf, len);
if (asn1_get_oid(pos, end - pos, &oid, &pos)) {
wpa_printf(MSG_DEBUG,
"PKCS #12: Failed to parse OID (BAG-TYPE)");
return -1;
}{...}
asn1_oid_to_str(&oid, obuf, sizeof(obuf));
wpa_printf(MSG_DEBUG, "PKCS #12: BAG-TYPE %s", obuf);
if (asn1_get_next(pos, end - pos, &hdr) < 0 || !hdr.constructed ||
!asn1_is_cs_tag(&hdr, 0)) {
asn1_unexpected(&hdr,
"PKCS #12: Expected [0] EXPLICIT (bagValue)");
return 0;
}{...}
value = hdr.payload;
value_len = hdr.length;
wpa_hexdump_key(MSG_MSGDUMP, "PKCS #12: bagValue", value, value_len);
pos = hdr.payload + hdr.length;
if (pos < end) {
if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
!asn1_is_set(&hdr)) {
asn1_unexpected(&hdr,
"PKCS #12: Expected SET (bagAttributes)");
return -1;
}{...}
wpa_hexdump_key(MSG_MSGDUMP, "PKCS #12: bagAttributes",
hdr.payload, hdr.length);
pos = hdr.payload;
end = hdr.payload + hdr.length;
while (pos < end) {
if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
!asn1_is_sequence(&hdr)) {
asn1_unexpected(&hdr,
"PKCS #12: Expected SEQUENCE (PKCS12Attribute)");
return -1;
}{...}
if (pkcs12_parse_attr(hdr.payload, hdr.length) < 0)
return -1;
pos = hdr.payload + hdr.length;
}{...}
}{...}
if (pkcs12_is_bagtype_oid(&oid, 1))
return pkcs12_keybag(cred, value, value_len);
if (pkcs12_is_bagtype_oid(&oid, 2))
return pkcs12_pkcs8_keybag(cred, value, value_len, passwd);
if (pkcs12_is_bagtype_oid(&oid, 3))
return pkcs12_certbag(cred, value, value_len);
wpa_printf(MSG_DEBUG, "PKCS #12: Ignore unsupported BAG-TYPE");
return 0;
}{...}
static int pkcs12_safecontents(struct tlsv1_credentials *cred,
const u8 *buf, size_t len,
const char *passwd)
{
struct asn1_hdr hdr;
const u8 *pos, *end;
if (asn1_get_next(buf, len, &hdr) < 0 || !asn1_is_sequence(&hdr)) {
asn1_unexpected(&hdr,
"PKCS #12: Expected SEQUENCE (SafeContents)");
return -1;
}{...}
pos = hdr.payload;
end = hdr.payload + hdr.length;
/* ... */
while (pos < end) {
if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
!asn1_is_sequence(&hdr)) {
asn1_unexpected(&hdr,
"PKCS #12: Expected SEQUENCE (SafeBag)");
return -1;
}{...}
if (pkcs12_safebag(cred, hdr.payload, hdr.length, passwd) < 0)
return -1;
pos = hdr.payload + hdr.length;
}{...}
return 0;
}{...}
static int pkcs12_parse_content_data(struct tlsv1_credentials *cred,
const u8 *pos, const u8 *end,
const char *passwd)
{
struct asn1_hdr hdr;
if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
!asn1_is_octetstring(&hdr)) {
asn1_unexpected(&hdr, "PKCS #12: Expected OCTET STRING (Data)");
return -1;
}{...}
wpa_hexdump(MSG_MSGDUMP, "PKCS #12: Data", hdr.payload, hdr.length);
return pkcs12_safecontents(cred, hdr.payload, hdr.length, passwd);
}{...}
static int pkcs12_parse_content_enc_data(struct tlsv1_credentials *cred,
const u8 *pos, const u8 *end,
const char *passwd)
{
struct asn1_hdr hdr;
struct asn1_oid oid;
char buf[80];
const u8 *enc_alg;
u8 *data;
size_t enc_alg_len, data_len;
int res = -1;
/* ... */
if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
!asn1_is_sequence(&hdr)) {
asn1_unexpected(&hdr,
"PKCS #12: Expected SEQUENCE (EncryptedData)");
return 0;
}{...}
pos = hdr.payload;
if (asn1_get_next(pos, end - pos, &hdr) < 0 || !asn1_is_integer(&hdr)) {
asn1_unexpected(&hdr,
"PKCS #12: No INTEGER tag found for version");
return -1;
}{...}
if (hdr.length != 1 || hdr.payload[0] != 0) {
wpa_printf(MSG_DEBUG, "PKCS #12: Unrecognized PKCS #7 version");
return -1;
}{...}
pos = hdr.payload + hdr.length;
wpa_hexdump(MSG_MSGDUMP, "PKCS #12: EncryptedContentInfo",
pos, end - pos);
/* ... */
if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
!asn1_is_sequence(&hdr)) {
asn1_unexpected(&hdr,
"PKCS #12: Expected SEQUENCE (EncryptedContentInfo)");
return -1;
}{...}
pos = hdr.payload;
end = pos + hdr.length;
if (asn1_get_oid(pos, end - pos, &oid, &pos)) {
wpa_printf(MSG_DEBUG,
"PKCS #12: Could not find OBJECT IDENTIFIER (contentType)");
return -1;
}{...}
asn1_oid_to_str(&oid, buf, sizeof(buf));
wpa_printf(MSG_DEBUG, "PKCS #12: EncryptedContentInfo::contentType %s",
buf);
if (!is_oid_pkcs7_data(&oid)) {
wpa_printf(MSG_DEBUG,
"PKCS #12: Unsupported EncryptedContentInfo::contentType %s",
buf);
return 0;
}{...}
if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
!asn1_is_sequence(&hdr)) {
asn1_unexpected(&hdr,
"PKCS #12: Expected SEQUENCE (ContentEncryptionAlgorithmIdentifier)");
return -1;
}{...}
enc_alg = hdr.payload;
enc_alg_len = hdr.length;
pos = hdr.payload + hdr.length;
if (asn1_get_next(pos, end - pos, &hdr) < 0 || hdr.constructed ||
!asn1_is_cs_tag(&hdr, 0)) {
asn1_unexpected(&hdr,
"PKCS #12: Expected [0] IMPLICIT (encryptedContent)");
return -1;
}{...}
data = pkcs5_decrypt(enc_alg, enc_alg_len, hdr.payload, hdr.length,
passwd, &data_len);
if (data) {
wpa_hexdump_key(MSG_MSGDUMP,
"PKCS #12: Decrypted encryptedContent",
data, data_len);
res = pkcs12_safecontents(cred, data, data_len, passwd);
os_free(data);
}{...}
return res;
}{...}
static int pkcs12_parse_content(struct tlsv1_credentials *cred,
const u8 *buf, size_t len,
const char *passwd)
{
const u8 *pos = buf;
const u8 *end = buf + len;
struct asn1_oid oid;
char txt[80];
struct asn1_hdr hdr;
wpa_hexdump(MSG_MSGDUMP, "PKCS #12: ContentInfo", buf, len);
if (asn1_get_oid(pos, end - pos, &oid, &pos)) {
wpa_printf(MSG_DEBUG,
"PKCS #12: Could not find OBJECT IDENTIFIER (contentType)");
return 0;
}{...}
asn1_oid_to_str(&oid, txt, sizeof(txt));
wpa_printf(MSG_DEBUG, "PKCS #12: contentType %s", txt);
if (asn1_get_next(pos, end - pos, &hdr) < 0 || !hdr.constructed ||
!asn1_is_cs_tag(&hdr, 0)) {
asn1_unexpected(&hdr,
"PKCS #12: Expected [0] EXPLICIT (content)");
return 0;
}{...}
pos = hdr.payload;
if (is_oid_pkcs7_data(&oid))
return pkcs12_parse_content_data(cred, pos, end, passwd);
if (is_oid_pkcs7_enc_data(&oid))
return pkcs12_parse_content_enc_data(cred, pos, end, passwd);
wpa_printf(MSG_DEBUG, "PKCS #12: Ignored unsupported contentType %s",
txt);
return 0;
}{...}
static int pkcs12_parse(struct tlsv1_credentials *cred,
const u8 *key, size_t len, const char *passwd)
{
struct asn1_hdr hdr;
const u8 *pos, *end;
struct asn1_oid oid;
char buf[80];
/* ... */
if (asn1_get_next(key, len, &hdr) < 0 || !asn1_is_sequence(&hdr)) {
asn1_unexpected(&hdr,
"PKCS #12: Expected SEQUENCE (PFX); assume PKCS #12 not used");
return -1;
}{...}
pos = hdr.payload;
end = pos + hdr.length;
if (asn1_get_next(pos, end - pos, &hdr) < 0 || !asn1_is_integer(&hdr)) {
asn1_unexpected(&hdr,
"PKCS #12: No INTEGER tag found for version");
return -1;
}{...}
if (hdr.length != 1 || hdr.payload[0] != 3) {
wpa_printf(MSG_DEBUG, "PKCS #12: Unrecognized version");
return -1;
}{...}
pos = hdr.payload + hdr.length;
/* ... */
if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
!asn1_is_sequence(&hdr)) {
asn1_unexpected(&hdr,
"PKCS #12: Expected SEQUENCE (authSafe); assume PKCS #12 not used");
return -1;
}{...}
pos = hdr.payload;
end = pos + hdr.length;
if (asn1_get_oid(pos, end - pos, &oid, &pos)) {
wpa_printf(MSG_DEBUG,
"PKCS #12: Could not find OBJECT IDENTIFIER (contentType); assume PKCS #12 not used");
return -1;
}{...}
asn1_oid_to_str(&oid, buf, sizeof(buf));
wpa_printf(MSG_DEBUG, "PKCS #12: contentType %s", buf);
if (!is_oid_pkcs7_data(&oid)) {
wpa_printf(MSG_DEBUG, "PKCS #12: Unsupported contentType %s",
buf);
return -1;
}{...}
if (asn1_get_next(pos, end - pos, &hdr) < 0 || !hdr.constructed ||
!asn1_is_cs_tag(&hdr, 0)) {
asn1_unexpected(&hdr,
"PKCS #12: Expected [0] EXPLICIT (content); assume PKCS #12 not used");
return -1;
}{...}
pos = hdr.payload;
if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
!asn1_is_octetstring(&hdr)) {
asn1_unexpected(&hdr,
"PKCS #12: Expected OCTET STRING (Data); assume PKCS #12 not used");
return -1;
}{...}
/* ... */
wpa_hexdump(MSG_MSGDUMP, "PKCS #12: Data content",
hdr.payload, hdr.length);
if (asn1_get_next(hdr.payload, hdr.length, &hdr) < 0 ||
!asn1_is_sequence(&hdr)) {
asn1_unexpected(&hdr,
"PKCS #12: Expected SEQUENCE within Data content; assume PKCS #12 not used");
return -1;
}{...}
pos = hdr.payload;
end = pos + hdr.length;
while (end > pos) {
if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
!asn1_is_sequence(&hdr)) {
asn1_unexpected(&hdr,
"PKCS #12: Expected SEQUENCE (ContentInfo); assume PKCS #12 not used");
return -1;
}{...}
if (pkcs12_parse_content(cred, hdr.payload, hdr.length,
passwd) < 0)
return -1;
pos = hdr.payload + hdr.length;
}{...}
return 0;
}{...}
/* ... */
#endif
static int tlsv1_set_key(struct tlsv1_credentials *cred,
const u8 *key, size_t len, const char *passwd)
{
cred->key = crypto_private_key_import(key, len, passwd);
if (cred->key == NULL)
cred->key = tlsv1_set_key_pem(key, len);
if (cred->key == NULL)
cred->key = tlsv1_set_key_enc_pem(key, len, passwd);
#ifdef PKCS12_FUNCS
if (!cred->key)
pkcs12_parse(cred, key, len, passwd);/* ... */
#endif
if (cred->key == NULL) {
wpa_printf(MSG_INFO, "TLSv1: Failed to parse private key");
return -1;
}{...}
return 0;
}{ ... }
/* ... */
int tlsv1_set_private_key(struct tlsv1_credentials *cred,
const char *private_key,
const char *private_key_passwd,
const u8 *private_key_blob,
size_t private_key_blob_len)
{
crypto_private_key_free(cred->key);
cred->key = NULL;
if (private_key_blob)
return tlsv1_set_key(cred, private_key_blob,
private_key_blob_len,
private_key_passwd);
if (private_key) {
u8 *buf;
size_t len;
int ret;
buf = (u8 *) os_readfile(private_key, &len);
if (buf == NULL) {
wpa_printf(MSG_INFO, "TLSv1: Failed to read '%s'",
private_key);
return -1;
}{...}
ret = tlsv1_set_key(cred, buf, len, private_key_passwd);
os_free(buf);
return ret;
}{...}
return 0;
}{ ... }
static int tlsv1_set_dhparams_der(struct tlsv1_credentials *cred,
const u8 *dh, size_t len)
{
struct asn1_hdr hdr;
const u8 *pos, *end;
pos = dh;
end = dh + len;
/* ... */
if (asn1_get_next(pos, len, &hdr) < 0 || !asn1_is_sequence(&hdr)) {
asn1_unexpected(&hdr,
"DH: DH parameters did not start with a valid SEQUENCE");
return -1;
}{...}
pos = hdr.payload;
if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
!asn1_is_integer(&hdr)) {
asn1_unexpected(&hdr, "DH: No INTEGER tag found for p");
return -1;
}{...}
wpa_hexdump(MSG_MSGDUMP, "DH: prime (p)", hdr.payload, hdr.length);
if (hdr.length == 0)
return -1;
os_free(cred->dh_p);
cred->dh_p = os_memdup(hdr.payload, hdr.length);
if (cred->dh_p == NULL)
return -1;
cred->dh_p_len = hdr.length;
pos = hdr.payload + hdr.length;
if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
!asn1_is_integer(&hdr)) {
asn1_unexpected(&hdr, "DH: No INTEGER tag found for g");
return -1;
}{...}
wpa_hexdump(MSG_MSGDUMP, "DH: base (g)", hdr.payload, hdr.length);
if (hdr.length == 0)
return -1;
os_free(cred->dh_g);
cred->dh_g = os_memdup(hdr.payload, hdr.length);
if (cred->dh_g == NULL)
return -1;
cred->dh_g_len = hdr.length;
return 0;
}{ ... }
static const char *pem_dhparams_begin = "-----BEGIN DH PARAMETERS-----";
static const char *pem_dhparams_end = "-----END DH PARAMETERS-----";
static int tlsv1_set_dhparams_blob(struct tlsv1_credentials *cred,
const u8 *buf, size_t len)
{
const u8 *pos, *end;
unsigned char *der;
size_t der_len;
pos = search_tag(pem_dhparams_begin, buf, len);
if (!pos) {
wpa_printf(MSG_DEBUG, "TLSv1: No PEM dhparams tag found - "
"assume DER format");
return tlsv1_set_dhparams_der(cred, buf, len);
}{...}
wpa_printf(MSG_DEBUG, "TLSv1: Converting PEM format dhparams into DER "
"format");
pos += os_strlen(pem_dhparams_begin);
end = search_tag(pem_dhparams_end, pos, buf + len - pos);
if (end == NULL) {
wpa_printf(MSG_INFO, "TLSv1: Could not find PEM dhparams end "
"tag (%s)", pem_dhparams_end);
return -1;
}{...}
der = base64_decode((const char *) pos, end - pos, &der_len);
if (der == NULL) {
wpa_printf(MSG_INFO, "TLSv1: Could not decode PEM dhparams");
return -1;
}{...}
if (tlsv1_set_dhparams_der(cred, der, der_len) < 0) {
wpa_printf(MSG_INFO, "TLSv1: Failed to parse PEM dhparams "
"DER conversion");
os_free(der);
return -1;
}{...}
os_free(der);
return 0;
}{ ... }
/* ... */
int tlsv1_set_dhparams(struct tlsv1_credentials *cred, const char *dh_file,
const u8 *dh_blob, size_t dh_blob_len)
{
if (dh_blob)
return tlsv1_set_dhparams_blob(cred, dh_blob, dh_blob_len);
if (dh_file) {
u8 *buf;
size_t len;
int ret;
buf = (u8 *) os_readfile(dh_file, &len);
if (buf == NULL) {
wpa_printf(MSG_INFO, "TLSv1: Failed to read '%s'",
dh_file);
return -1;
}{...}
ret = tlsv1_set_dhparams_blob(cred, buf, len);
os_free(buf);
return ret;
}{...}
return 0;
}{ ... }