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