jdk/test/sun/security/pkcs11/Signature/TestDSA.java
author asmotrak
Tue, 26 Jan 2016 13:32:07 -0800
changeset 35379 1e8e336ef66b
parent 32138 23830562d3d1
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: 32138
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 4856966
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary basic test of SHA1withDSA and RawDSA signing/verifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @library ..
30046
cf2c86e1819e 8078334: Mark regression tests using randomness
darcy
parents: 5506
diff changeset
    30
 * @key randomness
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32138
diff changeset
    31
 * @run main/othervm TestDSA
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32138
diff changeset
    32
 * @run main/othervm TestDSA sm
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: 32138
diff changeset
    35
import java.io.ByteArrayOutputStream;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32138
diff changeset
    36
import java.io.IOException;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32138
diff changeset
    37
import java.io.StringReader;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.math.BigInteger;
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32138
diff changeset
    39
import java.security.KeyFactory;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32138
diff changeset
    40
import java.security.MessageDigest;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32138
diff changeset
    41
import java.security.PrivateKey;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32138
diff changeset
    42
import java.security.Provider;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32138
diff changeset
    43
import java.security.PublicKey;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32138
diff changeset
    44
import java.security.Signature;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32138
diff changeset
    45
import java.security.SignatureException;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32138
diff changeset
    46
import java.security.spec.DSAPrivateKeySpec;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32138
diff changeset
    47
import java.security.spec.DSAPublicKeySpec;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32138
diff changeset
    48
