jdk/test/sun/security/x509/X509CRLImpl/OrderAndDup.java
author weijun
Wed, 29 Feb 2012 14:06:00 +0800
changeset 13038 e6024efff1b6
child 30820 0d4717a011d3
permissions -rw-r--r--
7143872: Improve certificate extension processing Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13038
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
     1
/*
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
     2
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
     4
 *
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
     7
 * published by the Free Software Foundation.
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
     8
 *
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    13
 * accompanied this code).
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    14
 *
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    18
 *
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    21
 * questions.
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    22
 */
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    23
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    24
/*
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    25
 * @test
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    26
 * @bug 7143872
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    27
 * @summary Improve certificate extension processing
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    28
 */
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    29
import java.io.ByteArrayInputStream;
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    30
import java.math.BigInteger;
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    31
import java.security.KeyPairGenerator;
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    32
import java.security.cert.CertificateFactory;
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    33
import java.security.cert.X509CRLEntry;
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    34
import java.util.Date;
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    35
import sun.security.util.DerInputStream;
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    36
import sun.security.util.DerValue;
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    37
import sun.security.x509.*;
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    38
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    39
public class OrderAndDup {
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    40
    public static void main(String[] args) throws Exception {
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    41
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    42
        // Generate 20 serial numbers with dup and a special order
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    43
        int count = 20;
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    44
        BigInteger[] serials = new BigInteger[count];
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    45
        for (int i=0; i<count; i++) {
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    46
            serials[i] = BigInteger.valueOf(i*7%10);
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    47
        }
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    48
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    49
        // Generates a CRL
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    50
        X509CRLEntry[] badCerts = new X509CRLEntry[count];
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    51
        for (int i=0; i<count; i++) {
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    52
            badCerts[i] = new X509CRLEntryImpl(serials[i],
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    53
                    new Date(System.currentTimeMillis()+i*1000));
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    54
        }
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    55
        X500Name owner = new X500Name("CN=CA");
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    56
        X509CRLImpl crl = new X509CRLImpl(owner, new Date(), new Date(), badCerts);
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    57
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    58
        crl.sign(kpg.genKeyPair().getPrivate(), "SHA1withRSA");
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    59
        byte[] data = crl.getEncodedInternal();
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    60
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    61
        // Check the encoding
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    62
        checkData(crl, data, serials);
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    63
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    64
        // Load a CRL from raw data
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    65
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    66
        X509CRLImpl crl2 = (X509CRLImpl)cf.generateCRL(new ByteArrayInputStream(data));
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    67
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    68
        // Check the encoding again
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    69
        data = crl2.getEncodedInternal();
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    70
        checkData(crl2, data, serials);
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    71
    }
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    72
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    73
    // Check the raw data's ASN.1 structure to see if the revoked certs
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    74
    // have the same number and correct order as inserted
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    75
    static void checkData(X509CRLImpl c, byte[] data, BigInteger[] expected)
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    76
            throws Exception {
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    77
        if (c.getRevokedCertificates().size() != expected.length) {
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    78
            throw new Exception("Wrong count in CRL object, now " +
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    79
                    c.getRevokedCertificates().size());
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    80
        }
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    81
        DerValue d1 = new DerValue(data);
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    82
        // revokedCertificates at 5th place of TBSCertList
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    83
        DerValue[] d2 = new DerInputStream(
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    84
                d1.data.getSequence(0)[4].toByteArray())
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    85
                .getSequence(0);
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    86
        if (d2.length != expected.length) {
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    87
            throw new Exception("Wrong count in raw data, now " + d2.length);
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    88
        }
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    89
        for (int i=0; i<d2.length; i++) {
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    90
            // Serial is first in revokedCertificates entry
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    91
            BigInteger bi = d2[i].data.getBigInteger();
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    92
            if (!bi.equals(expected[i])) {
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    93
                throw new Exception("Entry at #" + i + " is " + bi
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    94
                        + ", should be " + expected[i]);
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    95
            }
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    96
        }
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    97
    }
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    98
}
e6024efff1b6 7143872: Improve certificate extension processing
weijun
parents:
diff changeset
    99