--- old/test/sun/security/pkcs/pkcs8/PKCS8Test.java 2015-11-24 13:59:38.000000000 -0800 +++ new/test/sun/security/pkcs/pkcs8/PKCS8Test.java 2015-11-24 13:59:38.000000000 -0800 @@ -43,7 +43,6 @@ import sun.security.util.DerOutputStream; import sun.security.util.DerValue; import sun.security.x509.AlgorithmId; - import static java.lang.System.out; public class PKCS8Test { @@ -191,7 +190,14 @@ public static void main(String[] args) throws IOException, InvalidKeyException { - byte[] encodedKey = getEncodedKey(); + BigInteger p = BigInteger.valueOf(1); + BigInteger q = BigInteger.valueOf(2); + BigInteger g = BigInteger.valueOf(3); + BigInteger x = BigInteger.valueOf(4); + + DSAPrivateKey priv = new DSAPrivateKey(p, q, g, x); + + byte[] encodedKey = priv.getEncoded(); byte[] expectedBytes = new byte[EXPECTED.length]; for (int i = 0; i < EXPECTED.length; i++) { expectedBytes[i] = (byte) EXPECTED[i]; @@ -202,93 +208,80 @@ raiseException(new String(expectedBytes), new String(encodedKey)); } - PKCS8Key decodedKey = PKCS8Key.parse(new DerValue(encodedKey)); - String alg = decodedKey.getAlgorithm(); - AlgorithmId algId = decodedKey.getAlgorithmId(); - out.println("Algorithm :" + alg); - out.println("AlgorithmId: " + algId); + // skip Solaris since the DSAPrivateKeys returned by SunPKCS11 Provider + // are not a subclasses of PKCS8Key + if (!System.getProperty("os.name").equalsIgnoreCase("sunos")) { + PKCS8Key decodedKey = PKCS8Key.parse(new DerValue(encodedKey)); + + String alg = decodedKey.getAlgorithm(); + AlgorithmId algId = decodedKey.getAlgorithmId(); + out.println("Algorithm :" + alg); + out.println("AlgorithmId: " + algId); - if (!ALGORITHM.equals(alg)) { - raiseException(ALGORITHM, alg); - } - if (!EXPECTED_ALG_ID_CHRS.equalsIgnoreCase(algId.toString())) { - raiseException(EXPECTED_ALG_ID_CHRS, algId.toString()); - } + if (!ALGORITHM.equals(alg)) { + raiseException(ALGORITHM, alg); + } + if (!EXPECTED_ALG_ID_CHRS.equalsIgnoreCase(algId.toString())) { + raiseException(EXPECTED_ALG_ID_CHRS, algId.toString()); + } - decodedKey.encode(derOutput); - dumpByteArray("Stream encode: ", derOutput.toByteArray()); - if (!Arrays.equals(derOutput.toByteArray(), expectedBytes)) { - raiseException(new String(expectedBytes), derOutput.toString()); - } + decodedKey.encode(derOutput); + dumpByteArray("Stream encode: ", derOutput.toByteArray()); + if (!Arrays.equals(derOutput.toByteArray(), expectedBytes)) { + raiseException(new String(expectedBytes), derOutput.toString()); + } - dumpByteArray("byte[] encoding: ", decodedKey.getEncoded()); - if (!Arrays.equals(decodedKey.getEncoded(), expectedBytes)) { - raiseException(new String(expectedBytes), - new String(decodedKey.getEncoded())); - } + dumpByteArray("byte[] encoding: ", decodedKey.getEncoded()); + if (!Arrays.equals(decodedKey.getEncoded(), expectedBytes)) { + raiseException(new String(expectedBytes), + new String(decodedKey.getEncoded())); + } - if (!FORMAT.equals(decodedKey.getFormat())) { - raiseException(FORMAT, decodedKey.getFormat()); - } + if (!FORMAT.equals(decodedKey.getFormat())) { + raiseException(FORMAT, decodedKey.getFormat()); + } - try { - byte[] newEncodedKey = new byte[NEW_ENCODED_KEY_INTS.length]; - for (int i = 0; i < newEncodedKey.length; i++) { - newEncodedKey[i] = (byte) NEW_ENCODED_KEY_INTS[i]; - } - PKCS8Key newDecodedKey = PKCS8Key - .parse(new DerValue(newEncodedKey)); - - throw new RuntimeException( - "key1: Expected an IOException during " + "parsing"); - } catch (IOException e) { - System.out.println("newEncodedKey: should have excess data due to " - + "attributes, which are not supported"); - } + try { + byte[] newEncodedKey = new byte[NEW_ENCODED_KEY_INTS.length]; + for (int i = 0; i < newEncodedKey.length; i++) { + newEncodedKey[i] = (byte) NEW_ENCODED_KEY_INTS[i]; + } + PKCS8Key newDecodedKey = PKCS8Key + .parse(new DerValue(newEncodedKey)); + + throw new RuntimeException( + "key1: Expected an IOException during " + "parsing"); + } catch (IOException e) { + System.out.println( + "newEncodedKey: should have excess data due to " + + "attributes, which are not supported"); + } - try { - byte[] newEncodedKey2 = new byte[NEW_ENCODED_KEY_INTS_2.length]; - for (int i = 0; i < newEncodedKey2.length; i++) { - newEncodedKey2[i] = (byte) NEW_ENCODED_KEY_INTS_2[i]; - } - - PKCS8Key newDecodedKey2 = PKCS8Key - .parse(new DerValue(newEncodedKey2)); - - throw new RuntimeException( - "key2: Expected an IOException during " + "parsing"); - } catch (IOException e) { - out.println("Key 2: should be illegal version"); - out.println(e.getMessage()); - if (!EXCEPTION_MESSAGE.equals(e.getMessage())) { - throw new RuntimeException("Key2: expected: " - + EXCEPTION_MESSAGE + " get: " + e.getMessage()); + try { + byte[] newEncodedKey2 = new byte[NEW_ENCODED_KEY_INTS_2.length]; + for (int i = 0; i < newEncodedKey2.length; i++) { + newEncodedKey2[i] = (byte) NEW_ENCODED_KEY_INTS_2[i]; + } + + PKCS8Key newDecodedKey2 = PKCS8Key + .parse(new DerValue(newEncodedKey2)); + + throw new RuntimeException( + "key2: Expected an IOException during " + "parsing"); + } catch (IOException e) { + out.println("Key 2: should be illegal version"); + out.println(e.getMessage()); + if (!EXCEPTION_MESSAGE.equals(e.getMessage())) { + throw new RuntimeException("Key2: expected: " + + EXCEPTION_MESSAGE + " get: " + e.getMessage()); + } } } } - // get a byte array from somewhere - static byte[] getEncodedKey() throws InvalidKeyException { - BigInteger p = BigInteger.valueOf(1); - BigInteger q = BigInteger.valueOf(2); - BigInteger g = BigInteger.valueOf(3); - BigInteger x = BigInteger.valueOf(4); - - DSAPrivateKey priv = new DSAPrivateKey(p, q, g, x); - return priv.getEncoded(); - } - static void dumpByteArray(String nm, byte[] bytes) throws IOException { out.println(nm + " length: " + bytes.length); hexDump.encodeBuffer(bytes, out); } - - static String toString(PKCS8Key key) { - StringBuilder builder = new StringBuilder(key.getAlgorithm()); - builder.append('\n').append("parameters:") - .append(key.getAlgorithmId().toString()); - return builder.toString(); - } - }