1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
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.haxx.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 #include "curl_setup.h"
25 #if defined(USE_GSKIT) || defined(USE_NSS) || defined(USE_GNUTLS) || \
26 defined(USE_WOLFSSL) || defined(USE_SCHANNEL)
28 #include <curl/curl.h>
31 #include "hostcheck.h"
32 #include "vtls/vtls.h"
34 #include "inet_pton.h"
35 #include "curl_base64.h"
38 /* The last 3 #include files should be in this order */
39 #include "curl_printf.h"
40 #include "curl_memory.h"
44 static const char cnOID[] = "2.5.4.3"; /* Common name. */
45 static const char sanOID[] = "2.5.29.17"; /* Subject alternative name. */
47 static const struct Curl_OID OIDtable[] = {
48 { "1.2.840.10040.4.1", "dsa" },
49 { "1.2.840.10040.4.3", "dsa-with-sha1" },
50 { "1.2.840.10045.2.1", "ecPublicKey" },
51 { "1.2.840.10045.3.0.1", "c2pnb163v1" },
52 { "1.2.840.10045.4.1", "ecdsa-with-SHA1" },
53 { "1.2.840.10046.2.1", "dhpublicnumber" },
54 { "1.2.840.113549.1.1.1", "rsaEncryption" },
55 { "1.2.840.113549.1.1.2", "md2WithRSAEncryption" },
56 { "1.2.840.113549.1.1.4", "md5WithRSAEncryption" },
57 { "1.2.840.113549.1.1.5", "sha1WithRSAEncryption" },
58 { "1.2.840.113549.1.1.10", "RSASSA-PSS" },
59 { "1.2.840.113549.1.1.14", "sha224WithRSAEncryption" },
60 { "1.2.840.113549.1.1.11", "sha256WithRSAEncryption" },
61 { "1.2.840.113549.1.1.12", "sha384WithRSAEncryption" },
62 { "1.2.840.113549.1.1.13", "sha512WithRSAEncryption" },
63 { "1.2.840.113549.2.2", "md2" },
64 { "1.2.840.113549.2.5", "md5" },
65 { "1.3.14.3.2.26", "sha1" },
68 { "2.5.4.5", "serialNumber" },
72 { "2.5.4.9", "streetAddress" },
75 { "2.5.4.12", "title" },
76 { "2.5.4.13", "description" },
77 { "2.5.4.17", "postalCode" },
78 { "2.5.4.41", "name" },
79 { "2.5.4.42", "givenName" },
80 { "2.5.4.43", "initials" },
81 { "2.5.4.44", "generationQualifier" },
82 { "2.5.4.45", "X500UniqueIdentifier" },
83 { "2.5.4.46", "dnQualifier" },
84 { "2.5.4.65", "pseudonym" },
85 { "1.2.840.113549.1.9.1", "emailAddress" },
86 { "2.5.4.72", "role" },
87 { sanOID, "subjectAltName" },
88 { "2.5.29.18", "issuerAltName" },
89 { "2.5.29.19", "basicConstraints" },
90 { "2.16.840.1.101.3.4.2.4", "sha224" },
91 { "2.16.840.1.101.3.4.2.1", "sha256" },
92 { "2.16.840.1.101.3.4.2.2", "sha384" },
93 { "2.16.840.1.101.3.4.2.3", "sha512" },
94 { (const char *) NULL, (const char *) NULL }
98 * Lightweight ASN.1 parser.
99 * In particular, it does not check for syntactic/lexical errors.
100 * It is intended to support certificate information gathering for SSL backends
101 * that offer a mean to get certificates as a whole, but do not supply
102 * entry points to get particular certificate sub-fields.
103 * Please note there is no pretention here to rewrite a full SSL library.
106 static const char *getASN1Element(struct Curl_asn1Element *elem,
107 const char *beg, const char *end)
110 static const char *getASN1Element(struct Curl_asn1Element *elem,
111 const char *beg, const char *end)
115 struct Curl_asn1Element lelem;
117 /* Get a single ASN.1 element into `elem', parse ASN.1 string at `beg'
119 Returns a pointer in source string after the parsed element, or NULL
120 if an error occurs. */
121 if(!beg || !end || beg >= end || !*beg ||
122 (size_t)(end - beg) > CURL_ASN1_MAX)
125 /* Process header byte. */
127 b = (unsigned char) *beg++;
128 elem->constructed = (b & 0x20) != 0;
129 elem->class = (b >> 6) & 3;
132 return NULL; /* Long tag values not supported here. */
135 /* Process length. */
138 b = (unsigned char) *beg++;
141 else if(!(b &= 0x7F)) {
142 /* Unspecified length. Since we have all the data, we can determine the
143 effective length by skipping element until an end element is found. */
144 if(!elem->constructed)
147 while(beg < end && *beg) {
148 beg = getASN1Element(&lelem, beg, end);
157 else if((unsigned)b > (size_t)(end - beg))
158 return NULL; /* Does not fit in source. */
160 /* Get long length. */
163 if(len & 0xFF000000L)
164 return NULL; /* Lengths > 32 bits are not supported. */
165 len = (len << 8) | (unsigned char) *beg++;
168 if(len > (size_t)(end - beg))
169 return NULL; /* Element data does not fit in source. */
171 elem->end = beg + len;
176 * Search the null terminated OID or OID identifier in local table.
177 * Return the table entry pointer or NULL if not found.
179 static const struct Curl_OID *searchOID(const char *oid)
181 const struct Curl_OID *op;
182 for(op = OIDtable; op->numoid; op++)
183 if(!strcmp(op->numoid, oid) || strcasecompare(op->textoid, oid))
190 * Convert an ASN.1 Boolean value into its string representation. Return the
191 * dynamically allocated string, or NULL if source is not an ASN.1 Boolean
195 static const char *bool2str(const char *beg, const char *end)
199 return strdup(*beg? "TRUE": "FALSE");
203 * Convert an ASN.1 octet string to a printable string.
204 * Return the dynamically allocated string, or NULL if an error occurs.
206 static const char *octet2str(const char *beg, const char *end)
208 size_t n = end - beg;
211 if(n <= (SIZE_T_MAX - 1) / 3) {
212 buf = malloc(3 * n + 1);
214 for(n = 0; beg < end; n += 3)
215 msnprintf(buf + n, 4, "%02x:", *(const unsigned char *) beg++);
220 static const char *bit2str(const char *beg, const char *end)
222 /* Convert an ASN.1 bit string to a printable string.
223 Return the dynamically allocated string, or NULL if an error occurs. */
227 return octet2str(beg, end);
231 * Convert an ASN.1 integer value into its string representation.
232 * Return the dynamically allocated string, or NULL if source is not an
233 * ASN.1 integer value.
235 static const char *int2str(const char *beg, const char *end)
237 unsigned long val = 0;
238 size_t n = end - beg;
244 return octet2str(beg, end);
246 /* Represent integers <= 32-bit as a single value. */
251 val = (val << 8) | *(const unsigned char *) beg++;
253 return curl_maprintf("%s%lx", val >= 10? "0x": "", val);
257 * Perform a lazy conversion from an ASN.1 typed string to UTF8. Allocate the
258 * destination buffer dynamically. The allocation size will normally be too
259 * large: this is to avoid buffer overflows.
260 * Terminate the string with a nul byte and return the converted
264 utf8asn1str(char **to, int type, const char *from, const char *end)
266 size_t inlength = end - from;
273 case CURL_ASN1_BMP_STRING:
276 case CURL_ASN1_UNIVERSAL_STRING:
279 case CURL_ASN1_NUMERIC_STRING:
280 case CURL_ASN1_PRINTABLE_STRING:
281 case CURL_ASN1_TELETEX_STRING:
282 case CURL_ASN1_IA5_STRING:
283 case CURL_ASN1_VISIBLE_STRING:
284 case CURL_ASN1_UTF8_STRING:
287 return -1; /* Conversion not supported. */
291 return -1; /* Length inconsistent with character size. */
292 if(inlength / size > (SIZE_T_MAX - 1) / 4)
293 return -1; /* Too big. */
294 buf = malloc(4 * (inlength / size) + 1);
296 return -1; /* Not enough memory. */
298 if(type == CURL_ASN1_UTF8_STRING) {
300 outlength = inlength;
302 memcpy(buf, from, outlength);
305 for(outlength = 0; from < end;) {
312 wc = (wc << 8) | *(const unsigned char *) from++;
313 wc = (wc << 8) | *(const unsigned char *) from++;
316 wc = (wc << 8) | *(const unsigned char *) from++;
318 default: /* case 1: */
319 wc = (wc << 8) | *(const unsigned char *) from++;
322 if(wc >= 0x00000080) {
323 if(wc >= 0x00000800) {
324 if(wc >= 0x00010000) {
325 if(wc >= 0x00200000) {
327 return -1; /* Invalid char. size for target encoding. */
329 buf[outlength + 3] = (char) (0x80 | (wc & 0x3F));
330 wc = (wc >> 6) | 0x00010000;
333 buf[outlength + 2] = (char) (0x80 | (wc & 0x3F));
334 wc = (wc >> 6) | 0x00000800;
337 buf[outlength + 1] = (char) (0x80 | (wc & 0x3F));
338 wc = (wc >> 6) | 0x000000C0;
341 buf[outlength] = (char) wc;
342 outlength += charsize;
345 buf[outlength] = '\0';
351 * Convert an ASN.1 String into its UTF-8 string representation.
352 * Return the dynamically allocated string, or NULL if an error occurs.
354 static const char *string2str(int type, const char *beg, const char *end)
357 if(utf8asn1str(&buf, type, beg, end) < 0)
363 * Decimal ASCII encode unsigned integer `x' into the buflen sized buffer at
364 * buf. Return the total number of encoded digits, even if larger than
367 static size_t encodeUint(char *buf, size_t buflen, unsigned int x)
370 unsigned int y = x / 10;
373 i = encodeUint(buf, buflen, y);
377 buf[i] = (char) ('0' + x);
380 buf[i] = '\0'; /* Store a terminator if possible. */
385 * Convert an ASN.1 OID into its dotted string representation.
386 * Store the result in th `n'-byte buffer at `buf'.
387 * Return the converted string length, or 0 on errors.
389 static size_t encodeOID(char *buf, size_t buflen,
390 const char *beg, const char *end)
396 /* Process the first two numbers. */
397 y = *(const unsigned char *) beg++;
400 i = encodeUint(buf, buflen, x);
405 i += encodeUint(NULL, 0, y);
407 i += encodeUint(buf + i, buflen - i, y);
409 /* Process the trailing numbers. */
418 y = *(const unsigned char *) beg++;
419 x = (x << 7) | (y & 0x7F);
422 i += encodeUint(NULL, 0, x);
424 i += encodeUint(buf + i, buflen - i, x);
432 * Convert an ASN.1 OID into its dotted or symbolic string representation.
433 * Return the dynamically allocated string, or NULL if an error occurs.
436 static const char *OID2str(const char *beg, const char *end, bool symbolic)
440 size_t buflen = encodeOID(NULL, 0, beg, end);
442 buf = malloc(buflen + 1); /* one extra for the zero byte */
444 encodeOID(buf, buflen, beg, end);
448 const struct Curl_OID *op = searchOID(buf);
451 buf = strdup(op->textoid);
460 static const char *GTime2str(const char *beg, const char *end)
467 const char *sep = "";
469 /* Convert an ASN.1 Generalized time to a printable string.
470 Return the dynamically allocated string, or NULL if an error occurs. */
472 for(fracp = beg; fracp < end && *fracp >= '0' && *fracp <= '9'; fracp++)
475 /* Get seconds digits. */
477 switch(fracp - beg - 12) {
491 /* Scan for timezone, measure fractional seconds. */
494 if(fracp < end && (*fracp == '.' || *fracp == ',')) {
498 while(tzp < end && *tzp >= '0' && *tzp <= '9');
499 /* Strip leading zeroes in fractional seconds. */
500 for(fracl = tzp - fracp - 1; fracl && fracp[fracl - 1] == '0'; fracl--)
504 /* Process timezone. */
506 ; /* Nothing to do. */
507 else if(*tzp == 'Z') {
517 return curl_maprintf("%.4s-%.2s-%.2s %.2s:%.2s:%c%c%s%.*s%s%.*s",
518 beg, beg + 4, beg + 6,
519 beg + 8, beg + 10, sec1, sec2,
520 fracl? ".": "", fracl, fracp,
525 * Convert an ASN.1 UTC time to a printable string.
526 * Return the dynamically allocated string, or NULL if an error occurs.
528 static const char *UTime2str(const char *beg, const char *end)
534 for(tzp = beg; tzp < end && *tzp >= '0' && *tzp <= '9'; tzp++)
536 /* Get the seconds. */
547 /* Process timezone. */
558 return curl_maprintf("%u%.2s-%.2s-%.2s %.2s:%.2s:%.2s %.*s",
559 20 - (*beg >= '5'), beg, beg + 2, beg + 4,
560 beg + 6, beg + 8, sec,
565 * Convert an ASN.1 element to a printable string.
566 * Return the dynamically allocated string, or NULL if an error occurs.
568 static const char *ASN1tostr(struct Curl_asn1Element *elem, int type)
570 if(elem->constructed)
571 return NULL; /* No conversion of structured elements. */
574 type = elem->tag; /* Type not forced: use element tag as type. */
577 case CURL_ASN1_BOOLEAN:
578 return bool2str(elem->beg, elem->end);
579 case CURL_ASN1_INTEGER:
580 case CURL_ASN1_ENUMERATED:
581 return int2str(elem->beg, elem->end);
582 case CURL_ASN1_BIT_STRING:
583 return bit2str(elem->beg, elem->end);
584 case CURL_ASN1_OCTET_STRING:
585 return octet2str(elem->beg, elem->end);
588 case CURL_ASN1_OBJECT_IDENTIFIER:
589 return OID2str(elem->beg, elem->end, TRUE);
590 case CURL_ASN1_UTC_TIME:
591 return UTime2str(elem->beg, elem->end);
592 case CURL_ASN1_GENERALIZED_TIME:
593 return GTime2str(elem->beg, elem->end);
594 case CURL_ASN1_UTF8_STRING:
595 case CURL_ASN1_NUMERIC_STRING:
596 case CURL_ASN1_PRINTABLE_STRING:
597 case CURL_ASN1_TELETEX_STRING:
598 case CURL_ASN1_IA5_STRING:
599 case CURL_ASN1_VISIBLE_STRING:
600 case CURL_ASN1_UNIVERSAL_STRING:
601 case CURL_ASN1_BMP_STRING:
602 return string2str(type, elem->beg, elem->end);
605 return NULL; /* Unsupported. */
609 * ASCII encode distinguished name at `dn' into the `buflen'-sized buffer at
610 * `buf'. Return the total string length, even if larger than `buflen'.
612 static ssize_t encodeDN(char *buf, size_t buflen, struct Curl_asn1Element *dn)
614 struct Curl_asn1Element rdn;
615 struct Curl_asn1Element atv;
616 struct Curl_asn1Element oid;
617 struct Curl_asn1Element value;
624 for(p1 = dn->beg; p1 < dn->end;) {
625 p1 = getASN1Element(&rdn, p1, dn->end);
628 for(p2 = rdn.beg; p2 < rdn.end;) {
629 p2 = getASN1Element(&atv, p2, rdn.end);
632 p3 = getASN1Element(&oid, atv.beg, atv.end);
635 if(!getASN1Element(&value, p3, atv.end))
637 str = ASN1tostr(&oid, 0);
642 If attribute has a short uppercase name, delimiter is ", ". */
644 for(p3 = str; isupper(*p3); p3++)
646 for(p3 = (*p3 || p3 - str > 2)? "/": ", "; *p3; p3++) {
653 /* Encode attribute name. */
654 for(p3 = str; *p3; p3++) {
661 /* Generate equal sign. */
666 /* Generate value. */
667 str = ASN1tostr(&value, 0);
670 for(p3 = str; *p3; p3++) {
683 * Convert an ASN.1 distinguished name into a printable string.
684 * Return the dynamically allocated string, or NULL if an error occurs.
686 static const char *DNtostr(struct Curl_asn1Element *dn)
689 ssize_t buflen = encodeDN(NULL, 0, dn);
692 buf = malloc(buflen + 1);
694 encodeDN(buf, buflen + 1, dn);
702 * ASN.1 parse an X509 certificate into structure subfields.
703 * Syntax is assumed to have already been checked by the SSL backend.
706 int Curl_parseX509(struct Curl_X509certificate *cert,
707 const char *beg, const char *end)
709 struct Curl_asn1Element elem;
710 struct Curl_asn1Element tbsCertificate;
712 static const char defaultVersion = 0; /* v1. */
714 cert->certificate.header = NULL;
715 cert->certificate.beg = beg;
716 cert->certificate.end = end;
718 /* Get the sequence content. */
719 if(!getASN1Element(&elem, beg, end))
720 return -1; /* Invalid bounds/size. */
724 /* Get tbsCertificate. */
725 beg = getASN1Element(&tbsCertificate, beg, end);
728 /* Skip the signatureAlgorithm. */
729 beg = getASN1Element(&cert->signatureAlgorithm, beg, end);
732 /* Get the signatureValue. */
733 if(!getASN1Element(&cert->signature, beg, end))
736 /* Parse TBSCertificate. */
737 beg = tbsCertificate.beg;
738 end = tbsCertificate.end;
739 /* Get optional version, get serialNumber. */
740 cert->version.header = NULL;
741 cert->version.beg = &defaultVersion;
742 cert->version.end = &defaultVersion + sizeof(defaultVersion);
743 beg = getASN1Element(&elem, beg, end);
747 if(!getASN1Element(&cert->version, elem.beg, elem.end))
749 beg = getASN1Element(&elem, beg, end);
753 cert->serialNumber = elem;
754 /* Get signature algorithm. */
755 beg = getASN1Element(&cert->signatureAlgorithm, beg, end);
757 beg = getASN1Element(&cert->issuer, beg, end);
760 /* Get notBefore and notAfter. */
761 beg = getASN1Element(&elem, beg, end);
764 ccp = getASN1Element(&cert->notBefore, elem.beg, elem.end);
767 if(!getASN1Element(&cert->notAfter, ccp, elem.end))
770 beg = getASN1Element(&cert->subject, beg, end);
773 /* Get subjectPublicKeyAlgorithm and subjectPublicKey. */
774 beg = getASN1Element(&cert->subjectPublicKeyInfo, beg, end);
777 ccp = getASN1Element(&cert->subjectPublicKeyAlgorithm,
778 cert->subjectPublicKeyInfo.beg,
779 cert->subjectPublicKeyInfo.end);
782 if(!getASN1Element(&cert->subjectPublicKey, ccp,
783 cert->subjectPublicKeyInfo.end))
785 /* Get optional issuerUiqueID, subjectUniqueID and extensions. */
786 cert->issuerUniqueID.tag = cert->subjectUniqueID.tag = 0;
787 cert->extensions.tag = elem.tag = 0;
788 cert->issuerUniqueID.header = cert->subjectUniqueID.header = NULL;
789 cert->issuerUniqueID.beg = cert->issuerUniqueID.end = "";
790 cert->subjectUniqueID.beg = cert->subjectUniqueID.end = "";
791 cert->extensions.header = NULL;
792 cert->extensions.beg = cert->extensions.end = "";
794 beg = getASN1Element(&elem, beg, end);
799 cert->issuerUniqueID = elem;
801 beg = getASN1Element(&elem, beg, end);
807 cert->subjectUniqueID = elem;
809 beg = getASN1Element(&elem, beg, end);
815 if(!getASN1Element(&cert->extensions, elem.beg, elem.end))
822 * Copy at most 64-characters, terminate with a newline and returns the
823 * effective number of stored characters.
825 static size_t copySubstring(char *to, const char *from)
828 for(i = 0; i < 64; i++) {
838 static const char *dumpAlgo(struct Curl_asn1Element *param,
839 const char *beg, const char *end)
841 struct Curl_asn1Element oid;
843 /* Get algorithm parameters and return algorithm name. */
845 beg = getASN1Element(&oid, beg, end);
848 param->header = NULL;
850 param->beg = param->end = end;
852 if(!getASN1Element(param, beg, end))
854 return OID2str(oid.beg, oid.end, TRUE);
857 static void do_pubkey_field(struct Curl_easy *data, int certnum,
858 const char *label, struct Curl_asn1Element *elem)
862 /* Generate a certificate information record for the public key. */
864 output = ASN1tostr(elem, 0);
866 if(data->set.ssl.certinfo)
867 Curl_ssl_push_certinfo(data, certnum, label, output);
869 infof(data, " %s: %s\n", label, output);
870 free((char *) output);
874 static void do_pubkey(struct Curl_easy *data, int certnum,
875 const char *algo, struct Curl_asn1Element *param,
876 struct Curl_asn1Element *pubkey)
878 struct Curl_asn1Element elem;
879 struct Curl_asn1Element pk;
882 /* Generate all information records for the public key. */
884 /* Get the public key (single element). */
885 if(!getASN1Element(&pk, pubkey->beg + 1, pubkey->end))
888 if(strcasecompare(algo, "rsaEncryption")) {
892 p = getASN1Element(&elem, pk.beg, pk.end);
896 /* Compute key length. */
897 for(q = elem.beg; !*q && q < elem.end; q++)
899 len = (unsigned long)((elem.end - q) * 8);
902 for(i = *(unsigned char *) q; !(i & 0x80); i <<= 1)
906 elem.beg = q; /* Strip leading zero bytes. */
908 infof(data, " RSA Public Key (%lu bits)\n", len);
909 if(data->set.ssl.certinfo) {
910 q = curl_maprintf("%lu", len);
912 Curl_ssl_push_certinfo(data, certnum, "RSA Public Key", q);
916 /* Generate coefficients. */
917 do_pubkey_field(data, certnum, "rsa(n)", &elem);
918 if(!getASN1Element(&elem, p, pk.end))
920 do_pubkey_field(data, certnum, "rsa(e)", &elem);
922 else if(strcasecompare(algo, "dsa")) {
923 p = getASN1Element(&elem, param->beg, param->end);
925 do_pubkey_field(data, certnum, "dsa(p)", &elem);
926 p = getASN1Element(&elem, p, param->end);
928 do_pubkey_field(data, certnum, "dsa(q)", &elem);
929 if(getASN1Element(&elem, p, param->end)) {
930 do_pubkey_field(data, certnum, "dsa(g)", &elem);
931 do_pubkey_field(data, certnum, "dsa(pub_key)", &pk);
936 else if(strcasecompare(algo, "dhpublicnumber")) {
937 p = getASN1Element(&elem, param->beg, param->end);
939 do_pubkey_field(data, certnum, "dh(p)", &elem);
940 if(getASN1Element(&elem, param->beg, param->end)) {
941 do_pubkey_field(data, certnum, "dh(g)", &elem);
942 do_pubkey_field(data, certnum, "dh(pub_key)", &pk);
948 CURLcode Curl_extract_certinfo(struct connectdata *conn,
953 struct Curl_X509certificate cert;
954 struct Curl_easy *data = conn->data;
955 struct Curl_asn1Element param;
961 unsigned long version;
965 if(!data->set.ssl.certinfo)
969 /* Prepare the certificate information for curl_easy_getinfo(). */
971 /* Extract the certificate ASN.1 elements. */
972 if(Curl_parseX509(&cert, beg, end))
973 return CURLE_PEER_FAILED_VERIFICATION;
976 ccp = DNtostr(&cert.subject);
978 return CURLE_OUT_OF_MEMORY;
979 if(data->set.ssl.certinfo)
980 Curl_ssl_push_certinfo(data, certnum, "Subject", ccp);
982 infof(data, "%2d Subject: %s\n", certnum, ccp);
986 ccp = DNtostr(&cert.issuer);
988 return CURLE_OUT_OF_MEMORY;
989 if(data->set.ssl.certinfo)
990 Curl_ssl_push_certinfo(data, certnum, "Issuer", ccp);
992 infof(data, " Issuer: %s\n", ccp);
995 /* Version (always fits in less than 32 bits). */
997 for(ccp = cert.version.beg; ccp < cert.version.end; ccp++)
998 version = (version << 8) | *(const unsigned char *) ccp;
999 if(data->set.ssl.certinfo) {
1000 ccp = curl_maprintf("%lx", version);
1002 return CURLE_OUT_OF_MEMORY;
1003 Curl_ssl_push_certinfo(data, certnum, "Version", ccp);
1007 infof(data, " Version: %lu (0x%lx)\n", version + 1, version);
1009 /* Serial number. */
1010 ccp = ASN1tostr(&cert.serialNumber, 0);
1012 return CURLE_OUT_OF_MEMORY;
1013 if(data->set.ssl.certinfo)
1014 Curl_ssl_push_certinfo(data, certnum, "Serial Number", ccp);
1016 infof(data, " Serial Number: %s\n", ccp);
1019 /* Signature algorithm .*/
1020 ccp = dumpAlgo(¶m, cert.signatureAlgorithm.beg,
1021 cert.signatureAlgorithm.end);
1023 return CURLE_OUT_OF_MEMORY;
1024 if(data->set.ssl.certinfo)
1025 Curl_ssl_push_certinfo(data, certnum, "Signature Algorithm", ccp);
1027 infof(data, " Signature Algorithm: %s\n", ccp);
1031 ccp = ASN1tostr(&cert.notBefore, 0);
1033 return CURLE_OUT_OF_MEMORY;
1034 if(data->set.ssl.certinfo)
1035 Curl_ssl_push_certinfo(data, certnum, "Start Date", ccp);
1037 infof(data, " Start Date: %s\n", ccp);
1041 ccp = ASN1tostr(&cert.notAfter, 0);
1043 return CURLE_OUT_OF_MEMORY;
1044 if(data->set.ssl.certinfo)
1045 Curl_ssl_push_certinfo(data, certnum, "Expire Date", ccp);
1047 infof(data, " Expire Date: %s\n", ccp);
1050 /* Public Key Algorithm. */
1051 ccp = dumpAlgo(¶m, cert.subjectPublicKeyAlgorithm.beg,
1052 cert.subjectPublicKeyAlgorithm.end);
1054 return CURLE_OUT_OF_MEMORY;
1055 if(data->set.ssl.certinfo)
1056 Curl_ssl_push_certinfo(data, certnum, "Public Key Algorithm", ccp);
1058 infof(data, " Public Key Algorithm: %s\n", ccp);
1059 do_pubkey(data, certnum, ccp, ¶m, &cert.subjectPublicKey);
1063 ccp = ASN1tostr(&cert.signature, 0);
1065 return CURLE_OUT_OF_MEMORY;
1066 if(data->set.ssl.certinfo)
1067 Curl_ssl_push_certinfo(data, certnum, "Signature", ccp);
1069 infof(data, " Signature: %s\n", ccp);
1072 /* Generate PEM certificate. */
1073 result = Curl_base64_encode(data, cert.certificate.beg,
1074 cert.certificate.end - cert.certificate.beg,
1078 /* Compute the number of characters in final certificate string. Format is:
1079 -----BEGIN CERTIFICATE-----\n
1080 <max 64 base64 characters>\n
1084 -----END CERTIFICATE-----\n
1086 i = 28 + cl1 + (cl1 + 64 - 1) / 64 + 26;
1087 cp2 = malloc(i + 1);
1090 return CURLE_OUT_OF_MEMORY;
1092 /* Build the certificate string. */
1093 i = copySubstring(cp2, "-----BEGIN CERTIFICATE-----");
1094 for(j = 0; j < cl1; j += 64)
1095 i += copySubstring(cp2 + i, cp1 + j);
1096 i += copySubstring(cp2 + i, "-----END CERTIFICATE-----");
1099 if(data->set.ssl.certinfo)
1100 Curl_ssl_push_certinfo(data, certnum, "Cert", cp2);
1102 infof(data, "%s\n", cp2);
1107 #endif /* USE_GSKIT or USE_NSS or USE_GNUTLS or USE_WOLFSSL or USE_SCHANNEL */
1109 #if defined(USE_GSKIT)
1111 static const char *checkOID(const char *beg, const char *end,
1114 struct Curl_asn1Element e;
1119 /* Check if first ASN.1 element at `beg' is the given OID.
1120 Return a pointer in the source after the OID if found, else NULL. */
1122 ccp = getASN1Element(&e, beg, end);
1123 if(!ccp || e.tag != CURL_ASN1_OBJECT_IDENTIFIER)
1126 p = OID2str(e.beg, e.end, FALSE);
1130 matched = !strcmp(p, oid);
1132 return matched? ccp: NULL;
1135 CURLcode Curl_verifyhost(struct connectdata *conn,
1136 const char *beg, const char *end)
1138 struct Curl_easy *data = conn->data;
1139 struct Curl_X509certificate cert;
1140 struct Curl_asn1Element dn;
1141 struct Curl_asn1Element elem;
1142 struct Curl_asn1Element ext;
1143 struct Curl_asn1Element name;
1148 size_t addrlen = (size_t) -1;
1150 const char *const hostname = SSL_IS_PROXY()?
1151 conn->http_proxy.host.name : conn->host.name;
1152 const char *const dispname = SSL_IS_PROXY()?
1153 conn->http_proxy.host.dispname : conn->host.dispname;
1155 struct in6_addr addr;
1157 struct in_addr addr;
1160 /* Verify that connection server matches info in X509 certificate at
1163 if(!SSL_CONN_CONFIG(verifyhost))
1166 if(Curl_parseX509(&cert, beg, end))
1167 return CURLE_PEER_FAILED_VERIFICATION;
1169 /* Get the server IP address. */
1171 if(conn->bits.ipv6_ip && Curl_inet_pton(AF_INET6, hostname, &addr))
1172 addrlen = sizeof(struct in6_addr);
1175 if(Curl_inet_pton(AF_INET, hostname, &addr))
1176 addrlen = sizeof(struct in_addr);
1178 /* Process extensions. */
1179 for(p = cert.extensions.beg; p < cert.extensions.end && matched != 1;) {
1180 p = getASN1Element(&ext, p, cert.extensions.end);
1182 return CURLE_PEER_FAILED_VERIFICATION;
1184 /* Check if extension is a subjectAlternativeName. */
1185 ext.beg = checkOID(ext.beg, ext.end, sanOID);
1187 ext.beg = getASN1Element(&elem, ext.beg, ext.end);
1189 return CURLE_PEER_FAILED_VERIFICATION;
1190 /* Skip critical if present. */
1191 if(elem.tag == CURL_ASN1_BOOLEAN) {
1192 ext.beg = getASN1Element(&elem, ext.beg, ext.end);
1194 return CURLE_PEER_FAILED_VERIFICATION;
1196 /* Parse the octet string contents: is a single sequence. */
1197 if(!getASN1Element(&elem, elem.beg, elem.end))
1198 return CURLE_PEER_FAILED_VERIFICATION;
1199 /* Check all GeneralNames. */
1200 for(q = elem.beg; matched != 1 && q < elem.end;) {
1201 q = getASN1Element(&name, q, elem.end);
1205 case 2: /* DNS name. */
1206 len = utf8asn1str(&dnsname, CURL_ASN1_IA5_STRING,
1207 name.beg, name.end);
1208 if(len > 0 && (size_t)len == strlen(dnsname))
1209 matched = Curl_cert_hostcheck(dnsname, hostname);
1215 case 7: /* IP address. */
1216 matched = (size_t) (name.end - name.beg) == addrlen &&
1217 !memcmp(&addr, name.beg, addrlen);
1226 /* an alternative name matched the server hostname */
1227 infof(data, "\t subjectAltName: %s matched\n", dispname);
1230 /* an alternative name field existed, but didn't match and then
1232 infof(data, "\t subjectAltName does not match %s\n", dispname);
1233 return CURLE_PEER_FAILED_VERIFICATION;
1236 /* Process subject. */
1238 name.beg = name.end = "";
1239 q = cert.subject.beg;
1240 /* we have to look to the last occurrence of a commonName in the
1241 distinguished one to get the most significant one. */
1242 while(q < cert.subject.end) {
1243 q = getASN1Element(&dn, q, cert.subject.end);
1246 for(p = dn.beg; p < dn.end;) {
1247 p = getASN1Element(&elem, p, dn.end);
1249 return CURLE_PEER_FAILED_VERIFICATION;
1250 /* We have a DN's AttributeTypeAndValue: check it in case it's a CN. */
1251 elem.beg = checkOID(elem.beg, elem.end, cnOID);
1253 name = elem; /* Latch CN. */
1257 /* Check the CN if found. */
1258 if(!getASN1Element(&elem, name.beg, name.end))
1259 failf(data, "SSL: unable to obtain common name from peer certificate");
1261 len = utf8asn1str(&dnsname, elem.tag, elem.beg, elem.end);
1264 return CURLE_OUT_OF_MEMORY;
1266 if(strlen(dnsname) != (size_t) len) /* Nul byte in string ? */
1267 failf(data, "SSL: illegal cert name field");
1268 else if(Curl_cert_hostcheck((const char *) dnsname, hostname)) {
1269 infof(data, "\t common name: %s (matched)\n", dnsname);
1274 failf(data, "SSL: certificate subject name '%s' does not match "
1275 "target host name '%s'", dnsname, dispname);
1279 return CURLE_PEER_FAILED_VERIFICATION;
1282 #endif /* USE_GSKIT */