Path: blob/master/test/jdk/javax/crypto/EncryptedPrivateKeyInfo/GetKeySpecException.java
41152 views
/*1* Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 4508341 705536226* @library ../../../java/security/testlibrary27* @summary Test the error conditions of28* EncryptedPrivateKeyInfo.getKeySpec(...) methods.29* @author Valerie Peng30*/31import java.security.*;32import java.util.Arrays;33import java.util.Vector;34import java.security.spec.*;35import javax.crypto.*;36import javax.crypto.spec.*;3738public class GetKeySpecException {39private static final String cipherAlg = "PBEWithMD5AndDES";40private static final char[] passwd = { 'p','a','s','s','w','d' };41private static SecretKey cipherKey;42private static Cipher cipher = null;43private static byte[] encryptedData = null;44private static Provider sunjce = null;45private static final SecretKey INVALID_KEY =46new SecretKeySpec(new byte[8], "DES");47private static AlgorithmParameters BAD_PARAMS;48private static AlgorithmParameters GOOD_PARAMS;4950static {51try {52sunjce = Security.getProvider("SunJCE");53PBEParameterSpec badParamSpec =54new PBEParameterSpec(new byte[10], 10);55BAD_PARAMS = AlgorithmParameters.getInstance(cipherAlg, sunjce);56BAD_PARAMS.init(badParamSpec);57PBEParameterSpec goodParamSpec =58new PBEParameterSpec(new byte[8], 1024);59GOOD_PARAMS = AlgorithmParameters.getInstance(cipherAlg, sunjce);60GOOD_PARAMS.init(goodParamSpec);61PBEKeySpec keySpec = new PBEKeySpec(passwd);62SecretKeyFactory skf =63SecretKeyFactory.getInstance(cipherAlg, "SunJCE");64cipherKey = skf.generateSecret(keySpec);65} catch (Exception ex) {66// should never happen67BAD_PARAMS = null;68GOOD_PARAMS = null;69}70}7172private static void throwException(String msg) throws Exception {73throw new Exception(msg);74}7576private static Provider[] removeProviders(String cipherAlg) {77Vector providers = new Vector();78boolean done = false;79while (!done) {80try {81Cipher c = Cipher.getInstance(cipherAlg);82Provider p = c.getProvider();83providers.add(p);84Security.removeProvider(p.getName());85} catch (NoSuchAlgorithmException nsae) {86done = true;87} catch (NoSuchPaddingException nspe) {88// should never happen89}90}91return (Provider[]) (providers.toArray(new Provider[0]));92}9394private static void addProviders(Provider[] provs) {95for (int i=0; i<provs.length; i++) {96Security.addProvider(provs[i]);97}98}99100public static void main(String[] args) throws Exception {101ProvidersSnapshot snapshot = ProvidersSnapshot.create();102try {103main0(args);104} finally {105snapshot.restore();106}107}108109public static void main0(String[] args) throws Exception {110if ((GOOD_PARAMS == null) || (BAD_PARAMS == null)) {111throw new Exception("Static parameter generation failed");112}113// use random data114byte[] encryptedData = new byte[30];115encryptedData[20] = (byte) 8;116117PKCS8EncodedKeySpec pkcs8Spec = null;118119// generate encrypted data and EncryptedPrivateKeyInfo objects120EncryptedPrivateKeyInfo epki =121new EncryptedPrivateKeyInfo(GOOD_PARAMS, encryptedData);122EncryptedPrivateKeyInfo epkiBad =123new EncryptedPrivateKeyInfo(BAD_PARAMS, encryptedData);124125// TEST#1: getKeySpec(Cipher)126System.out.println("Testing getKeySpec(Cipher)...");127try {128pkcs8Spec = epki.getKeySpec((Cipher) null);129throwException("Should throw NPE for null Cipher!");130} catch (NullPointerException npe) {131System.out.println("Expected NPE thrown");132}133134// TEST#2: getKeySpec(Key)135System.out.println("Testing getKeySpec(Key)...");136try {137pkcs8Spec = epki.getKeySpec((Key) null);138throwException("Should throw NPE for null Key!");139} catch (NullPointerException npe) {140System.out.println("Expected NPE thrown");141}142try {143pkcs8Spec = epki.getKeySpec(INVALID_KEY);144throwException("Should throw IKE for invalid Key!");145} catch (InvalidKeyException ikse) {146System.out.println("Expected IKE thrown");147}148try {149pkcs8Spec = epkiBad.getKeySpec(cipherKey);150throwException("Should throw IKE for corrupted epki!");151} catch (InvalidKeyException ike) {152System.out.println("Expected IKE thrown");153}154Provider[] removedProvs = null;155try {156removedProvs = removeProviders(cipherAlg);157pkcs8Spec = epki.getKeySpec(cipherKey);158throwException("Should throw NSAE if no matching impl!");159} catch (NoSuchAlgorithmException nsae) {160System.out.println("Expected NSAE thrown");161addProviders(removedProvs);162}163// TEST#3: getKeySpec(Key, String)164System.out.println("Testing getKeySpec(Key, String)...");165try {166pkcs8Spec = epki.getKeySpec(null, "SunJCE");167throwException("Should throw NPE for null Key!");168} catch (NullPointerException npe) {169System.out.println("Expected NPE thrown");170}171try {172pkcs8Spec = epki.getKeySpec(cipherKey, (String)null);173throwException("Should throw NPE for null String!");174} catch (NullPointerException npe) {175System.out.println("Expected NPE thrown");176}177try {178pkcs8Spec = epki.getKeySpec(INVALID_KEY, "SunJCE");179throwException("Should throw IKE for invalid Key!");180} catch (InvalidKeyException ikse) {181System.out.println("Expected IKE thrown");182}183try {184pkcs8Spec = epkiBad.getKeySpec(cipherKey, "SunJCE");185throwException("Should throw IKE for corrupted epki!");186} catch (InvalidKeyException ike) {187System.out.println("Expected IKE thrown");188}189try {190pkcs8Spec = epki.getKeySpec(cipherKey, "SUN");191throwException("Should throw NSAE for provider without " +192"matching implementation!");193} catch (NoSuchAlgorithmException nsae) {194System.out.println("Expected NSAE thrown");195}196try {197Security.removeProvider("SunJCE");198pkcs8Spec = epki.getKeySpec(cipherKey, "SunJCE");199throwException("Should throw NSPE for unconfigured provider!");200} catch (NoSuchProviderException nspe) {201System.out.println("Expected NSPE thrown");202Security.addProvider(sunjce);203}204// TEST#4: getKeySpec(Key, Provider)205System.out.println("Testing getKeySpec(Key, Provider)...");206try {207pkcs8Spec = epki.getKeySpec(null, sunjce);208throwException("Should throw NPE for null Key!");209} catch (NullPointerException npe) {210System.out.println("Expected NPE thrown");211}212try {213pkcs8Spec = epki.getKeySpec(cipherKey, (Provider)null);214throwException("Should throw NPE for null Provider!");215} catch (NullPointerException npe) {216System.out.println("Expected NPE thrown");217}218try {219pkcs8Spec = epki.getKeySpec(INVALID_KEY, sunjce);220throwException("Should throw IKE for invalid Key!");221} catch (InvalidKeyException ikse) {222System.out.println("Expected IKE thrown");223}224try {225pkcs8Spec = epkiBad.getKeySpec(cipherKey, sunjce);226throwException("Should throw IKE for corrupted epki!");227} catch (InvalidKeyException ike) {228System.out.println("Expected IKE thrown");229}230System.out.println("All Tests Passed");231}232}233234235