0N/A/*
5360N/A * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A * @test
5360N/A * @bug 4508341 7055362
5360N/A * @library ../../../java/security/testlibrary
0N/A * @summary Test the error conditions of
0N/A * EncryptedPrivateKeyInfo.getKeySpec(...) methods.
0N/A * @author Valerie Peng
0N/A */
0N/Aimport java.security.*;
0N/Aimport java.util.Arrays;
0N/Aimport java.util.Vector;
0N/Aimport java.security.spec.*;
0N/Aimport javax.crypto.*;
0N/Aimport javax.crypto.spec.*;
0N/A
0N/Apublic class GetKeySpecException {
0N/A private static final String cipherAlg = "PBEWithMD5AndDES";
0N/A private static final char[] passwd = { 'p','a','s','s','w','d' };
0N/A private static SecretKey cipherKey;
0N/A private static Cipher cipher = null;
0N/A private static byte[] encryptedData = null;
0N/A private static Provider sunjce = null;
0N/A private static final SecretKey INVALID_KEY =
0N/A new SecretKeySpec(new byte[8], "DES");
0N/A private static AlgorithmParameters BAD_PARAMS;
0N/A private static AlgorithmParameters GOOD_PARAMS;
0N/A
0N/A static {
0N/A try {
0N/A sunjce = Security.getProvider("SunJCE");
0N/A PBEParameterSpec badParamSpec =
0N/A new PBEParameterSpec(new byte[10], 10);
0N/A BAD_PARAMS = AlgorithmParameters.getInstance(cipherAlg, sunjce);
0N/A BAD_PARAMS.init(badParamSpec);
0N/A PBEParameterSpec goodParamSpec =
0N/A new PBEParameterSpec(new byte[8], 1024);
0N/A GOOD_PARAMS = AlgorithmParameters.getInstance(cipherAlg, sunjce);
0N/A GOOD_PARAMS.init(goodParamSpec);
0N/A PBEKeySpec keySpec = new PBEKeySpec(passwd);
0N/A SecretKeyFactory skf =
0N/A SecretKeyFactory.getInstance(cipherAlg, "SunJCE");
0N/A cipherKey = skf.generateSecret(keySpec);
0N/A } catch (Exception ex) {
0N/A // should never happen
0N/A BAD_PARAMS = null;
0N/A GOOD_PARAMS = null;
0N/A }
0N/A }
0N/A
0N/A private static void throwException(String msg) throws Exception {
0N/A throw new Exception(msg);
0N/A }
0N/A
0N/A private static Provider[] removeProviders(String cipherAlg) {
0N/A Vector providers = new Vector();
0N/A boolean done = false;
0N/A while (!done) {
0N/A try {
0N/A Cipher c = Cipher.getInstance(cipherAlg);
0N/A Provider p = c.getProvider();
0N/A providers.add(p);
0N/A Security.removeProvider(p.getName());
0N/A } catch (NoSuchAlgorithmException nsae) {
0N/A done = true;
0N/A } catch (NoSuchPaddingException nspe) {
0N/A // should never happen
0N/A }
0N/A }
0N/A return (Provider[]) (providers.toArray(new Provider[0]));
0N/A }
0N/A
0N/A private static void addProviders(Provider[] provs) {
0N/A for (int i=0; i<provs.length; i++) {
0N/A Security.addProvider(provs[i]);
0N/A }
0N/A }
0N/A
5360N/A public static void main(String[] args) throws Exception {
5360N/A ProvidersSnapshot snapshot = ProvidersSnapshot.create();
5360N/A try {
5360N/A main0(args);
5360N/A } finally {
5360N/A snapshot.restore();
5360N/A }
5360N/A }
5360N/A
5360N/A public static void main0(String[] args) throws Exception {
0N/A if ((GOOD_PARAMS == null) || (BAD_PARAMS == null)) {
0N/A throw new Exception("Static parameter generation failed");
0N/A }
0N/A // use random data
0N/A byte[] encryptedData = new byte[30];
0N/A encryptedData[20] = (byte) 8;
0N/A
0N/A PKCS8EncodedKeySpec pkcs8Spec = null;
0N/A
0N/A // generate encrypted data and EncryptedPrivateKeyInfo objects
0N/A EncryptedPrivateKeyInfo epki =
0N/A new EncryptedPrivateKeyInfo(GOOD_PARAMS, encryptedData);
0N/A EncryptedPrivateKeyInfo epkiBad =
0N/A new EncryptedPrivateKeyInfo(BAD_PARAMS, encryptedData);
0N/A
0N/A // TEST#1: getKeySpec(Cipher)
0N/A System.out.println("Testing getKeySpec(Cipher)...");
0N/A try {
0N/A pkcs8Spec = epki.getKeySpec((Cipher) null);
0N/A throwException("Should throw NPE for null Cipher!");
0N/A } catch (NullPointerException npe) {
0N/A System.out.println("Expected NPE thrown");
0N/A }
0N/A
0N/A // TEST#2: getKeySpec(Key)
0N/A System.out.println("Testing getKeySpec(Key)...");
0N/A try {
0N/A pkcs8Spec = epki.getKeySpec((Key) null);
0N/A throwException("Should throw NPE for null Key!");
0N/A } catch (NullPointerException npe) {
0N/A System.out.println("Expected NPE thrown");
0N/A }
0N/A try {
0N/A pkcs8Spec = epki.getKeySpec(INVALID_KEY);
0N/A throwException("Should throw IKE for invalid Key!");
0N/A } catch (InvalidKeyException ikse) {
0N/A System.out.println("Expected IKE thrown");
0N/A }
0N/A try {
0N/A pkcs8Spec = epkiBad.getKeySpec(cipherKey);
0N/A throwException("Should throw IKE for corrupted epki!");
0N/A } catch (InvalidKeyException ike) {
0N/A System.out.println("Expected IKE thrown");
0N/A }
0N/A Provider[] removedProvs = null;
0N/A try {
0N/A removedProvs = removeProviders(cipherAlg);
0N/A pkcs8Spec = epki.getKeySpec(cipherKey);
0N/A throwException("Should throw NSAE if no matching impl!");
0N/A } catch (NoSuchAlgorithmException nsae) {
0N/A System.out.println("Expected NSAE thrown");
0N/A addProviders(removedProvs);
0N/A }
0N/A // TEST#3: getKeySpec(Key, String)
0N/A System.out.println("Testing getKeySpec(Key, String)...");
0N/A try {
0N/A pkcs8Spec = epki.getKeySpec(null, "SunJCE");
0N/A throwException("Should throw NPE for null Key!");
0N/A } catch (NullPointerException npe) {
0N/A System.out.println("Expected NPE thrown");
0N/A }
0N/A try {
0N/A pkcs8Spec = epki.getKeySpec(cipherKey, (String)null);
0N/A throwException("Should throw NPE for null String!");
0N/A } catch (NullPointerException npe) {
0N/A System.out.println("Expected NPE thrown");
0N/A }
0N/A try {
0N/A pkcs8Spec = epki.getKeySpec(INVALID_KEY, "SunJCE");
0N/A throwException("Should throw IKE for invalid Key!");
0N/A } catch (InvalidKeyException ikse) {
0N/A System.out.println("Expected IKE thrown");
0N/A }
0N/A try {
0N/A pkcs8Spec = epkiBad.getKeySpec(cipherKey, "SunJCE");
0N/A throwException("Should throw IKE for corrupted epki!");
0N/A } catch (InvalidKeyException ike) {
0N/A System.out.println("Expected IKE thrown");
0N/A }
0N/A try {
0N/A pkcs8Spec = epki.getKeySpec(cipherKey, "SUN");
0N/A throwException("Should throw NSAE for provider without " +
0N/A "matching implementation!");
0N/A } catch (NoSuchAlgorithmException nsae) {
0N/A System.out.println("Expected NSAE thrown");
0N/A }
0N/A try {
0N/A Security.removeProvider("SunJCE");
0N/A pkcs8Spec = epki.getKeySpec(cipherKey, "SunJCE");
0N/A throwException("Should throw NSPE for unconfigured provider!");
0N/A } catch (NoSuchProviderException nspe) {
0N/A System.out.println("Expected NSPE thrown");
0N/A Security.addProvider(sunjce);
0N/A }
0N/A // TEST#4: getKeySpec(Key, Provider)
0N/A System.out.println("Testing getKeySpec(Key, Provider)...");
0N/A try {
0N/A pkcs8Spec = epki.getKeySpec(null, sunjce);
0N/A throwException("Should throw NPE for null Key!");
0N/A } catch (NullPointerException npe) {
0N/A System.out.println("Expected NPE thrown");
0N/A }
0N/A try {
0N/A pkcs8Spec = epki.getKeySpec(cipherKey, (Provider)null);
0N/A throwException("Should throw NPE for null Provider!");
0N/A } catch (NullPointerException npe) {
0N/A System.out.println("Expected NPE thrown");
0N/A }
0N/A try {
0N/A pkcs8Spec = epki.getKeySpec(INVALID_KEY, sunjce);
0N/A throwException("Should throw IKE for invalid Key!");
0N/A } catch (InvalidKeyException ikse) {
0N/A System.out.println("Expected IKE thrown");
0N/A }
0N/A try {
0N/A pkcs8Spec = epkiBad.getKeySpec(cipherKey, sunjce);
0N/A throwException("Should throw IKE for corrupted epki!");
0N/A } catch (InvalidKeyException ike) {
0N/A System.out.println("Expected IKE thrown");
0N/A }
0N/A System.out.println("All Tests Passed");
0N/A }
0N/A}