1 /*
2  * ASN.1 buffer writing functionality
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_ASN1_WRITE_C) || defined(MBEDTLS_X509_USE_C)
11 
12 #include "mbedtls/asn1write.h"
13 #include "mbedtls/error.h"
14 
15 #include <string.h>
16 
17 #include "mbedtls/platform.h"
18 
19 #if defined(MBEDTLS_ASN1_PARSE_C)
20 #include "mbedtls/asn1.h"
21 #endif
22 
mbedtls_asn1_write_len(unsigned char ** p,const unsigned char * start,size_t len)23 int mbedtls_asn1_write_len(unsigned char **p, const unsigned char *start, size_t len)
24 {
25 #if SIZE_MAX > 0xFFFFFFFF
26     if (len > 0xFFFFFFFF) {
27         return MBEDTLS_ERR_ASN1_INVALID_LENGTH;
28     }
29 #endif
30 
31     int required = 1;
32 
33     if (len >= 0x80) {
34         for (size_t l = len; l != 0; l >>= 8) {
35             required++;
36         }
37     }
38 
39     if (required > (*p - start)) {
40         return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
41     }
42 
43     do {
44         *--(*p) = MBEDTLS_BYTE_0(len);
45         len >>= 8;
46     } while (len);
47 
48     if (required > 1) {
49         *--(*p) = (unsigned char) (0x80 + required - 1);
50     }
51 
52     return required;
53 }
54 
mbedtls_asn1_write_tag(unsigned char ** p,const unsigned char * start,unsigned char tag)55 int mbedtls_asn1_write_tag(unsigned char **p, const unsigned char *start, unsigned char tag)
56 {
57     if (*p - start < 1) {
58         return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
59     }
60 
61     *--(*p) = tag;
62 
63     return 1;
64 }
65 #endif /* MBEDTLS_ASN1_WRITE_C || MBEDTLS_X509_USE_C */
66 
67 #if defined(MBEDTLS_ASN1_WRITE_C)
mbedtls_asn1_write_len_and_tag(unsigned char ** p,const unsigned char * start,size_t len,unsigned char tag)68 static int mbedtls_asn1_write_len_and_tag(unsigned char **p,
69                                           const unsigned char *start,
70                                           size_t len,
71                                           unsigned char tag)
72 {
73     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
74 
75     MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
76     MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, tag));
77 
78     return (int) len;
79 }
80 
mbedtls_asn1_write_raw_buffer(unsigned char ** p,const unsigned char * start,const unsigned char * buf,size_t size)81 int mbedtls_asn1_write_raw_buffer(unsigned char **p, const unsigned char *start,
82                                   const unsigned char *buf, size_t size)
83 {
84     size_t len = 0;
85 
86     if (*p < start || (size_t) (*p - start) < size) {
87         return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
88     }
89 
90     len = size;
91     (*p) -= len;
92     memcpy(*p, buf, len);
93 
94     return (int) len;
95 }
96 
97 #if defined(MBEDTLS_BIGNUM_C)
mbedtls_asn1_write_mpi(unsigned char ** p,const unsigned char * start,const mbedtls_mpi * X)98 int mbedtls_asn1_write_mpi(unsigned char **p, const unsigned char *start, const mbedtls_mpi *X)
99 {
100     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
101     size_t len = 0;
102 
103     // Write the MPI
104     //
105     len = mbedtls_mpi_size(X);
106 
107     /* DER represents 0 with a sign bit (0=nonnegative) and 7 value bits, not
108      * as 0 digits. We need to end up with 020100, not with 0200. */
109     if (len == 0) {
110         len = 1;
111     }
112 
113     if (*p < start || (size_t) (*p - start) < len) {
114         return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
115     }
116 
117     (*p) -= len;
118     MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(X, *p, len));
119 
120     // DER format assumes 2s complement for numbers, so the leftmost bit
121     // should be 0 for positive numbers and 1 for negative numbers.
122     //
123     if (X->s == 1 && **p & 0x80) {
124         if (*p - start < 1) {
125             return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
126         }
127 
128         *--(*p) = 0x00;
129         len += 1;
130     }
131 
132     ret = mbedtls_asn1_write_len_and_tag(p, start, len, MBEDTLS_ASN1_INTEGER);
133 
134 cleanup:
135     return ret;
136 }
137 #endif /* MBEDTLS_BIGNUM_C */
138 
mbedtls_asn1_write_null(unsigned char ** p,const unsigned char * start)139 int mbedtls_asn1_write_null(unsigned char **p, const unsigned char *start)
140 {
141     // Write NULL
142     //
143     return mbedtls_asn1_write_len_and_tag(p, start, 0, MBEDTLS_ASN1_NULL);
144 }
145 
mbedtls_asn1_write_oid(unsigned char ** p,const unsigned char * start,const char * oid,size_t oid_len)146 int mbedtls_asn1_write_oid(unsigned char **p, const unsigned char *start,
147                            const char *oid, size_t oid_len)
148 {
149     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
150     size_t len = 0;
151 
152     MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start,
153                                                             (const unsigned char *) oid, oid_len));
154     return mbedtls_asn1_write_len_and_tag(p, start, len, MBEDTLS_ASN1_OID);
155 }
156 
mbedtls_asn1_write_algorithm_identifier(unsigned char ** p,const unsigned char * start,const char * oid,size_t oid_len,size_t par_len)157 int mbedtls_asn1_write_algorithm_identifier(unsigned char **p, const unsigned char *start,
158                                             const char *oid, size_t oid_len,
159                                             size_t par_len)
160 {
161     return mbedtls_asn1_write_algorithm_identifier_ext(p, start, oid, oid_len, par_len, 1);
162 }
163 
mbedtls_asn1_write_algorithm_identifier_ext(unsigned char ** p,const unsigned char * start,const char * oid,size_t oid_len,size_t par_len,int has_par)164 int mbedtls_asn1_write_algorithm_identifier_ext(unsigned char **p, const unsigned char *start,
165                                                 const char *oid, size_t oid_len,
166                                                 size_t par_len, int has_par)
167 {
168     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
169     size_t len = 0;
170 
171     if (has_par) {
172         if (par_len == 0) {
173             MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_null(p, start));
174         } else {
175             len += par_len;
176         }
177     }
178 
179     MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(p, start, oid, oid_len));
180 
181     return mbedtls_asn1_write_len_and_tag(p, start, len,
182                                           MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
183 }
184 
mbedtls_asn1_write_bool(unsigned char ** p,const unsigned char * start,int boolean)185 int mbedtls_asn1_write_bool(unsigned char **p, const unsigned char *start, int boolean)
186 {
187     size_t len = 0;
188 
189     if (*p - start < 1) {
190         return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
191     }
192 
193     *--(*p) = (boolean) ? 255 : 0;
194     len++;
195 
196     return mbedtls_asn1_write_len_and_tag(p, start, len, MBEDTLS_ASN1_BOOLEAN);
197 }
198 
asn1_write_tagged_int(unsigned char ** p,const unsigned char * start,int val,int tag)199 static int asn1_write_tagged_int(unsigned char **p, const unsigned char *start, int val, int tag)
200 {
201     size_t len = 0;
202 
203     do {
204         if (*p - start < 1) {
205             return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
206         }
207         len += 1;
208         *--(*p) = val & 0xff;
209         val >>= 8;
210     } while (val > 0);
211 
212     if (**p & 0x80) {
213         if (*p - start < 1) {
214             return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
215         }
216         *--(*p) = 0x00;
217         len += 1;
218     }
219 
220     return mbedtls_asn1_write_len_and_tag(p, start, len, tag);
221 }
222 
mbedtls_asn1_write_int(unsigned char ** p,const unsigned char * start,int val)223 int mbedtls_asn1_write_int(unsigned char **p, const unsigned char *start, int val)
224 {
225     return asn1_write_tagged_int(p, start, val, MBEDTLS_ASN1_INTEGER);
226 }
227 
mbedtls_asn1_write_enum(unsigned char ** p,const unsigned char * start,int val)228 int mbedtls_asn1_write_enum(unsigned char **p, const unsigned char *start, int val)
229 {
230     return asn1_write_tagged_int(p, start, val, MBEDTLS_ASN1_ENUMERATED);
231 }
232 
mbedtls_asn1_write_tagged_string(unsigned char ** p,const unsigned char * start,int tag,const char * text,size_t text_len)233 int mbedtls_asn1_write_tagged_string(unsigned char **p, const unsigned char *start, int tag,
234                                      const char *text, size_t text_len)
235 {
236     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
237     size_t len = 0;
238 
239     MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start,
240                                                             (const unsigned char *) text,
241                                                             text_len));
242 
243     return mbedtls_asn1_write_len_and_tag(p, start, len, tag);
244 }
245 
mbedtls_asn1_write_utf8_string(unsigned char ** p,const unsigned char * start,const char * text,size_t text_len)246 int mbedtls_asn1_write_utf8_string(unsigned char **p, const unsigned char *start,
247                                    const char *text, size_t text_len)
248 {
249     return mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_UTF8_STRING, text, text_len);
250 }
251 
mbedtls_asn1_write_printable_string(unsigned char ** p,const unsigned char * start,const char * text,size_t text_len)252 int mbedtls_asn1_write_printable_string(unsigned char **p, const unsigned char *start,
253                                         const char *text, size_t text_len)
254 {
255     return mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_PRINTABLE_STRING, text,
256                                             text_len);
257 }
258 
mbedtls_asn1_write_ia5_string(unsigned char ** p,const unsigned char * start,const char * text,size_t text_len)259 int mbedtls_asn1_write_ia5_string(unsigned char **p, const unsigned char *start,
260                                   const char *text, size_t text_len)
261 {
262     return mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_IA5_STRING, text, text_len);
263 }
264 
mbedtls_asn1_write_named_bitstring(unsigned char ** p,const unsigned char * start,const unsigned char * buf,size_t bits)265 int mbedtls_asn1_write_named_bitstring(unsigned char **p,
266                                        const unsigned char *start,
267                                        const unsigned char *buf,
268                                        size_t bits)
269 {
270     size_t unused_bits, byte_len;
271     const unsigned char *cur_byte;
272     unsigned char cur_byte_shifted;
273     unsigned char bit;
274 
275     byte_len = (bits + 7) / 8;
276     unused_bits = (byte_len * 8) - bits;
277 
278     /*
279      * Named bitstrings require that trailing 0s are excluded in the encoding
280      * of the bitstring. Trailing 0s are considered part of the 'unused' bits
281      * when encoding this value in the first content octet
282      */
283     if (bits != 0) {
284         cur_byte = buf + byte_len - 1;
285         cur_byte_shifted = *cur_byte >> unused_bits;
286 
287         for (;;) {
288             bit = cur_byte_shifted & 0x1;
289             cur_byte_shifted >>= 1;
290 
291             if (bit != 0) {
292                 break;
293             }
294 
295             bits--;
296             if (bits == 0) {
297                 break;
298             }
299 
300             if (bits % 8 == 0) {
301                 cur_byte_shifted = *--cur_byte;
302             }
303         }
304     }
305 
306     return mbedtls_asn1_write_bitstring(p, start, buf, bits);
307 }
308 
mbedtls_asn1_write_bitstring(unsigned char ** p,const unsigned char * start,const unsigned char * buf,size_t bits)309 int mbedtls_asn1_write_bitstring(unsigned char **p, const unsigned char *start,
310                                  const unsigned char *buf, size_t bits)
311 {
312     size_t len = 0;
313     size_t unused_bits, byte_len;
314 
315     byte_len = (bits + 7) / 8;
316     unused_bits = (byte_len * 8) - bits;
317 
318     if (*p < start || (size_t) (*p - start) < byte_len + 1) {
319         return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
320     }
321 
322     len = byte_len + 1;
323 
324     /* Write the bitstring. Ensure the unused bits are zeroed */
325     if (byte_len > 0) {
326         byte_len--;
327         *--(*p) = buf[byte_len] & ~((0x1 << unused_bits) - 1);
328         (*p) -= byte_len;
329         memcpy(*p, buf, byte_len);
330     }
331 
332     /* Write unused bits */
333     *--(*p) = (unsigned char) unused_bits;
334 
335     return mbedtls_asn1_write_len_and_tag(p, start, len, MBEDTLS_ASN1_BIT_STRING);
336 }
337 
mbedtls_asn1_write_octet_string(unsigned char ** p,const unsigned char * start,const unsigned char * buf,size_t size)338 int mbedtls_asn1_write_octet_string(unsigned char **p, const unsigned char *start,
339                                     const unsigned char *buf, size_t size)
340 {
341     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
342     size_t len = 0;
343 
344     MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, buf, size));
345 
346     return mbedtls_asn1_write_len_and_tag(p, start, len, MBEDTLS_ASN1_OCTET_STRING);
347 }
348 
349 
350 #if !defined(MBEDTLS_ASN1_PARSE_C)
351 /* This is a copy of the ASN.1 parsing function mbedtls_asn1_find_named_data(),
352  * which is replicated to avoid a dependency ASN1_WRITE_C on ASN1_PARSE_C. */
asn1_find_named_data(mbedtls_asn1_named_data * list,const char * oid,size_t len)353 static mbedtls_asn1_named_data *asn1_find_named_data(
354     mbedtls_asn1_named_data *list,
355     const char *oid, size_t len)
356 {
357     while (list != NULL) {
358         if (list->oid.len == len &&
359             memcmp(list->oid.p, oid, len) == 0) {
360             break;
361         }
362 
363         list = list->next;
364     }
365 
366     return list;
367 }
368 #else
369 #define asn1_find_named_data(list, oid, len) \
370     ((mbedtls_asn1_named_data *) mbedtls_asn1_find_named_data(list, oid, len))
371 #endif
372 
mbedtls_asn1_store_named_data(mbedtls_asn1_named_data ** head,const char * oid,size_t oid_len,const unsigned char * val,size_t val_len)373 mbedtls_asn1_named_data *mbedtls_asn1_store_named_data(
374     mbedtls_asn1_named_data **head,
375     const char *oid, size_t oid_len,
376     const unsigned char *val,
377     size_t val_len)
378 {
379     mbedtls_asn1_named_data *cur;
380 
381     if ((cur = asn1_find_named_data(*head, oid, oid_len)) == NULL) {
382         // Add new entry if not present yet based on OID
383         //
384         cur = (mbedtls_asn1_named_data *) mbedtls_calloc(1,
385                                                          sizeof(mbedtls_asn1_named_data));
386         if (cur == NULL) {
387             return NULL;
388         }
389 
390         cur->oid.len = oid_len;
391         cur->oid.p = mbedtls_calloc(1, oid_len);
392         if (cur->oid.p == NULL) {
393             mbedtls_free(cur);
394             return NULL;
395         }
396 
397         memcpy(cur->oid.p, oid, oid_len);
398 
399         cur->val.len = val_len;
400         if (val_len != 0) {
401             cur->val.p = mbedtls_calloc(1, val_len);
402             if (cur->val.p == NULL) {
403                 mbedtls_free(cur->oid.p);
404                 mbedtls_free(cur);
405                 return NULL;
406             }
407         }
408 
409         cur->next = *head;
410         *head = cur;
411     } else if (val_len == 0) {
412         mbedtls_free(cur->val.p);
413         cur->val.p = NULL;
414     } else if (cur->val.len != val_len) {
415         /*
416          * Enlarge existing value buffer if needed
417          * Preserve old data until the allocation succeeded, to leave list in
418          * a consistent state in case allocation fails.
419          */
420         void *p = mbedtls_calloc(1, val_len);
421         if (p == NULL) {
422             return NULL;
423         }
424 
425         mbedtls_free(cur->val.p);
426         cur->val.p = p;
427         cur->val.len = val_len;
428     }
429 
430     if (val != NULL && val_len != 0) {
431         memcpy(cur->val.p, val, val_len);
432     }
433 
434     return cur;
435 }
436 #endif /* MBEDTLS_ASN1_WRITE_C */
437