summaryrefslogtreecommitdiffstats
path: root/security/nss/lib/certdb/cert.h
blob: 1981b8f549fd5ac3fd2288f2cf02348721e4a854 (plain)
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
1015
1016
1017
1018
1019
1020
1021
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
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
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
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
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
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/*
 * cert.h - public data structures and prototypes for the certificate library
 */

#ifndef _CERT_H_
#define _CERT_H_

#include "utilrename.h"
#include "plarena.h"
#include "plhash.h"
#include "prlong.h"
#include "prlog.h"

#include "seccomon.h"
#include "secdert.h"
#include "secoidt.h"
#include "keythi.h"
#include "certt.h"

SEC_BEGIN_PROTOS

/****************************************************************************
 *
 * RFC1485 ascii to/from X.? RelativeDistinguishedName (aka CERTName)
 *
 ****************************************************************************/

/*
** Convert an ascii RFC1485 encoded name into its CERTName equivalent.
*/
extern CERTName *CERT_AsciiToName(const char *string);

/*
** Convert an CERTName into its RFC1485 encoded equivalent.
** Returns a string that must be freed with PORT_Free().
** This version produces a string for maximum human readability,
** not for strict RFC compliance.
*/
extern char *CERT_NameToAscii(CERTName *name);

/*
** Convert an CERTName into its RFC1485 encoded equivalent.
** Returns a string that must be freed with PORT_Free().
** Caller chooses encoding rules.
*/
extern char *CERT_NameToAsciiInvertible(CERTName *name,
                                        CertStrictnessLevel strict);

extern CERTAVA *CERT_CopyAVA(PLArenaPool *arena, CERTAVA *src);

/* convert an OID to dotted-decimal representation */
/* Returns a string that must be freed with PR_smprintf_free(). */
extern char *CERT_GetOidString(const SECItem *oid);

/*
** Examine an AVA and return the tag that refers to it. The AVA tags are
** defined as SEC_OID_AVA*.
*/
extern SECOidTag CERT_GetAVATag(CERTAVA *ava);

/*
** Compare two AVA's, returning the difference between them.
*/
extern SECComparison CERT_CompareAVA(const CERTAVA *a, const CERTAVA *b);

/*
** Create an RDN (relative-distinguished-name). The argument list is a
** NULL terminated list of AVA's.
*/
extern CERTRDN *CERT_CreateRDN(PLArenaPool *arena, CERTAVA *avas, ...);

/*
** Make a copy of "src" storing it in "dest".
*/
extern SECStatus CERT_CopyRDN(PLArenaPool *arena, CERTRDN *dest, CERTRDN *src);

/*
** Add an AVA to an RDN.
**	"rdn" the RDN to add to
**	"ava" the AVA to add
*/
extern SECStatus CERT_AddAVA(PLArenaPool *arena, CERTRDN *rdn, CERTAVA *ava);

/*
** Compare two RDN's, returning the difference between them.
*/
extern SECComparison CERT_CompareRDN(const CERTRDN *a, const CERTRDN *b);

/*
** Create an X.500 style name using a NULL terminated list of RDN's.
*/
extern CERTName *CERT_CreateName(CERTRDN *rdn, ...);

/*
** Make a copy of "src" storing it in "dest". Memory is allocated in
** "dest" for each of the appropriate sub objects. Memory is not freed in
** "dest" before allocation is done (use CERT_DestroyName(dest, PR_FALSE) to
** do that).
*/
extern SECStatus CERT_CopyName(PLArenaPool *arena, CERTName *dest,
                               const CERTName *src);

/*
** Destroy a Name object.
**	"name" the CERTName to destroy
**	"freeit" if PR_TRUE then free the object as well as its sub-objects
*/
extern void CERT_DestroyName(CERTName *name);

/*
** Add an RDN to a name.
**	"name" the name to add the RDN to
**	"rdn" the RDN to add to name
*/
extern SECStatus CERT_AddRDN(CERTName *name, CERTRDN *rdn);

/*
** Compare two names, returning the difference between them.
*/
extern SECComparison CERT_CompareName(const CERTName *a, const CERTName *b);

/*
** Convert a CERTName into something readable
*/
extern char *CERT_FormatName(CERTName *name);

/*
** Convert a der-encoded integer to a hex printable string form.
** Perhaps this should be a SEC function but it's only used for certs.
*/
extern char *CERT_Hexify(SECItem *i, int do_colon);

/*
** Converts DER string (with explicit length) into zString, if destination
** buffer is big enough to receive it.  Does quoting and/or escaping as
** specified in RFC 1485.  Input string must be single or multi-byte DER
** character set, (ASCII, UTF8, or ISO 8851-x) not a wide character set.
** Returns SECSuccess or SECFailure with error code set. If output buffer
** is too small, sets error code SEC_ERROR_OUTPUT_LEN.
*/
extern SECStatus CERT_RFC1485_EscapeAndQuote(char *dst, int dstlen, char *src,
                                             int srclen);

/******************************************************************************
 *
 * Certificate handling operations
 *
 *****************************************************************************/

/*
** Create a new validity object given two unix time values.
**	"notBefore" the time before which the validity is not valid
**	"notAfter" the time after which the validity is not valid
*/
extern CERTValidity *CERT_CreateValidity(PRTime notBefore, PRTime notAfter);

/*
** Destroy a validity object.
**	"v" the validity to destroy
**	"freeit" if PR_TRUE then free the object as well as its sub-objects
*/
extern void CERT_DestroyValidity(CERTValidity *v);

/*
** Copy the "src" object to "dest". Memory is allocated in "dest" for
** each of the appropriate sub-objects. Memory in "dest" is not freed
** before memory is allocated (use CERT_DestroyValidity(v, PR_FALSE) to do
** that).
*/
extern SECStatus CERT_CopyValidity(PLArenaPool *arena, CERTValidity *dest,
                                   CERTValidity *src);

/*
** The cert lib considers a cert or CRL valid if the "notBefore" time is
** in the not-too-distant future, e.g. within the next 24 hours. This
** prevents freshly issued certificates from being considered invalid
** because the local system's time zone is incorrectly set.
** The amount of "pending slop time" is adjustable by the application.
** Units of SlopTime are seconds.  Default is 86400  (24 hours).
** Negative SlopTime values are not allowed.
*/
PRInt32 CERT_GetSlopTime(void);

SECStatus CERT_SetSlopTime(PRInt32 slop);

/*
** Create a new certificate object. The result must be wrapped with an
** CERTSignedData to create a signed certificate.
**	"serialNumber" the serial number
**	"issuer" the name of the certificate issuer
**	"validity" the validity period of the certificate
**	"req" the certificate request that prompted the certificate issuance
*/
extern CERTCertificate *CERT_CreateCertificate(unsigned long serialNumber,
                                               CERTName *issuer,
                                               CERTValidity *validity,
                                               CERTCertificateRequest *req);

/*
** Destroy a certificate object
**	"cert" the certificate to destroy
** NOTE: certificate's are reference counted. This call decrements the
** reference count, and if the result is zero, then the object is destroyed
** and optionally freed.
*/
extern void CERT_DestroyCertificate(CERTCertificate *cert);

/*
** Make a shallow copy of a certificate "c". Just increments the
** reference count on "c".
*/
extern CERTCertificate *CERT_DupCertificate(CERTCertificate *c);

/* Access the DER of the certificate. This only creates a reference to the DER
 * in the outparam not a copy.  To avoid the pointer becoming invalid, use
 * CERT_DupCertificate() and keep a reference to the duplicate alive.
 */
