Path: blob/master/test/jdk/sun/security/pkcs12/StoreTrustedCertTest.java
41152 views
/*1* Copyright (c) 2013, 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 800540826* @summary KeyStore API enhancements27*/2829import java.io.*;30import java.security.*;31import java.security.cert.*;32import java.util.*;33import java.security.cert.Certificate;34import javax.crypto.*;35import javax.crypto.spec.*;3637// Store a trusted certificate in a keystore and retrieve it again.3839public class StoreTrustedCertTest {40private final static String DIR = System.getProperty("test.src", ".");41private static final char[] PASSWORD = "passphrase".toCharArray();42private static final String KEYSTORE = "truststore.p12";43private static final String CERT = DIR + "/trusted.pem";44private static final String ALIAS = "my trustedcert";45private static final String ALIAS2 = "my trustedcert with attributes";4647public static void main(String[] args) throws Exception {4849new File(KEYSTORE).delete();5051KeyStore keystore = KeyStore.getInstance("PKCS12");52keystore.load(null, null);5354Certificate cert = loadCertificate(CERT);55Set<KeyStore.Entry.Attribute> attributes = new HashSet<>();56attributes.add(new PKCS12Attribute("1.3.5.7.9", "that's odd"));57attributes.add(new PKCS12Attribute("2.4.6.8.10", "that's even"));5859// Set trusted certificate entry60keystore.setEntry(ALIAS,61new KeyStore.TrustedCertificateEntry(cert), null);6263// Set trusted certificate entry with attributes64keystore.setEntry(ALIAS2,65new KeyStore.TrustedCertificateEntry(cert, attributes), null);6667try (FileOutputStream outStream = new FileOutputStream(KEYSTORE)) {68System.out.println("Storing keystore to: " + KEYSTORE);69keystore.store(outStream, PASSWORD);70}7172try (FileInputStream inStream = new FileInputStream(KEYSTORE)) {73System.out.println("Loading keystore from: " + KEYSTORE);74keystore.load(inStream, PASSWORD);75System.out.println("Loaded keystore with " + keystore.size() +76" entries");77}7879KeyStore.Entry entry = keystore.getEntry(ALIAS, null);80if (entry instanceof KeyStore.TrustedCertificateEntry) {81System.out.println("Retrieved trusted certificate entry: " + entry);82} else {83throw new Exception("Not a trusted certificate entry");84}85System.out.println();8687entry = keystore.getEntry(ALIAS2, null);88if (entry instanceof KeyStore.TrustedCertificateEntry) {89KeyStore.TrustedCertificateEntry trustedEntry =90(KeyStore.TrustedCertificateEntry) entry;91Set<KeyStore.Entry.Attribute> entryAttributes =92trustedEntry.getAttributes();9394if (entryAttributes.containsAll(attributes)) {95System.out.println("Retrieved trusted certificate entry " +96"with attributes: " + entry);97} else {98throw new Exception("Failed to retrieve entry attributes");99}100} else {101throw new Exception("Not a trusted certificate entry");102}103}104105private static Certificate loadCertificate(String certFile)106throws Exception {107X509Certificate cert = null;108try (FileInputStream certStream = new FileInputStream(certFile)) {109CertificateFactory factory =110CertificateFactory.getInstance("X.509");111return factory.generateCertificate(certStream);112}113}114}115116117