jdk/test/sun/security/TestSignatureOidHelper.java
author stefank
Mon, 04 Apr 2016 09:15:01 +0200
changeset 37241 b9961c99c356
parent 32773 6fb58bd2c745
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:
32773
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
     1
/*
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
     4
 *
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
     7
 * published by the Free Software Foundation.
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
     8
 *
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    13
 * accompanied this code).
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    14
 *
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    18
 *
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    21
 * questions.
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    22
 */
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    23
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    24
import java.security.InvalidKeyException;
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    25
import java.security.KeyPair;
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    26
import java.security.KeyPairGenerator;
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    27
import java.security.NoSuchAlgorithmException;
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    28
import java.security.NoSuchProviderException;
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    29
import java.security.Signature;
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    30
import java.security.SignatureException;
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    31
import java.util.List;
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    32
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    33
/*
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    34
 * Utilities for testing the signature algorithm OIDs.
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    35
 */
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    36
public class TestSignatureOidHelper {
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    37
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    38
    private static final byte[] INPUT = "1234567890".getBytes();
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    39
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    40
    private final String algorithm;
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    41
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    42
    private final String provider;
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    43
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    44
    private final int keySize;
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    45
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    46
    private final List<OidAlgorithmPair> data;
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    47
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    48
    public TestSignatureOidHelper(String algorithm, String provider,
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    49
            int keySize, List<OidAlgorithmPair> data) {
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    50
        this.algorithm = algorithm;
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    51
        this.provider = provider;
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    52
        this.keySize = keySize;
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    53
        this.data = data;
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    54
    }
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    55
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    56
    public void execute() throws Exception {
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    57
        KeyPair keyPair = createKeyPair();
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    58
        for (OidAlgorithmPair oidAlgorithmPair : data) {
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    59
            runTest(oidAlgorithmPair, keyPair);
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    60
            System.out.println("passed");
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    61
        }
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    62
        System.out.println("All tests passed");
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    63
    }
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    64
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    65
    private KeyPair createKeyPair()
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    66
            throws NoSuchAlgorithmException, NoSuchProviderException {
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    67
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance(algorithm,
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    68
                provider);
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    69
        keyGen.initialize(keySize);
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    70
        return keyGen.generateKeyPair();
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    71
    }
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    72
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    73
    private void runTest(OidAlgorithmPair oidAlgorithmPair, KeyPair keyPair)
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    74
            throws NoSuchAlgorithmException, NoSuchProviderException,
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    75
            InvalidKeyException, SignatureException {
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    76
        Signature sgAlgorithm =
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    77
                Signature.getInstance(oidAlgorithmPair.algorithm, provider);
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    78
        Signature sgOid = Signature.getInstance(oidAlgorithmPair.oid, provider);
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    79
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    80
        if (sgAlgorithm == null) {
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    81
            throw new RuntimeException(String.format(
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    82
                    "Test failed: algorithm string %s getInstance failed.%n",
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    83
                    oidAlgorithmPair.algorithm));
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    84
        }
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    85
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    86
        if (sgOid == null) {
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    87
            throw new RuntimeException(
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    88
                    String.format("Test failed: OID %s getInstance failed.%n",
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    89
                            oidAlgorithmPair.oid));
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    90
        }
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    91
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    92
        if (!sgAlgorithm.getAlgorithm().equals(oidAlgorithmPair.algorithm)) {
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    93
            throw new RuntimeException(String.format(
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    94
                    "Test failed: algorithm string %s getInstance "
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    95
                            + "doesn't generate expected algorithm.%n",
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    96
                    oidAlgorithmPair.algorithm));
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    97
        }
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    98
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
    99
        sgAlgorithm.initSign(keyPair.getPrivate());
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
   100
        sgAlgorithm.update(INPUT);
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
   101
        sgOid.initVerify(keyPair.getPublic());
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
   102
        sgOid.update(INPUT);
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
   103
        if (!sgOid.verify(sgAlgorithm.sign())) {
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
   104
            throw new RuntimeException(
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
   105
                    "Signature verification failed unexpectedly");
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
   106
        }
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
   107
    }
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
   108
}
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
   109
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
   110
class OidAlgorithmPair {
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
   111
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
   112
    public final String oid;
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
   113
    public final String algorithm;
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
   114
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
   115
    public OidAlgorithmPair(String oid, String algorithm) {
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
   116
        this.oid = oid;
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
   117
        this.algorithm = algorithm;
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
   118
    }
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
   119
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
   120
    @Override
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
   121
    public String toString() {
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
   122
        return "[oid=" + oid + ", algorithm=" + algorithm + "]";
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
   123
    }
6fb58bd2c745 8075286: Additional tests for signature algorithm OIDs and transformation string
valeriep
parents:
diff changeset
   124
}