Path: blob/master/test/jdk/javax/crypto/EncryptedPrivateKeyInfo/GetKeySpecException2.java
41149 views
/*1* Copyright (c) 2004, 2007, 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 502866126* @summary Test the error conditions of Cipher initialized27* with wrong mode with EncryptedPrivateKeyInfo.getKeySpec28* (Cipher) method.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.interfaces.PBEKey;37import javax.crypto.spec.*;3839public class GetKeySpecException2 {40private static final String cipherAlg = "PBEWithMD5AndDES";41private static final char[] passwd = { 'p','a','s','s','w','d' };4243public static void main(String[] argv) throws Exception {4445// use random data46byte[] encryptedData = new byte[30];47encryptedData[20] = (byte) 8;4849// generate encrypted data and EncryptedPrivateKeyInfo objects50EncryptedPrivateKeyInfo epki =51new EncryptedPrivateKeyInfo(cipherAlg, encryptedData);5253// TEST#1: getKeySpec(Cipher) with Cipher in an illegal state,54// i.e. WRAP_MODE, UNWRAP_MODE.55System.out.println("Testing getKeySpec(Cipher) with WRAP_MODE...");56Cipher c = Cipher.getInstance(cipherAlg, "SunJCE");57MyPBEKey key = new MyPBEKey(passwd);58c.init(Cipher.WRAP_MODE, key);59try {60epki.getKeySpec(c);61throw new Exception("Should throw InvalidKeyException");62} catch (InvalidKeySpecException npe) {63System.out.println("Expected IKE thrown");64}65AlgorithmParameters params = c.getParameters();66System.out.println("Testing getKeySpec(Cipher) with UNWRAP_MODE...");67c.init(Cipher.UNWRAP_MODE, key, params);68try {69epki.getKeySpec(c);70throw new Exception("Should throw InvalidKeyException");71} catch (InvalidKeySpecException npe) {72System.out.println("Expected IKE thrown");73}74System.out.println("All Tests Passed");75}76}7778class MyPBEKey implements PBEKey {7980private char[] password = null;8182MyPBEKey(char[] password) {83this.password = (char[]) password.clone();84}85public int getIterationCount() {86return 0;87}88public char[] getPassword() {89return (char[]) password.clone();90}91public byte[] getSalt() {92return null;93}94public String getAlgorithm() {95return "PBE";96}97public String getFormat() {98return "RAW";99}100public byte[] getEncoded() {101return new byte[8];102}103}104105106