Coverage Report

Created: 2026-10-17 06:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/mbedtls-3.6.6/library/x509_create.c
Line
Count
Source
1
/*
2
 *  X.509 base functions for creating certificates / CSRs
3
 *
4
 *  Copyright The Mbed TLS Contributors
5
 *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6
 */
7
8
#include "common.h"
9
10
#if defined(MBEDTLS_X509_CREATE_C)
11
12
#include "x509_internal.h"
13
#include "mbedtls/asn1write.h"
14
#include "mbedtls/error.h"
15
#include "mbedtls/oid.h"
16
17
#include <string.h>
18
19
#include "mbedtls/platform.h"
20
21
#include "mbedtls/asn1.h"
22
23
/* Structure linking OIDs for X.509 DN AttributeTypes to their
24
 * string representations and default string encodings used by Mbed TLS. */
25
typedef struct {
26
    const char *name; /* String representation of AttributeType, e.g.
27
                       * "CN" or "emailAddress". */
28
    size_t name_len; /* Length of 'name', without trailing 0 byte. */
29
    const char *oid; /* String representation of OID of AttributeType,
30
                      * as per RFC 5280, Appendix A.1. encoded as per
31
                      * X.690 */
32
    int default_tag; /* The default character encoding used for the
33
                      * given attribute type, e.g.
34
                      * MBEDTLS_ASN1_UTF8_STRING for UTF-8. */
35
} x509_attr_descriptor_t;
36
37
#define ADD_STRLEN(s)     s, sizeof(s) - 1
38
39
/* X.509 DN attributes from RFC 5280, Appendix A.1. */
40
static const x509_attr_descriptor_t x509_attrs[] =
41
{
42
    { ADD_STRLEN("CN"),
43
      MBEDTLS_OID_AT_CN, MBEDTLS_ASN1_UTF8_STRING },
44
    { ADD_STRLEN("commonName"),
45
      MBEDTLS_OID_AT_CN, MBEDTLS_ASN1_UTF8_STRING },
46
    { ADD_STRLEN("C"),
47
      MBEDTLS_OID_AT_COUNTRY, MBEDTLS_ASN1_PRINTABLE_STRING },
48
    { ADD_STRLEN("countryName"),
49
      MBEDTLS_OID_AT_COUNTRY, MBEDTLS_ASN1_PRINTABLE_STRING },
50
    { ADD_STRLEN("O"),
51
      MBEDTLS_OID_AT_ORGANIZATION, MBEDTLS_ASN1_UTF8_STRING },
52
    { ADD_STRLEN("organizationName"),
53
      MBEDTLS_OID_AT_ORGANIZATION, MBEDTLS_ASN1_UTF8_STRING },
54
    { ADD_STRLEN("L"),
55
      MBEDTLS_OID_AT_LOCALITY, MBEDTLS_ASN1_UTF8_STRING },
56
    { ADD_STRLEN("locality"),
57
      MBEDTLS_OID_AT_LOCALITY, MBEDTLS_ASN1_UTF8_STRING },
58
    { ADD_STRLEN("R"),
59
      MBEDTLS_OID_PKCS9_EMAIL, MBEDTLS_ASN1_IA5_STRING },
60
    { ADD_STRLEN("OU"),
61
      MBEDTLS_OID_AT_ORG_UNIT, MBEDTLS_ASN1_UTF8_STRING },
62
    { ADD_STRLEN("organizationalUnitName"),
63
      MBEDTLS_OID_AT_ORG_UNIT, MBEDTLS_ASN1_UTF8_STRING },
64
    { ADD_STRLEN("ST"),
65
      MBEDTLS_OID_AT_STATE, MBEDTLS_ASN1_UTF8_STRING },
66
    { ADD_STRLEN("stateOrProvinceName"),
67
      MBEDTLS_OID_AT_STATE, MBEDTLS_ASN1_UTF8_STRING },
68
    { ADD_STRLEN("emailAddress"),
69
      MBEDTLS_OID_PKCS9_EMAIL, MBEDTLS_ASN1_IA5_STRING },
70
    { ADD_STRLEN("serialNumber"),
71
      MBEDTLS_OID_AT_SERIAL_NUMBER, MBEDTLS_ASN1_PRINTABLE_STRING },
72
    { ADD_STRLEN("postalAddress"),
73
      MBEDTLS_OID_AT_POSTAL_ADDRESS, MBEDTLS_ASN1_PRINTABLE_STRING },
74
    { ADD_STRLEN("postalCode"),
75
      MBEDTLS_OID_AT_POSTAL_CODE, MBEDTLS_ASN1_PRINTABLE_STRING },
76
    { ADD_STRLEN("dnQualifier"),
77
      MBEDTLS_OID_AT_DN_QUALIFIER, MBEDTLS_ASN1_PRINTABLE_STRING },
78
    { ADD_STRLEN("title"),
79
      MBEDTLS_OID_AT_TITLE, MBEDTLS_ASN1_UTF8_STRING },
80
    { ADD_STRLEN("surName"),
81
      MBEDTLS_OID_AT_SUR_NAME, MBEDTLS_ASN1_UTF8_STRING },
82
    { ADD_STRLEN("SN"),
83
      MBEDTLS_OID_AT_SUR_NAME, MBEDTLS_ASN1_UTF8_STRING },
84
    { ADD_STRLEN("givenName"),
85
      MBEDTLS_OID_AT_GIVEN_NAME, MBEDTLS_ASN1_UTF8_STRING },
86
    { ADD_STRLEN("GN"),
87
      MBEDTLS_OID_AT_GIVEN_NAME, MBEDTLS_ASN1_UTF8_STRING },
88
    { ADD_STRLEN("initials"),
89
      MBEDTLS_OID_AT_INITIALS, MBEDTLS_ASN1_UTF8_STRING },
90
    { ADD_STRLEN("pseudonym"),
91
      MBEDTLS_OID_AT_PSEUDONYM, MBEDTLS_ASN1_UTF8_STRING },
92
    { ADD_STRLEN("generationQualifier"),
93
      MBEDTLS_OID_AT_GENERATION_QUALIFIER, MBEDTLS_ASN1_UTF8_STRING },
94
    { ADD_STRLEN("domainComponent"),
95
      MBEDTLS_OID_DOMAIN_COMPONENT, MBEDTLS_ASN1_IA5_STRING },
96
    { ADD_STRLEN("DC"),
97
      MBEDTLS_OID_DOMAIN_COMPONENT,   MBEDTLS_ASN1_IA5_STRING },
98
    { NULL, 0, NULL, MBEDTLS_ASN1_NULL }
99
};
100
101
static const x509_attr_descriptor_t *x509_attr_descr_from_name(const char *name, size_t name_len)
102
0
{
103
0
    const x509_attr_descriptor_t *cur;
104
105
0
    for (cur = x509_attrs; cur->name != NULL; cur++) {
106
0
        if (cur->name_len == name_len &&
107
0
            strncmp(cur->name, name, name_len) == 0) {
108
0
            break;
109
0
        }
110
0
    }
111
112
0
    if (cur->name == NULL) {
113
0
        return NULL;
114
0
    }
115
116
0
    return cur;
117
0
}
118
119
static int hex_to_int(char c)
120
0
{
121
0
    return ('0' <= c && c <= '9') ? (c - '0') :
122
0
           ('a' <= c && c <= 'f') ? (c - 'a' + 10) :
123
0
           ('A' <= c && c <= 'F') ? (c - 'A' + 10) : -1;
124
0
}
125
126
static int hexpair_to_int(const char *hexpair)
127
0
{
128
0
    int n1 = hex_to_int(*hexpair);
129
0
    int n2 = hex_to_int(*(hexpair + 1));
130
131
0
    if (n1 != -1 && n2 != -1) {
132
0
        return (n1 << 4) | n2;
133
0
    } else {
134
0
        return -1;
135
0
    }
136
0
}
137
138
static int parse_attribute_value_string(const char *s,
139
                                        int len,
140
                                        unsigned char *data,
141
                                        size_t *data_len)
