Path: blob/master/test/jdk/java/security/Signature/ResetAfterException.java
41149 views
/*1* Copyright (c) 2016, 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 814980226* @summary Ensure that Signature objects are reset after verification errored out.27*/28import java.util.Arrays;29import java.security.*;3031public class ResetAfterException {3233public static void main(String[] args) throws Exception {3435byte[] data = "data to be signed".getBytes();36byte[] shortBuffer = new byte[2];3738Provider[] provs = Security.getProviders();39boolean failed = false;4041for (Provider p : provs) {42Signature sig;43try {44sig = Signature.getInstance("SHA256withRSA", p);45} catch (NoSuchAlgorithmException nsae) {46// no support, skip47continue;48}4950boolean res = true;51System.out.println("Testing Provider: " + p.getName());52KeyPairGenerator keyGen = null;53try {54// It's possible that some provider, e.g. SunMSCAPI,55// doesn't work well with keys from other providers56// so we use the same provider to generate key first57keyGen = KeyPairGenerator.getInstance("RSA", p);58} catch (NoSuchAlgorithmException nsae) {59keyGen = KeyPairGenerator.getInstance("RSA");60}61if (keyGen == null) {62throw new RuntimeException("Error: No support for RSA KeyPairGenerator");63}64keyGen.initialize(1024);65KeyPair keyPair = keyGen.generateKeyPair();6667sig.initSign(keyPair.getPrivate());68sig.update(data);69byte[] signature = sig.sign();70// First check signing71try {72sig.update(data);73// sign with short output buffer to cause exception74int len = sig.sign(shortBuffer, 0, shortBuffer.length);75System.out.println("FAIL: Should throw SE with short buffer");76res = false;77} catch (SignatureException e) {78// expected exception; ignore79System.out.println("Expected Ex for short output buffer: " + e);80}81// Signature object should reset after a failed generation82sig.update(data);83byte[] signature2 = sig.sign();84if (!Arrays.equals(signature, signature2)) {85System.out.println("FAIL: Generated different signature");86res = false;87} else {88System.out.println("Generated same signature");89}9091// Now, check signature verification92sig.initVerify(keyPair.getPublic());93sig.update(data);94try {95// first verify with valid signature bytes96res = sig.verify(signature);97} catch (SignatureException e) {98System.out.println("FAIL: Valid signature rejected");99e.printStackTrace();100res = false;101}102103try {104sig.update(data);105// verify with short signaure to cause exception106if (sig.verify(shortBuffer)) {107System.out.println("FAIL: Invalid signature verified");108res = false;109} else {110System.out.println("Invalid signature rejected");111}112} catch (SignatureException e) {113// expected exception; ignore114System.out.println("Expected Ex for short output buffer: " + e);115}116// Signature object should reset after an a failed verification117sig.update(data);118try {119// verify with valid signature bytes again120res = sig.verify(signature);121if (!res) {122System.out.println("FAIL: Valid signature is rejected");123} else {124System.out.println("Valid signature is accepted");125}126} catch (GeneralSecurityException e) {127System.out.println("FAIL: Valid signature is rejected");128e.printStackTrace();129res = false;130}131failed |= !res;132}133if (failed) {134throw new RuntimeException("One or more test failed");135} else {136System.out.println("Test Passed");137}138}139}140141142