1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2013, 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 http://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_QSOSSL) || defined(USE_GSKIT)
27 #include <curl/curl.h>
30 #include "hostcheck.h"
33 #include "inet_pton.h"
34 #include "curl_base64.h"
37 #define _MPRINTF_REPLACE /* use our functions only */
38 #include <curl/mprintf.h>
40 #include "curl_memory.h"
41 /* The last #include file should be: */
46 static const char cnOID[] = "2.5.4.3"; /* Common name. */
47 static const char sanOID[] = "2.5.29.17"; /* Subject alternative name. */
49 static const curl_OID OIDtable[] = {
50 { "1.2.840.10040.4.1", "dsa" },
51 { "1.2.840.10040.4.3", "dsa-with-sha1" },
52 { "1.2.840.10045.2.1", "ecPublicKey" },
53 { "1.2.840.10045.3.0.1", "c2pnb163v1" },
54 { "1.2.840.10045.4.1", "ecdsa-with-SHA1" },
55 { "1.2.840.10046.2.1", "dhpublicnumber" },
56 { "1.2.840.113549.1.1.1", "rsaEncryption" },
57 { "1.2.840.113549.1.1.2", "md2WithRSAEncryption" },
58 { "1.2.840.113549.1.1.4", "md5WithRSAEncryption" },
59 { "1.2.840.113549.1.1.5", "sha1WithRSAEncryption" },
60 { "1.2.840.113549.1.1.10", "RSASSA-PSS" },
61 { "1.2.840.113549.1.1.14", "sha224WithRSAEncryption" },
62 { "1.2.840.113549.1.1.11", "sha256WithRSAEncryption" },
63 { "1.2.840.113549.1.1.12", "sha384WithRSAEncryption" },
64 { "1.2.840.113549.1.1.13", "sha512WithRSAEncryption" },
65 { "1.2.840.113549.2.2", "md2" },
66 { "1.2.840.113549.2.5", "md5" },
67 { "1.3.14.3.2.26", "sha1" },
70 { "2.5.4.5", "serialNumber" },
74 { "2.5.4.9", "streetAddress" },
77 { "2.5.4.12", "title" },
78 { "2.5.4.13", "description" },
79 { "2.5.4.17", "postalCode" },
80 { "2.5.4.41", "name" },
81 { "2.5.4.42", "givenName" },
82 { "2.5.4.43", "initials" },
83 { "2.5.4.44", "generationQualifier" },
84 { "2.5.4.45", "X500UniqueIdentifier" },
85 { "2.5.4.46", "dnQualifier" },
86 { "2.5.4.65", "pseudonym" },
87 { "1.2.840.113549.1.9.1", "emailAddress" },
88 { "2.5.4.72", "role" },
89 { sanOID, "subjectAltName" },
90 { "2.5.29.18", "issuerAltName" },
91 { "2.5.29.19", "basicConstraints" },
92 { "2.16.840.1.101.3.4.2.4", "sha224" },
93 { "2.16.840.1.101.3.4.2.1", "sha256" },
94 { "2.16.840.1.101.3.4.2.2", "sha384" },
95 { "2.16.840.1.101.3.4.2.3", "sha512" },
96 { (const char *) NULL, (const char *) NULL }
100 * Lightweight ASN.1 parser.
101 * In particular, it does not check for syntactic/lexical errors.
102 * It is intended to support certificate information gathering for SSL backends
103 * that offer a mean to get certificates as a whole, but do not supply
104 * entry points to get particular certificate sub-fields.
105 * Please note there is no pretention here to rewrite a full SSL library.
109 const char * Curl_getASN1Element(curl_asn1Element * elem,
110 const char * beg, const char * end)
114 curl_asn1Element lelem;
116 /* Get a single ASN.1 element into `elem', parse ASN.1 string at `beg'
118 Returns a pointer in source string after the parsed element, or NULL
119 if an error occurs. */
121 if(beg >= end || !*beg)
122 return (const char *) NULL;
124 /* Process header byte. */
125 b = (unsigned char) *beg++;
126 elem->constructed = (b & 0x20) != 0;
127 elem->class = (b >> 6) & 3;
130 return (const char *) NULL; /* Long tag values not supported here. */
133 /* Process length. */
135 return (const char *) NULL;
136 b = (unsigned char) *beg++;
139 else if(!(b &= 0x7F)) {
140 /* Unspecified length. Since we have all the data, we can determine the
141 effective length by skipping element until an end element is found. */
142 if(!elem->constructed)
143 return (const char *) NULL;
145 while(beg < end && *beg) {
146 beg = Curl_getASN1Element(&lelem, beg, end);
148 return (const char *) NULL;
151 return (const char *) NULL;
155 else if(beg + b > end)
156 return (const char *) NULL; /* Does not fit in source. */
158 /* Get long length. */
161 if(len & 0xFF000000L)
162 return (const char *) NULL; /* Lengths > 32 bits are not supported. */
163 len = (len << 8) | (unsigned char) *beg++;
166 if((unsigned long) (end - beg) < len)
167 return (const char *) NULL; /* Element data does not fit in source. */
169 elem->end = beg + len;
173 static const curl_OID * searchOID(const char * oid)
177 /* Search the null terminated OID or OID identifier in local table.
178 Return the table entry pointer or NULL if not found. */
180 for(op = OIDtable; op->numoid; op++)
181 if(!strcmp(op->numoid, oid) || curl_strequal(op->textoid, oid))
184 return (const curl_OID *) NULL;
187 static const char * bool2str(const char * beg, const char * end)
189 /* Convert an ASN.1 Boolean value into its string representation.
190 Return the dynamically allocated string, or NULL if source is not an
191 ASN.1 Boolean value. */
194 return (const char *) NULL;
195 return strdup(*beg? "TRUE": "FALSE");
198 static const char * octet2str(const char * beg, const char * end)
200 size_t n = end - beg;
203 /* Convert an ASN.1 octet string to a printable string.
204 Return the dynamically allocated string, or NULL if an error occurs. */
206 buf = malloc(3 * n + 1);
208 for(n = 0; beg < end; n += 3)
209 snprintf(buf + n, 4, "%02x:", *(const unsigned char *) beg++);
213 static const char * bit2str(const char * beg, const char * end)
216 /* Convert an ASN.1 bit string to a printable string.
217 Return the dynamically allocated string, or NULL if an error occurs. */
220 return (const char *) NULL;
221 return octet2str(beg, end);
224 static const char * int2str(const char * beg, const char * end)
227 size_t n = end - beg;
229 /* Convert an ASN.1 integer value into its string representation.
230 Return the dynamically allocated string, or NULL if source is not an
231 ASN.1 integer value. */
234 return (const char *) NULL;
237 return octet2str(beg, end);
239 /* Represent integers <= 32-bit as a single value. */
244 val = (val << 8) | *(const unsigned char *) beg++;
246 return curl_maprintf("%s%lx", (val < 0 || val >= 10)? "0x": "", val);
250 utf8asn1str(char * * to, int type, const char * from, const char * end)
252 size_t inlength = end - from;
259 /* Perform a lazy conversion from an ASN.1 typed string to UTF8. Allocate the
260 destination buffer dynamically. The allocation size will normally be too
261 large: this is to avoid buffer overflows.
262 Terminate the string with a nul byte and return the converted
267 case CURL_ASN1_BMP_STRING:
270 case CURL_ASN1_UNIVERSAL_STRING:
273 case CURL_ASN1_NUMERIC_STRING:
274 case CURL_ASN1_PRINTABLE_STRING:
275 case CURL_ASN1_TELETEX_STRING:
276 case CURL_ASN1_IA5_STRING:
277 case CURL_ASN1_VISIBLE_STRING:
278 case CURL_ASN1_UTF8_STRING:
281 return -1; /* Conversion not supported. */
285 return -1; /* Length inconsistent with character size. */
286 buf = malloc(4 * (inlength / size) + 1);
288 return -1; /* Not enough memory. */
290 if(type == CURL_ASN1_UTF8_STRING) {
292 outlength = inlength;
294 memcpy(buf, from, outlength);
297 for(outlength = 0; from < end;) {
301 wc = (wc << 8) | *(const unsigned char *) from++;
302 wc = (wc << 8) | *(const unsigned char *) from++;
304 wc = (wc << 8) | *(const unsigned char *) from++;
305 default: /* case 1: */
306 wc = (wc << 8) | *(const unsigned char *) from++;
309 if(wc >= 0x00000080) {
310 if(wc >= 0x00000800) {
311 if(wc >= 0x00010000) {
312 if(wc >= 0x00200000) {
314 return -1; /* Invalid char. size for target encoding. */
316 buf[outlength + 3] = (char) (0x80 | (wc & 0x3F));
317 wc = (wc >> 6) | 0x00010000;
320 buf[outlength + 2] = (char) (0x80 | (wc & 0x3F));
321 wc = (wc >> 6) | 0x00000800;
324 buf[outlength + 1] = (char) (0x80 | (wc & 0x3F));
325 wc = (wc >> 6) | 0x000000C0;
328 buf[outlength] = (char) wc;
332 buf[outlength] = '\0';
337 static const char * string2str(int type, const char * beg, const char * end)
341 /* Convert an ASN.1 String into its UTF-8 string representation.
342 Return the dynamically allocated string, or NULL if an error occurs. */
344 if(utf8asn1str(&buf, type, beg, end) < 0)
345 return (const char *) NULL;
349 static int encodeUint(char * buf, int n, unsigned int x)
352 unsigned int y = x / 10;
354 /* Decimal ASCII encode unsigned integer `x' in the `n'-byte buffer at `buf'.
355 Return the total number of encoded digits, even if larger than `n'. */
358 i += encodeUint(buf, n, y);
362 buf[i] = (char) ('0' + x);
365 buf[i] = '\0'; /* Store a terminator if possible. */
369 static int encodeOID(char * buf, int n, const char * beg, const char * end)
375 /* Convert an ASN.1 OID into its dotted string representation.
376 Store the result in th `n'-byte buffer at `buf'.
377 Return the converted string length, or -1 if an error occurs. */
379 /* Process the first two numbers. */
380 y = *(const unsigned char *) beg++;
383 i += encodeUint(buf + i, n - i, x);
387 i += encodeUint(buf + i, n - i, y);
389 /* Process the trailing numbers. */
398 y = *(const unsigned char *) beg++;
399 x = (x << 7) | (y & 0x7F);
401 i += encodeUint(buf + i, n - i, x);
408 static const char * OID2str(const char * beg, const char * end, bool symbolic)
410 char * buf = (char *) NULL;
414 /* Convert an ASN.1 OID into its dotted or symbolic string representation.
415 Return the dynamically allocated string, or NULL if an error occurs. */
418 n = encodeOID((char *) NULL, -1, beg, end);
422 encodeOID(buf, n, beg, end);
429 buf = strdup(op->textoid);
438 static const char * GTime2str(const char * beg, const char * end)
445 const char * sep = "";
447 /* Convert an ASN.1 Generalized time to a printable string.
448 Return the dynamically allocated string, or NULL if an error occurs. */
450 for(fracp = beg; fracp < end && *fracp >= '0' && *fracp <= '9'; fracp++)
453 /* Get seconds digits. */
455 switch (fracp - beg - 12) {
465 return (const char *) NULL;
468 /* Scan for timezone, measure fractional seconds. */
471 if(fracp < end && (*fracp == '.' || *fracp == ',')) {
475 while(tzp < end && *tzp >= '0' && *tzp <= '9');
476 /* Strip leading zeroes in fractional seconds. */
477 for(fracl = tzp - fracp - 1; fracl && fracp[fracl - 1] == '0'; fracl--)
481 /* Process timezone. */
483 ; /* Nothing to do. */
484 else if(*tzp == 'Z') {
494 return curl_maprintf("%.4s-%.2s-%.2s %.2s:%.2s:%c%c%s%.*s%s%.*s",
495 beg, beg + 4, beg + 6,
496 beg + 8, beg + 10, sec1, sec2,
497 fracl? ".": "", fracl, fracp,
501 static const char * UTime2str(const char * beg, const char * end)
507 /* Convert an ASN.1 UTC time to a printable string.
508 Return the dynamically allocated string, or NULL if an error occurs. */
510 for(tzp = beg; tzp < end && *tzp >= '0' && *tzp <= '9'; tzp++)
512 /* Get the seconds. */
520 return (const char *) NULL;
523 /* Process timezone. */
525 return (const char *) NULL;
534 return curl_maprintf("%u%.2s-%.2s-%.2s %.2s:%.2s:%.2s %.*s",
535 20 - (*beg >= '5'), beg, beg + 2, beg + 4,
536 beg + 6, beg + 8, sec,
540 const char * Curl_ASN1tostr(curl_asn1Element * elem, int type)
542 static const char zero = '\0';
544 /* Convert an ASN.1 element to a printable string.
545 Return the dynamically allocated string, or NULL if an error occurs. */
547 if(elem->constructed)
548 return (const char *) NULL; /* No conversion of structured elements. */
551 type = elem->tag; /* Type not forced: use element tag as type. */
554 case CURL_ASN1_BOOLEAN:
555 return bool2str(elem->beg, elem->end);
556 case CURL_ASN1_INTEGER:
557 case CURL_ASN1_ENUMERATED:
558 return int2str(elem->beg, elem->end);
559 case CURL_ASN1_BIT_STRING:
560 return bit2str(elem->beg, elem->end);
561 case CURL_ASN1_OCTET_STRING:
562 return octet2str(elem->beg, elem->end);
564 return strdup(&zero);
565 case CURL_ASN1_OBJECT_IDENTIFIER:
566 return OID2str(elem->beg, elem->end, TRUE);
567 case CURL_ASN1_UTC_TIME:
568 return UTime2str(elem->beg, elem->end);
569 case CURL_ASN1_GENERALIZED_TIME:
570 return GTime2str(elem->beg, elem->end);
571 case CURL_ASN1_UTF8_STRING:
572 case CURL_ASN1_NUMERIC_STRING:
573 case CURL_ASN1_PRINTABLE_STRING:
574 case CURL_ASN1_TELETEX_STRING:
575 case CURL_ASN1_IA5_STRING:
576 case CURL_ASN1_VISIBLE_STRING:
577 case CURL_ASN1_UNIVERSAL_STRING:
578 case CURL_ASN1_BMP_STRING:
579 return string2str(type, elem->beg, elem->end);
582 return (const char *) NULL; /* Unsupported. */
585 static ssize_t encodeDN(char * buf, size_t n, curl_asn1Element * dn)
587 curl_asn1Element rdn;
588 curl_asn1Element atv;
589 curl_asn1Element oid;
590 curl_asn1Element value;
597 /* ASCII encode distinguished name at `dn' into the `n'-byte buffer at `buf'.
598 Return the total string length, even if larger than `n'. */
600 for(p1 = dn->beg; p1 < dn->end;) {
601 p1 = Curl_getASN1Element(&rdn, p1, dn->end);
602 for(p2 = rdn.beg; p2 < rdn.end;) {
603 p2 = Curl_getASN1Element(&atv, p2, rdn.end);
604 p3 = Curl_getASN1Element(&oid, atv.beg, atv.end);
605 Curl_getASN1Element(&value, p3, atv.end);
606 str = Curl_ASN1tostr(&oid, 0);
611 If attribute has a short uppercase name, delimiter is ", ". */
613 for(p3 = str; isupper(*p3); p3++)
615 for(p3 = (*p3 || p3 - str > 2)? "/": ", "; *p3; p3++) {
622 /* Encode attribute name. */
623 for(p3 = str; *p3; p3++) {
630 /* Generate equal sign. */
635 /* Generate value. */
636 str = Curl_ASN1tostr(&value, 0);
639 for(p3 = str; *p3; p3++) {
651 const char * Curl_DNtostr(curl_asn1Element * dn)
653 char * buf = (char *) NULL;
654 ssize_t n = encodeDN(buf, 0, dn);
656 /* Convert an ASN.1 distinguished name into a printable string.
657 Return the dynamically allocated string, or NULL if an error occurs. */
662 encodeDN(buf, n + 1, dn);
666 return (const char *) buf;
669 static const char * checkOID(const char * beg, const char * end,
677 /* Check if first ASN.1 element at `beg' is the given OID.
678 Return a pointer in the source after the OID if found, else NULL. */
680 ccp = Curl_getASN1Element(&e, beg, end);
681 if(!ccp || e.tag != CURL_ASN1_OBJECT_IDENTIFIER)
682 return (const char *) NULL;
684 p = OID2str(e.beg, e.end, FALSE);
686 return (const char *) NULL;
688 matched = !strcmp(p, oid);
690 return matched? ccp: (const char *) NULL;
698 void Curl_parseX509(curl_X509certificate * cert,
699 const char * beg, const char * end)
701 curl_asn1Element elem;
702 curl_asn1Element tbsCertificate;
704 static const char defaultVersion = 0; /* v1. */
706 /* ASN.1 parse an X509 certificate into structure subfields.
707 Syntax is assumed to have already been checked by the SSL backend.
710 cert->certificate.beg = beg;
711 cert->certificate.end = end;
713 /* Get the sequence content. */
714 Curl_getASN1Element(&elem, beg, end);
718 /* Get tbsCertificate. */
719 beg = Curl_getASN1Element(&tbsCertificate, beg, end);
720 /* Skip the signatureAlgorithm. */
721 beg = Curl_getASN1Element(&cert->signatureAlgorithm, beg, end);
722 /* Get the signatureValue. */
723 Curl_getASN1Element(&cert->signature, beg, end);
725 /* Parse TBSCertificate. */
726 beg = tbsCertificate.beg;
727 end = tbsCertificate.end;
728 /* Get optional version, get serialNumber. */
729 cert->version.beg = &defaultVersion;
730 cert->version.end = &defaultVersion + sizeof defaultVersion;;
731 beg = Curl_getASN1Element(&elem, beg, end);
733 Curl_getASN1Element(&cert->version, elem.beg, elem.end);
734 beg = Curl_getASN1Element(&elem, beg, end);
736 cert->serialNumber = elem;
737 /* Get signature algorithm. */
738 beg = Curl_getASN1Element(&cert->signatureAlgorithm, beg, end);
740 beg = Curl_getASN1Element(&cert->issuer, beg, end);
741 /* Get notBefore and notAfter. */
742 beg = Curl_getASN1Element(&elem, beg, end);
743 ccp = Curl_getASN1Element(&cert->notBefore, elem.beg, elem.end);
744 Curl_getASN1Element(&cert->notAfter, ccp, elem.end);
746 beg = Curl_getASN1Element(&cert->subject, beg, end);
747 /* Get subjectPublicKeyAlgorithm and subjectPublicKey. */
748 beg = Curl_getASN1Element(&elem, beg, end);
749 ccp = Curl_getASN1Element(&cert->subjectPublicKeyAlgorithm,
751 Curl_getASN1Element(&cert->subjectPublicKey, ccp, elem.end);
752 /* Get optional issuerUiqueID, subjectUniqueID and extensions. */
753 cert->issuerUniqueID.tag = cert->subjectUniqueID.tag = 0;
754 cert->extensions.tag = elem.tag = 0;
755 cert->issuerUniqueID.beg = cert->issuerUniqueID.end = "";
756 cert->subjectUniqueID.beg = cert->subjectUniqueID.end = "";
757 cert->extensions.beg = cert->extensions.end = "";
759 beg = Curl_getASN1Element(&elem, beg, end);
761 cert->issuerUniqueID = elem;
763 beg = Curl_getASN1Element(&elem, beg, end);
766 cert->subjectUniqueID = elem;
768 beg = Curl_getASN1Element(&elem, beg, end);
771 Curl_getASN1Element(&cert->extensions, elem.beg, elem.end);
774 static size_t copySubstring(char * to, const char * from)
778 /* Copy at most 64-characters, terminate with a newline and returns the
779 effective number of stored characters. */
781 for(i = 0; i < 64; i++) {
791 static const char * dumpAlgo(curl_asn1Element * param,
792 const char * beg, const char * end)
794 curl_asn1Element oid;
796 /* Get algorithm parameters and return algorithm name. */
798 beg = Curl_getASN1Element(&oid, beg, end);
800 param->beg = param->end = end;
802 Curl_getASN1Element(param, beg, end);
803 return OID2str(oid.beg, oid.end, TRUE);
806 static void do_pubkey_field(struct SessionHandle *data, int certnum,
807 const char * label, curl_asn1Element * elem)
811 /* Generate a certificate information record for the public key. */
813 output = Curl_ASN1tostr(elem, 0);
815 Curl_ssl_push_certinfo(data, certnum, label, output);
816 infof(data, " %s: %s\n", label, output);
817 free((char *) output);
821 static void do_pubkey(struct SessionHandle * data, int certnum,
822 const char * algo, curl_asn1Element * param,
823 curl_asn1Element * pubkey)
825 curl_asn1Element elem;
832 /* Generate all information records for the public key. */
834 /* Get the public key (single element). */
835 Curl_getASN1Element(&pk, pubkey->beg + 1, pubkey->end);
837 if(curl_strequal(algo, "rsaEncryption")) {
838 p = Curl_getASN1Element(&elem, pk.beg, pk.end);
839 /* Compute key length. */
840 for(q = elem.beg; !*q && q < elem.end; q++)
842 len = (elem.end - q) * 8;
844 for(i = *(unsigned char *) q; !(i & 0x80); i <<= 1)
847 elem.beg = q; /* Strip leading zero bytes. */
848 infof(data, " RSA Public Key (%lu bits)\n", len);
849 q = curl_maprintf("%lu", len);
851 Curl_ssl_push_certinfo(data, certnum, "RSA Public Key", q);
854 /* Generate coefficients. */
855 do_pubkey_field(data, certnum, "rsa(n)", &elem);
856 Curl_getASN1Element(&elem, p, pk.end);
857 do_pubkey_field(data, certnum, "rsa(e)", &elem);
859 else if(curl_strequal(algo, "dsa")) {
860 p = Curl_getASN1Element(&elem, param->beg, param->end);
861 do_pubkey_field(data, certnum, "dsa(p)", &elem);
862 p = Curl_getASN1Element(&elem, p, param->end);
863 do_pubkey_field(data, certnum, "dsa(q)", &elem);
864 Curl_getASN1Element(&elem, p, param->end);
865 do_pubkey_field(data, certnum, "dsa(g)", &elem);
866 do_pubkey_field(data, certnum, "dsa(pub_key)", &pk);
868 else if(curl_strequal(algo, "dhpublicnumber")) {
869 p = Curl_getASN1Element(&elem, param->beg, param->end);
870 do_pubkey_field(data, certnum, "dh(p)", &elem);
871 Curl_getASN1Element(&elem, param->beg, param->end);
872 do_pubkey_field(data, certnum, "dh(g)", &elem);
873 do_pubkey_field(data, certnum, "dh(pub_key)", &pk);
875 #if 0 /* Patent-encumbered. */
876 else if(curl_strequal(algo, "ecPublicKey")) {
882 CURLcode Curl_extract_certinfo(struct connectdata * conn,
887 curl_X509certificate cert;
888 struct SessionHandle * data = conn->data;
889 curl_asn1Element param;
895 unsigned long version;
899 /* Prepare the certificate information for curl_easy_getinfo(). */
901 /* Extract the certificate ASN.1 elements. */
902 Curl_parseX509(&cert, beg, end);
905 ccp = Curl_DNtostr(&cert.subject);
907 return CURLE_OUT_OF_MEMORY;
908 Curl_ssl_push_certinfo(data, certnum, "Subject", ccp);
909 infof(data, "%2d Subject: %s\n", certnum, ccp);
913 ccp = Curl_DNtostr(&cert.issuer);
915 return CURLE_OUT_OF_MEMORY;
916 Curl_ssl_push_certinfo(data, certnum, "Issuer", ccp);
917 infof(data, " Issuer: %s\n", ccp);
920 /* Version (always fits in less than 32 bits). */
922 for(ccp = cert.version.beg; ccp < cert.version.end; ccp++)
923 version = (version << 8) | *(const unsigned char *) ccp;
924 ccp = curl_maprintf("%lx", version);
926 return CURLE_OUT_OF_MEMORY;
927 Curl_ssl_push_certinfo(data, certnum, "Version", ccp);
929 infof(data, " Version: %lu (0x%lx)\n", version + 1, version);
932 ccp = Curl_ASN1tostr(&cert.serialNumber, 0);
934 return CURLE_OUT_OF_MEMORY;
935 Curl_ssl_push_certinfo(data, certnum, "Serial Number", ccp);
936 infof(data, " Serial Number: %s\n", ccp);
939 /* Signature algorithm .*/
940 ccp = dumpAlgo(¶m, cert.signatureAlgorithm.beg,
941 cert.signatureAlgorithm.end);
943 return CURLE_OUT_OF_MEMORY;
944 Curl_ssl_push_certinfo(data, certnum, "Signature Algorithm", ccp);
945 infof(data, " Signature Algorithm: %s\n", ccp);
949 ccp = Curl_ASN1tostr(&cert.notBefore, 0);
951 return CURLE_OUT_OF_MEMORY;
952 Curl_ssl_push_certinfo(data, certnum, "Start Date", ccp);
953 infof(data, " Start Date: %s\n", ccp);
957 ccp = Curl_ASN1tostr(&cert.notAfter, 0);
959 return CURLE_OUT_OF_MEMORY;
960 Curl_ssl_push_certinfo(data, certnum, "Expire Date", ccp);
961 infof(data, " Expire Date: %s\n", ccp);
964 /* Public Key Algorithm. */
965 ccp = dumpAlgo(¶m, cert.subjectPublicKeyAlgorithm.beg,
966 cert.subjectPublicKeyAlgorithm.end);
968 return CURLE_OUT_OF_MEMORY;
969 Curl_ssl_push_certinfo(data, certnum, "Public Key Algorithm", ccp);
970 infof(data, " Public Key Algorithm: %s\n", ccp);
971 do_pubkey(data, certnum, ccp, ¶m, &cert.subjectPublicKey);
974 /* TODO: extensions. */
977 ccp = Curl_ASN1tostr(&cert.signature, 0);
979 return CURLE_OUT_OF_MEMORY;
980 Curl_ssl_push_certinfo(data, certnum, "Signature", ccp);
981 infof(data, " Signature: %s\n", ccp);
984 /* Generate PEM certificate. */
985 cc = Curl_base64_encode(data, cert.certificate.beg,
986 cert.certificate.end - cert.certificate.beg,
990 /* Compute the number of charaters in final certificate string. Format is:
991 -----BEGIN CERTIFICATE-----\n
992 <max 64 base64 characters>\n
996 -----END CERTIFICATE-----\n
998 i = 28 + cl1 + (cl1 + 64 - 1) / 64 + 26;
1002 return CURLE_OUT_OF_MEMORY;
1004 /* Build the certificate string. */
1005 i = copySubstring(cp2, "-----BEGIN CERTIFICATE-----");
1006 for(j = 0; j < cl1; j += 64)
1007 i += copySubstring(cp2 + i, cp1 + j);
1008 i += copySubstring(cp2 + i, "-----END CERTIFICATE-----");
1011 Curl_ssl_push_certinfo(data, certnum, "Cert", cp2);
1012 infof(data, "%s\n", cp2);
1018 CURLcode Curl_verifyhost(struct connectdata * conn,
1019 const char * beg, const char * end)
1021 struct SessionHandle * data = conn->data;
1022 curl_X509certificate cert;
1023 curl_asn1Element dn;
1024 curl_asn1Element elem;
1025 curl_asn1Element ext;
1026 curl_asn1Element name;
1032 size_t addrlen = (size_t) -1;
1035 struct in6_addr addr;
1037 struct in_addr addr;
1040 /* Verify that connection server matches info in X509 certificate at
1043 if(!data->set.ssl.verifyhost)
1047 return CURLE_PEER_FAILED_VERIFICATION;
1048 Curl_parseX509(&cert, beg, end);
1050 /* Get the server IP address. */
1052 if(conn->bits.ipv6_ip && Curl_inet_pton(AF_INET6, conn->host.name, &addr))
1053 addrlen = sizeof(struct in6_addr);
1056 if(Curl_inet_pton(AF_INET, conn->host.name, &addr))
1057 addrlen = sizeof(struct in_addr);
1059 /* Process extensions. */
1060 for(p = cert.extensions.beg; p < cert.extensions.end && matched != 1;) {
1061 p = Curl_getASN1Element(&ext, p, cert.extensions.end);
1062 /* Check if extension is a subjectAlternativeName. */
1063 ext.beg = checkOID(ext.beg, ext.end, sanOID);
1065 ext.beg = Curl_getASN1Element(&elem, ext.beg, ext.end);
1066 /* Skip critical if present. */
1067 if(elem.tag == CURL_ASN1_BOOLEAN)
1068 ext.beg = Curl_getASN1Element(&elem, ext.beg, ext.end);
1069 /* Parse the octet string contents: is a single sequence. */
1070 Curl_getASN1Element(&elem, elem.beg, elem.end);
1071 /* Check all GeneralNames. */
1072 for(q = elem.beg; matched != 1 && q < elem.end;) {
1073 q = Curl_getASN1Element(&name, q, elem.end);
1075 case 2: /* DNS name. */
1077 len = utf8asn1str(&dnsname, CURL_ASN1_IA5_STRING,
1078 name.beg, name.end);
1080 if(strlen(dnsname) == (size_t) len)
1081 i = Curl_cert_hostcheck((const char *) dnsname, conn->host.name);
1085 return CURLE_PEER_FAILED_VERIFICATION;
1089 case 7: /* IP address. */
1090 matched = (size_t) (name.end - q) == addrlen &&
1091 !memcmp(&addr, q, addrlen);
1100 /* an alternative name matched the server hostname */
1101 infof(data, "\t subjectAltName: %s matched\n", conn->host.dispname);
1104 /* an alternative name field existed, but didn't match and then
1106 infof(data, "\t subjectAltName does not match %s\n", conn->host.dispname);
1107 return CURLE_PEER_FAILED_VERIFICATION;
1110 /* Process subject. */
1111 name.beg = name.end = "";
1112 q = cert.subject.beg;
1113 /* we have to look to the last occurrence of a commonName in the
1114 distinguished one to get the most significant one. */
1115 while(q < cert.subject.end) {
1116 q = Curl_getASN1Element(&dn, q, cert.subject.end);
1117 for(p = dn.beg; p < dn.end;) {
1118 p = Curl_getASN1Element(&elem, p, dn.end);
1119 /* We have a DN's AttributeTypeAndValue: check it in case it's a CN. */
1120 elem.beg = checkOID(elem.beg, elem.end, cnOID);
1122 name = elem; /* Latch CN. */
1126 /* Check the CN if found. */
1127 if(!Curl_getASN1Element(&elem, name.beg, name.end))
1128 failf(data, "SSL: unable to obtain common name from peer certificate");
1130 len = utf8asn1str(&dnsname, elem.tag, elem.beg, elem.end);
1133 return CURLE_OUT_OF_MEMORY;
1135 if(strlen(dnsname) != (size_t) len) /* Nul byte in string ? */
1136 failf(data, "SSL: illegal cert name field");
1137 else if(Curl_cert_hostcheck((const char *) dnsname, conn->host.name)) {
1138 infof(data, "\t common name: %s (matched)\n", dnsname);
1143 failf(data, "SSL: certificate subject name '%s' does not match "
1144 "target host name '%s'", dnsname, conn->host.dispname);
1148 return CURLE_PEER_FAILED_VERIFICATION;
1151 #endif /* USE_QSOSSL or USE_GSKIT */