1b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampackage org.bouncycastle.asn1.x509;
2b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
370c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstromimport java.math.BigInteger;
470c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom
5b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.ASN1EncodableVector;
64c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstromimport org.bouncycastle.asn1.ASN1Integer;
74c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstromimport org.bouncycastle.asn1.ASN1Object;
84c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstromimport org.bouncycastle.asn1.ASN1Primitive;
9b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.ASN1Sequence;
10b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.ASN1TaggedObject;
11b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.DERBitString;
12b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.DERSequence;
135db505e1f6a68c8d5dfdb0fed0b8607dea7bed96Kenny Rootimport org.bouncycastle.asn1.x500.X500Name;
14b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
15b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampublic class IssuerSerial
164c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    extends ASN1Object
17b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam{
18b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    GeneralNames            issuer;
194c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    ASN1Integer              serial;
20b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    DERBitString            issuerUID;
21b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
22b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public static IssuerSerial getInstance(
23b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            Object  obj)
24b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
2570c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom        if (obj instanceof IssuerSerial)
26b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
27b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            return (IssuerSerial)obj;
28b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
29b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
3070c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom        if (obj != null)
31b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
3270c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom            return new IssuerSerial(ASN1Sequence.getInstance(obj));
33b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
34b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
3570c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom        return null;
36b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
37b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
38b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public static IssuerSerial getInstance(
39b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        ASN1TaggedObject obj,
40b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        boolean          explicit)
41b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
42b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return getInstance(ASN1Sequence.getInstance(obj, explicit));
43b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
44b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
4570c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom    private IssuerSerial(
46b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        ASN1Sequence    seq)
47b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
48b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (seq.size() != 2 && seq.size() != 3)
49b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
50b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            throw new IllegalArgumentException("Bad sequence size: " + seq.size());
51b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
52b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
53b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        issuer = GeneralNames.getInstance(seq.getObjectAt(0));
544c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        serial = ASN1Integer.getInstance(seq.getObjectAt(1));
55b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
56b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (seq.size() == 3)
57b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
58b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            issuerUID = DERBitString.getInstance(seq.getObjectAt(2));
59b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
60b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
6170c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom
6270c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom    public IssuerSerial(
635db505e1f6a68c8d5dfdb0fed0b8607dea7bed96Kenny Root        X500Name   issuer,
645db505e1f6a68c8d5dfdb0fed0b8607dea7bed96Kenny Root        BigInteger serial)
655db505e1f6a68c8d5dfdb0fed0b8607dea7bed96Kenny Root    {
665db505e1f6a68c8d5dfdb0fed0b8607dea7bed96Kenny Root        this(new GeneralNames(new GeneralName(issuer)), new ASN1Integer(serial));
675db505e1f6a68c8d5dfdb0fed0b8607dea7bed96Kenny Root    }
685db505e1f6a68c8d5dfdb0fed0b8607dea7bed96Kenny Root
695db505e1f6a68c8d5dfdb0fed0b8607dea7bed96Kenny Root    public IssuerSerial(
7070c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom        GeneralNames    issuer,
7170c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom        BigInteger serial)
7270c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom    {
7370c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom        this(issuer, new ASN1Integer(serial));
7470c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom    }
7570c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom
76b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public IssuerSerial(
77b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        GeneralNames    issuer,
784c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        ASN1Integer      serial)
79b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
80b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.issuer = issuer;
81b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.serial = serial;
82b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
83b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
84b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public GeneralNames getIssuer()
85b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
86b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return issuer;
87b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
88b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
894c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    public ASN1Integer getSerial()
90b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
91b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return serial;
92b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
93b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
94b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public DERBitString getIssuerUID()
95b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
96b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return issuerUID;
97b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
98b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
99b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
100b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * Produce an object suitable for an ASN1OutputStream.
101b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * <pre>
102b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *  IssuerSerial  ::=  SEQUENCE {
103b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *       issuer         GeneralNames,
104b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *       serial         CertificateSerialNumber,
105b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *       issuerUID      UniqueIdentifier OPTIONAL
106b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *  }
107b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * </pre>
108b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
1094c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    public ASN1Primitive toASN1Primitive()
110b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
111b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        ASN1EncodableVector  v = new ASN1EncodableVector();
112b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
113b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        v.add(issuer);
114b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        v.add(serial);
115b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
116b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (issuerUID != null)
117b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
118b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            v.add(issuerUID);
119b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
120b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
121b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return new DERSequence(v);
122b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
123b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam}
124