cert.h 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. /*
  5. * cert.h - public data structures and prototypes for the certificate library
  6. */
  7. #ifndef _CERT_H_
  8. #define _CERT_H_
  9. #include "utilrename.h"
  10. #include "plarena.h"
  11. #include "plhash.h"
  12. #include "prlong.h"
  13. #include "prlog.h"
  14. #include "seccomon.h"
  15. #include "secdert.h"
  16. #include "secoidt.h"
  17. #include "keythi.h"
  18. #include "certt.h"
  19. SEC_BEGIN_PROTOS
  20. /****************************************************************************
  21. *
  22. * RFC1485 ascii to/from X.? RelativeDistinguishedName (aka CERTName)
  23. *
  24. ****************************************************************************/
  25. /*
  26. ** Convert an ascii RFC1485 encoded name into its CERTName equivalent.
  27. */
  28. extern CERTName *CERT_AsciiToName(const char *string);
  29. /*
  30. ** Convert an CERTName into its RFC1485 encoded equivalent.
  31. ** Returns a string that must be freed with PORT_Free().
  32. ** This version produces a string for maximum human readability,
  33. ** not for strict RFC compliance.
  34. */
  35. extern char *CERT_NameToAscii(CERTName *name);
  36. /*
  37. ** Convert an CERTName into its RFC1485 encoded equivalent.
  38. ** Returns a string that must be freed with PORT_Free().
  39. ** Caller chooses encoding rules.
  40. */
  41. extern char *CERT_NameToAsciiInvertible(CERTName *name,
  42. CertStrictnessLevel strict);
  43. extern CERTAVA *CERT_CopyAVA(PLArenaPool *arena, CERTAVA *src);
  44. /* convert an OID to dotted-decimal representation */
  45. /* Returns a string that must be freed with PR_smprintf_free(). */
  46. extern char *CERT_GetOidString(const SECItem *oid);
  47. /*
  48. ** Examine an AVA and return the tag that refers to it. The AVA tags are
  49. ** defined as SEC_OID_AVA*.
  50. */
  51. extern SECOidTag CERT_GetAVATag(CERTAVA *ava);
  52. /*
  53. ** Compare two AVA's, returning the difference between them.
  54. */
  55. extern SECComparison CERT_CompareAVA(const CERTAVA *a, const CERTAVA *b);
  56. /*
  57. ** Create an RDN (relative-distinguished-name). The argument list is a
  58. ** NULL terminated list of AVA's.
  59. */
  60. extern CERTRDN *CERT_CreateRDN(PLArenaPool *arena, CERTAVA *avas, ...);
  61. /*
  62. ** Make a copy of "src" storing it in "dest".
  63. */
  64. extern SECStatus CERT_CopyRDN(PLArenaPool *arena, CERTRDN *dest, CERTRDN *src);
  65. /*
  66. ** Add an AVA to an RDN.
  67. ** "rdn" the RDN to add to
  68. ** "ava" the AVA to add
  69. */
  70. extern SECStatus CERT_AddAVA(PLArenaPool *arena, CERTRDN *rdn, CERTAVA *ava);
  71. /*
  72. ** Compare two RDN's, returning the difference between them.
  73. */
  74. extern SECComparison CERT_CompareRDN(const CERTRDN *a, const CERTRDN *b);
  75. /*
  76. ** Create an X.500 style name using a NULL terminated list of RDN's.
  77. */
  78. extern CERTName *CERT_CreateName(CERTRDN *rdn, ...);
  79. /*
  80. ** Make a copy of "src" storing it in "dest". Memory is allocated in
  81. ** "dest" for each of the appropriate sub objects. Memory is not freed in
  82. ** "dest" before allocation is done (use CERT_DestroyName(dest, PR_FALSE) to
  83. ** do that).
  84. */
  85. extern SECStatus CERT_CopyName(PLArenaPool *arena, CERTName *dest,
  86. const CERTName *src);
  87. /*
  88. ** Destroy a Name object.
  89. ** "name" the CERTName to destroy
  90. ** "freeit" if PR_TRUE then free the object as well as its sub-objects
  91. */
  92. extern void CERT_DestroyName(CERTName *name);
  93. /*
  94. ** Add an RDN to a name.
  95. ** "name" the name to add the RDN to
  96. ** "rdn" the RDN to add to name
  97. */
  98. extern SECStatus CERT_AddRDN(CERTName *name, CERTRDN *rdn);
  99. /*
  100. ** Compare two names, returning the difference between them.
  101. */
  102. extern SECComparison CERT_CompareName(const CERTName *a, const CERTName *b);
  103. /*
  104. ** Convert a CERTName into something readable
  105. */
  106. extern char *CERT_FormatName(CERTName *name);
  107. /*
  108. ** Convert a der-encoded integer to a hex printable string form.
  109. ** Perhaps this should be a SEC function but it's only used for certs.
  110. */
  111. extern char *CERT_Hexify(SECItem *i, int do_colon);
  112. /*
  113. ** Converts DER string (with explicit length) into zString, if destination
  114. ** buffer is big enough to receive it. Does quoting and/or escaping as
  115. ** specified in RFC 1485. Input string must be single or multi-byte DER
  116. ** character set, (ASCII, UTF8, or ISO 8851-x) not a wide character set.
  117. ** Returns SECSuccess or SECFailure with error code set. If output buffer
  118. ** is too small, sets error code SEC_ERROR_OUTPUT_LEN.
  119. */
  120. extern SECStatus CERT_RFC1485_EscapeAndQuote(char *dst, int dstlen, char *src,
  121. int srclen);
  122. /******************************************************************************
  123. *
  124. * Certificate handling operations
  125. *
  126. *****************************************************************************/
  127. /*
  128. ** Create a new validity object given two unix time values.
  129. ** "notBefore" the time before which the validity is not valid
  130. ** "notAfter" the time after which the validity is not valid
  131. */
  132. extern CERTValidity *CERT_CreateValidity(PRTime notBefore, PRTime notAfter);
  133. /*
  134. ** Destroy a validity object.
  135. ** "v" the validity to destroy
  136. ** "freeit" if PR_TRUE then free the object as well as its sub-objects
  137. */
  138. extern void CERT_DestroyValidity(CERTValidity *v);
  139. /*
  140. ** Copy the "src" object to "dest". Memory is allocated in "dest" for
  141. ** each of the appropriate sub-objects. Memory in "dest" is not freed
  142. ** before memory is allocated (use CERT_DestroyValidity(v, PR_FALSE) to do
  143. ** that).
  144. */
  145. extern SECStatus CERT_CopyValidity(PLArenaPool *arena, CERTValidity *dest,
  146. CERTValidity *src);
  147. /*
  148. ** The cert lib considers a cert or CRL valid if the "notBefore" time is
  149. ** in the not-too-distant future, e.g. within the next 24 hours. This
  150. ** prevents freshly issued certificates from being considered invalid
  151. ** because the local system's time zone is incorrectly set.
  152. ** The amount of "pending slop time" is adjustable by the application.
  153. ** Units of SlopTime are seconds. Default is 86400 (24 hours).
  154. ** Negative SlopTime values are not allowed.
  155. */
  156. PRInt32 CERT_GetSlopTime(void);
  157. SECStatus CERT_SetSlopTime(PRInt32 slop);
  158. /*
  159. ** Create a new certificate object. The result must be wrapped with an
  160. ** CERTSignedData to create a signed certificate.
  161. ** "serialNumber" the serial number
  162. ** "issuer" the name of the certificate issuer
  163. ** "validity" the validity period of the certificate
  164. ** "req" the certificate request that prompted the certificate issuance
  165. */
  166. extern CERTCertificate *CERT_CreateCertificate(unsigned long serialNumber,
  167. CERTName *issuer,
  168. CERTValidity *validity,
  169. CERTCertificateRequest *req);
  170. /*
  171. ** Destroy a certificate object
  172. ** "cert" the certificate to destroy
  173. ** NOTE: certificate's are reference counted. This call decrements the
  174. ** reference count, and if the result is zero, then the object is destroyed
  175. ** and optionally freed.
  176. */
  177. extern void CERT_DestroyCertificate(CERTCertificate *cert);
  178. /*
  179. ** Make a shallow copy of a certificate "c". Just increments the
  180. ** reference count on "c".
  181. */
  182. extern CERTCertificate *CERT_DupCertificate(CERTCertificate *c);
  183. /* Access the DER of the certificate. This only creates a reference to the DER
  184. * in the outparam not a copy. To avoid the pointer becoming invalid, use
  185. * CERT_DupCertificate() and keep a reference to the duplicate alive.
  186. */
  187. extern SECStatus CERT_GetCertificateDer(const CERTCertificate *c, SECItem *der);
  188. /*
  189. ** Create a new certificate request. This result must be wrapped with an
  190. ** CERTSignedData to create a signed certificate request.
  191. ** "name" the subject name (who the certificate request is from)
  192. ** "spki" describes/defines the public key the certificate is for
  193. ** "attributes" if non-zero, some optional attribute data
  194. */
  195. extern CERTCertificateRequest *CERT_CreateCertificateRequest(
  196. CERTName *name, CERTSubjectPublicKeyInfo *spki, SECItem **attributes);
  197. /*
  198. ** Destroy a certificate-request object
  199. ** "r" the certificate-request to destroy
  200. ** "freeit" if PR_TRUE then free the object as well as its sub-objects
  201. */
  202. extern void CERT_DestroyCertificateRequest(CERTCertificateRequest *r);
  203. /*
  204. ** Start adding extensions to a certificate request.
  205. */
  206. void *CERT_StartCertificateRequestAttributes(CERTCertificateRequest *req);
  207. /*
  208. ** Reformat the certificate extension list into a CertificateRequest
  209. ** attribute list.
  210. */
  211. SECStatus CERT_FinishCertificateRequestAttributes(CERTCertificateRequest *req);
  212. /*
  213. ** Extract the Extension Requests from a DER CertRequest attribute list.
  214. */
  215. SECStatus CERT_GetCertificateRequestExtensions(CERTCertificateRequest *req,
  216. CERTCertExtension ***exts);
  217. /*
  218. ** Extract a public key object from a certificate
  219. */
  220. extern SECKEYPublicKey *CERT_ExtractPublicKey(CERTCertificate *cert);
  221. /*
  222. ** Retrieve the Key Type associated with the cert we're dealing with
  223. */
  224. extern KeyType CERT_GetCertKeyType(const CERTSubjectPublicKeyInfo *spki);
  225. /*
  226. ** Initialize the certificate database. This is called to create
  227. ** the initial list of certificates in the database.
  228. */
  229. extern SECStatus CERT_InitCertDB(CERTCertDBHandle *handle);
  230. extern int CERT_GetDBContentVersion(CERTCertDBHandle *handle);
  231. /*
  232. ** Default certificate database routines
  233. */
  234. extern void CERT_SetDefaultCertDB(CERTCertDBHandle *handle);
  235. extern CERTCertDBHandle *CERT_GetDefaultCertDB(void);
  236. extern CERTCertList *CERT_GetCertChainFromCert(CERTCertificate *cert,
  237. PRTime time, SECCertUsage usage);
  238. extern CERTCertificate *CERT_NewTempCertificate(CERTCertDBHandle *handle,
  239. SECItem *derCert,
  240. char *nickname, PRBool isperm,
  241. PRBool copyDER);
  242. /******************************************************************************
  243. *
  244. * X.500 Name handling operations
  245. *
  246. *****************************************************************************/
  247. /*
  248. ** Create an AVA (attribute-value-assertion)
  249. ** "arena" the memory arena to alloc from
  250. ** "kind" is one of SEC_OID_AVA_*
  251. ** "valueType" is one of DER_PRINTABLE_STRING, DER_IA5_STRING, or
  252. ** DER_T61_STRING
  253. ** "value" is the null terminated string containing the value
  254. */
  255. extern CERTAVA *CERT_CreateAVA(PLArenaPool *arena, SECOidTag kind,
  256. int valueType, char *value);
  257. /*
  258. ** Extract the Distinguished Name from a DER encoded certificate
  259. ** "derCert" is the DER encoded certificate
  260. ** "derName" is the SECItem that the name is returned in
  261. */
  262. extern SECStatus CERT_NameFromDERCert(SECItem *derCert, SECItem *derName);
  263. /*
  264. ** Extract the Issuers Distinguished Name from a DER encoded certificate
  265. ** "derCert" is the DER encoded certificate
  266. ** "derName" is the SECItem that the name is returned in
  267. */
  268. extern SECStatus CERT_IssuerNameFromDERCert(SECItem *derCert, SECItem *derName);
  269. extern SECItem *CERT_EncodeGeneralName(CERTGeneralName *genName, SECItem *dest,
  270. PLArenaPool *arena);
  271. extern CERTGeneralName *CERT_DecodeGeneralName(PLArenaPool *reqArena,
  272. SECItem *encodedName,
  273. CERTGeneralName *genName);
  274. /*
  275. ** Generate a database search key for a certificate, based on the
  276. ** issuer and serial number.
  277. ** "arena" the memory arena to alloc from
  278. ** "derCert" the DER encoded certificate
  279. ** "key" the returned key
  280. */
  281. extern SECStatus CERT_KeyFromDERCert(PLArenaPool *reqArena, SECItem *derCert,
  282. SECItem *key);
  283. extern SECStatus CERT_KeyFromIssuerAndSN(PLArenaPool *arena, SECItem *issuer,
  284. SECItem *sn, SECItem *key);
  285. extern SECStatus CERT_SerialNumberFromDERCert(SECItem *derCert,
  286. SECItem *derName);
  287. /*
  288. ** Generate a database search key for a crl, based on the
  289. ** issuer.
  290. ** "arena" the memory arena to alloc from
  291. ** "derCrl" the DER encoded crl
  292. ** "key" the returned key
  293. */
  294. extern SECStatus CERT_KeyFromDERCrl(PLArenaPool *arena, SECItem *derCrl,
  295. SECItem *key);
  296. /*
  297. ** Open the certificate database. Use callback to get name of database.
  298. */
  299. extern SECStatus CERT_OpenCertDB(CERTCertDBHandle *handle, PRBool readOnly,
  300. CERTDBNameFunc namecb, void *cbarg);
  301. /* Open the certificate database. Use given filename for database. */
  302. extern SECStatus CERT_OpenCertDBFilename(CERTCertDBHandle *handle,
  303. char *certdbname, PRBool readOnly);
  304. /*
  305. ** Open and initialize a cert database that is entirely in memory. This
  306. ** can be used when the permanent database can not be opened or created.
  307. */
  308. extern SECStatus CERT_OpenVolatileCertDB(CERTCertDBHandle *handle);
  309. /*
  310. ** Extract the list of host names, host name patters, IP address strings
  311. ** this cert is valid for.
  312. ** This function does NOT return nicknames.
  313. ** Type CERTCertNicknames is being used because it's a convenient
  314. ** data structure to carry a list of strings and its count.
  315. */
  316. extern CERTCertNicknames *CERT_GetValidDNSPatternsFromCert(
  317. CERTCertificate *cert);
  318. /*
  319. ** Check the hostname to make sure that it matches the shexp that
  320. ** is given in the common name of the certificate.
  321. */
  322. extern SECStatus CERT_VerifyCertName(const CERTCertificate *cert,
  323. const char *hostname);
  324. /*
  325. ** Add a domain name to the list of names that the user has explicitly
  326. ** allowed (despite cert name mismatches) for use with a server cert.
  327. */
  328. extern SECStatus CERT_AddOKDomainName(CERTCertificate *cert,
  329. const char *hostname);
  330. /*
  331. ** Decode a DER encoded certificate into an CERTCertificate structure
  332. ** "derSignedCert" is the DER encoded signed certificate
  333. ** "copyDER" is true if the DER should be copied, false if the
  334. ** existing copy should be referenced
  335. ** "nickname" is the nickname to use in the database. If it is NULL
  336. ** then a temporary nickname is generated.
  337. */
  338. extern CERTCertificate *CERT_DecodeDERCertificate(SECItem *derSignedCert,
  339. PRBool copyDER,
  340. char *nickname);
  341. /*
  342. ** Decode a DER encoded CRL into a CERTSignedCrl structure
  343. ** "derSignedCrl" is the DER encoded signed CRL.
  344. ** "type" must be SEC_CRL_TYPE.
  345. */
  346. #define SEC_CRL_TYPE 1
  347. #define SEC_KRL_TYPE 0 /* deprecated */
  348. extern CERTSignedCrl *CERT_DecodeDERCrl(PLArenaPool *arena,
  349. SECItem *derSignedCrl, int type);
  350. /*
  351. * same as CERT_DecodeDERCrl, plus allow options to be passed in
  352. */
  353. extern CERTSignedCrl *CERT_DecodeDERCrlWithFlags(PLArenaPool *narena,
  354. SECItem *derSignedCrl,
  355. int type, PRInt32 options);
  356. /* CRL options to pass */
  357. #define CRL_DECODE_DEFAULT_OPTIONS 0x00000000
  358. /* when CRL_DECODE_DONT_COPY_DER is set, the DER is not copied . The
  359. application must then keep derSignedCrl until it destroys the
  360. CRL . Ideally, it should allocate derSignedCrl in an arena
  361. and pass that arena in as the first argument to
  362. CERT_DecodeDERCrlWithFlags */
  363. #define CRL_DECODE_DONT_COPY_DER 0x00000001
  364. #define CRL_DECODE_SKIP_ENTRIES 0x00000002
  365. #define CRL_DECODE_KEEP_BAD_CRL 0x00000004
  366. #define CRL_DECODE_ADOPT_HEAP_DER 0x00000008
  367. /* complete the decoding of a partially decoded CRL, ie. decode the
  368. entries. Note that entries is an optional field in a CRL, so the
  369. "entries" pointer in CERTCrlStr may still be NULL even after
  370. function returns SECSuccess */
  371. extern SECStatus CERT_CompleteCRLDecodeEntries(CERTSignedCrl *crl);
  372. /* Validate CRL then import it to the dbase. If there is already a CRL with the
  373. * same CA in the dbase, it will be replaced if derCRL is more up to date.
  374. * If the process successes, a CRL will be returned. Otherwise, a NULL will
  375. * be returned. The caller should call PORT_GetError() for the exactly error
  376. * code.
  377. */
  378. extern CERTSignedCrl *CERT_ImportCRL(CERTCertDBHandle *handle, SECItem *derCRL,
  379. char *url, int type, void *wincx);
  380. extern void CERT_DestroyCrl(CERTSignedCrl *crl);
  381. /* this is a hint to flush the CRL cache. crlKey is the DER subject of
  382. the issuer (CA). */
  383. void CERT_CRLCacheRefreshIssuer(CERTCertDBHandle *dbhandle, SECItem *crlKey);
  384. /* add the specified DER CRL object to the CRL cache. Doing so will allow
  385. certificate verification functions (such as CERT_VerifyCertificate)
  386. to automatically find and make use of this CRL object.
  387. Once a CRL is added to the CRL cache, the application must hold on to
  388. the object's memory, because the cache will reference it directly. The
  389. application can only free the object after it calls CERT_UncacheCRL to
  390. remove it from the CRL cache.
  391. */
  392. SECStatus CERT_CacheCRL(CERTCertDBHandle *dbhandle, SECItem *newcrl);
  393. /* remove a previously added CRL object from the CRL cache. It is OK
  394. for the application to free the memory after a successful removal
  395. */
  396. SECStatus CERT_UncacheCRL(CERTCertDBHandle *dbhandle, SECItem *oldcrl);
  397. /*
  398. ** Find a certificate in the database
  399. ** "key" is the database key to look for
  400. */
  401. extern CERTCertificate *CERT_FindCertByKey(CERTCertDBHandle *handle,
  402. SECItem *key);
  403. /*
  404. ** Find a certificate in the database by name
  405. ** "name" is the distinguished name to look up
  406. */
  407. extern CERTCertificate *CERT_FindCertByName(CERTCertDBHandle *handle,
  408. SECItem *name);
  409. /*
  410. ** Find a certificate in the database by name
  411. ** "name" is the distinguished name to look up (in ascii)
  412. */
  413. extern CERTCertificate *CERT_FindCertByNameString(CERTCertDBHandle *handle,
  414. char *name);
  415. /*
  416. ** Find a certificate in the database by name and keyid
  417. ** "name" is the distinguished name to look up
  418. ** "keyID" is the value of the subjectKeyID to match
  419. */
  420. extern CERTCertificate *CERT_FindCertByKeyID(CERTCertDBHandle *handle,
  421. SECItem *name, SECItem *keyID);
  422. /*
  423. ** Generate a certificate key from the issuer and serialnumber, then look it
  424. ** up in the database. Return the cert if found.
  425. ** "issuerAndSN" is the issuer and serial number to look for
  426. */
  427. extern CERTCertificate *CERT_FindCertByIssuerAndSN(
  428. CERTCertDBHandle *handle, CERTIssuerAndSN *issuerAndSN);
  429. extern CERTCertificate *CERT_FindCertByIssuerAndSNCX(
  430. CERTCertDBHandle *handle, CERTIssuerAndSN *issuerAndSN, void *wincx);
  431. /*
  432. ** Find a certificate in the database by a subject key ID
  433. ** "subjKeyID" is the subject Key ID to look for
  434. */
  435. extern CERTCertificate *CERT_FindCertBySubjectKeyID(CERTCertDBHandle *handle,
  436. SECItem *subjKeyID);
  437. /*
  438. ** Encode Certificate SKID (Subject Key ID) extension.
  439. **
  440. */
  441. extern SECStatus CERT_EncodeSubjectKeyID(PLArenaPool *arena,
  442. const SECItem *srcString,
  443. SECItem *encodedValue);
  444. /*
  445. ** Find a certificate in the database by a nickname
  446. ** "nickname" is the ascii string nickname to look for
  447. */
  448. extern CERTCertificate *CERT_FindCertByNickname(CERTCertDBHandle *handle,
  449. const char *nickname);
  450. /*
  451. ** Find a certificate in the database by a DER encoded certificate
  452. ** "derCert" is the DER encoded certificate
  453. */
  454. extern CERTCertificate *CERT_FindCertByDERCert(CERTCertDBHandle *handle,
  455. SECItem *derCert);
  456. /*
  457. ** Find a certificate in the database by a email address
  458. ** "emailAddr" is the email address to look up
  459. */
  460. CERTCertificate *CERT_FindCertByEmailAddr(CERTCertDBHandle *handle,
  461. char *emailAddr);
  462. /*
  463. ** Find a certificate in the database by a email address or nickname
  464. ** "name" is the email address or nickname to look up
  465. */
  466. CERTCertificate *CERT_FindCertByNicknameOrEmailAddr(CERTCertDBHandle *handle,
  467. const char *name);
  468. CERTCertificate *CERT_FindCertByNicknameOrEmailAddrCX(CERTCertDBHandle *handle,
  469. const char *name,
  470. void *wincx);
  471. /*
  472. ** Find a certificate in the database by a email address or nickname
  473. ** and require it to have the given usage.
  474. ** "name" is the email address or nickname to look up
  475. */
  476. CERTCertificate *CERT_FindCertByNicknameOrEmailAddrForUsage(
  477. CERTCertDBHandle *handle, const char *name, SECCertUsage lookingForUsage);
  478. CERTCertificate *CERT_FindCertByNicknameOrEmailAddrForUsageCX(
  479. CERTCertDBHandle *handle, const char *name, SECCertUsage lookingForUsage,
  480. void *wincx);
  481. /*
  482. ** Find a certificate in the database by a digest of a subject public key
  483. ** "spkDigest" is the digest to look up
  484. */
  485. extern CERTCertificate *CERT_FindCertBySPKDigest(CERTCertDBHandle *handle,
  486. SECItem *spkDigest);
  487. /*
  488. * Find the issuer of a cert
  489. */
  490. CERTCertificate *CERT_FindCertIssuer(CERTCertificate *cert, PRTime validTime,
  491. SECCertUsage usage);
  492. /*
  493. ** Check the validity times of a certificate vs. time 't', allowing
  494. ** some slop for broken clocks and stuff.
  495. ** "cert" is the certificate to be checked
  496. ** "t" is the time to check against
  497. ** "allowOverride" if true then check to see if the invalidity has
  498. ** been overridden by the user.
  499. */
  500. extern SECCertTimeValidity CERT_CheckCertValidTimes(const CERTCertificate *cert,
  501. PRTime t,
  502. PRBool allowOverride);
  503. /*
  504. ** WARNING - this function is deprecated, and will either go away or have
  505. ** a new API in the near future.
  506. **
  507. ** Check the validity times of a certificate vs. the current time, allowing
  508. ** some slop for broken clocks and stuff.
  509. ** "cert" is the certificate to be checked
  510. */
  511. extern SECStatus CERT_CertTimesValid(CERTCertificate *cert);
  512. /*
  513. ** Extract the validity times from a certificate
  514. ** "c" is the certificate
  515. ** "notBefore" is the start of the validity period
  516. ** "notAfter" is the end of the validity period
  517. */
  518. extern SECStatus CERT_GetCertTimes(const CERTCertificate *c, PRTime *notBefore,
  519. PRTime *notAfter);
  520. /*
  521. ** Extract the issuer and serial number from a certificate
  522. */
  523. extern CERTIssuerAndSN *CERT_GetCertIssuerAndSN(PLArenaPool *,
  524. CERTCertificate *);
  525. /*
  526. ** verify the signature of a signed data object with a given certificate
  527. ** "sd" the signed data object to be verified
  528. ** "cert" the certificate to use to check the signature
  529. */
  530. extern SECStatus CERT_VerifySignedData(CERTSignedData *sd,
  531. CERTCertificate *cert, PRTime t,
  532. void *wincx);
  533. /*
  534. ** verify the signature of a signed data object with the given DER publickey
  535. */
  536. extern SECStatus CERT_VerifySignedDataWithPublicKeyInfo(
  537. CERTSignedData *sd, CERTSubjectPublicKeyInfo *pubKeyInfo, void *wincx);
  538. /*
  539. ** verify the signature of a signed data object with a SECKEYPublicKey.
  540. */
  541. extern SECStatus CERT_VerifySignedDataWithPublicKey(const CERTSignedData *sd,
  542. SECKEYPublicKey *pubKey,
  543. void *wincx);
  544. /*
  545. ** NEW FUNCTIONS with new bit-field-FIELD SECCertificateUsage - please use
  546. ** verify a certificate by checking validity times against a certain time,
  547. ** that we trust the issuer, and that the signature on the certificate is
  548. ** valid.
  549. ** "cert" the certificate to verify
  550. ** "checkSig" only check signatures if true
  551. */
  552. extern SECStatus CERT_VerifyCertificate(CERTCertDBHandle *handle,
  553. CERTCertificate *cert, PRBool checkSig,
  554. SECCertificateUsage requiredUsages,
  555. PRTime t, void *wincx,
  556. CERTVerifyLog *log,
  557. SECCertificateUsage *returnedUsages);
  558. /* same as above, but uses current time */
  559. extern SECStatus CERT_VerifyCertificateNow(CERTCertDBHandle *handle,
  560. CERTCertificate *cert,
  561. PRBool checkSig,
  562. SECCertificateUsage requiredUsages,
  563. void *wincx,
  564. SECCertificateUsage *returnedUsages);
  565. /*
  566. ** Verify that a CA cert can certify some (unspecified) leaf cert for a given
  567. ** purpose. This is used by UI code to help identify where a chain may be
  568. ** broken and why. This takes identical parameters to CERT_VerifyCert
  569. */
  570. extern SECStatus CERT_VerifyCACertForUsage(CERTCertDBHandle *handle,
  571. CERTCertificate *cert,
  572. PRBool checkSig,
  573. SECCertUsage certUsage, PRTime t,
  574. void *wincx, CERTVerifyLog *log);
  575. /*
  576. ** OLD OBSOLETE FUNCTIONS with enum SECCertUsage - DO NOT USE FOR NEW CODE
  577. ** verify a certificate by checking validity times against a certain time,
  578. ** that we trust the issuer, and that the signature on the certificate is
  579. ** valid.
  580. ** "cert" the certificate to verify
  581. ** "checkSig" only check signatures if true
  582. */
  583. extern SECStatus CERT_VerifyCert(CERTCertDBHandle *handle,
  584. CERTCertificate *cert, PRBool checkSig,
  585. SECCertUsage certUsage, PRTime t, void *wincx,
  586. CERTVerifyLog *log);
  587. /* same as above, but uses current time */
  588. extern SECStatus CERT_VerifyCertNow(CERTCertDBHandle *handle,
  589. CERTCertificate *cert, PRBool checkSig,
  590. SECCertUsage certUsage, void *wincx);
  591. SECStatus CERT_VerifyCertChain(CERTCertDBHandle *handle, CERTCertificate *cert,
  592. PRBool checkSig, SECCertUsage certUsage,
  593. PRTime t, void *wincx, CERTVerifyLog *log);
  594. /*
  595. ** Read a base64 ascii encoded DER certificate and convert it to our
  596. ** internal format.
  597. ** "certstr" is a null-terminated string containing the certificate
  598. */
  599. extern CERTCertificate *CERT_ConvertAndDecodeCertificate(char *certstr);
  600. /*
  601. ** Read a certificate in some foreign format, and convert it to our
  602. ** internal format.
  603. ** "certbuf" is the buffer containing the certificate
  604. ** "certlen" is the length of the buffer
  605. ** NOTE - currently supports netscape base64 ascii encoded raw certs
  606. ** and netscape binary DER typed files.
  607. */
  608. extern CERTCertificate *CERT_DecodeCertFromPackage(char *certbuf, int certlen);
  609. extern SECStatus CERT_ImportCAChain(SECItem *certs, int numcerts,
  610. SECCertUsage certUsage);
  611. extern SECStatus CERT_ImportCAChainTrusted(SECItem *certs, int numcerts,
  612. SECCertUsage certUsage);
  613. /*
  614. ** Read a certificate chain in some foreign format, and pass it to a
  615. ** callback function.
  616. ** "certbuf" is the buffer containing the certificate
  617. ** "certlen" is the length of the buffer
  618. ** "f" is the callback function
  619. ** "arg" is the callback argument
  620. */
  621. typedef SECStatus(PR_CALLBACK *CERTImportCertificateFunc)(void *arg,
  622. SECItem **certs,
  623. int numcerts);
  624. extern SECStatus CERT_DecodeCertPackage(char *certbuf, int certlen,
  625. CERTImportCertificateFunc f, void *arg);
  626. /*
  627. ** Returns the value of an AVA. This was a formerly static
  628. ** function that has been exposed due to the need to decode
  629. ** and convert unicode strings to UTF8.
  630. **
  631. ** XXX This function resides in certhtml.c, should it be
  632. ** moved elsewhere?
  633. */
  634. extern SECItem *CERT_DecodeAVAValue(const SECItem *derAVAValue);
  635. /*
  636. ** extract various element strings from a distinguished name.
  637. ** "name" the distinguished name
  638. */
  639. extern char *CERT_GetCertificateEmailAddress(CERTCertificate *cert);
  640. extern char *CERT_GetCertEmailAddress(const CERTName *name);
  641. extern const char *CERT_GetFirstEmailAddress(CERTCertificate *cert);
  642. extern const char *CERT_GetNextEmailAddress(CERTCertificate *cert,
  643. const char *prev);
  644. /* The return value must be freed with PORT_Free. */
  645. extern char *CERT_GetCommonName(const CERTName *name);
  646. extern char *CERT_GetCountryName(const CERTName *name);
  647. extern char *CERT_GetLocalityName(const CERTName *name);
  648. extern char *CERT_GetStateName(const CERTName *name);
  649. extern char *CERT_GetOrgName(const CERTName *name);
  650. extern char *CERT_GetOrgUnitName(const CERTName *name);
  651. extern char *CERT_GetDomainComponentName(const CERTName *name);
  652. extern char *CERT_GetCertUid(const CERTName *name);
  653. /* manipulate the trust parameters of a certificate */
  654. extern SECStatus CERT_GetCertTrust(const CERTCertificate *cert,
  655. CERTCertTrust *trust);
  656. extern SECStatus CERT_ChangeCertTrust(CERTCertDBHandle *handle,
  657. CERTCertificate *cert,
  658. CERTCertTrust *trust);
  659. extern SECStatus CERT_ChangeCertTrustByUsage(CERTCertDBHandle *certdb,
  660. CERTCertificate *cert,
  661. SECCertUsage usage);
  662. /*************************************************************************
  663. *
  664. * manipulate the extensions of a certificate
  665. *
  666. ************************************************************************/
  667. /*
  668. ** Set up a cert for adding X509v3 extensions. Returns an opaque handle
  669. ** used by the next two routines.
  670. ** "cert" is the certificate we are adding extensions to
  671. */
  672. extern void *CERT_StartCertExtensions(CERTCertificate *cert);
  673. /*
  674. ** Add an extension to a certificate.
  675. ** "exthandle" is the handle returned by the previous function
  676. ** "idtag" is the integer tag for the OID that should ID this extension
  677. ** "value" is the value of the extension
  678. ** "critical" is the critical extension flag
  679. ** "copyData" is a flag indicating whether the value data should be
  680. ** copied.
  681. */
  682. extern SECStatus CERT_AddExtension(void *exthandle, int idtag, SECItem *value,
  683. PRBool critical, PRBool copyData);
  684. extern SECStatus CERT_AddExtensionByOID(void *exthandle, SECItem *oid,
  685. SECItem *value, PRBool critical,
  686. PRBool copyData);
  687. extern SECStatus CERT_EncodeAndAddExtension(void *exthandle, int idtag,
  688. void *value, PRBool critical,
  689. const SEC_ASN1Template *atemplate);
  690. extern SECStatus CERT_EncodeAndAddBitStrExtension(void *exthandle, int idtag,
  691. SECItem *value,
  692. PRBool critical);
  693. extern SECStatus CERT_EncodeAltNameExtension(PLArenaPool *arena,
  694. CERTGeneralName *value,
  695. SECItem *encodedValue);
  696. /*
  697. ** Finish adding cert extensions. Does final processing on extension
  698. ** data, putting it in the right format, and freeing any temporary
  699. ** storage.
  700. ** "exthandle" is the handle used to add extensions to a certificate
  701. */
  702. extern SECStatus CERT_FinishExtensions(void *exthandle);
  703. /*
  704. ** Merge an external list of extensions into a cert's extension list, adding one
  705. ** only when its OID matches none of the cert's existing extensions. Call this
  706. ** immediately before calling CERT_FinishExtensions().
  707. */
  708. SECStatus CERT_MergeExtensions(void *exthandle, CERTCertExtension **exts);
  709. /* If the extension is found, return its criticality and value.
  710. ** This allocate storage for the returning extension value.
  711. */
  712. extern SECStatus CERT_GetExtenCriticality(CERTCertExtension **extensions,
  713. int tag, PRBool *isCritical);
  714. extern void CERT_DestroyOidSequence(CERTOidSequence *oidSeq);
  715. /****************************************************************************
  716. *
  717. * DER encode and decode extension values
  718. *
  719. ****************************************************************************/
  720. /* Encode the value of the basicConstraint extension.
  721. ** arena - where to allocate memory for the encoded value.
  722. ** value - extension value to encode
  723. ** encodedValue - output encoded value
  724. */
  725. extern SECStatus CERT_EncodeBasicConstraintValue(PLArenaPool *arena,
  726. CERTBasicConstraints *value,
  727. SECItem *encodedValue);
  728. /*
  729. ** Encode the value of the authorityKeyIdentifier extension.
  730. */
  731. extern SECStatus CERT_EncodeAuthKeyID(PLArenaPool *arena, CERTAuthKeyID *value,
  732. SECItem *encodedValue);
  733. /*
  734. ** Encode the value of the crlDistributionPoints extension.
  735. */
  736. extern SECStatus CERT_EncodeCRLDistributionPoints(
  737. PLArenaPool *arena, CERTCrlDistributionPoints *value, SECItem *derValue);
  738. /*
  739. ** Decodes a DER encoded basicConstaint extension value into a readable format
  740. ** value - decoded value
  741. ** encodedValue - value to decoded
  742. */
  743. extern SECStatus CERT_DecodeBasicConstraintValue(CERTBasicConstraints *value,
  744. const SECItem *encodedValue);
  745. /* Decodes a DER encoded authorityKeyIdentifier extension value into a
  746. ** readable format.
  747. ** arena - where to allocate memory for the decoded value
  748. ** encodedValue - value to be decoded
  749. ** Returns a CERTAuthKeyID structure which contains the decoded value
  750. */
  751. extern CERTAuthKeyID *CERT_DecodeAuthKeyID(PLArenaPool *arena,
  752. const SECItem *encodedValue);
  753. /* Decodes a DER encoded crlDistributionPoints extension value into a
  754. ** readable format.
  755. ** arena - where to allocate memory for the decoded value
  756. ** der - value to be decoded
  757. ** Returns a CERTCrlDistributionPoints structure which contains the
  758. ** decoded value
  759. */
  760. extern CERTCrlDistributionPoints *CERT_DecodeCRLDistributionPoints(
  761. PLArenaPool *arena, SECItem *der);
  762. /* Extract certain name type from a generalName */
  763. extern void *CERT_GetGeneralNameByType(CERTGeneralName *genNames,
  764. CERTGeneralNameType type,
  765. PRBool derFormat);
  766. extern CERTOidSequence *CERT_DecodeOidSequence(const SECItem *seqItem);
  767. /****************************************************************************
  768. *
  769. * Find extension values of a certificate
  770. *
  771. ***************************************************************************/
  772. extern SECStatus CERT_FindCertExtension(const CERTCertificate *cert, int tag,
  773. SECItem *value);
  774. extern SECStatus CERT_FindNSCertTypeExtension(CERTCertificate *cert,
  775. SECItem *value);
  776. extern char *CERT_FindNSStringExtension(CERTCertificate *cert, int oidtag);
  777. extern SECStatus CERT_FindCertExtensionByOID(CERTCertificate *cert,
  778. SECItem *oid, SECItem *value);
  779. /* Returns the decoded value of the authKeyID extension.
  780. ** Note that this uses passed in the arena to allocate storage for the result
  781. */
  782. extern CERTAuthKeyID *CERT_FindAuthKeyIDExten(PLArenaPool *arena,
  783. CERTCertificate *cert);
  784. /* Returns the decoded value of the basicConstraint extension.
  785. */
  786. extern SECStatus CERT_FindBasicConstraintExten(CERTCertificate *cert,
  787. CERTBasicConstraints *value);
  788. /* Returns the decoded value of the crlDistributionPoints extension.
  789. ** Note that the arena in cert is used to allocate storage for the result
  790. */
  791. extern CERTCrlDistributionPoints *CERT_FindCRLDistributionPoints(
  792. CERTCertificate *cert);
  793. /* Returns value of the keyUsage extension. This uses PR_Alloc to allocate
  794. ** buffer for the decoded value. The caller should free up the storage
  795. ** allocated in value->data.
  796. */
  797. extern SECStatus CERT_FindKeyUsageExtension(CERTCertificate *cert,
  798. SECItem *value);
  799. /* Return the decoded value of the subjectKeyID extension. The caller should
  800. ** free up the storage allocated in retItem->data.
  801. */
  802. extern SECStatus CERT_FindSubjectKeyIDExtension(CERTCertificate *cert,
  803. SECItem *retItem);
  804. /*
  805. ** If cert is a v3 certificate, and a critical keyUsage extension is included,
  806. ** then check the usage against the extension value. If a non-critical
  807. ** keyUsage extension is included, this will return SECSuccess without
  808. ** checking, since the extension is an advisory field, not a restriction.
  809. ** If cert is not a v3 certificate, this will return SECSuccess.
  810. ** cert - certificate
  811. ** usage - one of the x.509 v3 the Key Usage Extension flags
  812. */
  813. extern SECStatus CERT_CheckCertUsage(CERTCertificate *cert,
  814. unsigned char usage);
  815. /****************************************************************************
  816. *
  817. * CRL v2 Extensions supported routines
  818. *
  819. ****************************************************************************/
  820. extern SECStatus CERT_FindCRLExtensionByOID(CERTCrl *crl, SECItem *oid,
  821. SECItem *value);
  822. extern SECStatus CERT_FindCRLExtension(CERTCrl *crl, int tag, SECItem *value);
  823. extern SECStatus CERT_FindInvalidDateExten(CERTCrl *crl, PRTime *value);
  824. /*
  825. ** Set up a crl for adding X509v3 extensions. Returns an opaque handle
  826. ** used by routines that take an exthandle (void*) argument .
  827. ** "crl" is the CRL we are adding extensions to
  828. */
  829. extern void *CERT_StartCRLExtensions(CERTCrl *crl);
  830. /*
  831. ** Set up a crl entry for adding X509v3 extensions. Returns an opaque handle
  832. ** used by routines that take an exthandle (void*) argument .
  833. ** "crl" is the crl we are adding certs entries to
  834. ** "entry" is the crl entry we are adding extensions to
  835. */
  836. extern void *CERT_StartCRLEntryExtensions(CERTCrl *crl, CERTCrlEntry *entry);
  837. extern CERTCertNicknames *CERT_GetCertNicknames(CERTCertDBHandle *handle,
  838. int what, void *wincx);
  839. /*
  840. ** Finds the crlNumber extension and decodes its value into 'value'
  841. */
  842. extern SECStatus CERT_FindCRLNumberExten(PLArenaPool *arena, CERTCrl *crl,
  843. SECItem *value);
  844. extern SECStatus CERT_FindCRLEntryReasonExten(CERTCrlEntry *crlEntry,
  845. CERTCRLEntryReasonCode *value);
  846. extern void CERT_FreeNicknames(CERTCertNicknames *nicknames);
  847. extern PRBool CERT_CompareCerts(const CERTCertificate *c1,
  848. const CERTCertificate *c2);
  849. extern PRBool CERT_CompareCertsForRedirection(CERTCertificate *c1,
  850. CERTCertificate *c2);
  851. /*
  852. ** Generate an array of the Distinguished Names that the given cert database
  853. ** "trusts"
  854. */
  855. extern CERTDistNames *CERT_GetSSLCACerts(CERTCertDBHandle *handle);
  856. extern void CERT_FreeDistNames(CERTDistNames *names);
  857. /* Duplicate distinguished name array */
  858. extern CERTDistNames *CERT_DupDistNames(CERTDistNames *orig);
  859. /*
  860. ** Generate an array of Distinguished names from an array of nicknames
  861. */
  862. extern CERTDistNames *CERT_DistNamesFromNicknames(CERTCertDBHandle *handle,
  863. char **nicknames, int nnames);
  864. /*
  865. ** Generate an array of Distinguished names from a list of certs.
  866. */
  867. extern CERTDistNames *CERT_DistNamesFromCertList(CERTCertList *list);
  868. /*
  869. ** Generate a certificate chain from a certificate.
  870. */
  871. extern CERTCertificateList *CERT_CertChainFromCert(CERTCertificate *cert,
  872. SECCertUsage usage,
  873. PRBool includeRoot);
  874. extern CERTCertificateList *CERT_CertListFromCert(CERTCertificate *cert);
  875. extern CERTCertificateList *CERT_DupCertList(
  876. const CERTCertificateList *oldList);
  877. extern void CERT_DestroyCertificateList(CERTCertificateList *list);
  878. /*
  879. ** is cert a user cert? i.e. does it have CERTDB_USER trust,
  880. ** i.e. a private key?
  881. */
  882. PRBool CERT_IsUserCert(CERTCertificate *cert);
  883. /* is cert a newer than cert b? */
  884. PRBool CERT_IsNewer(CERTCertificate *certa, CERTCertificate *certb);
  885. /* currently a stub for address book */
  886. PRBool CERT_IsCertRevoked(CERTCertificate *cert);
  887. void CERT_DestroyCertArray(CERTCertificate **certs, unsigned int ncerts);
  888. /* convert an email address to lower case */
  889. char *CERT_FixupEmailAddr(const char *emailAddr);
  890. /* decode string representation of trust flags into trust struct */
  891. SECStatus CERT_DecodeTrustString(CERTCertTrust *trust, const char *trusts);
  892. /* encode trust struct into string representation of trust flags */
  893. char *CERT_EncodeTrustString(CERTCertTrust *trust);
  894. /* find the next or prev cert in a subject list */
  895. CERTCertificate *CERT_PrevSubjectCert(CERTCertificate *cert);
  896. CERTCertificate *CERT_NextSubjectCert(CERTCertificate *cert);
  897. /*
  898. * import a collection of certs into the temporary or permanent cert
  899. * database
  900. */
  901. SECStatus CERT_ImportCerts(CERTCertDBHandle *certdb, SECCertUsage usage,
  902. unsigned int ncerts, SECItem **derCerts,
  903. CERTCertificate ***retCerts, PRBool keepCerts,
  904. PRBool caOnly, char *nickname);
  905. char *CERT_MakeCANickname(CERTCertificate *cert);
  906. PRBool CERT_IsCACert(CERTCertificate *cert, unsigned int *rettype);
  907. PRBool CERT_IsCADERCert(SECItem *derCert, unsigned int *rettype);
  908. PRBool CERT_IsRootDERCert(SECItem *derCert);
  909. SECStatus CERT_SaveSMimeProfile(CERTCertificate *cert, SECItem *emailProfile,
  910. SECItem *profileTime);
  911. /*
  912. * find the smime symmetric capabilities profile for a given cert
  913. */
  914. SECItem *CERT_FindSMimeProfile(CERTCertificate *cert);
  915. SECStatus CERT_AddNewCerts(CERTCertDBHandle *handle);
  916. CERTCertificatePolicies *CERT_DecodeCertificatePoliciesExtension(
  917. const SECItem *extnValue);
  918. void CERT_DestroyCertificatePoliciesExtension(
  919. CERTCertificatePolicies *policies);
  920. CERTCertificatePolicyMappings *CERT_DecodePolicyMappingsExtension(
  921. SECItem *encodedCertPolicyMaps);
  922. SECStatus CERT_DestroyPolicyMappingsExtension(
  923. CERTCertificatePolicyMappings *mappings);
  924. SECStatus CERT_DecodePolicyConstraintsExtension(
  925. CERTCertificatePolicyConstraints *decodedValue,
  926. const SECItem *encodedValue);
  927. SECStatus CERT_DecodeInhibitAnyExtension(
  928. CERTCertificateInhibitAny *decodedValue, SECItem *extnValue);
  929. CERTUserNotice *CERT_DecodeUserNotice(SECItem *noticeItem);
  930. extern CERTGeneralName *CERT_DecodeAltNameExtension(PLArenaPool *reqArena,
  931. SECItem *EncodedAltName);
  932. extern CERTNameConstraints *CERT_DecodeNameConstraintsExtension(
  933. PLArenaPool *arena, const SECItem *encodedConstraints);
  934. /* returns addr of a NULL termainated array of pointers to CERTAuthInfoAccess */
  935. extern CERTAuthInfoAccess **CERT_DecodeAuthInfoAccessExtension(
  936. PLArenaPool *reqArena, const SECItem *encodedExtension);
  937. extern CERTPrivKeyUsagePeriod *CERT_DecodePrivKeyUsagePeriodExtension(
  938. PLArenaPool *arena, SECItem *extnValue);
  939. extern CERTGeneralName *CERT_GetNextGeneralName(CERTGeneralName *current);
  940. extern CERTGeneralName *CERT_GetPrevGeneralName(CERTGeneralName *current);
  941. /*
  942. * Look up name constraints for some certs that do not include name constraints
  943. * (Most importantly, root certificates)
  944. *
  945. * If a matching subject is found, |extensions| will be populated with a copy of
  946. * the
  947. * DER-encoded name constraints extension. The data in |extensions| will point
  948. * to
  949. * memory that the caller owns.
  950. *
  951. * There is no mechanism to configure imposed name constraints right now. All
  952. * imposed name constraints are built into NSS.
  953. */
  954. SECStatus CERT_GetImposedNameConstraints(const SECItem *derSubject,
  955. SECItem *extensions);
  956. CERTNameConstraint *CERT_GetNextNameConstraint(CERTNameConstraint *current);
  957. CERTNameConstraint *CERT_GetPrevNameConstraint(CERTNameConstraint *current);
  958. void CERT_DestroyUserNotice(CERTUserNotice *userNotice);
  959. typedef char *(*CERTPolicyStringCallback)(char *org, unsigned long noticeNumber,
  960. void *arg);
  961. void CERT_SetCAPolicyStringCallback(CERTPolicyStringCallback cb, void *cbarg);
  962. char *CERT_GetCertCommentString(CERTCertificate *cert);
  963. PRBool CERT_GovtApprovedBitSet(CERTCertificate *cert);
  964. SECStatus CERT_AddPermNickname(CERTCertificate *cert, char *nickname);
  965. CERTCertList *CERT_MatchUserCert(CERTCertDBHandle *handle, SECCertUsage usage,
  966. int nCANames, char **caNames, void *proto_win);
  967. CERTCertList *CERT_NewCertList(void);
  968. /* free the cert list and all the certs in the list */
  969. void CERT_DestroyCertList(CERTCertList *certs);
  970. /* remove the node and free the cert */
  971. void CERT_RemoveCertListNode(CERTCertListNode *node);
  972. /* equivalent to CERT_AddCertToListTailWithData(certs, cert, NULL) */
  973. SECStatus CERT_AddCertToListTail(CERTCertList *certs, CERTCertificate *cert);
  974. /* equivalent to CERT_AddCertToListHeadWithData(certs, cert, NULL) */
  975. SECStatus CERT_AddCertToListHead(CERTCertList *certs, CERTCertificate *cert);
  976. /*
  977. * The new cert list node takes ownership of "cert". "cert" is freed
  978. * when the list node is removed.
  979. */
  980. SECStatus CERT_AddCertToListTailWithData(CERTCertList *certs,
  981. CERTCertificate *cert, void *appData);
  982. /*
  983. * The new cert list node takes ownership of "cert". "cert" is freed
  984. * when the list node is removed.
  985. */
  986. SECStatus CERT_AddCertToListHeadWithData(CERTCertList *certs,
  987. CERTCertificate *cert, void *appData);
  988. typedef PRBool (*CERTSortCallback)(CERTCertificate *certa,
  989. CERTCertificate *certb, void *arg);
  990. SECStatus CERT_AddCertToListSorted(CERTCertList *certs, CERTCertificate *cert,
  991. CERTSortCallback f, void *arg);
  992. /* callback for CERT_AddCertToListSorted that sorts based on validity
  993. * period and a given time.
  994. */
  995. PRBool CERT_SortCBValidity(CERTCertificate *certa, CERTCertificate *certb,
  996. void *arg);
  997. SECStatus CERT_CheckForEvilCert(CERTCertificate *cert);
  998. CERTGeneralName *CERT_GetCertificateNames(CERTCertificate *cert,
  999. PLArenaPool *arena);
  1000. CERTGeneralName *CERT_GetConstrainedCertificateNames(
  1001. const CERTCertificate *cert, PLArenaPool *arena,
  1002. PRBool includeSubjectCommonName);
  1003. /*
  1004. * Creates or adds to a list of all certs with a give subject name, sorted by
  1005. * validity time, newest first. Invalid certs are considered older than
  1006. * valid certs. If validOnly is set, do not include invalid certs on list.
  1007. */
  1008. CERTCertList *CERT_CreateSubjectCertList(CERTCertList *certList,
  1009. CERTCertDBHandle *handle,
  1010. const SECItem *name, PRTime sorttime,
  1011. PRBool validOnly);
  1012. /*
  1013. * remove certs from a list that don't have keyUsage and certType
  1014. * that match the given usage.
  1015. */
  1016. SECStatus CERT_FilterCertListByUsage(CERTCertList *certList, SECCertUsage usage,
  1017. PRBool ca);
  1018. /*
  1019. * check the key usage of a cert against a set of required values
  1020. */
  1021. SECStatus CERT_CheckKeyUsage(CERTCertificate *cert, unsigned int requiredUsage);
  1022. /*
  1023. * return required key usage and cert type based on cert usage
  1024. */
  1025. SECStatus CERT_KeyUsageAndTypeForCertUsage(SECCertUsage usage, PRBool ca,
  1026. unsigned int *retKeyUsage,
  1027. unsigned int *retCertType);
  1028. /*
  1029. * return required trust flags for various cert usages for CAs
  1030. */
  1031. SECStatus CERT_TrustFlagsForCACertUsage(SECCertUsage usage,
  1032. unsigned int *retFlags,
  1033. SECTrustType *retTrustType);
  1034. /*
  1035. * Find all user certificates that match the given criteria.
  1036. *
  1037. * "handle" - database to search
  1038. * "usage" - certificate usage to match
  1039. * "oneCertPerName" - if set then only return the "best" cert per
  1040. * name
  1041. * "validOnly" - only return certs that are curently valid
  1042. * "proto_win" - window handle passed to pkcs11
  1043. */
  1044. CERTCertList *CERT_FindUserCertsByUsage(CERTCertDBHandle *handle,
  1045. SECCertUsage usage,
  1046. PRBool oneCertPerName, PRBool validOnly,
  1047. void *proto_win);
  1048. /*
  1049. * Find a user certificate that matchs the given criteria.
  1050. *
  1051. * "handle" - database to search
  1052. * "nickname" - nickname to match
  1053. * "usage" - certificate usage to match
  1054. * "validOnly" - only return certs that are curently valid
  1055. * "proto_win" - window handle passed to pkcs11
  1056. */
  1057. CERTCertificate *CERT_FindUserCertByUsage(CERTCertDBHandle *handle,
  1058. const char *nickname,
  1059. SECCertUsage usage, PRBool validOnly,
  1060. void *proto_win);
  1061. /*
  1062. * Filter a list of certificates, removing those certs that do not have
  1063. * one of the named CA certs somewhere in their cert chain.
  1064. *
  1065. * "certList" - the list of certificates to filter
  1066. * "nCANames" - number of CA names
  1067. * "caNames" - array of CA names in string(rfc 1485) form
  1068. * "usage" - what use the certs are for, this is used when
  1069. * selecting CA certs
  1070. */
  1071. SECStatus CERT_FilterCertListByCANames(CERTCertList *certList, int nCANames,
  1072. char **caNames, SECCertUsage usage);
  1073. /*
  1074. * Filter a list of certificates, removing those certs that aren't user certs
  1075. */
  1076. SECStatus CERT_FilterCertListForUserCerts(CERTCertList *certList);
  1077. /*
  1078. * Collect the nicknames from all certs in a CertList. If the cert is not
  1079. * valid, append a string to that nickname.
  1080. *
  1081. * "certList" - the list of certificates
  1082. * "expiredString" - the string to append to the nickname of any expired cert
  1083. * "notYetGoodString" - the string to append to the nickname of any cert
  1084. * that is not yet valid
  1085. */
  1086. CERTCertNicknames *CERT_NicknameStringsFromCertList(CERTCertList *certList,
  1087. char *expiredString,
  1088. char *notYetGoodString);
  1089. /*
  1090. * Extract the nickname from a nickmake string that may have either
  1091. * expiredString or notYetGoodString appended.
  1092. *
  1093. * Args:
  1094. * "namestring" - the string containing the nickname, and possibly
  1095. * one of the validity label strings
  1096. * "expiredString" - the expired validity label string
  1097. * "notYetGoodString" - the not yet good validity label string
  1098. *
  1099. * Returns the raw nickname
  1100. */
  1101. char *CERT_ExtractNicknameString(char *namestring, char *expiredString,
  1102. char *notYetGoodString);
  1103. /*
  1104. * Given a certificate, return a string containing the nickname, and possibly
  1105. * one of the validity strings, based on the current validity state of the
  1106. * certificate.
  1107. *
  1108. * "arena" - arena to allocate returned string from. If NULL, then heap
  1109. * is used.
  1110. * "cert" - the cert to get nickname from
  1111. * "expiredString" - the string to append to the nickname if the cert is
  1112. * expired.
  1113. * "notYetGoodString" - the string to append to the nickname if the cert is
  1114. * not yet good.
  1115. */
  1116. char *CERT_GetCertNicknameWithValidity(PLArenaPool *arena,
  1117. CERTCertificate *cert,
  1118. char *expiredString,
  1119. char *notYetGoodString);
  1120. /*
  1121. * Return the string representation of a DER encoded distinguished name
  1122. * "dername" - The DER encoded name to convert
  1123. */
  1124. char *CERT_DerNameToAscii(SECItem *dername);
  1125. /*
  1126. * Supported usage values and types:
  1127. * certUsageSSLClient
  1128. * certUsageSSLServer
  1129. * certUsageSSLServerWithStepUp
  1130. * certUsageEmailSigner
  1131. * certUsageEmailRecipient
  1132. * certUsageObjectSigner
  1133. */
  1134. CERTCertificate *CERT_FindMatchingCert(CERTCertDBHandle *handle,
  1135. SECItem *derName, CERTCertOwner owner,
  1136. SECCertUsage usage, PRBool preferTrusted,
  1137. PRTime validTime, PRBool validOnly);
  1138. /*
  1139. * Acquire the global lock on the cert database.
  1140. * This lock is currently used for the following operations:
  1141. * adding or deleting a cert to either the temp or perm databases
  1142. * converting a temp to perm or perm to temp
  1143. * changing(maybe just adding?) the trust of a cert
  1144. * adjusting the reference count of a cert
  1145. */
  1146. void CERT_LockDB(CERTCertDBHandle *handle);
  1147. /*
  1148. * Free the global cert database lock.
  1149. */
  1150. void CERT_UnlockDB(CERTCertDBHandle *handle);
  1151. /*
  1152. * Get the certificate status checking configuratino data for
  1153. * the certificate database
  1154. */
  1155. CERTStatusConfig *CERT_GetStatusConfig(CERTCertDBHandle *handle);
  1156. /*
  1157. * Set the certificate status checking information for the
  1158. * database. The input structure becomes part of the certificate
  1159. * database and will be freed by calling the 'Destroy' function in
  1160. * the configuration object.
  1161. */
  1162. void CERT_SetStatusConfig(CERTCertDBHandle *handle, CERTStatusConfig *config);
  1163. /*
  1164. * Acquire the cert reference count lock
  1165. * There is currently one global lock for all certs, but I'm putting a cert
  1166. * arg here so that it will be easy to make it per-cert in the future if
  1167. * that turns out to be necessary.
  1168. */
  1169. void CERT_LockCertRefCount(CERTCertificate *cert);
  1170. /*
  1171. * Release the cert reference count lock
  1172. */
  1173. void CERT_UnlockCertRefCount(CERTCertificate *cert);
  1174. /*
  1175. * Digest the cert's subject public key using the specified algorithm.
  1176. * NOTE: this digests the value of the BIT STRING subjectPublicKey (excluding
  1177. * the tag, length, and number of unused bits) rather than the whole
  1178. * subjectPublicKeyInfo field.
  1179. *
  1180. * The necessary storage for the digest data is allocated. If "fill" is
  1181. * non-null, the data is put there, otherwise a SECItem is allocated.
  1182. * Allocation from "arena" if it is non-null, heap otherwise. Any problem
  1183. * results in a NULL being returned (and an appropriate error set).
  1184. */
  1185. extern SECItem *CERT_GetSubjectPublicKeyDigest(PLArenaPool *arena,
  1186. const CERTCertificate *cert,
  1187. SECOidTag digestAlg,
  1188. SECItem *fill);
  1189. /*
  1190. * Digest the cert's subject name using the specified algorithm.
  1191. */
  1192. extern SECItem *CERT_GetSubjectNameDigest(PLArenaPool *arena,
  1193. const CERTCertificate *cert,
  1194. SECOidTag digestAlg, SECItem *fill);
  1195. SECStatus CERT_CheckCRL(CERTCertificate *cert, CERTCertificate *issuer,
  1196. const SECItem *dp, PRTime t, void *wincx);
  1197. /*
  1198. * Add a CERTNameConstraint to the CERTNameConstraint list
  1199. */
  1200. extern CERTNameConstraint *CERT_AddNameConstraint(
  1201. CERTNameConstraint *list, CERTNameConstraint *constraint);
  1202. /*
  1203. * Allocate space and copy CERTNameConstraint from src to dest.
  1204. * Arena is used to allocate result(if dest eq NULL) and its members
  1205. * SECItem data.
  1206. */
  1207. extern CERTNameConstraint *CERT_CopyNameConstraint(PLArenaPool *arena,
  1208. CERTNameConstraint *dest,
  1209. CERTNameConstraint *src);
  1210. /*
  1211. * Verify name against all the constraints relevant to that type of
  1212. * the name.
  1213. */
  1214. extern SECStatus CERT_CheckNameSpace(PLArenaPool *arena,
  1215. const CERTNameConstraints *constraints,
  1216. const CERTGeneralName *currentName);
  1217. /*
  1218. * Extract and allocate the name constraints extension from the CA cert.
  1219. * If the certificate contains no name constraints extension, but
  1220. * CERT_GetImposedNameConstraints returns a name constraints extension
  1221. * for the subject of the certificate, then that extension will be returned.
  1222. */
  1223. extern SECStatus CERT_FindNameConstraintsExten(
  1224. PLArenaPool *arena, CERTCertificate *cert,
  1225. CERTNameConstraints **constraints);
  1226. /*
  1227. * Initialize a new GERTGeneralName fields (link)
  1228. */
  1229. extern CERTGeneralName *CERT_NewGeneralName(PLArenaPool *arena,
  1230. CERTGeneralNameType type);
  1231. /*
  1232. * Lookup a CERTGeneralNameType constant by its human readable string.
  1233. */
  1234. extern CERTGeneralNameType CERT_GetGeneralNameTypeFromString(
  1235. const char *string);
  1236. /*
  1237. * PKIX extension encoding routines
  1238. */
  1239. extern SECStatus CERT_EncodePolicyConstraintsExtension(
  1240. PLArenaPool *arena, CERTCertificatePolicyConstraints *constr,
  1241. SECItem *dest);
  1242. extern SECStatus CERT_EncodeInhibitAnyExtension(
  1243. PLArenaPool *arena, CERTCertificateInhibitAny *inhibitAny, SECItem *dest);
  1244. extern SECStatus CERT_EncodePolicyMappingExtension(
  1245. PLArenaPool *arena, CERTCertificatePolicyMappings *maps, SECItem *dest);
  1246. extern SECStatus CERT_EncodeInfoAccessExtension(PLArenaPool *arena,
  1247. CERTAuthInfoAccess **info,
  1248. SECItem *dest);
  1249. extern SECStatus CERT_EncodeUserNotice(PLArenaPool *arena,
  1250. CERTUserNotice *notice, SECItem *dest);
  1251. extern SECStatus CERT_EncodeDisplayText(PLArenaPool *arena, SECItem *text,
  1252. SECItem *dest);
  1253. extern SECStatus CERT_EncodeCertPoliciesExtension(PLArenaPool *arena,
  1254. CERTPolicyInfo **info,
  1255. SECItem *dest);
  1256. extern SECStatus CERT_EncodeNoticeReference(PLArenaPool *arena,
  1257. CERTNoticeReference *reference,
  1258. SECItem *dest);
  1259. /*
  1260. * Returns a pointer to a static structure.
  1261. */
  1262. extern const CERTRevocationFlags *CERT_GetPKIXVerifyNistRevocationPolicy(void);
  1263. /*
  1264. * Returns a pointer to a static structure.
  1265. */
  1266. extern const CERTRevocationFlags *CERT_GetClassicOCSPEnabledSoftFailurePolicy(
  1267. void);
  1268. /*
  1269. * Returns a pointer to a static structure.
  1270. */
  1271. extern const CERTRevocationFlags *CERT_GetClassicOCSPEnabledHardFailurePolicy(
  1272. void);
  1273. /*
  1274. * Returns a pointer to a static structure.
  1275. */
  1276. extern const CERTRevocationFlags *CERT_GetClassicOCSPDisabledPolicy(void);
  1277. /*
  1278. * Verify a Cert with libpkix
  1279. * paramsIn control the verification options. If a value isn't specified
  1280. * in paramsIn, it reverts to the application default.
  1281. * paramsOut specifies the parameters the caller would like to get back.
  1282. * the caller may pass NULL, in which case no parameters are returned.
  1283. */
  1284. extern SECStatus CERT_PKIXVerifyCert(CERTCertificate *cert,
  1285. SECCertificateUsage usages,
  1286. CERTValInParam *paramsIn,
  1287. CERTValOutParam *paramsOut, void *wincx);
  1288. /* Makes old cert validation APIs(CERT_VerifyCert, CERT_VerifyCertificate)
  1289. * to use libpkix validation engine. The function should be called ones at
  1290. * application initialization time.
  1291. * Function is not thread safe.*/
  1292. extern SECStatus CERT_SetUsePKIXForValidation(PRBool enable);
  1293. /* The function return PR_TRUE if cert validation should use
  1294. * libpkix cert validation engine. */
  1295. extern PRBool CERT_GetUsePKIXForValidation(void);
  1296. /*
  1297. * Allocate a parameter container of type CERTRevocationFlags,
  1298. * and allocate the inner arrays of the given sizes.
  1299. * To cleanup call CERT_DestroyCERTRevocationFlags.
  1300. */
  1301. extern CERTRevocationFlags *CERT_AllocCERTRevocationFlags(
  1302. PRUint32 number_leaf_methods, PRUint32 number_leaf_pref_methods,
  1303. PRUint32 number_chain_methods, PRUint32 number_chain_pref_methods);
  1304. /*
  1305. * Destroy the arrays inside flags,
  1306. * and destroy the object pointed to by flags, too.
  1307. */
  1308. extern void CERT_DestroyCERTRevocationFlags(CERTRevocationFlags *flags);
  1309. /*
  1310. * Get istemp and isperm fields from a cert in a thread safe way.
  1311. */
  1312. extern SECStatus CERT_GetCertIsTemp(const CERTCertificate *cert, PRBool *istemp);
  1313. extern SECStatus CERT_GetCertIsPerm(const CERTCertificate *cert, PRBool *isperm);
  1314. SEC_END_PROTOS
  1315. #endif /* _CERT_H_ */