jdk/test/sun/security/pkcs11/tls/TestMasterSecret.java
author erikj
Fri, 01 Apr 2016 17:08:43 +0200
changeset 36725 f458544b0d76
parent 35379 1e8e336ef66b
child 40975 680639c9b307
permissions -rw-r--r--
8153261: Clean up fix for JDK-8153217 Reviewed-by: tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
     2
 * Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
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 TlsMasterSecret generator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @library ..
30820
0d4717a011d3 8081347: Add @modules to jdk_core tests
mchung
parents: 7043
diff changeset
    30
 * @modules java.base/sun.security.internal.interfaces
0d4717a011d3 8081347: Add @modules to jdk_core tests
mchung
parents: 7043
diff changeset
    31
 *          java.base/sun.security.internal.spec
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    32
 * @run main/othervm TestMasterSecret
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    33
 * @run main/othervm TestMasterSecret sm TestMasterSecret.policy
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    36
import java.io.BufferedReader;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    37
import java.nio.file.Files;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    38
import java.nio.file.Paths;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.Provider;
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    40
import java.util.Arrays;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.crypto.KeyGenerator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.crypto.SecretKey;
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    43
import javax.crypto.spec.SecretKeySpec;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import sun.security.internal.interfaces.TlsMasterSecret;
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    45
import sun.security.internal.spec.TlsMasterSecretParameterSpec;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
public class TestMasterSecret extends PKCS11Test {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    49
    private static final int PREFIX_LENGTH = "m-premaster:  ".length();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public static void main(String[] args) throws Exception {
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    52
        main(new TestMasterSecret(), args);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    55
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    public void main(Provider provider) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        if (provider.getService("KeyGenerator", "SunTlsMasterSecret") == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            System.out.println("Not supported by provider, skipping");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        }
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    61
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    62
        try (BufferedReader reader = Files.newBufferedReader(
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    63
                Paths.get(BASE, "masterdata.txt"))) {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    64
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    65
            int n = 0;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    66
            int lineNumber = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    68
            String algorithm = null;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    69
            byte[] premaster = null;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    70
            byte[] clientRandom = null;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    71
            byte[] serverRandom = null;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    72
            int protoMajor = 0;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    73
            int protoMinor = 0;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    74
            int preMajor = 0;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    75
            int preMinor = 0;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    76
            byte[] master = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    78
            while (true) {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    79
                String line = reader.readLine();
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    80
                lineNumber++;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    81
                if (line == null) {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    82
                    break;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    83
                }
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    84
                if (line.startsWith("m-") == false) {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    85
                    continue;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    86
                }
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    87
                String data = line.substring(PREFIX_LENGTH);
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    88
                if (line.startsWith("m-algorithm:")) {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    89
                    algorithm = data;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    90
                } else if (line.startsWith("m-premaster:")) {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    91
                    premaster = parse(data);
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    92
                } else if (line.startsWith("m-crandom:")) {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    93
                    clientRandom = parse(data);
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    94
                } else if (line.startsWith("m-srandom:")) {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    95
                    serverRandom = parse(data);
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    96
                } else if (line.startsWith("m-protomajor:")) {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    97
                    protoMajor = Integer.parseInt(data);
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    98
                } else if (line.startsWith("m-protominor:")) {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    99
                    protoMinor = Integer.parseInt(data);
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   100
                } else if (line.startsWith("m-premajor:")) {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   101
                    preMajor = Integer.parseInt(data);
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   102
                } else if (line.startsWith("m-preminor:")) {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   103
                    preMinor = Integer.parseInt(data);
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   104
                } else if (line.startsWith("m-master:")) {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   105
                    master = parse(data);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   107
                    System.out.print(".");
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   108
                    n++;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   109
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   110
                    KeyGenerator kg =
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   111
                        KeyGenerator.getInstance("SunTlsMasterSecret", provider);
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   112
                    SecretKey premasterKey =
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   113
                        new SecretKeySpec(premaster, algorithm);
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   114
                    TlsMasterSecretParameterSpec spec =
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   115
                        new TlsMasterSecretParameterSpec(premasterKey,
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   116
                            protoMajor, protoMinor, clientRandom, serverRandom,
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   117
                            null, -1, -1);
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   118
                    kg.init(spec);
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   119
                    TlsMasterSecret key = (TlsMasterSecret)kg.generateKey();
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   120
                    byte[] enc = key.getEncoded();
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   121
                    if (Arrays.equals(master, enc) == false) {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   122
                        throw new Exception("mismatch line: " + lineNumber);
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   123
                    }
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   124
                    if ((preMajor != key.getMajorVersion()) ||
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   125
                            (preMinor != key.getMinorVersion())) {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   126
                        throw new Exception("version mismatch line: " + lineNumber);
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   127
                    }
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   128
                } else {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   129
                    throw new Exception("Unknown line: " + line);
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   130
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            }
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   132
            if (n == 0) {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   133
                throw new Exception("no tests");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            }
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   135
            System.out.println();
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   136
            System.out.println("OK: " + n + " tests");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
}