x509_crt.h 32 KB

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