x509.h 29 KB

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