@@ -16,8 +16,9 @@ use crate::padding::PaddingScheme;
16
16
use crate :: raw:: { DecryptionPrimitive , EncryptionPrimitive } ;
17
17
use crate :: { oaep, pkcs1v15, pss} ;
18
18
19
- static MIN_PUB_EXPONENT : u64 = 2 ;
20
- static MAX_PUB_EXPONENT : u64 = 1 << ( 31 - 1 ) ;
19
+ const MIN_PUB_EXPONENT : u64 = 2 ;
20
+ const MAX_PUB_EXPONENT : u64 = ( 1 << 33 ) - 1 ;
21
+ const MAX_MODULUS_BITS : usize = 16384 ;
21
22
22
23
pub trait PublicKeyParts {
23
24
/// Returns the modulus of the key.
@@ -548,16 +549,20 @@ impl RsaPrivateKey {
548
549
/// Check that the public key is well formed and has an exponent within acceptable bounds.
549
550
#[ inline]
550
551
pub fn check_public ( public_key : & impl PublicKeyParts ) -> Result < ( ) > {
551
- let public_key = public_key
552
+ if public_key. n ( ) . bits ( ) > MAX_MODULUS_BITS {
553
+ return Err ( Error :: ModulusTooLarge ) ;
554
+ }
555
+
556
+ let e = public_key
552
557
. e ( )
553
558
. to_u64 ( )
554
559
. ok_or ( Error :: PublicExponentTooLarge ) ?;
555
560
556
- if public_key < MIN_PUB_EXPONENT {
561
+ if e < MIN_PUB_EXPONENT {
557
562
return Err ( Error :: PublicExponentTooSmall ) ;
558
563
}
559
564
560
- if public_key > MAX_PUB_EXPONENT {
565
+ if e > MAX_PUB_EXPONENT {
561
566
return Err ( Error :: PublicExponentTooLarge ) ;
562
567
}
563
568
0 commit comments