jdk/src/share/classes/sun/security/provider/certpath/X509CertificatePair.java
author mullan
Mon, 17 Feb 2014 11:36:40 -0500
changeset 22977 f8f315760941
parent 12860 9ffbd4e43413
permissions -rw-r--r--
8025708: Certificate Path Building problem with AKI serial number Reviewed-by: xuelei, juh
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10785
diff changeset
     2
 * Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package sun.security.provider.certpath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.security.GeneralSecurityException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.PublicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.cert.CertificateEncodingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.cert.CertificateException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.cert.X509Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.interfaces.DSAPublicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.security.auth.x500.X500Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.security.util.DerOutputStream;
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.Cache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.security.x509.X509CertImpl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.security.provider.X509Factory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * This class represents an X.509 Certificate Pair object, which is primarily
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * used to hold a pair of cross certificates issued between Certification
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Authorities. The ASN.1 structure is listed below. The forward certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * of the CertificatePair contains a certificate issued to this CA by another
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * CA. The reverse certificate of the CertificatePair contains a certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * issued by this CA to another CA. When both the forward and the reverse
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * certificates are present in the CertificatePair, the issuer name in one
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * certificate shall match the subject name in the other and vice versa, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * the subject public key in one certificate shall be capable of verifying the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * digital signature on the other certificate and vice versa.  If a subject
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * public key in one certificate does not contain required key algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * parameters, then the signature check involving that key is not done.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * The ASN.1 syntax for this object is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * CertificatePair      ::=     SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *      forward [0]     Certificate OPTIONAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *      reverse [1]     Certificate OPTIONAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *                      -- at least one of the pair shall be present -- }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * </pre><p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * This structure uses EXPLICIT tagging. References: Annex A of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * X.509(2000), X.509(1997).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * @author      Sean Mullan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * @since       1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
public class X509CertificatePair {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /* ASN.1 explicit tags */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private static final byte TAG_FORWARD = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private static final byte TAG_REVERSE = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private X509Certificate forward;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private X509Certificate reverse;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private byte[] encoded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 5506
diff changeset
    82
    private static final Cache<Object, X509CertificatePair> cache
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 5506
diff changeset
    83
        = Cache.newSoftMemoryCache(750);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Creates an empty instance of X509CertificatePair.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public X509CertificatePair() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Creates an instance of X509CertificatePair. At least one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * the pair must be non-null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @param forward The forward component of the certificate pair
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *          which represents a certificate issued to this CA by other CAs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @param reverse The reverse component of the certificate pair
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *          which represents a certificate issued by this CA to other CAs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @throws CertificateException If an exception occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public X509CertificatePair(X509Certificate forward, X509Certificate reverse)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (forward == null && reverse == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            throw new CertificateException("at least one of certificate pair "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                + "must be non-null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        this.forward = forward;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        this.reverse = reverse;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        checkPair();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Create a new X509CertificatePair from its encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * For internal use only, external code should use generateCertificatePair.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 5506
diff changeset
   118
    private X509CertificatePair(byte[] encoded) throws CertificateException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            parse(new DerValue(encoded));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            this.encoded = encoded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            throw new CertificateException(ex.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        checkPair();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * Clear the cache for debugging.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public static synchronized void clearCache() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        cache.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Create a X509CertificatePair from its encoding. Uses cache lookup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * if possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public static synchronized X509CertificatePair generateCertificatePair
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            (byte[] encoded) throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        Object key = new Cache.EqualByteArray(encoded);
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 5506
diff changeset
   142
        X509CertificatePair pair = cache.get(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if (pair != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            return pair;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        pair = new X509CertificatePair(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        key = new Cache.EqualByteArray(pair.encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        cache.put(key, pair);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        return pair;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * Sets the forward component of the certificate pair.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    public void setForward(X509Certificate cert) throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        checkPair();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        forward = cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * Sets the reverse component of the certificate pair.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    public void setReverse(X509Certificate cert) throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        checkPair();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        reverse = cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Returns the forward component of the certificate pair.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @return The forward certificate, or null if not set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public X509Certificate getForward() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        return forward;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * Returns the reverse component of the certificate pair.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @return The reverse certificate, or null if not set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public X509Certificate getReverse() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        return reverse;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * Return the DER encoded form of the certificate pair.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @return The encoded form of the certificate pair.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @throws CerticateEncodingException If an encoding exception occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    public byte[] getEncoded() throws CertificateEncodingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            if (encoded == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                DerOutputStream tmp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                emit(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                encoded = tmp.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            throw new CertificateEncodingException(ex.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        return encoded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * Return a printable representation of the certificate pair.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @return A String describing the contents of the pair.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     */
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10785
diff changeset
   210
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    public String toString() {
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10785
diff changeset
   212
        StringBuilder sb = new StringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        sb.append("X.509 Certificate Pair: [\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        if (forward != null)
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10785
diff changeset
   215
            sb.append("  Forward: ").append(forward).append("\n");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        if (reverse != null)
12860
9ffbd4e43413 6854712: Revocation checking enhancements (JEP-124)
mullan
parents: 10785
diff changeset
   217
            sb.append("  Reverse: ").append(reverse).append("\n");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        sb.append("]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    /* Parse the encoded bytes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    private void parse(DerValue val)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        throws IOException, CertificateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        if (val.tag != DerValue.tag_Sequence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            throw new IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                ("Sequence tag missing for X509CertificatePair");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        while (val.data != null && val.data.available() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            DerValue opt = val.data.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            short tag = (byte) (opt.tag & 0x01f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            switch (tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                case TAG_FORWARD:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                    if (opt.isContextSpecific() && opt.isConstructed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                        if (forward != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                            throw new IOException("Duplicate forward "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                                + "certificate in X509CertificatePair");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                        opt = opt.data.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                        forward = X509Factory.intern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                                        (new X509CertImpl(opt.toByteArray()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                case TAG_REVERSE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                    if (opt.isContextSpecific() && opt.isConstructed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                        if (reverse != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                            throw new IOException("Duplicate reverse "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                                + "certificate in X509CertificatePair");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                        opt = opt.data.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                        reverse = X509Factory.intern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                                        (new X509CertImpl(opt.toByteArray()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    throw new IOException("Invalid encoding of "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                        + "X509CertificatePair");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        if (forward == null && reverse == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            throw new CertificateException("at least one of certificate pair "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                + "must be non-null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    /* Translate to encoded bytes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    private void emit(DerOutputStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        throws IOException, CertificateEncodingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        DerOutputStream tagged = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        if (forward != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            DerOutputStream tmp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            tmp.putDerValue(new DerValue(forward.getEncoded()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            tagged.write(DerValue.createTag(DerValue.TAG_CONTEXT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                         true, TAG_FORWARD), tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        if (reverse != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            DerOutputStream tmp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            tmp.putDerValue(new DerValue(reverse.getEncoded()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            tagged.write(DerValue.createTag(DerValue.TAG_CONTEXT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                         true, TAG_REVERSE), tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        out.write(DerValue.tag_Sequence, tagged);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * Check for a valid certificate pair
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    private void checkPair() throws CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        /* if either of pair is missing, return w/o error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        if (forward == null || reverse == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
         * If both elements of the pair are present, check that they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
         * are a valid pair.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        X500Principal fwSubject = forward.getSubjectX500Principal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        X500Principal fwIssuer = forward.getIssuerX500Principal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        X500Principal rvSubject = reverse.getSubjectX500Principal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        X500Principal rvIssuer = reverse.getIssuerX500Principal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        if (!fwIssuer.equals(rvSubject) || !rvIssuer.equals(fwSubject)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            throw new CertificateException("subject and issuer names in "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                + "forward and reverse certificates do not match");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        /* check signatures unless key parameters are missing */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            PublicKey pk = reverse.getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            if (!(pk instanceof DSAPublicKey) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                        ((DSAPublicKey)pk).getParams() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                forward.verify(pk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            pk = forward.getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            if (!(pk instanceof DSAPublicKey) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                        ((DSAPublicKey)pk).getParams() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                reverse.verify(pk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            throw new CertificateException("invalid signature: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                + e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
}