Add answers for EVP_PKEY_get_default_digest_name() in RSA and DSA keymgmt
[openssl.git] / providers / implementations / keymgmt / rsa_kmgmt.c
1 /*
2  * Copyright 2019 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 #include <openssl/core_numbers.h>
11 #include <openssl/core_names.h>
12 #include <openssl/bn.h>
13 #include <openssl/err.h>
14 #include <openssl/rsa.h>
15 #include <openssl/evp.h>
16 #include <openssl/params.h>
17 #include <openssl/types.h>
18 #include "internal/param_build.h"
19 #include "prov/implementations.h"
20 #include "prov/providercommon.h"
21 #include "crypto/rsa.h"
22
23 static OSSL_OP_keymgmt_importkey_fn rsa_importkey;
24 static OSSL_OP_keymgmt_exportkey_fn rsa_exportkey;
25 static OSSL_OP_keymgmt_get_key_params_fn rsa_get_key_params;
26
27 #define RSA_DEFAULT_MD "SHA256"
28
29 DEFINE_STACK_OF(BIGNUM)
30 DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
31
32 static int collect_numbers(STACK_OF(BIGNUM) *numbers,
33                            const OSSL_PARAM params[], const char *key)
34 {
35     const OSSL_PARAM *p = NULL;
36
37     if (numbers == NULL)
38         return 0;
39
40     for (p = params; (p = OSSL_PARAM_locate_const(p, key)) != NULL; p++) {
41         BIGNUM *tmp = NULL;
42
43         if (!OSSL_PARAM_get_BN(p, &tmp))
44             return 0;
45         sk_BIGNUM_push(numbers, tmp);
46     }
47
48     return 1;
49 }
50
51 static int params_to_key(RSA *rsa, const OSSL_PARAM params[])
52 {
53     const OSSL_PARAM *param_n, *param_e,  *param_d;
54     BIGNUM *n = NULL, *e = NULL, *d = NULL;
55     STACK_OF(BIGNUM) *factors = NULL, *exps = NULL, *coeffs = NULL;
56     int is_private = 0;
57
58     if (rsa == NULL)
59         return 0;
60
61     param_n = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_N);
62     param_e = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_E);
63     param_d = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_D);
64
65     if ((param_n != NULL && !OSSL_PARAM_get_BN(param_n, &n))
66         || (param_e != NULL && !OSSL_PARAM_get_BN(param_e, &e))
67         || (param_d != NULL && !OSSL_PARAM_get_BN(param_d, &d)))
68         goto err;
69
70     is_private = (d != NULL);
71
72     if (!RSA_set0_key(rsa, n, e, d))
73         goto err;
74     n = e = d = NULL;
75
76     if (is_private) {
77         if (!collect_numbers(factors = sk_BIGNUM_new_null(), params,
78                              OSSL_PKEY_PARAM_RSA_FACTOR)
79             || !collect_numbers(exps = sk_BIGNUM_new_null(), params,
80                                 OSSL_PKEY_PARAM_RSA_EXPONENT)
81             || !collect_numbers(coeffs = sk_BIGNUM_new_null(), params,
82                                 OSSL_PKEY_PARAM_RSA_COEFFICIENT))
83             goto err;
84
85         /* It's ok if this private key just has n, e and d */
86         if (sk_BIGNUM_num(factors) != 0
87             && !rsa_set0_all_params(rsa, factors, exps, coeffs))
88             goto err;
89     }
90
91     sk_BIGNUM_free(factors);
92     sk_BIGNUM_free(exps);
93     sk_BIGNUM_free(coeffs);
94     return 1;
95
96  err:
97     BN_free(n);
98     BN_free(e);
99     BN_free(d);
100     sk_BIGNUM_pop_free(factors, BN_free);
101     sk_BIGNUM_pop_free(exps, BN_free);
102     sk_BIGNUM_pop_free(coeffs, BN_free);
103     return 0;
104 }
105
106 static int export_numbers(OSSL_PARAM_BLD *tmpl, const char *key,
107                           STACK_OF(BIGNUM_const) *numbers)
108 {
109     int i, nnum;
110
111     if (numbers == NULL)
112         return 0;
113
114     nnum = sk_BIGNUM_const_num(numbers);
115
116     for (i = 0; i < nnum; i++) {
117         if (!ossl_param_bld_push_BN(tmpl, key,
118                                     sk_BIGNUM_const_value(numbers, i)))
119             return 0;
120     }
121
122     return 1;
123 }
124
125 static int key_to_params(RSA *rsa, OSSL_PARAM_BLD *tmpl)
126 {
127     int ret = 0;
128     const BIGNUM *rsa_d = NULL, *rsa_n = NULL, *rsa_e = NULL;
129     STACK_OF(BIGNUM_const) *factors = sk_BIGNUM_const_new_null();
130     STACK_OF(BIGNUM_const) *exps = sk_BIGNUM_const_new_null();
131     STACK_OF(BIGNUM_const) *coeffs = sk_BIGNUM_const_new_null();
132
133     if (rsa == NULL || factors == NULL || exps == NULL || coeffs == NULL)
134         goto err;
135
136     RSA_get0_key(rsa, &rsa_n, &rsa_e, &rsa_d);
137     rsa_get0_all_params(rsa, factors, exps, coeffs);
138
139     if (rsa_n != NULL
140         && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_RSA_N, rsa_n))
141         goto err;
142     if (rsa_e != NULL
143         && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_RSA_E, rsa_e))
144         goto err;
145     if (rsa_d != NULL
146         && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_RSA_D, rsa_d))
147         goto err;
148
149     if (!export_numbers(tmpl, OSSL_PKEY_PARAM_RSA_FACTOR, factors)
150         || !export_numbers(tmpl, OSSL_PKEY_PARAM_RSA_EXPONENT, exps)
151         || !export_numbers(tmpl, OSSL_PKEY_PARAM_RSA_COEFFICIENT, coeffs))
152         goto err;
153
154     ret = 1;
155  err:
156     sk_BIGNUM_const_free(factors);
157     sk_BIGNUM_const_free(exps);
158     sk_BIGNUM_const_free(coeffs);
159     return ret;
160 }
161
162 static void *rsa_importkey(void *provctx, const OSSL_PARAM params[])
163 {
164     RSA *rsa;
165
166     if ((rsa = RSA_new()) == NULL
167         || !params_to_key(rsa, params)) {
168         RSA_free(rsa);
169         rsa = NULL;
170     }
171     return rsa;
172 }
173
174 static int rsa_exportkey(void *key, OSSL_CALLBACK *param_callback, void *cbarg)
175 {
176     RSA *rsa = key;
177     OSSL_PARAM_BLD tmpl;
178     OSSL_PARAM *params = NULL;
179     int ret;
180
181     ossl_param_bld_init(&tmpl);
182     if (rsa == NULL
183         || !key_to_params(rsa, &tmpl)
184         || (params = ossl_param_bld_to_param(&tmpl)) == NULL)
185         return 0;
186     ret = param_callback(params, cbarg);
187     ossl_param_bld_free(params);
188     return ret;
189 }
190
191 /*
192  * This provider can export everything in an RSA key, so we use the exact
193  * same type description for export as for import.  Other providers might
194  * choose to import full keys, but only export the public parts, and will
195  * therefore have the importkey_types and importkey_types functions return
196  * different arrays.
197  */
198 static const OSSL_PARAM rsa_key_types[] = {
199     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_N, NULL, 0),
200     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_E, NULL, 0),
201     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_D, NULL, 0),
202     /* We tolerate up to 10 factors... */
203     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
204     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
205     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
206     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
207     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
208     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
209     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
210     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
211     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
212     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
213     /* ..., up to 10 CRT exponents... */
214     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
215     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
216     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
217     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
218     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
219     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
220     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
221     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
222     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
223     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
224     /* ..., and up to 9 CRT coefficients */
225     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
226     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
227     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
228     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
229     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
230     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
231     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
232     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
233     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
234 };
235 /*
236  * We lied about the amount of factors, exponents and coefficients, the
237  * export and import functions can really deal with an infinite amount
238  * of these numbers.  However, RSA keys with too many primes are futile,
239  * so we at least pretend to have some limits.
240  */
241
242 static const OSSL_PARAM *rsa_exportkey_types(void)
243 {
244     return rsa_key_types;
245 }
246
247 static const OSSL_PARAM *rsa_importkey_types(void)
248 {
249     return rsa_key_types;
250 }
251
252 static int rsa_get_key_params(void *key, OSSL_PARAM params[])
253 {
254     RSA *rsa = key;
255     OSSL_PARAM *p;
256
257     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
258         && !OSSL_PARAM_set_int(p, RSA_bits(rsa)))
259         return 0;
260     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL
261         && !OSSL_PARAM_set_int(p, RSA_security_bits(rsa)))
262         return 0;
263     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
264         && !OSSL_PARAM_set_int(p, RSA_size(rsa)))
265         return 0;
266
267 # if 0                           /* PSS support pending */
268     if ((p = OSSL_PARAM_locate(params,
269                                OSSL_PKEY_PARAM_MANDATORY_DIGEST)) != NULL
270         && RSA_get0_pss_params(rsa) != NULL) {
271         const EVP_MD *md, *mgf1md;
272         int min_saltlen;
273
274         if (!rsa_pss_get_param(RSA_get0_pss_params(rsa),
275                                &md, &mgf1md, &min_saltlen)) {
276             ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
277             return 0;
278         }
279         if (!OSSL_PARAM_set_utf8_string(p, EVP_MD_name(md)))
280             return 0;
281     }
282 #endif
283     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_DEFAULT_DIGEST)) != NULL
284         && RSA_get0_pss_params(rsa) == NULL)
285         if (!OSSL_PARAM_set_utf8_string(p, RSA_DEFAULT_MD))
286             return 0;
287
288     return 1;
289 }
290
291 const OSSL_DISPATCH rsa_keymgmt_functions[] = {
292     { OSSL_FUNC_KEYMGMT_IMPORTKEY, (void (*)(void))rsa_importkey },
293     { OSSL_FUNC_KEYMGMT_IMPORTKEY_TYPES, (void (*)(void))rsa_importkey_types },
294     { OSSL_FUNC_KEYMGMT_EXPORTKEY, (void (*)(void))rsa_exportkey },
295     { OSSL_FUNC_KEYMGMT_EXPORTKEY_TYPES, (void (*)(void))rsa_exportkey_types },
296     { OSSL_FUNC_KEYMGMT_FREEKEY, (void (*)(void))RSA_free },
297     { OSSL_FUNC_KEYMGMT_GET_KEY_PARAMS,  (void (*) (void))rsa_get_key_params },
298     { 0, NULL }
299 };