ReasonCode.java revision 866e7ae17a3da81a02b0b144e0c9c2b3196d293a
1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package org.apache.harmony.security.x509;
19
20import java.io.IOException;
21import org.apache.harmony.security.asn1.ASN1Enumerated;
22import org.apache.harmony.security.asn1.ASN1Type;
23
24/**
25 * CRL Entry's Reason Code Extension (OID = 2.5.29.21).
26 * <pre>
27 *  id-ce-cRLReason OBJECT IDENTIFIER ::= { id-ce 21 }
28 *
29 *  -- reasonCode ::= { CRLReason }
30 *  CRLReason ::= ENUMERATED {
31 *       unspecified             (0),
32 *       keyCompromise           (1),
33 *       cACompromise            (2),
34 *       affiliationChanged      (3),
35 *       superseded              (4),
36 *       cessationOfOperation    (5),
37 *       certificateHold         (6),
38 *       removeFromCRL           (8),
39 *       privilegeWithdrawn      (9),
40 *       aACompromise           (10)
41 *  }
42 * </pre>
43 * (as specified in RFC 3280 http://www.ietf.org/rfc/rfc3280.txt)
44 */
45public class ReasonCode extends ExtensionValue {
46
47    // predefined reason code values
48    public static final byte UNSPECIFIED = 0;
49    public static final byte KEY_COMPROMISE = 1;
50    public static final byte CA_COMPROMISE = 2;
51    public static final byte AFFILIATION_CHANGED = 3;
52    public static final byte SUPERSEDED = 4;
53    public static final byte CESSATION_OF_OPERATION = 5;
54    public static final byte CERTIFICATE_HOLD = 6;
55    public static final byte REMOVE_FROM_CRL = 8;
56    public static final byte PRIVILEGE_WITHDRAWN = 9;
57    public static final byte AA_COMPROMISE = 10;
58
59    // the reason code value
60    private final byte code;
61
62    public ReasonCode(byte code) {
63        this.code = code;
64    }
65
66    public ReasonCode(byte[] encoding) throws IOException {
67        super(encoding);
68        this.code = ((byte[]) ASN1.decode(encoding))[0];
69    }
70
71    public int getCode() {
72        return code;
73    }
74
75    /**
76     * Returns ASN.1 encoded form of this X.509 ReasonCode value.
77     * @return a byte array containing ASN.1 encode form.
78     */
79    public byte[] getEncoded() {
80        if (encoding == null) {
81            encoding = ASN1.encode(new byte[] { code });
82        }
83        return encoding;
84    }
85
86    /**
87     * Places the string representation of extension value
88     * into the StringBuffer object.
89     */
90    public void dumpValue(StringBuffer buffer, String prefix) {
91        buffer.append(prefix).append("Reason Code: [ ");
92        switch (code) {
93            case UNSPECIFIED:
94                buffer.append("unspecified");
95                break;
96            case KEY_COMPROMISE:
97                buffer.append("keyCompromise");
98                break;
99            case CA_COMPROMISE:
100                buffer.append("cACompromise");
101                break;
102            case AFFILIATION_CHANGED:
103                buffer.append("affiliationChanged");
104                break;
105            case SUPERSEDED:
106                buffer.append("superseded");
107                break;
108            case CESSATION_OF_OPERATION:
109                buffer.append("cessationOfOperation");
110                break;
111            case CERTIFICATE_HOLD:
112                buffer.append("certificateHold");
113                break;
114            case REMOVE_FROM_CRL:
115                buffer.append("removeFromCRL");
116                break;
117            case PRIVILEGE_WITHDRAWN:
118                buffer.append("privilegeWithdrawn");
119                break;
120            case AA_COMPROMISE:
121                buffer.append("aACompromise");
122                break;
123        }
124        buffer.append(" ]\n");
125    }
126
127    /**
128     * ASN.1 Encoder/Decoder.
129     */
130    public static final ASN1Type ASN1 = ASN1Enumerated.getInstance();
131}
132