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