Path: blob/master/test/jdk/sun/security/TestSignatureOidHelper.java
41145 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.security.InvalidKeyException;24import java.security.KeyPair;25import java.security.KeyPairGenerator;26import java.security.NoSuchAlgorithmException;27import java.security.NoSuchProviderException;28import java.security.Signature;29import java.security.SignatureException;30import java.util.List;3132/*33* Utilities for testing the signature algorithm OIDs.34*/35public class TestSignatureOidHelper {3637private static final byte[] INPUT = "1234567890".getBytes();3839private final String algorithm;4041private final String provider;4243private final int keySize;4445private final List<OidAlgorithmPair> data;4647public TestSignatureOidHelper(String algorithm, String provider,48int keySize, List<OidAlgorithmPair> data) {49this.algorithm = algorithm;50this.provider = provider;51this.keySize = keySize;52this.data = data;53}5455public void execute() throws Exception {56KeyPair keyPair = createKeyPair();57for (OidAlgorithmPair oidAlgorithmPair : data) {58runTest(oidAlgorithmPair, keyPair);59System.out.println("passed");60}61System.out.println("All tests passed");62}6364private KeyPair createKeyPair()65throws NoSuchAlgorithmException, NoSuchProviderException {66KeyPairGenerator keyGen = KeyPairGenerator.getInstance(algorithm,67provider);68keyGen.initialize(keySize);69return keyGen.generateKeyPair();70}7172private void runTest(OidAlgorithmPair oidAlgorithmPair, KeyPair keyPair)73throws NoSuchAlgorithmException, NoSuchProviderException,74InvalidKeyException, SignatureException {75Signature sgAlgorithm =76Signature.getInstance(oidAlgorithmPair.algorithm, provider);77Signature sgOid = Signature.getInstance(oidAlgorithmPair.oid, provider);7879if (sgAlgorithm == null) {80throw new RuntimeException(String.format(81"Test failed: algorithm string %s getInstance failed.%n",82oidAlgorithmPair.algorithm));83}8485if (sgOid == null) {86throw new RuntimeException(87String.format("Test failed: OID %s getInstance failed.%n",88oidAlgorithmPair.oid));89}9091if (!sgAlgorithm.getAlgorithm().equals(oidAlgorithmPair.algorithm)) {92throw new RuntimeException(String.format(93"Test failed: algorithm string %s getInstance "94+ "doesn't generate expected algorithm.%n",95oidAlgorithmPair.algorithm));96}9798sgAlgorithm.initSign(keyPair.getPrivate());99sgAlgorithm.update(INPUT);100sgOid.initVerify(keyPair.getPublic());101sgOid.update(INPUT);102if (!sgOid.verify(sgAlgorithm.sign())) {103throw new RuntimeException(104"Signature verification failed unexpectedly");105}106}107}108109class OidAlgorithmPair {110111public final String oid;112public final String algorithm;113114public OidAlgorithmPair(String oid, String algorithm) {115this.oid = oid;116this.algorithm = algorithm;117}118119@Override120public String toString() {121return "[oid=" + oid + ", algorithm=" + algorithm + "]";122}123}124125126