Path: blob/master/test/jdk/com/sun/crypto/provider/TLS/TestKeyMaterial.java
41155 views
/*1* Copyright (c) 2005, 2010, 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 631366126* @summary Known-answer-test for TlsKeyMaterial generator27* @author Andreas Sterbenz28* @modules java.base/sun.security.internal.spec29*/3031import java.io.*;32import java.util.*;3334import java.security.Security;35import java.security.Provider;3637import javax.crypto.KeyGenerator;38import javax.crypto.SecretKey;3940import javax.crypto.spec.*;4142import sun.security.internal.spec.*;4344public class TestKeyMaterial extends Utils {4546private static int PREFIX_LENGTH = "km-master: ".length();4748public static void main(String[] args) throws Exception {49Provider provider = Security.getProvider("SunJCE");5051InputStream in = new FileInputStream(new File(BASE, "keymatdata.txt"));52BufferedReader reader = new BufferedReader(new InputStreamReader(in));5354int n = 0;55int lineNumber = 0;5657byte[] master = null;58int major = 0;59int minor = 0;60byte[] clientRandom = null;61byte[] serverRandom = null;62String cipherAlgorithm = null;63int keyLength = 0;64int expandedKeyLength = 0;65int ivLength = 0;66int macLength = 0;67byte[] clientCipherBytes = null;68byte[] serverCipherBytes = null;69byte[] clientIv = null;70byte[] serverIv = null;71byte[] clientMacBytes = null;72byte[] serverMacBytes = null;7374while (true) {75String line = reader.readLine();76lineNumber++;77if (line == null) {78break;79}80if (line.startsWith("km-") == false) {81continue;82}83String data = line.substring(PREFIX_LENGTH);84if (line.startsWith("km-master:")) {85master = parse(data);86} else if (line.startsWith("km-major:")) {87major = Integer.parseInt(data);88} else if (line.startsWith("km-minor:")) {89minor = Integer.parseInt(data);90} else if (line.startsWith("km-crandom:")) {91clientRandom = parse(data);92} else if (line.startsWith("km-srandom:")) {93serverRandom = parse(data);94} else if (line.startsWith("km-cipalg:")) {95cipherAlgorithm = data;96} else if (line.startsWith("km-keylen:")) {97keyLength = Integer.parseInt(data);98} else if (line.startsWith("km-explen:")) {99expandedKeyLength = Integer.parseInt(data);100} else if (line.startsWith("km-ivlen:")) {101ivLength = Integer.parseInt(data);102} else if (line.startsWith("km-maclen:")) {103macLength = Integer.parseInt(data);104} else if (line.startsWith("km-ccipkey:")) {105clientCipherBytes = parse(data);106} else if (line.startsWith("km-scipkey:")) {107serverCipherBytes = parse(data);108} else if (line.startsWith("km-civ:")) {109clientIv = parse(data);110} else if (line.startsWith("km-siv:")) {111serverIv = parse(data);112} else if (line.startsWith("km-cmackey:")) {113clientMacBytes = parse(data);114} else if (line.startsWith("km-smackey:")) {115serverMacBytes = parse(data);116117System.out.print(".");118n++;119120KeyGenerator kg =121KeyGenerator.getInstance("SunTlsKeyMaterial", provider);122SecretKey masterKey =123new SecretKeySpec(master, "TlsMasterSecret");124TlsKeyMaterialParameterSpec spec =125new TlsKeyMaterialParameterSpec(masterKey, major, minor,126clientRandom, serverRandom, cipherAlgorithm,127keyLength, expandedKeyLength, ivLength, macLength,128null, -1, -1);129130kg.init(spec);131TlsKeyMaterialSpec result =132(TlsKeyMaterialSpec)kg.generateKey();133match(lineNumber, clientCipherBytes,134result.getClientCipherKey());135match(lineNumber, serverCipherBytes,136result.getServerCipherKey());137match(lineNumber, clientIv, result.getClientIv());138match(lineNumber, serverIv, result.getServerIv());139match(lineNumber, clientMacBytes, result.getClientMacKey());140match(lineNumber, serverMacBytes, result.getServerMacKey());141142} else {143throw new Exception("Unknown line: " + line);144}145}146if (n == 0) {147throw new Exception("no tests");148}149in.close();150System.out.println();151System.out.println("OK: " + n + " tests");152}153154private static void match(int lineNumber, byte[] out, Object res)155throws Exception {156if ((out == null) || (res == null)) {157if (out != res) {158throw new Exception("null mismatch line " + lineNumber);159} else {160return;161}162}163byte[] b;164if (res instanceof SecretKey) {165b = ((SecretKey)res).getEncoded();166} else if (res instanceof IvParameterSpec) {167b = ((IvParameterSpec)res).getIV();168} else {169throw new Exception(res.getClass().getName());170}171if (Arrays.equals(out, b) == false) {172throw new Exception("mismatch line " + lineNumber);173}174}175176}177178179