< prev index next >

test/sun/security/pkcs/pkcs8/PKCS8Test.java

Print this page




   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8048357
  27  * @summary PKCS8 Standards Conformance Tests

  28  * @modules java.base/sun.security.pkcs
  29  *          java.base/sun.security.util
  30  *          java.base/sun.security.provider
  31  *          java.base/sun.security.x509
  32  *          java.base/sun.misc
  33  * @compile -XDignore.symbol.file PKCS8Test.java
  34  * @run main PKCS8Test
  35  */





  36 import java.io.IOException;
  37 import java.math.BigInteger;
  38 import java.security.InvalidKeyException;
  39 import java.util.Arrays;
  40 import sun.misc.HexDumpEncoder;
  41 import sun.security.pkcs.PKCS8Key;
  42 import sun.security.provider.DSAPrivateKey;
  43 import sun.security.util.DerOutputStream;
  44 import sun.security.util.DerValue;
  45 import sun.security.x509.AlgorithmId;
  46 
  47 import static java.lang.System.out;
  48 
  49 public class PKCS8Test {
  50 
  51     static final HexDumpEncoder hexDump = new HexDumpEncoder();
  52 
  53     static final DerOutputStream derOutput = new DerOutputStream();
  54 
  55     static final String FORMAT = "PKCS#8";
  56     static final String EXPECTED_ALG_ID_CHRS = "DSA\n\tp:     02\n\tq:     03\n"
  57             + "\tg:     04\n";
  58     static final String ALGORITHM = "DSA";
  59     static final String EXCEPTION_MESSAGE = "version mismatch: (supported:     "
  60             + "00, parsed:     01";
  61 
  62     // test second branch in byte[] encode()
  63     // DER encoding,include (empty) set of attributes
  64     static final int[] NEW_ENCODED_KEY_INTS = { 0x30,
  65             // length 30 = 0x1e
  66             0x1e,


 174             0x01,
 175             // length
 176             0x04,
 177             // privateKey contents
 178             0x04, 0x03, 0x02,
 179             // 4th (optional) element -- attributes [0] IMPLICIT Attributes
 180             // OPTIONAL
 181             // (Attributes = SET OF Attribute) Here, it will be empty.
 182             0x01,
 183             // length
 184             0x01 };
 185 
 186     static void raiseException(String expected, String received) {
 187         throw new RuntimeException(
 188                 "Expected " + expected + "; Received " + received);
 189     }
 190 
 191     public static void main(String[] args)
 192             throws IOException, InvalidKeyException {
 193 
 194         byte[] encodedKey = getEncodedKey();







 195         byte[] expectedBytes = new byte[EXPECTED.length];
 196         for (int i = 0; i < EXPECTED.length; i++) {
 197             expectedBytes[i] = (byte) EXPECTED[i];
 198         }
 199 
 200         dumpByteArray("encodedKey :", encodedKey);
 201         if (!Arrays.equals(encodedKey, expectedBytes)) {
 202             raiseException(new String(expectedBytes), new String(encodedKey));
 203         }
 204 
 205         PKCS8Key decodedKey = PKCS8Key.parse(new DerValue(encodedKey));

 206         String alg = decodedKey.getAlgorithm();
 207         AlgorithmId algId = decodedKey.getAlgorithmId();
 208         out.println("Algorithm :" + alg);
 209         out.println("AlgorithmId: " + algId);
 210 
 211         if (!ALGORITHM.equals(alg)) {
 212             raiseException(ALGORITHM, alg);
 213         }
 214         if (!EXPECTED_ALG_ID_CHRS.equalsIgnoreCase(algId.toString())) {
 215             raiseException(EXPECTED_ALG_ID_CHRS, algId.toString());
 216         }
 217 
 218         decodedKey.encode(derOutput);
 219         dumpByteArray("Stream encode: ", derOutput.toByteArray());
 220         if (!Arrays.equals(derOutput.toByteArray(), expectedBytes)) {
 221             raiseException(new String(expectedBytes), derOutput.toString());
 222         }
 223 
 224         dumpByteArray("byte[] encoding: ", decodedKey.getEncoded());
 225         if (!Arrays.equals(decodedKey.getEncoded(), expectedBytes)) {


 248 
 249         try {
 250             byte[] newEncodedKey2 = new byte[NEW_ENCODED_KEY_INTS_2.length];
 251             for (int i = 0; i < newEncodedKey2.length; i++) {
 252                 newEncodedKey2[i] = (byte) NEW_ENCODED_KEY_INTS_2[i];
 253             }
 254 
 255             PKCS8Key newDecodedKey2 = PKCS8Key
 256                     .parse(new DerValue(newEncodedKey2));
 257 
 258             throw new RuntimeException(
 259                     "key2: Expected an IOException during " + "parsing");
 260         } catch (IOException e) {
 261             out.println("Key 2: should be illegal version");
 262             out.println(e.getMessage());
 263             if (!EXCEPTION_MESSAGE.equals(e.getMessage())) {
 264                 throw new RuntimeException("Key2: expected: "
 265                         + EXCEPTION_MESSAGE + " get: " + e.getMessage());
 266             }
 267         }
 268 
 269     }
 270 
 271     // get a byte array from somewhere
 272     static byte[] getEncodedKey() throws InvalidKeyException {
 273         BigInteger p = BigInteger.valueOf(1);
 274         BigInteger q = BigInteger.valueOf(2);
 275         BigInteger g = BigInteger.valueOf(3);
 276         BigInteger x = BigInteger.valueOf(4);
 277 
 278         DSAPrivateKey priv = new DSAPrivateKey(p, q, g, x);
 279         return priv.getEncoded();
 280     }
 281 
 282     static void dumpByteArray(String nm, byte[] bytes) throws IOException {
 283         out.println(nm + " length: " + bytes.length);
 284         hexDump.encodeBuffer(bytes, out);
 285     }
 286 
 287     static String toString(PKCS8Key key) {
 288         StringBuilder builder = new StringBuilder(key.getAlgorithm());
 289         builder.append('\n').append("parameters:")
 290                 .append(key.getAlgorithmId().toString());
 291         return builder.toString();
 292     }
 293 
 294 }


   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8048357
  27  * @summary PKCS8 Standards Conformance Tests
  28  * @requires (os.family != "solaris")
  29  * @modules java.base/sun.security.pkcs
  30  *          java.base/sun.security.util
  31  *          java.base/sun.security.provider
  32  *          java.base/sun.security.x509
  33  *          java.base/sun.misc
  34  * @compile -XDignore.symbol.file PKCS8Test.java
  35  * @run main PKCS8Test
  36  */
  37 
  38 /*
  39  * Skip Solaris since the DSAPrivateKeys returned by
  40  * SunPKCS11 Provider are not subclasses of PKCS8Key
  41  */
  42 import java.io.IOException;
  43 import java.math.BigInteger;
  44 import java.security.InvalidKeyException;
  45 import java.util.Arrays;
  46 import sun.misc.HexDumpEncoder;
  47 import sun.security.pkcs.PKCS8Key;
  48 import sun.security.provider.DSAPrivateKey;
  49 import sun.security.util.DerOutputStream;
  50 import sun.security.util.DerValue;
  51 import sun.security.x509.AlgorithmId;

  52 import static java.lang.System.out;
  53 
  54 public class PKCS8Test {
  55 
  56     static final HexDumpEncoder hexDump = new HexDumpEncoder();
  57 
  58     static final DerOutputStream derOutput = new DerOutputStream();
  59 
  60     static final String FORMAT = "PKCS#8";
  61     static final String EXPECTED_ALG_ID_CHRS = "DSA\n\tp:     02\n\tq:     03\n"
  62             + "\tg:     04\n";
  63     static final String ALGORITHM = "DSA";
  64     static final String EXCEPTION_MESSAGE = "version mismatch: (supported:     "
  65             + "00, parsed:     01";
  66 
  67     // test second branch in byte[] encode()
  68     // DER encoding,include (empty) set of attributes
  69     static final int[] NEW_ENCODED_KEY_INTS = { 0x30,
  70             // length 30 = 0x1e
  71             0x1e,


 179             0x01,
 180             // length
 181             0x04,
 182             // privateKey contents
 183             0x04, 0x03, 0x02,
 184             // 4th (optional) element -- attributes [0] IMPLICIT Attributes
 185             // OPTIONAL
 186             // (Attributes = SET OF Attribute) Here, it will be empty.
 187             0x01,
 188             // length
 189             0x01 };
 190 
 191     static void raiseException(String expected, String received) {
 192         throw new RuntimeException(
 193                 "Expected " + expected + "; Received " + received);
 194     }
 195 
 196     public static void main(String[] args)
 197             throws IOException, InvalidKeyException {
 198 
 199         BigInteger p = BigInteger.valueOf(1);
 200         BigInteger q = BigInteger.valueOf(2);
 201         BigInteger g = BigInteger.valueOf(3);
 202         BigInteger x = BigInteger.valueOf(4);
 203 
 204         DSAPrivateKey priv = new DSAPrivateKey(p, q, g, x);
 205 
 206         byte[] encodedKey = priv.getEncoded();
 207         byte[] expectedBytes = new byte[EXPECTED.length];
 208         for (int i = 0; i < EXPECTED.length; i++) {
 209             expectedBytes[i] = (byte) EXPECTED[i];
 210         }
 211 
 212         dumpByteArray("encodedKey :", encodedKey);
 213         if (!Arrays.equals(encodedKey, expectedBytes)) {
 214             raiseException(new String(expectedBytes), new String(encodedKey));
 215         }
 216 
 217         PKCS8Key decodedKey = PKCS8Key.parse(new DerValue(encodedKey));
 218 
 219         String alg = decodedKey.getAlgorithm();
 220         AlgorithmId algId = decodedKey.getAlgorithmId();
 221         out.println("Algorithm :" + alg);
 222         out.println("AlgorithmId: " + algId);
 223 
 224         if (!ALGORITHM.equals(alg)) {
 225             raiseException(ALGORITHM, alg);
 226         }
 227         if (!EXPECTED_ALG_ID_CHRS.equalsIgnoreCase(algId.toString())) {
 228             raiseException(EXPECTED_ALG_ID_CHRS, algId.toString());
 229         }
 230 
 231         decodedKey.encode(derOutput);
 232         dumpByteArray("Stream encode: ", derOutput.toByteArray());
 233         if (!Arrays.equals(derOutput.toByteArray(), expectedBytes)) {
 234             raiseException(new String(expectedBytes), derOutput.toString());
 235         }
 236 
 237         dumpByteArray("byte[] encoding: ", decodedKey.getEncoded());
 238         if (!Arrays.equals(decodedKey.getEncoded(), expectedBytes)) {


 261 
 262         try {
 263             byte[] newEncodedKey2 = new byte[NEW_ENCODED_KEY_INTS_2.length];
 264             for (int i = 0; i < newEncodedKey2.length; i++) {
 265                 newEncodedKey2[i] = (byte) NEW_ENCODED_KEY_INTS_2[i];
 266             }
 267 
 268             PKCS8Key newDecodedKey2 = PKCS8Key
 269                     .parse(new DerValue(newEncodedKey2));
 270 
 271             throw new RuntimeException(
 272                     "key2: Expected an IOException during " + "parsing");
 273         } catch (IOException e) {
 274             out.println("Key 2: should be illegal version");
 275             out.println(e.getMessage());
 276             if (!EXCEPTION_MESSAGE.equals(e.getMessage())) {
 277                 throw new RuntimeException("Key2: expected: "
 278                         + EXCEPTION_MESSAGE + " get: " + e.getMessage());
 279             }
 280         }












 281     }
 282 
 283     static void dumpByteArray(String nm, byte[] bytes) throws IOException {
 284         out.println(nm + " length: " + bytes.length);
 285         hexDump.encodeBuffer(bytes, out);
 286     }








 287 }
< prev index next >