BaseBlockCipher anonymous inner Exception class

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

BaseBlockCipher anonymous inner Exception class

Marnix Bindels

Dear mailing-list,

 

Can anyone explain why it’s better to handle exceptions the way BaseBlockCipher.engineInit() does it since rev. 1.5 than how it used to do it upto revision 1.4?

 

Current code results in an anon inner class like this:

 

          throw new InvalidKeyException(e.getMessage())

793

 

             {

794

 

                 public Throwable getCause()

795

 

                 {

796

 

                     return e;

797

 

                 }

798

 

             };

 

Instead of throw new InvalidKeyException(e.getMessage(), e);

 

I am unfamiliar with this programming idiom.

 

I ended up at this point in the code after encountering the following stack trace from a live application:

 

Caused by: org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher$1: Key length not 128/192/256 bits.

        at org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.engineInit(Unknown Source)

        at javax.crypto.Cipher.init(Cipher.java:1394)

        at javax.crypto.Cipher.init(Cipher.java:1327)

        at nl.myApplication.CryptoClass.getCipher(CryptoClass.java:118)

        ... 35 more

Caused by: java.lang.IllegalArgumentException: Key length not 128/192/256 bits.

        at org.bouncycastle.crypto.engines.AESFastEngine.generateWorkingKey(Unknown Source)

        at org.bouncycastle.crypto.engines.AESFastEngine.init(Unknown Source)

        at org.bouncycastle.crypto.modes.CBCBlockCipher.init(Unknown Source)

        at org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher.init(Unknown Source)

        at org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher$BufferedGenericBlockCipher.init(Unknown Source)

        ... 39 more

 

Where the application uses release 1.54. Trying to replay the situation yielded java.security.InvalidKeyException: Key length not 128/192/256 bits. under release 1.52, which I find to be more helpful thanks to the name of the exception class.

 

Thanks for getting back to me,

 

Marnix

 

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: BaseBlockCipher anonymous inner Exception class

martijn.list
On 06/20/2016 02:29 PM, Marnix Bindels wrote:

> Dear mailing-list,
>
>  
>
> Can anyone explain why it’s better to handle exceptions the way
> BaseBlockCipher.engineInit() does it since rev. 1.5 than how it used to
> do it upto revision 1.4?
>
>  
>
> Current code results in an anon inner class like this:
>
>  
>
>           throw new InvalidKeyException(e.getMessage())
>
> 793
> <http://www.bouncycastle.org/viewcvs/viewcvs.cgi/bc-java/prov/src/main/java/org/bouncycastle/jcajce/provider/symmetric/util/BaseBlockCipher.java?annotate=1.5#l793>
>
>
>
>  
>
>
>
>              {
>
> 794
> <http://www.bouncycastle.org/viewcvs/viewcvs.cgi/bc-java/prov/src/main/java/org/bouncycastle/jcajce/provider/symmetric/util/BaseBlockCipher.java?annotate=1.5#l794>
>
>
>
>  
>
>
>
>                  public Throwable getCause()
>
> 795
> <http://www.bouncycastle.org/viewcvs/viewcvs.cgi/bc-java/prov/src/main/java/org/bouncycastle/jcajce/provider/symmetric/util/BaseBlockCipher.java?annotate=1.5#l795>
>
>
>
>  
>
>
>
>                  {
>
> 796
> <http://www.bouncycastle.org/viewcvs/viewcvs.cgi/bc-java/prov/src/main/java/org/bouncycastle/jcajce/provider/symmetric/util/BaseBlockCipher.java?annotate=1.5#l796>
>
>
>
>  
>
>
>
>                      return e;
>
> 797
> <http://www.bouncycastle.org/viewcvs/viewcvs.cgi/bc-java/prov/src/main/java/org/bouncycastle/jcajce/provider/symmetric/util/BaseBlockCipher.java?annotate=1.5#l797>
>
>
>
>  
>
>
>
>                  }
>
> 798
> <http://www.bouncycastle.org/viewcvs/viewcvs.cgi/bc-java/prov/src/main/java/org/bouncycastle/jcajce/provider/symmetric/util/BaseBlockCipher.java?annotate=1.5#l798>
>
>
>
>  
>
>
>
>              };
>
>  
>
> Instead of throw new InvalidKeyException(e.getMessage(), e);
>
>  
>
> I am unfamiliar with this programming idiom.
>
>  
>
> I ended up at this point in the code after encountering the following
> stack trace from a live application:
>
>  
>
> Caused by:
> org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher$1: Key
> length not 128/192/256 bits.
>
>         at
> org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.engineInit(Unknown
> Source)
>
>         at javax.crypto.Cipher.init(Cipher.java:1394)
>
>         at javax.crypto.Cipher.init(Cipher.java:1327)
>
>         at nl.myApplication.CryptoClass.getCipher(CryptoClass.java:118)
>
>         ... 35 more
>
> Caused by: java.lang.IllegalArgumentException: Key length not
> 128/192/256 bits.
>
>         at
> org.bouncycastle.crypto.engines.AESFastEngine.generateWorkingKey(Unknown
> Source)
>
>         at org.bouncycastle.crypto.engines.AESFastEngine.init(Unknown
> Source)
>
>         at org.bouncycastle.crypto.modes.CBCBlockCipher.init(Unknown Source)
>
>         at
> org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher.init(Unknown
> Source)
>
>         at
> org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher$BufferedGenericBlockCipher.init(Unknown
> Source)
>
>         ... 39 more
>
>  
>
> Where the application uses release 1.54. Trying to replay the situation
> yielded java.security.InvalidKeyException: Key length not 128/192/256
> bits. under release 1.52, which I find to be more helpful thanks to the
> name of the exception class.

