changeset 9724:303a29c24e3b jdk7u211-b02

8219570: JDK-6383200 wrongly extends PBEParameterSpec API Reviewed-by: mbalao
author andrew
date Fri, 22 Feb 2019 11:34:23 +0000
parents 758e83340d0a
children ca1fa5965ae7
files make/com/sun/crypto/provider/Makefile make/common/Release.gmk src/share/classes/com/sun/crypto/provider/PBEParameters.java src/share/classes/com/sun/crypto/provider/PBES2Core.java src/share/classes/com/sun/crypto/provider/PBES2Parameters.java src/share/classes/com/sun/crypto/spec/PBE2ParameterSpec.java src/share/classes/javax/crypto/spec/PBEParameterSpec.java test/com/sun/crypto/provider/Cipher/PBE/PBES2Test.java
diffstat 8 files changed, 103 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/make/com/sun/crypto/provider/Makefile	Thu Feb 21 03:38:58 2019 +0000
+++ b/make/com/sun/crypto/provider/Makefile	Fri Feb 22 11:34:23 2019 +0000
@@ -126,6 +126,7 @@
 # Subdirectories of these are automatically included.
 #
 AUTO_FILES_JAVA_DIRS = \
+    com/sun/crypto/spec \
     com/sun/crypto/provider
 
 include $(BUILDDIR)/common/Classes.gmk
--- a/make/common/Release.gmk	Thu Feb 21 03:38:58 2019 +0000
+++ b/make/common/Release.gmk	Fri Feb 22 11:34:23 2019 +0000
@@ -606,6 +606,7 @@
 	$(ECHO) "com/sun/net/ssl/internal/ssl/" >> $@
 	$(ECHO) "javax/crypto/" >> $@
 	$(ECHO) "sun/security/internal/" >> $@
+	$(ECHO) "com/sun/crypto/spec/" >> $@
 	$(ECHO) "com/sun/crypto/provider/" >> $@
 	$(ECHO) "META-INF/services/com.sun.tools.attach.spi.AttachProvider" >> $@
 	$(ECHO) "com/sun/tools/attach/" >> $@
--- a/src/share/classes/com/sun/crypto/provider/PBEParameters.java	Thu Feb 21 03:38:58 2019 +0000
+++ b/src/share/classes/com/sun/crypto/provider/PBEParameters.java	Fri Feb 22 11:34:23 2019 +0000
@@ -25,6 +25,8 @@
 
 package com.sun.crypto.provider;
 
+import com.sun.crypto.spec.PBE2ParameterSpec;
+
 import java.io.*;
 import java.math.BigInteger;
 import java.security.AlgorithmParametersSpi;
@@ -69,7 +71,9 @@
        }
        this.salt = ((PBEParameterSpec)paramSpec).getSalt().clone();
        this.iCount = ((PBEParameterSpec)paramSpec).getIterationCount();
-       this.cipherParam = ((PBEParameterSpec)paramSpec).getParameterSpec();
+       if (paramSpec instanceof PBE2ParameterSpec) {
+           this.cipherParam = ((PBE2ParameterSpec)paramSpec).getParameterSpec();
+       }
     }
 
     protected void engineInit(byte[] encoded)
@@ -107,7 +111,7 @@
     {
         if (PBEParameterSpec.class.isAssignableFrom(paramSpec)) {
             return paramSpec.cast(
-                new PBEParameterSpec(this.salt, this.iCount, this.cipherParam));
+                new PBE2ParameterSpec(this.salt, this.iCount, this.cipherParam));
         } else {
             throw new InvalidParameterSpecException
                 ("Inappropriate parameter specification");
--- a/src/share/classes/com/sun/crypto/provider/PBES2Core.java	Thu Feb 21 03:38:58 2019 +0000
+++ b/src/share/classes/com/sun/crypto/provider/PBES2Core.java	Fri Feb 22 11:34:23 2019 +0000
@@ -25,6 +25,8 @@
 
 package com.sun.crypto.provider;
 
+import com.sun.crypto.spec.PBE2ParameterSpec;
+
 import java.io.UnsupportedEncodingException;
 import java.security.*;
 import java.security.spec.*;
@@ -144,7 +146,7 @@
             SunJCE.RANDOM.nextBytes(ivBytes);
             ivSpec = new IvParameterSpec(ivBytes);
         }
