1 /*
2  *  X.509 certificate writing
3  *
4  *  Copyright The Mbed TLS Contributors
5  *  SPDX-License-Identifier: Apache-2.0
6  *
7  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
8  *  not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *
11  *  http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  */
19 /*
20  * References:
21  * - certificates: RFC 5280, updated by RFC 6818
22  * - CSRs: PKCS#10 v1.7 aka RFC 2986
23  * - attributes: PKCS#9 v2.0 aka RFC 2985
24  */
25 
26 #include "common.h"
27 
28 #if defined(MBEDTLS_X509_CRT_WRITE_C)
29 
30 #include "mbedtls/x509_crt.h"
31 #include "mbedtls/asn1write.h"
32 #include "mbedtls/error.h"
33 #include "mbedtls/oid.h"
34 #include "mbedtls/platform_util.h"
35 #include "mbedtls/sha1.h"
36 
37 #include <string.h>
38 
39 #if defined(MBEDTLS_PEM_WRITE_C)
40 #include "mbedtls/pem.h"
41 #endif /* MBEDTLS_PEM_WRITE_C */
42 
43 #if defined(MBEDTLS_USE_PSA_CRYPTO)
44 #include "psa/crypto.h"
45 #include "mbedtls/psa_util.h"
46 #endif /* MBEDTLS_USE_PSA_CRYPTO */
47 
48 #include "hash_info.h"
49 #include "mbedtls/legacy_or_psa.h"
50 
mbedtls_x509write_crt_init(mbedtls_x509write_cert * ctx)51 void mbedtls_x509write_crt_init( mbedtls_x509write_cert *ctx )
52 {
53     memset( ctx, 0, sizeof( mbedtls_x509write_cert ) );
54 
55     mbedtls_mpi_init( &ctx->serial );
56     ctx->version = MBEDTLS_X509_CRT_VERSION_3;
57 }
58 
mbedtls_x509write_crt_free(mbedtls_x509write_cert * ctx)59 void mbedtls_x509write_crt_free( mbedtls_x509write_cert *ctx )
60 {
61     mbedtls_mpi_free( &ctx->serial );
62 
63     mbedtls_asn1_free_named_data_list( &ctx->subject );
64     mbedtls_asn1_free_named_data_list( &ctx->issuer );
65     mbedtls_asn1_free_named_data_list( &ctx->extensions );
66 
67     mbedtls_platform_zeroize( ctx, sizeof( mbedtls_x509write_cert ) );
68 }
69 
mbedtls_x509write_crt_set_version(mbedtls_x509write_cert * ctx,int version)70 void mbedtls_x509write_crt_set_version( mbedtls_x509write_cert *ctx,
71                                         int version )
72 {
73     ctx->version = version;
74 }
75 
mbedtls_x509write_crt_set_md_alg(mbedtls_x509write_cert * ctx,mbedtls_md_type_t md_alg)76 void mbedtls_x509write_crt_set_md_alg( mbedtls_x509write_cert *ctx,
77                                        mbedtls_md_type_t md_alg )
78 {
79     ctx->md_alg = md_alg;
80 }
81 
mbedtls_x509write_crt_set_subject_key(mbedtls_x509write_cert * ctx,mbedtls_pk_context * key)82 void mbedtls_x509write_crt_set_subject_key( mbedtls_x509write_cert *ctx,
83                                             mbedtls_pk_context *key )
84 {
85     ctx->subject_key = key;
86 }
87 
mbedtls_x509write_crt_set_issuer_key(mbedtls_x509write_cert * ctx,mbedtls_pk_context * key)88 void mbedtls_x509write_crt_set_issuer_key( mbedtls_x509write_cert *ctx,
89                                            mbedtls_pk_context *key )
90 {
91     ctx->issuer_key = key;
92 }
93 
mbedtls_x509write_crt_set_subject_name(mbedtls_x509write_cert * ctx,const char * subject_name)94 int mbedtls_x509write_crt_set_subject_name( mbedtls_x509write_cert *ctx,
95                                             const char *subject_name )
96 {
97     return mbedtls_x509_string_to_names( &ctx->subject, subject_name );
98 }
99 
mbedtls_x509write_crt_set_issuer_name(mbedtls_x509write_cert * ctx,const char * issuer_name)100 int mbedtls_x509write_crt_set_issuer_name( mbedtls_x509write_cert *ctx,
101                                            const char *issuer_name )
102 {
103     return mbedtls_x509_string_to_names( &ctx->issuer, issuer_name );
104 }
105 
mbedtls_x509write_crt_set_serial(mbedtls_x509write_cert * ctx,const mbedtls_mpi * serial)106 int mbedtls_x509write_crt_set_serial( mbedtls_x509write_cert *ctx,
107                                       const mbedtls_mpi *serial )
108 {
109     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
110 
111     if( ( ret = mbedtls_mpi_copy( &ctx->serial, serial ) ) != 0 )
112         return( ret );
113 
114     return( 0 );
115 }
116 
mbedtls_x509write_crt_set_validity(mbedtls_x509write_cert * ctx,const char * not_before,const char * not_after)117 int mbedtls_x509write_crt_set_validity( mbedtls_x509write_cert *ctx,
118                                         const char *not_before,
119                                         const char *not_after )
120 {
121     if( strlen( not_before ) != MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1 ||
122         strlen( not_after )  != MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1 )
123     {
124         return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
125     }
126     strncpy( ctx->not_before, not_before, MBEDTLS_X509_RFC5280_UTC_TIME_LEN );
127     strncpy( ctx->not_after , not_after , MBEDTLS_X509_RFC5280_UTC_TIME_LEN );
128     ctx->not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1] = 'Z';
129     ctx->not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1] = 'Z';
130 
131     return( 0 );
132 }
133 
mbedtls_x509write_crt_set_extension(mbedtls_x509write_cert * ctx,const char * oid,size_t oid_len,int critical,const unsigned char * val,size_t val_len)134 int mbedtls_x509write_crt_set_extension( mbedtls_x509write_cert *ctx,
135                                  const char *oid, size_t oid_len,
136                                  int critical,
137                                  const unsigned char *val, size_t val_len )
138 {
139     return( mbedtls_x509_set_extension( &ctx->extensions, oid, oid_len,
140                                         critical, val, val_len ) );
141 }
142 
mbedtls_x509write_crt_set_basic_constraints(mbedtls_x509write_cert * ctx,int is_ca,int max_pathlen)143 int mbedtls_x509write_crt_set_basic_constraints( mbedtls_x509write_cert *ctx,
144                                                  int is_ca, int max_pathlen )
145 {
146     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
147     unsigned char buf[9];
148     unsigned char *c = buf + sizeof(buf);
149     size_t len = 0;
150 
151     memset( buf, 0, sizeof(buf) );
152 
153     if( is_ca && max_pathlen > 127 )
154         return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
155 
156     if( is_ca )
157     {
158         if( max_pathlen >= 0 )
159         {
160             MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf,
161                                                                max_pathlen ) );
162         }
163         MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_bool( &c, buf, 1 ) );
164     }
165 
166     MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
167     MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf,
168                                                 MBEDTLS_ASN1_CONSTRUCTED |
169                                                 MBEDTLS_ASN1_SEQUENCE ) );
170 
171     return(
172         mbedtls_x509write_crt_set_extension( ctx, MBEDTLS_OID_BASIC_CONSTRAINTS,
173                              MBEDTLS_OID_SIZE( MBEDTLS_OID_BASIC_CONSTRAINTS ),
174                              is_ca, buf + sizeof(buf) - len, len ) );
175 }
176 
177 #if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
mbedtls_x509write_crt_set_key_identifier(mbedtls_x509write_cert * ctx,int is_ca,unsigned char tag)178 static int mbedtls_x509write_crt_set_key_identifier( mbedtls_x509write_cert *ctx,
179                                               int is_ca,
180                                               unsigned char tag )
181 {
182     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
183     unsigned char buf[MBEDTLS_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */
184     unsigned char *c = buf + sizeof(buf);
185     size_t len = 0;
186 #if defined(MBEDTLS_USE_PSA_CRYPTO)
187     psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
188     size_t hash_length;
189 #endif /* MBEDTLS_USE_PSA_CRYPTO */
190 
191     memset( buf, 0, sizeof(buf) );
192     MBEDTLS_ASN1_CHK_ADD( len,
193                           mbedtls_pk_write_pubkey( &c,
194                                                    buf,
195                                                    is_ca ?
196                                                    ctx->issuer_key :
197                                                    ctx->subject_key ) );
198 
199 
200 #if defined(MBEDTLS_USE_PSA_CRYPTO)
201     status = psa_hash_compute( PSA_ALG_SHA_1,
202                                buf + sizeof(buf) - len,
203                                len,
204                                buf + sizeof(buf) - 20,
205                                20,
206                                &hash_length );
207     if( status != PSA_SUCCESS )
208     {
209         return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
210     }
211 #else
212     ret = mbedtls_sha1( buf + sizeof( buf ) - len, len,
213                         buf + sizeof( buf ) - 20 );
214     if( ret != 0 )
215         return( ret );
216 #endif /* MBEDTLS_USE_PSA_CRYPTO */
217 
218     c = buf + sizeof( buf ) - 20;
219     len = 20;
220 
221     MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
222     MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, tag ) );
223 
224     if( is_ca ) // writes AuthorityKeyIdentifier sequence
225     {
226         MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ));
227         MBEDTLS_ASN1_CHK_ADD( len,
228                               mbedtls_asn1_write_tag( &c,
229                                                       buf,
230                                                       MBEDTLS_ASN1_CONSTRUCTED |
231                                                       MBEDTLS_ASN1_SEQUENCE ) );
232     }
233 
234     if( is_ca )
235         return( mbedtls_x509write_crt_set_extension( ctx,
236                 MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER,
237                 MBEDTLS_OID_SIZE( MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER ),
238                 0, buf + sizeof(buf) - len, len ) );
239     else
240         return( mbedtls_x509write_crt_set_extension( ctx,
241                 MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER,
242                 MBEDTLS_OID_SIZE( MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER ),
243                 0, buf + sizeof(buf) - len, len ) );
244 }
245 
mbedtls_x509write_crt_set_subject_key_identifier(mbedtls_x509write_cert * ctx)246 int mbedtls_x509write_crt_set_subject_key_identifier( mbedtls_x509write_cert *ctx )
247 {
248     return mbedtls_x509write_crt_set_key_identifier( ctx,
249                                                      0,
250                                                      MBEDTLS_ASN1_OCTET_STRING );
251 }
252 
mbedtls_x509write_crt_set_authority_key_identifier(mbedtls_x509write_cert * ctx)253 int mbedtls_x509write_crt_set_authority_key_identifier( mbedtls_x509write_cert *ctx )
254 {
255     return mbedtls_x509write_crt_set_key_identifier( ctx,
256                                                      1,
257                                                      (MBEDTLS_ASN1_CONTEXT_SPECIFIC | 0) );
258 }
259 #endif /* MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
260 
mbedtls_x509write_crt_set_key_usage(mbedtls_x509write_cert * ctx,unsigned int key_usage)261 int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx,
262                                          unsigned int key_usage )
263 {
264     unsigned char buf[5] = {0}, ku[2] = {0};
265     unsigned char *c;
266     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
267     const unsigned int allowed_bits = MBEDTLS_X509_KU_DIGITAL_SIGNATURE |
268         MBEDTLS_X509_KU_NON_REPUDIATION   |
269         MBEDTLS_X509_KU_KEY_ENCIPHERMENT  |
270         MBEDTLS_X509_KU_DATA_ENCIPHERMENT |
271         MBEDTLS_X509_KU_KEY_AGREEMENT     |
272         MBEDTLS_X509_KU_KEY_CERT_SIGN     |
273         MBEDTLS_X509_KU_CRL_SIGN          |
274         MBEDTLS_X509_KU_ENCIPHER_ONLY     |
275         MBEDTLS_X509_KU_DECIPHER_ONLY;
276 
277     /* Check that nothing other than the allowed flags is set */
278     if( ( key_usage & ~allowed_bits ) != 0 )
279         return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
280 
281     c = buf + 5;
282     MBEDTLS_PUT_UINT16_LE( key_usage, ku, 0 );
283     ret = mbedtls_asn1_write_named_bitstring( &c, buf, ku, 9 );
284 
285     if( ret < 0 )
286         return( ret );
287     else if( ret < 3 || ret > 5 )
288         return( MBEDTLS_ERR_X509_INVALID_FORMAT );
289 
290     ret = mbedtls_x509write_crt_set_extension( ctx, MBEDTLS_OID_KEY_USAGE,
291                                    MBEDTLS_OID_SIZE( MBEDTLS_OID_KEY_USAGE ),
292                                    1, c, (size_t)ret );
293     if( ret != 0 )
294         return( ret );
295 
296     return( 0 );
297 }
298 
mbedtls_x509write_crt_set_ext_key_usage(mbedtls_x509write_cert * ctx,const mbedtls_asn1_sequence * exts)299 int mbedtls_x509write_crt_set_ext_key_usage( mbedtls_x509write_cert *ctx,
300                                              const mbedtls_asn1_sequence *exts )
301 {
302     unsigned char buf[256];
303     unsigned char *c = buf + sizeof(buf);
304     int ret;
305     size_t len = 0;
306     const mbedtls_asn1_sequence *last_ext = NULL;
307     const mbedtls_asn1_sequence *ext;
308 
309     memset( buf, 0, sizeof(buf) );
310 
311     /* We need at least one extension: SEQUENCE SIZE (1..MAX) OF KeyPurposeId */
312     if( exts == NULL )
313         return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
314 
315     /* Iterate over exts backwards, so we write them out in the requested order */
316     while( last_ext != exts )
317     {
318         for( ext = exts; ext->next != last_ext; ext = ext->next ) {}
319         if( ext->buf.tag != MBEDTLS_ASN1_OID )
320             return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
321         MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( &c, buf, ext->buf.p, ext->buf.len ) );
322         MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, ext->buf.len ) );
323         MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_OID ) );
324         last_ext = ext;
325     }
326 
327     MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
328     MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
329 
330     return mbedtls_x509write_crt_set_extension( ctx,
331                          MBEDTLS_OID_EXTENDED_KEY_USAGE,
332                          MBEDTLS_OID_SIZE( MBEDTLS_OID_EXTENDED_KEY_USAGE ),
333                          1, c, len );
334 }
335 
mbedtls_x509write_crt_set_ns_cert_type(mbedtls_x509write_cert * ctx,unsigned char ns_cert_type)336 int mbedtls_x509write_crt_set_ns_cert_type( mbedtls_x509write_cert *ctx,
337                                     unsigned char ns_cert_type )
338 {
339     unsigned char buf[4] = {0};
340     unsigned char *c;
341     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
342 
343     c = buf + 4;
344 
345     ret = mbedtls_asn1_write_named_bitstring( &c, buf, &ns_cert_type, 8 );
346     if( ret < 3 || ret > 4 )
347         return( ret );
348 
349     ret = mbedtls_x509write_crt_set_extension( ctx, MBEDTLS_OID_NS_CERT_TYPE,
350                                    MBEDTLS_OID_SIZE( MBEDTLS_OID_NS_CERT_TYPE ),
351                                    0, c, (size_t)ret );
352     if( ret != 0 )
353         return( ret );
354 
355     return( 0 );
356 }
357 
x509_write_time(unsigned char ** p,unsigned char * start,const char * t,size_t size)358 static int x509_write_time( unsigned char **p, unsigned char *start,
359                             const char *t, size_t size )
360 {
361     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
362     size_t len = 0;
363 
364     /*
365      * write MBEDTLS_ASN1_UTC_TIME if year < 2050 (2 bytes shorter)
366      */
367     if( t[0] < '2' || ( t[0] == '2' && t[1] == '0' && t[2] < '5' ) )
368     {
369         MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
370                                              (const unsigned char *) t + 2,
371                                              size - 2 ) );
372         MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
373         MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
374                                              MBEDTLS_ASN1_UTC_TIME ) );
375     }
376     else
377     {
378         MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
379                                                   (const unsigned char *) t,
380                                                   size ) );
381         MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
382         MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
383                                              MBEDTLS_ASN1_GENERALIZED_TIME ) );
384     }
385 
386     return( (int) len );
387 }
388 
mbedtls_x509write_crt_der(mbedtls_x509write_cert * ctx,unsigned char * buf,size_t size,int (* f_rng)(void *,unsigned char *,size_t),void * p_rng)389 int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx,
390                                unsigned char *buf, size_t size,
391                                int (*f_rng)(void *, unsigned char *, size_t),
392                                void *p_rng )
393 {
394     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
395     const char *sig_oid;
396     size_t sig_oid_len = 0;
397     unsigned char *c, *c2;
398     unsigned char sig[MBEDTLS_PK_SIGNATURE_MAX_SIZE];
399     size_t hash_length = 0;
400     unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
401 #if defined(MBEDTLS_USE_PSA_CRYPTO)
402     psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
403     psa_algorithm_t psa_algorithm;
404 #endif /* MBEDTLS_USE_PSA_CRYPTO */
405 
406     size_t sub_len = 0, pub_len = 0, sig_and_oid_len = 0, sig_len;
407     size_t len = 0;
408     mbedtls_pk_type_t pk_alg;
409 
410     /*
411      * Prepare data to be signed at the end of the target buffer
412      */
413     c = buf + size;
414 
415     /* Signature algorithm needed in TBS, and later for actual signature */
416 
417     /* There's no direct way of extracting a signature algorithm
418      * (represented as an element of mbedtls_pk_type_t) from a PK instance. */
419     if( mbedtls_pk_can_do( ctx->issuer_key, MBEDTLS_PK_RSA ) )
420         pk_alg = MBEDTLS_PK_RSA;
421     else if( mbedtls_pk_can_do( ctx->issuer_key, MBEDTLS_PK_ECDSA ) )
422         pk_alg = MBEDTLS_PK_ECDSA;
423     else
424         return( MBEDTLS_ERR_X509_INVALID_ALG );
425 
426     if( ( ret = mbedtls_oid_get_oid_by_sig_alg( pk_alg, ctx->md_alg,
427                                           &sig_oid, &sig_oid_len ) ) != 0 )
428     {
429         return( ret );
430     }
431 
432     /*
433      *  Extensions  ::=  SEQUENCE SIZE (1..MAX) OF Extension
434      */
435 
436     /* Only for v3 */
437     if( ctx->version == MBEDTLS_X509_CRT_VERSION_3 )
438     {
439         MBEDTLS_ASN1_CHK_ADD( len,
440                               mbedtls_x509_write_extensions( &c,
441                                                       buf, ctx->extensions ) );
442         MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
443         MBEDTLS_ASN1_CHK_ADD( len,
444                               mbedtls_asn1_write_tag( &c, buf,
445                                                       MBEDTLS_ASN1_CONSTRUCTED |
446                                                       MBEDTLS_ASN1_SEQUENCE ) );
447         MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
448         MBEDTLS_ASN1_CHK_ADD( len,
449                               mbedtls_asn1_write_tag( &c, buf,
450                                                MBEDTLS_ASN1_CONTEXT_SPECIFIC |
451                                                MBEDTLS_ASN1_CONSTRUCTED | 3 ) );
452     }
453 
454     /*
455      *  SubjectPublicKeyInfo
456      */
457     MBEDTLS_ASN1_CHK_ADD( pub_len,
458                           mbedtls_pk_write_pubkey_der( ctx->subject_key,
459                                                        buf, c - buf ) );
460     c -= pub_len;
461     len += pub_len;
462 
463     /*
464      *  Subject  ::=  Name
465      */
466     MBEDTLS_ASN1_CHK_ADD( len,
467                           mbedtls_x509_write_names( &c, buf,
468                                                     ctx->subject ) );
469 
470     /*
471      *  Validity ::= SEQUENCE {
472      *       notBefore      Time,
473      *       notAfter       Time }
474      */
475     sub_len = 0;
476 
477     MBEDTLS_ASN1_CHK_ADD( sub_len,
478                           x509_write_time( &c, buf, ctx->not_after,
479                                         MBEDTLS_X509_RFC5280_UTC_TIME_LEN ) );
480 
481     MBEDTLS_ASN1_CHK_ADD( sub_len,
482                           x509_write_time( &c, buf, ctx->not_before,
483                                         MBEDTLS_X509_RFC5280_UTC_TIME_LEN ) );
484 
485     len += sub_len;
486     MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, sub_len ) );
487     MBEDTLS_ASN1_CHK_ADD( len,
488                           mbedtls_asn1_write_tag( &c, buf,
489                                                   MBEDTLS_ASN1_CONSTRUCTED |
490                                                   MBEDTLS_ASN1_SEQUENCE ) );
491 
492     /*
493      *  Issuer  ::=  Name
494      */
495     MBEDTLS_ASN1_CHK_ADD( len, mbedtls_x509_write_names( &c, buf,
496                                                          ctx->issuer ) );
497 
498     /*
499      *  Signature   ::=  AlgorithmIdentifier
500      */
501     MBEDTLS_ASN1_CHK_ADD( len,
502                           mbedtls_asn1_write_algorithm_identifier( &c, buf,
503                                               sig_oid, strlen( sig_oid ), 0 ) );
504 
505     /*
506      *  Serial   ::=  INTEGER
507      */
508     MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, buf,
509                                                        &ctx->serial ) );
510 
511     /*
512      *  Version  ::=  INTEGER  {  v1(0), v2(1), v3(2)  }
513      */
514 
515     /* Can be omitted for v1 */
516     if( ctx->version != MBEDTLS_X509_CRT_VERSION_1 )
517     {
518         sub_len = 0;
519         MBEDTLS_ASN1_CHK_ADD( sub_len,
520                               mbedtls_asn1_write_int( &c, buf, ctx->version ) );
521         len += sub_len;
522         MBEDTLS_ASN1_CHK_ADD( len,
523                               mbedtls_asn1_write_len( &c, buf, sub_len ) );
524         MBEDTLS_ASN1_CHK_ADD( len,
525                               mbedtls_asn1_write_tag( &c, buf,
526                                                MBEDTLS_ASN1_CONTEXT_SPECIFIC |
527                                                MBEDTLS_ASN1_CONSTRUCTED | 0 ) );
528     }
529 
530     MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
531     MBEDTLS_ASN1_CHK_ADD( len,
532                 mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
533                                                      MBEDTLS_ASN1_SEQUENCE ) );
534 
535     /*
536      * Make signature
537      */
538 
539     /* Compute hash of CRT. */
540 #if defined(MBEDTLS_USE_PSA_CRYPTO)
541     psa_algorithm = mbedtls_hash_info_psa_from_md( ctx->md_alg );
542 
543     status = psa_hash_compute( psa_algorithm,
544                                c,
545                                len,
546                                hash,
547                                sizeof( hash ),
548                                &hash_length );
549     if( status != PSA_SUCCESS )
550     {
551         return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
552     }
553 #else
554     if( ( ret = mbedtls_md( mbedtls_md_info_from_type( ctx->md_alg ), c,
555                             len, hash ) ) != 0 )
556     {
557         return( ret );
558     }
559 #endif /* MBEDTLS_USE_PSA_CRYPTO */
560 
561 
562     if( ( ret = mbedtls_pk_sign( ctx->issuer_key, ctx->md_alg,
563                                  hash, hash_length, sig, sizeof( sig ), &sig_len,
564                                  f_rng, p_rng ) ) != 0 )
565     {
566         return( ret );
567     }
568 
569     /* Move CRT to the front of the buffer to have space
570      * for the signature. */
571     memmove( buf, c, len );
572     c = buf + len;
573 
574     /* Add signature at the end of the buffer,
575      * making sure that it doesn't underflow
576      * into the CRT buffer. */
577     c2 = buf + size;
578     MBEDTLS_ASN1_CHK_ADD( sig_and_oid_len, mbedtls_x509_write_sig( &c2, c,
579                                         sig_oid, sig_oid_len, sig, sig_len ) );
580 
581     /*
582      * Memory layout after this step:
583      *
584      * buf       c=buf+len                c2            buf+size
585      * [CRT0,...,CRTn, UNUSED, ..., UNUSED, SIG0, ..., SIGm]
586      */
587 
588     /* Move raw CRT to just before the signature. */
589     c = c2 - len;
590     memmove( c, buf, len );
591 
592     len += sig_and_oid_len;
593     MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
594     MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf,
595                                                  MBEDTLS_ASN1_CONSTRUCTED |
596                                                  MBEDTLS_ASN1_SEQUENCE ) );
597 
598     return( (int) len );
599 }
600 
601 #define PEM_BEGIN_CRT           "-----BEGIN CERTIFICATE-----\n"
602 #define PEM_END_CRT             "-----END CERTIFICATE-----\n"
603 
604 #if defined(MBEDTLS_PEM_WRITE_C)
mbedtls_x509write_crt_pem(mbedtls_x509write_cert * crt,unsigned char * buf,size_t size,int (* f_rng)(void *,unsigned char *,size_t),void * p_rng)605 int mbedtls_x509write_crt_pem( mbedtls_x509write_cert *crt,
606                                unsigned char *buf, size_t size,
607                                int (*f_rng)(void *, unsigned char *, size_t),
608                                void *p_rng )
609 {
610     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
611     size_t olen;
612 
613     if( ( ret = mbedtls_x509write_crt_der( crt, buf, size,
614                                    f_rng, p_rng ) ) < 0 )
615     {
616         return( ret );
617     }
618 
619     if( ( ret = mbedtls_pem_write_buffer( PEM_BEGIN_CRT, PEM_END_CRT,
620                                           buf + size - ret, ret,
621                                           buf, size, &olen ) ) != 0 )
622     {
623         return( ret );
624     }
625 
626     return( 0 );
627 }
628 #endif /* MBEDTLS_PEM_WRITE_C */
629 
630 #endif /* MBEDTLS_X509_CRT_WRITE_C */
631