Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/tls/TestKeyMaterial.java
38855 views
/*1* Copyright (c) 2005, 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.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 631653926* @summary Known-answer-test for TlsKeyMaterial generator27* @author Andreas Sterbenz28* @library ..29* @run main/othervm TestKeyMaterial30* @run main/othervm TestKeyMaterial sm policy31*/3233import java.io.BufferedReader;34import java.nio.file.Files;35import java.nio.file.Paths;36import java.security.Provider;37import java.util.Arrays;38import javax.crypto.KeyGenerator;39import javax.crypto.SecretKey;40import javax.crypto.spec.IvParameterSpec;41import javax.crypto.spec.SecretKeySpec;42import sun.security.internal.spec.TlsKeyMaterialParameterSpec;43import sun.security.internal.spec.TlsKeyMaterialSpec;4445public class TestKeyMaterial extends PKCS11Test {4647private static final int PREFIX_LENGTH = "km-master: ".length();4849public static void main(String[] args) throws Exception {50main(new TestKeyMaterial(), args);51}5253@Override54public void main(Provider provider) throws Exception {55if (provider.getService("KeyGenerator", "SunTlsKeyMaterial") == null) {56System.out.println("Provider does not support algorithm, skipping");57return;58}5960try (BufferedReader reader = Files.newBufferedReader(61Paths.get(BASE, "keymatdata.txt"))) {6263int n = 0;64int lineNumber = 0;6566byte[] master = null;67int major = 0;68int minor = 0;69byte[] clientRandom = null;70byte[] serverRandom = null;71String cipherAlgorithm = null;72int keyLength = 0;73int expandedKeyLength = 0;74int ivLength = 0;75int macLength = 0;76byte[] clientCipherBytes = null;77byte[] serverCipherBytes = null;78byte[] clientIv = null;79byte[] serverIv = null;80byte[] clientMacBytes = null;81byte[] serverMacBytes = null;8283while (true) {84String line = reader.readLine();85lineNumber++;86if (line == null) {87break;88}89if (line.startsWith("km-") == false) {90continue;91}92String data = line.substring(PREFIX_LENGTH);93if (line.startsWith("km-master:")) {94master = parse(data);95} else if (line.startsWith("km-major:")) {96major = Integer.parseInt(data);97} else if (line.startsWith("km-minor:")) {98minor = Integer.parseInt(data);99} else if (line.startsWith("km-crandom:")) {100clientRandom = parse(data);101} else if (line.startsWith("km-srandom:")) {102serverRandom = parse(data);103} else if (line.startsWith("km-cipalg:")) {104cipherAlgorithm = data;105} else if (line.startsWith("km-keylen:")) {106keyLength = Integer.parseInt(data);107} else if (line.startsWith("km-explen:")) {108expandedKeyLength = Integer.parseInt(data);109} else if (line.startsWith("km-ivlen:")) {110ivLength = Integer.parseInt(data);111} else if (line.startsWith("km-maclen:")) {112macLength = Integer.parseInt(data);113} else if (line.startsWith("km-ccipkey:")) {114clientCipherBytes = parse(data);115} else if (line.startsWith("km-scipkey:")) {116serverCipherBytes = parse(data);117} else if (line.startsWith("km-civ:")) {118clientIv = parse(data);119} else if (line.startsWith("km-siv:")) {120serverIv = parse(data);121} else if (line.startsWith("km-cmackey:")) {122clientMacBytes = parse(data);123} else if (line.startsWith("km-smackey:")) {124serverMacBytes = parse(data);125126System.out.print(".");127n++;128129KeyGenerator kg =130KeyGenerator.getInstance("SunTlsKeyMaterial", provider);131SecretKey masterKey =132new SecretKeySpec(master, "TlsMasterSecret");133TlsKeyMaterialParameterSpec spec =134new TlsKeyMaterialParameterSpec(masterKey, major, minor,135clientRandom, serverRandom, cipherAlgorithm,136keyLength, expandedKeyLength, ivLength, macLength,137null, -1, -1);138139kg.init(spec);140TlsKeyMaterialSpec result =141(TlsKeyMaterialSpec)kg.generateKey();142match(lineNumber, clientCipherBytes,143result.getClientCipherKey(), cipherAlgorithm);144match(lineNumber, serverCipherBytes,145result.getServerCipherKey(), cipherAlgorithm);146match(lineNumber, clientIv, result.getClientIv(), "");147match(lineNumber, serverIv, result.getServerIv(), "");148match(lineNumber, clientMacBytes, result.getClientMacKey(), "");149match(lineNumber, serverMacBytes, result.getServerMacKey(), "");150151} else {152throw new Exception("Unknown line: " + line);153}154}155if (n == 0) {156throw new Exception("no tests");157}158System.out.println();159System.out.println("OK: " + n + " tests");160}161}162163private static void stripParity(byte[] b) {164for (int i = 0; i < b.length; i++) {165b[i] &= 0xfe;166}167}168169private static void match(int lineNumber, byte[] out, Object res,170String cipherAlgorithm) throws Exception {171if ((out == null) || (res == null)) {172if (out != res) {173throw new Exception("null mismatch line " + lineNumber);174} else {175return;176}177}178byte[] b;179if (res instanceof SecretKey) {180b = ((SecretKey)res).getEncoded();181if (cipherAlgorithm.equalsIgnoreCase("DES") ||182cipherAlgorithm.equalsIgnoreCase("DESede")) {183// strip DES parity bits before comparision184stripParity(out);185stripParity(b);186}187} else if (res instanceof IvParameterSpec) {188b = ((IvParameterSpec)res).getIV();189} else {190throw new Exception(res.getClass().getName());191}192if (Arrays.equals(out, b) == false) {193System.out.println();194System.out.println("out: " + toString(out));195System.out.println("b: " + toString(b));196throw new Exception("mismatch line " + lineNumber);197}198}199200}201202203