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