142
0
{
143
0
    const char *c;
144
0
    const char *end = s + len;
145
0
    unsigned char *d = data;
146
0
    int n;
147
148
0
    for (c = s; c < end; c++) {
149
0
        if (*c == '\\') {
150
0
            c++;
151
152
            /* Check for valid escaped characters as per RFC 4514 Section 3 */
153
0
            if (c + 1 < end && (n = hexpair_to_int(c)) != -1) {
154
0
                if (n == 0) {
155
0
                    return MBEDTLS_ERR_X509_INVALID_NAME;
156
0
                }
157
0
                *(d++) = n;
158
0
                c++;
159
0
            } else if (c < end && strchr(" ,=+<>#;\"\\", *c)) {
160
0
                *(d++) = *c;
161
0
            } else {
162
0
                return MBEDTLS_ERR_X509_INVALID_NAME;
163
0
            }
164
0
        } else {
165
0
            *(d++) = *c;
166
0
        }
167
168
0
        if (d - data == MBEDTLS_X509_MAX_DN_NAME_SIZE) {
169
0
            return MBEDTLS_ERR_X509_INVALID_NAME;
170
0
        }
171
0
    }
172
0
    *data_len = (size_t) (d - data);
173
0
    return 0;
174
0
}
175
176
/** Parse a hexstring containing a DER-encoded string.
177
 *
178
 * \param s         A string of \p len bytes hexadecimal digits.
179
 * \param len       Number of bytes to read from \p s.
180
 * \param data      Output buffer of size \p data_size.
181
 *                  On success, it contains the payload that's DER-encoded
182
 *                  in the input (content without the tag and length).
183
 *                  If the DER tag is a string tag, the payload is guaranteed
184
 *                  not to contain null bytes.
185
 * \param data_size Length of the \p data buffer.
186
 * \param data_len  On success, the length of the parsed string.
187
 *                  It is guaranteed to be less than
188
 *                  #MBEDTLS_X509_MAX_DN_NAME_SIZE.
189
 * \param tag       The ASN.1 tag that the payload in \p data is encoded in.
190
 *
191
 * \retval          0 on success.
192
 * \retval          #MBEDTLS_ERR_X509_INVALID_NAME if \p s does not contain
193
 *                  a valid hexstring,
194
 *                  or if the decoded hexstring is not valid DER,
195
 *                  or if the payload does not fit in \p data,
196
 *                  or if the payload is more than
197
 *                  #MBEDTLS_X509_MAX_DN_NAME_SIZE bytes,
198
 *                  of if \p *tag is an ASN.1 string tag and the payload
199
 *                  contains a null byte.
200
 * \retval          #MBEDTLS_ERR_X509_ALLOC_FAILED on low memory.
201
 */
202
static int parse_attribute_value_hex_der_encoded(const char *s,
203
                                                 size_t len,
204
                                                 unsigned char *data,
205
                                                 size_t data_size,
206
                                                 size_t *data_len,
207
                                                 int *tag)
208
0
{
209
    /* Step 1: preliminary length checks. */
210
    /* Each byte is encoded by exactly two hexadecimal digits. */
211
0
    if (len % 2 != 0) {
212
        /* Odd number of hex digits */
213
0
        return MBEDTLS_ERR_X509_INVALID_NAME;
214
0
    }
215
0
    size_t const der_length = len / 2;
216
0
    if (der_length > MBEDTLS_X509_MAX_DN_NAME_SIZE + 4) {
217
        /* The payload would be more than MBEDTLS_X509_MAX_DN_NAME_SIZE
218
         * (after subtracting the ASN.1 tag and length). Reject this early
219
         * to avoid allocating a large intermediate buffer. */
220
0
        return MBEDTLS_ERR_X509_INVALID_NAME;
221
0
    }
222
0
    if (der_length < 1) {
223
        /* Avoid empty-buffer shenanigans. A valid DER encoding is never
224
         * empty. */
225
0
        return MBEDTLS_ERR_X509_INVALID_NAME;
226
0
    }
227
228
    /* Step 2: Decode the hex string into an intermediate buffer. */
229
0
    unsigned char *der = mbedtls_calloc(1, der_length);
230
0
    if (der == NULL) {
231
0
        return MBEDTLS_ERR_X509_ALLOC_FAILED;
232
0
    }
233
    /* Beyond this point, der needs to be freed on exit. */
234
0
    for (size_t i = 0; i < der_length; i++) {
235
0
        int c = hexpair_to_int(s + 2 * i);
236
0
        if (c < 0) {
237
0
            goto error;
238
0
        }
239
0
        der[i] = c;
240
0
    }
241
242
    /* Step 3: decode the DER. */
243
    /* We've checked that der_length >= 1 above. */
244
0
    *tag = der[0];
245
0
    {
246
0
        unsigned char *p = der + 1;
247
0
        if (mbedtls_asn1_get_len(&p, der + der_length, data_len) != 0) {
248
0
            goto error;
249
0
        }
250
        /* Now p points to the first byte of the payload inside der,
251
         * and *data_len is the length of the payload. */
252
253
        /* Step 4: payload validation */
254
0
        if (*data_len > MBEDTLS_X509_MAX_DN_NAME_SIZE) {
255
0
            goto error;
256
0
        }
257
        /* Strings must not contain null bytes. */
258
0
        if (MBEDTLS_ASN1_IS_STRING_TAG(*tag)) {
259
0
            for (size_t i = 0; i < *data_len; i++) {
260
0
                if (p[i] == 0) {
261
0
                    goto error;
262
0
                }
263
0
            }
264
0
        }
265
266
        /* Step 5: output the payload. */
267
0
        if (*data_len > data_size) {
268
0
            goto error;
269
0
        }
270
0
        memcpy(data, p, *data_len);
271
0
    }
272
0
    mbedtls_free(der);
273
274
0
    return 0;
275
276
0
error:
277
0
    mbedtls_free(der);
278
0
    return MBEDTLS_ERR_X509_INVALID_NAME;
279
0
}
280
281
int mbedtls_x509_string_to_names(mbedtls_asn1_named_data **head, const char *name)
282
0
{
283
0
    int ret = MBEDTLS_ERR_X509_INVALID_NAME;
284
0
    int parse_ret = 0;
285
0
    const char *s = name, *c = s;
286
0
    const char *end = s + strlen(s);
287
0
    mbedtls_asn1_buf oid = { .p = NULL, .len = 0, .tag = MBEDTLS_ASN1_NULL };
288
0
    const x509_attr_descriptor_t *attr_descr = NULL;
289
0
    int in_attr_type = 1;
290
0
    int tag;
291
0
    int numericoid = 0;
292
0
    unsigned char data[MBEDTLS_X509_MAX_DN_NAME_SIZE];
293
0
    size_t data_len = 0;
294
295
    /* Ensure the output parameter is not already populated.
296
     * (If it were, overwriting it would likely cause a memory leak.)
297
     */
298
0
    if (*head != NULL) {
299
0
        return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
300
0
    }
301
302
0
    while (c <= end) {
303
0
        if (in_attr_type && *c == '=') {
304
0
            if ((attr_descr = x509_attr_descr_from_name(s, (size_t) (c - s))) == NULL) {
305
0
                if ((mbedtls_oid_from_numeric_string(&oid, s, (size_t) (c - s))) != 0) {
306
0
                    return MBEDTLS_ERR_X509_INVALID_NAME;
307
0
                } else {
308
0
                    numericoid = 1;
309
0
                }
310
0
            } else {
311
0
                oid.len = strlen(attr_descr->oid);
312
0
                oid.p = mbedtls_calloc(1, oid.len);
313
0
                if (oid.p == NULL) {
314
0
                    return MBEDTLS_ERR_X509_ALLOC_FAILED;
315
0
                }
316
0
                memcpy(oid.p, attr_descr->oid, oid.len);
317
0
                numericoid = 0;
318
0
            }
319
320
0
            s = c + 1;
321
0
            in_attr_type = 0;
322
0
        }
323
324
0
        if (!in_attr_type && ((*c == ',' && *(c-1) != '\\') || c == end)) {
325
0
            if (s == c) {
326
0
                mbedtls_free(oid.p);
327
0
                return MBEDTLS_ERR_X509_INVALID_NAME;
328
0
            } else if (*s == '#') {
329
                /* We know that c >= s (loop invariant) and c != s (in this
330
                 * else branch), hence c - s - 1 >= 0. */
331
0
                parse_ret = parse_attribute_value_hex_der_encoded(
332
0
                    s + 1, (size_t) (c - s) - 1,
333
0
                    data, sizeof(data), &data_len, &tag);
334
0
                if (parse_ret != 0) {
335
0
                    mbedtls_free(oid.p);
336
0
                    return parse_ret;
337
0
                }
338
0
            } else {
339
0
                if (numericoid) {
340
0
                    mbedtls_free(oid.p);
341
0
                    return MBEDTLS_ERR_X509_INVALID_NAME;
342
0
                } else {
343
0
                    if ((parse_ret =
344
0
                             parse_attribute_value_string(s, (int) (c - s), data,
345
0
                                                          &data_len)) != 0) {
346
0
                        mbedtls_free(oid.p);
347
0
                        return parse_ret;
348
0
                    }
349
0
                    tag = attr_descr->default_tag;
350
0
                }
351
0
            }
352
353
0
            mbedtls_asn1_named_data *cur =
354
0
                mbedtls_asn1_store_named_data(head, (char *) oid.p, oid.len,
355
0
                                              (unsigned char *) data,
356
0
                                              data_len);
357
0
            mbedtls_free(oid.p);
358
0
            oid.p = NULL;
359
0
            if (cur == NULL) {
360
0
                return MBEDTLS_ERR_X509_ALLOC_FAILED;
361
0
            }
362
363
            // set tagType
364
0
            cur->val.tag = tag;
365
366
0
            while (c < end && *(c + 1) == ' ') {
367
0
                c++;
368
0
            }
369
370
0
            s = c + 1;
371
0
            in_attr_type = 1;
372
373
            /* Successfully parsed one name, update ret to success */
374
0
            ret = 0;
375
0
        }
376
0
        c++;
377
0
    }
378
0
    if (oid.p != NULL) {
379
0
        mbedtls_free(oid.p);
380
0
    }
381
0
    return ret;
382
0
}
383
384
/* The first byte of the value in the mbedtls_asn1_named_data structure is reserved
385
 * to store the critical boolean for us
386
 */
