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
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
|
/*
SSSD - certificate handling utils - NSS version
The calls defined here should be useable outside of SSSD as well, e.g. in
libsss_certmap.
Copyright (C) Sumit Bose <sbose@redhat.com> 2017
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <nss.h>
#include <cert.h>
#include <base64.h>
#include <prerror.h>
#include <secport.h>
#include <secerr.h>
#include <prprf.h>
#include <prnetdb.h>
#include <talloc.h>
#include "util/crypto/sss_crypto.h"
#include "util/crypto/nss/nss_util.h"
#include "util/cert.h"
#include "lib/certmap/sss_certmap.h"
#include "lib/certmap/sss_certmap_int.h"
/* The following two functions are copied from NSS's lib/certdb/secname.c
* becasue CERT_AddAVA is not exported. I just renamed it and made it static
* to avoid issues if the call gets exported some time in future. */
static void **
AddToArray(PLArenaPool *arena, void **array, void *element)
{
unsigned count;
void **ap;
/* Count up number of slots already in use in the array */
count = 0;
ap = array;
if (ap) {
while (*ap++) {
count++;
}
}
if (array) {
array = (void**) PORT_ArenaGrow(arena, array,
(count + 1) * sizeof(void *),
(count + 2) * sizeof(void *));
} else {
array = (void**) PORT_ArenaAlloc(arena, (count + 2) * sizeof(void *));
}
if (array) {
array[count] = element;
array[count+1] = 0;
}
return array;
}
static SECStatus
sss_CERT_AddAVA(PLArenaPool *arena, CERTRDN *rdn, CERTAVA *ava)
{
rdn->avas = (CERTAVA**) AddToArray(arena, (void**) rdn->avas, ava);
return rdn->avas ? SECSuccess : SECFailure;
}
static SECItem *
cert_get_ext_by_tag(CERTCertificate *cert, SECOidTag tag)
{
SECOidData *oid;
int i;
oid = SECOID_FindOIDByTag(tag);
for (i = 0;
(cert->extensions != NULL) && (cert->extensions[i] != NULL);
i++)
if (SECITEM_ItemsAreEqual(&cert->extensions[i]->id, &oid->oid))
return &cert->extensions[i]->value;
return NULL;
}
static int get_extended_key_usage_oids(TALLOC_CTX *mem_ctx,
CERTCertificate *cert,
const char ***_oids)
{
PLArenaPool *pool;
SECItem *ext;
SECItem **oids = NULL;
const char **oids_list = NULL;
size_t c;
SECStatus rv;
char *tmp_str;
int ret;
pool = PORT_NewArena(sizeof(double));
ext = cert_get_ext_by_tag(cert, SEC_OID_X509_EXT_KEY_USAGE);
if (ext != NULL) {
rv = SEC_ASN1DecodeItem(pool, &oids,
SEC_ASN1_GET(SEC_SequenceOfObjectIDTemplate),
ext);
if (rv != SECSuccess) {
ret = EINVAL;
goto done;
}
}
for (c = 0; (oids != NULL && oids[c] != NULL); c++);
oids_list = talloc_zero_array(mem_ctx, const char *, c + 1);
if (oids_list == NULL) {
return ENOMEM;
}
for (c = 0; (oids != NULL && oids[c] != NULL); c++) {
tmp_str = CERT_GetOidString(oids[c]);
/* is it expexted that NSS OID strings start with "OID." but we
* prefer the plain dotted-decimal version so the prefix is skipped */
if (tmp_str == NULL || strncmp(tmp_str, "OID.", 4) != 0) {
PR_smprintf_free(tmp_str);
ret = EINVAL;
goto done;
}
oids_list[c] = talloc_strdup(oids_list, tmp_str + 4);
PR_smprintf_free(tmp_str);
if(oids_list[c] == NULL) {
ret = ENOMEM;
goto done;
}
}
ret = 0;
done:
PORT_FreeArena(pool, PR_TRUE);
if (ret == 0) {
*_oids = oids_list;
} else {
talloc_free(oids_list);
}
return ret;
}
static int get_rdn_str(TALLOC_CTX *mem_ctx, CERTAVA **avas,
const char **rdn_str)
{
size_t c;
char *tmp_name = NULL;
const char *tmp_str = NULL;
int ret;
SECStatus rv;
CERTRDN rdn = { 0 };
CERTName *name = NULL;
PLArenaPool *arena = NULL;
arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
if (arena == NULL) {
ret = ENOMEM;
goto done;
}
/* Multiple AVAs should be avoided becasue there is no general ordering
* rule and the RDN strings are not reproducible */
for (c = 0; avas[c] != NULL; c++) {
rv = sss_CERT_AddAVA(arena, &rdn, avas[c]);
if (rv != SECSuccess) {
ret = EIO;
goto done;
}
}
name = CERT_CreateName(&rdn, NULL);
if (name == NULL) {
ret = EIO;
goto done;
}
tmp_name = CERT_NameToAscii(name);
CERT_DestroyName(name);
if (tmp_name == NULL) {
ret = EIO;
goto done;
}
tmp_str = talloc_strdup(mem_ctx, tmp_name);
PORT_Free(tmp_name);
if (tmp_str == NULL) {
ret = ENOMEM;
goto done;
}
ret = 0;
done:
if (ret == 0) {
*rdn_str = tmp_str;
} else {
talloc_free(discard_const(tmp_str));
}
PORT_FreeArena(arena, PR_FALSE);
return ret;
}
static int get_rdn_list(TALLOC_CTX *mem_ctx, CERTRDN **rdns,
const char ***rdn_list)
{
int ret;
size_t c;
const char **list = NULL;
for (c = 0; rdns[c] != NULL; c++);
list = talloc_zero_array(mem_ctx, const char *, c + 1);
if (list == NULL) {
ret = ENOMEM;
goto done;
}
for (c = 0; rdns[c] != NULL; c++) {
ret = get_rdn_str(list, rdns[c]->avas,
&(list[c]));
if (ret != 0) {
goto done;
}
}
ret = 0;
done:
if (ret == 0) {
*rdn_list = list;
} else {
talloc_free(list);
}
return ret;
}
enum san_opt nss_name_type_to_san_opt(CERTGeneralNameType type)
{
switch (type) {
case certOtherName:
return SAN_OTHER_NAME;
case certRFC822Name:
return SAN_RFC822_NAME;
case certDNSName:
return SAN_DNS_NAME;
case certX400Address:
return SAN_X400_ADDRESS;
case certDirectoryName:
return SAN_DIRECTORY_NAME;
case certEDIPartyName:
return SAN_EDIPART_NAME;
case certURI:
return SAN_URI;
case certIPAddress:
return SAN_IP_ADDRESS;
case certRegisterID:
return SAN_REGISTERED_ID;
default:
return SAN_INVALID;
}
}
static int add_to_san_list(TALLOC_CTX *mem_ctx, bool is_bin,
enum san_opt san_opt, uint8_t *data, size_t len,
struct san_list **item)
{
struct san_list *i;
if (data == NULL || len == 0 || san_opt == SAN_INVALID) {
return EINVAL;
}
i = talloc_zero(mem_ctx, struct san_list);
if (i == NULL) {
return ENOMEM;
}
i->san_opt = san_opt;
if (is_bin) {
i->bin_val = talloc_memdup(i, data, len);
i->bin_val_len = len;
} else {
i->val = talloc_strndup(i, (char *) data, len);
}
if (i->val == NULL) {
talloc_free(i);
return ENOMEM;
}
*item = i;
return 0;
}
/* taken from pkinit_crypto_nss.c of MIT Kerberos */
/* KerberosString: RFC 4120, 5.2.1. */
static const SEC_ASN1Template kerberos_string_template[] = {
{
SEC_ASN1_GENERAL_STRING,
0,
NULL,
sizeof(SECItem),
}
};
/* Realm: RFC 4120, 5.2.2. */
struct realm {
SECItem name;
};
static const SEC_ASN1Template realm_template[] = {
{
SEC_ASN1_GENERAL_STRING,
0,
NULL,
sizeof(SECItem),
}
};
/* PrincipalName: RFC 4120, 5.2.2. */
static const SEC_ASN1Template sequence_of_kerberos_string_template[] = {
{
SEC_ASN1_SEQUENCE_OF,
0,
&kerberos_string_template,
0,
}
};
struct principal_name {
SECItem name_type;
SECItem **name_string;
};
static const SEC_ASN1Template principal_name_template[] = {
{
SEC_ASN1_SEQUENCE,
0,
NULL,
sizeof(struct principal_name),
},
{
SEC_ASN1_CONTEXT_SPECIFIC | 0 | SEC_ASN1_CONSTRUCTED | SEC_ASN1_EXPLICIT,
offsetof(struct principal_name, name_type),
&SEC_IntegerTemplate,
sizeof(SECItem),
},
{
SEC_ASN1_CONTEXT_SPECIFIC | 1 | SEC_ASN1_CONSTRUCTED | SEC_ASN1_EXPLICIT,
offsetof(struct principal_name, name_string),
sequence_of_kerberos_string_template,
sizeof(struct SECItem **),
},
{0, 0, NULL, 0},
};
/* KRB5PrincipalName: RFC 4556, 3.2.2. */
struct kerberos_principal_name {
SECItem realm;
struct principal_name principal_name;
};
static const SEC_ASN1Template kerberos_principal_name_template[] = {
{
SEC_ASN1_SEQUENCE,
0,
NULL,
sizeof(struct kerberos_principal_name),
},
{
SEC_ASN1_CONTEXT_SPECIFIC | 0 | SEC_ASN1_CONSTRUCTED | SEC_ASN1_EXPLICIT,
offsetof(struct kerberos_principal_name, realm),
&realm_template,
sizeof(struct realm),
},
{
SEC_ASN1_CONTEXT_SPECIFIC | 1 | SEC_ASN1_CONSTRUCTED | SEC_ASN1_EXPLICIT,
offsetof(struct kerberos_principal_name, principal_name),
&principal_name_template,
sizeof(struct principal_name),
},
{0, 0, NULL, 0}
};
#define PKINIT_OID "1.3.6.1.5.2.2"
#define NT_PRINCIPAL_OID "1.3.6.1.4.1.311.20.2.3"
static int add_string_other_name_to_san_list(TALLOC_CTX *mem_ctx,
enum san_opt san_opt,
CERTGeneralName *current,
struct san_list **item)
{
struct san_list *i = NULL;
int ret;
char *tmp_str;
tmp_str = CERT_GetOidString(&(current->name.OthName.oid));
/* is it expexted that NSS OID strings start with "OID." but we
* prefer the plain dotted-decimal version so the prefix is skipped */
if (tmp_str == NULL || strncmp(tmp_str, "OID.", 4) != 0) {
PR_smprintf_free(tmp_str);
return EINVAL;
}
i = talloc_zero(mem_ctx, struct san_list);
if (i == NULL) {
PR_smprintf_free(tmp_str);
return ENOMEM;
}
i->san_opt = san_opt;
i->other_name_oid = talloc_strdup(i, tmp_str + 4);
PR_smprintf_free(tmp_str);
if (i->other_name_oid == NULL) {
ret = ENOMEM;
goto done;
}
i->bin_val = talloc_memdup(i, current->name.OthName.name.data,
current->name.OthName.name.len);
if (i->bin_val == NULL) {
ret = ENOMEM;
goto done;
}
i->bin_val_len = current->name.OthName.name.len;
ret = 0;
done:
if (ret == 0) {
*item = i;
} else {
talloc_free(i);
}
return ret;
}
static int get_short_name(TALLOC_CTX *mem_ctx, const char *full_name,
char delim, char **short_name)
{
char *at;
char *s;
if (full_name == NULL || delim == '\0' || short_name == NULL) {
return EINVAL;
}
at = strchr(full_name, delim);
if (at != NULL) {
s = talloc_strndup(mem_ctx, full_name, (at - full_name));
} else {
s = talloc_strdup(mem_ctx, full_name);
}
if (s == NULL) {
return ENOMEM;
}
*short_name = s;
return 0;
}
static int add_nt_princ_to_san_list(TALLOC_CTX *mem_ctx,
PLArenaPool *pool,
enum san_opt san_opt,
CERTGeneralName *current,
struct san_list **item)
{
struct san_list *i = NULL;
SECStatus rv;
SECItem tmp_secitem = { 0 };
int ret;
rv = SEC_ASN1DecodeItem(pool, &tmp_secitem,
SEC_ASN1_GET(SEC_UTF8StringTemplate),
&(current->name.OthName.name));
if (rv != SECSuccess) {
return EINVAL;
}
i = talloc_zero(mem_ctx, struct san_list);
if (i == NULL) {
return ENOMEM;
}
i->san_opt = san_opt;
i->val = talloc_strndup(i, (char *) tmp_secitem.data,
tmp_secitem.len);
if (i->val == NULL) {
ret = ENOMEM;
goto done;
}
ret = get_short_name(i, i->val, '@', &(i->short_name));
if (ret != 0) {
goto done;
}
ret = 0;
done:
if (ret == 0) {
*item = i;
} else {
talloc_free(i);
}
return ret;
}
static int add_pkinit_princ_to_san_list(TALLOC_CTX *mem_ctx,
PLArenaPool *pool,
enum san_opt san_opt,
CERTGeneralName *current,
struct san_list **item)
{
struct san_list *i = NULL;
SECStatus rv;
struct kerberos_principal_name kname;
int ret;
size_t c;
rv = SEC_ASN1DecodeItem(pool, &kname,
kerberos_principal_name_template,
&(current->name.OthName.name));
if (rv != SECSuccess) {
return EINVAL;
}
i = talloc_zero(mem_ctx, struct san_list);
if (i == NULL) {
return ENOMEM;
}
i->san_opt = san_opt;
if (kname.principal_name.name_string != NULL) {
i->val = talloc_strdup(i, "");
if (i->val == NULL) {
ret = ENOMEM;
goto done;
}
for (c = 0; kname.principal_name.name_string[c] != NULL; c++) {
if (c > 0) {
i->val = talloc_strdup_append(i->val, "/");
if (i->val == NULL) {
ret = ENOMEM;
goto done;
}
}
i->val = talloc_strndup_append(i->val,
(char *) kname.principal_name.name_string[c]->data,
kname.principal_name.name_string[c]->len);
if (i->val == NULL) {
ret = ENOMEM;
goto done;
}
}
i->val = talloc_strndup_append(i->val,
(char *) kname.realm.data,
kname.realm.len);
if (i->val == NULL) {
ret = ENOMEM;
goto done;
}
ret = get_short_name(i, i->val, '@', &(i->short_name));
if (ret != 0) {
goto done;
}
}
ret = 0;
done:
if (ret == 0) {
*item = i;
} else {
talloc_free(i);
}
return ret;
}
static int add_oid_to_san_list(TALLOC_CTX *mem_ctx,
enum san_opt san_opt,
SECItem oid,
struct san_list **item)
{
struct san_list *i = NULL;
char *tmp_str;
tmp_str = CERT_GetOidString(&oid);
/* is it expexted that NSS OID strings start with "OID." but we
* prefer the plain dotted-decimal version so the prefix is skipped */
if (tmp_str == NULL || strncmp(tmp_str, "OID.", 4) != 0) {
PR_smprintf_free(tmp_str);
return EINVAL;
}
i = talloc_zero(mem_ctx, struct san_list);
if (i == NULL) {
PR_smprintf_free(tmp_str);
return ENOMEM;
}
i->san_opt = san_opt;
i->val = talloc_strdup(i, tmp_str + 4);
PR_smprintf_free(tmp_str);
if (i->val == NULL) {
talloc_free(i);
return ENOMEM;
}
*item = i;
return 0;
}
static int add_rdn_list_to_san_list(TALLOC_CTX *mem_ctx,
enum san_opt san_opt,
CERTName name,
struct san_list **item)
{
struct san_list *i = NULL;
int ret;
i = talloc_zero(mem_ctx, struct san_list);
if (i == NULL) {
return ENOMEM;
}
i->san_opt = san_opt;
ret = get_rdn_list(i, name.rdns, &(i->rdn_list));
if (ret != 0) {
talloc_free(i);
return ret;
}
*item = i;
return 0;
}
static int add_ip_to_san_list(TALLOC_CTX *mem_ctx, enum san_opt san_opt,
uint8_t *data, size_t len,
struct san_list **item)
{
struct san_list *i;
PRStatus st;
PRNetAddr addr;
char addrBuf[80];
if (data == NULL || len == 0 || san_opt == SAN_INVALID) {
return EINVAL;
}
/* taken from secu_PrintIPAddress() */
memset(&addr, 0, sizeof addr);
if (len == 4) {
addr.inet.family = PR_AF_INET;
memcpy(&addr.inet.ip, data, len);
} else if (len == 16) {
addr.ipv6.family = PR_AF_INET6;
memcpy(addr.ipv6.ip.pr_s6_addr, data, len);
if (PR_IsNetAddrType(&addr, PR_IpAddrV4Mapped)) {
/* convert to IPv4. */
addr.inet.family = PR_AF_INET;
memcpy(&addr.inet.ip, &addr.ipv6.ip.pr_s6_addr[12], 4);
memset(&addr.inet.pad[0], 0, sizeof addr.inet.pad);
}
} else {
return EINVAL;
}
st = PR_NetAddrToString(&addr, addrBuf, sizeof addrBuf);
if (st != PR_SUCCESS) {
return EIO;
}
i = talloc_zero(mem_ctx, struct san_list);
if (i == NULL) {
return ENOMEM;
}
i->san_opt = san_opt;
i->val = talloc_strdup(i, addrBuf);
if (i->val == NULL) {
talloc_free(i);
return ENOMEM;
}
*item = i;
return 0;
}
static int add_principal_to_san_list(TALLOC_CTX *mem_ctx,
enum san_opt san_opt,
const char *princ,
struct san_list **item)
{
struct san_list *i = NULL;
int ret;
i = talloc_zero(mem_ctx, struct san_list);
if (i == NULL) {
return ENOMEM;
}
i->san_opt = san_opt;
i->val = talloc_strdup(i, princ);
if (i->val == NULL) {
ret = ENOMEM;
goto done;
}
ret = get_short_name(i, i->val, '@', &(i->short_name));
if (ret != 0) {
goto done;
}
ret = 0;
done:
if (ret == 0) {
*item = i;
} else {
talloc_free(i);
}
return ret;
}
static int get_san(TALLOC_CTX *mem_ctx, CERTCertificate *cert,
struct san_list **san_list)
{
SECItem subAltName = { 0 };
SECStatus rv;
CERTGeneralName *name_list = NULL;
CERTGeneralName *current;
PLArenaPool *pool = NULL;
int ret;
struct san_list *list = NULL;
struct san_list *item = NULL;
struct san_list *item_s = NULL;
struct san_list *item_p = NULL;
struct san_list *item_pb = NULL;
rv = CERT_FindCertExtension(cert, SEC_OID_X509_SUBJECT_ALT_NAME,
&subAltName);
if (rv != SECSuccess) {
if (rv == SECFailure
&& PORT_GetError() == SEC_ERROR_EXTENSION_NOT_FOUND) {
ret = EOK;
} else {
ret = EIO;
}
goto done;
}
pool = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
if (pool == NULL) {
ret = ENOMEM;
goto done;
}
name_list = CERT_DecodeAltNameExtension(pool, &subAltName);
if (name_list == NULL ) {
ret = EIO;
goto done;
}
current = name_list;
do {
switch (current->type) {
case certOtherName:
ret = add_string_other_name_to_san_list(mem_ctx,
SAN_STRING_OTHER_NAME,
current, &item_s);
if (ret != 0) {
goto done;
}
DLIST_ADD(list, item_s);
item_p = NULL;
if (strcmp(item_s->other_name_oid, NT_PRINCIPAL_OID) == 0) {
ret = add_nt_princ_to_san_list(mem_ctx, pool, SAN_NT, current,
&item_p);
if (ret != 0) {
goto done;
}
DLIST_ADD(list, item_p);
} else if (strcmp(item_s->other_name_oid, PKINIT_OID) == 0) {
ret = add_pkinit_princ_to_san_list(mem_ctx, pool, SAN_PKINIT,
current, &item_p);
if (ret != 0) {
goto done;
}
DLIST_ADD(list, item_p);
}
if (item_p != NULL) {
ret = add_principal_to_san_list(mem_ctx, SAN_PRINCIPAL,
item_p->val, &item_pb);
if (ret != 0) {
goto done;
}
DLIST_ADD(list, item_pb);
}
break;
case certRFC822Name:
case certDNSName:
case certURI:
ret = add_to_san_list(mem_ctx, false,
nss_name_type_to_san_opt(current->type),
current->name.other.data,
current->name.other.len, &item);
if (ret != 0) {
goto done;
}
if (current->type == certRFC822Name
|| current->type == certDNSName) {
ret = get_short_name(item, item->val,
(current->type == certRFC822Name
? '@' : '.'),
&(item->short_name));
if (ret != 0) {
goto done;
}
}
DLIST_ADD(list, item);
break;
case certIPAddress:
ret = add_ip_to_san_list(mem_ctx,
nss_name_type_to_san_opt(current->type),
current->name.other.data,
current->name.other.len, &item);
if (ret != 0) {
goto done;
}
DLIST_ADD(list, item);
break;
case certDirectoryName:
ret = add_rdn_list_to_san_list(mem_ctx,
nss_name_type_to_san_opt(current->type),
current->name.directoryName, &item);
if (ret != 0) {
goto done;
}
DLIST_ADD(list, item);
break;
case certRegisterID:
ret = add_oid_to_san_list(mem_ctx,
nss_name_type_to_san_opt(current->type),
current->name.other, &item);
if (ret != 0) {
goto done;
}
DLIST_ADD(list, item);
break;
case certX400Address:
case certEDIPartyName:
ret = add_to_san_list(mem_ctx, true,
nss_name_type_to_san_opt(current->type),
current->name.other.data,
current->name.other.len, &item);
if (ret != 0) {
goto done;
}
DLIST_ADD(list, item);
break;
default:
ret = EINVAL;
}
current = CERT_GetNextGeneralName(current);
if (current == NULL) {
ret = EIO;
goto done;
}
} while (current != name_list);
done:
/* Don't free nameList, it's part of the arena. */
if (pool != NULL) {
PORT_FreeArena(pool, PR_FALSE);
}
if (subAltName.data != NULL) {
SECITEM_FreeItem(&subAltName, PR_FALSE);
}
if (ret == EOK) {
*san_list = list;
}
return ret;
}
int sss_cert_get_content(TALLOC_CTX *mem_ctx,
const uint8_t *der_blob, size_t der_size,
struct sss_cert_content **content)
{
int ret;
struct sss_cert_content *cont = NULL;
CERTCertDBHandle *handle;
CERTCertificate *cert = NULL;
SECItem der_item;
NSSInitContext *nss_ctx;
if (der_blob == NULL || der_size == 0) {
return EINVAL;
}
nss_ctx = NSS_InitContext("", "", "", "", NULL, NSS_INIT_READONLY
| NSS_INIT_NOCERTDB
| NSS_INIT_NOMODDB
| NSS_INIT_FORCEOPEN
| NSS_INIT_NOROOTINIT
| NSS_INIT_OPTIMIZESPACE);
if (nss_ctx == NULL) {
return EIO;
}
cont = talloc_zero(mem_ctx, struct sss_cert_content);
if (cont == NULL) {
return ENOMEM;
}
handle = CERT_GetDefaultCertDB();
der_item.len = der_size;
der_item.data = discard_const(der_blob);
cert = CERT_NewTempCertificate(handle, &der_item, NULL, PR_FALSE, PR_TRUE);
if (cert == NULL) {
ret = EINVAL;
goto done;
}
cont->issuer_str = talloc_strdup(cont, cert->issuerName);
if (cont->issuer_str == NULL) {
ret = ENOMEM;
goto done;
}
ret = get_rdn_list(cont, cert->issuer.rdns, &cont->issuer_rdn_list);
if (ret != 0) {
goto done;
}
cont->subject_str = talloc_strdup(cont, cert->subjectName);
if (cont->subject_str == NULL) {
ret = ENOMEM;
goto done;
}
ret = get_rdn_list(cont, cert->subject.rdns, &cont->subject_rdn_list);
if (ret != 0) {
goto done;
}
cont->key_usage = cert->keyUsage;
ret = get_extended_key_usage_oids(cont, cert,
&(cont->extended_key_usage_oids));
if (ret != 0) {
goto done;
}
ret = get_san(cont, cert, &(cont->san_list));
if (ret != 0) {
goto done;
}
cont->cert_der = talloc_memdup(cont, der_blob, der_size);
if (cont->cert_der == NULL) {
ret = ENOMEM;
goto done;
}
cont->cert_der_size = der_size;
ret = EOK;
done:
CERT_DestroyCertificate(cert);
NSS_ShutdownContext(nss_ctx);
if (ret == EOK) {
*content = cont;
} else {
talloc_free(cont);
}
return ret;
}
|