13038
|
1 |
/*
|
|
2 |
* Copyright (c) 2012, 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 7143872
|
|
27 |
* @summary Improve certificate extension processing
|
|
28 |
*/
|
|
29 |
import java.io.ByteArrayInputStream;
|
|
30 |
import java.math.BigInteger;
|
|
31 |
import java.security.KeyPairGenerator;
|
|
32 |
import java.security.cert.CertificateFactory;
|
|
33 |
import java.security.cert.X509CRLEntry;
|
|
34 |
import java.util.Date;
|
|
35 |
import sun.security.util.DerInputStream;
|
|
36 |
import sun.security.util.DerValue;
|
|
37 |
import sun.security.x509.*;
|
|
38 |
|
|
39 |
public class OrderAndDup {
|
|
40 |
public static void main(String[] args) throws Exception {
|
|
41 |
|
|
42 |
// Generate 20 serial numbers with dup and a special order
|
|
43 |
int count = 20;
|
|
44 |
BigInteger[] serials = new BigInteger[count];
|
|
45 |
for (int i=0; i<count; i++) {
|
|
46 |
serials[i] = BigInteger.valueOf(i*7%10);
|
|
47 |
}
|
|
48 |
|
|
49 |
// Generates a CRL
|
|
50 |
X509CRLEntry[] badCerts = new X509CRLEntry[count];
|
|
51 |
for (int i=0; i<count; i++) {
|
|
52 |
badCerts[i] = new X509CRLEntryImpl(serials[i],
|
|
53 |
new Date(System.currentTimeMillis()+i*1000));
|
|
54 |
}
|
|
55 |
X500Name owner = new X500Name("CN=CA");
|
|
56 |
X509CRLImpl crl = new X509CRLImpl(owner, new Date(), new Date(), badCerts);
|
|
57 |
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
|
|
58 |
crl.sign(kpg.genKeyPair().getPrivate(), "SHA1withRSA");
|
|
59 |
byte[] data = crl.getEncodedInternal();
|
|
60 |
|
|
61 |
// Check the encoding
|
|
62 |
checkData(crl, data, serials);
|
|
63 |
|
|
64 |
// Load a CRL from raw data
|
|
65 |
CertificateFactory cf = CertificateFactory.getInstance("X.509");
|
|
66 |
X509CRLImpl crl2 = (X509CRLImpl)cf.generateCRL(new ByteArrayInputStream(data));
|
|
67 |
|
|
68 |
// Check the encoding again
|
|
69 |
data = crl2.getEncodedInternal();
|
|
70 |
checkData(crl2, data, serials);
|
|
71 |
}
|
|
72 |
|
|
73 |
// Check the raw data's ASN.1 structure to see if the revoked certs
|
|
74 |
// have the same number and correct order as inserted
|
|
75 |
static void checkData(X509CRLImpl c, byte[] data, BigInteger[] expected)
|
|
76 |
throws Exception {
|
|
77 |
if (c.getRevokedCertificates().size() != expected.length) {
|
|
78 |
throw new Exception("Wrong count in CRL object, now " +
|
|
79 |
c.getRevokedCertificates().size());
|
|
80 |
}
|
|
81 |
DerValue d1 = new DerValue(data);
|
|
82 |
// revokedCertificates at 5th place of TBSCertList
|
|
83 |
DerValue[] d2 = new DerInputStream(
|
|
84 |
d1.data.getSequence(0)[4].toByteArray())
|
|
85 |
.getSequence(0);
|
|
86 |
if (d2.length != expected.length) {
|
|
87 |
throw new Exception("Wrong count in raw data, now " + d2.length);
|
|
88 |
}
|
|
89 |
for (int i=0; i<d2.length; i++) {
|
|
90 |
// Serial is first in revokedCertificates entry
|
|
91 |
BigInteger bi = d2[i].data.getBigInteger();
|
|
92 |
if (!bi.equals(expected[i])) {
|
|
93 |
throw new Exception("Entry at #" + i + " is " + bi
|
|
94 |
+ ", should be " + expected[i]);
|
|
95 |
}
|
|
96 |
}
|
|
97 |
}
|
|
98 |
}
|
|
99 |
|