x509_crt.h 25 KB

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