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

ArrayIndexOutOfBoundsException in RSAPrivateCrtKey.getEncoded() & other methods

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 1.4.0
    • 1.3.0
    • security-libs
    • beta
    • generic, x86
    • generic, windows_nt

      call. This time we are simply trying
      to obtain the publicExponent from the PrivateKey. This should obviously work
      because we've created the key with the public exponent near the top of the test
      case. Unfortunately, the Java Virtual Machine throws an array out of bounds
      exception when you try and make this call.

      We are in the process of making a wrapper API that converts our Private Key
      types to the Sun JCA Private Key types - a patch specifically for us won't
      work. How about a workaround ?
      (Review ID: 115406)
      ======================================================================


      Name: krC82822 Date: 02/08/2001


      8 Feb 2001, eval1127@eng -- reproducible with
      merlin (1.4) beta build 51 on Solaris:

      java version "1.4.0-beta"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta-b51)
      Java HotSpot(TM) Client VM (build 1.4beta-B51, mixed mode)
      -------------------
      java version "1.3.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
      Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)
      &
      java version "1.2.2"
      Classic VM (build JDK-1.2.2-W, native threads, symcjit)
      -----------------

      Just run the following test application:

      import java.math.BigInteger;
      import java.security.*;
      import java.security.interfaces.*;
      import java.security.spec.*;

      public class KeyTest {


          public static void main(String[] args) {
      try {
      KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
      SecureRandom sr = new SecureRandom();
      kpg.initialize(1024, sr);
      KeyPair kp = kpg.generateKeyPair();

      PrivateKey private_key = kp.getPrivate();
      PublicKey public_key = kp.getPublic();

      RSAPrivateKey rsa_private_key = (RSAPrivateKey) private_key;
      BigInteger modulus = rsa_private_key.getModulus();
      BigInteger private_exponent = rsa_private_key.getPrivateExponent();

      System.out.println("Algorithm: " + rsa_private_key.getAlgorithm());
      System.out.println("Format: " + rsa_private_key.getFormat());
      System.out.println("Modulus");
      System.out.println(modulus);
      System.out.println();
      System.out.println("Private Exponent");
      System.out.println(private_exponent);
      System.out.println();
      System.out.println("Encoded");
      System.out.println(new String(rsa_private_key.getEncoded()));
      System.out.println();

      KeyFactory kf = KeyFactory.getInstance("RSA");
      RSAPrivateKeySpec spec =
      new RSAPrivateKeySpec(modulus, private_exponent);

      PrivateKey private_key2 = kf.generatePrivate(spec);
      RSAPrivateKey rsa_private_key2 = (RSAPrivateKey) private_key2;

      System.out.println("Algorithm2: " + private_key2.getAlgorithm());
      System.out.println("Format2: " + private_key2.getFormat());
      System.out.println("Modulus2");
      System.out.println(rsa_private_key2.getModulus());
      System.out.println();
      System.out.println("Private Exponent2");
      System.out.println(rsa_private_key2.getPrivateExponent());
      System.out.println();
      System.out.println("Encoded2");
      System.out.println(new String(private_key2.getEncoded()));
      }
      catch(Exception e) {
      e.printStackTrace();
      }
          }

      }

      Output with Java2 SDK SE1.3:

      bash-2.02$ java KeyTest
      Algorithm: RSA
      Format: PKCS8
      Modulus
      1313377044958837654881411559453633681507429669284.............
      3276253101493629817565508371291819282960457086739.............

      Private Exponent
      1267725383936571878635628939980124403901284670480.............
      8473029340258526617412586538587451007941494430870.............

      Encoded
      <removed the mess of characters>

      Algorithm2: RSA
      Format2: PKCS8
      Modulus2
      1313377044958837654881411559453633681507429669284.............
      3276253101493629817565508371291819282960457086739.............

      Private Exponent2
      1267725383936571878635628939980124403901284670480.............
      8473029340258526617412586538587451007941494430870.............

      Encoded2
      java.lang.ArrayIndexOutOfBoundsException
              at com.sun.rsajca.JSA_RSAPrivateKey.getEncoded([DashoPro-V1.2-120198])
              at KeyTest.main(KeyTest.java:52)


      I installed both JCE1.2 and JSSE1.0.2. Key size does not seem to be
      the matter.
      (Review ID: 114037)
      ======================================================================

      Name: krC82822 Date: 02/08/2001


      orig synopsis: "Problems with accessor functions in the JCA RSAPrivateKey class"

      java version "1.3.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
      Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)

      Hello.

      Here is the source code to reproduce my bug. It relies on no
      security providers and can be compiled straight from the command line
      with no classpath modifications. The description of the bug will be
      added after the code is pasted:

      //<BEGIN CODE>
      import java.security.*;
      import java.security.KeyFactory.*;
      import java.security.interfaces.*;
      import java.security.spec.*;
      import java.math.*;

      class Test {
          
        public static void main (String args[]) {
        
          KeyFactory keyFactory = null;
          PrivateKey pk = null;
          RSAPrivateCrtKey rsaPrivKeyCRT = null;
          BigInteger biPrivExponent = new BigInteger(1,priExpo);
          BigInteger biModulus = new BigInteger(1,modulus);
          BigInteger biPrimeP = new BigInteger(1,primeP);
          BigInteger biPrimeQ = new BigInteger(1,primeQ);
          BigInteger biExpoP = new BigInteger(1,expoP);
          BigInteger biExpoQ = new BigInteger(1,expoQ);
          BigInteger biCoeff = new BigInteger(1,coeff);
          BigInteger biPubExponent = new BigInteger(1,pubExpo);
              
          // We need a KeyFactory for RSA keys:
          try {
          
            keyFactory = KeyFactory.getInstance("RSA");
                  
            // Creat an RSA Private Key from the CRT information
            RSAPrivateCrtKeySpec rsaPrivKeySpecCRT =
            new RSAPrivateCrtKeySpec(biModulus,biPubExponent,biPrivExponent,biPrimeP,
                                     biPrimeQ,biExpoP,biExpoQ,biCoeff);

            rsaPrivKeyCRT = (RSAPrivateCrtKey)keyFactory.generatePrivate
      (rsaPrivKeySpecCRT);
                                  
            System.out.println(rsaPrivKeyCRT.getFormat());
          
            try {
              byte[] encoded = rsaPrivKeyCRT.getEncoded();
            } catch (Exception xp) {
              xp.printStackTrace();
            }
          
            BigInteger bi = rsaPrivKeyCRT.getPublicExponent();
          
          } catch (Exception xp) {
            xp.printStackTrace();
          }
            
        }
       
       
        static byte[] modulus = {
          (byte)0xab, (byte)0x38, (byte)0x39, (byte)0x40,
          (byte)0x54, (byte)0x2c, (byte)0xac, (byte)0x9a,
          (byte)0xc0, (byte)0x37, (byte)0x40, (byte)0xd0,
          (byte)0x49, (byte)0x04, (byte)0xed, (byte)0x51,
          (byte)0x0e, (byte)0x95, (byte)0x72, (byte)0x02,
          (byte)0x51, (byte)0xc2, (byte)0xad, (byte)0x9d,
          (byte)0xa7, (byte)0xeb, (byte)0xba, (byte)0x29,
          (byte)0xae, (byte)0xd4, (byte)0x49, (byte)0x79,
          (byte)0x53, (byte)0xfa, (byte)0xdf, (byte)0x01,
          (byte)0x6c, (byte)0xbc, (byte)0x69, (byte)0x46,
          (byte)0x4c, (byte)0x83, (byte)0x1b, (byte)0xd9,
          (byte)0x3b, (byte)0x59, (byte)0x42, (byte)0x04,
          (byte)0x99, (byte)0x0f, (byte)0x63, (byte)0x24,
          (byte)0x75, (byte)0xa0, (byte)0xbe, (byte)0x6f,
          (byte)0x92, (byte)0x4d, (byte)0x9d, (byte)0xa2,
          (byte)0x40, (byte)0xda, (byte)0xf8, (byte)0x49
        };

        static byte[] pubExpo = {
          (byte)0x01, (byte)0x00, (byte)0x01
        };

        static byte[] priExpo = {
          (byte)0x4a, (byte)0xd2, (byte)0xe7, (byte)0x32,
          (byte)0x15, (byte)0x96, (byte)0xf0, (byte)0x57,
          (byte)0x30, (byte)0x68, (byte)0xf5, (byte)0x0a,
          (byte)0x10, (byte)0xde, (byte)0xf6, (byte)0x56,
          (byte)0xd5, (byte)0xe8, (byte)0xb9, (byte)0x4a,
          (byte)0x0a, (byte)0x30, (byte)0xe9, (byte)0x6e,
          (byte)0x5c, (byte)0x53, (byte)0xc7, (byte)0xa7,
          (byte)0x2f, (byte)0x9f, (byte)0xd5, (byte)0xfb,
          (byte)0x58, (byte)0x9b, (byte)0x1e, (byte)0x5b,
          (byte)0xe8, (byte)0x6e, (byte)0xae, (byte)0x02,
          (byte)0xaa, (byte)0x15, (byte)0x23, (byte)0x67,
          (byte)0xaa, (byte)0x20, (byte)0x9e, (byte)0x82,
          (byte)0x76, (byte)0x4c, (byte)0xad, (byte)0xe1,
          (byte)0x95, (byte)0xde, (byte)0xe3, (byte)0x25,
          (byte)0x66, (byte)0x2f, (byte)0xb0, (byte)0xab,
          (byte)0x1c, (byte)0xe5, (byte)0xa0, (byte)0x01
        };
        static byte[] primeP = {
          (byte)0xd1, (byte)0xeb, (byte)0x51, (byte)0xbd,
          (byte)0x09, (byte)0x26, (byte)0x7e, (byte)0xe7,
          (byte)0x12, (byte)0x8c, (byte)0xeb, (byte)0x5c,
          (byte)0x32, (byte)0x18, (byte)0xd1, (byte)0x60,
          (byte)0x0b, (byte)0x49, (byte)0x67, (byte)0x8f,
          (byte)0x78, (byte)0x3c, (byte)0x58, (byte)0xc5,
          (byte)0xb0, (byte)0x01, (byte)0x70, (byte)0xee,
          (byte)0x1a, (byte)0xcf, (byte)0x6e, (byte)0xe1
        };
        static byte[] primeQ = {
          (byte)0xd0, (byte)0xce, (byte)0x21, (byte)0x83,
          (byte)0x41, (byte)0x73, (byte)0xf6, (byte)0x84,
          (byte)0x32, (byte)0x06, (byte)0xa8, (byte)0xa6,
          (byte)0xad, (byte)0x13, (byte)0x2b, (byte)0x65,
          (byte)0x27, (byte)0x86, (byte)0x28, (byte)0xef,
          (byte)0x0e, (byte)0x8c, (byte)0xca, (byte)0x4f,
          (byte)0x20, (byte)0xc0, (byte)0x19, (byte)0x95,
          (byte)0xfe, (byte)0x6c, (byte)0x3e, (byte)0x69
        };
        static byte[] expoP = {
          (byte)0x1a, (byte)0x49, (byte)0x9c, (byte)0xb7,
          (byte)0xce, (byte)0x80, (byte)0x8a, (byte)0x9d,
          (byte)0xc7, (byte)0x3d, (byte)0xec, (byte)0x6f,
          (byte)0x64, (byte)0x3a, (byte)0xa5, (byte)0x65,
          (byte)0xa0, (byte)0xa4, (byte)0x35, (byte)0x9a,
          (byte)0xca, (byte)0xd4, (byte)0xcb, (byte)0xcd,
          (byte)0x1d, (byte)0xc8, (byte)0x60, (byte)0x6b,
          (byte)0x00, (byte)0xe2, (byte)0x7f, (byte)0x21
        };
        static byte[] expoQ = {
          (byte)0xa7, (byte)0x93, (byte)0xd7, (byte)0x77,
          (byte)0x94, (byte)0xef, (byte)0x31, (byte)0x78,
          (byte)0x55, (byte)0x01, (byte)0xdd, (byte)0x16,
          (byte)0xaf, (byte)0xae, (byte)0xc3, (byte)0xd4,
          (byte)0x12, (byte)0x0d, (byte)0x6d, (byte)0x0a,
          (byte)0xb6, (byte)0xdd, (byte)0xad, (byte)0x7c,
          (byte)0x25, (byte)0xe7, (byte)0xa6, (byte)0x61,
          (byte)0x27, (byte)0xe8, (byte)0xcd, (byte)0x89
        };
        static byte[] coeff = {
          (byte)0x0b, (byte)0xdb, (byte)0x90, (byte)0x7f,
          (byte)0x33, (byte)0xc5, (byte)0x1f, (byte)0x5b,
          (byte)0x4d, (byte)0xa4, (byte)0x86, (byte)0xda,
          (byte)0x77, (byte)0xd4, (byte)0xb3, (byte)0x1d,
          (byte)0xbc, (byte)0xc3, (byte)0xae, (byte)0x0b,
          (byte)0xac, (byte)0x91, (byte)0xf3, (byte)0x38,
          (byte)0x4a, (byte)0xcf, (byte)0x10, (byte)0xb1,
          (byte)0x5e, (byte)0x5a, (byte)0xd1, (byte)0x86
        };

        static byte[] RSAPublicKeyBER = {
          (byte)0x30, (byte)0x4A, (byte)0x30, (byte)0x0D,
          (byte)0x06, (byte)0x09, (byte)0x2A, (byte)0x86,
          (byte)0x48, (byte)0x86, (byte)0xF7, (byte)0x0D,
          (byte)0x01, (byte)0x01, (byte)0x01, (byte)0x05,
          (byte)0x00, (byte)0x03, (byte)0x39, (byte)0x00,
          (byte)0x30, (byte)0x36, (byte)0x02, (byte)0x31,
          (byte)0x00, (byte)0xA8, (byte)0x61, (byte)0xBA,
          (byte)0x49, (byte)0x76, (byte)0x97, (byte)0x7C,
          (byte)0xA3, (byte)0x11, (byte)0xB7, (byte)0x2F,
          (byte)0xFC, (byte)0xF1, (byte)0x0A, (byte)0x52,
          (byte)0xDC, (byte)0xBC, (byte)0x25, (byte)0xE7,
          (byte)0xE9, (byte)0x2A, (byte)0x4A, (byte)0x6A,
          (byte)0x6E, (byte)0x3F, (byte)0xC4, (byte)0x3B,
          (byte)0x36, (byte)0xB0, (byte)0x9B, (byte)0xBC,
          (byte)0x12, (byte)0x45, (byte)0x18, (byte)0xD0,
          (byte)0x89, (byte)0x16, (byte)0xAB, (byte)0xDA,
          (byte)0x7B, (byte)0xD5, (byte)0x99, (byte)0x42,
          (byte)0x08, (byte)0xC8, (byte)0xC7, (byte)0x51,
          (byte)0x27, (byte)0x02, (byte)0x01, (byte)0x11
        };

        static byte[] RSAPrivateKeyBER = {
          (byte)0x30, (byte)0x82, (byte)0x01, (byte)0x0A,
          (byte)0x02, (byte)0x01, (byte)0x00, (byte)0x30,
          (byte)0x0D, (byte)0x06, (byte)0x09, (byte)0x2A,
          (byte)0x86, (byte)0x48, (byte)0x86, (byte)0xF7,
          (byte)0x0D, (byte)0x01, (byte)0x01, (byte)0x01,
          (byte)0x05, (byte)0x00, (byte)0x04, (byte)0x81,
          (byte)0xF5, (byte)0x30, (byte)0x81, (byte)0xF2,
          (byte)0x02, (byte)0x01, (byte)0x00, (byte)0x02,
          (byte)0x31, (byte)0x00, (byte)0xA8, (byte)0x61,
          (byte)0xBA, (byte)0x49, (byte)0x76, (byte)0x97,
          (byte)0x7C, (byte)0xA3, (byte)0x11, (byte)0xB7,
          (byte)0x2F, (byte)0xFC, (byte)0xF1, (byte)0x0A,
          (byte)0x52, (byte)0xDC, (byte)0xBC, (byte)0x25,
          (byte)0xE7, (byte)0xE9, (byte)0x2A, (byte)0x4A,
          (byte)0x6A, (byte)0x6E, (byte)0x3F, (byte)0xC4,
          (byte)0x3B, (byte)0x36, (byte)0xB0, (byte)0x9B,
          (byte)0xBC, (byte)0x12, (byte)0x45, (byte)0x18,
          (byte)0xD0, (byte)0x89, (byte)0x16, (byte)0xAB,
          (byte)0xDA, (byte)0x7B, (byte)0xD5, (byte)0x99,
          (byte)0x42, (byte)0x08, (byte)0xC8, (byte)0xC7,
          (byte)0x51, (byte)0x27, (byte)0x02, (byte)0x01,
          (byte)0x11, (byte)0x02, (byte)0x31, (byte)0x00,
          (byte)0x94, (byte)0x92, (byte)0x77, (byte)0x31,
          (byte)0xC2, (byte)0xFE, (byte)0x22, (byte)0xAE,
          (byte)0x00, (byte)0x92, (byte)0x93, (byte)0xC1,
          (byte)0x10, (byte)0xEA, (byte)0xFD, (byte)0xD1,
          (byte)0xD3, (byte)0x30, (byte)0x81, (byte)0x55,
          (byte)0x43, (byte)0x6E, (byte)0xD6, (byte)0x5F,
          (byte)0xD8, (byte)0x4B, (byte)0xCE, (byte)0x84,
          (byte)0x5E, (byte)0x4B, (byte)0xEA, (byte)0x2E,
          (byte)0xD7, (byte)0x0C, (byte)0xA5, (byte)0x4F,
          (byte)0xA0, (byte)0x42, (byte)0xE9, (byte)0x27,
          (byte)0x73, (byte)0xBD, (byte)0x0E, (byte)0x51,
          (byte)0x18, (byte)0x13, (byte)0xFC, (byte)0x61,
          (byte)0x02, (byte)0x19, (byte)0x00, (byte)0xDC,
          (byte)0x43, (byte)0x86, (byte)0x15, (byte)0x63,
          (byte)0x0E, (byte)0x97, (byte)0x21, (byte)0xBC,
          (byte)0x15, (byte)0x21, (byte)0x8F, (byte)0x4A,
          (byte)0x74, (byte)0x8D, (byte)0x23, (byte)0x2B,
          (byte)0x0D, (byte)0x75, (byte)0x68, (byte)0x8B,
          (byte)0x29, (byte)0x2B, (byte)0x39, (byte)0x02,
          (byte)0x19, (byte)0x00, (byte)0xC3, (byte)0xB3,
          (byte)0x53, (byte)0x9C, (byte)0x5A, (byte)0x26,
          (byte)0x0A, (byte)0x77, (byte)0x2E, (byte)0xE4,
          (byte)0x48, (byte)0xF4, (byte)0xE3, (byte)0x62,
          (byte)0xEF, (byte)0xC5, (byte)0x8D, (byte)0xC6,
          (byte)0x9A, (byte)0x44, (byte)0x55, (byte)0x87,
          (byte)0x7F, (byte)0x5F, (byte)0x02, (byte)0x19,
          (byte)0x00, (byte)0xB5, (byte)0x64, (byte)0xC8,
          (byte)0xC6, (byte)0x51, (byte)0x93, (byte)0x8B,
          (byte)0x85, (byte)0x31, (byte)0x7A, (byte)0xD0,
          (byte)0x57, (byte)0xE2, (byte)0xF6, (byte)0x92,
          (byte)0x59, (byte)0x32, (byte)0x83, (byte)0x8D,
          (byte)0xDD, (byte)0x9F, (byte)0xC7, (byte)0x8D,
          (byte)0x01, (byte)0x02, (byte)0x18, (byte)0x39,
          (byte)0x8F, (byte)0x18, (byte)0x97, (byte)0x65,
          (byte)0xCE, (byte)0xF4, (byte)0x04, (byte)0xEF,
          (byte)0xAC, (byte)0x8D, (byte)0xED, (byte)0xAC,
          (byte)0x4A, (byte)0x46, (byte)0x85, (byte)0x65,
          (byte)0xEF, (byte)0x1E, (byte)0x50, (byte)0x55,
          (byte)0x64, (byte)0x16, (byte)0x67, (byte)0x02,
          (byte)0x19, (byte)0x00, (byte)0xA3, (byte)0x85,
          (byte)0x7D, (byte)0x9C, (byte)0xC4, (byte)0x91,
          (byte)0xAA, (byte)0x39, (byte)0x24, (byte)0x01,
          (byte)0x3C, (byte)0x1A, (byte)0x0D, (byte)0x19,
          (byte)0x42, (byte)0xC5, (byte)0x95, (byte)0xFF,
          (byte)0x4D, (byte)0x49, (byte)0x7E, (byte)0x98,
          (byte)0x61, (byte)0x3C
        };
      }

      //<END CODE>

        Description of what is happening:

      I am creating an RSA Private Key based on the "CRT" information for the private
      key. After this happens, I am trying to get information about my new private
      key. The first accessor function "getFormat()" returns a String "PKCS8" which
      indicates that this key supports PKCS#8 encoding.

      The next function call, however, reveals a bug. When we try and actually get the
      encoding (getEncoded()) the Java Virtual Machine throws an array out of bounds
      exception. This is inconsistent with the API documentation which says a null
      pointer should be returned if the key doesn't support encoding.

      The last bug is another accessor function

            mullan Sean Mullan
            kryansunw Kevin Ryan (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: