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 javax.security.auth.x500.X500Principal;
22import org.apache.harmony.security.asn1.ASN1Sequence;
23import org.apache.harmony.security.asn1.ASN1Type;
24import org.apache.harmony.security.asn1.BerInputStream;
25import org.apache.harmony.security.x501.Name;
26
27/**
28 * CRL Entry's Certificate Issuer Extension (OID = 2.5.29.29).
29 * It is a CRL entry extension and contains the GeneralNames describing
30 * the issuer of revoked certificate. Its ASN.1 notation is as follows:
31 * <pre>
32 *   id-ce-certificateIssuer   OBJECT IDENTIFIER ::= { id-ce 29 }
33 *
34 *   certificateIssuer ::=     GeneralNames
35 * </pre>
36 * (as specified in RFC 3280)
37 * In java implementation it is presumed that GeneralNames consist of
38 * one element and its type is directoryName.
39 */
40public final class CertificateIssuer extends ExtensionValue {
41    /** certificate issuer value */
42    private X500Principal issuer;
43
44    /**
45     * Creates an object on the base of its encoded form.
46     */
47    public CertificateIssuer(byte[] encoding) {
48        super(encoding);
49    }
50
51    public X500Principal getIssuer() throws IOException {
52        if (issuer == null) {
53            issuer = (X500Principal) ASN1.decode(getEncoded());
54        }
55        return issuer;
56    }
57
58    @Override public void dumpValue(StringBuilder sb, String prefix) {
59        sb.append(prefix).append("Certificate Issuer: ");
60        if (issuer == null) {
61            try {
62                issuer = getIssuer();
63            } catch (IOException e) {
64                // incorrect extension value encoding
65                sb.append("Unparseable (incorrect!) extension value:\n");
66                super.dumpValue(sb);
67            }
68        }
69        sb.append(issuer).append('\n');
70    }
71
72    /**
73     * ASN.1 Encoder/Decoder.
74     */
75    public static final ASN1Type ASN1 = new ASN1Sequence(new ASN1Type[] { GeneralName.ASN1 }) {
76        @Override public Object getDecodedObject(BerInputStream in) {
77            return ((Name) ((GeneralName) ((Object[]) in.content)[0])
78                    .getName()).getX500Principal();
79        }
80
81        @Override protected void getValues(Object object, Object[] values) {
82            values[0] = object;
83        }
84    };
85}
86