blob: 8f4a4f5ea3b38f7971178ccf31b7ca5410433f3f [file] [log] [blame]
Jens Wiklander817466c2018-05-22 13:49:31 +02001/*
2 * X.509 certificate writing
3 *
Jerome Forissier79013242021-07-28 10:24:04 +02004 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
Jens Wiklander817466c2018-05-22 13:49:31 +02006 *
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.
Jens Wiklander817466c2018-05-22 13:49:31 +020018 */
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
Jerome Forissier79013242021-07-28 10:24:04 +020026#include "common.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020027
28#if defined(MBEDTLS_X509_CRT_WRITE_C)
29
30#include "mbedtls/x509_crt.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020031#include "mbedtls/asn1write.h"
Jerome Forissier11fa71b2020-04-20 17:17:56 +020032#include "mbedtls/error.h"
33#include "mbedtls/oid.h"
Jens Wiklander3d3b0592019-03-20 15:30:29 +010034#include "mbedtls/platform_util.h"
Jerome Forissier11fa71b2020-04-20 17:17:56 +020035#include "mbedtls/sha1.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020036
37#include <string.h>
38
39#if defined(MBEDTLS_PEM_WRITE_C)
40#include "mbedtls/pem.h"
41#endif /* MBEDTLS_PEM_WRITE_C */
42
Jens Wiklander817466c2018-05-22 13:49:31 +020043void mbedtls_x509write_crt_init( mbedtls_x509write_cert *ctx )
44{
Jens Wiklander3d3b0592019-03-20 15:30:29 +010045 memset( ctx, 0, sizeof( mbedtls_x509write_cert ) );
Jens Wiklander817466c2018-05-22 13:49:31 +020046
47 mbedtls_mpi_init( &ctx->serial );
48 ctx->version = MBEDTLS_X509_CRT_VERSION_3;
49}
50
51void mbedtls_x509write_crt_free( mbedtls_x509write_cert *ctx )
52{
53 mbedtls_mpi_free( &ctx->serial );
54
55 mbedtls_asn1_free_named_data_list( &ctx->subject );
56 mbedtls_asn1_free_named_data_list( &ctx->issuer );
57 mbedtls_asn1_free_named_data_list( &ctx->extensions );
58
Jens Wiklander3d3b0592019-03-20 15:30:29 +010059 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_x509write_cert ) );
Jens Wiklander817466c2018-05-22 13:49:31 +020060}
61
Jerome Forissier11fa71b2020-04-20 17:17:56 +020062void mbedtls_x509write_crt_set_version( mbedtls_x509write_cert *ctx,
63 int version )
Jens Wiklander817466c2018-05-22 13:49:31 +020064{
65 ctx->version = version;
66}
67
Jerome Forissier11fa71b2020-04-20 17:17:56 +020068void mbedtls_x509write_crt_set_md_alg( mbedtls_x509write_cert *ctx,
69 mbedtls_md_type_t md_alg )
Jens Wiklander817466c2018-05-22 13:49:31 +020070{
71 ctx->md_alg = md_alg;
72}
73
Jerome Forissier11fa71b2020-04-20 17:17:56 +020074void mbedtls_x509write_crt_set_subject_key( mbedtls_x509write_cert *ctx,
75 mbedtls_pk_context *key )
Jens Wiklander817466c2018-05-22 13:49:31 +020076{
77 ctx->subject_key = key;
78}
79
Jerome Forissier11fa71b2020-04-20 17:17:56 +020080void mbedtls_x509write_crt_set_issuer_key( mbedtls_x509write_cert *ctx,
81 mbedtls_pk_context *key )
Jens Wiklander817466c2018-05-22 13:49:31 +020082{
83 ctx->issuer_key = key;
84}
85
86int mbedtls_x509write_crt_set_subject_name( mbedtls_x509write_cert *ctx,
Jerome Forissier11fa71b2020-04-20 17:17:56 +020087 const char *subject_name )
Jens Wiklander817466c2018-05-22 13:49:31 +020088{
89 return mbedtls_x509_string_to_names( &ctx->subject, subject_name );
90}
91
92int mbedtls_x509write_crt_set_issuer_name( mbedtls_x509write_cert *ctx,
Jerome Forissier11fa71b2020-04-20 17:17:56 +020093 const char *issuer_name )
Jens Wiklander817466c2018-05-22 13:49:31 +020094{
95 return mbedtls_x509_string_to_names( &ctx->issuer, issuer_name );
96}
97
Jerome Forissier11fa71b2020-04-20 17:17:56 +020098int mbedtls_x509write_crt_set_serial( mbedtls_x509write_cert *ctx,
99 const mbedtls_mpi *serial )
Jens Wiklander817466c2018-05-22 13:49:31 +0200100{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200101 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200102
103 if( ( ret = mbedtls_mpi_copy( &ctx->serial, serial ) ) != 0 )
104 return( ret );
105
106 return( 0 );
107}
108
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200109int mbedtls_x509write_crt_set_validity( mbedtls_x509write_cert *ctx,
110 const char *not_before,
111 const char *not_after )
Jens Wiklander817466c2018-05-22 13:49:31 +0200112{
113 if( strlen( not_before ) != MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1 ||
114 strlen( not_after ) != MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1 )
115 {
116 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
117 }
118 strncpy( ctx->not_before, not_before, MBEDTLS_X509_RFC5280_UTC_TIME_LEN );
119 strncpy( ctx->not_after , not_after , MBEDTLS_X509_RFC5280_UTC_TIME_LEN );
120 ctx->not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1] = 'Z';
121 ctx->not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1] = 'Z';
122
123 return( 0 );
124}
125
126int mbedtls_x509write_crt_set_extension( mbedtls_x509write_cert *ctx,
127 const char *oid, size_t oid_len,
128 int critical,
129 const unsigned char *val, size_t val_len )
130{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200131 return( mbedtls_x509_set_extension( &ctx->extensions, oid, oid_len,
132 critical, val, val_len ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200133}
134
135int mbedtls_x509write_crt_set_basic_constraints( mbedtls_x509write_cert *ctx,
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200136 int is_ca, int max_pathlen )
Jens Wiklander817466c2018-05-22 13:49:31 +0200137{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200138 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200139 unsigned char buf[9];
140 unsigned char *c = buf + sizeof(buf);
141 size_t len = 0;
142
143 memset( buf, 0, sizeof(buf) );
144
145 if( is_ca && max_pathlen > 127 )
146 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
147
148 if( is_ca )
149 {
150 if( max_pathlen >= 0 )
151 {
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200152 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf,
153 max_pathlen ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200154 }
155 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_bool( &c, buf, 1 ) );
156 }
157
158 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200159 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf,
160 MBEDTLS_ASN1_CONSTRUCTED |
Jens Wiklander817466c2018-05-22 13:49:31 +0200161 MBEDTLS_ASN1_SEQUENCE ) );
162
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200163 return(
164 mbedtls_x509write_crt_set_extension( ctx, MBEDTLS_OID_BASIC_CONSTRAINTS,
165 MBEDTLS_OID_SIZE( MBEDTLS_OID_BASIC_CONSTRAINTS ),
Jerome Forissier79013242021-07-28 10:24:04 +0200166 is_ca, buf + sizeof(buf) - len, len ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200167}
168
169#if defined(MBEDTLS_SHA1_C)
170int mbedtls_x509write_crt_set_subject_key_identifier( mbedtls_x509write_cert *ctx )
171{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200172 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200173 unsigned char buf[MBEDTLS_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */
174 unsigned char *c = buf + sizeof(buf);
175 size_t len = 0;
176
177 memset( buf, 0, sizeof(buf) );
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200178 MBEDTLS_ASN1_CHK_ADD( len,
179 mbedtls_pk_write_pubkey( &c, buf, ctx->subject_key ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200180
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100181 ret = mbedtls_sha1_ret( buf + sizeof( buf ) - len, len,
182 buf + sizeof( buf ) - 20 );
183 if( ret != 0 )
184 return( ret );
185 c = buf + sizeof( buf ) - 20;
Jens Wiklander817466c2018-05-22 13:49:31 +0200186 len = 20;
187
188 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200189 MBEDTLS_ASN1_CHK_ADD( len,
190 mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_OCTET_STRING ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200191
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200192 return mbedtls_x509write_crt_set_extension( ctx,
193 MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER,
194 MBEDTLS_OID_SIZE( MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER ),
195 0, buf + sizeof(buf) - len, len );
Jens Wiklander817466c2018-05-22 13:49:31 +0200196}
197
198int mbedtls_x509write_crt_set_authority_key_identifier( mbedtls_x509write_cert *ctx )
199{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200200 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200201 unsigned char buf[MBEDTLS_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100202 unsigned char *c = buf + sizeof( buf );
Jens Wiklander817466c2018-05-22 13:49:31 +0200203 size_t len = 0;
204
205 memset( buf, 0, sizeof(buf) );
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200206 MBEDTLS_ASN1_CHK_ADD( len,
207 mbedtls_pk_write_pubkey( &c, buf, ctx->issuer_key ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200208
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100209 ret = mbedtls_sha1_ret( buf + sizeof( buf ) - len, len,
210 buf + sizeof( buf ) - 20 );
211 if( ret != 0 )
212 return( ret );
213 c = buf + sizeof( buf ) - 20;
Jens Wiklander817466c2018-05-22 13:49:31 +0200214 len = 20;
215
216 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200217 MBEDTLS_ASN1_CHK_ADD( len,
218 mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONTEXT_SPECIFIC | 0 ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200219
220 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200221 MBEDTLS_ASN1_CHK_ADD( len,
222 mbedtls_asn1_write_tag( &c, buf,
223 MBEDTLS_ASN1_CONSTRUCTED |
224 MBEDTLS_ASN1_SEQUENCE ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200225
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200226 return mbedtls_x509write_crt_set_extension(
227 ctx, MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER,
228 MBEDTLS_OID_SIZE( MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER ),
229 0, buf + sizeof( buf ) - len, len );
Jens Wiklander817466c2018-05-22 13:49:31 +0200230}
231#endif /* MBEDTLS_SHA1_C */
232
233int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx,
234 unsigned int key_usage )
235{
Jerome Forissier79013242021-07-28 10:24:04 +0200236 unsigned char buf[5] = {0}, ku[2] = {0};
Jens Wiklander817466c2018-05-22 13:49:31 +0200237 unsigned char *c;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200238 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerome Forissier5b25c762020-04-07 11:18:49 +0200239 const unsigned int allowed_bits = MBEDTLS_X509_KU_DIGITAL_SIGNATURE |
240 MBEDTLS_X509_KU_NON_REPUDIATION |
241 MBEDTLS_X509_KU_KEY_ENCIPHERMENT |
242 MBEDTLS_X509_KU_DATA_ENCIPHERMENT |
243 MBEDTLS_X509_KU_KEY_AGREEMENT |
244 MBEDTLS_X509_KU_KEY_CERT_SIGN |
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200245 MBEDTLS_X509_KU_CRL_SIGN |
246 MBEDTLS_X509_KU_ENCIPHER_ONLY |
247 MBEDTLS_X509_KU_DECIPHER_ONLY;
Jens Wiklander817466c2018-05-22 13:49:31 +0200248
Jerome Forissier5b25c762020-04-07 11:18:49 +0200249 /* Check that nothing other than the allowed flags is set */
250 if( ( key_usage & ~allowed_bits ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +0200251 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
252
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200253 c = buf + 5;
254 ku[0] = (unsigned char)( key_usage );
255 ku[1] = (unsigned char)( key_usage >> 8 );
256 ret = mbedtls_asn1_write_named_bitstring( &c, buf, ku, 9 );
Jens Wiklander817466c2018-05-22 13:49:31 +0200257
Jerome Forissier5b25c762020-04-07 11:18:49 +0200258 if( ret < 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +0200259 return( ret );
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200260 else if( ret < 3 || ret > 5 )
Jerome Forissier5b25c762020-04-07 11:18:49 +0200261 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Jens Wiklander817466c2018-05-22 13:49:31 +0200262
263 ret = mbedtls_x509write_crt_set_extension( ctx, MBEDTLS_OID_KEY_USAGE,
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200264 MBEDTLS_OID_SIZE( MBEDTLS_OID_KEY_USAGE ),
265 1, c, (size_t)ret );
Jens Wiklander817466c2018-05-22 13:49:31 +0200266 if( ret != 0 )
267 return( ret );
268
269 return( 0 );
270}
271
272int mbedtls_x509write_crt_set_ns_cert_type( mbedtls_x509write_cert *ctx,
273 unsigned char ns_cert_type )
274{
Jerome Forissier79013242021-07-28 10:24:04 +0200275 unsigned char buf[4] = {0};
Jens Wiklander817466c2018-05-22 13:49:31 +0200276 unsigned char *c;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200277 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200278
279 c = buf + 4;
280
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200281 ret = mbedtls_asn1_write_named_bitstring( &c, buf, &ns_cert_type, 8 );
Jerome Forissier5b25c762020-04-07 11:18:49 +0200282 if( ret < 3 || ret > 4 )
Jens Wiklander817466c2018-05-22 13:49:31 +0200283 return( ret );
284
285 ret = mbedtls_x509write_crt_set_extension( ctx, MBEDTLS_OID_NS_CERT_TYPE,
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200286 MBEDTLS_OID_SIZE( MBEDTLS_OID_NS_CERT_TYPE ),
287 0, c, (size_t)ret );
Jens Wiklander817466c2018-05-22 13:49:31 +0200288 if( ret != 0 )
289 return( ret );
290
291 return( 0 );
292}
293
294static int x509_write_time( unsigned char **p, unsigned char *start,
295 const char *t, size_t size )
296{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200297 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200298 size_t len = 0;
299
300 /*
301 * write MBEDTLS_ASN1_UTC_TIME if year < 2050 (2 bytes shorter)
302 */
303 if( t[0] == '2' && t[1] == '0' && t[2] < '5' )
304 {
305 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
306 (const unsigned char *) t + 2,
307 size - 2 ) );
308 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200309 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
310 MBEDTLS_ASN1_UTC_TIME ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200311 }
312 else
313 {
314 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
315 (const unsigned char *) t,
316 size ) );
317 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200318 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
319 MBEDTLS_ASN1_GENERALIZED_TIME ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200320 }
321
322 return( (int) len );
323}
324
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200325int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx,
326 unsigned char *buf, size_t size,
327 int (*f_rng)(void *, unsigned char *, size_t),
328 void *p_rng )
Jens Wiklander817466c2018-05-22 13:49:31 +0200329{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200330 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200331 const char *sig_oid;
332 size_t sig_oid_len = 0;
333 unsigned char *c, *c2;
334 unsigned char hash[64];
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200335 unsigned char sig[MBEDTLS_PK_SIGNATURE_MAX_SIZE];
Jens Wiklander817466c2018-05-22 13:49:31 +0200336 size_t sub_len = 0, pub_len = 0, sig_and_oid_len = 0, sig_len;
337 size_t len = 0;
338 mbedtls_pk_type_t pk_alg;
339
340 /*
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200341 * Prepare data to be signed at the end of the target buffer
Jens Wiklander817466c2018-05-22 13:49:31 +0200342 */
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200343 c = buf + size;
Jens Wiklander817466c2018-05-22 13:49:31 +0200344
345 /* Signature algorithm needed in TBS, and later for actual signature */
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100346
347 /* There's no direct way of extracting a signature algorithm
348 * (represented as an element of mbedtls_pk_type_t) from a PK instance. */
349 if( mbedtls_pk_can_do( ctx->issuer_key, MBEDTLS_PK_RSA ) )
350 pk_alg = MBEDTLS_PK_RSA;
351 else if( mbedtls_pk_can_do( ctx->issuer_key, MBEDTLS_PK_ECDSA ) )
Jens Wiklander817466c2018-05-22 13:49:31 +0200352 pk_alg = MBEDTLS_PK_ECDSA;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100353 else
354 return( MBEDTLS_ERR_X509_INVALID_ALG );
Jens Wiklander817466c2018-05-22 13:49:31 +0200355
356 if( ( ret = mbedtls_oid_get_oid_by_sig_alg( pk_alg, ctx->md_alg,
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100357 &sig_oid, &sig_oid_len ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +0200358 {
359 return( ret );
360 }
361
362 /*
363 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
364 */
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100365
366 /* Only for v3 */
367 if( ctx->version == MBEDTLS_X509_CRT_VERSION_3 )
368 {
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200369 MBEDTLS_ASN1_CHK_ADD( len,
370 mbedtls_x509_write_extensions( &c,
371 buf, ctx->extensions ) );
372 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
373 MBEDTLS_ASN1_CHK_ADD( len,
374 mbedtls_asn1_write_tag( &c, buf,
375 MBEDTLS_ASN1_CONSTRUCTED |
376 MBEDTLS_ASN1_SEQUENCE ) );
377 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
378 MBEDTLS_ASN1_CHK_ADD( len,
379 mbedtls_asn1_write_tag( &c, buf,
380 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
381 MBEDTLS_ASN1_CONSTRUCTED | 3 ) );
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100382 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200383
384 /*
385 * SubjectPublicKeyInfo
386 */
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200387 MBEDTLS_ASN1_CHK_ADD( pub_len,
388 mbedtls_pk_write_pubkey_der( ctx->subject_key,
389 buf, c - buf ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200390 c -= pub_len;
391 len += pub_len;
392
393 /*
394 * Subject ::= Name
395 */
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200396 MBEDTLS_ASN1_CHK_ADD( len,
397 mbedtls_x509_write_names( &c, buf,
398 ctx->subject ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200399
400 /*
401 * Validity ::= SEQUENCE {
402 * notBefore Time,
403 * notAfter Time }
404 */
405 sub_len = 0;
406
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200407 MBEDTLS_ASN1_CHK_ADD( sub_len,
408 x509_write_time( &c, buf, ctx->not_after,
409 MBEDTLS_X509_RFC5280_UTC_TIME_LEN ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200410
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200411 MBEDTLS_ASN1_CHK_ADD( sub_len,
412 x509_write_time( &c, buf, ctx->not_before,
413 MBEDTLS_X509_RFC5280_UTC_TIME_LEN ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200414
415 len += sub_len;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200416 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, sub_len ) );
417 MBEDTLS_ASN1_CHK_ADD( len,
418 mbedtls_asn1_write_tag( &c, buf,
419 MBEDTLS_ASN1_CONSTRUCTED |
420 MBEDTLS_ASN1_SEQUENCE ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200421
422 /*
423 * Issuer ::= Name
424 */
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200425 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_x509_write_names( &c, buf,
426 ctx->issuer ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200427
428 /*
429 * Signature ::= AlgorithmIdentifier
430 */
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200431 MBEDTLS_ASN1_CHK_ADD( len,
432 mbedtls_asn1_write_algorithm_identifier( &c, buf,
433 sig_oid, strlen( sig_oid ), 0 ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200434
435 /*
436 * Serial ::= INTEGER
437 */
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200438 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, buf,
439 &ctx->serial ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200440
441 /*
442 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
443 */
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100444
445 /* Can be omitted for v1 */
446 if( ctx->version != MBEDTLS_X509_CRT_VERSION_1 )
447 {
448 sub_len = 0;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200449 MBEDTLS_ASN1_CHK_ADD( sub_len,
450 mbedtls_asn1_write_int( &c, buf, ctx->version ) );
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100451 len += sub_len;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200452 MBEDTLS_ASN1_CHK_ADD( len,
453 mbedtls_asn1_write_len( &c, buf, sub_len ) );
454 MBEDTLS_ASN1_CHK_ADD( len,
455 mbedtls_asn1_write_tag( &c, buf,
456 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
457 MBEDTLS_ASN1_CONSTRUCTED | 0 ) );
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100458 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200459
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200460 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
461 MBEDTLS_ASN1_CHK_ADD( len,
462 mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
463 MBEDTLS_ASN1_SEQUENCE ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200464
465 /*
466 * Make signature
467 */
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200468
469 /* Compute hash of CRT. */
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100470 if( ( ret = mbedtls_md( mbedtls_md_info_from_type( ctx->md_alg ), c,
471 len, hash ) ) != 0 )
472 {
473 return( ret );
474 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200475
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200476 if( ( ret = mbedtls_pk_sign( ctx->issuer_key, ctx->md_alg,
477 hash, 0, sig, &sig_len,
478 f_rng, p_rng ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +0200479 {
480 return( ret );
481 }
482
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200483 /* Move CRT to the front of the buffer to have space
484 * for the signature. */
485 memmove( buf, c, len );
486 c = buf + len;
487
488 /* Add signature at the end of the buffer,
489 * making sure that it doesn't underflow
490 * into the CRT buffer. */
Jens Wiklander817466c2018-05-22 13:49:31 +0200491 c2 = buf + size;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200492 MBEDTLS_ASN1_CHK_ADD( sig_and_oid_len, mbedtls_x509_write_sig( &c2, c,
Jens Wiklander817466c2018-05-22 13:49:31 +0200493 sig_oid, sig_oid_len, sig, sig_len ) );
494
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200495 /*
496 * Memory layout after this step:
497 *
498 * buf c=buf+len c2 buf+size
499 * [CRT0,...,CRTn, UNUSED, ..., UNUSED, SIG0, ..., SIGm]
500 */
Jens Wiklander817466c2018-05-22 13:49:31 +0200501
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200502 /* Move raw CRT to just before the signature. */
503 c = c2 - len;
504 memmove( c, buf, len );
Jens Wiklander817466c2018-05-22 13:49:31 +0200505
506 len += sig_and_oid_len;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200507 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
508 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf,
509 MBEDTLS_ASN1_CONSTRUCTED |
Jens Wiklander817466c2018-05-22 13:49:31 +0200510 MBEDTLS_ASN1_SEQUENCE ) );
511
512 return( (int) len );
513}
514
515#define PEM_BEGIN_CRT "-----BEGIN CERTIFICATE-----\n"
516#define PEM_END_CRT "-----END CERTIFICATE-----\n"
517
518#if defined(MBEDTLS_PEM_WRITE_C)
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200519int mbedtls_x509write_crt_pem( mbedtls_x509write_cert *crt,
520 unsigned char *buf, size_t size,
521 int (*f_rng)(void *, unsigned char *, size_t),
522 void *p_rng )
Jens Wiklander817466c2018-05-22 13:49:31 +0200523{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200524 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
525 size_t olen;
Jens Wiklander817466c2018-05-22 13:49:31 +0200526
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200527 if( ( ret = mbedtls_x509write_crt_der( crt, buf, size,
Jens Wiklander817466c2018-05-22 13:49:31 +0200528 f_rng, p_rng ) ) < 0 )
529 {
530 return( ret );
531 }
532
533 if( ( ret = mbedtls_pem_write_buffer( PEM_BEGIN_CRT, PEM_END_CRT,
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200534 buf + size - ret, ret,
535 buf, size, &olen ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +0200536 {
537 return( ret );
538 }
539
540 return( 0 );
541}
542#endif /* MBEDTLS_PEM_WRITE_C */
543
544#endif /* MBEDTLS_X509_CRT_WRITE_C */