import java.util.Random;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
public class TestDSA extends PKCS11Test {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    // values of the keys we use for the tests
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private final static String ps =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        "fd7f53811d75122952df4a9c2eece4e7f611b7523cef4400c31e3f80b6512669" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        "455d402251fb593d8d58fabfc5f5ba30f6cb9b556cd7813b801d346ff26660b7" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        "6b9950a5a49f9fe8047b1022c24fbba9d7feb7c61bf83b57e7c6a8a6150f04fb" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        "83f6d3c51ec3023554135a169132f675f3ae2b61d72aeff22203199dd14801c7";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private final static String qs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        "9760508f15230bccb292b982a2eb840bf0581cf5";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private final static String gs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        "f7e1a085d69b3ddecbbcab5c36b857b97994afbbfa3aea82f9574c0b3d078267" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        "5159578ebad4594fe67107108180b449167123e84c281613b7cf09328cc8a6e1" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        "3c167a8b547c8d28e0a3ae1e2bb3a675916ea37f0bfa213562f1fb627a01243b" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        "cca4f1bea8519089a883dfe15ae59f06928b665e807b552564014c3bfecf492a";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private final static String xs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        "2952afd9aef9527f9b40d23c8916f7d046028f9d";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private final static String ys =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        "b16ddb0f9394c328c983ecf23b20014ace368a1af5728dffbf1162de9ed8ebf6" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        "384f323930e091503035caa797e3674221fc16136240b5474799ede2b7b11313" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        "7574a9c26bcf900940027b4bcd511ef1d1daf2e69c416aebaf3bdf39f02473b9" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        "d963f99414c09d97bb0830d9fbdcf7bb9dad8a2179fcdf296838c4cfab8f4d8f";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private final static BigInteger p = new BigInteger(ps, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private final static BigInteger q = new BigInteger(qs, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private final static BigInteger g = new BigInteger(gs, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private final static BigInteger x = new BigInteger(xs, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private final static BigInteger y = new BigInteger(ys, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    // data for test 1, original and SHA-1 hashed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private final static byte[] data1Raw = b("0102030405060708090a0b0c0d0e0f10111213");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private final static byte[] data1SHA = b("00:e2:5f:c9:1c:8f:d6:8c:6a:dc:c6:bd:f0:46:60:5e:a2:cd:8d:ad");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    // valid signatures of data1. sig1b uses incorrect ASN.1 encoding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    // which we want to accept anyway for compatibility
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private final static byte[] sig1a = b("30:2d:02:14:53:06:3f:7d:ec:48:3c:99:17:9a:2c:a9:4d:e8:00:da:70:fb:35:d7:02:15:00:92:6a:39:6b:15:63:2f:e7:32:90:35:bf:af:47:55:e7:ff:33:a5:13");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private final static byte[] sig1b = b("30:2c:02:14:53:06:3f:7d:ec:48:3c:99:17:9a:2c:a9:4d:e8:00:da:70:fb:35:d7:02:14:92:6a:39:6b:15:63:2f:e7:32:90:35:bf:af:47:55:e7:ff:33:a5:13");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    // data for test 2 (invalid signatures)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private final static byte[] data2Raw = {};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private final static byte[] data2SHA = b("da:39:a3:ee:5e:6b:4b:0d:32:55:bf:ef:95:60:18:90:af:d8:07:09");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private static void verify(Provider provider, String alg, PublicKey key, byte[] data, byte[] sig, boolean result) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        Signature s = Signature.getInstance(alg, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        s.initVerify(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        boolean r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        s.update(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        r = s.verify(sig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        if (r != result) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            throw new Exception("Result mismatch, actual: " + r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        s.update(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        r = s.verify(sig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if (r != result) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            throw new Exception("Result mismatch, actual: " + r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        System.out.println("Passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public static void main(String[] args) throws Exception {
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32138
diff changeset
   115
        main(new TestDSA(), args);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32138
diff changeset
   118
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public void main(Provider provider) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        long start = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        System.out.println("Testing provider " + provider + "...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
32138
23830562d3d1 8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents: 30046
diff changeset
   124
        /*
23830562d3d1 8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents: 30046
diff changeset
   125
         * Use Solaris SPARC 11.2 or later to avoid an intermittent failure
23830562d3d1 8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents: 30046
diff changeset
   126
         * when running SunPKCS11-Solaris (8044554)
23830562d3d1 8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents: 30046
diff changeset
   127
         */
23830562d3d1 8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents: 30046
diff changeset
   128
        if (provider.getName().equals("SunPKCS11-Solaris") &&
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32138
diff changeset
   129
            props.getProperty("os.name").equals("SunOS") &&
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32138
diff changeset
   130
            props.getProperty("os.arch").equals("sparcv9") &&
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32138
diff changeset
   131
            props.getProperty("os.version").compareTo("5.11") <= 0 &&
32138
23830562d3d1 8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents: 30046
diff changeset
   132
            getDistro().compareTo("11.2") < 0) {
23830562d3d1 8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents: 30046
diff changeset
   133
23830562d3d1 8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents: 30046
diff changeset
   134
            System.out.println("SunPKCS11-Solaris provider requires " +
23830562d3d1 8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents: 30046
diff changeset
   135
                "Solaris SPARC 11.2 or later, skipping");
23830562d3d1 8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents: 30046
diff changeset
   136
            return;
23830562d3d1 8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents: 30046
diff changeset
   137
        }
23830562d3d1 8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents: 30046
diff changeset
   138
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (provider.getService("Signature", "SHA1withDSA") == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            System.out.println("DSA not supported, skipping");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        KeyFactory kf = KeyFactory.getInstance("DSA", provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        DSAPrivateKeySpec privSpec = new DSAPrivateKeySpec(x, p, q, g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        DSAPublicKeySpec pubSpec = new DSAPublicKeySpec(y, p, q, g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        PrivateKey privateKey = kf.generatePrivate(privSpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        PublicKey publicKey = kf.generatePublic(pubSpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        // verify known-good and known-bad signatures using SHA1withDSA and RawDSA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        verify(provider, "SHA1withDSA", publicKey, data1Raw, sig1a, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        verify(provider, "SHA1withDSA", publicKey, data1Raw, sig1b, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        verify(provider, "SHA1withDSA", publicKey, data2Raw, sig1a, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        verify(provider, "SHA1withDSA", publicKey, data2Raw, sig1b, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        verify(provider, "RawDSA", publicKey, data1SHA, sig1a, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        verify(provider, "RawDSA", publicKey, data1SHA, sig1b, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        verify(provider, "RawDSA", publicKey, data2SHA, sig1a, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        verify(provider, "RawDSA", publicKey, data2SHA, sig1b, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        testSigning(provider, privateKey, publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        long stop = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        System.out.println("All tests passed (" + (stop - start) + " ms).");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    private void testSigning(Provider provider, PrivateKey privateKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            PublicKey publicKey) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        byte[] data = new byte[2048];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        new Random().nextBytes(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        // sign random data using SHA1withDSA and verify using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        // SHA1withDSA and RawDSA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        Signature s = Signature.getInstance("SHA1withDSA", provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        s.initSign(privateKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        s.update(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        byte[] s1 = s.sign();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        s.initVerify(publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        s.update(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        if (!s.verify(s1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            throw new Exception("Sign/verify 1 failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        s = Signature.getInstance("RawDSA", provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        MessageDigest md = MessageDigest.getInstance("SHA-1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        byte[] digest = md.digest(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        s.initVerify(publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        s.update(digest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        if (!s.verify(s1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            throw new Exception("Sign/verify 2 failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        // sign random data using RawDSA and verify using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        // SHA1withDSA and RawDSA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        s.initSign(privateKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        s.update(digest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        byte[] s2 = s.sign();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        s.initVerify(publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        s.update(digest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        if (!s.verify(s2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            throw new Exception("Sign/verify 3 failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        s = Signature.getInstance("SHA1withDSA", provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        s.initVerify(publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        s.update(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        if (!s.verify(s2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            throw new Exception("Sign/verify 4 failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        // test behavior if data of incorrect length is passed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        s = Signature.getInstance("RawDSA", provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        s.initSign(privateKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        s.update(new byte[8]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        s.update(new byte[64]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            s.sign();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            throw new Exception("No error RawDSA signing long data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        } catch (SignatureException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            // expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    private final static char[] hexDigits = "0123456789abcdef".toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    public static String toString(byte[] b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        StringBuffer sb = new StringBuffer(b.length * 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        for (int i = 0; i < b.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            int k = b[i] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            if (i != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                sb.append(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            sb.append(hexDigits[k >>> 4]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            sb.append(hexDigits[k & 0xf]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    public static byte[] parse(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            int n = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            ByteArrayOutputStream out = new ByteArrayOutputStream(n / 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            StringReader r = new StringReader(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                int b1 = nextNibble(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                if (b1 < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                int b2 = nextNibble(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                if (b2 < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    throw new RuntimeException("Invalid string " + s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                int b = (b1 << 4) | b2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                out.write(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            return out.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    public static byte[] b(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        return parse(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    private static int nextNibble(StringReader r) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            int ch = r.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            if (ch == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            } else if ((ch >= '0') && (ch <= '9')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                return ch - '0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            } else if ((ch >= 'a') && (ch <= 'f')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                return ch - 'a' + 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            } else if ((ch >= 'A') && (ch <= 'F')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                return ch - 'A' + 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
}