extern SECStatus CERT_GetCertificateDer(const CERTCertificate *c, SECItem *der);

/*
** Create a new certificate request. This result must be wrapped with an
** CERTSignedData to create a signed certificate request.
**	"name" the subject name (who the certificate request is from)
**	"spki" describes/defines the public key the certificate is for
**	"attributes" if non-zero, some optional attribute data
*/
extern CERTCertificateRequest *CERT_CreateCertificateRequest(
    CERTName *name, CERTSubjectPublicKeyInfo *spki, SECItem **attributes);

/*
** Destroy a certificate-request object
**	"r" the certificate-request to destroy
**	"freeit" if PR_TRUE then free the object as well as its sub-objects
*/
extern void CERT_DestroyCertificateRequest(CERTCertificateRequest *r);

/*
** Start adding extensions to a certificate request.
*/
void *CERT_StartCertificateRequestAttributes(CERTCertificateRequest *req);

/*
** Reformat the certificate extension list into a CertificateRequest
** attribute list.
*/
SECStatus CERT_FinishCertificateRequestAttributes(CERTCertificateRequest *req);

/*
** Extract the Extension Requests from a DER CertRequest attribute list.
*/
SECStatus CERT_GetCertificateRequestExtensions(CERTCertificateRequest *req,
                                               CERTCertExtension ***exts);

/*
** Extract a public key object from a certificate
*/
extern SECKEYPublicKey *CERT_ExtractPublicKey(CERTCertificate *cert);

/*
** Retrieve the Key Type associated with the cert we're dealing with
*/

extern KeyType CERT_GetCertKeyType(const CERTSubjectPublicKeyInfo *spki);

/*
** Initialize the certificate database.  This is called to create
**  the initial list of certificates in the database.
*/
extern SECStatus CERT_InitCertDB(CERTCertDBHandle *handle);

extern int CERT_GetDBContentVersion(CERTCertDBHandle *handle);

/*
** Default certificate database routines
*/
extern void CERT_SetDefaultCertDB(CERTCertDBHandle *handle);

extern CERTCertDBHandle *CERT_GetDefaultCertDB(void);

extern CERTCertList *CERT_GetCertChainFromCert(CERTCertificate *cert,
                                               PRTime time, SECCertUsage usage);
extern CERTCertificate *CERT_NewTempCertificate(CERTCertDBHandle *handle,
                                                SECItem *derCert,
                                                char *nickname, PRBool isperm,
                                                PRBool copyDER);

/******************************************************************************
 *
 * X.500 Name handling operations
 *
 *****************************************************************************/

/*
** Create an AVA (attribute-value-assertion)
**	"arena" the memory arena to alloc from
**	"kind" is one of SEC_OID_AVA_*
**	"valueType" is one of DER_PRINTABLE_STRING, DER_IA5_STRING, or
**	   DER_T61_STRING
**	"value" is the null terminated string containing the value
*/
extern CERTAVA *CERT_CreateAVA(PLArenaPool *arena, SECOidTag kind,
                               int valueType, char *value);

/*
** Extract the Distinguished Name from a DER encoded certificate
**	"derCert" is the DER encoded certificate
**	"derName" is the SECItem that the name is returned in
*/
extern SECStatus CERT_NameFromDERCert(SECItem *derCert, SECItem *derName);

/*
** Extract the Issuers Distinguished Name from a DER encoded certificate
**	"derCert" is the DER encoded certificate
**	"derName" is the SECItem that the name is returned in
*/
extern SECStatus CERT_IssuerNameFromDERCert(SECItem *derCert, SECItem *derName);

extern SECItem *CERT_EncodeGeneralName(CERTGeneralName *genName, SECItem *dest,
                                       PLArenaPool *arena);

extern CERTGeneralName *CERT_DecodeGeneralName(PLArenaPool *reqArena,
                                               SECItem *encodedName,
                                               CERTGeneralName *genName);

/*
** Generate a database search key for a certificate, based on the
** issuer and serial number.
**	"arena" the memory arena to alloc from
**	"derCert" the DER encoded certificate
**	"key" the returned key
*/
extern SECStatus CERT_KeyFromDERCert(PLArenaPool *reqArena, SECItem *derCert,
                                     SECItem *key);

extern SECStatus CERT_KeyFromIssuerAndSN(PLArenaPool *arena, SECItem *issuer,
                                         SECItem *sn, SECItem *key);

extern SECStatus CERT_SerialNumberFromDERCert(SECItem *derCert,
                                              SECItem *derName);

/*
** Generate a database search key for a crl, based on the
** issuer.
**	"arena" the memory arena to alloc from
**	"derCrl" the DER encoded crl
**	"key" the returned key
*/
extern SECStatus CERT_KeyFromDERCrl(PLArenaPool *arena, SECItem *derCrl,
                                    SECItem *key);

/*
** Open the certificate database.  Use callback to get name of database.
*/
extern SECStatus CERT_OpenCertDB(CERTCertDBHandle *handle, PRBool readOnly,
                                 CERTDBNameFunc namecb, void *cbarg);

/* Open the certificate database.  Use given filename for database. */
extern SECStatus CERT_OpenCertDBFilename(CERTCertDBHandle *handle,
                                         char *certdbname, PRBool readOnly);

/*
** Open and initialize a cert database that is entirely in memory.  This
** can be used when the permanent database can not be opened or created.
*/
extern SECStatus CERT_OpenVolatileCertDB(CERTCertDBHandle *handle);

/*
** Extract the list of host names, host name patters, IP address strings
** this cert is valid for.
** This function does NOT return nicknames.
** Type CERTCertNicknames is being used because it's a convenient
** data structure to carry a list of strings and its count.
*/
extern CERTCertNicknames *CERT_GetValidDNSPatternsFromCert(
    CERTCertificate *cert);

/*
** Check the hostname to make sure that it matches the shexp that
** is given in the common name of the certificate.
*/
extern SECStatus CERT_VerifyCertName(const CERTCertificate *cert,
                                     const char *hostname);

/*
** Add a domain name to the list of names that the user has explicitly
** allowed (despite cert name mismatches) for use with a server cert.
*/
extern SECStatus CERT_AddOKDomainName(CERTCertificate *cert,
                                      const char *hostname);

/*
** Decode a DER encoded certificate into an CERTCertificate structure
**	"derSignedCert" is the DER encoded signed certificate
**	"copyDER" is true if the DER should be copied, false if the
**		existing copy should be referenced
**	"nickname" is the nickname to use in the database.  If it is NULL
**		then a temporary nickname is generated.
*/
extern CERTCertificate *CERT_DecodeDERCertificate(SECItem *derSignedCert,
                                                  PRBool copyDER,
                                                  char *nickname);
/*
** Decode a DER encoded CRL into a CERTSignedCrl structure
**	"derSignedCrl" is the DER encoded signed CRL.
**	"type" must be SEC_CRL_TYPE.
*/
#define SEC_CRL_TYPE 1
#define SEC_KRL_TYPE 0 /* deprecated */

extern CERTSignedCrl *CERT_DecodeDERCrl(PLArenaPool *arena,
                                        SECItem *derSignedCrl, int type);

/*
 * same as CERT_DecodeDERCrl, plus allow options to be passed in
 */

extern CERTSignedCrl *CERT_DecodeDERCrlWithFlags(PLArenaPool *narena,
                                                 SECItem *derSignedCrl,
                                                 int type, PRInt32 options);

