x509_crt.h 26 KB

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