File: Offsets.java

package info (click to toggle)
openjdk-11 11.0.4%2B11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 757,028 kB
  • sloc: java: 5,016,041; xml: 1,191,974; cpp: 934,731; ansic: 555,697; sh: 24,299; objc: 12,703; python: 3,602; asm: 3,415; makefile: 2,772; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (260 lines) | stat: -rw-r--r-- 9,924 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

import java.security.*;
import java.security.spec.*;
import jdk.test.lib.RandomFactory;

/*
 * @test
 * @bug 8050374 8181048 8146293
 * @key randomness
 * @summary This test validates signature verification
 *          Signature.verify(byte[], int, int). The test uses RandomFactory to
 *          get random set of clear text data to sign. After the signature
 *          generation, the test tries to verify signature with the above API
 *          and passing in different signature offset (0, 33, 66, 99).
 * @library /test/lib
 * @build jdk.test.lib.RandomFactory
 * @run main Offsets SUN NONEwithDSA
 * @run main Offsets SUN SHA1withDSA
 * @run main Offsets SUN SHA224withDSA
 * @run main Offsets SUN SHA256withDSA
 * @run main Offsets SunRsaSign SHA224withRSA
 * @run main Offsets SunRsaSign SHA256withRSA
 * @run main Offsets SunRsaSign SHA384withRSA
 * @run main Offsets SunRsaSign SHA512withRSA
 * @run main Offsets SunRsaSign SHA512/224withRSA
 * @run main Offsets SunRsaSign SHA512/256withRSA
 */
public class Offsets {

    private final int size;
    private final byte[] cleartext;
    private final PublicKey pubkey;
    private final Signature signature;
    private final byte[] signed;

    private Offsets(Signature signature, PublicKey pubkey, PrivateKey privkey,
            int size, byte[] cleartext) throws InvalidKeyException,
                SignatureException {
        System.out.println("Testing signature " + signature.getAlgorithm());
        this.pubkey = pubkey;
        this.signature = signature;
        this.size = size;
        this.cleartext = cleartext;

        String sigAlg = signature.getAlgorithm();
        signature.initSign(privkey);
        signature.update(cleartext, 0, size);
        signed = signature.sign();
    }

    int getDataSize() {
        return size;
    }

    int getSignatureLength() {
        return signed.length;
    }

    byte[] shiftSignData(int offset) {
        byte[] testSignData = new byte[offset + signed.length];
        System.arraycopy(signed, 0, testSignData, offset,
                signed.length);
        return testSignData;
    }

    boolean verifySignature(byte[] sigData, int sigOffset, int sigLength,
            int updateOffset, int updateLength)
            throws InvalidKeyException, SignatureException {
        signature.initVerify(pubkey);
        signature.update(cleartext, updateOffset, updateLength);
        return signature.verify(sigData, sigOffset, sigLength);
    }

    static Offsets init(String provider, String algorithm)
            throws NoSuchAlgorithmException, NoSuchProviderException,
            InvalidKeyException, SignatureException {
        // fill the cleartext data with random bytes
        byte[] cleartext = new byte[100];
        RandomFactory.getRandom().nextBytes(cleartext);

        // NONEwith requires input to be of 20 bytes
        int size = algorithm.contains("NONEwith") ? 20 : 100;

        // create signature instance
        Signature signature = Signature.getInstance(algorithm, provider);

        String keyAlgo;
        int keySize = 2048;
        if (algorithm.contains("RSA")) {
            keyAlgo = "RSA";
        } else if (algorithm.contains("ECDSA")) {
            keyAlgo = "EC";
            keySize = 256;
        } else if (algorithm.contains("DSA")) {
            keyAlgo = "DSA";
            if (algorithm.startsWith("SHAwith") ||
                    algorithm.startsWith("SHA1with")) {
                keySize = 1024;
            }
        } else {
            throw new RuntimeException("Test doesn't support this signature "
                    + "algorithm: " + algorithm);
        }

        KeyPairGenerator kpg = KeyPairGenerator.getInstance(keyAlgo, provider);
        kpg.initialize(keySize);
        KeyPair kp = kpg.generateKeyPair();
        PublicKey pubkey = kp.getPublic();
        PrivateKey privkey = kp.getPrivate();

        return new Offsets(signature, pubkey, privkey, size, cleartext);
    }

