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