67
67
#include <openssl/engine.h>
70
const char RSA_version[]="RSA" OPENSSL_VERSION_PTEXT;
72
static const RSA_METHOD *default_RSA_meth=NULL;
76
RSA *r=RSA_new_method(NULL);
81
void RSA_set_default_method(const RSA_METHOD *meth)
83
default_RSA_meth = meth;
86
const RSA_METHOD *RSA_get_default_method(void)
88
if (default_RSA_meth == NULL)
91
default_RSA_meth=RSA_null_method();
93
#if 0 /* was: #ifdef RSAref */
94
default_RSA_meth=RSA_PKCS1_RSAref();
96
default_RSA_meth=RSA_PKCS1_SSLeay();
101
return default_RSA_meth;
104
const RSA_METHOD *RSA_get_method(const RSA *rsa)
109
int RSA_set_method(RSA *rsa, const RSA_METHOD *meth)
111
/* NB: The caller is specifically setting a method, so it's not up to us
112
* to deal with which ENGINE it comes from. */
113
const RSA_METHOD *mtmp;
115
if (mtmp->finish) mtmp->finish(rsa);
116
#ifndef OPENSSL_NO_ENGINE
119
ENGINE_finish(rsa->engine);
124
if (meth->init) meth->init(rsa);
128
RSA *RSA_new_method(ENGINE *engine)
132
ret=(RSA *)OPENSSL_malloc(sizeof(RSA));
135
RSAerr(RSA_F_RSA_NEW_METHOD,ERR_R_MALLOC_FAILURE);
139
ret->meth = RSA_get_default_method();
140
#ifndef OPENSSL_NO_ENGINE
143
if (!ENGINE_init(engine))
145
RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
149
ret->engine = engine;
152
ret->engine = ENGINE_get_default_RSA();
155
ret->meth = ENGINE_get_RSA(ret->engine);
158
RSAerr(RSA_F_RSA_NEW_METHOD,
160
ENGINE_finish(ret->engine);
178
ret->_method_mod_n=NULL;
179
ret->_method_mod_p=NULL;
180
ret->_method_mod_q=NULL;
182
ret->mt_blinding=NULL;
183
ret->bignum_data=NULL;
184
ret->flags=ret->meth->flags;
185
CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data);
186
if ((ret->meth->init != NULL) && !ret->meth->init(ret))
188
#ifndef OPENSSL_NO_ENGINE
190
ENGINE_finish(ret->engine);
192
CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data);
199
void RSA_free(RSA *r)
203
if (r == NULL) return;
205
i=CRYPTO_add(&r->references,-1,CRYPTO_LOCK_RSA);
213
fprintf(stderr,"RSA_free, bad reference count\n");
220
#ifndef OPENSSL_NO_ENGINE
222
ENGINE_finish(r->engine);
225
CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
227
if (r->n != NULL) BN_clear_free(r->n);
228
if (r->e != NULL) BN_clear_free(r->e);
229
if (r->d != NULL) BN_clear_free(r->d);
230
if (r->p != NULL) BN_clear_free(r->p);
231
if (r->q != NULL) BN_clear_free(r->q);
232
if (r->dmp1 != NULL) BN_clear_free(r->dmp1);
233
if (r->dmq1 != NULL) BN_clear_free(r->dmq1);
234
if (r->iqmp != NULL) BN_clear_free(r->iqmp);
235
if (r->blinding != NULL) BN_BLINDING_free(r->blinding);
236
if (r->mt_blinding != NULL) BN_BLINDING_free(r->mt_blinding);
237
if (r->bignum_data != NULL) OPENSSL_free_locked(r->bignum_data);
241
int RSA_up_ref(RSA *r)
243
int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_RSA);
250
fprintf(stderr, "RSA_up_ref, bad reference count\n");
254
return ((i > 1) ? 1 : 0);
257
int RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
258
CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
260
return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_RSA, argl, argp,
261
new_func, dup_func, free_func);
264
int RSA_set_ex_data(RSA *r, int idx, void *arg)
266
return(CRYPTO_set_ex_data(&r->ex_data,idx,arg));
269
void *RSA_get_ex_data(const RSA *r, int idx)
271
return(CRYPTO_get_ex_data(&r->ex_data,idx));
274
int RSA_size(const RSA *r)
276
return(BN_num_bytes(r->n));
279
70
int RSA_public_encrypt(int flen, const unsigned char *from, unsigned char *to,
280
71
RSA *rsa, int padding)
431
int RSA_memory_lock(RSA *r)
435
BIGNUM *bn,**t[6],*b;
438
if (r->d == NULL) return(1);
446
off=k/sizeof(BN_ULONG)+1;
450
if ((p=OPENSSL_malloc_locked((off+j)*sizeof(BN_ULONG))) == NULL)
452
RSAerr(RSA_F_RSA_MEMORY_LOCK,ERR_R_MALLOC_FAILURE);
456
ul=(BN_ULONG *)&(p[off]);
461
memcpy((char *)&(bn[i]),(char *)b,sizeof(BIGNUM));
462
bn[i].flags=BN_FLG_STATIC_DATA;
464
memcpy((char *)ul,b->d,sizeof(BN_ULONG)*b->top);
469
/* I should fix this so it can still be done */
470
r->flags&= ~(RSA_FLAG_CACHE_PRIVATE|RSA_FLAG_CACHE_PUBLIC);