2016-05-17 18:51:34 +00:00
|
|
|
/*
|
2017-08-22 17:25:23 +00:00
|
|
|
* Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
|
1998-12-21 10:52:47 +00:00
|
|
|
*
|
2016-05-17 18:51:34 +00: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 10:52:47 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
1999-04-23 22:13:45 +00:00
|
|
|
#include <openssl/crypto.h>
|
2015-05-14 14:56:48 +00:00
|
|
|
#include "internal/cryptlib.h"
|
2017-08-21 21:17:35 +00:00
|
|
|
#include "internal/refcount.h"
|
2014-10-28 23:00:29 +00:00
|
|
|
#include "internal/bn_int.h"
|
2016-03-18 18:30:20 +00:00
|
|
|
#include <openssl/engine.h>
|
2016-11-21 00:44:01 +00:00
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include "internal/evp_int.h"
|
2016-04-02 13:12:58 +00:00
|
|
|
#include "rsa_locl.h"
|
1998-12-21 10:52:47 +00:00
|
|
|
|
1999-04-19 21:31:43 +00:00
|
|
|
RSA *RSA_new(void)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2017-04-07 16:07:42 +00:00
|
|
|
return RSA_new_method(NULL);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1999-06-29 22:22:42 +00:00
|
|
|
|
2000-11-06 22:34:17 +00:00
|
|
|
const RSA_METHOD *RSA_get_method(const RSA *rsa)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
return rsa->meth;
|
|
|
|
}
|
2001-09-25 20:23:40 +00:00
|
|
|
|
|
|
|
int RSA_set_method(RSA *rsa, const RSA_METHOD *meth)
|
2015-01-22 03:40:55 +00: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-30 17:39:26 +00:00
|
|
|
#ifndef OPENSSL_NO_ENGINE
|
2016-02-25 17:09:06 +00:00
|
|
|
ENGINE_finish(rsa->engine);
|
|
|
|
rsa->engine = NULL;
|
2003-01-30 17:39:26 +00:00
|
|
|
#endif
|
2015-01-22 03:40:55 +00:00
|
|
|
rsa->meth = meth;
|
|
|
|
if (meth->init)
|
|
|
|
meth->init(rsa);
|
|
|
|
return 1;
|
|
|
|
}
|
1999-06-29 22:22:42 +00:00
|
|
|
|
2000-10-26 21:07:28 +00:00
|
|
|
RSA *RSA_new_method(ENGINE *engine)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2016-05-17 19:21:46 +00:00
|
|
|
RSA *ret = OPENSSL_zalloc(sizeof(*ret));
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (ret == NULL) {
|
|
|
|
RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
|
|
|
|
return NULL;
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2016-05-17 19:21:46 +00: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 03:40:55 +00:00
|
|
|
ret->meth = RSA_get_default_method();
|
2003-01-30 17:39:26 +00:00
|
|
|
#ifndef OPENSSL_NO_ENGINE
|
2016-05-17 19:21:46 +00:00
|
|
|
ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
|
2015-01-22 03:40:55 +00:00
|
|
|
if (engine) {
|
|
|
|
if (!ENGINE_init(engine)) {
|
|
|
|
RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
|
2016-05-17 19:21:46 +00:00
|
|
|
goto err;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
ret->engine = engine;
|
2017-08-22 17:36:49 +00:00
|
|
|
} else {
|
2015-01-22 03:40:55 +00:00
|
|
|
ret->engine = ENGINE_get_default_RSA();
|
2017-08-22 17:36:49 +00:00
|
|
|
}
|
2015-01-22 03:40:55 +00:00
|
|
|
if (ret->engine) {
|
|
|
|
ret->meth = ENGINE_get_RSA(ret->engine);
|
2016-02-25 17:09:06 +00:00
|
|
|
if (ret->meth == NULL) {
|
2015-01-22 03:40:55 +00:00
|
|
|
RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
|
2016-05-17 19:21:46 +00:00
|
|
|
goto err;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
}
|
2003-01-30 17:39:26 +00:00
|
|
|
#endif
|
2001-06-23 23:07:34 +00:00
|
|
|
|
2015-01-22 03:40:55 +00: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-17 19:21:46 +00:00
|
|
|
goto err;
|
2016-03-04 15:43:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
|
2016-05-17 19:21:46 +00:00
|
|
|
RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_INIT_FAIL);
|
|
|
|
goto err;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2016-03-04 15:43:46 +00:00
|
|
|
|
|
|
|
return ret;
|
2016-05-17 19:21:46 +00:00
|
|
|
|
|
|
|
err:
|
|
|
|
RSA_free(ret);
|
|
|
|
return NULL;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
1999-04-19 21:31:43 +00:00
|
|
|
void RSA_free(RSA *r)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
int i;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (r == NULL)
|
|
|
|
return;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2016-08-27 14:01:08 +00:00
|
|
|
CRYPTO_DOWN_REF(&r->references, &i, r->lock);
|
2016-01-30 17:04:25 +00:00
|
|
|
REF_PRINT_COUNT("RSA", r);
|
2015-01-22 03:40:55 +00:00
|
|
|
if (i > 0)
|
|
|
|
return;
|
2016-01-30 17:04:25 +00:00
|
|
|
REF_ASSERT_ISNT(i < 0);
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (r->meth->finish)
|
|
|
|
r->meth->finish(r);
|
2003-01-30 17:39:26 +00:00
|
|
|
#ifndef OPENSSL_NO_ENGINE
|
2016-02-25 21:34:27 +00:00
|
|
|
ENGINE_finish(r->engine);
|
2003-01-30 17:39:26 +00:00
|
|
|
#endif
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
|
2000-11-26 18:34:45 +00:00
|
|
|
|
2016-03-04 15:43:46 +00:00
|
|
|
CRYPTO_THREAD_lock_free(r->lock);
|
|
|
|
|
2015-05-01 01:37:06 +00: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 01:34:56 +00:00
|
|
|
RSA_PSS_PARAMS_free(r->pss);
|
2015-05-01 01:37:06 +00:00
|
|
|
BN_BLINDING_free(r->blinding);
|
|
|
|
BN_BLINDING_free(r->mt_blinding);
|
2015-08-26 04:25:11 +00:00
|
|
|
OPENSSL_free(r->bignum_data);
|
2015-01-22 03:40:55 +00:00
|
|
|
OPENSSL_free(r);
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2001-09-03 13:40:07 +00:00
|
|
|
int RSA_up_ref(RSA *r)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2016-03-04 15:43:46 +00:00
|
|
|
int i;
|
|
|
|
|
2016-08-27 14:01:08 +00:00
|
|
|
if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
|
2016-03-04 15:43:46 +00:00
|
|
|
return 0;
|
2016-01-30 17:04:25 +00:00
|
|
|
|
|
|
|
REF_PRINT_COUNT("RSA", r);
|
|
|
|
REF_ASSERT_ISNT(i < 2);
|
2017-08-22 17:25:23 +00:00
|
|
|
return i > 1 ? 1 : 0;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2001-08-25 17:24:21 +00:00
|
|
|
|
2000-01-23 23:41:49 +00:00
|
|
|
int RSA_set_ex_data(RSA *r, int idx, void *arg)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2017-08-22 17:25:23 +00:00
|
|
|
return CRYPTO_set_ex_data(&r->ex_data, idx, arg);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1998-12-21 10:56:39 +00:00
|
|
|
|
2000-11-06 22:34:17 +00:00
|
|
|
void *RSA_get_ex_data(const RSA *r, int idx)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2017-08-22 17:25:23 +00:00
|
|
|
return CRYPTO_get_ex_data(&r->ex_data, idx);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1998-12-21 10:56:39 +00:00
|
|
|
|
2014-01-18 14:51:40 +00:00
|
|
|
int RSA_security_bits(const RSA *rsa)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
return BN_security_bits(BN_num_bits(rsa->n), -1);
|
|
|
|
}
|
2016-04-02 13:12:58 +00:00
|
|
|
|
|
|
|
int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
|
|
|
|
{
|
2016-06-14 13:48:16 +00: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-25 18:28:54 +00: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 09:07:32 +00:00
|
|
|
if ((r->n == NULL && n == NULL)
|
|
|
|
|| (r->e == NULL && e == NULL))
|
2016-04-02 13:12:58 +00: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-25 18:28:54 +00: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 13:12:58 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
|
|
|
|
{
|
2016-06-14 13:48:16 +00: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-25 18:28:54 +00:00
|
|
|
* parameters MUST be non-NULL.
|
|
|
|
*/
|
2016-06-16 09:07:32 +00:00
|
|
|
if ((r->p == NULL && p == NULL)
|
|
|
|
|| (r->q == NULL && q == NULL))
|
2016-04-02 13:12:58 +00: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-25 18:28:54 +00:00
|
|
|
if (p != NULL) {
|
|
|
|
BN_free(r->p);
|
|
|
|
r->p = p;
|
|
|
|
}
|
|
|
|
if (q != NULL) {
|
|
|
|
BN_free(r->q);
|
|
|
|
r->q = q;
|
|
|
|
}
|
2016-04-02 13:12:58 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
|
|
|
|
{
|
2016-06-14 13:48:16 +00: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-25 18:28:54 +00:00
|
|
|
* parameters MUST be non-NULL.
|
|
|
|
*/
|
2016-06-16 09:07:32 +00:00
|
|
|
if ((r->dmp1 == NULL && dmp1 == NULL)
|
|
|
|
|| (r->dmq1 == NULL && dmq1 == NULL)
|
|
|
|
|| (r->iqmp == NULL && iqmp == NULL))
|
2016-04-02 13:12:58 +00: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-25 18:28:54 +00: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 13:12:58 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-06-14 13:48:16 +00:00
|
|
|
void RSA_get0_key(const RSA *r,
|
|
|
|
const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
|
2016-04-02 13:12:58 +00:00
|
|
|
{
|
|
|
|
if (n != NULL)
|
|
|
|
*n = r->n;
|
|
|
|
if (e != NULL)
|
|
|
|
*e = r->e;
|
|
|
|
if (d != NULL)
|
|
|
|
*d = r->d;
|
|
|
|
}
|
|
|
|
|
2016-06-14 13:48:16 +00:00
|
|
|
void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
|
2016-04-02 13:12:58 +00:00
|
|
|
{
|
|
|
|
if (p != NULL)
|
|
|
|
*p = r->p;
|
|
|
|
if (q != NULL)
|
|
|
|
*q = r->q;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RSA_get0_crt_params(const RSA *r,
|
2016-06-14 13:48:16 +00:00
|
|
|
const BIGNUM **dmp1, const BIGNUM **dmq1,
|
|
|
|
const BIGNUM **iqmp)
|
2016-04-02 13:12:58 +00: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-26 19:55:03 +00:00
|
|
|
ENGINE *RSA_get0_engine(const RSA *r)
|
2016-04-02 13:12:58 +00:00
|
|
|
{
|
|
|
|
return r->engine;
|
|
|
|
}
|
2016-11-21 00:44:01 +00: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);
|
|
|
|
}
|