Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/crypto/provider/TLS/TestKeyMaterial.java
38867 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*/2930import java.io.*;31import java.util.*;3233import java.security.Security;34import java.security.Provider;3536import javax.crypto.KeyGenerator;37import javax.crypto.SecretKey;3839import javax.crypto.spec.*;4041import sun.security.internal.spec.*;4243public class TestKeyMaterial extends Utils {4445private static int PREFIX_LENGTH = "km-master: ".length();4647public static void main(String[] args) throws Exception {48Provider provider = Security.getProvider("SunJCE");4950InputStream in = new FileInputStream(new File(BASE, "keymatdata.txt"));51BufferedReader reader = new BufferedReader(new InputStreamReader(in));5253int n = 0;54int lineNumber = 0;5556byte[] master = null;57int major = 0;58int minor = 0;59byte[] clientRandom = null;60byte[] serverRandom = null;61String cipherAlgorithm = null;62int keyLength = 0;63int expandedKeyLength = 0;64int ivLength = 0;65int macLength = 0;66byte[] clientCipherBytes = null;67byte[] serverCipherBytes = null;68byte[] clientIv = null;69byte[] serverIv = null;70byte[] clientMacBytes = null;71byte[] serverMacBytes = null;7273while (true) {74String line = reader.readLine();75lineNumber++;76if (line == null) {77break;78}79if (line.startsWith("km-") == false) {80continue;81}82String data = line.substring(PREFIX_LENGTH);83if (line.startsWith("km-master:")) {84master = parse(data);85} else if (line.startsWith("km-major:")) {86major = Integer.parseInt(data);87} else if (line.startsWith("km-minor:")) {88minor = Integer.parseInt(data);89} else if (line.startsWith("km-crandom:")) {90clientRandom = parse(data);91} else if (line.startsWith("km-srandom:")) {92serverRandom = parse(data);93} else if (line.startsWith("km-cipalg:")) {94cipherAlgorithm = data;95} else if (line.startsWith("km-keylen:")) {96keyLength = Integer.parseInt(data);97} else if (line.startsWith("km-explen:")) {98expandedKeyLength = Integer.parseInt(data);99} else if (line.startsWith("km-ivlen:")) {100ivLength = Integer.parseInt(data);101} else if (line.startsWith("km-maclen:")) {102macLength = Integer.parseInt(data);103} else if (line.startsWith("km-ccipkey:")) {104clientCipherBytes = parse(data);105} else if (line.startsWith("km-scipkey:")) {106serverCipherBytes = parse(data);107} else if (line.startsWith("km-civ:")) {108clientIv = parse(data);109} else if (line.startsWith("km-siv:")) {110serverIv = parse(data);111} else if (line.startsWith("km-cmackey:")) {112clientMacBytes = parse(data);113} else if (line.startsWith("km-smackey:")) {114serverMacBytes = parse(data);115116System.out.print(".");117n++;118119KeyGenerator kg =120KeyGenerator.getInstance("SunTlsKeyMaterial", provider);121SecretKey masterKey =122new SecretKeySpec(master, "TlsMasterSecret");123TlsKeyMaterialParameterSpec spec =124new TlsKeyMaterialParameterSpec(masterKey, major, minor,125clientRandom, serverRandom, cipherAlgorithm,126keyLength, expandedKeyLength, ivLength, macLength,127null, -1, -1);128129kg.init(spec);130TlsKeyMaterialSpec result =131(TlsKeyMaterialSpec)kg.generateKey();132match(lineNumber, clientCipherBytes,133result.getClientCipherKey());134match(lineNumber, serverCipherBytes,135result.getServerCipherKey());136match(lineNumber, clientIv, result.getClientIv());137match(lineNumber, serverIv, result.getServerIv());138match(lineNumber, clientMacBytes, result.getClientMacKey());139match(lineNumber, serverMacBytes, result.getServerMacKey());140141} else {142throw new Exception("Unknown line: " + line);143}144}145if (n == 0) {146throw new Exception("no tests");147}148in.close();149System.out.println();150System.out.println("OK: " + n + " tests");151}152153private static void match(int lineNumber, byte[] out, Object res)154throws Exception {155if ((out == null) || (res == null)) {156if (out != res) {157throw new Exception("null mismatch line " + lineNumber);158} else {159return;160}161}162byte[] b;163if (res instanceof SecretKey) {164b = ((SecretKey)res).getEncoded();165} else if (res instanceof IvParameterSpec) {166b = ((IvParameterSpec)res).getIV();167} else {168throw new Exception(res.getClass().getName());169}170if (Arrays.equals(out, b) == false) {171throw new Exception("mismatch line " + lineNumber);172}173}174175}176177178