[test] ectest: check custom generators
[openssl.git] / test / evp_libctx_test.c
1 /*
2  * Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 /*
11
12  * These tests are setup to load null into the default library context.
13  * Any tests are expected to use the created 'libctx' to find algorithms.
14  * The framework runs the tests twice using the 'default' provider or
15  * 'fips' provider as inputs.
16  */
17
18 /*
19  * DSA/DH low level APIs are deprecated for public use, but still ok for
20  * internal use.
21  */
22 #include "internal/deprecated.h"
23 #include <openssl/evp.h>
24 #include <openssl/provider.h>
25 #include <openssl/dsa.h>
26 #include "testutil.h"
27 #include "internal/nelem.h"
28 #include "crypto/bn_dh.h"        /* _bignum_ffdhe2048_p */
29
30 static OPENSSL_CTX *libctx = NULL;
31 static OSSL_PROVIDER *nullprov = NULL;
32 static OSSL_PROVIDER *libprov = NULL;
33
34 typedef enum OPTION_choice {
35     OPT_ERR = -1,
36     OPT_EOF = 0,
37     OPT_CONFIG_FILE,
38     OPT_PROVIDER_NAME,
39     OPT_TEST_ENUM
40 } OPTION_CHOICE;
41
42 const OPTIONS *test_get_options(void)
43 {
44     static const OPTIONS test_options[] = {
45         OPT_TEST_OPTIONS_DEFAULT_USAGE,
46         { "config", OPT_CONFIG_FILE, '<',
47           "The configuration file to use for the libctx" },
48         { "provider", OPT_PROVIDER_NAME, 's',
49           "The provider to load (The default value is 'default'" },
50         { NULL }
51     };
52     return test_options;
53 }
54
55 #if !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_DH)
56 static const char *getname(int id)
57 {
58     const char *name[] = {"p", "q", "g" };
59
60     if (id >= 0 && id < 3)
61         return name[id];
62     return "?";
63 }
64 #endif
65
66 #ifndef OPENSSL_NO_DSA
67
68 static int test_dsa_param_keygen(int tstid)
69 {
70     int ret = 0;
71     int expected;
72     EVP_PKEY_CTX *gen_ctx = NULL;
73     EVP_PKEY *pkey_parm = NULL;
74     EVP_PKEY *pkey = NULL;
75     DSA *dsa = NULL;
76     int pind, qind, gind;
77     BIGNUM *p = NULL, *q = NULL, *g = NULL;
78
79     /*
80      * Just grab some fixed dh p, q, g values for testing,
81      * these 'safe primes' should not be used normally for dsa *.
82      */
83     static const BIGNUM *bn[] = {
84         &_bignum_dh2048_256_p,  &_bignum_dh2048_256_q, &_bignum_dh2048_256_g
85     };
86
87     /*
88      * These tests are using bad values for p, q, g by reusing the values.
89      * A value of 0 uses p, 1 uses q and 2 uses g.
90      * There are 27 different combinations, with only the 1 valid combination.
91      */
92     pind = tstid / 9;
93     qind = (tstid / 3) % 3;
94     gind = tstid % 3;
95     expected  = (pind == 0 && qind == 1 && gind == 2);
96
97     TEST_note("Testing with (p, q, g) = (%s, %s, %s)\n", getname(pind),
98               getname(qind), getname(gind));
99
100     if (!TEST_ptr(pkey_parm = EVP_PKEY_new())
101         || !TEST_ptr(dsa = DSA_new())
102         || !TEST_ptr(p = BN_dup(bn[pind]))
103         || !TEST_ptr(q = BN_dup(bn[qind]))
104         || !TEST_ptr(g = BN_dup(bn[gind]))
105         || !TEST_true(DSA_set0_pqg(dsa, p, q, g)))
106         goto err;
107     p = q = g = NULL;
108
109     if (!TEST_true(EVP_PKEY_assign_DSA(pkey_parm, dsa)))
110         goto err;
111     dsa = NULL;
112
113     if (!TEST_ptr(gen_ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey_parm, NULL))
114         || !TEST_int_gt(EVP_PKEY_keygen_init(gen_ctx), 0)
115         || !TEST_int_eq(EVP_PKEY_keygen(gen_ctx, &pkey), expected))
116         goto err;
117     ret = 1;
118 err:
119     EVP_PKEY_free(pkey);
120     EVP_PKEY_CTX_free(gen_ctx);
121     EVP_PKEY_free(pkey_parm);
122     DSA_free(dsa);
123     BN_free(g);
124     BN_free(q);
125     BN_free(p);
126     return ret;
127 }
128 #endif /* OPENSSL_NO_DSA */
129
130 #ifndef OPENSSL_NO_DH
131 static int do_dh_param_keygen(int tstid, const BIGNUM **bn)
132 {
133     int ret = 0;
134     int expected;
135     EVP_PKEY_CTX *gen_ctx = NULL;
136     EVP_PKEY *pkey_parm = NULL;
137     EVP_PKEY *pkey = NULL;
138     DH *dh = NULL;
139     int pind, qind, gind;
140     BIGNUM *p = NULL, *q = NULL, *g = NULL;
141
142     /*
143      * These tests are using bad values for p, q, g by reusing the values.
144      * A value of 0 uses p, 1 uses q and 2 uses g.
145      * There are 27 different combinations, with only the 1 valid combination.
146      */
147     pind = tstid / 9;
148     qind = (tstid / 3) % 3;
149     gind = tstid % 3;
150     expected  = (pind == 0 && qind == 1 && gind == 2);
151
152     TEST_note("Testing with (p, q, g) = (%s, %s, %s)", getname(pind),
153               getname(qind), getname(gind));
154
155     if (!TEST_ptr(pkey_parm = EVP_PKEY_new())
156         || !TEST_ptr(dh = DH_new())
157         || !TEST_ptr(p = BN_dup(bn[pind]))
158         || !TEST_ptr(q = BN_dup(bn[qind]))
159         || !TEST_ptr(g = BN_dup(bn[gind]))
160         || !TEST_true(DH_set0_pqg(dh, p, q, g)))
161         goto err;
162     p = q = g = NULL;
163
164     if (!TEST_true(EVP_PKEY_assign_DH(pkey_parm, dh)))
165         goto err;
166     dh = NULL;
167
168     if (!TEST_ptr(gen_ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey_parm, NULL))
169         || !TEST_int_gt(EVP_PKEY_keygen_init(gen_ctx), 0)
170         || !TEST_int_eq(EVP_PKEY_keygen(gen_ctx, &pkey), expected))
171         goto err;
172     ret = 1;
173 err:
174     EVP_PKEY_free(pkey);
175     EVP_PKEY_CTX_free(gen_ctx);
176     EVP_PKEY_free(pkey_parm);
177     DH_free(dh);
178     BN_free(g);
179     BN_free(q);
180     BN_free(p);
181     return ret;
182 }
183
184 /*
185  * Note that we get the fips186-4 path being run for most of these cases since
186  * the internal code will detect that the p, q, g does not match a safe prime
187  * group (Except for when tstid = 5, which sets the correct p, q, g)
188  */
189 static int test_dh_safeprime_param_keygen(int tstid)
190 {
191     static const BIGNUM *bn[] = {
192         &_bignum_ffdhe2048_p,  &_bignum_ffdhe2048_q, &_bignum_const_2
193     };
194     return do_dh_param_keygen(tstid, bn);
195 }
196
197 #endif /* OPENSSL_NO_DH */
198
199 int setup_tests(void)
200 {
201     const char *prov_name = "default";
202     char *config_file = NULL;
203     OPTION_CHOICE o;
204
205     while ((o = opt_next()) != OPT_EOF) {
206         switch (o) {
207         case OPT_PROVIDER_NAME:
208             prov_name = opt_arg();
209             break;
210         case OPT_CONFIG_FILE:
211             config_file = opt_arg();
212             break;
213         case OPT_TEST_CASES:
214            break;
215         default:
216         case OPT_ERR:
217             return 0;
218         }
219     }
220
221     nullprov = OSSL_PROVIDER_load(NULL, "null");
222     if (!TEST_ptr(nullprov))
223         return 0;
224
225     libctx = OPENSSL_CTX_new();
226
227     if (!TEST_ptr(libctx))
228         return 0;
229
230     if (config_file != NULL) {
231         if (!TEST_true(OPENSSL_CTX_load_config(libctx, config_file)))
232             return 0;
233     }
234
235     libprov = OSSL_PROVIDER_load(libctx, prov_name);
236     if (!TEST_ptr(libprov))
237         return 0;
238
239 #ifndef OPENSSL_NO_DSA
240     ADD_ALL_TESTS(test_dsa_param_keygen, 3 * 3 * 3);
241 #endif
242 #ifndef OPENSSL_NO_DH
243     ADD_ALL_TESTS(test_dh_safeprime_param_keygen, 3 * 3 * 3);
244 #endif
245     return 1;
246 }
247
248 void cleanup_tests(void)
249 {
250     OSSL_PROVIDER_unload(libprov);
251     OPENSSL_CTX_free(libctx);
252     OSSL_PROVIDER_unload(nullprov);
253 }