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