1*66bae5e7Schristos /*
2*66bae5e7Schristos  * Copyright 2021-2022 The OpenSSL Project Authors. All Rights Reserved.
3*66bae5e7Schristos  *
4*66bae5e7Schristos  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*66bae5e7Schristos  * this file except in compliance with the License.  You can obtain a copy
6*66bae5e7Schristos  * in the file LICENSE in the source distribution or at
7*66bae5e7Schristos  * https://www.openssl.org/source/license.html
8*66bae5e7Schristos  */
9*66bae5e7Schristos 
10*66bae5e7Schristos #include <stddef.h>
11*66bae5e7Schristos #include <string.h>
12*66bae5e7Schristos #include <openssl/provider.h>
13*66bae5e7Schristos #include <openssl/params.h>
14*66bae5e7Schristos #include <openssl/core_names.h>
15*66bae5e7Schristos #include <openssl/evp.h>
16*66bae5e7Schristos #include <openssl/store.h>
17*66bae5e7Schristos #include "testutil.h"
18*66bae5e7Schristos #include "fake_rsaprov.h"
19*66bae5e7Schristos 
20*66bae5e7Schristos static OSSL_LIB_CTX *libctx = NULL;
21*66bae5e7Schristos 
22*66bae5e7Schristos /* Fetch SIGNATURE method using a libctx and propq */
fetch_sig(OSSL_LIB_CTX * ctx,const char * alg,const char * propq,OSSL_PROVIDER * expected_prov)23*66bae5e7Schristos static int fetch_sig(OSSL_LIB_CTX *ctx, const char *alg, const char *propq,
24*66bae5e7Schristos                      OSSL_PROVIDER *expected_prov)
25*66bae5e7Schristos {
26*66bae5e7Schristos     OSSL_PROVIDER *prov;
27*66bae5e7Schristos     EVP_SIGNATURE *sig = EVP_SIGNATURE_fetch(ctx, "RSA", propq);
28*66bae5e7Schristos     int ret = 0;
29*66bae5e7Schristos 
30*66bae5e7Schristos     if (!TEST_ptr(sig))
31*66bae5e7Schristos         return 0;
32*66bae5e7Schristos 
33*66bae5e7Schristos     if (!TEST_ptr(prov = EVP_SIGNATURE_get0_provider(sig)))
34*66bae5e7Schristos         goto end;
35*66bae5e7Schristos 
36*66bae5e7Schristos     if (!TEST_ptr_eq(prov, expected_prov)) {
37*66bae5e7Schristos         TEST_info("Fetched provider: %s, Expected provider: %s",
38*66bae5e7Schristos                   OSSL_PROVIDER_get0_name(prov),
39*66bae5e7Schristos                   OSSL_PROVIDER_get0_name(expected_prov));
40*66bae5e7Schristos         goto end;
41*66bae5e7Schristos     }
42*66bae5e7Schristos 
43*66bae5e7Schristos     ret = 1;
44*66bae5e7Schristos end:
45*66bae5e7Schristos     EVP_SIGNATURE_free(sig);
46*66bae5e7Schristos     return ret;
47*66bae5e7Schristos }
48*66bae5e7Schristos 
49*66bae5e7Schristos 
test_pkey_sig(void)50*66bae5e7Schristos static int test_pkey_sig(void)
51*66bae5e7Schristos {
52*66bae5e7Schristos     OSSL_PROVIDER *deflt = NULL;
53*66bae5e7Schristos     OSSL_PROVIDER *fake_rsa = NULL;
54*66bae5e7Schristos     int i, ret = 0;
55*66bae5e7Schristos     EVP_PKEY *pkey = NULL;
56*66bae5e7Schristos     EVP_PKEY_CTX *ctx = NULL;
57*66bae5e7Schristos 
58*66bae5e7Schristos     if (!TEST_ptr(fake_rsa = fake_rsa_start(libctx)))
59*66bae5e7Schristos         return 0;
60*66bae5e7Schristos 
61*66bae5e7Schristos     if (!TEST_ptr(deflt = OSSL_PROVIDER_load(libctx, "default")))
62*66bae5e7Schristos         goto end;
63*66bae5e7Schristos 
64*66bae5e7Schristos     /* Do a direct fetch to see it works */
65*66bae5e7Schristos     if (!TEST_true(fetch_sig(libctx, "RSA", "provider=fake-rsa", fake_rsa))
66*66bae5e7Schristos         || !TEST_true(fetch_sig(libctx, "RSA", "?provider=fake-rsa", fake_rsa)))
67*66bae5e7Schristos         goto end;
68*66bae5e7Schristos 
69*66bae5e7Schristos     /* Construct a pkey using precise propq to use our provider */
70*66bae5e7Schristos     if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(libctx, "RSA",
71*66bae5e7Schristos                                                    "provider=fake-rsa"))
72*66bae5e7Schristos         || !TEST_true(EVP_PKEY_fromdata_init(ctx))
73*66bae5e7Schristos         || !TEST_true(EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEYPAIR, NULL))
74*66bae5e7Schristos         || !TEST_ptr(pkey))
75*66bae5e7Schristos         goto end;
76*66bae5e7Schristos 
77*66bae5e7Schristos     EVP_PKEY_CTX_free(ctx);
78*66bae5e7Schristos     ctx = NULL;
79*66bae5e7Schristos 
80*66bae5e7Schristos     /* try exercising signature_init ops a few times */
81*66bae5e7Schristos     for (i = 0; i < 3; i++) {
82*66bae5e7Schristos         size_t siglen;
83*66bae5e7Schristos 
84*66bae5e7Schristos         /*
85*66bae5e7Schristos          * Create a signing context for our pkey with optional propq.
86*66bae5e7Schristos          * The sign init should pick both keymgmt and signature from
87*66bae5e7Schristos          * fake-rsa as the key is not exportable.
88*66bae5e7Schristos          */
89*66bae5e7Schristos         if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey,
90*66bae5e7Schristos                                                        "?provider=default")))
91*66bae5e7Schristos             goto end;
92*66bae5e7Schristos 
93*66bae5e7Schristos         /*
94*66bae5e7Schristos          * If this picks the wrong signature without realizing it
95*66bae5e7Schristos          * we can get a segfault or some internal error. At least watch
96*66bae5e7Schristos          * whether fake-rsa sign_init is is exercised by calling sign.
97*66bae5e7Schristos          */
98*66bae5e7Schristos         if (!TEST_int_eq(EVP_PKEY_sign_init(ctx), 1))
99*66bae5e7Schristos             goto end;
100*66bae5e7Schristos 
101*66bae5e7Schristos         if (!TEST_int_eq(EVP_PKEY_sign(ctx, NULL, &siglen, NULL, 0), 1)
102*66bae5e7Schristos             || !TEST_size_t_eq(siglen, 256))
103*66bae5e7Schristos             goto end;
104*66bae5e7Schristos 
105*66bae5e7Schristos         EVP_PKEY_CTX_free(ctx);
106*66bae5e7Schristos         ctx = NULL;
107*66bae5e7Schristos     }
108*66bae5e7Schristos 
109*66bae5e7Schristos     ret = 1;
110*66bae5e7Schristos 
111*66bae5e7Schristos end:
112*66bae5e7Schristos     fake_rsa_finish(fake_rsa);
113*66bae5e7Schristos     OSSL_PROVIDER_unload(deflt);
114*66bae5e7Schristos     EVP_PKEY_CTX_free(ctx);
115*66bae5e7Schristos     EVP_PKEY_free(pkey);
116*66bae5e7Schristos     return ret;
117*66bae5e7Schristos }
118*66bae5e7Schristos 
test_alternative_keygen_init(void)119*66bae5e7Schristos static int test_alternative_keygen_init(void)
120*66bae5e7Schristos {
121*66bae5e7Schristos     EVP_PKEY_CTX *ctx = NULL;
122*66bae5e7Schristos     OSSL_PROVIDER *deflt = NULL;
123*66bae5e7Schristos     OSSL_PROVIDER *fake_rsa = NULL;
124*66bae5e7Schristos     const OSSL_PROVIDER *provider;
125*66bae5e7Schristos     const char *provname;
126*66bae5e7Schristos     int ret = 0;
127*66bae5e7Schristos 
128*66bae5e7Schristos     if (!TEST_ptr(deflt = OSSL_PROVIDER_load(libctx, "default")))
129*66bae5e7Schristos         goto end;
130*66bae5e7Schristos 
131*66bae5e7Schristos     /* first try without the fake RSA provider loaded */
132*66bae5e7Schristos     if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(libctx, "RSA", NULL)))
133*66bae5e7Schristos         goto end;
134*66bae5e7Schristos 
135*66bae5e7Schristos     if (!TEST_int_gt(EVP_PKEY_keygen_init(ctx), 0))
136*66bae5e7Schristos         goto end;
137*66bae5e7Schristos 
138*66bae5e7Schristos     if (!TEST_ptr(provider = EVP_PKEY_CTX_get0_provider(ctx)))
139*66bae5e7Schristos         goto end;
140*66bae5e7Schristos 
141*66bae5e7Schristos     if (!TEST_ptr(provname = OSSL_PROVIDER_get0_name(provider)))
142*66bae5e7Schristos         goto end;
143*66bae5e7Schristos 
144*66bae5e7Schristos     if (!TEST_str_eq(provname, "default"))
145*66bae5e7Schristos         goto end;
146*66bae5e7Schristos 
147*66bae5e7Schristos     EVP_PKEY_CTX_free(ctx);
148*66bae5e7Schristos     ctx = NULL;
149*66bae5e7Schristos 
150*66bae5e7Schristos     /* now load fake RSA and try again */
151*66bae5e7Schristos     if (!TEST_ptr(fake_rsa = fake_rsa_start(libctx)))
152*66bae5e7Schristos         return 0;
153*66bae5e7Schristos 
154*66bae5e7Schristos     if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(libctx, "RSA",
155*66bae5e7Schristos                                                    "?provider=fake-rsa")))
156*66bae5e7Schristos         goto end;
157*66bae5e7Schristos 
158*66bae5e7Schristos     if (!TEST_int_gt(EVP_PKEY_keygen_init(ctx), 0))
159*66bae5e7Schristos         goto end;
160*66bae5e7Schristos 
161*66bae5e7Schristos     if (!TEST_ptr(provider = EVP_PKEY_CTX_get0_provider(ctx)))
162*66bae5e7Schristos         goto end;
163*66bae5e7Schristos 
164*66bae5e7Schristos     if (!TEST_ptr(provname = OSSL_PROVIDER_get0_name(provider)))
165*66bae5e7Schristos         goto end;
166*66bae5e7Schristos 
167*66bae5e7Schristos     if (!TEST_str_eq(provname, "fake-rsa"))
168*66bae5e7Schristos         goto end;
169*66bae5e7Schristos 
170*66bae5e7Schristos     ret = 1;
171*66bae5e7Schristos 
172*66bae5e7Schristos end:
173*66bae5e7Schristos     fake_rsa_finish(fake_rsa);
174*66bae5e7Schristos     OSSL_PROVIDER_unload(deflt);
175*66bae5e7Schristos     EVP_PKEY_CTX_free(ctx);
176*66bae5e7Schristos     return ret;
177*66bae5e7Schristos }
178*66bae5e7Schristos 
test_pkey_eq(void)179*66bae5e7Schristos static int test_pkey_eq(void)
180*66bae5e7Schristos {
181*66bae5e7Schristos     OSSL_PROVIDER *deflt = NULL;
182*66bae5e7Schristos     OSSL_PROVIDER *fake_rsa = NULL;
183*66bae5e7Schristos     EVP_PKEY *pkey_fake = NULL;
184*66bae5e7Schristos     EVP_PKEY *pkey_dflt = NULL;
185*66bae5e7Schristos     EVP_PKEY_CTX *ctx = NULL;
186*66bae5e7Schristos     OSSL_PARAM *params = NULL;
187*66bae5e7Schristos     int ret = 0;
188*66bae5e7Schristos 
189*66bae5e7Schristos     if (!TEST_ptr(fake_rsa = fake_rsa_start(libctx)))
190*66bae5e7Schristos         return 0;
191*66bae5e7Schristos 
192*66bae5e7Schristos     if (!TEST_ptr(deflt = OSSL_PROVIDER_load(libctx, "default")))
193*66bae5e7Schristos         goto end;
194*66bae5e7Schristos 
195*66bae5e7Schristos     /* Construct a public key for fake-rsa */
196*66bae5e7Schristos     if (!TEST_ptr(params = fake_rsa_key_params(0))
197*66bae5e7Schristos         || !TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(libctx, "RSA",
198*66bae5e7Schristos                                                       "provider=fake-rsa"))
199*66bae5e7Schristos         || !TEST_true(EVP_PKEY_fromdata_init(ctx))
200*66bae5e7Schristos         || !TEST_true(EVP_PKEY_fromdata(ctx, &pkey_fake, EVP_PKEY_PUBLIC_KEY,
201*66bae5e7Schristos                                         params))
202*66bae5e7Schristos         || !TEST_ptr(pkey_fake))
203*66bae5e7Schristos         goto end;
204*66bae5e7Schristos 
205*66bae5e7Schristos     EVP_PKEY_CTX_free(ctx);
206*66bae5e7Schristos     ctx = NULL;
207*66bae5e7Schristos     OSSL_PARAM_free(params);
208*66bae5e7Schristos     params = NULL;
209*66bae5e7Schristos 
210*66bae5e7Schristos     /* Construct a public key for default */
211*66bae5e7Schristos     if (!TEST_ptr(params = fake_rsa_key_params(0))
212*66bae5e7Schristos         || !TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(libctx, "RSA",
213*66bae5e7Schristos                                                       "provider=default"))
214*66bae5e7Schristos         || !TEST_true(EVP_PKEY_fromdata_init(ctx))
215*66bae5e7Schristos         || !TEST_true(EVP_PKEY_fromdata(ctx, &pkey_dflt, EVP_PKEY_PUBLIC_KEY,
216*66bae5e7Schristos                                         params))
217*66bae5e7Schristos         || !TEST_ptr(pkey_dflt))
218*66bae5e7Schristos         goto end;
219*66bae5e7Schristos 
220*66bae5e7Schristos     EVP_PKEY_CTX_free(ctx);
221*66bae5e7Schristos     ctx = NULL;
222*66bae5e7Schristos     OSSL_PARAM_free(params);
223*66bae5e7Schristos     params = NULL;
224*66bae5e7Schristos 
225*66bae5e7Schristos     /* now test for equality */
226*66bae5e7Schristos     if (!TEST_int_eq(EVP_PKEY_eq(pkey_fake, pkey_dflt), 1))
227*66bae5e7Schristos         goto end;
228*66bae5e7Schristos 
229*66bae5e7Schristos     ret = 1;
230*66bae5e7Schristos end:
231*66bae5e7Schristos     fake_rsa_finish(fake_rsa);
232*66bae5e7Schristos     OSSL_PROVIDER_unload(deflt);
233*66bae5e7Schristos     EVP_PKEY_CTX_free(ctx);
234*66bae5e7Schristos     EVP_PKEY_free(pkey_fake);
235*66bae5e7Schristos     EVP_PKEY_free(pkey_dflt);
236*66bae5e7Schristos     OSSL_PARAM_free(params);
237*66bae5e7Schristos     return ret;
238*66bae5e7Schristos }
239*66bae5e7Schristos 
test_pkey_store(int idx)240*66bae5e7Schristos static int test_pkey_store(int idx)
241*66bae5e7Schristos {
242*66bae5e7Schristos     OSSL_PROVIDER *deflt = NULL;
243*66bae5e7Schristos     OSSL_PROVIDER *fake_rsa = NULL;
244*66bae5e7Schristos     int ret = 0;
245*66bae5e7Schristos     EVP_PKEY *pkey = NULL;
246*66bae5e7Schristos     OSSL_STORE_LOADER *loader = NULL;
247*66bae5e7Schristos     OSSL_STORE_CTX *ctx = NULL;
248*66bae5e7Schristos     OSSL_STORE_INFO *info;
249*66bae5e7Schristos     const char *propq = idx == 0 ? "?provider=fake-rsa"
250*66bae5e7Schristos                                  : "?provider=default";
251*66bae5e7Schristos 
252*66bae5e7Schristos     /* It's important to load the default provider first for this test */
253*66bae5e7Schristos     if (!TEST_ptr(deflt = OSSL_PROVIDER_load(libctx, "default")))
254*66bae5e7Schristos         goto end;
255*66bae5e7Schristos 
256*66bae5e7Schristos     if (!TEST_ptr(fake_rsa = fake_rsa_start(libctx)))
257*66bae5e7Schristos         goto end;
258*66bae5e7Schristos 
259*66bae5e7Schristos     if (!TEST_ptr(loader = OSSL_STORE_LOADER_fetch(libctx, "fake_rsa",
260*66bae5e7Schristos                                                    propq)))
261*66bae5e7Schristos         goto end;
262*66bae5e7Schristos 
263*66bae5e7Schristos     OSSL_STORE_LOADER_free(loader);
264*66bae5e7Schristos 
265*66bae5e7Schristos     if (!TEST_ptr(ctx = OSSL_STORE_open_ex("fake_rsa:test", libctx, propq,
266*66bae5e7Schristos                                            NULL, NULL, NULL, NULL, NULL)))
267*66bae5e7Schristos         goto end;
268*66bae5e7Schristos 
269*66bae5e7Schristos     while (!OSSL_STORE_eof(ctx)
270*66bae5e7Schristos            && (info = OSSL_STORE_load(ctx)) != NULL
271*66bae5e7Schristos            && pkey == NULL) {
272*66bae5e7Schristos         if (OSSL_STORE_INFO_get_type(info) == OSSL_STORE_INFO_PKEY)
273*66bae5e7Schristos             pkey = OSSL_STORE_INFO_get1_PKEY(info);
274*66bae5e7Schristos         OSSL_STORE_INFO_free(info);
275*66bae5e7Schristos         info = NULL;
276*66bae5e7Schristos     }
277*66bae5e7Schristos 
278*66bae5e7Schristos     if (!TEST_ptr(pkey) || !TEST_int_eq(EVP_PKEY_is_a(pkey, "RSA"), 1))
279*66bae5e7Schristos         goto end;
280*66bae5e7Schristos 
281*66bae5e7Schristos     ret = 1;
282*66bae5e7Schristos 
283*66bae5e7Schristos end:
284*66bae5e7Schristos     fake_rsa_finish(fake_rsa);
285*66bae5e7Schristos     OSSL_PROVIDER_unload(deflt);
286*66bae5e7Schristos     OSSL_STORE_close(ctx);
287*66bae5e7Schristos     EVP_PKEY_free(pkey);
288*66bae5e7Schristos     return ret;
289*66bae5e7Schristos }
290*66bae5e7Schristos 
setup_tests(void)291*66bae5e7Schristos int setup_tests(void)
292*66bae5e7Schristos {
293*66bae5e7Schristos     libctx = OSSL_LIB_CTX_new();
294*66bae5e7Schristos     if (libctx == NULL)
295*66bae5e7Schristos         return 0;
296*66bae5e7Schristos 
297*66bae5e7Schristos     ADD_TEST(test_pkey_sig);
298*66bae5e7Schristos     ADD_TEST(test_alternative_keygen_init);
299*66bae5e7Schristos     ADD_TEST(test_pkey_eq);
300*66bae5e7Schristos     ADD_ALL_TESTS(test_pkey_store, 2);
301*66bae5e7Schristos 
302*66bae5e7Schristos     return 1;
303*66bae5e7Schristos }
304*66bae5e7Schristos 
cleanup_tests(void)305*66bae5e7Schristos void cleanup_tests(void)
306*66bae5e7Schristos {
307*66bae5e7Schristos     OSSL_LIB_CTX_free(libctx);
308*66bae5e7Schristos }
309