Add the X509v3_cache_extensions() function
[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 *key)
24 {
25     const OSSL_PARAM *p = NULL;
26
27     if (numbers == NULL)
28         return 0;
29
30     for (p = params; (p = OSSL_PARAM_locate_const(p, key)) != NULL; p++) {
31         BIGNUM *tmp = NULL;
32
33         if (!OSSL_PARAM_get_BN(p, &tmp)
34             || sk_BIGNUM_push(numbers, tmp) == 0)
35             return 0;
36     }
37
38     return 1;
39 }
40
41 int rsa_fromdata(RSA *rsa, const OSSL_PARAM params[])
42 {
43     const OSSL_PARAM *param_n, *param_e,  *param_d;
44     BIGNUM *n = NULL, *e = NULL, *d = NULL;
45     STACK_OF(BIGNUM) *factors = NULL, *exps = NULL, *coeffs = NULL;
46     int is_private = 0;
47
48     if (rsa == NULL)
49         return 0;
50
51     param_n = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_N);
52     param_e = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_E);
53     param_d = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_D);
54
55     if ((param_n != NULL && !OSSL_PARAM_get_BN(param_n, &n))
56         || (param_e != NULL && !OSSL_PARAM_get_BN(param_e, &e))
57         || (param_d != NULL && !OSSL_PARAM_get_BN(param_d, &d)))
58         goto err;
59
60     is_private = (d != NULL);
61
62     if (!RSA_set0_key(rsa, n, e, d))
63         goto err;
64     n = e = d = NULL;
65
66     if (is_private) {
67         if (!collect_numbers(factors = sk_BIGNUM_new_null(), params,
68                              OSSL_PKEY_PARAM_RSA_FACTOR)
69             || !collect_numbers(exps = sk_BIGNUM_new_null(), params,
70                                 OSSL_PKEY_PARAM_RSA_EXPONENT)
71             || !collect_numbers(coeffs = sk_BIGNUM_new_null(), params,
72                                 OSSL_PKEY_PARAM_RSA_COEFFICIENT))
73             goto err;
74
75         /* It's ok if this private key just has n, e and d */
76         if (sk_BIGNUM_num(factors) != 0
77             && !rsa_set0_all_params(rsa, factors, exps, coeffs))
78             goto err;
79     }
80
81     sk_BIGNUM_free(factors);
82     sk_BIGNUM_free(exps);
83     sk_BIGNUM_free(coeffs);
84     return 1;
85
86  err:
87     BN_free(n);
88     BN_free(e);
89     BN_free(d);
90     sk_BIGNUM_pop_free(factors, BN_free);
91     sk_BIGNUM_pop_free(exps, BN_free);
92     sk_BIGNUM_pop_free(coeffs, BN_free);
93     return 0;
94 }
95