1 | /* |
2 | * X.509 common functions for parsing and verification |
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 | * The ITU-T X.509 standard defines a certificate format for PKI. |
21 | * |
22 | * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs) |
23 | * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs) |
24 | * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10) |
25 | * |
26 | * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf |
27 | * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf |
28 | */ |
29 | |
30 | #include "common.h" |
31 | |
32 | #if defined(MBEDTLS_X509_USE_C) |
33 | |
34 | #include "mbedtls/x509.h" |
35 | #include "mbedtls/asn1.h" |
36 | #include "mbedtls/error.h" |
37 | #include "mbedtls/oid.h" |
38 | |
39 | #include <stdio.h> |
40 | #include <string.h> |
41 | |
42 | #if defined(MBEDTLS_PEM_PARSE_C) |
43 | #include "mbedtls/pem.h" |
44 | #endif |
45 | |
46 | #include "mbedtls/platform.h" |
47 | |
48 | #if defined(MBEDTLS_HAVE_TIME) |
49 | #include "mbedtls/platform_time.h" |
50 | #endif |
51 | #if defined(MBEDTLS_HAVE_TIME_DATE) |
52 | #include "mbedtls/platform_util.h" |
53 | #include <time.h> |
54 | #endif |
55 | |
56 | #define CHECK(code) \ |
57 | do { \ |
58 | if ((ret = (code)) != 0) { \ |
59 | return ret; \ |
60 | } \ |
61 | } while (0) |
62 | |
63 | #define CHECK_RANGE(min, max, val) \ |
64 | do { \ |
65 | if ((val) < (min) || (val) > (max)) { \ |
66 | return ret; \ |
67 | } \ |
68 | } while (0) |
69 | |
70 | /* |
71 | * CertificateSerialNumber ::= INTEGER |
72 | */ |
73 | int mbedtls_x509_get_serial(unsigned char **p, const unsigned char *end, |
74 | mbedtls_x509_buf *serial) |
75 | { |
76 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
77 | |
78 | if ((end - *p) < 1) { |
79 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL, |
80 | MBEDTLS_ERR_ASN1_OUT_OF_DATA); |
81 | } |
82 | |
83 | if (**p != (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_PRIMITIVE | 2) && |
84 | **p != MBEDTLS_ASN1_INTEGER) { |
85 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL, |
86 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG); |
87 | } |
88 | |
89 | serial->tag = *(*p)++; |
90 | |
91 | if ((ret = mbedtls_asn1_get_len(p, end, &serial->len)) != 0) { |
92 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL, ret); |
93 | } |
94 | |
95 | serial->p = *p; |
96 | *p += serial->len; |
97 | |
98 | return 0; |
99 | } |
100 | |
101 | /* Get an algorithm identifier without parameters (eg for signatures) |
102 | * |
103 | * AlgorithmIdentifier ::= SEQUENCE { |
104 | * algorithm OBJECT IDENTIFIER, |
105 | * parameters ANY DEFINED BY algorithm OPTIONAL } |
106 | */ |
107 | int mbedtls_x509_get_alg_null(unsigned char **p, const unsigned char *end, |
108 | mbedtls_x509_buf *alg) |
109 | { |
110 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
111 | |
112 | if ((ret = mbedtls_asn1_get_alg_null(p, end, alg)) != 0) { |
113 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret); |
114 | } |
115 | |
116 | return 0; |
117 | } |
118 | |
119 | /* |
120 | * Parse an algorithm identifier with (optional) parameters |
121 | */ |
122 | int mbedtls_x509_get_alg(unsigned char **p, const unsigned char *end, |
123 | mbedtls_x509_buf *alg, mbedtls_x509_buf *params) |
124 | { |
125 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
126 | |
127 | if ((ret = mbedtls_asn1_get_alg(p, end, alg, params)) != 0) { |
128 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret); |
129 | } |
130 | |
131 | return 0; |
132 | } |
133 | |
134 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
135 | /* |
136 | * HashAlgorithm ::= AlgorithmIdentifier |
137 | * |
138 | * AlgorithmIdentifier ::= SEQUENCE { |
139 | * algorithm OBJECT IDENTIFIER, |
140 | * parameters ANY DEFINED BY algorithm OPTIONAL } |
141 | * |
142 | * For HashAlgorithm, parameters MUST be NULL or absent. |
143 | */ |
144 | static int x509_get_hash_alg(const mbedtls_x509_buf *alg, mbedtls_md_type_t *md_alg) |
145 | { |
146 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
147 | unsigned char *p; |
148 | const unsigned char *end; |
149 | mbedtls_x509_buf md_oid; |
150 | size_t len; |
151 | |
152 | /* Make sure we got a SEQUENCE and setup bounds */ |
153 | if (alg->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) { |
154 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, |
155 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG); |
156 | } |
157 | |
158 | p = alg->p; |
159 | end = p + alg->len; |
160 | |
161 | if (p >= end) { |
162 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, |
163 | MBEDTLS_ERR_ASN1_OUT_OF_DATA); |
164 | } |
165 | |
166 | /* Parse md_oid */ |
167 | md_oid.tag = *p; |
168 | |
169 | if ((ret = mbedtls_asn1_get_tag(&p, end, &md_oid.len, MBEDTLS_ASN1_OID)) != 0) { |
170 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret); |
171 | } |
172 | |
173 | md_oid.p = p; |
174 | p += md_oid.len; |
175 | |
176 | /* Get md_alg from md_oid */ |
177 | if ((ret = mbedtls_oid_get_md_alg(&md_oid, md_alg)) != 0) { |
178 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret); |
179 | } |
180 | |
181 | /* Make sure params is absent of NULL */ |
182 | if (p == end) { |
183 | return 0; |
184 | } |
185 | |
186 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_NULL)) != 0 || len != 0) { |
187 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret); |
188 | } |
189 | |
190 | if (p != end) { |
191 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, |
192 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
193 | } |
194 | |
195 | return 0; |
196 | } |
197 | |
198 | /* |
199 | * RSASSA-PSS-params ::= SEQUENCE { |
200 | * hashAlgorithm [0] HashAlgorithm DEFAULT sha1Identifier, |
201 | * maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1Identifier, |
202 | * saltLength [2] INTEGER DEFAULT 20, |
203 | * trailerField [3] INTEGER DEFAULT 1 } |
204 | * -- Note that the tags in this Sequence are explicit. |
205 | * |
206 | * RFC 4055 (which defines use of RSASSA-PSS in PKIX) states that the value |
207 | * of trailerField MUST be 1, and PKCS#1 v2.2 doesn't even define any other |
208 | * option. Enforce this at parsing time. |
209 | */ |
210 | int mbedtls_x509_get_rsassa_pss_params(const mbedtls_x509_buf *params, |
211 | mbedtls_md_type_t *md_alg, mbedtls_md_type_t *mgf_md, |
212 | int *salt_len) |
213 | { |
214 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
215 | unsigned char *p; |
216 | const unsigned char *end, *end2; |
217 | size_t len; |
218 | mbedtls_x509_buf alg_id, alg_params; |
219 | |
220 | /* First set everything to defaults */ |
221 | *md_alg = MBEDTLS_MD_SHA1; |
222 | *mgf_md = MBEDTLS_MD_SHA1; |
223 | *salt_len = 20; |
224 | |
225 | /* Make sure params is a SEQUENCE and setup bounds */ |
226 | if (params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) { |
227 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, |
228 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG); |
229 | } |
230 | |
231 | p = (unsigned char *) params->p; |
232 | end = p + params->len; |
233 | |
234 | if (p == end) { |
235 | return 0; |
236 | } |
237 | |
238 | /* |
239 | * HashAlgorithm |
240 | */ |
241 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, |
242 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | |
243 | 0)) == 0) { |
244 | end2 = p + len; |
245 | |
246 | /* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */ |
247 | if ((ret = mbedtls_x509_get_alg_null(&p, end2, &alg_id)) != 0) { |
248 | return ret; |
249 | } |
250 | |
251 | if ((ret = mbedtls_oid_get_md_alg(&alg_id, md_alg)) != 0) { |
252 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret); |
253 | } |
254 | |
255 | if (p != end2) { |
256 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, |
257 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
258 | } |
259 | } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) { |
260 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret); |
261 | } |
262 | |
263 | if (p == end) { |
264 | return 0; |
265 | } |
266 | |
267 | /* |
268 | * MaskGenAlgorithm |
269 | */ |
270 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, |
271 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | |
272 | 1)) == 0) { |
273 | end2 = p + len; |
274 | |
275 | /* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */ |
276 | if ((ret = mbedtls_x509_get_alg(&p, end2, &alg_id, &alg_params)) != 0) { |
277 | return ret; |
278 | } |
279 | |
280 | /* Only MFG1 is recognised for now */ |
281 | if (MBEDTLS_OID_CMP(MBEDTLS_OID_MGF1, &alg_id) != 0) { |
282 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE, |
283 | MBEDTLS_ERR_OID_NOT_FOUND); |
284 | } |
285 | |
286 | /* Parse HashAlgorithm */ |
287 | if ((ret = x509_get_hash_alg(&alg_params, mgf_md)) != 0) { |
288 | return ret; |
289 | } |
290 | |
291 | if (p != end2) { |
292 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, |
293 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
294 | } |
295 | } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) { |
296 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret); |
297 | } |
298 | |
299 | if (p == end) { |
300 | return 0; |
301 | } |
302 | |
303 | /* |
304 | * salt_len |
305 | */ |
306 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, |
307 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | |
308 | 2)) == 0) { |
309 | end2 = p + len; |
310 | |
311 | if ((ret = mbedtls_asn1_get_int(&p, end2, salt_len)) != 0) { |
312 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret); |
313 | } |
314 | |
315 | if (p != end2) { |
316 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, |
317 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
318 | } |
319 | } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) { |
320 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret); |
321 | } |
322 | |
323 | if (p == end) { |
324 | return 0; |
325 | } |
326 | |
327 | /* |
328 | * trailer_field (if present, must be 1) |
329 | */ |
330 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, |
331 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | |
332 | 3)) == 0) { |
333 | int trailer_field; |
334 | |
335 | end2 = p + len; |
336 | |
337 | if ((ret = mbedtls_asn1_get_int(&p, end2, &trailer_field)) != 0) { |
338 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret); |
339 | } |
340 | |
341 | if (p != end2) { |
342 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, |
343 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
344 | } |
345 | |
346 | if (trailer_field != 1) { |
347 | return MBEDTLS_ERR_X509_INVALID_ALG; |
348 | } |
349 | } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) { |
350 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret); |
351 | } |
352 | |
353 | if (p != end) { |
354 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, |
355 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
356 | } |
357 | |
358 | return 0; |
359 | } |
360 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
361 | |
362 | /* |
363 | * AttributeTypeAndValue ::= SEQUENCE { |
364 | * type AttributeType, |
365 | * value AttributeValue } |
366 | * |
367 | * AttributeType ::= OBJECT IDENTIFIER |
368 | * |
369 | * AttributeValue ::= ANY DEFINED BY AttributeType |
370 | */ |
371 | static int x509_get_attr_type_value(unsigned char **p, |
372 | const unsigned char *end, |
373 | mbedtls_x509_name *cur) |
374 | { |
375 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
376 | size_t len; |
377 | mbedtls_x509_buf *oid; |
378 | mbedtls_x509_buf *val; |
379 | |
380 | if ((ret = mbedtls_asn1_get_tag(p, end, &len, |
381 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
382 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret); |
383 | } |
384 | |
385 | end = *p + len; |
386 | |
387 | if ((end - *p) < 1) { |
388 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, |
389 | MBEDTLS_ERR_ASN1_OUT_OF_DATA); |
390 | } |
391 | |
392 | oid = &cur->oid; |
393 | oid->tag = **p; |
394 | |
395 | if ((ret = mbedtls_asn1_get_tag(p, end, &oid->len, MBEDTLS_ASN1_OID)) != 0) { |
396 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret); |
397 | } |
398 | |
399 | oid->p = *p; |
400 | *p += oid->len; |
401 | |
402 | if ((end - *p) < 1) { |
403 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, |
404 | MBEDTLS_ERR_ASN1_OUT_OF_DATA); |
405 | } |
406 | |
407 | if (**p != MBEDTLS_ASN1_BMP_STRING && **p != MBEDTLS_ASN1_UTF8_STRING && |
408 | **p != MBEDTLS_ASN1_T61_STRING && **p != MBEDTLS_ASN1_PRINTABLE_STRING && |
409 | **p != MBEDTLS_ASN1_IA5_STRING && **p != MBEDTLS_ASN1_UNIVERSAL_STRING && |
410 | **p != MBEDTLS_ASN1_BIT_STRING) { |
411 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, |
412 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG); |
413 | } |
414 | |
415 | val = &cur->val; |
416 | val->tag = *(*p)++; |
417 | |
418 | if ((ret = mbedtls_asn1_get_len(p, end, &val->len)) != 0) { |
419 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret); |
420 | } |
421 | |
422 | val->p = *p; |
423 | *p += val->len; |
424 | |
425 | if (*p != end) { |
426 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, |
427 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
428 | } |
429 | |
430 | cur->next = NULL; |
431 | |
432 | return 0; |
433 | } |
434 | |
435 | /* |
436 | * Name ::= CHOICE { -- only one possibility for now -- |
437 | * rdnSequence RDNSequence } |
438 | * |
439 | * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName |
440 | * |
441 | * RelativeDistinguishedName ::= |
442 | * SET OF AttributeTypeAndValue |
443 | * |
444 | * AttributeTypeAndValue ::= SEQUENCE { |
445 | * type AttributeType, |
446 | * value AttributeValue } |
447 | * |
448 | * AttributeType ::= OBJECT IDENTIFIER |
449 | * |
450 | * AttributeValue ::= ANY DEFINED BY AttributeType |
451 | * |
452 | * The data structure is optimized for the common case where each RDN has only |
453 | * one element, which is represented as a list of AttributeTypeAndValue. |
454 | * For the general case we still use a flat list, but we mark elements of the |
455 | * same set so that they are "merged" together in the functions that consume |
456 | * this list, eg mbedtls_x509_dn_gets(). |
457 | * |
458 | * On success, this function may allocate a linked list starting at cur->next |
459 | * that must later be free'd by the caller using mbedtls_free(). In error |
460 | * cases, this function frees all allocated memory internally and the caller |
461 | * has no freeing responsibilities. |
462 | */ |
463 | int mbedtls_x509_get_name(unsigned char **p, const unsigned char *end, |
464 | mbedtls_x509_name *cur) |
465 | { |
466 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
467 | size_t set_len; |
468 | const unsigned char *end_set; |
469 | mbedtls_x509_name *head = cur; |
470 | mbedtls_x509_name *prev, *allocated; |
471 | |
472 | /* don't use recursion, we'd risk stack overflow if not optimized */ |
473 | while (1) { |
474 | /* |
475 | * parse SET |
476 | */ |
477 | if ((ret = mbedtls_asn1_get_tag(p, end, &set_len, |
478 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET)) != 0) { |
479 | ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret); |
480 | goto error; |
481 | } |
482 | |
483 | end_set = *p + set_len; |
484 | |
485 | while (1) { |
486 | if ((ret = x509_get_attr_type_value(p, end_set, cur)) != 0) { |
487 | goto error; |
488 | } |
489 | |
490 | if (*p == end_set) { |
491 | break; |
492 | } |
493 | |
494 | /* Mark this item as being no the only one in a set */ |
495 | cur->next_merged = 1; |
496 | |
497 | cur->next = mbedtls_calloc(1, sizeof(mbedtls_x509_name)); |
498 | |
499 | if (cur->next == NULL) { |
500 | ret = MBEDTLS_ERR_X509_ALLOC_FAILED; |
501 | goto error; |
502 | } |
503 | |
504 | cur = cur->next; |
505 | } |
506 | |
507 | /* |
508 | * continue until end of SEQUENCE is reached |
509 | */ |
510 | if (*p == end) { |
511 | return 0; |
512 | } |
513 | |
514 | cur->next = mbedtls_calloc(1, sizeof(mbedtls_x509_name)); |
515 | |
516 | if (cur->next == NULL) { |
517 | ret = MBEDTLS_ERR_X509_ALLOC_FAILED; |
518 | goto error; |
519 | } |
520 | |
521 | cur = cur->next; |
522 | } |
523 | |
524 | error: |
525 | /* Skip the first element as we did not allocate it */ |
526 | allocated = head->next; |
527 | |
528 | while (allocated != NULL) { |
529 | prev = allocated; |
530 | allocated = allocated->next; |
531 | |
532 | mbedtls_platform_zeroize(prev, sizeof(*prev)); |
533 | mbedtls_free(prev); |
534 | } |
535 | |
536 | mbedtls_platform_zeroize(head, sizeof(*head)); |
537 | |
538 | return ret; |
539 | } |
540 | |
541 | static int x509_parse_int(unsigned char **p, size_t n, int *res) |
542 | { |
543 | *res = 0; |
544 | |
545 | for (; n > 0; --n) { |
546 | if ((**p < '0') || (**p > '9')) { |
547 | return MBEDTLS_ERR_X509_INVALID_DATE; |
548 | } |
549 | |
550 | *res *= 10; |
551 | *res += (*(*p)++ - '0'); |
552 | } |
553 | |
554 | return 0; |
555 | } |
556 | |
557 | static int x509_date_is_valid(const mbedtls_x509_time *t) |
558 | { |
559 | int ret = MBEDTLS_ERR_X509_INVALID_DATE; |
560 | int month_len; |
561 | |
562 | CHECK_RANGE(0, 9999, t->year); |
563 | CHECK_RANGE(0, 23, t->hour); |
564 | CHECK_RANGE(0, 59, t->min); |
565 | CHECK_RANGE(0, 59, t->sec); |
566 | |
567 | switch (t->mon) { |
568 | case 1: case 3: case 5: case 7: case 8: case 10: case 12: |
569 | month_len = 31; |
570 | break; |
571 | case 4: case 6: case 9: case 11: |
572 | month_len = 30; |
573 | break; |
574 | case 2: |
575 | if ((!(t->year % 4) && t->year % 100) || |
576 | !(t->year % 400)) { |
577 | month_len = 29; |
578 | } else { |
579 | month_len = 28; |
580 | } |
581 | break; |
582 | default: |
583 | return ret; |
584 | } |
585 | CHECK_RANGE(1, month_len, t->day); |
586 | |
587 | return 0; |
588 | } |
589 | |
590 | /* |
591 | * Parse an ASN1_UTC_TIME (yearlen=2) or ASN1_GENERALIZED_TIME (yearlen=4) |
592 | * field. |
593 | */ |
594 | static int x509_parse_time(unsigned char **p, size_t len, size_t yearlen, |
595 | mbedtls_x509_time *tm) |
596 | { |
597 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
598 | |
599 | /* |
600 | * Minimum length is 10 or 12 depending on yearlen |
601 | */ |
602 | if (len < yearlen + 8) { |
603 | return MBEDTLS_ERR_X509_INVALID_DATE; |
604 | } |
605 | len -= yearlen + 8; |
606 | |
607 | /* |
608 | * Parse year, month, day, hour, minute |
609 | */ |
610 | CHECK(x509_parse_int(p, yearlen, &tm->year)); |
611 | if (2 == yearlen) { |
612 | if (tm->year < 50) { |
613 | tm->year += 100; |
614 | } |
615 | |
616 | tm->year += 1900; |
617 | } |
618 | |
619 | CHECK(x509_parse_int(p, 2, &tm->mon)); |
620 | CHECK(x509_parse_int(p, 2, &tm->day)); |
621 | CHECK(x509_parse_int(p, 2, &tm->hour)); |
622 | CHECK(x509_parse_int(p, 2, &tm->min)); |
623 | |
624 | /* |
625 | * Parse seconds if present |
626 | */ |
627 | if (len >= 2) { |
628 | CHECK(x509_parse_int(p, 2, &tm->sec)); |
629 | len -= 2; |
630 | } else { |
631 | return MBEDTLS_ERR_X509_INVALID_DATE; |
632 | } |
633 | |
634 | /* |
635 | * Parse trailing 'Z' if present |
636 | */ |
637 | if (1 == len && 'Z' == **p) { |
638 | (*p)++; |
639 | len--; |
640 | } |
641 | |
642 | /* |
643 | * We should have parsed all characters at this point |
644 | */ |
645 | if (0 != len) { |
646 | return MBEDTLS_ERR_X509_INVALID_DATE; |
647 | } |
648 | |
649 | CHECK(x509_date_is_valid(tm)); |
650 | |
651 | return 0; |
652 | } |
653 | |
654 | /* |
655 | * Time ::= CHOICE { |
656 | * utcTime UTCTime, |
657 | * generalTime GeneralizedTime } |
658 | */ |
659 | int mbedtls_x509_get_time(unsigned char **p, const unsigned char *end, |
660 | mbedtls_x509_time *tm) |
661 | { |
662 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
663 | size_t len, year_len; |
664 | unsigned char tag; |
665 | |
666 | if ((end - *p) < 1) { |
667 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, |
668 | MBEDTLS_ERR_ASN1_OUT_OF_DATA); |
669 | } |
670 | |
671 | tag = **p; |
672 | |
673 | if (tag == MBEDTLS_ASN1_UTC_TIME) { |
674 | year_len = 2; |
675 | } else if (tag == MBEDTLS_ASN1_GENERALIZED_TIME) { |
676 | year_len = 4; |
677 | } else { |
678 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, |
679 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG); |
680 | } |
681 | |
682 | (*p)++; |
683 | ret = mbedtls_asn1_get_len(p, end, &len); |
684 | |
685 | if (ret != 0) { |
686 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, ret); |
687 | } |
688 | |
689 | return x509_parse_time(p, len, year_len, tm); |
690 | } |
691 | |
692 | int mbedtls_x509_get_sig(unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig) |
693 | { |
694 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
695 | size_t len; |
696 | int tag_type; |
697 | |
698 | if ((end - *p) < 1) { |
699 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SIGNATURE, |
700 | MBEDTLS_ERR_ASN1_OUT_OF_DATA); |
701 | } |
702 | |
703 | tag_type = **p; |
704 | |
705 | if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) { |
706 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SIGNATURE, ret); |
707 | } |
708 | |
709 | sig->tag = tag_type; |
710 | sig->len = len; |
711 | sig->p = *p; |
712 | |
713 | *p += len; |
714 | |
715 | return 0; |
716 | } |
717 | |
718 | /* |
719 | * Get signature algorithm from alg OID and optional parameters |
720 | */ |
721 | int mbedtls_x509_get_sig_alg(const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params, |
722 | mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg, |
723 | void **sig_opts) |
724 | { |
725 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
726 | |
727 | if (*sig_opts != NULL) { |
728 | return MBEDTLS_ERR_X509_BAD_INPUT_DATA; |
729 | } |
730 | |
731 | if ((ret = mbedtls_oid_get_sig_alg(sig_oid, md_alg, pk_alg)) != 0) { |
732 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG, ret); |
733 | } |
734 | |
735 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
736 | if (*pk_alg == MBEDTLS_PK_RSASSA_PSS) { |
737 | mbedtls_pk_rsassa_pss_options *pss_opts; |
738 | |
739 | pss_opts = mbedtls_calloc(1, sizeof(mbedtls_pk_rsassa_pss_options)); |
740 | if (pss_opts == NULL) { |
741 | return MBEDTLS_ERR_X509_ALLOC_FAILED; |
742 | } |
743 | |
744 | ret = mbedtls_x509_get_rsassa_pss_params(sig_params, |
745 | md_alg, |
746 | &pss_opts->mgf1_hash_id, |
747 | &pss_opts->expected_salt_len); |
748 | if (ret != 0) { |
749 | mbedtls_free(pss_opts); |
750 | return ret; |
751 | } |
752 | |
753 | *sig_opts = (void *) pss_opts; |
754 | } else |
755 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
756 | { |
757 | /* Make sure parameters are absent or NULL */ |
758 | if ((sig_params->tag != MBEDTLS_ASN1_NULL && sig_params->tag != 0) || |
759 | sig_params->len != 0) { |
760 | return MBEDTLS_ERR_X509_INVALID_ALG; |
761 | } |
762 | } |
763 | |
764 | return 0; |
765 | } |
766 | |
767 | /* |
768 | * X.509 Extensions (No parsing of extensions, pointer should |
769 | * be either manually updated or extensions should be parsed!) |
770 | */ |
771 | int mbedtls_x509_get_ext(unsigned char **p, const unsigned char *end, |
772 | mbedtls_x509_buf *ext, int tag) |
773 | { |
774 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
775 | size_t len; |
776 | |
777 | /* Extension structure use EXPLICIT tagging. That is, the actual |
778 | * `Extensions` structure is wrapped by a tag-length pair using |
779 | * the respective context-specific tag. */ |
780 | ret = mbedtls_asn1_get_tag(p, end, &ext->len, |
781 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag); |
782 | if (ret != 0) { |
783 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret); |
784 | } |
785 | |
786 | ext->tag = MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag; |
787 | ext->p = *p; |
788 | end = *p + ext->len; |
789 | |
790 | /* |
791 | * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension |
792 | */ |
793 | if ((ret = mbedtls_asn1_get_tag(p, end, &len, |
794 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
795 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret); |
796 | } |
797 | |
798 | if (end != *p + len) { |
799 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
800 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
801 | } |
802 | |
803 | return 0; |
804 | } |
805 | |
806 | /* |
807 | * Store the name in printable form into buf; no more |
808 | * than size characters will be written |
809 | */ |
810 | int mbedtls_x509_dn_gets(char *buf, size_t size, const mbedtls_x509_name *dn) |
811 | { |
812 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
813 | size_t i, j, n; |
814 | unsigned char c, merge = 0; |
815 | const mbedtls_x509_name *name; |
816 | const char *short_name = NULL; |
817 | char s[MBEDTLS_X509_MAX_DN_NAME_SIZE], *p; |
818 | |
819 | memset(s, 0, sizeof(s)); |
820 | |
821 | name = dn; |
822 | p = buf; |
823 | n = size; |
824 | |
825 | while (name != NULL) { |
826 | if (!name->oid.p) { |
827 | name = name->next; |
828 | continue; |
829 | } |
830 | |
831 | if (name != dn) { |
832 | ret = mbedtls_snprintf(p, n, merge ? " + " : ", " ); |
833 | MBEDTLS_X509_SAFE_SNPRINTF; |
834 | } |
835 | |
836 | ret = mbedtls_oid_get_attr_short_name(&name->oid, &short_name); |
837 | |
838 | if (ret == 0) { |
839 | ret = mbedtls_snprintf(p, n, "%s=" , short_name); |
840 | } else { |
841 | ret = mbedtls_snprintf(p, n, "\?\?=" ); |
842 | } |
843 | MBEDTLS_X509_SAFE_SNPRINTF; |
844 | |
845 | for (i = 0, j = 0; i < name->val.len; i++, j++) { |
846 | if (j >= sizeof(s) - 1) { |
847 | return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL; |
848 | } |
849 | |
850 | c = name->val.p[i]; |
851 | // Special characters requiring escaping, RFC 1779 |
852 | if (c && strchr(",=+<>#;\"\\" , c)) { |
853 | if (j + 1 >= sizeof(s) - 1) { |
854 | return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL; |
855 | } |
856 | s[j++] = '\\'; |
857 | } |
858 | if (c < 32 || c >= 127) { |
859 | s[j] = '?'; |
860 | } else { |
861 | s[j] = c; |
862 | } |
863 | } |
864 | s[j] = '\0'; |
865 | ret = mbedtls_snprintf(p, n, "%s" , s); |
866 | MBEDTLS_X509_SAFE_SNPRINTF; |
867 | |
868 | merge = name->next_merged; |
869 | name = name->next; |
870 | } |
871 | |
872 | return (int) (size - n); |
873 | } |
874 | |
875 | /* |
876 | * Store the serial in printable form into buf; no more |
877 | * than size characters will be written |
878 | */ |
879 | int mbedtls_x509_serial_gets(char *buf, size_t size, const mbedtls_x509_buf *serial) |
880 | { |
881 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
882 | size_t i, n, nr; |
883 | char *p; |
884 | |
885 | p = buf; |
886 | n = size; |
887 | |
888 | nr = (serial->len <= 32) |
889 | ? serial->len : 28; |
890 | |
891 | for (i = 0; i < nr; i++) { |
892 | if (i == 0 && nr > 1 && serial->p[i] == 0x0) { |
893 | continue; |
894 | } |
895 | |
896 | ret = mbedtls_snprintf(p, n, "%02X%s" , |
897 | serial->p[i], (i < nr - 1) ? ":" : "" ); |
898 | MBEDTLS_X509_SAFE_SNPRINTF; |
899 | } |
900 | |
901 | if (nr != serial->len) { |
902 | ret = mbedtls_snprintf(p, n, "...." ); |
903 | MBEDTLS_X509_SAFE_SNPRINTF; |
904 | } |
905 | |
906 | return (int) (size - n); |
907 | } |
908 | |
909 | /* |
910 | * Helper for writing signature algorithms |
911 | */ |
912 | int mbedtls_x509_sig_alg_gets(char *buf, size_t size, const mbedtls_x509_buf *sig_oid, |
913 | mbedtls_pk_type_t pk_alg, mbedtls_md_type_t md_alg, |
914 | const void *sig_opts) |
915 | { |
916 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
917 | char *p = buf; |
918 | size_t n = size; |
919 | const char *desc = NULL; |
920 | |
921 | ret = mbedtls_oid_get_sig_alg_desc(sig_oid, &desc); |
922 | if (ret != 0) { |
923 | ret = mbedtls_snprintf(p, n, "???" ); |
924 | } else { |
925 | ret = mbedtls_snprintf(p, n, "%s" , desc); |
926 | } |
927 | MBEDTLS_X509_SAFE_SNPRINTF; |
928 | |
929 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
930 | if (pk_alg == MBEDTLS_PK_RSASSA_PSS) { |
931 | const mbedtls_pk_rsassa_pss_options *pss_opts; |
932 | const mbedtls_md_info_t *md_info, *mgf_md_info; |
933 | |
934 | pss_opts = (const mbedtls_pk_rsassa_pss_options *) sig_opts; |
935 | |
936 | md_info = mbedtls_md_info_from_type(md_alg); |
937 | mgf_md_info = mbedtls_md_info_from_type(pss_opts->mgf1_hash_id); |
938 | |
939 | ret = mbedtls_snprintf(p, n, " (%s, MGF1-%s, 0x%02X)" , |
940 | md_info ? mbedtls_md_get_name(md_info) : "???" , |
941 | mgf_md_info ? mbedtls_md_get_name(mgf_md_info) : "???" , |
942 | (unsigned int) pss_opts->expected_salt_len); |
943 | MBEDTLS_X509_SAFE_SNPRINTF; |
944 | } |
945 | #else |
946 | ((void) pk_alg); |
947 | ((void) md_alg); |
948 | ((void) sig_opts); |
949 | #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
950 | |
951 | return (int) (size - n); |
952 | } |
953 | |
954 | /* |
955 | * Helper for writing "RSA key size", "EC key size", etc |
956 | */ |
957 | int mbedtls_x509_key_size_helper(char *buf, size_t buf_size, const char *name) |
958 | { |
959 | char *p = buf; |
960 | size_t n = buf_size; |
961 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
962 | |
963 | ret = mbedtls_snprintf(p, n, "%s key size" , name); |
964 | MBEDTLS_X509_SAFE_SNPRINTF; |
965 | |
966 | return 0; |
967 | } |
968 | |
969 | #if defined(MBEDTLS_HAVE_TIME_DATE) |
970 | /* |
971 | * Set the time structure to the current time. |
972 | * Return 0 on success, non-zero on failure. |
973 | */ |
974 | static int x509_get_current_time(mbedtls_x509_time *now) |
975 | { |
976 | struct tm *lt, tm_buf; |
977 | mbedtls_time_t tt; |
978 | int ret = 0; |
979 | |
980 | tt = mbedtls_time(NULL); |
981 | lt = mbedtls_platform_gmtime_r(&tt, &tm_buf); |
982 | |
983 | if (lt == NULL) { |
984 | ret = -1; |
985 | } else { |
986 | now->year = lt->tm_year + 1900; |
987 | now->mon = lt->tm_mon + 1; |
988 | now->day = lt->tm_mday; |
989 | now->hour = lt->tm_hour; |
990 | now->min = lt->tm_min; |
991 | now->sec = lt->tm_sec; |
992 | } |
993 | |
994 | return ret; |
995 | } |
996 | |
997 | /* |
998 | * Return 0 if before <= after, 1 otherwise |
999 | */ |
1000 | static int x509_check_time(const mbedtls_x509_time *before, const mbedtls_x509_time *after) |
1001 | { |
1002 | if (before->year > after->year) { |
1003 | return 1; |
1004 | } |
1005 | |
1006 | if (before->year == after->year && |
1007 | before->mon > after->mon) { |
1008 | return 1; |
1009 | } |
1010 | |
1011 | if (before->year == after->year && |
1012 | before->mon == after->mon && |
1013 | before->day > after->day) { |
1014 | return 1; |
1015 | } |
1016 | |
1017 | if (before->year == after->year && |
1018 | before->mon == after->mon && |
1019 | before->day == after->day && |
1020 | before->hour > after->hour) { |
1021 | return 1; |
1022 | } |
1023 | |
1024 | if (before->year == after->year && |
1025 | before->mon == after->mon && |
1026 | before->day == after->day && |
1027 | before->hour == after->hour && |
1028 | before->min > after->min) { |
1029 | return 1; |
1030 | } |
1031 | |
1032 | if (before->year == after->year && |
1033 | before->mon == after->mon && |
1034 | before->day == after->day && |
1035 | before->hour == after->hour && |
1036 | before->min == after->min && |
1037 | before->sec > after->sec) { |
1038 | return 1; |
1039 | } |
1040 | |
1041 | return 0; |
1042 | } |
1043 | |
1044 | int mbedtls_x509_time_is_past(const mbedtls_x509_time *to) |
1045 | { |
1046 | mbedtls_x509_time now; |
1047 | |
1048 | if (x509_get_current_time(&now) != 0) { |
1049 | return 1; |
1050 | } |
1051 | |
1052 | return x509_check_time(&now, to); |
1053 | } |
1054 | |
1055 | int mbedtls_x509_time_is_future(const mbedtls_x509_time *from) |
1056 | { |
1057 | mbedtls_x509_time now; |
1058 | |
1059 | if (x509_get_current_time(&now) != 0) { |
1060 | return 1; |
1061 | } |
1062 | |
1063 | return x509_check_time(from, &now); |
1064 | } |
1065 | |
1066 | #else /* MBEDTLS_HAVE_TIME_DATE */ |
1067 | |
1068 | int mbedtls_x509_time_is_past(const mbedtls_x509_time *to) |
1069 | { |
1070 | ((void) to); |
1071 | return 0; |
1072 | } |
1073 | |
1074 | int mbedtls_x509_time_is_future(const mbedtls_x509_time *from) |
1075 | { |
1076 | ((void) from); |
1077 | return 0; |
1078 | } |
1079 | #endif /* MBEDTLS_HAVE_TIME_DATE */ |
1080 | |
1081 | #if defined(MBEDTLS_SELF_TEST) |
1082 | |
1083 | #include "mbedtls/x509_crt.h" |
1084 | #include "mbedtls/certs.h" |
1085 | |
1086 | /* |
1087 | * Checkup routine |
1088 | */ |
1089 | int mbedtls_x509_self_test(int verbose) |
1090 | { |
1091 | int ret = 0; |
1092 | #if defined(MBEDTLS_CERTS_C) && defined(MBEDTLS_SHA256_C) |
1093 | uint32_t flags; |
1094 | mbedtls_x509_crt cacert; |
1095 | mbedtls_x509_crt clicert; |
1096 | |
1097 | if (verbose != 0) { |
1098 | mbedtls_printf(" X.509 certificate load: " ); |
1099 | } |
1100 | |
1101 | mbedtls_x509_crt_init(&cacert); |
1102 | mbedtls_x509_crt_init(&clicert); |
1103 | |
1104 | ret = mbedtls_x509_crt_parse(&clicert, (const unsigned char *) mbedtls_test_cli_crt, |
1105 | mbedtls_test_cli_crt_len); |
1106 | if (ret != 0) { |
1107 | if (verbose != 0) { |
1108 | mbedtls_printf("failed\n" ); |
1109 | } |
1110 | |
1111 | goto cleanup; |
1112 | } |
1113 | |
1114 | ret = mbedtls_x509_crt_parse(&cacert, (const unsigned char *) mbedtls_test_ca_crt, |
1115 | mbedtls_test_ca_crt_len); |
1116 | if (ret != 0) { |
1117 | if (verbose != 0) { |
1118 | mbedtls_printf("failed\n" ); |
1119 | } |
1120 | |
1121 | goto cleanup; |
1122 | } |
1123 | |
1124 | if (verbose != 0) { |
1125 | mbedtls_printf("passed\n X.509 signature verify: " ); |
1126 | } |
1127 | |
1128 | ret = mbedtls_x509_crt_verify(&clicert, &cacert, NULL, NULL, &flags, NULL, NULL); |
1129 | if (ret != 0) { |
1130 | if (verbose != 0) { |
1131 | mbedtls_printf("failed\n" ); |
1132 | } |
1133 | |
1134 | goto cleanup; |
1135 | } |
1136 | |
1137 | if (verbose != 0) { |
1138 | mbedtls_printf("passed\n\n" ); |
1139 | } |
1140 | |
1141 | cleanup: |
1142 | mbedtls_x509_crt_free(&cacert); |
1143 | mbedtls_x509_crt_free(&clicert); |
1144 | #else |
1145 | ((void) verbose); |
1146 | #endif /* MBEDTLS_CERTS_C && MBEDTLS_SHA256_C */ |
1147 | return ret; |
1148 | } |
1149 | |
1150 | #endif /* MBEDTLS_SELF_TEST */ |
1151 | |
1152 | #endif /* MBEDTLS_X509_USE_C */ |
1153 | |