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