Path: blob/master/test/jdk/javax/crypto/EncryptedPrivateKeyInfo/GetKeySpecInvalidEncoding.java
41152 views
/*1* Copyright (c) 2003, 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 490089126* @summary Test the EncryptedPrivateKeyInfo.getKeySpec(...)27* methods with scenarios where the decrypted bytes are not28* encoded correctly per PKCS#8 standard.29* @author Valerie Peng30*/31import java.util.*;32import java.nio.*;33import java.io.*;34import java.security.*;35import java.util.Arrays;36import java.security.spec.*;37import javax.crypto.*;38import javax.crypto.spec.*;3940public class GetKeySpecInvalidEncoding {41private static final String cipherAlg = "PBEWithMD5AndDES";42private static final char[] passwd = { 'p','a','s','s', 'w', 'd' };43private static AlgorithmParameters GOOD_PARAMS;4445static {46try {47PBEParameterSpec goodParamSpec =48new PBEParameterSpec(new byte[8], 6);49GOOD_PARAMS = AlgorithmParameters.getInstance50(cipherAlg, "SunJCE");51GOOD_PARAMS.init(goodParamSpec);52} catch (Exception ex) {53// should never happen54GOOD_PARAMS = null;55}56}5758private static String encryptedPKCS8 = "5C:BC:13:5D:40:2F:02:28:94:3C:A9:F7:98:A6:58:DC:F9:12:B7:CB:0F:40:DD:66:AB:58:6B:23:2F:8A:5A:81:9D:55:2A:EB:3F:AA:6A:CE:AE:23:8A:96:12:21:5A:09:BF:59:65:3F:B8:48:59:69:C6:D0:9C:48:B6:78:C3:C6:B4:24:F9:BE:10:00:D5:F3:52:88:53:CD:07:CA:88:93:15:3F:BA:19:4A:E9:5D:C7:44:46:49:F6:19:83:86:E0:25:51:9E:83:6D:AE:F2:14:9C:BF:02:7B:8C:64:B4:5F:F1:7B:28:2F:39:55:32:A4:F5:41:85:9E:77:F2:07:09:CD:97:90:5A:04:81:23:30";5960private static byte[] parse(String s) {61try {62int n = s.length();63ByteArrayOutputStream out = new ByteArrayOutputStream(n / 3);64StringReader r = new StringReader(s);65while (true) {66int b1 = nextNibble(r);67if (b1 < 0) {68break;69}70int b2 = nextNibble(r);71if (b2 < 0) {72throw new RuntimeException("Invalid string " + s);73}74int b = (b1 << 4) | b2;75out.write(b);76}77return out.toByteArray();78} catch (IOException e) {79throw new RuntimeException(e);80}81}82private static int nextNibble(StringReader r) throws IOException {83while (true) {84int ch = r.read();85if (ch == -1) {86return -1;87} else if ((ch >= '0') && (ch <= '9')) {88return ch - '0';89} else if ((ch >= 'a') && (ch <= 'f')) {90return ch - 'a' + 10;91} else if ((ch >= 'A') && (ch <= 'F')) {92return ch - 'A' + 10;93}94}95}9697public static void main(String[] argv) throws Exception {98if (GOOD_PARAMS == null) {99throw new Exception("Static parameter generation failed");100}101byte[] encryptedData = parse(encryptedPKCS8);102103Provider p = Security.getProvider("SunJCE");104105// generate encrypted data and EncryptedPrivateKeyInfo object106EncryptedPrivateKeyInfo epki =107new EncryptedPrivateKeyInfo(GOOD_PARAMS, encryptedData);108109PKCS8EncodedKeySpec pkcs8Spec;110// TEST#1 getKeySpec(Cipher)111System.out.println("Testing getKeySpec(Cipher)...");112// Prepare Cipher for decryption113PBEKeySpec pbeKeySpec = new PBEKeySpec(passwd);114SecretKeyFactory skf = SecretKeyFactory.getInstance(cipherAlg, p);115SecretKey cipherKey = skf.generateSecret(pbeKeySpec);116Cipher cipher = Cipher.getInstance(cipherAlg, p);117cipher.init(Cipher.DECRYPT_MODE, cipherKey, GOOD_PARAMS);118try {119pkcs8Spec = epki.getKeySpec(cipher);120throw new Exception("getKeySpec(Cipher): should throw IKSE");121} catch (InvalidKeySpecException ikse) {122// expected123}124// TEST#2 getKeySpec(Key)125System.out.println("Testing getKeySpec(Key)...");126try {127pkcs8Spec = epki.getKeySpec(cipherKey);128throw new Exception("getKeySpec(Key): should throw IKE");129} catch (InvalidKeyException ike) {130// expected131}132133// TEST#3 getKeySpec(Key, String)134System.out.println("Testing getKeySpec(Key, String)...");135try {136pkcs8Spec = epki.getKeySpec(cipherKey, p.getName());137throw new Exception("getKeySpec(Key, String): should throw IKE");138} catch (InvalidKeyException ike) {139// expected140}141142// TEST#4 getKeySpec(Key, Provider)143System.out.println("Testing getKeySpec(Key, Provider)...");144try {145pkcs8Spec = epki.getKeySpec(cipherKey, p);146throw new Exception("getKeySpec(Key, Provider): should throw IKE");147} catch (InvalidKeyException ike) {148// expected149}150System.out.println("All Tests Passed");151}152}153154155