-        PBEParameterSpec pbeSpec = new PBEParameterSpec(salt, iCount, ivSpec);
+        PBE2ParameterSpec pbeSpec =new PBE2ParameterSpec(salt, iCount, ivSpec);
         try {
             params = AlgorithmParameters.getInstance(pbeAlgo, "SunJCE");
         } catch (NoSuchAlgorithmException nsae) {
@@ -247,9 +249,9 @@
                 }
                 iCount = specICount;
 
-                AlgorithmParameterSpec specParams =
-                        ((PBEParameterSpec) params).getParameterSpec();
-                if (specParams != null) {
+                AlgorithmParameterSpec specParams;
+                if (params instanceof PBE2ParameterSpec &&
+                   (specParams = ((PBE2ParameterSpec) params).getParameterSpec()) != null) {
                     if (specParams instanceof IvParameterSpec) {
                         ivSpec = (IvParameterSpec)specParams;
                     } else {
--- a/src/share/classes/com/sun/crypto/provider/PBES2Parameters.java	Thu Feb 21 03:38:58 2019 +0000
+++ b/src/share/classes/com/sun/crypto/provider/PBES2Parameters.java	Fri Feb 22 11:34:23 2019 +0000
@@ -25,6 +25,8 @@
 
 package com.sun.crypto.provider;
 
+import com.sun.crypto.spec.PBE2ParameterSpec;
+
 import java.io.*;
 import java.math.BigInteger;
 import java.security.NoSuchAlgorithmException;
@@ -250,7 +252,9 @@
        }
        this.salt = ((PBEParameterSpec)paramSpec).getSalt().clone();
        this.iCount = ((PBEParameterSpec)paramSpec).getIterationCount();
-       this.cipherParam = ((PBEParameterSpec)paramSpec).getParameterSpec();
+       if (paramSpec instanceof PBE2ParameterSpec) {
+           this.cipherParam = ((PBE2ParameterSpec)paramSpec).getParameterSpec();
+       }
     }
 
     protected void engineInit(byte[] encoded)
@@ -387,7 +391,7 @@
     {
         if (PBEParameterSpec.class.isAssignableFrom(paramSpec)) {
             return paramSpec.cast(
-                new PBEParameterSpec(this.salt, this.iCount, this.cipherParam));
+                new PBE2ParameterSpec(this.salt, this.iCount, this.cipherParam));
         } else {
             throw new InvalidParameterSpecException
                 ("Inappropriate parameter specification");
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/share/classes/com/sun/crypto/spec/PBE2ParameterSpec.java	Fri Feb 22 11:34:23 2019 +0000
@@ -0,0 +1,75 @@
+/*
+ * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package com.sun.crypto.spec;
+
+import java.security.spec.AlgorithmParameterSpec;
+
+import javax.crypto.spec.PBEParameterSpec;
+
+/**
+ * This class specifies the set of parameters used with generation
+ * 2 password-based encryption (PBE), as defined in the
+ * <a href="http://www.ietf.org/rfc/rfc2898.txt">PKCS #5</a>
+ * standard.
+ *
+ * @author Jan Luehe
+ *
+ * @since 1.7
+ */
+public class PBE2ParameterSpec extends PBEParameterSpec {
+
+    private AlgorithmParameterSpec paramSpec = null;
+
+    /**
+     * Constructs a parameter set for password-based encryption as defined in
+     * the PKCS #5 standard.
+     *
+     * @param salt the salt. The contents of <code>salt</code> are copied
+     * to protect against subsequent modification.
+     * @param iterationCount the iteration count.
+     * @param paramSpec the cipher algorithm parameter specification.
+     * @exception NullPointerException if <code>salt</code> is null.
+     *
+     * @since 1.8
+     */
+    public PBE2ParameterSpec(byte[] salt, int iterationCount,
+                            AlgorithmParameterSpec paramSpec) {
+        super(salt, iterationCount);
+        this.paramSpec = paramSpec;
+    }
+
+    /**
+     * Returns the cipher algorithm parameter specification.
+     *
+     * @return the parameter specification, or null if none was set.
+     *
+     * @since 1.8
+     */
+    public AlgorithmParameterSpec getParameterSpec() {
+        return this.paramSpec;
+    }
+
+}
--- a/src/share/classes/javax/crypto/spec/PBEParameterSpec.java	Thu Feb 21 03:38:58 2019 +0000
+++ b/src/share/classes/javax/crypto/spec/PBEParameterSpec.java	Fri Feb 22 11:34:23 2019 +0000
@@ -41,7 +41,6 @@
 
     private byte[] salt;
     private int iterationCount;
-    private AlgorithmParameterSpec paramSpec = null;
 
     /**
      * Constructs a parameter set for password-based encryption as defined in
@@ -58,25 +57,6 @@
     }
 
     /**
-     * Constructs a parameter set for password-based encryption as defined in
-     * the PKCS #5 standard.
-     *
-     * @param salt the salt. The contents of <code>salt</code> are copied
-     * to protect against subsequent modification.
-     * @param iterationCount the iteration count.
-     * @param paramSpec the cipher algorithm parameter specification.
-     * @exception NullPointerException if <code>salt</code> is null.
-     *
-     * @since 1.8
-     */
-    public PBEParameterSpec(byte[] salt, int iterationCount,
-            AlgorithmParameterSpec paramSpec) {
-        this.salt = salt.clone();
-        this.iterationCount = iterationCount;
-        this.paramSpec = paramSpec;
-    }
-
-    /**
      * Returns the salt.
      *
      * @return the salt. Returns a new array
@@ -95,14 +75,4 @@
         return this.iterationCount;
     }
 
-    /**
-     * Returns the cipher algorithm parameter specification.
-     *
-     * @return the parameter specification, or null if none was set.
-     *
-     * @since 1.8
-     */
-    public AlgorithmParameterSpec getParameterSpec() {
-        return this.paramSpec;
-    }
 }
--- a/test/com/sun/crypto/provider/Cipher/PBE/PBES2Test.java	Thu Feb 21 03:38:58 2019 +0000
+++ b/test/com/sun/crypto/provider/Cipher/PBE/PBES2Test.java	Fri Feb 22 11:34:23 2019 +0000
@@ -23,9 +23,12 @@
 
 /*
  * @test
- * @bug 6383200
+ * @bug 6383200 8219570
  * @summary PBE: need new algorithm support in password based encryption
  */
+
+import com.sun.crypto.spec.PBE2ParameterSpec;
+
 import java.security.*;
 import java.util.Arrays;
 import javax.crypto.*;
@@ -76,8 +79,8 @@
         Cipher pbeCipher = Cipher.getInstance(algo);
         if (suppliedParams) {
             pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey,
-                new PBEParameterSpec(salt, iterationCount,
-                    new IvParameterSpec(ivBytes)));
+                new PBE2ParameterSpec(salt, iterationCount,
+                                      new IvParameterSpec(ivBytes)));
         } else {
             pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey);
         }
@@ -99,8 +102,8 @@
         if (suppliedParams) {
             iv = ivBytes;
         } else {
-            PBEParameterSpec pbeSpec =
-                aps.getParameterSpec(PBEParameterSpec.class);
+            PBE2ParameterSpec pbeSpec =
+                aps.getParameterSpec(PBE2ParameterSpec.class);
             salt = pbeSpec.getSalt();
             iterationCount = pbeSpec.getIterationCount();
             IvParameterSpec ivSpec =