x509_crt.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  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
  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. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  122. /**
  123. * Default security profile. Should provide a good balance between security
  124. * and compatibility with current deployments.
  125. */
  126. extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default;
  127. /**
  128. * Expected next default profile. Recommended for new deployments.
  129. * Currently targets a 128-bit security level, except for RSA-2048.
  130. */
  131. extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next;
  132. /**
  133. * NSA Suite B profile.
  134. */
  135. extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb;
  136. /**
  137. * \brief Parse a single DER formatted certificate and add it
  138. * to the chained list.
  139. *
  140. * \param chain points to the start of the chain
  141. * \param buf buffer holding the certificate DER data
  142. * \param buflen size of the buffer
  143. *
  144. * \return 0 if successful, or a specific X509 or PEM error code
  145. */
  146. int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain, const unsigned char *buf,
  147. size_t buflen );
  148. /**
  149. * \brief Parse one DER-encoded or one or more concatenated PEM-encoded
  150. * certificates and add them to the chained list.
  151. *
  152. * For CRTs in PEM encoding, the function parses permissively:
  153. * if at least one certificate can be parsed, the function
  154. * returns the number of certificates for which parsing failed
  155. * (hence \c 0 if all certificates were parsed successfully).
  156. * If no certificate could be parsed, the function returns
  157. * the first (negative) error encountered during parsing.
  158. *
  159. * PEM encoded certificates may be interleaved by other data
  160. * such as human readable descriptions of their content, as
  161. * long as the certificates are enclosed in the PEM specific
  162. * '-----{BEGIN/END} CERTIFICATE-----' delimiters.
  163. *
  164. * \param chain The chain to which to add the parsed certificates.
  165. * \param buf The buffer holding the certificate data in PEM or DER format.
  166. * For certificates in PEM encoding, this may be a concatenation
  167. * of multiple certificates; for DER encoding, the buffer must
  168. * comprise exactly one certificate.
  169. * \param buflen The size of \p buf, including the terminating \c NULL byte
  170. * in case of PEM encoded data.
  171. *
  172. * \return \c 0 if all certificates were parsed successfully.
  173. * \return The (positive) number of certificates that couldn't
  174. * be parsed if parsing was partly successful (see above).
  175. * \return A negative X509 or PEM error code otherwise.
  176. *
  177. */
  178. int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen );
  179. #if defined(MBEDTLS_FS_IO)
  180. /**
  181. * \brief Load one or more certificates 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 filename to read the certificates 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_file( mbedtls_x509_crt *chain, const char *path );
  194. /**
  195. * \brief Load one or more certificate files from a path and add them
  196. * to the chained list. Parses permissively. If some
  197. * certificates can be parsed, the result is the number
  198. * of failed certificates it encountered. If none complete
  199. * correctly, the first error is returned.
  200. *
  201. * \param chain points to the start of the chain
  202. * \param path directory / folder to read the certificate files from
  203. *
  204. * \return 0 if all certificates parsed successfully, a positive number
  205. * if partly successful or a specific X509 or PEM error code
  206. */
  207. int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path );
  208. #endif /* MBEDTLS_FS_IO */
  209. /**
  210. * \brief Returns an informational string about the
  211. * certificate.
  212. *
  213. * \param buf Buffer to write to
  214. * \param size Maximum size of buffer
  215. * \param prefix A line prefix
  216. * \param crt The X509 certificate to represent
  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_info( char *buf, size_t size, const char *prefix,
  222. const mbedtls_x509_crt *crt );
  223. /**
  224. * \brief Returns an informational string about the
  225. * verification status of a certificate.
  226. *
  227. * \param buf Buffer to write to
  228. * \param size Maximum size of buffer
  229. * \param prefix A line prefix
  230. * \param flags Verification flags created by mbedtls_x509_crt_verify()
  231. *
  232. * \return The length of the string written (not including the
  233. * terminated nul byte), or a negative error code.
  234. */
  235. int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
  236. uint32_t flags );
  237. /**
  238. * \brief Verify the certificate signature
  239. *
  240. * The verify callback is a user-supplied callback that
  241. * can clear / modify / add flags for a certificate. If set,
  242. * the verification callback is called for each
  243. * certificate in the chain (from the trust-ca down to the
  244. * presented crt). The parameters for the callback are:
  245. * (void *parameter, mbedtls_x509_crt *crt, int certificate_depth,
  246. * int *flags). With the flags representing current flags for
  247. * that specific certificate and the certificate depth from
  248. * the bottom (Peer cert depth = 0).
  249. *
  250. * All flags left after returning from the callback
  251. * are also returned to the application. The function should
  252. * return 0 for anything (including invalid certificates)
  253. * other than fatal error, as a non-zero return code
  254. * immediately aborts the verification process. For fatal
  255. * errors, a specific error code should be used (different
  256. * from MBEDTLS_ERR_X509_CERT_VERIFY_FAILED which should not
  257. * be returned at this point), or MBEDTLS_ERR_X509_FATAL_ERROR
  258. * can be used if no better code is available.
  259. *
  260. * \note In case verification failed, the results can be displayed
  261. * using \c mbedtls_x509_crt_verify_info()
  262. *
  263. * \note Same as \c mbedtls_x509_crt_verify_with_profile() with the
  264. * default security profile.
  265. *
  266. * \note It is your responsibility to provide up-to-date CRLs for
  267. * all trusted CAs. If no CRL is provided for the CA that was
  268. * used to sign the certificate, CRL verification is skipped
  269. * silently, that is *without* setting any flag.
  270. *
  271. * \note The \c trust_ca list can contain two types of certificates:
  272. * (1) those of trusted root CAs, so that certificates
  273. * chaining up to those CAs will be trusted, and (2)
  274. * self-signed end-entity certificates to be trusted (for
  275. * specific peers you know) - in that case, the self-signed
  276. * certificate doesn't need to have the CA bit set.
  277. *
  278. * \param crt a certificate (chain) to be verified
  279. * \param trust_ca the list of trusted CAs (see note above)
  280. * \param ca_crl the list of CRLs for trusted CAs (see note above)
  281. * \param cn expected Common Name (can be set to
  282. * NULL if the CN must not be verified)
  283. * \param flags result of the verification
  284. * \param f_vrfy verification function
  285. * \param p_vrfy verification parameter
  286. *
  287. * \return 0 (and flags set to 0) if the chain was verified and valid,
  288. * MBEDTLS_ERR_X509_CERT_VERIFY_FAILED if the chain was verified
  289. * but found to be invalid, in which case *flags will have one
  290. * or more MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX
  291. * flags set, or another error (and flags set to 0xffffffff)
  292. * in case of a fatal error encountered during the
  293. * verification process.
  294. */
  295. int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
  296. mbedtls_x509_crt *trust_ca,
  297. mbedtls_x509_crl *ca_crl,
  298. const char *cn, uint32_t *flags,
  299. int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
  300. void *p_vrfy );
  301. /**
  302. * \brief Verify the certificate signature according to profile
  303. *
  304. * \note Same as \c mbedtls_x509_crt_verify(), but with explicit
  305. * security profile.
  306. *
  307. * \note The restrictions on keys (RSA minimum size, allowed curves
  308. * for ECDSA) apply to all certificates: trusted root,
  309. * intermediate CAs if any, and end entity certificate.
  310. *
  311. * \param crt a certificate (chain) to be verified
  312. * \param trust_ca the list of trusted CAs
  313. * \param ca_crl the list of CRLs for trusted CAs
  314. * \param profile security profile for verification
  315. * \param cn expected Common Name (can be set to
  316. * NULL if the CN must not be verified)
  317. * \param flags result of the verification
  318. * \param f_vrfy verification function
  319. * \param p_vrfy verification parameter
  320. *
  321. * \return 0 if successful or MBEDTLS_ERR_X509_CERT_VERIFY_FAILED
  322. * in which case *flags will have one or more
  323. * MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX flags
  324. * set,
  325. * or another error in case of a fatal error encountered
  326. * during the verification process.
  327. */
  328. int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
  329. mbedtls_x509_crt *trust_ca,
  330. mbedtls_x509_crl *ca_crl,
  331. const mbedtls_x509_crt_profile *profile,
  332. const char *cn, uint32_t *flags,
  333. int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
  334. void *p_vrfy );
  335. #if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
  336. /**
  337. * \brief Check usage of certificate against keyUsage extension.
  338. *
  339. * \param crt Leaf certificate used.
  340. * \param usage Intended usage(s) (eg MBEDTLS_X509_KU_KEY_ENCIPHERMENT
  341. * before using the certificate to perform an RSA key
  342. * exchange).
  343. *
  344. * \note Except for decipherOnly and encipherOnly, a bit set in the
  345. * usage argument means this bit MUST be set in the
  346. * certificate. For decipherOnly and encipherOnly, it means
  347. * that bit MAY be set.
  348. *
  349. * \return 0 is these uses of the certificate are allowed,
  350. * MBEDTLS_ERR_X509_BAD_INPUT_DATA if the keyUsage extension
  351. * is present but does not match the usage argument.
  352. *
  353. * \note You should only call this function on leaf certificates, on
  354. * (intermediate) CAs the keyUsage extension is automatically
  355. * checked by \c mbedtls_x509_crt_verify().
  356. */
  357. int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
  358. unsigned int usage );
  359. #endif /* MBEDTLS_X509_CHECK_KEY_USAGE) */
  360. #if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
  361. /**
  362. * \brief Check usage of certificate against extendedKeyUsage.
  363. *
  364. * \param crt Leaf certificate used.
  365. * \param usage_oid Intended usage (eg MBEDTLS_OID_SERVER_AUTH or
  366. * MBEDTLS_OID_CLIENT_AUTH).
  367. * \param usage_len Length of usage_oid (eg given by MBEDTLS_OID_SIZE()).
  368. *
  369. * \return 0 if this use of the certificate is allowed,
  370. * MBEDTLS_ERR_X509_BAD_INPUT_DATA if not.
  371. *
  372. * \note Usually only makes sense on leaf certificates.
  373. */
  374. int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
  375. const char *usage_oid,
  376. size_t usage_len );
  377. #endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
  378. #if defined(MBEDTLS_X509_CRL_PARSE_C)
  379. /**
  380. * \brief Verify the certificate revocation status
  381. *
  382. * \param crt a certificate to be verified
  383. * \param crl the CRL to verify against
  384. *
  385. * \return 1 if the certificate is revoked, 0 otherwise
  386. *
  387. */
  388. int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl );
  389. #endif /* MBEDTLS_X509_CRL_PARSE_C */
  390. /**
  391. * \brief Initialize a certificate (chain)
  392. *
  393. * \param crt Certificate chain to initialize
  394. */
  395. void mbedtls_x509_crt_init( mbedtls_x509_crt *crt );
  396. /**
  397. * \brief Unallocate all certificate data
  398. *
  399. * \param crt Certificate chain to free
  400. */
  401. void mbedtls_x509_crt_free( mbedtls_x509_crt *crt );
  402. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  403. /* \} name */
  404. /* \} addtogroup x509_module */
  405. #if defined(MBEDTLS_X509_CRT_WRITE_C)
  406. /**
  407. * \brief Initialize a CRT writing context
  408. *
  409. * \param ctx CRT context to initialize
  410. */
  411. void mbedtls_x509write_crt_init( mbedtls_x509write_cert *ctx );
  412. /**
  413. * \brief Set the verion for a Certificate
  414. * Default: MBEDTLS_X509_CRT_VERSION_3
  415. *
  416. * \param ctx CRT context to use
  417. * \param version version to set (MBEDTLS_X509_CRT_VERSION_1, MBEDTLS_X509_CRT_VERSION_2 or
  418. * MBEDTLS_X509_CRT_VERSION_3)
  419. */
  420. void mbedtls_x509write_crt_set_version( mbedtls_x509write_cert *ctx, int version );
  421. /**
  422. * \brief Set the serial number for a Certificate.
  423. *
  424. * \param ctx CRT context to use
  425. * \param serial serial number to set
  426. *
  427. * \return 0 if successful
  428. */
  429. int mbedtls_x509write_crt_set_serial( mbedtls_x509write_cert *ctx, const mbedtls_mpi *serial );
  430. /**
  431. * \brief Set the validity period for a Certificate
  432. * Timestamps should be in string format for UTC timezone
  433. * i.e. "YYYYMMDDhhmmss"
  434. * e.g. "20131231235959" for December 31st 2013
  435. * at 23:59:59
  436. *
  437. * \param ctx CRT context to use
  438. * \param not_before not_before timestamp
  439. * \param not_after not_after timestamp
  440. *
  441. * \return 0 if timestamp was parsed successfully, or
  442. * a specific error code
  443. */
  444. int mbedtls_x509write_crt_set_validity( mbedtls_x509write_cert *ctx, const char *not_before,
  445. const char *not_after );
  446. /**
  447. * \brief Set the issuer name for a Certificate
  448. * Issuer names should contain a comma-separated list
  449. * of OID types and values:
  450. * e.g. "C=UK,O=ARM,CN=mbed TLS CA"
  451. *
  452. * \param ctx CRT context to use
  453. * \param issuer_name issuer name to set
  454. *
  455. * \return 0 if issuer name was parsed successfully, or
  456. * a specific error code
  457. */
  458. int mbedtls_x509write_crt_set_issuer_name( mbedtls_x509write_cert *ctx,
  459. const char *issuer_name );
  460. /**
  461. * \brief Set the subject name for a Certificate
  462. * Subject names should contain a comma-separated list
  463. * of OID types and values:
  464. * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
  465. *
  466. * \param ctx CRT context to use
  467. * \param subject_name subject name to set
  468. *
  469. * \return 0 if subject name was parsed successfully, or
  470. * a specific error code
  471. */
  472. int mbedtls_x509write_crt_set_subject_name( mbedtls_x509write_cert *ctx,
  473. const char *subject_name );
  474. /**
  475. * \brief Set the subject public key for the certificate
  476. *
  477. * \param ctx CRT context to use
  478. * \param key public key to include
  479. */
  480. void mbedtls_x509write_crt_set_subject_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
  481. /**
  482. * \brief Set the issuer key used for signing the certificate
  483. *
  484. * \param ctx CRT context to use
  485. * \param key private key to sign with
  486. */
  487. void mbedtls_x509write_crt_set_issuer_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
  488. /**
  489. * \brief Set the MD algorithm to use for the signature
  490. * (e.g. MBEDTLS_MD_SHA1)
  491. *
  492. * \param ctx CRT context to use
  493. * \param md_alg MD algorithm to use
  494. */
  495. void mbedtls_x509write_crt_set_md_alg( mbedtls_x509write_cert *ctx, mbedtls_md_type_t md_alg );
  496. /**
  497. * \brief Generic function to add to or replace an extension in the
  498. * CRT
  499. *
  500. * \param ctx CRT context to use
  501. * \param oid OID of the extension
  502. * \param oid_len length of the OID
  503. * \param critical if the extension is critical (per the RFC's definition)
  504. * \param val value of the extension OCTET STRING
  505. * \param val_len length of the value data
  506. *
  507. * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
  508. */
  509. int mbedtls_x509write_crt_set_extension( mbedtls_x509write_cert *ctx,
  510. const char *oid, size_t oid_len,
  511. int critical,
  512. const unsigned char *val, size_t val_len );
  513. /**
  514. * \brief Set the basicConstraints extension for a CRT
  515. *
  516. * \param ctx CRT context to use
  517. * \param is_ca is this a CA certificate
  518. * \param max_pathlen maximum length of certificate chains below this
  519. * certificate (only for CA certificates, -1 is
  520. * inlimited)
  521. *
  522. * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
  523. */
  524. int mbedtls_x509write_crt_set_basic_constraints( mbedtls_x509write_cert *ctx,
  525. int is_ca, int max_pathlen );
  526. #if defined(MBEDTLS_SHA1_C)
  527. /**
  528. * \brief Set the subjectKeyIdentifier extension for a CRT
  529. * Requires that mbedtls_x509write_crt_set_subject_key() has been
  530. * called before
  531. *
  532. * \param ctx CRT context to use
  533. *
  534. * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
  535. */
  536. int mbedtls_x509write_crt_set_subject_key_identifier( mbedtls_x509write_cert *ctx );
  537. /**
  538. * \brief Set the authorityKeyIdentifier extension for a CRT
  539. * Requires that mbedtls_x509write_crt_set_issuer_key() has been
  540. * called before
  541. *
  542. * \param ctx CRT context to use
  543. *
  544. * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
  545. */
  546. int mbedtls_x509write_crt_set_authority_key_identifier( mbedtls_x509write_cert *ctx );
  547. #endif /* MBEDTLS_SHA1_C */
  548. /**
  549. * \brief Set the Key Usage Extension flags
  550. * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
  551. *
  552. * \param ctx CRT context to use
  553. * \param key_usage key usage flags to set
  554. *
  555. * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
  556. */
  557. int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx,
  558. unsigned int key_usage );
  559. /**
  560. * \brief Set the Netscape Cert Type flags
  561. * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
  562. *
  563. * \param ctx CRT context to use
  564. * \param ns_cert_type Netscape Cert Type flags to set
  565. *
  566. * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
  567. */
  568. int mbedtls_x509write_crt_set_ns_cert_type( mbedtls_x509write_cert *ctx,
  569. unsigned char ns_cert_type );
  570. /**
  571. * \brief Free the contents of a CRT write context
  572. *
  573. * \param ctx CRT context to free
  574. */
  575. void mbedtls_x509write_crt_free( mbedtls_x509write_cert *ctx );
  576. /**
  577. * \brief Write a built up certificate to a X509 DER structure
  578. * Note: data is written at the end of the buffer! Use the
  579. * return value to determine where you should start
  580. * using the buffer
  581. *
  582. * \param ctx certificate to write away
  583. * \param buf buffer to write to
  584. * \param size size of the buffer
  585. * \param f_rng RNG function (for signature, see note)
  586. * \param p_rng RNG parameter
  587. *
  588. * \return length of data written if successful, or a specific
  589. * error code
  590. *
  591. * \note f_rng may be NULL if RSA is used for signature and the
  592. * signature is made offline (otherwise f_rng is desirable
  593. * for countermeasures against timing attacks).
  594. * ECDSA signatures always require a non-NULL f_rng.
  595. */
  596. int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
  597. int (*f_rng)(void *, unsigned char *, size_t),
  598. void *p_rng );
  599. #if defined(MBEDTLS_PEM_WRITE_C)
  600. /**
  601. * \brief Write a built up certificate to a X509 PEM string
  602. *
  603. * \param ctx certificate to write away
  604. * \param buf buffer to write to
  605. * \param size size of the buffer
  606. * \param f_rng RNG function (for signature, see note)
  607. * \param p_rng RNG parameter
  608. *
  609. * \return 0 if successful, or a specific error code
  610. *
  611. * \note f_rng may be NULL if RSA is used for signature and the
  612. * signature is made offline (otherwise f_rng is desirable
  613. * for countermeasures against timing attacks).
  614. * ECDSA signatures always require a non-NULL f_rng.
  615. */
  616. int mbedtls_x509write_crt_pem( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
  617. int (*f_rng)(void *, unsigned char *, size_t),
  618. void *p_rng );
  619. #endif /* MBEDTLS_PEM_WRITE_C */
  620. #endif /* MBEDTLS_X509_CRT_WRITE_C */
  621. #ifdef __cplusplus
  622. }
  623. #endif
  624. #endif /* mbedtls_x509_crt.h */