x509_crt.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. /**
  2. * \file x509_crt.h
  3. *
  4. * \brief X.509 certificate parsing and writing
  5. */
  6. /*
  7. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  8. * SPDX-License-Identifier: Apache-2.0
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  11. * not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  18. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. *
  22. * This file is part of mbed TLS (https://tls.mbed.org)
  23. */
  24. #ifndef MBEDTLS_X509_CRT_H
  25. #define MBEDTLS_X509_CRT_H
  26. #if !defined(MBEDTLS_CONFIG_FILE)
  27. #include "config.h"
  28. #else
  29. #include MBEDTLS_CONFIG_FILE
  30. #endif
  31. #include "x509.h"
  32. #include "x509_crl.h"
  33. /**
  34. * \addtogroup x509_module
  35. * \{
  36. */
  37. #ifdef __cplusplus
  38. extern "C" {
  39. #endif
  40. /**
  41. * \name Structures and functions for parsing and writing X.509 certificates
  42. * \{
  43. */
  44. /**
  45. * Container for an X.509 certificate. The certificate may be chained.
  46. */
  47. typedef struct mbedtls_x509_crt
  48. {
  49. mbedtls_x509_buf raw; /**< The raw certificate data (DER). */
  50. mbedtls_x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */
  51. int version; /**< The X.509 version. (1=v1, 2=v2, 3=v3) */
  52. mbedtls_x509_buf serial; /**< Unique id for certificate issued by a specific CA. */
  53. mbedtls_x509_buf sig_oid; /**< Signature algorithm, e.g. sha1RSA */
  54. mbedtls_x509_buf issuer_raw; /**< The raw issuer data (DER). Used for quick comparison. */
  55. mbedtls_x509_buf subject_raw; /**< The raw subject data (DER). Used for quick comparison. */
  56. mbedtls_x509_name issuer; /**< The parsed issuer data (named information object). */
  57. mbedtls_x509_name subject; /**< The parsed subject data (named information object). */
  58. mbedtls_x509_time valid_from; /**< Start time of certificate validity. */
  59. mbedtls_x509_time valid_to; /**< End time of certificate validity. */
  60. mbedtls_pk_context pk; /**< Container for the public key context. */
  61. mbedtls_x509_buf issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */
  62. mbedtls_x509_buf subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */
  63. mbedtls_x509_buf v3_ext; /**< Optional X.509 v3 extensions. */
  64. mbedtls_x509_sequence subject_alt_names; /**< Optional list of Subject Alternative Names (Only dNSName supported). */
  65. int ext_types; /**< Bit string containing detected and parsed extensions */
  66. int ca_istrue; /**< Optional Basic Constraint extension value: 1 if this certificate belongs to a CA, 0 otherwise. */
  67. 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+ */
  68. unsigned int key_usage; /**< Optional key usage extension value: See the values in x509.h */
  69. mbedtls_x509_sequence ext_key_usage; /**< Optional list of extended key usage OIDs. */
  70. unsigned char ns_cert_type; /**< Optional Netscape certificate type extension value: See the values in x509.h */
  71. mbedtls_x509_buf sig; /**< Signature: hash of the tbs part signed with the private key. */
  72. mbedtls_md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
  73. mbedtls_pk_type_t sig_pk; /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
  74. void *sig_opts; /**< Signature options to be passed to mbedtls_pk_verify_ext(), e.g. for RSASSA-PSS */
  75. struct mbedtls_x509_crt *next; /**< Next certificate in the CA-chain. */
  76. }
  77. mbedtls_x509_crt;
  78. /**
  79. * Build flag from an algorithm/curve identifier (pk, md, ecp)
  80. * Since 0 is always XXX_NONE, ignore it.
  81. */
  82. #define MBEDTLS_X509_ID_FLAG( id ) ( 1 << ( (id) - 1 ) )
  83. /**
  84. * Security profile for certificate verification.
  85. *
  86. * All lists are bitfields, built by ORing flags from MBEDTLS_X509_ID_FLAG().
  87. */
  88. typedef struct mbedtls_x509_crt_profile
  89. {
  90. uint32_t allowed_mds; /**< MDs for signatures */
  91. uint32_t allowed_pks; /**< PK algs for signatures */
  92. uint32_t allowed_curves; /**< Elliptic curves for ECDSA */
  93. uint32_t rsa_min_bitlen; /**< Minimum size for RSA keys */
  94. }
  95. mbedtls_x509_crt_profile;
  96. #define MBEDTLS_X509_CRT_VERSION_1 0
  97. #define MBEDTLS_X509_CRT_VERSION_2 1
  98. #define MBEDTLS_X509_CRT_VERSION_3 2
  99. #define MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN 32
  100. #define MBEDTLS_X509_RFC5280_UTC_TIME_LEN 15
  101. #if !defined( MBEDTLS_X509_MAX_FILE_PATH_LEN )
  102. #define MBEDTLS_X509_MAX_FILE_PATH_LEN 512
  103. #endif
  104. /**
  105. * Container for writing a certificate (CRT)
  106. */
  107. typedef struct mbedtls_x509write_cert
  108. {
  109. int version;
  110. mbedtls_mpi serial;
  111. mbedtls_pk_context *subject_key;
  112. mbedtls_pk_context *issuer_key;
  113. mbedtls_asn1_named_data *subject;
  114. mbedtls_asn1_named_data *issuer;
  115. mbedtls_md_type_t md_alg;
  116. char not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
  117. char not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
  118. mbedtls_asn1_named_data *extensions;
  119. }
  120. mbedtls_x509write_cert;
  121. /**
  122. * Item in a verification chain: cert and flags for it
  123. */
  124. typedef struct {
  125. mbedtls_x509_crt *crt;
  126. uint32_t flags;
  127. } mbedtls_x509_crt_verify_chain_item;
  128. /**
  129. * Max size of verification chain: end-entity + intermediates + trusted root
  130. */
  131. #define MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
  132. /**
  133. * Verification chain as built by \c mbedtls_crt_verify_chain()
  134. */
  135. typedef struct
  136. {
  137. mbedtls_x509_crt_verify_chain_item items[MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE];
  138. unsigned len;
  139. } mbedtls_x509_crt_verify_chain;
  140. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  141. /**
  142. * \brief Context for resuming X.509 verify operations
  143. */
  144. typedef struct
  145. {
  146. /* for check_signature() */
  147. mbedtls_pk_restart_ctx pk;
  148. /* for find_parent_in() */
  149. mbedtls_x509_crt *parent; /* non-null iff parent_in in progress */
  150. mbedtls_x509_crt *fallback_parent;
  151. int fallback_signature_is_good;
  152. /* for find_parent() */
  153. int parent_is_trusted; /* -1 if find_parent is not in progress */
  154. /* for verify_chain() */
  155. enum {
  156. x509_crt_rs_none,
  157. x509_crt_rs_find_parent,
  158. } in_progress; /* none if no operation is in progress */
  159. int self_cnt;
  160. mbedtls_x509_crt_verify_chain ver_chain;
  161. } mbedtls_x509_crt_restart_ctx;
  162. #else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
  163. /* Now we can declare functions that take a pointer to that */
  164. typedef void mbedtls_x509_crt_restart_ctx;
  165. #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
  166. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  167. /**
  168. * Default security profile. Should provide a good balance between security
  169. * and compatibility with current deployments.
  170. */
  171. extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default;
  172. /**
  173. * Expected next default profile. Recommended for new deployments.
  174. * Currently targets a 128-bit security level, except for RSA-2048.
  175. */
  176. extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next;
  177. /**
  178. * NSA Suite B profile.
  179. */
  180. extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb;
  181. /**
  182. * \brief Parse a single DER formatted certificate and add it
  183. * to the chained list.
  184. *
  185. * \param chain points to the start of the chain
  186. * \param buf buffer holding the certificate DER data
  187. * \param buflen size of the buffer
  188. *
  189. * \return 0 if successful, or a specific X509 or PEM error code
  190. */
  191. int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain, const unsigned char *buf,
  192. size_t buflen );
  193. /**
  194. * \brief Parse one DER-encoded or one or more concatenated PEM-encoded
  195. * certificates and add them to the chained list.
  196. *
  197. * For CRTs in PEM encoding, the function parses permissively:
  198. * if at least one certificate can be parsed, the function
  199. * returns the number of certificates for which parsing failed
  200. * (hence \c 0 if all certificates were parsed successfully).
  201. * If no certificate could be parsed, the function returns
  202. * the first (negative) error encountered during parsing.
  203. *
  204. * PEM encoded certificates may be interleaved by other data
  205. * such as human readable descriptions of their content, as
  206. * long as the certificates are enclosed in the PEM specific
  207. * '-----{BEGIN/END} CERTIFICATE-----' delimiters.
  208. *
  209. * \param chain The chain to which to add the parsed certificates.
  210. * \param buf The buffer holding the certificate data in PEM or DER format.
  211. * For certificates in PEM encoding, this may be a concatenation
  212. * of multiple certificates; for DER encoding, the buffer must
  213. * comprise exactly one certificate.
  214. * \param buflen The size of \p buf, including the terminating \c NULL byte
  215. * in case of PEM encoded data.
  216. *
  217. * \return \c 0 if all certificates were parsed successfully.
  218. * \return The (positive) number of certificates that couldn't
  219. * be parsed if parsing was partly successful (see above).
  220. * \return A negative X509 or PEM error code otherwise.
  221. *
  222. */
  223. int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen );
  224. #if defined(MBEDTLS_FS_IO)
  225. /**
  226. * \brief Load one or more certificates and add them
  227. * to the chained list. Parses permissively. If some
  228. * certificates can be parsed, the result is the number
  229. * of failed certificates it encountered. If none complete
  230. * correctly, the first error is returned.
  231. *
  232. * \param chain points to the start of the chain
  233. * \param path filename to read the certificates from
  234. *
  235. * \return 0 if all certificates parsed successfully, a positive number
  236. * if partly successful or a specific X509 or PEM error code
  237. */
  238. int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path );
  239. /**
  240. * \brief Load one or more certificate files from a path and add them
  241. * to the chained list. Parses permissively. If some
  242. * certificates can be parsed, the result is the number
  243. * of failed certificates it encountered. If none complete
  244. * correctly, the first error is returned.
  245. *
  246. * \param chain points to the start of the chain
  247. * \param path directory / folder to read the certificate files from
  248. *
  249. * \return 0 if all certificates parsed successfully, a positive number
  250. * if partly successful or a specific X509 or PEM error code
  251. */
  252. int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path );
  253. #endif /* MBEDTLS_FS_IO */
  254. /**
  255. * \brief Returns an informational string about the
  256. * certificate.
  257. *
  258. * \param buf Buffer to write to
  259. * \param size Maximum size of buffer
  260. * \param prefix A line prefix
  261. * \param crt The X509 certificate to represent
  262. *
  263. * \return The length of the string written (not including the
  264. * terminated nul byte), or a negative error code.
  265. */
  266. int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
  267. const mbedtls_x509_crt *crt );
  268. /**
  269. * \brief Returns an informational string about the
  270. * verification status of a certificate.
  271. *
  272. * \param buf Buffer to write to
  273. * \param size Maximum size of buffer
  274. * \param prefix A line prefix
  275. * \param flags Verification flags created by mbedtls_x509_crt_verify()
  276. *
  277. * \return The length of the string written (not including the
  278. * terminated nul byte), or a negative error code.
  279. */
  280. int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
  281. uint32_t flags );
  282. /**
  283. * \brief Verify the certificate signature
  284. *
  285. * The verify callback is a user-supplied callback that
  286. * can clear / modify / add flags for a certificate. If set,
  287. * the verification callback is called for each
  288. * certificate in the chain (from the trust-ca down to the
  289. * presented crt). The parameters for the callback are:
  290. * (void *parameter, mbedtls_x509_crt *crt, int certificate_depth,
  291. * int *flags). With the flags representing current flags for
  292. * that specific certificate and the certificate depth from
  293. * the bottom (Peer cert depth = 0).
  294. *
  295. * All flags left after returning from the callback
  296. * are also returned to the application. The function should
  297. * return 0 for anything (including invalid certificates)
  298. * other than fatal error, as a non-zero return code
  299. * immediately aborts the verification process. For fatal
  300. * errors, a specific error code should be used (different
  301. * from MBEDTLS_ERR_X509_CERT_VERIFY_FAILED which should not
  302. * be returned at this point), or MBEDTLS_ERR_X509_FATAL_ERROR
  303. * can be used if no better code is available.
  304. *
  305. * \note In case verification failed, the results can be displayed
  306. * using \c mbedtls_x509_crt_verify_info()
  307. *
  308. * \note Same as \c mbedtls_x509_crt_verify_with_profile() with the
  309. * default security profile.
  310. *
  311. * \note It is your responsibility to provide up-to-date CRLs for
  312. * all trusted CAs. If no CRL is provided for the CA that was
  313. * used to sign the certificate, CRL verification is skipped
  314. * silently, that is *without* setting any flag.
  315. *
  316. * \note The \c trust_ca list can contain two types of certificates:
  317. * (1) those of trusted root CAs, so that certificates
  318. * chaining up to those CAs will be trusted, and (2)
  319. * self-signed end-entity certificates to be trusted (for
  320. * specific peers you know) - in that case, the self-signed
  321. * certificate doesn't need to have the CA bit set.
  322. *
  323. * \param crt a certificate (chain) to be verified
  324. * \param trust_ca the list of trusted CAs (see note above)
  325. * \param ca_crl the list of CRLs for trusted CAs (see note above)
  326. * \param cn expected Common Name (can be set to
  327. * NULL if the CN must not be verified)
  328. * \param flags result of the verification
  329. * \param f_vrfy verification function
  330. * \param p_vrfy verification parameter
  331. *
  332. * \return 0 (and flags set to 0) if the chain was verified and valid,
  333. * MBEDTLS_ERR_X509_CERT_VERIFY_FAILED if the chain was verified
  334. * but found to be invalid, in which case *flags will have one
  335. * or more MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX
  336. * flags set, or another error (and flags set to 0xffffffff)
  337. * in case of a fatal error encountered during the
  338. * verification process.
  339. */
  340. int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
  341. mbedtls_x509_crt *trust_ca,
  342. mbedtls_x509_crl *ca_crl,
  343. const char *cn, uint32_t *flags,
  344. int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
  345. void *p_vrfy );
  346. /**
  347. * \brief Verify the certificate signature according to profile
  348. *
  349. * \note Same as \c mbedtls_x509_crt_verify(), but with explicit
  350. * security profile.
  351. *
  352. * \note The restrictions on keys (RSA minimum size, allowed curves
  353. * for ECDSA) apply to all certificates: trusted root,
  354. * intermediate CAs if any, and end entity certificate.
  355. *
  356. * \param crt a certificate (chain) to be verified
  357. * \param trust_ca the list of trusted CAs
  358. * \param ca_crl the list of CRLs for trusted CAs
  359. * \param profile security profile for verification
  360. * \param cn expected Common Name (can be set to
  361. * NULL if the CN must not be verified)
  362. * \param flags result of the verification
  363. * \param f_vrfy verification function
  364. * \param p_vrfy verification parameter
  365. *
  366. * \return 0 if successful or MBEDTLS_ERR_X509_CERT_VERIFY_FAILED
  367. * in which case *flags will have one or more
  368. * MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX flags
  369. * set,
  370. * or another error in case of a fatal error encountered
  371. * during the verification process.
  372. */
  373. int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
  374. mbedtls_x509_crt *trust_ca,
  375. mbedtls_x509_crl *ca_crl,
  376. const mbedtls_x509_crt_profile *profile,
  377. const char *cn, uint32_t *flags,
  378. int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
  379. void *p_vrfy );
  380. /**
  381. * \brief Restartable version of \c mbedtls_crt_verify_with_profile()
  382. *
  383. * \note Performs the same job as \c mbedtls_crt_verify_with_profile()
  384. * but can return early and restart according to the limit
  385. * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
  386. *
  387. * \param crt a certificate (chain) to be verified
  388. * \param trust_ca the list of trusted CAs
  389. * \param ca_crl the list of CRLs for trusted CAs
  390. * \param profile security profile for verification
  391. * \param cn expected Common Name (can be set to
  392. * NULL if the CN must not be verified)
  393. * \param flags result of the verification
  394. * \param f_vrfy verification function
  395. * \param p_vrfy verification parameter
  396. * \param rs_ctx restart context (NULL to disable restart)
  397. *
  398. * \return See \c mbedtls_crt_verify_with_profile(), or
  399. * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
  400. * operations was reached: see \c mbedtls_ecp_set_max_ops().
  401. */
  402. int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
  403. mbedtls_x509_crt *trust_ca,
  404. mbedtls_x509_crl *ca_crl,
  405. const mbedtls_x509_crt_profile *profile,
  406. const char *cn, uint32_t *flags,
  407. int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
  408. void *p_vrfy,
  409. mbedtls_x509_crt_restart_ctx *rs_ctx );
  410. #if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
  411. /**
  412. * \brief Check usage of certificate against keyUsage extension.
  413. *
  414. * \param crt Leaf certificate used.
  415. * \param usage Intended usage(s) (eg MBEDTLS_X509_KU_KEY_ENCIPHERMENT
  416. * before using the certificate to perform an RSA key
  417. * exchange).
  418. *
  419. * \note Except for decipherOnly and encipherOnly, a bit set in the
  420. * usage argument means this bit MUST be set in the
  421. * certificate. For decipherOnly and encipherOnly, it means
  422. * that bit MAY be set.
  423. *
  424. * \return 0 is these uses of the certificate are allowed,
  425. * MBEDTLS_ERR_X509_BAD_INPUT_DATA if the keyUsage extension
  426. * is present but does not match the usage argument.
  427. *
  428. * \note You should only call this function on leaf certificates, on
  429. * (intermediate) CAs the keyUsage extension is automatically
  430. * checked by \c mbedtls_x509_crt_verify().
  431. */
  432. int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
  433. unsigned int usage );
  434. #endif /* MBEDTLS_X509_CHECK_KEY_USAGE) */
  435. #if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
  436. /**
  437. * \brief Check usage of certificate against extendedKeyUsage.
  438. *
  439. * \param crt Leaf certificate used.
  440. * \param usage_oid Intended usage (eg MBEDTLS_OID_SERVER_AUTH or
  441. * MBEDTLS_OID_CLIENT_AUTH).
  442. * \param usage_len Length of usage_oid (eg given by MBEDTLS_OID_SIZE()).
  443. *
  444. * \return 0 if this use of the certificate is allowed,
  445. * MBEDTLS_ERR_X509_BAD_INPUT_DATA if not.
  446. *
  447. * \note Usually only makes sense on leaf certificates.
  448. */
  449. int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
  450. const char *usage_oid,
  451. size_t usage_len );
  452. #endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
  453. #if defined(MBEDTLS_X509_CRL_PARSE_C)
  454. /**
  455. * \brief Verify the certificate revocation status
  456. *
  457. * \param crt a certificate to be verified
  458. * \param crl the CRL to verify against
  459. *
  460. * \return 1 if the certificate is revoked, 0 otherwise
  461. *
  462. */
  463. int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl );
  464. #endif /* MBEDTLS_X509_CRL_PARSE_C */
  465. /**
  466. * \brief Initialize a certificate (chain)
  467. *
  468. * \param crt Certificate chain to initialize
  469. */
  470. void mbedtls_x509_crt_init( mbedtls_x509_crt *crt );
  471. /**
  472. * \brief Unallocate all certificate data
  473. *
  474. * \param crt Certificate chain to free
  475. */
  476. void mbedtls_x509_crt_free( mbedtls_x509_crt *crt );
  477. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  478. /**
  479. * \brief Initialize a restart context
  480. */
  481. void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx );
  482. /**
  483. * \brief Free the components of a restart context
  484. */
  485. void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx );
  486. #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
  487. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  488. /* \} name */
  489. /* \} addtogroup x509_module */
  490. #if defined(MBEDTLS_X509_CRT_WRITE_C)
  491. /**
  492. * \brief Initialize a CRT writing context
  493. *
  494. * \param ctx CRT context to initialize
  495. */
  496. void mbedtls_x509write_crt_init( mbedtls_x509write_cert *ctx );
  497. /**
  498. * \brief Set the verion for a Certificate
  499. * Default: MBEDTLS_X509_CRT_VERSION_3
  500. *
  501. * \param ctx CRT context to use
  502. * \param version version to set (MBEDTLS_X509_CRT_VERSION_1, MBEDTLS_X509_CRT_VERSION_2 or
  503. * MBEDTLS_X509_CRT_VERSION_3)
  504. */
  505. void mbedtls_x509write_crt_set_version( mbedtls_x509write_cert *ctx, int version );
  506. /**
  507. * \brief Set the serial number for a Certificate.
  508. *
  509. * \param ctx CRT context to use
  510. * \param serial serial number to set
  511. *
  512. * \return 0 if successful
  513. */
  514. int mbedtls_x509write_crt_set_serial( mbedtls_x509write_cert *ctx, const mbedtls_mpi *serial );
  515. /**
  516. * \brief Set the validity period for a Certificate
  517. * Timestamps should be in string format for UTC timezone
  518. * i.e. "YYYYMMDDhhmmss"
  519. * e.g. "20131231235959" for December 31st 2013
  520. * at 23:59:59
  521. *
  522. * \param ctx CRT context to use
  523. * \param not_before not_before timestamp
  524. * \param not_after not_after timestamp
  525. *
  526. * \return 0 if timestamp was parsed successfully, or
  527. * a specific error code
  528. */
  529. int mbedtls_x509write_crt_set_validity( mbedtls_x509write_cert *ctx, const char *not_before,
  530. const char *not_after );
  531. /**
  532. * \brief Set the issuer name for a Certificate
  533. * Issuer names should contain a comma-separated list
  534. * of OID types and values:
  535. * e.g. "C=UK,O=ARM,CN=mbed TLS CA"
  536. *
  537. * \param ctx CRT context to use
  538. * \param issuer_name issuer name to set
  539. *
  540. * \return 0 if issuer name was parsed successfully, or
  541. * a specific error code
  542. */
  543. int mbedtls_x509write_crt_set_issuer_name( mbedtls_x509write_cert *ctx,
  544. const char *issuer_name );
  545. /**
  546. * \brief Set the subject name for a Certificate
  547. * Subject names should contain a comma-separated list
  548. * of OID types and values:
  549. * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
  550. *
  551. * \param ctx CRT context to use
  552. * \param subject_name subject name to set
  553. *
  554. * \return 0 if subject name was parsed successfully, or
  555. * a specific error code
  556. */
  557. int mbedtls_x509write_crt_set_subject_name( mbedtls_x509write_cert *ctx,
  558. const char *subject_name );
  559. /**
  560. * \brief Set the subject public key for the certificate
  561. *
  562. * \param ctx CRT context to use
  563. * \param key public key to include
  564. */
  565. void mbedtls_x509write_crt_set_subject_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
  566. /**
  567. * \brief Set the issuer key used for signing the certificate
  568. *
  569. * \param ctx CRT context to use
  570. * \param key private key to sign with
  571. */
  572. void mbedtls_x509write_crt_set_issuer_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
  573. /**
  574. * \brief Set the MD algorithm to use for the signature
  575. * (e.g. MBEDTLS_MD_SHA1)
  576. *
  577. * \param ctx CRT context to use
  578. * \param md_alg MD algorithm to use
  579. */
  580. void mbedtls_x509write_crt_set_md_alg( mbedtls_x509write_cert *ctx, mbedtls_md_type_t md_alg );
  581. /**
  582. * \brief Generic function to add to or replace an extension in the
  583. * CRT
  584. *
  585. * \param ctx CRT context to use
  586. * \param oid OID of the extension
  587. * \param oid_len length of the OID
  588. * \param critical if the extension is critical (per the RFC's definition)
  589. * \param val value of the extension OCTET STRING
  590. * \param val_len length of the value data
  591. *
  592. * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
  593. */
  594. int mbedtls_x509write_crt_set_extension( mbedtls_x509write_cert *ctx,
  595. const char *oid, size_t oid_len,
  596. int critical,
  597. const unsigned char *val, size_t val_len );
  598. /**
  599. * \brief Set the basicConstraints extension for a CRT
  600. *
  601. * \param ctx CRT context to use
  602. * \param is_ca is this a CA certificate
  603. * \param max_pathlen maximum length of certificate chains below this
  604. * certificate (only for CA certificates, -1 is
  605. * inlimited)
  606. *
  607. * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
  608. */
  609. int mbedtls_x509write_crt_set_basic_constraints( mbedtls_x509write_cert *ctx,
  610. int is_ca, int max_pathlen );
  611. #if defined(MBEDTLS_SHA1_C)
  612. /**
  613. * \brief Set the subjectKeyIdentifier extension for a CRT
  614. * Requires that mbedtls_x509write_crt_set_subject_key() has been
  615. * called before
  616. *
  617. * \param ctx CRT context to use
  618. *
  619. * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
  620. */
  621. int mbedtls_x509write_crt_set_subject_key_identifier( mbedtls_x509write_cert *ctx );
  622. /**
  623. * \brief Set the authorityKeyIdentifier extension for a CRT
  624. * Requires that mbedtls_x509write_crt_set_issuer_key() has been
  625. * called before
  626. *
  627. * \param ctx CRT context to use
  628. *
  629. * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
  630. */
  631. int mbedtls_x509write_crt_set_authority_key_identifier( mbedtls_x509write_cert *ctx );
  632. #endif /* MBEDTLS_SHA1_C */
  633. /**
  634. * \brief Set the Key Usage Extension flags
  635. * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
  636. *
  637. * \param ctx CRT context to use
  638. * \param key_usage key usage flags to set
  639. *
  640. * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
  641. */
  642. int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx,
  643. unsigned int key_usage );
  644. /**
  645. * \brief Set the Netscape Cert Type flags
  646. * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
  647. *
  648. * \param ctx CRT context to use
  649. * \param ns_cert_type Netscape Cert Type flags to set
  650. *
  651. * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
  652. */
  653. int mbedtls_x509write_crt_set_ns_cert_type( mbedtls_x509write_cert *ctx,
  654. unsigned char ns_cert_type );
  655. /**
  656. * \brief Free the contents of a CRT write context
  657. *
  658. * \param ctx CRT context to free
  659. */
  660. void mbedtls_x509write_crt_free( mbedtls_x509write_cert *ctx );
  661. /**
  662. * \brief Write a built up certificate to a X509 DER structure
  663. * Note: data is written at the end of the buffer! Use the
  664. * return value to determine where you should start
  665. * using the buffer
  666. *
  667. * \param ctx certificate to write away
  668. * \param buf buffer to write to
  669. * \param size size of the buffer
  670. * \param f_rng RNG function (for signature, see note)
  671. * \param p_rng RNG parameter
  672. *
  673. * \return length of data written if successful, or a specific
  674. * error code
  675. *
  676. * \note f_rng may be NULL if RSA is used for signature and the
  677. * signature is made offline (otherwise f_rng is desirable
  678. * for countermeasures against timing attacks).
  679. * ECDSA signatures always require a non-NULL f_rng.
  680. */
  681. int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
  682. int (*f_rng)(void *, unsigned char *, size_t),
  683. void *p_rng );
  684. #if defined(MBEDTLS_PEM_WRITE_C)
  685. /**
  686. * \brief Write a built up certificate to a X509 PEM string
  687. *
  688. * \param ctx certificate to write away
  689. * \param buf buffer to write to
  690. * \param size size of the buffer
  691. * \param f_rng RNG function (for signature, see note)
  692. * \param p_rng RNG parameter
  693. *
  694. * \return 0 if successful, or a specific error code
  695. *
  696. * \note f_rng may be NULL if RSA is used for signature and the
  697. * signature is made offline (otherwise f_rng is desirable
  698. * for countermeasures against timing attacks).
  699. * ECDSA signatures always require a non-NULL f_rng.
  700. */
  701. int mbedtls_x509write_crt_pem( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
  702. int (*f_rng)(void *, unsigned char *, size_t),
  703. void *p_rng );
  704. #endif /* MBEDTLS_PEM_WRITE_C */
  705. #endif /* MBEDTLS_X509_CRT_WRITE_C */
  706. #ifdef __cplusplus
  707. }
  708. #endif
  709. #endif /* mbedtls_x509_crt.h */