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 final 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[] encoding) throws IOException {
63        super(encoding);
64        this.code = ((byte[]) ASN1.decode(encoding))[0];
65    }
66
67    @Override public byte[] getEncoded() {
68        if (encoding == null) {
69            encoding = ASN1.encode(new byte[] { code });
70        }
71        return encoding;
72    }
73
74    @Override public void dumpValue(StringBuilder sb, String prefix) {
75        sb.append(prefix).append("Reason Code: [ ");
76        switch (code) {
77        case UNSPECIFIED:
78            sb.append("unspecified");
79            break;
80        case KEY_COMPROMISE:
81            sb.append("keyCompromise");
82            break;
83        case CA_COMPROMISE:
84            sb.append("cACompromise");
85            break;
86        case AFFILIATION_CHANGED:
87            sb.append("affiliationChanged");
88            break;
89        case SUPERSEDED:
90            sb.append("superseded");
91            break;
92        case CESSATION_OF_OPERATION:
93            sb.append("cessationOfOperation");
94            break;
95        case CERTIFICATE_HOLD:
96            sb.append("certificateHold");
97            break;
98        case REMOVE_FROM_CRL:
99            sb.append("removeFromCRL");
100            break;
101        case PRIVILEGE_WITHDRAWN:
102            sb.append("privilegeWithdrawn");
103            break;
104        case AA_COMPROMISE:
105            sb.append("aACompromise");
106            break;
107        }
108        sb.append(" ]\n");
109    }
110
111    /**
112     * ASN.1 Encoder/Decoder.
113     */
114    public static final ASN1Type ASN1 = ASN1Enumerated.getInstance();
115}
116