1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24
25 #include "curl_setup.h"
26
27 #if defined(USE_GNUTLS) || defined(USE_WOLFSSL) || \
28 defined(USE_SCHANNEL) || defined(USE_SECTRANSP)
29
30 #if defined(USE_WOLFSSL) || defined(USE_SCHANNEL)
31 #define WANT_PARSEX509 /* uses Curl_parseX509() */
32 #endif
33
34 #if defined(USE_GNUTLS) || defined(USE_SCHANNEL) || defined(USE_SECTRANSP)
35 #define WANT_EXTRACT_CERTINFO /* uses Curl_extract_certinfo() */
36 #define WANT_PARSEX509 /* ... uses Curl_parseX509() */
37 #endif
38
39 #include <curl/curl.h>
40 #include "urldata.h"
41 #include "strcase.h"
42 #include "curl_ctype.h"
43 #include "hostcheck.h"
44 #include "vtls/vtls.h"
45 #include "vtls/vtls_int.h"
46 #include "sendf.h"
47 #include "inet_pton.h"
48 #include "curl_base64.h"
49 #include "x509asn1.h"
50 #include "dynbuf.h"
51
52 /* The last 3 #include files should be in this order */
53 #include "curl_printf.h"
54 #include "curl_memory.h"
55 #include "memdebug.h"
56
57 /*
58 * Constants.
59 */
60
61 /* Largest supported ASN.1 structure. */
62 #define CURL_ASN1_MAX ((size_t) 0x40000) /* 256K */
63
64 /* ASN.1 classes. */
65 #define CURL_ASN1_UNIVERSAL 0
66 #define CURL_ASN1_APPLICATION 1
67 #define CURL_ASN1_CONTEXT_SPECIFIC 2
68 #define CURL_ASN1_PRIVATE 3
69
70 /* ASN.1 types. */
71 #define CURL_ASN1_BOOLEAN 1
72 #define CURL_ASN1_INTEGER 2
73 #define CURL_ASN1_BIT_STRING 3
74 #define CURL_ASN1_OCTET_STRING 4
75 #define CURL_ASN1_NULL 5
76 #define CURL_ASN1_OBJECT_IDENTIFIER 6
77 #define CURL_ASN1_OBJECT_DESCRIPTOR 7
78 #define CURL_ASN1_INSTANCE_OF 8
79 #define CURL_ASN1_REAL 9
80 #define CURL_ASN1_ENUMERATED 10
81 #define CURL_ASN1_EMBEDDED 11
82 #define CURL_ASN1_UTF8_STRING 12
83 #define CURL_ASN1_RELATIVE_OID 13
84 #define CURL_ASN1_SEQUENCE 16
85 #define CURL_ASN1_SET 17
86 #define CURL_ASN1_NUMERIC_STRING 18
87 #define CURL_ASN1_PRINTABLE_STRING 19
88 #define CURL_ASN1_TELETEX_STRING 20
89 #define CURL_ASN1_VIDEOTEX_STRING 21
90 #define CURL_ASN1_IA5_STRING 22
91 #define CURL_ASN1_UTC_TIME 23
92 #define CURL_ASN1_GENERALIZED_TIME 24
93 #define CURL_ASN1_GRAPHIC_STRING 25
94 #define CURL_ASN1_VISIBLE_STRING 26
95 #define CURL_ASN1_GENERAL_STRING 27
96 #define CURL_ASN1_UNIVERSAL_STRING 28
97 #define CURL_ASN1_CHARACTER_STRING 29
98 #define CURL_ASN1_BMP_STRING 30
99
100 /* Max sixes */
101
102 #define MAX_X509_STR 10000
103 #define MAX_X509_CERT 100000
104
105 #ifdef WANT_EXTRACT_CERTINFO
106 /* ASN.1 OID table entry. */
107 struct Curl_OID {
108 const char *numoid; /* Dotted-numeric OID. */
109 const char *textoid; /* OID name. */
110 };
111
112 /* ASN.1 OIDs. */
113 static const char cnOID[] = "2.5.4.3"; /* Common name. */
114 static const char sanOID[] = "2.5.29.17"; /* Subject alternative name. */
115
116 static const struct Curl_OID OIDtable[] = {
117 { "1.2.840.10040.4.1", "dsa" },
118 { "1.2.840.10040.4.3", "dsa-with-sha1" },
119 { "1.2.840.10045.2.1", "ecPublicKey" },
120 { "1.2.840.10045.3.0.1", "c2pnb163v1" },
121 { "1.2.840.10045.4.1", "ecdsa-with-SHA1" },
122 { "1.2.840.10046.2.1", "dhpublicnumber" },
123 { "1.2.840.113549.1.1.1", "rsaEncryption" },
124 { "1.2.840.113549.1.1.2", "md2WithRSAEncryption" },
125 { "1.2.840.113549.1.1.4", "md5WithRSAEncryption" },
126 { "1.2.840.113549.1.1.5", "sha1WithRSAEncryption" },
127 { "1.2.840.113549.1.1.10", "RSASSA-PSS" },
128 { "1.2.840.113549.1.1.14", "sha224WithRSAEncryption" },
129 { "1.2.840.113549.1.1.11", "sha256WithRSAEncryption" },
130 { "1.2.840.113549.1.1.12", "sha384WithRSAEncryption" },
131 { "1.2.840.113549.1.1.13", "sha512WithRSAEncryption" },
132 { "1.2.840.113549.2.2", "md2" },
133 { "1.2.840.113549.2.5", "md5" },
134 { "1.3.14.3.2.26", "sha1" },
135 { cnOID, "CN" },
136 { "2.5.4.4", "SN" },
137 { "2.5.4.5", "serialNumber" },
138 { "2.5.4.6", "C" },
139 { "2.5.4.7", "L" },
140 { "2.5.4.8", "ST" },
141 { "2.5.4.9", "streetAddress" },
142 { "2.5.4.10", "O" },
143 { "2.5.4.11", "OU" },
144 { "2.5.4.12", "title" },
145 { "2.5.4.13", "description" },
146 { "2.5.4.17", "postalCode" },
147 { "2.5.4.41", "name" },
148 { "2.5.4.42", "givenName" },
149 { "2.5.4.43", "initials" },
150 { "2.5.4.44", "generationQualifier" },
151 { "2.5.4.45", "X500UniqueIdentifier" },
152 { "2.5.4.46", "dnQualifier" },
153 { "2.5.4.65", "pseudonym" },
154 { "1.2.840.113549.1.9.1", "emailAddress" },
155 { "2.5.4.72", "role" },
156 { sanOID, "subjectAltName" },
157 { "2.5.29.18", "issuerAltName" },
158 { "2.5.29.19", "basicConstraints" },
159 { "2.16.840.1.101.3.4.2.4", "sha224" },
160 { "2.16.840.1.101.3.4.2.1", "sha256" },
161 { "2.16.840.1.101.3.4.2.2", "sha384" },
162 { "2.16.840.1.101.3.4.2.3", "sha512" },
163 { "1.2.840.113549.1.9.2", "unstructuredName" },
164 { (const char *) NULL, (const char *) NULL }
165 };
166
167 #endif /* WANT_EXTRACT_CERTINFO */
168
169 /*
170 * Lightweight ASN.1 parser.
171 * In particular, it does not check for syntactic/lexical errors.
172 * It is intended to support certificate information gathering for SSL backends
173 * that offer a mean to get certificates as a whole, but do not supply
174 * entry points to get particular certificate sub-fields.
175 * Please note there is no pretension here to rewrite a full SSL library.
176 */
177
178 static const char *getASN1Element(struct Curl_asn1Element *elem,
179 const char *beg, const char *end)
180 WARN_UNUSED_RESULT;
181
getASN1Element(struct Curl_asn1Element * elem,const char * beg,const char * end)182 static const char *getASN1Element(struct Curl_asn1Element *elem,
183 const char *beg, const char *end)
184 {
185 unsigned char b;
186 size_t len;
187 struct Curl_asn1Element lelem;
188
189 /* Get a single ASN.1 element into `elem', parse ASN.1 string at `beg'
190 ending at `end'.
191 Returns a pointer in source string after the parsed element, or NULL
192 if an error occurs. */
193 if(!beg || !end || beg >= end || !*beg ||
194 (size_t)(end - beg) > CURL_ASN1_MAX)
195 return NULL;
196
197 /* Process header byte. */
198 elem->header = beg;
199 b = (unsigned char) *beg++;
200 elem->constructed = (b & 0x20) != 0;
201 elem->class = (b >> 6) & 3;
202 b &= 0x1F;
203 if(b == 0x1F)
204 return NULL; /* Long tag values not supported here. */
205 elem->tag = b;
206
207 /* Process length. */
208 if(beg >= end)
209 return NULL;
210 b = (unsigned char) *beg++;
211 if(!(b & 0x80))
212 len = b;
213 else if(!(b &= 0x7F)) {
214 /* Unspecified length. Since we have all the data, we can determine the
215 effective length by skipping element until an end element is found. */
216 if(!elem->constructed)
217 return NULL;
218 elem->beg = beg;
219 while(beg < end && *beg) {
220 beg = getASN1Element(&lelem, beg, end);
221 if(!beg)
222 return NULL;
223 }
224 if(beg >= end)
225 return NULL;
226 elem->end = beg;
227 return beg + 1;
228 }
229 else if((unsigned)b > (size_t)(end - beg))
230 return NULL; /* Does not fit in source. */
231 else {
232 /* Get long length. */
233 len = 0;
234 do {
235 if(len & 0xFF000000L)
236 return NULL; /* Lengths > 32 bits are not supported. */
237 len = (len << 8) | (unsigned char) *beg++;
238 } while(--b);
239 }
240 if(len > (size_t)(end - beg))
241 return NULL; /* Element data does not fit in source. */
242 elem->beg = beg;
243 elem->end = beg + len;
244 return elem->end;
245 }
246
247 #ifdef WANT_EXTRACT_CERTINFO
248
249 /*
250 * Search the null terminated OID or OID identifier in local table.
251 * Return the table entry pointer or NULL if not found.
252 */
searchOID(const char * oid)253 static const struct Curl_OID *searchOID(const char *oid)
254 {
255 const struct Curl_OID *op;
256 for(op = OIDtable; op->numoid; op++)
257 if(!strcmp(op->numoid, oid) || strcasecompare(op->textoid, oid))
258 return op;
259
260 return NULL;
261 }
262
263 /*
264 * Convert an ASN.1 Boolean value into its string representation.
265 *
266 * Return error code.
267 */
268
bool2str(struct dynbuf * store,const char * beg,const char * end)269 static CURLcode bool2str(struct dynbuf *store,
270 const char *beg, const char *end)
271 {
272 if(end - beg != 1)
273 return CURLE_BAD_FUNCTION_ARGUMENT;
274 return Curl_dyn_add(store, *beg? "TRUE": "FALSE");
275 }
276
277 /*
278 * Convert an ASN.1 octet string to a printable string.
279 *
280 * Return error code.
281 */
octet2str(struct dynbuf * store,const char * beg,const char * end)282 static CURLcode octet2str(struct dynbuf *store,
283 const char *beg, const char *end)
284 {
285 CURLcode result = CURLE_OK;
286
287 while(!result && beg < end)
288 result = Curl_dyn_addf(store, "%02x:", (unsigned char) *beg++);
289
290 return result;
291 }
292
bit2str(struct dynbuf * store,const char * beg,const char * end)293 static CURLcode bit2str(struct dynbuf *store,
294 const char *beg, const char *end)
295 {
296 /* Convert an ASN.1 bit string to a printable string. */
297
298 if(++beg > end)
299 return CURLE_BAD_FUNCTION_ARGUMENT;
300 return octet2str(store, beg, end);
301 }
302
303 /*
304 * Convert an ASN.1 integer value into its string representation.
305 *
306 * Returns error.
307 */
int2str(struct dynbuf * store,const char * beg,const char * end)308 static CURLcode int2str(struct dynbuf *store,
309 const char *beg, const char *end)
310 {
311 unsigned int val = 0;
312 size_t n = end - beg;
313
314 if(!n)
315 return CURLE_BAD_FUNCTION_ARGUMENT;
316
317 if(n > 4)
318 return octet2str(store, beg, end);
319
320 /* Represent integers <= 32-bit as a single value. */
321 if(*beg & 0x80)
322 val = ~val;
323
324 do
325 val = (val << 8) | *(const unsigned char *) beg++;
326 while(beg < end);
327 return Curl_dyn_addf(store, "%s%x", val >= 10? "0x": "", val);
328 }
329
330 /*
331 * Convert from an ASN.1 typed string to UTF8.
332 *
333 * The result is stored in a dynbuf that is inited by the user of this
334 * function.
335 *
336 * Returns error.
337 */
338 static CURLcode
utf8asn1str(struct dynbuf * to,int type,const char * from,const char * end)339 utf8asn1str(struct dynbuf *to, int type, const char *from, const char *end)
340 {
341 size_t inlength = end - from;
342 int size = 1;
343 CURLcode result = CURLE_OK;
344
345 switch(type) {
346 case CURL_ASN1_BMP_STRING:
347 size = 2;
348 break;
349 case CURL_ASN1_UNIVERSAL_STRING:
350 size = 4;
351 break;
352 case CURL_ASN1_NUMERIC_STRING:
353 case CURL_ASN1_PRINTABLE_STRING:
354 case CURL_ASN1_TELETEX_STRING:
355 case CURL_ASN1_IA5_STRING:
356 case CURL_ASN1_VISIBLE_STRING:
357 case CURL_ASN1_UTF8_STRING:
358 break;
359 default:
360 return CURLE_BAD_FUNCTION_ARGUMENT; /* Conversion not supported. */
361 }
362
363 if(inlength % size)
364 /* Length inconsistent with character size. */
365 return CURLE_BAD_FUNCTION_ARGUMENT;
366
367 if(type == CURL_ASN1_UTF8_STRING) {
368 /* Just copy. */
369 if(inlength)
370 result = Curl_dyn_addn(to, from, inlength);
371 }
372 else {
373 while(!result && (from < end)) {
374 char buf[4]; /* decode buffer */
375 int charsize = 1;
376 unsigned int wc = 0;
377
378 switch(size) {
379 case 4:
380 wc = (wc << 8) | *(const unsigned char *) from++;
381 wc = (wc << 8) | *(const unsigned char *) from++;
382 FALLTHROUGH();
383 case 2:
384 wc = (wc << 8) | *(const unsigned char *) from++;
385 FALLTHROUGH();
386 default: /* case 1: */
387 wc = (wc << 8) | *(const unsigned char *) from++;
388 }
389 if(wc >= 0x00000080) {
390 if(wc >= 0x00000800) {
391 if(wc >= 0x00010000) {
392 if(wc >= 0x00200000) {
393 free(buf);
394 /* Invalid char. size for target encoding. */
395 return CURLE_WEIRD_SERVER_REPLY;
396 }
397 buf[3] = (char) (0x80 | (wc & 0x3F));
398 wc = (wc >> 6) | 0x00010000;
399 charsize++;
400 }
401 buf[2] = (char) (0x80 | (wc & 0x3F));
402 wc = (wc >> 6) | 0x00000800;
403 charsize++;
404 }
405 buf[1] = (char) (0x80 | (wc & 0x3F));
406 wc = (wc >> 6) | 0x000000C0;
407 charsize++;
408 }
409 buf[0] = (char) wc;
410 result = Curl_dyn_addn(to, buf, charsize);
411 }
412 }
413 return result;
414 }
415
416 /*
417 * Convert an ASN.1 OID into its dotted string representation.
418 *
419 * Return error code.
420 */
encodeOID(struct dynbuf * store,const char * beg,const char * end)421 static CURLcode encodeOID(struct dynbuf *store,
422 const char *beg, const char *end)
423 {
424 unsigned int x;
425 unsigned int y;
426 CURLcode result = CURLE_OK;
427
428 /* Process the first two numbers. */
429 y = *(const unsigned char *) beg++;
430 x = y / 40;
431 y -= x * 40;
432
433 result = Curl_dyn_addf(store, "%u.%u", x, y);
434 if(result)
435 return result;
436
437 /* Process the trailing numbers. */
438 while(beg < end) {
439 x = 0;
440 do {
441 if(x & 0xFF000000)
442 return 0;
443 y = *(const unsigned char *) beg++;
444 x = (x << 7) | (y & 0x7F);
445 } while(y & 0x80);
446 result = Curl_dyn_addf(store, ".%u", x);
447 }
448 return result;
449 }
450
451 /*
452 * Convert an ASN.1 OID into its dotted or symbolic string representation.
453 *
454 * Return error code.
455 */
456
OID2str(struct dynbuf * store,const char * beg,const char * end,bool symbolic)457 static CURLcode OID2str(struct dynbuf *store,
458 const char *beg, const char *end, bool symbolic)
459 {
460 CURLcode result = CURLE_OK;
461 if(beg < end) {
462 if(symbolic) {
463 struct dynbuf buf;
464 Curl_dyn_init(&buf, MAX_X509_STR);
465 result = encodeOID(&buf, beg, end);
466
467 if(!result) {
468 const struct Curl_OID *op = searchOID(Curl_dyn_ptr(&buf));
469 if(op)
470 result = Curl_dyn_add(store, op->textoid);
471 else
472 result = CURLE_BAD_FUNCTION_ARGUMENT;
473 Curl_dyn_free(&buf);
474 }
475 }
476 else
477 result = encodeOID(store, beg, end);
478 }
479 return result;
480 }
481
GTime2str(struct dynbuf * store,const char * beg,const char * end)482 static CURLcode GTime2str(struct dynbuf *store,
483 const char *beg, const char *end)
484 {
485 const char *tzp;
486 const char *fracp;
487 char sec1, sec2;
488 size_t fracl;
489 size_t tzl;
490 const char *sep = "";
491
492 /* Convert an ASN.1 Generalized time to a printable string.
493 Return the dynamically allocated string, or NULL if an error occurs. */
494
495 for(fracp = beg; fracp < end && *fracp >= '0' && *fracp <= '9'; fracp++)
496 ;
497
498 /* Get seconds digits. */
499 sec1 = '0';
500 switch(fracp - beg - 12) {
501 case 0:
502 sec2 = '0';
503 break;
504 case 2:
505 sec1 = fracp[-2];
506 FALLTHROUGH();
507 case 1:
508 sec2 = fracp[-1];
509 break;
510 default:
511 return CURLE_BAD_FUNCTION_ARGUMENT;
512 }
513
514 /* Scan for timezone, measure fractional seconds. */
515 tzp = fracp;
516 fracl = 0;
517 if(fracp < end && (*fracp == '.' || *fracp == ',')) {
518 fracp++;
519 do
520 tzp++;
521 while(tzp < end && *tzp >= '0' && *tzp <= '9');
522 /* Strip leading zeroes in fractional seconds. */
523 for(fracl = tzp - fracp - 1; fracl && fracp[fracl - 1] == '0'; fracl--)
524 ;
525 }
526
527 /* Process timezone. */
528 if(tzp >= end)
529 ; /* Nothing to do. */
530 else if(*tzp == 'Z') {
531 tzp = " GMT";
532 end = tzp + 4;
533 }
534 else {
535 sep = " ";
536 tzp++;
537 }
538
539 tzl = end - tzp;
540 return Curl_dyn_addf(store,
541 "%.4s-%.2s-%.2s %.2s:%.2s:%c%c%s%.*s%s%.*s",
542 beg, beg + 4, beg + 6,
543 beg + 8, beg + 10, sec1, sec2,
544 fracl? ".": "", (int)fracl, fracp,
545 sep, (int)tzl, tzp);
546 }
547
548 /*
549 * Convert an ASN.1 UTC time to a printable string.
550 *
551 * Return error code.
552 */
UTime2str(struct dynbuf * store,const char * beg,const char * end)553 static CURLcode UTime2str(struct dynbuf *store,
554 const char *beg, const char *end)
555 {
556 const char *tzp;
557 size_t tzl;
558 const char *sec;
559
560 for(tzp = beg; tzp < end && *tzp >= '0' && *tzp <= '9'; tzp++)
561 ;
562 /* Get the seconds. */
563 sec = beg + 10;
564 switch(tzp - sec) {
565 case 0:
566 sec = "00";
567 FALLTHROUGH();
568 case 2:
569 break;
570 default:
571 return CURLE_BAD_FUNCTION_ARGUMENT;
572 }
573
574 /* Process timezone. */
575 if(tzp >= end)
576 return CURLE_BAD_FUNCTION_ARGUMENT;
577 if(*tzp == 'Z') {
578 tzp = "GMT";
579 end = tzp + 3;
580 }
581 else
582 tzp++;
583
584 tzl = end - tzp;
585 return Curl_dyn_addf(store, "%u%.2s-%.2s-%.2s %.2s:%.2s:%.2s %.*s",
586 20 - (*beg >= '5'), beg, beg + 2, beg + 4,
587 beg + 6, beg + 8, sec,
588 (int)tzl, tzp);
589 }
590
591 /*
592 * Convert an ASN.1 element to a printable string.
593 *
594 * Return error
595 */
ASN1tostr(struct dynbuf * store,struct Curl_asn1Element * elem,int type)596 static CURLcode ASN1tostr(struct dynbuf *store,
597 struct Curl_asn1Element *elem, int type)
598 {
599 CURLcode result = CURLE_BAD_FUNCTION_ARGUMENT;
600 if(elem->constructed)
601 return CURLE_OK; /* No conversion of structured elements. */
602
603 if(!type)
604 type = elem->tag; /* Type not forced: use element tag as type. */
605
606 switch(type) {
607 case CURL_ASN1_BOOLEAN:
608 result = bool2str(store, elem->beg, elem->end);
609 break;
610 case CURL_ASN1_INTEGER:
611 case CURL_ASN1_ENUMERATED:
612 result = int2str(store, elem->beg, elem->end);
613 break;
614 case CURL_ASN1_BIT_STRING:
615 result = bit2str(store, elem->beg, elem->end);
616 break;
617 case CURL_ASN1_OCTET_STRING:
618 result = octet2str(store, elem->beg, elem->end);
619 break;
620 case CURL_ASN1_NULL:
621 result = Curl_dyn_addn(store, "", 1);
622 break;
623 case CURL_ASN1_OBJECT_IDENTIFIER:
624 result = OID2str(store, elem->beg, elem->end, TRUE);
625 break;
626 case CURL_ASN1_UTC_TIME:
627 result = UTime2str(store, elem->beg, elem->end);
628 break;
629 case CURL_ASN1_GENERALIZED_TIME:
630 result = GTime2str(store, elem->beg, elem->end);
631 break;
632 case CURL_ASN1_UTF8_STRING:
633 case CURL_ASN1_NUMERIC_STRING:
634 case CURL_ASN1_PRINTABLE_STRING:
635 case CURL_ASN1_TELETEX_STRING:
636 case CURL_ASN1_IA5_STRING:
637 case CURL_ASN1_VISIBLE_STRING:
638 case CURL_ASN1_UNIVERSAL_STRING:
639 case CURL_ASN1_BMP_STRING:
640 result = utf8asn1str(store, type, elem->beg, elem->end);
641 break;
642 }
643
644 return result;
645 }
646
647 /*
648 * ASCII encode distinguished name at `dn' into the store dynbuf.
649 *
650 * Returns error.
651 */
encodeDN(struct dynbuf * store,struct Curl_asn1Element * dn)652 static CURLcode encodeDN(struct dynbuf *store, struct Curl_asn1Element *dn)
653 {
654 struct Curl_asn1Element rdn;
655 struct Curl_asn1Element atv;
656 struct Curl_asn1Element oid;
657 struct Curl_asn1Element value;
658 const char *p1;
659 const char *p2;
660 const char *p3;
661 const char *str;
662 CURLcode result = CURLE_OK;
663 bool added = FALSE;
664 struct dynbuf temp;
665 Curl_dyn_init(&temp, MAX_X509_STR);
666
667 for(p1 = dn->beg; p1 < dn->end;) {
668 p1 = getASN1Element(&rdn, p1, dn->end);
669 if(!p1) {
670 result = CURLE_BAD_FUNCTION_ARGUMENT;
671 goto error;
672 }
673 for(p2 = rdn.beg; p2 < rdn.end;) {
674 p2 = getASN1Element(&atv, p2, rdn.end);
675 if(!p2) {
676 result = CURLE_BAD_FUNCTION_ARGUMENT;
677 goto error;
678 }
679 p3 = getASN1Element(&oid, atv.beg, atv.end);
680 if(!p3) {
681 result = CURLE_BAD_FUNCTION_ARGUMENT;
682 goto error;
683 }
684 if(!getASN1Element(&value, p3, atv.end)) {
685 result = CURLE_BAD_FUNCTION_ARGUMENT;
686 goto error;
687 }
688 Curl_dyn_reset(&temp);
689 result = ASN1tostr(&temp, &oid, 0);
690 if(result)
691 goto error;
692
693 str = Curl_dyn_ptr(&temp);
694
695 /* Encode delimiter.
696 If attribute has a short uppercase name, delimiter is ", ". */
697 for(p3 = str; ISUPPER(*p3); p3++)
698 ;
699 if(added) {
700 if(p3 - str > 2)
701 result = Curl_dyn_addn(store, "/", 1);
702 else
703 result = Curl_dyn_addn(store, ", ", 2);
704 if(result)
705 goto error;
706 }
707
708 /* Encode attribute name. */
709 result = Curl_dyn_add(store, str);
710 if(result)
711 goto error;
712
713 /* Generate equal sign. */
714 result = Curl_dyn_addn(store, "=", 1);
715 if(result)
716 goto error;
717
718 /* Generate value. */
719 result = ASN1tostr(store, &value, 0);
720 if(result)
721 goto error;
722 Curl_dyn_reset(&temp);
723 added = TRUE; /* use separator for next */
724 }
725 }
726 error:
727 Curl_dyn_free(&temp);
728
729 return result;
730 }
731
732 #endif /* WANT_EXTRACT_CERTINFO */
733
734 #ifdef WANT_PARSEX509
735 /*
736 * ASN.1 parse an X509 certificate into structure subfields.
737 * Syntax is assumed to have already been checked by the SSL backend.
738 * See RFC 5280.
739 */
Curl_parseX509(struct Curl_X509certificate * cert,const char * beg,const char * end)740 int Curl_parseX509(struct Curl_X509certificate *cert,
741 const char *beg, const char *end)
742 {
743 struct Curl_asn1Element elem;
744 struct Curl_asn1Element tbsCertificate;
745 const char *ccp;
746 static const char defaultVersion = 0; /* v1. */
747
748 cert->certificate.header = NULL;
749 cert->certificate.beg = beg;
750 cert->certificate.end = end;
751
752 /* Get the sequence content. */
753 if(!getASN1Element(&elem, beg, end))
754 return -1; /* Invalid bounds/size. */
755 beg = elem.beg;
756 end = elem.end;
757
758 /* Get tbsCertificate. */
759 beg = getASN1Element(&tbsCertificate, beg, end);
760 if(!beg)
761 return -1;
762 /* Skip the signatureAlgorithm. */
763 beg = getASN1Element(&cert->signatureAlgorithm, beg, end);
764 if(!beg)
765 return -1;
766 /* Get the signatureValue. */
767 if(!getASN1Element(&cert->signature, beg, end))
768 return -1;
769
770 /* Parse TBSCertificate. */
771 beg = tbsCertificate.beg;
772 end = tbsCertificate.end;
773 /* Get optional version, get serialNumber. */
774 cert->version.header = NULL;
775 cert->version.beg = &defaultVersion;
776 cert->version.end = &defaultVersion + sizeof(defaultVersion);
777 beg = getASN1Element(&elem, beg, end);
778 if(!beg)
779 return -1;
780 if(elem.tag == 0) {
781 if(!getASN1Element(&cert->version, elem.beg, elem.end))
782 return -1;
783 beg = getASN1Element(&elem, beg, end);
784 if(!beg)
785 return -1;
786 }
787 cert->serialNumber = elem;
788 /* Get signature algorithm. */
789 beg = getASN1Element(&cert->signatureAlgorithm, beg, end);
790 /* Get issuer. */
791 beg = getASN1Element(&cert->issuer, beg, end);
792 if(!beg)
793 return -1;
794 /* Get notBefore and notAfter. */
795 beg = getASN1Element(&elem, beg, end);
796 if(!beg)
797 return -1;
798 ccp = getASN1Element(&cert->notBefore, elem.beg, elem.end);
799 if(!ccp)
800 return -1;
801 if(!getASN1Element(&cert->notAfter, ccp, elem.end))
802 return -1;
803 /* Get subject. */
804 beg = getASN1Element(&cert->subject, beg, end);
805 if(!beg)
806 return -1;
807 /* Get subjectPublicKeyAlgorithm and subjectPublicKey. */
808 beg = getASN1Element(&cert->subjectPublicKeyInfo, beg, end);
809 if(!beg)
810 return -1;
811 ccp = getASN1Element(&cert->subjectPublicKeyAlgorithm,
812 cert->subjectPublicKeyInfo.beg,
813 cert->subjectPublicKeyInfo.end);
814 if(!ccp)
815 return -1;
816 if(!getASN1Element(&cert->subjectPublicKey, ccp,
817 cert->subjectPublicKeyInfo.end))
818 return -1;
819 /* Get optional issuerUiqueID, subjectUniqueID and extensions. */
820 cert->issuerUniqueID.tag = cert->subjectUniqueID.tag = 0;
821 cert->extensions.tag = elem.tag = 0;
822 cert->issuerUniqueID.header = cert->subjectUniqueID.header = NULL;
823 cert->issuerUniqueID.beg = cert->issuerUniqueID.end = "";
824 cert->subjectUniqueID.beg = cert->subjectUniqueID.end = "";
825 cert->extensions.header = NULL;
826 cert->extensions.beg = cert->extensions.end = "";
827 if(beg < end) {
828 beg = getASN1Element(&elem, beg, end);
829 if(!beg)
830 return -1;
831 }
832 if(elem.tag == 1) {
833 cert->issuerUniqueID = elem;
834 if(beg < end) {
835 beg = getASN1Element(&elem, beg, end);
836 if(!beg)
837 return -1;
838 }
839 }
840 if(elem.tag == 2) {
841 cert->subjectUniqueID = elem;
842 if(beg < end) {
843 beg = getASN1Element(&elem, beg, end);
844 if(!beg)
845 return -1;
846 }
847 }
848 if(elem.tag == 3)
849 if(!getASN1Element(&cert->extensions, elem.beg, elem.end))
850 return -1;
851 return 0;
852 }
853
854 #endif /* WANT_PARSEX509 */
855
856 #ifdef WANT_EXTRACT_CERTINFO
857
dumpAlgo(struct dynbuf * store,struct Curl_asn1Element * param,const char * beg,const char * end)858 static CURLcode dumpAlgo(struct dynbuf *store,
859 struct Curl_asn1Element *param,
860 const char *beg, const char *end)
861 {
862 struct Curl_asn1Element oid;
863
864 /* Get algorithm parameters and return algorithm name. */
865
866 beg = getASN1Element(&oid, beg, end);
867 if(!beg)
868 return CURLE_BAD_FUNCTION_ARGUMENT;
869 param->header = NULL;
870 param->tag = 0;
871 param->beg = param->end = end;
872 if(beg < end) {
873 const char *p = getASN1Element(param, beg, end);
874 if(!p)
875 return CURLE_BAD_FUNCTION_ARGUMENT;
876 }
877 return OID2str(store, oid.beg, oid.end, TRUE);
878 }
879
880 /*
881 * This is a convenience function for push_certinfo_len that takes a zero
882 * terminated value.
883 */
ssl_push_certinfo(struct Curl_easy * data,int certnum,const char * label,const char * value)884 static CURLcode ssl_push_certinfo(struct Curl_easy *data,
885 int certnum,
886 const char *label,
887 const char *value)
888 {
889 size_t valuelen = strlen(value);
890
891 return Curl_ssl_push_certinfo_len(data, certnum, label, value, valuelen);
892 }
893
894 /*
895 * This is a convenience function for push_certinfo_len that takes a
896 * dynbuf value.
897 *
898 * It also does the verbose output if !certnum.
899 */
ssl_push_certinfo_dyn(struct Curl_easy * data,int certnum,const char * label,struct dynbuf * ptr)900 static CURLcode ssl_push_certinfo_dyn(struct Curl_easy *data,
901 int certnum,
902 const char *label,
903 struct dynbuf *ptr)
904 {
905 size_t valuelen = Curl_dyn_len(ptr);
906 char *value = Curl_dyn_ptr(ptr);
907
908 CURLcode result = Curl_ssl_push_certinfo_len(data, certnum, label,
909 value, valuelen);
910
911 if(!certnum && !result)
912 infof(data, " %s: %s", label, value);
913
914 return result;
915 }
916
do_pubkey_field(struct Curl_easy * data,int certnum,const char * label,struct Curl_asn1Element * elem)917 static CURLcode do_pubkey_field(struct Curl_easy *data, int certnum,
918 const char *label,
919 struct Curl_asn1Element *elem)
920 {
921 CURLcode result;
922 struct dynbuf out;
923
924 Curl_dyn_init(&out, MAX_X509_STR);
925
926 /* Generate a certificate information record for the public key. */
927
928 result = ASN1tostr(&out, elem, 0);
929 if(!result) {
930 if(data->set.ssl.certinfo)
931 result = ssl_push_certinfo_dyn(data, certnum, label, &out);
932 Curl_dyn_free(&out);
933 }
934 return result;
935 }
936
937 /* return 0 on success, 1 on error */
do_pubkey(struct Curl_easy * data,int certnum,const char * algo,struct Curl_asn1Element * param,struct Curl_asn1Element * pubkey)938 static int do_pubkey(struct Curl_easy *data, int certnum,
939 const char *algo, struct Curl_asn1Element *param,
940 struct Curl_asn1Element *pubkey)
941 {
942 struct Curl_asn1Element elem;
943 struct Curl_asn1Element pk;
944 const char *p;
945
946 /* Generate all information records for the public key. */
947
948 if(strcasecompare(algo, "ecPublicKey")) {
949 /*
950 * ECC public key is all the data, a value of type BIT STRING mapped to
951 * OCTET STRING and should not be parsed as an ASN.1 value.
952 */
953 const size_t len = ((pubkey->end - pubkey->beg - 2) * 4);
954 if(!certnum)
955 infof(data, " ECC Public Key (%zu bits)", len);
956 if(data->set.ssl.certinfo) {
957 char q[sizeof(len) * 8 / 3 + 1];
958 (void)msnprintf(q, sizeof(q), "%zu", len);
959 if(ssl_push_certinfo(data, certnum, "ECC Public Key", q))
960 return 1;
961 }
962 return do_pubkey_field(data, certnum, "ecPublicKey", pubkey);
963 }
964
965 /* Get the public key (single element). */
966 if(!getASN1Element(&pk, pubkey->beg + 1, pubkey->end))
967 return 1;
968
969 if(strcasecompare(algo, "rsaEncryption")) {
970 const char *q;
971 size_t len;
972
973 p = getASN1Element(&elem, pk.beg, pk.end);
974 if(!p)
975 return 1;
976
977 /* Compute key length. */
978 for(q = elem.beg; !*q && q < elem.end; q++)
979 ;
980 len = ((elem.end - q) * 8);
981 if(len) {
982 unsigned int i;
983 for(i = *(unsigned char *) q; !(i & 0x80); i <<= 1)
984 len--;
985 }
986 if(len > 32)
987 elem.beg = q; /* Strip leading zero bytes. */
988 if(!certnum)
989 infof(data, " RSA Public Key (%zu bits)", len);
990 if(data->set.ssl.certinfo) {
991 char r[sizeof(len) * 8 / 3 + 1];
992 msnprintf(r, sizeof(r), "%zu", len);
993 if(ssl_push_certinfo(data, certnum, "RSA Public Key", r))
994 return 1;
995 }
996 /* Generate coefficients. */
997 if(do_pubkey_field(data, certnum, "rsa(n)", &elem))
998 return 1;
999 if(!getASN1Element(&elem, p, pk.end))
1000 return 1;
1001 if(do_pubkey_field(data, certnum, "rsa(e)", &elem))
1002 return 1;
1003 }
1004 else if(strcasecompare(algo, "dsa")) {
1005 p = getASN1Element(&elem, param->beg, param->end);
1006 if(p) {
1007 if(do_pubkey_field(data, certnum, "dsa(p)", &elem))
1008 return 1;
1009 p = getASN1Element(&elem, p, param->end);
1010 if(p) {
1011 if(do_pubkey_field(data, certnum, "dsa(q)", &elem))
1012 return 1;
1013 if(getASN1Element(&elem, p, param->end)) {
1014 if(do_pubkey_field(data, certnum, "dsa(g)", &elem))
1015 return 1;
1016 if(do_pubkey_field(data, certnum, "dsa(pub_key)", &pk))
1017 return 1;
1018 }
1019 }
1020 }
1021 }
1022 else if(strcasecompare(algo, "dhpublicnumber")) {
1023 p = getASN1Element(&elem, param->beg, param->end);
1024 if(p) {
1025 if(do_pubkey_field(data, certnum, "dh(p)", &elem))
1026 return 1;
1027 if(getASN1Element(&elem, param->beg, param->end)) {
1028 if(do_pubkey_field(data, certnum, "dh(g)", &elem))
1029 return 1;
1030 if(do_pubkey_field(data, certnum, "dh(pub_key)", &pk))
1031 return 1;
1032 }
1033 }
1034 }
1035 return 0;
1036 }
1037
1038 /*
1039 * Convert an ASN.1 distinguished name into a printable string.
1040 * Return error.
1041 */
DNtostr(struct dynbuf * store,struct Curl_asn1Element * dn)1042 static CURLcode DNtostr(struct dynbuf *store,
1043 struct Curl_asn1Element *dn)
1044 {
1045 return encodeDN(store, dn);
1046 }
1047
Curl_extract_certinfo(struct Curl_easy * data,int certnum,const char * beg,const char * end)1048 CURLcode Curl_extract_certinfo(struct Curl_easy *data,
1049 int certnum,
1050 const char *beg,
1051 const char *end)
1052 {
1053 struct Curl_X509certificate cert;
1054 struct Curl_asn1Element param;
1055 char *certptr;
1056 size_t clen;
1057 struct dynbuf out;
1058 CURLcode result = CURLE_OK;
1059 unsigned int version;
1060 const char *ptr;
1061 int rc;
1062
1063 if(!data->set.ssl.certinfo)
1064 if(certnum)
1065 return CURLE_OK;
1066
1067 Curl_dyn_init(&out, MAX_X509_STR);
1068 /* Prepare the certificate information for curl_easy_getinfo(). */
1069
1070 /* Extract the certificate ASN.1 elements. */
1071 if(Curl_parseX509(&cert, beg, end))
1072 return CURLE_PEER_FAILED_VERIFICATION;
1073
1074 /* Subject. */
1075 result = DNtostr(&out, &cert.subject);
1076 if(result)
1077 goto done;
1078 if(data->set.ssl.certinfo) {
1079 result = ssl_push_certinfo_dyn(data, certnum, "Subject", &out);
1080 if(result)
1081 goto done;
1082 }
1083 Curl_dyn_reset(&out);
1084
1085 /* Issuer. */
1086 result = DNtostr(&out, &cert.issuer);
1087 if(result)
1088 goto done;
1089 if(data->set.ssl.certinfo) {
1090 result = ssl_push_certinfo_dyn(data, certnum, "Issuer", &out);
1091 if(result)
1092 goto done;
1093 }
1094 Curl_dyn_reset(&out);
1095
1096 /* Version (always fits in less than 32 bits). */
1097 version = 0;
1098 for(ptr = cert.version.beg; ptr < cert.version.end; ptr++)
1099 version = (version << 8) | *(const unsigned char *) ptr;
1100 if(data->set.ssl.certinfo) {
1101 result = Curl_dyn_addf(&out, "%x", version);
1102 if(result)
1103 goto done;
1104 result = ssl_push_certinfo_dyn(data, certnum, "Version", &out);
1105 if(result)
1106 goto done;
1107 Curl_dyn_reset(&out);
1108 }
1109
1110 /* Serial number. */
1111 result = ASN1tostr(&out, &cert.serialNumber, 0);
1112 if(result)
1113 goto done;
1114 if(data->set.ssl.certinfo) {
1115 result = ssl_push_certinfo_dyn(data, certnum, "Serial Number", &out);
1116 if(result)
1117 goto done;
1118 }
1119 Curl_dyn_reset(&out);
1120
1121 /* Signature algorithm .*/
1122 result = dumpAlgo(&out, ¶m, cert.signatureAlgorithm.beg,
1123 cert.signatureAlgorithm.end);
1124 if(result)
1125 goto done;
1126 if(data->set.ssl.certinfo) {
1127 result = ssl_push_certinfo_dyn(data, certnum, "Signature Algorithm",
1128 &out);
1129 if(result)
1130 goto done;
1131 }
1132 Curl_dyn_reset(&out);
1133
1134 /* Start Date. */
1135 result = ASN1tostr(&out, &cert.notBefore, 0);
1136 if(result)
1137 goto done;
1138 if(data->set.ssl.certinfo) {
1139 result = ssl_push_certinfo_dyn(data, certnum, "Start Date", &out);
1140 if(result)
1141 goto done;
1142 }
1143 Curl_dyn_reset(&out);
1144
1145 /* Expire Date. */
1146 result = ASN1tostr(&out, &cert.notAfter, 0);
1147 if(result)
1148 goto done;
1149 if(data->set.ssl.certinfo) {
1150 result = ssl_push_certinfo_dyn(data, certnum, "Expire Date", &out);
1151 if(result)
1152 goto done;
1153 }
1154 Curl_dyn_reset(&out);
1155
1156 /* Public Key Algorithm. */
1157 result = dumpAlgo(&out, ¶m, cert.subjectPublicKeyAlgorithm.beg,
1158 cert.subjectPublicKeyAlgorithm.end);
1159 if(result)
1160 goto done;
1161 if(data->set.ssl.certinfo) {
1162 result = ssl_push_certinfo_dyn(data, certnum, "Public Key Algorithm",
1163 &out);
1164 if(result)
1165 goto done;
1166 }
1167
1168 rc = do_pubkey(data, certnum, Curl_dyn_ptr(&out),
1169 ¶m, &cert.subjectPublicKey);
1170 if(rc) {
1171 result = CURLE_OUT_OF_MEMORY; /* the most likely error */
1172 goto done;
1173 }
1174 Curl_dyn_reset(&out);
1175
1176 /* Signature. */
1177 result = ASN1tostr(&out, &cert.signature, 0);
1178 if(result)
1179 goto done;
1180 if(data->set.ssl.certinfo) {
1181 result = ssl_push_certinfo_dyn(data, certnum, "Signature", &out);
1182 if(result)
1183 goto done;
1184 }
1185 Curl_dyn_reset(&out);
1186
1187 /* Generate PEM certificate. */
1188 result = Curl_base64_encode(cert.certificate.beg,
1189 cert.certificate.end - cert.certificate.beg,
1190 &certptr, &clen);
1191 if(result)
1192 goto done;
1193
1194 /* Generate the final output certificate string. Format is:
1195 -----BEGIN CERTIFICATE-----\n
1196 <max 64 base64 characters>\n
1197 .
1198 .
1199 .
1200 -----END CERTIFICATE-----\n
1201 */
1202
1203 Curl_dyn_reset(&out);
1204
1205 /* Build the certificate string. */
1206 result = Curl_dyn_add(&out, "-----BEGIN CERTIFICATE-----\n");
1207 if(!result) {
1208 size_t j = 0;
1209
1210 while(!result && (j < clen)) {
1211 size_t chunksize = (clen - j) > 64 ? 64 : (clen - j);
1212 result = Curl_dyn_addn(&out, &certptr[j], chunksize);
1213 if(!result)
1214 result = Curl_dyn_addn(&out, "\n", 1);
1215 j += chunksize;
1216 }
1217 if(!result)
1218 result = Curl_dyn_add(&out, "-----END CERTIFICATE-----\n");
1219 }
1220 free(certptr);
1221 if(!result)
1222 if(data->set.ssl.certinfo)
1223 result = ssl_push_certinfo_dyn(data, certnum, "Cert", &out);
1224
1225 done:
1226 Curl_dyn_free(&out);
1227 return result;
1228 }
1229
1230 #endif /* WANT_EXTRACT_CERTINFO */
1231
1232 #endif /* USE_GNUTLS or USE_WOLFSSL or USE_SCHANNEL or USE_SECTRANSP */
1233