50647eb6f5650b849eab96b50a3c655d7f41e05b
[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     tmpl = OSSL_PARAM_BLD_new();
188     if (tmpl == NULL)
189         return 0;
190
191     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
192         ok = ok && key_to_params(rsa, tmpl);
193
194     if (!ok
195         || (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL) {
196         OSSL_PARAM_BLD_free(tmpl);
197         return 0;
198     }
199     OSSL_PARAM_BLD_free(tmpl);
200
201     ok = param_callback(params, cbarg);
202     OSSL_PARAM_BLD_free_params(params);
203     return ok;
204 }
205
206 /*
207  * This provider can export everything in an RSA key, so we use the exact
208  * same type description for export as for import.  Other providers might
209  * choose to import full keys, but only export the public parts, and will
210  * therefore have the importkey_types and importkey_types functions return
211  * different arrays.
212  */
213 static const OSSL_PARAM rsa_key_types[] = {
214     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_N, NULL, 0),
215     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_E, NULL, 0),
216     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_D, NULL, 0),
217     /* We tolerate up to 10 factors... */
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     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
224     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
225     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
226     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
227     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
228     /* ..., up to 10 CRT exponents... */
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     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
235     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
236     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
237     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
238     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
239     /* ..., and up to 9 CRT coefficients */
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     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
245     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
246     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
247     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
248     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
249 };
250 /*
251  * We lied about the amount of factors, exponents and coefficients, the
252  * export and import functions can really deal with an infinite amount
253  * of these numbers.  However, RSA keys with too many primes are futile,
254  * so we at least pretend to have some limits.
255  */
256
257 static const OSSL_PARAM *rsa_imexport_types(int selection)
258 {
259     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
260         return rsa_key_types;
261     return NULL;
262 }
263
264 static const OSSL_PARAM *rsa_import_types(int selection)
265 {
266     return rsa_imexport_types(selection);
267 }
268
269
270 static const OSSL_PARAM *rsa_export_types(int selection)
271 {
272     return rsa_imexport_types(selection);
273 }
274
275 static int rsa_get_params(void *key, OSSL_PARAM params[])
276 {
277     RSA *rsa = key;
278     OSSL_PARAM *p;
279
280     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
281         && !OSSL_PARAM_set_int(p, RSA_bits(rsa)))
282         return 0;
283     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL
284         && !OSSL_PARAM_set_int(p, RSA_security_bits(rsa)))
285         return 0;
286     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
287         && !OSSL_PARAM_set_int(p, RSA_size(rsa)))
288         return 0;
289
290 # if 0  /* TODO(3.0): PSS support pending */
291     if ((p = OSSL_PARAM_locate(params,
292                                OSSL_PKEY_PARAM_MANDATORY_DIGEST)) != NULL
293         && RSA_get0_pss_params(rsa) != NULL) {
294         const EVP_MD *md, *mgf1md;
295         int min_saltlen;
296
297         if (!rsa_pss_get_param(RSA_get0_pss_params(rsa),
298                                &md, &mgf1md, &min_saltlen)) {
299             ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
300             return 0;
301         }
302         if (!OSSL_PARAM_set_utf8_string(p, EVP_MD_name(md)))
303             return 0;
304     }
305 #endif
306     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_DEFAULT_DIGEST)) != NULL
307 /* TODO(3.0): PSS support pending */
308 #if 0
309             && RSA_get0_pss_params(rsa) == NULL
310 #endif
311             ) {
312         if (!OSSL_PARAM_set_utf8_string(p, RSA_DEFAULT_MD))
313             return 0;
314     }
315
316     return 1;
317 }
318
319 static const OSSL_PARAM rsa_params[] = {
320     OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
321     OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
322     OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
323     OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST, NULL, 0),
324     OSSL_PARAM_END
325 };
326
327 static const OSSL_PARAM *rsa_gettable_params(void)
328 {
329     return rsa_params;
330 }
331
332 static int rsa_validate(void *keydata, int selection)
333 {
334     RSA *rsa = keydata;
335     int ok = 0;
336
337     if ((selection & RSA_POSSIBLE_SELECTIONS) != 0)
338         ok = 1;
339
340     /* If the whole key is selected, we do a pairwise validation */
341     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR)
342         == OSSL_KEYMGMT_SELECT_KEYPAIR) {
343         ok = ok && rsa_validate_pairwise(rsa);
344     } else {
345         if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
346             ok = ok && rsa_validate_private(rsa);
347         if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
348             ok = ok && rsa_validate_public(rsa);
349     }
350     return ok;
351 }
352
353 struct rsa_gen_ctx {
354     OPENSSL_CTX *libctx;
355
356     size_t nbits;
357     BIGNUM *pub_exp;
358     size_t primes;
359
360     /* For generation callback */
361     OSSL_CALLBACK *cb;
362     void *cbarg;
363 };
364
365 static int rsa_gencb(int p, int n, BN_GENCB *cb)
366 {
367     struct rsa_gen_ctx *gctx = BN_GENCB_get_arg(cb);
368     OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END };
369
370     params[0] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_POTENTIAL, &p);
371     params[1] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_ITERATION, &n);
372
373     return gctx->cb(params, gctx->cbarg);
374 }
375
376 static void *rsa_gen_init(void *provctx, int selection)
377 {
378     OPENSSL_CTX *libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
379     struct rsa_gen_ctx *gctx = NULL;
380
381     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
382         return NULL;
383
384     if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL) {
385         gctx->libctx = libctx;
386         if ((gctx->pub_exp = BN_new()) == NULL
387             || !BN_set_word(gctx->pub_exp, RSA_F4)) {
388             BN_free(gctx->pub_exp);
389             gctx = NULL;
390         } else {
391             gctx->nbits = 2048;
392             gctx->primes = RSA_DEFAULT_PRIME_NUM;
393         }
394     }
395     return gctx;
396 }
397
398 static int rsa_gen_set_params(void *genctx, const OSSL_PARAM params[])
399 {
400     struct rsa_gen_ctx *gctx = genctx;
401     const OSSL_PARAM *p;
402
403     if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_BITS)) != NULL
404         && !OSSL_PARAM_get_size_t(p, &gctx->nbits))
405         return 0;
406     if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_PRIMES)) != NULL
407         && !OSSL_PARAM_get_size_t(p, &gctx->primes))
408         return 0;
409     if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_E)) != NULL
410         && !OSSL_PARAM_get_BN(p, &gctx->pub_exp))
411         return 0;
412     return 1;
413 }
414
415 static const OSSL_PARAM *rsa_gen_settable_params(void *provctx)
416 {
417     static OSSL_PARAM settable[] = {
418         OSSL_PARAM_size_t(OSSL_PKEY_PARAM_RSA_BITS, NULL),
419         OSSL_PARAM_size_t(OSSL_PKEY_PARAM_RSA_PRIMES, NULL),
420         OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_E, NULL, 0),
421         OSSL_PARAM_END
422     };
423
424     return settable;
425 }
426
427 static void *rsa_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
428 {
429     struct rsa_gen_ctx *gctx = genctx;
430     RSA *rsa = NULL;
431     BN_GENCB *gencb = NULL;
432
433     if (gctx == NULL
434         || (rsa = rsa_new_with_ctx(gctx->libctx)) == NULL)
435         return NULL;
436
437     gctx->cb = osslcb;
438     gctx->cbarg = cbarg;
439     gencb = BN_GENCB_new();
440     if (gencb != NULL)
441         BN_GENCB_set(gencb, rsa_gencb, genctx);
442
443     if (!RSA_generate_multi_prime_key(rsa, (int)gctx->nbits, (int)gctx->primes,
444                                       gctx->pub_exp, gencb)) {
445         RSA_free(rsa);
446         rsa = NULL;
447     }
448
449     BN_GENCB_free(gencb);
450
451     return rsa;
452 }
453
454 static void rsa_gen_cleanup(void *genctx)
455 {
456     struct rsa_gen_ctx *gctx = genctx;
457
458     if (gctx == NULL)
459         return;
460
461     BN_clear_free(gctx->pub_exp);
462     OPENSSL_free(gctx);
463 }
464
465 const OSSL_DISPATCH rsa_keymgmt_functions[] = {
466     { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))rsa_newdata },
467     { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))rsa_gen_init },
468     { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS,
469       (void (*)(void))rsa_gen_set_params },
470     { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
471       (void (*)(void))rsa_gen_settable_params },
472     { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))rsa_gen },
473     { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))rsa_gen_cleanup },
474     { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))rsa_freedata },
475     { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))rsa_get_params },
476     { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))rsa_gettable_params },
477     { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))rsa_has },
478     { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))rsa_match },
479     { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))rsa_validate },
480     { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))rsa_import },
481     { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))rsa_import_types },
482     { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))rsa_export },
483     { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))rsa_export_types },
484     { 0, NULL }
485 };