1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2016, 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_CYASSL) || 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"
43 /* For overflow checks. */
44 #define CURL_SIZE_T_MAX ((size_t)-1)
48 static const char cnOID[] = "2.5.4.3"; /* Common name. */
49 static const char sanOID[] = "2.5.29.17"; /* Subject alternative name. */
51 static const curl_OID OIDtable[] = {
52 { "1.2.840.10040.4.1", "dsa" },
53 { "1.2.840.10040.4.3", "dsa-with-sha1" },
54 { "1.2.840.10045.2.1", "ecPublicKey" },
55 { "1.2.840.10045.3.0.1", "c2pnb163v1" },
56 { "1.2.840.10045.4.1", "ecdsa-with-SHA1" },
57 { "1.2.840.10046.2.1", "dhpublicnumber" },
58 { "1.2.840.113549.1.1.1", "rsaEncryption" },
59 { "1.2.840.113549.1.1.2", "md2WithRSAEncryption" },
60 { "1.2.840.113549.1.1.4", "md5WithRSAEncryption" },
61 { "1.2.840.113549.1.1.5", "sha1WithRSAEncryption" },
62 { "1.2.840.113549.1.1.10", "RSASSA-PSS" },
63 { "1.2.840.113549.1.1.14", "sha224WithRSAEncryption" },
64 { "1.2.840.113549.1.1.11", "sha256WithRSAEncryption" },
65 { "1.2.840.113549.1.1.12", "sha384WithRSAEncryption" },
66 { "1.2.840.113549.1.1.13", "sha512WithRSAEncryption" },
67 { "1.2.840.113549.2.2", "md2" },
68 { "1.2.840.113549.2.5", "md5" },
69 { "1.3.14.3.2.26", "sha1" },
72 { "2.5.4.5", "serialNumber" },
76 { "2.5.4.9", "streetAddress" },
79 { "2.5.4.12", "title" },
80 { "2.5.4.13", "description" },
81 { "2.5.4.17", "postalCode" },
82 { "2.5.4.41", "name" },
83 { "2.5.4.42", "givenName" },
84 { "2.5.4.43", "initials" },
85 { "2.5.4.44", "generationQualifier" },
86 { "2.5.4.45", "X500UniqueIdentifier" },
87 { "2.5.4.46", "dnQualifier" },
88 { "2.5.4.65", "pseudonym" },
89 { "1.2.840.113549.1.9.1", "emailAddress" },
90 { "2.5.4.72", "role" },
91 { sanOID, "subjectAltName" },
92 { "2.5.29.18", "issuerAltName" },
93 { "2.5.29.19", "basicConstraints" },
94 { "2.16.840.1.101.3.4.2.4", "sha224" },
95 { "2.16.840.1.101.3.4.2.1", "sha256" },
96 { "2.16.840.1.101.3.4.2.2", "sha384" },
97 { "2.16.840.1.101.3.4.2.3", "sha512" },
98 { (const char *) NULL, (const char *) NULL }
102 * Lightweight ASN.1 parser.
103 * In particular, it does not check for syntactic/lexical errors.
104 * It is intended to support certificate information gathering for SSL backends
105 * that offer a mean to get certificates as a whole, but do not supply
106 * entry points to get particular certificate sub-fields.
107 * Please note there is no pretention here to rewrite a full SSL library.
111 const char *Curl_getASN1Element(curl_asn1Element *elem,
112 const char *beg, const char *end)
116 curl_asn1Element lelem;
118 /* Get a single ASN.1 element into `elem', parse ASN.1 string at `beg'
120 Returns a pointer in source string after the parsed element, or NULL
121 if an error occurs. */
122 if(!beg || !end || beg >= end || !*beg ||
123 (size_t)(end - beg) > CURL_ASN1_MAX)
124 return (const char *) NULL;
126 /* Process header byte. */
128 b = (unsigned char) *beg++;
129 elem->constructed = (b & 0x20) != 0;
130 elem->class = (b >> 6) & 3;
133 return (const char *) NULL; /* Long tag values not supported here. */
136 /* Process length. */
138 return (const char *) NULL;
139 b = (unsigned char) *beg++;
142 else if(!(b &= 0x7F)) {
143 /* Unspecified length. Since we have all the data, we can determine the
144 effective length by skipping element until an end element is found. */
145 if(!elem->constructed)
146 return (const char *) NULL;
148 while(beg < end && *beg) {
149 beg = Curl_getASN1Element(&lelem, beg, end);
151 return (const char *) NULL;
154 return (const char *) NULL;
158 else if((unsigned)b > (size_t)(end - beg))
159 return (const char *) NULL; /* Does not fit in source. */
161 /* Get long length. */
164 if(len & 0xFF000000L)
165 return (const char *) NULL; /* Lengths > 32 bits are not supported. */
166 len = (len << 8) | (unsigned char) *beg++;
169 if(len > (size_t)(end - beg))
170 return (const char *) NULL; /* Element data does not fit in source. */
172 elem->end = beg + len;
176 static const curl_OID * searchOID(const char *oid)
180 /* Search the null terminated OID or OID identifier in local table.
181 Return the table entry pointer or NULL if not found. */
183 for(op = OIDtable; op->numoid; op++)
184 if(!strcmp(op->numoid, oid) || strcasecompare(op->textoid, oid))
187 return (const curl_OID *) NULL;
190 static const char *bool2str(const char *beg, const char *end)
192 /* Convert an ASN.1 Boolean value into its string representation.
193 Return the dynamically allocated string, or NULL if source is not an
194 ASN.1 Boolean value. */
197 return (const char *) NULL;
198 return strdup(*beg? "TRUE": "FALSE");
201 static const char *octet2str(const char *beg, const char *end)
203 size_t n = end - beg;
206 /* Convert an ASN.1 octet string to a printable string.
207 Return the dynamically allocated string, or NULL if an error occurs. */
209 if(n <= (CURL_SIZE_T_MAX - 1) / 3) {
210 buf = malloc(3 * n + 1);
212 for(n = 0; beg < end; n += 3)
213 snprintf(buf + n, 4, "%02x:", *(const unsigned char *) beg++);
218 static const char *bit2str(const char *beg, const char *end)
220 /* Convert an ASN.1 bit string to a printable string.
221 Return the dynamically allocated string, or NULL if an error occurs. */
224 return (const char *) NULL;
225 return octet2str(beg, end);
228 static const char *int2str(const char *beg, const char *end)
231 size_t n = end - beg;
233 /* Convert an ASN.1 integer value into its string representation.
234 Return the dynamically allocated string, or NULL if source is not an
235 ASN.1 integer value. */
238 return (const char *) NULL;
241 return octet2str(beg, end);
243 /* Represent integers <= 32-bit as a single value. */
248 val = (val << 8) | *(const unsigned char *) beg++;
250 return curl_maprintf("%s%lx", (val < 0 || val >= 10)? "0x": "", val);
254 utf8asn1str(char **to, int type, const char *from, const char *end)
256 size_t inlength = end - from;
263 /* Perform a lazy conversion from an ASN.1 typed string to UTF8. Allocate the
264 destination buffer dynamically. The allocation size will normally be too
265 large: this is to avoid buffer overflows.
266 Terminate the string with a nul byte and return the converted
271 case CURL_ASN1_BMP_STRING:
274 case CURL_ASN1_UNIVERSAL_STRING:
277 case CURL_ASN1_NUMERIC_STRING:
278 case CURL_ASN1_PRINTABLE_STRING:
279 case CURL_ASN1_TELETEX_STRING:
280 case CURL_ASN1_IA5_STRING:
281 case CURL_ASN1_VISIBLE_STRING:
282 case CURL_ASN1_UTF8_STRING:
285 return -1; /* Conversion not supported. */
289 return -1; /* Length inconsistent with character size. */
290 if(inlength / size > (CURL_SIZE_T_MAX - 1) / 4)
291 return -1; /* Too big. */
292 buf = malloc(4 * (inlength / size) + 1);
294 return -1; /* Not enough memory. */
296 if(type == CURL_ASN1_UTF8_STRING) {
298 outlength = inlength;
300 memcpy(buf, from, outlength);
303 for(outlength = 0; from < end;) {
307 wc = (wc << 8) | *(const unsigned char *) from++;
308 wc = (wc << 8) | *(const unsigned char *) from++;
311 wc = (wc << 8) | *(const unsigned char *) from++;
313 default: /* case 1: */
314 wc = (wc << 8) | *(const unsigned char *) from++;
317 if(wc >= 0x00000080) {
318 if(wc >= 0x00000800) {
319 if(wc >= 0x00010000) {
320 if(wc >= 0x00200000) {
322 return -1; /* Invalid char. size for target encoding. */
324 buf[outlength + 3] = (char) (0x80 | (wc & 0x3F));
325 wc = (wc >> 6) | 0x00010000;
328 buf[outlength + 2] = (char) (0x80 | (wc & 0x3F));
329 wc = (wc >> 6) | 0x00000800;
332 buf[outlength + 1] = (char) (0x80 | (wc & 0x3F));
333 wc = (wc >> 6) | 0x000000C0;
336 buf[outlength] = (char) wc;
337 outlength += charsize;
340 buf[outlength] = '\0';
345 static const char *string2str(int type, const char *beg, const char *end)
349 /* Convert an ASN.1 String into its UTF-8 string representation.
350 Return the dynamically allocated string, or NULL if an error occurs. */
352 if(utf8asn1str(&buf, type, beg, end) < 0)
353 return (const char *) NULL;
357 static int encodeUint(char *buf, int n, unsigned int x)
360 unsigned int y = x / 10;
362 /* Decimal ASCII encode unsigned integer `x' in the `n'-byte buffer at `buf'.
363 Return the total number of encoded digits, even if larger than `n'. */
366 i += encodeUint(buf, n, y);
370 buf[i] = (char) ('0' + x);
373 buf[i] = '\0'; /* Store a terminator if possible. */
377 static int encodeOID(char *buf, int n, const char *beg, const char *end)
383 /* Convert an ASN.1 OID into its dotted string representation.
384 Store the result in th `n'-byte buffer at `buf'.
385 Return the converted string length, or -1 if an error occurs. */
387 /* Process the first two numbers. */
388 y = *(const unsigned char *) beg++;
391 i += encodeUint(buf + i, n - i, x);
395 i += encodeUint(buf + i, n - i, y);
397 /* Process the trailing numbers. */
406 y = *(const unsigned char *) beg++;
407 x = (x << 7) | (y & 0x7F);
409 i += encodeUint(buf + i, n - i, x);
416 static const char *OID2str(const char *beg, const char *end, bool symbolic)
418 char *buf = (char *) NULL;
422 /* Convert an ASN.1 OID into its dotted or symbolic string representation.
423 Return the dynamically allocated string, or NULL if an error occurs. */
426 n = encodeOID((char *) NULL, -1, beg, end);
430 encodeOID(buf, n, beg, end);
437 buf = strdup(op->textoid);
446 static const char *GTime2str(const char *beg, const char *end)
453 const char *sep = "";
455 /* Convert an ASN.1 Generalized time to a printable string.
456 Return the dynamically allocated string, or NULL if an error occurs. */
458 for(fracp = beg; fracp < end && *fracp >= '0' && *fracp <= '9'; fracp++)
461 /* Get seconds digits. */
463 switch(fracp - beg - 12) {
473 return (const char *) NULL;
476 /* Scan for timezone, measure fractional seconds. */
479 if(fracp < end && (*fracp == '.' || *fracp == ',')) {
483 while(tzp < end && *tzp >= '0' && *tzp <= '9');
484 /* Strip leading zeroes in fractional seconds. */
485 for(fracl = tzp - fracp - 1; fracl && fracp[fracl - 1] == '0'; fracl--)
489 /* Process timezone. */
491 ; /* Nothing to do. */
492 else if(*tzp == 'Z') {
502 return curl_maprintf("%.4s-%.2s-%.2s %.2s:%.2s:%c%c%s%.*s%s%.*s",
503 beg, beg + 4, beg + 6,
504 beg + 8, beg + 10, sec1, sec2,
505 fracl? ".": "", fracl, fracp,
509 static const char *UTime2str(const char *beg, const char *end)
515 /* Convert an ASN.1 UTC time to a printable string.
516 Return the dynamically allocated string, or NULL if an error occurs. */
518 for(tzp = beg; tzp < end && *tzp >= '0' && *tzp <= '9'; tzp++)
520 /* Get the seconds. */
528 return (const char *) NULL;
531 /* Process timezone. */
533 return (const char *) NULL;
542 return curl_maprintf("%u%.2s-%.2s-%.2s %.2s:%.2s:%.2s %.*s",
543 20 - (*beg >= '5'), beg, beg + 2, beg + 4,
544 beg + 6, beg + 8, sec,
548 const char *Curl_ASN1tostr(curl_asn1Element *elem, int type)
550 /* Convert an ASN.1 element to a printable string.
551 Return the dynamically allocated string, or NULL if an error occurs. */
553 if(elem->constructed)
554 return (const char *) NULL; /* No conversion of structured elements. */
557 type = elem->tag; /* Type not forced: use element tag as type. */
560 case CURL_ASN1_BOOLEAN:
561 return bool2str(elem->beg, elem->end);
562 case CURL_ASN1_INTEGER:
563 case CURL_ASN1_ENUMERATED:
564 return int2str(elem->beg, elem->end);
565 case CURL_ASN1_BIT_STRING:
566 return bit2str(elem->beg, elem->end);
567 case CURL_ASN1_OCTET_STRING:
568 return octet2str(elem->beg, elem->end);
571 case CURL_ASN1_OBJECT_IDENTIFIER:
572 return OID2str(elem->beg, elem->end, TRUE);
573 case CURL_ASN1_UTC_TIME:
574 return UTime2str(elem->beg, elem->end);
575 case CURL_ASN1_GENERALIZED_TIME:
576 return GTime2str(elem->beg, elem->end);
577 case CURL_ASN1_UTF8_STRING:
578 case CURL_ASN1_NUMERIC_STRING:
579 case CURL_ASN1_PRINTABLE_STRING:
580 case CURL_ASN1_TELETEX_STRING:
581 case CURL_ASN1_IA5_STRING:
582 case CURL_ASN1_VISIBLE_STRING:
583 case CURL_ASN1_UNIVERSAL_STRING:
584 case CURL_ASN1_BMP_STRING:
585 return string2str(type, elem->beg, elem->end);
588 return (const char *) NULL; /* Unsupported. */
591 static ssize_t encodeDN(char *buf, size_t n, curl_asn1Element *dn)
593 curl_asn1Element rdn;
594 curl_asn1Element atv;
595 curl_asn1Element oid;
596 curl_asn1Element value;
603 /* ASCII encode distinguished name at `dn' into the `n'-byte buffer at `buf'.
604 Return the total string length, even if larger than `n'. */
606 for(p1 = dn->beg; p1 < dn->end;) {
607 p1 = Curl_getASN1Element(&rdn, p1, dn->end);
608 for(p2 = rdn.beg; p2 < rdn.end;) {
609 p2 = Curl_getASN1Element(&atv, p2, rdn.end);
610 p3 = Curl_getASN1Element(&oid, atv.beg, atv.end);
611 Curl_getASN1Element(&value, p3, atv.end);
612 str = Curl_ASN1tostr(&oid, 0);
617 If attribute has a short uppercase name, delimiter is ", ". */
619 for(p3 = str; isupper(*p3); p3++)
621 for(p3 = (*p3 || p3 - str > 2)? "/": ", "; *p3; p3++) {
628 /* Encode attribute name. */
629 for(p3 = str; *p3; p3++) {
636 /* Generate equal sign. */
641 /* Generate value. */
642 str = Curl_ASN1tostr(&value, 0);
645 for(p3 = str; *p3; p3++) {
657 const char *Curl_DNtostr(curl_asn1Element *dn)
659 char *buf = (char *) NULL;
660 ssize_t n = encodeDN(buf, 0, dn);
662 /* Convert an ASN.1 distinguished name into a printable string.
663 Return the dynamically allocated string, or NULL if an error occurs. */
668 encodeDN(buf, n + 1, dn);
672 return (const char *) buf;
679 int Curl_parseX509(curl_X509certificate *cert,
680 const char *beg, const char *end)
682 curl_asn1Element elem;
683 curl_asn1Element tbsCertificate;
685 static const char defaultVersion = 0; /* v1. */
687 /* ASN.1 parse an X509 certificate into structure subfields.
688 Syntax is assumed to have already been checked by the SSL backend.
691 cert->certificate.header = NULL;
692 cert->certificate.beg = beg;
693 cert->certificate.end = end;
695 /* Get the sequence content. */
696 if(!Curl_getASN1Element(&elem, beg, end))
697 return -1; /* Invalid bounds/size. */
701 /* Get tbsCertificate. */
702 beg = Curl_getASN1Element(&tbsCertificate, beg, end);
703 /* Skip the signatureAlgorithm. */
704 beg = Curl_getASN1Element(&cert->signatureAlgorithm, beg, end);
705 /* Get the signatureValue. */
706 Curl_getASN1Element(&cert->signature, beg, end);
708 /* Parse TBSCertificate. */
709 beg = tbsCertificate.beg;
710 end = tbsCertificate.end;
711 /* Get optional version, get serialNumber. */
712 cert->version.header = NULL;
713 cert->version.beg = &defaultVersion;
714 cert->version.end = &defaultVersion + sizeof defaultVersion;;
715 beg = Curl_getASN1Element(&elem, beg, end);
717 Curl_getASN1Element(&cert->version, elem.beg, elem.end);
718 beg = Curl_getASN1Element(&elem, beg, end);
720 cert->serialNumber = elem;
721 /* Get signature algorithm. */
722 beg = Curl_getASN1Element(&cert->signatureAlgorithm, beg, end);
724 beg = Curl_getASN1Element(&cert->issuer, beg, end);
725 /* Get notBefore and notAfter. */
726 beg = Curl_getASN1Element(&elem, beg, end);
727 ccp = Curl_getASN1Element(&cert->notBefore, elem.beg, elem.end);
728 Curl_getASN1Element(&cert->notAfter, ccp, elem.end);
730 beg = Curl_getASN1Element(&cert->subject, beg, end);
731 /* Get subjectPublicKeyAlgorithm and subjectPublicKey. */
732 beg = Curl_getASN1Element(&cert->subjectPublicKeyInfo, beg, end);
733 ccp = Curl_getASN1Element(&cert->subjectPublicKeyAlgorithm,
734 cert->subjectPublicKeyInfo.beg,
735 cert->subjectPublicKeyInfo.end);
736 Curl_getASN1Element(&cert->subjectPublicKey, ccp,
737 cert->subjectPublicKeyInfo.end);
738 /* Get optional issuerUiqueID, subjectUniqueID and extensions. */
739 cert->issuerUniqueID.tag = cert->subjectUniqueID.tag = 0;
740 cert->extensions.tag = elem.tag = 0;
741 cert->issuerUniqueID.header = cert->subjectUniqueID.header = NULL;
742 cert->issuerUniqueID.beg = cert->issuerUniqueID.end = "";
743 cert->subjectUniqueID.beg = cert->subjectUniqueID.end = "";
744 cert->extensions.header = NULL;
745 cert->extensions.beg = cert->extensions.end = "";
747 beg = Curl_getASN1Element(&elem, beg, end);
749 cert->issuerUniqueID = elem;
751 beg = Curl_getASN1Element(&elem, beg, end);
754 cert->subjectUniqueID = elem;
756 beg = Curl_getASN1Element(&elem, beg, end);
759 Curl_getASN1Element(&cert->extensions, elem.beg, elem.end);
763 static size_t copySubstring(char *to, const char *from)
767 /* Copy at most 64-characters, terminate with a newline and returns the
768 effective number of stored characters. */
770 for(i = 0; i < 64; i++) {
780 static const char *dumpAlgo(curl_asn1Element *param,
781 const char *beg, const char *end)
783 curl_asn1Element oid;
785 /* Get algorithm parameters and return algorithm name. */
787 beg = Curl_getASN1Element(&oid, beg, end);
788 param->header = NULL;
790 param->beg = param->end = end;
792 Curl_getASN1Element(param, beg, end);
793 return OID2str(oid.beg, oid.end, TRUE);
796 static void do_pubkey_field(struct Curl_easy *data, int certnum,
797 const char *label, curl_asn1Element *elem)
801 /* Generate a certificate information record for the public key. */
803 output = Curl_ASN1tostr(elem, 0);
805 if(data->set.ssl.certinfo)
806 Curl_ssl_push_certinfo(data, certnum, label, output);
808 infof(data, " %s: %s\n", label, output);
809 free((char *) output);
813 static void do_pubkey(struct Curl_easy *data, int certnum,
814 const char *algo, curl_asn1Element *param,
815 curl_asn1Element *pubkey)
817 curl_asn1Element elem;
824 /* Generate all information records for the public key. */
826 /* Get the public key (single element). */
827 Curl_getASN1Element(&pk, pubkey->beg + 1, pubkey->end);
829 if(strcasecompare(algo, "rsaEncryption")) {
830 p = Curl_getASN1Element(&elem, pk.beg, pk.end);
831 /* Compute key length. */
832 for(q = elem.beg; !*q && q < elem.end; q++)
834 len = (unsigned long)((elem.end - q) * 8);
836 for(i = *(unsigned char *) q; !(i & 0x80); i <<= 1)
839 elem.beg = q; /* Strip leading zero bytes. */
841 infof(data, " RSA Public Key (%lu bits)\n", len);
842 if(data->set.ssl.certinfo) {
843 q = curl_maprintf("%lu", len);
845 Curl_ssl_push_certinfo(data, certnum, "RSA Public Key", q);
849 /* Generate coefficients. */
850 do_pubkey_field(data, certnum, "rsa(n)", &elem);
851 Curl_getASN1Element(&elem, p, pk.end);
852 do_pubkey_field(data, certnum, "rsa(e)", &elem);
854 else if(strcasecompare(algo, "dsa")) {
855 p = Curl_getASN1Element(&elem, param->beg, param->end);
856 do_pubkey_field(data, certnum, "dsa(p)", &elem);
857 p = Curl_getASN1Element(&elem, p, param->end);
858 do_pubkey_field(data, certnum, "dsa(q)", &elem);
859 Curl_getASN1Element(&elem, p, param->end);
860 do_pubkey_field(data, certnum, "dsa(g)", &elem);
861 do_pubkey_field(data, certnum, "dsa(pub_key)", &pk);
863 else if(strcasecompare(algo, "dhpublicnumber")) {
864 p = Curl_getASN1Element(&elem, param->beg, param->end);
865 do_pubkey_field(data, certnum, "dh(p)", &elem);
866 Curl_getASN1Element(&elem, param->beg, param->end);
867 do_pubkey_field(data, certnum, "dh(g)", &elem);
868 do_pubkey_field(data, certnum, "dh(pub_key)", &pk);
870 #if 0 /* Patent-encumbered. */
871 else if(strcasecompare(algo, "ecPublicKey")) {
877 CURLcode Curl_extract_certinfo(struct connectdata *conn,
882 curl_X509certificate cert;
883 struct Curl_easy *data = conn->data;
884 curl_asn1Element param;
890 unsigned long version;
894 if(!data->set.ssl.certinfo)
898 /* Prepare the certificate information for curl_easy_getinfo(). */
900 /* Extract the certificate ASN.1 elements. */
901 if(Curl_parseX509(&cert, beg, end))
902 return CURLE_OUT_OF_MEMORY;
905 ccp = Curl_DNtostr(&cert.subject);
907 return CURLE_OUT_OF_MEMORY;
908 if(data->set.ssl.certinfo)
909 Curl_ssl_push_certinfo(data, certnum, "Subject", ccp);
911 infof(data, "%2d Subject: %s\n", certnum, ccp);
915 ccp = Curl_DNtostr(&cert.issuer);
917 return CURLE_OUT_OF_MEMORY;
918 if(data->set.ssl.certinfo)
919 Curl_ssl_push_certinfo(data, certnum, "Issuer", ccp);
921 infof(data, " Issuer: %s\n", ccp);
924 /* Version (always fits in less than 32 bits). */
926 for(ccp = cert.version.beg; ccp < cert.version.end; ccp++)
927 version = (version << 8) | *(const unsigned char *) ccp;
928 if(data->set.ssl.certinfo) {
929 ccp = curl_maprintf("%lx", version);
931 return CURLE_OUT_OF_MEMORY;
932 Curl_ssl_push_certinfo(data, certnum, "Version", ccp);
936 infof(data, " Version: %lu (0x%lx)\n", version + 1, version);
939 ccp = Curl_ASN1tostr(&cert.serialNumber, 0);
941 return CURLE_OUT_OF_MEMORY;
942 if(data->set.ssl.certinfo)
943 Curl_ssl_push_certinfo(data, certnum, "Serial Number", ccp);
945 infof(data, " Serial Number: %s\n", ccp);
948 /* Signature algorithm .*/
949 ccp = dumpAlgo(¶m, cert.signatureAlgorithm.beg,
950 cert.signatureAlgorithm.end);
952 return CURLE_OUT_OF_MEMORY;
953 if(data->set.ssl.certinfo)
954 Curl_ssl_push_certinfo(data, certnum, "Signature Algorithm", ccp);
956 infof(data, " Signature Algorithm: %s\n", ccp);
960 ccp = Curl_ASN1tostr(&cert.notBefore, 0);
962 return CURLE_OUT_OF_MEMORY;
963 if(data->set.ssl.certinfo)
964 Curl_ssl_push_certinfo(data, certnum, "Start Date", ccp);
966 infof(data, " Start Date: %s\n", ccp);
970 ccp = Curl_ASN1tostr(&cert.notAfter, 0);
972 return CURLE_OUT_OF_MEMORY;
973 if(data->set.ssl.certinfo)
974 Curl_ssl_push_certinfo(data, certnum, "Expire Date", ccp);
976 infof(data, " Expire Date: %s\n", ccp);
979 /* Public Key Algorithm. */
980 ccp = dumpAlgo(¶m, cert.subjectPublicKeyAlgorithm.beg,
981 cert.subjectPublicKeyAlgorithm.end);
983 return CURLE_OUT_OF_MEMORY;
984 if(data->set.ssl.certinfo)
985 Curl_ssl_push_certinfo(data, certnum, "Public Key Algorithm", ccp);
987 infof(data, " Public Key Algorithm: %s\n", ccp);
988 do_pubkey(data, certnum, ccp, ¶m, &cert.subjectPublicKey);
991 /* TODO: extensions. */
994 ccp = Curl_ASN1tostr(&cert.signature, 0);
996 return CURLE_OUT_OF_MEMORY;
997 if(data->set.ssl.certinfo)
998 Curl_ssl_push_certinfo(data, certnum, "Signature", ccp);
1000 infof(data, " Signature: %s\n", ccp);
1003 /* Generate PEM certificate. */
1004 result = Curl_base64_encode(data, cert.certificate.beg,
1005 cert.certificate.end - cert.certificate.beg,
1009 /* Compute the number of characters in final certificate string. Format is:
1010 -----BEGIN CERTIFICATE-----\n
1011 <max 64 base64 characters>\n
1015 -----END CERTIFICATE-----\n
1017 i = 28 + cl1 + (cl1 + 64 - 1) / 64 + 26;
1018 cp2 = malloc(i + 1);
1021 return CURLE_OUT_OF_MEMORY;
1023 /* Build the certificate string. */
1024 i = copySubstring(cp2, "-----BEGIN CERTIFICATE-----");
1025 for(j = 0; j < cl1; j += 64)
1026 i += copySubstring(cp2 + i, cp1 + j);
1027 i += copySubstring(cp2 + i, "-----END CERTIFICATE-----");
1030 if(data->set.ssl.certinfo)
1031 Curl_ssl_push_certinfo(data, certnum, "Cert", cp2);
1033 infof(data, "%s\n", cp2);
1038 #endif /* USE_GSKIT or USE_NSS or USE_GNUTLS or USE_CYASSL or USE_SCHANNEL */
1040 #if defined(USE_GSKIT)
1042 static const char *checkOID(const char *beg, const char *end,
1050 /* Check if first ASN.1 element at `beg' is the given OID.
1051 Return a pointer in the source after the OID if found, else NULL. */
1053 ccp = Curl_getASN1Element(&e, beg, end);
1054 if(!ccp || e.tag != CURL_ASN1_OBJECT_IDENTIFIER)
1055 return (const char *) NULL;
1057 p = OID2str(e.beg, e.end, FALSE);
1059 return (const char *) NULL;
1061 matched = !strcmp(p, oid);
1063 return matched? ccp: (const char *) NULL;
1066 CURLcode Curl_verifyhost(struct connectdata *conn,
1067 const char *beg, const char *end)
1069 struct Curl_easy *data = conn->data;
1070 curl_X509certificate cert;
1071 curl_asn1Element dn;
1072 curl_asn1Element elem;
1073 curl_asn1Element ext;
1074 curl_asn1Element name;
1079 size_t addrlen = (size_t) -1;
1081 const char * const hostname = SSL_IS_PROXY()? conn->http_proxy.host.name:
1083 const char * const dispname = SSL_IS_PROXY()?
1084 conn->http_proxy.host.dispname:
1085 conn->host.dispname;
1087 struct in6_addr addr;
1089 struct in_addr addr;
1092 /* Verify that connection server matches info in X509 certificate at
1095 if(!SSL_CONN_CONFIG(verifyhost))
1098 if(Curl_parseX509(&cert, beg, end))
1099 return CURLE_PEER_FAILED_VERIFICATION;
1101 /* Get the server IP address. */
1103 if(conn->bits.ipv6_ip && Curl_inet_pton(AF_INET6, hostname, &addr))
1104 addrlen = sizeof(struct in6_addr);
1107 if(Curl_inet_pton(AF_INET, hostname, &addr))
1108 addrlen = sizeof(struct in_addr);
1110 /* Process extensions. */
1111 for(p = cert.extensions.beg; p < cert.extensions.end && matched != 1;) {
1112 p = Curl_getASN1Element(&ext, p, cert.extensions.end);
1113 /* Check if extension is a subjectAlternativeName. */
1114 ext.beg = checkOID(ext.beg, ext.end, sanOID);
1116 ext.beg = Curl_getASN1Element(&elem, ext.beg, ext.end);
1117 /* Skip critical if present. */
1118 if(elem.tag == CURL_ASN1_BOOLEAN)
1119 ext.beg = Curl_getASN1Element(&elem, ext.beg, ext.end);
1120 /* Parse the octet string contents: is a single sequence. */
1121 Curl_getASN1Element(&elem, elem.beg, elem.end);
1122 /* Check all GeneralNames. */
1123 for(q = elem.beg; matched != 1 && q < elem.end;) {
1124 q = Curl_getASN1Element(&name, q, elem.end);
1126 case 2: /* DNS name. */
1127 len = utf8asn1str(&dnsname, CURL_ASN1_IA5_STRING,
1128 name.beg, name.end);
1129 if(len > 0 && (size_t)len == strlen(dnsname))
1130 matched = Curl_cert_hostcheck(dnsname, hostname);
1136 case 7: /* IP address. */
1137 matched = (size_t) (name.end - q) == addrlen &&
1138 !memcmp(&addr, q, addrlen);
1147 /* an alternative name matched the server hostname */
1148 infof(data, "\t subjectAltName: %s matched\n", dispname);
1151 /* an alternative name field existed, but didn't match and then
1153 infof(data, "\t subjectAltName does not match %s\n", dispname);
1154 return CURLE_PEER_FAILED_VERIFICATION;
1157 /* Process subject. */
1159 name.beg = name.end = "";
1160 q = cert.subject.beg;
1161 /* we have to look to the last occurrence of a commonName in the
1162 distinguished one to get the most significant one. */
1163 while(q < cert.subject.end) {
1164 q = Curl_getASN1Element(&dn, q, cert.subject.end);
1165 for(p = dn.beg; p < dn.end;) {
1166 p = Curl_getASN1Element(&elem, p, dn.end);
1167 /* We have a DN's AttributeTypeAndValue: check it in case it's a CN. */
1168 elem.beg = checkOID(elem.beg, elem.end, cnOID);
1170 name = elem; /* Latch CN. */
1174 /* Check the CN if found. */
1175 if(!Curl_getASN1Element(&elem, name.beg, name.end))
1176 failf(data, "SSL: unable to obtain common name from peer certificate");
1178 len = utf8asn1str(&dnsname, elem.tag, elem.beg, elem.end);
1181 return CURLE_OUT_OF_MEMORY;
1183 if(strlen(dnsname) != (size_t) len) /* Nul byte in string ? */
1184 failf(data, "SSL: illegal cert name field");
1185 else if(Curl_cert_hostcheck((const char *) dnsname, hostname)) {
1186 infof(data, "\t common name: %s (matched)\n", dnsname);
1191 failf(data, "SSL: certificate subject name '%s' does not match "
1192 "target host name '%s'", dnsname, dispname);
1196 return CURLE_PEER_FAILED_VERIFICATION;
1199 #endif /* USE_GSKIT */