jdk/src/share/classes/sun/security/validator/SimpleValidator.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 2926 6fbaec35c792
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2002-2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.validator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.cert.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.security.auth.x500.X500Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.security.x509.X509CertImpl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.security.x509.NetscapeCertTypeExtension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.security.util.DerValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.security.util.DerInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.security.util.DerOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.security.util.ObjectIdentifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * A simple validator implementation. It is based on code from the JSSE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * X509TrustManagerImpl. This implementation is designed for compatibility with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * deployed certificates and previous J2SE versions. It will never support
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * more advanced features and will be deemphasized in favor of the PKIX
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * validator going forward.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @author Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
public final class SimpleValidator extends Validator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    // Constants for the OIDs we need
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    final static String OID_BASIC_CONSTRAINTS = "2.5.29.19";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    final static String OID_NETSCAPE_CERT_TYPE = "2.16.840.1.113730.1.1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    final static String OID_KEY_USAGE = "2.5.29.15";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    final static String OID_EXTENDED_KEY_USAGE = "2.5.29.37";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    final static String OID_EKU_ANY_USAGE = "2.5.29.37.0";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    final static ObjectIdentifier OBJID_NETSCAPE_CERT_TYPE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        NetscapeCertTypeExtension.NetscapeCertType_Id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private final static String NSCT_SSL_CA =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                                NetscapeCertTypeExtension.SSL_CA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private final static String NSCT_CODE_SIGNING_CA =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                                NetscapeCertTypeExtension.OBJECT_SIGNING_CA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * The trusted certificates as:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * Map (X500Principal)subject of trusted cert -> List of X509Certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * The list is used because there may be multiple certificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * with an identical subject DN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private Map<X500Principal, List<X509Certificate>> trustedX500Principals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Set of the trusted certificates. Present only for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * getTrustedCertificates().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private Collection<X509Certificate> trustedCerts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    SimpleValidator(String variant, Collection<X509Certificate> trustedCerts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        super(TYPE_SIMPLE, variant);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        this.trustedCerts = trustedCerts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        trustedX500Principals =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                        new HashMap<X500Principal, List<X509Certificate>>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        for (X509Certificate cert : trustedCerts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            X500Principal principal = cert.getSubjectX500Principal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            List<X509Certificate> list = trustedX500Principals.get(principal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            if (list == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                // this actually should be a set, but duplicate entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                // are not a problem and we can avoid the Set overhead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                list = new ArrayList<X509Certificate>(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                trustedX500Principals.put(principal, list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            list.add(cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public Collection<X509Certificate> getTrustedCertificates() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        return trustedCerts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * Perform simple validation of chain. The arguments otherCerts and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * parameter are ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    X509Certificate[] engineValidate(X509Certificate[] chain,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            Collection<X509Certificate> otherCerts, Object parameter)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        if ((chain == null) || (chain.length == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            throw new CertificateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                ("null or zero-length certificate chain");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        // make sure chain includes a trusted cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        chain = buildTrustedChain(chain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        Date date = validationDate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        if (date == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            date = new Date();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        // verify top down, starting at the certificate issued by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        // the trust anchor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        for (int i = chain.length - 2; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            X509Certificate issuerCert = chain[i + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            X509Certificate cert = chain[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            // no validity check for code signing certs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            if ((variant.equals(VAR_CODE_SIGNING) == false)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                        && (variant.equals(VAR_JCE_SIGNING) == false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                cert.checkValidity(date);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            // check name chaining
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            if (cert.getIssuerX500Principal().equals(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                        issuerCert.getSubjectX500Principal()) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                throw new ValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                        (ValidatorException.T_NAME_CHAINING, cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            // check signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                cert.verify(issuerCert.getPublicKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                throw new ValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                        (ValidatorException.T_SIGNATURE_ERROR, cert, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            // check extensions for CA certs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            if (i != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                checkExtensions(cert, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        return chain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    private void checkExtensions(X509Certificate cert, int index)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        Set<String> critSet = cert.getCriticalExtensionOIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (critSet == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            critSet = Collections.<String>emptySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        // Check the basic constraints extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        checkBasicConstraints(cert, critSet, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        // Check the key usage and extended key usage extensions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        checkKeyUsage(cert, critSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        // check Netscape certificate type extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        checkNetscapeCertType(cert, critSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        if (!critSet.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            throw new ValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                ("Certificate contains unknown critical extensions: " + critSet,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                ValidatorException.T_CA_EXTENSIONS, cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    private void checkNetscapeCertType(X509Certificate cert,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            Set<String> critSet) throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        if (variant.equals(VAR_GENERIC)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            // nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        } else if (variant.equals(VAR_TLS_CLIENT)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                || variant.equals(VAR_TLS_SERVER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            if (getNetscapeCertTypeBit(cert, NSCT_SSL_CA) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                throw new ValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                        ("Invalid Netscape CertType extension for SSL CA "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                        + "certificate",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                        ValidatorException.T_CA_EXTENSIONS, cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            critSet.remove(OID_NETSCAPE_CERT_TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        } else if (variant.equals(VAR_CODE_SIGNING)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                || variant.equals(VAR_JCE_SIGNING)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            if (getNetscapeCertTypeBit(cert, NSCT_CODE_SIGNING_CA) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                throw new ValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                        ("Invalid Netscape CertType extension for code "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                        + "signing CA certificate",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                        ValidatorException.T_CA_EXTENSIONS, cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            critSet.remove(OID_NETSCAPE_CERT_TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            throw new CertificateException("Unknown variant " + variant);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * Get the value of the specified bit in the Netscape certificate type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * extension. If the extension is not present at all, we return true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    static boolean getNetscapeCertTypeBit(X509Certificate cert, String type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            NetscapeCertTypeExtension ext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            if (cert instanceof X509CertImpl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                X509CertImpl certImpl = (X509CertImpl)cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                ObjectIdentifier oid = OBJID_NETSCAPE_CERT_TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                ext = (NetscapeCertTypeExtension)certImpl.getExtension(oid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                if (ext == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                byte[] extVal = cert.getExtensionValue(OID_NETSCAPE_CERT_TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                if (extVal == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                DerInputStream in = new DerInputStream(extVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                byte[] encoded = in.getOctetString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                encoded = new DerValue(encoded).getUnalignedBitString()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                                                                .toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                ext = new NetscapeCertTypeExtension(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            Boolean val = (Boolean)ext.get(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            return val.booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    private void checkBasicConstraints(X509Certificate cert,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            Set<String> critSet, int index) throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        critSet.remove(OID_BASIC_CONSTRAINTS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        int constraints = cert.getBasicConstraints();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        // reject, if extension missing or not a CA (constraints == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        if (constraints < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            throw new ValidatorException("End user tried to act as a CA",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                ValidatorException.T_CA_EXTENSIONS, cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        if (index - 1 > constraints) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            throw new ValidatorException("Violated path length constraints",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                ValidatorException.T_CA_EXTENSIONS, cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * Verify the key usage and extended key usage for intermediate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * certificates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    private void checkKeyUsage(X509Certificate cert, Set<String> critSet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        critSet.remove(OID_KEY_USAGE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        // EKU irrelevant in CA certificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        critSet.remove(OID_EXTENDED_KEY_USAGE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        // check key usage extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        boolean[] keyUsageInfo = cert.getKeyUsage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        if (keyUsageInfo != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            // keyUsageInfo[5] is for keyCertSign.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            if ((keyUsageInfo.length < 6) || (keyUsageInfo[5] == false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                throw new ValidatorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                        ("Wrong key usage: expected keyCertSign",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                        ValidatorException.T_CA_EXTENSIONS, cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * Build a trusted certificate chain. This method always returns a chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * with a trust anchor as the final cert in the chain. If no trust anchor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * could be found, a CertificateException is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    private X509Certificate[] buildTrustedChain(X509Certificate[] chain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        List<X509Certificate> c = new ArrayList<X509Certificate>(chain.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        // scan chain starting at EE cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        // if a trusted certificate is found, append it and return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        for (int i = 0; i < chain.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            X509Certificate cert = chain[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            X509Certificate trustedCert = getTrustedCertificate(cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            if (trustedCert != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                c.add(trustedCert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                return c.toArray(CHAIN0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            c.add(cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        // check if we can append a trusted cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        X509Certificate cert = chain[chain.length - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        X500Principal subject = cert.getSubjectX500Principal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        X500Principal issuer = cert.getIssuerX500Principal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        if (subject.equals(issuer) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            List<X509Certificate> list = trustedX500Principals.get(issuer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            if (list != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                X509Certificate trustedCert = list.iterator().next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                c.add(trustedCert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                return c.toArray(CHAIN0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        // no trusted cert found, error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        throw new ValidatorException(ValidatorException.T_NO_TRUST_ANCHOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * Return a trusted certificate that matches the input certificate,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * or null if no such certificate can be found. This method also handles
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * cases where a CA re-issues a trust anchor with the same public key and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * same subject and issuer names but a new validity period, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    private X509Certificate getTrustedCertificate(X509Certificate cert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        Principal certSubjectName = cert.getSubjectX500Principal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        List<X509Certificate> list = trustedX500Principals.get(certSubjectName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        if (list == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        Principal certIssuerName = cert.getIssuerX500Principal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        PublicKey certPublicKey = cert.getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        for (X509Certificate mycert : list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            if (mycert.equals(cert)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                return cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            if (!mycert.getIssuerX500Principal().equals(certIssuerName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            if (!mycert.getPublicKey().equals(certPublicKey)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            // All tests pass, this must be the one to use...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            return mycert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
}