1 /* 2 * Copyright (c) 2016, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /** 25 * @test 26 * @bug 8151149 27 * @modules java.base/javax.crypto:open 28 * java.base/com.sun.crypto.provider:+open 29 */ 30 31 import java.lang.reflect.*; 32 import java.security.*; 33 import javax.crypto.*; 34 import javax.crypto.spec.*; 35 import com.sun.crypto.provider.*; 36 37 public class CheckPBEKeySize { 38 39 private static final String ALGO = "PBEWithSHA1AndDESede"; 40 private static final int KEYSIZE = 112; // Triple DES effective key size 41 main(String[] args)42 public static final void main(String[] args) throws Exception { 43 44 // Generate a PBE key 45 SecretKeyFactory skFac = SecretKeyFactory.getInstance("PBE"); 46 SecretKey skey = 47 skFac.generateSecret(new PBEKeySpec("test123".toCharArray())); 48 49 // Initialize the PBE cipher 50 Cipher cipher = Cipher.getInstance(ALGO); 51 cipher.init(Cipher.ENCRYPT_MODE, skey); 52 53 // Permit access to the Cipher.spi field (a CipherSpi object) 54 Field spi = Cipher.class.getDeclaredField("spi"); 55 spi.setAccessible(true); 56 Object value = spi.get(cipher); 57 58 // Permit access to the CipherSpi.engineGetKeySize method 59 Method engineGetKeySize = 60 PKCS12PBECipherCore$PBEWithSHA1AndDESede.class 61 .getDeclaredMethod("engineGetKeySize", Key.class); 62 engineGetKeySize.setAccessible(true); 63 64 // Check the key size 65 int keySize = (int) engineGetKeySize.invoke(value, skey); 66 if (keySize == KEYSIZE) { 67 System.out.println(ALGO + ".engineGetKeySize returns " + keySize + 68 " bits, as expected"); 69 System.out.println("OK"); 70 } else { 71 throw new Exception("ERROR: " + ALGO + " key size is incorrect"); 72 } 73 } 74 } 75