/* CRL options to pass */

#define CRL_DECODE_DEFAULT_OPTIONS 0x00000000

/* when CRL_DECODE_DONT_COPY_DER is set, the DER is not copied . The
   application must then keep derSignedCrl until it destroys the
   CRL . Ideally, it should allocate derSignedCrl in an arena
   and pass that arena in as the first argument to
   CERT_DecodeDERCrlWithFlags */

#define CRL_DECODE_DONT_COPY_DER 0x00000001
#define CRL_DECODE_SKIP_ENTRIES 0x00000002
#define CRL_DECODE_KEEP_BAD_CRL 0x00000004
#define CRL_DECODE_ADOPT_HEAP_DER 0x00000008

/* complete the decoding of a partially decoded CRL, ie. decode the
   entries. Note that entries is an optional field in a CRL, so the
   "entries" pointer in CERTCrlStr may still be NULL even after
   function returns SECSuccess */

extern SECStatus CERT_CompleteCRLDecodeEntries(CERTSignedCrl *crl);

/* Validate CRL then import it to the dbase.  If there is already a CRL with the
 * same CA in the dbase, it will be replaced if derCRL is more up to date.
 * If the process successes, a CRL will be returned.  Otherwise, a NULL will
 * be returned. The caller should call PORT_GetError() for the exactly error
 * code.
 */
extern CERTSignedCrl *CERT_ImportCRL(CERTCertDBHandle *handle, SECItem *derCRL,
                                     char *url, int type, void *wincx);

extern void CERT_DestroyCrl(CERTSignedCrl *crl);

/* this is a hint to flush the CRL cache. crlKey is the DER subject of
   the issuer (CA). */
void CERT_CRLCacheRefreshIssuer(CERTCertDBHandle *dbhandle, SECItem *crlKey);

/* add the specified DER CRL object to the CRL cache. Doing so will allow
   certificate verification functions (such as CERT_VerifyCertificate)
   to automatically find and make use of this CRL object.
   Once a CRL is added to the CRL cache, the application must hold on to
   the object's memory, because the cache will reference it directly. The
   application can only free the object after it calls CERT_UncacheCRL to
   remove it from the CRL cache.
*/
SECStatus CERT_CacheCRL(CERTCertDBHandle *dbhandle, SECItem *newcrl);

/* remove a previously added CRL object from the CRL cache. It is OK
   for the application to free the memory after a successful removal
*/
SECStatus CERT_UncacheCRL(CERTCertDBHandle *dbhandle, SECItem *oldcrl);

/*
** Find a certificate in the database
**	"key" is the database key to look for
*/
extern CERTCertificate *CERT_FindCertByKey(CERTCertDBHandle *handle,
                                           SECItem *key);

/*
** Find a certificate in the database by name
**	"name" is the distinguished name to look up
*/
extern CERTCertificate *CERT_FindCertByName(CERTCertDBHandle *handle,
                                            SECItem *name);

/*
** Find a certificate in the database by name
**	"name" is the distinguished name to look up (in ascii)
*/
extern CERTCertificate *CERT_FindCertByNameString(CERTCertDBHandle *handle,
                                                  char *name);

/*
** Find a certificate in the database by name and keyid
**	"name" is the distinguished name to look up
**	"keyID" is the value of the subjectKeyID to match
*/
extern CERTCertificate *CERT_FindCertByKeyID(CERTCertDBHandle *handle,
                                             SECItem *name, SECItem *keyID);

/*
** Generate a certificate key from the issuer and serialnumber, then look it
** up in the database.  Return the cert if found.
**	"issuerAndSN" is the issuer and serial number to look for
*/
extern CERTCertificate *CERT_FindCertByIssuerAndSN(
    CERTCertDBHandle *handle, CERTIssuerAndSN *issuerAndSN);
extern CERTCertificate *CERT_FindCertByIssuerAndSNCX(
    CERTCertDBHandle *handle, CERTIssuerAndSN *issuerAndSN, void *wincx);

/*
** Find a certificate in the database by a subject key ID
**	"subjKeyID" is the subject Key ID to look for
*/
extern CERTCertificate *CERT_FindCertBySubjectKeyID(CERTCertDBHandle *handle,
                                                    SECItem *subjKeyID);

/*
** Encode Certificate SKID (Subject Key ID) extension.
**
*/
extern SECStatus CERT_EncodeSubjectKeyID(PLArenaPool *arena,
                                         const SECItem *srcString,
                                         SECItem *encodedValue);

/*
** Find a certificate in the database by a nickname
**	"nickname" is the ascii string nickname to look for
*/
extern CERTCertificate *CERT_FindCertByNickname(CERTCertDBHandle *handle,
                                                const char *nickname);

/*
** Find a certificate in the database by a DER encoded certificate
**	"derCert" is the DER encoded certificate
*/
extern CERTCertificate *CERT_FindCertByDERCert(CERTCertDBHandle *handle,
                                               SECItem *derCert);

/*
** Find a certificate in the database by a email address
**	"emailAddr" is the email address to look up
*/
CERTCertificate *CERT_FindCertByEmailAddr(CERTCertDBHandle *handle,
                                          char *emailAddr);

/*
** Find a certificate in the database by a email address or nickname
**	"name" is the email address or nickname to look up
*/
CERTCertificate *CERT_FindCertByNicknameOrEmailAddr(CERTCertDBHandle *handle,
                                                    const char *name);
CERTCertificate *CERT_FindCertByNicknameOrEmailAddrCX(CERTCertDBHandle *handle,
                                                      const char *name,
                                                      void *wincx);

/*
** Find a certificate in the database by a email address or nickname
** and require it to have the given usage.
**      "name" is the email address or nickname to look up
*/
CERTCertificate *CERT_FindCertByNicknameOrEmailAddrForUsage(
    CERTCertDBHandle *handle, const char *name, SECCertUsage lookingForUsage);
CERTCertificate *CERT_FindCertByNicknameOrEmailAddrForUsageCX(
    CERTCertDBHandle *handle, const char *name, SECCertUsage lookingForUsage,
    void *wincx);

/*
** Find a certificate in the database by a digest of a subject public key
**	"spkDigest" is the digest to look up
*/
extern CERTCertificate *CERT_FindCertBySPKDigest(CERTCertDBHandle *handle,
                                                 SECItem *spkDigest);

/*
 * Find the issuer of a cert
 */
CERTCertificate *CERT_FindCertIssuer(CERTCertificate *cert, PRTime validTime,
                                     SECCertUsage usage);

/*
** Check the validity times of a certificate vs. time 't', allowing
** some slop for broken clocks and stuff.
**	"cert" is the certificate to be checked
**	"t" is the time to check against
**	"allowOverride" if true then check to see if the invalidity has
**		been overridden by the user.
*/
extern SECCertTimeValidity CERT_CheckCertValidTimes(const CERTCertificate *cert,
                                                    PRTime t,
                                                    PRBool allowOverride);

/*
** WARNING - this function is deprecated, and will either go away or have
**		a new API in the near future.
**
** Check the validity times of a certificate vs. the current time, allowing
** some slop for broken clocks and stuff.
**	"cert" is the certificate to be checked
*/
extern SECStatus CERT_CertTimesValid(CERTCertificate *cert);

