Home | History | Annotate | Download | only in cert
      1 package org.bouncycastle.cert;
      2 
      3 import java.util.ArrayList;
      4 import java.util.List;
      5 
      6 import org.bouncycastle.asn1.ASN1Encodable;
      7 import org.bouncycastle.asn1.x500.X500Name;
      8 import org.bouncycastle.asn1.x509.AttCertIssuer;
      9 import org.bouncycastle.asn1.x509.GeneralName;
     10 import org.bouncycastle.asn1.x509.GeneralNames;
     11 import org.bouncycastle.asn1.x509.V2Form;
     12 import org.bouncycastle.util.Selector;
     13 
     14 /**
     15  * Carrying class for an attribute certificate issuer.
     16  */
     17 public class AttributeCertificateIssuer
     18     implements Selector
     19 {
     20     final ASN1Encodable form;
     21 
     22     /**
     23      * Set the issuer directly with the ASN.1 structure.
     24      *
     25      * @param issuer The issuer
     26      */
     27     public AttributeCertificateIssuer(AttCertIssuer issuer)
     28     {
     29         form = issuer.getIssuer();
     30     }
     31 
     32     public AttributeCertificateIssuer(X500Name principal)
     33     {
     34         form = new V2Form(new GeneralNames(new GeneralName(principal)));
     35     }
     36 
     37     public X500Name[] getNames()
     38     {
     39         GeneralNames name;
     40 
     41         if (form instanceof V2Form)
     42         {
     43             name = ((V2Form)form).getIssuerName();
     44         }
     45         else
     46         {
     47             name = (GeneralNames)form;
     48         }
     49 
     50         GeneralName[] names = name.getNames();
     51 
     52         List l = new ArrayList(names.length);
     53 
     54         for (int i = 0; i != names.length; i++)
     55         {
     56             if (names[i].getTagNo() == GeneralName.directoryName)
     57             {
     58                 l.add(X500Name.getInstance(names[i].getName()));
     59             }
     60         }
     61 
     62         return (X500Name[])l.toArray(new X500Name[l.size()]);
     63     }
     64 
     65     private boolean matchesDN(X500Name subject, GeneralNames targets)
     66     {
     67         GeneralName[] names = targets.getNames();
     68 
     69         for (int i = 0; i != names.length; i++)
     70         {
     71             GeneralName gn = names[i];
     72 
     73             if (gn.getTagNo() == GeneralName.directoryName)
     74             {
     75                 if (X500Name.getInstance(gn.getName()).equals(subject))
     76                 {
     77                     return true;
     78                 }
     79             }
     80         }
     81 
     82         return false;
     83     }
     84 
     85     public Object clone()
     86     {
     87         return new AttributeCertificateIssuer(AttCertIssuer.getInstance(form));
     88     }
     89 
     90     public boolean equals(Object obj)
     91     {
     92         if (obj == this)
     93         {
     94             return true;
     95         }
     96 
     97         if (!(obj instanceof AttributeCertificateIssuer))
     98         {
     99             return false;
    100         }
    101 
    102         AttributeCertificateIssuer other = (AttributeCertificateIssuer)obj;
    103 
    104         return this.form.equals(other.form);
    105     }
    106 
    107     public int hashCode()
    108     {
    109         return this.form.hashCode();
    110     }
    111 
    112     public boolean match(Object obj)
    113     {
    114         if (!(obj instanceof X509CertificateHolder))
    115         {
    116             return false;
    117         }
    118 
    119         X509CertificateHolder x509Cert = (X509CertificateHolder)obj;
    120 
    121         if (form instanceof V2Form)
    122         {
    123             V2Form issuer = (V2Form)form;
    124             if (issuer.getBaseCertificateID() != null)
    125             {
    126                 return issuer.getBaseCertificateID().getSerial().getValue().equals(x509Cert.getSerialNumber())
    127                     && matchesDN(x509Cert.getIssuer(), issuer.getBaseCertificateID().getIssuer());
    128             }
    129 
    130             GeneralNames name = issuer.getIssuerName();
    131             if (matchesDN(x509Cert.getSubject(), name))
    132             {
    133                 return true;
    134             }
    135         }
    136         else
    137         {
    138             GeneralNames name = (GeneralNames)form;
    139             if (matchesDN(x509Cert.getSubject(), name))
    140             {
    141                 return true;
    142             }
    143         }
    144 
    145         return false;
    146     }
    147 }
    148