1 | /***************************************************************************
|
---|
2 | * _ _ ____ _
|
---|
3 | * Project ___| | | | _ \| |
|
---|
4 | * / __| | | | |_) | |
|
---|
5 | * | (__| |_| | _ <| |___
|
---|
6 | * \___|\___/|_| \_\_____|
|
---|
7 | *
|
---|
8 | * Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
|
---|
9 | *
|
---|
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 https://curl.haxx.se/docs/copyright.html.
|
---|
13 | *
|
---|
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.
|
---|
17 | *
|
---|
18 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
---|
19 | * KIND, either express or implied.
|
---|
20 | *
|
---|
21 | ***************************************************************************/
|
---|
22 |
|
---|
23 | #include "curl_setup.h"
|
---|
24 |
|
---|
25 | #if defined(USE_GSKIT) || defined(USE_NSS) || defined(USE_GNUTLS) || \
|
---|
26 | defined(USE_CYASSL) || defined(USE_SCHANNEL)
|
---|
27 |
|
---|
28 | #include <curl/curl.h>
|
---|
29 | #include "urldata.h"
|
---|
30 | #include "strcase.h"
|
---|
31 | #include "hostcheck.h"
|
---|
32 | #include "vtls/vtls.h"
|
---|
33 | #include "sendf.h"
|
---|
34 | #include "inet_pton.h"
|
---|
35 | #include "curl_base64.h"
|
---|
36 | #include "x509asn1.h"
|
---|
37 |
|
---|
38 | /* The last 3 #include files should be in this order */
|
---|
39 | #include "curl_printf.h"
|
---|
40 | #include "curl_memory.h"
|
---|
41 | #include "memdebug.h"
|
---|
42 |
|
---|
43 | /* ASN.1 OIDs. */
|
---|
44 | static const char cnOID[] = "2.5.4.3"; /* Common name. */
|
---|
45 | static const char sanOID[] = "2.5.29.17"; /* Subject alternative name. */
|
---|
46 |
|
---|
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" },
|
---|
66 | { cnOID, "CN" },
|
---|
67 | { "2.5.4.4", "SN" },
|
---|
68 | { "2.5.4.5", "serialNumber" },
|
---|
69 | { "2.5.4.6", "C" },
|
---|
70 | { "2.5.4.7", "L" },
|
---|
71 | { "2.5.4.8", "ST" },
|
---|
72 | { "2.5.4.9", "streetAddress" },
|
---|
73 | { "2.5.4.10", "O" },
|
---|
74 | { "2.5.4.11", "OU" },
|
---|
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 }
|
---|
95 | };
|
---|
96 |
|
---|
97 | /*
|
---|
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.
|
---|
104 | */
|
---|
105 |
|
---|
106 | static const char *getASN1Element(curl_asn1Element *elem,
|
---|
107 | const char *beg, const char *end)
|
---|
108 | WARN_UNUSED_RESULT;
|
---|
109 |
|
---|
110 | static const char *getASN1Element(curl_asn1Element *elem,
|
---|
111 | const char *beg, const char *end)
|
---|
112 | {
|
---|
113 | unsigned char b;
|
---|
114 | unsigned long len;
|
---|
115 | curl_asn1Element lelem;
|
---|
116 |
|
---|
117 | /* Get a single ASN.1 element into `elem', parse ASN.1 string at `beg'
|
---|
118 | ending at `end'.
|
---|
119 | Returns a pointer in source string after the parsed element, or NULL
|
---|
120 | if an error occurs. */
|
---|
121 | if(!beg || !end || beg >= end || !*beg ||
|
---|
122 | (size_t)(end - beg) > CURL_ASN1_MAX)
|
---|
123 | return (const char *) NULL;
|
---|
124 |
|
---|
125 | /* Process header byte. */
|
---|
126 | elem->header = beg;
|
---|
127 | b = (unsigned char) *beg++;
|
---|
128 | elem->constructed = (b & 0x20) != 0;
|
---|
129 | elem->class = (b >> 6) & 3;
|
---|
130 | b &= 0x1F;
|
---|
131 | if(b == 0x1F)
|
---|
132 | return (const char *) NULL; /* Long tag values not supported here. */
|
---|
133 | elem->tag = b;
|
---|
134 |
|
---|
135 | /* Process length. */
|
---|
136 | if(beg >= end)
|
---|
137 | return (const char *) NULL;
|
---|
138 | b = (unsigned char) *beg++;
|
---|
139 | if(!(b & 0x80))
|
---|
140 | len = b;
|
---|
141 | else if(!(b &= 0x7F)) {
|
---|
142 | /* Unspecified length. Since we have all the data, we can determine the
|
---|
143 | effective length by skipping element until an end element is found. */
|
---|
144 | if(!elem->constructed)
|
---|
145 | return (const char *) NULL;
|
---|
146 | elem->beg = beg;
|
---|
147 | while(beg < end && *beg) {
|
---|
148 | beg = getASN1Element(&lelem, beg, end);
|
---|
149 | if(!beg)
|
---|
150 | return (const char *) NULL;
|
---|
151 | }
|
---|
152 | if(beg >= end)
|
---|
153 | return (const char *) NULL;
|
---|
154 | elem->end = beg;
|
---|
155 | return beg + 1;
|
---|
156 | }
|
---|
157 | else if((unsigned)b > (size_t)(end - beg))
|
---|
158 | return (const char *) NULL; /* Does not fit in source. */
|
---|
159 | else {
|
---|
160 | /* Get long length. */
|
---|
161 | len = 0;
|
---|
162 | do {
|
---|
163 | if(len & 0xFF000000L)
|
---|
164 | return (const char *) NULL; /* Lengths > 32 bits are not supported. */
|
---|
165 | len = (len << 8) | (unsigned char) *beg++;
|
---|
166 | } while(--b);
|
---|
167 | }
|
---|
168 | if(len > (size_t)(end - beg))
|
---|
169 | return (const char *) NULL; /* Element data does not fit in source. */
|
---|
170 | elem->beg = beg;
|
---|
171 | elem->end = beg + len;
|
---|
172 | return elem->end;
|
---|
173 | }
|
---|
174 |
|
---|
175 | static const curl_OID * searchOID(const char *oid)
|
---|
176 | {
|
---|
177 | const curl_OID *op;
|
---|
178 |
|
---|
179 | /* Search the null terminated OID or OID identifier in local table.
|
---|
180 | Return the table entry pointer or NULL if not found. */
|
---|
181 |
|
---|
182 | for(op = OIDtable; op->numoid; op++)
|
---|
183 | if(!strcmp(op->numoid, oid) || strcasecompare(op->textoid, oid))
|
---|
184 | return op;
|
---|
185 |
|
---|
186 | return (const curl_OID *) NULL;
|
---|
187 | }
|
---|
188 |
|
---|
189 | static const char *bool2str(const char *beg, const char *end)
|
---|
190 | {
|
---|
191 | /* Convert an ASN.1 Boolean value into its string representation.
|
---|
192 | Return the dynamically allocated string, or NULL if source is not an
|
---|
193 | ASN.1 Boolean value. */
|
---|
194 |
|
---|
195 | if(end - beg != 1)
|
---|
196 | return (const char *) NULL;
|
---|
197 | return strdup(*beg? "TRUE": "FALSE");
|
---|
198 | }
|
---|
199 |
|
---|
200 | static const char *octet2str(const char *beg, const char *end)
|
---|
201 | {
|
---|
202 | size_t n = end - beg;
|
---|
203 | char *buf = NULL;
|
---|
204 |
|
---|
205 | /* Convert an ASN.1 octet string to a printable string.
|
---|
206 | Return the dynamically allocated string, or NULL if an error occurs. */
|
---|
207 |
|
---|
208 | if(n <= (SIZE_T_MAX - 1) / 3) {
|
---|
209 | buf = malloc(3 * n + 1);
|
---|
210 | if(buf)
|
---|
211 | for(n = 0; beg < end; n += 3)
|
---|
212 | msnprintf(buf + n, 4, "%02x:", *(const unsigned char *) beg++);
|
---|
213 | }
|
---|
214 | return buf;
|
---|
215 | }
|
---|
216 |
|
---|
217 | static const char *bit2str(const char *beg, const char *end)
|
---|
218 | {
|
---|
219 | /* Convert an ASN.1 bit string to a printable string.
|
---|
220 | Return the dynamically allocated string, or NULL if an error occurs. */
|
---|
221 |
|
---|
222 | if(++beg > end)
|
---|
223 | return (const char *) NULL;
|
---|
224 | return octet2str(beg, end);
|
---|
225 | }
|
---|
226 |
|
---|
227 | static const char *int2str(const char *beg, const char *end)
|
---|
228 | {
|
---|
229 | unsigned long val = 0;
|
---|
230 | size_t n = end - beg;
|
---|
231 |
|
---|
232 | /* Convert an ASN.1 integer value into its string representation.
|
---|
233 | Return the dynamically allocated string, or NULL if source is not an
|
---|
234 | ASN.1 integer value. */
|
---|
235 |
|
---|
236 | if(!n)
|
---|
237 | return (const char *) NULL;
|
---|
238 |
|
---|
239 | if(n > 4)
|
---|
240 | return octet2str(beg, end);
|
---|
241 |
|
---|
242 | /* Represent integers <= 32-bit as a single value. */
|
---|
243 | if(*beg & 0x80)
|
---|
244 | val = ~val;
|
---|
245 |
|
---|
246 | do
|
---|
247 | val = (val << 8) | *(const unsigned char *) beg++;
|
---|
248 | while(beg < end);
|
---|
249 | return curl_maprintf("%s%lx", val >= 10? "0x": "", val);
|
---|
250 | }
|
---|
251 |
|
---|
252 | static ssize_t
|
---|
253 | utf8asn1str(char **to, int type, const char *from, const char *end)
|
---|
254 | {
|
---|
255 | size_t inlength = end - from;
|
---|
256 | int size = 1;
|
---|
257 | size_t outlength;
|
---|
258 | int charsize;
|
---|
259 | unsigned int wc;
|
---|
260 | char *buf;
|
---|
261 |
|
---|
262 | /* Perform a lazy conversion from an ASN.1 typed string to UTF8. Allocate the
|
---|
263 | destination buffer dynamically. The allocation size will normally be too
|
---|
264 | large: this is to avoid buffer overflows.
|
---|
265 | Terminate the string with a nul byte and return the converted
|
---|
266 | string length. */
|
---|
267 |
|
---|
268 | *to = (char *) NULL;
|
---|
269 | switch(type) {
|
---|
270 | case CURL_ASN1_BMP_STRING:
|
---|
271 | size = 2;
|
---|
272 | break;
|
---|
273 | case CURL_ASN1_UNIVERSAL_STRING:
|
---|
274 | size = 4;
|
---|
275 | break;
|
---|
276 | case CURL_ASN1_NUMERIC_STRING:
|
---|
277 | case CURL_ASN1_PRINTABLE_STRING:
|
---|
278 | case CURL_ASN1_TELETEX_STRING:
|
---|
279 | case CURL_ASN1_IA5_STRING:
|
---|
280 | case CURL_ASN1_VISIBLE_STRING:
|
---|
281 | case CURL_ASN1_UTF8_STRING:
|
---|
282 | break;
|
---|
283 | default:
|
---|
284 | return -1; /* Conversion not supported. */
|
---|
285 | }
|
---|
286 |
|
---|
287 | if(inlength % size)
|
---|
288 | return -1; /* Length inconsistent with character size. */
|
---|
289 | if(inlength / size > (SIZE_T_MAX - 1) / 4)
|
---|
290 | return -1; /* Too big. */
|
---|
291 | buf = malloc(4 * (inlength / size) + 1);
|
---|
292 | if(!buf)
|
---|
293 | return -1; /* Not enough memory. */
|
---|
294 |
|
---|
295 | if(type == CURL_ASN1_UTF8_STRING) {
|
---|
296 | /* Just copy. */
|
---|
297 | outlength = inlength;
|
---|
298 | if(outlength)
|
---|
299 | memcpy(buf, from, outlength);
|
---|
300 | }
|
---|
301 | else {
|
---|
302 | for(outlength = 0; from < end;) {
|
---|
303 | wc = 0;
|
---|
304 | switch(size) {
|
---|
305 | case 4:
|
---|
306 | wc = (wc << 8) | *(const unsigned char *) from++;
|
---|
307 | wc = (wc << 8) | *(const unsigned char *) from++;
|
---|
308 | /* FALLTHROUGH */
|
---|
309 | case 2:
|
---|
310 | wc = (wc << 8) | *(const unsigned char *) from++;
|
---|
311 | /* FALLTHROUGH */
|
---|
312 | default: /* case 1: */
|
---|
313 | wc = (wc << 8) | *(const unsigned char *) from++;
|
---|
314 | }
|
---|
315 | charsize = 1;
|
---|
316 | if(wc >= 0x00000080) {
|
---|
317 | if(wc >= 0x00000800) {
|
---|
318 | if(wc >= 0x00010000) {
|
---|
319 | if(wc >= 0x00200000) {
|
---|
320 | free(buf);
|
---|
321 | return -1; /* Invalid char. size for target encoding. */
|
---|
322 | }
|
---|
323 | buf[outlength + 3] = (char) (0x80 | (wc & 0x3F));
|
---|
324 | wc = (wc >> 6) | 0x00010000;
|
---|
325 | charsize++;
|
---|
326 | }
|
---|
327 | buf[outlength + 2] = (char) (0x80 | (wc & 0x3F));
|
---|
328 | wc = (wc >> 6) | 0x00000800;
|
---|
329 | charsize++;
|
---|
330 | }
|
---|
331 | buf[outlength + 1] = (char) (0x80 | (wc & 0x3F));
|
---|
332 | wc = (wc >> 6) | 0x000000C0;
|
---|
333 | charsize++;
|
---|
334 | }
|
---|
335 | buf[outlength] = (char) wc;
|
---|
336 | outlength += charsize;
|
---|
337 | }
|
---|
338 | }
|
---|
339 | buf[outlength] = '\0';
|
---|
340 | *to = buf;
|
---|
341 | return outlength;
|
---|
342 | }
|
---|
343 |
|
---|
344 | static const char *string2str(int type, const char *beg, const char *end)
|
---|
345 | {
|
---|
346 | char *buf;
|
---|
347 |
|
---|
348 | /* Convert an ASN.1 String into its UTF-8 string representation.
|
---|
349 | Return the dynamically allocated string, or NULL if an error occurs. */
|
---|
350 |
|
---|
351 | if(utf8asn1str(&buf, type, beg, end) < 0)
|
---|
352 | return (const char *) NULL;
|
---|
353 | return buf;
|
---|
354 | }
|
---|
355 |
|
---|
356 | static int encodeUint(char *buf, int n, unsigned int x)
|
---|
357 | {
|
---|
358 | int i = 0;
|
---|
359 | unsigned int y = x / 10;
|
---|
360 |
|
---|
361 | /* Decimal ASCII encode unsigned integer `x' in the `n'-byte buffer at `buf'.
|
---|
362 | Return the total number of encoded digits, even if larger than `n'. */
|
---|
363 |
|
---|
364 | if(y) {
|
---|
365 | i += encodeUint(buf, n, y);
|
---|
366 | x -= y * 10;
|
---|
367 | }
|
---|
368 | if(i < n)
|
---|
369 | buf[i] = (char) ('0' + x);
|
---|
370 | i++;
|
---|
371 | if(i < n)
|
---|
372 | buf[i] = '\0'; /* Store a terminator if possible. */
|
---|
373 | return i;
|
---|
374 | }
|
---|
375 |
|
---|
376 | static int encodeOID(char *buf, int n, const char *beg, const char *end)
|
---|
377 | {
|
---|
378 | int i = 0;
|
---|
379 | unsigned int x;
|
---|
380 | unsigned int y;
|
---|
381 |
|
---|
382 | /* Convert an ASN.1 OID into its dotted string representation.
|
---|
383 | Store the result in th `n'-byte buffer at `buf'.
|
---|
384 | Return the converted string length, or -1 if an error occurs. */
|
---|
385 |
|
---|
386 | /* Process the first two numbers. */
|
---|
387 | y = *(const unsigned char *) beg++;
|
---|
388 | x = y / 40;
|
---|
389 | y -= x * 40;
|
---|
390 | i += encodeUint(buf + i, n - i, x);
|
---|
391 | if(i < n)
|
---|
392 | buf[i] = '.';
|
---|
393 | i++;
|
---|
394 | i += encodeUint(buf + i, n - i, y);
|
---|
395 |
|
---|
396 | /* Process the trailing numbers. */
|
---|
397 | while(beg < end) {
|
---|
398 | if(i < n)
|
---|
399 | buf[i] = '.';
|
---|
400 | i++;
|
---|
401 | x = 0;
|
---|
402 | do {
|
---|
403 | if(x & 0xFF000000)
|
---|
404 | return -1;
|
---|
405 | y = *(const unsigned char *) beg++;
|
---|
406 | x = (x << 7) | (y & 0x7F);
|
---|
407 | } while(y & 0x80);
|
---|
408 | i += encodeUint(buf + i, n - i, x);
|
---|
409 | }
|
---|
410 | if(i < n)
|
---|
411 | buf[i] = '\0';
|
---|
412 | return i;
|
---|
413 | }
|
---|
414 |
|
---|
415 | static const char *OID2str(const char *beg, const char *end, bool symbolic)
|
---|
416 | {
|
---|
417 | char *buf = (char *) NULL;
|
---|
418 | const curl_OID * op;
|
---|
419 | int n;
|
---|
420 |
|
---|
421 | /* Convert an ASN.1 OID into its dotted or symbolic string representation.
|
---|
422 | Return the dynamically allocated string, or NULL if an error occurs. */
|
---|
423 |
|
---|
424 | if(beg < end) {
|
---|
425 | n = encodeOID((char *) NULL, -1, beg, end);
|
---|
426 | if(n >= 0) {
|
---|
427 | buf = malloc(n + 1);
|
---|
428 | if(buf) {
|
---|
429 | encodeOID(buf, n, beg, end);
|
---|
430 | buf[n] = '\0';
|
---|
431 |
|
---|
432 | if(symbolic) {
|
---|
433 | op = searchOID(buf);
|
---|
434 | if(op) {
|
---|
435 | free(buf);
|
---|
436 | buf = strdup(op->textoid);
|
---|
437 | }
|
---|
438 | }
|
---|
439 | }
|
---|
440 | }
|
---|
441 | }
|
---|
442 | return buf;
|
---|
443 | }
|
---|
444 |
|
---|
445 | static const char *GTime2str(const char *beg, const char *end)
|
---|
446 | {
|
---|
447 | const char *tzp;
|
---|
448 | const char *fracp;
|
---|
449 | char sec1, sec2;
|
---|
450 | size_t fracl;
|
---|
451 | size_t tzl;
|
---|
452 | const char *sep = "";
|
---|
453 |
|
---|
454 | /* Convert an ASN.1 Generalized time to a printable string.
|
---|
455 | Return the dynamically allocated string, or NULL if an error occurs. */
|
---|
456 |
|
---|
457 | for(fracp = beg; fracp < end && *fracp >= '0' && *fracp <= '9'; fracp++)
|
---|
458 | ;
|
---|
459 |
|
---|
460 | /* Get seconds digits. */
|
---|
461 | sec1 = '0';
|
---|
462 | switch(fracp - beg - 12) {
|
---|
463 | case 0:
|
---|
464 | sec2 = '0';
|
---|
465 | break;
|
---|
466 | case 2:
|
---|
467 | sec1 = fracp[-2];
|
---|
468 | /* FALLTHROUGH */
|
---|
469 | case 1:
|
---|
470 | sec2 = fracp[-1];
|
---|
471 | break;
|
---|
472 | default:
|
---|
473 | return (const char *) NULL;
|
---|
474 | }
|
---|
475 |
|
---|
476 | /* Scan for timezone, measure fractional seconds. */
|
---|
477 | tzp = fracp;
|
---|
478 | fracl = 0;
|
---|
479 | if(fracp < end && (*fracp == '.' || *fracp == ',')) {
|
---|
480 | fracp++;
|
---|
481 | do
|
---|
482 | tzp++;
|
---|
483 | while(tzp < end && *tzp >= '0' && *tzp <= '9');
|
---|
484 | /* Strip leading zeroes in fractional seconds. */
|
---|
485 | for(fracl = tzp - fracp - 1; fracl && fracp[fracl - 1] == '0'; fracl--)
|
---|
486 | ;
|
---|
487 | }
|
---|
488 |
|
---|
489 | /* Process timezone. */
|
---|
490 | if(tzp >= end)
|
---|
491 | ; /* Nothing to do. */
|
---|
492 | else if(*tzp == 'Z') {
|
---|
493 | tzp = " GMT";
|
---|
494 | end = tzp + 4;
|
---|
495 | }
|
---|
496 | else {
|
---|
497 | sep = " ";
|
---|
498 | tzp++;
|
---|
499 | }
|
---|
500 |
|
---|
501 | tzl = end - tzp;
|
---|
502 | return curl_maprintf("%.4s-%.2s-%.2s %.2s:%.2s:%c%c%s%.*s%s%.*s",
|
---|
503 | beg, beg + 4, beg + 6,
|
---|
504 | beg + 8, beg + 10, sec1, sec2,
|
---|
505 | fracl? ".": "", fracl, fracp,
|
---|
506 | sep, tzl, tzp);
|
---|
507 | }
|
---|
508 |
|
---|
509 | static const char *UTime2str(const char *beg, const char *end)
|
---|
510 | {
|
---|
511 | const char *tzp;
|
---|
512 | size_t tzl;
|
---|
513 | const char *sec;
|
---|
514 |
|
---|
515 | /* Convert an ASN.1 UTC time to a printable string.
|
---|
516 | Return the dynamically allocated string, or NULL if an error occurs. */
|
---|
517 |
|
---|
518 | for(tzp = beg; tzp < end && *tzp >= '0' && *tzp <= '9'; tzp++)
|
---|
519 | ;
|
---|
520 | /* Get the seconds. */
|
---|
521 | sec = beg + 10;
|
---|
522 | switch(tzp - sec) {
|
---|
523 | case 0:
|
---|
524 | sec = "00";
|
---|
525 | case 2:
|
---|
526 | break;
|
---|
527 | default:
|
---|
528 | return (const char *) NULL;
|
---|
529 | }
|
---|
530 |
|
---|
531 | /* Process timezone. */
|
---|
532 | if(tzp >= end)
|
---|
533 | return (const char *) NULL;
|
---|
534 | if(*tzp == 'Z') {
|
---|
535 | tzp = "GMT";
|
---|
536 | end = tzp + 3;
|
---|
537 | }
|
---|
538 | else
|
---|
539 | tzp++;
|
---|
540 |
|
---|
541 | tzl = end - tzp;
|
---|
542 | return curl_maprintf("%u%.2s-%.2s-%.2s %.2s:%.2s:%.2s %.*s",
|
---|
543 | 20 - (*beg >= '5'), beg, beg + 2, beg + 4,
|
---|
544 | beg + 6, beg + 8, sec,
|
---|
545 | tzl, tzp);
|
---|
546 | }
|
---|
547 |
|
---|
548 | static const char *ASN1tostr(curl_asn1Element *elem, int type)
|
---|
549 | {
|
---|
550 | /* Convert an ASN.1 element to a printable string.
|
---|
551 | Return the dynamically allocated string, or NULL if an error occurs. */
|
---|
552 |
|
---|
553 | if(elem->constructed)
|
---|
554 | return (const char *) NULL; /* No conversion of structured elements. */
|
---|
555 |
|
---|
556 | if(!type)
|
---|
557 | type = elem->tag; /* Type not forced: use element tag as type. */
|
---|
558 |
|
---|
559 | switch(type) {
|
---|
560 | case CURL_ASN1_BOOLEAN:
|
---|
561 | return bool2str(elem->beg, elem->end);
|
---|
562 | case CURL_ASN1_INTEGER:
|
---|
563 | case CURL_ASN1_ENUMERATED:
|
---|
564 | return int2str(elem->beg, elem->end);
|
---|
565 | case CURL_ASN1_BIT_STRING:
|
---|
566 | return bit2str(elem->beg, elem->end);
|
---|
567 | case CURL_ASN1_OCTET_STRING:
|
---|
568 | return octet2str(elem->beg, elem->end);
|
---|
569 | case CURL_ASN1_NULL:
|
---|
570 | return strdup("");
|
---|
571 | case CURL_ASN1_OBJECT_IDENTIFIER:
|
---|
572 | return OID2str(elem->beg, elem->end, TRUE);
|
---|
573 | case CURL_ASN1_UTC_TIME:
|
---|
574 | return UTime2str(elem->beg, elem->end);
|
---|
575 | case CURL_ASN1_GENERALIZED_TIME:
|
---|
576 | return GTime2str(elem->beg, elem->end);
|
---|
577 | case CURL_ASN1_UTF8_STRING:
|
---|
578 | case CURL_ASN1_NUMERIC_STRING:
|
---|
579 | case CURL_ASN1_PRINTABLE_STRING:
|
---|
580 | case CURL_ASN1_TELETEX_STRING:
|
---|
581 | case CURL_ASN1_IA5_STRING:
|
---|
582 | case CURL_ASN1_VISIBLE_STRING:
|
---|
583 | case CURL_ASN1_UNIVERSAL_STRING:
|
---|
584 | case CURL_ASN1_BMP_STRING:
|
---|
585 | return string2str(type, elem->beg, elem->end);
|
---|
586 | }
|
---|
587 |
|
---|
588 | return (const char *) NULL; /* Unsupported. */
|
---|
589 | }
|
---|
590 |
|
---|
591 | static ssize_t encodeDN(char *buf, size_t n, curl_asn1Element *dn)
|
---|
592 | {
|
---|
593 | curl_asn1Element rdn;
|
---|
594 | curl_asn1Element atv;
|
---|
595 | curl_asn1Element oid;
|
---|
596 | curl_asn1Element value;
|
---|
597 | size_t l = 0;
|
---|
598 | const char *p1;
|
---|
599 | const char *p2;
|
---|
600 | const char *p3;
|
---|
601 | const char *str;
|
---|
602 |
|
---|
603 | /* ASCII encode distinguished name at `dn' into the `n'-byte buffer at `buf'.
|
---|
604 | Return the total string length, even if larger than `n'. */
|
---|
605 |
|
---|
606 | for(p1 = dn->beg; p1 < dn->end;) {
|
---|
607 | p1 = getASN1Element(&rdn, p1, dn->end);
|
---|
608 | if(!p1)
|
---|
609 | return -1;
|
---|
610 | for(p2 = rdn.beg; p2 < rdn.end;) {
|
---|
611 | p2 = getASN1Element(&atv, p2, rdn.end);
|
---|
612 | if(!p2)
|
---|
613 | return -1;
|
---|
614 | p3 = getASN1Element(&oid, atv.beg, atv.end);
|
---|
615 | if(!p3)
|
---|
616 | return -1;
|
---|
617 | if(!getASN1Element(&value, p3, atv.end))
|
---|
618 | return -1;
|
---|
619 | str = ASN1tostr(&oid, 0);
|
---|
620 | if(!str)
|
---|
621 | return -1;
|
---|
622 |
|
---|
623 | /* Encode delimiter.
|
---|
624 | If attribute has a short uppercase name, delimiter is ", ". */
|
---|
625 | if(l) {
|
---|
626 | for(p3 = str; isupper(*p3); p3++)
|
---|
627 | ;
|
---|
628 | for(p3 = (*p3 || p3 - str > 2)? "/": ", "; *p3; p3++) {
|
---|
629 | if(l < n)
|
---|
630 | buf[l] = *p3;
|
---|
631 | l++;
|
---|
632 | }
|
---|
633 | }
|
---|
634 |
|
---|
635 | /* Encode attribute name. */
|
---|
636 | for(p3 = str; *p3; p3++) {
|
---|
637 | if(l < n)
|
---|
638 | buf[l] = *p3;
|
---|
639 | l++;
|
---|
640 | }
|
---|
641 | free((char *) str);
|
---|
642 |
|
---|
643 | /* Generate equal sign. */
|
---|
644 | if(l < n)
|
---|
645 | buf[l] = '=';
|
---|
646 | l++;
|
---|
647 |
|
---|
648 | /* Generate value. */
|
---|
649 | str = ASN1tostr(&value, 0);
|
---|
650 | if(!str)
|
---|
651 | return -1;
|
---|
652 | for(p3 = str; *p3; p3++) {
|
---|
653 | if(l < n)
|
---|
654 | buf[l] = *p3;
|
---|
655 | l++;
|
---|
656 | }
|
---|
657 | free((char *) str);
|
---|
658 | }
|
---|
659 | }
|
---|
660 |
|
---|
661 | return l;
|
---|
662 | }
|
---|
663 |
|
---|
664 | static const char *DNtostr(curl_asn1Element *dn)
|
---|
665 | {
|
---|
666 | char *buf = (char *) NULL;
|
---|
667 | ssize_t n = encodeDN(buf, 0, dn);
|
---|
668 |
|
---|
669 | /* Convert an ASN.1 distinguished name into a printable string.
|
---|
670 | Return the dynamically allocated string, or NULL if an error occurs. */
|
---|
671 |
|
---|
672 | if(n >= 0) {
|
---|
673 | buf = malloc(n + 1);
|
---|
674 | if(buf) {
|
---|
675 | encodeDN(buf, n + 1, dn);
|
---|
676 | buf[n] = '\0';
|
---|
677 | }
|
---|
678 | }
|
---|
679 | return (const char *) buf;
|
---|
680 | }
|
---|
681 |
|
---|
682 | /*
|
---|
683 | * X509 parser.
|
---|
684 | */
|
---|
685 |
|
---|
686 | int Curl_parseX509(curl_X509certificate *cert,
|
---|
687 | const char *beg, const char *end)
|
---|
688 | {
|
---|
689 | curl_asn1Element elem;
|
---|
690 | curl_asn1Element tbsCertificate;
|
---|
691 | const char *ccp;
|
---|
692 | static const char defaultVersion = 0; /* v1. */
|
---|
693 |
|
---|
694 | /* ASN.1 parse an X509 certificate into structure subfields.
|
---|
695 | Syntax is assumed to have already been checked by the SSL backend.
|
---|
696 | See RFC 5280. */
|
---|
697 |
|
---|
698 | cert->certificate.header = NULL;
|
---|
699 | cert->certificate.beg = beg;
|
---|
700 | cert->certificate.end = end;
|
---|
701 |
|
---|
702 | /* Get the sequence content. */
|
---|
703 | if(!getASN1Element(&elem, beg, end))
|
---|
704 | return -1; /* Invalid bounds/size. */
|
---|
705 | beg = elem.beg;
|
---|
706 | end = elem.end;
|
---|
707 |
|
---|
708 | /* Get tbsCertificate. */
|
---|
709 | beg = getASN1Element(&tbsCertificate, beg, end);
|
---|
710 | if(!beg)
|
---|
711 | return -1;
|
---|
712 | /* Skip the signatureAlgorithm. */
|
---|
713 | beg = getASN1Element(&cert->signatureAlgorithm, beg, end);
|
---|
714 | if(!beg)
|
---|
715 | return -1;
|
---|
716 | /* Get the signatureValue. */
|
---|
717 | if(!getASN1Element(&cert->signature, beg, end))
|
---|
718 | return -1;
|
---|
719 |
|
---|
720 | /* Parse TBSCertificate. */
|
---|
721 | beg = tbsCertificate.beg;
|
---|
722 | end = tbsCertificate.end;
|
---|
723 | /* Get optional version, get serialNumber. */
|
---|
724 | cert->version.header = NULL;
|
---|
725 | cert->version.beg = &defaultVersion;
|
---|
726 | cert->version.end = &defaultVersion + sizeof(defaultVersion);
|
---|
727 | beg = getASN1Element(&elem, beg, end);
|
---|
728 | if(!beg)
|
---|
729 | return -1;
|
---|
730 | if(elem.tag == 0) {
|
---|
731 | if(!getASN1Element(&cert->version, elem.beg, elem.end))
|
---|
732 | return -1;
|
---|
733 | beg = getASN1Element(&elem, beg, end);
|
---|
734 | if(!beg)
|
---|
735 | return -1;
|
---|
736 | }
|
---|
737 | cert->serialNumber = elem;
|
---|
738 | /* Get signature algorithm. */
|
---|
739 | beg = getASN1Element(&cert->signatureAlgorithm, beg, end);
|
---|
740 | /* Get issuer. */
|
---|
741 | beg = getASN1Element(&cert->issuer, beg, end);
|
---|
742 | if(!beg)
|
---|
743 | return -1;
|
---|
744 | /* Get notBefore and notAfter. */
|
---|
745 | beg = getASN1Element(&elem, beg, end);
|
---|
746 | if(!beg)
|
---|
747 | return -1;
|
---|
748 | ccp = getASN1Element(&cert->notBefore, elem.beg, elem.end);
|
---|
749 | if(!ccp)
|
---|
750 | return -1;
|
---|
751 | if(!getASN1Element(&cert->notAfter, ccp, elem.end))
|
---|
752 | return -1;
|
---|
753 | /* Get subject. */
|
---|
754 | beg = getASN1Element(&cert->subject, beg, end);
|
---|
755 | if(!beg)
|
---|
756 | return -1;
|
---|
757 | /* Get subjectPublicKeyAlgorithm and subjectPublicKey. */
|
---|
758 | beg = getASN1Element(&cert->subjectPublicKeyInfo, beg, end);
|
---|
759 | if(!beg)
|
---|
760 | return -1;
|
---|
761 | ccp = getASN1Element(&cert->subjectPublicKeyAlgorithm,
|
---|
762 | cert->subjectPublicKeyInfo.beg,
|
---|
763 | cert->subjectPublicKeyInfo.end);
|
---|
764 | if(!ccp)
|
---|
765 | return -1;
|
---|
766 | if(!getASN1Element(&cert->subjectPublicKey, ccp,
|
---|
767 | cert->subjectPublicKeyInfo.end))
|
---|
768 | return -1;
|
---|
769 | /* Get optional issuerUiqueID, subjectUniqueID and extensions. */
|
---|
770 | cert->issuerUniqueID.tag = cert->subjectUniqueID.tag = 0;
|
---|
771 | cert->extensions.tag = elem.tag = 0;
|
---|
772 | cert->issuerUniqueID.header = cert->subjectUniqueID.header = NULL;
|
---|
773 | cert->issuerUniqueID.beg = cert->issuerUniqueID.end = "";
|
---|
774 | cert->subjectUniqueID.beg = cert->subjectUniqueID.end = "";
|
---|
775 | cert->extensions.header = NULL;
|
---|
776 | cert->extensions.beg = cert->extensions.end = "";
|
---|
777 | if(beg < end) {
|
---|
778 | beg = getASN1Element(&elem, beg, end);
|
---|
779 | if(!beg)
|
---|
780 | return -1;
|
---|
781 | }
|
---|
782 | if(elem.tag == 1) {
|
---|
783 | cert->issuerUniqueID = elem;
|
---|
784 | if(beg < end) {
|
---|
785 | beg = getASN1Element(&elem, beg, end);
|
---|
786 | if(!beg)
|
---|
787 | return -1;
|
---|
788 | }
|
---|
789 | }
|
---|
790 | if(elem.tag == 2) {
|
---|
791 | cert->subjectUniqueID = elem;
|
---|
792 | if(beg < end) {
|
---|
793 | beg = getASN1Element(&elem, beg, end);
|
---|
794 | if(!beg)
|
---|
795 | return -1;
|
---|
796 | }
|
---|
797 | }
|
---|
798 | if(elem.tag == 3)
|
---|
799 | if(!getASN1Element(&cert->extensions, elem.beg, elem.end))
|
---|
800 | return -1;
|
---|
801 | return 0;
|
---|
802 | }
|
---|
803 |
|
---|
804 | static size_t copySubstring(char *to, const char *from)
|
---|
805 | {
|
---|
806 | size_t i;
|
---|
807 |
|
---|
808 | /* Copy at most 64-characters, terminate with a newline and returns the
|
---|
809 | effective number of stored characters. */
|
---|
810 |
|
---|
811 | for(i = 0; i < 64; i++) {
|
---|
812 | to[i] = *from;
|
---|
813 | if(!*from++)
|
---|
814 | break;
|
---|
815 | }
|
---|
816 |
|
---|
817 | to[i++] = '\n';
|
---|
818 | return i;
|
---|
819 | }
|
---|
820 |
|
---|
821 | static const char *dumpAlgo(curl_asn1Element *param,
|
---|
822 | const char *beg, const char *end)
|
---|
823 | {
|
---|
824 | curl_asn1Element oid;
|
---|
825 |
|
---|
826 | /* Get algorithm parameters and return algorithm name. */
|
---|
827 |
|
---|
828 | beg = getASN1Element(&oid, beg, end);
|
---|
829 | if(!beg)
|
---|
830 | return NULL;
|
---|
831 | param->header = NULL;
|
---|
832 | param->tag = 0;
|
---|
833 | param->beg = param->end = end;
|
---|
834 | if(beg < end)
|
---|
835 | if(!getASN1Element(param, beg, end))
|
---|
836 | return NULL;
|
---|
837 | return OID2str(oid.beg, oid.end, TRUE);
|
---|
838 | }
|
---|
839 |
|
---|
840 | static void do_pubkey_field(struct Curl_easy *data, int certnum,
|
---|
841 | const char *label, curl_asn1Element *elem)
|
---|
842 | {
|
---|
843 | const char *output;
|
---|
844 |
|
---|
845 | /* Generate a certificate information record for the public key. */
|
---|
846 |
|
---|
847 | output = ASN1tostr(elem, 0);
|
---|
848 | if(output) {
|
---|
849 | if(data->set.ssl.certinfo)
|
---|
850 | Curl_ssl_push_certinfo(data, certnum, label, output);
|
---|
851 | if(!certnum)
|
---|
852 | infof(data, " %s: %s\n", label, output);
|
---|
853 | free((char *) output);
|
---|
854 | }
|
---|
855 | }
|
---|
856 |
|
---|
857 | static void do_pubkey(struct Curl_easy *data, int certnum,
|
---|
858 | const char *algo, curl_asn1Element *param,
|
---|
859 | curl_asn1Element *pubkey)
|
---|
860 | {
|
---|
861 | curl_asn1Element elem;
|
---|
862 | curl_asn1Element pk;
|
---|
863 | const char *p;
|
---|
864 | const char *q;
|
---|
865 | unsigned long len;
|
---|
866 | unsigned int i;
|
---|
867 |
|
---|
868 | /* Generate all information records for the public key. */
|
---|
869 |
|
---|
870 | /* Get the public key (single element). */
|
---|
871 | if(!getASN1Element(&pk, pubkey->beg + 1, pubkey->end))
|
---|
872 | return;
|
---|
873 |
|
---|
874 | if(strcasecompare(algo, "rsaEncryption")) {
|
---|
875 | p = getASN1Element(&elem, pk.beg, pk.end);
|
---|
876 | if(!p)
|
---|
877 | return;
|
---|
878 |
|
---|
879 | /* Compute key length. */
|
---|
880 | for(q = elem.beg; !*q && q < elem.end; q++)
|
---|
881 | ;
|
---|
882 | len = (unsigned long)((elem.end - q) * 8);
|
---|
883 | if(len)
|
---|
884 | for(i = *(unsigned char *) q; !(i & 0x80); i <<= 1)
|
---|
885 | len--;
|
---|
886 | if(len > 32)
|
---|
887 | elem.beg = q; /* Strip leading zero bytes. */
|
---|
888 | if(!certnum)
|
---|
889 | infof(data, " RSA Public Key (%lu bits)\n", len);
|
---|
890 | if(data->set.ssl.certinfo) {
|
---|
891 | q = curl_maprintf("%lu", len);
|
---|
892 | if(q) {
|
---|
893 | Curl_ssl_push_certinfo(data, certnum, "RSA Public Key", q);
|
---|
894 | free((char *) q);
|
---|
895 | }
|
---|
896 | }
|
---|
897 | /* Generate coefficients. */
|
---|
898 | do_pubkey_field(data, certnum, "rsa(n)", &elem);
|
---|
899 | if(!getASN1Element(&elem, p, pk.end))
|
---|
900 | return;
|
---|
901 | do_pubkey_field(data, certnum, "rsa(e)", &elem);
|
---|
902 | }
|
---|
903 | else if(strcasecompare(algo, "dsa")) {
|
---|
904 | p = getASN1Element(&elem, param->beg, param->end);
|
---|
905 | if(p) {
|
---|
906 | do_pubkey_field(data, certnum, "dsa(p)", &elem);
|
---|
907 | p = getASN1Element(&elem, p, param->end);
|
---|
908 | if(p) {
|
---|
909 | do_pubkey_field(data, certnum, "dsa(q)", &elem);
|
---|
910 | if(getASN1Element(&elem, p, param->end)) {
|
---|
911 | do_pubkey_field(data, certnum, "dsa(g)", &elem);
|
---|
912 | do_pubkey_field(data, certnum, "dsa(pub_key)", &pk);
|
---|
913 | }
|
---|
914 | }
|
---|
915 | }
|
---|
916 | }
|
---|
917 | else if(strcasecompare(algo, "dhpublicnumber")) {
|
---|
918 | p = getASN1Element(&elem, param->beg, param->end);
|
---|
919 | if(p) {
|
---|
920 | do_pubkey_field(data, certnum, "dh(p)", &elem);
|
---|
921 | if(getASN1Element(&elem, param->beg, param->end)) {
|
---|
922 | do_pubkey_field(data, certnum, "dh(g)", &elem);
|
---|
923 | do_pubkey_field(data, certnum, "dh(pub_key)", &pk);
|
---|
924 | }
|
---|
925 | }
|
---|
926 | }
|
---|
927 | }
|
---|
928 |
|
---|
929 | CURLcode Curl_extract_certinfo(struct connectdata *conn,
|
---|
930 | int certnum,
|
---|
931 | const char *beg,
|
---|
932 | const char *end)
|
---|
933 | {
|
---|
934 | curl_X509certificate cert;
|
---|
935 | struct Curl_easy *data = conn->data;
|
---|
936 | curl_asn1Element param;
|
---|
937 | const char *ccp;
|
---|
938 | char *cp1;
|
---|
939 | size_t cl1;
|
---|
940 | char *cp2;
|
---|
941 | CURLcode result;
|
---|
942 | unsigned long version;
|
---|
943 | size_t i;
|
---|
944 | size_t j;
|
---|
945 |
|
---|
946 | if(!data->set.ssl.certinfo)
|
---|
947 | if(certnum)
|
---|
948 | return CURLE_OK;
|
---|
949 |
|
---|
950 | /* Prepare the certificate information for curl_easy_getinfo(). */
|
---|
951 |
|
---|
952 | /* Extract the certificate ASN.1 elements. */
|
---|
953 | if(Curl_parseX509(&cert, beg, end))
|
---|
954 | return CURLE_PEER_FAILED_VERIFICATION;
|
---|
955 |
|
---|
956 | /* Subject. */
|
---|
957 | ccp = DNtostr(&cert.subject);
|
---|
958 | if(!ccp)
|
---|
959 | return CURLE_OUT_OF_MEMORY;
|
---|
960 | if(data->set.ssl.certinfo)
|
---|
961 | Curl_ssl_push_certinfo(data, certnum, "Subject", ccp);
|
---|
962 | if(!certnum)
|
---|
963 | infof(data, "%2d Subject: %s\n", certnum, ccp);
|
---|
964 | free((char *) ccp);
|
---|
965 |
|
---|
966 | /* Issuer. */
|
---|
967 | ccp = DNtostr(&cert.issuer);
|
---|
968 | if(!ccp)
|
---|
969 | return CURLE_OUT_OF_MEMORY;
|
---|
970 | if(data->set.ssl.certinfo)
|
---|
971 | Curl_ssl_push_certinfo(data, certnum, "Issuer", ccp);
|
---|
972 | if(!certnum)
|
---|
973 | infof(data, " Issuer: %s\n", ccp);
|
---|
974 | free((char *) ccp);
|
---|
975 |
|
---|
976 | /* Version (always fits in less than 32 bits). */
|
---|
977 | version = 0;
|
---|
978 | for(ccp = cert.version.beg; ccp < cert.version.end; ccp++)
|
---|
979 | version = (version << 8) | *(const unsigned char *) ccp;
|
---|
980 | if(data->set.ssl.certinfo) {
|
---|
981 | ccp = curl_maprintf("%lx", version);
|
---|
982 | if(!ccp)
|
---|
983 | return CURLE_OUT_OF_MEMORY;
|
---|
984 | Curl_ssl_push_certinfo(data, certnum, "Version", ccp);
|
---|
985 | free((char *) ccp);
|
---|
986 | }
|
---|
987 | if(!certnum)
|
---|
988 | infof(data, " Version: %lu (0x%lx)\n", version + 1, version);
|
---|
989 |
|
---|
990 | /* Serial number. */
|
---|
991 | ccp = ASN1tostr(&cert.serialNumber, 0);
|
---|
992 | if(!ccp)
|
---|
993 | return CURLE_OUT_OF_MEMORY;
|
---|
994 | if(data->set.ssl.certinfo)
|
---|
995 | Curl_ssl_push_certinfo(data, certnum, "Serial Number", ccp);
|
---|
996 | if(!certnum)
|
---|
997 | infof(data, " Serial Number: %s\n", ccp);
|
---|
998 | free((char *) ccp);
|
---|
999 |
|
---|
1000 | /* Signature algorithm .*/
|
---|
1001 | ccp = dumpAlgo(¶m, cert.signatureAlgorithm.beg,
|
---|
1002 | cert.signatureAlgorithm.end);
|
---|
1003 | if(!ccp)
|
---|
1004 | return CURLE_OUT_OF_MEMORY;
|
---|
1005 | if(data->set.ssl.certinfo)
|
---|
1006 | Curl_ssl_push_certinfo(data, certnum, "Signature Algorithm", ccp);
|
---|
1007 | if(!certnum)
|
---|
1008 | infof(data, " Signature Algorithm: %s\n", ccp);
|
---|
1009 | free((char *) ccp);
|
---|
1010 |
|
---|
1011 | /* Start Date. */
|
---|
1012 | ccp = ASN1tostr(&cert.notBefore, 0);
|
---|
1013 | if(!ccp)
|
---|
1014 | return CURLE_OUT_OF_MEMORY;
|
---|
1015 | if(data->set.ssl.certinfo)
|
---|
1016 | Curl_ssl_push_certinfo(data, certnum, "Start Date", ccp);
|
---|
1017 | if(!certnum)
|
---|
1018 | infof(data, " Start Date: %s\n", ccp);
|
---|
1019 | free((char *) ccp);
|
---|
1020 |
|
---|
1021 | /* Expire Date. */
|
---|
1022 | ccp = ASN1tostr(&cert.notAfter, 0);
|
---|
1023 | if(!ccp)
|
---|
1024 | return CURLE_OUT_OF_MEMORY;
|
---|
1025 | if(data->set.ssl.certinfo)
|
---|
1026 | Curl_ssl_push_certinfo(data, certnum, "Expire Date", ccp);
|
---|
1027 | if(!certnum)
|
---|
1028 | infof(data, " Expire Date: %s\n", ccp);
|
---|
1029 | free((char *) ccp);
|
---|
1030 |
|
---|
1031 | /* Public Key Algorithm. */
|
---|
1032 | ccp = dumpAlgo(¶m, cert.subjectPublicKeyAlgorithm.beg,
|
---|
1033 | cert.subjectPublicKeyAlgorithm.end);
|
---|
1034 | if(!ccp)
|
---|
1035 | return CURLE_OUT_OF_MEMORY;
|
---|
1036 | if(data->set.ssl.certinfo)
|
---|
1037 | Curl_ssl_push_certinfo(data, certnum, "Public Key Algorithm", ccp);
|
---|
1038 | if(!certnum)
|
---|
1039 | infof(data, " Public Key Algorithm: %s\n", ccp);
|
---|
1040 | do_pubkey(data, certnum, ccp, ¶m, &cert.subjectPublicKey);
|
---|
1041 | free((char *) ccp);
|
---|
1042 |
|
---|
1043 | /* TODO: extensions. */
|
---|
1044 |
|
---|
1045 | /* Signature. */
|
---|
1046 | ccp = ASN1tostr(&cert.signature, 0);
|
---|
1047 | if(!ccp)
|
---|
1048 | return CURLE_OUT_OF_MEMORY;
|
---|
1049 | if(data->set.ssl.certinfo)
|
---|
1050 | Curl_ssl_push_certinfo(data, certnum, "Signature", ccp);
|
---|
1051 | if(!certnum)
|
---|
1052 | infof(data, " Signature: %s\n", ccp);
|
---|
1053 | free((char *) ccp);
|
---|
1054 |
|
---|
1055 | /* Generate PEM certificate. */
|
---|
1056 | result = Curl_base64_encode(data, cert.certificate.beg,
|
---|
1057 | cert.certificate.end - cert.certificate.beg,
|
---|
1058 | &cp1, &cl1);
|
---|
1059 | if(result)
|
---|
1060 | return result;
|
---|
1061 | /* Compute the number of characters in final certificate string. Format is:
|
---|
1062 | -----BEGIN CERTIFICATE-----\n
|
---|
1063 | <max 64 base64 characters>\n
|
---|
1064 | .
|
---|
1065 | .
|
---|
1066 | .
|
---|
1067 | -----END CERTIFICATE-----\n
|
---|
1068 | */
|
---|
1069 | i = 28 + cl1 + (cl1 + 64 - 1) / 64 + 26;
|
---|
1070 | cp2 = malloc(i + 1);
|
---|
1071 | if(!cp2) {
|
---|
1072 | free(cp1);
|
---|
1073 | return CURLE_OUT_OF_MEMORY;
|
---|
1074 | }
|
---|
1075 | /* Build the certificate string. */
|
---|
1076 | i = copySubstring(cp2, "-----BEGIN CERTIFICATE-----");
|
---|
1077 | for(j = 0; j < cl1; j += 64)
|
---|
1078 | i += copySubstring(cp2 + i, cp1 + j);
|
---|
1079 | i += copySubstring(cp2 + i, "-----END CERTIFICATE-----");
|
---|
1080 | cp2[i] = '\0';
|
---|
1081 | free(cp1);
|
---|
1082 | if(data->set.ssl.certinfo)
|
---|
1083 | Curl_ssl_push_certinfo(data, certnum, "Cert", cp2);
|
---|
1084 | if(!certnum)
|
---|
1085 | infof(data, "%s\n", cp2);
|
---|
1086 | free(cp2);
|
---|
1087 | return CURLE_OK;
|
---|
1088 | }
|
---|
1089 |
|
---|
1090 | #endif /* USE_GSKIT or USE_NSS or USE_GNUTLS or USE_CYASSL or USE_SCHANNEL */
|
---|
1091 |
|
---|
1092 | #if defined(USE_GSKIT)
|
---|
1093 |
|
---|
1094 | static const char *checkOID(const char *beg, const char *end,
|
---|
1095 | const char *oid)
|
---|
1096 | {
|
---|
1097 | curl_asn1Element e;
|
---|
1098 | const char *ccp;
|
---|
1099 | const char *p;
|
---|
1100 | bool matched;
|
---|
1101 |
|
---|
1102 | /* Check if first ASN.1 element at `beg' is the given OID.
|
---|
1103 | Return a pointer in the source after the OID if found, else NULL. */
|
---|
1104 |
|
---|
1105 | ccp = getASN1Element(&e, beg, end);
|
---|
1106 | if(!ccp || e.tag != CURL_ASN1_OBJECT_IDENTIFIER)
|
---|
1107 | return (const char *) NULL;
|
---|
1108 |
|
---|
1109 | p = OID2str(e.beg, e.end, FALSE);
|
---|
1110 | if(!p)
|
---|
1111 | return (const char *) NULL;
|
---|
1112 |
|
---|
1113 | matched = !strcmp(p, oid);
|
---|
1114 | free((char *) p);
|
---|
1115 | return matched? ccp: (const char *) NULL;
|
---|
1116 | }
|
---|
1117 |
|
---|
1118 | CURLcode Curl_verifyhost(struct connectdata *conn,
|
---|
1119 | const char *beg, const char *end)
|
---|
1120 | {
|
---|
1121 | struct Curl_easy *data = conn->data;
|
---|
1122 | curl_X509certificate cert;
|
---|
1123 | curl_asn1Element dn;
|
---|
1124 | curl_asn1Element elem;
|
---|
1125 | curl_asn1Element ext;
|
---|
1126 | curl_asn1Element name;
|
---|
1127 | const char *p;
|
---|
1128 | const char *q;
|
---|
1129 | char *dnsname;
|
---|
1130 | int matched = -1;
|
---|
1131 | size_t addrlen = (size_t) -1;
|
---|
1132 | ssize_t len;
|
---|
1133 | const char * const hostname = SSL_IS_PROXY()? conn->http_proxy.host.name:
|
---|
1134 | conn->host.name;
|
---|
1135 | const char * const dispname = SSL_IS_PROXY()?
|
---|
1136 | conn->http_proxy.host.dispname:
|
---|
1137 | conn->host.dispname;
|
---|
1138 | #ifdef ENABLE_IPV6
|
---|
1139 | struct in6_addr addr;
|
---|
1140 | #else
|
---|
1141 | struct in_addr addr;
|
---|
1142 | #endif
|
---|
1143 |
|
---|
1144 | /* Verify that connection server matches info in X509 certificate at
|
---|
1145 | `beg'..`end'. */
|
---|
1146 |
|
---|
1147 | if(!SSL_CONN_CONFIG(verifyhost))
|
---|
1148 | return CURLE_OK;
|
---|
1149 |
|
---|
1150 | if(Curl_parseX509(&cert, beg, end))
|
---|
1151 | return CURLE_PEER_FAILED_VERIFICATION;
|
---|
1152 |
|
---|
1153 | /* Get the server IP address. */
|
---|
1154 | #ifdef ENABLE_IPV6
|
---|
1155 | if(conn->bits.ipv6_ip && Curl_inet_pton(AF_INET6, hostname, &addr))
|
---|
1156 | addrlen = sizeof(struct in6_addr);
|
---|
1157 | else
|
---|
1158 | #endif
|
---|
1159 | if(Curl_inet_pton(AF_INET, hostname, &addr))
|
---|
1160 | addrlen = sizeof(struct in_addr);
|
---|
1161 |
|
---|
1162 | /* Process extensions. */
|
---|
1163 | for(p = cert.extensions.beg; p < cert.extensions.end && matched != 1;) {
|
---|
1164 | p = getASN1Element(&ext, p, cert.extensions.end);
|
---|
1165 | if(!p)
|
---|
1166 | return CURLE_PEER_FAILED_VERIFICATION;
|
---|
1167 |
|
---|
1168 | /* Check if extension is a subjectAlternativeName. */
|
---|
1169 | ext.beg = checkOID(ext.beg, ext.end, sanOID);
|
---|
1170 | if(ext.beg) {
|
---|
1171 | ext.beg = getASN1Element(&elem, ext.beg, ext.end);
|
---|
1172 | if(!ext.beg)
|
---|
1173 | return CURLE_PEER_FAILED_VERIFICATION;
|
---|
1174 | /* Skip critical if present. */
|
---|
1175 | if(elem.tag == CURL_ASN1_BOOLEAN) {
|
---|
1176 | ext.beg = getASN1Element(&elem, ext.beg, ext.end);
|
---|
1177 | if(!ext.beg)
|
---|
1178 | return CURLE_PEER_FAILED_VERIFICATION;
|
---|
1179 | }
|
---|
1180 | /* Parse the octet string contents: is a single sequence. */
|
---|
1181 | if(!getASN1Element(&elem, elem.beg, elem.end))
|
---|
1182 | return CURLE_PEER_FAILED_VERIFICATION;
|
---|
1183 | /* Check all GeneralNames. */
|
---|
1184 | for(q = elem.beg; matched != 1 && q < elem.end;) {
|
---|
1185 | q = getASN1Element(&name, q, elem.end);
|
---|
1186 | if(!q)
|
---|
1187 | break;
|
---|
1188 | switch(name.tag) {
|
---|
1189 | case 2: /* DNS name. */
|
---|
1190 | len = utf8asn1str(&dnsname, CURL_ASN1_IA5_STRING,
|
---|
1191 | name.beg, name.end);
|
---|
1192 | if(len > 0 && (size_t)len == strlen(dnsname))
|
---|
1193 | matched = Curl_cert_hostcheck(dnsname, hostname);
|
---|
1194 | else
|
---|
1195 | matched = 0;
|
---|
1196 | free(dnsname);
|
---|
1197 | break;
|
---|
1198 |
|
---|
1199 | case 7: /* IP address. */
|
---|
1200 | matched = (size_t) (name.end - name.beg) == addrlen &&
|
---|
1201 | !memcmp(&addr, name.beg, addrlen);
|
---|
1202 | break;
|
---|
1203 | }
|
---|
1204 | }
|
---|
1205 | }
|
---|
1206 | }
|
---|
1207 |
|
---|
1208 | switch(matched) {
|
---|
1209 | case 1:
|
---|
1210 | /* an alternative name matched the server hostname */
|
---|
1211 | infof(data, "\t subjectAltName: %s matched\n", dispname);
|
---|
1212 | return CURLE_OK;
|
---|
1213 | case 0:
|
---|
1214 | /* an alternative name field existed, but didn't match and then
|
---|
1215 | we MUST fail */
|
---|
1216 | infof(data, "\t subjectAltName does not match %s\n", dispname);
|
---|
1217 | return CURLE_PEER_FAILED_VERIFICATION;
|
---|
1218 | }
|
---|
1219 |
|
---|
1220 | /* Process subject. */
|
---|
1221 | name.header = NULL;
|
---|
1222 | name.beg = name.end = "";
|
---|
1223 | q = cert.subject.beg;
|
---|
1224 | /* we have to look to the last occurrence of a commonName in the
|
---|
1225 | distinguished one to get the most significant one. */
|
---|
1226 | while(q < cert.subject.end) {
|
---|
1227 | q = getASN1Element(&dn, q, cert.subject.end);
|
---|
1228 | if(!q)
|
---|
1229 | break;
|
---|
1230 | for(p = dn.beg; p < dn.end;) {
|
---|
1231 | p = getASN1Element(&elem, p, dn.end);
|
---|
1232 | if(!p)
|
---|
1233 | return CURLE_PEER_FAILED_VERIFICATION;
|
---|
1234 | /* We have a DN's AttributeTypeAndValue: check it in case it's a CN. */
|
---|
1235 | elem.beg = checkOID(elem.beg, elem.end, cnOID);
|
---|
1236 | if(elem.beg)
|
---|
1237 | name = elem; /* Latch CN. */
|
---|
1238 | }
|
---|
1239 | }
|
---|
1240 |
|
---|
1241 | /* Check the CN if found. */
|
---|
1242 | if(!getASN1Element(&elem, name.beg, name.end))
|
---|
1243 | failf(data, "SSL: unable to obtain common name from peer certificate");
|
---|
1244 | else {
|
---|
1245 | len = utf8asn1str(&dnsname, elem.tag, elem.beg, elem.end);
|
---|
1246 | if(len < 0) {
|
---|
1247 | free(dnsname);
|
---|
1248 | return CURLE_OUT_OF_MEMORY;
|
---|
1249 | }
|
---|
1250 | if(strlen(dnsname) != (size_t) len) /* Nul byte in string ? */
|
---|
1251 | failf(data, "SSL: illegal cert name field");
|
---|
1252 | else if(Curl_cert_hostcheck((const char *) dnsname, hostname)) {
|
---|
1253 | infof(data, "\t common name: %s (matched)\n", dnsname);
|
---|
1254 | free(dnsname);
|
---|
1255 | return CURLE_OK;
|
---|
1256 | }
|
---|
1257 | else
|
---|
1258 | failf(data, "SSL: certificate subject name '%s' does not match "
|
---|
1259 | "target host name '%s'", dnsname, dispname);
|
---|
1260 | free(dnsname);
|
---|
1261 | }
|
---|
1262 |
|
---|
1263 | return CURLE_PEER_FAILED_VERIFICATION;
|
---|
1264 | }
|
---|
1265 |
|
---|
1266 | #endif /* USE_GSKIT */
|
---|