jdk/test/java/security/KeyPairGenerator/SolarisShortDSA.java
author jjg
Mon, 25 Nov 2013 17:42:28 -0800
changeset 21894 3535c1819067
parent 10428 0bb8e9b89b98
child 30820 0d4717a011d3
permissions -rw-r--r--
8028318: [doclint] doclint will reject existing user-written doc comments using custom tags that follow the recommended rules Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10428
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
     1
/*
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
     2
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
     4
 *
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
     7
 * published by the Free Software Foundation.
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
     8
 *
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    13
 * accompanied this code).
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    14
 *
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    18
 *
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    21
 * questions.
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    22
 */
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    23
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    24
/*
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    25
 * @test
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    26
 * @bug 7081411
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    27
 * @summary DSA keypair generation affected by Solaris bug
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    28
 */
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    29
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    30
import java.security.KeyPair;
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    31
import java.security.KeyPairGenerator;
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    32
import java.security.Signature;
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    33
import sun.security.provider.DSAPrivateKey;
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    34
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    35
public class SolarisShortDSA {
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    36
    static byte[] data = new byte[0];
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    37
    public static void main(String args[]) throws Exception {
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    38
        for (int i=0; i<10000; i++) {
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    39
            KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA");
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    40
            KeyPair kp = kpg.generateKeyPair();
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    41
            DSAPrivateKey dpk = (DSAPrivateKey)kp.getPrivate();
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    42
            int len = dpk.getX().bitLength();
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    43
            if (len <= 152) {
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    44
                if (!use(kp)) {
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    45
                    String os = System.getProperty("os.name");
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    46
                    // Solaris bug, update the following line once it's fixed
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    47
                    if (os.equals("SunOS")) {
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    48
                        throw new IllegalStateException(
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    49
                                "Don't panic. This is a Solaris bug");
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    50
                    } else {
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    51
                        throw new RuntimeException("Real test failure");
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    52
                    }
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    53
                }
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    54
                break;
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    55
            }
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    56
        }
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    57
    }
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    58
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    59
    static boolean use(KeyPair kp) throws Exception {
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    60
        Signature sig = Signature.getInstance("SHA1withDSA");
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    61
        sig.initSign(kp.getPrivate());
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    62
        sig.update(data);
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    63
        byte[] signed = sig.sign();
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    64
        Signature sig2 = Signature.getInstance("SHA1withDSA");
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    65
        sig2.initVerify(kp.getPublic());
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    66
        sig2.update(data);
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    67
        return sig2.verify(signed);
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    68
   }
0bb8e9b89b98 7081411: DSA keypair generation affected by Solaris bug
weijun
parents:
diff changeset
    69
}