/*
** Extract the validity times from a certificate
**	"c" is the certificate
**	"notBefore" is the start of the validity period
**	"notAfter" is the end of the validity period
*/
extern SECStatus CERT_GetCertTimes(const CERTCertificate *c, PRTime *notBefore,
                                   PRTime *notAfter);

/*
** Extract the issuer and serial number from a certificate
*/
extern CERTIssuerAndSN *CERT_GetCertIssuerAndSN(PLArenaPool *,
                                                CERTCertificate *);

/*
** verify the signature of a signed data object with a given certificate
**	"sd" the signed data object to be verified
**	"cert" the certificate to use to check the signature
*/
extern SECStatus CERT_VerifySignedData(CERTSignedData *sd,
                                       CERTCertificate *cert, PRTime t,
                                       void *wincx);
/*
** verify the signature of a signed data object with the given DER publickey
*/
extern SECStatus CERT_VerifySignedDataWithPublicKeyInfo(
    CERTSignedData *sd, CERTSubjectPublicKeyInfo *pubKeyInfo, void *wincx);

/*
** verify the signature of a signed data object with a SECKEYPublicKey.
*/
extern SECStatus CERT_VerifySignedDataWithPublicKey(const CERTSignedData *sd,
                                                    SECKEYPublicKey *pubKey,
                                                    void *wincx);

/*
** NEW FUNCTIONS with new bit-field-FIELD SECCertificateUsage - please use
** verify a certificate by checking validity times against a certain time,
** that we trust the issuer, and that the signature on the certificate is
** valid.
**	"cert" the certificate to verify
**	"checkSig" only check signatures if true
*/
extern SECStatus CERT_VerifyCertificate(CERTCertDBHandle *handle,
                                        CERTCertificate *cert, PRBool checkSig,
                                        SECCertificateUsage requiredUsages,
                                        PRTime t, void *wincx,
                                        CERTVerifyLog *log,
                                        SECCertificateUsage *returnedUsages);

/* same as above, but uses current time */
extern SECStatus CERT_VerifyCertificateNow(CERTCertDBHandle *handle,
                                           CERTCertificate *cert,
                                           PRBool checkSig,
                                           SECCertificateUsage requiredUsages,
                                           void *wincx,
                                           SECCertificateUsage *returnedUsages);

/*
** Verify that a CA cert can certify some (unspecified) leaf cert for a given
** purpose. This is used by UI code to help identify where a chain may be
** broken and why. This takes identical parameters to CERT_VerifyCert
*/
extern SECStatus CERT_VerifyCACertForUsage(CERTCertDBHandle *handle,
                                           CERTCertificate *cert,
                                           PRBool checkSig,
                                           SECCertUsage certUsage, PRTime t,
                                           void *wincx, CERTVerifyLog *log);

/*
** OLD OBSOLETE FUNCTIONS with enum SECCertUsage - DO NOT USE FOR NEW CODE
** verify a certificate by checking validity times against a certain time,
** that we trust the issuer, and that the signature on the certificate is
** valid.
**	"cert" the certificate to verify
**	"checkSig" only check signatures if true
*/
extern SECStatus CERT_VerifyCert(CERTCertDBHandle *handle,
                                 CERTCertificate *cert, PRBool checkSig,
                                 SECCertUsage certUsage, PRTime t, void *wincx,
                                 CERTVerifyLog *log);

/* same as above, but uses current time */
extern SECStatus CERT_VerifyCertNow(CERTCertDBHandle *handle,
                                    CERTCertificate *cert, PRBool checkSig,
                                    SECCertUsage certUsage, void *wincx);

SECStatus CERT_VerifyCertChain(CERTCertDBHandle *handle, CERTCertificate *cert,
                               PRBool checkSig, SECCertUsage certUsage,
                               PRTime t, void *wincx, CERTVerifyLog *log);

/*
** Read a base64 ascii encoded DER certificate and convert it to our
** internal format.
**	"certstr" is a null-terminated string containing the certificate
*/
extern CERTCertificate *CERT_ConvertAndDecodeCertificate(char *certstr);

/*
** Read a certificate in some foreign format, and convert it to our
** internal format.
**	"certbuf" is the buffer containing the certificate
**	"certlen" is the length of the buffer
** NOTE - currently supports netscape base64 ascii encoded raw certs
**  and netscape binary DER typed files.
*/
extern CERTCertificate *CERT_DecodeCertFromPackage(char *certbuf, int certlen);

extern SECStatus CERT_ImportCAChain(SECItem *certs, int numcerts,
                                    SECCertUsage certUsage);

extern SECStatus CERT_ImportCAChainTrusted(SECItem *certs, int numcerts,
                                           SECCertUsage certUsage);

/*
** Read a certificate chain in some foreign format, and pass it to a
** callback function.
**	"certbuf" is the buffer containing the certificate
**	"certlen" is the length of the buffer
**	"f" is the callback function
**	"arg" is the callback argument
*/
typedef SECStatus(PR_CALLBACK *CERTImportCertificateFunc)(void *arg,
                                                          SECItem **certs,
                                                          int numcerts);

extern SECStatus CERT_DecodeCertPackage(char *certbuf, int certlen,
                                        CERTImportCertificateFunc f, void *arg);

/*
** Returns the value of an AVA.  This was a formerly static
** function that has been exposed due to the need to decode
** and convert unicode strings to UTF8.
**
** XXX This function resides in certhtml.c, should it be
** moved elsewhere?
*/
extern SECItem *CERT_DecodeAVAValue(const SECItem *derAVAValue);

/*
** extract various element strings from a distinguished name.
**	"name" the distinguished name
*/

extern char *CERT_GetCertificateEmailAddress(CERTCertificate *cert);

extern char *CERT_GetCertEmailAddress(const CERTName *name);

extern const char *CERT_GetFirstEmailAddress(CERTCertificate *cert);

extern const char *CERT_GetNextEmailAddress(CERTCertificate *cert,
                                            const char *prev);

/* The return value must be freed with PORT_Free. */
extern char *CERT_GetCommonName(const CERTName *name);

extern char *CERT_GetCountryName(const CERTName *name);

extern char *CERT_GetLocalityName(const CERTName *name);

extern char *CERT_GetStateName(const CERTName *name);

extern char *CERT_GetOrgName(const CERTName *name);

extern char *CERT_GetOrgUnitName(const CERTName *name);

extern char *CERT_GetDomainComponentName(const CERTName *name);

extern char *CERT_GetCertUid(const CERTName *name);

/* manipulate the trust parameters of a certificate */

extern SECStatus CERT_GetCertTrust(const CERTCertificate *cert,
                                   CERTCertTrust *trust);

extern SECStatus CERT_ChangeCertTrust(CERTCertDBHandle *handle,
                                      CERTCertificate *cert,
                                      CERTCertTrust *trust);

extern SECStatus CERT_ChangeCertTrustByUsage(CERTCertDBHandle *certdb,
                                             CERTCertificate *cert,
                                             SECCertUsage usage);

/*************************************************************************
 *
 * manipulate the extensions of a certificate
 *
 ************************************************************************/

/*
** Set up a cert for adding X509v3 extensions.  Returns an opaque handle
** used by the next two routines.
**	"cert" is the certificate we are adding extensions to
*/
extern void *CERT_StartCertExtensions(CERTCertificate *cert);

/*
** Add an extension to a certificate.
**	"exthandle" is the handle returned by the previous function
**	"idtag" is the integer tag for the OID that should ID this extension
**	"value" is the value of the extension
**	"critical" is the critical extension flag
**	"copyData" is a flag indicating whether the value data should be
**		copied.
*/
extern SECStatus CERT_AddExtension(void *exthandle, int idtag, SECItem *value,
                                   PRBool critical, PRBool copyData);

