1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2014, 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) || defined(USE_NSS)
27 #include <curl/curl.h>
30 #include "hostcheck.h"
31 #include "vtls/vtls.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;
329 outlength += charsize;
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;
673 void Curl_parseX509(curl_X509certificate * cert,
674 const char * beg, const char * end)
676 curl_asn1Element elem;
677 curl_asn1Element tbsCertificate;
679 static const char defaultVersion = 0; /* v1. */
681 /* ASN.1 parse an X509 certificate into structure subfields.
682 Syntax is assumed to have already been checked by the SSL backend.
685 cert->certificate.beg = beg;
686 cert->certificate.end = end;
688 /* Get the sequence content. */
689 Curl_getASN1Element(&elem, beg, end);
693 /* Get tbsCertificate. */
694 beg = Curl_getASN1Element(&tbsCertificate, beg, end);
695 /* Skip the signatureAlgorithm. */
696 beg = Curl_getASN1Element(&cert->signatureAlgorithm, beg, end);
697 /* Get the signatureValue. */
698 Curl_getASN1Element(&cert->signature, beg, end);
700 /* Parse TBSCertificate. */
701 beg = tbsCertificate.beg;
702 end = tbsCertificate.end;
703 /* Get optional version, get serialNumber. */
704 cert->version.beg = &defaultVersion;
705 cert->version.end = &defaultVersion + sizeof defaultVersion;;
706 beg = Curl_getASN1Element(&elem, beg, end);
708 Curl_getASN1Element(&cert->version, elem.beg, elem.end);
709 beg = Curl_getASN1Element(&elem, beg, end);
711 cert->serialNumber = elem;
712 /* Get signature algorithm. */
713 beg = Curl_getASN1Element(&cert->signatureAlgorithm, beg, end);
715 beg = Curl_getASN1Element(&cert->issuer, beg, end);
716 /* Get notBefore and notAfter. */
717 beg = Curl_getASN1Element(&elem, beg, end);
718 ccp = Curl_getASN1Element(&cert->notBefore, elem.beg, elem.end);
719 Curl_getASN1Element(&cert->notAfter, ccp, elem.end);
721 beg = Curl_getASN1Element(&cert->subject, beg, end);
722 /* Get subjectPublicKeyAlgorithm and subjectPublicKey. */
723 beg = Curl_getASN1Element(&elem, beg, end);
724 ccp = Curl_getASN1Element(&cert->subjectPublicKeyAlgorithm,
726 Curl_getASN1Element(&cert->subjectPublicKey, ccp, elem.end);
727 /* Get optional issuerUiqueID, subjectUniqueID and extensions. */
728 cert->issuerUniqueID.tag = cert->subjectUniqueID.tag = 0;
729 cert->extensions.tag = elem.tag = 0;
730 cert->issuerUniqueID.beg = cert->issuerUniqueID.end = "";
731 cert->subjectUniqueID.beg = cert->subjectUniqueID.end = "";
732 cert->extensions.beg = cert->extensions.end = "";
734 beg = Curl_getASN1Element(&elem, beg, end);
736 cert->issuerUniqueID = elem;
738 beg = Curl_getASN1Element(&elem, beg, end);
741 cert->subjectUniqueID = elem;
743 beg = Curl_getASN1Element(&elem, beg, end);
746 Curl_getASN1Element(&cert->extensions, elem.beg, elem.end);
749 static size_t copySubstring(char * to, const char * from)
753 /* Copy at most 64-characters, terminate with a newline and returns the
754 effective number of stored characters. */
756 for(i = 0; i < 64; i++) {
766 static const char * dumpAlgo(curl_asn1Element * param,
767 const char * beg, const char * end)
769 curl_asn1Element oid;
771 /* Get algorithm parameters and return algorithm name. */
773 beg = Curl_getASN1Element(&oid, beg, end);
775 param->beg = param->end = end;
777 Curl_getASN1Element(param, beg, end);
778 return OID2str(oid.beg, oid.end, TRUE);
781 static void do_pubkey_field(struct SessionHandle * data, int certnum,
782 const char * label, curl_asn1Element * elem)
786 /* Generate a certificate information record for the public key. */
788 output = Curl_ASN1tostr(elem, 0);
790 if(data->set.ssl.certinfo)
791 Curl_ssl_push_certinfo(data, certnum, label, output);
793 infof(data, " %s: %s\n", label, output);
794 free((char *) output);
798 static void do_pubkey(struct SessionHandle * data, int certnum,
799 const char * algo, curl_asn1Element * param,
800 curl_asn1Element * pubkey)
802 curl_asn1Element elem;
809 /* Generate all information records for the public key. */
811 /* Get the public key (single element). */
812 Curl_getASN1Element(&pk, pubkey->beg + 1, pubkey->end);
814 if(curl_strequal(algo, "rsaEncryption")) {
815 p = Curl_getASN1Element(&elem, pk.beg, pk.end);
816 /* Compute key length. */
817 for(q = elem.beg; !*q && q < elem.end; q++)
819 len = (elem.end - q) * 8;
821 for(i = *(unsigned char *) q; !(i & 0x80); i <<= 1)
824 elem.beg = q; /* Strip leading zero bytes. */
826 infof(data, " RSA Public Key (%lu bits)\n", len);
827 if(data->set.ssl.certinfo) {
828 q = curl_maprintf("%lu", len);
830 Curl_ssl_push_certinfo(data, certnum, "RSA Public Key", q);
834 /* Generate coefficients. */
835 do_pubkey_field(data, certnum, "rsa(n)", &elem);
836 Curl_getASN1Element(&elem, p, pk.end);
837 do_pubkey_field(data, certnum, "rsa(e)", &elem);
839 else if(curl_strequal(algo, "dsa")) {
840 p = Curl_getASN1Element(&elem, param->beg, param->end);
841 do_pubkey_field(data, certnum, "dsa(p)", &elem);
842 p = Curl_getASN1Element(&elem, p, param->end);
843 do_pubkey_field(data, certnum, "dsa(q)", &elem);
844 Curl_getASN1Element(&elem, p, param->end);
845 do_pubkey_field(data, certnum, "dsa(g)", &elem);
846 do_pubkey_field(data, certnum, "dsa(pub_key)", &pk);
848 else if(curl_strequal(algo, "dhpublicnumber")) {
849 p = Curl_getASN1Element(&elem, param->beg, param->end);
850 do_pubkey_field(data, certnum, "dh(p)", &elem);
851 Curl_getASN1Element(&elem, param->beg, param->end);
852 do_pubkey_field(data, certnum, "dh(g)", &elem);
853 do_pubkey_field(data, certnum, "dh(pub_key)", &pk);
855 #if 0 /* Patent-encumbered. */
856 else if(curl_strequal(algo, "ecPublicKey")) {
862 CURLcode Curl_extract_certinfo(struct connectdata * conn,
867 curl_X509certificate cert;
868 struct SessionHandle * data = conn->data;
869 curl_asn1Element param;
875 unsigned long version;
879 if(!data->set.ssl.certinfo)
883 /* Prepare the certificate information for curl_easy_getinfo(). */
885 /* Extract the certificate ASN.1 elements. */
886 Curl_parseX509(&cert, beg, end);
889 ccp = Curl_DNtostr(&cert.subject);
891 return CURLE_OUT_OF_MEMORY;
892 if(data->set.ssl.certinfo)
893 Curl_ssl_push_certinfo(data, certnum, "Subject", ccp);
895 infof(data, "%2d Subject: %s\n", certnum, ccp);
899 ccp = Curl_DNtostr(&cert.issuer);
901 return CURLE_OUT_OF_MEMORY;
902 if(data->set.ssl.certinfo)
903 Curl_ssl_push_certinfo(data, certnum, "Issuer", ccp);
905 infof(data, " Issuer: %s\n", ccp);
908 /* Version (always fits in less than 32 bits). */
910 for(ccp = cert.version.beg; ccp < cert.version.end; ccp++)
911 version = (version << 8) | *(const unsigned char *) ccp;
912 if(data->set.ssl.certinfo) {
913 ccp = curl_maprintf("%lx", version);
915 return CURLE_OUT_OF_MEMORY;
916 Curl_ssl_push_certinfo(data, certnum, "Version", ccp);
920 infof(data, " Version: %lu (0x%lx)\n", version + 1, version);
923 ccp = Curl_ASN1tostr(&cert.serialNumber, 0);
925 return CURLE_OUT_OF_MEMORY;
926 if(data->set.ssl.certinfo)
927 Curl_ssl_push_certinfo(data, certnum, "Serial Number", ccp);
929 infof(data, " Serial Number: %s\n", ccp);
932 /* Signature algorithm .*/
933 ccp = dumpAlgo(¶m, cert.signatureAlgorithm.beg,
934 cert.signatureAlgorithm.end);
936 return CURLE_OUT_OF_MEMORY;
937 if(data->set.ssl.certinfo)
938 Curl_ssl_push_certinfo(data, certnum, "Signature Algorithm", ccp);
940 infof(data, " Signature Algorithm: %s\n", ccp);
944 ccp = Curl_ASN1tostr(&cert.notBefore, 0);
946 return CURLE_OUT_OF_MEMORY;
947 if(data->set.ssl.certinfo)
948 Curl_ssl_push_certinfo(data, certnum, "Start Date", ccp);
950 infof(data, " Start Date: %s\n", ccp);
954 ccp = Curl_ASN1tostr(&cert.notAfter, 0);
956 return CURLE_OUT_OF_MEMORY;
957 if(data->set.ssl.certinfo)
958 Curl_ssl_push_certinfo(data, certnum, "Expire Date", ccp);
960 infof(data, " Expire Date: %s\n", ccp);
963 /* Public Key Algorithm. */
964 ccp = dumpAlgo(¶m, cert.subjectPublicKeyAlgorithm.beg,
965 cert.subjectPublicKeyAlgorithm.end);
967 return CURLE_OUT_OF_MEMORY;
968 if(data->set.ssl.certinfo)
969 Curl_ssl_push_certinfo(data, certnum, "Public Key Algorithm", ccp);
971 infof(data, " Public Key Algorithm: %s\n", ccp);
972 do_pubkey(data, certnum, ccp, ¶m, &cert.subjectPublicKey);
975 /* TODO: extensions. */
978 ccp = Curl_ASN1tostr(&cert.signature, 0);
980 return CURLE_OUT_OF_MEMORY;
981 if(data->set.ssl.certinfo)
982 Curl_ssl_push_certinfo(data, certnum, "Signature", ccp);
984 infof(data, " Signature: %s\n", ccp);
987 /* Generate PEM certificate. */
988 cc = Curl_base64_encode(data, cert.certificate.beg,
989 cert.certificate.end - cert.certificate.beg,
993 /* Compute the number of characters in final certificate string. Format is:
994 -----BEGIN CERTIFICATE-----\n
995 <max 64 base64 characters>\n
999 -----END CERTIFICATE-----\n
1001 i = 28 + cl1 + (cl1 + 64 - 1) / 64 + 26;
1002 cp2 = malloc(i + 1);
1005 return CURLE_OUT_OF_MEMORY;
1007 /* Build the certificate string. */
1008 i = copySubstring(cp2, "-----BEGIN CERTIFICATE-----");
1009 for(j = 0; j < cl1; j += 64)
1010 i += copySubstring(cp2 + i, cp1 + j);
1011 i += copySubstring(cp2 + i, "-----END CERTIFICATE-----");
1014 if(data->set.ssl.certinfo)
1015 Curl_ssl_push_certinfo(data, certnum, "Cert", cp2);
1017 infof(data, "%s\n", cp2);
1022 #endif /* USE_QSOSSL or USE_GSKIT or USE_NSS */
1024 #if defined(USE_QSOSSL) || defined(USE_GSKIT)
1026 static const char * checkOID(const char * beg, const char * end,
1034 /* Check if first ASN.1 element at `beg' is the given OID.
1035 Return a pointer in the source after the OID if found, else NULL. */
1037 ccp = Curl_getASN1Element(&e, beg, end);
1038 if(!ccp || e.tag != CURL_ASN1_OBJECT_IDENTIFIER)
1039 return (const char *) NULL;
1041 p = OID2str(e.beg, e.end, FALSE);
1043 return (const char *) NULL;
1045 matched = !strcmp(p, oid);
1047 return matched? ccp: (const char *) NULL;
1050 CURLcode Curl_verifyhost(struct connectdata * conn,
1051 const char * beg, const char * end)
1053 struct SessionHandle * data = conn->data;
1054 curl_X509certificate cert;
1055 curl_asn1Element dn;
1056 curl_asn1Element elem;
1057 curl_asn1Element ext;
1058 curl_asn1Element name;
1064 size_t addrlen = (size_t) -1;
1067 struct in6_addr addr;
1069 struct in_addr addr;
1072 /* Verify that connection server matches info in X509 certificate at
1075 if(!data->set.ssl.verifyhost)
1079 return CURLE_PEER_FAILED_VERIFICATION;
1080 Curl_parseX509(&cert, beg, end);
1082 /* Get the server IP address. */
1084 if(conn->bits.ipv6_ip && Curl_inet_pton(AF_INET6, conn->host.name, &addr))
1085 addrlen = sizeof(struct in6_addr);
1088 if(Curl_inet_pton(AF_INET, conn->host.name, &addr))
1089 addrlen = sizeof(struct in_addr);
1091 /* Process extensions. */
1092 for(p = cert.extensions.beg; p < cert.extensions.end && matched != 1;) {
1093 p = Curl_getASN1Element(&ext, p, cert.extensions.end);
1094 /* Check if extension is a subjectAlternativeName. */
1095 ext.beg = checkOID(ext.beg, ext.end, sanOID);
1097 ext.beg = Curl_getASN1Element(&elem, ext.beg, ext.end);
1098 /* Skip critical if present. */
1099 if(elem.tag == CURL_ASN1_BOOLEAN)
1100 ext.beg = Curl_getASN1Element(&elem, ext.beg, ext.end);
1101 /* Parse the octet string contents: is a single sequence. */
1102 Curl_getASN1Element(&elem, elem.beg, elem.end);
1103 /* Check all GeneralNames. */
1104 for(q = elem.beg; matched != 1 && q < elem.end;) {
1105 q = Curl_getASN1Element(&name, q, elem.end);
1107 case 2: /* DNS name. */
1109 len = utf8asn1str(&dnsname, CURL_ASN1_IA5_STRING,
1110 name.beg, name.end);
1112 if(strlen(dnsname) == (size_t) len)
1113 i = Curl_cert_hostcheck((const char *) dnsname, conn->host.name);
1117 return CURLE_PEER_FAILED_VERIFICATION;
1121 case 7: /* IP address. */
1122 matched = (size_t) (name.end - q) == addrlen &&
1123 !memcmp(&addr, q, addrlen);
1132 /* an alternative name matched the server hostname */
1133 infof(data, "\t subjectAltName: %s matched\n", conn->host.dispname);
1136 /* an alternative name field existed, but didn't match and then
1138 infof(data, "\t subjectAltName does not match %s\n", conn->host.dispname);
1139 return CURLE_PEER_FAILED_VERIFICATION;
1142 /* Process subject. */
1143 name.beg = name.end = "";
1144 q = cert.subject.beg;
1145 /* we have to look to the last occurrence of a commonName in the
1146 distinguished one to get the most significant one. */
1147 while(q < cert.subject.end) {
1148 q = Curl_getASN1Element(&dn, q, cert.subject.end);
1149 for(p = dn.beg; p < dn.end;) {
1150 p = Curl_getASN1Element(&elem, p, dn.end);
1151 /* We have a DN's AttributeTypeAndValue: check it in case it's a CN. */
1152 elem.beg = checkOID(elem.beg, elem.end, cnOID);
1154 name = elem; /* Latch CN. */
1158 /* Check the CN if found. */
1159 if(!Curl_getASN1Element(&elem, name.beg, name.end))
1160 failf(data, "SSL: unable to obtain common name from peer certificate");
1162 len = utf8asn1str(&dnsname, elem.tag, elem.beg, elem.end);
1165 return CURLE_OUT_OF_MEMORY;
1167 if(strlen(dnsname) != (size_t) len) /* Nul byte in string ? */
1168 failf(data, "SSL: illegal cert name field");
1169 else if(Curl_cert_hostcheck((const char *) dnsname, conn->host.name)) {
1170 infof(data, "\t common name: %s (matched)\n", dnsname);
1175 failf(data, "SSL: certificate subject name '%s' does not match "
1176 "target host name '%s'", dnsname, conn->host.dispname);
1180 return CURLE_PEER_FAILED_VERIFICATION;
1183 #endif /* USE_QSOSSL or USE_GSKIT */