Path: blob/master/test/jdk/sun/security/pkcs12/WrongPBES2.java
41149 views
/*1* Copyright (c) 2018, 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 821451326* @summary A PKCS12 keystore from Java 8 using custom PBE parameters cannot be read in Java 1127*/2829import javax.crypto.spec.PBEParameterSpec;30import java.io.*;31import java.security.KeyStore;32import java.security.PrivateKey;33import java.security.cert.Certificate;3435public class WrongPBES2 {3637private static final char[] PASS = "changeit".toCharArray();3839// This is a PKCS 12 file using PBES2 with an incorrect encoding before40// JDK-8202837 is fixed. It is generated with these 2 commands:41//42// keytool -genkeypair -alias a -dname CN=A -keystore ks -storepass changeit43// $JDK10/bin/java WrongPBES2 reenc44static final String P12_FILE =45"308208940201033082084d06092a864886f70d010701a082083e0482083a3082" +46"08363082035206092a864886f70d010701a08203430482033f3082033b308203" +47"37060b2a864886f70d010c0a0102a08202ee308202ea307406092a864886f70d" +48"01050d306706092a864886f70d01050d305a303906092a864886f70d01050c30" +49"2c0414000000000000000000000000000000000000000002030186a002012030" +50"0c06082a864886f70d020b0500301d060960864801650304012a04105abc352d" +51"35d98fd77d8c8cf205d690c6048202703ff97bd4488a6a5569993fb6f89792e7" +52"33632b20fdc502bf0801c8ee80683d064c4dae5b8880ade893a87bfdb4427dfd" +53"01bc5656fdb50f45f1e8d60b37416d2f3b1be9e8ae145bfe035f9d947fa49a31" +54"baef86dec6780a0f96a7ad5d9ef850b15f56d6b6f2f0798c190b01b42d955d36" +55"96f1416efb0c246b56e3715c1ccef701f975f6faf7e7640b8edca79f996682d8" +56"5a414c8210004f247058c3362f328c12d024ea88de5ab3d6c50a7cf8f32b69de" +57"999930ccb29be0d278216db0959d9b142e4c46207edaf91f37708e0250e99262" +58"c4acba4b83c2c0affb3bfc2d5bfe210baffe56ba1f667c9e3c33e0c4164d0c16" +59"abc89bca2c1af7fc234163a6a17e4852ee08053bdf699c8659ea57f77427ab3b" +60"cea357a85ffd80589403bc73ff50490f9b0bfb37fedf495aae8ef8a00da7d1e1" +61"62c49dacf82503e803953f48e302d1ae1b7e7870a479c7861ffc236469807f31" +62"da0b15a915a62d4f0a42698a4c9779595619effa04765f91342821229752ec6d" +63"6e0657ad4e41979847b5903ddb6a6819b859797a2f663898694fe294cc85b9c3" +64"d485516659dcd6d89f631903be35367315b131f9f5ec63ee5bf25f3afcf8bc9c" +65"d9056a56f951746664cfa3ed0596e005fda6fb753d9c67c92fd1a9f22d06a59e" +66"9c7af058dddad3b9265fc29c18122f55c07a1780050251990c221ba2b3767586" +67"405def7a1b224135ac4a694f0e3d27666486531aad9e27a3855f54e2916a642a" +68"ed06fc5275b675bbf457a26978b0f4306c166119dedc3aedf366cbf34efa647e" +69"a65b8451c7b1d083fe1ee3a8493a0330ee7da68552614e50ca7326a25d741e40" +70"ccac2149d1ff5cb8778a3b08ed3d9a1774c971525454b7c20b18610b9bc847cc" +71"3136301106092a864886f70d01091431041e020061302106092a864886f70d01" +72"09153114041254696d652031353433363738383539323632308204dc06092a86" +73"4886f70d010706a08204cd308204c9020100308204c206092a864886f70d0107" +74"013029060a2a864886f70d010c0106301b0414e6d3162d3b75f103fac6ea3fbf" +75"96d08c0225d308020300c3508082048834d05f8953cad673f7de3b5446bcdd31" +76"310b376eec04c393f1077cfda8ad69fe0133cf79fbdd204eeca165e1d6e288b9" +77"a6625dfe34aa722f230c50da44d463753d3bbd7e762cb0f164b7fbfe4a3aff8c" +78"dc664ef2c6afd01232de66d81221e977738b81838cadd0fe1f87bdd7278dcdbd" +79"2307bda516e931691e3da46f82f72073ab6502b9f2b23788c620756eedddf21e" +80"705a6043d29a8e27285fea79e4a9966f5b08d5f13bc6f4d854f47879ee90e72d" +81"3a5cc6eccda9edf60c4d73edf3e4dd6c715c06c53e83ba461e10868e5ef5e218" +82"726bc6714f3071b3f17a075ff9d28d2cb48896b785d48551e8410f2bc882ad04" +83"88d60c85c3b966e1b484e4190267b38e3179107f3c49abd2b058fb32eab4abfc" +84"ee914ee4407954e1edb338042ca04b1561df364139a64a1080c79bfdfa17d15b" +85"6fc26b0572427b2f1b9bbea6d1a730a7dd1b08d22093658f9a5665ae8e9a04b8" +86"af89a60ea7994d17949b22e2fc6d5d4dd1d280283fd649e1aca6e0e4c90ae509" +87"afc01bd92127321798562cf5ba75fed840f8ca9082c8c58334a95593fa5b1b57" +88"ea86cb8452136dee7ecd63872d4848d9052e181f6bca0772a4d4dd43817b9005" +89"a6314cbd90474a24b6ed9c9896097bba51ca7cd48120b5f9fee3f98dd99bbcd3" +90"e0ab43bcbb922bcf48cf710f7a5b9d192f419f607b6ca7937d8ed6f990835b88" +91"7f480cc5fad2708c57c674d0e082af60d8b0108df2bf88b321df4d0431d4dfc0" +92"4ae21cfd5446715e4af6bcb2224a8d6dc26d71806f21c1e17e8dd2ac57632234" +93"1d3e72cbcce4c4e59421fc5202ec6d76c623b81a872ab6b8d53e4215234ee8d9" +94"61148b0f42300fc0c89ec91f528698d79a27af593e5fd56484c97763ebc4a797" +95"13511ec0e13c59318d1ac444f6121a81a4f505b7e3dca70d6cd74b209fc10003" +96"78d5f2016c63acd019a1175e41fe60b88ff5e7238b32ef25243da462cabcd6c7" +97"5789c6055ef4999522acb386138f085e9bc2e9b41ecaf3bbb0990ac8b9efde0e" +98"eedff4c636bd13ef2dc18bfe5e900d88f4649ce326bc4f8e9bfe058832a595e3" +99"5c65415ca9606b0d2c705519eb83995a6da302f53e57a93f2be20dc858d3d5e9" +100"e8fdbc10af092b47c108d2e44eed00879626389b35df014e54ea3bf067d2d800" +101"b74f67c3576d585e6fedf9634e1ae7941d0de0faed7c76e0296d701728792bf4" +102"9bf007738d9273af8981bd71ed4a6360c08a4c5ab83ee337141c37679cf5db7a" +103"5aff7353ed6285bab948cac12559ee6ed9f34ee20e01b538975e26c310a33790" +104"f8be9fbb907f2585f55d2a456c6be4e46e08edd3aa5392d1c9415a6ded826776" +105"b04a3804f9b5f078888320ee5b279a4bba3b5c73ae1e8444b9320b92eb6b8e16" +106"75f505bcf79ff0e0cc80326e9bedbc6baebce47725aef3914e4326b924984788" +107"1d21d59d71b08ba34283f5784deeeecb9f4a3cfbcb21e48e8a9a94972ee7366e" +108"04d1d9c60534fdb7bfe5ec559427b6bbe96d41f551dde0800684226920cbc4f4" +109"1f3a7225e0c6a15d3ebee4e87ae4c40fef4d2e5272bb5b0037030665ae1faba7" +110"4c41a04277bbe1851e090c8d31e48575bbc96618e3cfeea5a2f80fc1e565adc8" +111"ffd84b803877ff9b305a4da59e6f707b3dbe95f7c037fbec303e302130090605" +112"2b0e03021a05000414adb4dd32f3ce248ec62e2fd45835255c2316739e041441" +113"f87d07019980ef69b635f06017567e36aa3d8802030186a0";114115public static void main(String[] args) throws Exception {116if (args.length > 0 && args[0].equals("reenc")) {117reEncodeWithPBES2();118} else {119test();120}121}122123private static void test() throws Exception {124byte[] p12 = new byte[P12_FILE.length() / 2];125for (int i = 0; i < p12.length; i++) {126p12[i] = Integer.valueOf(P12_FILE.substring(2 * i, 2 * i + 2), 16)127.byteValue();128}129KeyStore ks = KeyStore.getInstance("pkcs12");130ks.load(new ByteArrayInputStream(p12), PASS);131System.out.println(ks.getKey("a", PASS));132}133134private static void reEncodeWithPBES2() throws Exception {135KeyStore keyStore = KeyStore.getInstance(new File("ks"), PASS);136KeyStore.PrivateKeyEntry privateKeyEntry = new KeyStore.PrivateKeyEntry(137(PrivateKey)keyStore.getKey("a", PASS),138new Certificate[] { keyStore.getCertificate("a") });139keyStore.setEntry("a", privateKeyEntry, new KeyStore.PasswordProtection(140PASS, "PBEWithHmacSHA512AndAES_256",141new PBEParameterSpec(new byte[20], 100_000)));142ByteArrayOutputStream bout = new ByteArrayOutputStream();143keyStore.store(bout, PASS);144byte[] p12 = bout.toByteArray();145StringBuilder sb = new StringBuilder();146for (int i = 0; i < p12.length; i++) {147if (i % 32 == 0) {148if (i != 0) {149sb.append("\" +\n");150}151sb.append("\"");152}153sb.append(String.format("%02x", p12[i] & 0xff));154}155sb.append("\";\n");156System.out.println(sb.toString());157}158}159160161