J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2005-2007 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /** |
| 25 | * @test |
| 26 | * @bug 6313661 |
| 27 | * @summary Known-answer-test for TlsKeyMaterial generator |
| 28 | * @author Andreas Sterbenz |
| 29 | */ |
| 30 | |
| 31 | import java.io.*; |
| 32 | import java.util.*; |
| 33 | |
| 34 | import java.security.Security; |
| 35 | import java.security.Provider; |
| 36 | |
| 37 | import javax.crypto.KeyGenerator; |
| 38 | import javax.crypto.SecretKey; |
| 39 | |
| 40 | import javax.crypto.spec.*; |
| 41 | |
| 42 | import sun.security.internal.spec.*; |
| 43 | |
| 44 | public class TestKeyMaterial extends Utils { |
| 45 | |
| 46 | private static int PREFIX_LENGTH = "km-master: ".length(); |
| 47 | |
| 48 | public static void main(String[] args) throws Exception { |
| 49 | Provider provider = Security.getProvider("SunJCE"); |
| 50 | |
| 51 | InputStream in = new FileInputStream(new File(BASE, "keymatdata.txt")); |
| 52 | BufferedReader reader = new BufferedReader(new InputStreamReader(in)); |
| 53 | |
| 54 | int n = 0; |
| 55 | int lineNumber = 0; |
| 56 | |
| 57 | byte[] master = null; |
| 58 | int major = 0; |
| 59 | int minor = 0; |
| 60 | byte[] clientRandom = null; |
| 61 | byte[] serverRandom = null; |
| 62 | String cipherAlgorithm = null; |
| 63 | int keyLength = 0; |
| 64 | int expandedKeyLength = 0; |
| 65 | int ivLength = 0; |
| 66 | int macLength = 0; |
| 67 | byte[] clientCipherBytes = null; |
| 68 | byte[] serverCipherBytes = null; |
| 69 | byte[] clientIv = null; |
| 70 | byte[] serverIv = null; |
| 71 | byte[] clientMacBytes = null; |
| 72 | byte[] serverMacBytes = null; |
| 73 | |
| 74 | while (true) { |
| 75 | String line = reader.readLine(); |
| 76 | lineNumber++; |
| 77 | if (line == null) { |
| 78 | break; |
| 79 | } |
| 80 | if (line.startsWith("km-") == false) { |
| 81 | continue; |
| 82 | } |
| 83 | String data = line.substring(PREFIX_LENGTH); |
| 84 | if (line.startsWith("km-master:")) { |
| 85 | master = parse(data); |
| 86 | } else if (line.startsWith("km-major:")) { |
| 87 | major = Integer.parseInt(data); |
| 88 | } else if (line.startsWith("km-minor:")) { |
| 89 | minor = Integer.parseInt(data); |
| 90 | } else if (line.startsWith("km-crandom:")) { |
| 91 | clientRandom = parse(data); |
| 92 | } else if (line.startsWith("km-srandom:")) { |
| 93 | serverRandom = parse(data); |
| 94 | } else if (line.startsWith("km-cipalg:")) { |
| 95 | cipherAlgorithm = data; |
| 96 | } else if (line.startsWith("km-keylen:")) { |
| 97 | keyLength = Integer.parseInt(data); |
| 98 | } else if (line.startsWith("km-explen:")) { |
| 99 | expandedKeyLength = Integer.parseInt(data); |
| 100 | } else if (line.startsWith("km-ivlen:")) { |
| 101 | ivLength = Integer.parseInt(data); |
| 102 | } else if (line.startsWith("km-maclen:")) { |
| 103 | macLength = Integer.parseInt(data); |
| 104 | } else if (line.startsWith("km-ccipkey:")) { |
| 105 | clientCipherBytes = parse(data); |
| 106 | } else if (line.startsWith("km-scipkey:")) { |
| 107 | serverCipherBytes = parse(data); |
| 108 | } else if (line.startsWith("km-civ:")) { |
| 109 | clientIv = parse(data); |
| 110 | } else if (line.startsWith("km-siv:")) { |
| 111 | serverIv = parse(data); |
| 112 | } else if (line.startsWith("km-cmackey:")) { |
| 113 | clientMacBytes = parse(data); |
| 114 | } else if (line.startsWith("km-smackey:")) { |
| 115 | serverMacBytes = parse(data); |
| 116 | |
| 117 | System.out.print("."); |
| 118 | n++; |
| 119 | |
| 120 | KeyGenerator kg = KeyGenerator.getInstance("SunTlsKeyMaterial", provider); |
| 121 | SecretKey masterKey = new SecretKeySpec(master, "TlsMasterSecret"); |
| 122 | TlsKeyMaterialParameterSpec spec = new TlsKeyMaterialParameterSpec |
| 123 | (masterKey, major, minor, clientRandom, serverRandom, cipherAlgorithm, |
| 124 | keyLength, expandedKeyLength, ivLength, macLength); |
| 125 | |
| 126 | kg.init(spec); |
| 127 | TlsKeyMaterialSpec result = (TlsKeyMaterialSpec)kg.generateKey(); |
| 128 | match(lineNumber, clientCipherBytes, result.getClientCipherKey()); |
| 129 | match(lineNumber, serverCipherBytes, result.getServerCipherKey()); |
| 130 | match(lineNumber, clientIv, result.getClientIv()); |
| 131 | match(lineNumber, serverIv, result.getServerIv()); |
| 132 | match(lineNumber, clientMacBytes, result.getClientMacKey()); |
| 133 | match(lineNumber, serverMacBytes, result.getServerMacKey()); |
| 134 | |
| 135 | } else { |
| 136 | throw new Exception("Unknown line: " + line); |
| 137 | } |
| 138 | } |
| 139 | if (n == 0) { |
| 140 | throw new Exception("no tests"); |
| 141 | } |
| 142 | in.close(); |
| 143 | System.out.println(); |
| 144 | System.out.println("OK: " + n + " tests"); |
| 145 | } |
| 146 | |
| 147 | private static void match(int lineNumber, byte[] out, Object res) throws Exception { |
| 148 | if ((out == null) || (res == null)) { |
| 149 | if (out != res) { |
| 150 | throw new Exception("null mismatch line " + lineNumber); |
| 151 | } else { |
| 152 | return; |
| 153 | } |
| 154 | } |
| 155 | byte[] b; |
| 156 | if (res instanceof SecretKey) { |
| 157 | b = ((SecretKey)res).getEncoded(); |
| 158 | } else if (res instanceof IvParameterSpec) { |
| 159 | b = ((IvParameterSpec)res).getIV(); |
| 160 | } else { |
| 161 | throw new Exception(res.getClass().getName()); |
| 162 | } |
| 163 | if (Arrays.equals(out, b) == false) { |
| 164 | throw new Exception("mismatch line " + lineNumber); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | } |