Adapt existing KEYMGMT implementations to the redesigned interface
[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_new_fn rsa_newdata;
24 static OSSL_OP_keymgmt_free_fn rsa_freedata;
25 static OSSL_OP_keymgmt_has_fn rsa_has;
26 static OSSL_OP_keymgmt_import_fn rsa_import;
27 static OSSL_OP_keymgmt_import_types_fn rsa_import_types;
28 static OSSL_OP_keymgmt_export_fn rsa_export;
29 static OSSL_OP_keymgmt_export_types_fn rsa_export_types;
30 static OSSL_OP_keymgmt_get_params_fn rsa_get_params;
31 static OSSL_OP_keymgmt_validate_fn rsa_validate;
32
33 #define RSA_DEFAULT_MD "SHA256"
34 #define RSA_POSSIBLE_SELECTIONS                 \
35     (OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS)
36
37 DEFINE_STACK_OF(BIGNUM)
38 DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
39
40 static int collect_numbers(STACK_OF(BIGNUM) *numbers,
41                            const OSSL_PARAM params[], const char *key)
42 {
43     const OSSL_PARAM *p = NULL;
44
45     if (numbers == NULL)
46         return 0;
47
48     for (p = params; (p = OSSL_PARAM_locate_const(p, key)) != NULL; p++) {
49         BIGNUM *tmp = NULL;
50
51         if (!OSSL_PARAM_get_BN(p, &tmp))
52             return 0;
53         sk_BIGNUM_push(numbers, tmp);
54     }
55
56     return 1;
57 }
58
59 static int params_to_key(RSA *rsa, const OSSL_PARAM params[])
60 {
61     const OSSL_PARAM *param_n, *param_e,  *param_d;
62     BIGNUM *n = NULL, *e = NULL, *d = NULL;
63     STACK_OF(BIGNUM) *factors = NULL, *exps = NULL, *coeffs = NULL;
64     int is_private = 0;
65
66     if (rsa == NULL)
67         return 0;
68
69     param_n = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_N);
70     param_e = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_E);
71     param_d = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_D);
72
73     if ((param_n != NULL && !OSSL_PARAM_get_BN(param_n, &n))
74         || (param_e != NULL && !OSSL_PARAM_get_BN(param_e, &e))
75         || (param_d != NULL && !OSSL_PARAM_get_BN(param_d, &d)))
76         goto err;
77
78     is_private = (d != NULL);
79
80     if (!RSA_set0_key(rsa, n, e, d))
81         goto err;
82     n = e = d = NULL;
83
84     if (is_private) {
85         if (!collect_numbers(factors = sk_BIGNUM_new_null(), params,
86                              OSSL_PKEY_PARAM_RSA_FACTOR)
87             || !collect_numbers(exps = sk_BIGNUM_new_null(), params,
88                                 OSSL_PKEY_PARAM_RSA_EXPONENT)
89             || !collect_numbers(coeffs = sk_BIGNUM_new_null(), params,
90                                 OSSL_PKEY_PARAM_RSA_COEFFICIENT))
91             goto err;
92
93         /* It's ok if this private key just has n, e and d */
94         if (sk_BIGNUM_num(factors) != 0
95             && !rsa_set0_all_params(rsa, factors, exps, coeffs))
96             goto err;
97     }
98
99     sk_BIGNUM_free(factors);
100     sk_BIGNUM_free(exps);
101     sk_BIGNUM_free(coeffs);
102     return 1;
103
104  err:
105     BN_free(n);
106     BN_free(e);
107     BN_free(d);
108     sk_BIGNUM_pop_free(factors, BN_free);
109     sk_BIGNUM_pop_free(exps, BN_free);
110     sk_BIGNUM_pop_free(coeffs, BN_free);
111     return 0;
112 }
113
114 static int export_numbers(OSSL_PARAM_BLD *tmpl, const char *key,
115                           STACK_OF(BIGNUM_const) *numbers)
116 {
117     int i, nnum;
118
119     if (numbers == NULL)
120         return 0;
121
122     nnum = sk_BIGNUM_const_num(numbers);
123
124     for (i = 0; i < nnum; i++) {
125         if (!ossl_param_bld_push_BN(tmpl, key,
126                                     sk_BIGNUM_const_value(numbers, i)))
127             return 0;
128     }
129
130     return 1;
131 }
132
133 static int key_to_params(RSA *rsa, OSSL_PARAM_BLD *tmpl)
134 {
135     int ret = 0;
136     const BIGNUM *rsa_d = NULL, *rsa_n = NULL, *rsa_e = NULL;
137     STACK_OF(BIGNUM_const) *factors = sk_BIGNUM_const_new_null();
138     STACK_OF(BIGNUM_const) *exps = sk_BIGNUM_const_new_null();
139     STACK_OF(BIGNUM_const) *coeffs = sk_BIGNUM_const_new_null();
140
141     if (rsa == NULL || factors == NULL || exps == NULL || coeffs == NULL)
142         goto err;
143
144     RSA_get0_key(rsa, &rsa_n, &rsa_e, &rsa_d);
145     rsa_get0_all_params(rsa, factors, exps, coeffs);
146
147     if (rsa_n != NULL
148         && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_RSA_N, rsa_n))
149         goto err;
150     if (rsa_e != NULL
151         && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_RSA_E, rsa_e))
152         goto err;
153     if (rsa_d != NULL
154         && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_RSA_D, rsa_d))
155         goto err;
156
157     if (!export_numbers(tmpl, OSSL_PKEY_PARAM_RSA_FACTOR, factors)
158         || !export_numbers(tmpl, OSSL_PKEY_PARAM_RSA_EXPONENT, exps)
159         || !export_numbers(tmpl, OSSL_PKEY_PARAM_RSA_COEFFICIENT, coeffs))
160         goto err;
161
162     ret = 1;
163  err:
164     sk_BIGNUM_const_free(factors);
165     sk_BIGNUM_const_free(exps);
166     sk_BIGNUM_const_free(coeffs);
167     return ret;
168 }
169
170 static void *rsa_newdata(void *provctx)
171 {
172     return RSA_new();
173 }
174
175 static void rsa_freedata(void *keydata)
176 {
177     RSA_free(keydata);
178 }
179
180 static int rsa_has(void *keydata, int selection)
181 {
182     RSA *rsa = keydata;
183     int ok = 0;
184
185     if ((selection & RSA_POSSIBLE_SELECTIONS) != 0)
186         ok = 1;
187
188     ok = ok && (RSA_get0_e(rsa) != NULL);
189     if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
190         ok = ok && (RSA_get0_n(rsa) != NULL);
191     if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
192         ok = ok && (RSA_get0_d(rsa) != NULL);
193     return ok;
194 }
195
196 static int rsa_import(void *keydata, int selection, const OSSL_PARAM params[])
197 {
198     RSA *rsa = keydata;
199     int ok = 1;
200
201     if (rsa == NULL)
202         return 0;
203
204     /* TODO(3.0) PSS and OAEP should bring on parameters */
205
206     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
207         ok = ok && params_to_key(rsa, params);
208
209     return ok;
210 }
211
212 static int rsa_export(void *keydata, int selection,
213                       OSSL_CALLBACK *param_callback, void *cbarg)
214 {
215     RSA *rsa = keydata;
216     OSSL_PARAM_BLD tmpl;
217     OSSL_PARAM *params = NULL;
218     int ok = 1;
219
220     if (rsa == NULL)
221         return 0;
222
223     /* TODO(3.0) PSS and OAEP should bring on parameters */
224
225     ossl_param_bld_init(&tmpl);
226
227     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
228         ok = ok && key_to_params(rsa, &tmpl);
229
230     if (!ok
231         || (params = ossl_param_bld_to_param(&tmpl)) == NULL)
232         return 0;
233
234     ok = param_callback(params, cbarg);
235     ossl_param_bld_free(params);
236     return ok;
237 }
238
239 /*
240  * This provider can export everything in an RSA key, so we use the exact
241  * same type description for export as for import.  Other providers might
242  * choose to import full keys, but only export the public parts, and will
243  * therefore have the importkey_types and importkey_types functions return
244  * different arrays.
245  */
246 static const OSSL_PARAM rsa_key_types[] = {
247     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_N, NULL, 0),
248     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_E, NULL, 0),
249     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_D, NULL, 0),
250     /* We tolerate up to 10 factors... */
251     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
252     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
253     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
254     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
255     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
256     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
257     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
258     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
259     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
260     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
261     /* ..., up to 10 CRT exponents... */
262     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
263     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
264     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
265     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
266     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
267     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
268     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
269     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
270     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
271     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
272     /* ..., and up to 9 CRT coefficients */
273     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
274     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
275     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
276     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
277     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
278     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
279     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
280     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
281     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
282 };
283 /*
284  * We lied about the amount of factors, exponents and coefficients, the
285  * export and import functions can really deal with an infinite amount
286  * of these numbers.  However, RSA keys with too many primes are futile,
287  * so we at least pretend to have some limits.
288  */
289
290 static const OSSL_PARAM *rsa_export_types(int selection)
291 {
292     if ((selection & OSSL_KEYMGMT_SELECT_KEY) != 0)
293         return rsa_key_types;
294     return NULL;
295 }
296
297 static const OSSL_PARAM *rsa_import_types(int selection)
298 {
299     if ((selection & OSSL_KEYMGMT_SELECT_KEY) != 0)
300         return rsa_key_types;
301     return NULL;
302 }
303
304 static int rsa_get_params(void *key, OSSL_PARAM params[])
305 {
306     RSA *rsa = key;
307     OSSL_PARAM *p;
308
309     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
310         && !OSSL_PARAM_set_int(p, RSA_bits(rsa)))
311         return 0;
312     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL
313         && !OSSL_PARAM_set_int(p, RSA_security_bits(rsa)))
314         return 0;
315     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
316         && !OSSL_PARAM_set_int(p, RSA_size(rsa)))
317         return 0;
318
319 # if 0                           /* PSS support pending */
320     if ((p = OSSL_PARAM_locate(params,
321                                OSSL_PKEY_PARAM_MANDATORY_DIGEST)) != NULL
322         && RSA_get0_pss_params(rsa) != NULL) {
323         const EVP_MD *md, *mgf1md;
324         int min_saltlen;
325
326         if (!rsa_pss_get_param(RSA_get0_pss_params(rsa),
327                                &md, &mgf1md, &min_saltlen)) {
328             ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
329             return 0;
330         }
331         if (!OSSL_PARAM_set_utf8_string(p, EVP_MD_name(md)))
332             return 0;
333     }
334 #endif
335     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_DEFAULT_DIGEST)) != NULL
336         && RSA_get0_pss_params(rsa) == NULL)
337         if (!OSSL_PARAM_set_utf8_string(p, RSA_DEFAULT_MD))
338             return 0;
339
340     return 1;
341 }
342
343 static int rsa_validate(void *keydata, int selection)
344 {
345     RSA *rsa = keydata;
346     int ok = 0;
347
348     if ((selection & RSA_POSSIBLE_SELECTIONS) != 0)
349         ok = 1;
350
351     /* If the whole key is selected, we do a pairwise validation */
352     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR)
353         == OSSL_KEYMGMT_SELECT_KEYPAIR) {
354         ok = ok && rsa_validate_pairwise(rsa);
355     } else {
356         if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
357             ok = ok && rsa_validate_private(rsa);
358         if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
359             ok = ok && rsa_validate_public(rsa);
360     }
361     return ok;
362 }
363
364 const OSSL_DISPATCH rsa_keymgmt_functions[] = {
365     { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))rsa_newdata },
366     { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))rsa_freedata },
367     { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))rsa_has },
368     { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))rsa_import },
369     { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))rsa_import_types },
370     { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))rsa_export },
371     { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))rsa_export_types },
372     { OSSL_FUNC_KEYMGMT_GET_PARAMS,  (void (*) (void))rsa_get_params },
373     { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))rsa_validate },
374     { 0, NULL }
375 };