2 * Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include <openssl/core_names.h>
11 #include <openssl/params.h>
12 #include "crypto/rsa.h"
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.
20 DEFINE_STACK_OF(BIGNUM)
22 static int collect_numbers(STACK_OF(BIGNUM) *numbers,
23 const OSSL_PARAM params[], const char *names[])
25 const OSSL_PARAM *p = NULL;
31 for (i = 0; names[i] != NULL; i++){
32 p = OSSL_PARAM_locate_const(params, names[i]);
36 if (!OSSL_PARAM_get_BN(p, &tmp)
37 || sk_BIGNUM_push(numbers, tmp) == 0)
45 int rsa_fromdata(RSA *rsa, const OSSL_PARAM params[])
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;
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);
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)))
64 is_private = (d != NULL);
66 if (!RSA_set0_key(rsa, n, e, d))
71 if (!collect_numbers(factors = sk_BIGNUM_new_null(), params,
73 || !collect_numbers(exps = sk_BIGNUM_new_null(), params,
75 || !collect_numbers(coeffs = sk_BIGNUM_new_null(), params,
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))
85 sk_BIGNUM_free(factors);
87 sk_BIGNUM_free(coeffs);
94 sk_BIGNUM_pop_free(factors, BN_free);
95 sk_BIGNUM_pop_free(exps, BN_free);
96 sk_BIGNUM_pop_free(coeffs, BN_free);
100 DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
102 int rsa_todata(RSA *rsa, OSSL_PARAM_BLD *bld, OSSL_PARAM params[])
105 const BIGNUM *rsa_d = NULL, *rsa_n = NULL, *rsa_e = NULL;
106 STACK_OF(BIGNUM_const) *factors = sk_BIGNUM_const_new_null();
107 STACK_OF(BIGNUM_const) *exps = sk_BIGNUM_const_new_null();
108 STACK_OF(BIGNUM_const) *coeffs = sk_BIGNUM_const_new_null();
110 if (rsa == NULL || factors == NULL || exps == NULL || coeffs == NULL)
113 RSA_get0_key(rsa, &rsa_n, &rsa_e, &rsa_d);
114 rsa_get0_all_params(rsa, factors, exps, coeffs);
116 /* Check private key data integrity */
118 int numprimes = sk_BIGNUM_const_num(factors);
119 int numexps = sk_BIGNUM_const_num(exps);
120 int numcoeffs = sk_BIGNUM_const_num(coeffs);
123 * It's permisssible to have zero primes, i.e. no CRT params.
124 * Otherwise, there must be at least two, as many exponents,
125 * and one coefficient less.
128 && (numprimes < 2 || numexps < 2 || numcoeffs < 1))
132 if (!ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_RSA_N, rsa_n)
133 || !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_RSA_E, rsa_e)
134 || !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_RSA_D, rsa_d)
135 || !ossl_param_build_set_multi_key_bn(bld, params, rsa_mp_factor_names,
137 || !ossl_param_build_set_multi_key_bn(bld, params, rsa_mp_exp_names,
139 || !ossl_param_build_set_multi_key_bn(bld, params, rsa_mp_coeff_names,
144 sk_BIGNUM_const_free(factors);
145 sk_BIGNUM_const_free(exps);
146 sk_BIGNUM_const_free(coeffs);