J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2003-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 4508341 |
| 27 | * @summary Test the error conditions of |
| 28 | * EncryptedPrivateKeyInfo.getKeySpec(...) methods. |
| 29 | * @author Valerie Peng |
| 30 | */ |
| 31 | import java.security.*; |
| 32 | import java.util.Arrays; |
| 33 | import java.util.Vector; |
| 34 | import java.security.spec.*; |
| 35 | import javax.crypto.*; |
| 36 | import javax.crypto.spec.*; |
| 37 | |
| 38 | public class GetKeySpecException { |
| 39 | private static final String cipherAlg = "PBEWithMD5AndDES"; |
| 40 | private static final char[] passwd = { 'p','a','s','s','w','d' }; |
| 41 | private static SecretKey cipherKey; |
| 42 | private static Cipher cipher = null; |
| 43 | private static byte[] encryptedData = null; |
| 44 | private static Provider sunjce = null; |
| 45 | private static final SecretKey INVALID_KEY = |
| 46 | new SecretKeySpec(new byte[8], "DES"); |
| 47 | private static AlgorithmParameters BAD_PARAMS; |
| 48 | private static AlgorithmParameters GOOD_PARAMS; |
| 49 | |
| 50 | static { |
| 51 | try { |
| 52 | sunjce = Security.getProvider("SunJCE"); |
| 53 | PBEParameterSpec badParamSpec = |
| 54 | new PBEParameterSpec(new byte[10], 10); |
| 55 | BAD_PARAMS = AlgorithmParameters.getInstance(cipherAlg, sunjce); |
| 56 | BAD_PARAMS.init(badParamSpec); |
| 57 | PBEParameterSpec goodParamSpec = |
| 58 | new PBEParameterSpec(new byte[8], 1024); |
| 59 | GOOD_PARAMS = AlgorithmParameters.getInstance(cipherAlg, sunjce); |
| 60 | GOOD_PARAMS.init(goodParamSpec); |
| 61 | PBEKeySpec keySpec = new PBEKeySpec(passwd); |
| 62 | SecretKeyFactory skf = |
| 63 | SecretKeyFactory.getInstance(cipherAlg, "SunJCE"); |
| 64 | cipherKey = skf.generateSecret(keySpec); |
| 65 | } catch (Exception ex) { |
| 66 | // should never happen |
| 67 | BAD_PARAMS = null; |
| 68 | GOOD_PARAMS = null; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | private static void throwException(String msg) throws Exception { |
| 73 | throw new Exception(msg); |
| 74 | } |
| 75 | |
| 76 | private static Provider[] removeProviders(String cipherAlg) { |
| 77 | Vector providers = new Vector(); |
| 78 | boolean done = false; |
| 79 | while (!done) { |
| 80 | try { |
| 81 | Cipher c = Cipher.getInstance(cipherAlg); |
| 82 | Provider p = c.getProvider(); |
| 83 | providers.add(p); |
| 84 | Security.removeProvider(p.getName()); |
| 85 | } catch (NoSuchAlgorithmException nsae) { |
| 86 | done = true; |
| 87 | } catch (NoSuchPaddingException nspe) { |
| 88 | // should never happen |
| 89 | } |
| 90 | } |
| 91 | return (Provider[]) (providers.toArray(new Provider[0])); |
| 92 | } |
| 93 | |
| 94 | private static void addProviders(Provider[] provs) { |
| 95 | for (int i=0; i<provs.length; i++) { |
| 96 | Security.addProvider(provs[i]); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | public static void main(String[] argv) throws Exception { |
| 101 | if ((GOOD_PARAMS == null) || (BAD_PARAMS == null)) { |
| 102 | throw new Exception("Static parameter generation failed"); |
| 103 | } |
| 104 | // use random data |
| 105 | byte[] encryptedData = new byte[30]; |
| 106 | encryptedData[20] = (byte) 8; |
| 107 | |
| 108 | PKCS8EncodedKeySpec pkcs8Spec = null; |
| 109 | |
| 110 | // generate encrypted data and EncryptedPrivateKeyInfo objects |
| 111 | EncryptedPrivateKeyInfo epki = |
| 112 | new EncryptedPrivateKeyInfo(GOOD_PARAMS, encryptedData); |
| 113 | EncryptedPrivateKeyInfo epkiBad = |
| 114 | new EncryptedPrivateKeyInfo(BAD_PARAMS, encryptedData); |
| 115 | |
| 116 | // TEST#1: getKeySpec(Cipher) |
| 117 | System.out.println("Testing getKeySpec(Cipher)..."); |
| 118 | try { |
| 119 | pkcs8Spec = epki.getKeySpec((Cipher) null); |
| 120 | throwException("Should throw NPE for null Cipher!"); |
| 121 | } catch (NullPointerException npe) { |
| 122 | System.out.println("Expected NPE thrown"); |
| 123 | } |
| 124 | |
| 125 | // TEST#2: getKeySpec(Key) |
| 126 | System.out.println("Testing getKeySpec(Key)..."); |
| 127 | try { |
| 128 | pkcs8Spec = epki.getKeySpec((Key) null); |
| 129 | throwException("Should throw NPE for null Key!"); |
| 130 | } catch (NullPointerException npe) { |
| 131 | System.out.println("Expected NPE thrown"); |
| 132 | } |
| 133 | try { |
| 134 | pkcs8Spec = epki.getKeySpec(INVALID_KEY); |
| 135 | throwException("Should throw IKE for invalid Key!"); |
| 136 | } catch (InvalidKeyException ikse) { |
| 137 | System.out.println("Expected IKE thrown"); |
| 138 | } |
| 139 | try { |
| 140 | pkcs8Spec = epkiBad.getKeySpec(cipherKey); |
| 141 | throwException("Should throw IKE for corrupted epki!"); |
| 142 | } catch (InvalidKeyException ike) { |
| 143 | System.out.println("Expected IKE thrown"); |
| 144 | } |
| 145 | Provider[] removedProvs = null; |
| 146 | try { |
| 147 | removedProvs = removeProviders(cipherAlg); |
| 148 | pkcs8Spec = epki.getKeySpec(cipherKey); |
| 149 | throwException("Should throw NSAE if no matching impl!"); |
| 150 | } catch (NoSuchAlgorithmException nsae) { |
| 151 | System.out.println("Expected NSAE thrown"); |
| 152 | addProviders(removedProvs); |
| 153 | } |
| 154 | // TEST#3: getKeySpec(Key, String) |
| 155 | System.out.println("Testing getKeySpec(Key, String)..."); |
| 156 | try { |
| 157 | pkcs8Spec = epki.getKeySpec(null, "SunJCE"); |
| 158 | throwException("Should throw NPE for null Key!"); |
| 159 | } catch (NullPointerException npe) { |
| 160 | System.out.println("Expected NPE thrown"); |
| 161 | } |
| 162 | try { |
| 163 | pkcs8Spec = epki.getKeySpec(cipherKey, (String)null); |
| 164 | throwException("Should throw NPE for null String!"); |
| 165 | } catch (NullPointerException npe) { |
| 166 | System.out.println("Expected NPE thrown"); |
| 167 | } |
| 168 | try { |
| 169 | pkcs8Spec = epki.getKeySpec(INVALID_KEY, "SunJCE"); |
| 170 | throwException("Should throw IKE for invalid Key!"); |
| 171 | } catch (InvalidKeyException ikse) { |
| 172 | System.out.println("Expected IKE thrown"); |
| 173 | } |
| 174 | try { |
| 175 | pkcs8Spec = epkiBad.getKeySpec(cipherKey, "SunJCE"); |
| 176 | throwException("Should throw IKE for corrupted epki!"); |
| 177 | } catch (InvalidKeyException ike) { |
| 178 | System.out.println("Expected IKE thrown"); |
| 179 | } |
| 180 | try { |
| 181 | pkcs8Spec = epki.getKeySpec(cipherKey, "SUN"); |
| 182 | throwException("Should throw NSAE for provider without " + |
| 183 | "matching implementation!"); |
| 184 | } catch (NoSuchAlgorithmException nsae) { |
| 185 | System.out.println("Expected NSAE thrown"); |
| 186 | } |
| 187 | try { |
| 188 | Security.removeProvider("SunJCE"); |
| 189 | pkcs8Spec = epki.getKeySpec(cipherKey, "SunJCE"); |
| 190 | throwException("Should throw NSPE for unconfigured provider!"); |
| 191 | } catch (NoSuchProviderException nspe) { |
| 192 | System.out.println("Expected NSPE thrown"); |
| 193 | Security.addProvider(sunjce); |
| 194 | } |
| 195 | // TEST#4: getKeySpec(Key, Provider) |
| 196 | System.out.println("Testing getKeySpec(Key, Provider)..."); |
| 197 | try { |
| 198 | pkcs8Spec = epki.getKeySpec(null, sunjce); |
| 199 | throwException("Should throw NPE for null Key!"); |
| 200 | } catch (NullPointerException npe) { |
| 201 | System.out.println("Expected NPE thrown"); |
| 202 | } |
| 203 | try { |
| 204 | pkcs8Spec = epki.getKeySpec(cipherKey, (Provider)null); |
| 205 | throwException("Should throw NPE for null Provider!"); |
| 206 | } catch (NullPointerException npe) { |
| 207 | System.out.println("Expected NPE thrown"); |
| 208 | } |
| 209 | try { |
| 210 | pkcs8Spec = epki.getKeySpec(INVALID_KEY, sunjce); |
| 211 | throwException("Should throw IKE for invalid Key!"); |
| 212 | } catch (InvalidKeyException ikse) { |
| 213 | System.out.println("Expected IKE thrown"); |
| 214 | } |
| 215 | try { |
| 216 | pkcs8Spec = epkiBad.getKeySpec(cipherKey, sunjce); |
| 217 | throwException("Should throw IKE for corrupted epki!"); |
| 218 | } catch (InvalidKeyException ike) { |
| 219 | System.out.println("Expected IKE thrown"); |
| 220 | } |
| 221 | System.out.println("All Tests Passed"); |
| 222 | } |
| 223 | } |