Path: blob/master/test/jdk/java/security/KeyStore/PKCS12/MetadataStoreLoadTest.java
41153 views
/*1* Copyright (c) 2012, 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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425import java.io.File;26import java.io.IOException;27import java.security.GeneralSecurityException;28import java.security.Key;29import java.security.KeyStore;30import java.security.KeyStoreException;31import java.security.NoSuchAlgorithmException;32import java.security.PKCS12Attribute;33import java.security.PrivateKey;34import java.security.UnrecoverableEntryException;35import java.security.cert.Certificate;36import java.util.Arrays;37import java.util.Set;38import static java.lang.System.out;39import java.util.HashSet;4041/**42* @test43* @bug 804883044* @summary Test store metadata attributes to PKCS12 keystore.45* @library ../46* @library /test/lib47* @run main MetadataStoreLoadTest48*/49public class MetadataStoreLoadTest {50private static final char[] PASSWORD = "passwd".toCharArray();51private static final char[] KEY_PASSWORD = "keypasswd".toCharArray();52private static final String ALIAS = "testkey_metadata";53private static final String KEYSTORE = "ks.pkcs12";54private static final String KESTORE_NEW = "ks-attr.pkcs12";55private static final int MAX_HUGE_SIZE = 2000000;56private static final String WORKING_DIRECTORY = System.getProperty(57"test.classes", "." + File.separator);58private static final String KEYSTORE_PATH = WORKING_DIRECTORY59+ File.separator + KEYSTORE;60private static KeyStore.Entry.Attribute[] ATTR_SET;6162private void runTest() throws GeneralSecurityException,63UnrecoverableEntryException, NoSuchAlgorithmException,64KeyStoreException, IOException {65storeAttrs();66checkAttrs();67}6869private void storeAttrs() throws UnrecoverableEntryException,70GeneralSecurityException, NoSuchAlgorithmException,71KeyStoreException, IOException {72KeyStore ksIn = Utils.loadKeyStore(KEYSTORE_PATH,73Utils.KeyStoreType.pkcs12, PASSWORD);74KeyStore ksAttr = KeyStore75.getInstance(Utils.KeyStoreType.pkcs12.name());76ksAttr.load(null);77Key key = ksIn.getKey(ALIAS, PASSWORD);78Certificate cert = ksIn.getCertificate(ALIAS);79Set<KeyStore.Entry.Attribute> attrs =80new HashSet<>(Arrays.asList(ATTR_SET));81KeyStore.Entry e = new KeyStore.PrivateKeyEntry((PrivateKey) key,82new Certificate[]{cert}, attrs);83ksAttr.setEntry(ALIAS, e, new KeyStore.PasswordProtection(84KEY_PASSWORD));8586out.println("Attributes before store:");87e.getAttributes().stream().forEach((attr) -> {88out.println(attr.getName() + ", '" + attr.getValue() + "'");89});90Utils.saveKeyStore(ksAttr, WORKING_DIRECTORY + File.separator91+ KESTORE_NEW, PASSWORD);92}9394private void checkAttrs() throws UnrecoverableEntryException,95GeneralSecurityException, NoSuchAlgorithmException,96KeyStoreException, IOException {97KeyStore ks = Utils.loadKeyStore(WORKING_DIRECTORY98+ File.separator99+ KESTORE_NEW, Utils.KeyStoreType.pkcs12, PASSWORD);100KeyStore.Entry keyStoreEntry = ks.getEntry(ALIAS,101new KeyStore.PasswordProtection(KEY_PASSWORD));102out.println("Attributes after store:");103//print attribute values104keyStoreEntry.getAttributes().stream().forEach((attr) -> {105out.println(attr.getName() + ", '" + attr.getValue() + "'");106});107Arrays.stream(ATTR_SET).forEach((attr) -> {108if (!keyStoreEntry.getAttributes().contains(attr)) {109throw new RuntimeException("Entry doesn't contain attribute: ("110+ attr.getName() + ", '" + attr.getValue() + "')");111}112});113}114115public static void main(String[] args) throws Exception {116MetadataStoreLoadTest test = new MetadataStoreLoadTest();117test.setUp();118test.runTest();119out.println("Test Passed");120}121122private void setUp() {123Utils.createKeyStore(Utils.KeyStoreType.pkcs12, KEYSTORE_PATH, ALIAS);124final String allCharsString = "`1234567890-=qwertyuiop[]asdfghjkl;'\\zx"125+ "cvbnm,./!@#$%^&*()_+QWERTYUIOP{}ASDFGHJKL:|>ZXCVBNM<>?\"";126StringBuilder sbPrintable = new StringBuilder();127while (sbPrintable.length() < MAX_HUGE_SIZE) {128sbPrintable.append(allCharsString);129}130final String hugePrintable = sbPrintable.toString();131final String binaryString = "00:11:22:33:44:55:66:77:88:99:AA:BB:DD:"132+ "EE:FF:";133StringBuilder sbBinary = new StringBuilder();134sbBinary.append(binaryString);135while (sbBinary.length() < MAX_HUGE_SIZE) {136sbBinary.append(":").append(binaryString);137}138sbBinary.insert(0, "[").append("]");139final String hugeBinary = sbBinary.toString();140ATTR_SET = new PKCS12Attribute[5];141ATTR_SET[0] = new PKCS12Attribute("1.2.840.113549.1.9.1",142"Test email addres attr <[email protected]>");143ATTR_SET[1] = new PKCS12Attribute("1.2.110.1", "not registered attr");144ATTR_SET[2] = new PKCS12Attribute("1.2.110.2", hugePrintable);145ATTR_SET[3] = new PKCS12Attribute("1.2.110.3", hugeBinary);146ATTR_SET[4] = new PKCS12Attribute("1.2.110.2", " ");147}148}149150151