Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/security/Signature/Offsets.java
38812 views
/*1* Copyright (c) 2015, 2020, 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.*;24import java.security.spec.*;25import jdk.testlibrary.RandomFactory;2627/*28* @test29* @bug 8050374 8181048 814629330* @key randomness31* @summary This test validates signature verification32* Signature.verify(byte[], int, int). The test uses RandomFactory to33* get random set of clear text data to sign. After the signature34* generation, the test tries to verify signature with the above API35* and passing in different signature offset (0, 33, 66, 99).36* @library /lib/testlibrary37* @run main Offsets SUN NONEwithDSA38* @run main Offsets SUN SHA1withDSA39* @run main Offsets SUN SHA224withDSA40* @run main Offsets SUN SHA256withDSA41* @run main Offsets SunRsaSign SHA224withRSA42* @run main Offsets SunRsaSign SHA256withRSA43* @run main Offsets SunRsaSign SHA384withRSA44* @run main Offsets SunRsaSign SHA512withRSA45* @run main Offsets SunRsaSign SHA512/224withRSA46* @run main Offsets SunRsaSign SHA512/256withRSA47*/48public class Offsets {4950private final int size;51private final byte[] cleartext;52private final PublicKey pubkey;53private final Signature signature;54private final byte[] signed;5556private Offsets(Signature signature, PublicKey pubkey, PrivateKey privkey,57int size, byte[] cleartext) throws InvalidKeyException,58SignatureException {59System.out.println("Testing signature " + signature.getAlgorithm());60this.pubkey = pubkey;61this.signature = signature;62this.size = size;63this.cleartext = cleartext;6465String sigAlg = signature.getAlgorithm();66signature.initSign(privkey);67signature.update(cleartext, 0, size);68signed = signature.sign();69}7071int getDataSize() {72return size;73}7475int getSignatureLength() {76return signed.length;77}7879byte[] shiftSignData(int offset) {80byte[] testSignData = new byte[offset + signed.length];81System.arraycopy(signed, 0, testSignData, offset,82signed.length);83return testSignData;84}8586boolean verifySignature(byte[] sigData, int sigOffset, int sigLength,87int updateOffset, int updateLength)88throws InvalidKeyException, SignatureException {89signature.initVerify(pubkey);90signature.update(cleartext, updateOffset, updateLength);91return signature.verify(sigData, sigOffset, sigLength);92}9394static Offsets init(String provider, String algorithm)95throws NoSuchAlgorithmException, NoSuchProviderException,96InvalidKeyException, SignatureException {97// fill the cleartext data with random bytes98byte[] cleartext = new byte[100];99RandomFactory.getRandom().nextBytes(cleartext);100101// NONEwith requires input to be of 20 bytes102int size = algorithm.contains("NONEwith") ? 20 : 100;103104// create signature instance105Signature signature = Signature.getInstance(algorithm, provider);106107String keyAlgo;108int keySize = 2048;109if (algorithm.contains("RSA")) {110keyAlgo = "RSA";111} else if (algorithm.contains("ECDSA")) {112keyAlgo = "EC";113keySize = 256;114} else if (algorithm.contains("DSA")) {115keyAlgo = "DSA";116if (algorithm.startsWith("SHAwith") ||117algorithm.startsWith("SHA1with")) {118keySize = 1024;119}120} else {121throw new RuntimeException("Test doesn't support this signature "122+ "algorithm: " + algorithm);123}124125KeyPairGenerator kpg = KeyPairGenerator.getInstance(keyAlgo, provider);126kpg.initialize(keySize);127KeyPair kp = kpg.generateKeyPair();128PublicKey pubkey = kp.getPublic();129PrivateKey privkey = kp.getPrivate();130131return new Offsets(signature, pubkey, privkey, size, cleartext);132}133134public static void main(String[] args) throws NoSuchAlgorithmException,135InvalidKeyException, SignatureException {136if (args.length < 2) {137throw new RuntimeException("Wrong parameters");138}139140boolean result = true;141try {142Offsets test = init(args[0], args[1]);143144// We are trying 3 different offsets, data size has nothing to do145// with signature length146for (int chunk = 3; chunk > 0; chunk--) {147int signOffset = test.getDataSize() / chunk;148149System.out.println("Running test with offset " + signOffset);150byte[] signData = test.shiftSignData(signOffset);151152boolean success = test.verifySignature(signData, signOffset,153test.getSignatureLength(), 0, test.getDataSize());154155if (success) {156System.out.println("Successfully verified with offset "157+ signOffset);158} else {159System.out.println("Verification failed with offset "160+ signOffset);161result = false;162}163}164165// save signature to offset 0166byte[] signData = test.shiftSignData(0);167168// Negative tests169170// Test signature offset 0.171// Wrong test data will be passed to update,172// so signature verification should fail.173for (int chunk = 3; chunk > 0; chunk--) {174int dataOffset = (test.getDataSize() - 1) / chunk;175boolean success;176try {177success = test.verifySignature(signData, 0,178test.getSignatureLength(), dataOffset,179(test.getDataSize() - dataOffset));180} catch (SignatureException e) {181// Since we are trying different data size, it can throw182// SignatureException183success = false;184}185186if (!success) {187System.out.println("Signature verification failed "188+ "as expected, with data offset " + dataOffset189+ " and length "190+ (test.getDataSize() - dataOffset));191} else {192System.out.println("Signature verification "193+ "should not succeed, with data offset "194+ dataOffset + " and length "195+ (test.getDataSize() - dataOffset));196result = false;197}198}199200// Tests with manipulating offset and length201result &= Offsets.checkFailure(test, signData, -1,202test.getSignatureLength());203204result &= Offsets.checkFailure(test, signData, 0,205test.getSignatureLength() - 1);206207result &= Offsets.checkFailure(test, signData,208test.getSignatureLength() + 1, test.getSignatureLength());209210result &= Offsets.checkFailure(test, signData, 0,211test.getSignatureLength() + 1);212213result &= Offsets.checkFailure(test, signData, 0, 0);214215result &= Offsets.checkFailure(test, signData, 0, -1);216217result &= Offsets.checkFailure(test, signData,2182147483646, test.getSignatureLength());219220result &= Offsets.checkFailure(test, null, 0,221test.getSignatureLength());222} catch (NoSuchProviderException nspe) {223System.out.println("No such provider: " + nspe);224}225226if (!result) {227throw new RuntimeException("Some test cases failed");228}229}230231static boolean checkFailure(Offsets test, byte[] signData, int offset,232int length) {233boolean success;234try {235success = test.verifySignature(signData, offset, length, 0,236test.getDataSize());237} catch (IllegalArgumentException | SignatureException e) {238System.out.println("Expected exception: " + e);239success = false;240} catch (InvalidKeyException e) {241System.out.println("Unexpected exception: " + e);242return false;243}244245if (!success) {246System.out.println("Signature verification failed as expected, "247+ "with signature offset " + offset + " and length "248+ length);249return true;250} else {251System.out.println("Signature verification should not succeed, "252+ "with signature offset " + offset + " and length "253+ length);254return false;255}256}257258}259260261