PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/include/polarssl/x509.h

https://github.com/leg0/polarssl
C Header | 764 lines | 254 code | 93 blank | 417 comment | 0 complexity | 553690526712a5de0bda71d31770a2b2 MD5 | raw file
Possible License(s): GPL-2.0
  1. /**
  2. * \file x509.h
  3. *
  4. * \brief X.509 certificate and private key decoding
  5. *
  6. * Copyright (C) 2006-2011, Brainspark B.V.
  7. *
  8. * This file is part of PolarSSL (http://www.polarssl.org)
  9. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  10. *
  11. * All rights reserved.
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License along
  24. * with this program; if not, write to the Free Software Foundation, Inc.,
  25. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  26. */
  27. #ifndef POLARSSL_X509_H
  28. #define POLARSSL_X509_H
  29. #include "asn1.h"
  30. #include "rsa.h"
  31. #include "dhm.h"
  32. /**
  33. * \addtogroup x509_module
  34. * \{
  35. */
  36. /**
  37. * \name X509 Error codes
  38. * \{
  39. */
  40. #define POLARSSL_ERR_X509_FEATURE_UNAVAILABLE -0x2080 /**< Unavailable feature, e.g. RSA hashing/encryption combination. */
  41. #define POLARSSL_ERR_X509_CERT_INVALID_PEM -0x2100 /**< The PEM-encoded certificate contains invalid elements, e.g. invalid character. */
  42. #define POLARSSL_ERR_X509_CERT_INVALID_FORMAT -0x2180 /**< The certificate format is invalid, e.g. different type expected. */
  43. #define POLARSSL_ERR_X509_CERT_INVALID_VERSION -0x2200 /**< The certificate version element is invalid. */
  44. #define POLARSSL_ERR_X509_CERT_INVALID_SERIAL -0x2280 /**< The serial tag or value is invalid. */
  45. #define POLARSSL_ERR_X509_CERT_INVALID_ALG -0x2300 /**< The algorithm tag or value is invalid. */
  46. #define POLARSSL_ERR_X509_CERT_INVALID_NAME -0x2380 /**< The name tag or value is invalid. */
  47. #define POLARSSL_ERR_X509_CERT_INVALID_DATE -0x2400 /**< The date tag or value is invalid. */
  48. #define POLARSSL_ERR_X509_CERT_INVALID_PUBKEY -0x2480 /**< The pubkey tag or value is invalid (only RSA is supported). */
  49. #define POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE -0x2500 /**< The signature tag or value invalid. */
  50. #define POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS -0x2580 /**< The extension tag or value is invalid. */
  51. #define POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION -0x2600 /**< Certificate or CRL has an unsupported version number. */
  52. #define POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG -0x2680 /**< Signature algorithm (oid) is unsupported. */
  53. #define POLARSSL_ERR_X509_UNKNOWN_PK_ALG -0x2700 /**< Key algorithm is unsupported (only RSA is supported). */
  54. #define POLARSSL_ERR_X509_CERT_SIG_MISMATCH -0x2780 /**< Certificate signature algorithms do not match. (see \c ::x509_cert sig_oid) */
  55. #define POLARSSL_ERR_X509_CERT_VERIFY_FAILED -0x2800 /**< Certificate verification failed, e.g. CRL, CA or signature check failed. */
  56. #define POLARSSL_ERR_X509_KEY_INVALID_VERSION -0x2880 /**< Unsupported RSA key version */
  57. #define POLARSSL_ERR_X509_KEY_INVALID_FORMAT -0x2900 /**< Invalid RSA key tag or value. */
  58. #define POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT -0x2980 /**< Format not recognized as DER or PEM. */
  59. #define POLARSSL_ERR_X509_INVALID_INPUT -0x2A00 /**< Input invalid. */
  60. #define POLARSSL_ERR_X509_MALLOC_FAILED -0x2A80 /**< Allocation of memory failed. */
  61. #define POLARSSL_ERR_X509_FILE_IO_ERROR -0x2B00 /**< Read/write of file failed. */
  62. /* \} name */
  63. /**
  64. * \name X509 Verify codes
  65. * \{
  66. */
  67. #define BADCERT_EXPIRED 0x01 /**< The certificate validity has expired. */
  68. #define BADCERT_REVOKED 0x02 /**< The certificate has been revoked (is on a CRL). */
  69. #define BADCERT_CN_MISMATCH 0x04 /**< The certificate Common Name (CN) does not match with the expected CN. */
  70. #define BADCERT_NOT_TRUSTED 0x08 /**< The certificate is not correctly signed by the trusted CA. */
  71. #define BADCRL_NOT_TRUSTED 0x10 /**< CRL is not correctly signed by the trusted CA. */
  72. #define BADCRL_EXPIRED 0x20 /**< CRL is expired. */
  73. #define BADCERT_MISSING 0x40 /**< Certificate was missing. */
  74. #define BADCERT_SKIP_VERIFY 0x80 /**< Certificate verification was skipped. */
  75. #define BADCERT_OTHER 0x0100 /**< Other reason (can be used by verify callback) */
  76. /* \} name */
  77. /* \} addtogroup x509_module */
  78. /*
  79. * various object identifiers
  80. */
  81. #define X520_COMMON_NAME 3
  82. #define X520_COUNTRY 6
  83. #define X520_LOCALITY 7
  84. #define X520_STATE 8
  85. #define X520_ORGANIZATION 10
  86. #define X520_ORG_UNIT 11
  87. #define PKCS9_EMAIL 1
  88. #define X509_OUTPUT_DER 0x01
  89. #define X509_OUTPUT_PEM 0x02
  90. #define PEM_LINE_LENGTH 72
  91. #define X509_ISSUER 0x01
  92. #define X509_SUBJECT 0x02
  93. #define OID_X520 "\x55\x04"
  94. #define OID_CN OID_X520 "\x03"
  95. #define OID_COUNTRY OID_X520 "\x06"
  96. #define OID_LOCALITY OID_X520 "\x07"
  97. #define OID_STATE OID_X520 "\x08"
  98. #define OID_ORGANIZATION OID_X520 "\x0A"
  99. #define OID_ORG_UNIT OID_X520 "\x0B"
  100. #define OID_PKCS1 "\x2A\x86\x48\x86\xF7\x0D\x01\x01"
  101. #define OID_PKCS1_RSA OID_PKCS1 "\x01"
  102. #define OID_PKCS1_SHA1 OID_PKCS1 "\x05"
  103. #define OID_RSA_SHA_OBS "\x2B\x0E\x03\x02\x1D"
  104. #define OID_PKCS9 "\x2A\x86\x48\x86\xF7\x0D\x01\x09"
  105. #define OID_PKCS9_EMAIL OID_PKCS9 "\x01"
  106. /** ISO arc for standard certificate and CRL extensions */
  107. #define OID_ID_CE "\x55\x1D" /**< id-ce OBJECT IDENTIFIER ::= {joint-iso-ccitt(2) ds(5) 29} */
  108. /**
  109. * Private Internet Extensions
  110. * { iso(1) identified-organization(3) dod(6) internet(1)
  111. * security(5) mechanisms(5) pkix(7) }
  112. */
  113. #define OID_PKIX "\x2B\x06\x01\x05\x05\x07"
  114. /*
  115. * OIDs for standard certificate extensions
  116. */
  117. #define OID_AUTHORITY_KEY_IDENTIFIER OID_ID_CE "\x23" /**< id-ce-authorityKeyIdentifier OBJECT IDENTIFIER ::= { id-ce 35 } */
  118. #define OID_SUBJECT_KEY_IDENTIFIER OID_ID_CE "\x0E" /**< id-ce-subjectKeyIdentifier OBJECT IDENTIFIER ::= { id-ce 14 } */
  119. #define OID_KEY_USAGE OID_ID_CE "\x0F" /**< id-ce-keyUsage OBJECT IDENTIFIER ::= { id-ce 15 } */
  120. #define OID_CERTIFICATE_POLICIES OID_ID_CE "\x20" /**< id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 } */
  121. #define OID_POLICY_MAPPINGS OID_ID_CE "\x21" /**< id-ce-policyMappings OBJECT IDENTIFIER ::= { id-ce 33 } */
  122. #define OID_SUBJECT_ALT_NAME OID_ID_CE "\x11" /**< id-ce-subjectAltName OBJECT IDENTIFIER ::= { id-ce 17 } */
  123. #define OID_ISSUER_ALT_NAME OID_ID_CE "\x12" /**< id-ce-issuerAltName OBJECT IDENTIFIER ::= { id-ce 18 } */
  124. #define OID_SUBJECT_DIRECTORY_ATTRS OID_ID_CE "\x09" /**< id-ce-subjectDirectoryAttributes OBJECT IDENTIFIER ::= { id-ce 9 } */
  125. #define OID_BASIC_CONSTRAINTS OID_ID_CE "\x13" /**< id-ce-basicConstraints OBJECT IDENTIFIER ::= { id-ce 19 } */
  126. #define OID_NAME_CONSTRAINTS OID_ID_CE "\x1E" /**< id-ce-nameConstraints OBJECT IDENTIFIER ::= { id-ce 30 } */
  127. #define OID_POLICY_CONSTRAINTS OID_ID_CE "\x24" /**< id-ce-policyConstraints OBJECT IDENTIFIER ::= { id-ce 36 } */
  128. #define OID_EXTENDED_KEY_USAGE OID_ID_CE "\x25" /**< id-ce-extKeyUsage OBJECT IDENTIFIER ::= { id-ce 37 } */
  129. #define OID_CRL_DISTRIBUTION_POINTS OID_ID_CE "\x1F" /**< id-ce-cRLDistributionPoints OBJECT IDENTIFIER ::= { id-ce 31 } */
  130. #define OID_INIHIBIT_ANYPOLICY OID_ID_CE "\x36" /**< id-ce-inhibitAnyPolicy OBJECT IDENTIFIER ::= { id-ce 54 } */
  131. #define OID_FRESHEST_CRL OID_ID_CE "\x2E" /**< id-ce-freshestCRL OBJECT IDENTIFIER ::= { id-ce 46 } */
  132. /*
  133. * X.509 v3 Key Usage Extension flags
  134. */
  135. #define KU_DIGITAL_SIGNATURE (0x80) /* bit 0 */
  136. #define KU_NON_REPUDIATION (0x40) /* bit 1 */
  137. #define KU_KEY_ENCIPHERMENT (0x20) /* bit 2 */
  138. #define KU_DATA_ENCIPHERMENT (0x10) /* bit 3 */
  139. #define KU_KEY_AGREEMENT (0x08) /* bit 4 */
  140. #define KU_KEY_CERT_SIGN (0x04) /* bit 5 */
  141. #define KU_CRL_SIGN (0x02) /* bit 6 */
  142. /*
  143. * X.509 v3 Extended key usage OIDs
  144. */
  145. #define OID_ANY_EXTENDED_KEY_USAGE OID_EXTENDED_KEY_USAGE "\x00" /**< anyExtendedKeyUsage OBJECT IDENTIFIER ::= { id-ce-extKeyUsage 0 } */
  146. #define OID_KP OID_PKIX "\x03" /**< id-kp OBJECT IDENTIFIER ::= { id-pkix 3 } */
  147. #define OID_SERVER_AUTH OID_KP "\x01" /**< id-kp-serverAuth OBJECT IDENTIFIER ::= { id-kp 1 } */
  148. #define OID_CLIENT_AUTH OID_KP "\x02" /**< id-kp-clientAuth OBJECT IDENTIFIER ::= { id-kp 2 } */
  149. #define OID_CODE_SIGNING OID_KP "\x03" /**< id-kp-codeSigning OBJECT IDENTIFIER ::= { id-kp 3 } */
  150. #define OID_EMAIL_PROTECTION OID_KP "\x04" /**< id-kp-emailProtection OBJECT IDENTIFIER ::= { id-kp 4 } */
  151. #define OID_TIME_STAMPING OID_KP "\x08" /**< id-kp-timeStamping OBJECT IDENTIFIER ::= { id-kp 8 } */
  152. #define OID_OCSP_SIGNING OID_KP "\x09" /**< id-kp-OCSPSigning OBJECT IDENTIFIER ::= { id-kp 9 } */
  153. #define STRING_SERVER_AUTH "TLS Web Server Authentication"
  154. #define STRING_CLIENT_AUTH "TLS Web Client Authentication"
  155. #define STRING_CODE_SIGNING "Code Signing"
  156. #define STRING_EMAIL_PROTECTION "E-mail Protection"
  157. #define STRING_TIME_STAMPING "Time Stamping"
  158. #define STRING_OCSP_SIGNING "OCSP Signing"
  159. /*
  160. * OIDs for CRL extensions
  161. */
  162. #define OID_PRIVATE_KEY_USAGE_PERIOD OID_ID_CE "\x10"
  163. #define OID_CRL_NUMBER OID_ID_CE "\x14" /**< id-ce-cRLNumber OBJECT IDENTIFIER ::= { id-ce 20 } */
  164. /*
  165. * Netscape certificate extensions
  166. */
  167. #define OID_NETSCAPE "\x60\x86\x48\x01\x86\xF8\x42" /**< Netscape OID */
  168. #define OID_NS_CERT OID_NETSCAPE "\x01"
  169. #define OID_NS_CERT_TYPE OID_NS_CERT "\x01"
  170. #define OID_NS_BASE_URL OID_NS_CERT "\x02"
  171. #define OID_NS_REVOCATION_URL OID_NS_CERT "\x03"
  172. #define OID_NS_CA_REVOCATION_URL OID_NS_CERT "\x04"
  173. #define OID_NS_RENEWAL_URL OID_NS_CERT "\x07"
  174. #define OID_NS_CA_POLICY_URL OID_NS_CERT "\x08"
  175. #define OID_NS_SSL_SERVER_NAME OID_NS_CERT "\x0C"
  176. #define OID_NS_COMMENT OID_NS_CERT "\x0D"
  177. #define OID_NS_DATA_TYPE OID_NETSCAPE "\x02"
  178. #define OID_NS_CERT_SEQUENCE OID_NS_DATA_TYPE "\x05"
  179. /*
  180. * Netscape certificate types
  181. * (http://www.mozilla.org/projects/security/pki/nss/tech-notes/tn3.html)
  182. */
  183. #define NS_CERT_TYPE_SSL_CLIENT (0x80) /* bit 0 */
  184. #define NS_CERT_TYPE_SSL_SERVER (0x40) /* bit 1 */
  185. #define NS_CERT_TYPE_EMAIL (0x20) /* bit 2 */
  186. #define NS_CERT_TYPE_OBJECT_SIGNING (0x10) /* bit 3 */
  187. #define NS_CERT_TYPE_RESERVED (0x08) /* bit 4 */
  188. #define NS_CERT_TYPE_SSL_CA (0x04) /* bit 5 */
  189. #define NS_CERT_TYPE_EMAIL_CA (0x02) /* bit 6 */
  190. #define NS_CERT_TYPE_OBJECT_SIGNING_CA (0x01) /* bit 7 */
  191. #define EXT_AUTHORITY_KEY_IDENTIFIER (1 << 0)
  192. #define EXT_SUBJECT_KEY_IDENTIFIER (1 << 1)
  193. #define EXT_KEY_USAGE (1 << 2)
  194. #define EXT_CERTIFICATE_POLICIES (1 << 3)
  195. #define EXT_POLICY_MAPPINGS (1 << 4)
  196. #define EXT_SUBJECT_ALT_NAME (1 << 5)
  197. #define EXT_ISSUER_ALT_NAME (1 << 6)
  198. #define EXT_SUBJECT_DIRECTORY_ATTRS (1 << 7)
  199. #define EXT_BASIC_CONSTRAINTS (1 << 8)
  200. #define EXT_NAME_CONSTRAINTS (1 << 9)
  201. #define EXT_POLICY_CONSTRAINTS (1 << 10)
  202. #define EXT_EXTENDED_KEY_USAGE (1 << 11)
  203. #define EXT_CRL_DISTRIBUTION_POINTS (1 << 12)
  204. #define EXT_INIHIBIT_ANYPOLICY (1 << 13)
  205. #define EXT_FRESHEST_CRL (1 << 14)
  206. #define EXT_NS_CERT_TYPE (1 << 16)
  207. /*
  208. * Storage format identifiers
  209. * Recognized formats: PEM and DER
  210. */
  211. #define X509_FORMAT_DER 1
  212. #define X509_FORMAT_PEM 2
  213. /**
  214. * \addtogroup x509_module
  215. * \{ */
  216. /**
  217. * \name Structures for parsing X.509 certificates and CRLs
  218. * \{
  219. */
  220. /**
  221. * Type-length-value structure that allows for ASN1 using DER.
  222. */
  223. typedef asn1_buf x509_buf;
  224. /**
  225. * Container for ASN1 bit strings.
  226. */
  227. typedef asn1_bitstring x509_bitstring;
  228. /**
  229. * Container for ASN1 named information objects.
  230. * It allows for Relative Distinguished Names (e.g. cn=polarssl,ou=code,etc.).
  231. */
  232. typedef struct _x509_name
  233. {
  234. x509_buf oid; /**< The object identifier. */
  235. x509_buf val; /**< The named value. */
  236. struct _x509_name *next; /**< The next named information object. */
  237. }
  238. x509_name;
  239. /**
  240. * Container for a sequence of ASN.1 items
  241. */
  242. typedef asn1_sequence x509_sequence;
  243. /** Container for date and time (precision in seconds). */
  244. typedef struct _x509_time
  245. {
  246. int year, mon, day; /**< Date. */
  247. int hour, min, sec; /**< Time. */
  248. }
  249. x509_time;
  250. /**
  251. * Container for an X.509 certificate. The certificate may be chained.
  252. */
  253. typedef struct _x509_cert
  254. {
  255. x509_buf raw; /**< The raw certificate data (DER). */
  256. x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
  257. int version; /**< The X.509 version. (0=v1, 1=v2, 2=v3) */
  258. x509_buf serial; /**< Unique id for certificate issued by a specific CA. */
  259. x509_buf sig_oid1; /**< Signature algorithm, e.g. sha1RSA */
  260. x509_buf issuer_raw; /**< The raw issuer data (DER). Used for quick comparison. */
  261. x509_buf subject_raw; /**< The raw subject data (DER). Used for quick comparison. */
  262. x509_name issuer; /**< The parsed issuer data (named information object). */
  263. x509_name subject; /**< The parsed subject data (named information object). */
  264. x509_time valid_from; /**< Start time of certificate validity. */
  265. x509_time valid_to; /**< End time of certificate validity. */
  266. x509_buf pk_oid; /**< Subject public key info. Includes the public key algorithm and the key itself. */
  267. rsa_context rsa; /**< Container for the RSA context. Only RSA is supported for public keys at this time. */
  268. x509_buf issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */
  269. x509_buf subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */
  270. x509_buf v3_ext; /**< Optional X.509 v3 extensions. Only Basic Contraints are supported at this time. */
  271. x509_sequence subject_alt_names; /**< Optional list of Subject Alternative Names (Only dNSName supported). */
  272. int ext_types; /**< Bit string containing detected and parsed extensions */
  273. int ca_istrue; /**< Optional Basic Constraint extension value: 1 if this certificate belongs to a CA, 0 otherwise. */
  274. int max_pathlen; /**< Optional Basic Constraint extension value: The maximum path length to the root certificate. Path length is 1 higher than RFC 5280 'meaning', so 1+ */
  275. unsigned char key_usage; /**< Optional key usage extension value: See the values below */
  276. x509_sequence ext_key_usage; /**< Optional list of extended key usage OIDs. */
  277. unsigned char ns_cert_type; /**< Optional Netscape certificate type extension value: See the values below */
  278. x509_buf sig_oid2; /**< Signature algorithm. Must match sig_oid1. */
  279. x509_buf sig; /**< Signature: hash of the tbs part signed with the private key. */
  280. int sig_alg; /**< Internal representation of the signature algorithm, e.g. SIG_RSA_MD2 */
  281. struct _x509_cert *next; /**< Next certificate in the CA-chain. */
  282. }
  283. x509_cert;
  284. /**
  285. * Certificate revocation list entry.
  286. * Contains the CA-specific serial numbers and revocation dates.
  287. */
  288. typedef struct _x509_crl_entry
  289. {
  290. x509_buf raw;
  291. x509_buf serial;
  292. x509_time revocation_date;
  293. x509_buf entry_ext;
  294. struct _x509_crl_entry *next;
  295. }
  296. x509_crl_entry;
  297. /**
  298. * Certificate revocation list structure.
  299. * Every CRL may have multiple entries.
  300. */
  301. typedef struct _x509_crl
  302. {
  303. x509_buf raw; /**< The raw certificate data (DER). */
  304. x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
  305. int version;
  306. x509_buf sig_oid1;
  307. x509_buf issuer_raw; /**< The raw issuer data (DER). */
  308. x509_name issuer; /**< The parsed issuer data (named information object). */
  309. x509_time this_update;
  310. x509_time next_update;
  311. x509_crl_entry entry; /**< The CRL entries containing the certificate revocation times for this CA. */
  312. x509_buf crl_ext;
  313. x509_buf sig_oid2;
  314. x509_buf sig;
  315. int sig_alg;
  316. struct _x509_crl *next;
  317. }
  318. x509_crl;
  319. /** \} name Structures for parsing X.509 certificates and CRLs */
  320. /** \} addtogroup x509_module */
  321. /**
  322. * \name Structures for writing X.509 certificates.
  323. * XvP: commented out as they are not used.
  324. * - <tt>typedef struct _x509_node x509_node;</tt>
  325. * - <tt>typedef struct _x509_raw x509_raw;</tt>
  326. */
  327. /*
  328. typedef struct _x509_node
  329. {
  330. unsigned char *data;
  331. unsigned char *p;
  332. unsigned char *end;
  333. size_t len;
  334. }
  335. x509_node;
  336. typedef struct _x509_raw
  337. {
  338. x509_node raw;
  339. x509_node tbs;
  340. x509_node version;
  341. x509_node serial;
  342. x509_node tbs_signalg;
  343. x509_node issuer;
  344. x509_node validity;
  345. x509_node subject;
  346. x509_node subpubkey;
  347. x509_node signalg;
  348. x509_node sign;
  349. }
  350. x509_raw;
  351. */
  352. #ifdef __cplusplus
  353. extern "C" {
  354. #endif
  355. /**
  356. * \name Functions to read in DHM parameters, a certificate, CRL or private RSA key
  357. * \{
  358. */
  359. /** \ingroup x509_module */
  360. /**
  361. * \brief Parse one or more certificates and add them
  362. * to the chained list. Parses permissively. If some
  363. * certificates can be parsed, the result is the number
  364. * of failed certificates it encountered. If none complete
  365. * correctly, the first error is returned.
  366. *
  367. * \param chain points to the start of the chain
  368. * \param buf buffer holding the certificate data
  369. * \param buflen size of the buffer
  370. *
  371. * \return 0 if all certificates parsed successfully, a positive number
  372. * if partly successful or a specific X509 or PEM error code
  373. */
  374. int x509parse_crt( x509_cert *chain, const unsigned char *buf, size_t buflen );
  375. /** \ingroup x509_module */
  376. /**
  377. * \brief Load one or more certificates and add them
  378. * to the chained list. Parses permissively. If some
  379. * certificates can be parsed, the result is the number
  380. * of failed certificates it encountered. If none complete
  381. * correctly, the first error is returned.
  382. *
  383. * \param chain points to the start of the chain
  384. * \param path filename to read the certificates from
  385. *
  386. * \return 0 if all certificates parsed successfully, a positive number
  387. * if partly successful or a specific X509 or PEM error code
  388. */
  389. int x509parse_crtfile( x509_cert *chain, const char *path );
  390. /** \ingroup x509_module */
  391. /**
  392. * \brief Load one or more certificate files from a path and add them
  393. * to the chained list. Parses permissively. If some
  394. * certificates can be parsed, the result is the number
  395. * of failed certificates it encountered. If none complete
  396. * correctly, the first error is returned.
  397. *
  398. * \param chain points to the start of the chain
  399. * \param path directory / folder to read the certificate files from
  400. *
  401. * \return 0 if all certificates parsed successfully, a positive number
  402. * if partly successful or a specific X509 or PEM error code
  403. */
  404. int x509parse_crtpath( x509_cert *chain, const char *path );
  405. /** \ingroup x509_module */
  406. /**
  407. * \brief Parse one or more CRLs and add them
  408. * to the chained list
  409. *
  410. * \param chain points to the start of the chain
  411. * \param buf buffer holding the CRL data
  412. * \param buflen size of the buffer
  413. *
  414. * \return 0 if successful, or a specific X509 or PEM error code
  415. */
  416. int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen );
  417. /** \ingroup x509_module */
  418. /**
  419. * \brief Load one or more CRLs and add them
  420. * to the chained list
  421. *
  422. * \param chain points to the start of the chain
  423. * \param path filename to read the CRLs from
  424. *
  425. * \return 0 if successful, or a specific X509 or PEM error code
  426. */
  427. int x509parse_crlfile( x509_crl *chain, const char *path );
  428. /** \ingroup x509_module */
  429. /**
  430. * \brief Parse a private RSA key
  431. *
  432. * \param rsa RSA context to be initialized
  433. * \param key input buffer
  434. * \param keylen size of the buffer
  435. * \param pwd password for decryption (optional)
  436. * \param pwdlen size of the password
  437. *
  438. * \return 0 if successful, or a specific X509 or PEM error code
  439. */
  440. int x509parse_key( rsa_context *rsa,
  441. const unsigned char *key, size_t keylen,
  442. const unsigned char *pwd, size_t pwdlen );
  443. /** \ingroup x509_module */
  444. /**
  445. * \brief Load and parse a private RSA key
  446. *
  447. * \param rsa RSA context to be initialized
  448. * \param path filename to read the private key from
  449. * \param password password to decrypt the file (can be NULL)
  450. *
  451. * \return 0 if successful, or a specific X509 or PEM error code
  452. */
  453. int x509parse_keyfile( rsa_context *rsa, const char *path,
  454. const char *password );
  455. /** \ingroup x509_module */
  456. /**
  457. * \brief Parse a public RSA key
  458. *
  459. * \param rsa RSA context to be initialized
  460. * \param key input buffer
  461. * \param keylen size of the buffer
  462. *
  463. * \return 0 if successful, or a specific X509 or PEM error code
  464. */
  465. int x509parse_public_key( rsa_context *rsa,
  466. const unsigned char *key, size_t keylen );
  467. /** \ingroup x509_module */
  468. /**
  469. * \brief Load and parse a public RSA key
  470. *
  471. * \param rsa RSA context to be initialized
  472. * \param path filename to read the private key from
  473. *
  474. * \return 0 if successful, or a specific X509 or PEM error code
  475. */
  476. int x509parse_public_keyfile( rsa_context *rsa, const char *path );
  477. /** \ingroup x509_module */
  478. /**
  479. * \brief Parse DHM parameters
  480. *
  481. * \param dhm DHM context to be initialized
  482. * \param dhmin input buffer
  483. * \param dhminlen size of the buffer
  484. *
  485. * \return 0 if successful, or a specific X509 or PEM error code
  486. */
  487. int x509parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen );
  488. /** \ingroup x509_module */
  489. /**
  490. * \brief Load and parse DHM parameters
  491. *
  492. * \param dhm DHM context to be initialized
  493. * \param path filename to read the DHM Parameters from
  494. *
  495. * \return 0 if successful, or a specific X509 or PEM error code
  496. */
  497. int x509parse_dhmfile( dhm_context *dhm, const char *path );
  498. /** \} name Functions to read in DHM parameters, a certificate, CRL or private RSA key */
  499. /**
  500. * \brief Store the certificate DN in printable form into buf;
  501. * no more than size characters will be written.
  502. *
  503. * \param buf Buffer to write to
  504. * \param size Maximum size of buffer
  505. * \param dn The X509 name to represent
  506. *
  507. * \return The amount of data written to the buffer, or -1 in
  508. * case of an error.
  509. */
  510. int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn );
  511. /**
  512. * \brief Store the certificate serial in printable form into buf;
  513. * no more than size characters will be written.
  514. *
  515. * \param buf Buffer to write to
  516. * \param size Maximum size of buffer
  517. * \param serial The X509 serial to represent
  518. *
  519. * \return The amount of data written to the buffer, or -1 in
  520. * case of an error.
  521. */
  522. int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial );
  523. /**
  524. * \brief Returns an informational string about the
  525. * certificate.
  526. *
  527. * \param buf Buffer to write to
  528. * \param size Maximum size of buffer
  529. * \param prefix A line prefix
  530. * \param crt The X509 certificate to represent
  531. *
  532. * \return The amount of data written to the buffer, or -1 in
  533. * case of an error.
  534. */
  535. int x509parse_cert_info( char *buf, size_t size, const char *prefix,
  536. const x509_cert *crt );
  537. /**
  538. * \brief Returns an informational string about the
  539. * CRL.
  540. *
  541. * \param buf Buffer to write to
  542. * \param size Maximum size of buffer
  543. * \param prefix A line prefix
  544. * \param crl The X509 CRL to represent
  545. *
  546. * \return The amount of data written to the buffer, or -1 in
  547. * case of an error.
  548. */
  549. int x509parse_crl_info( char *buf, size_t size, const char *prefix,
  550. const x509_crl *crl );
  551. /**
  552. * \brief Give an known OID, return its descriptive string.
  553. *
  554. * \param oid buffer containing the oid
  555. *
  556. * \return Return a string if the OID is known,
  557. * or NULL otherwise.
  558. */
  559. const char *x509_oid_get_description( x509_buf *oid );
  560. /**
  561. * \brief Give an OID, return a string version of its OID number.
  562. *
  563. * \param buf Buffer to write to
  564. * \param size Maximum size of buffer
  565. * \param oid Buffer containing the OID
  566. *
  567. * \return The amount of data written to the buffer, or -1 in
  568. * case of an error.
  569. */
  570. int x509_oid_get_numeric_string( char *buf, size_t size, x509_buf *oid );
  571. /**
  572. * \brief Check a given x509_time against the system time and check
  573. * if it is valid.
  574. *
  575. * \param time x509_time to check
  576. *
  577. * \return Return 0 if the x509_time is still valid,
  578. * or 1 otherwise.
  579. */
  580. int x509parse_time_expired( const x509_time *time );
  581. /**
  582. * \name Functions to verify a certificate
  583. * \{
  584. */
  585. /** \ingroup x509_module */
  586. /**
  587. * \brief Verify the certificate signature
  588. *
  589. * The verify callback is a user-supplied callback that
  590. * can clear / modify / add flags for a certificate. If set,
  591. * the verification callback is called for each
  592. * certificate in the chain (from the trust-ca down to the
  593. * presented crt). The parameters for the callback are:
  594. * (void *parameter, x509_cert *crt, int certificate_depth,
  595. * int *flags). With the flags representing current flags for
  596. * that specific certificate and the certificate depth from
  597. * the bottom (Peer cert depth = 0).
  598. *
  599. * All flags left after returning from the callback
  600. * are also returned to the application. The function should
  601. * return 0 for anything but a fatal error.
  602. *
  603. * \param crt a certificate to be verified
  604. * \param trust_ca the trusted CA chain
  605. * \param ca_crl the CRL chain for trusted CA's
  606. * \param cn expected Common Name (can be set to
  607. * NULL if the CN must not be verified)
  608. * \param flags result of the verification
  609. * \param f_vrfy verification function
  610. * \param p_vrfy verification parameter
  611. *
  612. * \return 0 if successful or POLARSSL_ERR_X509_SIG_VERIFY_FAILED,
  613. * in which case *flags will have one or more of
  614. * the following values set:
  615. * BADCERT_EXPIRED --
  616. * BADCERT_REVOKED --
  617. * BADCERT_CN_MISMATCH --
  618. * BADCERT_NOT_TRUSTED
  619. * or another error in case of a fatal error encountered
  620. * during the verification process.
  621. */
  622. int x509parse_verify( x509_cert *crt,
  623. x509_cert *trust_ca,
  624. x509_crl *ca_crl,
  625. const char *cn, int *flags,
  626. int (*f_vrfy)(void *, x509_cert *, int, int *),
  627. void *p_vrfy );
  628. /**
  629. * \brief Verify the certificate signature
  630. *
  631. * \param crt a certificate to be verified
  632. * \param crl the CRL to verify against
  633. *
  634. * \return 1 if the certificate is revoked, 0 otherwise
  635. *
  636. */
  637. int x509parse_revoked( const x509_cert *crt, const x509_crl *crl );
  638. /** \} name Functions to verify a certificate */
  639. /**
  640. * \name Functions to clear a certificate, CRL or private RSA key
  641. * \{
  642. */
  643. /** \ingroup x509_module */
  644. /**
  645. * \brief Unallocate all certificate data
  646. *
  647. * \param crt Certificate chain to free
  648. */
  649. void x509_free( x509_cert *crt );
  650. /** \ingroup x509_module */
  651. /**
  652. * \brief Unallocate all CRL data
  653. *
  654. * \param crl CRL chain to free
  655. */
  656. void x509_crl_free( x509_crl *crl );
  657. /** \} name Functions to clear a certificate, CRL or private RSA key */
  658. /**
  659. * \brief Checkup routine
  660. *
  661. * \return 0 if successful, or 1 if the test failed
  662. */
  663. int x509_self_test( int verbose );
  664. #ifdef __cplusplus
  665. }
  666. #endif
  667. #endif /* x509.h */