2016-05-18 02:51:34 +08:00
|
|
|
/*
|
|
|
|
* Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
|
1998-12-21 18:52:47 +08:00
|
|
|
*
|
2016-05-18 02:51:34 +08:00
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
1998-12-21 18:52:47 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
1999-04-24 06:13:45 +08:00
|
|
|
#include <openssl/crypto.h>
|
2015-05-14 22:56:48 +08:00
|
|
|
#include "internal/cryptlib.h"
|
1999-04-24 06:13:45 +08:00
|
|
|
#include <openssl/lhash.h>
|
2014-10-29 07:00:29 +08:00
|
|
|
#include "internal/bn_int.h"
|
2016-03-19 02:30:20 +08:00
|
|
|
#include <openssl/engine.h>
|
2016-11-21 08:44:01 +08:00
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include "internal/evp_int.h"
|
2016-04-02 21:12:58 +08:00
|
|
|
#include "rsa_locl.h"
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
static const RSA_METHOD *default_RSA_meth = NULL;
|
1998-12-21 18:52:47 +08:00
|
|
|
|
1999-04-20 05:31:43 +08:00
|
|
|
RSA *RSA_new(void)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
RSA *r = RSA_new_method(NULL);
|
2003-03-21 01:31:30 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
return r;
|
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2001-09-26 04:23:40 +08:00
|
|
|
void RSA_set_default_method(const RSA_METHOD *meth)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
default_RSA_meth = meth;
|
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2001-09-26 04:23:40 +08:00
|
|
|
const RSA_METHOD *RSA_get_default_method(void)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
if (default_RSA_meth == NULL) {
|
2000-04-20 14:44:18 +08:00
|
|
|
#ifdef RSA_NULL
|
2015-01-22 11:40:55 +08:00
|
|
|
default_RSA_meth = RSA_null_method();
|
2000-04-20 14:44:18 +08:00
|
|
|
#else
|
2015-10-28 03:11:48 +08:00
|
|
|
default_RSA_meth = RSA_PKCS1_OpenSSL();
|
2000-04-20 14:44:18 +08:00
|
|
|
#endif
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2000-04-20 14:44:18 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
return default_RSA_meth;
|
|
|
|
}
|
1999-06-30 06:22:42 +08:00
|
|
|
|
2000-11-07 06:34:17 +08:00
|
|
|
const RSA_METHOD *RSA_get_method(const RSA *rsa)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
return rsa->meth;
|
|
|
|
}
|
2001-09-26 04:23:40 +08:00
|
|
|
|
|
|
|
int RSA_set_method(RSA *rsa, const RSA_METHOD *meth)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* NB: The caller is specifically setting a method, so it's not up to us
|
|
|
|
* to deal with which ENGINE it comes from.
|
|
|
|
*/
|
|
|
|
const RSA_METHOD *mtmp;
|
|
|
|
mtmp = rsa->meth;
|
|
|
|
if (mtmp->finish)
|
|
|
|
mtmp->finish(rsa);
|
2003-01-31 01:39:26 +08:00
|
|
|
#ifndef OPENSSL_NO_ENGINE
|
2016-02-26 01:09:06 +08:00
|
|
|
ENGINE_finish(rsa->engine);
|
|
|
|
rsa->engine = NULL;
|
2003-01-31 01:39:26 +08:00
|
|
|
#endif
|
2015-01-22 11:40:55 +08:00
|
|
|
rsa->meth = meth;
|
|
|
|
if (meth->init)
|
|
|
|
meth->init(rsa);
|
|
|
|
return 1;
|
|
|
|
}
|
1999-06-30 06:22:42 +08:00
|
|
|
|
2000-10-27 05:07:28 +08:00
|
|
|
RSA *RSA_new_method(ENGINE *engine)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2016-05-18 03:21:46 +08:00
|
|
|
RSA *ret = OPENSSL_zalloc(sizeof(*ret));
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (ret == NULL) {
|
|
|
|
RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
|
|
|
|
return NULL;
|
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2016-05-18 03:21:46 +08:00
|
|
|
ret->references = 1;
|
|
|
|
ret->lock = CRYPTO_THREAD_lock_new();
|
|
|
|
if (ret->lock == NULL) {
|
|
|
|
RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
|
|
|
|
OPENSSL_free(ret);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
ret->meth = RSA_get_default_method();
|
2003-01-31 01:39:26 +08:00
|
|
|
#ifndef OPENSSL_NO_ENGINE
|
2016-05-18 03:21:46 +08:00
|
|
|
ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
|
2015-01-22 11:40:55 +08:00
|
|
|
if (engine) {
|
|
|
|
if (!ENGINE_init(engine)) {
|
|
|
|
RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
|
2016-05-18 03:21:46 +08:00
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
ret->engine = engine;
|
|
|
|
} else
|
|
|
|
ret->engine = ENGINE_get_default_RSA();
|
|
|
|
if (ret->engine) {
|
|
|
|
ret->meth = ENGINE_get_RSA(ret->engine);
|
2016-02-26 01:09:06 +08:00
|
|
|
if (ret->meth == NULL) {
|
2015-01-22 11:40:55 +08:00
|
|
|
RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
|
2016-05-18 03:21:46 +08:00
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
}
|
2003-01-31 01:39:26 +08:00
|
|
|
#endif
|
2001-06-24 07:07:34 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
|
|
|
|
if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data)) {
|
2016-05-18 03:21:46 +08:00
|
|
|
goto err;
|
2016-03-04 23:43:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
|
2016-05-18 03:21:46 +08:00
|
|
|
RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_INIT_FAIL);
|
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2016-03-04 23:43:46 +08:00
|
|
|
|
|
|
|
return ret;
|
2016-05-18 03:21:46 +08:00
|
|
|
|
|
|
|
err:
|
|
|
|
RSA_free(ret);
|
|
|
|
return NULL;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
1999-04-20 05:31:43 +08:00
|
|
|
void RSA_free(RSA *r)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
int i;
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (r == NULL)
|
|
|
|
return;
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2016-08-27 22:01:08 +08:00
|
|
|
CRYPTO_DOWN_REF(&r->references, &i, r->lock);
|
2016-01-31 01:04:25 +08:00
|
|
|
REF_PRINT_COUNT("RSA", r);
|
2015-01-22 11:40:55 +08:00
|
|
|
if (i > 0)
|
|
|
|
return;
|
2016-01-31 01:04:25 +08:00
|
|
|
REF_ASSERT_ISNT(i < 0);
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (r->meth->finish)
|
|
|
|
r->meth->finish(r);
|
2003-01-31 01:39:26 +08:00
|
|
|
#ifndef OPENSSL_NO_ENGINE
|
2016-02-26 05:34:27 +08:00
|
|
|
ENGINE_finish(r->engine);
|
2003-01-31 01:39:26 +08:00
|
|
|
#endif
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
|
2000-11-27 02:34:45 +08:00
|
|
|
|
2016-03-04 23:43:46 +08:00
|
|
|
CRYPTO_THREAD_lock_free(r->lock);
|
|
|
|
|
2015-05-01 09:37:06 +08:00
|
|
|
BN_clear_free(r->n);
|
|
|
|
BN_clear_free(r->e);
|
|
|
|
BN_clear_free(r->d);
|
|
|
|
BN_clear_free(r->p);
|
|
|
|
BN_clear_free(r->q);
|
|
|
|
BN_clear_free(r->dmp1);
|
|
|
|
BN_clear_free(r->dmq1);
|
|
|
|
BN_clear_free(r->iqmp);
|
2016-11-21 09:34:56 +08:00
|
|
|
RSA_PSS_PARAMS_free(r->pss);
|
2015-05-01 09:37:06 +08:00
|
|
|
BN_BLINDING_free(r->blinding);
|
|
|
|
BN_BLINDING_free(r->mt_blinding);
|
2015-08-26 12:25:11 +08:00
|
|
|
OPENSSL_free(r->bignum_data);
|
2015-01-22 11:40:55 +08:00
|
|
|
OPENSSL_free(r);
|
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2001-09-03 21:40:07 +08:00
|
|
|
int RSA_up_ref(RSA *r)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2016-03-04 23:43:46 +08:00
|
|
|
int i;
|
|
|
|
|
2016-08-27 22:01:08 +08:00
|
|
|
if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
|
2016-03-04 23:43:46 +08:00
|
|
|
return 0;
|
2016-01-31 01:04:25 +08:00
|
|
|
|
|
|
|
REF_PRINT_COUNT("RSA", r);
|
|
|
|
REF_ASSERT_ISNT(i < 2);
|
2015-01-22 11:40:55 +08:00
|
|
|
return ((i > 1) ? 1 : 0);
|
|
|
|
}
|
2001-08-26 01:24:21 +08:00
|
|
|
|
2000-01-24 07:41:49 +08:00
|
|
|
int RSA_set_ex_data(RSA *r, int idx, void *arg)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
return (CRYPTO_set_ex_data(&r->ex_data, idx, arg));
|
|
|
|
}
|
1998-12-21 18:56:39 +08:00
|
|
|
|
2000-11-07 06:34:17 +08:00
|
|
|
void *RSA_get_ex_data(const RSA *r, int idx)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
return (CRYPTO_get_ex_data(&r->ex_data, idx));
|
|
|
|
}
|
1998-12-21 18:56:39 +08:00
|
|
|
|
2014-01-18 22:51:40 +08:00
|
|
|
int RSA_security_bits(const RSA *rsa)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
return BN_security_bits(BN_num_bits(rsa->n), -1);
|
|
|
|
}
|
2016-04-02 21:12:58 +08:00
|
|
|
|
|
|
|
int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
|
|
|
|
{
|
2016-06-14 21:48:16 +08:00
|
|
|
/* If the fields n and e in r are NULL, the corresponding input
|
RSA, DSA, DH: Allow some given input to be NULL on already initialised keys
The diverse {RSA,DSA,DH}_set0_* functions are made to allow some
parameters to be NULL IF the corresponding numbers in the given key
structure have already been previously initialised. Specifically,
this allows the addition of private components to be added to a key
that already has the public half, approximately like this:
RSA_get0_key(rsa, NULL, &e, NULL);
RSA_get0_factors(rsa, &p, &q);
/* calculate new d */
RSA_set0_key(rsa, NULL, NULL, d);
Reviewed-by: Matt Caswell <matt@openssl.org>
2016-04-26 02:28:54 +08:00
|
|
|
* parameters MUST be non-NULL for n and e. d may be
|
|
|
|
* left NULL (in case only the public key is used).
|
|
|
|
*/
|
2016-06-16 17:07:32 +08:00
|
|
|
if ((r->n == NULL && n == NULL)
|
|
|
|
|| (r->e == NULL && e == NULL))
|
2016-04-02 21:12:58 +08:00
|
|
|
return 0;
|
|
|
|
|
RSA, DSA, DH: Allow some given input to be NULL on already initialised keys
The diverse {RSA,DSA,DH}_set0_* functions are made to allow some
parameters to be NULL IF the corresponding numbers in the given key
structure have already been previously initialised. Specifically,
this allows the addition of private components to be added to a key
that already has the public half, approximately like this:
RSA_get0_key(rsa, NULL, &e, NULL);
RSA_get0_factors(rsa, &p, &q);
/* calculate new d */
RSA_set0_key(rsa, NULL, NULL, d);
Reviewed-by: Matt Caswell <matt@openssl.org>
2016-04-26 02:28:54 +08:00
|
|
|
if (n != NULL) {
|
|
|
|
BN_free(r->n);
|
|
|
|
r->n = n;
|
|
|
|
}
|
|
|
|
if (e != NULL) {
|
|
|
|
BN_free(r->e);
|
|
|
|
r->e = e;
|
|
|
|
}
|
|
|
|
if (d != NULL) {
|
|
|
|
BN_free(r->d);
|
|
|
|
r->d = d;
|
|
|
|
}
|
2016-04-02 21:12:58 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
|
|
|
|
{
|
2016-06-14 21:48:16 +08:00
|
|
|
/* If the fields p and q in r are NULL, the corresponding input
|
RSA, DSA, DH: Allow some given input to be NULL on already initialised keys
The diverse {RSA,DSA,DH}_set0_* functions are made to allow some
parameters to be NULL IF the corresponding numbers in the given key
structure have already been previously initialised. Specifically,
this allows the addition of private components to be added to a key
that already has the public half, approximately like this:
RSA_get0_key(rsa, NULL, &e, NULL);
RSA_get0_factors(rsa, &p, &q);
/* calculate new d */
RSA_set0_key(rsa, NULL, NULL, d);
Reviewed-by: Matt Caswell <matt@openssl.org>
2016-04-26 02:28:54 +08:00
|
|
|
* parameters MUST be non-NULL.
|
|
|
|
*/
|
2016-06-16 17:07:32 +08:00
|
|
|
if ((r->p == NULL && p == NULL)
|
|
|
|
|| (r->q == NULL && q == NULL))
|
2016-04-02 21:12:58 +08:00
|
|
|
return 0;
|
|
|
|
|
RSA, DSA, DH: Allow some given input to be NULL on already initialised keys
The diverse {RSA,DSA,DH}_set0_* functions are made to allow some
parameters to be NULL IF the corresponding numbers in the given key
structure have already been previously initialised. Specifically,
this allows the addition of private components to be added to a key
that already has the public half, approximately like this:
RSA_get0_key(rsa, NULL, &e, NULL);
RSA_get0_factors(rsa, &p, &q);
/* calculate new d */
RSA_set0_key(rsa, NULL, NULL, d);
Reviewed-by: Matt Caswell <matt@openssl.org>
2016-04-26 02:28:54 +08:00
|
|
|
if (p != NULL) {
|
|
|
|
BN_free(r->p);
|
|
|
|
r->p = p;
|
|
|
|
}
|
|
|
|
if (q != NULL) {
|
|
|
|
BN_free(r->q);
|
|
|
|
r->q = q;
|
|
|
|
}
|
2016-04-02 21:12:58 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
|
|
|
|
{
|
2016-06-14 21:48:16 +08:00
|
|
|
/* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input
|
RSA, DSA, DH: Allow some given input to be NULL on already initialised keys
The diverse {RSA,DSA,DH}_set0_* functions are made to allow some
parameters to be NULL IF the corresponding numbers in the given key
structure have already been previously initialised. Specifically,
this allows the addition of private components to be added to a key
that already has the public half, approximately like this:
RSA_get0_key(rsa, NULL, &e, NULL);
RSA_get0_factors(rsa, &p, &q);
/* calculate new d */
RSA_set0_key(rsa, NULL, NULL, d);
Reviewed-by: Matt Caswell <matt@openssl.org>
2016-04-26 02:28:54 +08:00
|
|
|
* parameters MUST be non-NULL.
|
|
|
|
*/
|
2016-06-16 17:07:32 +08:00
|
|
|
if ((r->dmp1 == NULL && dmp1 == NULL)
|
|
|
|
|| (r->dmq1 == NULL && dmq1 == NULL)
|
|
|
|
|| (r->iqmp == NULL && iqmp == NULL))
|
2016-04-02 21:12:58 +08:00
|
|
|
return 0;
|
|
|
|
|
RSA, DSA, DH: Allow some given input to be NULL on already initialised keys
The diverse {RSA,DSA,DH}_set0_* functions are made to allow some
parameters to be NULL IF the corresponding numbers in the given key
structure have already been previously initialised. Specifically,
this allows the addition of private components to be added to a key
that already has the public half, approximately like this:
RSA_get0_key(rsa, NULL, &e, NULL);
RSA_get0_factors(rsa, &p, &q);
/* calculate new d */
RSA_set0_key(rsa, NULL, NULL, d);
Reviewed-by: Matt Caswell <matt@openssl.org>
2016-04-26 02:28:54 +08:00
|
|
|
if (dmp1 != NULL) {
|
|
|
|
BN_free(r->dmp1);
|
|
|
|
r->dmp1 = dmp1;
|
|
|
|
}
|
|
|
|
if (dmq1 != NULL) {
|
|
|
|
BN_free(r->dmq1);
|
|
|
|
r->dmq1 = dmq1;
|
|
|
|
}
|
|
|
|
if (iqmp != NULL) {
|
|
|
|
BN_free(r->iqmp);
|
|
|
|
r->iqmp = iqmp;
|
|
|
|
}
|
2016-04-02 21:12:58 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-06-14 21:48:16 +08:00
|
|
|
void RSA_get0_key(const RSA *r,
|
|
|
|
const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
|
2016-04-02 21:12:58 +08:00
|
|
|
{
|
|
|
|
if (n != NULL)
|
|
|
|
*n = r->n;
|
|
|
|
if (e != NULL)
|
|
|
|
*e = r->e;
|
|
|
|
if (d != NULL)
|
|
|
|
*d = r->d;
|
|
|
|
}
|
|
|
|
|
2016-06-14 21:48:16 +08:00
|
|
|
void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
|
2016-04-02 21:12:58 +08:00
|
|
|
{
|
|
|
|
if (p != NULL)
|
|
|
|
*p = r->p;
|
|
|
|
if (q != NULL)
|
|
|
|
*q = r->q;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RSA_get0_crt_params(const RSA *r,
|
2016-06-14 21:48:16 +08:00
|
|
|
const BIGNUM **dmp1, const BIGNUM **dmq1,
|
|
|
|
const BIGNUM **iqmp)
|
2016-04-02 21:12:58 +08:00
|
|
|
{
|
|
|
|
if (dmp1 != NULL)
|
|
|
|
*dmp1 = r->dmp1;
|
|
|
|
if (dmq1 != NULL)
|
|
|
|
*dmq1 = r->dmq1;
|
|
|
|
if (iqmp != NULL)
|
|
|
|
*iqmp = r->iqmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RSA_clear_flags(RSA *r, int flags)
|
|
|
|
{
|
|
|
|
r->flags &= ~flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
int RSA_test_flags(const RSA *r, int flags)
|
|
|
|
{
|
|
|
|
return r->flags & flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RSA_set_flags(RSA *r, int flags)
|
|
|
|
{
|
|
|
|
r->flags |= flags;
|
|
|
|
}
|
|
|
|
|
2016-06-27 03:55:03 +08:00
|
|
|
ENGINE *RSA_get0_engine(const RSA *r)
|
2016-04-02 21:12:58 +08:00
|
|
|
{
|
|
|
|
return r->engine;
|
|
|
|
}
|
2016-11-21 08:44:01 +08:00
|
|
|
|
|
|
|
int RSA_pkey_ctx_ctrl(EVP_PKEY_CTX *ctx, int optype, int cmd, int p1, void *p2)
|
|
|
|
{
|
|
|
|
/* If key type not RSA or RSA-PSS return error */
|
|
|
|
if (ctx != NULL && ctx->pmeth != NULL
|
|
|
|
&& ctx->pmeth->pkey_id != EVP_PKEY_RSA
|
|
|
|
&& ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
|
|
|
|
return -1;
|
|
|
|
return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, p1, p2);
|
|
|
|
}
|