Path: blob/master/test/jdk/sun/security/pkcs12/StoreSecretKeyTest.java
41152 views
/*1* Copyright (c) 2013, 2015, 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 8005408 8079129 804883026* @summary KeyStore API enhancements27* @run main StoreSecretKeyTest28*/2930import java.io.*;31import java.security.*;32import java.security.cert.*;33import java.security.cert.Certificate;34import java.util.*;35import javax.crypto.*;36import javax.crypto.spec.*;3738// Store a secret key in a keystore and retrieve it again.3940public class StoreSecretKeyTest {41private final static String DIR = System.getProperty("test.src", ".");42private static final char[] PASSWORD = "passphrase".toCharArray();43private static final String KEYSTORE = "keystore.p12";44private static final String CERT = DIR + "/trusted.pem";45private static final String ALIAS = "my trusted cert";46private static final String ALIAS2 = "my secret key";47private enum ALGORITHM {48DES(56),49DESede(168),50AES(128);51final int len;52ALGORITHM(int l) {53len = l;54}55final int getLength() {56return len;57}58}59public static void main(String[] args) throws Exception {60boolean isSecretkeyAlgSupported = false;61// Skip test if AES is unavailable62try {63SecretKeyFactory.getInstance("AES");64} catch (NoSuchAlgorithmException nsae) {65System.out.println("AES is unavailable. Skipping test...");66return;67}6869for (ALGORITHM alg : ALGORITHM.values()) {70isSecretkeyAlgSupported |= testSecretKeyAlgorithm(alg);71}72if (!isSecretkeyAlgSupported) {73throw new Exception("None of the SecretKey algorithms is "74+ "supported");75}76}7778private static boolean testSecretKeyAlgorithm(ALGORITHM algorithm) throws79Exception {8081System.out.println("Testing algorithm : " + algorithm.name());82new File(KEYSTORE).delete();83try {84KeyStore keystore = KeyStore.getInstance("PKCS12");85keystore.load(null, null);8687// Set trusted certificate entry88Certificate cert = loadCertificate(CERT);89keystore.setEntry(ALIAS,90new KeyStore.TrustedCertificateEntry(cert), null);91// Set secret key entry92SecretKey secretKey = generateSecretKey(algorithm.name(),93algorithm.len);94if(secretKey == null) {95return false;96}97keystore.setEntry(ALIAS2,98new KeyStore.SecretKeyEntry(secretKey),99new KeyStore.PasswordProtection(PASSWORD));100101try (FileOutputStream outStream = new FileOutputStream(KEYSTORE)) {102System.out.println("Storing keystore to: " + KEYSTORE);103keystore.store(outStream, PASSWORD);104}105106try (FileInputStream inStream = new FileInputStream(KEYSTORE)) {107System.out.println("Loading keystore from: " + KEYSTORE);108keystore.load(inStream, PASSWORD);109System.out.println("Loaded keystore with " + keystore.size() +110" entries");111}112113KeyStore.Entry entry = keystore.getEntry(ALIAS2,114new KeyStore.PasswordProtection(PASSWORD));115System.out.println("Retrieved entry: " + entry);116117if (entry instanceof KeyStore.SecretKeyEntry) {118System.out.println("Retrieved secret key entry: " + entry);119} else {120throw new Exception("Not a secret key entry");121}122} catch (KeyStoreException | UnrecoverableKeyException ex) {123System.out.println("Unable to check SecretKey algorithm due to "124+ "exception: " + ex.getMessage());125return false;126}127return true;128}129130private static SecretKey generateSecretKey(String algorithm, int size)131throws NoSuchAlgorithmException {132KeyGenerator generator = KeyGenerator.getInstance(algorithm);133generator.init(size);134return generator.generateKey();135}136137private static Certificate loadCertificate(String certFile)138throws Exception {139X509Certificate cert = null;140try (FileInputStream certStream = new FileInputStream(certFile)) {141CertificateFactory factory =142CertificateFactory.getInstance("X.509");143return factory.generateCertificate(certStream);144}145}146}147148149