test/jdk/sun/security/pkcs11/KeyAgreement/TestShort.java
author jjiang
Wed, 15 Aug 2018 18:41:18 +0800
changeset 51460 97e361fe3433
parent 47216 71c04702a3d5
permissions -rw-r--r--
8164639: Configure PKCS11 tests to use user-supplied NSS libraries Summary: Provide system property "test.nss.lib.paths" for specifying a set of absolute paths to the custom NSS lib directories Reviewed-by: weijun, rhalade
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
51460
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 47216
diff changeset
     2
 * Copyright (c) 2003, 2018, 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
40975
680639c9b307 8165689: Fix module dependencies for sun/security/pkcs11/* tests
skovalev
parents: 35379
diff changeset
    24
/*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
12201
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
    26
 * @bug 4942494 7146728
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary KAT test for DH (normal and with secret that has leading a 0x00 byte)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Andreas Sterbenz
51460
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 47216
diff changeset
    29
 * @library /test/lib ..
43248
5e15de85a1a0 8172527: Rename jdk.crypto.token to jdk.crypto.cryptoki
ascarpino
parents: 42693
diff changeset
    30
 * @modules jdk.crypto.cryptoki
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 12201
diff changeset
    31
 * @run main/othervm TestShort
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 12201
diff changeset
    32
 * @run main/othervm TestShort sm
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.math.BigInteger;
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 12201
diff changeset
    36
import java.security.KeyFactory;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 12201
diff changeset
    37
import java.security.PrivateKey;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 12201
diff changeset
    38
import java.security.Provider;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 12201
diff changeset
    39
import java.security.PublicKey;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 12201
diff changeset
    40
import java.util.Arrays;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 12201
diff changeset
    41
import javax.crypto.KeyAgreement;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 12201
diff changeset
    42
import javax.crypto.spec.DHPrivateKeySpec;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 12201
diff changeset
    43
import javax.crypto.spec.DHPublicKeySpec;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public class TestShort extends PKCS11Test {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private final static BigInteger p = new BigInteger
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    ("132323768951986124075479307182674357577285270296234088722451560397577130"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    + "29036368719146452186041204237350521785240337048752071462798273003935646"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    + "236777459223");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private final static BigInteger g = new BigInteger
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    ("542164405743647514160964848832570512804742839438047437683466730076610826"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    + "26139005426812890807137245973106730741193551360857959820973906708903671"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    + "85141189796");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private final static BigInteger y1 = new BigInteger
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    ("917822587297202019713917824657175324360828836418754472207798053179332700"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    + "39938196470323405362414543604756313574842317687108720161868374135893507"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    + "32549013008");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private final static BigInteger x1 = new BigInteger
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    ("44680539865608058021525420137770558786664900449");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private final static BigInteger y2 = new BigInteger
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    ("971516093764754129400636279042779828227876735997548759620533874940954728"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    + "96003923584532197641582422156725687657451980378160229472095259392582713"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    + "54693857368");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private final static BigInteger x2 = new BigInteger
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    ("433011588852527167500079509018272713204454720683");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private final static byte[] s2 = parse
12201
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
    74
    ("00:19:c7:f1:bb:2e:3d:93:fa:02:d2:e9:9f:75:32:b9:e6:7a:a0:4a:10:45:81:d4:2b:"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    + "e2:77:4c:70:41:39:7c:19:fa:65:64:47:49:8a:ad:0a:fa:9d:e9:62:68:97:c5:52"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    + ":b1:37:03:d9:cd:aa:e1:bd:7e:71:0c:fc:15:a1:95");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private final static BigInteger y3 = new BigInteger
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    ("487191942830952492045314176949691887949505843590154039270855000076570641"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    + "84133173374554778014985281423493547105556633876312739488944445812738030"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    + "00691614787");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private final static BigInteger x3 = new BigInteger
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    ("1105612503769813327556221318510360767544481637404");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private final static byte[] s3 = parse
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    ("98:62:f3:e4:ff:2b:8d:8a:5a:20:fe:52:35:56:73:09:8e:b3:e2:cb:e2:45:e5:b7:"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    + "1a:6a:15:d8:a4:8c:0a:ce:f0:15:03:0c:c2:56:82:a2:75:9b:49:fe:ed:60:c5:6e"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    + ":de:47:55:62:4f:16:20:6d:74:cc:7b:95:93:25:2c:ea");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 12201
diff changeset
    91
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public void main(Provider provider) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        if (provider.getService("KeyAgreement", "DH") == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            System.out.println("DH not supported, skipping");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
12201
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
    97
        try {
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
    98
            DHPublicKeySpec publicSpec;
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
    99
            DHPrivateKeySpec privateSpec;
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   100
            KeyFactory kf = KeyFactory.getInstance("DH", provider);
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   101
            KeyAgreement ka = KeyAgreement.getInstance("DH", provider);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
12201
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   103
            PrivateKey pr1 = kf.generatePrivate(new DHPrivateKeySpec(x1, p, g));
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   104
            PublicKey pu2 = kf.generatePublic(new DHPublicKeySpec(y2, p, g));
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   105
            PublicKey pu3 = kf.generatePublic(new DHPublicKeySpec(y3, p, g));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
12201
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   107
            ka.init(pr1);
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   108
            ka.doPhase(pu2, true);
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   109
            byte[] n2 = ka.generateSecret();
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   110
            if (Arrays.equals(s2, n2) == false) {
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   111
                throw new Exception("mismatch 2");
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   112
            }
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   113
            System.out.println("short ok");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
12201
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   115
            ka.init(pr1);
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   116
            ka.doPhase(pu3, true);
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   117
            byte[] n3 = ka.generateSecret();
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   118
            if (Arrays.equals(s3, n3) == false) {
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   119
                throw new Exception("mismatch 3");
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   120
            }
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   121
            System.out.println("normal ok");
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   122
        } catch (Exception ex) {
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   123
            System.out.println("Unexpected Exception: " + ex);
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   124
            ex.printStackTrace();
d77ed23f4992 7146728: Inconsistent length for the generated secret using DH key agreement impl from SunJCE and PKCS11
valeriep
parents: 5506
diff changeset
   125
            throw ex;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("DH", provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        kpg.initialize(512);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
//        KeyPair kp1 = kpg.generateKeyPair();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
//      System.out.println(kp1.getPublic());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
//      System.out.println(kp1.getPrivate());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            KeyAgreement ka = KeyAgreement.getInstance("DH", provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            ka.init(pr1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            KeyPair kp2 = kpg.generateKeyPair();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            ka.doPhase(kp2.getPublic(), true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            byte[] sec = ka.generateSecret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            if (sec.length == 64) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                System.out.println(kp2.getPrivate());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                System.out.println(kp2.getPublic());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                System.out.println(toString(sec));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
/**/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    public static void main(String[] args) throws Exception {
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 12201
diff changeset
   151
        main(new TestShort(), args);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
}