2 * Copyright (C) 2011-2023 Tobias Brunner
3 * Copyright (C) 2010 Martin Willi
5 * Copyright (C) secunet Security Networks AG
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 * Copyright (C) 2013 Michael Rossberg
20 * Copyright (C) 2013 Technische Universität Ilmenau
22 * Copyright (C) 2010 Thomas Egerer
24 * Permission is hereby granted, free of charge, to any person obtaining a copy
25 * of this software and associated documentation files (the "Software"), to deal
26 * in the Software without restriction, including without limitation the rights
27 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
28 * copies of the Software, and to permit persons to whom the Software is
29 * furnished to do so, subject to the following conditions:
31 * The above copyright notice and this permission notice shall be included in
32 * all copies or substantial portions of the Software.
34 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
35 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
36 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
37 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
38 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
39 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
45 #include <openssl/x509.h>
46 #include <openssl/x509v3.h>
48 #include "openssl_x509.h"
49 #include "openssl_util.h"
51 #include <utils/debug.h>
53 #include <collections/linked_list.h>
54 #include <selectors/traffic_selector.h>
56 /* IP Addr block extension support was introduced with 0.9.8e */
57 #if OPENSSL_VERSION_NUMBER < 0x0090805fL
58 #define OPENSSL_NO_RFC3779
61 /* added with 1.0.2 */
62 #if OPENSSL_VERSION_NUMBER < 0x10002000L
63 static inline void X509_get0_signature(ASN1_BIT_STRING
**psig
, X509_ALGOR
**palg
, const X509
*x
) {
64 if (psig
) { *psig
= x
->signature
; }
65 if (palg
) { *palg
= x
->sig_alg
; }
69 /* added with 1.1.0 when X509 etc. was made opaque */
70 #if OPENSSL_VERSION_NUMBER < 0x10100000L
71 #define X509_get0_extensions(x509) ({ (x509)->cert_info->extensions; })
72 #define X509_get0_tbs_sigalg(x509) ({ (x509)->cert_info->signature; })
73 #define X509_ALGOR_get0(oid, ppt, ppv, alg) ({ *(oid) = (alg)->algorithm; })
74 #define X509_PUBKEY_get0_param(oid, pk, len, pa, pub) X509_ALGOR_get0(oid, NULL, NULL, (pub)->algor)
75 #define X509v3_addr_get_afi v3_addr_get_afi
76 #define X509v3_addr_get_range v3_addr_get_range
77 #define X509v3_addr_is_canonical v3_addr_is_canonical
78 #define X509_get0_notBefore X509_get_notBefore
79 #define X509_get0_notAfter X509_get_notAfter
82 typedef struct private_openssl_x509_t private_openssl_x509_t
;
85 * Private data of an openssl_x509_t object.
87 struct private_openssl_x509_t
{
90 * Public openssl_x509_t interface.
92 openssl_x509_t
public;
95 * OpenSSL certificate representation
100 * DER encoded certificate
105 * SHA1 hash of the certificate
120 * certificate subject
122 identification_t
*subject
;
127 identification_t
*issuer
;
130 * Certificates public key
132 public_key_t
*pubkey
;
135 * subjectKeyIdentifier as read from cert
137 chunk_t subjectKeyIdentifier
;
140 * authorityKeyIdentifier as read from cert
142 chunk_t authKeyIdentifier
;
145 * Start time of certificate validity
150 * End time of certificate validity
155 * Signature scheme of the certificate
157 signature_params_t
*scheme
;
162 linked_list_t
*subjectAltNames
;
167 linked_list_t
*issuerAltNames
;
170 * List of CRL URIs, as x509_cdp_t
172 linked_list_t
*crl_uris
;
177 linked_list_t
*ocsp_uris
;
180 * List of ipAddrBlocks as traffic_selector_t
182 linked_list_t
*ipAddrBlocks
;
185 * List of permitted name constraints
187 linked_list_t
*permitted_names
;
190 * List of excluded name constraints
192 linked_list_t
*excluded_names
;
195 * References to this cert
201 * Convert a GeneralName to an identification_t.
203 static identification_t
*general_name2id(GENERAL_NAME
*name
)
212 return identification_create_from_encoding(ID_RFC822_ADDR
,
213 openssl_asn1_str2chunk(name
->d
.rfc822Name
));
215 return identification_create_from_encoding(ID_FQDN
,
216 openssl_asn1_str2chunk(name
->d
.dNSName
));
218 return identification_create_from_encoding(ID_DER_ASN1_GN_URI
,
219 openssl_asn1_str2chunk(name
->d
.uniformResourceIdentifier
));
222 chunk_t chunk
= openssl_asn1_str2chunk(name
->d
.iPAddress
);
225 return identification_create_from_encoding(ID_IPV4_ADDR
, chunk
);
229 return identification_create_from_encoding(ID_IPV4_ADDR_SUBNET
,
234 return identification_create_from_encoding(ID_IPV6_ADDR
, chunk
);
238 return identification_create_from_encoding(ID_IPV6_ADDR_SUBNET
,
244 return openssl_x509_name2id(name
->d
.directoryName
);
246 if (OBJ_obj2nid(name
->d
.otherName
->type_id
) == NID_ms_upn
&&
247 name
->d
.otherName
->value
->type
== V_ASN1_UTF8STRING
)
249 return identification_create_from_encoding(ID_RFC822_ADDR
,
250 openssl_asn1_str2chunk(
251 name
->d
.otherName
->value
->value
.utf8string
));
259 METHOD(x509_t
, get_flags
, x509_flag_t
,
260 private_openssl_x509_t
*this)
265 METHOD(x509_t
, get_serial
, chunk_t
,
266 private_openssl_x509_t
*this)
268 return openssl_asn1_int2chunk(X509_get_serialNumber(this->x509
));
271 METHOD(x509_t
, get_subjectKeyIdentifier
, chunk_t
,
272 private_openssl_x509_t
*this)
276 if (this->subjectKeyIdentifier
.len
)
278 return this->subjectKeyIdentifier
;
280 if (this->pubkey
->get_fingerprint(this->pubkey
, KEYID_PUBKEY_SHA1
,
288 METHOD(x509_t
, get_authKeyIdentifier
, chunk_t
,
289 private_openssl_x509_t
*this)
291 if (this->authKeyIdentifier
.len
)
293 return this->authKeyIdentifier
;
298 METHOD(x509_t
, get_constraint
, u_int
,
299 private_openssl_x509_t
*this, x509_constraint_t type
)
304 return this->pathlen
;
306 return X509_NO_CONSTRAINT
;
310 METHOD(x509_t
, create_subjectAltName_enumerator
, enumerator_t
*,
311 private_openssl_x509_t
*this)
313 return this->subjectAltNames
->create_enumerator(this->subjectAltNames
);
316 METHOD(x509_t
, create_crl_uri_enumerator
, enumerator_t
*,
317 private_openssl_x509_t
*this)
319 return this->crl_uris
->create_enumerator(this->crl_uris
);
322 METHOD(x509_t
, create_ocsp_uri_enumerator
, enumerator_t
*,
323 private_openssl_x509_t
*this)
325 return this->ocsp_uris
->create_enumerator(this->ocsp_uris
);
328 METHOD(x509_t
, create_ipAddrBlock_enumerator
, enumerator_t
*,
329 private_openssl_x509_t
*this)
331 return this->ipAddrBlocks
->create_enumerator(this->ipAddrBlocks
);
334 METHOD(x509_t
, create_name_constraint_enumerator
, enumerator_t
*,
335 private_openssl_x509_t
*this, bool perm
)
339 return this->permitted_names
->create_enumerator(this->permitted_names
);
341 return this->excluded_names
->create_enumerator(this->excluded_names
);
344 METHOD(certificate_t
, get_type
, certificate_type_t
,
345 private_openssl_x509_t
*this)
350 METHOD(certificate_t
, get_subject
, identification_t
*,
351 private_openssl_x509_t
*this)
353 return this->subject
;
356 METHOD(certificate_t
, get_issuer
, identification_t
*,
357 private_openssl_x509_t
*this)
362 METHOD(certificate_t
, has_subject
, id_match_t
,
363 private_openssl_x509_t
*this, identification_t
*subject
)
365 identification_t
*current
;
366 enumerator_t
*enumerator
;
367 id_match_t match
, best
;
370 if (subject
->get_type(subject
) == ID_KEY_ID
)
372 encoding
= subject
->get_encoding(subject
);
374 if (chunk_equals(this->hash
, encoding
))
376 return ID_MATCH_PERFECT
;
378 if (this->subjectKeyIdentifier
.len
&&
379 chunk_equals(this->subjectKeyIdentifier
, encoding
))
381 return ID_MATCH_PERFECT
;
384 this->pubkey
->has_fingerprint(this->pubkey
, encoding
))
386 return ID_MATCH_PERFECT
;
388 if (chunk_equals(get_serial(this), encoding
))
390 return ID_MATCH_PERFECT
;
393 best
= this->subject
->matches(this->subject
, subject
);
394 enumerator
= create_subjectAltName_enumerator(this);
395 while (enumerator
->enumerate(enumerator
, ¤t
))
397 match
= current
->matches(current
, subject
);
403 enumerator
->destroy(enumerator
);
407 METHOD(certificate_t
, has_issuer
, id_match_t
,
408 private_openssl_x509_t
*this, identification_t
*issuer
)
410 /* issuerAltNames currently not supported */
411 return this->issuer
->matches(this->issuer
, issuer
);
414 METHOD(certificate_t
, issued_by
, bool,
415 private_openssl_x509_t
*this, certificate_t
*issuer
,
416 signature_params_t
**scheme
)
420 x509_t
*x509
= (x509_t
*)issuer
;
421 chunk_t keyid
= chunk_empty
;
422 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
423 const ASN1_BIT_STRING
*sig
;
425 ASN1_BIT_STRING
*sig
;
429 if (&this->public.x509
.interface
== issuer
)
431 if (this->flags
& X509_SELF_SIGNED
)
439 if (issuer
->get_type(issuer
) != CERT_X509
)
443 if (!(x509
->get_flags(x509
) & X509_CA
))
449 /* compare keyIdentifiers if available, otherwise use DNs */
450 if (this->authKeyIdentifier
.ptr
)
452 keyid
= x509
->get_subjectKeyIdentifier(x509
);
453 if (keyid
.len
&& !chunk_equals(keyid
, this->authKeyIdentifier
))
460 if (!this->issuer
->equals(this->issuer
, issuer
->get_subject(issuer
)))
466 key
= issuer
->get_public_key(issuer
);
471 /* i2d_re_X509_tbs() was added with 1.1.0 when X509 was made opaque */
472 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
473 tbs
= openssl_i2chunk(re_X509_tbs
, this->x509
);
475 tbs
= openssl_i2chunk(X509_CINF
, this->x509
->cert_info
);
477 X509_get0_signature(&sig
, NULL
, this->x509
);
478 valid
= key
->verify(key
, this->scheme
->scheme
, this->scheme
->params
, tbs
,
479 openssl_asn1_str2chunk(sig
));
486 *scheme
= signature_params_clone(this->scheme
);
491 METHOD(certificate_t
, get_public_key
, public_key_t
*,
492 private_openssl_x509_t
*this)
494 return this->pubkey
->get_ref(this->pubkey
);
497 METHOD(certificate_t
, get_validity
, bool,
498 private_openssl_x509_t
*this,
499 time_t *when
, time_t *not_before
, time_t *not_after
)
513 *not_before
= this->notBefore
;
517 *not_after
= this->notAfter
;
519 return (t
>= this->notBefore
&& t
<= this->notAfter
);
522 METHOD(certificate_t
, get_encoding
, bool,
523 private_openssl_x509_t
*this, cred_encoding_type_t type
, chunk_t
*encoding
)
525 if (type
== CERT_ASN1_DER
)
527 *encoding
= chunk_clone(this->encoding
);
530 return lib
->encoding
->encode(lib
->encoding
, type
, NULL
, encoding
,
531 CRED_PART_X509_ASN1_DER
, this->encoding
, CRED_PART_END
);
535 METHOD(certificate_t
, equals
, bool,
536 private_openssl_x509_t
*this, certificate_t
*other
)
541 if (this == (private_openssl_x509_t
*)other
)
545 if (other
->get_type(other
) != CERT_X509
)
549 if (other
->equals
== (void*)equals
)
550 { /* skip allocation if we have the same implementation */
551 encoding
= ((private_openssl_x509_t
*)other
)->encoding
;
552 return chunk_equals(this->encoding
, encoding
);
554 if (!other
->get_encoding(other
, CERT_ASN1_DER
, &encoding
))
558 equal
= chunk_equals(this->encoding
, encoding
);
563 METHOD(certificate_t
, get_ref
, certificate_t
*,
564 private_openssl_x509_t
*this)
567 return &this->public.x509
.interface
;
570 METHOD(certificate_t
, destroy
, void,
571 private_openssl_x509_t
*this)
573 if (ref_put(&this->ref
))
577 X509_free(this->x509
);
579 signature_params_destroy(this->scheme
);
580 DESTROY_IF(this->subject
);
581 DESTROY_IF(this->issuer
);
582 DESTROY_IF(this->pubkey
);
583 free(this->subjectKeyIdentifier
.ptr
);
584 free(this->authKeyIdentifier
.ptr
);
585 free(this->encoding
.ptr
);
586 free(this->hash
.ptr
);
587 this->subjectAltNames
->destroy_offset(this->subjectAltNames
,
588 offsetof(identification_t
, destroy
));
589 this->issuerAltNames
->destroy_offset(this->issuerAltNames
,
590 offsetof(identification_t
, destroy
));
591 this->crl_uris
->destroy_function(this->crl_uris
,
592 (void*)x509_cdp_destroy
);
593 this->ocsp_uris
->destroy_function(this->ocsp_uris
, free
);
594 this->ipAddrBlocks
->destroy_offset(this->ipAddrBlocks
,
595 offsetof(traffic_selector_t
, destroy
));
596 this->permitted_names
->destroy_offset(this->permitted_names
,
597 offsetof(identification_t
, destroy
));
598 this->excluded_names
->destroy_offset(this->excluded_names
,
599 offsetof(identification_t
, destroy
));
605 * Create an empty certificate
607 static private_openssl_x509_t
*create_empty()
609 private_openssl_x509_t
*this;
615 .get_type
= _get_type
,
616 .get_subject
= _get_subject
,
617 .get_issuer
= _get_issuer
,
618 .has_subject
= _has_subject
,
619 .has_issuer
= _has_issuer
,
620 .issued_by
= _issued_by
,
621 .get_public_key
= _get_public_key
,
622 .get_validity
= _get_validity
,
623 .get_encoding
= _get_encoding
,
628 .get_flags
= _get_flags
,
629 .get_serial
= _get_serial
,
630 .get_subjectKeyIdentifier
= _get_subjectKeyIdentifier
,
631 .get_authKeyIdentifier
= _get_authKeyIdentifier
,
632 .get_constraint
= _get_constraint
,
633 .create_subjectAltName_enumerator
= _create_subjectAltName_enumerator
,
634 .create_crl_uri_enumerator
= _create_crl_uri_enumerator
,
635 .create_ocsp_uri_enumerator
= _create_ocsp_uri_enumerator
,
636 .create_ipAddrBlock_enumerator
= _create_ipAddrBlock_enumerator
,
637 .create_name_constraint_enumerator
= _create_name_constraint_enumerator
,
638 .create_cert_policy_enumerator
= (void*)enumerator_create_empty
,
639 .create_policy_mapping_enumerator
= (void*)enumerator_create_empty
,
642 .subjectAltNames
= linked_list_create(),
643 .issuerAltNames
= linked_list_create(),
644 .crl_uris
= linked_list_create(),
645 .ocsp_uris
= linked_list_create(),
646 .ipAddrBlocks
= linked_list_create(),
647 .permitted_names
= linked_list_create(),
648 .excluded_names
= linked_list_create(),
649 .pathlen
= X509_NO_CONSTRAINT
,
657 * parse an extension containing GENERAL_NAMES into a list
659 static bool parse_generalNames_ext(linked_list_t
*list
,
662 GENERAL_NAMES
*names
;
664 identification_t
*id
;
667 names
= X509V3_EXT_d2i(ext
);
673 num
= sk_GENERAL_NAME_num(names
);
674 for (i
= 0; i
< num
; i
++)
676 name
= sk_GENERAL_NAME_value(names
, i
);
677 id
= general_name2id(name
);
680 list
->insert_last(list
, id
);
682 GENERAL_NAME_free(name
);
684 sk_GENERAL_NAME_free(names
);
689 * parse basic constraints
691 static bool parse_basicConstraints_ext(private_openssl_x509_t
*this,
694 BASIC_CONSTRAINTS
*constraints
;
697 constraints
= (BASIC_CONSTRAINTS
*)X509V3_EXT_d2i(ext
);
702 this->flags
|= X509_CA
;
704 if (constraints
->pathlen
)
707 pathlen
= ASN1_INTEGER_get(constraints
->pathlen
);
708 this->pathlen
= (pathlen
>= 0 && pathlen
< 128) ?
709 pathlen
: X509_NO_CONSTRAINT
;
711 BASIC_CONSTRAINTS_free(constraints
);
720 static bool parse_keyUsage_ext(private_openssl_x509_t
*this,
723 ASN1_BIT_STRING
*usage
;
725 usage
= X509V3_EXT_d2i(ext
);
728 if (usage
->length
> 0)
730 int flags
= usage
->data
[0];
731 if (usage
->length
> 1)
733 flags
|= usage
->data
[1] << 8;
735 if (flags
& X509v3_KU_CRL_SIGN
)
737 this->flags
|= X509_CRL_SIGN
;
739 if (flags
& X509v3_KU_DIGITAL_SIGNATURE
||
740 flags
& X509v3_KU_NON_REPUDIATION
)
742 this->flags
|= X509_IKE_COMPLIANT
;
744 if (flags
& X509v3_KU_KEY_CERT_SIGN
)
746 /* we use the caBasicConstraint, MUST be set */
749 ASN1_BIT_STRING_free(usage
);
756 * Parse ExtendedKeyUsage
758 static bool parse_extKeyUsage_ext(private_openssl_x509_t
*this,
761 EXTENDED_KEY_USAGE
*usage
;
764 usage
= X509V3_EXT_d2i(ext
);
767 for (i
= 0; i
< sk_ASN1_OBJECT_num(usage
); i
++)
769 switch (OBJ_obj2nid(sk_ASN1_OBJECT_value(usage
, i
)))
771 case NID_server_auth
:
772 this->flags
|= X509_SERVER_AUTH
;
774 case NID_client_auth
:
775 this->flags
|= X509_CLIENT_AUTH
;
778 this->flags
|= X509_OCSP_SIGNER
;
784 sk_ASN1_OBJECT_pop_free(usage
, ASN1_OBJECT_free
);
791 * Parse CRL distribution points
793 bool openssl_parse_crlDistributionPoints(X509_EXTENSION
*ext
,
796 CRL_DIST_POINTS
*cdps
;
798 identification_t
*id
, *issuer
;
801 int i
, j
, k
, point_num
, name_num
, issuer_num
, len
;
803 cdps
= X509V3_EXT_d2i(ext
);
808 point_num
= sk_DIST_POINT_num(cdps
);
809 for (i
= 0; i
< point_num
; i
++)
811 cdp
= sk_DIST_POINT_value(cdps
, i
);
814 if (cdp
->distpoint
&& cdp
->distpoint
->type
== 0 &&
815 cdp
->distpoint
->name
.fullname
)
817 name_num
= sk_GENERAL_NAME_num(cdp
->distpoint
->name
.fullname
);
818 for (j
= 0; j
< name_num
; j
++)
820 id
= general_name2id(sk_GENERAL_NAME_value(
821 cdp
->distpoint
->name
.fullname
, j
));
824 len
= asprintf(&uri
, "%Y", id
);
833 issuer_num
= sk_GENERAL_NAME_num(cdp
->CRLissuer
);
834 for (k
= 0; k
< issuer_num
; k
++)
836 issuer
= general_name2id(
837 sk_GENERAL_NAME_value(cdp
->CRLissuer
, k
));
844 list
->insert_last(list
, entry
);
854 list
->insert_last(list
, entry
);
862 DIST_POINT_free(cdp
);
865 sk_DIST_POINT_free(cdps
);
870 * Parse authorityInfoAccess with OCSP URIs
872 static bool parse_authorityInfoAccess_ext(private_openssl_x509_t
*this,
875 AUTHORITY_INFO_ACCESS
*infos
;
876 ACCESS_DESCRIPTION
*desc
;
877 identification_t
*id
;
881 infos
= X509V3_EXT_d2i(ext
);
886 num
= sk_ACCESS_DESCRIPTION_num(infos
);
887 for (i
= 0; i
< num
; i
++)
889 desc
= sk_ACCESS_DESCRIPTION_value(infos
, i
);
892 if (openssl_asn1_known_oid(desc
->method
) == OID_OCSP
)
894 id
= general_name2id(desc
->location
);
897 len
= asprintf(&uri
, "%Y", id
);
904 this->ocsp_uris
->insert_last(this->ocsp_uris
, uri
);
909 ACCESS_DESCRIPTION_free(desc
);
912 sk_ACCESS_DESCRIPTION_free(infos
);
916 #ifndef OPENSSL_NO_RFC3779
919 * Parse a single block of ipAddrBlock extension
921 static void parse_ipAddrBlock_ext_fam(private_openssl_x509_t
*this,
922 IPAddressFamily
*fam
)
924 const IPAddressOrRanges
*list
;
925 IPAddressOrRange
*aor
;
926 traffic_selector_t
*ts
;
931 if (fam
->ipAddressChoice
->type
!= IPAddressChoice_addressesOrRanges
)
936 afi
= X509v3_addr_get_afi(fam
);
940 from
= chunk_alloca(4);
941 to
= chunk_alloca(4);
942 type
= TS_IPV4_ADDR_RANGE
;
945 from
= chunk_alloca(16);
946 to
= chunk_alloca(16);
947 type
= TS_IPV6_ADDR_RANGE
;
953 list
= fam
->ipAddressChoice
->u
.addressesOrRanges
;
954 for (i
= 0; i
< sk_IPAddressOrRange_num(list
); i
++)
956 aor
= sk_IPAddressOrRange_value(list
, i
);
957 if (X509v3_addr_get_range(aor
, afi
, from
.ptr
, to
.ptr
, from
.len
) > 0)
959 ts
= traffic_selector_create_from_bytes(0, type
, from
, 0, to
, 65535);
962 this->ipAddrBlocks
->insert_last(this->ipAddrBlocks
, ts
);
969 * Parse ipAddrBlock extension
971 static bool parse_ipAddrBlock_ext(private_openssl_x509_t
*this,
974 STACK_OF(IPAddressFamily
) *blocks
;
975 IPAddressFamily
*fam
;
977 blocks
= (STACK_OF(IPAddressFamily
)*)X509V3_EXT_d2i(ext
);
983 if (!X509v3_addr_is_canonical(blocks
))
985 sk_IPAddressFamily_free(blocks
);
989 while (sk_IPAddressFamily_num(blocks
) > 0)
991 fam
= sk_IPAddressFamily_pop(blocks
);
992 parse_ipAddrBlock_ext_fam(this, fam
);
993 IPAddressFamily_free(fam
);
995 sk_IPAddressFamily_free(blocks
);
997 this->flags
|= X509_IP_ADDR_BLOCKS
;
1000 #endif /* !OPENSSL_NO_RFC3779 */
1003 * Parse a "generalSubtree" structure (sequence of generalNames)
1005 static bool parse_generalSubtrees(linked_list_t
*list
,
1006 STACK_OF(GENERAL_SUBTREE
) *subtrees
)
1008 GENERAL_SUBTREE
*subtree
;
1009 identification_t
*id
;
1012 for (i
= 0; i
< sk_GENERAL_SUBTREE_num(subtrees
); i
++)
1014 subtree
= sk_GENERAL_SUBTREE_value(subtrees
, i
);
1015 id
= general_name2id(subtree
->base
);
1018 list
->insert_last(list
, id
);
1029 * Parse permitted/excluded nameConstraints
1031 static bool parse_nameConstraints_ext(private_openssl_x509_t
*this,
1032 X509_EXTENSION
*ext
)
1034 NAME_CONSTRAINTS
*nc
;
1037 nc
= (NAME_CONSTRAINTS
*)X509V3_EXT_d2i(ext
);
1040 ok
= parse_generalSubtrees(this->permitted_names
, nc
->permittedSubtrees
) &&
1041 parse_generalSubtrees(this->excluded_names
, nc
->excludedSubtrees
);
1042 NAME_CONSTRAINTS_free(nc
);
1048 * Parse authorityKeyIdentifier extension
1050 static bool parse_authKeyIdentifier_ext(private_openssl_x509_t
*this,
1051 X509_EXTENSION
*ext
)
1053 AUTHORITY_KEYID
*keyid
;
1055 keyid
= (AUTHORITY_KEYID
*)X509V3_EXT_d2i(ext
);
1058 free(this->authKeyIdentifier
.ptr
);
1059 this->authKeyIdentifier
= chunk_clone(
1060 openssl_asn1_str2chunk(keyid
->keyid
));
1061 AUTHORITY_KEYID_free(keyid
);
1068 * Parse subjectKeyIdentifier extension
1070 static bool parse_subjectKeyIdentifier_ext(private_openssl_x509_t
*this,
1071 X509_EXTENSION
*ext
)
1075 ostr
= openssl_asn1_str2chunk(X509_EXTENSION_get_data(ext
));
1076 /* quick and dirty unwrap of octet string */
1078 ostr
.ptr
[0] == V_ASN1_OCTET_STRING
&& ostr
.ptr
[1] == ostr
.len
- 2)
1080 free(this->subjectKeyIdentifier
.ptr
);
1081 this->subjectKeyIdentifier
= chunk_clone(chunk_skip(ostr
, 2));
1088 * Parse X509 extensions we are interested in
1090 static bool parse_extensions(private_openssl_x509_t
*this)
1092 const STACK_OF(X509_EXTENSION
) *extensions
;
1093 bool key_usage_parsed
= FALSE
;
1096 extensions
= X509_get0_extensions(this->x509
);
1099 num
= sk_X509_EXTENSION_num(extensions
);
1101 for (i
= 0; i
< num
; i
++)
1103 X509_EXTENSION
*ext
;
1106 ext
= sk_X509_EXTENSION_value(extensions
, i
);
1107 switch (OBJ_obj2nid(X509_EXTENSION_get_object(ext
)))
1109 case NID_info_access
:
1110 ok
= parse_authorityInfoAccess_ext(this, ext
);
1112 case NID_authority_key_identifier
:
1113 ok
= parse_authKeyIdentifier_ext(this, ext
);
1115 case NID_subject_key_identifier
:
1116 ok
= parse_subjectKeyIdentifier_ext(this, ext
);
1118 case NID_subject_alt_name
:
1119 ok
= parse_generalNames_ext(this->subjectAltNames
, ext
);
1121 case NID_issuer_alt_name
:
1122 ok
= parse_generalNames_ext(this->issuerAltNames
, ext
);
1124 case NID_basic_constraints
:
1125 ok
= parse_basicConstraints_ext(this, ext
);
1128 ok
= parse_keyUsage_ext(this, ext
);
1129 key_usage_parsed
= TRUE
;
1131 case NID_ext_key_usage
:
1132 ok
= parse_extKeyUsage_ext(this, ext
);
1134 case NID_crl_distribution_points
:
1135 ok
= openssl_parse_crlDistributionPoints(ext
, this->crl_uris
);
1137 #ifndef OPENSSL_NO_RFC3779
1138 case NID_sbgp_ipAddrBlock
:
1139 ok
= parse_ipAddrBlock_ext(this, ext
);
1141 #endif /* !OPENSSL_NO_RFC3779 */
1142 case NID_name_constraints
:
1143 ok
= parse_nameConstraints_ext(this, ext
);
1146 ok
= X509_EXTENSION_get_critical(ext
) == 0 ||
1147 !lib
->settings
->get_bool(lib
->settings
,
1148 "%s.x509.enforce_critical", TRUE
, lib
->ns
);
1153 OBJ_obj2txt(buf
, sizeof(buf
),
1154 X509_EXTENSION_get_object(ext
), 0);
1155 DBG1(DBG_LIB
, "found unsupported critical X.509 "
1156 "extension: %s", buf
);
1166 if (!key_usage_parsed
)
1168 /* we are compliant with RFC 4945 without keyUsage extension */
1169 this->flags
|= X509_IKE_COMPLIANT
;
1170 /* allow CA certificates without keyUsage extension to sign CRLs */
1171 if (this->flags
& X509_CA
)
1173 this->flags
|= X509_CRL_SIGN
;
1180 * Parse a DER encoded x509 certificate
1182 static bool parse_certificate(private_openssl_x509_t
*this)
1184 const unsigned char *ptr
= this->encoding
.ptr
;
1186 chunk_t chunk
, sig_scheme
, sig_scheme_tbs
;
1188 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
1189 const X509_ALGOR
*alg
;
1193 key_type_t ed_type
= KEY_ED448
;
1195 this->x509
= d2i_X509(NULL
, &ptr
, this->encoding
.len
);
1200 if (X509_get_version(this->x509
) < 0 || X509_get_version(this->x509
) > 2)
1202 DBG1(DBG_LIB
, "unsupported x509 version: %d",
1203 X509_get_version(this->x509
) + 1);
1207 this->subject
= openssl_x509_name2id(X509_get_subject_name(this->x509
));
1208 this->issuer
= openssl_x509_name2id(X509_get_issuer_name(this->x509
));
1210 if (!X509_PUBKEY_get0_param(&oid
, NULL
, NULL
, NULL
,
1211 X509_get_X509_PUBKEY(this->x509
)))
1215 switch (openssl_asn1_known_oid(oid
))
1217 case OID_RSASSA_PSS
:
1218 /* TODO: we should treat such keys special and use the params as
1219 * restrictions regarding the use of this key (or rather the
1220 * associated private key) */
1221 case OID_RSA_ENCRYPTION
:
1222 this->pubkey
= lib
->creds
->create(lib
->creds
,
1223 CRED_PUBLIC_KEY
, KEY_RSA
, BUILD_BLOB_ASN1_DER
,
1224 openssl_asn1_str2chunk(X509_get0_pubkey_bitstr(this->x509
)),
1227 case OID_EC_PUBLICKEY
:
1228 /* for ECDSA, we need the full subjectPublicKeyInfo, as it contains
1229 * the curve parameters. */
1230 chunk
= openssl_i2chunk(X509_PUBKEY
, X509_get_X509_PUBKEY(this->x509
));
1231 this->pubkey
= lib
->creds
->create(lib
->creds
,
1232 CRED_PUBLIC_KEY
, KEY_ECDSA
, BUILD_BLOB_ASN1_DER
,
1237 ed_type
= KEY_ED25519
;
1240 /* for EdDSA, the parsers expect the full subjectPublicKeyInfo */
1241 chunk
= openssl_i2chunk(X509_PUBKEY
, X509_get_X509_PUBKEY(this->x509
));
1242 this->pubkey
= lib
->creds
->create(lib
->creds
,
1243 CRED_PUBLIC_KEY
, ed_type
, BUILD_BLOB_ASN1_DER
,
1248 DBG1(DBG_LIB
, "unsupported public key algorithm");
1251 if (!this->subject
|| !this->issuer
|| !this->pubkey
)
1256 this->notBefore
= openssl_asn1_to_time(X509_get0_notBefore(this->x509
));
1257 this->notAfter
= openssl_asn1_to_time(X509_get0_notAfter(this->x509
));
1259 /* while X509_ALGOR_cmp() is declared in the headers of older OpenSSL
1260 * versions, at least on Ubuntu 14.04 it is not actually defined */
1261 X509_get0_signature(NULL
, &alg
, this->x509
);
1262 sig_scheme
= openssl_i2chunk(X509_ALGOR
, (X509_ALGOR
*)alg
);
1263 alg
= X509_get0_tbs_sigalg(this->x509
);
1264 sig_scheme_tbs
= openssl_i2chunk(X509_ALGOR
, (X509_ALGOR
*)alg
);
1265 if (!chunk_equals(sig_scheme
, sig_scheme_tbs
))
1267 free(sig_scheme_tbs
.ptr
);
1268 free(sig_scheme
.ptr
);
1271 free(sig_scheme_tbs
.ptr
);
1274 if (!signature_params_parse(sig_scheme
, 0, this->scheme
))
1276 DBG1(DBG_ASN
, "unable to parse signature algorithm");
1277 free(sig_scheme
.ptr
);
1280 free(sig_scheme
.ptr
);
1282 if (!parse_extensions(this))
1287 hasher
= lib
->crypto
->create_hasher(lib
->crypto
, HASH_SHA1
);
1288 if (!hasher
|| !hasher
->allocate_hash(hasher
, this->encoding
, &this->hash
))
1293 hasher
->destroy(hasher
);
1295 if (issued_by(this, &this->public.x509
.interface
, NULL
))
1297 this->flags
|= X509_SELF_SIGNED
;
1302 openssl_x509_t
*openssl_x509_load(certificate_type_t type
, va_list args
)
1304 chunk_t blob
= chunk_empty
;
1305 x509_flag_t flags
= 0;
1309 switch (va_arg(args
, builder_part_t
))
1311 case BUILD_BLOB_ASN1_DER
:
1312 blob
= va_arg(args
, chunk_t
);
1314 case BUILD_X509_FLAG
:
1315 flags
|= va_arg(args
, x509_flag_t
);
1327 private_openssl_x509_t
*this;
1329 this = create_empty();
1330 this->encoding
= chunk_clone(blob
);
1331 this->flags
|= flags
;
1332 if (parse_certificate(this))
1334 return &this->public;
1336 DBG1(DBG_LIB
, "OpenSSL X.509 parsing failed");