Home | History | Annotate | Download | only in ocsp
      1 package org.bouncycastle.cert.ocsp;
      2 
      3 import java.io.OutputStream;
      4 
      5 import org.bouncycastle.asn1.DERNull;
      6 import org.bouncycastle.asn1.DEROctetString;
      7 import org.bouncycastle.asn1.ocsp.ResponderID;
      8 import org.bouncycastle.asn1.oiw.OIWObjectIdentifiers;
      9 import org.bouncycastle.asn1.x500.X500Name;
     10 import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
     11 import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
     12 import org.bouncycastle.operator.DigestCalculator;
     13 
     14 /**
     15  * Carrier for a ResponderID.
     16  */
     17 public class RespID
     18 {
     19     public static final AlgorithmIdentifier HASH_SHA1 = new AlgorithmIdentifier(OIWObjectIdentifiers.idSHA1, DERNull.INSTANCE);
     20 
     21     ResponderID id;
     22 
     23     public RespID(
     24         ResponderID id)
     25     {
     26         this.id = id;
     27     }
     28 
     29     public RespID(
     30         X500Name name)
     31     {
     32         this.id = new ResponderID(name);
     33     }
     34 
     35     /**
     36      * Calculate a RespID based on the public key of the responder.
     37      *
     38      * @param subjectPublicKeyInfo the info structure for the responder public key.
     39      * @param digCalc a SHA-1 digest calculator.
     40      * @throws OCSPException on exception creating ID.
     41      */
     42     public RespID(
     43         SubjectPublicKeyInfo     subjectPublicKeyInfo,
     44         DigestCalculator         digCalc)
     45         throws OCSPException
     46     {
     47         try
     48         {
     49             if (!digCalc.getAlgorithmIdentifier().equals(HASH_SHA1))
     50             {
     51                 throw new IllegalArgumentException("only SHA-1 can be used with RespID");
     52             }
     53 
     54             OutputStream     digOut = digCalc.getOutputStream();
     55 
     56             digOut.write(subjectPublicKeyInfo.getPublicKeyData().getBytes());
     57             digOut.close();
     58 
     59             this.id = new ResponderID(new DEROctetString(digCalc.getDigest()));
     60         }
     61         catch (Exception e)
     62         {
     63             throw new OCSPException("problem creating ID: " + e, e);
     64         }
     65     }
     66 
     67     public ResponderID toASN1Primitive()
     68     {
     69         return id;
     70     }
     71 
     72     public boolean equals(
     73         Object  o)
     74     {
     75         if (!(o instanceof RespID))
     76         {
     77             return false;
     78         }
     79 
     80         RespID obj = (RespID)o;
     81 
     82         return id.equals(obj.id);
     83     }
     84 
     85     public int hashCode()
     86     {
     87         return id.hashCode();
     88     }
     89 }
     90