387
int mbedtls_x509_set_extension(mbedtls_asn1_named_data **head, const char *oid, size_t oid_len,
388
                               int critical, const unsigned char *val, size_t val_len)
389
0
{
390
0
    mbedtls_asn1_named_data *cur;
391
392
0
    if (val_len > (SIZE_MAX  - 1)) {
393
0
        return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
394
0
    }
395
396
0
    if ((cur = mbedtls_asn1_store_named_data(head, oid, oid_len,
397
0
                                             NULL, val_len + 1)) == NULL) {
398
0
        return MBEDTLS_ERR_X509_ALLOC_FAILED;
399
0
    }
400
401
0
    cur->val.p[0] = critical;
402
0
    memcpy(cur->val.p + 1, val, val_len);
403
404
0
    return 0;
405
0
}
406
407
/*
408
 *  RelativeDistinguishedName ::=
409
 *    SET OF AttributeTypeAndValue
410
 *
411
 *  AttributeTypeAndValue ::= SEQUENCE {
412
 *    type     AttributeType,
413
 *    value    AttributeValue }
414
 *
415
 *  AttributeType ::= OBJECT IDENTIFIER
416
 *
417
 *  AttributeValue ::= ANY DEFINED BY AttributeType
418
 */
419
static int x509_write_name(unsigned char **p,
420
                           unsigned char *start,
421
                           mbedtls_asn1_named_data *cur_name)