extern SECStatus CERT_AddExtensionByOID(void *exthandle, SECItem *oid,
                                        SECItem *value, PRBool critical,
                                        PRBool copyData);

extern SECStatus CERT_EncodeAndAddExtension(void *exthandle, int idtag,
                                            void *value, PRBool critical,
                                            const SEC_ASN1Template *atemplate);

extern SECStatus CERT_EncodeAndAddBitStrExtension(void *exthandle, int idtag,
                                                  SECItem *value,
                                                  PRBool critical);

extern SECStatus CERT_EncodeAltNameExtension(PLArenaPool *arena,
                                             CERTGeneralName *value,
                                             SECItem *encodedValue);

/*
** Finish adding cert extensions.  Does final processing on extension
** data, putting it in the right format, and freeing any temporary
** storage.
**	"exthandle" is the handle used to add extensions to a certificate
*/
extern SECStatus CERT_FinishExtensions(void *exthandle);

/*
** Merge an external list of extensions into a cert's extension list, adding one
** only when its OID matches none of the cert's existing extensions. Call this
** immediately before calling CERT_FinishExtensions().
*/
SECStatus CERT_MergeExtensions(void *exthandle, CERTCertExtension **exts);

/* If the extension is found, return its criticality and value.
** This allocate storage for the returning extension value.
*/
extern SECStatus CERT_GetExtenCriticality(CERTCertExtension **extensions,
                                          int tag, PRBool *isCritical);

extern void CERT_DestroyOidSequence(CERTOidSequence *oidSeq);

/****************************************************************************
 *
 * DER encode and decode extension values
 *
 ****************************************************************************/

/* Encode the value of the basicConstraint extension.
**	arena - where to allocate memory for the encoded value.
**	value - extension value to encode
**	encodedValue - output encoded value
*/
extern SECStatus CERT_EncodeBasicConstraintValue(PLArenaPool *arena,
                                                 CERTBasicConstraints *value,
                                                 SECItem *encodedValue);

/*
** Encode the value of the authorityKeyIdentifier extension.
*/
extern SECStatus CERT_EncodeAuthKeyID(PLArenaPool *arena, CERTAuthKeyID *value,
                                      SECItem *encodedValue);

/*
** Encode the value of the crlDistributionPoints extension.
*/
extern SECStatus CERT_EncodeCRLDistributionPoints(
    PLArenaPool *arena, CERTCrlDistributionPoints *value, SECItem *derValue);

/*
** Decodes a DER encoded basicConstaint extension value into a readable format
**	value - decoded value
**	encodedValue - value to decoded
*/
extern SECStatus CERT_DecodeBasicConstraintValue(CERTBasicConstraints *value,
                                                 const SECItem *encodedValue);

/* Decodes a DER encoded authorityKeyIdentifier extension value into a
** readable format.
**	arena - where to allocate memory for the decoded value
**	encodedValue - value to be decoded
**	Returns a CERTAuthKeyID structure which contains the decoded value
*/
extern CERTAuthKeyID *CERT_DecodeAuthKeyID(PLArenaPool *arena,
                                           const SECItem *encodedValue);

/* Decodes a DER encoded crlDistributionPoints extension value into a
** readable format.
**	arena - where to allocate memory for the decoded value
**	der - value to be decoded
**	Returns a CERTCrlDistributionPoints structure which contains the
**          decoded value
*/
extern CERTCrlDistributionPoints *CERT_DecodeCRLDistributionPoints(
    PLArenaPool *arena, SECItem *der);

/* Extract certain name type from a generalName */
extern void *CERT_GetGeneralNameByType(CERTGeneralName *genNames,
                                       CERTGeneralNameType type,
                                       PRBool derFormat);

extern CERTOidSequence *CERT_DecodeOidSequence(const SECItem *seqItem);

/****************************************************************************
 *
 * Find extension values of a certificate
 *
 ***************************************************************************/

extern SECStatus CERT_FindCertExtension(const CERTCertificate *cert, int tag,
                                        SECItem *value);

extern SECStatus CERT_FindNSCertTypeExtension(CERTCertificate *cert,
                                              SECItem *value);

extern char *CERT_FindNSStringExtension(CERTCertificate *cert, int oidtag);

extern SECStatus CERT_FindCertExtensionByOID(CERTCertificate *cert,
                                             SECItem *oid, SECItem *value);

/* Returns the decoded value of the authKeyID extension.
**   Note that this uses passed in the arena to allocate storage for the result
*/
extern CERTAuthKeyID *CERT_FindAuthKeyIDExten(PLArenaPool *arena,
                                              CERTCertificate *cert);

/* Returns the decoded value of the basicConstraint extension.
 */
extern SECStatus CERT_FindBasicConstraintExten(CERTCertificate *cert,
                                               CERTBasicConstraints *value);

/* Returns the decoded value of the crlDistributionPoints extension.
**  Note that the arena in cert is used to allocate storage for the result
*/
extern CERTCrlDistributionPoints *CERT_FindCRLDistributionPoints(
    CERTCertificate *cert);

/* Returns value of the keyUsage extension.  This uses PR_Alloc to allocate
** buffer for the decoded value. The caller should free up the storage
** allocated in value->data.
*/
extern SECStatus CERT_FindKeyUsageExtension(CERTCertificate *cert,
                                            SECItem *value);

/* Return the decoded value of the subjectKeyID extension. The caller should
** free up the storage allocated in retItem->data.
*/
extern SECStatus CERT_FindSubjectKeyIDExtension(CERTCertificate *cert,
                                                SECItem *retItem);

/*
** If cert is a v3 certificate, and a critical keyUsage extension is included,
** then check the usage against the extension value.  If a non-critical
** keyUsage extension is included, this will return SECSuccess without
** checking, since the extension is an advisory field, not a restriction.
** If cert is not a v3 certificate, this will return SECSuccess.
**	cert - certificate
**	usage - one of the x.509 v3 the Key Usage Extension flags
*/
extern SECStatus CERT_CheckCertUsage(CERTCertificate *cert,
                                     unsigned char usage);

/****************************************************************************
 *
 *  CRL v2 Extensions supported routines
 *
 ****************************************************************************/

extern SECStatus CERT_FindCRLExtensionByOID(CERTCrl *crl, SECItem *oid,
                                            SECItem *value);

extern SECStatus CERT_FindCRLExtension(CERTCrl *crl, int tag, SECItem *value);

extern SECStatus CERT_FindInvalidDateExten(CERTCrl *crl, PRTime *value);

/*
** Set up a crl for adding X509v3 extensions.  Returns an opaque handle
** used by routines that take an exthandle (void*) argument .
**	"crl" is the CRL we are adding extensions to
*/
extern void *CERT_StartCRLExtensions(CERTCrl *crl);

/*
** Set up a crl entry for adding X509v3 extensions.  Returns an opaque handle
** used by routines that take an exthandle (void*) argument .
**	"crl" is the crl we are adding certs entries to
**      "entry" is the crl entry we are adding extensions to
*/
extern void *CERT_StartCRLEntryExtensions(CERTCrl *crl, CERTCrlEntry *entry);

extern CERTCertNicknames *CERT_GetCertNicknames(CERTCertDBHandle *handle,
                                                int what, void *wincx);

