Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/KeyStore/SecretKeysBasic.java
38855 views
/*1* Copyright (c) 2008, 2014, 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*/2223import java.io.*;24import java.util.*;25import java.security.*;26import javax.crypto.*;27import javax.crypto.spec.*;28import javax.xml.bind.DatatypeConverter;2930public class SecretKeysBasic extends PKCS11Test {3132private static final char SEP = File.separatorChar;33private static char[] tokenPwd;34private static final char[] nssPwd =35new char[]{'t', 'e', 's', 't', '1', '2'};36private static final char[] solarisPwd =37new char[]{'p', 'i', 'n'};38private static SecretKey sk1;39private static SecretKey sk2;40private static SecretKey softkey;41private static KeyStore ks;42private static final String KS_TYPE = "PKCS11";43private static Provider provider;4445public static void main(String[] args) throws Exception {46main(new SecretKeysBasic());47}4849public void main(Provider p) throws Exception {50this.provider = p;5152// create secret key53byte[] keyVal = new byte[16];54(new SecureRandom()).nextBytes(keyVal);55// NSS will throw CKR_HOST_MEMORY if calling C_DecryptInit w/56// (keyVal[0] == 0)57if (keyVal[0] == 0) {58keyVal[0] = 1;59}60softkey = new SecretKeySpec(keyVal, "AES");61dumpKey("softkey", softkey);6263KeyGenerator kg = KeyGenerator.getInstance("DESede", provider);64sk1 = kg.generateKey();65dumpKey("skey1", sk1);66sk2 = kg.generateKey();67dumpKey("skey2", sk2);6869String token = System.getProperty("TOKEN");7071if (token == null || token.length() == 0) {72System.out.println("Error: missing TOKEN system property");73throw new Exception("token arg required");74}7576if ("nss".equals(token)) {77tokenPwd = nssPwd;78} else if ("solaris".equals(token)) {79tokenPwd = solarisPwd;80}8182int testnum = 1;83doTest();84}8586private static boolean checkSecretKeyEntry(String alias,87SecretKey expected,88boolean saveBeforeCheck)89throws Exception {9091// A bug in NSS 3.12 (Mozilla bug 471665) causes AES key lengths92// to be read incorrectly. Checking for improper 16 byte length93// in key string.94if (isNSS(provider) && expected.getAlgorithm().equals("AES") &&95(getNSSVersion() >= 3.12 && getNSSVersion() <= 3.122)) {96System.out.println("NSS 3.12 bug returns incorrect AES key "+97"length breaking key storage. Aborting...");98return true;99}100101if (saveBeforeCheck) {102ks.setKeyEntry(alias, expected, null, null);103}104SecretKey result = (SecretKey) (ks.getKey(alias, null));105String keyEncFormat = result.getFormat();106if (keyEncFormat == null) {107// sensitive or un-extractable keys - verify by encrypt/decrypt108byte[] data = new byte[64];109Cipher c =110Cipher.getInstance(result.getAlgorithm() + "/CBC/NoPadding",111provider);112c.init(Cipher.ENCRYPT_MODE, expected);113byte[] encOut = c.doFinal(data);114c.init(Cipher.DECRYPT_MODE, result, c.getParameters());115byte[] decOut = c.doFinal(encOut);116if (!Arrays.equals(data, decOut)) {117return false;118}119} else if (keyEncFormat.toUpperCase().equals("RAW")) {120if (!Arrays.equals(result.getEncoded(), expected.getEncoded())) {121dumpKey("\texpected:", expected);122dumpKey("\treturns:", result);123return false;124}125}126return true;127}128129private static void dumpKey(String info, SecretKey key) {130System.out.println(info + "> " + key);131System.out.println("\tALGO=" + key.getAlgorithm());132if (key.getFormat() != null) {133System.out.println("\t[" + key.getFormat() + "] VALUE=" +134DatatypeConverter.printHexBinary(key.getEncoded()));135} else {136System.out.println("\tVALUE=n/a");137}138}139140private static void doTest() throws Exception {141// Make sure both NSS libraries are the same version.142if (isNSS(provider) &&143(getLibsoftokn3Version() != getLibnss3Version())) {144System.out.println("libsoftokn3 and libnss3 versions do not match. Aborting test...");145return;146}147148if (ks == null) {149ks = KeyStore.getInstance(KS_TYPE, provider);150ks.load(null, tokenPwd);151}152153System.out.println("Number of entries: " + ks.size());154if (ks.size() != 0) {155System.out.println("Deleting entries under aliases: ");156for (Enumeration<String> aliases = ks.aliases();157aliases.hasMoreElements();) {158String alias = aliases.nextElement();159System.out.println("\t" + alias);160ks.deleteEntry(alias);161}162}163164String alias = "testSKey";165166boolean testResult = checkSecretKeyEntry(alias, softkey, true);167if (!testResult) {168System.out.println("FAILURE: setKey() w/ softSecretKey failed");169}170171if (!checkSecretKeyEntry(alias, sk1, true)) {172testResult = false;173System.out.println("FAILURE: setKey() w/ skey1 failed");174}175if (!checkSecretKeyEntry(alias, sk2, true)) {176testResult = false;177System.out.println("FAILURE: setKey() w/ skey2 failed");178}179180ks.store(null);181System.out.println("Reloading keystore...");182183ks.load(null, "whatever".toCharArray());184if (ks.size() != 1) {185System.out.println("FAILURE: reload#1 ks.size() != 1");186}187if (!checkSecretKeyEntry(alias, sk2, false)) {188testResult = false;189System.out.println("FAILURE: reload#1 ks entry check failed");190}191192ks.deleteEntry(alias);193ks.store(null);194195System.out.println("Reloading keystore...");196ks.load(null, "whatever".toCharArray());197if (ks.size() != 0) {198testResult = false;199System.out.println("FAILURE: reload#2 ks.size() != 0");200}201if (!testResult) {202throw new Exception("One or more test failed!");203}204}205}206207208