jdk/src/share/classes/sun/security/x509/CRLNumberExtension.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 10336 0bb1999251f8
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1997, 2006, 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
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.math.BigInteger;
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
 * Represent the CRL Number Extension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <p>This extension, if present, conveys a monotonically increasing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * sequence number for each CRL issued by a given CA through a specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * CA X.500 Directory entry or CRL distribution point. This extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * allows users to easily determine when a particular CRL supersedes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * another CRL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @author Hemma Prafullchandra
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @see Extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @see CertAttrSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public class CRLNumberExtension extends Extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
implements CertAttrSet<String> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * Attribute name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public static final String NAME = "CRLNumber";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public static final String NUMBER = "value";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private static final String LABEL = "CRL Number";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private BigInteger crlNumber = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private String extensionName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private String extensionLabel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    // Encode this extension value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private void encodeThis() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        if (crlNumber == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            this.extensionValue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        DerOutputStream os = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        os.putInteger(this.crlNumber);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        this.extensionValue = os.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Create a CRLNumberExtension with the integer value .
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * The criticality is set to false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * @param crlNum the value to be set for the extension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public CRLNumberExtension(int crlNum) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        this(PKIXExtensions.CRLNumber_Id, false, BigInteger.valueOf(crlNum),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        NAME, LABEL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Create a CRLNumberExtension with the BigInteger value .
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * The criticality is set to false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @param crlNum the value to be set for the extension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public CRLNumberExtension(BigInteger crlNum) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        this(PKIXExtensions.CRLNumber_Id, false, crlNum, NAME, LABEL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Creates the extension (also called by the subclass).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    protected CRLNumberExtension(ObjectIdentifier extensionId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        boolean isCritical, BigInteger crlNum, String extensionName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        String extensionLabel) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        this.extensionId = extensionId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        this.critical = isCritical;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        this.crlNumber = crlNum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        this.extensionName = extensionName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        this.extensionLabel = extensionLabel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        encodeThis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Create the extension from the passed DER encoded value of the same.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @param critical true if the extension is to be treated as critical.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @param value an array of DER encoded bytes of the actual value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @exception ClassCastException if value is not an array of bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @exception IOException on error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public CRLNumberExtension(Boolean critical, Object value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        this(PKIXExtensions.CRLNumber_Id, critical, value, NAME, LABEL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * Creates the extension (also called by the subclass).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    protected CRLNumberExtension(ObjectIdentifier extensionId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        Boolean critical, Object value, String extensionName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        String extensionLabel) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        this.extensionId = extensionId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        this.critical = critical.booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        this.extensionValue = (byte[]) value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        DerValue val = new DerValue(this.extensionValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        this.crlNumber = val.getBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        this.extensionName = extensionName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        this.extensionLabel = extensionLabel;
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 (name.equalsIgnoreCase(NUMBER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            if (!(obj instanceof BigInteger)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                throw new IOException("Attribute must be of type BigInteger.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            crlNumber = (BigInteger)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
          throw new IOException("Attribute name not recognized by"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                                + " CertAttrSet:" + extensionName + ".");
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(NUMBER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            if (crlNumber == null) return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            else return crlNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
          throw new IOException("Attribute name not recognized by"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                                + " CertAttrSet:" + extensionName + ".");
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Delete the attribute value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    public void delete(String name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        if (name.equalsIgnoreCase(NUMBER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            crlNumber = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
          throw new IOException("Attribute name not recognized by"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                                + " CertAttrSet:" + extensionName + ".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        encodeThis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * Returns a printable representation of the CRLNumberExtension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        String s = super.toString() + extensionLabel + ": " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                   ((crlNumber == null) ? "" : Debug.toHexString(crlNumber))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                   + "\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        return (s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * Write the extension to the DerOutputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @param out the DerOutputStream to write the extension to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @exception IOException on encoding errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    public void encode(OutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
       DerOutputStream  tmp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        encode(out, PKIXExtensions.CRLNumber_Id, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * Write the extension to the DerOutputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * (Also called by the subclass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    protected void encode(OutputStream out, ObjectIdentifier extensionId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        boolean isCritical) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
       DerOutputStream  tmp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
       if (this.extensionValue == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
           this.extensionId = extensionId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
           this.critical = isCritical;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
           encodeThis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
       super.encode(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
       out.write(tmp.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * Return an enumeration of names of attributes existing within this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    public Enumeration<String> getElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        AttributeNameEnumeration elements = new AttributeNameEnumeration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        elements.addElement(NUMBER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        return (elements.elements());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * Return the name of this attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        return (extensionName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
}