/*
** Finds the crlNumber extension and decodes its value into 'value'
*/
extern SECStatus CERT_FindCRLNumberExten(PLArenaPool *arena, CERTCrl *crl,
                                         SECItem *value);

extern SECStatus CERT_FindCRLEntryReasonExten(CERTCrlEntry *crlEntry,
                                              CERTCRLEntryReasonCode *value);

extern void CERT_FreeNicknames(CERTCertNicknames *nicknames);

extern PRBool CERT_CompareCerts(const CERTCertificate *c1,
                                const CERTCertificate *c2);

extern PRBool CERT_CompareCertsForRedirection(CERTCertificate *c1,
                                              CERTCertificate *c2);

/*
** Generate an array of the Distinguished Names that the given cert database
** "trusts"
*/
extern CERTDistNames *CERT_GetSSLCACerts(CERTCertDBHandle *handle);

extern void CERT_FreeDistNames(CERTDistNames *names);

/* Duplicate distinguished name array */
extern CERTDistNames *CERT_DupDistNames(CERTDistNames *orig);

/*
** Generate an array of Distinguished names from an array of nicknames
*/
extern CERTDistNames *CERT_DistNamesFromNicknames(CERTCertDBHandle *handle,
                                                  char **nicknames, int nnames);

/*
** Generate an array of Distinguished names from a list of certs.
*/
extern CERTDistNames *CERT_DistNamesFromCertList(CERTCertList *list);

/*
** Generate a certificate chain from a certificate.
*/
extern CERTCertificateList *CERT_CertChainFromCert(CERTCertificate *cert,
                                                   SECCertUsage usage,
                                                   PRBool includeRoot);

extern CERTCertificateList *CERT_CertListFromCert(CERTCertificate *cert);

extern CERTCertificateList *CERT_DupCertList(
    const CERTCertificateList *oldList);

extern void CERT_DestroyCertificateList(CERTCertificateList *list);

/*
** is cert a user cert? i.e. does it have CERTDB_USER trust,
** i.e. a private key?
*/
PRBool CERT_IsUserCert(CERTCertificate *cert);

/* is cert a newer than cert b? */
PRBool CERT_IsNewer(CERTCertificate *certa, CERTCertificate *certb);

/* currently a stub for address book */
PRBool CERT_IsCertRevoked(CERTCertificate *cert);

void CERT_DestroyCertArray(CERTCertificate **certs, unsigned int ncerts);

/* convert an email address to lower case */
char *CERT_FixupEmailAddr(const char *emailAddr);

/* decode string representation of trust flags into trust struct */
SECStatus CERT_DecodeTrustString(CERTCertTrust *trust, const char *trusts);

/* encode trust struct into string representation of trust flags */
char *CERT_EncodeTrustString(CERTCertTrust *trust);

/* find the next or prev cert in a subject list */
CERTCertificate *CERT_PrevSubjectCert(CERTCertificate *cert);
CERTCertificate *CERT_NextSubjectCert(CERTCertificate *cert);

/*
 * import a collection of certs into the temporary or permanent cert
 * database
 */
SECStatus CERT_ImportCerts(CERTCertDBHandle *certdb, SECCertUsage usage,
                           unsigned int ncerts, SECItem **derCerts,
                           CERTCertificate ***retCerts, PRBool keepCerts,
                           PRBool caOnly, char *nickname);

char *CERT_MakeCANickname(CERTCertificate *cert);

PRBool CERT_IsCACert(CERTCertificate *cert, unsigned int *rettype);

PRBool CERT_IsCADERCert(SECItem *derCert, unsigned int *rettype);

PRBool CERT_IsRootDERCert(SECItem *derCert);

SECStatus CERT_SaveSMimeProfile(CERTCertificate *cert, SECItem *emailProfile,
                                SECItem *profileTime);

/*
 * find the smime symmetric capabilities profile for a given cert
 */
SECItem *CERT_FindSMimeProfile(CERTCertificate *cert);

SECStatus CERT_AddNewCerts(CERTCertDBHandle *handle);

CERTCertificatePolicies *CERT_DecodeCertificatePoliciesExtension(
    const SECItem *extnValue);

void CERT_DestroyCertificatePoliciesExtension(
    CERTCertificatePolicies *policies);

CERTCertificatePolicyMappings *CERT_DecodePolicyMappingsExtension(
    SECItem *encodedCertPolicyMaps);

SECStatus CERT_DestroyPolicyMappingsExtension(
    CERTCertificatePolicyMappings *mappings);

SECStatus CERT_DecodePolicyConstraintsExtension(
    CERTCertificatePolicyConstraints *decodedValue,
    const SECItem *encodedValue);

SECStatus CERT_DecodeInhibitAnyExtension(
    CERTCertificateInhibitAny *decodedValue, SECItem *extnValue);

CERTUserNotice *CERT_DecodeUserNotice(SECItem *noticeItem);

extern CERTGeneralName *CERT_DecodeAltNameExtension(PLArenaPool *reqArena,
                                                    SECItem *EncodedAltName);

extern CERTNameConstraints *CERT_DecodeNameConstraintsExtension(
    PLArenaPool *arena, const SECItem *encodedConstraints);

/* returns addr of a NULL termainated array of pointers to CERTAuthInfoAccess */
extern CERTAuthInfoAccess **CERT_DecodeAuthInfoAccessExtension(
    PLArenaPool *reqArena, const SECItem *encodedExtension);

extern CERTPrivKeyUsagePeriod *CERT_DecodePrivKeyUsagePeriodExtension(
    PLArenaPool *arena, SECItem *extnValue);

extern CERTGeneralName *CERT_GetNextGeneralName(CERTGeneralName *current);

extern CERTGeneralName *CERT_GetPrevGeneralName(CERTGeneralName *current);

/*
 * Look up name constraints for some certs that do not include name constraints
 * (Most importantly, root certificates)
 *
 * If a matching subject is found, |extensions| will be populated with a copy of
 * the
 * DER-encoded name constraints extension. The data in |extensions| will point
 * to
 * memory that the caller owns.
 *
 * There is no mechanism to configure imposed name constraints right now.  All
 * imposed name constraints are built into NSS.
 */
SECStatus CERT_GetImposedNameConstraints(const SECItem *derSubject,
                                         SECItem *extensions);

CERTNameConstraint *CERT_GetNextNameConstraint(CERTNameConstraint *current);

CERTNameConstraint *CERT_GetPrevNameConstraint(CERTNameConstraint *current);

void CERT_DestroyUserNotice(CERTUserNotice *userNotice);

typedef char *(*CERTPolicyStringCallback)(char *org, unsigned long noticeNumber,
                                          void *arg);
void CERT_SetCAPolicyStringCallback(CERTPolicyStringCallback cb, void *cbarg);

char *CERT_GetCertCommentString(CERTCertificate *cert);

PRBool CERT_GovtApprovedBitSet(CERTCertificate *cert);

SECStatus CERT_AddPermNickname(CERTCertificate *cert, char *nickname);

CERTCertList *CERT_MatchUserCert(CERTCertDBHandle *handle, SECCertUsage usage,
                                 int nCANames, char **caNames, void *proto_win);

CERTCertList *CERT_NewCertList(void);

/* free the cert list and all the certs in the list */
void CERT_DestroyCertList(CERTCertList *certs);

/* remove the node and free the cert */
void CERT_RemoveCertListNode(CERTCertListNode *node);

