jdk/src/share/classes/sun/security/x509/CRLReasonCodeExtension.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
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 1997-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.x509;
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.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.cert.CRLReason;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * The reasonCode is a non-critical CRL entry extension that identifies
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * the reason for the certificate revocation. CAs are strongly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * encouraged to include reason codes in CRL entries; however, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * reason code CRL entry extension should be absent instead of using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * unspecified (0) reasonCode value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p>The ASN.1 syntax for this is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *  id-ce-cRLReason OBJECT IDENTIFIER ::= { id-ce 21 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *  -- reasonCode ::= { CRLReason }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * CRLReason ::= ENUMERATED {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *    unspecified             (0),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *    keyCompromise           (1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *    cACompromise            (2),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *    affiliationChanged      (3),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *    superseded              (4),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *    cessationOfOperation    (5),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *    certificateHold         (6),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *    removeFromCRL           (8),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *    privilegeWithdrawn      (9),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *    aACompromise           (10) }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @author Hemma Prafullchandra
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @see Extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @see CertAttrSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
public class CRLReasonCodeExtension extends Extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        implements CertAttrSet<String> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Attribute name and Reason codes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public static final String NAME = "CRLReasonCode";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public static final String REASON = "reason";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public static final int UNSPECIFIED = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public static final int KEY_COMPROMISE = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public static final int CA_COMPROMISE = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public static final int AFFLIATION_CHANGED = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public static final int SUPERSEDED = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public static final int CESSATION_OF_OPERATION = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public static final int CERTIFICATE_HOLD = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    // note 7 missing in syntax
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public static final int REMOVE_FROM_CRL = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public static final int PRIVILEGE_WITHDRAWN = 9;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public static final int AA_COMPROMISE = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private static CRLReason[] values = CRLReason.values();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private int reasonCode = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private void encodeThis() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if (reasonCode == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            this.extensionValue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        DerOutputStream dos = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        dos.putEnumerated(reasonCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        this.extensionValue = dos.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * Create a CRLReasonCodeExtension with the passed in reason.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * Criticality automatically set to false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @param reason the enumerated value for the reason code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public CRLReasonCodeExtension(int reason) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        this(false, reason);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * Create a CRLReasonCodeExtension with the passed in reason.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @param critical true if the extension is to be treated as critical.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @param reason the enumerated value for the reason code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public CRLReasonCodeExtension(boolean critical, int reason)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        this.extensionId = PKIXExtensions.ReasonCode_Id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        this.critical = critical;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        this.reasonCode = reason;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        encodeThis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Create the extension from the passed DER encoded value of the same.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @param critical true if the extension is to be treated as critical.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @param value an array of DER encoded bytes of the actual value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @exception ClassCastException if value is not an array of bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @exception IOException on error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public CRLReasonCodeExtension(Boolean critical, Object value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        this.extensionId = PKIXExtensions.ReasonCode_Id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        this.critical = critical.booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        this.extensionValue = (byte[]) value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        DerValue val = new DerValue(this.extensionValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        this.reasonCode = val.getEnumerated();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * Set the attribute value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public void set(String name, Object obj) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if (!(obj instanceof Integer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            throw new IOException("Attribute must be of type Integer.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (name.equalsIgnoreCase(REASON)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            reasonCode = ((Integer)obj).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            throw new IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                ("Name not supported by CRLReasonCodeExtension");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        encodeThis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * Get the attribute value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public Object get(String name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (name.equalsIgnoreCase(REASON)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            return new Integer(reasonCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            throw new IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                ("Name not supported by CRLReasonCodeExtension");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * Delete the attribute value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public void delete(String name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        if (name.equalsIgnoreCase(REASON)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            reasonCode = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            throw new IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                ("Name not supported by CRLReasonCodeExtension");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        encodeThis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * Returns a printable representation of the Reason code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        return super.toString() + "    Reason Code: " + values[reasonCode];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * Write the extension to the DerOutputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @param out the DerOutputStream to write the extension to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @exception IOException on encoding errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    public void encode(OutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        DerOutputStream  tmp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        if (this.extensionValue == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            this.extensionId = PKIXExtensions.ReasonCode_Id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            this.critical = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            encodeThis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        super.encode(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        out.write(tmp.toByteArray());
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 an enumeration of names of attributes existing within this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    public Enumeration<String> getElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        AttributeNameEnumeration elements = new AttributeNameEnumeration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        elements.addElement(REASON);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        return elements.elements();
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
     * Return the name of this attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        return NAME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * Return the reason as a CRLReason enum.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    public CRLReason getReasonCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        // if out-of-range, return UNSPECIFIED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        if (reasonCode > 0 && reasonCode < values.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            return values[reasonCode];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            return CRLReason.UNSPECIFIED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
}