My educated guess is that the constructor:

public InvalidKeyException(String message, Throwable cause)

Is only valid for Java 5 and up. Since BC also needs to support Java 4
(from BC website "A signed jar version suitable for JDK 1.4-1.7 and the
Sun JCE."), BC cannot use this constructor without breaking support for
Java 4.

Kind regards,

Martijn Brinkers

--
CipherMail email encryption

Email encryption with support for S/MIME, OpenPGP, PDF encryption and
secure webmail pull.

https://www.ciphermail.com

Twitter: http://twitter.com/CipherMail

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: BaseBlockCipher anonymous inner Exception class

Marnix Bindels
Interesting, would not have thought of that. Java 1.4...! The since-attributes support the idea. Now Throwable initCause(Throwable cause) is there since 1.4 and seems intended for this exact scenario. Is initCause somehow to be avoided? Or would it simple be a matter of taste?

> Op 20 jun. 2016 om 15:01 heeft martijn.list <[hidden email]> het volgende geschreven:
>
>> On 06/20/2016 02:29 PM, Marnix Bindels wrote:
>> Dear mailing-list,
>>
>>
>>
>> Can anyone explain why it’s better to handle exceptions the way
>> BaseBlockCipher.engineInit() does it since rev. 1.5 than how it used to
>> do it upto revision 1.4?
>>
>>
>>
>> Current code results in an anon inner class like this:
>>
>>
>>
>>          throw new InvalidKeyException(e.getMessage())
>>
>> 793
>> <http://www.bouncycastle.org/viewcvs/viewcvs.cgi/bc-java/prov/src/main/java/org/bouncycastle/jcajce/provider/symmetric/util/BaseBlockCipher.java?annotate=1.5#l793>
>>
>>    
>>
>>
>>
>>    
>>
>>             {
>>
>> 794
>> <http://www.bouncycastle.org/viewcvs/viewcvs.cgi/bc-java/prov/src/main/java/org/bouncycastle/jcajce/provider/symmetric/util/BaseBlockCipher.java?annotate=1.5#l794>
>>
>>    
>>
>>
>>
>>    
>>
>>                 public Throwable getCause()
>>
>> 795
>> <http://www.bouncycastle.org/viewcvs/viewcvs.cgi/bc-java/prov/src/main/java/org/bouncycastle/jcajce/provider/symmetric/util/BaseBlockCipher.java?annotate=1.5#l795>
>>
>>    
>>
>>
>>
>>    
>>
>>                 {
>>
>> 796
>> <http://www.bouncycastle.org/viewcvs/viewcvs.cgi/bc-java/prov/src/main/java/org/bouncycastle/jcajce/provider/symmetric/util/BaseBlockCipher.java?annotate=1.5#l796>
>>
>>    
>>
>>
>>
>>    
>>
>>                     return e;
>>
>> 797
>> <http://www.bouncycastle.org/viewcvs/viewcvs.cgi/bc-java/prov/src/main/java/org/bouncycastle/jcajce/provider/symmetric/util/BaseBlockCipher.java?annotate=1.5#l797>
>>
>>    
>>
>>
>>
>>    
>>
>>                 }
>>
>> 798
>> <http://www.bouncycastle.org/viewcvs/viewcvs.cgi/bc-java/prov/src/main/java/org/bouncycastle/jcajce/provider/symmetric/util/BaseBlockCipher.java?annotate=1.5#l798>
>>
>>    
>>
>>
>>
>>    
>>
>>             };
>>
>>
>>
>> Instead of throw new InvalidKeyException(e.getMessage(), e);
>>
>>
>>
>> I am unfamiliar with this programming idiom.
>>
>>
>>
>> I ended up at this point in the code after encountering the following
>> stack trace from a live application:
>>
>>
>>
>> Caused by:
>> org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher$1: Key
>> length not 128/192/256 bits.
>>
>>        at
>> org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.engineInit(Unknown
>> Source)
>>
>>        at javax.crypto.Cipher.init(Cipher.java:1394)
>>
>>        at javax.crypto.Cipher.init(Cipher.java:1327)
>>
>>        at nl.myApplication.CryptoClass.getCipher(CryptoClass.java:118)
>>
>>        ... 35 more
>>
>> Caused by: java.lang.IllegalArgumentException: Key length not
>> 128/192/256 bits.
>>
>>        at
>> org.bouncycastle.crypto.engines.AESFastEngine.generateWorkingKey(Unknown
>> Source)
>>
>>        at org.bouncycastle.crypto.engines.AESFastEngine.init(Unknown
>> Source)
>>
>>        at org.bouncycastle.crypto.modes.CBCBlockCipher.init(Unknown Source)
>>
>>        at
>> org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher.init(Unknown
>> Source)
>>
>>        at
>> org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher$BufferedGenericBlockCipher.init(Unknown
>> Source)
>>
>>        ... 39 more
>>
>>
>>
>> Where the application uses release 1.54. Trying to replay the situation
>> yielded java.security.InvalidKeyException: Key length not 128/192/256
>> bits. under release 1.52, which I find to be more helpful thanks to the
>> name of the exception class.
>
> My educated guess is that the constructor:
>
> public InvalidKeyException(String message, Throwable cause)
>
> Is only valid for Java 5 and up. Since BC also needs to support Java 4
> (from BC website "A signed jar version suitable for JDK 1.4-1.7 and the
> Sun JCE."), BC cannot use this constructor without breaking support for
> Java 4.
>
> Kind regards,
>
> Martijn Brinkers
>
> --
> CipherMail email encryption
>
> Email encryption with support for S/MIME, OpenPGP, PDF encryption and
> secure webmail pull.
>
> https://www.ciphermail.com
>
> Twitter: http://twitter.com/CipherMail

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: BaseBlockCipher anonymous inner Exception class

Lothar Kimmeringer-4
In reply to this post by martijn.list
Hi,

Am 20.06.2016 um 15:00 schrieb martijn.list:

> My educated guess is that the constructor:
>
> public InvalidKeyException(String message, Throwable cause)
>
> Is only valid for Java 5 and up. Since BC also needs to support Java 4
> (from BC website "A signed jar version suitable for JDK 1.4-1.7 and the
> Sun JCE."), BC cannot use this constructor without breaking support for
> Java 4.

then use initCause as Marnix already suggested. I use that all the
time, especially when throwin IOExceptions that don't come with
a constructor that allow to set a cause. The line looks strange but
not as strange as the code in BC:

catch(Exception e){
   throw (MyOwnException) new MyOwnException("error while doing stuff").initCause(e);
}

Works as described including the "Caused by"-blocks when calling printStackTrace.


Thanks and cheers, Lothar

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: BaseBlockCipher anonymous inner Exception class

martijn.list
On 06/21/2016 09:28 AM, Lothar Kimmeringer wrote:

> Hi,
>
> Am 20.06.2016 um 15:00 schrieb martijn.list:
>
>> My educated guess is that the constructor:
>>
>> public InvalidKeyException(String message, Throwable cause)
>>
>> Is only valid for Java 5 and up. Since BC also needs to support Java 4
>> (from BC website "A signed jar version suitable for JDK 1.4-1.7 and the
>> Sun JCE."), BC cannot use this constructor without breaking support for
>> Java 4.
>
> then use initCause as Marnix already suggested. I use that all the
> time, especially when throwin IOExceptions that don't come with
> a constructor that allow to set a cause. The line looks strange but
> not as strange as the code in BC:
>
> catch(Exception e){
>   throw (MyOwnException) new MyOwnException("error while doing
> stuff").initCause(e);
> }
>
> Works as described including the "Caused by"-blocks when calling
> printStackTrace.


The page https://www.bouncycastle.org/latest_releases.html shows that
some parts of BC are even supported on JDK1.1. The construction with the
anonymous innerclass and adding getCause will I think compile on JDKs <
4. Using #initCause however will not compile on Java < 4. Might be that
this is a left-over from support for older Java versions.

Kind regards,

Martijn Brinkers


--
CipherMail email encryption

Email encryption with support for S/MIME, OpenPGP, PDF encryption and
secure webmail pull.

https://www.ciphermail.com

Twitter: http://twitter.com/CipherMail

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: BaseBlockCipher anonymous inner Exception class

Lothar Kimmeringer-4
Hi,

Am 21.06.2016 um 09:49 schrieb martijn.list:
> On 06/21/2016 09:28 AM, Lothar Kimmeringer wrote:
>> then use initCause as Marnix already suggested.

[...]

> The page https://www.bouncycastle.org/latest_releases.html shows that
> some parts of BC are even supported on JDK1.1.

I thought that getCause and initCause exist since 1.1 but after
checking the source it's been added with Java 1.4. So I was wrong, keep
it this way and there is nothing to see here ;-)


Thanks and cheers, Lothar

Loading...