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;
21
22import org.apache.harmony.security.asn1.ASN1Enumerated;
23import org.apache.harmony.security.asn1.ASN1Type;
24
25/**
26 * CRL Entry's Reason Code Extension (OID = 2.5.29.21).
27 * <pre>
28 *  id-ce-cRLReason OBJECT IDENTIFIER ::= { id-ce 21 }
29 *
30 *  -- reasonCode ::= { CRLReason }
31 *  CRLReason ::= ENUMERATED {
32 *       unspecified             (0),
33 *       keyCompromise           (1),
34 *       cACompromise            (2),
35 *       affiliationChanged      (3),
36 *       superseded              (4),
37 *       cessationOfOperation    (5),
38 *       certificateHold         (6),
39 *       removeFromCRL           (8),
40 *       privilegeWithdrawn      (9),
41 *       aACompromise           (10)
42 *  }
43 * </pre>
44 * (as specified in RFC 3280 http://www.ietf.org/rfc/rfc3280.txt)
45 */
46public class ReasonCode extends ExtensionValue {
47
48    // predefined reason code values
49    public static final byte UNSPECIFIED = 0;
50    public static final byte KEY_COMPROMISE = 1;
51    public static final byte CA_COMPROMISE = 2;
52    public static final byte AFFILIATION_CHANGED = 3;
53    public static final byte SUPERSEDED = 4;
54    public static final byte CESSATION_OF_OPERATION = 5;
55    public static final byte CERTIFICATE_HOLD = 6;
56    public static final byte REMOVE_FROM_CRL = 8;
57    public static final byte PRIVILEGE_WITHDRAWN = 9;
58    public static final byte AA_COMPROMISE = 10;
59
60    // the reason code value
61    private final byte code;
62
63    public ReasonCode(byte code) {
64        this.code = code;
65    }
66
67    public ReasonCode(byte[] encoding) throws IOException {
68        super(encoding);
69        this.code = ((byte[]) ASN1.decode(encoding))[0];
70    }
71
72    public int getCode() {
73        return code;
74    }
75
76    /**
77     * Returns ASN.1 encoded form of this X.509 ReasonCode value.
78     * @return a byte array containing ASN.1 encode form.
79     */
80    public byte[] getEncoded() {
81        if (encoding == null) {
82            encoding = ASN1.encode(new byte[] {(byte) code});
83        }
84        return encoding;
85    }
86
87    /**
88     * Places the string representation of extension value
89     * into the StringBuffer object.
90     */
91    public void dumpValue(StringBuffer buffer, String prefix) {
92        buffer.append(prefix).append("Reason Code: [ "); //$NON-NLS-1$
93        switch (code) {
94            case UNSPECIFIED:
95                buffer.append("unspecified"); //$NON-NLS-1$
96                break;
97            case KEY_COMPROMISE:
98                buffer.append("keyCompromise"); //$NON-NLS-1$
99                break;
100            case CA_COMPROMISE:
101                buffer.append("cACompromise"); //$NON-NLS-1$
102                break;
103            case AFFILIATION_CHANGED:
104                buffer.append("affiliationChanged"); //$NON-NLS-1$
105                break;
106            case SUPERSEDED:
107                buffer.append("superseded"); //$NON-NLS-1$
108                break;
109            case CESSATION_OF_OPERATION:
110                buffer.append("cessationOfOperation"); //$NON-NLS-1$
111                break;
112            case CERTIFICATE_HOLD:
113                buffer.append("certificateHold"); //$NON-NLS-1$
114                break;
115            case REMOVE_FROM_CRL:
116                buffer.append("removeFromCRL"); //$NON-NLS-1$
117                break;
118            case PRIVILEGE_WITHDRAWN:
119                buffer.append("privilegeWithdrawn"); //$NON-NLS-1$
120                break;
121            case AA_COMPROMISE:
122                buffer.append("aACompromise"); //$NON-NLS-1$
123                break;
124        }
125        buffer.append(" ]\n"); //$NON-NLS-1$
126    }
127
128    /**
129     * ASN.1 Encoder/Decoder.
130     */
131    public static final ASN1Type ASN1 = ASN1Enumerated.getInstance();
132}
133
134