4 * \brief X.509 certificate parsing and writing
7 * Copyright The Mbed TLS Contributors
8 * SPDX-License-Identifier: Apache-2.0
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
14 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #ifndef MBEDTLS_X509_CRT_H
23 #define MBEDTLS_X509_CRT_H
25 #if !defined(MBEDTLS_CONFIG_FILE)
26 #include "mbedtls/config.h"
28 #include MBEDTLS_CONFIG_FILE
31 #include "mbedtls/x509.h"
32 #include "mbedtls/x509_crl.h"
33 #include "mbedtls/bignum.h"
36 * \addtogroup x509_module
45 * \name Structures and functions for parsing and writing X.509 certificates
50 * Container for an X.509 certificate. The certificate may be chained.
52 typedef struct mbedtls_x509_crt
{
53 int own_buffer
; /**< Indicates if \c raw is owned
54 * by the structure or not. */
55 mbedtls_x509_buf raw
; /**< The raw certificate data (DER). */
56 mbedtls_x509_buf tbs
; /**< The raw certificate body (DER). The part that is To Be Signed. */
58 int version
; /**< The X.509 version. (1=v1, 2=v2, 3=v3) */
59 mbedtls_x509_buf serial
; /**< Unique id for certificate issued by a specific CA. */
60 mbedtls_x509_buf sig_oid
; /**< Signature algorithm, e.g. sha1RSA */
62 mbedtls_x509_buf issuer_raw
; /**< The raw issuer data (DER). Used for quick comparison. */
63 mbedtls_x509_buf subject_raw
; /**< The raw subject data (DER). Used for quick comparison. */
65 mbedtls_x509_name issuer
; /**< The parsed issuer data (named information object). */
66 mbedtls_x509_name subject
; /**< The parsed subject data (named information object). */
68 mbedtls_x509_time valid_from
; /**< Start time of certificate validity. */
69 mbedtls_x509_time valid_to
; /**< End time of certificate validity. */
71 mbedtls_x509_buf pk_raw
;
72 mbedtls_pk_context pk
; /**< Container for the public key context. */
74 mbedtls_x509_buf issuer_id
; /**< Optional X.509 v2/v3 issuer unique identifier. */
75 mbedtls_x509_buf subject_id
; /**< Optional X.509 v2/v3 subject unique identifier. */
76 mbedtls_x509_buf v3_ext
; /**< Optional X.509 v3 extensions. */
77 mbedtls_x509_sequence subject_alt_names
; /**< Optional list of raw entries of Subject Alternative Names extension (currently only dNSName and OtherName are listed). */
79 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). */
81 int ext_types
; /**< Bit string containing detected and parsed extensions */
82 int ca_istrue
; /**< Optional Basic Constraint extension value: 1 if this certificate belongs to a CA, 0 otherwise. */
83 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+ */
85 unsigned int key_usage
; /**< Optional key usage extension value: See the values in x509.h */
87 mbedtls_x509_sequence ext_key_usage
; /**< Optional list of extended key usage OIDs. */
89 unsigned char ns_cert_type
; /**< Optional Netscape certificate type extension value: See the values in x509.h */
91 mbedtls_x509_buf sig
; /**< Signature: hash of the tbs part signed with the private key. */
92 mbedtls_md_type_t sig_md
; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
93 mbedtls_pk_type_t sig_pk
; /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
94 void *sig_opts
; /**< Signature options to be passed to mbedtls_pk_verify_ext(), e.g. for RSASSA-PSS */
96 struct mbedtls_x509_crt
*next
; /**< Next certificate in the CA-chain. */
101 * From RFC 5280 section 4.2.1.6:
102 * OtherName ::= SEQUENCE {
103 * type-id OBJECT IDENTIFIER,
104 * value [0] EXPLICIT ANY DEFINED BY type-id }
106 typedef struct mbedtls_x509_san_other_name
{
108 * The type_id is an OID as deifned in RFC 5280.
109 * To check the value of the type id, you should use
110 * \p MBEDTLS_OID_CMP with a known OID mbedtls_x509_buf.
112 mbedtls_x509_buf type_id
; /**< The type id. */
115 * From RFC 4108 section 5:
116 * HardwareModuleName ::= SEQUENCE {
117 * hwType OBJECT IDENTIFIER,
118 * hwSerialNum OCTET STRING }
121 mbedtls_x509_buf oid
; /**< The object identifier. */
122 mbedtls_x509_buf val
; /**< The named value. */
124 hardware_module_name
;
128 mbedtls_x509_san_other_name
;
131 * A structure for holding the parsed Subject Alternative Name, according to type
133 typedef struct mbedtls_x509_subject_alternative_name
{
134 int type
; /**< The SAN type, value of MBEDTLS_X509_SAN_XXX. */
136 mbedtls_x509_san_other_name other_name
; /**< The otherName supported type. */
137 mbedtls_x509_buf unstructured_name
; /**< The buffer for the un constructed types. Only dnsName currently supported */
139 san
; /**< A union of the supported SAN types */
141 mbedtls_x509_subject_alternative_name
;
144 * Build flag from an algorithm/curve identifier (pk, md, ecp)
145 * Since 0 is always XXX_NONE, ignore it.
147 #define MBEDTLS_X509_ID_FLAG( id ) ( 1 << ( (id) - 1 ) )
150 * Security profile for certificate verification.
152 * All lists are bitfields, built by ORing flags from MBEDTLS_X509_ID_FLAG().
154 typedef struct mbedtls_x509_crt_profile
{
155 uint32_t allowed_mds
; /**< MDs for signatures */
156 uint32_t allowed_pks
; /**< PK algs for signatures */
157 uint32_t allowed_curves
; /**< Elliptic curves for ECDSA */
158 uint32_t rsa_min_bitlen
; /**< Minimum size for RSA keys */
160 mbedtls_x509_crt_profile
;
162 #define MBEDTLS_X509_CRT_VERSION_1 0
163 #define MBEDTLS_X509_CRT_VERSION_2 1
164 #define MBEDTLS_X509_CRT_VERSION_3 2
166 #define MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN 32
167 #define MBEDTLS_X509_RFC5280_UTC_TIME_LEN 15
169 #if !defined( MBEDTLS_X509_MAX_FILE_PATH_LEN )
170 #define MBEDTLS_X509_MAX_FILE_PATH_LEN 512
174 * Container for writing a certificate (CRT)
176 typedef struct mbedtls_x509write_cert
{
179 mbedtls_pk_context
*subject_key
;
180 mbedtls_pk_context
*issuer_key
;
181 mbedtls_asn1_named_data
*subject
;
182 mbedtls_asn1_named_data
*issuer
;
183 mbedtls_md_type_t md_alg
;
184 char not_before
[MBEDTLS_X509_RFC5280_UTC_TIME_LEN
+ 1];
185 char not_after
[MBEDTLS_X509_RFC5280_UTC_TIME_LEN
+ 1];
186 mbedtls_asn1_named_data
*extensions
;
188 mbedtls_x509write_cert
;
191 * Item in a verification chain: cert and flags for it
194 mbedtls_x509_crt
*crt
;
196 } mbedtls_x509_crt_verify_chain_item
;
199 * Max size of verification chain: end-entity + intermediates + trusted root
201 #define MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
204 * Verification chain as built by \c mbedtls_crt_verify_chain()
207 mbedtls_x509_crt_verify_chain_item items
[MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE
];
210 #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
211 /* This stores the list of potential trusted signers obtained from
212 * the CA callback used for the CRT verification, if configured.
213 * We must track it somewhere because the callback passes its
214 * ownership to the caller. */
215 mbedtls_x509_crt
*trust_ca_cb_result
;
216 #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
217 } mbedtls_x509_crt_verify_chain
;
219 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
222 * \brief Context for resuming X.509 verify operations
225 /* for check_signature() */
226 mbedtls_pk_restart_ctx pk
;
228 /* for find_parent_in() */
229 mbedtls_x509_crt
*parent
; /* non-null iff parent_in in progress */
230 mbedtls_x509_crt
*fallback_parent
;
231 int fallback_signature_is_good
;
233 /* for find_parent() */
234 int parent_is_trusted
; /* -1 if find_parent is not in progress */
236 /* for verify_chain() */
239 x509_crt_rs_find_parent
,
240 } in_progress
; /* none if no operation is in progress */
242 mbedtls_x509_crt_verify_chain ver_chain
;
244 } mbedtls_x509_crt_restart_ctx
;
246 #else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
248 /* Now we can declare functions that take a pointer to that */
249 typedef void mbedtls_x509_crt_restart_ctx
;
251 #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
253 #if defined(MBEDTLS_X509_CRT_PARSE_C)
255 * Default security profile. Should provide a good balance between security
256 * and compatibility with current deployments.
258 extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default
;
261 * Expected next default profile. Recommended for new deployments.
262 * Currently targets a 128-bit security level, except for RSA-2048.
264 extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next
;
267 * NSA Suite B profile.
269 extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb
;
272 * \brief Parse a single DER formatted certificate and add it
273 * to the end of the provided chained list.
275 * \param chain The pointer to the start of the CRT chain to attach to.
276 * When parsing the first CRT in a chain, this should point
277 * to an instance of ::mbedtls_x509_crt initialized through
278 * mbedtls_x509_crt_init().
279 * \param buf The buffer holding the DER encoded certificate.
280 * \param buflen The size in Bytes of \p buf.
282 * \note This function makes an internal copy of the CRT buffer
283 * \p buf. In particular, \p buf may be destroyed or reused
284 * after this call returns. To avoid duplicating the CRT
285 * buffer (at the cost of stricter lifetime constraints),
286 * use mbedtls_x509_crt_parse_der_nocopy() instead.
288 * \return \c 0 if successful.
289 * \return A negative error code on failure.
291 int mbedtls_x509_crt_parse_der(mbedtls_x509_crt
*chain
,
292 const unsigned char *buf
,
296 * \brief The type of certificate extension callbacks.
298 * Callbacks of this type are passed to and used by the
299 * mbedtls_x509_crt_parse_der_with_ext_cb() routine when
300 * it encounters either an unsupported extension or a
301 * "certificate policies" extension containing any
302 * unsupported certificate policies.
303 * Future versions of the library may invoke the callback
304 * in other cases, if and when the need arises.
306 * \param p_ctx An opaque context passed to the callback.
307 * \param crt The certificate being parsed.
308 * \param oid The OID of the extension.
309 * \param critical Whether the extension is critical.
310 * \param p Pointer to the start of the extension value
311 * (the content of the OCTET STRING).
312 * \param end End of extension value.
314 * \note The callback must fail and return a negative error code
315 * if it can not parse or does not support the extension.
316 * When the callback fails to parse a critical extension
317 * mbedtls_x509_crt_parse_der_with_ext_cb() also fails.
318 * When the callback fails to parse a non critical extension
319 * mbedtls_x509_crt_parse_der_with_ext_cb() simply skips
320 * the extension and continues parsing.
322 * \return \c 0 on success.
323 * \return A negative error code on failure.
325 typedef int (*mbedtls_x509_crt_ext_cb_t
)(void *p_ctx
,
326 mbedtls_x509_crt
const *crt
,
327 mbedtls_x509_buf
const *oid
,
329 const unsigned char *p
,
330 const unsigned char *end
);
333 * \brief Parse a single DER formatted certificate and add it
334 * to the end of the provided chained list.
336 * \param chain The pointer to the start of the CRT chain to attach to.
337 * When parsing the first CRT in a chain, this should point
338 * to an instance of ::mbedtls_x509_crt initialized through
339 * mbedtls_x509_crt_init().
340 * \param buf The buffer holding the DER encoded certificate.
341 * \param buflen The size in Bytes of \p buf.
342 * \param make_copy When not zero this function makes an internal copy of the
343 * CRT buffer \p buf. In particular, \p buf may be destroyed
344 * or reused after this call returns.
345 * When zero this function avoids duplicating the CRT buffer
346 * by taking temporary ownership thereof until the CRT
347 * is destroyed (like mbedtls_x509_crt_parse_der_nocopy())
348 * \param cb A callback invoked for every unsupported certificate
350 * \param p_ctx An opaque context passed to the callback.
352 * \note This call is functionally equivalent to
353 * mbedtls_x509_crt_parse_der(), and/or
354 * mbedtls_x509_crt_parse_der_nocopy()
355 * but it calls the callback with every unsupported
356 * certificate extension and additionally the
357 * "certificate policies" extension if it contains any
358 * unsupported certificate policies.
359 * The callback must return a negative error code if it
360 * does not know how to handle such an extension.
361 * When the callback fails to parse a critical extension
362 * mbedtls_x509_crt_parse_der_with_ext_cb() also fails.
363 * When the callback fails to parse a non critical extension
364 * mbedtls_x509_crt_parse_der_with_ext_cb() simply skips
365 * the extension and continues parsing.
366 * Future versions of the library may invoke the callback
367 * in other cases, if and when the need arises.
369 * \return \c 0 if successful.
370 * \return A negative error code on failure.
372 int mbedtls_x509_crt_parse_der_with_ext_cb(mbedtls_x509_crt
*chain
,
373 const unsigned char *buf
,
376 mbedtls_x509_crt_ext_cb_t cb
,
380 * \brief Parse a single DER formatted certificate and add it
381 * to the end of the provided chained list. This is a
382 * variant of mbedtls_x509_crt_parse_der() which takes
383 * temporary ownership of the CRT buffer until the CRT
386 * \param chain The pointer to the start of the CRT chain to attach to.
387 * When parsing the first CRT in a chain, this should point
388 * to an instance of ::mbedtls_x509_crt initialized through
389 * mbedtls_x509_crt_init().
390 * \param buf The address of the readable buffer holding the DER encoded
391 * certificate to use. On success, this buffer must be
392 * retained and not be changed for the liftetime of the
393 * CRT chain \p chain, that is, until \p chain is destroyed
394 * through a call to mbedtls_x509_crt_free().
395 * \param buflen The size in Bytes of \p buf.
397 * \note This call is functionally equivalent to
398 * mbedtls_x509_crt_parse_der(), but it avoids creating a
399 * copy of the input buffer at the cost of stronger lifetime
400 * constraints. This is useful in constrained environments
401 * where duplication of the CRT cannot be tolerated.
403 * \return \c 0 if successful.
404 * \return A negative error code on failure.
406 int mbedtls_x509_crt_parse_der_nocopy(mbedtls_x509_crt
*chain
,
407 const unsigned char *buf
,
411 * \brief Parse one DER-encoded or one or more concatenated PEM-encoded
412 * certificates and add them to the chained list.
414 * For CRTs in PEM encoding, the function parses permissively:
415 * if at least one certificate can be parsed, the function
416 * returns the number of certificates for which parsing failed
417 * (hence \c 0 if all certificates were parsed successfully).
418 * If no certificate could be parsed, the function returns
419 * the first (negative) error encountered during parsing.
421 * PEM encoded certificates may be interleaved by other data
422 * such as human readable descriptions of their content, as
423 * long as the certificates are enclosed in the PEM specific
424 * '-----{BEGIN/END} CERTIFICATE-----' delimiters.
426 * \param chain The chain to which to add the parsed certificates.
427 * \param buf The buffer holding the certificate data in PEM or DER format.
428 * For certificates in PEM encoding, this may be a concatenation
429 * of multiple certificates; for DER encoding, the buffer must
430 * comprise exactly one certificate.
431 * \param buflen The size of \p buf, including the terminating \c NULL byte
432 * in case of PEM encoded data.
434 * \return \c 0 if all certificates were parsed successfully.
435 * \return The (positive) number of certificates that couldn't
436 * be parsed if parsing was partly successful (see above).
437 * \return A negative X509 or PEM error code otherwise.
440 int mbedtls_x509_crt_parse(mbedtls_x509_crt
*chain
, const unsigned char *buf
, size_t buflen
);
442 #if defined(MBEDTLS_FS_IO)
444 * \brief Load one or more certificates and add them
445 * to the chained list. Parses permissively. If some
446 * certificates can be parsed, the result is the number
447 * of failed certificates it encountered. If none complete
448 * correctly, the first error is returned.
450 * \param chain points to the start of the chain
451 * \param path filename to read the certificates from
453 * \return 0 if all certificates parsed successfully, a positive number
454 * if partly successful or a specific X509 or PEM error code
456 int mbedtls_x509_crt_parse_file(mbedtls_x509_crt
*chain
, const char *path
);
459 * \brief Load one or more certificate files from a path and add them
460 * to the chained list. Parses permissively. If some
461 * certificates can be parsed, the result is the number
462 * of failed certificates it encountered. If none complete
463 * correctly, the first error is returned.
465 * \param chain points to the start of the chain
466 * \param path directory / folder to read the certificate files from
468 * \return 0 if all certificates parsed successfully, a positive number
469 * if partly successful or a specific X509 or PEM error code
471 int mbedtls_x509_crt_parse_path(mbedtls_x509_crt
*chain
, const char *path
);
473 #endif /* MBEDTLS_FS_IO */
475 * \brief This function parses an item in the SubjectAlternativeNames
478 * \param san_buf The buffer holding the raw data item of the subject
480 * \param san The target structure to populate with the parsed presentation
481 * of the subject alternative name encoded in \p san_raw.
483 * \note Only "dnsName" and "otherName" of type hardware_module_name
484 * as defined in RFC 4180 is supported.
486 * \note This function should be called on a single raw data of
487 * subject alternative name. For example, after successful
488 * certificate parsing, one must iterate on every item in the
489 * \p crt->subject_alt_names sequence, and pass it to
492 * \warning The target structure contains pointers to the raw data of the
493 * parsed certificate, and its lifetime is restricted by the
494 * lifetime of the certificate.
496 * \return \c 0 on success
497 * \return #MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE for an unsupported
499 * \return Another negative value for any other failure.
501 int mbedtls_x509_parse_subject_alt_name(const mbedtls_x509_buf
*san_buf
,
502 mbedtls_x509_subject_alternative_name
*san
);
504 * \brief Returns an informational string about the
507 * \param buf Buffer to write to
508 * \param size Maximum size of buffer
509 * \param prefix A line prefix
510 * \param crt The X509 certificate to represent
512 * \return The length of the string written (not including the
513 * terminated nul byte), or a negative error code.
515 int mbedtls_x509_crt_info(char *buf
, size_t size
, const char *prefix
,
516 const mbedtls_x509_crt
*crt
);
519 * \brief Returns an informational string about the
520 * verification status of a certificate.
522 * \param buf Buffer to write to
523 * \param size Maximum size of buffer
524 * \param prefix A line prefix
525 * \param flags Verification flags created by mbedtls_x509_crt_verify()
527 * \return The length of the string written (not including the
528 * terminated nul byte), or a negative error code.
530 int mbedtls_x509_crt_verify_info(char *buf
, size_t size
, const char *prefix
,
534 * \brief Verify a chain of certificates.
536 * The verify callback is a user-supplied callback that
537 * can clear / modify / add flags for a certificate. If set,
538 * the verification callback is called for each
539 * certificate in the chain (from the trust-ca down to the
540 * presented crt). The parameters for the callback are:
541 * (void *parameter, mbedtls_x509_crt *crt, int certificate_depth,
542 * int *flags). With the flags representing current flags for
543 * that specific certificate and the certificate depth from
544 * the bottom (Peer cert depth = 0).
546 * All flags left after returning from the callback
547 * are also returned to the application. The function should
548 * return 0 for anything (including invalid certificates)
549 * other than fatal error, as a non-zero return code
550 * immediately aborts the verification process. For fatal
551 * errors, a specific error code should be used (different
552 * from MBEDTLS_ERR_X509_CERT_VERIFY_FAILED which should not
553 * be returned at this point), or MBEDTLS_ERR_X509_FATAL_ERROR
554 * can be used if no better code is available.
556 * \note In case verification failed, the results can be displayed
557 * using \c mbedtls_x509_crt_verify_info()
559 * \note Same as \c mbedtls_x509_crt_verify_with_profile() with the
560 * default security profile.
562 * \note It is your responsibility to provide up-to-date CRLs for
563 * all trusted CAs. If no CRL is provided for the CA that was
564 * used to sign the certificate, CRL verification is skipped
565 * silently, that is *without* setting any flag.
567 * \note The \c trust_ca list can contain two types of certificates:
568 * (1) those of trusted root CAs, so that certificates
569 * chaining up to those CAs will be trusted, and (2)
570 * self-signed end-entity certificates to be trusted (for
571 * specific peers you know) - in that case, the self-signed
572 * certificate doesn't need to have the CA bit set.
574 * \param crt The certificate chain to be verified.
575 * \param trust_ca The list of trusted CAs.
576 * \param ca_crl The list of CRLs for trusted CAs.
577 * \param cn The expected Common Name. This will be checked to be
578 * present in the certificate's subjectAltNames extension or,
579 * if this extension is absent, as a CN component in its
580 * Subject name. Currently only DNS names are supported. This
581 * may be \c NULL if the CN need not be verified.
582 * \param flags The address at which to store the result of the verification.
583 * If the verification couldn't be completed, the flag value is
584 * set to (uint32_t) -1.
585 * \param f_vrfy The verification callback to use. See the documentation
586 * of mbedtls_x509_crt_verify() for more information.
587 * \param p_vrfy The context to be passed to \p f_vrfy.
589 * \return \c 0 if the chain is valid with respect to the
590 * passed CN, CAs, CRLs and security profile.
591 * \return #MBEDTLS_ERR_X509_CERT_VERIFY_FAILED in case the
592 * certificate chain verification failed. In this case,
593 * \c *flags will have one or more
594 * \c MBEDTLS_X509_BADCERT_XXX or \c MBEDTLS_X509_BADCRL_XXX
596 * \return Another negative error code in case of a fatal error
597 * encountered during the verification process.
599 int mbedtls_x509_crt_verify(mbedtls_x509_crt
*crt
,
600 mbedtls_x509_crt
*trust_ca
,
601 mbedtls_x509_crl
*ca_crl
,
602 const char *cn
, uint32_t *flags
,
603 int (*f_vrfy
)(void *, mbedtls_x509_crt
*, int, uint32_t *),
607 * \brief Verify a chain of certificates with respect to
608 * a configurable security profile.
610 * \note Same as \c mbedtls_x509_crt_verify(), but with explicit
613 * \note The restrictions on keys (RSA minimum size, allowed curves
614 * for ECDSA) apply to all certificates: trusted root,
615 * intermediate CAs if any, and end entity certificate.
617 * \param crt The certificate chain to be verified.
618 * \param trust_ca The list of trusted CAs.
619 * \param ca_crl The list of CRLs for trusted CAs.
620 * \param profile The security profile to use for the verification.
621 * \param cn The expected Common Name. This may be \c NULL if the
622 * CN need not be verified.
623 * \param flags The address at which to store the result of the verification.
624 * If the verification couldn't be completed, the flag value is
625 * set to (uint32_t) -1.
626 * \param f_vrfy The verification callback to use. See the documentation
627 * of mbedtls_x509_crt_verify() for more information.
628 * \param p_vrfy The context to be passed to \p f_vrfy.
630 * \return \c 0 if the chain is valid with respect to the
631 * passed CN, CAs, CRLs and security profile.
632 * \return #MBEDTLS_ERR_X509_CERT_VERIFY_FAILED in case the
633 * certificate chain verification failed. In this case,
634 * \c *flags will have one or more
635 * \c MBEDTLS_X509_BADCERT_XXX or \c MBEDTLS_X509_BADCRL_XXX
637 * \return Another negative error code in case of a fatal error
638 * encountered during the verification process.
640 int mbedtls_x509_crt_verify_with_profile(mbedtls_x509_crt
*crt
,
641 mbedtls_x509_crt
*trust_ca
,
642 mbedtls_x509_crl
*ca_crl
,
643 const mbedtls_x509_crt_profile
*profile
,
644 const char *cn
, uint32_t *flags
,
645 int (*f_vrfy
)(void *, mbedtls_x509_crt
*, int, uint32_t *),
649 * \brief Restartable version of \c mbedtls_crt_verify_with_profile()
651 * \note Performs the same job as \c mbedtls_crt_verify_with_profile()
652 * but can return early and restart according to the limit
653 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
655 * \param crt The certificate chain to be verified.
656 * \param trust_ca The list of trusted CAs.
657 * \param ca_crl The list of CRLs for trusted CAs.
658 * \param profile The security profile to use for the verification.
659 * \param cn The expected Common Name. This may be \c NULL if the
660 * CN need not be verified.
661 * \param flags The address at which to store the result of the verification.
662 * If the verification couldn't be completed, the flag value is
663 * set to (uint32_t) -1.
664 * \param f_vrfy The verification callback to use. See the documentation
665 * of mbedtls_x509_crt_verify() for more information.
666 * \param p_vrfy The context to be passed to \p f_vrfy.
667 * \param rs_ctx The restart context to use. This may be set to \c NULL
668 * to disable restartable ECC.
670 * \return See \c mbedtls_crt_verify_with_profile(), or
671 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
672 * operations was reached: see \c mbedtls_ecp_set_max_ops().
674 int mbedtls_x509_crt_verify_restartable(mbedtls_x509_crt
*crt
,
675 mbedtls_x509_crt
*trust_ca
,
676 mbedtls_x509_crl
*ca_crl
,
677 const mbedtls_x509_crt_profile
*profile
,
678 const char *cn
, uint32_t *flags
,
679 int (*f_vrfy
)(void *, mbedtls_x509_crt
*, int, uint32_t *),
681 mbedtls_x509_crt_restart_ctx
*rs_ctx
);
684 * \brief The type of trusted certificate callbacks.
686 * Callbacks of this type are passed to and used by the CRT
687 * verification routine mbedtls_x509_crt_verify_with_ca_cb()
688 * when looking for trusted signers of a given certificate.
690 * On success, the callback returns a list of trusted
691 * certificates to be considered as potential signers
692 * for the input certificate.
694 * \param p_ctx An opaque context passed to the callback.
695 * \param child The certificate for which to search a potential signer.
696 * This will point to a readable certificate.
697 * \param candidate_cas The address at which to store the address of the first
698 * entry in the generated linked list of candidate signers.
699 * This will not be \c NULL.
701 * \note The callback must only return a non-zero value on a
702 * fatal error. If, in contrast, the search for a potential
703 * signer completes without a single candidate, the
704 * callback must return \c 0 and set \c *candidate_cas
707 * \return \c 0 on success. In this case, \c *candidate_cas points
708 * to a heap-allocated linked list of instances of
709 * ::mbedtls_x509_crt, and ownership of this list is passed
711 * \return A negative error code on failure.
713 typedef int (*mbedtls_x509_crt_ca_cb_t
)(void *p_ctx
,
714 mbedtls_x509_crt
const *child
,
715 mbedtls_x509_crt
**candidate_cas
);
717 #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
719 * \brief Version of \c mbedtls_x509_crt_verify_with_profile() which
720 * uses a callback to acquire the list of trusted CA
723 * \param crt The certificate chain to be verified.
724 * \param f_ca_cb The callback to be used to query for potential signers
725 * of a given child certificate. See the documentation of
726 * ::mbedtls_x509_crt_ca_cb_t for more information.
727 * \param p_ca_cb The opaque context to be passed to \p f_ca_cb.
728 * \param profile The security profile for the verification.
729 * \param cn The expected Common Name. This may be \c NULL if the
730 * CN need not be verified.
731 * \param flags The address at which to store the result of the verification.
732 * If the verification couldn't be completed, the flag value is
733 * set to (uint32_t) -1.
734 * \param f_vrfy The verification callback to use. See the documentation
735 * of mbedtls_x509_crt_verify() for more information.
736 * \param p_vrfy The context to be passed to \p f_vrfy.
738 * \return See \c mbedtls_crt_verify_with_profile().
740 int mbedtls_x509_crt_verify_with_ca_cb(mbedtls_x509_crt
*crt
,
741 mbedtls_x509_crt_ca_cb_t f_ca_cb
,
743 const mbedtls_x509_crt_profile
*profile
,
744 const char *cn
, uint32_t *flags
,
745 int (*f_vrfy
)(void *, mbedtls_x509_crt
*, int, uint32_t *),
748 #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
750 #if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
752 * \brief Check usage of certificate against keyUsage extension.
754 * \param crt Leaf certificate used.
755 * \param usage Intended usage(s) (eg MBEDTLS_X509_KU_KEY_ENCIPHERMENT
756 * before using the certificate to perform an RSA key
759 * \note Except for decipherOnly and encipherOnly, a bit set in the
760 * usage argument means this bit MUST be set in the
761 * certificate. For decipherOnly and encipherOnly, it means
762 * that bit MAY be set.
764 * \return 0 is these uses of the certificate are allowed,
765 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if the keyUsage extension
766 * is present but does not match the usage argument.
768 * \note You should only call this function on leaf certificates, on
769 * (intermediate) CAs the keyUsage extension is automatically
770 * checked by \c mbedtls_x509_crt_verify().
772 int mbedtls_x509_crt_check_key_usage(const mbedtls_x509_crt
*crt
,
774 #endif /* MBEDTLS_X509_CHECK_KEY_USAGE) */
776 #if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
778 * \brief Check usage of certificate against extendedKeyUsage.
780 * \param crt Leaf certificate used.
781 * \param usage_oid Intended usage (eg MBEDTLS_OID_SERVER_AUTH or
782 * MBEDTLS_OID_CLIENT_AUTH).
783 * \param usage_len Length of usage_oid (eg given by MBEDTLS_OID_SIZE()).
785 * \return 0 if this use of the certificate is allowed,
786 * MBEDTLS_ERR_X509_BAD_INPUT_DATA if not.
788 * \note Usually only makes sense on leaf certificates.
790 int mbedtls_x509_crt_check_extended_key_usage(const mbedtls_x509_crt
*crt
,
791 const char *usage_oid
,
793 #endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
795 #if defined(MBEDTLS_X509_CRL_PARSE_C)
797 * \brief Verify the certificate revocation status
799 * \param crt a certificate to be verified
800 * \param crl the CRL to verify against
802 * \return 1 if the certificate is revoked, 0 otherwise
805 int mbedtls_x509_crt_is_revoked(const mbedtls_x509_crt
*crt
, const mbedtls_x509_crl
*crl
);
806 #endif /* MBEDTLS_X509_CRL_PARSE_C */
809 * \brief Initialize a certificate (chain)
811 * \param crt Certificate chain to initialize
813 void mbedtls_x509_crt_init(mbedtls_x509_crt
*crt
);
816 * \brief Unallocate all certificate data
818 * \param crt Certificate chain to free
820 void mbedtls_x509_crt_free(mbedtls_x509_crt
*crt
);
822 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
824 * \brief Initialize a restart context
826 void mbedtls_x509_crt_restart_init(mbedtls_x509_crt_restart_ctx
*ctx
);
829 * \brief Free the components of a restart context
831 void mbedtls_x509_crt_restart_free(mbedtls_x509_crt_restart_ctx
*ctx
);
832 #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
833 #endif /* MBEDTLS_X509_CRT_PARSE_C */
836 /* \} addtogroup x509_module */
838 #if defined(MBEDTLS_X509_CRT_WRITE_C)
840 * \brief Initialize a CRT writing context
842 * \param ctx CRT context to initialize
844 void mbedtls_x509write_crt_init(mbedtls_x509write_cert
*ctx
);
847 * \brief Set the verion for a Certificate
848 * Default: MBEDTLS_X509_CRT_VERSION_3
850 * \param ctx CRT context to use
851 * \param version version to set (MBEDTLS_X509_CRT_VERSION_1, MBEDTLS_X509_CRT_VERSION_2 or
852 * MBEDTLS_X509_CRT_VERSION_3)
854 void mbedtls_x509write_crt_set_version(mbedtls_x509write_cert
*ctx
, int version
);
857 * \brief Set the serial number for a Certificate.
859 * \param ctx CRT context to use
860 * \param serial serial number to set
862 * \return 0 if successful
864 int mbedtls_x509write_crt_set_serial(mbedtls_x509write_cert
*ctx
, const mbedtls_mpi
*serial
);
867 * \brief Set the validity period for a Certificate
868 * Timestamps should be in string format for UTC timezone
869 * i.e. "YYYYMMDDhhmmss"
870 * e.g. "20131231235959" for December 31st 2013
873 * \param ctx CRT context to use
874 * \param not_before not_before timestamp
875 * \param not_after not_after timestamp
877 * \return 0 if timestamp was parsed successfully, or
878 * a specific error code
880 int mbedtls_x509write_crt_set_validity(mbedtls_x509write_cert
*ctx
, const char *not_before
,
881 const char *not_after
);
884 * \brief Set the issuer name for a Certificate
885 * Issuer names should contain a comma-separated list
886 * of OID types and values:
887 * e.g. "C=UK,O=ARM,CN=mbed TLS CA"
889 * \param ctx CRT context to use
890 * \param issuer_name issuer name to set
892 * \return 0 if issuer name was parsed successfully, or
893 * a specific error code
895 int mbedtls_x509write_crt_set_issuer_name(mbedtls_x509write_cert
*ctx
,
896 const char *issuer_name
);
899 * \brief Set the subject name for a Certificate
900 * Subject names should contain a comma-separated list
901 * of OID types and values:
902 * e.g. "C=UK,O=ARM,CN=mbed TLS Server 1"
904 * \param ctx CRT context to use
905 * \param subject_name subject name to set
907 * \return 0 if subject name was parsed successfully, or
908 * a specific error code
910 int mbedtls_x509write_crt_set_subject_name(mbedtls_x509write_cert
*ctx
,
911 const char *subject_name
);
914 * \brief Set the subject public key for the certificate
916 * \param ctx CRT context to use
917 * \param key public key to include
919 void mbedtls_x509write_crt_set_subject_key(mbedtls_x509write_cert
*ctx
, mbedtls_pk_context
*key
);
922 * \brief Set the issuer key used for signing the certificate
924 * \param ctx CRT context to use
925 * \param key private key to sign with
927 void mbedtls_x509write_crt_set_issuer_key(mbedtls_x509write_cert
*ctx
, mbedtls_pk_context
*key
);
930 * \brief Set the MD algorithm to use for the signature
931 * (e.g. MBEDTLS_MD_SHA1)
933 * \param ctx CRT context to use
934 * \param md_alg MD algorithm to use
936 void mbedtls_x509write_crt_set_md_alg(mbedtls_x509write_cert
*ctx
, mbedtls_md_type_t md_alg
);
939 * \brief Generic function to add to or replace an extension in the
942 * \param ctx CRT context to use
943 * \param oid OID of the extension
944 * \param oid_len length of the OID
945 * \param critical if the extension is critical (per the RFC's definition)
946 * \param val value of the extension OCTET STRING
947 * \param val_len length of the value data
949 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
951 int mbedtls_x509write_crt_set_extension(mbedtls_x509write_cert
*ctx
,
952 const char *oid
, size_t oid_len
,
954 const unsigned char *val
, size_t val_len
);
957 * \brief Set the basicConstraints extension for a CRT
959 * \param ctx CRT context to use
960 * \param is_ca is this a CA certificate
961 * \param max_pathlen maximum length of certificate chains below this
962 * certificate (only for CA certificates, -1 is
965 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
967 int mbedtls_x509write_crt_set_basic_constraints(mbedtls_x509write_cert
*ctx
,
968 int is_ca
, int max_pathlen
);
970 #if defined(MBEDTLS_SHA1_C)
972 * \brief Set the subjectKeyIdentifier extension for a CRT
973 * Requires that mbedtls_x509write_crt_set_subject_key() has been
976 * \param ctx CRT context to use
978 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
980 int mbedtls_x509write_crt_set_subject_key_identifier(mbedtls_x509write_cert
*ctx
);
983 * \brief Set the authorityKeyIdentifier extension for a CRT
984 * Requires that mbedtls_x509write_crt_set_issuer_key() has been
987 * \param ctx CRT context to use
989 * \return 0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
991 int mbedtls_x509write_crt_set_authority_key_identifier(mbedtls_x509write_cert
*ctx
);
992 #endif /* MBEDTLS_SHA1_C */
995 * \brief Set the Key Usage Extension flags
996 * (e.g. MBEDTLS_X509_KU_DIGITAL_SIGNATURE | MBEDTLS_X509_KU_KEY_CERT_SIGN)
998 * \param ctx CRT context to use
999 * \param key_usage key usage flags to set
1001 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
1003 int mbedtls_x509write_crt_set_key_usage(mbedtls_x509write_cert
*ctx
,
1004 unsigned int key_usage
);
1007 * \brief Set the Netscape Cert Type flags
1008 * (e.g. MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT | MBEDTLS_X509_NS_CERT_TYPE_EMAIL)
1010 * \param ctx CRT context to use
1011 * \param ns_cert_type Netscape Cert Type flags to set
1013 * \return 0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
1015 int mbedtls_x509write_crt_set_ns_cert_type(mbedtls_x509write_cert
*ctx
,
1016 unsigned char ns_cert_type
);
1019 * \brief Free the contents of a CRT write context
1021 * \param ctx CRT context to free
1023 void mbedtls_x509write_crt_free(mbedtls_x509write_cert
*ctx
);
1026 * \brief Write a built up certificate to a X509 DER structure
1027 * Note: data is written at the end of the buffer! Use the
1028 * return value to determine where you should start
1031 * \param ctx certificate to write away
1032 * \param buf buffer to write to
1033 * \param size size of the buffer
1034 * \param f_rng RNG function (for signature, see note)
1035 * \param p_rng RNG parameter
1037 * \return length of data written if successful, or a specific
1040 * \note f_rng may be NULL if RSA is used for signature and the
1041 * signature is made offline (otherwise f_rng is desirable
1042 * for countermeasures against timing attacks).
1043 * ECDSA signatures always require a non-NULL f_rng.
1045 int mbedtls_x509write_crt_der(mbedtls_x509write_cert
*ctx
, unsigned char *buf
, size_t size
,
1046 int (*f_rng
)(void *, unsigned char *, size_t),
1049 #if defined(MBEDTLS_PEM_WRITE_C)
1051 * \brief Write a built up certificate to a X509 PEM string
1053 * \param ctx certificate to write away
1054 * \param buf buffer to write to
1055 * \param size size of the buffer
1056 * \param f_rng RNG function (for signature, see note)
1057 * \param p_rng RNG parameter
1059 * \return 0 if successful, or a specific error code
1061 * \note f_rng may be NULL if RSA is used for signature and the
1062 * signature is made offline (otherwise f_rng is desirable
1063 * for countermeasures against timing attacks).
1064 * ECDSA signatures always require a non-NULL f_rng.
1066 int mbedtls_x509write_crt_pem(mbedtls_x509write_cert
*ctx
, unsigned char *buf
, size_t size
,
1067 int (*f_rng
)(void *, unsigned char *, size_t),
1069 #endif /* MBEDTLS_PEM_WRITE_C */
1070 #endif /* MBEDTLS_X509_CRT_WRITE_C */
1076 #endif /* mbedtls_x509_crt.h */