summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/rsa/rsa_lib.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/rsa/rsa_lib.c')
-rw-r--r--src/lib/libcrypto/rsa/rsa_lib.c175
1 files changed, 83 insertions, 92 deletions
diff --git a/src/lib/libcrypto/rsa/rsa_lib.c b/src/lib/libcrypto/rsa/rsa_lib.c
index 94395cc22c..93235744f7 100644
--- a/src/lib/libcrypto/rsa/rsa_lib.c
+++ b/src/lib/libcrypto/rsa/rsa_lib.c
@@ -66,42 +66,26 @@
66 66
67const char *RSA_version="RSA" OPENSSL_VERSION_PTEXT; 67const char *RSA_version="RSA" OPENSSL_VERSION_PTEXT;
68 68
69static RSA_METHOD *default_RSA_meth=NULL; 69static const RSA_METHOD *default_RSA_meth=NULL;
70static int rsa_meth_num=0;
71static STACK_OF(CRYPTO_EX_DATA_FUNCS) *rsa_meth=NULL;
72 70
73RSA *RSA_new(void) 71RSA *RSA_new(void)
74 { 72 {
75 return(RSA_new_method(NULL)); 73 return(RSA_new_method(NULL));
76 } 74 }
77 75
78void RSA_set_default_openssl_method(RSA_METHOD *meth) 76void RSA_set_default_method(const RSA_METHOD *meth)
79 { 77 {
80 ENGINE *e; 78 default_RSA_meth = meth;
81 /* We'll need to notify the "openssl" ENGINE of this
82 * change too. We won't bother locking things down at
83 * our end as there was never any locking in these
84 * functions! */
85 if(default_RSA_meth != meth)
86 {
87 default_RSA_meth = meth;
88 e = ENGINE_by_id("openssl");
89 if(e)
90 {
91 ENGINE_set_RSA(e, meth);
92 ENGINE_free(e);
93 }
94 }
95 } 79 }
96 80
97RSA_METHOD *RSA_get_default_openssl_method(void) 81const RSA_METHOD *RSA_get_default_method(void)
98{ 82 {
99 if (default_RSA_meth == NULL) 83 if (default_RSA_meth == NULL)
100 { 84 {
101#ifdef RSA_NULL 85#ifdef RSA_NULL
102 default_RSA_meth=RSA_null_method(); 86 default_RSA_meth=RSA_null_method();
103#else 87#else
104#ifdef RSAref 88#if 0 /* was: #ifdef RSAref */
105 default_RSA_meth=RSA_PKCS1_RSAref(); 89 default_RSA_meth=RSA_PKCS1_RSAref();
106#else 90#else
107 default_RSA_meth=RSA_PKCS1_SSLeay(); 91 default_RSA_meth=RSA_PKCS1_SSLeay();
@@ -110,69 +94,66 @@ RSA_METHOD *RSA_get_default_openssl_method(void)
110 } 94 }
111 95
112 return default_RSA_meth; 96 return default_RSA_meth;
113} 97 }
114 98
115RSA_METHOD *RSA_get_method(RSA *rsa) 99const RSA_METHOD *RSA_get_method(const RSA *rsa)
116{ 100 {
117 return ENGINE_get_RSA(rsa->engine); 101 return rsa->meth;
118} 102 }
119 103
120#if 0 104int RSA_set_method(RSA *rsa, const RSA_METHOD *meth)
121RSA_METHOD *RSA_set_method(RSA *rsa, RSA_METHOD *meth) 105 {
122{ 106 /* NB: The caller is specifically setting a method, so it's not up to us
123 RSA_METHOD *mtmp; 107 * to deal with which ENGINE it comes from. */
108 const RSA_METHOD *mtmp;
124 mtmp = rsa->meth; 109 mtmp = rsa->meth;
125 if (mtmp->finish) mtmp->finish(rsa); 110 if (mtmp->finish) mtmp->finish(rsa);
111 if (rsa->engine)
112 {
113 ENGINE_finish(rsa->engine);
114 rsa->engine = NULL;
115 }
126 rsa->meth = meth; 116 rsa->meth = meth;
127 if (meth->init) meth->init(rsa); 117 if (meth->init) meth->init(rsa);
128 return mtmp;
129}
130#else
131int RSA_set_method(RSA *rsa, ENGINE *engine)
132{
133 ENGINE *mtmp;
134 RSA_METHOD *meth;
135 mtmp = rsa->engine;
136 meth = ENGINE_get_RSA(mtmp);
137 if (!ENGINE_init(engine))
138 return 0;
139 if (meth->finish) meth->finish(rsa);
140 rsa->engine = engine;
141 meth = ENGINE_get_RSA(engine);
142 if (meth->init) meth->init(rsa);
143 /* SHOULD ERROR CHECK THIS!!! */
144 ENGINE_finish(mtmp);
145 return 1; 118 return 1;
146} 119 }
147#endif
148 120
149#if 0
150RSA *RSA_new_method(RSA_METHOD *meth)
151#else
152RSA *RSA_new_method(ENGINE *engine) 121RSA *RSA_new_method(ENGINE *engine)
153#endif
154 { 122 {
155 RSA_METHOD *meth;
156 RSA *ret; 123 RSA *ret;
157 124
158 ret=(RSA *)OPENSSL_malloc(sizeof(RSA)); 125 ret=(RSA *)OPENSSL_malloc(sizeof(RSA));
159 if (ret == NULL) 126 if (ret == NULL)
160 { 127 {
161 RSAerr(RSA_F_RSA_NEW_METHOD,ERR_R_MALLOC_FAILURE); 128 RSAerr(RSA_F_RSA_NEW_METHOD,ERR_R_MALLOC_FAILURE);
162 return(NULL); 129 return NULL;
163 } 130 }
164 131
165 if (engine == NULL) 132 ret->meth = RSA_get_default_method();
133 if (engine)
166 { 134 {
167 if((ret->engine=ENGINE_get_default_RSA()) == NULL) 135 if (!ENGINE_init(engine))
168 { 136 {
137 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
169 OPENSSL_free(ret); 138 OPENSSL_free(ret);
170 return NULL; 139 return NULL;
171 } 140 }
141 ret->engine = engine;
172 } 142 }
173 else 143 else
174 ret->engine=engine; 144 ret->engine = ENGINE_get_default_RSA();
175 meth = ENGINE_get_RSA(ret->engine); 145 if(ret->engine)
146 {
147 ret->meth = ENGINE_get_RSA(ret->engine);
148 if(!ret->meth)
149 {
150 RSAerr(RSA_F_RSA_NEW_METHOD,
151 ERR_R_ENGINE_LIB);
152 ENGINE_finish(ret->engine);
153 OPENSSL_free(ret);
154 return NULL;
155 }
156 }
176 157
177 ret->pad=0; 158 ret->pad=0;
178 ret->version=0; 159 ret->version=0;
@@ -190,11 +171,13 @@ RSA *RSA_new_method(ENGINE *engine)
190 ret->_method_mod_q=NULL; 171 ret->_method_mod_q=NULL;
191 ret->blinding=NULL; 172 ret->blinding=NULL;
192 ret->bignum_data=NULL; 173 ret->bignum_data=NULL;
193 ret->flags=meth->flags; 174 ret->flags=ret->meth->flags;
194 CRYPTO_new_ex_data(rsa_meth,ret,&ret->ex_data); 175 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data);
195 if ((meth->init != NULL) && !meth->init(ret)) 176 if ((ret->meth->init != NULL) && !ret->meth->init(ret))
196 { 177 {
197 CRYPTO_free_ex_data(rsa_meth,ret,&ret->ex_data); 178 if (ret->engine)
179 ENGINE_finish(ret->engine);
180 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data);
198 OPENSSL_free(ret); 181 OPENSSL_free(ret);
199 ret=NULL; 182 ret=NULL;
200 } 183 }
@@ -203,7 +186,6 @@ RSA *RSA_new_method(ENGINE *engine)
203 186
204void RSA_free(RSA *r) 187void RSA_free(RSA *r)
205 { 188 {
206 RSA_METHOD *meth;
207 int i; 189 int i;
208 190
209 if (r == NULL) return; 191 if (r == NULL) return;
@@ -221,12 +203,12 @@ void RSA_free(RSA *r)
221 } 203 }
222#endif 204#endif
223 205
224 meth = ENGINE_get_RSA(r->engine); 206 if (r->meth->finish)
225 if (meth->finish != NULL) 207 r->meth->finish(r);
226 meth->finish(r); 208 if (r->engine)
227 ENGINE_finish(r->engine); 209 ENGINE_finish(r->engine);
228 210
229 CRYPTO_free_ex_data(rsa_meth,r,&r->ex_data); 211 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
230 212
231 if (r->n != NULL) BN_clear_free(r->n); 213 if (r->n != NULL) BN_clear_free(r->n);
232 if (r->e != NULL) BN_clear_free(r->e); 214 if (r->e != NULL) BN_clear_free(r->e);
@@ -241,12 +223,27 @@ void RSA_free(RSA *r)
241 OPENSSL_free(r); 223 OPENSSL_free(r);
242 } 224 }
243 225
226int RSA_up_ref(RSA *r)
227 {
228 int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_RSA);
229#ifdef REF_PRINT
230 REF_PRINT("RSA",r);
231#endif
232#ifdef REF_CHECK
233 if (i < 2)
234 {
235 fprintf(stderr, "RSA_up_ref, bad reference count\n");
236 abort();
237 }
238#endif
239 return ((i > 1) ? 1 : 0);
240 }
241
244int RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, 242int RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
245 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) 243 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
246 { 244 {
247 rsa_meth_num++; 245 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_RSA, argl, argp,
248 return(CRYPTO_get_ex_new_index(rsa_meth_num-1, 246 new_func, dup_func, free_func);
249 &rsa_meth,argl,argp,new_func,dup_func,free_func));
250 } 247 }
251 248
252int RSA_set_ex_data(RSA *r, int idx, void *arg) 249int RSA_set_ex_data(RSA *r, int idx, void *arg)
@@ -254,47 +251,43 @@ int RSA_set_ex_data(RSA *r, int idx, void *arg)
254 return(CRYPTO_set_ex_data(&r->ex_data,idx,arg)); 251 return(CRYPTO_set_ex_data(&r->ex_data,idx,arg));
255 } 252 }
256 253
257void *RSA_get_ex_data(RSA *r, int idx) 254void *RSA_get_ex_data(const RSA *r, int idx)
258 { 255 {
259 return(CRYPTO_get_ex_data(&r->ex_data,idx)); 256 return(CRYPTO_get_ex_data(&r->ex_data,idx));
260 } 257 }
261 258
262int RSA_size(RSA *r) 259int RSA_size(const RSA *r)
263 { 260 {
264 return(BN_num_bytes(r->n)); 261 return(BN_num_bytes(r->n));
265 } 262 }
266 263
267int RSA_public_encrypt(int flen, unsigned char *from, unsigned char *to, 264int RSA_public_encrypt(int flen, const unsigned char *from, unsigned char *to,
268 RSA *rsa, int padding) 265 RSA *rsa, int padding)
269 { 266 {
270 return(ENGINE_get_RSA(rsa->engine)->rsa_pub_enc(flen, 267 return(rsa->meth->rsa_pub_enc(flen, from, to, rsa, padding));
271 from, to, rsa, padding));
272 } 268 }
273 269
274int RSA_private_encrypt(int flen, unsigned char *from, unsigned char *to, 270int RSA_private_encrypt(int flen, const unsigned char *from, unsigned char *to,
275 RSA *rsa, int padding) 271 RSA *rsa, int padding)
276 { 272 {
277 return(ENGINE_get_RSA(rsa->engine)->rsa_priv_enc(flen, 273 return(rsa->meth->rsa_priv_enc(flen, from, to, rsa, padding));
278 from, to, rsa, padding));
279 } 274 }
280 275
281int RSA_private_decrypt(int flen, unsigned char *from, unsigned char *to, 276int RSA_private_decrypt(int flen, const unsigned char *from, unsigned char *to,
282 RSA *rsa, int padding) 277 RSA *rsa, int padding)
283 { 278 {
284 return(ENGINE_get_RSA(rsa->engine)->rsa_priv_dec(flen, 279 return(rsa->meth->rsa_priv_dec(flen, from, to, rsa, padding));
285 from, to, rsa, padding));
286 } 280 }
287 281
288int RSA_public_decrypt(int flen, unsigned char *from, unsigned char *to, 282int RSA_public_decrypt(int flen, const unsigned char *from, unsigned char *to,
289 RSA *rsa, int padding) 283 RSA *rsa, int padding)
290 { 284 {
291 return(ENGINE_get_RSA(rsa->engine)->rsa_pub_dec(flen, 285 return(rsa->meth->rsa_pub_dec(flen, from, to, rsa, padding));
292 from, to, rsa, padding));
293 } 286 }
294 287
295int RSA_flags(RSA *r) 288int RSA_flags(const RSA *r)
296 { 289 {
297 return((r == NULL)?0:ENGINE_get_RSA(r->engine)->flags); 290 return((r == NULL)?0:r->meth->flags);
298 } 291 }
299 292
300void RSA_blinding_off(RSA *rsa) 293void RSA_blinding_off(RSA *rsa)
@@ -328,8 +321,7 @@ int RSA_blinding_on(RSA *rsa, BN_CTX *p_ctx)
328 if (!BN_rand_range(A,rsa->n)) goto err; 321 if (!BN_rand_range(A,rsa->n)) goto err;
329 if ((Ai=BN_mod_inverse(NULL,A,rsa->n,ctx)) == NULL) goto err; 322 if ((Ai=BN_mod_inverse(NULL,A,rsa->n,ctx)) == NULL) goto err;
330 323
331 if (!ENGINE_get_RSA(rsa->engine)->bn_mod_exp(A,A, 324 if (!rsa->meth->bn_mod_exp(A,A,rsa->e,rsa->n,ctx,rsa->_method_mod_n))
332 rsa->e,rsa->n,ctx,rsa->_method_mod_n))
333 goto err; 325 goto err;
334 rsa->blinding=BN_BLINDING_new(A,Ai,rsa->n); 326 rsa->blinding=BN_BLINDING_new(A,Ai,rsa->n);
335 rsa->flags|=RSA_FLAG_BLINDING; 327 rsa->flags|=RSA_FLAG_BLINDING;
@@ -385,4 +377,3 @@ int RSA_memory_lock(RSA *r)
385 r->bignum_data=p; 377 r->bignum_data=p;
386 return(1); 378 return(1);
387 } 379 }
388