1b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampackage org.bouncycastle.x509;
2b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
36e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstromimport java.io.IOException;
46e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstromimport java.security.Principal;
56e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstromimport java.security.cert.CertSelector;
66e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstromimport java.security.cert.Certificate;
76e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstromimport java.security.cert.X509Certificate;
86e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstromimport java.util.ArrayList;
96e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstromimport java.util.List;
106e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom
116e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstromimport javax.security.auth.x500.X500Principal;
126e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom
13b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.ASN1Encodable;
14b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.DERSequence;
15b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.x509.AttCertIssuer;
16b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.x509.GeneralName;
17b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.x509.GeneralNames;
18b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.x509.V2Form;
19b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.jce.X509Principal;
20c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstromimport org.bouncycastle.util.Selector;
21c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
22b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam/**
23b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * Carrying class for an attribute certificate issuer.
246e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom * @deprecated use org.bouncycastle.cert.AttributeCertificateIssuer
25b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam */
26b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampublic class AttributeCertificateIssuer
27c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    implements CertSelector, Selector
28b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam{
29c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    final ASN1Encodable form;
30c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
31b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
32c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     * Set the issuer directly with the ASN.1 structure.
33c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     *
34c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     * @param issuer The issuer
35b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
36c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    public AttributeCertificateIssuer(AttCertIssuer issuer)
37b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
38b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        form = issuer.getIssuer();
39b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
40b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
41c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    public AttributeCertificateIssuer(X500Principal principal)
42c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        throws IOException
43c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    {
44b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this(new X509Principal(principal.getEncoded()));
45b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
46c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
47c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    public AttributeCertificateIssuer(X509Principal principal)
48c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    {
494c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        form = new V2Form(GeneralNames.getInstance(new DERSequence(new GeneralName(principal))));
50b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
51c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
52b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    private Object[] getNames()
53b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
54c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        GeneralNames name;
55c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
56b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (form instanceof V2Form)
57b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
58b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            name = ((V2Form)form).getIssuerName();
59b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
60b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        else
61b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
62b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            name = (GeneralNames)form;
63b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
64c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
65c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        GeneralName[] names = name.getNames();
66c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
67c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        List l = new ArrayList(names.length);
68c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
69b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        for (int i = 0; i != names.length; i++)
70b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
71b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            if (names[i].getTagNo() == GeneralName.directoryName)
72b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            {
73b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam                try
74b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam                {
75c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                    l.add(new X500Principal(
764c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom                        ((ASN1Encodable)names[i].getName()).toASN1Primitive().getEncoded()));
77b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam                }
78b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam                catch (IOException e)
79b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam                {
80b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam                    throw new RuntimeException("badly formed Name object");
81b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam                }
82b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            }
83b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
84c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
85b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return l.toArray(new Object[l.size()]);
86b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
87c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
88b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
89c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     * Return any principal objects inside the attribute certificate issuer
90c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     * object.
91b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *
92b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @return an array of Principal objects (usually X500Principal)
93b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
94b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public Principal[] getPrincipals()
95b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
96c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        Object[] p = this.getNames();
97c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        List l = new ArrayList();
98c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
99b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        for (int i = 0; i != p.length; i++)
100b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
101b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            if (p[i] instanceof Principal)
102b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            {
103b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam                l.add(p[i]);
104b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            }
105b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
106b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
107b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return (Principal[])l.toArray(new Principal[l.size()]);
108b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
109c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
110b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    private boolean matchesDN(X500Principal subject, GeneralNames targets)
111b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
112c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        GeneralName[] names = targets.getNames();
113b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
114b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        for (int i = 0; i != names.length; i++)
115b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
116b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            GeneralName gn = names[i];
117b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
118b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            if (gn.getTagNo() == GeneralName.directoryName)
119b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            {
120b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam                try
121b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam                {
1224c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom                    if (new X500Principal(((ASN1Encodable)gn.getName()).toASN1Primitive().getEncoded()).equals(subject))
123b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam                    {
124b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam                        return true;
125b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam                    }
126b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam                }
127b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam                catch (IOException e)
128b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam                {
129b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam                }
130b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            }
131b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
132b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
133b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return false;
134b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
135c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
136b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public Object clone()
137b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
138b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return new AttributeCertificateIssuer(AttCertIssuer.getInstance(form));
139b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
140c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
141b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public boolean match(Certificate cert)
142b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
143b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (!(cert instanceof X509Certificate))
144b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
145b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            return false;
146b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
147c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
148b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        X509Certificate x509Cert = (X509Certificate)cert;
149c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
150b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (form instanceof V2Form)
151b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
152b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            V2Form issuer = (V2Form)form;
153b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            if (issuer.getBaseCertificateID() != null)
154b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            {
155b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam                return issuer.getBaseCertificateID().getSerial().getValue().equals(x509Cert.getSerialNumber())
156b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam                    && matchesDN(x509Cert.getIssuerX500Principal(), issuer.getBaseCertificateID().getIssuer());
157b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            }
158c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
159b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            GeneralNames name = issuer.getIssuerName();
160b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            if (matchesDN(x509Cert.getSubjectX500Principal(), name))
161b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            {
162b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam                return true;
163b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            }
164b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
165b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        else
166b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
167b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            GeneralNames name = (GeneralNames)form;
168b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            if (matchesDN(x509Cert.getSubjectX500Principal(), name))
169b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            {
170b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam                return true;
171b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            }
172b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
173b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
174b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return false;
175b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
176c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
177c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    public boolean equals(Object obj)
178c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    {
179c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        if (obj == this)
180c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        {
181c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            return true;
182c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        }
183c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
184c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        if (!(obj instanceof AttributeCertificateIssuer))
185c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        {
186c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            return false;
187c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        }
188c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
189c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        AttributeCertificateIssuer other = (AttributeCertificateIssuer)obj;
190c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
191c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        return this.form.equals(other.form);
192c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    }
193c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
194c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    public int hashCode()
195c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    {
196c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        return this.form.hashCode();
197c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    }
198c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
199c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    public boolean match(Object obj)
200c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    {
201c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        if (!(obj instanceof X509Certificate))
202c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        {
203c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            return false;
204c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        }
205c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
206c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        return match((Certificate)obj);
207c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    }
208b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam}
209