422
0
{
423
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
424
0
    size_t len = 0;
425
0
    const char *oid             = (const char *) cur_name->oid.p;
426
0
    size_t oid_len              = cur_name->oid.len;
427
0
    const unsigned char *name   = cur_name->val.p;
428
0
    size_t name_len             = cur_name->val.len;
429
430
    // Write correct string tag and value
431
0
    MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tagged_string(p, start,
432
0
                                                               cur_name->val.tag,
433
0
                                                               (const char *) name,
434
0
                                                               name_len));
435
    // Write OID
436
    //
437
0
    MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(p, start, oid,
438
0
                                                     oid_len));
439
440
0
    MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
441
0
    MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
442
0
                                                     MBEDTLS_ASN1_CONSTRUCTED |
443
0
                                                     MBEDTLS_ASN1_SEQUENCE));
444
445
0
    MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
446
0
    MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
447
0
                                                     MBEDTLS_ASN1_CONSTRUCTED |
448
0
                                                     MBEDTLS_ASN1_SET));
449
450
0
    return (int) len;
451
0
}
452
453
int mbedtls_x509_write_names(unsigned char **p, unsigned char *start,
454
                             mbedtls_asn1_named_data *first)
455
0
{
456
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
457
0
    size_t len = 0;
458
0
    mbedtls_asn1_named_data *cur = first;
459
460
0
    while (cur != NULL) {
461
0
        MBEDTLS_ASN1_CHK_ADD(len, x509_write_name(p, start, cur));
462
0
        cur = cur->next;
463
0
    }
464
465
0
    MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
466
0
    MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_CONSTRUCTED |
467
0
                                                     MBEDTLS_ASN1_SEQUENCE));
