Path: blob/master/test/jdk/sun/security/pkcs11/tls/TestKeyMaterial.java
41155 views
/*1* Copyright (c) 2005, 2020, 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 6316539 813635526* @summary Known-answer-test for TlsKeyMaterial generator27* @author Andreas Sterbenz28* @library /test/lib ..29* @modules java.base/sun.security.internal.spec30* jdk.crypto.cryptoki31* @run main/othervm TestKeyMaterial32* @run main/othervm -Djava.security.manager=allow TestKeyMaterial sm policy33*/3435import java.io.BufferedReader;36import java.nio.file.Files;37import java.nio.file.Paths;38import java.security.InvalidAlgorithmParameterException;39import java.security.Provider;40import java.security.ProviderException;41import java.util.Arrays;4243import javax.crypto.KeyGenerator;44import javax.crypto.SecretKey;45import javax.crypto.spec.IvParameterSpec;46import javax.crypto.spec.SecretKeySpec;4748import sun.security.internal.spec.TlsKeyMaterialParameterSpec;49import sun.security.internal.spec.TlsKeyMaterialSpec;5051public class TestKeyMaterial extends PKCS11Test {5253private static final int PREFIX_LENGTH = "km-master: ".length();5455public static void main(String[] args) throws Exception {56System.out.println("NSS Version: " + getNSSVersion());57main(new TestKeyMaterial(), args);58}5960@Override61public void main(Provider provider) throws Exception {62if (provider.getService("KeyGenerator", "SunTlsKeyMaterial") == null) {63System.out.println("Provider does not support algorithm, skipping");64return;65}6667try (BufferedReader reader = Files.newBufferedReader(68Paths.get(BASE, "keymatdata.txt"))) {6970int n = 0;71int lineNumber = 0;7273byte[] master = null;74int major = 0;75int minor = 0;76byte[] clientRandom = null;77byte[] serverRandom = null;78String cipherAlgorithm = null;79int keyLength = 0;80int expandedKeyLength = 0;81int ivLength = 0;82int macLength = 0;83byte[] clientCipherBytes = null;84byte[] serverCipherBytes = null;85byte[] clientIv = null;86byte[] serverIv = null;87byte[] clientMacBytes = null;88byte[] serverMacBytes = null;8990while (true) {91String line = reader.readLine();92lineNumber++;93if (line == null) {94break;95}96if (line.startsWith("km-") == false) {97continue;98}99String data = line.substring(PREFIX_LENGTH);100if (line.startsWith("km-master:")) {101master = parse(data);102} else if (line.startsWith("km-major:")) {103major = Integer.parseInt(data);104} else if (line.startsWith("km-minor:")) {105minor = Integer.parseInt(data);106} else if (line.startsWith("km-crandom:")) {107clientRandom = parse(data);108} else if (line.startsWith("km-srandom:")) {109serverRandom = parse(data);110} else if (line.startsWith("km-cipalg:")) {111cipherAlgorithm = data;112} else if (line.startsWith("km-keylen:")) {113keyLength = Integer.parseInt(data);114} else if (line.startsWith("km-explen:")) {115expandedKeyLength = Integer.parseInt(data);116} else if (line.startsWith("km-ivlen:")) {117ivLength = Integer.parseInt(data);118} else if (line.startsWith("km-maclen:")) {119macLength = Integer.parseInt(data);120} else if (line.startsWith("km-ccipkey:")) {121clientCipherBytes = parse(data);122} else if (line.startsWith("km-scipkey:")) {123serverCipherBytes = parse(data);124} else if (line.startsWith("km-civ:")) {125clientIv = parse(data);126} else if (line.startsWith("km-siv:")) {127serverIv = parse(data);128} else if (line.startsWith("km-cmackey:")) {129clientMacBytes = parse(data);130} else if (line.startsWith("km-smackey:")) {131serverMacBytes = parse(data);132133System.out.print(".");134n++;135136KeyGenerator kg =137KeyGenerator.getInstance("SunTlsKeyMaterial", provider);138SecretKey masterKey =139new SecretKeySpec(master, "TlsMasterSecret");140TlsKeyMaterialParameterSpec spec =141new TlsKeyMaterialParameterSpec(masterKey, major, minor,142clientRandom, serverRandom, cipherAlgorithm,143keyLength, expandedKeyLength, ivLength, macLength,144null, -1, -1);145146try {147kg.init(spec);148TlsKeyMaterialSpec result =149(TlsKeyMaterialSpec)kg.generateKey();150match(lineNumber, clientCipherBytes,151result.getClientCipherKey(), cipherAlgorithm);152match(lineNumber, serverCipherBytes,153result.getServerCipherKey(), cipherAlgorithm);154match(lineNumber, clientIv, result.getClientIv(), "");155match(lineNumber, serverIv, result.getServerIv(), "");156match(lineNumber, clientMacBytes, result.getClientMacKey(), "");157match(lineNumber, serverMacBytes, result.getServerMacKey(), "");158} catch (ProviderException pe) {159if (provider.getName().indexOf("NSS") != -1) {160Throwable t = pe.getCause();161if (expandedKeyLength != 0162&& t.getMessage().indexOf(163"CKR_MECHANISM_PARAM_INVALID") != -1) {164// NSS removed support for export-grade cipher suites in 3.28,165// see https://bugzilla.mozilla.org/show_bug.cgi?id=1252849166System.out.println("Ignore known NSS failure on CKR_MECHANISM_PARAM_INVALID");167continue;168}169}170throw pe;171}172} else {173throw new Exception("Unknown line: " + line);174}175}176if (n == 0) {177throw new Exception("no tests");178}179System.out.println();180System.out.println("OK: " + n + " tests");181}182}183184private static void stripParity(byte[] b) {185for (int i = 0; i < b.length; i++) {186b[i] &= 0xfe;187}188}189190private static void match(int lineNumber, byte[] out, Object res,191String cipherAlgorithm) throws Exception {192if ((out == null) || (res == null)) {193if (out != res) {194throw new Exception("null mismatch line " + lineNumber);195} else {196return;197}198}199byte[] b;200if (res instanceof SecretKey) {201b = ((SecretKey)res).getEncoded();202if (cipherAlgorithm.equalsIgnoreCase("DES") ||203cipherAlgorithm.equalsIgnoreCase("DESede")) {204// strip DES parity bits before comparision205stripParity(out);206stripParity(b);207}208} else if (res instanceof IvParameterSpec) {209b = ((IvParameterSpec)res).getIV();210} else {211throw new Exception(res.getClass().getName());212}213if (Arrays.equals(out, b) == false) {214System.out.println();215System.out.println("out: " + toString(out));216System.out.println("b: " + toString(b));217throw new Exception("mismatch line " + lineNumber);218}219}220221}222223224