/* equivalent to CERT_AddCertToListTailWithData(certs, cert, NULL) */
SECStatus CERT_AddCertToListTail(CERTCertList *certs, CERTCertificate *cert);

/* equivalent to CERT_AddCertToListHeadWithData(certs, cert, NULL) */
SECStatus CERT_AddCertToListHead(CERTCertList *certs, CERTCertificate *cert);

/*
 * The new cert list node takes ownership of "cert". "cert" is freed
 * when the list node is removed.
 */
SECStatus CERT_AddCertToListTailWithData(CERTCertList *certs,
                                         CERTCertificate *cert, void *appData);

/*
 * The new cert list node takes ownership of "cert". "cert" is freed
 * when the list node is removed.
 */
SECStatus CERT_AddCertToListHeadWithData(CERTCertList *certs,
                                         CERTCertificate *cert, void *appData);

typedef PRBool (*CERTSortCallback)(CERTCertificate *certa,
                                   CERTCertificate *certb, void *arg);
SECStatus CERT_AddCertToListSorted(CERTCertList *certs, CERTCertificate *cert,
                                   CERTSortCallback f, void *arg);

/* callback for CERT_AddCertToListSorted that sorts based on validity
 * period and a given time.
 */
PRBool CERT_SortCBValidity(CERTCertificate *certa, CERTCertificate *certb,
                           void *arg);

SECStatus CERT_CheckForEvilCert(CERTCertificate *cert);

CERTGeneralName *CERT_GetCertificateNames(CERTCertificate *cert,
                                          PLArenaPool *arena);

CERTGeneralName *CERT_GetConstrainedCertificateNames(
    const CERTCertificate *cert, PLArenaPool *arena,
    PRBool includeSubjectCommonName);

/*
 * Creates or adds to a list of all certs with a give subject name, sorted by
 * validity time, newest first.  Invalid certs are considered older than
 * valid certs. If validOnly is set, do not include invalid certs on list.
 */
CERTCertList *CERT_CreateSubjectCertList(CERTCertList *certList,
                                         CERTCertDBHandle *handle,
                                         const SECItem *name, PRTime sorttime,
                                         PRBool validOnly);

/*
 * remove certs from a list that don't have keyUsage and certType
 * that match the given usage.
 */
SECStatus CERT_FilterCertListByUsage(CERTCertList *certList, SECCertUsage usage,
                                     PRBool ca);

/*
 * check the key usage of a cert against a set of required values
 */
SECStatus CERT_CheckKeyUsage(CERTCertificate *cert, unsigned int requiredUsage);

/*
 * return required key usage and cert type based on cert usage
 */
SECStatus CERT_KeyUsageAndTypeForCertUsage(SECCertUsage usage, PRBool ca,
                                           unsigned int *retKeyUsage,
                                           unsigned int *retCertType);
/*
 * return required trust flags for various cert usages for CAs
 */
SECStatus CERT_TrustFlagsForCACertUsage(SECCertUsage usage,
                                        unsigned int *retFlags,
                                        SECTrustType *retTrustType);

/*
 * Find all user certificates that match the given criteria.
 *
 *	"handle" - database to search
 *	"usage" - certificate usage to match
 *	"oneCertPerName" - if set then only return the "best" cert per
 *			name
 *	"validOnly" - only return certs that are curently valid
 *	"proto_win" - window handle passed to pkcs11
 */
CERTCertList *CERT_FindUserCertsByUsage(CERTCertDBHandle *handle,
                                        SECCertUsage usage,
                                        PRBool oneCertPerName, PRBool validOnly,
                                        void *proto_win);

/*
 * Find a user certificate that matchs the given criteria.
 *
 *	"handle" - database to search
 *	"nickname" - nickname to match
 *	"usage" - certificate usage to match
 *	"validOnly" - only return certs that are curently valid
 *	"proto_win" - window handle passed to pkcs11
 */
CERTCertificate *CERT_FindUserCertByUsage(CERTCertDBHandle *handle,
                                          const char *nickname,
                                          SECCertUsage usage, PRBool validOnly,
                                          void *proto_win);

/*
 * Filter a list of certificates, removing those certs that do not have
 * one of the named CA certs somewhere in their cert chain.
 *
 *	"certList" - the list of certificates to filter
 *	"nCANames" - number of CA names
 *	"caNames" - array of CA names in string(rfc 1485) form
 *	"usage" - what use the certs are for, this is used when
 *		selecting CA certs
 */
SECStatus CERT_FilterCertListByCANames(CERTCertList *certList, int nCANames,
                                       char **caNames, SECCertUsage usage);

/*
 * Filter a list of certificates, removing those certs that aren't user certs
 */
SECStatus CERT_FilterCertListForUserCerts(CERTCertList *certList);

/*
 * Collect the nicknames from all certs in a CertList.  If the cert is not
 * valid, append a string to that nickname.
 *
 * "certList" - the list of certificates
 * "expiredString" - the string to append to the nickname of any expired cert
 * "notYetGoodString" - the string to append to the nickname of any cert
 *		that is not yet valid
 */
CERTCertNicknames *CERT_NicknameStringsFromCertList(CERTCertList *certList,
                                                    char *expiredString,
                                                    char *notYetGoodString);

/*
 * Extract the nickname from a nickmake string that may have either
 * expiredString or notYetGoodString appended.
 *
 * Args:
 *	"namestring" - the string containing the nickname, and possibly
 *		one of the validity label strings
 *	"expiredString" - the expired validity label string
 *	"notYetGoodString" - the not yet good validity label string
 *
 * Returns the raw nickname
 */
char *CERT_ExtractNicknameString(char *namestring, char *expiredString,
                                 char *notYetGoodString);

/*
 * Given a certificate, return a string containing the nickname, and possibly
 * one of the validity strings, based on the current validity state of the
 * certificate.
 *
 * "arena" - arena to allocate returned string from.  If NULL, then heap
 *	is used.
 * "cert" - the cert to get nickname from
 * "expiredString" - the string to append to the nickname if the cert is
 *		expired.
 * "notYetGoodString" - the string to append to the nickname if the cert is
 *		not yet good.
 */
char *CERT_GetCertNicknameWithValidity(PLArenaPool *arena,
                                       CERTCertificate *cert,
                                       char *expiredString,
                                       char *notYetGoodString);

/*
 * Return the string representation of a DER encoded distinguished name
 * "dername" - The DER encoded name to convert
 */
char *CERT_DerNameToAscii(SECItem *dername);

/*
 * Supported usage values and types:
 *	certUsageSSLClient
 *	certUsageSSLServer
 *	certUsageSSLServerWithStepUp
 *	certUsageEmailSigner
 *	certUsageEmailRecipient
 *	certUsageObjectSigner
 */

CERTCertificate *CERT_FindMatchingCert(CERTCertDBHandle *handle,
                                       SECItem *derName, CERTCertOwner owner,
                                       SECCertUsage usage, PRBool preferTrusted,
                                       PRTime validTime, PRBool validOnly);

/*
 * Acquire the global lock on the cert database.
 * This lock is currently used for the following operations:
 *	adding or deleting a cert to either the temp or perm databases
 *	converting a temp to perm or perm to temp
 *	changing(maybe just adding?) the trust of a cert
 *	adjusting the reference count of a cert
 */
void CERT_LockDB(CERTCertDBHandle *handle);

/*
 * Free the global cert database lock.
 */
void CERT_UnlockDB(CERTCertDBHandle *handle);

/*
 * Get the certificate status checking configuratino data for
 * the certificate database
 */
