]> git.ipfire.org Git - thirdparty/strongswan.git/blame - src/libstrongswan/plugins/openssl/openssl_x509.c
openssl: Add support for delta CRLs
[thirdparty/strongswan.git] / src / libstrongswan / plugins / openssl / openssl_x509.c
CommitLineData
5728c6aa 1/*
28bcbc29
TB
2 * Copyright (C) 2011 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
5728c6aa
MW
5 * Copyright (C) 2010 Martin Willi
6 * Copyright (C) 2010 revosec AG
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 */
18
19/*
5e4b1ad2
MR
20 * Copyright (C) 2013 Michael Rossberg
21 * Copyright (C) 2013 Technische Universität Ilmenau
22 *
5728c6aa
MW
23 * Copyright (C) 2010 secunet Security Networks AG
24 * Copyright (C) 2010 Thomas Egerer
25 *
26 * Permission is hereby granted, free of charge, to any person obtaining a copy
27 * of this software and associated documentation files (the "Software"), to deal
28 * in the Software without restriction, including without limitation the rights
29 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
30 * copies of the Software, and to permit persons to whom the Software is
31 * furnished to do so, subject to the following conditions:
32 *
33 * The above copyright notice and this permission notice shall be included in
34 * all copies or substantial portions of the Software.
35 *
36 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
37 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
38 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
39 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
40 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
41 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
42 * THE SOFTWARE.
43 */
44
45#define _GNU_SOURCE
46#include <stdio.h>
47#include <openssl/x509.h>
48#include <openssl/x509v3.h>
49
50#include "openssl_x509.h"
51#include "openssl_util.h"
52
f05b4272 53#include <utils/debug.h>
5728c6aa 54#include <asn1/oid.h>
12642a68 55#include <collections/linked_list.h>
5e4b1ad2 56#include <selectors/traffic_selector.h>
5728c6aa 57
5e4b1ad2
MR
58/* IP Addr block extension support was introduced with 0.9.8e */
59#if OPENSSL_VERSION_NUMBER < 0x0090805fL
60#define OPENSSL_NO_RFC3779
61#endif
5728c6aa 62
08d7e1f1
TB
63/* added with 1.0.2 */
64#if OPENSSL_VERSION_NUMBER < 0x10002000L
65static inline void X509_get0_signature(ASN1_BIT_STRING **psig, X509_ALGOR **palg, const X509 *x) {
66 if (psig) { *psig = x->signature; }
67 if (palg) { *palg = x->sig_alg; }
68}
69#endif
70
71/* added with 1.1.0 when X509 etc. was made opaque */
72#if OPENSSL_VERSION_NUMBER < 0x10100000L
73#define X509_get0_extensions(x509) ({ (x509)->cert_info->extensions; })
74#define X509_get0_tbs_sigalg(x509) ({ (x509)->cert_info->signature; })
75#define X509_ALGOR_get0(oid, ppt, ppv, alg) ({ *(oid) = (alg)->algorithm; })
76#define X509_PUBKEY_get0_param(oid, pk, len, pa, pub) X509_ALGOR_get0(oid, NULL, NULL, (pub)->algor)
77#define X509v3_addr_get_afi v3_addr_get_afi
78#define X509v3_addr_get_range v3_addr_get_range
79#define X509v3_addr_is_canonical v3_addr_is_canonical
80#endif
81
5728c6aa
MW
82typedef struct private_openssl_x509_t private_openssl_x509_t;
83
84/**
85 * Private data of an openssl_x509_t object.
86 */
87struct private_openssl_x509_t {
88
89 /**
90 * Public openssl_x509_t interface.
91 */
92 openssl_x509_t public;
93
94 /**
95 * OpenSSL certificate representation
96 */
97 X509 *x509;
98
99 /**
100 * DER encoded certificate
101 */
102 chunk_t encoding;
103
104 /**
105 * SHA1 hash of the certificate
106 */
107 chunk_t hash;
108
109 /**
110 * X509 flags
111 */
112 x509_flag_t flags;
113
114 /**
115 * Pathlen constraint
116 */
d390b3b9 117 u_char pathlen;
5728c6aa
MW
118
119 /**
120 * certificate subject
121 */
122 identification_t *subject;
123
124 /**
125 * certificate issuer
126 */
127 identification_t *issuer;
128
129 /**
130 * Certificates public key
131 */
132 public_key_t *pubkey;
133
134 /**
135 * subjectKeyIdentifier as read from cert
136 */
137 chunk_t subjectKeyIdentifier;
138
139 /**
140 * authorityKeyIdentifier as read from cert
141 */
142 chunk_t authKeyIdentifier;
143
144 /**
145 * Start time of certificate validity
146 */
147 time_t notBefore;
148
149 /**
150 * End time of certificate validity
151 */
152 time_t notAfter;
153
154 /**
155 * Signature scheme of the certificate
156 */
157 signature_scheme_t scheme;
158
159 /**
160 * subjectAltNames
161 */
162 linked_list_t *subjectAltNames;
163
164 /**
165 * issuerAltNames
166 */
167 linked_list_t *issuerAltNames;
168
169 /**
a6478a04 170 * List of CRL URIs, as x509_cdp_t
5728c6aa
MW
171 */
172 linked_list_t *crl_uris;
173
174 /**
175 * List of OCSP URIs
176 */
177 linked_list_t *ocsp_uris;
178
5e4b1ad2
MR
179 /**
180 * List of ipAddrBlocks as traffic_selector_t
181 */
182 linked_list_t *ipAddrBlocks;
183
184
5728c6aa
MW
185 /**
186 * References to this cert
187 */
188 refcount_t ref;
189};
190
191/**
192 * Convert a GeneralName to an identification_t.
193 */
194static identification_t *general_name2id(GENERAL_NAME *name)
195{
196 if (!name)
197 {
198 return NULL;
199 }
200 switch (name->type)
201 {
202 case GEN_EMAIL:
203 return identification_create_from_encoding(ID_RFC822_ADDR,
204 openssl_asn1_str2chunk(name->d.rfc822Name));
205 case GEN_DNS:
206 return identification_create_from_encoding(ID_FQDN,
207 openssl_asn1_str2chunk(name->d.dNSName));
208 case GEN_URI:
209 return identification_create_from_encoding(ID_DER_ASN1_GN_URI,
210 openssl_asn1_str2chunk(name->d.uniformResourceIdentifier));
211 case GEN_IPADD:
07c5aacc
MW
212 {
213 chunk_t chunk = openssl_asn1_str2chunk(name->d.iPAddress);
214 if (chunk.len == 4)
215 {
216 return identification_create_from_encoding(ID_IPV4_ADDR, chunk);
217 }
218 if (chunk.len == 16)
219 {
220 return identification_create_from_encoding(ID_IPV6_ADDR, chunk);
221 }
222 return NULL;
223 }
5728c6aa
MW
224 case GEN_DIRNAME :
225 return openssl_x509_name2id(name->d.directoryName);
a0a8aaaf
MW
226 case GEN_OTHERNAME:
227 if (OBJ_obj2nid(name->d.otherName->type_id) == NID_ms_upn &&
228 name->d.otherName->value->type == V_ASN1_UTF8STRING)
229 {
230 return identification_create_from_encoding(ID_RFC822_ADDR,
231 openssl_asn1_str2chunk(
232 name->d.otherName->value->value.utf8string));
233 }
234 return NULL;
5728c6aa
MW
235 default:
236 return NULL;
237 }
238}
239
240METHOD(x509_t, get_flags, x509_flag_t,
241 private_openssl_x509_t *this)
242{
243 return this->flags;
244}
245
246METHOD(x509_t, get_serial, chunk_t,
247 private_openssl_x509_t *this)
248{
249 return openssl_asn1_str2chunk(X509_get_serialNumber(this->x509));
250}
251
252METHOD(x509_t, get_subjectKeyIdentifier, chunk_t,
253 private_openssl_x509_t *this)
254{
255 chunk_t fingerprint;
256
257 if (this->subjectKeyIdentifier.len)
258 {
259 return this->subjectKeyIdentifier;
260 }
da9724e6 261 if (this->pubkey->get_fingerprint(this->pubkey, KEYID_PUBKEY_SHA1,
5728c6aa
MW
262 &fingerprint))
263 {
264 return fingerprint;
265 }
266 return chunk_empty;
267}
268
269METHOD(x509_t, get_authKeyIdentifier, chunk_t,
270 private_openssl_x509_t *this)
271{
272 if (this->authKeyIdentifier.len)
273 {
274 return this->authKeyIdentifier;
275 }
276 return chunk_empty;
277}
278
d390b3b9 279METHOD(x509_t, get_constraint, u_int,
b3d359e5 280 private_openssl_x509_t *this, x509_constraint_t type)
a742d97f 281{
b3d359e5
MW
282 switch (type)
283 {
284 case X509_PATH_LEN:
285 return this->pathlen;
286 default:
287 return X509_NO_CONSTRAINT;
288 }
a742d97f
MW
289}
290
5728c6aa
MW
291METHOD(x509_t, create_subjectAltName_enumerator, enumerator_t*,
292 private_openssl_x509_t *this)
293{
294 return this->subjectAltNames->create_enumerator(this->subjectAltNames);
295}
296
297METHOD(x509_t, create_crl_uri_enumerator, enumerator_t*,
298 private_openssl_x509_t *this)
299{
a6478a04 300 return this->crl_uris->create_enumerator(this->crl_uris);
5728c6aa
MW
301}
302
303METHOD(x509_t, create_ocsp_uri_enumerator, enumerator_t*,
304 private_openssl_x509_t *this)
305{
306 return this->ocsp_uris->create_enumerator(this->ocsp_uris);
307}
308
5e4b1ad2
MR
309METHOD(x509_t, create_ipAddrBlock_enumerator, enumerator_t*,
310 private_openssl_x509_t *this)
311{
312 return this->ipAddrBlocks->create_enumerator(this->ipAddrBlocks);
313}
314
5728c6aa
MW
315METHOD(certificate_t, get_type, certificate_type_t,
316 private_openssl_x509_t *this)
317{
318 return CERT_X509;
319}
320
321METHOD(certificate_t, get_subject, identification_t*,
322 private_openssl_x509_t *this)
323{
324 return this->subject;
325}
326
327METHOD(certificate_t, get_issuer, identification_t*,
328 private_openssl_x509_t *this)
329{
330 return this->issuer;
331}
332
333METHOD(certificate_t, has_subject, id_match_t,
334 private_openssl_x509_t *this, identification_t *subject)
335{
336 identification_t *current;
337 enumerator_t *enumerator;
338 id_match_t match, best;
663e7355 339 chunk_t encoding;
5728c6aa
MW
340
341 if (subject->get_type(subject) == ID_KEY_ID)
342 {
663e7355
MW
343 encoding = subject->get_encoding(subject);
344
345 if (chunk_equals(this->hash, encoding))
346 {
347 return ID_MATCH_PERFECT;
348 }
349 if (this->subjectKeyIdentifier.len &&
350 chunk_equals(this->subjectKeyIdentifier, encoding))
351 {
352 return ID_MATCH_PERFECT;
353 }
354 if (this->pubkey &&
355 this->pubkey->has_fingerprint(this->pubkey, encoding))
5728c6aa
MW
356 {
357 return ID_MATCH_PERFECT;
358 }
ed1c4303
MW
359 if (chunk_equals(get_serial(this), encoding))
360 {
361 return ID_MATCH_PERFECT;
362 }
5728c6aa
MW
363 }
364 best = this->subject->matches(this->subject, subject);
365 enumerator = create_subjectAltName_enumerator(this);
366 while (enumerator->enumerate(enumerator, &current))
367 {
368 match = current->matches(current, subject);
369 if (match > best)
370 {
371 best = match;
372 }
373 }
374 enumerator->destroy(enumerator);
375 return best;
376}
377
378METHOD(certificate_t, has_issuer, id_match_t,
379 private_openssl_x509_t *this, identification_t *issuer)
380{
381 /* issuerAltNames currently not supported */
382 return this->issuer->matches(this->issuer, issuer);
383}
384
385METHOD(certificate_t, issued_by, bool,
a37f2d20
MW
386 private_openssl_x509_t *this, certificate_t *issuer,
387 signature_scheme_t *scheme)
5728c6aa
MW
388{
389 public_key_t *key;
390 bool valid;
391 x509_t *x509 = (x509_t*)issuer;
08d7e1f1 392 ASN1_BIT_STRING *sig;
5728c6aa
MW
393 chunk_t tbs;
394
395 if (&this->public.x509.interface == issuer)
396 {
397 if (this->flags & X509_SELF_SIGNED)
398 {
399 return TRUE;
400 }
401 }
402 else
403 {
404 if (issuer->get_type(issuer) != CERT_X509)
405 {
406 return FALSE;
407 }
408 if (!(x509->get_flags(x509) & X509_CA))
409 {
410 return FALSE;
411 }
412 if (!this->issuer->equals(this->issuer, issuer->get_subject(issuer)))
413 {
414 return FALSE;
415 }
416 }
417 if (this->scheme == SIGN_UNKNOWN)
418 {
419 return FALSE;
420 }
421 key = issuer->get_public_key(issuer);
422 if (!key)
423 {
424 return FALSE;
425 }
08d7e1f1
TB
426 /* i2d_re_X509_tbs() was added with 1.1.0 when X509 was made opaque */
427#if OPENSSL_VERSION_NUMBER >= 0x10100000L
428 tbs = openssl_i2chunk(re_X509_tbs, this->x509);
429#else
5728c6aa 430 tbs = openssl_i2chunk(X509_CINF, this->x509->cert_info);
08d7e1f1
TB
431#endif
432 X509_get0_signature(&sig, NULL, this->x509);
433 valid = key->verify(key, this->scheme, tbs, openssl_asn1_str2chunk(sig));
5728c6aa
MW
434 free(tbs.ptr);
435 key->destroy(key);
a37f2d20
MW
436 if (valid && scheme)
437 {
438 *scheme = this->scheme;
439 }
5728c6aa
MW
440 return valid;
441}
442
443METHOD(certificate_t, get_public_key, public_key_t*,
444 private_openssl_x509_t *this)
445{
446 return this->pubkey->get_ref(this->pubkey);
447}
448
449METHOD(certificate_t, get_validity, bool,
450 private_openssl_x509_t *this,
451 time_t *when, time_t *not_before, time_t *not_after)
452{
453 time_t t;
454
455 if (when)
456 {
457 t = *when;
458 }
459 else
460 {
461 t = time(NULL);
462 }
463 if (not_before)
464 {
465 *not_before = this->notBefore;
466 }
467 if (not_after)
468 {
469 *not_after = this->notAfter;
470 }
471 return (t >= this->notBefore && t <= this->notAfter);
472}
473
0406eeaa
MW
474METHOD(certificate_t, get_encoding, bool,
475 private_openssl_x509_t *this, cred_encoding_type_t type, chunk_t *encoding)
5728c6aa 476{
0406eeaa
MW
477 if (type == CERT_ASN1_DER)
478 {
479 *encoding = chunk_clone(this->encoding);
480 return TRUE;
481 }
482 return lib->encoding->encode(lib->encoding, type, NULL, encoding,
483 CRED_PART_X509_ASN1_DER, this->encoding, CRED_PART_END);
5728c6aa
MW
484}
485
0406eeaa 486
5728c6aa
MW
487METHOD(certificate_t, equals, bool,
488 private_openssl_x509_t *this, certificate_t *other)
489{
490 chunk_t encoding;
491 bool equal;
492
493 if (this == (private_openssl_x509_t*)other)
494 {
495 return TRUE;
496 }
497 if (other->get_type(other) != CERT_X509)
498 {
499 return FALSE;
500 }
501 if (other->equals == (void*)equals)
502 { /* skip allocation if we have the same implementation */
503 encoding = ((private_openssl_x509_t*)other)->encoding;
504 return chunk_equals(this->encoding, encoding);
505 }
0406eeaa
MW
506 if (!other->get_encoding(other, CERT_ASN1_DER, &encoding))
507 {
508 return FALSE;
509 }
5728c6aa
MW
510 equal = chunk_equals(this->encoding, encoding);
511 free(encoding.ptr);
512 return equal;
513}
514
515METHOD(certificate_t, get_ref, certificate_t*,
516 private_openssl_x509_t *this)
517{
518 ref_get(&this->ref);
519 return &this->public.x509.interface;
520}
521
522METHOD(certificate_t, destroy, void,
523 private_openssl_x509_t *this)
524{
525 if (ref_put(&this->ref))
526 {
527 if (this->x509)
528 {
529 X509_free(this->x509);
530 }
531 DESTROY_IF(this->subject);
532 DESTROY_IF(this->issuer);
533 DESTROY_IF(this->pubkey);
534 free(this->subjectKeyIdentifier.ptr);
535 free(this->authKeyIdentifier.ptr);
536 free(this->encoding.ptr);
537 free(this->hash.ptr);
538 this->subjectAltNames->destroy_offset(this->subjectAltNames,
539 offsetof(identification_t, destroy));
540 this->issuerAltNames->destroy_offset(this->issuerAltNames,
541 offsetof(identification_t, destroy));
4e7b7db6
TB
542 this->crl_uris->destroy_function(this->crl_uris,
543 (void*)x509_cdp_destroy);
5728c6aa 544 this->ocsp_uris->destroy_function(this->ocsp_uris, free);
5e4b1ad2
MR
545 this->ipAddrBlocks->destroy_offset(this->ipAddrBlocks,
546 offsetof(traffic_selector_t, destroy));
5728c6aa
MW
547 free(this);
548 }
549}
550
551/**
552 * Create an empty certificate
553 */
554static private_openssl_x509_t *create_empty()
555{
556 private_openssl_x509_t *this;
557
558 INIT(this,
559 .public = {
560 .x509 = {
561 .interface = {
562 .get_type = _get_type,
563 .get_subject = _get_subject,
564 .get_issuer = _get_issuer,
565 .has_subject = _has_subject,
566 .has_issuer = _has_issuer,
567 .issued_by = _issued_by,
568 .get_public_key = _get_public_key,
569 .get_validity = _get_validity,
5728c6aa
MW
570 .get_encoding = _get_encoding,
571 .equals = _equals,
572 .get_ref = _get_ref,
573 .destroy = _destroy,
574 },
575 .get_flags = _get_flags,
576 .get_serial = _get_serial,
577 .get_subjectKeyIdentifier = _get_subjectKeyIdentifier,
578 .get_authKeyIdentifier = _get_authKeyIdentifier,
b3d359e5 579 .get_constraint = _get_constraint,
5728c6aa
MW
580 .create_subjectAltName_enumerator = _create_subjectAltName_enumerator,
581 .create_crl_uri_enumerator = _create_crl_uri_enumerator,
582 .create_ocsp_uri_enumerator = _create_ocsp_uri_enumerator,
5e4b1ad2 583 .create_ipAddrBlock_enumerator = _create_ipAddrBlock_enumerator,
dbfbbec3 584 .create_name_constraint_enumerator = (void*)enumerator_create_empty,
20bd7810 585 .create_cert_policy_enumerator = (void*)enumerator_create_empty,
5a0caa4b 586 .create_policy_mapping_enumerator = (void*)enumerator_create_empty,
5728c6aa
MW
587 },
588 },
589 .subjectAltNames = linked_list_create(),
590 .issuerAltNames = linked_list_create(),
591 .crl_uris = linked_list_create(),
592 .ocsp_uris = linked_list_create(),
5e4b1ad2 593 .ipAddrBlocks = linked_list_create(),
5dba5852 594 .pathlen = X509_NO_CONSTRAINT,
5728c6aa
MW
595 .ref = 1,
596 );
597
598 return this;
599}
600
601/**
602 * parse an extionsion containing GENERAL_NAMES into a list
603 */
604static bool parse_generalNames_ext(linked_list_t *list,
605 X509_EXTENSION *ext)
606{
607 GENERAL_NAMES *names;
608 GENERAL_NAME *name;
609 identification_t *id;
610 int i, num;
611
612 names = X509V3_EXT_d2i(ext);
613 if (!names)
614 {
615 return FALSE;
616 }
617
618 num = sk_GENERAL_NAME_num(names);
619 for (i = 0; i < num; i++)
620 {
621 name = sk_GENERAL_NAME_value(names, i);
622 id = general_name2id(name);
623 if (id)
624 {
625 list->insert_last(list, id);
626 }
627 GENERAL_NAME_free(name);
628 }
629 sk_GENERAL_NAME_free(names);
630 return TRUE;
631}
632
633/**
634 * parse basic constraints
635 */
636static bool parse_basicConstraints_ext(private_openssl_x509_t *this,
637 X509_EXTENSION *ext)
638{
639 BASIC_CONSTRAINTS *constraints;
d390b3b9 640 long pathlen;
5728c6aa
MW
641
642 constraints = (BASIC_CONSTRAINTS*)X509V3_EXT_d2i(ext);
643 if (constraints)
644 {
645 if (constraints->ca)
646 {
647 this->flags |= X509_CA;
648 }
649 if (constraints->pathlen)
650 {
28bcbc29 651
d390b3b9
AS
652 pathlen = ASN1_INTEGER_get(constraints->pathlen);
653 this->pathlen = (pathlen >= 0 && pathlen < 128) ?
654 pathlen : X509_NO_CONSTRAINT;
5728c6aa
MW
655 }
656 BASIC_CONSTRAINTS_free(constraints);
657 return TRUE;
658 }
659 return FALSE;
660}
661
28bcbc29
TB
662/**
663 * parse key usage
664 */
665static bool parse_keyUsage_ext(private_openssl_x509_t *this,
666 X509_EXTENSION *ext)
667{
668 ASN1_BIT_STRING *usage;
669
670 usage = X509V3_EXT_d2i(ext);
671 if (usage)
672 {
673 if (usage->length > 0)
674 {
675 int flags = usage->data[0];
676 if (usage->length > 1)
677 {
678 flags |= usage->data[1] << 8;
679 }
e793d65a 680 if (flags & X509v3_KU_CRL_SIGN)
28bcbc29 681 {
e793d65a
TB
682 this->flags |= X509_CRL_SIGN;
683 }
684 if (flags & X509v3_KU_KEY_CERT_SIGN)
685 {
686 /* we use the caBasicContraint, MUST be set */
28bcbc29
TB
687 }
688 }
689 ASN1_BIT_STRING_free(usage);
690 return TRUE;
691 }
692 return FALSE;
693}
694
c3e7b3de
MW
695/**
696 * Parse ExtendedKeyUsage
697 */
698static bool parse_extKeyUsage_ext(private_openssl_x509_t *this,
699 X509_EXTENSION *ext)
700{
701 EXTENDED_KEY_USAGE *usage;
702 int i;
703
704 usage = X509V3_EXT_d2i(ext);
705 if (usage)
706 {
707 for (i = 0; i < sk_ASN1_OBJECT_num(usage); i++)
708 {
709 switch (OBJ_obj2nid(sk_ASN1_OBJECT_value(usage, i)))
710 {
711 case NID_server_auth:
712 this->flags |= X509_SERVER_AUTH;
713 break;
714 case NID_client_auth:
715 this->flags |= X509_CLIENT_AUTH;
716 break;
717 case NID_OCSP_sign:
718 this->flags |= X509_OCSP_SIGNER;
719 break;
720 default:
721 break;
722 }
723 }
724 sk_ASN1_OBJECT_pop_free(usage, ASN1_OBJECT_free);
725 return TRUE;
726 }
727 return FALSE;
728}
729
5728c6aa
MW
730/**
731 * Parse CRL distribution points
732 */
86c10a95
TB
733bool openssl_parse_crlDistributionPoints(X509_EXTENSION *ext,
734 linked_list_t *list)
5728c6aa
MW
735{
736 CRL_DIST_POINTS *cdps;
737 DIST_POINT *cdp;
a6478a04
MW
738 identification_t *id, *issuer;
739 x509_cdp_t *entry;
5728c6aa 740 char *uri;
86c10a95 741 int i, j, k, point_num, name_num, issuer_num, len;
5728c6aa
MW
742
743 cdps = X509V3_EXT_d2i(ext);
744 if (!cdps)
745 {
746 return FALSE;
747 }
748 point_num = sk_DIST_POINT_num(cdps);
749 for (i = 0; i < point_num; i++)
750 {
751 cdp = sk_DIST_POINT_value(cdps, i);
752 if (cdp)
753 {
754 if (cdp->distpoint && cdp->distpoint->type == 0 &&
755 cdp->distpoint->name.fullname)
756 {
757 name_num = sk_GENERAL_NAME_num(cdp->distpoint->name.fullname);
758 for (j = 0; j < name_num; j++)
759 {
760 id = general_name2id(sk_GENERAL_NAME_value(
761 cdp->distpoint->name.fullname, j));
762 if (id)
763 {
86c10a95
TB
764 len = asprintf(&uri, "%Y", id);
765 if (!len)
766 {
767 free(uri);
768 }
769 else if (len > 0)
5728c6aa 770 {
a6478a04
MW
771 if (cdp->CRLissuer)
772 {
773 issuer_num = sk_GENERAL_NAME_num(cdp->CRLissuer);
774 for (k = 0; k < issuer_num; k++)
775 {
776 issuer = general_name2id(
777 sk_GENERAL_NAME_value(cdp->CRLissuer, k));
778 if (issuer)
779 {
780 INIT(entry,
781 .uri = strdup(uri),
782 .issuer = issuer,
783 );
86c10a95 784 list->insert_last(list, entry);
a6478a04
MW
785 }
786 }
787 free(uri);
788 }
789 else
790 {
791 INIT(entry,
792 .uri = uri,
793 );
86c10a95 794 list->insert_last(list, entry);
a6478a04 795 }
5728c6aa
MW
796 }
797 id->destroy(id);
798 }
799 }
800 }
a6478a04 801
5728c6aa
MW
802 DIST_POINT_free(cdp);
803 }
804 }
805 sk_DIST_POINT_free(cdps);
806 return TRUE;
807}
808
809/**
810 * Parse authorityInfoAccess with OCSP URIs
811 */
812static bool parse_authorityInfoAccess_ext(private_openssl_x509_t *this,
813 X509_EXTENSION *ext)
814{
815 AUTHORITY_INFO_ACCESS *infos;
816 ACCESS_DESCRIPTION *desc;
817 identification_t *id;
86c10a95 818 int i, num, len;
5728c6aa
MW
819 char *uri;
820
821 infos = X509V3_EXT_d2i(ext);
822 if (!infos)
823 {
824 return FALSE;
825 }
826 num = sk_ACCESS_DESCRIPTION_num(infos);
827 for (i = 0; i < num; i++)
828 {
829 desc = sk_ACCESS_DESCRIPTION_value(infos, i);
830 if (desc)
831 {
832 if (openssl_asn1_known_oid(desc->method) == OID_OCSP)
833 {
834 id = general_name2id(desc->location);
835 if (id)
836 {
86c10a95
TB
837 len = asprintf(&uri, "%Y", id);
838 if (!len)
839 {
840 free(uri);
841 }
842 else if (len > 0)
5728c6aa 843 {
93818392 844 this->ocsp_uris->insert_last(this->ocsp_uris, uri);
5728c6aa
MW
845 }
846 id->destroy(id);
847 }
848 }
849 ACCESS_DESCRIPTION_free(desc);
850 }
851 }
852 sk_ACCESS_DESCRIPTION_free(infos);
853 return TRUE;
854}
855
5e4b1ad2
MR
856#ifndef OPENSSL_NO_RFC3779
857
858/**
859 * Parse a single block of ipAddrBlock extension
860 */
861static void parse_ipAddrBlock_ext_fam(private_openssl_x509_t *this,
862 IPAddressFamily *fam)
863{
864 const IPAddressOrRanges *list;
865 IPAddressOrRange *aor;
866 traffic_selector_t *ts;
867 ts_type_t type;
868 chunk_t from, to;
869 int i, afi;
870
871 if (fam->ipAddressChoice->type != IPAddressChoice_addressesOrRanges)
872 {
873 return;
874 }
875
08d7e1f1 876 afi = X509v3_addr_get_afi(fam);
5e4b1ad2
MR
877 switch (afi)
878 {
879 case IANA_AFI_IPV4:
880 from = chunk_alloca(4);
881 to = chunk_alloca(4);
882 type = TS_IPV4_ADDR_RANGE;
883 break;
884 case IANA_AFI_IPV6:
885 from = chunk_alloca(16);
886 to = chunk_alloca(16);
887 type = TS_IPV6_ADDR_RANGE;
888 break;
889 default:
890 return;
891 }
892
893 list = fam->ipAddressChoice->u.addressesOrRanges;
894 for (i = 0; i < sk_IPAddressOrRange_num(list); i++)
895 {
896 aor = sk_IPAddressOrRange_value(list, i);
08d7e1f1 897 if (X509v3_addr_get_range(aor, afi, from.ptr, to.ptr, from.len) > 0)
5e4b1ad2
MR
898 {
899 ts = traffic_selector_create_from_bytes(0, type, from, 0, to, 65535);
900 if (ts)
901 {
902 this->ipAddrBlocks->insert_last(this->ipAddrBlocks, ts);
903 }
904 }
905 }
906}
907
908/**
909 * Parse ipAddrBlock extension
910 */
911static bool parse_ipAddrBlock_ext(private_openssl_x509_t *this,
912 X509_EXTENSION *ext)
913{
914 STACK_OF(IPAddressFamily) *blocks;
915 IPAddressFamily *fam;
916
917 blocks = (STACK_OF(IPAddressFamily)*)X509V3_EXT_d2i(ext);
918 if (!blocks)
919 {
920 return FALSE;
921 }
922
08d7e1f1 923 if (!X509v3_addr_is_canonical(blocks))
5e4b1ad2
MR
924 {
925 sk_IPAddressFamily_free(blocks);
926 return FALSE;
927 }
928
929 while (sk_IPAddressFamily_num(blocks) > 0)
930 {
931 fam = sk_IPAddressFamily_pop(blocks);
932 parse_ipAddrBlock_ext_fam(this, fam);
933 IPAddressFamily_free(fam);
934 }
935 sk_IPAddressFamily_free(blocks);
936
937 this->flags |= X509_IP_ADDR_BLOCKS;
938 return TRUE;
939}
940#endif /* !OPENSSL_NO_RFC3779 */
941
5728c6aa
MW
942/**
943 * Parse authorityKeyIdentifier extension
944 */
945static bool parse_authKeyIdentifier_ext(private_openssl_x509_t *this,
946 X509_EXTENSION *ext)
947{
948 AUTHORITY_KEYID *keyid;
949
950 keyid = (AUTHORITY_KEYID*)X509V3_EXT_d2i(ext);
951 if (keyid)
952 {
953 free(this->authKeyIdentifier.ptr);
954 this->authKeyIdentifier = chunk_clone(
955 openssl_asn1_str2chunk(keyid->keyid));
956 AUTHORITY_KEYID_free(keyid);
957 return TRUE;
958 }
959 return FALSE;
960}
961
962/**
963 * Parse subjectKeyIdentifier extension
964 */
965static bool parse_subjectKeyIdentifier_ext(private_openssl_x509_t *this,
966 X509_EXTENSION *ext)
967{
f00a1015
MW
968 chunk_t ostr;
969
970 ostr = openssl_asn1_str2chunk(X509_EXTENSION_get_data(ext));
971 /* quick and dirty unwrap of octet string */
972 if (ostr.len > 2 &&
973 ostr.ptr[0] == V_ASN1_OCTET_STRING && ostr.ptr[1] == ostr.len - 2)
974 {
975 free(this->subjectKeyIdentifier.ptr);
976 this->subjectKeyIdentifier = chunk_clone(chunk_skip(ostr, 2));
977 return TRUE;
978 }
979 return FALSE;
5728c6aa
MW
980}
981
982/**
983 * Parse X509 extensions we are interested in
984 */
985static bool parse_extensions(private_openssl_x509_t *this)
986{
987 STACK_OF(X509_EXTENSION) *extensions;
988 int i, num;
989
08d7e1f1 990 extensions = X509_get0_extensions(this->x509);
5728c6aa
MW
991 if (extensions)
992 {
993 num = sk_X509_EXTENSION_num(extensions);
994
995 for (i = 0; i < num; i++)
996 {
997 X509_EXTENSION *ext;
998 bool ok;
999
1000 ext = sk_X509_EXTENSION_value(extensions, i);
1001 switch (OBJ_obj2nid(X509_EXTENSION_get_object(ext)))
1002 {
1003 case NID_info_access:
1004 ok = parse_authorityInfoAccess_ext(this, ext);
1005 break;
1006 case NID_authority_key_identifier:
1007 ok = parse_authKeyIdentifier_ext(this, ext);
1008 break;
1009 case NID_subject_key_identifier:
1010 ok = parse_subjectKeyIdentifier_ext(this, ext);
1011 break;
1012 case NID_subject_alt_name:
1013 ok = parse_generalNames_ext(this->subjectAltNames, ext);
1014 break;
1015 case NID_issuer_alt_name:
1016 ok = parse_generalNames_ext(this->issuerAltNames, ext);
1017 break;
1018 case NID_basic_constraints:
1019 ok = parse_basicConstraints_ext(this, ext);
1020 break;
28bcbc29
TB
1021 case NID_key_usage:
1022 ok = parse_keyUsage_ext(this, ext);
1023 break;
c3e7b3de
MW
1024 case NID_ext_key_usage:
1025 ok = parse_extKeyUsage_ext(this, ext);
1026 break;
5728c6aa 1027 case NID_crl_distribution_points:
86c10a95 1028 ok = openssl_parse_crlDistributionPoints(ext, this->crl_uris);
5728c6aa 1029 break;
5e4b1ad2
MR
1030#ifndef OPENSSL_NO_RFC3779
1031 case NID_sbgp_ipAddrBlock:
1032 ok = parse_ipAddrBlock_ext(this, ext);
1033 break;
1034#endif /* !OPENSSL_NO_RFC3779 */
5728c6aa 1035 default:
c4fd3b2f
AS
1036 ok = X509_EXTENSION_get_critical(ext) == 0 ||
1037 !lib->settings->get_bool(lib->settings,
8dc6e716 1038 "%s.x509.enforce_critical", TRUE, lib->ns);
b0892d09
MW
1039 if (!ok)
1040 {
3f55f203
MW
1041 char buf[80] = "";
1042
1043 OBJ_obj2txt(buf, sizeof(buf),
1044 X509_EXTENSION_get_object(ext), 0);
1045 DBG1(DBG_LIB, "found unsupported critical X.509 "
1046 "extension: %s", buf);
b0892d09 1047 }
5728c6aa
MW
1048 break;
1049 }
1050 if (!ok)
1051 {
1052 return FALSE;
1053 }
1054 }
1055 }
1056 return TRUE;
1057}
1058
1059/**
1060 * Parse a DER encoded x509 certificate
1061 */
1062static bool parse_certificate(private_openssl_x509_t *this)
1063{
1064 const unsigned char *ptr = this->encoding.ptr;
1065 hasher_t *hasher;
1066 chunk_t chunk;
08d7e1f1
TB
1067 ASN1_OBJECT *oid, *oid_tbs;
1068 X509_ALGOR *alg;
5728c6aa
MW
1069
1070 this->x509 = d2i_X509(NULL, &ptr, this->encoding.len);
1071 if (!this->x509)
1072 {
1073 return FALSE;
1074 }
a6850b84
MW
1075 if (X509_get_version(this->x509) < 0 || X509_get_version(this->x509) > 2)
1076 {
1077 DBG1(DBG_LIB, "unsupported x509 version: %d",
1078 X509_get_version(this->x509) + 1);
1079 return FALSE;
1080 }
1081
5728c6aa
MW
1082 this->subject = openssl_x509_name2id(X509_get_subject_name(this->x509));
1083 this->issuer = openssl_x509_name2id(X509_get_issuer_name(this->x509));
1084
08d7e1f1
TB
1085 if (!X509_PUBKEY_get0_param(&oid, NULL, NULL, NULL,
1086 X509_get_X509_PUBKEY(this->x509)))
1087 {
1088 return FALSE;
1089 }
1090 switch (openssl_asn1_known_oid(oid))
5728c6aa
MW
1091 {
1092 case OID_RSA_ENCRYPTION:
1093 this->pubkey = lib->creds->create(lib->creds,
1094 CRED_PUBLIC_KEY, KEY_RSA, BUILD_BLOB_ASN1_DER,
1095 openssl_asn1_str2chunk(X509_get0_pubkey_bitstr(this->x509)),
1096 BUILD_END);
1097 break;
1098 case OID_EC_PUBLICKEY:
1099 /* for ECDSA, we need the full subjectPublicKeyInfo, as it contains
1100 * the curve parameters. */
1101 chunk = openssl_i2chunk(X509_PUBKEY, X509_get_X509_PUBKEY(this->x509));
1102 this->pubkey = lib->creds->create(lib->creds,
1103 CRED_PUBLIC_KEY, KEY_ECDSA, BUILD_BLOB_ASN1_DER,
1104 chunk, BUILD_END);
1105 free(chunk.ptr);
1106 break;
1107 default:
1108 DBG1(DBG_LIB, "unsupported public key algorithm");
1109 break;
1110 }
1111 if (!this->subject || !this->issuer || !this->pubkey)
1112 {
1113 return FALSE;
1114 }
1115
1116 this->notBefore = openssl_asn1_to_time(X509_get_notBefore(this->x509));
1117 this->notAfter = openssl_asn1_to_time(X509_get_notAfter(this->x509));
1118
08d7e1f1
TB
1119 /* while X509_ALGOR_cmp() is declared in the headers of older OpenSSL
1120 * versions, at least on Ubuntu 14.04 it is not actually defined */
1121 X509_get0_signature(NULL, &alg, this->x509);
1122 X509_ALGOR_get0(&oid, NULL, NULL, alg);
1123 alg = X509_get0_tbs_sigalg(this->x509);
1124 X509_ALGOR_get0(&oid_tbs, NULL, NULL, alg);
1125 if (!chunk_equals(openssl_asn1_obj2chunk(oid),
1126 openssl_asn1_obj2chunk(oid_tbs)))
5728c6aa
MW
1127 {
1128 return FALSE;
1129 }
08d7e1f1 1130 this->scheme = signature_scheme_from_oid(openssl_asn1_known_oid(oid));
5728c6aa
MW
1131
1132 if (!parse_extensions(this))
1133 {
c4fd3b2f 1134 return FALSE;
5728c6aa
MW
1135 }
1136
1137 hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA1);
87dd205b 1138 if (!hasher || !hasher->allocate_hash(hasher, this->encoding, &this->hash))
5728c6aa 1139 {
87dd205b 1140 DESTROY_IF(hasher);
5728c6aa
MW
1141 return FALSE;
1142 }
5728c6aa
MW
1143 hasher->destroy(hasher);
1144
a37f2d20 1145 if (issued_by(this, &this->public.x509.interface, NULL))
5728c6aa
MW
1146 {
1147 this->flags |= X509_SELF_SIGNED;
1148 }
1149 return TRUE;
1150}
1151
1152openssl_x509_t *openssl_x509_load(certificate_type_t type, va_list args)
1153{
1154 chunk_t blob = chunk_empty;
1155 x509_flag_t flags = 0;
1156
1157 while (TRUE)
1158 {
1159 switch (va_arg(args, builder_part_t))
1160 {
1161 case BUILD_BLOB_ASN1_DER:
1162 blob = va_arg(args, chunk_t);
1163 continue;
1164 case BUILD_X509_FLAG:
1165 flags |= va_arg(args, x509_flag_t);
1166 continue;
1167 case BUILD_END:
1168 break;
1169 default:
1170 return NULL;
1171 }
1172 break;
1173 }
1174
1175 if (blob.ptr)
1176 {
1177 private_openssl_x509_t *this;
1178
1179 this = create_empty();
1180 this->encoding = chunk_clone(blob);
1181 this->flags |= flags;
1182 if (parse_certificate(this))
1183 {
1184 return &this->public;
1185 }
1186 DBG1(DBG_LIB, "OpenSSL X.509 parsing failed");
1187 destroy(this);
1188 }
1189 return NULL;
1190}