Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/security/KeyStore/PKCS12/MetadataStoreLoadTest.java
38828 views
/*1* Copyright (c) 2012, 2016, 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 /lib/testlibrary ../46* @run main MetadataStoreLoadTest47*/48public class MetadataStoreLoadTest {49private static final char[] PASSWORD = "passwd".toCharArray();50private static final char[] KEY_PASSWORD = "keypasswd".toCharArray();51private static final String ALIAS = "testkey_metadata";52private static final String KEYSTORE = "ks.pkcs12";53private static final String KESTORE_NEW = "ks-attr.pkcs12";54private static final int MAX_HUGE_SIZE = 2000000;55private static final String WORKING_DIRECTORY = System.getProperty(56"test.classes", "." + File.separator);57private static final String KEYSTORE_PATH = WORKING_DIRECTORY58+ File.separator + KEYSTORE;59private static KeyStore.Entry.Attribute[] ATTR_SET;6061private void runTest() throws GeneralSecurityException,62UnrecoverableEntryException, NoSuchAlgorithmException,63KeyStoreException, IOException {64storeAttrs();65checkAttrs();66}6768private void storeAttrs() throws UnrecoverableEntryException,69GeneralSecurityException, NoSuchAlgorithmException,70KeyStoreException, IOException {71KeyStore ksIn = Utils.loadKeyStore(KEYSTORE_PATH,72Utils.KeyStoreType.pkcs12, PASSWORD);73KeyStore ksAttr = KeyStore74.getInstance(Utils.KeyStoreType.pkcs12.name());75ksAttr.load(null);76Key key = ksIn.getKey(ALIAS, PASSWORD);77Certificate cert = ksIn.getCertificate(ALIAS);78Set<KeyStore.Entry.Attribute> attrs =79new HashSet<>(Arrays.asList(ATTR_SET));80KeyStore.Entry e = new KeyStore.PrivateKeyEntry((PrivateKey) key,81new Certificate[]{cert}, attrs);82ksAttr.setEntry(ALIAS, e, new KeyStore.PasswordProtection(83KEY_PASSWORD));8485out.println("Attributes before store:");86e.getAttributes().stream().forEach((attr) -> {87out.println(attr.getName() + ", '" + attr.getValue() + "'");88});89Utils.saveKeyStore(ksAttr, WORKING_DIRECTORY + File.separator90+ KESTORE_NEW, PASSWORD);91}9293private void checkAttrs() throws UnrecoverableEntryException,94GeneralSecurityException, NoSuchAlgorithmException,95KeyStoreException, IOException {96KeyStore ks = Utils.loadKeyStore(WORKING_DIRECTORY97+ File.separator98+ KESTORE_NEW, Utils.KeyStoreType.pkcs12, PASSWORD);99KeyStore.Entry keyStoreEntry = ks.getEntry(ALIAS,100new KeyStore.PasswordProtection(KEY_PASSWORD));101out.println("Attributes after store:");102//print attribute values103keyStoreEntry.getAttributes().stream().forEach((attr) -> {104out.println(attr.getName() + ", '" + attr.getValue() + "'");105});106Arrays.stream(ATTR_SET).forEach((attr) -> {107if (!keyStoreEntry.getAttributes().contains(attr)) {108throw new RuntimeException("Entry doesn't contain attribute: ("109+ attr.getName() + ", '" + attr.getValue() + "')");110}111});112}113114public static void main(String[] args) throws Exception {115MetadataStoreLoadTest test = new MetadataStoreLoadTest();116test.setUp();117test.runTest();118out.println("Test Passed");119}120121private void setUp() {122Utils.createKeyStore(Utils.KeyStoreType.pkcs12, KEYSTORE_PATH, ALIAS);123final String allCharsString = "`1234567890-=qwertyuiop[]asdfghjkl;'\\zx"124+ "cvbnm,./!@#$%^&*()_+QWERTYUIOP{}ASDFGHJKL:|>ZXCVBNM<>?\"";125StringBuilder sbPrintable = new StringBuilder();126while (sbPrintable.length() < MAX_HUGE_SIZE) {127sbPrintable.append(allCharsString);128}129final String hugePrintable = sbPrintable.toString();130final String binaryString = "00:11:22:33:44:55:66:77:88:99:AA:BB:DD:"131+ "EE:FF:";132StringBuilder sbBinary = new StringBuilder();133sbBinary.append(binaryString);134while (sbBinary.length() < MAX_HUGE_SIZE) {135sbBinary.append(":").append(binaryString);136}137sbBinary.insert(0, "[").append("]");138final String hugeBinary = sbBinary.toString();139ATTR_SET = new PKCS12Attribute[5];140ATTR_SET[0] = new PKCS12Attribute("1.2.840.113549.1.9.1",141"Test email addres attr <[email protected]>");142ATTR_SET[1] = new PKCS12Attribute("1.2.110.1", "not registered attr");143ATTR_SET[2] = new PKCS12Attribute("1.2.110.2", hugePrintable);144ATTR_SET[3] = new PKCS12Attribute("1.2.110.3", hugeBinary);145ATTR_SET[4] = new PKCS12Attribute("1.2.110.2", " ");146}147}148149150