CERTStatusConfig *CERT_GetStatusConfig(CERTCertDBHandle *handle);

/*
 * Set the certificate status checking information for the
 * database.  The input structure becomes part of the certificate
 * database and will be freed by calling the 'Destroy' function in
 * the configuration object.
 */
void CERT_SetStatusConfig(CERTCertDBHandle *handle, CERTStatusConfig *config);

/*
 * Acquire the cert reference count lock
 * There is currently one global lock for all certs, but I'm putting a cert
 * arg here so that it will be easy to make it per-cert in the future if
 * that turns out to be necessary.
 */
void CERT_LockCertRefCount(CERTCertificate *cert);

/*
 * Release the cert reference count lock
 */
void CERT_UnlockCertRefCount(CERTCertificate *cert);

/*
 * Digest the cert's subject public key using the specified algorithm.
 * NOTE: this digests the value of the BIT STRING subjectPublicKey (excluding
 * the tag, length, and number of unused bits) rather than the whole
 * subjectPublicKeyInfo field.
 *
 * The necessary storage for the digest data is allocated.  If "fill" is
 * non-null, the data is put there, otherwise a SECItem is allocated.
 * Allocation from "arena" if it is non-null, heap otherwise.  Any problem
 * results in a NULL being returned (and an appropriate error set).
 */
extern SECItem *CERT_GetSubjectPublicKeyDigest(PLArenaPool *arena,
                                               const CERTCertificate *cert,
                                               SECOidTag digestAlg,
                                               SECItem *fill);

/*
 * Digest the cert's subject name using the specified algorithm.
 */
extern SECItem *CERT_GetSubjectNameDigest(PLArenaPool *arena,
                                          const CERTCertificate *cert,
                                          SECOidTag digestAlg, SECItem *fill);

SECStatus CERT_CheckCRL(CERTCertificate *cert, CERTCertificate *issuer,
                        const SECItem *dp, PRTime t, void *wincx);

/*
 * Add a CERTNameConstraint to the CERTNameConstraint list
 */
extern CERTNameConstraint *CERT_AddNameConstraint(
    CERTNameConstraint *list, CERTNameConstraint *constraint);

/*
 * Allocate space and copy CERTNameConstraint from src to dest.
 * Arena is used to allocate result(if dest eq NULL) and its members
 * SECItem data.
 */
extern CERTNameConstraint *CERT_CopyNameConstraint(PLArenaPool *arena,
                                                   CERTNameConstraint *dest,
                                                   CERTNameConstraint *src);

/*
 * Verify name against all the constraints relevant to that type of
 * the name.
 */
extern SECStatus CERT_CheckNameSpace(PLArenaPool *arena,
                                     const CERTNameConstraints *constraints,
                                     const CERTGeneralName *currentName);

/*
 * Extract and allocate the name constraints extension from the CA cert.
 * If the certificate contains no name constraints extension, but
 * CERT_GetImposedNameConstraints returns a name constraints extension
 * for the subject of the certificate, then that extension will be returned.
 */
extern SECStatus CERT_FindNameConstraintsExten(
    PLArenaPool *arena, CERTCertificate *cert,
    CERTNameConstraints **constraints);

/*
 * Initialize a new GERTGeneralName fields (link)
 */
extern CERTGeneralName *CERT_NewGeneralName(PLArenaPool *arena,
                                            CERTGeneralNameType type);

/*
 * Lookup a CERTGeneralNameType constant by its human readable string.
 */
extern CERTGeneralNameType CERT_GetGeneralNameTypeFromString(
    const char *string);

/*
 * PKIX extension encoding routines
 */
extern SECStatus CERT_EncodePolicyConstraintsExtension(
    PLArenaPool *arena, CERTCertificatePolicyConstraints *constr,
    SECItem *dest);
extern SECStatus CERT_EncodeInhibitAnyExtension(
    PLArenaPool *arena, CERTCertificateInhibitAny *inhibitAny, SECItem *dest);
extern SECStatus CERT_EncodePolicyMappingExtension(
    PLArenaPool *arena, CERTCertificatePolicyMappings *maps, SECItem *dest);

extern SECStatus CERT_EncodeInfoAccessExtension(PLArenaPool *arena,
                                                CERTAuthInfoAccess **info,
                                                SECItem *dest);
extern SECStatus CERT_EncodeUserNotice(PLArenaPool *arena,
                                       CERTUserNotice *notice, SECItem *dest);

extern SECStatus CERT_EncodeDisplayText(PLArenaPool *arena, SECItem *text,
                                        SECItem *dest);

extern SECStatus CERT_EncodeCertPoliciesExtension(PLArenaPool *arena,
                                                  CERTPolicyInfo **info,
                                                  SECItem *dest);
extern SECStatus CERT_EncodeNoticeReference(PLArenaPool *arena,
                                            CERTNoticeReference *reference,
                                            SECItem *dest);

/*
 * Returns a pointer to a static structure.
 */
extern const CERTRevocationFlags *CERT_GetPKIXVerifyNistRevocationPolicy(void);

/*
 * Returns a pointer to a static structure.
 */
extern const CERTRevocationFlags *CERT_GetClassicOCSPEnabledSoftFailurePolicy(
    void);

/*
 * Returns a pointer to a static structure.
 */
extern const CERTRevocationFlags *CERT_GetClassicOCSPEnabledHardFailurePolicy(
    void);

/*
 * Returns a pointer to a static structure.
 */
extern const CERTRevocationFlags *CERT_GetClassicOCSPDisabledPolicy(void);

/*
 * Verify a Cert with libpkix
 *  paramsIn control the verification options. If a value isn't specified
 *   in paramsIn, it reverts to the application default.
 *  paramsOut specifies the parameters the caller would like to get back.
 *   the caller may pass NULL, in which case no parameters are returned.
 */
extern SECStatus CERT_PKIXVerifyCert(CERTCertificate *cert,
                                     SECCertificateUsage usages,
                                     CERTValInParam *paramsIn,
                                     CERTValOutParam *paramsOut, void *wincx);

/* Makes old cert validation APIs(CERT_VerifyCert, CERT_VerifyCertificate)
 * to use libpkix validation engine. The function should be called ones at
 * application initialization time.
 * Function is not thread safe.*/
extern SECStatus CERT_SetUsePKIXForValidation(PRBool enable);

/* The function return PR_TRUE if cert validation should use
 * libpkix cert validation engine. */
extern PRBool CERT_GetUsePKIXForValidation(void);

/*
 * Allocate a parameter container of type CERTRevocationFlags,
 * and allocate the inner arrays of the given sizes.
 * To cleanup call CERT_DestroyCERTRevocationFlags.
 */
extern CERTRevocationFlags *CERT_AllocCERTRevocationFlags(
    PRUint32 number_leaf_methods, PRUint32 number_leaf_pref_methods,
    PRUint32 number_chain_methods, PRUint32 number_chain_pref_methods);

/*
 * Destroy the arrays inside flags,
 * and destroy the object pointed to by flags, too.
 */
extern void CERT_DestroyCERTRevocationFlags(CERTRevocationFlags *flags);

/*
 * Get istemp and isperm fields from a cert in a thread safe way.
 */
extern SECStatus CERT_GetCertIsTemp(const CERTCertificate *cert, PRBool *istemp);
extern SECStatus CERT_GetCertIsPerm(const CERTCertificate *cert, PRBool *isperm);

SEC_END_PROTOS

#endif /* _CERT_H_ */