summaryrefslogtreecommitdiff
path: root/bcprov/src/main/java/org/bouncycastle/asn1/x509/OtherName.java
blob: eb652f7f15d665c138595896ed319f70236bdd91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package org.bouncycastle.asn1.x509;

import org.bouncycastle.asn1.ASN1Encodable;
import org.bouncycastle.asn1.ASN1EncodableVector;
import org.bouncycastle.asn1.ASN1Object;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.ASN1TaggedObject;
import org.bouncycastle.asn1.DERSequence;
import org.bouncycastle.asn1.DERTaggedObject;

/**
 * The OtherName object.
 * <pre>
 * OtherName ::= SEQUENCE {
 *      type-id    OBJECT IDENTIFIER,
 *      value      [0] EXPLICIT ANY DEFINED BY type-id }
 * </pre>
 */
public class OtherName
    extends ASN1Object
{
    private final ASN1ObjectIdentifier typeID;
    private final ASN1Encodable value;

    /**
     * OtherName factory method.
     * @param obj the object used to construct an instance of <code>
     * OtherName</code>. It must be an instance of <code>OtherName
     * </code> or <code>ASN1Sequence</code>.
     * @return the instance of <code>OtherName</code> built from the
     * supplied object.
     * @throws java.lang.IllegalArgumentException if the object passed
     * to the factory is not an instance of <code>OtherName</code> or something that
     * can be converted into an appropriate <code>ASN1Sequence</code>.
     */
    public static OtherName getInstance(
        Object obj)
    {

        if (obj instanceof OtherName)
        {
            return (OtherName)obj;
        }
        else if (obj != null)
        {
            return new OtherName(ASN1Sequence.getInstance(obj));
        }

        return null;
    }

    /**
     * Base constructor.
     * @param typeID the type of the other name.
     * @param value the ANY object that represents the value.
     */
    public OtherName(
        ASN1ObjectIdentifier typeID,
        ASN1Encodable value)
    {
        this.typeID = typeID;
        this.value  = value;
    }

    private OtherName(ASN1Sequence seq)
    {
        this.typeID = ASN1ObjectIdentifier.getInstance(seq.getObjectAt(0));
        this.value = ASN1TaggedObject.getInstance(seq.getObjectAt(1)).getObject(); // explicitly tagged
    }

    public ASN1ObjectIdentifier getTypeID()
    {
        return typeID;
    }

    public ASN1Encodable getValue()
    {
        return value;
    }

    public ASN1Primitive toASN1Primitive()
    {
        ASN1EncodableVector v = new ASN1EncodableVector();

        v.add(typeID);
        v.add(new DERTaggedObject(true, 0, value));

        return new DERSequence(v);
    }
}