VirtualBox

source: vbox/trunk/src/libs/curl-7.83.1/lib/vtls/x509asn1.c@ 97623

Last change on this file since 97623 was 95312, checked in by vboxsync, 3 years ago

libs/{curl,libxml2}: OSE export fixes, bugref:8515

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette