1 /*
2  * Copyright (c) 2017, 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 /*
25  * @test
26  * @bug 8183591
27  * @summary Test decoding of DER length fields containing Integer.MAX_VALUE
28  * @run main TestMaxLengthDER
29  */
30 
31 import java.io.*;
32 import java.math.*;
33 import java.security.*;
34 import java.security.spec.*;
35 
36 public class TestMaxLengthDER {
37 
main(String[] args)38     public static void main(String[] args) throws Exception {
39 
40         String message = "Message";
41         Signature sig = Signature.getInstance("SHA256withDSA");
42         KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA");
43         SecureRandom rnd = new SecureRandom();
44         rnd.setSeed(1);
45         kpg.initialize(2048, rnd);
46         KeyPair kp = kpg.generateKeyPair();
47         sig.initSign(kp.getPrivate());
48         sig.update(message.getBytes());
49         byte[] sigData = sig.sign();
50 
51         // Set the length of the second integer to Integer.MAX_VALUE
52         // First copy all the signature data to the correct location
53         int lengthPos = sigData[3] + 5;
54         byte[] modifiedSigData = new byte[sigData.length + 4];
55         System.arraycopy(sigData, 0, modifiedSigData, 0, lengthPos);
56         System.arraycopy(sigData, lengthPos + 1, modifiedSigData,
57             lengthPos + 5, sigData.length - (lengthPos + 1));
58 
59         // Increase the length (in bytes) of the sequence to account for
60         // the larger length field
61         modifiedSigData[1] += 4;
62 
63         // Modify the length field
64         modifiedSigData[lengthPos] = (byte) 0x84;
65         modifiedSigData[lengthPos + 1] = (byte) 0x7F;
66         modifiedSigData[lengthPos + 2] = (byte) 0xFF;
67         modifiedSigData[lengthPos + 3] = (byte) 0xFF;
68         modifiedSigData[lengthPos + 4] = (byte) 0xFF;
69 
70         sig.initVerify(kp.getPublic());
71         sig.update(message.getBytes());
72 
73         try {
74             sig.verify(modifiedSigData);
75             throw new RuntimeException("No exception on misencoded signature");
76         } catch (SignatureException ex) {
77             if (ex.getCause() instanceof EOFException) {
78                 // this is expected
79             } else {
80                 throw ex;
81             }
82         }
83     }
84 }
85