1b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampackage org.bouncycastle.asn1.pkcs;
2b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
3b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.ASN1EncodableVector;
44c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstromimport org.bouncycastle.asn1.ASN1Integer;
54c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstromimport org.bouncycastle.asn1.ASN1Object;
64c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstromimport org.bouncycastle.asn1.ASN1Primitive;
7b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.ASN1Sequence;
8b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.ASN1Set;
9b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.DERSequence;
10b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.DERTaggedObject;
116e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstromimport org.bouncycastle.asn1.x500.X500Name;
12b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
13b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.x509.X509Name;
14b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
15b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam/**
16b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * PKCS10 CertificationRequestInfo object.
17b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * <pre>
18b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam *  CertificationRequestInfo ::= SEQUENCE {
19b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam *   version             INTEGER { v1(0) } (v1,...),
20b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam *   subject             Name,
21b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam *   subjectPKInfo   SubjectPublicKeyInfo{{ PKInfoAlgorithms }},
22b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam *   attributes          [0] Attributes{{ CRIAttributes }}
23b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam *  }
24b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam *
25b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam *  Attributes { ATTRIBUTE:IOSet } ::= SET OF Attribute{{ IOSet }}
26b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam *
27b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam *  Attribute { ATTRIBUTE:IOSet } ::= SEQUENCE {
28b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam *    type    ATTRIBUTE.&id({IOSet}),
29b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam *    values  SET SIZE(1..MAX) OF ATTRIBUTE.&Type({IOSet}{\@type})
30b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam *  }
31b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * </pre>
32b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam */
33b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampublic class CertificationRequestInfo
344c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    extends ASN1Object
35b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam{
364c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    ASN1Integer              version = new ASN1Integer(0);
374c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    X500Name                subject;
38b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    SubjectPublicKeyInfo    subjectPKInfo;
39b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    ASN1Set                 attributes = null;
40b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
41b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public static CertificationRequestInfo getInstance(
42b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        Object  obj)
43b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
44b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (obj instanceof CertificationRequestInfo)
45b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
46b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            return (CertificationRequestInfo)obj;
47b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
484c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        else if (obj != null)
49b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
504c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom            return new CertificationRequestInfo(ASN1Sequence.getInstance(obj));
51b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
52b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
534c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        return null;
54b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
55b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
56b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public CertificationRequestInfo(
576e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        X500Name subject,
586e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        SubjectPublicKeyInfo    pkInfo,
596e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        ASN1Set                 attributes)
606e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom    {
614c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        this.subject = subject;
626e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        this.subjectPKInfo = pkInfo;
636e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        this.attributes = attributes;
646e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom
656e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        if ((subject == null) || (version == null) || (subjectPKInfo == null))
666e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        {
676e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom            throw new IllegalArgumentException("Not all mandatory fields set in CertificationRequestInfo generator.");
686e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        }
696e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom    }
706e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom
714c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    /**
724c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom     * @deprecated use X500Name method.
734c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom     */
746e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom    public CertificationRequestInfo(
75b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        X509Name                subject,
76b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        SubjectPublicKeyInfo    pkInfo,
77b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        ASN1Set                 attributes)
78b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
794c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        this.subject = X500Name.getInstance(subject.toASN1Primitive());
80b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.subjectPKInfo = pkInfo;
81b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.attributes = attributes;
82b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
83b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if ((subject == null) || (version == null) || (subjectPKInfo == null))
84b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
85b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            throw new IllegalArgumentException("Not all mandatory fields set in CertificationRequestInfo generator.");
86b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
87b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
88b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
89b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public CertificationRequestInfo(
90b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        ASN1Sequence  seq)
91b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
924c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        version = (ASN1Integer)seq.getObjectAt(0);
93b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
944c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        subject = X500Name.getInstance(seq.getObjectAt(1));
95b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        subjectPKInfo = SubjectPublicKeyInfo.getInstance(seq.getObjectAt(2));
96b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
97b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        //
98b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        // some CertificationRequestInfo objects seem to treat this field
99b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        // as optional.
100b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        //
101b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (seq.size() > 3)
102b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
103b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            DERTaggedObject tagobj = (DERTaggedObject)seq.getObjectAt(3);
104b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            attributes = ASN1Set.getInstance(tagobj, false);
105b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
106b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
107b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if ((subject == null) || (version == null) || (subjectPKInfo == null))
108b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
109b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            throw new IllegalArgumentException("Not all mandatory fields set in CertificationRequestInfo generator.");
110b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
111b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
112b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
1134c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    public ASN1Integer getVersion()
114b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
115b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return version;
116b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
117b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
1184c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    public X500Name getSubject()
119b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
120b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return subject;
121b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
122b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
123b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public SubjectPublicKeyInfo getSubjectPublicKeyInfo()
124b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
125b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return subjectPKInfo;
126b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
127b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
128b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public ASN1Set getAttributes()
129b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
130b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return attributes;
131b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
132b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
1334c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    public ASN1Primitive toASN1Primitive()
134b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
135b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        ASN1EncodableVector  v = new ASN1EncodableVector();
136b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
137b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        v.add(version);
138b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        v.add(subject);
139b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        v.add(subjectPKInfo);
140b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
141b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (attributes != null)
142b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
143b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            v.add(new DERTaggedObject(false, 0, attributes));
144b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
145b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
146b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return new DERSequence(v);
147b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
148b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam}
149