summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/rsa
diff options
context:
space:
mode:
authormarkus <>2003-05-12 02:18:40 +0000
committermarkus <>2003-05-12 02:18:40 +0000
commitd4fcd82bb7f6d603bd61e19a81ba97337b89dfca (patch)
treed52e3a0f1f08f65ad283027e560e17ed0d720462 /src/lib/libcrypto/rsa
parent582bbd139cd2afd58d10dc051c5b0b989b441074 (diff)
downloadopenbsd-d4fcd82bb7f6d603bd61e19a81ba97337b89dfca.tar.gz
openbsd-d4fcd82bb7f6d603bd61e19a81ba97337b89dfca.tar.bz2
openbsd-d4fcd82bb7f6d603bd61e19a81ba97337b89dfca.zip
merge 0.9.7b with local changes; crank majors for libssl/libcrypto
Diffstat (limited to 'src/lib/libcrypto/rsa')
-rw-r--r--src/lib/libcrypto/rsa/Makefile.ssl44
-rw-r--r--src/lib/libcrypto/rsa/rsa.h9
-rw-r--r--src/lib/libcrypto/rsa/rsa_eay.c131
-rw-r--r--src/lib/libcrypto/rsa/rsa_lib.c43
-rw-r--r--src/lib/libcrypto/rsa/rsa_pk1.c2
-rw-r--r--src/lib/libcrypto/rsa/rsa_saos.c6
-rw-r--r--src/lib/libcrypto/rsa/rsa_sign.c25
-rw-r--r--src/lib/libcrypto/rsa/rsa_test.c1
8 files changed, 195 insertions, 66 deletions
diff --git a/src/lib/libcrypto/rsa/Makefile.ssl b/src/lib/libcrypto/rsa/Makefile.ssl
index 8c44b7f142..8089344a04 100644
--- a/src/lib/libcrypto/rsa/Makefile.ssl
+++ b/src/lib/libcrypto/rsa/Makefile.ssl
@@ -72,7 +72,7 @@ lint:
72 lint -DLINT $(INCLUDES) $(SRC)>fluff 72 lint -DLINT $(INCLUDES) $(SRC)>fluff
73 73
74depend: 74depend:
75 $(MAKEDEPEND) $(CFLAG) $(INCLUDES) $(DEPFLAG) $(PROGS) $(LIBSRC) 75 $(MAKEDEPEND) -- $(CFLAG) $(INCLUDES) $(DEPFLAG) -- $(PROGS) $(LIBSRC)
76 76
77dclean: 77dclean:
78 $(PERL) -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new 78 $(PERL) -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
@@ -104,14 +104,12 @@ rsa_chk.o: rsa_chk.c
104rsa_eay.o: ../../e_os.h ../../include/openssl/asn1.h 104rsa_eay.o: ../../e_os.h ../../include/openssl/asn1.h
105rsa_eay.o: ../../include/openssl/bio.h ../../include/openssl/bn.h 105rsa_eay.o: ../../include/openssl/bio.h ../../include/openssl/bn.h
106rsa_eay.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h 106rsa_eay.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h
107rsa_eay.o: ../../include/openssl/dh.h ../../include/openssl/dsa.h 107rsa_eay.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h
108rsa_eay.o: ../../include/openssl/e_os2.h ../../include/openssl/engine.h 108rsa_eay.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h
109rsa_eay.o: ../../include/openssl/err.h ../../include/openssl/lhash.h 109rsa_eay.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
110rsa_eay.o: ../../include/openssl/opensslconf.h ../../include/openssl/opensslv.h 110rsa_eay.o: ../../include/openssl/rand.h ../../include/openssl/rsa.h
111rsa_eay.o: ../../include/openssl/ossl_typ.h ../../include/openssl/rand.h 111rsa_eay.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h
112rsa_eay.o: ../../include/openssl/rsa.h ../../include/openssl/safestack.h 112rsa_eay.o: ../../include/openssl/symhacks.h ../cryptlib.h rsa_eay.c
113rsa_eay.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h
114rsa_eay.o: ../../include/openssl/ui.h ../cryptlib.h rsa_eay.c
115rsa_err.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h 113rsa_err.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h
116rsa_err.o: ../../include/openssl/bn.h ../../include/openssl/crypto.h 114rsa_err.o: ../../include/openssl/bn.h ../../include/openssl/crypto.h
117rsa_err.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h 115rsa_err.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h
@@ -217,21 +215,21 @@ rsa_sign.o: ../../include/openssl/buffer.h ../../include/openssl/cast.h
217rsa_sign.o: ../../include/openssl/crypto.h ../../include/openssl/des.h 215rsa_sign.o: ../../include/openssl/crypto.h ../../include/openssl/des.h
218rsa_sign.o: ../../include/openssl/des_old.h ../../include/openssl/dh.h 216rsa_sign.o: ../../include/openssl/des_old.h ../../include/openssl/dh.h
219rsa_sign.o: ../../include/openssl/dsa.h ../../include/openssl/e_os2.h 217rsa_sign.o: ../../include/openssl/dsa.h ../../include/openssl/e_os2.h
220rsa_sign.o: ../../include/openssl/engine.h ../../include/openssl/err.h 218rsa_sign.o: ../../include/openssl/err.h ../../include/openssl/evp.h
221rsa_sign.o: ../../include/openssl/evp.h ../../include/openssl/idea.h 219rsa_sign.o: ../../include/openssl/idea.h ../../include/openssl/lhash.h
222rsa_sign.o: ../../include/openssl/lhash.h ../../include/openssl/md2.h 220rsa_sign.o: ../../include/openssl/md2.h ../../include/openssl/md4.h
223rsa_sign.o: ../../include/openssl/md4.h ../../include/openssl/md5.h 221rsa_sign.o: ../../include/openssl/md5.h ../../include/openssl/mdc2.h
224rsa_sign.o: ../../include/openssl/mdc2.h ../../include/openssl/obj_mac.h 222rsa_sign.o: ../../include/openssl/obj_mac.h ../../include/openssl/objects.h
225rsa_sign.o: ../../include/openssl/objects.h ../../include/openssl/opensslconf.h 223rsa_sign.o: ../../include/openssl/opensslconf.h
226rsa_sign.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h 224rsa_sign.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
227rsa_sign.o: ../../include/openssl/pkcs7.h ../../include/openssl/rand.h 225rsa_sign.o: ../../include/openssl/pkcs7.h ../../include/openssl/rc2.h
228rsa_sign.o: ../../include/openssl/rc2.h ../../include/openssl/rc4.h 226rsa_sign.o: ../../include/openssl/rc4.h ../../include/openssl/rc5.h
229rsa_sign.o: ../../include/openssl/rc5.h ../../include/openssl/ripemd.h 227rsa_sign.o: ../../include/openssl/ripemd.h ../../include/openssl/rsa.h
230rsa_sign.o: ../../include/openssl/rsa.h ../../include/openssl/safestack.h 228rsa_sign.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h
231rsa_sign.o: ../../include/openssl/sha.h ../../include/openssl/stack.h 229rsa_sign.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h
232rsa_sign.o: ../../include/openssl/symhacks.h ../../include/openssl/ui.h 230rsa_sign.o: ../../include/openssl/ui.h ../../include/openssl/ui_compat.h
233rsa_sign.o: ../../include/openssl/ui_compat.h ../../include/openssl/x509.h 231rsa_sign.o: ../../include/openssl/x509.h ../../include/openssl/x509_vfy.h
234rsa_sign.o: ../../include/openssl/x509_vfy.h ../cryptlib.h rsa_sign.c 232rsa_sign.o: ../cryptlib.h rsa_sign.c
235rsa_ssl.o: ../../e_os.h ../../include/openssl/asn1.h 233rsa_ssl.o: ../../e_os.h ../../include/openssl/asn1.h
236rsa_ssl.o: ../../include/openssl/bio.h ../../include/openssl/bn.h 234rsa_ssl.o: ../../include/openssl/bio.h ../../include/openssl/bn.h
237rsa_ssl.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h 235rsa_ssl.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h
diff --git a/src/lib/libcrypto/rsa/rsa.h b/src/lib/libcrypto/rsa/rsa.h
index 98b3bd7cc5..e26a68b482 100644
--- a/src/lib/libcrypto/rsa/rsa.h
+++ b/src/lib/libcrypto/rsa/rsa.h
@@ -158,6 +158,11 @@ struct rsa_st
158#define RSA_FLAG_CACHE_PUBLIC 0x02 158#define RSA_FLAG_CACHE_PUBLIC 0x02
159#define RSA_FLAG_CACHE_PRIVATE 0x04 159#define RSA_FLAG_CACHE_PRIVATE 0x04
160#define RSA_FLAG_BLINDING 0x08 160#define RSA_FLAG_BLINDING 0x08
161#define RSA_FLAG_NO_BLINDING 0x80 /* new with 0.9.6j and 0.9.7b; the built-in
162 * RSA implementation now uses blinding by
163 * default (ignoring RSA_FLAG_BLINDING),
164 * but other engines might not need it
165 */
161#define RSA_FLAG_THREAD_SAFE 0x10 166#define RSA_FLAG_THREAD_SAFE 0x10
162/* This flag means the private key operations will be handled by rsa_mod_exp 167/* This flag means the private key operations will be handled by rsa_mod_exp
163 * and that they do not depend on the private key components being present: 168 * and that they do not depend on the private key components being present:
@@ -170,11 +175,15 @@ struct rsa_st
170 */ 175 */
171#define RSA_FLAG_SIGN_VER 0x40 176#define RSA_FLAG_SIGN_VER 0x40
172 177
178#define RSA_FLAG_NO_BLINDING 0x80
179
173#define RSA_PKCS1_PADDING 1 180#define RSA_PKCS1_PADDING 1
174#define RSA_SSLV23_PADDING 2 181#define RSA_SSLV23_PADDING 2
175#define RSA_NO_PADDING 3 182#define RSA_NO_PADDING 3
176#define RSA_PKCS1_OAEP_PADDING 4 183#define RSA_PKCS1_OAEP_PADDING 4
177 184
185#define RSA_PKCS1_PADDING_SIZE 11
186
178#define RSA_set_app_data(s,arg) RSA_set_ex_data(s,0,arg) 187#define RSA_set_app_data(s,arg) RSA_set_ex_data(s,0,arg)
179#define RSA_get_app_data(s) RSA_get_ex_data(s,0) 188#define RSA_get_app_data(s) RSA_get_ex_data(s,0)
180 189
diff --git a/src/lib/libcrypto/rsa/rsa_eay.c b/src/lib/libcrypto/rsa/rsa_eay.c
index a3f549d8e6..027b4dc754 100644
--- a/src/lib/libcrypto/rsa/rsa_eay.c
+++ b/src/lib/libcrypto/rsa/rsa_eay.c
@@ -61,7 +61,6 @@
61#include <openssl/bn.h> 61#include <openssl/bn.h>
62#include <openssl/rsa.h> 62#include <openssl/rsa.h>
63#include <openssl/rand.h> 63#include <openssl/rand.h>
64#include <openssl/engine.h>
65 64
66#ifndef RSA_NULL 65#ifndef RSA_NULL
67 66
@@ -187,7 +186,7 @@ err:
187 BN_clear_free(&ret); 186 BN_clear_free(&ret);
188 if (buf != NULL) 187 if (buf != NULL)
189 { 188 {
190 memset(buf,0,num); 189 OPENSSL_cleanse(buf,num);
191 OPENSSL_free(buf); 190 OPENSSL_free(buf);
192 } 191 }
193 return(r); 192 return(r);
@@ -206,12 +205,46 @@ static int rsa_eay_blinding(RSA *rsa, BN_CTX *ctx)
206 205
207#define BLINDING_HELPER(rsa, ctx, err_instr) \ 206#define BLINDING_HELPER(rsa, ctx, err_instr) \
208 do { \ 207 do { \
209 if(((rsa)->flags & RSA_FLAG_BLINDING) && \ 208 if((!((rsa)->flags & RSA_FLAG_NO_BLINDING)) && \
210 ((rsa)->blinding == NULL) && \ 209 ((rsa)->blinding == NULL) && \
211 !rsa_eay_blinding(rsa, ctx)) \ 210 !rsa_eay_blinding(rsa, ctx)) \
212 err_instr \ 211 err_instr \
213 } while(0) 212 } while(0)
214 213
214static BN_BLINDING *setup_blinding(RSA *rsa, BN_CTX *ctx)
215 {
216 BIGNUM *A, *Ai;
217 BN_BLINDING *ret = NULL;
218
219 /* added in OpenSSL 0.9.6j and 0.9.7b */
220
221 /* NB: similar code appears in RSA_blinding_on (rsa_lib.c);
222 * this should be placed in a new function of its own, but for reasons
223 * of binary compatibility can't */
224
225 BN_CTX_start(ctx);
226 A = BN_CTX_get(ctx);
227 if ((RAND_status() == 0) && rsa->d != NULL && rsa->d->d != NULL)
228 {
229 /* if PRNG is not properly seeded, resort to secret exponent as unpredictable seed */
230 RAND_add(rsa->d->d, rsa->d->dmax * sizeof rsa->d->d[0], 0);
231 if (!BN_pseudo_rand_range(A,rsa->n)) goto err;
232 }
233 else
234 {
235 if (!BN_rand_range(A,rsa->n)) goto err;
236 }
237 if ((Ai=BN_mod_inverse(NULL,A,rsa->n,ctx)) == NULL) goto err;
238
239 if (!rsa->meth->bn_mod_exp(A,A,rsa->e,rsa->n,ctx,rsa->_method_mod_n))
240 goto err;
241 ret = BN_BLINDING_new(A,Ai,rsa->n);
242 BN_free(Ai);
243err:
244 BN_CTX_end(ctx);
245 return ret;
246 }
247
215/* signing */ 248/* signing */
216static int RSA_eay_private_encrypt(int flen, const unsigned char *from, 249static int RSA_eay_private_encrypt(int flen, const unsigned char *from,
217 unsigned char *to, RSA *rsa, int padding) 250 unsigned char *to, RSA *rsa, int padding)
@@ -220,6 +253,8 @@ static int RSA_eay_private_encrypt(int flen, const unsigned char *from,
220 int i,j,k,num=0,r= -1; 253 int i,j,k,num=0,r= -1;
221 unsigned char *buf=NULL; 254 unsigned char *buf=NULL;
222 BN_CTX *ctx=NULL; 255 BN_CTX *ctx=NULL;
256 int local_blinding = 0;
257 BN_BLINDING *blinding = NULL;
223 258
224 BN_init(&f); 259 BN_init(&f);
225 BN_init(&ret); 260 BN_init(&ret);
@@ -257,9 +292,38 @@ static int RSA_eay_private_encrypt(int flen, const unsigned char *from,
257 } 292 }
258 293
259 BLINDING_HELPER(rsa, ctx, goto err;); 294 BLINDING_HELPER(rsa, ctx, goto err;);
295 blinding = rsa->blinding;
296
297 /* Now unless blinding is disabled, 'blinding' is non-NULL.
298 * But the BN_BLINDING object may be owned by some other thread
299 * (we don't want to keep it constant and we don't want to use
300 * lots of locking to avoid race conditions, so only a single
301 * thread can use it; other threads have to use local blinding
302 * factors) */
303 if (!(rsa->flags & RSA_FLAG_NO_BLINDING))
304 {
305 if (blinding == NULL)
306 {
307 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, ERR_R_INTERNAL_ERROR);
308 goto err;
309 }
310 }
311
312 if (blinding != NULL)
313 {
314 if (blinding->thread_id != CRYPTO_thread_id())
315 {
316 /* we need a local one-time blinding factor */
317
318 blinding = setup_blinding(rsa, ctx);
319 if (blinding == NULL)
320 goto err;
321 local_blinding = 1;
322 }
323 }
260 324
261 if (rsa->flags & RSA_FLAG_BLINDING) 325 if (blinding)
262 if (!BN_BLINDING_convert(&f,rsa->blinding,ctx)) goto err; 326 if (!BN_BLINDING_convert(&f, blinding, ctx)) goto err;
263 327
264 if ( (rsa->flags & RSA_FLAG_EXT_PKEY) || 328 if ( (rsa->flags & RSA_FLAG_EXT_PKEY) ||
265 ((rsa->p != NULL) && 329 ((rsa->p != NULL) &&
@@ -273,8 +337,8 @@ static int RSA_eay_private_encrypt(int flen, const unsigned char *from,
273 if (!rsa->meth->bn_mod_exp(&ret,&f,rsa->d,rsa->n,ctx,NULL)) goto err; 337 if (!rsa->meth->bn_mod_exp(&ret,&f,rsa->d,rsa->n,ctx,NULL)) goto err;
274 } 338 }
275 339
276 if (rsa->flags & RSA_FLAG_BLINDING) 340 if (blinding)
277 if (!BN_BLINDING_invert(&ret,rsa->blinding,ctx)) goto err; 341 if (!BN_BLINDING_invert(&ret, blinding, ctx)) goto err;
278 342
279 /* put in leading 0 bytes if the number is less than the 343 /* put in leading 0 bytes if the number is less than the
280 * length of the modulus */ 344 * length of the modulus */
@@ -288,9 +352,11 @@ err:
288 if (ctx != NULL) BN_CTX_free(ctx); 352 if (ctx != NULL) BN_CTX_free(ctx);
289 BN_clear_free(&ret); 353 BN_clear_free(&ret);
290 BN_clear_free(&f); 354 BN_clear_free(&f);
355 if (local_blinding)
356 BN_BLINDING_free(blinding);
291 if (buf != NULL) 357 if (buf != NULL)
292 { 358 {
293 memset(buf,0,num); 359 OPENSSL_cleanse(buf,num);
294 OPENSSL_free(buf); 360 OPENSSL_free(buf);
295 } 361 }
296 return(r); 362 return(r);
@@ -304,6 +370,8 @@ static int RSA_eay_private_decrypt(int flen, const unsigned char *from,
304 unsigned char *p; 370 unsigned char *p;
305 unsigned char *buf=NULL; 371 unsigned char *buf=NULL;
306 BN_CTX *ctx=NULL; 372 BN_CTX *ctx=NULL;
373 int local_blinding = 0;
374 BN_BLINDING *blinding = NULL;
307 375
308 BN_init(&f); 376 BN_init(&f);
309 BN_init(&ret); 377 BN_init(&ret);
@@ -336,9 +404,38 @@ static int RSA_eay_private_decrypt(int flen, const unsigned char *from,
336 } 404 }
337 405
338 BLINDING_HELPER(rsa, ctx, goto err;); 406 BLINDING_HELPER(rsa, ctx, goto err;);
407 blinding = rsa->blinding;
408
409 /* Now unless blinding is disabled, 'blinding' is non-NULL.
410 * But the BN_BLINDING object may be owned by some other thread
411 * (we don't want to keep it constant and we don't want to use
412 * lots of locking to avoid race conditions, so only a single
413 * thread can use it; other threads have to use local blinding
414 * factors) */
415 if (!(rsa->flags & RSA_FLAG_NO_BLINDING))
416 {
417 if (blinding == NULL)
418 {
419 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT, ERR_R_INTERNAL_ERROR);
420 goto err;
421 }
422 }
423
424 if (blinding != NULL)
425 {
426 if (blinding->thread_id != CRYPTO_thread_id())
427 {
428 /* we need a local one-time blinding factor */
429
430 blinding = setup_blinding(rsa, ctx);
431 if (blinding == NULL)
432 goto err;
433 local_blinding = 1;
434 }
435 }
339 436
340 if (rsa->flags & RSA_FLAG_BLINDING) 437 if (blinding)
341 if (!BN_BLINDING_convert(&f,rsa->blinding,ctx)) goto err; 438 if (!BN_BLINDING_convert(&f, blinding, ctx)) goto err;
342 439
343 /* do the decrypt */ 440 /* do the decrypt */
344 if ( (rsa->flags & RSA_FLAG_EXT_PKEY) || 441 if ( (rsa->flags & RSA_FLAG_EXT_PKEY) ||
@@ -354,8 +451,8 @@ static int RSA_eay_private_decrypt(int flen, const unsigned char *from,
354 goto err; 451 goto err;
355 } 452 }
356 453
357 if (rsa->flags & RSA_FLAG_BLINDING) 454 if (blinding)
358 if (!BN_BLINDING_invert(&ret,rsa->blinding,ctx)) goto err; 455 if (!BN_BLINDING_invert(&ret, blinding, ctx)) goto err;
359 456
360 p=buf; 457 p=buf;
361 j=BN_bn2bin(&ret,p); /* j is only used with no-padding mode */ 458 j=BN_bn2bin(&ret,p); /* j is only used with no-padding mode */
@@ -389,7 +486,7 @@ err:
389 BN_clear_free(&ret); 486 BN_clear_free(&ret);
390 if (buf != NULL) 487 if (buf != NULL)
391 { 488 {
392 memset(buf,0,num); 489 OPENSSL_cleanse(buf,num);
393 OPENSSL_free(buf); 490 OPENSSL_free(buf);
394 } 491 }
395 return(r); 492 return(r);
@@ -486,7 +583,7 @@ err:
486 BN_clear_free(&ret); 583 BN_clear_free(&ret);
487 if (buf != NULL) 584 if (buf != NULL)
488 { 585 {
489 memset(buf,0,num); 586 OPENSSL_cleanse(buf,num);
490 OPENSSL_free(buf); 587 OPENSSL_free(buf);
491 } 588 }
492 return(r); 589 return(r);
diff --git a/src/lib/libcrypto/rsa/rsa_lib.c b/src/lib/libcrypto/rsa/rsa_lib.c
index 37fff8bce3..53c5092014 100644
--- a/src/lib/libcrypto/rsa/rsa_lib.c
+++ b/src/lib/libcrypto/rsa/rsa_lib.c
@@ -62,7 +62,10 @@
62#include <openssl/lhash.h> 62#include <openssl/lhash.h>
63#include <openssl/bn.h> 63#include <openssl/bn.h>
64#include <openssl/rsa.h> 64#include <openssl/rsa.h>
65#include <openssl/rand.h>
66#ifndef OPENSSL_NO_ENGINE
65#include <openssl/engine.h> 67#include <openssl/engine.h>
68#endif
66 69
67const char *RSA_version="RSA" OPENSSL_VERSION_PTEXT; 70const char *RSA_version="RSA" OPENSSL_VERSION_PTEXT;
68 71
@@ -72,10 +75,6 @@ RSA *RSA_new(void)
72 { 75 {
73 RSA *r=RSA_new_method(NULL); 76 RSA *r=RSA_new_method(NULL);
74 77
75#ifndef OPENSSL_NO_FORCE_RSA_BLINDING
76 r->flags|=RSA_FLAG_BLINDING;
77#endif
78
79 return r; 78 return r;
80 } 79 }
81 80
@@ -114,11 +113,13 @@ int RSA_set_method(RSA *rsa, const RSA_METHOD *meth)
114 const RSA_METHOD *mtmp; 113 const RSA_METHOD *mtmp;
115 mtmp = rsa->meth; 114 mtmp = rsa->meth;
116 if (mtmp->finish) mtmp->finish(rsa); 115 if (mtmp->finish) mtmp->finish(rsa);
116#ifndef OPENSSL_NO_ENGINE
117 if (rsa->engine) 117 if (rsa->engine)
118 { 118 {
119 ENGINE_finish(rsa->engine); 119 ENGINE_finish(rsa->engine);
120 rsa->engine = NULL; 120 rsa->engine = NULL;
121 } 121 }
122#endif
122 rsa->meth = meth; 123 rsa->meth = meth;
123 if (meth->init) meth->init(rsa); 124 if (meth->init) meth->init(rsa);
124 return 1; 125 return 1;
@@ -136,6 +137,7 @@ RSA *RSA_new_method(ENGINE *engine)
136 } 137 }
137 138
138 ret->meth = RSA_get_default_method(); 139 ret->meth = RSA_get_default_method();
140#ifndef OPENSSL_NO_ENGINE
139 if (engine) 141 if (engine)
140 { 142 {
141 if (!ENGINE_init(engine)) 143 if (!ENGINE_init(engine))
@@ -160,6 +162,7 @@ RSA *RSA_new_method(ENGINE *engine)
160 return NULL; 162 return NULL;
161 } 163 }
162 } 164 }
165#endif
163 166
164 ret->pad=0; 167 ret->pad=0;
165 ret->version=0; 168 ret->version=0;
@@ -181,8 +184,10 @@ RSA *RSA_new_method(ENGINE *engine)
181 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data); 184 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data);
182 if ((ret->meth->init != NULL) && !ret->meth->init(ret)) 185 if ((ret->meth->init != NULL) && !ret->meth->init(ret))
183 { 186 {
187#ifndef OPENSSL_NO_ENGINE
184 if (ret->engine) 188 if (ret->engine)
185 ENGINE_finish(ret->engine); 189 ENGINE_finish(ret->engine);
190#endif
186 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data); 191 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data);
187 OPENSSL_free(ret); 192 OPENSSL_free(ret);
188 ret=NULL; 193 ret=NULL;
@@ -211,8 +216,10 @@ void RSA_free(RSA *r)
211 216
212 if (r->meth->finish) 217 if (r->meth->finish)
213 r->meth->finish(r); 218 r->meth->finish(r);
219#ifndef OPENSSL_NO_ENGINE
214 if (r->engine) 220 if (r->engine)
215 ENGINE_finish(r->engine); 221 ENGINE_finish(r->engine);
222#endif
216 223
217 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data); 224 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
218 225
@@ -303,7 +310,8 @@ void RSA_blinding_off(RSA *rsa)
303 BN_BLINDING_free(rsa->blinding); 310 BN_BLINDING_free(rsa->blinding);
304 rsa->blinding=NULL; 311 rsa->blinding=NULL;
305 } 312 }
306 rsa->flags&= ~RSA_FLAG_BLINDING; 313 rsa->flags &= ~RSA_FLAG_BLINDING;
314 rsa->flags |= RSA_FLAG_NO_BLINDING;
307 } 315 }
308 316
309int RSA_blinding_on(RSA *rsa, BN_CTX *p_ctx) 317int RSA_blinding_on(RSA *rsa, BN_CTX *p_ctx)
@@ -322,15 +330,32 @@ int RSA_blinding_on(RSA *rsa, BN_CTX *p_ctx)
322 if (rsa->blinding != NULL) 330 if (rsa->blinding != NULL)
323 BN_BLINDING_free(rsa->blinding); 331 BN_BLINDING_free(rsa->blinding);
324 332
333 /* NB: similar code appears in setup_blinding (rsa_eay.c);
334 * this should be placed in a new function of its own, but for reasons
335 * of binary compatibility can't */
336
325 BN_CTX_start(ctx); 337 BN_CTX_start(ctx);
326 A = BN_CTX_get(ctx); 338 A = BN_CTX_get(ctx);
327 if (!BN_rand_range(A,rsa->n)) goto err; 339 if ((RAND_status() == 0) && rsa->d != NULL && rsa->d->d != NULL)
340 {
341 /* if PRNG is not properly seeded, resort to secret exponent as unpredictable seed */
342 RAND_add(rsa->d->d, rsa->d->dmax * sizeof rsa->d->d[0], 0);
343 if (!BN_pseudo_rand_range(A,rsa->n)) goto err;
344 }
345 else
346 {
347 if (!BN_rand_range(A,rsa->n)) goto err;
348 }
328 if ((Ai=BN_mod_inverse(NULL,A,rsa->n,ctx)) == NULL) goto err; 349 if ((Ai=BN_mod_inverse(NULL,A,rsa->n,ctx)) == NULL) goto err;
329 350
330 if (!rsa->meth->bn_mod_exp(A,A,rsa->e,rsa->n,ctx,rsa->_method_mod_n)) 351 if (!rsa->meth->bn_mod_exp(A,A,rsa->e,rsa->n,ctx,rsa->_method_mod_n))
331 goto err; 352 goto err;
332 rsa->blinding=BN_BLINDING_new(A,Ai,rsa->n); 353 if ((rsa->blinding=BN_BLINDING_new(A,Ai,rsa->n)) == NULL) goto err;
333 rsa->flags|=RSA_FLAG_BLINDING; 354 /* to make things thread-safe without excessive locking,
355 * rsa->blinding will be used just by the current thread: */
356 rsa->blinding->thread_id = CRYPTO_thread_id();
357 rsa->flags |= RSA_FLAG_BLINDING;
358 rsa->flags &= ~RSA_FLAG_NO_BLINDING;
334 BN_free(Ai); 359 BN_free(Ai);
335 ret=1; 360 ret=1;
336err: 361err:
diff --git a/src/lib/libcrypto/rsa/rsa_pk1.c b/src/lib/libcrypto/rsa/rsa_pk1.c
index c1edd6764f..8560755f1d 100644
--- a/src/lib/libcrypto/rsa/rsa_pk1.c
+++ b/src/lib/libcrypto/rsa/rsa_pk1.c
@@ -68,7 +68,7 @@ int RSA_padding_add_PKCS1_type_1(unsigned char *to, int tlen,
68 int j; 68 int j;
69 unsigned char *p; 69 unsigned char *p;
70 70
71 if (flen > (tlen-11)) 71 if (flen > (tlen-RSA_PKCS1_PADDING_SIZE))
72 { 72 {
73 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_1,RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); 73 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_1,RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
74 return(0); 74 return(0);
diff --git a/src/lib/libcrypto/rsa/rsa_saos.c b/src/lib/libcrypto/rsa/rsa_saos.c
index 85adacc08f..f462716a57 100644
--- a/src/lib/libcrypto/rsa/rsa_saos.c
+++ b/src/lib/libcrypto/rsa/rsa_saos.c
@@ -77,7 +77,7 @@ int RSA_sign_ASN1_OCTET_STRING(int type,
77 77
78 i=i2d_ASN1_OCTET_STRING(&sig,NULL); 78 i=i2d_ASN1_OCTET_STRING(&sig,NULL);
79 j=RSA_size(rsa); 79 j=RSA_size(rsa);
80 if ((i-RSA_PKCS1_PADDING) > j) 80 if (i > (j-RSA_PKCS1_PADDING_SIZE))
81 { 81 {
82 RSAerr(RSA_F_RSA_SIGN_ASN1_OCTET_STRING,RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY); 82 RSAerr(RSA_F_RSA_SIGN_ASN1_OCTET_STRING,RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY);
83 return(0); 83 return(0);
@@ -96,7 +96,7 @@ int RSA_sign_ASN1_OCTET_STRING(int type,
96 else 96 else
97 *siglen=i; 97 *siglen=i;
98 98
99 memset(s,0,(unsigned int)j+1); 99 OPENSSL_cleanse(s,(unsigned int)j+1);
100 OPENSSL_free(s); 100 OPENSSL_free(s);
101 return(ret); 101 return(ret);
102 } 102 }
@@ -139,7 +139,7 @@ int RSA_verify_ASN1_OCTET_STRING(int dtype,
139 ret=1; 139 ret=1;
140err: 140err:
141 if (sig != NULL) M_ASN1_OCTET_STRING_free(sig); 141 if (sig != NULL) M_ASN1_OCTET_STRING_free(sig);
142 memset(s,0,(unsigned int)siglen); 142 OPENSSL_cleanse(s,(unsigned int)siglen);
143 OPENSSL_free(s); 143 OPENSSL_free(s);
144 return(ret); 144 return(ret);
145 } 145 }
diff --git a/src/lib/libcrypto/rsa/rsa_sign.c b/src/lib/libcrypto/rsa/rsa_sign.c
index 2a440901de..8a1e642183 100644
--- a/src/lib/libcrypto/rsa/rsa_sign.c
+++ b/src/lib/libcrypto/rsa/rsa_sign.c
@@ -62,7 +62,6 @@
62#include <openssl/rsa.h> 62#include <openssl/rsa.h>
63#include <openssl/objects.h> 63#include <openssl/objects.h>
64#include <openssl/x509.h> 64#include <openssl/x509.h>
65#include <openssl/engine.h>
66 65
67/* Size of an SSL signature: MD5+SHA1 */ 66/* Size of an SSL signature: MD5+SHA1 */
68#define SSL_SIG_LENGTH 36 67#define SSL_SIG_LENGTH 36
@@ -77,10 +76,11 @@ int RSA_sign(int type, const unsigned char *m, unsigned int m_len,
77 const unsigned char *s = NULL; 76 const unsigned char *s = NULL;
78 X509_ALGOR algor; 77 X509_ALGOR algor;
79 ASN1_OCTET_STRING digest; 78 ASN1_OCTET_STRING digest;
80 if((rsa->flags & RSA_FLAG_SIGN_VER) 79 if((rsa->flags & RSA_FLAG_SIGN_VER) && rsa->meth->rsa_sign)
81 && ENGINE_get_RSA(rsa->engine)->rsa_sign) 80 {
82 return ENGINE_get_RSA(rsa->engine)->rsa_sign(type, 81 return rsa->meth->rsa_sign(type, m, m_len,
83 m, m_len, sigret, siglen, rsa); 82 sigret, siglen, rsa);
83 }
84 /* Special case: SSL signature, just check the length */ 84 /* Special case: SSL signature, just check the length */
85 if(type == NID_md5_sha1) { 85 if(type == NID_md5_sha1) {
86 if(m_len != SSL_SIG_LENGTH) { 86 if(m_len != SSL_SIG_LENGTH) {
@@ -113,7 +113,7 @@ int RSA_sign(int type, const unsigned char *m, unsigned int m_len,
113 i=i2d_X509_SIG(&sig,NULL); 113 i=i2d_X509_SIG(&sig,NULL);
114 } 114 }
115 j=RSA_size(rsa); 115 j=RSA_size(rsa);
116 if ((i-RSA_PKCS1_PADDING) > j) 116 if (i > (j-RSA_PKCS1_PADDING_SIZE))
117 { 117 {
118 RSAerr(RSA_F_RSA_SIGN,RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY); 118 RSAerr(RSA_F_RSA_SIGN,RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY);
119 return(0); 119 return(0);
@@ -136,7 +136,7 @@ int RSA_sign(int type, const unsigned char *m, unsigned int m_len,
136 *siglen=i; 136 *siglen=i;
137 137
138 if(type != NID_md5_sha1) { 138 if(type != NID_md5_sha1) {
139 memset(tmps,0,(unsigned int)j+1); 139 OPENSSL_cleanse(tmps,(unsigned int)j+1);
140 OPENSSL_free(tmps); 140 OPENSSL_free(tmps);
141 } 141 }
142 return(ret); 142 return(ret);
@@ -155,10 +155,11 @@ int RSA_verify(int dtype, const unsigned char *m, unsigned int m_len,
155 return(0); 155 return(0);
156 } 156 }
157 157
158 if((rsa->flags & RSA_FLAG_SIGN_VER) 158 if((rsa->flags & RSA_FLAG_SIGN_VER) && rsa->meth->rsa_verify)
159 && ENGINE_get_RSA(rsa->engine)->rsa_verify) 159 {
160 return ENGINE_get_RSA(rsa->engine)->rsa_verify(dtype, 160 return rsa->meth->rsa_verify(dtype, m, m_len,
161 m, m_len, sigbuf, siglen, rsa); 161 sigbuf, siglen, rsa);
162 }
162 163
163 s=(unsigned char *)OPENSSL_malloc((unsigned int)siglen); 164 s=(unsigned char *)OPENSSL_malloc((unsigned int)siglen);
164 if (s == NULL) 165 if (s == NULL)
@@ -221,7 +222,7 @@ int RSA_verify(int dtype, const unsigned char *m, unsigned int m_len,
221 } 222 }
222err: 223err:
223 if (sig != NULL) X509_SIG_free(sig); 224 if (sig != NULL) X509_SIG_free(sig);
224 memset(s,0,(unsigned int)siglen); 225 OPENSSL_cleanse(s,(unsigned int)siglen);
225 OPENSSL_free(s); 226 OPENSSL_free(s);
226 return(ret); 227 return(ret);
227 } 228 }
diff --git a/src/lib/libcrypto/rsa/rsa_test.c b/src/lib/libcrypto/rsa/rsa_test.c
index b8b462d33b..924e9ad1f6 100644
--- a/src/lib/libcrypto/rsa/rsa_test.c
+++ b/src/lib/libcrypto/rsa/rsa_test.c
@@ -16,7 +16,6 @@ int main(int argc, char *argv[])
16} 16}
17#else 17#else
18#include <openssl/rsa.h> 18#include <openssl/rsa.h>
19#include <openssl/engine.h>
20 19
21#define SetKey \ 20#define SetKey \
22 key->n = BN_bin2bn(n, sizeof(n)-1, key->n); \ 21 key->n = BN_bin2bn(n, sizeof(n)-1, key->n); \