Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4953556

no IllegalStateException for javax.crypto.Cipher.doFianal() and wrap()

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P3
    • Resolution: Fixed
    • Affects Version/s: 5.0
    • Fix Version/s: 5.0
    • Component/s: security-libs
    • Labels:
    • Subcomponent:
    • Resolved In Build:
      b36
    • CPU:
      sparc
    • OS:
      solaris_2.6
    • Verification:
      Verified

      Description

      Name: iiR10263 Date: 11/13/2003



      The specification requires Cipher objects to throw
      IllegaltateException "if this cipher is in a wrong state (e.g., has
      not been initialized)" when doFianal() or wrap() are called. For
      example we can consider that a Cipher object that is initialized for
      encryption is "in a wrong state" for wrapping.

      So it is reasonable to throw IllegalStateException if a Cipher was
      initialized for encryption but wrap() is called (or a Cipher was
      initialized for wrapping but doFinal() or update() is called).

      The implementation does not throw any exceptions in such situations.

      Please find the code example that reproduces the problem below

      import java.io.PrintWriter;
      import java.security.InvalidKeyException;
      import java.security.Key;
      import java.security.AlgorithmParameters;
      import java.security.spec.KeySpec;
      import javax.crypto.Cipher;
      import javax.crypto.SecretKeyFactory;
      import javax.crypto.spec.PBEKeySpec;
      import javax.crypto.spec.SecretKeySpec;

      public class e7 {
       
          public static void main(String argv[]) {
              Key k;
              Cipher c;
              AlgorithmParameters params = null;
              
              String alg = "PBEWITHMD5ANDDES";
              byte[] salt = {
                  (byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c,
                  (byte)0x7e, (byte)0xc8, (byte)0xee, (byte)0x99
              };
              int count = 20;
              String s = "My wonderfull password that is long enough. Tra-la-la, let me sing a song";
              
              try {
                  int kl = Cipher.getMaxAllowedKeyLength(alg);
                  
                  SecretKeyFactory skf =
                      SecretKeyFactory.getInstance(alg);

                  KeySpec ks = new PBEKeySpec(s.toCharArray(), salt, count, kl);

                  // PBE algorithm is symmetric.
                  k = skf.generateSecret(ks);

                  c = Cipher.getInstance(alg);

                  c.init(Cipher.WRAP_MODE, k);
                  
                  try {
                  
                      c.doFinal();
                      
                      System.out.println("Where is my exception?");
                  } catch (IllegalStateException e) {
                  }

              } catch (Exception e) {
                  e.printStackTrace(System.out);
              }
          }
      }

      Test output is
      Where is my exception?

      java full version "1.5.0-beta-b26"

      ======================================================================
      ###@###.### 10/26/04 10:26 GMT

        Attachments

          Activity

            People

            • Assignee:
              valeriep Valerie Peng
              Reporter:
              duke J. Duke (Inactive)
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Imported:
                Indexed: