|
1 /* |
|
2 * Copyright (c) 2016, 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 |
|
24 import java.security.*; |
|
25 |
|
26 /* |
|
27 * @test |
|
28 * @bug 8161571 |
|
29 * @summary Reject signatures presented for verification that contain extra |
|
30 * bytes. |
|
31 * @run main SignatureLength |
|
32 */ |
|
33 public class SignatureLength { |
|
34 |
|
35 public static void main(String[] args) throws Exception { |
|
36 main0("EC", 256, "SHA256withECDSA", "SunEC"); |
|
37 main0("RSA", 2048, "SHA256withRSA", "SunRsaSign"); |
|
38 main0("DSA", 2048, "SHA256withDSA", "SUN"); |
|
39 |
|
40 if (System.getProperty("os.name").equals("SunOS")) { |
|
41 main0("EC", 256, "SHA256withECDSA", null); |
|
42 main0("RSA", 2048, "SHA256withRSA", null); |
|
43 } |
|
44 } |
|
45 |
|
46 private static void main0(String keyAlgorithm, int keysize, |
|
47 String signatureAlgorithm, String provider) throws Exception { |
|
48 byte[] plaintext = "aaa".getBytes("UTF-8"); |
|
49 |
|
50 // Generate |
|
51 KeyPairGenerator generator = |
|
52 provider == null ? |
|
53 (KeyPairGenerator) KeyPairGenerator.getInstance(keyAlgorithm) : |
|
54 (KeyPairGenerator) KeyPairGenerator.getInstance( |
|
55 keyAlgorithm, provider); |
|
56 generator.initialize(keysize); |
|
57 System.out.println("Generating " + keyAlgorithm + " keypair using " + |
|
58 generator.getProvider().getName() + " JCE provider"); |
|
59 KeyPair keypair = generator.generateKeyPair(); |
|
60 |
|
61 // Sign |
|
62 Signature signer = |
|
63 provider == null ? |
|
64 Signature.getInstance(signatureAlgorithm) : |
|
65 Signature.getInstance(signatureAlgorithm, provider); |
|
66 signer.initSign(keypair.getPrivate()); |
|
67 signer.update(plaintext); |
|
68 System.out.println("Signing using " + signer.getProvider().getName() + |
|
69 " JCE provider"); |
|
70 byte[] signature = signer.sign(); |
|
71 |
|
72 // Invalidate |
|
73 System.out.println("Invalidating signature ..."); |
|
74 byte[] badSignature = new byte[signature.length + 5]; |
|
75 System.arraycopy(signature, 0, badSignature, 0, signature.length); |
|
76 badSignature[signature.length] = 0x01; |
|
77 badSignature[signature.length + 1] = 0x01; |
|
78 badSignature[signature.length + 2] = 0x01; |
|
79 badSignature[signature.length + 3] = 0x01; |
|
80 badSignature[signature.length + 4] = 0x01; |
|
81 |
|
82 // Verify |
|
83 Signature verifier = |
|
84 provider == null ? |
|
85 Signature.getInstance(signatureAlgorithm) : |
|
86 Signature.getInstance(signatureAlgorithm, provider); |
|
87 verifier.initVerify(keypair.getPublic()); |
|
88 verifier.update(plaintext); |
|
89 System.out.println("Verifying using " + |
|
90 verifier.getProvider().getName() + " JCE provider"); |
|
91 |
|
92 try { |
|
93 System.out.println("Valid? " + verifier.verify(badSignature)); |
|
94 throw new Exception( |
|
95 "ERROR: expected a SignatureException but none was thrown"); |
|
96 } catch (SignatureException e) { |
|
97 System.out.println("OK: caught expected exception: " + e); |
|
98 } |
|
99 System.out.println(); |
|
100 } |
|
101 } |