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