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