    public static void main(String[] args) throws NoSuchAlgorithmException,
            InvalidKeyException, SignatureException {
        if (args.length < 2) {
            throw new RuntimeException("Wrong parameters");
        }

        boolean result = true;
        try {
            Offsets test = init(args[0], args[1]);

            // We are trying 3 different offsets, data size has nothing to do
            // with signature length
            for (int chunk = 3; chunk > 0; chunk--) {
                int signOffset = test.getDataSize() / chunk;

                System.out.println("Running test with offset " + signOffset);
                byte[] signData = test.shiftSignData(signOffset);

                boolean success = test.verifySignature(signData, signOffset,
                        test.getSignatureLength(), 0, test.getDataSize());

                if (success) {
                    System.out.println("Successfully verified with offset "
                            + signOffset);
                } else {
                    System.out.println("Verification failed with offset "
                            + signOffset);
                    result = false;
                }
            }

            // save signature to offset 0
            byte[] signData = test.shiftSignData(0);

            // Negative tests

            // Test signature offset 0.
            // Wrong test data will be passed to update,
            // so signature verification should fail.
            for (int chunk = 3; chunk > 0; chunk--) {
                int dataOffset = (test.getDataSize() - 1) / chunk;
                boolean success;
                try {
                    success = test.verifySignature(signData, 0,
                            test.getSignatureLength(), dataOffset,
                            (test.getDataSize() - dataOffset));
                } catch (SignatureException e) {
                    // Since we are trying different data size, it can throw
                    // SignatureException
                    success = false;
                }

                if (!success) {
                    System.out.println("Signature verification failed "
                            + "as expected, with data offset " + dataOffset
                            + " and length "
                            + (test.getDataSize() - dataOffset));
                } else {
                    System.out.println("Signature verification "
                            + "should not succeed, with data offset "
                            + dataOffset + " and length "
                            + (test.getDataSize() - dataOffset));
                    result = false;
                }
            }

            // Tests with manipulating offset and length
            result &= Offsets.checkFailure(test, signData, -1,
                    test.getSignatureLength());

            result &= Offsets.checkFailure(test, signData, 0,
                    test.getSignatureLength() - 1);

            result &= Offsets.checkFailure(test, signData,
                    test.getSignatureLength() + 1, test.getSignatureLength());

            result &= Offsets.checkFailure(test, signData, 0,
                    test.getSignatureLength() + 1);

            result &= Offsets.checkFailure(test, signData, 0, 0);

            result &= Offsets.checkFailure(test, signData, 0, -1);

            result &= Offsets.checkFailure(test, signData,
                    2147483646, test.getSignatureLength());

            result &= Offsets.checkFailure(test, null, 0,
                    test.getSignatureLength());
        } catch (NoSuchProviderException nspe) {
            System.out.println("No such provider: " + nspe);
        }

        if (!result) {
            throw new RuntimeException("Some test cases failed");
        }
    }

    static boolean checkFailure(Offsets test, byte[] signData, int offset,
            int length) {
        boolean success;
        try {
            success = test.verifySignature(signData, offset, length, 0,
                    test.getDataSize());
        } catch (IllegalArgumentException | SignatureException e) {
            System.out.println("Expected exception: " + e);
            success = false;
        } catch (InvalidKeyException e) {
            System.out.println("Unexpected exception: " + e);
            return false;
        }

        if (!success) {
            System.out.println("Signature verification failed as expected, "
                    + "with signature offset " + offset + " and length "
                    + length);
            return true;
        } else {
            System.out.println("Signature verification should not succeed, "
                    + "with signature offset " + offset + " and length "
                    + length);
            return false;
        }
    }

}