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
18/**
19* @author Alexander Y. Kleymenov
20* @version $Revision$
21*/
22
23package org.apache.harmony.security.x509;
24
25import org.apache.harmony.security.asn1.ASN1Explicit;
26import org.apache.harmony.security.asn1.ASN1Sequence;
27import org.apache.harmony.security.asn1.ASN1Type;
28import org.apache.harmony.security.asn1.BerInputStream;
29import org.apache.harmony.security.x501.DirectoryString;
30
31/**
32 * The class encapsulates the ASN.1 DER encoding/decoding work
33 * with the following structure which is a subpart of GeneralName
34 * (as specified in RFC 3280 -
35 *  Internet X.509 Public Key Infrastructure.
36 *  Certificate and Certificate Revocation List (CRL) Profile.
37 *  http://www.ietf.org/rfc/rfc3280.txt):
38 *
39 * <pre>
40 *   EDIPartyName ::= SEQUENCE {
41 *        nameAssigner            [0]     DirectoryString OPTIONAL,
42 *        partyName               [1]     DirectoryString
43 *   }
44 *
45 *   DirectoryString ::= CHOICE {
46 *        teletexString             TeletexString   (SIZE (1..MAX)),
47 *        printableString           PrintableString (SIZE (1..MAX)),
48 *        universalString           UniversalString (SIZE (1..MAX)),
49 *        utf8String              UTF8String      (SIZE (1..MAX)),
50 *        bmpString               BMPString       (SIZE (1..MAX))
51 *   }
52 * </pre>
53 */
54public final class EDIPartyName {
55    /** the value of nameAssigner field of the structure */
56    private final String nameAssigner;
57    /** the value of partyName field of the structure */
58    private final String partyName;
59    /** the ASN.1 encoded form of EDIPartyName */
60    private byte[] encoding;
61
62    private EDIPartyName(String nameAssigner, String partyName, byte[] encoding) {
63        this.nameAssigner = nameAssigner;
64        this.partyName = partyName;
65        this.encoding = encoding;
66    }
67
68    /**
69     * Returns ASN.1 encoded form of this X.509 EDIPartyName value.
70     */
71    public byte[] getEncoded() {
72        if (encoding == null) {
73            encoding = ASN1.encode(this);
74        }
75        return encoding;
76    }
77
78    /**
79     * ASN.1 DER X.509 EDIPartyName encoder/decoder class.
80     */
81    public static final ASN1Sequence ASN1 = new ASN1Sequence(
82            new ASN1Type[] {
83                new ASN1Explicit(0, DirectoryString.ASN1),
84                new ASN1Explicit(1, DirectoryString.ASN1)
85            }) {
86        {
87            setOptional(0);
88        }
89
90        @Override protected Object getDecodedObject(BerInputStream in) {
91            Object[] values = (Object[]) in.content;
92            return new EDIPartyName((String) values[0], (String) values[1],
93                    in.getEncoded());
94        }
95
96        @Override protected void getValues(Object object, Object[] values) {
97            EDIPartyName epn = (EDIPartyName) object;
98            values[0] = epn.nameAssigner;
99            values[1] = epn.partyName;
100        }
101    };
102}
103