Path: blob/master/test/jdk/java/security/Signature/SignatureLength.java
41149 views
/*1* Copyright (c) 2016, 2018, 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*/2223/*24* @test25* @bug 8161571 817837026* @summary Reject signatures presented for verification that contain extra27* bytes.28* @modules jdk.crypto.ec29* @run main SignatureLength30*/3132import java.security.KeyPair;33import java.security.KeyPairGenerator;34import java.security.NoSuchAlgorithmException;35import java.security.Provider;36import java.security.Security;37import java.security.Signature;38import java.security.SignatureException;3940public class SignatureLength {4142public static void main(String[] args) throws Exception {43for (Provider p0 : Security.getProviders()) {44for (Provider p1 : Security.getProviders()) {45for (Provider p2 : Security.getProviders()) {46// SunMSCAPI signer can only be initialized with47// a key generated with SunMSCAPI48if (!p0.getName().equals("SunMSCAPI")49&& p1.getName().equals("SunMSCAPI")) continue;5051// SunMSCAPI generated key can only be signed52// with SunMSCAPI signer53if (p0.getName().equals("SunMSCAPI")54&& !p1.getName().equals("SunMSCAPI")) continue;5556// SunMSCAPI and SunPKCS11 verifiers may return false57// instead of throwing SignatureException58boolean mayNotThrow = p2.getName().equals("SunMSCAPI")59|| p2.getName().startsWith("SunPKCS11");6061main0("EC", 256, "SHA256withECDSA", p0, p1, p2, mayNotThrow);62main0("RSA", 2048, "SHA256withRSA", p0, p1, p2, mayNotThrow);63main0("DSA", 2048, "SHA256withDSA", p0, p1, p2, mayNotThrow);64}65}66}67}6869private static void main0(String keyAlgorithm, int keysize,70String signatureAlgorithm, Provider generatorProvider,71Provider signerProvider, Provider verifierProvider,72boolean mayNotThrow) throws Exception {7374KeyPairGenerator generator;75Signature signer;76Signature verifier;7778try {79generator = KeyPairGenerator.getInstance(keyAlgorithm,80generatorProvider);81signer = Signature.getInstance(signatureAlgorithm,82signerProvider);83verifier = Signature.getInstance(signatureAlgorithm,84verifierProvider);85} catch (NoSuchAlgorithmException nsae) {86// ignore this set of providers87return;88}8990byte[] plaintext = "aaa".getBytes("UTF-8");9192// Generate93generator.initialize(keysize);94System.out.println("Generating " + keyAlgorithm + " keypair using " +95generator.getProvider().getName() + " JCE provider");96KeyPair keypair = generator.generateKeyPair();9798// Sign99signer.initSign(keypair.getPrivate());100signer.update(plaintext);101System.out.println("Signing using " + signer.getProvider().getName() +102" JCE provider");103byte[] signature = signer.sign();104105// Invalidate106System.out.println("Invalidating signature ...");107byte[] badSignature = new byte[signature.length + 5];108System.arraycopy(signature, 0, badSignature, 0, signature.length);109badSignature[signature.length] = 0x01;110badSignature[signature.length + 1] = 0x01;111badSignature[signature.length + 2] = 0x01;112badSignature[signature.length + 3] = 0x01;113badSignature[signature.length + 4] = 0x01;114115// Verify116verifier.initVerify(keypair.getPublic());117verifier.update(plaintext);118System.out.println("Verifying using " +119verifier.getProvider().getName() + " JCE provider");120121try {122boolean valid = verifier.verify(badSignature);123System.out.println("Valid? " + valid);124if (mayNotThrow) {125if (valid) {126throw new Exception(127"ERROR: expected a SignatureException but none was thrown"128+ " and invalid signature was verified");129} else {130System.out.println("OK: verification failed as expected");131}132} else {133throw new Exception(134"ERROR: expected a SignatureException but none was thrown");135}136} catch (SignatureException e) {137System.out.println("OK: caught expected exception: " + e);138}139System.out.println();140}141}142143144