2013-07-16 00:16:13 +08:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2019-02-14 21:54:01 +08:00
|
|
|
* Copyright (C) 1998 - 2019, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2013-07-16 00:16:13 +08:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2016-02-03 07:19:02 +08:00
|
|
|
* are also available at https://curl.haxx.se/docs/copyright.html.
|
2013-07-16 00:16:13 +08:00
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#include "curl_setup.h"
|
|
|
|
|
2015-04-05 13:48:16 +08:00
|
|
|
#if defined(USE_GSKIT) || defined(USE_NSS) || defined(USE_GNUTLS) || \
|
2019-06-02 22:55:05 +08:00
|
|
|
defined(USE_WOLFSSL) || defined(USE_SCHANNEL)
|
2013-07-16 00:16:13 +08:00
|
|
|
|
|
|
|
#include <curl/curl.h>
|
|
|
|
#include "urldata.h"
|
2016-10-31 16:37:30 +08:00
|
|
|
#include "strcase.h"
|
2013-07-16 00:16:13 +08:00
|
|
|
#include "hostcheck.h"
|
2013-12-25 18:20:39 +08:00
|
|
|
#include "vtls/vtls.h"
|
2013-07-16 00:16:13 +08:00
|
|
|
#include "sendf.h"
|
|
|
|
#include "inet_pton.h"
|
|
|
|
#include "curl_base64.h"
|
|
|
|
#include "x509asn1.h"
|
2016-04-29 21:46:40 +08:00
|
|
|
|
|
|
|
/* The last 3 #include files should be in this order */
|
2015-03-03 19:36:18 +08:00
|
|
|
#include "curl_printf.h"
|
2013-07-16 00:16:13 +08:00
|
|
|
#include "curl_memory.h"
|
|
|
|
#include "memdebug.h"
|
|
|
|
|
|
|
|
/* ASN.1 OIDs. */
|
|
|
|
static const char cnOID[] = "2.5.4.3"; /* Common name. */
|
|
|
|
static const char sanOID[] = "2.5.29.17"; /* Subject alternative name. */
|
|
|
|
|
|
|
|
static const curl_OID OIDtable[] = {
|
|
|
|
{ "1.2.840.10040.4.1", "dsa" },
|
|
|
|
{ "1.2.840.10040.4.3", "dsa-with-sha1" },
|
|
|
|
{ "1.2.840.10045.2.1", "ecPublicKey" },
|
|
|
|
{ "1.2.840.10045.3.0.1", "c2pnb163v1" },
|
|
|
|
{ "1.2.840.10045.4.1", "ecdsa-with-SHA1" },
|
|
|
|
{ "1.2.840.10046.2.1", "dhpublicnumber" },
|
|
|
|
{ "1.2.840.113549.1.1.1", "rsaEncryption" },
|
|
|
|
{ "1.2.840.113549.1.1.2", "md2WithRSAEncryption" },
|
|
|
|
{ "1.2.840.113549.1.1.4", "md5WithRSAEncryption" },
|
|
|
|
{ "1.2.840.113549.1.1.5", "sha1WithRSAEncryption" },
|
|
|
|
{ "1.2.840.113549.1.1.10", "RSASSA-PSS" },
|
|
|
|
{ "1.2.840.113549.1.1.14", "sha224WithRSAEncryption" },
|
|
|
|
{ "1.2.840.113549.1.1.11", "sha256WithRSAEncryption" },
|
|
|
|
{ "1.2.840.113549.1.1.12", "sha384WithRSAEncryption" },
|
|
|
|
{ "1.2.840.113549.1.1.13", "sha512WithRSAEncryption" },
|
|
|
|
{ "1.2.840.113549.2.2", "md2" },
|
|
|
|
{ "1.2.840.113549.2.5", "md5" },
|
|
|
|
{ "1.3.14.3.2.26", "sha1" },
|
|
|
|
{ cnOID, "CN" },
|
|
|
|
{ "2.5.4.4", "SN" },
|
|
|
|
{ "2.5.4.5", "serialNumber" },
|
|
|
|
{ "2.5.4.6", "C" },
|
|
|
|
{ "2.5.4.7", "L" },
|
|
|
|
{ "2.5.4.8", "ST" },
|
|
|
|
{ "2.5.4.9", "streetAddress" },
|
|
|
|
{ "2.5.4.10", "O" },
|
|
|
|
{ "2.5.4.11", "OU" },
|
|
|
|
{ "2.5.4.12", "title" },
|
|
|
|
{ "2.5.4.13", "description" },
|
|
|
|
{ "2.5.4.17", "postalCode" },
|
|
|
|
{ "2.5.4.41", "name" },
|
|
|
|
{ "2.5.4.42", "givenName" },
|
|
|
|
{ "2.5.4.43", "initials" },
|
|
|
|
{ "2.5.4.44", "generationQualifier" },
|
|
|
|
{ "2.5.4.45", "X500UniqueIdentifier" },
|
|
|
|
{ "2.5.4.46", "dnQualifier" },
|
|
|
|
{ "2.5.4.65", "pseudonym" },
|
|
|
|
{ "1.2.840.113549.1.9.1", "emailAddress" },
|
|
|
|
{ "2.5.4.72", "role" },
|
|
|
|
{ sanOID, "subjectAltName" },
|
|
|
|
{ "2.5.29.18", "issuerAltName" },
|
|
|
|
{ "2.5.29.19", "basicConstraints" },
|
|
|
|
{ "2.16.840.1.101.3.4.2.4", "sha224" },
|
|
|
|
{ "2.16.840.1.101.3.4.2.1", "sha256" },
|
|
|
|
{ "2.16.840.1.101.3.4.2.2", "sha384" },
|
|
|
|
{ "2.16.840.1.101.3.4.2.3", "sha512" },
|
|
|
|
{ (const char *) NULL, (const char *) NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Lightweight ASN.1 parser.
|
|
|
|
* In particular, it does not check for syntactic/lexical errors.
|
|
|
|
* It is intended to support certificate information gathering for SSL backends
|
|
|
|
* that offer a mean to get certificates as a whole, but do not supply
|
|
|
|
* entry points to get particular certificate sub-fields.
|
|
|
|
* Please note there is no pretention here to rewrite a full SSL library.
|
|
|
|
*/
|
|
|
|
|
2018-10-23 07:04:42 +08:00
|
|
|
static const char *getASN1Element(curl_asn1Element *elem,
|
|
|
|
const char *beg, const char *end)
|
|
|
|
WARN_UNUSED_RESULT;
|
2013-07-16 00:16:13 +08:00
|
|
|
|
2018-08-21 15:44:22 +08:00
|
|
|
static const char *getASN1Element(curl_asn1Element *elem,
|
|
|
|
const char *beg, const char *end)
|
2013-07-16 00:16:13 +08:00
|
|
|
{
|
|
|
|
unsigned char b;
|
|
|
|
unsigned long len;
|
|
|
|
curl_asn1Element lelem;
|
|
|
|
|
|
|
|
/* Get a single ASN.1 element into `elem', parse ASN.1 string at `beg'
|
|
|
|
ending at `end'.
|
|
|
|
Returns a pointer in source string after the parsed element, or NULL
|
|
|
|
if an error occurs. */
|
2016-11-25 08:43:20 +08:00
|
|
|
if(!beg || !end || beg >= end || !*beg ||
|
|
|
|
(size_t)(end - beg) > CURL_ASN1_MAX)
|
2019-02-18 19:28:35 +08:00
|
|
|
return NULL;
|
2013-07-16 00:16:13 +08:00
|
|
|
|
|
|
|
/* Process header byte. */
|
2014-10-14 20:58:26 +08:00
|
|
|
elem->header = beg;
|
2013-07-16 00:16:13 +08:00
|
|
|
b = (unsigned char) *beg++;
|
|
|
|
elem->constructed = (b & 0x20) != 0;
|
|
|
|
elem->class = (b >> 6) & 3;
|
|
|
|
b &= 0x1F;
|
|
|
|
if(b == 0x1F)
|
2019-02-18 19:28:35 +08:00
|
|
|
return NULL; /* Long tag values not supported here. */
|
2013-07-16 00:16:13 +08:00
|
|
|
elem->tag = b;
|
|
|
|
|
|
|
|
/* Process length. */
|
|
|
|
if(beg >= end)
|
2019-02-18 19:28:35 +08:00
|
|
|
return NULL;
|
2013-07-16 00:16:13 +08:00
|
|
|
b = (unsigned char) *beg++;
|
|
|
|
if(!(b & 0x80))
|
|
|
|
len = b;
|
|
|
|
else if(!(b &= 0x7F)) {
|
|
|
|
/* Unspecified length. Since we have all the data, we can determine the
|
|
|
|
effective length by skipping element until an end element is found. */
|
|
|
|
if(!elem->constructed)
|
2019-02-18 19:28:35 +08:00
|
|
|
return NULL;
|
2013-07-16 00:16:13 +08:00
|
|
|
elem->beg = beg;
|
|
|
|
while(beg < end && *beg) {
|
2018-08-21 15:44:22 +08:00
|
|
|
beg = getASN1Element(&lelem, beg, end);
|
2013-07-16 00:16:13 +08:00
|
|
|
if(!beg)
|
2019-02-18 19:28:35 +08:00
|
|
|
return NULL;
|
2013-07-16 00:16:13 +08:00
|
|
|
}
|
|
|
|
if(beg >= end)
|
2019-02-18 19:28:35 +08:00
|
|
|
return NULL;
|
2013-07-16 00:16:13 +08:00
|
|
|
elem->end = beg;
|
|
|
|
return beg + 1;
|
|
|
|
}
|
2016-11-25 08:43:20 +08:00
|
|
|
else if((unsigned)b > (size_t)(end - beg))
|
2019-02-18 19:28:35 +08:00
|
|
|
return NULL; /* Does not fit in source. */
|
2013-07-16 00:16:13 +08:00
|
|
|
else {
|
|
|
|
/* Get long length. */
|
|
|
|
len = 0;
|
|
|
|
do {
|
|
|
|
if(len & 0xFF000000L)
|
2019-02-18 19:28:35 +08:00
|
|
|
return NULL; /* Lengths > 32 bits are not supported. */
|
2013-07-16 00:16:13 +08:00
|
|
|
len = (len << 8) | (unsigned char) *beg++;
|
|
|
|
} while(--b);
|
|
|
|
}
|
2016-11-25 08:43:20 +08:00
|
|
|
if(len > (size_t)(end - beg))
|
2019-02-18 19:28:35 +08:00
|
|
|
return NULL; /* Element data does not fit in source. */
|
2013-07-16 00:16:13 +08:00
|
|
|
elem->beg = beg;
|
|
|
|
elem->end = beg + len;
|
|
|
|
return elem->end;
|
|
|
|
}
|
|
|
|
|
2019-02-18 19:28:35 +08:00
|
|
|
/*
|
|
|
|
* Search the null terminated OID or OID identifier in local table.
|
|
|
|
* Return the table entry pointer or NULL if not found.
|
|
|
|
*/
|
2016-11-23 14:53:24 +08:00
|
|
|
static const curl_OID * searchOID(const char *oid)
|
2013-07-16 00:16:13 +08:00
|
|
|
{
|
2016-11-23 14:53:24 +08:00
|
|
|
const curl_OID *op;
|
2013-07-16 00:16:13 +08:00
|
|
|
for(op = OIDtable; op->numoid; op++)
|
2016-09-30 23:15:05 +08:00
|
|
|
if(!strcmp(op->numoid, oid) || strcasecompare(op->textoid, oid))
|
2013-07-16 00:16:13 +08:00
|
|
|
return op;
|
|
|
|
|
2019-02-18 19:28:35 +08:00
|
|
|
return NULL;
|
2013-07-16 00:16:13 +08:00
|
|
|
}
|
|
|
|
|
2019-02-18 19:28:35 +08:00
|
|
|
/*
|
|
|
|
* Convert an ASN.1 Boolean value into its string representation. Return the
|
|
|
|
* dynamically allocated string, or NULL if source is not an ASN.1 Boolean
|
|
|
|
* value.
|
|
|
|
*/
|
|
|
|
|
2016-11-23 14:53:24 +08:00
|
|
|
static const char *bool2str(const char *beg, const char *end)
|
2013-07-16 00:16:13 +08:00
|
|
|
{
|
|
|
|
if(end - beg != 1)
|
2019-02-18 19:28:35 +08:00
|
|
|
return NULL;
|
2013-07-16 00:16:13 +08:00
|
|
|
return strdup(*beg? "TRUE": "FALSE");
|
|
|
|
}
|
|
|
|
|
2019-02-18 19:28:35 +08:00
|
|
|
/*
|
|
|
|
* Convert an ASN.1 octet string to a printable string.
|
|
|
|
* Return the dynamically allocated string, or NULL if an error occurs.
|
|
|
|
*/
|
2016-11-23 14:53:24 +08:00
|
|
|
static const char *octet2str(const char *beg, const char *end)
|
2013-07-16 00:16:13 +08:00
|
|
|
{
|
|
|
|
size_t n = end - beg;
|
2016-11-23 14:53:24 +08:00
|
|
|
char *buf = NULL;
|
2013-07-16 00:16:13 +08:00
|
|
|
|
2018-08-21 15:44:22 +08:00
|
|
|
if(n <= (SIZE_T_MAX - 1) / 3) {
|
2016-11-24 21:28:39 +08:00
|
|
|
buf = malloc(3 * n + 1);
|
|
|
|
if(buf)
|
|
|
|
for(n = 0; beg < end; n += 3)
|
2018-11-22 16:01:24 +08:00
|
|
|
msnprintf(buf + n, 4, "%02x:", *(const unsigned char *) beg++);
|
2016-11-24 21:28:39 +08:00
|
|
|
}
|
2013-07-16 00:16:13 +08:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2016-11-23 14:53:24 +08:00
|
|
|
static const char *bit2str(const char *beg, const char *end)
|
2013-07-16 00:16:13 +08:00
|
|
|
{
|
|
|
|
/* Convert an ASN.1 bit string to a printable string.
|
|
|
|
Return the dynamically allocated string, or NULL if an error occurs. */
|
|
|
|
|
|
|
|
if(++beg > end)
|
2019-02-18 19:28:35 +08:00
|
|
|
return NULL;
|
2013-07-16 00:16:13 +08:00
|
|
|
return octet2str(beg, end);
|
|
|
|
}
|
|
|
|
|
2019-02-18 19:28:35 +08:00
|
|
|
/*
|
|
|
|
* Convert an ASN.1 integer value into its string representation.
|
|
|
|
* Return the dynamically allocated string, or NULL if source is not an
|
|
|
|
* ASN.1 integer value.
|
|
|
|
*/
|
2016-11-23 14:53:24 +08:00
|
|
|
static const char *int2str(const char *beg, const char *end)
|
2013-07-16 00:16:13 +08:00
|
|
|
{
|
2018-10-27 21:04:50 +08:00
|
|
|
unsigned long val = 0;
|
2013-07-16 00:16:13 +08:00
|
|
|
size_t n = end - beg;
|
|
|
|
|
|
|
|
if(!n)
|
2019-02-18 19:28:35 +08:00
|
|
|
return NULL;
|
2013-07-16 00:16:13 +08:00
|
|
|
|
|
|
|
if(n > 4)
|
|
|
|
return octet2str(beg, end);
|
|
|
|
|
|
|
|
/* Represent integers <= 32-bit as a single value. */
|
|
|
|
if(*beg & 0x80)
|
|
|
|
val = ~val;
|
|
|
|
|
|
|
|
do
|
|
|
|
val = (val << 8) | *(const unsigned char *) beg++;
|
|
|
|
while(beg < end);
|
2018-10-27 21:04:50 +08:00
|
|
|
return curl_maprintf("%s%lx", val >= 10? "0x": "", val);
|
2013-07-16 00:16:13 +08:00
|
|
|
}
|
|
|
|
|
2019-02-18 19:28:35 +08:00
|
|
|
/*
|
|
|
|
* Perform a lazy conversion from an ASN.1 typed string to UTF8. Allocate the
|
|
|
|
* destination buffer dynamically. The allocation size will normally be too
|
|
|
|
* large: this is to avoid buffer overflows.
|
|
|
|
* Terminate the string with a nul byte and return the converted
|
|
|
|
* string length.
|
|
|
|
*/
|
2013-07-16 00:16:13 +08:00
|
|
|
static ssize_t
|
2016-11-23 14:53:24 +08:00
|
|
|
utf8asn1str(char **to, int type, const char *from, const char *end)
|
2013-07-16 00:16:13 +08:00
|
|
|
{
|
|
|
|
size_t inlength = end - from;
|
|
|
|
int size = 1;
|
|
|
|
size_t outlength;
|
2016-11-23 14:53:24 +08:00
|
|
|
char *buf;
|
2013-07-16 00:16:13 +08:00
|
|
|
|
2019-02-18 19:28:35 +08:00
|
|
|
*to = NULL;
|
2016-12-14 06:34:59 +08:00
|
|
|
switch(type) {
|
2013-07-16 00:16:13 +08:00
|
|
|
case CURL_ASN1_BMP_STRING:
|
|
|
|
size = 2;
|
|
|
|
break;
|
|
|
|
case CURL_ASN1_UNIVERSAL_STRING:
|
|
|
|
size = 4;
|
|
|
|
break;
|
|
|
|
case CURL_ASN1_NUMERIC_STRING:
|
|
|
|
case CURL_ASN1_PRINTABLE_STRING:
|
|
|
|
case CURL_ASN1_TELETEX_STRING:
|
|
|
|
case CURL_ASN1_IA5_STRING:
|
|
|
|
case CURL_ASN1_VISIBLE_STRING:
|
|
|
|
case CURL_ASN1_UTF8_STRING:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -1; /* Conversion not supported. */
|
|
|
|
}
|
|
|
|
|
|
|
|
if(inlength % size)
|
|
|
|
return -1; /* Length inconsistent with character size. */
|
2018-08-21 15:44:22 +08:00
|
|
|
if(inlength / size > (SIZE_T_MAX - 1) / 4)
|
2016-11-24 21:28:39 +08:00
|
|
|
return -1; /* Too big. */
|
2013-07-16 00:16:13 +08:00
|
|
|
buf = malloc(4 * (inlength / size) + 1);
|
|
|
|
if(!buf)
|
|
|
|
return -1; /* Not enough memory. */
|
|
|
|
|
|
|
|
if(type == CURL_ASN1_UTF8_STRING) {
|
|
|
|
/* Just copy. */
|
|
|
|
outlength = inlength;
|
|
|
|
if(outlength)
|
|
|
|
memcpy(buf, from, outlength);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for(outlength = 0; from < end;) {
|
2019-05-12 03:42:48 +08:00
|
|
|
int charsize;
|
|
|
|
unsigned int wc;
|
|
|
|
|
2013-07-16 00:16:13 +08:00
|
|
|
wc = 0;
|
2016-12-14 06:34:59 +08:00
|
|
|
switch(size) {
|
2013-07-16 00:16:13 +08:00
|
|
|
case 4:
|
|
|
|
wc = (wc << 8) | *(const unsigned char *) from++;
|
|
|
|
wc = (wc << 8) | *(const unsigned char *) from++;
|
2018-08-22 04:26:36 +08:00
|
|
|
/* FALLTHROUGH */
|
2013-07-16 00:16:13 +08:00
|
|
|
case 2:
|
|
|
|
wc = (wc << 8) | *(const unsigned char *) from++;
|
2018-08-22 04:26:36 +08:00
|
|
|
/* FALLTHROUGH */
|
2013-07-16 00:16:13 +08:00
|
|
|
default: /* case 1: */
|
|
|
|
wc = (wc << 8) | *(const unsigned char *) from++;
|
|
|
|
}
|
2013-11-04 00:24:17 +08:00
|
|
|
charsize = 1;
|
2013-07-16 00:16:13 +08:00
|
|
|
if(wc >= 0x00000080) {
|
|
|
|
if(wc >= 0x00000800) {
|
|
|
|
if(wc >= 0x00010000) {
|
|
|
|
if(wc >= 0x00200000) {
|
|
|
|
free(buf);
|
|
|
|
return -1; /* Invalid char. size for target encoding. */
|
|
|
|
}
|
|
|
|
buf[outlength + 3] = (char) (0x80 | (wc & 0x3F));
|
|
|
|
wc = (wc >> 6) | 0x00010000;
|
2013-11-04 00:24:17 +08:00
|
|
|
charsize++;
|
2013-07-16 00:16:13 +08:00
|
|
|
}
|
|
|
|
buf[outlength + 2] = (char) (0x80 | (wc & 0x3F));
|
|
|
|
wc = (wc >> 6) | 0x00000800;
|
2013-11-04 00:24:17 +08:00
|
|
|
charsize++;
|
2013-07-16 00:16:13 +08:00
|
|
|
}
|
|
|
|
buf[outlength + 1] = (char) (0x80 | (wc & 0x3F));
|
|
|
|
wc = (wc >> 6) | 0x000000C0;
|
2013-11-04 00:24:17 +08:00
|
|
|
charsize++;
|
2013-07-16 00:16:13 +08:00
|
|
|
}
|
|
|
|
buf[outlength] = (char) wc;
|
2013-11-04 00:24:17 +08:00
|
|
|
outlength += charsize;
|
2013-07-16 00:16:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
buf[outlength] = '\0';
|
|
|
|
*to = buf;
|
|
|
|
return outlength;
|
|
|
|
}
|
|
|
|
|
2019-02-18 19:28:35 +08:00
|
|
|
/*
|
|
|
|
* Convert an ASN.1 String into its UTF-8 string representation.
|
|
|
|
* Return the dynamically allocated string, or NULL if an error occurs.
|
|
|
|
*/
|
2016-11-23 14:53:24 +08:00
|
|
|
static const char *string2str(int type, const char *beg, const char *end)
|
2013-07-16 00:16:13 +08:00
|
|
|
{
|
2016-11-23 14:53:24 +08:00
|
|
|
char *buf;
|
2013-07-16 00:16:13 +08:00
|
|
|
if(utf8asn1str(&buf, type, beg, end) < 0)
|
2019-02-18 19:28:35 +08:00
|
|
|
return NULL;
|
2013-07-16 00:16:13 +08:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2019-02-18 19:28:35 +08:00
|
|
|
/*
|
|
|
|
* Decimal ASCII encode unsigned integer `x' into the buflen sized buffer at
|
|
|
|
* buf. Return the total number of encoded digits, even if larger than
|
|
|
|
* `buflen'.
|
|
|
|
*/
|
|
|
|
static size_t encodeUint(char *buf, size_t buflen, unsigned int x)
|
2013-07-16 00:16:13 +08:00
|
|
|
{
|
2019-02-18 19:28:35 +08:00
|
|
|
size_t i = 0;
|
2013-07-16 00:16:13 +08:00
|
|
|
unsigned int y = x / 10;
|
|
|
|
|
|
|
|
if(y) {
|
2019-02-18 19:28:35 +08:00
|
|
|
i = encodeUint(buf, buflen, y);
|
2013-07-16 00:16:13 +08:00
|
|
|
x -= y * 10;
|
|
|
|
}
|
2019-02-18 19:28:35 +08:00
|
|
|
if(i < buflen)
|
2013-07-16 00:16:13 +08:00
|
|
|
buf[i] = (char) ('0' + x);
|
|
|
|
i++;
|
2019-02-18 19:28:35 +08:00
|
|
|
if(i < buflen)
|
2013-07-16 00:16:13 +08:00
|
|
|
buf[i] = '\0'; /* Store a terminator if possible. */
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2019-02-18 19:28:35 +08:00
|
|
|
/*
|
|
|
|
* Convert an ASN.1 OID into its dotted string representation.
|
|
|
|
* Store the result in th `n'-byte buffer at `buf'.
|
|
|
|
* Return the converted string length, or 0 on errors.
|
|
|
|
*/
|
|
|
|
static size_t encodeOID(char *buf, size_t buflen,
|
|
|
|
const char *beg, const char *end)
|
2013-07-16 00:16:13 +08:00
|
|
|
{
|
2019-02-18 19:28:35 +08:00
|
|
|
size_t i;
|
2013-07-16 00:16:13 +08:00
|
|
|
unsigned int x;
|
|
|
|
unsigned int y;
|
|
|
|
|
|
|
|
/* Process the first two numbers. */
|
|
|
|
y = *(const unsigned char *) beg++;
|
|
|
|
x = y / 40;
|
|
|
|
y -= x * 40;
|
2019-02-18 19:28:35 +08:00
|
|
|
i = encodeUint(buf, buflen, x);
|
|
|
|
if(i < buflen)
|
2013-07-16 00:16:13 +08:00
|
|
|
buf[i] = '.';
|
|
|
|
i++;
|
2019-02-18 19:28:35 +08:00
|
|
|
if(i >= buflen)
|
|
|
|
i += encodeUint(NULL, 0, y);
|
|
|
|
else
|
|
|
|
i += encodeUint(buf + i, buflen - i, y);
|
2013-07-16 00:16:13 +08:00
|
|
|
|
|
|
|
/* Process the trailing numbers. */
|
|
|
|
while(beg < end) {
|
2019-02-18 19:28:35 +08:00
|
|
|
if(i < buflen)
|
2013-07-16 00:16:13 +08:00
|
|
|
buf[i] = '.';
|
|
|
|
i++;
|
|
|
|
x = 0;
|
|
|
|
do {
|
|
|
|
if(x & 0xFF000000)
|
2019-02-18 19:28:35 +08:00
|
|
|
return 0;
|
2013-07-16 00:16:13 +08:00
|
|
|
y = *(const unsigned char *) beg++;
|
|
|
|
x = (x << 7) | (y & 0x7F);
|
|
|
|
} while(y & 0x80);
|
2019-02-18 19:28:35 +08:00
|
|
|
if(i >= buflen)
|
|
|
|
i += encodeUint(NULL, 0, x);
|
|
|
|
else
|
|
|
|
i += encodeUint(buf + i, buflen - i, x);
|
2013-07-16 00:16:13 +08:00
|
|
|
}
|
2019-02-18 19:28:35 +08:00
|
|
|
if(i < buflen)
|
2013-07-16 00:16:13 +08:00
|
|
|
buf[i] = '\0';
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2019-02-18 19:28:35 +08:00
|
|
|
/*
|
|
|
|
* Convert an ASN.1 OID into its dotted or symbolic string representation.
|
|
|
|
* Return the dynamically allocated string, or NULL if an error occurs.
|
|
|
|
*/
|
|
|
|
|
2016-11-23 14:53:24 +08:00
|
|
|
static const char *OID2str(const char *beg, const char *end, bool symbolic)
|
2013-07-16 00:16:13 +08:00
|
|
|
{
|
2019-02-18 19:28:35 +08:00
|
|
|
char *buf = NULL;
|
2013-07-16 00:16:13 +08:00
|
|
|
if(beg < end) {
|
2019-02-18 19:28:35 +08:00
|
|
|
size_t buflen = encodeOID(NULL, 0, beg, end);
|
|
|
|
if(buflen) {
|
|
|
|
buf = malloc(buflen + 1); /* one extra for the zero byte */
|
2013-07-16 00:16:13 +08:00
|
|
|
if(buf) {
|
2019-02-18 19:28:35 +08:00
|
|
|
encodeOID(buf, buflen, beg, end);
|
|
|
|
buf[buflen] = '\0';
|
2013-07-16 00:16:13 +08:00
|
|
|
|
|
|
|
if(symbolic) {
|
2019-02-18 19:28:35 +08:00
|
|
|
const curl_OID *op = searchOID(buf);
|
2013-07-16 00:16:13 +08:00
|
|
|
if(op) {
|
|
|
|
free(buf);
|
|
|
|
buf = strdup(op->textoid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2016-11-23 14:53:24 +08:00
|
|
|
static const char *GTime2str(const char *beg, const char *end)
|
2013-07-16 00:16:13 +08:00
|
|
|
{
|
2016-11-23 14:53:24 +08:00
|
|
|
const char *tzp;
|
|
|
|
const char *fracp;
|
2013-07-16 00:16:13 +08:00
|
|
|
char sec1, sec2;
|
|
|
|
size_t fracl;
|
|
|
|
size_t tzl;
|
2016-11-23 14:53:24 +08:00
|
|
|
const char *sep = "";
|
2013-07-16 00:16:13 +08:00
|
|
|
|
|
|
|
/* Convert an ASN.1 Generalized time to a printable string.
|
|
|
|
Return the dynamically allocated string, or NULL if an error occurs. */
|
|
|
|
|
|
|
|
for(fracp = beg; fracp < end && *fracp >= '0' && *fracp <= '9'; fracp++)
|
|
|
|
;
|
|
|
|
|
|
|
|
/* Get seconds digits. */
|
|
|
|
sec1 = '0';
|
2016-12-14 06:34:59 +08:00
|
|
|
switch(fracp - beg - 12) {
|
2013-07-16 00:16:13 +08:00
|
|
|
case 0:
|
|
|
|
sec2 = '0';
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
sec1 = fracp[-2];
|
2017-06-04 02:10:52 +08:00
|
|
|
/* FALLTHROUGH */
|
2013-07-16 00:16:13 +08:00
|
|
|
case 1:
|
|
|
|
sec2 = fracp[-1];
|
|
|
|
break;
|
|
|
|
default:
|
2019-02-18 19:28:35 +08:00
|
|
|
return NULL;
|
2013-07-16 00:16:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Scan for timezone, measure fractional seconds. */
|
|
|
|
tzp = fracp;
|
|
|
|
fracl = 0;
|
|
|
|
if(fracp < end && (*fracp == '.' || *fracp == ',')) {
|
|
|
|
fracp++;
|
|
|
|
do
|
|
|
|
tzp++;
|
|
|
|
while(tzp < end && *tzp >= '0' && *tzp <= '9');
|
|
|
|
/* Strip leading zeroes in fractional seconds. */
|
|
|
|
for(fracl = tzp - fracp - 1; fracl && fracp[fracl - 1] == '0'; fracl--)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Process timezone. */
|
|
|
|
if(tzp >= end)
|
|
|
|
; /* Nothing to do. */
|
|
|
|
else if(*tzp == 'Z') {
|
|
|
|
tzp = " GMT";
|
|
|
|
end = tzp + 4;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sep = " ";
|
|
|
|
tzp++;
|
|
|
|
}
|
|
|
|
|
|
|
|
tzl = end - tzp;
|
|
|
|
return curl_maprintf("%.4s-%.2s-%.2s %.2s:%.2s:%c%c%s%.*s%s%.*s",
|
|
|
|
beg, beg + 4, beg + 6,
|
|
|
|
beg + 8, beg + 10, sec1, sec2,
|
|
|
|
fracl? ".": "", fracl, fracp,
|
|
|
|
sep, tzl, tzp);
|
|
|
|
}
|
|
|
|
|
2019-02-18 19:28:35 +08:00
|
|
|
/*
|
|
|
|
* Convert an ASN.1 UTC time to a printable string.
|
|
|
|
* Return the dynamically allocated string, or NULL if an error occurs.
|
|
|
|
*/
|
2016-11-23 14:53:24 +08:00
|
|
|
static const char *UTime2str(const char *beg, const char *end)
|
2013-07-16 00:16:13 +08:00
|
|
|
{
|
2016-11-23 14:53:24 +08:00
|
|
|
const char *tzp;
|
2013-07-16 00:16:13 +08:00
|
|
|
size_t tzl;
|
2016-11-23 14:53:24 +08:00
|
|
|
const char *sec;
|
2013-07-16 00:16:13 +08:00
|
|
|
|
|
|
|
for(tzp = beg; tzp < end && *tzp >= '0' && *tzp <= '9'; tzp++)
|
|
|
|
;
|
|
|
|
/* Get the seconds. */
|
|
|
|
sec = beg + 10;
|
2016-12-14 06:34:59 +08:00
|
|
|
switch(tzp - sec) {
|
2013-07-16 00:16:13 +08:00
|
|
|
case 0:
|
|
|
|
sec = "00";
|
|
|
|
case 2:
|
|
|
|
break;
|
|
|
|
default:
|
2019-02-18 19:28:35 +08:00
|
|
|
return NULL;
|
2013-07-16 00:16:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Process timezone. */
|
|
|
|
if(tzp >= end)
|
2019-02-18 19:28:35 +08:00
|
|
|
return NULL;
|
2013-07-16 00:16:13 +08:00
|
|
|
if(*tzp == 'Z') {
|
|
|
|
tzp = "GMT";
|
|
|
|
end = tzp + 3;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
tzp++;
|
|
|
|
|
|
|
|
tzl = end - tzp;
|
|
|
|
return curl_maprintf("%u%.2s-%.2s-%.2s %.2s:%.2s:%.2s %.*s",
|
|
|
|
20 - (*beg >= '5'), beg, beg + 2, beg + 4,
|
|
|
|
beg + 6, beg + 8, sec,
|
|
|
|
tzl, tzp);
|
|
|
|
}
|
|
|
|
|
2019-02-18 19:28:35 +08:00
|
|
|
/*
|
|
|
|
* Convert an ASN.1 element to a printable string.
|
|
|
|
* Return the dynamically allocated string, or NULL if an error occurs.
|
|
|
|
*/
|
2018-08-21 15:44:22 +08:00
|
|
|
static const char *ASN1tostr(curl_asn1Element *elem, int type)
|
2013-07-16 00:16:13 +08:00
|
|
|
{
|
|
|
|
if(elem->constructed)
|
2019-02-18 19:28:35 +08:00
|
|
|
return NULL; /* No conversion of structured elements. */
|
2013-07-16 00:16:13 +08:00
|
|
|
|
|
|
|
if(!type)
|
|
|
|
type = elem->tag; /* Type not forced: use element tag as type. */
|
|
|
|
|
2016-12-14 06:34:59 +08:00
|
|
|
switch(type) {
|
2013-07-16 00:16:13 +08:00
|
|
|
case CURL_ASN1_BOOLEAN:
|
|
|
|
return bool2str(elem->beg, elem->end);
|
|
|
|
case CURL_ASN1_INTEGER:
|
|
|
|
case CURL_ASN1_ENUMERATED:
|
|
|
|
return int2str(elem->beg, elem->end);
|
|
|
|
case CURL_ASN1_BIT_STRING:
|
|
|
|
return bit2str(elem->beg, elem->end);
|
|
|
|
case CURL_ASN1_OCTET_STRING:
|
|
|
|
return octet2str(elem->beg, elem->end);
|
|
|
|
case CURL_ASN1_NULL:
|
2015-03-21 23:21:01 +08:00
|
|
|
return strdup("");
|
2013-07-16 00:16:13 +08:00
|
|
|
case CURL_ASN1_OBJECT_IDENTIFIER:
|
|
|
|
return OID2str(elem->beg, elem->end, TRUE);
|
|
|
|
case CURL_ASN1_UTC_TIME:
|
|
|
|
return UTime2str(elem->beg, elem->end);
|
|
|
|
case CURL_ASN1_GENERALIZED_TIME:
|
|
|
|
return GTime2str(elem->beg, elem->end);
|
|
|
|
case CURL_ASN1_UTF8_STRING:
|
|
|
|
case CURL_ASN1_NUMERIC_STRING:
|
|
|
|
case CURL_ASN1_PRINTABLE_STRING:
|
|
|
|
case CURL_ASN1_TELETEX_STRING:
|
|
|
|
case CURL_ASN1_IA5_STRING:
|
|
|
|
case CURL_ASN1_VISIBLE_STRING:
|
|
|
|
case CURL_ASN1_UNIVERSAL_STRING:
|
|
|
|
case CURL_ASN1_BMP_STRING:
|
|
|
|
return string2str(type, elem->beg, elem->end);
|
|
|
|
}
|
|
|
|
|
2019-02-18 19:28:35 +08:00
|
|
|
return NULL; /* Unsupported. */
|
2013-07-16 00:16:13 +08:00
|
|
|
}
|
|
|
|
|
2019-02-18 19:28:35 +08:00
|
|
|
/*
|
|
|
|
* ASCII encode distinguished name at `dn' into the `buflen'-sized buffer at
|
|
|
|
* `buf'. Return the total string length, even if larger than `buflen'.
|
|
|
|
*/
|
|
|
|
static ssize_t encodeDN(char *buf, size_t buflen, curl_asn1Element *dn)
|
2013-07-16 00:16:13 +08:00
|
|
|
{
|
|
|
|
curl_asn1Element rdn;
|
|
|
|
curl_asn1Element atv;
|
|
|
|
curl_asn1Element oid;
|
|
|
|
curl_asn1Element value;
|
|
|
|
size_t l = 0;
|
2016-11-23 14:53:24 +08:00
|
|
|
const char *p1;
|
|
|
|
const char *p2;
|
|
|
|
const char *p3;
|
|
|
|
const char *str;
|
2013-07-16 00:16:13 +08:00
|
|
|
|
|
|
|
for(p1 = dn->beg; p1 < dn->end;) {
|
2018-08-21 15:44:22 +08:00
|
|
|
p1 = getASN1Element(&rdn, p1, dn->end);
|
2018-10-23 07:04:42 +08:00
|
|
|
if(!p1)
|
|
|
|
return -1;
|
2013-07-16 00:16:13 +08:00
|
|
|
for(p2 = rdn.beg; p2 < rdn.end;) {
|
2018-08-21 15:44:22 +08:00
|
|
|
p2 = getASN1Element(&atv, p2, rdn.end);
|
2018-10-23 07:04:42 +08:00
|
|
|
if(!p2)
|
|
|
|
return -1;
|
2018-08-21 15:44:22 +08:00
|
|
|
p3 = getASN1Element(&oid, atv.beg, atv.end);
|
2018-10-23 07:04:42 +08:00
|
|
|
if(!p3)
|
|
|
|
return -1;
|
|
|
|
if(!getASN1Element(&value, p3, atv.end))
|
|
|
|
return -1;
|
2018-08-21 15:44:22 +08:00
|
|
|
str = ASN1tostr(&oid, 0);
|
2013-07-16 00:16:13 +08:00
|
|
|
if(!str)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Encode delimiter.
|
|
|
|
If attribute has a short uppercase name, delimiter is ", ". */
|
|
|
|
if(l) {
|
|
|
|
for(p3 = str; isupper(*p3); p3++)
|
|
|
|
;
|
|
|
|
for(p3 = (*p3 || p3 - str > 2)? "/": ", "; *p3; p3++) {
|
2019-02-18 19:28:35 +08:00
|
|
|
if(l < buflen)
|
2013-07-16 00:16:13 +08:00
|
|
|
buf[l] = *p3;
|
|
|
|
l++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Encode attribute name. */
|
|
|
|
for(p3 = str; *p3; p3++) {
|
2019-02-18 19:28:35 +08:00
|
|
|
if(l < buflen)
|
2013-07-16 00:16:13 +08:00
|
|
|
buf[l] = *p3;
|
|
|
|
l++;
|
|
|
|
}
|
|
|
|
free((char *) str);
|
|
|
|
|
|
|
|
/* Generate equal sign. */
|
2019-02-18 19:28:35 +08:00
|
|
|
if(l < buflen)
|
2013-07-16 00:16:13 +08:00
|
|
|
buf[l] = '=';
|
|
|
|
l++;
|
|
|
|
|
|
|
|
/* Generate value. */
|
2018-08-21 15:44:22 +08:00
|
|
|
str = ASN1tostr(&value, 0);
|
2013-07-16 00:16:13 +08:00
|
|
|
if(!str)
|
|
|
|
return -1;
|
|
|
|
for(p3 = str; *p3; p3++) {
|
2019-02-18 19:28:35 +08:00
|
|
|
if(l < buflen)
|
2013-07-16 00:16:13 +08:00
|
|
|
buf[l] = *p3;
|
|
|
|
l++;
|
|
|
|
}
|
|
|
|
free((char *) str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
2019-02-18 19:28:35 +08:00
|
|
|
/*
|
|
|
|
* Convert an ASN.1 distinguished name into a printable string.
|
|
|
|
* Return the dynamically allocated string, or NULL if an error occurs.
|
|
|
|
*/
|
2018-08-21 15:44:22 +08:00
|
|
|
static const char *DNtostr(curl_asn1Element *dn)
|
2013-07-16 00:16:13 +08:00
|
|
|
{
|
2019-02-18 19:28:35 +08:00
|
|
|
char *buf = NULL;
|
|
|
|
ssize_t buflen = encodeDN(NULL, 0, dn);
|
2013-07-16 00:16:13 +08:00
|
|
|
|
2019-02-18 19:28:35 +08:00
|
|
|
if(buflen >= 0) {
|
|
|
|
buf = malloc(buflen + 1);
|
2013-07-16 00:16:13 +08:00
|
|
|
if(buf) {
|
2019-02-18 19:28:35 +08:00
|
|
|
encodeDN(buf, buflen + 1, dn);
|
|
|
|
buf[buflen] = '\0';
|
2013-07-16 00:16:13 +08:00
|
|
|
}
|
|
|
|
}
|
2019-02-18 19:28:35 +08:00
|
|
|
return buf;
|
2013-07-16 00:16:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-02-18 19:28:35 +08:00
|
|
|
* ASN.1 parse an X509 certificate into structure subfields.
|
|
|
|
* Syntax is assumed to have already been checked by the SSL backend.
|
|
|
|
* See RFC 5280.
|
2013-07-16 00:16:13 +08:00
|
|
|
*/
|
2016-11-23 14:53:24 +08:00
|
|
|
int Curl_parseX509(curl_X509certificate *cert,
|
|
|
|
const char *beg, const char *end)
|
2013-07-16 00:16:13 +08:00
|
|
|
{
|
|
|
|
curl_asn1Element elem;
|
|
|
|
curl_asn1Element tbsCertificate;
|
2016-11-23 14:53:24 +08:00
|
|
|
const char *ccp;
|
2013-07-16 00:16:13 +08:00
|
|
|
static const char defaultVersion = 0; /* v1. */
|
|
|
|
|
2014-10-14 20:58:26 +08:00
|
|
|
cert->certificate.header = NULL;
|
2013-07-16 00:16:13 +08:00
|
|
|
cert->certificate.beg = beg;
|
|
|
|
cert->certificate.end = end;
|
|
|
|
|
|
|
|
/* Get the sequence content. */
|
2018-08-21 15:44:22 +08:00
|
|
|
if(!getASN1Element(&elem, beg, end))
|
2016-11-24 21:28:39 +08:00
|
|
|
return -1; /* Invalid bounds/size. */
|
2013-07-16 00:16:13 +08:00
|
|
|
beg = elem.beg;
|
|
|
|
end = elem.end;
|
|
|
|
|
|
|
|
/* Get tbsCertificate. */
|
2018-08-21 15:44:22 +08:00
|
|
|
beg = getASN1Element(&tbsCertificate, beg, end);
|
2018-10-23 07:04:42 +08:00
|
|
|
if(!beg)
|
|
|
|
return -1;
|
2013-07-16 00:16:13 +08:00
|
|
|
/* Skip the signatureAlgorithm. */
|
2018-08-21 15:44:22 +08:00
|
|
|
beg = getASN1Element(&cert->signatureAlgorithm, beg, end);
|
2018-10-23 07:04:42 +08:00
|
|
|
if(!beg)
|
|
|
|
return -1;
|
2013-07-16 00:16:13 +08:00
|
|
|
/* Get the signatureValue. */
|
2018-10-23 07:04:42 +08:00
|
|
|
if(!getASN1Element(&cert->signature, beg, end))
|
|
|
|
return -1;
|
2013-07-16 00:16:13 +08:00
|
|
|
|
|
|
|
/* Parse TBSCertificate. */
|
|
|
|
beg = tbsCertificate.beg;
|
|
|
|
end = tbsCertificate.end;
|
|
|
|
/* Get optional version, get serialNumber. */
|
2014-10-14 20:58:26 +08:00
|
|
|
cert->version.header = NULL;
|
2013-07-16 00:16:13 +08:00
|
|
|
cert->version.beg = &defaultVersion;
|
2018-05-12 05:40:58 +08:00
|
|
|
cert->version.end = &defaultVersion + sizeof(defaultVersion);
|
2018-08-21 15:44:22 +08:00
|
|
|
beg = getASN1Element(&elem, beg, end);
|
2018-10-23 07:04:42 +08:00
|
|
|
if(!beg)
|
|
|
|
return -1;
|
2013-07-16 00:16:13 +08:00
|
|
|
if(elem.tag == 0) {
|
2018-10-23 07:04:42 +08:00
|
|
|
if(!getASN1Element(&cert->version, elem.beg, elem.end))
|
|
|
|
return -1;
|
2018-08-21 15:44:22 +08:00
|
|
|
beg = getASN1Element(&elem, beg, end);
|
2018-10-23 07:04:42 +08:00
|
|
|
if(!beg)
|
|
|
|
return -1;
|
2013-07-16 00:16:13 +08:00
|
|
|
}
|
|
|
|
cert->serialNumber = elem;
|
|
|
|
/* Get signature algorithm. */
|
2018-08-21 15:44:22 +08:00
|
|
|
beg = getASN1Element(&cert->signatureAlgorithm, beg, end);
|
2013-07-16 00:16:13 +08:00
|
|
|
/* Get issuer. */
|
2018-08-21 15:44:22 +08:00
|
|
|
beg = getASN1Element(&cert->issuer, beg, end);
|
2018-10-23 07:04:42 +08:00
|
|
|
if(!beg)
|
|
|
|
return -1;
|
2013-07-16 00:16:13 +08:00
|
|
|
/* Get notBefore and notAfter. */
|
2018-08-21 15:44:22 +08:00
|
|
|
beg = getASN1Element(&elem, beg, end);
|
2018-10-23 07:04:42 +08:00
|
|
|
if(!beg)
|
|
|
|
return -1;
|
2018-08-21 15:44:22 +08:00
|
|
|
ccp = getASN1Element(&cert->notBefore, elem.beg, elem.end);
|
2018-10-23 07:04:42 +08:00
|
|
|
if(!ccp)
|
|
|
|
return -1;
|
|
|
|
if(!getASN1Element(&cert->notAfter, ccp, elem.end))
|
|
|
|
return -1;
|
2013-07-16 00:16:13 +08:00
|
|
|
/* Get subject. */
|
2018-08-21 15:44:22 +08:00
|
|
|
beg = getASN1Element(&cert->subject, beg, end);
|
2018-10-23 07:04:42 +08:00
|
|
|
if(!beg)
|
|
|
|
return -1;
|
2013-07-16 00:16:13 +08:00
|
|
|
/* Get subjectPublicKeyAlgorithm and subjectPublicKey. */
|
2018-08-21 15:44:22 +08:00
|
|
|
beg = getASN1Element(&cert->subjectPublicKeyInfo, beg, end);
|
2018-10-23 07:04:42 +08:00
|
|
|
if(!beg)
|
|
|
|
return -1;
|
2018-08-21 15:44:22 +08:00
|
|
|
ccp = getASN1Element(&cert->subjectPublicKeyAlgorithm,
|
2018-10-23 07:04:42 +08:00
|
|
|
cert->subjectPublicKeyInfo.beg,
|
|
|
|
cert->subjectPublicKeyInfo.end);
|
|
|
|
if(!ccp)
|
|
|
|
return -1;
|
|
|
|
if(!getASN1Element(&cert->subjectPublicKey, ccp,
|
|
|
|
cert->subjectPublicKeyInfo.end))
|
|
|
|
return -1;
|
2013-07-16 00:16:13 +08:00
|
|
|
/* Get optional issuerUiqueID, subjectUniqueID and extensions. */
|
|
|
|
cert->issuerUniqueID.tag = cert->subjectUniqueID.tag = 0;
|
|
|
|
cert->extensions.tag = elem.tag = 0;
|
2014-10-14 20:58:26 +08:00
|
|
|
cert->issuerUniqueID.header = cert->subjectUniqueID.header = NULL;
|
2013-07-16 00:16:13 +08:00
|
|
|
cert->issuerUniqueID.beg = cert->issuerUniqueID.end = "";
|
|
|
|
cert->subjectUniqueID.beg = cert->subjectUniqueID.end = "";
|
2014-10-14 20:58:26 +08:00
|
|
|
cert->extensions.header = NULL;
|
2013-07-16 00:16:13 +08:00
|
|
|
cert->extensions.beg = cert->extensions.end = "";
|
2018-10-23 07:04:42 +08:00
|
|
|
if(beg < end) {
|
2018-08-21 15:44:22 +08:00
|
|
|
beg = getASN1Element(&elem, beg, end);
|
2018-10-23 07:04:42 +08:00
|
|
|
if(!beg)
|
|
|
|
return -1;
|
|
|
|
}
|
2013-07-16 00:16:13 +08:00
|
|
|
if(elem.tag == 1) {
|
|
|
|
cert->issuerUniqueID = elem;
|
2018-10-23 07:04:42 +08:00
|
|
|
if(beg < end) {
|
2018-08-21 15:44:22 +08:00
|
|
|
beg = getASN1Element(&elem, beg, end);
|
2018-10-23 07:04:42 +08:00
|
|
|
if(!beg)
|
|
|
|
return -1;
|
|
|
|
}
|
2013-07-16 00:16:13 +08:00
|
|
|
}
|
|
|
|
if(elem.tag == 2) {
|
|
|
|
cert->subjectUniqueID = elem;
|
2018-10-23 07:04:42 +08:00
|
|
|
if(beg < end) {
|
2018-08-21 15:44:22 +08:00
|
|
|
beg = getASN1Element(&elem, beg, end);
|
2018-10-23 07:04:42 +08:00
|
|
|
if(!beg)
|
|
|
|
return -1;
|
|
|
|
}
|
2013-07-16 00:16:13 +08:00
|
|
|
}
|
|
|
|
if(elem.tag == 3)
|
2018-10-23 07:04:42 +08:00
|
|
|
if(!getASN1Element(&cert->extensions, elem.beg, elem.end))
|
|
|
|
return -1;
|
2016-11-24 21:28:39 +08:00
|
|
|
return 0;
|
2013-07-16 00:16:13 +08:00
|
|
|
}
|
|
|
|
|
2019-02-18 19:28:35 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy at most 64-characters, terminate with a newline and returns the
|
|
|
|
* effective number of stored characters.
|
|
|
|
*/
|
2016-11-23 14:53:24 +08:00
|
|
|
static size_t copySubstring(char *to, const char *from)
|
2013-07-16 00:16:13 +08:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
for(i = 0; i < 64; i++) {
|
|
|
|
to[i] = *from;
|
|
|
|
if(!*from++)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
to[i++] = '\n';
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2016-11-23 14:53:24 +08:00
|
|
|
static const char *dumpAlgo(curl_asn1Element *param,
|
|
|
|
const char *beg, const char *end)
|
2013-07-16 00:16:13 +08:00
|
|
|
{
|
|
|
|
curl_asn1Element oid;
|
|
|
|
|
|
|
|
/* Get algorithm parameters and return algorithm name. */
|
|
|
|
|
2018-08-21 15:44:22 +08:00
|
|
|
beg = getASN1Element(&oid, beg, end);
|
2018-10-23 07:04:42 +08:00
|
|
|
if(!beg)
|
|
|
|
return NULL;
|
2014-10-14 20:58:26 +08:00
|
|
|
param->header = NULL;
|
2013-07-16 00:16:13 +08:00
|
|
|
param->tag = 0;
|
|
|
|
param->beg = param->end = end;
|
|
|
|
if(beg < end)
|
2018-10-23 07:04:42 +08:00
|
|
|
if(!getASN1Element(param, beg, end))
|
|
|
|
return NULL;
|
2013-07-16 00:16:13 +08:00
|
|
|
return OID2str(oid.beg, oid.end, TRUE);
|
|
|
|
}
|
|
|
|
|
2016-11-23 14:53:24 +08:00
|
|
|
static void do_pubkey_field(struct Curl_easy *data, int certnum,
|
|
|
|
const char *label, curl_asn1Element *elem)
|
2013-07-16 00:16:13 +08:00
|
|
|
{
|
2016-11-23 14:53:24 +08:00
|
|
|
const char *output;
|
2013-07-16 00:16:13 +08:00
|
|
|
|
|
|
|
/* Generate a certificate information record for the public key. */
|
|
|
|
|
2018-08-21 15:44:22 +08:00
|
|
|
output = ASN1tostr(elem, 0);
|
2013-07-16 00:16:13 +08:00
|
|
|
if(output) {
|
2013-10-30 18:12:06 +08:00
|
|
|
if(data->set.ssl.certinfo)
|
|
|
|
Curl_ssl_push_certinfo(data, certnum, label, output);
|
|
|
|
if(!certnum)
|
|
|
|
infof(data, " %s: %s\n", label, output);
|
2013-07-16 00:16:13 +08:00
|
|
|
free((char *) output);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-23 14:53:24 +08:00
|
|
|
static void do_pubkey(struct Curl_easy *data, int certnum,
|
|
|
|
const char *algo, curl_asn1Element *param,
|
|
|
|
curl_asn1Element *pubkey)
|
2013-07-16 00:16:13 +08:00
|
|
|
{
|
|
|
|
curl_asn1Element elem;
|
|
|
|
curl_asn1Element pk;
|
2016-11-23 14:53:24 +08:00
|
|
|
const char *p;
|
2013-07-16 00:16:13 +08:00
|
|
|
|
|
|
|
/* Generate all information records for the public key. */
|
|
|
|
|
|
|
|
/* Get the public key (single element). */
|
2018-10-23 07:04:42 +08:00
|
|
|
if(!getASN1Element(&pk, pubkey->beg + 1, pubkey->end))
|
|
|
|
return;
|
2013-07-16 00:16:13 +08:00
|
|
|
|
2016-09-30 23:15:05 +08:00
|
|
|
if(strcasecompare(algo, "rsaEncryption")) {
|
2019-05-12 03:42:48 +08:00
|
|
|
const char *q;
|
|
|
|
unsigned long len;
|
|
|
|
|
2018-08-21 15:44:22 +08:00
|
|
|
p = getASN1Element(&elem, pk.beg, pk.end);
|
2018-10-23 07:04:42 +08:00
|
|
|
if(!p)
|
|
|
|
return;
|
|
|
|
|
2013-07-16 00:16:13 +08:00
|
|
|
/* Compute key length. */
|
|
|
|
for(q = elem.beg; !*q && q < elem.end; q++)
|
|
|
|
;
|
2015-04-05 14:25:33 +08:00
|
|
|
len = (unsigned long)((elem.end - q) * 8);
|
2019-05-12 03:42:48 +08:00
|
|
|
if(len) {
|
|
|
|
unsigned int i;
|
2013-07-16 00:16:13 +08:00
|
|
|
for(i = *(unsigned char *) q; !(i & 0x80); i <<= 1)
|
|
|
|
len--;
|
2019-05-12 03:42:48 +08:00
|
|
|
}
|
2013-07-16 00:16:13 +08:00
|
|
|
if(len > 32)
|
|
|
|
elem.beg = q; /* Strip leading zero bytes. */
|
2013-10-30 18:12:06 +08:00
|
|
|
if(!certnum)
|
|
|
|
infof(data, " RSA Public Key (%lu bits)\n", len);
|
|
|
|
if(data->set.ssl.certinfo) {
|
|
|
|
q = curl_maprintf("%lu", len);
|
|
|
|
if(q) {
|
|
|
|
Curl_ssl_push_certinfo(data, certnum, "RSA Public Key", q);
|
|
|
|
free((char *) q);
|
|
|
|
}
|
2013-07-16 00:16:13 +08:00
|
|
|
}
|
|
|
|
/* Generate coefficients. */
|
|
|
|
do_pubkey_field(data, certnum, "rsa(n)", &elem);
|
2018-10-23 07:04:42 +08:00
|
|
|
if(!getASN1Element(&elem, p, pk.end))
|
|
|
|
return;
|
2013-07-16 00:16:13 +08:00
|
|
|
do_pubkey_field(data, certnum, "rsa(e)", &elem);
|
|
|
|
}
|
2016-09-30 23:15:05 +08:00
|
|
|
else if(strcasecompare(algo, "dsa")) {
|
2018-08-21 15:44:22 +08:00
|
|
|
p = getASN1Element(&elem, param->beg, param->end);
|
2018-10-23 07:04:42 +08:00
|
|
|
if(p) {
|
|
|
|
do_pubkey_field(data, certnum, "dsa(p)", &elem);
|
|
|
|
p = getASN1Element(&elem, p, param->end);
|
|
|
|
if(p) {
|
|
|
|
do_pubkey_field(data, certnum, "dsa(q)", &elem);
|
|
|
|
if(getASN1Element(&elem, p, param->end)) {
|
|
|
|
do_pubkey_field(data, certnum, "dsa(g)", &elem);
|
|
|
|
do_pubkey_field(data, certnum, "dsa(pub_key)", &pk);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-16 00:16:13 +08:00
|
|
|
}
|
2016-09-30 23:15:05 +08:00
|
|
|
else if(strcasecompare(algo, "dhpublicnumber")) {
|
2018-08-21 15:44:22 +08:00
|
|
|
p = getASN1Element(&elem, param->beg, param->end);
|
2018-10-23 07:04:42 +08:00
|
|
|
if(p) {
|
|
|
|
do_pubkey_field(data, certnum, "dh(p)", &elem);
|
|
|
|
if(getASN1Element(&elem, param->beg, param->end)) {
|
|
|
|
do_pubkey_field(data, certnum, "dh(g)", &elem);
|
|
|
|
do_pubkey_field(data, certnum, "dh(pub_key)", &pk);
|
|
|
|
}
|
|
|
|
}
|
2015-11-24 16:32:42 +08:00
|
|
|
}
|
2013-07-16 00:16:13 +08:00
|
|
|
}
|
|
|
|
|
2016-11-23 14:53:24 +08:00
|
|
|
CURLcode Curl_extract_certinfo(struct connectdata *conn,
|
2013-07-16 00:16:13 +08:00
|
|
|
int certnum,
|
2016-11-23 14:53:24 +08:00
|
|
|
const char *beg,
|
|
|
|
const char *end)
|
2013-07-16 00:16:13 +08:00
|
|
|
{
|
|
|
|
curl_X509certificate cert;
|
2016-11-23 14:53:24 +08:00
|
|
|
struct Curl_easy *data = conn->data;
|
2013-07-16 00:16:13 +08:00
|
|
|
curl_asn1Element param;
|
2016-11-23 14:53:24 +08:00
|
|
|
const char *ccp;
|
|
|
|
char *cp1;
|
2013-07-16 00:16:13 +08:00
|
|
|
size_t cl1;
|
2016-11-23 14:53:24 +08:00
|
|
|
char *cp2;
|
2014-10-24 04:56:35 +08:00
|
|
|
CURLcode result;
|
2013-07-16 00:16:13 +08:00
|
|
|
unsigned long version;
|
|
|
|
size_t i;
|
|
|
|
size_t j;
|
|
|
|
|
2013-10-30 18:12:06 +08:00
|
|
|
if(!data->set.ssl.certinfo)
|
|
|
|
if(certnum)
|
|
|
|
return CURLE_OK;
|
|
|
|
|
2013-07-16 00:16:13 +08:00
|
|
|
/* Prepare the certificate information for curl_easy_getinfo(). */
|
|
|
|
|
|
|
|
/* Extract the certificate ASN.1 elements. */
|
2016-11-24 21:28:39 +08:00
|
|
|
if(Curl_parseX509(&cert, beg, end))
|
2018-08-21 06:10:40 +08:00
|
|
|
return CURLE_PEER_FAILED_VERIFICATION;
|
2013-07-16 00:16:13 +08:00
|
|
|
|
|
|
|
/* Subject. */
|
2018-08-21 15:44:22 +08:00
|
|
|
ccp = DNtostr(&cert.subject);
|
2013-07-16 00:16:13 +08:00
|
|
|
if(!ccp)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2013-10-30 18:12:06 +08:00
|
|
|
if(data->set.ssl.certinfo)
|
|
|
|
Curl_ssl_push_certinfo(data, certnum, "Subject", ccp);
|
|
|
|
if(!certnum)
|
|
|
|
infof(data, "%2d Subject: %s\n", certnum, ccp);
|
2013-07-16 00:16:13 +08:00
|
|
|
free((char *) ccp);
|
|
|
|
|
|
|
|
/* Issuer. */
|
2018-08-21 15:44:22 +08:00
|
|
|
ccp = DNtostr(&cert.issuer);
|
2013-07-16 00:16:13 +08:00
|
|
|
if(!ccp)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2013-10-30 18:12:06 +08:00
|
|
|
if(data->set.ssl.certinfo)
|
|
|
|
Curl_ssl_push_certinfo(data, certnum, "Issuer", ccp);
|
|
|
|
if(!certnum)
|
|
|
|
infof(data, " Issuer: %s\n", ccp);
|
2013-07-16 00:16:13 +08:00
|
|
|
free((char *) ccp);
|
|
|
|
|
|
|
|
/* Version (always fits in less than 32 bits). */
|
|
|
|
version = 0;
|
|
|
|
for(ccp = cert.version.beg; ccp < cert.version.end; ccp++)
|
|
|
|
version = (version << 8) | *(const unsigned char *) ccp;
|
2013-10-30 18:12:06 +08:00
|
|
|
if(data->set.ssl.certinfo) {
|
|
|
|
ccp = curl_maprintf("%lx", version);
|
|
|
|
if(!ccp)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
Curl_ssl_push_certinfo(data, certnum, "Version", ccp);
|
|
|
|
free((char *) ccp);
|
|
|
|
}
|
|
|
|
if(!certnum)
|
|
|
|
infof(data, " Version: %lu (0x%lx)\n", version + 1, version);
|
2013-07-16 00:16:13 +08:00
|
|
|
|
|
|
|
/* Serial number. */
|
2018-08-21 15:44:22 +08:00
|
|
|
ccp = ASN1tostr(&cert.serialNumber, 0);
|
2013-07-16 00:16:13 +08:00
|
|
|
if(!ccp)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2013-10-30 18:12:06 +08:00
|
|
|
if(data->set.ssl.certinfo)
|
|
|
|
Curl_ssl_push_certinfo(data, certnum, "Serial Number", ccp);
|
|
|
|
if(!certnum)
|
|
|
|
infof(data, " Serial Number: %s\n", ccp);
|
2013-07-16 00:16:13 +08:00
|
|
|
free((char *) ccp);
|
|
|
|
|
|
|
|
/* Signature algorithm .*/
|
|
|
|
ccp = dumpAlgo(¶m, cert.signatureAlgorithm.beg,
|
|
|
|
cert.signatureAlgorithm.end);
|
|
|
|
if(!ccp)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2013-10-30 18:12:06 +08:00
|
|
|
if(data->set.ssl.certinfo)
|
|
|
|
Curl_ssl_push_certinfo(data, certnum, "Signature Algorithm", ccp);
|
|
|
|
if(!certnum)
|
|
|
|
infof(data, " Signature Algorithm: %s\n", ccp);
|
2013-07-16 00:16:13 +08:00
|
|
|
free((char *) ccp);
|
|
|
|
|
|
|
|
/* Start Date. */
|
2018-08-21 15:44:22 +08:00
|
|
|
ccp = ASN1tostr(&cert.notBefore, 0);
|
2013-07-16 00:16:13 +08:00
|
|
|
if(!ccp)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2013-10-30 18:12:06 +08:00
|
|
|
if(data->set.ssl.certinfo)
|
|
|
|
Curl_ssl_push_certinfo(data, certnum, "Start Date", ccp);
|
|
|
|
if(!certnum)
|
|
|
|
infof(data, " Start Date: %s\n", ccp);
|
2013-07-16 00:16:13 +08:00
|
|
|
free((char *) ccp);
|
|
|
|
|
|
|
|
/* Expire Date. */
|
2018-08-21 15:44:22 +08:00
|
|
|
ccp = ASN1tostr(&cert.notAfter, 0);
|
2013-07-16 00:16:13 +08:00
|
|
|
if(!ccp)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2013-10-30 18:12:06 +08:00
|
|
|
if(data->set.ssl.certinfo)
|
|
|
|
Curl_ssl_push_certinfo(data, certnum, "Expire Date", ccp);
|
|
|
|
if(!certnum)
|
|
|
|
infof(data, " Expire Date: %s\n", ccp);
|
2013-07-16 00:16:13 +08:00
|
|
|
free((char *) ccp);
|
|
|
|
|
|
|
|
/* Public Key Algorithm. */
|
|
|
|
ccp = dumpAlgo(¶m, cert.subjectPublicKeyAlgorithm.beg,
|
|
|
|
cert.subjectPublicKeyAlgorithm.end);
|
|
|
|
if(!ccp)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2013-10-30 18:12:06 +08:00
|
|
|
if(data->set.ssl.certinfo)
|
|
|
|
Curl_ssl_push_certinfo(data, certnum, "Public Key Algorithm", ccp);
|
|
|
|
if(!certnum)
|
|
|
|
infof(data, " Public Key Algorithm: %s\n", ccp);
|
2013-07-16 00:16:13 +08:00
|
|
|
do_pubkey(data, certnum, ccp, ¶m, &cert.subjectPublicKey);
|
|
|
|
free((char *) ccp);
|
|
|
|
|
|
|
|
/* Signature. */
|
2018-08-21 15:44:22 +08:00
|
|
|
ccp = ASN1tostr(&cert.signature, 0);
|
2013-07-16 00:16:13 +08:00
|
|
|
if(!ccp)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2013-10-30 18:12:06 +08:00
|
|
|
if(data->set.ssl.certinfo)
|
|
|
|
Curl_ssl_push_certinfo(data, certnum, "Signature", ccp);
|
|
|
|
if(!certnum)
|
|
|
|
infof(data, " Signature: %s\n", ccp);
|
2013-07-16 00:16:13 +08:00
|
|
|
free((char *) ccp);
|
|
|
|
|
|
|
|
/* Generate PEM certificate. */
|
2014-10-24 04:56:35 +08:00
|
|
|
result = Curl_base64_encode(data, cert.certificate.beg,
|
|
|
|
cert.certificate.end - cert.certificate.beg,
|
|
|
|
&cp1, &cl1);
|
|
|
|
if(result)
|
|
|
|
return result;
|
2013-10-30 18:12:06 +08:00
|
|
|
/* Compute the number of characters in final certificate string. Format is:
|
2013-07-16 00:16:13 +08:00
|
|
|
-----BEGIN CERTIFICATE-----\n
|
|
|
|
<max 64 base64 characters>\n
|
|
|
|
.
|
|
|
|
.
|
|
|
|
.
|
|
|
|
-----END CERTIFICATE-----\n
|
|
|
|
*/
|
|
|
|
i = 28 + cl1 + (cl1 + 64 - 1) / 64 + 26;
|
|
|
|
cp2 = malloc(i + 1);
|
|
|
|
if(!cp2) {
|
|
|
|
free(cp1);
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
/* Build the certificate string. */
|
|
|
|
i = copySubstring(cp2, "-----BEGIN CERTIFICATE-----");
|
|
|
|
for(j = 0; j < cl1; j += 64)
|
|
|
|
i += copySubstring(cp2 + i, cp1 + j);
|
|
|
|
i += copySubstring(cp2 + i, "-----END CERTIFICATE-----");
|
|
|
|
cp2[i] = '\0';
|
|
|
|
free(cp1);
|
2013-10-30 18:12:06 +08:00
|
|
|
if(data->set.ssl.certinfo)
|
|
|
|
Curl_ssl_push_certinfo(data, certnum, "Cert", cp2);
|
|
|
|
if(!certnum)
|
|
|
|
infof(data, "%s\n", cp2);
|
2013-07-16 00:16:13 +08:00
|
|
|
free(cp2);
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2019-06-02 22:55:05 +08:00
|
|
|
#endif /* USE_GSKIT or USE_NSS or USE_GNUTLS or USE_WOLFSSL or USE_SCHANNEL */
|
2014-03-03 15:44:25 +08:00
|
|
|
|
2014-10-13 22:33:47 +08:00
|
|
|
#if defined(USE_GSKIT)
|
2014-03-03 15:44:25 +08:00
|
|
|
|
2016-11-23 14:53:24 +08:00
|
|
|
static const char *checkOID(const char *beg, const char *end,
|
|
|
|
const char *oid)
|
2014-03-03 15:44:25 +08:00
|
|
|
{
|
|
|
|
curl_asn1Element e;
|
2016-11-23 14:53:24 +08:00
|
|
|
const char *ccp;
|
|
|
|
const char *p;
|
2014-03-03 15:44:25 +08:00
|
|
|
bool matched;
|
|
|
|
|
|
|
|
/* Check if first ASN.1 element at `beg' is the given OID.
|
|
|
|
Return a pointer in the source after the OID if found, else NULL. */
|
|
|
|
|
2018-08-21 15:44:22 +08:00
|
|
|
ccp = getASN1Element(&e, beg, end);
|
2014-03-03 15:44:25 +08:00
|
|
|
if(!ccp || e.tag != CURL_ASN1_OBJECT_IDENTIFIER)
|
2019-02-18 19:28:35 +08:00
|
|
|
return NULL;
|
2014-03-03 15:44:25 +08:00
|
|
|
|
|
|
|
p = OID2str(e.beg, e.end, FALSE);
|
|
|
|
if(!p)
|
2019-02-18 19:28:35 +08:00
|
|
|
return NULL;
|
2014-03-03 15:44:25 +08:00
|
|
|
|
|
|
|
matched = !strcmp(p, oid);
|
|
|
|
free((char *) p);
|
2019-02-18 19:28:35 +08:00
|
|
|
return matched? ccp: NULL;
|
2014-03-03 15:44:25 +08:00
|
|
|
}
|
2013-07-16 00:16:13 +08:00
|
|
|
|
2016-11-23 14:53:24 +08:00
|
|
|
CURLcode Curl_verifyhost(struct connectdata *conn,
|
|
|
|
const char *beg, const char *end)
|
2013-07-16 00:16:13 +08:00
|
|
|
{
|
2016-11-23 14:53:24 +08:00
|
|
|
struct Curl_easy *data = conn->data;
|
2013-07-16 00:16:13 +08:00
|
|
|
curl_X509certificate cert;
|
|
|
|
curl_asn1Element dn;
|
|
|
|
curl_asn1Element elem;
|
|
|
|
curl_asn1Element ext;
|
|
|
|
curl_asn1Element name;
|
2016-11-23 14:53:24 +08:00
|
|
|
const char *p;
|
|
|
|
const char *q;
|
|
|
|
char *dnsname;
|
2013-07-16 00:16:13 +08:00
|
|
|
int matched = -1;
|
|
|
|
size_t addrlen = (size_t) -1;
|
|
|
|
ssize_t len;
|
2016-11-17 01:49:15 +08:00
|
|
|
const char * const hostname = SSL_IS_PROXY()? conn->http_proxy.host.name:
|
|
|
|
conn->host.name;
|
|
|
|
const char * const dispname = SSL_IS_PROXY()?
|
|
|
|
conn->http_proxy.host.dispname:
|
|
|
|
conn->host.dispname;
|
2013-07-16 00:16:13 +08:00
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
struct in6_addr addr;
|
|
|
|
#else
|
|
|
|
struct in_addr addr;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Verify that connection server matches info in X509 certificate at
|
|
|
|
`beg'..`end'. */
|
|
|
|
|
2016-11-17 01:49:15 +08:00
|
|
|
if(!SSL_CONN_CONFIG(verifyhost))
|
2013-07-16 00:16:13 +08:00
|
|
|
return CURLE_OK;
|
|
|
|
|
2016-11-24 21:28:39 +08:00
|
|
|
if(Curl_parseX509(&cert, beg, end))
|
2013-07-16 00:16:13 +08:00
|
|
|
return CURLE_PEER_FAILED_VERIFICATION;
|
|
|
|
|
|
|
|
/* Get the server IP address. */
|
|
|
|
#ifdef ENABLE_IPV6
|
2016-11-17 01:49:15 +08:00
|
|
|
if(conn->bits.ipv6_ip && Curl_inet_pton(AF_INET6, hostname, &addr))
|
2013-07-16 00:16:13 +08:00
|
|
|
addrlen = sizeof(struct in6_addr);
|
|
|
|
else
|
|
|
|
#endif
|
2016-11-17 01:49:15 +08:00
|
|
|
if(Curl_inet_pton(AF_INET, hostname, &addr))
|
2013-07-16 00:16:13 +08:00
|
|
|
addrlen = sizeof(struct in_addr);
|
|
|
|
|
|
|
|
/* Process extensions. */
|
|
|
|
for(p = cert.extensions.beg; p < cert.extensions.end && matched != 1;) {
|
2018-08-21 15:44:22 +08:00
|
|
|
p = getASN1Element(&ext, p, cert.extensions.end);
|
2018-10-23 07:04:42 +08:00
|
|
|
if(!p)
|
|
|
|
return CURLE_PEER_FAILED_VERIFICATION;
|
|
|
|
|
2013-07-16 00:16:13 +08:00
|
|
|
/* Check if extension is a subjectAlternativeName. */
|
|
|
|
ext.beg = checkOID(ext.beg, ext.end, sanOID);
|
|
|
|
if(ext.beg) {
|
2018-08-21 15:44:22 +08:00
|
|
|
ext.beg = getASN1Element(&elem, ext.beg, ext.end);
|
2018-10-23 07:04:42 +08:00
|
|
|
if(!ext.beg)
|
|
|
|
return CURLE_PEER_FAILED_VERIFICATION;
|
2013-07-16 00:16:13 +08:00
|
|
|
/* Skip critical if present. */
|
2018-10-23 07:04:42 +08:00
|
|
|
if(elem.tag == CURL_ASN1_BOOLEAN) {
|
2018-08-21 15:44:22 +08:00
|
|
|
ext.beg = getASN1Element(&elem, ext.beg, ext.end);
|
2018-10-23 07:04:42 +08:00
|
|
|
if(!ext.beg)
|
|
|
|
return CURLE_PEER_FAILED_VERIFICATION;
|
|
|
|
}
|
2013-07-16 00:16:13 +08:00
|
|
|
/* Parse the octet string contents: is a single sequence. */
|
2018-10-23 07:04:42 +08:00
|
|
|
if(!getASN1Element(&elem, elem.beg, elem.end))
|
|
|
|
return CURLE_PEER_FAILED_VERIFICATION;
|
2013-07-16 00:16:13 +08:00
|
|
|
/* Check all GeneralNames. */
|
|
|
|
for(q = elem.beg; matched != 1 && q < elem.end;) {
|
2018-08-21 15:44:22 +08:00
|
|
|
q = getASN1Element(&name, q, elem.end);
|
2018-10-23 07:04:42 +08:00
|
|
|
if(!q)
|
|
|
|
break;
|
2016-12-14 06:34:59 +08:00
|
|
|
switch(name.tag) {
|
2013-07-16 00:16:13 +08:00
|
|
|
case 2: /* DNS name. */
|
|
|
|
len = utf8asn1str(&dnsname, CURL_ASN1_IA5_STRING,
|
|
|
|
name.beg, name.end);
|
2015-12-15 05:43:08 +08:00
|
|
|
if(len > 0 && (size_t)len == strlen(dnsname))
|
2016-11-17 01:49:15 +08:00
|
|
|
matched = Curl_cert_hostcheck(dnsname, hostname);
|
2015-12-15 05:43:08 +08:00
|
|
|
else
|
|
|
|
matched = 0;
|
2015-03-12 00:41:01 +08:00
|
|
|
free(dnsname);
|
2013-07-16 00:16:13 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 7: /* IP address. */
|
2018-10-15 23:27:28 +08:00
|
|
|
matched = (size_t) (name.end - name.beg) == addrlen &&
|
|
|
|
!memcmp(&addr, name.beg, addrlen);
|
2013-07-16 00:16:13 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-14 06:34:59 +08:00
|
|
|
switch(matched) {
|
2013-07-16 00:16:13 +08:00
|
|
|
case 1:
|
|
|
|
/* an alternative name matched the server hostname */
|
2016-11-17 01:49:15 +08:00
|
|
|
infof(data, "\t subjectAltName: %s matched\n", dispname);
|
2013-07-16 00:16:13 +08:00
|
|
|
return CURLE_OK;
|
|
|
|
case 0:
|
|
|
|
/* an alternative name field existed, but didn't match and then
|
|
|
|
we MUST fail */
|
2016-11-17 01:49:15 +08:00
|
|
|
infof(data, "\t subjectAltName does not match %s\n", dispname);
|
2013-07-16 00:16:13 +08:00
|
|
|
return CURLE_PEER_FAILED_VERIFICATION;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Process subject. */
|
2014-10-14 20:58:26 +08:00
|
|
|
name.header = NULL;
|
2013-07-16 00:16:13 +08:00
|
|
|
name.beg = name.end = "";
|
|
|
|
q = cert.subject.beg;
|
|
|
|
/* we have to look to the last occurrence of a commonName in the
|
|
|
|
distinguished one to get the most significant one. */
|
|
|
|
while(q < cert.subject.end) {
|
2018-08-21 15:44:22 +08:00
|
|
|
q = getASN1Element(&dn, q, cert.subject.end);
|
2018-10-23 07:04:42 +08:00
|
|
|
if(!q)
|
|
|
|
break;
|
2013-07-16 00:16:13 +08:00
|
|
|
for(p = dn.beg; p < dn.end;) {
|
2018-08-21 15:44:22 +08:00
|
|
|
p = getASN1Element(&elem, p, dn.end);
|
2018-10-23 07:04:42 +08:00
|
|
|
if(!p)
|
|
|
|
return CURLE_PEER_FAILED_VERIFICATION;
|
2013-07-16 00:16:13 +08:00
|
|
|
/* We have a DN's AttributeTypeAndValue: check it in case it's a CN. */
|
|
|
|
elem.beg = checkOID(elem.beg, elem.end, cnOID);
|
|
|
|
if(elem.beg)
|
|
|
|
name = elem; /* Latch CN. */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check the CN if found. */
|
2018-08-21 15:44:22 +08:00
|
|
|
if(!getASN1Element(&elem, name.beg, name.end))
|
2013-07-16 00:16:13 +08:00
|
|
|
failf(data, "SSL: unable to obtain common name from peer certificate");
|
|
|
|
else {
|
|
|
|
len = utf8asn1str(&dnsname, elem.tag, elem.beg, elem.end);
|
|
|
|
if(len < 0) {
|
|
|
|
free(dnsname);
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
if(strlen(dnsname) != (size_t) len) /* Nul byte in string ? */
|
|
|
|
failf(data, "SSL: illegal cert name field");
|
2016-11-17 01:49:15 +08:00
|
|
|
else if(Curl_cert_hostcheck((const char *) dnsname, hostname)) {
|
2013-07-16 00:16:13 +08:00
|
|
|
infof(data, "\t common name: %s (matched)\n", dnsname);
|
|
|
|
free(dnsname);
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
failf(data, "SSL: certificate subject name '%s' does not match "
|
2016-11-17 01:49:15 +08:00
|
|
|
"target host name '%s'", dnsname, dispname);
|
2013-07-16 00:16:13 +08:00
|
|
|
free(dnsname);
|
|
|
|
}
|
|
|
|
|
|
|
|
return CURLE_PEER_FAILED_VERIFICATION;
|
|
|
|
}
|
|
|
|
|
2014-10-13 22:33:47 +08:00
|
|
|
#endif /* USE_GSKIT */
|