d7331c84b09864f97f9f8aee7c8c5902e58e77fe
[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 /*
11  * RSA low level APIs are deprecated for public use, but still ok for
12  * internal use.
13  */
14 #include "internal/deprecated.h"
15
16 #include <openssl/core_numbers.h>
17 #include <openssl/core_names.h>
18 #include <openssl/bn.h>
19 #include <openssl/err.h>
20 #include <openssl/rsa.h>
21 #include <openssl/evp.h>
22 #include <openssl/params.h>
23 #include <openssl/types.h>
24 #include "openssl/param_build.h"
25 #include "prov/implementations.h"
26 #include "prov/providercommon.h"
27 #include "prov/provider_ctx.h"
28 #include "crypto/rsa.h"
29
30 static OSSL_OP_keymgmt_new_fn rsa_newdata;
31 static OSSL_OP_keymgmt_gen_init_fn rsa_gen_init;
32 static OSSL_OP_keymgmt_gen_set_params_fn rsa_gen_set_params;
33 static OSSL_OP_keymgmt_gen_settable_params_fn rsa_gen_settable_params;
34 static OSSL_OP_keymgmt_gen_fn rsa_gen;
35 static OSSL_OP_keymgmt_gen_cleanup_fn rsa_gen_cleanup;
36 static OSSL_OP_keymgmt_free_fn rsa_freedata;
37 static OSSL_OP_keymgmt_get_params_fn rsa_get_params;
38 static OSSL_OP_keymgmt_gettable_params_fn rsa_gettable_params;
39 static OSSL_OP_keymgmt_has_fn rsa_has;
40 static OSSL_OP_keymgmt_match_fn rsa_match;
41 static OSSL_OP_keymgmt_validate_fn rsa_validate;
42 static OSSL_OP_keymgmt_import_fn rsa_import;
43 static OSSL_OP_keymgmt_import_types_fn rsa_import_types;
44 static OSSL_OP_keymgmt_export_fn rsa_export;
45 static OSSL_OP_keymgmt_export_types_fn rsa_export_types;
46
47 #define RSA_DEFAULT_MD "SHA256"
48 #define RSA_POSSIBLE_SELECTIONS                 \
49     (OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS)
50
51 DEFINE_STACK_OF(BIGNUM)
52 DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
53
54 static int export_numbers(OSSL_PARAM_BLD *tmpl, const char *key,
55                           STACK_OF(BIGNUM_const) *numbers)
56 {
57     int i, nnum;
58
59     if (numbers == NULL)
60         return 0;
61
62     nnum = sk_BIGNUM_const_num(numbers);
63
64     for (i = 0; i < nnum; i++) {
65         if (!OSSL_PARAM_BLD_push_BN(tmpl, key,
66                                     sk_BIGNUM_const_value(numbers, i)))
67             return 0;
68     }
69
70     return 1;
71 }
72
73 static int key_to_params(RSA *rsa, OSSL_PARAM_BLD *tmpl)
74 {
75     int ret = 0;
76     const BIGNUM *rsa_d = NULL, *rsa_n = NULL, *rsa_e = NULL;
77     STACK_OF(BIGNUM_const) *factors = sk_BIGNUM_const_new_null();
78     STACK_OF(BIGNUM_const) *exps = sk_BIGNUM_const_new_null();
79     STACK_OF(BIGNUM_const) *coeffs = sk_BIGNUM_const_new_null();
80
81     if (rsa == NULL || factors == NULL || exps == NULL || coeffs == NULL)
82         goto err;
83
84     RSA_get0_key(rsa, &rsa_n, &rsa_e, &rsa_d);
85     rsa_get0_all_params(rsa, factors, exps, coeffs);
86
87     if (rsa_n != NULL
88         && !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_RSA_N, rsa_n))
89         goto err;
90     if (rsa_e != NULL
91         && !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_RSA_E, rsa_e))
92         goto err;
93     if (rsa_d != NULL
94         && !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_RSA_D, rsa_d))
95         goto err;
96
97     if (!export_numbers(tmpl, OSSL_PKEY_PARAM_RSA_FACTOR, factors)
98         || !export_numbers(tmpl, OSSL_PKEY_PARAM_RSA_EXPONENT, exps)
99         || !export_numbers(tmpl, OSSL_PKEY_PARAM_RSA_COEFFICIENT, coeffs))
100         goto err;
101
102     ret = 1;
103  err:
104     sk_BIGNUM_const_free(factors);
105     sk_BIGNUM_const_free(exps);
106     sk_BIGNUM_const_free(coeffs);
107     return ret;
108 }
109
110 static void *rsa_newdata(void *provctx)
111 {
112     OPENSSL_CTX *libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
113
114     return rsa_new_with_ctx(libctx);
115 }
116
117 static void rsa_freedata(void *keydata)
118 {
119     RSA_free(keydata);
120 }
121
122 static int rsa_has(void *keydata, int selection)
123 {
124     RSA *rsa = keydata;
125     int ok = 0;
126
127     if (rsa != NULL) {
128         if ((selection & RSA_POSSIBLE_SELECTIONS) != 0)
129             ok = 1;
130
131         if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
132             ok = ok && 0;     /* This will change with PSS and OAEP */
133         if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
134             ok = ok && (RSA_get0_e(rsa) != NULL);
135         if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
136             ok = ok && (RSA_get0_n(rsa) != NULL);
137         if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
138             ok = ok && (RSA_get0_d(rsa) != NULL);
139     }
140     return ok;
141 }
142
143 static int rsa_match(const void *keydata1, const void *keydata2, int selection)
144 {
145     const RSA *rsa1 = keydata1;
146     const RSA *rsa2 = keydata2;
147     int ok = 1;
148
149     /* There is always an |e| */
150     ok = ok && BN_cmp(RSA_get0_e(rsa1), RSA_get0_e(rsa2)) == 0;
151     if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
152         ok = ok && BN_cmp(RSA_get0_n(rsa1), RSA_get0_n(rsa2)) == 0;
153     if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
154         ok = ok && BN_cmp(RSA_get0_d(rsa1), RSA_get0_d(rsa2)) == 0;
155     return ok;
156 }
157
158 static int rsa_import(void *keydata, int selection, const OSSL_PARAM params[])
159 {
160     RSA *rsa = keydata;
161     int ok = 1;
162
163     if (rsa == NULL)
164         return 0;
165
166     /* TODO(3.0) PSS and OAEP should bring on parameters */
167
168     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
169         ok = ok && rsa_fromdata(rsa, params);
170
171     return ok;
172 }
173
174 static int rsa_export(void *keydata, int selection,
175                       OSSL_CALLBACK *param_callback, void *cbarg)
176 {
177     RSA *rsa = keydata;
178     OSSL_PARAM_BLD tmpl;
179     OSSL_PARAM *params = NULL;
180     int ok = 1;
181
182     if (rsa == NULL)
183         return 0;
184
185     /* TODO(3.0) PSS and OAEP should bring on parameters */
186
187     OSSL_PARAM_BLD_init(&tmpl);
188
189     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
190         ok = ok && key_to_params(rsa, &tmpl);
191
192     if (!ok
193         || (params = OSSL_PARAM_BLD_to_param(&tmpl)) == NULL)
194         return 0;
195
196     ok = param_callback(params, cbarg);
197     OSSL_PARAM_BLD_free(params);
198     return ok;
199 }
200
201 /*
202  * This provider can export everything in an RSA key, so we use the exact
203  * same type description for export as for import.  Other providers might
204  * choose to import full keys, but only export the public parts, and will
205  * therefore have the importkey_types and importkey_types functions return
206  * different arrays.
207  */
208 static const OSSL_PARAM rsa_key_types[] = {
209     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_N, NULL, 0),
210     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_E, NULL, 0),
211     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_D, NULL, 0),
212     /* We tolerate up to 10 factors... */
213     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
214     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
215     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
216     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
217     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
218     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
219     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
220     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
221     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
222     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
223     /* ..., up to 10 CRT exponents... */
224     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
225     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
226     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
227     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
228     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
229     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
230     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
231     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
232     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
233     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
234     /* ..., and up to 9 CRT coefficients */
235     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
236     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
237     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
238     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
239     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
240     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
241     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
242     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
243     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
244 };
245 /*
246  * We lied about the amount of factors, exponents and coefficients, the
247  * export and import functions can really deal with an infinite amount
248  * of these numbers.  However, RSA keys with too many primes are futile,
249  * so we at least pretend to have some limits.
250  */
251
252 static const OSSL_PARAM *rsa_imexport_types(int selection)
253 {
254     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
255         return rsa_key_types;
256     return NULL;
257 }
258
259 static const OSSL_PARAM *rsa_import_types(int selection)
260 {
261     return rsa_imexport_types(selection);
262 }
263
264
265 static const OSSL_PARAM *rsa_export_types(int selection)
266 {
267     return rsa_imexport_types(selection);
268 }
269
270 static int rsa_get_params(void *key, OSSL_PARAM params[])
271 {
272     RSA *rsa = key;
273     OSSL_PARAM *p;
274
275     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
276         && !OSSL_PARAM_set_int(p, RSA_bits(rsa)))
277         return 0;
278     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL
279         && !OSSL_PARAM_set_int(p, RSA_security_bits(rsa)))
280         return 0;
281     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
282         && !OSSL_PARAM_set_int(p, RSA_size(rsa)))
283         return 0;
284
285 # if 0  /* TODO(3.0): PSS support pending */
286     if ((p = OSSL_PARAM_locate(params,
287                                OSSL_PKEY_PARAM_MANDATORY_DIGEST)) != NULL
288         && RSA_get0_pss_params(rsa) != NULL) {
289         const EVP_MD *md, *mgf1md;
290         int min_saltlen;
291
292         if (!rsa_pss_get_param(RSA_get0_pss_params(rsa),
293                                &md, &mgf1md, &min_saltlen)) {
294             ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
295             return 0;
296         }
297         if (!OSSL_PARAM_set_utf8_string(p, EVP_MD_name(md)))
298             return 0;
299     }
300 #endif
301     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_DEFAULT_DIGEST)) != NULL
302 /* TODO(3.0): PSS support pending */
303 #if 0
304             && RSA_get0_pss_params(rsa) == NULL
305 #endif
306             ) {
307         if (!OSSL_PARAM_set_utf8_string(p, RSA_DEFAULT_MD))
308             return 0;
309     }
310
311     return 1;
312 }
313
314 static const OSSL_PARAM rsa_params[] = {
315     OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
316     OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
317     OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
318     OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST, NULL, 0),
319     OSSL_PARAM_END
320 };
321
322 static const OSSL_PARAM *rsa_gettable_params(void)
323 {
324     return rsa_params;
325 }
326
327 static int rsa_validate(void *keydata, int selection)
328 {
329     RSA *rsa = keydata;
330     int ok = 0;
331
332     if ((selection & RSA_POSSIBLE_SELECTIONS) != 0)
333         ok = 1;
334
335     /* If the whole key is selected, we do a pairwise validation */
336     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR)
337         == OSSL_KEYMGMT_SELECT_KEYPAIR) {
338         ok = ok && rsa_validate_pairwise(rsa);
339     } else {
340         if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
341             ok = ok && rsa_validate_private(rsa);
342         if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
343             ok = ok && rsa_validate_public(rsa);
344     }
345     return ok;
346 }
347
348 struct rsa_gen_ctx {
349     OPENSSL_CTX *libctx;
350
351     size_t nbits;
352     BIGNUM *pub_exp;
353     size_t primes;
354
355     /* For generation callback */
356     OSSL_CALLBACK *cb;
357     void *cbarg;
358 };
359
360 static int rsa_gencb(int p, int n, BN_GENCB *cb)
361 {
362     struct rsa_gen_ctx *gctx = BN_GENCB_get_arg(cb);
363     OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END };
364
365     params[0] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_POTENTIAL, &p);
366     params[1] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_ITERATION, &n);
367
368     return gctx->cb(params, gctx->cbarg);
369 }
370
371 static void *rsa_gen_init(void *provctx, int selection)
372 {
373     OPENSSL_CTX *libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
374     struct rsa_gen_ctx *gctx = NULL;
375
376     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
377         return NULL;
378
379     if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL) {
380         gctx->libctx = libctx;
381         if ((gctx->pub_exp = BN_new()) == NULL
382             || !BN_set_word(gctx->pub_exp, RSA_F4)) {
383             BN_free(gctx->pub_exp);
384             gctx = NULL;
385         } else {
386             gctx->nbits = 2048;
387             gctx->primes = RSA_DEFAULT_PRIME_NUM;
388         }
389     }
390     return gctx;
391 }
392
393 static int rsa_gen_set_params(void *genctx, const OSSL_PARAM params[])
394 {
395     struct rsa_gen_ctx *gctx = genctx;
396     const OSSL_PARAM *p;
397
398     if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_BITS)) != NULL
399         && !OSSL_PARAM_get_size_t(p, &gctx->nbits))
400         return 0;
401     if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_PRIMES)) != NULL
402         && !OSSL_PARAM_get_size_t(p, &gctx->primes))
403         return 0;
404     if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_E)) != NULL
405         && !OSSL_PARAM_get_BN(p, &gctx->pub_exp))
406         return 0;
407     return 1;
408 }
409
410 static const OSSL_PARAM *rsa_gen_settable_params(void *provctx)
411 {
412     static OSSL_PARAM settable[] = {
413         OSSL_PARAM_size_t(OSSL_PKEY_PARAM_RSA_BITS, NULL),
414         OSSL_PARAM_size_t(OSSL_PKEY_PARAM_RSA_PRIMES, NULL),
415         OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_E, NULL, 0),
416         OSSL_PARAM_END
417     };
418
419     return settable;
420 }
421
422 static void *rsa_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
423 {
424     struct rsa_gen_ctx *gctx = genctx;
425     RSA *rsa = NULL;
426     BN_GENCB *gencb = NULL;
427
428     if (gctx == NULL
429         || (rsa = rsa_new_with_ctx(gctx->libctx)) == NULL)
430         return NULL;
431
432     gctx->cb = osslcb;
433     gctx->cbarg = cbarg;
434     gencb = BN_GENCB_new();
435     if (gencb != NULL)
436         BN_GENCB_set(gencb, rsa_gencb, genctx);
437
438     if (!RSA_generate_multi_prime_key(rsa, (int)gctx->nbits, (int)gctx->primes,
439                                       gctx->pub_exp, gencb)) {
440         RSA_free(rsa);
441         rsa = NULL;
442     }
443
444     BN_GENCB_free(gencb);
445
446     return rsa;
447 }
448
449 static void rsa_gen_cleanup(void *genctx)
450 {
451     struct rsa_gen_ctx *gctx = genctx;
452
453     if (gctx == NULL)
454         return;
455
456     BN_clear_free(gctx->pub_exp);
457     OPENSSL_free(gctx);
458 }
459
460 const OSSL_DISPATCH rsa_keymgmt_functions[] = {
461     { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))rsa_newdata },
462     { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))rsa_gen_init },
463     { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS,
464       (void (*)(void))rsa_gen_set_params },
465     { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
466       (void (*)(void))rsa_gen_settable_params },
467     { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))rsa_gen },
468     { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))rsa_gen_cleanup },
469     { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))rsa_freedata },
470     { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))rsa_get_params },
471     { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))rsa_gettable_params },
472     { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))rsa_has },
473     { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))rsa_match },
474     { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))rsa_validate },
475     { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))rsa_import },
476     { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))rsa_import_types },
477     { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))rsa_export },
478     { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))rsa_export_types },
479     { 0, NULL }
480 };