57a539c05134ff00be5e5e7dde0431fbece33baf
[openssl.git] / crypto / rsa / rsa_backend.c
1 /*
2  * Copyright 2020 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_names.h>
11 #include <openssl/params.h>
12 #include "crypto/rsa.h"
13
14 /*
15  * The intention with the "backend" source file is to offer backend support
16  * for legacy backends (EVP_PKEY_ASN1_METHOD and EVP_PKEY_METHOD) and provider
17  * implementations alike.
18  */
19
20 DEFINE_STACK_OF(BIGNUM)
21
22 static int collect_numbers(STACK_OF(BIGNUM) *numbers,
23                            const OSSL_PARAM params[], const char *names[])
24 {
25     const OSSL_PARAM *p = NULL;
26     int i;
27
28     if (numbers == NULL)
29         return 0;
30
31     for (i = 0; names[i] != NULL; i++){
32         p = OSSL_PARAM_locate_const(params, names[i]);
33         if (p != NULL) {
34             BIGNUM *tmp = NULL;
35
36             if (!OSSL_PARAM_get_BN(p, &tmp)
37                 || sk_BIGNUM_push(numbers, tmp) == 0)
38                 return 0;
39         }
40     }
41
42     return 1;
43 }
44
45 int rsa_fromdata(RSA *rsa, const OSSL_PARAM params[])
46 {
47     const OSSL_PARAM *param_n, *param_e,  *param_d;
48     BIGNUM *n = NULL, *e = NULL, *d = NULL;
49     STACK_OF(BIGNUM) *factors = NULL, *exps = NULL, *coeffs = NULL;
50     int is_private = 0;
51
52     if (rsa == NULL)
53         return 0;
54
55     param_n = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_N);
56     param_e = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_E);
57     param_d = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_D);
58
59     if ((param_n != NULL && !OSSL_PARAM_get_BN(param_n, &n))
60         || (param_e != NULL && !OSSL_PARAM_get_BN(param_e, &e))
61         || (param_d != NULL && !OSSL_PARAM_get_BN(param_d, &d)))
62         goto err;
63
64     is_private = (d != NULL);
65
66     if (!RSA_set0_key(rsa, n, e, d))
67         goto err;
68     n = e = d = NULL;
69
70     if (is_private) {
71         if (!collect_numbers(factors = sk_BIGNUM_new_null(), params,
72                              rsa_mp_factor_names)
73             || !collect_numbers(exps = sk_BIGNUM_new_null(), params,
74                                 rsa_mp_exp_names)
75             || !collect_numbers(coeffs = sk_BIGNUM_new_null(), params,
76                                 rsa_mp_coeff_names))
77             goto err;
78
79         /* It's ok if this private key just has n, e and d */
80         if (sk_BIGNUM_num(factors) != 0
81             && !rsa_set0_all_params(rsa, factors, exps, coeffs))
82             goto err;
83     }
84
85     sk_BIGNUM_free(factors);
86     sk_BIGNUM_free(exps);
87     sk_BIGNUM_free(coeffs);
88     return 1;
89
90  err:
91     BN_free(n);
92     BN_free(e);
93     BN_free(d);
94     sk_BIGNUM_pop_free(factors, BN_free);
95     sk_BIGNUM_pop_free(exps, BN_free);
96     sk_BIGNUM_pop_free(coeffs, BN_free);
97     return 0;
98 }
99