jdk/test/sun/security/pkcs11/KeyAgreement/TestDH.java
author stefank
Mon, 04 Apr 2016 09:15:01 +0200
changeset 37241 b9961c99c356
parent 35379 1e8e336ef66b
child 40975 680639c9b307
permissions -rw-r--r--
8152538: UL doesn't inline the LogTagSet::is_level check Reviewed-by: mlarsson, brutisso
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: 5506
diff changeset
     2
 * Copyright (c) 2003, 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 4921804 6324825
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Verify that DH works properly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @library ..
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 5506
diff changeset
    30
 * @run main/othervm TestDH
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 5506
diff changeset
    31
 * @run main/othervm TestDH sm
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 5506
diff changeset
    34
import java.security.KeyPair;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 5506
diff changeset
    35
import java.security.KeyPairGenerator;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 5506
diff changeset
    36
import java.security.Provider;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 5506
diff changeset
    37
import java.util.Arrays;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 5506
diff changeset
    38
import javax.crypto.KeyAgreement;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 5506
diff changeset
    39
import javax.crypto.SecretKey;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public class TestDH extends PKCS11Test {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 5506
diff changeset
    43
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    public void main(Provider p) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        if (p.getService("KeyAgreement", "DH") == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            System.out.println("DH not supported, skipping");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("DH", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        kpg.initialize(512);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        KeyPair kp1 = kpg.generateKeyPair();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        KeyPair kp2 = kpg.generateKeyPair();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        KeyAgreement ka1, ka2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        ka1 = KeyAgreement.getInstance("DH", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        ka1.init(kp1.getPrivate());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        ka1.doPhase(kp2.getPublic(), true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        System.out.println("Derive 1...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        byte[] secret1 = ka1.generateSecret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        ka1.init(kp2.getPrivate());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        ka1.doPhase(kp1.getPublic(), true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        System.out.println("Derive 2...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        byte[] secret2 = ka1.generateSecret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        if (Arrays.equals(secret1, secret2) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            throw new Exception("Secrets (1,2) do not match");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        ka2 = KeyAgreement.getInstance("DH", "SunJCE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        ka2.init(kp1.getPrivate());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        ka2.doPhase(kp2.getPublic(), true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        System.out.println("Derive 3...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        byte[] secret3 = ka2.generateSecret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        if (Arrays.equals(secret1, secret3) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            throw new Exception("Secrets (1,3) do not match");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        ka2.init(kp2.getPrivate());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        ka2.doPhase(kp1.getPublic(), true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        System.out.println("Derive 4...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        byte[] secret4 = ka2.generateSecret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        if (Arrays.equals(secret1, secret4) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            throw new Exception("Secrets (1,4) do not match");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        testAlgorithm(ka2, kp2, ka1, kp1, "DES");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        testAlgorithm(ka2, kp2, ka1, kp1, "DESede");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
//      testAlgorithm(ka2, kp2, ka1, kp1, "AES");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
//      testAlgorithm(ka2, kp2, ka1, kp1, "RC4");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        testAlgorithm(ka2, kp2, ka1, kp1, "Blowfish");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        testAlgorithm(ka2, kp2, ka1, kp1, "TlsPremasterSecret");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 5506
diff changeset
    97
    private static void testAlgorithm(KeyAgreement ka1, KeyPair kp1,
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 5506
diff changeset
    98
            KeyAgreement ka2, KeyPair kp2, String algorithm) throws Exception {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 5506
diff changeset
    99
        SecretKey key1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        ka1.init(kp1.getPrivate());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        ka1.doPhase(kp2.getPublic(), true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        System.out.println("Derive " + algorithm + " using SunJCE...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        key1 = ka1.generateSecret(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        ka2.init(kp1.getPrivate());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        ka2.doPhase(kp2.getPublic(), true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        System.out.println("Derive " + algorithm + " using PKCS#11...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        SecretKey key2 = ka2.generateSecret(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        byte[] b1 = key1.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        byte[] b2 = key2.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (Arrays.equals(b1, b2) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            System.out.println(b1.length + " bytes: " + toString(b1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            System.out.println(b2.length + " bytes: " + toString(b2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            throw new Exception(algorithm + " secret mismatch");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    public static void main(String[] args) throws Exception {
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 5506
diff changeset
   122
        main(new TestDH(), args);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
}