1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2015, 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_GSKIT) || defined(USE_NSS) || defined(USE_GNUTLS) || \
28 #include <curl/curl.h>
31 #include "hostcheck.h"
32 #include "vtls/vtls.h"
34 #include "inet_pton.h"
35 #include "curl_base64.h"
37 #include "curl_printf.h"
38 #include "curl_memory.h"
39 /* The last #include file should be: */
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 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.
107 const char * Curl_getASN1Element(curl_asn1Element * elem,
108 const char * beg, const char * end)
112 curl_asn1Element lelem;
114 /* Get a single ASN.1 element into `elem', parse ASN.1 string at `beg'
116 Returns a pointer in source string after the parsed element, or NULL
117 if an error occurs. */
119 if(beg >= end || !*beg)
120 return (const char *) NULL;
122 /* Process header byte. */
124 b = (unsigned char) *beg++;
125 elem->constructed = (b & 0x20) != 0;
126 elem->class = (b >> 6) & 3;
129 return (const char *) NULL; /* Long tag values not supported here. */
132 /* Process length. */
134 return (const char *) NULL;
135 b = (unsigned char) *beg++;
138 else if(!(b &= 0x7F)) {
139 /* Unspecified length. Since we have all the data, we can determine the
140 effective length by skipping element until an end element is found. */
141 if(!elem->constructed)
142 return (const char *) NULL;
144 while(beg < end && *beg) {
145 beg = Curl_getASN1Element(&lelem, beg, end);
147 return (const char *) NULL;
150 return (const char *) NULL;
154 else if(beg + b > end)
155 return (const char *) NULL; /* Does not fit in source. */
157 /* Get long length. */
160 if(len & 0xFF000000L)
161 return (const char *) NULL; /* Lengths > 32 bits are not supported. */
162 len = (len << 8) | (unsigned char) *beg++;
165 if((unsigned long) (end - beg) < len)
166 return (const char *) NULL; /* Element data does not fit in source. */
168 elem->end = beg + len;
172 static const curl_OID * searchOID(const char * oid)
176 /* Search the null terminated OID or OID identifier in local table.
177 Return the table entry pointer or NULL if not found. */
179 for(op = OIDtable; op->numoid; op++)
180 if(!strcmp(op->numoid, oid) || curl_strequal(op->textoid, oid))
183 return (const curl_OID *) NULL;
186 static const char * bool2str(const char * beg, const char * end)
188 /* Convert an ASN.1 Boolean value into its string representation.
189 Return the dynamically allocated string, or NULL if source is not an
190 ASN.1 Boolean value. */
193 return (const char *) NULL;
194 return strdup(*beg? "TRUE": "FALSE");
197 static const char * octet2str(const char * beg, const char * end)
199 size_t n = end - beg;
202 /* Convert an ASN.1 octet string to a printable string.
203 Return the dynamically allocated string, or NULL if an error occurs. */
205 buf = malloc(3 * n + 1);
207 for(n = 0; beg < end; n += 3)
208 snprintf(buf + n, 4, "%02x:", *(const unsigned char *) beg++);
212 static const char * bit2str(const char * beg, const char * end)
214 /* Convert an ASN.1 bit string to a printable string.
215 Return the dynamically allocated string, or NULL if an error occurs. */
218 return (const char *) NULL;
219 return octet2str(beg, end);
222 static const char * int2str(const char * beg, const char * end)
225 size_t n = end - beg;
227 /* Convert an ASN.1 integer value into its string representation.
228 Return the dynamically allocated string, or NULL if source is not an
229 ASN.1 integer value. */
232 return (const char *) NULL;
235 return octet2str(beg, end);
237 /* Represent integers <= 32-bit as a single value. */
242 val = (val << 8) | *(const unsigned char *) beg++;
244 return curl_maprintf("%s%lx", (val < 0 || val >= 10)? "0x": "", val);
248 utf8asn1str(char * * to, int type, const char * from, const char * end)
250 size_t inlength = end - from;
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
265 case CURL_ASN1_BMP_STRING:
268 case CURL_ASN1_UNIVERSAL_STRING:
271 case CURL_ASN1_NUMERIC_STRING:
272 case CURL_ASN1_PRINTABLE_STRING:
273 case CURL_ASN1_TELETEX_STRING:
274 case CURL_ASN1_IA5_STRING:
275 case CURL_ASN1_VISIBLE_STRING:
276 case CURL_ASN1_UTF8_STRING:
279 return -1; /* Conversion not supported. */
283 return -1; /* Length inconsistent with character size. */
284 buf = malloc(4 * (inlength / size) + 1);
286 return -1; /* Not enough memory. */
288 if(type == CURL_ASN1_UTF8_STRING) {
290 outlength = inlength;
292 memcpy(buf, from, outlength);
295 for(outlength = 0; from < end;) {
299 wc = (wc << 8) | *(const unsigned char *) from++;
300 wc = (wc << 8) | *(const unsigned char *) from++;
303 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 /* Convert an ASN.1 element to a printable string.
543 Return the dynamically allocated string, or NULL if an error occurs. */
545 if(elem->constructed)
546 return (const char *) NULL; /* No conversion of structured elements. */
549 type = elem->tag; /* Type not forced: use element tag as type. */
552 case CURL_ASN1_BOOLEAN:
553 return bool2str(elem->beg, elem->end);
554 case CURL_ASN1_INTEGER:
555 case CURL_ASN1_ENUMERATED:
556 return int2str(elem->beg, elem->end);
557 case CURL_ASN1_BIT_STRING:
558 return bit2str(elem->beg, elem->end);
559 case CURL_ASN1_OCTET_STRING:
560 return octet2str(elem->beg, elem->end);
563 case CURL_ASN1_OBJECT_IDENTIFIER:
564 return OID2str(elem->beg, elem->end, TRUE);
565 case CURL_ASN1_UTC_TIME:
566 return UTime2str(elem->beg, elem->end);
567 case CURL_ASN1_GENERALIZED_TIME:
568 return GTime2str(elem->beg, elem->end);
569 case CURL_ASN1_UTF8_STRING:
570 case CURL_ASN1_NUMERIC_STRING:
571 case CURL_ASN1_PRINTABLE_STRING:
572 case CURL_ASN1_TELETEX_STRING:
573 case CURL_ASN1_IA5_STRING:
574 case CURL_ASN1_VISIBLE_STRING:
575 case CURL_ASN1_UNIVERSAL_STRING:
576 case CURL_ASN1_BMP_STRING:
577 return string2str(type, elem->beg, elem->end);
580 return (const char *) NULL; /* Unsupported. */
583 static ssize_t encodeDN(char * buf, size_t n, curl_asn1Element * dn)
585 curl_asn1Element rdn;
586 curl_asn1Element atv;
587 curl_asn1Element oid;
588 curl_asn1Element value;
595 /* ASCII encode distinguished name at `dn' into the `n'-byte buffer at `buf'.
596 Return the total string length, even if larger than `n'. */
598 for(p1 = dn->beg; p1 < dn->end;) {
599 p1 = Curl_getASN1Element(&rdn, p1, dn->end);
600 for(p2 = rdn.beg; p2 < rdn.end;) {
601 p2 = Curl_getASN1Element(&atv, p2, rdn.end);
602 p3 = Curl_getASN1Element(&oid, atv.beg, atv.end);
603 Curl_getASN1Element(&value, p3, atv.end);
604 str = Curl_ASN1tostr(&oid, 0);
609 If attribute has a short uppercase name, delimiter is ", ". */
611 for(p3 = str; isupper(*p3); p3++)
613 for(p3 = (*p3 || p3 - str > 2)? "/": ", "; *p3; p3++) {
620 /* Encode attribute name. */
621 for(p3 = str; *p3; p3++) {
628 /* Generate equal sign. */
633 /* Generate value. */
634 str = Curl_ASN1tostr(&value, 0);
637 for(p3 = str; *p3; p3++) {
649 const char * Curl_DNtostr(curl_asn1Element * dn)
651 char * buf = (char *) NULL;
652 ssize_t n = encodeDN(buf, 0, dn);
654 /* Convert an ASN.1 distinguished name into a printable string.
655 Return the dynamically allocated string, or NULL if an error occurs. */
660 encodeDN(buf, n + 1, dn);
664 return (const char *) buf;
671 void Curl_parseX509(curl_X509certificate * cert,
672 const char * beg, const char * end)
674 curl_asn1Element elem;
675 curl_asn1Element tbsCertificate;
677 static const char defaultVersion = 0; /* v1. */
679 /* ASN.1 parse an X509 certificate into structure subfields.
680 Syntax is assumed to have already been checked by the SSL backend.
683 cert->certificate.header = NULL;
684 cert->certificate.beg = beg;
685 cert->certificate.end = end;
687 /* Get the sequence content. */
688 Curl_getASN1Element(&elem, beg, end);
692 /* Get tbsCertificate. */
693 beg = Curl_getASN1Element(&tbsCertificate, beg, end);
694 /* Skip the signatureAlgorithm. */
695 beg = Curl_getASN1Element(&cert->signatureAlgorithm, beg, end);
696 /* Get the signatureValue. */
697 Curl_getASN1Element(&cert->signature, beg, end);
699 /* Parse TBSCertificate. */
700 beg = tbsCertificate.beg;
701 end = tbsCertificate.end;
702 /* Get optional version, get serialNumber. */
703 cert->version.header = NULL;
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(&cert->subjectPublicKeyInfo, beg, end);
724 ccp = Curl_getASN1Element(&cert->subjectPublicKeyAlgorithm,
725 cert->subjectPublicKeyInfo.beg,
726 cert->subjectPublicKeyInfo.end);
727 Curl_getASN1Element(&cert->subjectPublicKey, ccp,
728 cert->subjectPublicKeyInfo.end);
729 /* Get optional issuerUiqueID, subjectUniqueID and extensions. */
730 cert->issuerUniqueID.tag = cert->subjectUniqueID.tag = 0;
731 cert->extensions.tag = elem.tag = 0;
732 cert->issuerUniqueID.header = cert->subjectUniqueID.header = NULL;
733 cert->issuerUniqueID.beg = cert->issuerUniqueID.end = "";
734 cert->subjectUniqueID.beg = cert->subjectUniqueID.end = "";
735 cert->extensions.header = NULL;
736 cert->extensions.beg = cert->extensions.end = "";
738 beg = Curl_getASN1Element(&elem, beg, end);
740 cert->issuerUniqueID = elem;
742 beg = Curl_getASN1Element(&elem, beg, end);
745 cert->subjectUniqueID = elem;
747 beg = Curl_getASN1Element(&elem, beg, end);
750 Curl_getASN1Element(&cert->extensions, elem.beg, elem.end);
753 static size_t copySubstring(char * to, const char * from)
757 /* Copy at most 64-characters, terminate with a newline and returns the
758 effective number of stored characters. */
760 for(i = 0; i < 64; i++) {
770 static const char * dumpAlgo(curl_asn1Element * param,
771 const char * beg, const char * end)
773 curl_asn1Element oid;
775 /* Get algorithm parameters and return algorithm name. */
777 beg = Curl_getASN1Element(&oid, beg, end);
778 param->header = NULL;
780 param->beg = param->end = end;
782 Curl_getASN1Element(param, beg, end);
783 return OID2str(oid.beg, oid.end, TRUE);
786 static void do_pubkey_field(struct SessionHandle * data, int certnum,
787 const char * label, curl_asn1Element * elem)
791 /* Generate a certificate information record for the public key. */
793 output = Curl_ASN1tostr(elem, 0);
795 if(data->set.ssl.certinfo)
796 Curl_ssl_push_certinfo(data, certnum, label, output);
798 infof(data, " %s: %s\n", label, output);
799 free((char *) output);
803 static void do_pubkey(struct SessionHandle * data, int certnum,
804 const char * algo, curl_asn1Element * param,
805 curl_asn1Element * pubkey)
807 curl_asn1Element elem;
814 /* Generate all information records for the public key. */
816 /* Get the public key (single element). */
817 Curl_getASN1Element(&pk, pubkey->beg + 1, pubkey->end);
819 if(curl_strequal(algo, "rsaEncryption")) {
820 p = Curl_getASN1Element(&elem, pk.beg, pk.end);
821 /* Compute key length. */
822 for(q = elem.beg; !*q && q < elem.end; q++)
824 len = (unsigned long)((elem.end - q) * 8);
826 for(i = *(unsigned char *) q; !(i & 0x80); i <<= 1)
829 elem.beg = q; /* Strip leading zero bytes. */
831 infof(data, " RSA Public Key (%lu bits)\n", len);
832 if(data->set.ssl.certinfo) {
833 q = curl_maprintf("%lu", len);
835 Curl_ssl_push_certinfo(data, certnum, "RSA Public Key", q);
839 /* Generate coefficients. */
840 do_pubkey_field(data, certnum, "rsa(n)", &elem);
841 Curl_getASN1Element(&elem, p, pk.end);
842 do_pubkey_field(data, certnum, "rsa(e)", &elem);
844 else if(curl_strequal(algo, "dsa")) {
845 p = Curl_getASN1Element(&elem, param->beg, param->end);
846 do_pubkey_field(data, certnum, "dsa(p)", &elem);
847 p = Curl_getASN1Element(&elem, p, param->end);
848 do_pubkey_field(data, certnum, "dsa(q)", &elem);
849 Curl_getASN1Element(&elem, p, param->end);
850 do_pubkey_field(data, certnum, "dsa(g)", &elem);
851 do_pubkey_field(data, certnum, "dsa(pub_key)", &pk);
853 else if(curl_strequal(algo, "dhpublicnumber")) {
854 p = Curl_getASN1Element(&elem, param->beg, param->end);
855 do_pubkey_field(data, certnum, "dh(p)", &elem);
856 Curl_getASN1Element(&elem, param->beg, param->end);
857 do_pubkey_field(data, certnum, "dh(g)", &elem);
858 do_pubkey_field(data, certnum, "dh(pub_key)", &pk);
860 #if 0 /* Patent-encumbered. */
861 else if(curl_strequal(algo, "ecPublicKey")) {
867 CURLcode Curl_extract_certinfo(struct connectdata * conn,
872 curl_X509certificate cert;
873 struct SessionHandle * data = conn->data;
874 curl_asn1Element param;
880 unsigned long version;
884 if(!data->set.ssl.certinfo)
888 /* Prepare the certificate information for curl_easy_getinfo(). */
890 /* Extract the certificate ASN.1 elements. */
891 Curl_parseX509(&cert, beg, end);
894 ccp = Curl_DNtostr(&cert.subject);
896 return CURLE_OUT_OF_MEMORY;
897 if(data->set.ssl.certinfo)
898 Curl_ssl_push_certinfo(data, certnum, "Subject", ccp);
900 infof(data, "%2d Subject: %s\n", certnum, ccp);
904 ccp = Curl_DNtostr(&cert.issuer);
906 return CURLE_OUT_OF_MEMORY;
907 if(data->set.ssl.certinfo)
908 Curl_ssl_push_certinfo(data, certnum, "Issuer", ccp);
910 infof(data, " Issuer: %s\n", ccp);
913 /* Version (always fits in less than 32 bits). */
915 for(ccp = cert.version.beg; ccp < cert.version.end; ccp++)
916 version = (version << 8) | *(const unsigned char *) ccp;
917 if(data->set.ssl.certinfo) {
918 ccp = curl_maprintf("%lx", version);
920 return CURLE_OUT_OF_MEMORY;
921 Curl_ssl_push_certinfo(data, certnum, "Version", ccp);
925 infof(data, " Version: %lu (0x%lx)\n", version + 1, version);
928 ccp = Curl_ASN1tostr(&cert.serialNumber, 0);
930 return CURLE_OUT_OF_MEMORY;
931 if(data->set.ssl.certinfo)
932 Curl_ssl_push_certinfo(data, certnum, "Serial Number", ccp);
934 infof(data, " Serial Number: %s\n", ccp);
937 /* Signature algorithm .*/
938 ccp = dumpAlgo(¶m, cert.signatureAlgorithm.beg,
939 cert.signatureAlgorithm.end);
941 return CURLE_OUT_OF_MEMORY;
942 if(data->set.ssl.certinfo)
943 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 if(data->set.ssl.certinfo)
953 Curl_ssl_push_certinfo(data, certnum, "Start Date", ccp);
955 infof(data, " Start Date: %s\n", ccp);
959 ccp = Curl_ASN1tostr(&cert.notAfter, 0);
961 return CURLE_OUT_OF_MEMORY;
962 if(data->set.ssl.certinfo)
963 Curl_ssl_push_certinfo(data, certnum, "Expire Date", ccp);
965 infof(data, " Expire Date: %s\n", ccp);
968 /* Public Key Algorithm. */
969 ccp = dumpAlgo(¶m, cert.subjectPublicKeyAlgorithm.beg,
970 cert.subjectPublicKeyAlgorithm.end);
972 return CURLE_OUT_OF_MEMORY;
973 if(data->set.ssl.certinfo)
974 Curl_ssl_push_certinfo(data, certnum, "Public Key Algorithm", ccp);
976 infof(data, " Public Key Algorithm: %s\n", ccp);
977 do_pubkey(data, certnum, ccp, ¶m, &cert.subjectPublicKey);
980 /* TODO: extensions. */
983 ccp = Curl_ASN1tostr(&cert.signature, 0);
985 return CURLE_OUT_OF_MEMORY;
986 if(data->set.ssl.certinfo)
987 Curl_ssl_push_certinfo(data, certnum, "Signature", ccp);
989 infof(data, " Signature: %s\n", ccp);
992 /* Generate PEM certificate. */
993 result = Curl_base64_encode(data, cert.certificate.beg,
994 cert.certificate.end - cert.certificate.beg,
998 /* Compute the number of characters in final certificate string. Format is:
999 -----BEGIN CERTIFICATE-----\n
1000 <max 64 base64 characters>\n
1004 -----END CERTIFICATE-----\n
1006 i = 28 + cl1 + (cl1 + 64 - 1) / 64 + 26;
1007 cp2 = malloc(i + 1);
1010 return CURLE_OUT_OF_MEMORY;
1012 /* Build the certificate string. */
1013 i = copySubstring(cp2, "-----BEGIN CERTIFICATE-----");
1014 for(j = 0; j < cl1; j += 64)
1015 i += copySubstring(cp2 + i, cp1 + j);
1016 i += copySubstring(cp2 + i, "-----END CERTIFICATE-----");
1019 if(data->set.ssl.certinfo)
1020 Curl_ssl_push_certinfo(data, certnum, "Cert", cp2);
1022 infof(data, "%s\n", cp2);
1027 #endif /* USE_GSKIT or USE_NSS or USE_GNUTLS or USE_CYASSL */
1029 #if defined(USE_GSKIT)
1031 static const char * checkOID(const char * beg, const char * end,
1039 /* Check if first ASN.1 element at `beg' is the given OID.
1040 Return a pointer in the source after the OID if found, else NULL. */
1042 ccp = Curl_getASN1Element(&e, beg, end);
1043 if(!ccp || e.tag != CURL_ASN1_OBJECT_IDENTIFIER)
1044 return (const char *) NULL;
1046 p = OID2str(e.beg, e.end, FALSE);
1048 return (const char *) NULL;
1050 matched = !strcmp(p, oid);
1052 return matched? ccp: (const char *) NULL;
1055 CURLcode Curl_verifyhost(struct connectdata * conn,
1056 const char * beg, const char * end)
1058 struct SessionHandle * data = conn->data;
1059 curl_X509certificate cert;
1060 curl_asn1Element dn;
1061 curl_asn1Element elem;
1062 curl_asn1Element ext;
1063 curl_asn1Element name;
1069 size_t addrlen = (size_t) -1;
1072 struct in6_addr addr;
1074 struct in_addr addr;
1077 /* Verify that connection server matches info in X509 certificate at
1080 if(!data->set.ssl.verifyhost)
1084 return CURLE_PEER_FAILED_VERIFICATION;
1085 Curl_parseX509(&cert, beg, end);
1087 /* Get the server IP address. */
1089 if(conn->bits.ipv6_ip && Curl_inet_pton(AF_INET6, conn->host.name, &addr))
1090 addrlen = sizeof(struct in6_addr);
1093 if(Curl_inet_pton(AF_INET, conn->host.name, &addr))
1094 addrlen = sizeof(struct in_addr);
1096 /* Process extensions. */
1097 for(p = cert.extensions.beg; p < cert.extensions.end && matched != 1;) {
1098 p = Curl_getASN1Element(&ext, p, cert.extensions.end);
1099 /* Check if extension is a subjectAlternativeName. */
1100 ext.beg = checkOID(ext.beg, ext.end, sanOID);
1102 ext.beg = Curl_getASN1Element(&elem, ext.beg, ext.end);
1103 /* Skip critical if present. */
1104 if(elem.tag == CURL_ASN1_BOOLEAN)
1105 ext.beg = Curl_getASN1Element(&elem, ext.beg, ext.end);
1106 /* Parse the octet string contents: is a single sequence. */
1107 Curl_getASN1Element(&elem, elem.beg, elem.end);
1108 /* Check all GeneralNames. */
1109 for(q = elem.beg; matched != 1 && q < elem.end;) {
1110 q = Curl_getASN1Element(&name, q, elem.end);
1112 case 2: /* DNS name. */
1114 len = utf8asn1str(&dnsname, CURL_ASN1_IA5_STRING,
1115 name.beg, name.end);
1117 if(strlen(dnsname) == (size_t) len)
1118 i = Curl_cert_hostcheck((const char *) dnsname, conn->host.name);
1121 return CURLE_PEER_FAILED_VERIFICATION;
1125 case 7: /* IP address. */
1126 matched = (size_t) (name.end - q) == addrlen &&
1127 !memcmp(&addr, q, addrlen);
1136 /* an alternative name matched the server hostname */
1137 infof(data, "\t subjectAltName: %s matched\n", conn->host.dispname);
1140 /* an alternative name field existed, but didn't match and then
1142 infof(data, "\t subjectAltName does not match %s\n", conn->host.dispname);
1143 return CURLE_PEER_FAILED_VERIFICATION;
1146 /* Process subject. */
1148 name.beg = name.end = "";
1149 q = cert.subject.beg;
1150 /* we have to look to the last occurrence of a commonName in the
1151 distinguished one to get the most significant one. */
1152 while(q < cert.subject.end) {
1153 q = Curl_getASN1Element(&dn, q, cert.subject.end);
1154 for(p = dn.beg; p < dn.end;) {
1155 p = Curl_getASN1Element(&elem, p, dn.end);
1156 /* We have a DN's AttributeTypeAndValue: check it in case it's a CN. */
1157 elem.beg = checkOID(elem.beg, elem.end, cnOID);
1159 name = elem; /* Latch CN. */
1163 /* Check the CN if found. */
1164 if(!Curl_getASN1Element(&elem, name.beg, name.end))
1165 failf(data, "SSL: unable to obtain common name from peer certificate");
1167 len = utf8asn1str(&dnsname, elem.tag, elem.beg, elem.end);
1170 return CURLE_OUT_OF_MEMORY;
1172 if(strlen(dnsname) != (size_t) len) /* Nul byte in string ? */
1173 failf(data, "SSL: illegal cert name field");
1174 else if(Curl_cert_hostcheck((const char *) dnsname, conn->host.name)) {
1175 infof(data, "\t common name: %s (matched)\n", dnsname);
1180 failf(data, "SSL: certificate subject name '%s' does not match "
1181 "target host name '%s'", dnsname, conn->host.dispname);
1185 return CURLE_PEER_FAILED_VERIFICATION;
1188 #endif /* USE_GSKIT */