468
469
0
    return (int) len;
470
0
}
471
472
int mbedtls_x509_write_sig(unsigned char **p, unsigned char *start,
473
                           const char *oid, size_t oid_len,
474
                           unsigned char *sig, size_t size,
475
                           mbedtls_pk_type_t pk_alg)
476
0
{
477
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
478
0
    int write_null_par;
479
0
    size_t len = 0;
480
481
0
    if (*p < start || (size_t) (*p - start) < size) {
482
0
        return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
483
0
    }
484
485
0
    len = size;
486
0
    (*p) -= len;
487
0
    memcpy(*p, sig, len);
488
489
0
    if (*p - start < 1) {
490
0
        return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
491
0
    }
492
493
0
    *--(*p) = 0;
494
0
    len += 1;
495
496
0
    MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
497
0
    MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_BIT_STRING));
498
499
    // Write OID
500
    //
501
0
    if (pk_alg == MBEDTLS_PK_ECDSA) {
502
        /*
503
         * The AlgorithmIdentifier's parameters field must be absent for DSA/ECDSA signature
504
         * algorithms, see https://www.rfc-editor.org/rfc/rfc5480#page-17 and
505
         * https://www.rfc-editor.org/rfc/rfc5758#section-3.
506
         */
507
0
        write_null_par = 0;
508
0
    } else {
509
0
        write_null_par = 1;
510
0
    }
511
0
    MBEDTLS_ASN1_CHK_ADD(len,
512
0
                         mbedtls_asn1_write_algorithm_identifier_ext(p, start, oid, oid_len,
513
0
                                                                     0, write_null_par));
514
515
0
    return (int) len;
516
0
}
517
518
static int x509_write_extension(unsigned char **p, unsigned char *start,
519
                                mbedtls_asn1_named_data *ext)
520
0
{
521
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
522
0
    size_t len = 0;
523
524
0
    MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, ext->val.p + 1,
525
0
                                                            ext->val.len - 1));
526
0
    MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, ext->val.len - 1));
527
0
    MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_OCTET_STRING));
528
529
0
    if (ext->val.p[0] != 0) {
530
0
        MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_bool(p, start, 1));
531
0
    }
532
533
0
    MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, ext->oid.p,
534
0
                                                            ext->oid.len));
535
0
    MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, ext->oid.len));
536
0
    MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_OID));
537
538
0
    MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
539
0
    MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_CONSTRUCTED |
540
0
                                                     MBEDTLS_ASN1_SEQUENCE));
541
542
0
    return (int) len;
543
0
}
544
545
/*
546
 * Extension  ::=  SEQUENCE  {
547
 *     extnID      OBJECT IDENTIFIER,
548
 *     critical    BOOLEAN DEFAULT FALSE,
549
 *     extnValue   OCTET STRING
550
 *                 -- contains the DER encoding of an ASN.1 value
551
 *                 -- corresponding to the extension type identified
552
 *                 -- by extnID
553
 *     }
554
 */
555
int mbedtls_x509_write_extensions(unsigned char **p, unsigned char *start,
556
                                  mbedtls_asn1_named_data *first)
557
0
{
558
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
559
0
    size_t len = 0;
560
0
    mbedtls_asn1_named_data *cur_ext = first;
561
562
0
    while (cur_ext != NULL) {
563
0
        MBEDTLS_ASN1_CHK_ADD(len, x509_write_extension(p, start, cur_ext));
564
0
        cur_ext = cur_ext->next;
565
0
    }
566
567
0
    return (int) len;
568
0
}
569
570
#endif /* MBEDTLS_X509_CREATE_C */