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_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. */
126 b = (unsigned char) *beg++;
127 elem->constructed = (b & 0x20) != 0;
128 elem->class = (b >> 6) & 3;
131 return (const char *) NULL; /* Long tag values not supported here. */
134 /* Process length. */
136 return (const char *) NULL;
137 b = (unsigned char) *beg++;
140 else if(!(b &= 0x7F)) {
141 /* Unspecified length. Since we have all the data, we can determine the
142 effective length by skipping element until an end element is found. */
143 if(!elem->constructed)
144 return (const char *) NULL;
146 while(beg < end && *beg) {
147 beg = Curl_getASN1Element(&lelem, beg, end);
149 return (const char *) NULL;
152 return (const char *) NULL;
156 else if(beg + b > end)
157 return (const char *) NULL; /* Does not fit in source. */
159 /* Get long length. */
162 if(len & 0xFF000000L)
163 return (const char *) NULL; /* Lengths > 32 bits are not supported. */
164 len = (len << 8) | (unsigned char) *beg++;
167 if((unsigned long) (end - beg) < len)
168 return (const char *) NULL; /* Element data does not fit in source. */
170 elem->end = beg + len;
174 static const curl_OID * searchOID(const char * oid)
178 /* Search the null terminated OID or OID identifier in local table.
179 Return the table entry pointer or NULL if not found. */
181 for(op = OIDtable; op->numoid; op++)
182 if(!strcmp(op->numoid, oid) || curl_strequal(op->textoid, oid))
185 return (const curl_OID *) NULL;
188 static const char * bool2str(const char * beg, const char * end)
190 /* Convert an ASN.1 Boolean value into its string representation.
191 Return the dynamically allocated string, or NULL if source is not an
192 ASN.1 Boolean value. */
195 return (const char *) NULL;
196 return strdup(*beg? "TRUE": "FALSE");
199 static const char * octet2str(const char * beg, const char * end)
201 size_t n = end - beg;
204 /* Convert an ASN.1 octet string to a printable string.
205 Return the dynamically allocated string, or NULL if an error occurs. */
207 buf = malloc(3 * n + 1);
209 for(n = 0; beg < end; n += 3)
210 snprintf(buf + n, 4, "%02x:", *(const unsigned char *) beg++);
214 static const char * bit2str(const char * beg, const char * end)
217 /* Convert an ASN.1 bit string to a printable string.
218 Return the dynamically allocated string, or NULL if an error occurs. */
221 return (const char *) NULL;
222 return octet2str(beg, end);
225 static const char * int2str(const char * beg, const char * end)
228 size_t n = end - beg;
230 /* Convert an ASN.1 integer value into its string representation.
231 Return the dynamically allocated string, or NULL if source is not an
232 ASN.1 integer value. */
235 return (const char *) NULL;
238 return octet2str(beg, end);
240 /* Represent integers <= 32-bit as a single value. */
245 val = (val << 8) | *(const unsigned char *) beg++;
247 return curl_maprintf("%s%lx", (val < 0 || val >= 10)? "0x": "", val);
251 utf8asn1str(char * * to, int type, const char * from, const char * end)
253 size_t inlength = end - from;
260 /* Perform a lazy conversion from an ASN.1 typed string to UTF8. Allocate the
261 destination buffer dynamically. The allocation size will normally be too
262 large: this is to avoid buffer overflows.
263 Terminate the string with a nul byte and return the converted
268 case CURL_ASN1_BMP_STRING:
271 case CURL_ASN1_UNIVERSAL_STRING:
274 case CURL_ASN1_NUMERIC_STRING:
275 case CURL_ASN1_PRINTABLE_STRING:
276 case CURL_ASN1_TELETEX_STRING:
277 case CURL_ASN1_IA5_STRING:
278 case CURL_ASN1_VISIBLE_STRING:
279 case CURL_ASN1_UTF8_STRING:
282 return -1; /* Conversion not supported. */
286 return -1; /* Length inconsistent with character size. */
287 buf = malloc(4 * (inlength / size) + 1);
289 return -1; /* Not enough memory. */
291 if(type == CURL_ASN1_UTF8_STRING) {
293 outlength = inlength;
295 memcpy(buf, from, outlength);
298 for(outlength = 0; from < end;) {
302 wc = (wc << 8) | *(const unsigned char *) from++;
303 wc = (wc << 8) | *(const unsigned char *) from++;
305 wc = (wc << 8) | *(const unsigned char *) from++;
306 default: /* case 1: */
307 wc = (wc << 8) | *(const unsigned char *) from++;
310 if(wc >= 0x00000080) {
311 if(wc >= 0x00000800) {
312 if(wc >= 0x00010000) {
313 if(wc >= 0x00200000) {
315 return -1; /* Invalid char. size for target encoding. */
317 buf[outlength + 3] = (char) (0x80 | (wc & 0x3F));
318 wc = (wc >> 6) | 0x00010000;
321 buf[outlength + 2] = (char) (0x80 | (wc & 0x3F));
322 wc = (wc >> 6) | 0x00000800;
325 buf[outlength + 1] = (char) (0x80 | (wc & 0x3F));
326 wc = (wc >> 6) | 0x000000C0;
329 buf[outlength] = (char) wc;
330 outlength += charsize;
333 buf[outlength] = '\0';
338 static const char * string2str(int type, const char * beg, const char * end)
342 /* Convert an ASN.1 String into its UTF-8 string representation.
343 Return the dynamically allocated string, or NULL if an error occurs. */
345 if(utf8asn1str(&buf, type, beg, end) < 0)
346 return (const char *) NULL;
350 static int encodeUint(char * buf, int n, unsigned int x)
353 unsigned int y = x / 10;
355 /* Decimal ASCII encode unsigned integer `x' in the `n'-byte buffer at `buf'.
356 Return the total number of encoded digits, even if larger than `n'. */
359 i += encodeUint(buf, n, y);
363 buf[i] = (char) ('0' + x);
366 buf[i] = '\0'; /* Store a terminator if possible. */
370 static int encodeOID(char * buf, int n, const char * beg, const char * end)
376 /* Convert an ASN.1 OID into its dotted string representation.
377 Store the result in th `n'-byte buffer at `buf'.
378 Return the converted string length, or -1 if an error occurs. */
380 /* Process the first two numbers. */
381 y = *(const unsigned char *) beg++;
384 i += encodeUint(buf + i, n - i, x);
388 i += encodeUint(buf + i, n - i, y);
390 /* Process the trailing numbers. */
399 y = *(const unsigned char *) beg++;
400 x = (x << 7) | (y & 0x7F);
402 i += encodeUint(buf + i, n - i, x);
409 static const char * OID2str(const char * beg, const char * end, bool symbolic)
411 char * buf = (char *) NULL;
415 /* Convert an ASN.1 OID into its dotted or symbolic string representation.
416 Return the dynamically allocated string, or NULL if an error occurs. */
419 n = encodeOID((char *) NULL, -1, beg, end);
423 encodeOID(buf, n, beg, end);
430 buf = strdup(op->textoid);
439 static const char * GTime2str(const char * beg, const char * end)
446 const char * sep = "";
448 /* Convert an ASN.1 Generalized time to a printable string.
449 Return the dynamically allocated string, or NULL if an error occurs. */
451 for(fracp = beg; fracp < end && *fracp >= '0' && *fracp <= '9'; fracp++)
454 /* Get seconds digits. */
456 switch (fracp - beg - 12) {
466 return (const char *) NULL;
469 /* Scan for timezone, measure fractional seconds. */
472 if(fracp < end && (*fracp == '.' || *fracp == ',')) {
476 while(tzp < end && *tzp >= '0' && *tzp <= '9');
477 /* Strip leading zeroes in fractional seconds. */
478 for(fracl = tzp - fracp - 1; fracl && fracp[fracl - 1] == '0'; fracl--)
482 /* Process timezone. */
484 ; /* Nothing to do. */
485 else if(*tzp == 'Z') {
495 return curl_maprintf("%.4s-%.2s-%.2s %.2s:%.2s:%c%c%s%.*s%s%.*s",
496 beg, beg + 4, beg + 6,
497 beg + 8, beg + 10, sec1, sec2,
498 fracl? ".": "", fracl, fracp,
502 static const char * UTime2str(const char * beg, const char * end)
508 /* Convert an ASN.1 UTC time to a printable string.
509 Return the dynamically allocated string, or NULL if an error occurs. */
511 for(tzp = beg; tzp < end && *tzp >= '0' && *tzp <= '9'; tzp++)
513 /* Get the seconds. */
521 return (const char *) NULL;
524 /* Process timezone. */
526 return (const char *) NULL;
535 return curl_maprintf("%u%.2s-%.2s-%.2s %.2s:%.2s:%.2s %.*s",
536 20 - (*beg >= '5'), beg, beg + 2, beg + 4,
537 beg + 6, beg + 8, sec,
541 const char * Curl_ASN1tostr(curl_asn1Element * elem, int type)
543 static const char zero = '\0';
545 /* Convert an ASN.1 element to a printable string.
546 Return the dynamically allocated string, or NULL if an error occurs. */
548 if(elem->constructed)
549 return (const char *) NULL; /* No conversion of structured elements. */
552 type = elem->tag; /* Type not forced: use element tag as type. */
555 case CURL_ASN1_BOOLEAN:
556 return bool2str(elem->beg, elem->end);
557 case CURL_ASN1_INTEGER:
558 case CURL_ASN1_ENUMERATED:
559 return int2str(elem->beg, elem->end);
560 case CURL_ASN1_BIT_STRING:
561 return bit2str(elem->beg, elem->end);
562 case CURL_ASN1_OCTET_STRING:
563 return octet2str(elem->beg, elem->end);
565 return strdup(&zero);
566 case CURL_ASN1_OBJECT_IDENTIFIER:
567 return OID2str(elem->beg, elem->end, TRUE);
568 case CURL_ASN1_UTC_TIME:
569 return UTime2str(elem->beg, elem->end);
570 case CURL_ASN1_GENERALIZED_TIME:
571 return GTime2str(elem->beg, elem->end);
572 case CURL_ASN1_UTF8_STRING:
573 case CURL_ASN1_NUMERIC_STRING:
574 case CURL_ASN1_PRINTABLE_STRING:
575 case CURL_ASN1_TELETEX_STRING:
576 case CURL_ASN1_IA5_STRING:
577 case CURL_ASN1_VISIBLE_STRING:
578 case CURL_ASN1_UNIVERSAL_STRING:
579 case CURL_ASN1_BMP_STRING:
580 return string2str(type, elem->beg, elem->end);
583 return (const char *) NULL; /* Unsupported. */
586 static ssize_t encodeDN(char * buf, size_t n, curl_asn1Element * dn)
588 curl_asn1Element rdn;
589 curl_asn1Element atv;
590 curl_asn1Element oid;
591 curl_asn1Element value;
598 /* ASCII encode distinguished name at `dn' into the `n'-byte buffer at `buf'.
599 Return the total string length, even if larger than `n'. */
601 for(p1 = dn->beg; p1 < dn->end;) {
602 p1 = Curl_getASN1Element(&rdn, p1, dn->end);
603 for(p2 = rdn.beg; p2 < rdn.end;) {
604 p2 = Curl_getASN1Element(&atv, p2, rdn.end);
605 p3 = Curl_getASN1Element(&oid, atv.beg, atv.end);
606 Curl_getASN1Element(&value, p3, atv.end);
607 str = Curl_ASN1tostr(&oid, 0);
612 If attribute has a short uppercase name, delimiter is ", ". */
614 for(p3 = str; isupper(*p3); p3++)
616 for(p3 = (*p3 || p3 - str > 2)? "/": ", "; *p3; p3++) {
623 /* Encode attribute name. */
624 for(p3 = str; *p3; p3++) {
631 /* Generate equal sign. */
636 /* Generate value. */
637 str = Curl_ASN1tostr(&value, 0);
640 for(p3 = str; *p3; p3++) {
652 const char * Curl_DNtostr(curl_asn1Element * dn)
654 char * buf = (char *) NULL;
655 ssize_t n = encodeDN(buf, 0, dn);
657 /* Convert an ASN.1 distinguished name into a printable string.
658 Return the dynamically allocated string, or NULL if an error occurs. */
663 encodeDN(buf, n + 1, dn);
667 return (const char *) buf;
674 void Curl_parseX509(curl_X509certificate * cert,
675 const char * beg, const char * end)
677 curl_asn1Element elem;
678 curl_asn1Element tbsCertificate;
680 static const char defaultVersion = 0; /* v1. */
682 /* ASN.1 parse an X509 certificate into structure subfields.
683 Syntax is assumed to have already been checked by the SSL backend.
686 cert->certificate.header = NULL;
687 cert->certificate.beg = beg;
688 cert->certificate.end = end;
690 /* Get the sequence content. */
691 Curl_getASN1Element(&elem, beg, end);
695 /* Get tbsCertificate. */
696 beg = Curl_getASN1Element(&tbsCertificate, beg, end);
697 /* Skip the signatureAlgorithm. */
698 beg = Curl_getASN1Element(&cert->signatureAlgorithm, beg, end);
699 /* Get the signatureValue. */
700 Curl_getASN1Element(&cert->signature, beg, end);
702 /* Parse TBSCertificate. */
703 beg = tbsCertificate.beg;
704 end = tbsCertificate.end;
705 /* Get optional version, get serialNumber. */
706 cert->version.header = NULL;
707 cert->version.beg = &defaultVersion;
708 cert->version.end = &defaultVersion + sizeof defaultVersion;;
709 beg = Curl_getASN1Element(&elem, beg, end);
711 Curl_getASN1Element(&cert->version, elem.beg, elem.end);
712 beg = Curl_getASN1Element(&elem, beg, end);
714 cert->serialNumber = elem;
715 /* Get signature algorithm. */
716 beg = Curl_getASN1Element(&cert->signatureAlgorithm, beg, end);
718 beg = Curl_getASN1Element(&cert->issuer, beg, end);
719 /* Get notBefore and notAfter. */
720 beg = Curl_getASN1Element(&elem, beg, end);
721 ccp = Curl_getASN1Element(&cert->notBefore, elem.beg, elem.end);
722 Curl_getASN1Element(&cert->notAfter, ccp, elem.end);
724 beg = Curl_getASN1Element(&cert->subject, beg, end);
725 /* Get subjectPublicKeyAlgorithm and subjectPublicKey. */
726 beg = Curl_getASN1Element(&cert->subjectPublicKeyInfo, beg, end);
727 ccp = Curl_getASN1Element(&cert->subjectPublicKeyAlgorithm,
728 cert->subjectPublicKeyInfo.beg,
729 cert->subjectPublicKeyInfo.end);
730 Curl_getASN1Element(&cert->subjectPublicKey, ccp,
731 cert->subjectPublicKeyInfo.end);
732 /* Get optional issuerUiqueID, subjectUniqueID and extensions. */
733 cert->issuerUniqueID.tag = cert->subjectUniqueID.tag = 0;
734 cert->extensions.tag = elem.tag = 0;
735 cert->issuerUniqueID.header = cert->subjectUniqueID.header = NULL;
736 cert->issuerUniqueID.beg = cert->issuerUniqueID.end = "";
737 cert->subjectUniqueID.beg = cert->subjectUniqueID.end = "";
738 cert->extensions.header = NULL;
739 cert->extensions.beg = cert->extensions.end = "";
741 beg = Curl_getASN1Element(&elem, beg, end);
743 cert->issuerUniqueID = elem;
745 beg = Curl_getASN1Element(&elem, beg, end);
748 cert->subjectUniqueID = elem;
750 beg = Curl_getASN1Element(&elem, beg, end);
753 Curl_getASN1Element(&cert->extensions, elem.beg, elem.end);
756 static size_t copySubstring(char * to, const char * from)
760 /* Copy at most 64-characters, terminate with a newline and returns the
761 effective number of stored characters. */
763 for(i = 0; i < 64; i++) {
773 static const char * dumpAlgo(curl_asn1Element * param,
774 const char * beg, const char * end)
776 curl_asn1Element oid;
778 /* Get algorithm parameters and return algorithm name. */
780 beg = Curl_getASN1Element(&oid, beg, end);
781 param->header = NULL;
783 param->beg = param->end = end;
785 Curl_getASN1Element(param, beg, end);
786 return OID2str(oid.beg, oid.end, TRUE);
789 static void do_pubkey_field(struct SessionHandle * data, int certnum,
790 const char * label, curl_asn1Element * elem)
794 /* Generate a certificate information record for the public key. */
796 output = Curl_ASN1tostr(elem, 0);
798 if(data->set.ssl.certinfo)
799 Curl_ssl_push_certinfo(data, certnum, label, output);
801 infof(data, " %s: %s\n", label, output);
802 free((char *) output);
806 static void do_pubkey(struct SessionHandle * data, int certnum,
807 const char * algo, curl_asn1Element * param,
808 curl_asn1Element * pubkey)
810 curl_asn1Element elem;
817 /* Generate all information records for the public key. */
819 /* Get the public key (single element). */
820 Curl_getASN1Element(&pk, pubkey->beg + 1, pubkey->end);
822 if(curl_strequal(algo, "rsaEncryption")) {
823 p = Curl_getASN1Element(&elem, pk.beg, pk.end);
824 /* Compute key length. */
825 for(q = elem.beg; !*q && q < elem.end; q++)
827 len = (elem.end - q) * 8;
829 for(i = *(unsigned char *) q; !(i & 0x80); i <<= 1)
832 elem.beg = q; /* Strip leading zero bytes. */
834 infof(data, " RSA Public Key (%lu bits)\n", len);
835 if(data->set.ssl.certinfo) {
836 q = curl_maprintf("%lu", len);
838 Curl_ssl_push_certinfo(data, certnum, "RSA Public Key", q);
842 /* Generate coefficients. */
843 do_pubkey_field(data, certnum, "rsa(n)", &elem);
844 Curl_getASN1Element(&elem, p, pk.end);
845 do_pubkey_field(data, certnum, "rsa(e)", &elem);
847 else if(curl_strequal(algo, "dsa")) {
848 p = Curl_getASN1Element(&elem, param->beg, param->end);
849 do_pubkey_field(data, certnum, "dsa(p)", &elem);
850 p = Curl_getASN1Element(&elem, p, param->end);
851 do_pubkey_field(data, certnum, "dsa(q)", &elem);
852 Curl_getASN1Element(&elem, p, param->end);
853 do_pubkey_field(data, certnum, "dsa(g)", &elem);
854 do_pubkey_field(data, certnum, "dsa(pub_key)", &pk);
856 else if(curl_strequal(algo, "dhpublicnumber")) {
857 p = Curl_getASN1Element(&elem, param->beg, param->end);
858 do_pubkey_field(data, certnum, "dh(p)", &elem);
859 Curl_getASN1Element(&elem, param->beg, param->end);
860 do_pubkey_field(data, certnum, "dh(g)", &elem);
861 do_pubkey_field(data, certnum, "dh(pub_key)", &pk);
863 #if 0 /* Patent-encumbered. */
864 else if(curl_strequal(algo, "ecPublicKey")) {
870 CURLcode Curl_extract_certinfo(struct connectdata * conn,
875 curl_X509certificate cert;
876 struct SessionHandle * data = conn->data;
877 curl_asn1Element param;
883 unsigned long version;
887 if(!data->set.ssl.certinfo)
891 /* Prepare the certificate information for curl_easy_getinfo(). */
893 /* Extract the certificate ASN.1 elements. */
894 Curl_parseX509(&cert, beg, end);
897 ccp = Curl_DNtostr(&cert.subject);
899 return CURLE_OUT_OF_MEMORY;
900 if(data->set.ssl.certinfo)
901 Curl_ssl_push_certinfo(data, certnum, "Subject", ccp);
903 infof(data, "%2d Subject: %s\n", certnum, ccp);
907 ccp = Curl_DNtostr(&cert.issuer);
909 return CURLE_OUT_OF_MEMORY;
910 if(data->set.ssl.certinfo)
911 Curl_ssl_push_certinfo(data, certnum, "Issuer", ccp);
913 infof(data, " Issuer: %s\n", ccp);
916 /* Version (always fits in less than 32 bits). */
918 for(ccp = cert.version.beg; ccp < cert.version.end; ccp++)
919 version = (version << 8) | *(const unsigned char *) ccp;
920 if(data->set.ssl.certinfo) {
921 ccp = curl_maprintf("%lx", version);
923 return CURLE_OUT_OF_MEMORY;
924 Curl_ssl_push_certinfo(data, certnum, "Version", ccp);
928 infof(data, " Version: %lu (0x%lx)\n", version + 1, version);
931 ccp = Curl_ASN1tostr(&cert.serialNumber, 0);
933 return CURLE_OUT_OF_MEMORY;
934 if(data->set.ssl.certinfo)
935 Curl_ssl_push_certinfo(data, certnum, "Serial Number", ccp);
937 infof(data, " Serial Number: %s\n", ccp);
940 /* Signature algorithm .*/
941 ccp = dumpAlgo(¶m, cert.signatureAlgorithm.beg,
942 cert.signatureAlgorithm.end);
944 return CURLE_OUT_OF_MEMORY;
945 if(data->set.ssl.certinfo)
946 Curl_ssl_push_certinfo(data, certnum, "Signature Algorithm", ccp);
948 infof(data, " Signature Algorithm: %s\n", ccp);
952 ccp = Curl_ASN1tostr(&cert.notBefore, 0);
954 return CURLE_OUT_OF_MEMORY;
955 if(data->set.ssl.certinfo)
956 Curl_ssl_push_certinfo(data, certnum, "Start Date", ccp);
958 infof(data, " Start Date: %s\n", ccp);
962 ccp = Curl_ASN1tostr(&cert.notAfter, 0);
964 return CURLE_OUT_OF_MEMORY;
965 if(data->set.ssl.certinfo)
966 Curl_ssl_push_certinfo(data, certnum, "Expire Date", ccp);
968 infof(data, " Expire Date: %s\n", ccp);
971 /* Public Key Algorithm. */
972 ccp = dumpAlgo(¶m, cert.subjectPublicKeyAlgorithm.beg,
973 cert.subjectPublicKeyAlgorithm.end);
975 return CURLE_OUT_OF_MEMORY;
976 if(data->set.ssl.certinfo)
977 Curl_ssl_push_certinfo(data, certnum, "Public Key Algorithm", ccp);
979 infof(data, " Public Key Algorithm: %s\n", ccp);
980 do_pubkey(data, certnum, ccp, ¶m, &cert.subjectPublicKey);
983 /* TODO: extensions. */
986 ccp = Curl_ASN1tostr(&cert.signature, 0);
988 return CURLE_OUT_OF_MEMORY;
989 if(data->set.ssl.certinfo)
990 Curl_ssl_push_certinfo(data, certnum, "Signature", ccp);
992 infof(data, " Signature: %s\n", ccp);
995 /* Generate PEM certificate. */
996 result = Curl_base64_encode(data, cert.certificate.beg,
997 cert.certificate.end - cert.certificate.beg,
1001 /* Compute the number of characters in final certificate string. Format is:
1002 -----BEGIN CERTIFICATE-----\n
1003 <max 64 base64 characters>\n
1007 -----END CERTIFICATE-----\n
1009 i = 28 + cl1 + (cl1 + 64 - 1) / 64 + 26;
1010 cp2 = malloc(i + 1);
1013 return CURLE_OUT_OF_MEMORY;
1015 /* Build the certificate string. */
1016 i = copySubstring(cp2, "-----BEGIN CERTIFICATE-----");
1017 for(j = 0; j < cl1; j += 64)
1018 i += copySubstring(cp2 + i, cp1 + j);
1019 i += copySubstring(cp2 + i, "-----END CERTIFICATE-----");
1022 if(data->set.ssl.certinfo)
1023 Curl_ssl_push_certinfo(data, certnum, "Cert", cp2);
1025 infof(data, "%s\n", cp2);
1030 #endif /* USE_GSKIT or USE_NSS */
1032 #if defined(USE_GSKIT)
1034 static const char * checkOID(const char * beg, const char * end,
1042 /* Check if first ASN.1 element at `beg' is the given OID.
1043 Return a pointer in the source after the OID if found, else NULL. */
1045 ccp = Curl_getASN1Element(&e, beg, end);
1046 if(!ccp || e.tag != CURL_ASN1_OBJECT_IDENTIFIER)
1047 return (const char *) NULL;
1049 p = OID2str(e.beg, e.end, FALSE);
1051 return (const char *) NULL;
1053 matched = !strcmp(p, oid);
1055 return matched? ccp: (const char *) NULL;
1058 CURLcode Curl_verifyhost(struct connectdata * conn,
1059 const char * beg, const char * end)
1061 struct SessionHandle * data = conn->data;
1062 curl_X509certificate cert;
1063 curl_asn1Element dn;
1064 curl_asn1Element elem;
1065 curl_asn1Element ext;
1066 curl_asn1Element name;
1072 size_t addrlen = (size_t) -1;
1075 struct in6_addr addr;
1077 struct in_addr addr;
1080 /* Verify that connection server matches info in X509 certificate at
1083 if(!data->set.ssl.verifyhost)
1087 return CURLE_PEER_FAILED_VERIFICATION;
1088 Curl_parseX509(&cert, beg, end);
1090 /* Get the server IP address. */
1092 if(conn->bits.ipv6_ip && Curl_inet_pton(AF_INET6, conn->host.name, &addr))
1093 addrlen = sizeof(struct in6_addr);
1096 if(Curl_inet_pton(AF_INET, conn->host.name, &addr))
1097 addrlen = sizeof(struct in_addr);
1099 /* Process extensions. */
1100 for(p = cert.extensions.beg; p < cert.extensions.end && matched != 1;) {
1101 p = Curl_getASN1Element(&ext, p, cert.extensions.end);
1102 /* Check if extension is a subjectAlternativeName. */
1103 ext.beg = checkOID(ext.beg, ext.end, sanOID);
1105 ext.beg = Curl_getASN1Element(&elem, ext.beg, ext.end);
1106 /* Skip critical if present. */
1107 if(elem.tag == CURL_ASN1_BOOLEAN)
1108 ext.beg = Curl_getASN1Element(&elem, ext.beg, ext.end);
1109 /* Parse the octet string contents: is a single sequence. */
1110 Curl_getASN1Element(&elem, elem.beg, elem.end);
1111 /* Check all GeneralNames. */
1112 for(q = elem.beg; matched != 1 && q < elem.end;) {
1113 q = Curl_getASN1Element(&name, q, elem.end);
1115 case 2: /* DNS name. */
1117 len = utf8asn1str(&dnsname, CURL_ASN1_IA5_STRING,
1118 name.beg, name.end);
1120 if(strlen(dnsname) == (size_t) len)
1121 i = Curl_cert_hostcheck((const char *) dnsname, conn->host.name);
1125 return CURLE_PEER_FAILED_VERIFICATION;
1129 case 7: /* IP address. */
1130 matched = (size_t) (name.end - q) == addrlen &&
1131 !memcmp(&addr, q, addrlen);
1140 /* an alternative name matched the server hostname */
1141 infof(data, "\t subjectAltName: %s matched\n", conn->host.dispname);
1144 /* an alternative name field existed, but didn't match and then
1146 infof(data, "\t subjectAltName does not match %s\n", conn->host.dispname);
1147 return CURLE_PEER_FAILED_VERIFICATION;
1150 /* Process subject. */
1152 name.beg = name.end = "";
1153 q = cert.subject.beg;
1154 /* we have to look to the last occurrence of a commonName in the
1155 distinguished one to get the most significant one. */
1156 while(q < cert.subject.end) {
1157 q = Curl_getASN1Element(&dn, q, cert.subject.end);
1158 for(p = dn.beg; p < dn.end;) {
1159 p = Curl_getASN1Element(&elem, p, dn.end);
1160 /* We have a DN's AttributeTypeAndValue: check it in case it's a CN. */
1161 elem.beg = checkOID(elem.beg, elem.end, cnOID);
1163 name = elem; /* Latch CN. */
1167 /* Check the CN if found. */
1168 if(!Curl_getASN1Element(&elem, name.beg, name.end))
1169 failf(data, "SSL: unable to obtain common name from peer certificate");
1171 len = utf8asn1str(&dnsname, elem.tag, elem.beg, elem.end);
1174 return CURLE_OUT_OF_MEMORY;
1176 if(strlen(dnsname) != (size_t) len) /* Nul byte in string ? */
1177 failf(data, "SSL: illegal cert name field");
1178 else if(Curl_cert_hostcheck((const char *) dnsname, conn->host.name)) {
1179 infof(data, "\t common name: %s (matched)\n", dnsname);
1184 failf(data, "SSL: certificate subject name '%s' does not match "
1185 "target host name '%s'", dnsname, conn->host.dispname);
1189 return CURLE_PEER_FAILED_VERIFICATION;
1192 #endif /* USE_GSKIT */