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