2 * Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
12 #include <openssl/crypto.h>
13 #include <openssl/bio.h>
14 #include <openssl/x509.h>
15 #include <openssl/pem.h>
16 #include <openssl/err.h>
19 static const char *roots_f
;
20 static const char *untrusted_f
;
21 static const char *bad_f
;
23 static STACK_OF(X509
) *load_certs_from_file(const char *filename
)
25 STACK_OF(X509
) *certs
;
29 bio
= BIO_new_file(filename
, "r");
35 certs
= sk_X509_new_null();
43 x
= PEM_read_bio_X509(bio
, NULL
, 0, NULL
);
44 if (x
!= NULL
&& !sk_X509_push(certs
, x
)) {
45 sk_X509_pop_free(certs
, X509_free
);
48 } else if (x
== NULL
) {
50 * We probably just ran out of certs, so ignore any errors
63 * Test for CVE-2015-1793 (Alternate Chains Certificate Forgery)
65 * Chain is as follows:
67 * rootCA (self-signed)
71 * subinterCA subinterCA (self-signed)
73 * leaf ------------------
77 * rootCA, interCA, subinterCA, subinterCA (ss) all have CA=TRUE
78 * leaf and bad have CA=FALSE
80 * subinterCA and subinterCA (ss) have the same subject name and keys
82 * interCA (but not rootCA) and subinterCA (ss) are in the trusted store
84 * leaf and subinterCA are in the untrusted list (untrusted.pem)
85 * bad is the certificate being verified (bad.pem)
87 * Versions vulnerable to CVE-2015-1793 will fail to detect that leaf has
88 * CA=FALSE, and will therefore incorrectly verify bad
91 static int test_alt_chains_cert_forgery(void)
96 STACK_OF(X509
) *untrusted
= NULL
;
98 X509_STORE_CTX
*sctx
= NULL
;
99 X509_STORE
*store
= NULL
;
100 X509_LOOKUP
*lookup
= NULL
;
102 store
= X509_STORE_new();
106 lookup
= X509_STORE_add_lookup(store
, X509_LOOKUP_file());
109 if (!X509_LOOKUP_load_file(lookup
, roots_f
, X509_FILETYPE_PEM
))
112 untrusted
= load_certs_from_file(untrusted_f
);
114 if ((bio
= BIO_new_file(bad_f
, "r")) == NULL
)
117 if ((x
= PEM_read_bio_X509(bio
, NULL
, 0, NULL
)) == NULL
)
120 sctx
= X509_STORE_CTX_new();
124 if (!X509_STORE_CTX_init(sctx
, store
, x
, untrusted
))
127 i
= X509_verify_cert(sctx
);
129 if (i
== 0 && X509_STORE_CTX_get_error(sctx
) == X509_V_ERR_INVALID_CA
) {
130 /* This is the result we were expecting: Test passed */
134 X509_STORE_CTX_free(sctx
);
137 sk_X509_pop_free(untrusted
, X509_free
);
138 X509_STORE_free(store
);
142 static int test_store_ctx(void)
144 X509_STORE_CTX
*sctx
= NULL
;
147 int testresult
= 0, ret
;
149 bio
= BIO_new_file(bad_f
, "r");
153 x
= PEM_read_bio_X509(bio
, NULL
, 0, NULL
);
157 sctx
= X509_STORE_CTX_new();
161 if (!X509_STORE_CTX_init(sctx
, NULL
, x
, NULL
))
164 /* Verifying a cert where we have no trusted certs should fail */
165 ret
= X509_verify_cert(sctx
);
168 /* This is the result we were expecting: Test passed */
173 X509_STORE_CTX_free(sctx
);
179 OPT_TEST_DECLARE_USAGE("roots.pem untrusted.pem bad.pem\n")
181 #ifndef OPENSSL_NO_SM2
182 static int test_sm2_id(void)
184 /* we only need an X509 structure, no matter if it's a real SM2 cert */
188 ASN1_OCTET_STRING
*v
= NULL
, *v2
= NULL
;
189 char *sm2id
= "this is an ID";
191 bio
= BIO_new_file(bad_f
, "r");
195 x
= PEM_read_bio_X509(bio
, NULL
, 0, NULL
);
199 v
= ASN1_OCTET_STRING_new();
203 if (!ASN1_OCTET_STRING_set(v
, (unsigned char *)sm2id
, (int)strlen(sm2id
))) {
204 ASN1_OCTET_STRING_free(v
);
208 X509_set0_sm2_id(x
, v
);
210 v2
= X509_get0_sm2_id(x
);
212 || !TEST_int_eq(ASN1_OCTET_STRING_cmp(v
, v2
), 0))
223 int setup_tests(void)
225 if (!TEST_ptr(roots_f
= test_get_argument(0))
226 || !TEST_ptr(untrusted_f
= test_get_argument(1))
227 || !TEST_ptr(bad_f
= test_get_argument(2)))
230 ADD_TEST(test_alt_chains_cert_forgery
);
231 ADD_TEST(test_store_ctx
);
232 #ifndef OPENSSL_NO_SM2
233 ADD_TEST(test_sm2_id
);