jdk/test/sun/security/pkcs11/tls/TestPRF.java
author asmotrak
Tue, 26 Jan 2016 13:32:07 -0800
changeset 35379 1e8e336ef66b
parent 30820 0d4717a011d3
child 40975 680639c9b307
permissions -rw-r--r--
8144539: Update PKCS11 tests to run with security manager Reviewed-by: valeriep, ascarpino
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 6345251
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Basic known-answer-test for TlsPrf
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.spec
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    31
 * @run main/othervm TestPRF
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    32
 * @run main/othervm TestPRF sm policy
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    35
import java.io.BufferedReader;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    36
import java.nio.file.Files;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    37
import java.nio.file.Paths;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.Provider;
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    39
import java.util.Arrays;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.crypto.KeyGenerator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.crypto.SecretKey;
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    42
import javax.crypto.spec.SecretKeySpec;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    43
import sun.security.internal.spec.TlsPrfParameterSpec;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public class TestPRF extends PKCS11Test {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    47
    private static final int PREFIX_LENGTH = "prf-output: ".length();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    public static void main(String[] args) throws Exception {
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    50
        main(new TestPRF(), args);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    53
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public void main(Provider provider) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        if (provider.getService("KeyGenerator", "SunTlsPrf") == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            System.out.println("Provider does not support algorithm, skipping");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    60
        try (BufferedReader reader = Files.newBufferedReader(
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    61
                Paths.get(BASE, "prfdata.txt"))) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    63
            int n = 0;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    64
            int lineNumber = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    66
            byte[] secret = null;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    67
            String label = null;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    68
            byte[] seed = null;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    69
            int length = 0;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    70
            byte[] output = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    72
            while (true) {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    73
                String line = reader.readLine();
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    74
                lineNumber++;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    75
                if (line == null) {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    76
                    break;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    77
                }
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    78
                if (line.startsWith("prf-") == false) {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    79
                    continue;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    80
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    82
                String data = line.substring(PREFIX_LENGTH);
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    83
                if (line.startsWith("prf-secret:")) {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    84
                    secret = parse(data);
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    85
                } else if (line.startsWith("prf-label:")) {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    86
                    label = data;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    87
                } else if (line.startsWith("prf-seed:")) {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    88
                    seed = parse(data);
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    89
                } else if (line.startsWith("prf-length:")) {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    90
                    length = Integer.parseInt(data);
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    91
                } else if (line.startsWith("prf-output:")) {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    92
                    output = parse(data);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    94
                    System.out.print(".");
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    95
                    n++;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    97
                    KeyGenerator kg =
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    98
                        KeyGenerator.getInstance("SunTlsPrf", provider);
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
    99
                    SecretKey inKey;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                    if (secret == null) {
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   101
                        inKey = null;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   102
                    } else {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   103
                        inKey = new SecretKeySpec(secret, "Generic");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                    }
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   105
                    TlsPrfParameterSpec spec =
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   106
                        new TlsPrfParameterSpec(inKey, label, seed, length,
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   107
                            null, -1, -1);
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   108
                    SecretKey key;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   109
                    try {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   110
                        kg.init(spec);
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   111
                        key = kg.generateKey();
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   112
                    } catch (Exception e) {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   113
                        if (secret == null) {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   114
                            // This fails on Solaris, but since we never call this
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   115
                            // API for this case in JSSE, ignore the failure.
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   116
                            // (SunJSSE uses the CKM_TLS_KEY_AND_MAC_DERIVE
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   117
                            // mechanism)
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   118
                            System.out.print("X");
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   119
                            continue;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   120
                        }
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   121
                        System.out.println();
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   122
                        throw new Exception("Error on line: " + lineNumber, e);
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
                    byte[] enc = key.getEncoded();
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   125
                    if (Arrays.equals(output, enc) == false) {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   126
                        System.out.println();
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   127
                        System.out.println("expected: " + toString(output));
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   128
                        System.out.println("actual:   " + toString(enc));
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   129
                        throw new Exception("mismatch line: " + lineNumber);
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   130
                    }
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   131
                } else {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   132
                    throw new Exception("Unknown line: " + line);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                }
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   134
            }
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   135
            if (n == 0) {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   136
                throw new Exception("no tests");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            }
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   138
            System.out.println();
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 30820
diff changeset
   139
            System.out.println("OK: " + n + " tests");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
}