Artifactspublic-v0

curl

master
ply4 git calls227 ms296 KiB2 cachedprotocol v2
1277 lines35 KiBRaw
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#include "curl_setup.h"
26#if defined(USE_GNUTLS) || defined(USE_WOLFSSL) || defined(USE_SCHANNEL) || \
27 defined(USE_MBEDTLS) || defined(USE_RUSTLS)
29#if defined(USE_GNUTLS) || defined(USE_SCHANNEL) || defined(USE_MBEDTLS) || \
30 defined(USE_WOLFSSL) || defined(USE_RUSTLS)
31#define WANT_PARSEX509 /* uses Curl_parseX509() */
32#endif
34#if defined(USE_GNUTLS) || defined(USE_SCHANNEL) || defined(USE_MBEDTLS) || \
35 defined(USE_RUSTLS)
36#define WANT_EXTRACT_CERTINFO /* uses Curl_extract_certinfo() */
37#endif
39#include "urldata.h"
40#include "vtls/vtls.h"
41#include "curl_trc.h"
42#include "curlx/base64.h"
43#include "vtls/x509asn1.h"
44#include "curlx/dynbuf.h"
46/*
47 * Constants.
48 */
50/* Largest supported ASN.1 structure. */
51#define CURL_ASN1_MAX ((size_t)0x40000) /* 256K */
53#ifdef WANT_EXTRACT_CERTINFO
55/* ASN.1 OID table entry. */
56struct Curl_OID {
57 const char *numoid; /* Dotted-numeric OID. */
58 const char *textoid; /* OID name. */
59};
61/* ASN.1 OIDs. */
62static const struct Curl_OID OIDtable[] = {
63 { "1.2.840.10040.4.1", "dsa" },
64 { "1.2.840.10040.4.3", "dsa-with-sha1" },
65 { "1.2.840.10045.2.1", "ecPublicKey" },
66 { "1.2.840.10045.3.0.1", "c2pnb163v1" },
67 { "1.2.840.10045.4.1", "ecdsa-with-SHA1" },
68 { "1.2.840.10045.4.3.1", "ecdsa-with-SHA224" },
69 { "1.2.840.10045.4.3.2", "ecdsa-with-SHA256" },
70 { "1.2.840.10045.4.3.3", "ecdsa-with-SHA384" },
71 { "1.2.840.10045.4.3.4", "ecdsa-with-SHA512" },
72 { "1.2.840.10046.2.1", "dhpublicnumber" },
73 { "1.2.840.113549.1.1.1", "rsaEncryption" },
74 { "1.2.840.113549.1.1.2", "md2WithRSAEncryption" },
75 { "1.2.840.113549.1.1.4", "md5WithRSAEncryption" },
76 { "1.2.840.113549.1.1.5", "sha1WithRSAEncryption" },
77 { "1.2.840.113549.1.1.10", "RSASSA-PSS" },
78 { "1.2.840.113549.1.1.14", "sha224WithRSAEncryption" },
79 { "1.2.840.113549.1.1.11", "sha256WithRSAEncryption" },
80 { "1.2.840.113549.1.1.12", "sha384WithRSAEncryption" },
81 { "1.2.840.113549.1.1.13", "sha512WithRSAEncryption" },
82 { "1.2.840.113549.2.2", "md2" },
83 { "1.2.840.113549.2.5", "md5" },
84 { "1.3.14.3.2.26", "sha1" },
85 { "2.5.4.3", "CN" },
86 { "2.5.4.4", "SN" },
87 { "2.5.4.5", "serialNumber" },
88 { "2.5.4.6", "C" },
89 { "2.5.4.7", "L" },
90 { "2.5.4.8", "ST" },
91 { "2.5.4.9", "streetAddress" },
92 { "2.5.4.10", "O" },
93 { "2.5.4.11", "OU" },
94 { "2.5.4.12", "title" },
95 { "2.5.4.13", "description" },
96 { "2.5.4.17", "postalCode" },
97 { "2.5.4.41", "name" },
98 { "2.5.4.42", "givenName" },
99 { "2.5.4.43", "initials" },
100 { "2.5.4.44", "generationQualifier" },
101 { "2.5.4.45", "X500UniqueIdentifier" },
102 { "2.5.4.46", "dnQualifier" },
103 { "2.5.4.65", "pseudonym" },
104 { "1.2.840.113549.1.9.1", "emailAddress" },
105 { "2.5.4.72", "role" },
106 { "2.5.29.17", "subjectAltName" },
107 { "2.5.29.18", "issuerAltName" },
108 { "2.5.29.19", "basicConstraints" },
109 { "2.16.840.1.101.3.4.2.4", "sha224" },
110 { "2.16.840.1.101.3.4.2.1", "sha256" },
111 { "2.16.840.1.101.3.4.2.2", "sha384" },
112 { "2.16.840.1.101.3.4.2.3", "sha512" },
113 { "1.2.840.113549.1.9.2", "unstructuredName" },
114 { NULL, NULL }
115};
117#endif /* WANT_EXTRACT_CERTINFO */
119/*
120 * Lightweight ASN.1 parser.
121 * In particular, it does not check for syntactic/lexical errors.
122 * It is intended to support certificate information gathering for SSL backends
123 * that offer a mean to get certificates as a whole, but do not supply
124 * entry points to get particular certificate sub-fields.
125 * Please note there is no pretension here to rewrite a full SSL library.
126 */
128#define CURL_ASN1_MAX_RECURSIONS 16
130static const uint8_t *getASN1Element_(struct Curl_asn1Element *elem,
131 const uint8_t *beg, const uint8_t *end,
132 size_t lvl)
134 uint8_t b;
135 size_t len;
136 struct Curl_asn1Element lelem;
138 /* Get a single ASN.1 element into `elem', parse ASN.1 string at `beg'
139 ending at `end'.
140 Returns a pointer in source string after the parsed element, or NULL
141 if an error occurs. */
142 if(!beg || !end || beg >= end || !*beg ||
143 ((size_t)(end - beg) > CURL_ASN1_MAX) ||
144 lvl++ >= CURL_ASN1_MAX_RECURSIONS)
145 return NULL;
147 /* Process header byte. */
148 elem->header = beg;
149 b = *beg++;
150 elem->constructed = (b & 0x20) != 0;
151 elem->eclass = (b >> 6) & 3;
152 b &= 0x1F;
153 if(b == 0x1F)
154 return NULL; /* Long tag values not supported here. */
155 elem->tag = b;
157 /* Process length. */
158 if(beg >= end)
159 return NULL;
160 b = *beg++;
161 if(!(b & 0x80))
162 len = b;
163 else {
164 b &= 0x7F;
165 if(!b) {
166 /* Unspecified length. Since we have all the data, we can determine the
167 effective length by skipping element until an EOC element is found. */
168 if(!elem->constructed)
169 return NULL;
170 elem->beg = beg;
171 while(beg < end && *beg) {
172 beg = getASN1Element_(&lelem, beg, end, lvl);
173 if(!beg)
174 return NULL;
175 }
176 if(end - beg < 2 || beg[1]) /* EOC must have a zero length. */
177 return NULL;
178 elem->end = beg; /* Do not include EOC as content. */
179 return beg + 2; /* After EOC element. */
180 }
181 else if(b > (size_t)(end - beg))
182 return NULL; /* Does not fit in source. */
183 else {
184 /* Get long length. */
185 len = 0;
186 do {
187 if(len & 0xFF000000L)
188 return NULL; /* Lengths > 32 bits are not supported. */
189 len = (len << 8) | *beg++;
190 } while(--b);
191 }
192 }
193 if(len > (size_t)(end - beg))
194 return NULL; /* Element data does not fit in source. */
195 elem->beg = beg;
196 elem->end = beg + len;
197 return elem->end;
200/*
201 * @unittest 1657
202 */
203UNITTEST const uint8_t *getASN1Element(struct Curl_asn1Element *elem,
204 const uint8_t *beg, const uint8_t *end);
205UNITTEST const uint8_t *getASN1Element(struct Curl_asn1Element *elem,
206 const uint8_t *beg, const uint8_t *end)
208 return getASN1Element_(elem, beg, end, 0);
211#ifdef WANT_EXTRACT_CERTINFO
213/*
214 * Search the null-terminated OID or OID identifier in local table.
215 * Return the table entry pointer or NULL if not found.
216 */
217static const struct Curl_OID *searchOID(const char *oid)
219 const struct Curl_OID *op;
220 for(op = OIDtable; op->numoid; op++)
221 if(!strcmp(op->numoid, oid) || curl_strequal(op->textoid, oid))
222 return op;
224 return NULL;
227/*
228 * Convert an ASN.1 Boolean value into its string representation.
229 *
230 * Return error code.
231 */
233static CURLcode bool2str(struct dynbuf *store,
234 const uint8_t *beg, const uint8_t *end)
236 uint8_t val;
237 if(end - beg != 1)
238 return CURLE_BAD_FUNCTION_ARGUMENT;
239 val = *beg;
240 if((val != 0xff) && (val != 0x00))
241 return CURLE_BAD_FUNCTION_ARGUMENT;
242 return curlx_dyn_add(store, val ? "TRUE" : "FALSE");
245/*
246 * Convert an ASN.1 octet string to a printable string.
247 *
248 * Return error code.
249 */
250static CURLcode octet2str(struct dynbuf *store,
251 const uint8_t *beg, const uint8_t *end)
253 CURLcode result = CURLE_OK;
255 while(!result && beg < end)
256 result = curlx_dyn_addf(store, "%02x:", *beg++);
258 return result;
261static CURLcode bit2str(struct dynbuf *store,
262 const uint8_t *beg, const uint8_t *end)
264 /* Convert an ASN.1 bit string to a printable string. */
266 if(beg < end) {
267 uint8_t unused_bits = *beg++;
268 if(unused_bits > 7)
269 return CURLE_BAD_FUNCTION_ARGUMENT;
270 }
271 else
272 return CURLE_BAD_FUNCTION_ARGUMENT;
273 return octet2str(store, beg, end);
276/*
277 * Convert an ASN.1 integer value into its string representation.
278 *
279 * Returns error.
280 */
281static CURLcode int2str(struct dynbuf *store,
282 const uint8_t *beg, const uint8_t *end)
284 uint32_t uval = 0;
285 int32_t sval = 0;
286 size_t n = end - beg;
287 size_t i;
288 const uint8_t *p = beg;
290 if(!n)
291 return CURLE_BAD_FUNCTION_ARGUMENT;
293 if(n > 4)
294 return octet2str(store, beg, end);
296 if(p[0] & 0x80)
297 uval = ~0U;
299 for(i = 0; i < n; i++)
300 uval = (uval << 8) | p[i];
302 sval = (int32_t)uval;
304 /* small values are easier to read as decimals */
305 if(sval > -10000 && sval < 10000)
306 return curlx_dyn_addf(store, "%d", sval);
308 /* represent larger values as hex */
309 return curlx_dyn_addf(store, "0x%x", uval);
312/*
313 * Convert from an ASN.1 typed string to UTF8.
314 *
315 * The result is stored in a dynbuf that is inited by the user of this
316 * function.
317 *
318 * Returns error.
319 */
320static CURLcode utf8asn1str(struct dynbuf *to, int type,
321 const uint8_t *from, const uint8_t *end)
323 size_t inlength = end - from;
324 int size = 1;
325 CURLcode result = CURLE_OK;
327 switch(type) {
328 case CURL_ASN1_BMP_STRING:
329 size = 2;
330 break;
331 case CURL_ASN1_UNIVERSAL_STRING:
332 size = 4;
333 break;
334 case CURL_ASN1_NUMERIC_STRING:
335 case CURL_ASN1_PRINTABLE_STRING:
336 case CURL_ASN1_TELETEX_STRING:
337 case CURL_ASN1_IA5_STRING:
338 case CURL_ASN1_VISIBLE_STRING:
339 case CURL_ASN1_UTF8_STRING:
340 break;
341 default:
342 return CURLE_BAD_FUNCTION_ARGUMENT; /* Conversion not supported. */
343 }
345 if(inlength % size)
346 /* Length inconsistent with character size. */
347 return CURLE_BAD_FUNCTION_ARGUMENT;
349 if(type == CURL_ASN1_UTF8_STRING) {
350 /* copy. */
351 if(inlength)
352 result = curlx_dyn_addn(to, from, inlength);
353 }
354 else {
355 while(!result && (from < end)) {
356 char buf[4]; /* decode buffer */
357 size_t charsize = 1;
358 unsigned int wc = 0;
360 switch(size) {
361 case 4:
362 wc = (wc << 8) | *from++;
363 wc = (wc << 8) | *from++;
364 FALLTHROUGH();
365 case 2:
366 wc = (wc << 8) | *from++;
367 FALLTHROUGH();
368 default: /* case 1: */
369 wc = (wc << 8) | *from++;
370 }
371 if(wc >= 0x00000080) {
372 if(wc >= 0x00000800) {
373 if(wc >= 0x00010000) {
374 if(wc >= 0x00200000) {
375 /* Invalid char. size for target encoding. */
376 return CURLE_WEIRD_SERVER_REPLY;
377 }
378 buf[3] = (char)(0x80 | (wc & 0x3F));
379 wc = (wc >> 6) | 0x00010000;
380 charsize++;
381 }
382 buf[2] = (char)(0x80 | (wc & 0x3F));
383 wc = (wc >> 6) | 0x00000800;
384 charsize++;
385 }
386 buf[1] = (char)(0x80 | (wc & 0x3F));
387 wc = (wc >> 6) | 0x000000C0;
388 charsize++;
389 }
390 buf[0] = (char)wc;
391 result = curlx_dyn_addn(to, buf, charsize);
392 }
393 }
394 return result;
397/*
398 * Convert an ASN.1 OID into its dotted string representation. Each field is
399 * limited to unsigned 32-bit values.
400 *
401 * A minor limitation is in the second number, which can be no larger than 32
402 * bits - 80 == 4294967215.
403 *
404 * Return error code.
405 *
406 * @unittest 1666
407 */
408UNITTEST CURLcode encodeOID(struct dynbuf *store,
409 const uint8_t *beg, const uint8_t *end);
410UNITTEST CURLcode encodeOID(struct dynbuf *store,
411 const uint8_t *beg, const uint8_t *end)
413 uint32_t x;
414 uint32_t y;
415 CURLcode result = CURLE_OK;
417 if(end <= beg)
418 return CURLE_BAD_FUNCTION_ARGUMENT;
420 /* Process the first two numbers. The initial digit cannot be larger than
421 2 */
422 y = *beg++;
423 if(y <= 80)
424 x = y / 40;
425 else
426 x = 2;
427 result = curlx_dyn_addf(store, "%u", x);
428 if(result)
429 return result;
431 if(y & 0x80) {
432 uint32_t t = (y & 0x7f); /* the second number */
433 if(!t)
434 /* reject leading 0x80 padding */
435 return CURLE_BAD_FUNCTION_ARGUMENT;
436 do {
437 if((t & 0xFE000000) || (beg >= end))
438 return CURLE_BAD_FUNCTION_ARGUMENT;
439 y = *beg++;
440 t = (t << 7) | (y & 0x7F);
441 } while(y & 0x80);
442 y = t;
443 }
445 result = curlx_dyn_addf(store, ".%u", y - (x * 40));
446 if(result)
447 return result;
449 /* Process the trailing numbers. */
450 while((beg < end) && !result) {
451 x = 0;
452 y = 0;
453 do {
454 /* 0x80 as the first value is invalid since it only adds padding */
455 if((y & 0x80) && !x)
456 return CURLE_BAD_FUNCTION_ARGUMENT;
457 else if((x & 0xFE000000) || (beg == end))
458 return CURLE_BAD_FUNCTION_ARGUMENT;
459 y = *beg++;
460 x = (x << 7) | (y & 0x7F);
461 } while(y & 0x80);
462 result = curlx_dyn_addf(store, ".%u", x);
463 }
464 return result;
467/*
468 * Convert an ASN.1 OID into its dotted or symbolic string representation.
469 *
470 * Return error code.
471 */
473static CURLcode OID2str(struct dynbuf *store,
474 const uint8_t *beg, const uint8_t *end)
476 CURLcode result = CURLE_OK;
477 if(beg < end) {
478 struct dynbuf buf;
479 curlx_dyn_init(&buf, CURL_X509_STR_MAX);
480 result = encodeOID(&buf, beg, end);
482 if(!result) {
483 const struct Curl_OID *op = searchOID(curlx_dyn_ptr(&buf));
484 if(op)
485 result = curlx_dyn_add(store, op->textoid);
486 else
487 result = curlx_dyn_add(store, curlx_dyn_ptr(&buf));
488 }
489 curlx_dyn_free(&buf);
490 }
491 return result;
494/*
495 * @unittest 1656
496 */
497UNITTEST CURLcode GTime2str(struct dynbuf *store,
498 const char *beg, const char *end);
499UNITTEST CURLcode GTime2str(struct dynbuf *store,
500 const char *beg, const char *end)
502 const char *tzp;
503 const char *fracp;
504 char sec1, sec2;
505 size_t fracl;
506 size_t tzl;
507 const char *sep = "";
509 /* Convert an ASN.1 Generalized time to a printable string.
510 Return the dynamically allocated string, or NULL if an error occurs. */
512 for(fracp = beg; fracp < end && ISDIGIT(*fracp); fracp++)
513 ;
515 /* Get seconds digits. */
516 sec1 = '0';
517 switch(fracp - beg - 12) {
518 case 0:
519 sec2 = '0';
520 break;
521 case 2:
522 sec1 = fracp[-2];
523 FALLTHROUGH();
524 case 1:
525 sec2 = fracp[-1];
526 break;
527 default:
528 return CURLE_BAD_FUNCTION_ARGUMENT;
529 }
531 /* timezone follows optional fractional seconds. */
532 tzp = fracp;
533 fracl = 0; /* no fractional seconds detected so far */
534 if(fracp < end && (*fracp == '.' || *fracp == ',')) {
535 /* Have fractional seconds, e.g. "[.,]\d+". How many? */
536 fracp++; /* should be a digit char or BAD ARGUMENT */
537 tzp = fracp;
538 while(tzp < end && ISDIGIT(*tzp))
539 tzp++;
540 if(tzp == fracp) /* never looped, no digit after [.,] */
541 return CURLE_BAD_FUNCTION_ARGUMENT;
542 fracl = tzp - fracp; /* number of fractional sec digits */
543 DEBUGASSERT(fracl > 0);
544 /* Strip trailing zeroes in fractional seconds.
545 * May reduce fracl to 0 if only '0's are present. */
546 while(fracl && fracp[fracl - 1] == '0')
547 fracl--;
548 }
550 /* Process timezone. */
551 if(tzp >= end) {
552 tzp = "";
553 tzl = 0;
554 }
555 else if(*tzp == 'Z') {
556 sep = " ";
557 tzp = "GMT";
558 tzl = 3;
559 }
560 else if((*tzp == '+') || (*tzp == '-')) {
561 sep = " UTC";
562 tzl = end - tzp;
563 }
564 else {
565 sep = " ";
566 tzl = end - tzp;
567 }
569 return curlx_dyn_addf(store,
570 "%.4s-%.2s-%.2s %.2s:%.2s:%c%c%s%.*s%s%.*s",
571 beg, beg + 4, beg + 6,
572 beg + 8, beg + 10, sec1, sec2,
573 fracl ? "." : "", (int)fracl, fracp,
574 sep, (int)tzl, tzp);
577/*
578 * Convert an ASN.1 UTC time to a printable string.
579 *
580 * Return error code.
581 */
582static CURLcode UTime2str(struct dynbuf *store,
583 const char *beg, const char *end)
585 const char *tzp;
586 size_t tzl;
587 const char *sec;
589 for(tzp = beg; tzp < end && *tzp >= '0' && *tzp <= '9'; tzp++)
590 ;
591 /* Get the seconds. */
592 sec = beg + 10;
593 switch(tzp - sec) {
594 case 0:
595 sec = "00";
596 FALLTHROUGH();
597 case 2:
598 break;
599 default:
600 return CURLE_BAD_FUNCTION_ARGUMENT;
601 }
603 /* Process timezone. */
604 if(tzp >= end)
605 return CURLE_BAD_FUNCTION_ARGUMENT;
606 if(*tzp == 'Z') {
607 tzp = "GMT";
608 end = tzp + 3;
609 }
611 tzl = end - tzp;
612 return curlx_dyn_addf(store, "%d%.2s-%.2s-%.2s %.2s:%.2s:%.2s %.*s",
613 20 - (*beg >= '5'), beg, beg + 2, beg + 4,
614 beg + 6, beg + 8, sec,
615 (int)tzl, tzp);
618/*
619 * Convert an ASN.1 element to a printable string.
620 *
621 * Return error
622 *
623 * @unittest 1667
624 */
625UNITTEST CURLcode ASN1tostr(struct dynbuf *store,
626 struct Curl_asn1Element *elem);
627UNITTEST CURLcode ASN1tostr(struct dynbuf *store,
628 struct Curl_asn1Element *elem)
630 CURLcode result = CURLE_BAD_FUNCTION_ARGUMENT;
631 int type = elem->tag;
632 if(elem->constructed)
633 return result; /* No conversion of structured elements. */
635 switch(type) {
636 case CURL_ASN1_BOOLEAN:
637 result = bool2str(store, elem->beg, elem->end);
638 break;
639 case CURL_ASN1_INTEGER:
640 case CURL_ASN1_ENUMERATED:
641 result = int2str(store, elem->beg, elem->end);
642 break;
643 case CURL_ASN1_BIT_STRING:
644 result = bit2str(store, elem->beg, elem->end);
645 break;
646 case CURL_ASN1_OCTET_STRING:
647 result = octet2str(store, elem->beg, elem->end);
648 break;
649 case CURL_ASN1_NULL:
650 result = curlx_dyn_addn(store, "", 1);
651 break;
652 case CURL_ASN1_OBJECT_IDENTIFIER:
653 result = OID2str(store, elem->beg, elem->end);
654 break;
655 case CURL_ASN1_UTC_TIME:
656 result = UTime2str(store, (const char *)elem->beg,
657 (const char *)elem->end);
658 break;
659 case CURL_ASN1_GENERALIZED_TIME:
660 result = GTime2str(store, (const char *)elem->beg,
661 (const char *)elem->end);
662 break;
663 case CURL_ASN1_UTF8_STRING:
664 case CURL_ASN1_NUMERIC_STRING:
665 case CURL_ASN1_PRINTABLE_STRING:
666 case CURL_ASN1_TELETEX_STRING:
667 case CURL_ASN1_IA5_STRING:
668 case CURL_ASN1_VISIBLE_STRING:
669 case CURL_ASN1_UNIVERSAL_STRING:
670 case CURL_ASN1_BMP_STRING:
671 result = utf8asn1str(store, type, elem->beg, elem->end);
672 break;
673 }
675 return result;
678/*
679 * ASCII encode distinguished name at `dn' into the store dynbuf.
680 *
681 * Returns error.
682 */
683static CURLcode encodeDN(struct dynbuf *store, struct Curl_asn1Element *dn)
685 struct Curl_asn1Element rdn;
686 struct Curl_asn1Element atv;
687 struct Curl_asn1Element oid;
688 struct Curl_asn1Element value;
689 const uint8_t *p1;
690 const uint8_t *p2;
691 const uint8_t *p3;
692 const uint8_t *str;
693 CURLcode result = CURLE_OK;
694 bool added = FALSE;
695 struct dynbuf temp;
696 curlx_dyn_init(&temp, CURL_X509_STR_MAX);
698 for(p1 = dn->beg; p1 < dn->end;) {
699 p1 = getASN1Element(&rdn, p1, dn->end);
700 if(!p1) {
701 result = CURLE_BAD_FUNCTION_ARGUMENT;
702 goto error;
703 }
704 for(p2 = rdn.beg; p2 < rdn.end;) {
705 p2 = getASN1Element(&atv, p2, rdn.end);
706 if(!p2) {
707 result = CURLE_BAD_FUNCTION_ARGUMENT;
708 goto error;
709 }
710 p3 = getASN1Element(&oid, atv.beg, atv.end);
711 if(!p3) {
712 result = CURLE_BAD_FUNCTION_ARGUMENT;
713 goto error;
714 }
715 if(!getASN1Element(&value, p3, atv.end)) {
716 result = CURLE_BAD_FUNCTION_ARGUMENT;
717 goto error;
718 }
719 curlx_dyn_reset(&temp);
720 result = ASN1tostr(&temp, &oid);
721 if(result)
722 goto error;
724 str = curlx_dyn_uptr(&temp);
726 if(!str) {
727 result = CURLE_BAD_FUNCTION_ARGUMENT;
728 goto error;
729 }
731 /* Encode delimiter.
732 If attribute has a short uppercase name, delimiter is ", ". */
733 for(p3 = str; ISUPPER(*p3); p3++)
734 ;
735 if(added) {
736 if(p3 - str > 2)
737 result = curlx_dyn_addn(store, "/", 1);
738 else
739 result = curlx_dyn_addn(store, ", ", 2);
740 if(result)
741 goto error;
742 }
744 /* Encode attribute name. */
745 result = curlx_dyn_add(store, (const char *)str);
746 if(result)
747 goto error;
749 /* Generate equal sign. */
750 result = curlx_dyn_addn(store, "=", 1);
751 if(result)
752 goto error;
754 /* Generate value. */
755 result = ASN1tostr(store, &value);
756 if(result)
757 goto error;
758 curlx_dyn_reset(&temp);
759 added = TRUE; /* use separator for next */
760 }
761 }
762error:
763 curlx_dyn_free(&temp);
765 return result;
768#endif /* WANT_EXTRACT_CERTINFO */
770#ifdef WANT_PARSEX509
771/*
772 * ASN.1 parse an X509 certificate into structure subfields.
773 * Syntax is assumed to have already been checked by the SSL backend.
774 * See RFC 5280.
775 */
776int Curl_parseX509(struct Curl_X509certificate *cert,
777 const uint8_t *beg, const uint8_t *end)
779 struct Curl_asn1Element elem;
780 struct Curl_asn1Element tbsCertificate;
781 const uint8_t *ccp;
782 static const uint8_t defaultVersion = 0; /* v1. */
784 memset(cert, 0, sizeof(*cert));
785 cert->certificate.header = NULL;
786 cert->certificate.beg = beg;
787 cert->certificate.end = end;
789 /* Get the sequence content. */
790 if(!getASN1Element(&elem, beg, end))
791 return -1; /* Invalid bounds/size. */
792 beg = elem.beg;
793 end = elem.end;
795 /* Get tbsCertificate. */
796 beg = getASN1Element(&tbsCertificate, beg, end);
797 if(!beg)
798 return -1;
799 /* Skip the signatureAlgorithm. */
800 beg = getASN1Element(&cert->signatureAlgorithm, beg, end);
801 if(!beg)
802 return -1;
803 /* Get the signatureValue. */
804 if(!getASN1Element(&cert->signature, beg, end))
805 return -1;
807 /* Parse TBSCertificate. */
808 beg = tbsCertificate.beg;
809 end = tbsCertificate.end;
810 /* Get optional version, get serialNumber. */
811 cert->version.header = NULL;
812 cert->version.beg = &defaultVersion;
813 cert->version.end = &defaultVersion + sizeof(defaultVersion);
814 beg = getASN1Element(&elem, beg, end);
815 if(!beg)
816 return -1;
817 if(elem.tag == 0) {
818 if(!getASN1Element(&cert->version, elem.beg, elem.end))
819 return -1;
820 beg = getASN1Element(&elem, beg, end);
821 if(!beg)
822 return -1;
823 }
824 cert->serialNumber = elem;
825 /* Get signature algorithm. */
826 beg = getASN1Element(&cert->signatureAlgorithm, beg, end);
827 /* Get issuer. */
828 beg = getASN1Element(&cert->issuer, beg, end);
829 if(!beg)
830 return -1;
831 /* Get notBefore and notAfter. */
832 beg = getASN1Element(&elem, beg, end);
833 if(!beg)
834 return -1;
835 ccp = getASN1Element(&cert->notBefore, elem.beg, elem.end);
836 if(!ccp)
837 return -1;
838 if(!getASN1Element(&cert->notAfter, ccp, elem.end))
839 return -1;
840 /* These two fields MUST consume all of the validity elem */
841 if(elem.end != cert->notAfter.end)
842 return -1;
843 /* Get subject. */
844 beg = getASN1Element(&cert->subject, beg, end);
845 if(!beg)
846 return -1;
847 /* Get subjectPublicKeyAlgorithm and subjectPublicKey. */
848 beg = getASN1Element(&cert->subjectPublicKeyInfo, beg, end);
849 if(!beg)
850 return -1;
851 ccp = getASN1Element(&cert->subjectPublicKeyAlgorithm,
852 cert->subjectPublicKeyInfo.beg,
853 cert->subjectPublicKeyInfo.end);
854 if(!ccp)
855 return -1;
856 if(!getASN1Element(&cert->subjectPublicKey, ccp,
857 cert->subjectPublicKeyInfo.end))
858 return -1;
859 /* Get optional issuerUniqueID, subjectUniqueID and extensions. */
860 cert->issuerUniqueID.tag = cert->subjectUniqueID.tag = 0;
861 cert->extensions.tag = elem.tag = 0;
862 cert->issuerUniqueID.header = cert->subjectUniqueID.header = NULL;
863 cert->issuerUniqueID.beg = cert->issuerUniqueID.end = beg;
864 cert->subjectUniqueID.beg = cert->subjectUniqueID.end = beg;
865 cert->extensions.header = NULL;
866 cert->extensions.beg = cert->extensions.end = beg;
867 if(beg < end) {
868 beg = getASN1Element(&elem, beg, end);
869 if(!beg)
870 return -1;
871 }
872 if(elem.tag == 1) {
873 cert->issuerUniqueID = elem;
874 if(beg < end) {
875 beg = getASN1Element(&elem, beg, end);
876 if(!beg)
877 return -1;
878 }
879 }
880 if(elem.tag == 2) {
881 cert->subjectUniqueID = elem;
882 if(beg < end) {
883 beg = getASN1Element(&elem, beg, end);
884 if(!beg)
885 return -1;
886 }
887 }
888 if(elem.tag == 3)
889 if(!getASN1Element(&cert->extensions, elem.beg, elem.end))
890 return -1;
891 return 0;
894#endif /* WANT_PARSEX509 */
896#ifdef WANT_EXTRACT_CERTINFO
898static CURLcode dumpAlgo(struct dynbuf *store,
899 struct Curl_asn1Element *param,
900 const uint8_t *beg, const uint8_t *end)
902 struct Curl_asn1Element oid;
904 /* Get algorithm parameters and return algorithm name. */
906 beg = getASN1Element(&oid, beg, end);
907 if(!beg)
908 return CURLE_BAD_FUNCTION_ARGUMENT;
909 param->header = NULL;
910 param->tag = 0;
911 param->beg = param->end = end;
912 if(beg < end) {
913 const uint8_t *p = getASN1Element(param, beg, end);
914 if(!p)
915 return CURLE_BAD_FUNCTION_ARGUMENT;
916 }
917 return OID2str(store, oid.beg, oid.end);
920/*
921 * This is a convenience function for push_certinfo_len that takes a zero
922 * terminated value.
923 */
924static CURLcode ssl_push_certinfo(struct Curl_easy *data,
925 int certnum,
926 const char *label,
927 const char *value)
929 size_t valuelen = strlen(value);
931 return Curl_ssl_push_certinfo_len(data, certnum, label, value, valuelen);
934/*
935 * This is a convenience function for push_certinfo_len that takes a
936 * dynbuf value.
937 *
938 * It also does the verbose output if !certnum.
939 */
940static CURLcode ssl_push_certinfo_dyn(struct Curl_easy *data,
941 int certnum,
942 const char *label,
943 struct dynbuf *ptr)
945 size_t valuelen = curlx_dyn_len(ptr);
946 const char *value = curlx_dyn_ptr(ptr);
948 CURLcode result = Curl_ssl_push_certinfo_len(data, certnum, label,
949 value, valuelen);
951 if(!certnum && !result)
952 infof(data, " %s: %s", label, value);
954 return result;
957static CURLcode do_pubkey_field(struct Curl_easy *data, int certnum,
958 const char *label,
959 struct Curl_asn1Element *elem)
961 CURLcode result;
962 struct dynbuf out;
964 curlx_dyn_init(&out, CURL_X509_STR_MAX);
966 /* Generate a certificate information record for the public key. */
968 result = ASN1tostr(&out, elem);
969 if(!result && data->set.ssl.certinfo)
970 result = ssl_push_certinfo_dyn(data, certnum, label, &out);
971 curlx_dyn_free(&out);
972 return result;
975/* return 0 on success, 1 on error */
976static int do_pubkey(struct Curl_easy *data, int certnum, const char *algo,
977 struct Curl_asn1Element *param,
978 struct Curl_asn1Element *pubkey)
980 struct Curl_asn1Element elem;
981 struct Curl_asn1Element pk;
982 const uint8_t *p;
984 /* Generate all information records for the public key. */
986 if(curl_strequal(algo, "ecPublicKey")) {
987 /*
988 * ECC public key is all the data, a value of type BIT STRING mapped to
989 * OCTET STRING and should not be parsed as an ASN.1 value.
990 */
991 const size_t dlen = pubkey->end - pubkey->beg;
992 size_t len;
993 if(dlen < 2)
994 /* too small */
995 return 1;
996 len = (dlen - 2) * 4;
997 if(!certnum)
998 infof(data, " ECC Public Key (%zu bits)", len);
999 if(data->set.ssl.certinfo) {
1000 char q[(sizeof(len) * 8 / 3) + 1];
1001 (void)curl_msnprintf(q, sizeof(q), "%zu", len);
1002 if(ssl_push_certinfo(data, certnum, "ECC Public Key", q))
1003 return 1;
1005 return do_pubkey_field(data, certnum, "ecPublicKey", pubkey) == CURLE_OK
1006 ? 0 : 1;
1009 /* Get the public key (single element). */
1010 if(!getASN1Element(&pk, pubkey->beg + 1, pubkey->end))
1011 return 1;
1013 if(curl_strequal(algo, "rsaEncryption")) {
1014 const uint8_t *q;
1015 size_t len;
1017 p = getASN1Element(&elem, pk.beg, pk.end);
1018 if(!p)
1019 return 1;
1021 /* Compute key length. */
1022 for(q = elem.beg; q < elem.end && !*q; q++)
1024 len = ((elem.end - q) * 8);
1025 if(len) {
1026 unsigned int i;
1027 for(i = *q; !(i & 0x80); i <<= 1)
1028 len--;
1030 if(len > 32)
1031 elem.beg = q; /* Strip leading zero bytes. */
1032 if(!certnum)
1033 infof(data, " RSA Public Key (%zu bits)", len);
1034 if(data->set.ssl.certinfo) {
1035 char r[(sizeof(len) * 8 / 3) + 1];
1036 curl_msnprintf(r, sizeof(r), "%zu", len);
1037 if(ssl_push_certinfo(data, certnum, "RSA Public Key", r))
1038 return 1;
1040 /* Generate coefficients. */
1041 if(do_pubkey_field(data, certnum, "rsa(n)", &elem))
1042 return 1;
1043 if(!getASN1Element(&elem, p, pk.end))
1044 return 1;
1045 if(do_pubkey_field(data, certnum, "rsa(e)", &elem))
1046 return 1;
1048 else if(curl_strequal(algo, "dsa")) {
1049 p = getASN1Element(&elem, param->beg, param->end);
1050 if(p) {
1051 if(do_pubkey_field(data, certnum, "dsa(p)", &elem))
1052 return 1;
1053 p = getASN1Element(&elem, p, param->end);
1054 if(p) {
1055 if(do_pubkey_field(data, certnum, "dsa(q)", &elem))
1056 return 1;
1057 if(getASN1Element(&elem, p, param->end)) {
1058 if(do_pubkey_field(data, certnum, "dsa(g)", &elem))
1059 return 1;
1060 if(do_pubkey_field(data, certnum, "dsa(pub_key)", &pk))
1061 return 1;
1066 else if(curl_strequal(algo, "dhpublicnumber")) {
1067 p = getASN1Element(&elem, param->beg, param->end);
1068 if(p) {
1069 if(do_pubkey_field(data, certnum, "dh(p)", &elem))
1070 return 1;
1071 if(getASN1Element(&elem, p, param->end)) {
1072 if(do_pubkey_field(data, certnum, "dh(g)", &elem))
1073 return 1;
1074 if(do_pubkey_field(data, certnum, "dh(pub_key)", &pk))
1075 return 1;
1079 return 0;
1083 * Convert an ASN.1 distinguished name into a printable string.
1084 * Return error.
1085 */
1086static CURLcode DNtostr(struct dynbuf *store, struct Curl_asn1Element *dn)
1088 return encodeDN(store, dn);
1091CURLcode Curl_extract_certinfo(struct Curl_easy *data,
1092 int certnum,
1093 const uint8_t *beg,
1094 const uint8_t *end)
1096 struct Curl_X509certificate cert;
1097 struct Curl_asn1Element param;
1098 char *certptr;
1099 size_t clen;
1100 struct dynbuf out;
1101 CURLcode result = CURLE_OK;
1102 unsigned int version;
1103 const uint8_t *ptr;
1104 int rc;
1106 if(!data->set.ssl.certinfo)
1107 if(certnum)
1108 return CURLE_OK;
1110 curlx_dyn_init(&out, CURL_X509_STR_MAX);
1111 /* Prepare the certificate information for curl_easy_getinfo(). */
1113 /* Extract the certificate ASN.1 elements. */
1114 if(Curl_parseX509(&cert, beg, end))
1115 return CURLE_PEER_FAILED_VERIFICATION;
1117 /* Subject. */
1118 result = DNtostr(&out, &cert.subject);
1119 if(result)
1120 goto done;
1121 if(data->set.ssl.certinfo) {
1122 result = ssl_push_certinfo_dyn(data, certnum, "Subject", &out);
1123 if(result)
1124 goto done;
1126 curlx_dyn_reset(&out);
1128 /* Issuer. */
1129 result = DNtostr(&out, &cert.issuer);
1130 if(result)
1131 goto done;
1132 if(data->set.ssl.certinfo) {
1133 result = ssl_push_certinfo_dyn(data, certnum, "Issuer", &out);
1134 if(result)
1135 goto done;
1137 curlx_dyn_reset(&out);
1139 /* Version (always fits in less than 32 bits). */
1140 version = 0;
1141 for(ptr = cert.version.beg; ptr < cert.version.end; ptr++)
1142 version = (version << 8) | *ptr;
1143 if(data->set.ssl.certinfo) {
1144 result = curlx_dyn_addf(&out, "%x", version);
1145 if(result)
1146 goto done;
1147 result = ssl_push_certinfo_dyn(data, certnum, "Version", &out);
1148 if(result)
1149 goto done;
1150 curlx_dyn_reset(&out);
1153 /* Serial number. */
1154 result = ASN1tostr(&out, &cert.serialNumber);
1155 if(result)
1156 goto done;
1157 if(data->set.ssl.certinfo) {
1158 result = ssl_push_certinfo_dyn(data, certnum, "Serial Number", &out);
1159 if(result)
1160 goto done;
1162 curlx_dyn_reset(&out);
1164 /* Signature algorithm .*/
1165 result = dumpAlgo(&out, &param, cert.signatureAlgorithm.beg,
1166 cert.signatureAlgorithm.end);
1167 if(result)
1168 goto done;
1169 if(data->set.ssl.certinfo) {
1170 result = ssl_push_certinfo_dyn(data, certnum, "Signature Algorithm", &out);
1171 if(result)
1172 goto done;
1174 curlx_dyn_reset(&out);
1176 /* Start Date. */
1177 result = ASN1tostr(&out, &cert.notBefore);
1178 if(result)
1179 goto done;
1180 if(data->set.ssl.certinfo) {
1181 result = ssl_push_certinfo_dyn(data, certnum, "Start Date", &out);
1182 if(result)
1183 goto done;
1185 curlx_dyn_reset(&out);
1187 /* Expire Date. */
1188 result = ASN1tostr(&out, &cert.notAfter);
1189 if(result)
1190 goto done;
1191 if(data->set.ssl.certinfo) {
1192 result = ssl_push_certinfo_dyn(data, certnum, "Expire Date", &out);
1193 if(result)
1194 goto done;
1196 curlx_dyn_reset(&out);
1198 /* Public Key Algorithm. */
1199 result = dumpAlgo(&out, &param, cert.subjectPublicKeyAlgorithm.beg,
1200 cert.subjectPublicKeyAlgorithm.end);
1201 if(result)
1202 goto done;
1203 if(data->set.ssl.certinfo) {
1204 result = ssl_push_certinfo_dyn(data, certnum, "Public Key Algorithm",
1205 &out);
1206 if(result)
1207 goto done;
1210 rc = do_pubkey(data, certnum, curlx_dyn_ptr(&out),
1211 &param, &cert.subjectPublicKey);
1212 if(rc) {
1213 result = CURLE_OUT_OF_MEMORY; /* the most likely error */
1214 goto done;
1216 curlx_dyn_reset(&out);
1218 /* Signature. */
1219 result = ASN1tostr(&out, &cert.signature);
1220 if(result)
1221 goto done;
1222 if(data->set.ssl.certinfo) {
1223 result = ssl_push_certinfo_dyn(data, certnum, "Signature", &out);
1224 if(result)
1225 goto done;
1227 curlx_dyn_reset(&out);
1229 /* Generate PEM certificate. */
1230 result = curlx_base64_encode((const uint8_t *)cert.certificate.beg,
1231 cert.certificate.end - cert.certificate.beg,
1232 &certptr, &clen);
1233 if(result)
1234 goto done;
1236 /* Generate the final output certificate string. Format is:
1237 -----BEGIN CERTIFICATE-----\n
1238 <max 64 base64 characters>\n
1242 -----END CERTIFICATE-----\n
1243 */
1245 curlx_dyn_reset(&out);
1247 /* Build the certificate string. */
1248 result = curlx_dyn_add(&out, "-----BEGIN CERTIFICATE-----\n");
1249 if(!result) {
1250 size_t j = 0;
1252 while(!result && (j < clen)) {
1253 size_t chunksize = (clen - j) > 64 ? 64 : (clen - j);
1254 result = curlx_dyn_addn(&out, &certptr[j], chunksize);
1255 if(!result)
1256 result = curlx_dyn_addn(&out, "\n", 1);
1257 j += chunksize;
1259 if(!result)
1260 result = curlx_dyn_add(&out, "-----END CERTIFICATE-----\n");
1262 curlx_free(certptr);
1263 if(!result)
1264 if(data->set.ssl.certinfo)
1265 result = ssl_push_certinfo_dyn(data, certnum, "Cert", &out);
1267done:
1268 if(result)
1269 failf(data, "Failed extracting certificate chain");
1270 curlx_dyn_free(&out);
1271 return result;
1274#endif /* WANT_EXTRACT_CERTINFO */
1276#endif /* USE_GNUTLS || USE_WOLFSSL || USE_SCHANNEL || USE_MBEDTLS ||
1277 USE_RUSTLS */
Diagnostics4 git calls · 227 ms
CallStatusSizeTime0227 ms
discover200152 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=152
git.server.agent=gitty/1.0
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch object-format
git.server.fetch=shallow filter sideband-all
git.protocol.version=2
git.pkt.frames=8
git.pkt.data=6
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=152
git.pkt.payload.max=34
ls-refs200149 B · hit2 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=149
git.response.cache=HIT
git.response.duration_ms=2
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=149
git.pkt.payload.max=78
git.ls_refs.count=2
git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=293470
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=blob:none
git.fetch.filter.requested=tree:0
git.fetch.filter.fallback=blob:none
git.fetch.filter.learned=false
git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=10
git.pkt.data=8
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=293470
git.pkt.payload.max=65516
git.sideband.band1.bytes=293354
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=87
git.pack.bytes=293354
git.pack.ofs_delta=0
git.pack.ref_delta=1
git.pack.chain.max=1
git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0
git.pack.full=86
Open pack map
fetch2009.5 KiB222 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=222
git.response.bytes=9691
git.response.cache=MISS
git.response.duration_ms=222
git.fetch.wants=ddb98b8616ef7970113290e7ff2a664976b104ec
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=9691
git.pkt.payload.max=9670
git.sideband.band1.bytes=9669
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
Ref
refs/heads/master
Commit
31c911efe5652725e930f4d2e60c9060e5f6646d
cf-ray
a5e393592ea35751
Request
07d93fe5…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format