x509_crt.h 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  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
  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. #ifndef MBEDTLS_X509_CRT_H
  23. #define MBEDTLS_X509_CRT_H
  24. #if !defined(MBEDTLS_CONFIG_FILE)
  25. #include "mbedtls/config.h"
  26. #else
  27. #include MBEDTLS_CONFIG_FILE
  28. #endif
  29. #include "mbedtls/x509.h"
  30. #include "mbedtls/x509_crl.h"
  31. #include "mbedtls/bignum.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. int own_buffer; /**< Indicates if \c raw is owned
  49. * by the structure or not. */
  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_x509_buf pk_raw;
  62. mbedtls_pk_context pk; /**< Container for the public key context. */
  63. mbedtls_x509_buf issuer_id; /**< Optional X.509 v2/v3 issuer unique identifier. */
  64. mbedtls_x509_buf subject_id; /**< Optional X.509 v2/v3 subject unique identifier. */
  65. mbedtls_x509_buf v3_ext; /**< Optional X.509 v3 extensions. */
  66. mbedtls_x509_sequence subject_alt_names; /**< Optional list of raw entries of Subject Alternative Names extension (currently only dNSName and OtherName are listed). */
  67. mbedtls_x509_sequence certificate_policies; /**< Optional list of certificate policies (Only anyPolicy is printed and enforced, however the rest of the policies are still listed). */
  68. int ext_types; /**< Bit string containing detected and parsed extensions */
  69. int ca_istrue; /**< Optional Basic Constraint extension value: 1 if this certificate belongs to a CA, 0 otherwise. */
  70. 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+ */
  71. unsigned int key_usage; /**< Optional key usage extension value: See the values in x509.h */
  72. mbedtls_x509_sequence ext_key_usage; /**< Optional list of extended key usage OIDs. */
  73. unsigned char ns_cert_type; /**< Optional Netscape certificate type extension value: See the values in x509.h */
  74. mbedtls_x509_buf sig; /**< Signature: hash of the tbs part signed with the private key. */
  75. mbedtls_md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
  76. mbedtls_pk_type_t sig_pk; /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
  77. void *sig_opts; /**< Signature options to be passed to mbedtls_pk_verify_ext(), e.g. for RSASSA-PSS */
  78. struct mbedtls_x509_crt *next; /**< Next certificate in the CA-chain. */
  79. }
  80. mbedtls_x509_crt;
  81. /**
  82. * From RFC 5280 section 4.2.1.6:
  83. * OtherName ::= SEQUENCE {
  84. * type-id OBJECT IDENTIFIER,
  85. * value [0] EXPLICIT ANY DEFINED BY type-id }
  86. */
  87. typedef struct mbedtls_x509_san_other_name
  88. {
  89. /**
  90. * The type_id is an OID as deifned in RFC 5280.
  91. * To check the value of the type id, you should use
  92. * \p MBEDTLS_OID_CMP with a known OID mbedtls_x509_buf.
  93. */
  94. mbedtls_x509_buf type_id; /**< The type id. */
  95. union
  96. {
  97. /**
  98. * From RFC 4108 section 5:
  99. * HardwareModuleName ::= SEQUENCE {
  100. * hwType OBJECT IDENTIFIER,
  101. * hwSerialNum OCTET STRING }
  102. */
  103. struct
  104. {
  105. mbedtls_x509_buf oid; /**< The object identifier. */
  106. mbedtls_x509_buf val; /**< The named value. */
  107. }
  108. hardware_module_name;
  109. }
  110. value;
  111. }
  112. mbedtls_x509_san_other_name;
  113. /**
  114. * A structure for holding the parsed Subject Alternative Name, according to type
  115. */
  116. typedef struct mbedtls_x509_subject_alternative_name
  117. {
  118. int type; /**< The SAN type, value of MBEDTLS_X509_SAN_XXX. */
  119. union {
  120. mbedtls_x509_san_other_name other_name; /**< The otherName supported type. */
  121. mbedtls_x509_buf unstructured_name; /**< The buffer for the un constructed types. Only dnsName currently supported */
  122. }
  123. san; /**< A union of the supported SAN types */
  124. }
  125. mbedtls_x509_subject_alternative_name;
  126. /**
  127. * Build flag from an algorithm/curve identifier (pk, md, ecp)
  128. * Since 0 is always XXX_NONE, ignore it.
  129. */
  130. #define MBEDTLS_X509_ID_FLAG( id ) ( 1 << ( (id) - 1 ) )
  131. /**
  132. * Security profile for certificate verification.
  133. *
  134. * All lists are bitfields, built by ORing flags from MBEDTLS_X509_ID_FLAG().
  135. */
  136. typedef struct mbedtls_x509_crt_profile
  137. {
  138. uint32_t allowed_mds; /**< MDs for signatures */
  139. uint32_t allowed_pks; /**< PK algs for signatures */
  140. uint32_t allowed_curves; /**< Elliptic curves for ECDSA */
  141. uint32_t rsa_min_bitlen; /**< Minimum size for RSA keys */
  142. }
  143. mbedtls_x509_crt_profile;
  144. #define MBEDTLS_X509_CRT_VERSION_1 0
  145. #define MBEDTLS_X509_CRT_VERSION_2 1
  146. #define MBEDTLS_X509_CRT_VERSION_3 2
  147. #define MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN 32
  148. #define MBEDTLS_X509_RFC5280_UTC_TIME_LEN 15
  149. #if !defined( MBEDTLS_X509_MAX_FILE_PATH_LEN )
  150. #define MBEDTLS_X509_MAX_FILE_PATH_LEN 512
  151. #endif
  152. /**
  153. * Container for writing a certificate (CRT)
  154. */
  155. typedef struct mbedtls_x509write_cert
  156. {
  157. int version;
  158. mbedtls_mpi serial;
  159. mbedtls_pk_context *subject_key;
  160. mbedtls_pk_context *issuer_key;
  161. mbedtls_asn1_named_data *subject;
  162. mbedtls_asn1_named_data *issuer;
  163. mbedtls_md_type_t md_alg;
  164. char not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
  165. char not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
  166. mbedtls_asn1_named_data *extensions;
  167. }
  168. mbedtls_x509write_cert;
  169. /**
  170. * Item in a verification chain: cert and flags for it
  171. */
  172. typedef struct {
  173. mbedtls_x509_crt *crt;
  174. uint32_t flags;
  175. } mbedtls_x509_crt_verify_chain_item;
  176. /**
  177. * Max size of verification chain: end-entity + intermediates + trusted root
  178. */
  179. #define MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
  180. /**
  181. * Verification chain as built by \c mbedtls_crt_verify_chain()
  182. */
  183. typedef struct
  184. {
  185. mbedtls_x509_crt_verify_chain_item items[MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE];
  186. unsigned len;
  187. #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
  188. /* This stores the list of potential trusted signers obtained from
  189. * the CA callback used for the CRT verification, if configured.
  190. * We must track it somewhere because the callback passes its
  191. * ownership to the caller. */
  192. mbedtls_x509_crt *trust_ca_cb_result;
  193. #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
  194. } mbedtls_x509_crt_verify_chain;
  195. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  196. /**
  197. * \brief Context for resuming X.509 verify operations
  198. */
  199. typedef struct
  200. {
  201. /* for check_signature() */
  202. mbedtls_pk_restart_ctx pk;
  203. /* for find_parent_in() */
  204. mbedtls_x509_crt *parent; /* non-null iff parent_in in progress */
  205. mbedtls_x509_crt *fallback_parent;
  206. int fallback_signature_is_good;
  207. /* for find_parent() */
  208. int parent_is_trusted; /* -1 if find_parent is not in progress */
  209. /* for verify_chain() */
  210. enum {
  211. x509_crt_rs_none,
  212. x509_crt_rs_find_parent,
  213. } in_progress; /* none if no operation is in progress */
  214. int self_cnt;
  215. mbedtls_x509_crt_verify_chain ver_chain;
  216. } mbedtls_x509_crt_restart_ctx;
  217. #else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
  218. /* Now we can declare functions that take a pointer to that */
  219. typedef void mbedtls_x509_crt_restart_ctx;
  220. #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
  221. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  222. /**
  223. * Default security profile. Should provide a good balance between security
  224. * and compatibility with current deployments.
  225. *
  226. * This profile permits:
  227. * - SHA2 hashes.
  228. * - All supported elliptic curves.
  229. * - RSA with 2048 bits and above.
  230. *
  231. * New minor versions of Mbed TLS may extend this profile, for example if
  232. * new curves are added to the library. New minor versions of Mbed TLS will
  233. * not reduce this profile unless serious security concerns require it.
  234. */
  235. extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default;
  236. /**
  237. * Expected next default profile. Recommended for new deployments.
  238. * Currently targets a 128-bit security level, except for allowing RSA-2048.
  239. */
  240. extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next;
  241. /**
  242. * NSA Suite B profile.
  243. */
  244. extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb;
  245. /**
  246. * \brief Parse a single DER formatted certificate and add it
  247. * to the end of the provided chained list.
  248. *
  249. * \param chain The pointer to the start of the CRT chain to attach to.
  250. * When parsing the first CRT in a chain, this should point
  251. * to an instance of ::mbedtls_x509_crt initialized through
  252. * mbedtls_x509_crt_init().
  253. * \param buf The buffer holding the DER encoded certificate.
  254. * \param buflen The size in Bytes of \p buf.
  255. *
  256. * \note This function makes an internal copy of the CRT buffer
  257. * \p buf. In particular, \p buf may be destroyed or reused
  258. * after this call returns. To avoid duplicating the CRT
  259. * buffer (at the cost of stricter lifetime constraints),
  260. * use mbedtls_x509_crt_parse_der_nocopy() instead.
  261. *
  262. * \return \c 0 if successful.
  263. * \return A negative error code on failure.
  264. */
  265. int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
  266. const unsigned char *buf,
  267. size_t buflen );
  268. /**
  269. * \brief The type of certificate extension callbacks.
  270. *
  271. * Callbacks of this type are passed to and used by the
  272. * mbedtls_x509_crt_parse_der_with_ext_cb() routine when
  273. * it encounters either an unsupported extension or a
  274. * "certificate policies" extension containing any
  275. * unsupported certificate policies.
  276. * Future versions of the library may invoke the callback
  277. * in other cases, if and when the need arises.
  278. *
  279. * \param p_ctx An opaque context passed to the callback.
  280. * \param crt The certificate being parsed.
  281. * \param oid The OID of the extension.
  282. * \param critical Whether the extension is critical.
  283. * \param p Pointer to the start of the extension value
  284. * (the content of the OCTET STRING).
  285. * \param end End of extension value.
  286. *
  287. * \note The callback must fail and return a negative error code
  288. * if it can not parse or does not support the extension.
  289. * When the callback fails to parse a critical extension
  290. * mbedtls_x509_crt_parse_der_with_ext_cb() also fails.
  291. * When the callback fails to parse a non critical extension
  292. * mbedtls_x509_crt_parse_der_with_ext_cb() simply skips
  293. * the extension and continues parsing.
  294. *
  295. * \return \c 0 on success.
  296. * \return A negative error code on failure.
  297. */
  298. typedef int (*mbedtls_x509_crt_ext_cb_t)( void *p_ctx,
  299. mbedtls_x509_crt const *crt,
  300. mbedtls_x509_buf const *oid,
  301. int critical,
  302. const unsigned char *p,
  303. const unsigned char *end );
  304. /**
  305. * \brief Parse a single DER formatted certificate and add it
  306. * to the end of the provided chained list.
  307. *
  308. * \param chain The pointer to the start of the CRT chain to attach to.
  309. * When parsing the first CRT in a chain, this should point
  310. * to an instance of ::mbedtls_x509_crt initialized through
  311. * mbedtls_x509_crt_init().
  312. * \param buf The buffer holding the DER encoded certificate.
  313. * \param buflen The size in Bytes of \p buf.
  314. * \param make_copy When not zero this function makes an internal copy of the
  315. * CRT buffer \p buf. In particular, \p buf may be destroyed
  316. * or reused after this call returns.
  317. * When zero this function avoids duplicating the CRT buffer
  318. * by taking temporary ownership thereof until the CRT
  319. * is destroyed (like mbedtls_x509_crt_parse_der_nocopy())
  320. * \param cb A callback invoked for every unsupported certificate
  321. * extension.
  322. * \param p_ctx An opaque context passed to the callback.
  323. *
  324. * \note This call is functionally equivalent to
  325. * mbedtls_x509_crt_parse_der(), and/or
  326. * mbedtls_x509_crt_parse_der_nocopy()
  327. * but it calls the callback with every unsupported
  328. * certificate extension and additionally the
  329. * "certificate policies" extension if it contains any
  330. * unsupported certificate policies.
  331. * The callback must return a negative error code if it
  332. * does not know how to handle such an extension.
  333. * When the callback fails to parse a critical extension
  334. * mbedtls_x509_crt_parse_der_with_ext_cb() also fails.
  335. * When the callback fails to parse a non critical extension
  336. * mbedtls_x509_crt_parse_der_with_ext_cb() simply skips
  337. * the extension and continues parsing.
  338. * Future versions of the library may invoke the callback
  339. * in other cases, if and when the need arises.
  340. *
  341. * \return \c 0 if successful.
  342. * \return A negative error code on failure.
  343. */
  344. int mbedtls_x509_crt_parse_der_with_ext_cb( mbedtls_x509_crt *chain,
  345. const unsigned char *buf,
  346. size_t buflen,
  347. int make_copy,
  348. mbedtls_x509_crt_ext_cb_t cb,
  349. void *p_ctx );
  350. /**
  351. * \brief Parse a single DER formatted certificate and add it
  352. * to the end of the provided chained list. This is a
  353. * variant of mbedtls_x509_crt_parse_der() which takes
  354. * temporary ownership of the CRT buffer until the CRT
  355. * is destroyed.
  356. *
  357. * \param chain The pointer to the start of the CRT chain to attach to.
  358. * When parsing the first CRT in a chain, this should point
  359. * to an instance of ::mbedtls_x509_crt initialized through
  360. * mbedtls_x509_crt_init().
  361. * \param buf The address of the readable buffer holding the DER encoded
  362. * certificate to use. On success, this buffer must be
  363. * retained and not be changed for the liftetime of the
  364. * CRT chain \p chain, that is, until \p chain is destroyed
  365. * through a call to mbedtls_x509_crt_free().
  366. * \param buflen The size in Bytes of \p buf.
  367. *
  368. * \note This call is functionally equivalent to
  369. * mbedtls_x509_crt_parse_der(), but it avoids creating a
  370. * copy of the input buffer at the cost of stronger lifetime
  371. * constraints. This is useful in constrained environments
  372. * where duplication of the CRT cannot be tolerated.
  373. *
  374. * \return \c 0 if successful.
  375. * \return A negative error code on failure.
  376. */
  377. int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
  378. const unsigned char *buf,
  379. size_t buflen );
  380. /**
  381. * \brief Parse one DER-encoded or one or more concatenated PEM-encoded
  382. * certificates and add them to the chained list.
  383. *
  384. * For CRTs in PEM encoding, the function parses permissively:
  385. * if at least one certificate can be parsed, the function
  386. * returns the number of certificates for which parsing failed
  387. * (hence \c 0 if all certificates were parsed successfully).
  388. * If no certificate could be parsed, the function returns
  389. * the first (negative) error encountered during parsing.
  390. *
  391. * PEM encoded certificates may be interleaved by other data
  392. * such as human readable descriptions of their content, as
  393. * long as the certificates are enclosed in the PEM specific
  394. * '-----{BEGIN/END} CERTIFICATE-----' delimiters.
  395. *
  396. * \param chain The chain to which to add the parsed certificates.
  397. * \param buf The buffer holding the certificate data in PEM or DER format.
  398. * For certificates in PEM encoding, this may be a concatenation
  399. * of multiple certificates; for DER encoding, the buffer must
  400. * comprise exactly one certificate.
  401. * \param buflen The size of \p buf, including the terminating \c NULL byte
  402. * in case of PEM encoded data.
  403. *
  404. * \return \c 0 if all certificates were parsed successfully.
  405. * \return The (positive) number of certificates that couldn't
  406. * be parsed if parsing was partly successful (see above).
  407. * \return A negative X509 or PEM error code otherwise.
  408. *
  409. */
  410. int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen );
  411. #if defined(MBEDTLS_FS_IO)
  412. /**
  413. * \brief Load one or more certificates and add them
  414. * to the chained list. Parses permissively. If some
  415. * certificates can be parsed, the result is the number
  416. * of failed certificates it encountered. If none complete
  417. * correctly, the first error is returned.
  418. *
  419. * \param chain points to the start of the chain
  420. * \param path filename to read the certificates from
  421. *
  422. * \return 0 if all certificates parsed successfully, a positive number
  423. * if partly successful or a specific X509 or PEM error code
  424. */
  425. int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path );
  426. /**
  427. * \brief Load one or more certificate files from a path and add them
  428. * to the chained list. Parses permissively. If some
  429. * certificates can be parsed, the result is the number
  430. * of failed certificates it encountered. If none complete
  431. * correctly, the first error is returned.
  432. *
  433. * \param chain points to the start of the chain
  434. * \param path directory / folder to read the certificate files from
  435. *
  436. * \return 0 if all certificates parsed successfully, a positive number
  437. * if partly successful or a specific X509 or PEM error code
  438. */
  439. int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path );
  440. #endif /* MBEDTLS_FS_IO */
  441. /**
  442. * \brief This function parses an item in the SubjectAlternativeNames
  443. * extension.
  444. *
  445. * \param san_buf The buffer holding the raw data item of the subject
  446. * alternative name.
  447. * \param san The target structure to populate with the parsed presentation
  448. * of the subject alternative name encoded in \p san_raw.
  449. *
  450. * \note Only "dnsName" and "otherName" of type hardware_module_name
  451. * as defined in RFC 4180 is supported.
  452. *
  453. * \note This function should be called on a single raw data of
  454. * subject alternative name. For example, after successful
  455. * certificate parsing, one must iterate on every item in the
  456. * \p crt->subject_alt_names sequence, and pass it to
  457. * this function.
  458. *
  459. * \warning The target structure contains pointers to the raw data of the
  460. * parsed certificate, and its lifetime is restricted by the
  461. * lifetime of the certificate.
  462. *
  463. * \return \c 0 on success
  464. * \return #MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE for an unsupported
  465. * SAN type.
  466. * \return Another negative value for any other failure.
  467. */
  468. int mbedtls_x509_parse_subject_alt_name( const mbedtls_x509_buf *san_buf,
  469. mbedtls_x509_subject_alternative_name *san );
  470. /**
  471. * \brief Returns an informational string about the
  472. * certificate.
  473. *
  474. * \param buf Buffer to write to
  475. * \param size Maximum size of buffer
  476. * \param prefix A line prefix
  477. * \param crt The X509 certificate to represent
  478. *
  479. * \return The length of the string written (not including the
  480. * terminated nul byte), or a negative error code.
  481. */
  482. int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
  483. const mbedtls_x509_crt *crt );
  484. /**
  485. * \brief Returns an informational string about the
  486. * verification status of a certificate.
  487. *
  488. * \param buf Buffer to write to
  489. * \param size Maximum size of buffer
  490. * \param prefix A line prefix
  491. * \param flags Verification flags created by mbedtls_x509_crt_verify()
  492. *
  493. * \return The length of the string written (not including the
  494. * terminated nul byte), or a negative error code.
  495. */
  496. int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
  497. uint32_t flags );
  498. /**
  499. * \brief Verify a chain of certificates.
  500. *
  501. * The verify callback is a user-supplied callback that
  502. * can clear / modify / add flags for a certificate. If set,
  503. * the verification callback is called for each
  504. * certificate in the chain (from the trust-ca down to the
  505. * presented crt). The parameters for the callback are:
  506. * (void *parameter, mbedtls_x509_crt *crt, int certificate_depth,
  507. * int *flags). With the flags representing current flags for
  508. * that specific certificate and the certificate depth from
  509. * the bottom (Peer cert depth = 0).
  510. *
  511. * All flags left after returning from the callback
  512. * are also returned to the application. The function should
  513. * return 0 for anything (including invalid certificates)
  514. * other than fatal error, as a non-zero return code
  515. * immediately aborts the verification process. For fatal
  516. * errors, a specific error code should be used (different
  517. * from MBEDTLS_ERR_X509_CERT_VERIFY_FAILED which should not
  518. * be returned at this point), or MBEDTLS_ERR_X509_FATAL_ERROR
  519. * can be used if no better code is available.
  520. *
  521. * \note In case verification failed, the results can be displayed
  522. * using \c mbedtls_x509_crt_verify_info()
  523. *
  524. * \note Same as \c mbedtls_x509_crt_verify_with_profile() with the
  525. * default security profile.
  526. *
  527. * \note It is your responsibility to provide up-to-date CRLs for
  528. * all trusted CAs. If no CRL is provided for the CA that was
  529. * used to sign the certificate, CRL verification is skipped
  530. * silently, that is *without* setting any flag.
  531. *
  532. * \note The \c trust_ca list can contain two types of certificates:
  533. * (1) those of trusted root CAs, so that certificates
  534. * chaining up to those CAs will be trusted, and (2)
  535. * self-signed end-entity certificates to be trusted (for
  536. * specific peers you know) - in that case, the self-signed
  537. * certificate doesn't need to have the CA bit set.
  538. *
  539. * \param crt The certificate chain to be verified.
  540. * \param trust_ca The list of trusted CAs.
  541. * \param ca_crl The list of CRLs for trusted CAs.
  542. * \param cn The expected Common Name. This will be checked to be
  543. * present in the certificate's subjectAltNames extension or,
  544. * if this extension is absent, as a CN component in its
  545. * Subject name. Currently only DNS names are supported. This
  546. * may be \c NULL if the CN need not be verified.
  547. * \param flags The address at which to store the result of the verification.
  548. * If the verification couldn't be completed, the flag value is
  549. * set to (uint32_t) -1.
  550. * \param f_vrfy The verification callback to use. See the documentation
  551. * of mbedtls_x509_crt_verify() for more information.
  552. * \param p_vrfy The context to be passed to \p f_vrfy.
  553. *
  554. * \return \c 0 if the chain is valid with respect to the
  555. * passed CN, CAs, CRLs and security profile.
  556. * \return #MBEDTLS_ERR_X509_CERT_VERIFY_FAILED in case the
  557. * certificate chain verification failed. In this case,
  558. * \c *flags will have one or more
  559. * \c MBEDTLS_X509_BADCERT_XXX or \c MBEDTLS_X509_BADCRL_XXX
  560. * flags set.
  561. * \return Another negative error code in case of a fatal error
  562. * encountered during the verification process.
  563. */
  564. int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
  565. mbedtls_x509_crt *trust_ca,
  566. mbedtls_x509_crl *ca_crl,
  567. const char *cn, uint32_t *flags,
  568. int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
  569. void *p_vrfy );
  570. /**
  571. * \brief Verify a chain of certificates with respect to
  572. * a configurable security profile.
  573. *
  574. * \note Same as \c mbedtls_x509_crt_verify(), but with explicit
  575. * security profile.
  576. *
  577. * \note The restrictions on keys (RSA minimum size, allowed curves
  578. * for ECDSA) apply to all certificates: trusted root,
  579. * intermediate CAs if any, and end entity certificate.
  580. *
  581. * \param crt The certificate chain to be verified.
  582. * \param trust_ca The list of trusted CAs.
  583. * \param ca_crl The list of CRLs for trusted CAs.
  584. * \param profile The security profile to use for the verification.
  585. * \param cn The expected Common Name. This may be \c NULL if the
  586. * CN need not be verified.
  587. * \param flags The address at which to store the result of the verification.
  588. * If the verification couldn't be completed, the flag value is
  589. * set to (uint32_t) -1.
  590. * \param f_vrfy The verification callback to use. See the documentation
  591. * of mbedtls_x509_crt_verify() for more information.
  592. * \param p_vrfy The context to be passed to \p f_vrfy.
  593. *
  594. * \return \c 0 if the chain is valid with respect to the
  595. * passed CN, CAs, CRLs and security profile.
  596. * \return #MBEDTLS_ERR_X509_CERT_VERIFY_FAILED in case the
  597. * certificate chain verification failed. In this case,
  598. * \c *flags will have one or more
  599. * \c MBEDTLS_X509_BADCERT_XXX or \c MBEDTLS_X509_BADCRL_XXX
  600. * flags set.
  601. * \return Another negative error code in case of a fatal error
  602. * encountered during the verification process.
  603. */
  604. int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
  605. mbedtls_x509_crt *trust_ca,
  606. mbedtls_x509_crl *ca_crl,
  607. const mbedtls_x509_crt_profile *profile,
  608. const char *cn, uint32_t *flags,
  609. int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
  610. void *p_vrfy );
  611. /**
  612. * \brief Restartable version of \c mbedtls_crt_verify_with_profile()
  613. *
  614. * \note Performs the same job as \c mbedtls_crt_verify_with_profile()
  615. * but can return early and restart according to the limit
  616. * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
  617. *
  618. * \param crt The certificate chain to be verified.
  619. * \param trust_ca The list of trusted CAs.
  620. * \param ca_crl The list of CRLs for trusted CAs.
  621. * \param profile The security profile to use for the verification.
  622. * \param cn The expected Common Name. This may be \c NULL if the
  623. * CN need not be verified.
  624. * \param flags The address at which to store the result of the verification.
  625. * If the verification couldn't be completed, the flag value is
  626. * set to (uint32_t) -1.
  627. * \param f_vrfy The verification callback to use. See the documentation
  628. * of mbedtls_x509_crt_verify() for more information.
  629. * \param p_vrfy The context to be passed to \p f_vrfy.
  630. * \param rs_ctx The restart context to use. This may be set to \c NULL
  631. * to disable restartable ECC.
  632. *
  633. * \return See \c mbedtls_crt_verify_with_profile(), or
  634. * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
  635. * operations was reached: see \c mbedtls_ecp_set_max_ops().
  636. */
  637. int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
  638. mbedtls_x509_crt *trust_ca,
  639. mbedtls_x509_crl *ca_crl,
  640. const mbedtls_x509_crt_profile *profile,
  641. const char *cn, uint32_t *flags,
  642. int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
  643. void *p_vrfy,
  644. mbedtls_x509_crt_restart_ctx *rs_ctx );
  645. /**
  646. * \brief The type of trusted certificate callbacks.
  647. *
  648. * Callbacks of this type are passed to and used by the CRT
  649. * verification routine mbedtls_x509_crt_verify_with_ca_cb()
  650. * when looking for trusted signers of a given certificate.
  651. *
  652. * On success, the callback returns a list of trusted
  653. * certificates to be considered as potential signers
  654. * for the input certificate.
  655. *
  656. * \param p_ctx An opaque context passed to the callback.
  657. * \param child The certificate for which to search a potential signer.
  658. * This will point to a readable certificate.
  659. * \param candidate_cas The address at which to store the address of the first
  660. * entry in the generated linked list of candidate signers.
  661. * This will not be \c NULL.
  662. *
  663. * \note The callback must only return a non-zero value on a
  664. * fatal error. If, in contrast, the search for a potential
  665. * signer completes without a single candidate, the
  666. * callback must return \c 0 and set \c *candidate_cas
  667. * to \c NULL.
  668. *
  669. * \return \c 0 on success. In this case, \c *candidate_cas points
  670. * to a heap-allocated linked list of instances of
  671. * ::mbedtls_x509_crt, and ownership of this list is passed
  672. * to the caller.
  673. * \return A negative error code on failure.
  674. */
  675. typedef int (*mbedtls_x509_crt_ca_cb_t)( void *p_ctx,
  676. mbedtls_x509_crt const *child,
  677. mbedtls_x509_crt **candidate_cas );
  678. #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
  679. /**
  680. * \brief Version of \c mbedtls_x509_crt_verify_with_profile() which
  681. * uses a callback to acquire the list of trusted CA
  682. * certificates.
  683. *
  684. * \param crt The certificate chain to be verified.
  685. * \param f_ca_cb The callback to be used to query for potential signers
  686. * of a given child certificate. See the documentation of
  687. * ::mbedtls_x509_crt_ca_cb_t for more information.
  688. * \param p_ca_cb The opaque context to be passed to \p f_ca_cb.
  689. * \param profile The security profile for the verification.
  690. * \param cn The expected Common Name. This may be \c NULL if the
  691. * CN need not be verified.
  692. * \param flags The address at which to store the result of the verification.
  693. * If the verification couldn't be completed, the flag value is
  694. * set to (uint32_t) -1.
  695. * \param f_vrfy The verification callback to use. See the documentation
  696. * of mbedtls_x509_crt_verify() for more information.
  697. * \param p_vrfy The context to be passed to \p f_vrfy.
  698. *
  699. * \return See \c mbedtls_crt_verify_with_profile().
  700. */
  701. int mbedtls_x509_crt_verify_with_ca_cb( mbedtls_x509_crt *crt,
  702. mbedtls_x509_crt_ca_cb_t f_ca_cb,
  703. void *p_ca_cb,
  704. const mbedtls_x509_crt_profile *profile,
  705. const char *cn, uint32_t *flags,
  706. int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
  707. void *p_vrfy );
  708. #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
  709. #if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
  710. /**
  711. * \brief Check usage of certificate against keyUsage extension.
  712. *
  713. * \param crt Leaf certificate used.
  714. * \param usage Intended usage(s) (eg MBEDTLS_X509_KU_KEY_ENCIPHERMENT
  715. * before using the certificate to perform an RSA key
  716. * exchange).
  717. *
  718. * \note Except for decipherOnly and encipherOnly, a bit set in the
  719. * usage argument means this bit MUST be set in the
  720. * certificate. For decipherOnly and encipherOnly, it means
  721. * that bit MAY be set.
  722. *
  723. * \return 0 is these uses of the certificate are allowed,
  724. * MBEDTLS_ERR_X509_BAD_INPUT_DATA if the keyUsage extension
  725. * is present but does not match the usage argument.
  726. *
  727. * \note You should only call this function on leaf certificates, on
  728. * (intermediate) CAs the keyUsage extension is automatically
  729. * checked by \c mbedtls_x509_crt_verify().
  730. */
  731. int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
  732. unsigned int usage );
  733. #endif /* MBEDTLS_X509_CHECK_KEY_USAGE) */
  734. #if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
  735. /**
  736. * \brief Check usage of certificate against extendedKeyUsage.
  737. *
  738. * \param crt Leaf certificate used.
  739. * \param usage_oid Intended usage (eg MBEDTLS_OID_SERVER_AUTH or
  740. * MBEDTLS_OID_CLIENT_AUTH).
  741. * \param usage_len Length of usage_oid (eg given by MBEDTLS_OID_SIZE()).
  742. *
  743. * \return 0 if this use of the certificate is allowed,
  744. * MBEDTLS_ERR_X509_BAD_INPUT_DATA if not.
  745. *
  746. * \note Usually only makes sense on leaf certificates.
  747. */
  748. int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
  749. const char *usage_oid,
  750. size_t usage_len );
  751. #endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
  752. #if defined(MBEDTLS_X509_CRL_PARSE_C)
  753. /**
  754. * \brief Verify the certificate revocation status
  755. *
  756. * \param crt a certificate to be verified
  757. * \param crl the CRL to verify against
  758. *
  759. * \return 1 if the certificate is revoked, 0 otherwise
  760. *
  761. */
  762. int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl );
  763. #endif /* MBEDTLS_X509_CRL_PARSE_C */
  764. /**
  765. * \brief Initialize a certificate (chain)
  766. *
  767. * \param crt Certificate chain to initialize
  768. */
  769. void mbedtls_x509_crt_init( mbedtls_x509_crt *crt );
  770. /**
  771. * \brief Unallocate all certificate data
  772. *
  773. * \param crt Certificate chain to free
  774. */
  775. void mbedtls_x509_crt_free( mbedtls_x509_crt *crt );
  776. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  777. /**
  778. * \brief Initialize a restart context
  779. */
  780. void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx );
  781. /**
  782. * \brief Free the components of a restart context
  783. */
  784. void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx );
  785. #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
  786. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  787. /* \} name */
  788. /* \} addtogroup x509_module */
  789. #if defined(MBEDTLS_X509_CRT_WRITE_C)
  790. /**
  791. * \brief Initialize a CRT writing context
  792. *
  793. * \param ctx CRT context to initialize
  794. */
  795. void mbedtls_x509write_crt_init( mbedtls_x509write_cert *ctx );
  796. /**
  797. * \brief Set the verion for a Certificate
  798. * Default: MBEDTLS_X509_CRT_VERSION_3
  799. *
  800. * \param ctx CRT context to use
  801. * \param version version to set (MBEDTLS_X509_CRT_VERSION_1, MBEDTLS_X509_CRT_VERSION_2 or
  802. * MBEDTLS_X509_CRT_VERSION_3)
  803. */
  804. void mbedtls_x509write_crt_set_version( mbedtls_x509write_cert *ctx, int version );
  805. /**
  806. * \brief Set the serial number for a Certificate.
  807. *
  808. * \param ctx CRT context to use
  809. * \param serial serial number to set
  810. *
  811. * \return 0 if successful
  812. */
  813. int mbedtls_x509write_crt_set_serial( mbedtls_x509write_cert *ctx, const mbedtls_mpi *serial );
  814. /**
  815. * \brief Set the validity period for a Certificate
  816. * Timestamps should be in string format for UTC timezone
  817. * i.e. "YYYYMMDDhhmmss"
  818. * e.g. "20131231235959" for December 31st 2013
  819. * at 23:59:59
  820. *
  821. * \param ctx CRT context to use
  822. * \param not_before not_before timestamp
  823. * \param not_after not_after timestamp
  824. *
  825. * \return 0 if timestamp was parsed successfully, or
  826. * a specific error code
  827. */
  828. int mbedtls_x509write_crt_set_validity( mbedtls_x509write_cert *ctx, const char *not_before,
  829. const char *not_after );
  830. /**
  831. * \brief Set the issuer name for a Certificate
  832. * Issuer names should contain a comma-separated list
  833. * of OID types and values:
  834. * e.g. "C=UK,O=ARM,CN=mbed TLS CA"
  835. *
  836. * \param ctx CRT context to use
  837. * \param issuer_name issuer name to set
  838. *
  839. * \return 0 if issuer name was parsed successfully, or
  840. * a specific error code
  841. */
  842. int mbedtls_x509write_crt_set_issuer_name( mbedtls_x509write_cert *ctx,
  843. const char *issuer_name );
  844. /**
  845. * \brief Set the subject name for a Certificate
  846. * Subject names should contain a comma-separated list
  847. * of OID types and values:
  848. * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
  849. *
  850. * \param ctx CRT context to use
  851. * \param subject_name subject name to set
  852. *
  853. * \return 0 if subject name was parsed successfully, or
  854. * a specific error code
  855. */
  856. int mbedtls_x509write_crt_set_subject_name( mbedtls_x509write_cert *ctx,
  857. const char *subject_name );
  858. /**
  859. * \brief Set the subject public key for the certificate
  860. *
  861. * \param ctx CRT context to use
  862. * \param key public key to include
  863. */
  864. void mbedtls_x509write_crt_set_subject_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
  865. /**
  866. * \brief Set the issuer key used for signing the certificate
  867. *
  868. * \param ctx CRT context to use
  869. * \param key private key to sign with
  870. */
  871. void mbedtls_x509write_crt_set_issuer_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
  872. /**
  873. * \brief Set the MD algorithm to use for the signature
  874. * (e.g. MBEDTLS_MD_SHA1)
  875. *
  876. * \param ctx CRT context to use
  877. * \param md_alg MD algorithm to use
  878. */
  879. void mbedtls_x509write_crt_set_md_alg( mbedtls_x509write_cert *ctx, mbedtls_md_type_t md_alg );
  880. /**
  881. * \brief Generic function to add to or replace an extension in the
  882. * CRT
  883. *
  884. * \param ctx CRT context to use
  885. * \param oid OID of the extension
  886. * \param oid_len length of the OID
  887. * \param critical if the extension is critical (per the RFC's definition)
  888. * \param val value of the extension OCTET STRING
  889. * \param val_len length of the value data
  890. *
  891. * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
  892. */
  893. int mbedtls_x509write_crt_set_extension( mbedtls_x509write_cert *ctx,
  894. const char *oid, size_t oid_len,
  895. int critical,
  896. const unsigned char *val, size_t val_len );
  897. /**
  898. * \brief Set the basicConstraints extension for a CRT
  899. *
  900. * \param ctx CRT context to use
  901. * \param is_ca is this a CA certificate
  902. * \param max_pathlen maximum length of certificate chains below this
  903. * certificate (only for CA certificates, -1 is
  904. * inlimited)
  905. *
  906. * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
  907. */
  908. int mbedtls_x509write_crt_set_basic_constraints( mbedtls_x509write_cert *ctx,
  909. int is_ca, int max_pathlen );
  910. #if defined(MBEDTLS_SHA1_C)
  911. /**
  912. * \brief Set the subjectKeyIdentifier extension for a CRT
  913. * Requires that mbedtls_x509write_crt_set_subject_key() has been
  914. * called before
  915. *
  916. * \param ctx CRT context to use
  917. *
  918. * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
  919. */
  920. int mbedtls_x509write_crt_set_subject_key_identifier( mbedtls_x509write_cert *ctx );
  921. /**
  922. * \brief Set the authorityKeyIdentifier extension for a CRT
  923. * Requires that mbedtls_x509write_crt_set_issuer_key() has been
  924. * called before
  925. *
  926. * \param ctx CRT context to use
  927. *
  928. * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
  929. */
  930. int mbedtls_x509write_crt_set_authority_key_identifier( mbedtls_x509write_cert *ctx );
  931. #endif /* MBEDTLS_SHA1_C */
  932. /**
  933. * \brief Set the Key Usage Extension flags
  934. * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
  935. *
  936. * \param ctx CRT context to use
  937. * \param key_usage key usage flags to set
  938. *
  939. * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
  940. */
  941. int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx,
  942. unsigned int key_usage );
  943. /**
  944. * \brief Set the Netscape Cert Type flags
  945. * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
  946. *
  947. * \param ctx CRT context to use
  948. * \param ns_cert_type Netscape Cert Type flags to set
  949. *
  950. * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
  951. */
  952. int mbedtls_x509write_crt_set_ns_cert_type( mbedtls_x509write_cert *ctx,
  953. unsigned char ns_cert_type );
  954. /**
  955. * \brief Free the contents of a CRT write context
  956. *
  957. * \param ctx CRT context to free
  958. */
  959. void mbedtls_x509write_crt_free( mbedtls_x509write_cert *ctx );
  960. /**
  961. * \brief Write a built up certificate to a X509 DER structure
  962. * Note: data is written at the end of the buffer! Use the
  963. * return value to determine where you should start
  964. * using the buffer
  965. *
  966. * \param ctx certificate to write away
  967. * \param buf buffer to write to
  968. * \param size size of the buffer
  969. * \param f_rng RNG function (for signature, see note)
  970. * \param p_rng RNG parameter
  971. *
  972. * \return length of data written if successful, or a specific
  973. * error code
  974. *
  975. * \note f_rng may be NULL if RSA is used for signature and the
  976. * signature is made offline (otherwise f_rng is desirable
  977. * for countermeasures against timing attacks).
  978. * ECDSA signatures always require a non-NULL f_rng.
  979. */
  980. int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
  981. int (*f_rng)(void *, unsigned char *, size_t),
  982. void *p_rng );
  983. #if defined(MBEDTLS_PEM_WRITE_C)
  984. /**
  985. * \brief Write a built up certificate to a X509 PEM string
  986. *
  987. * \param ctx certificate to write away
  988. * \param buf buffer to write to
  989. * \param size size of the buffer
  990. * \param f_rng RNG function (for signature, see note)
  991. * \param p_rng RNG parameter
  992. *
  993. * \return 0 if successful, or a specific error code
  994. *
  995. * \note f_rng may be NULL if RSA is used for signature and the
  996. * signature is made offline (otherwise f_rng is desirable
  997. * for countermeasures against timing attacks).
  998. * ECDSA signatures always require a non-NULL f_rng.
  999. */
  1000. int mbedtls_x509write_crt_pem( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
  1001. int (*f_rng)(void *, unsigned char *, size_t),
  1002. void *p_rng );
  1003. #endif /* MBEDTLS_PEM_WRITE_C */
  1004. #endif /* MBEDTLS_X509_CRT_WRITE_C */
  1005. #ifdef __cplusplus
  1006. }
  1007. #endif
  1008. #endif /* mbedtls_x509_crt.h */