J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /** |
| 25 | * @test |
| 26 | * @bug 5028661 |
| 27 | * @summary Test the error conditions of Cipher initialized |
| 28 | * with wrong mode with EncryptedPrivateKeyInfo.getKeySpec |
| 29 | * (Cipher) method. |
| 30 | * @author Valerie Peng |
| 31 | */ |
| 32 | import java.security.*; |
| 33 | import java.util.Arrays; |
| 34 | import java.util.Vector; |
| 35 | import java.security.spec.*; |
| 36 | import javax.crypto.*; |
| 37 | import javax.crypto.interfaces.PBEKey; |
| 38 | import javax.crypto.spec.*; |
| 39 | |
| 40 | public class GetKeySpecException2 { |
| 41 | private static final String cipherAlg = "PBEWithMD5AndDES"; |
| 42 | private static final char[] passwd = { 'p','a','s','s','w','d' }; |
| 43 | |
| 44 | public static void main(String[] argv) throws Exception { |
| 45 | |
| 46 | // use random data |
| 47 | byte[] encryptedData = new byte[30]; |
| 48 | encryptedData[20] = (byte) 8; |
| 49 | |
| 50 | // generate encrypted data and EncryptedPrivateKeyInfo objects |
| 51 | EncryptedPrivateKeyInfo epki = |
| 52 | new EncryptedPrivateKeyInfo(cipherAlg, encryptedData); |
| 53 | |
| 54 | // TEST#1: getKeySpec(Cipher) with Cipher in an illegal state, |
| 55 | // i.e. WRAP_MODE, UNWRAP_MODE. |
| 56 | System.out.println("Testing getKeySpec(Cipher) with WRAP_MODE..."); |
| 57 | Cipher c = Cipher.getInstance(cipherAlg, "SunJCE"); |
| 58 | MyPBEKey key = new MyPBEKey(passwd); |
| 59 | c.init(Cipher.WRAP_MODE, key); |
| 60 | try { |
| 61 | epki.getKeySpec(c); |
| 62 | throw new Exception("Should throw InvalidKeyException"); |
| 63 | } catch (InvalidKeySpecException npe) { |
| 64 | System.out.println("Expected IKE thrown"); |
| 65 | } |
| 66 | AlgorithmParameters params = c.getParameters(); |
| 67 | System.out.println("Testing getKeySpec(Cipher) with UNWRAP_MODE..."); |
| 68 | c.init(Cipher.UNWRAP_MODE, key, params); |
| 69 | try { |
| 70 | epki.getKeySpec(c); |
| 71 | throw new Exception("Should throw InvalidKeyException"); |
| 72 | } catch (InvalidKeySpecException npe) { |
| 73 | System.out.println("Expected IKE thrown"); |
| 74 | } |
| 75 | System.out.println("All Tests Passed"); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | class MyPBEKey implements PBEKey { |
| 80 | |
| 81 | private char[] password = null; |
| 82 | |
| 83 | MyPBEKey(char[] password) { |
| 84 | this.password = (char[]) password.clone(); |
| 85 | } |
| 86 | public int getIterationCount() { |
| 87 | return 0; |
| 88 | } |
| 89 | public char[] getPassword() { |
| 90 | return (char[]) password.clone(); |
| 91 | } |
| 92 | public byte[] getSalt() { |
| 93 | return null; |
| 94 | } |
| 95 | public String getAlgorithm() { |
| 96 | return "PBE"; |
| 97 | } |
| 98 | public String getFormat() { |
| 99 | return "RAW"; |
| 100 | } |
| 101 | public byte[] getEncoded() { |
| 102 | return new byte[8]; |
| 103 | } |
| 104 | } |