001: package org.bouncycastle.ocsp;
002:
003: import org.bouncycastle.asn1.ASN1InputStream;
004: import org.bouncycastle.asn1.ASN1OctetString;
005: import org.bouncycastle.asn1.DERInteger;
006: import org.bouncycastle.asn1.DERNull;
007: import org.bouncycastle.asn1.DERObjectIdentifier;
008: import org.bouncycastle.asn1.DEROctetString;
009: import org.bouncycastle.asn1.ocsp.CertID;
010: import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
011: import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
012: import org.bouncycastle.jce.PrincipalUtil;
013: import org.bouncycastle.jce.X509Principal;
014:
015: import java.math.BigInteger;
016: import java.security.MessageDigest;
017: import java.security.PublicKey;
018: import java.security.cert.X509Certificate;
019:
020: public class CertificateID {
021: public static final String HASH_SHA1 = "1.3.14.3.2.26";
022:
023: private CertID id;
024:
025: public CertificateID(CertID id) {
026: this .id = id;
027: }
028:
029: /**
030: * create from an issuer certificate and the serial number of the
031: * certificate it signed.
032: * @exception OCSPException if any problems occur creating the id fields.
033: */
034: public CertificateID(String hashAlgorithm,
035: X509Certificate issuerCert, BigInteger number,
036: String provider) throws OCSPException {
037: try {
038: MessageDigest digest = MessageDigest.getInstance(
039: hashAlgorithm, provider);
040: AlgorithmIdentifier hashAlg = new AlgorithmIdentifier(
041: new DERObjectIdentifier(hashAlgorithm),
042: new DERNull());
043:
044: X509Principal issuerName = PrincipalUtil
045: .getSubjectX509Principal(issuerCert);
046:
047: digest.update(issuerName.getEncoded());
048:
049: ASN1OctetString issuerNameHash = new DEROctetString(digest
050: .digest());
051: PublicKey issuerKey = issuerCert.getPublicKey();
052:
053: ASN1InputStream aIn = new ASN1InputStream(issuerKey
054: .getEncoded());
055: SubjectPublicKeyInfo info = SubjectPublicKeyInfo
056: .getInstance(aIn.readObject());
057:
058: digest.update(info.getPublicKeyData().getBytes());
059:
060: ASN1OctetString issuerKeyHash = new DEROctetString(digest
061: .digest());
062:
063: DERInteger serialNumber = new DERInteger(number);
064:
065: this .id = new CertID(hashAlg, issuerNameHash,
066: issuerKeyHash, serialNumber);
067: } catch (Exception e) {
068: throw new OCSPException("problem creating ID: " + e, e);
069: }
070: }
071:
072: /**
073: * create using the BC provider
074: */
075: public CertificateID(String hashAlgorithm,
076: X509Certificate issuerCert, BigInteger number)
077: throws OCSPException {
078: this (hashAlgorithm, issuerCert, number, "BC");
079: }
080:
081: public String getHashAlgOID() {
082: return id.getHashAlgorithm().getObjectId().getId();
083: }
084:
085: public byte[] getIssuerNameHash() {
086: return id.getIssuerNameHash().getOctets();
087: }
088:
089: public byte[] getIssuerKeyHash() {
090: return id.getIssuerKeyHash().getOctets();
091: }
092:
093: /**
094: * return the serial number for the certificate associated
095: * with this request.
096: */
097: public BigInteger getSerialNumber() {
098: return id.getSerialNumber().getValue();
099: }
100:
101: public CertID toASN1Object() {
102: return id;
103: }
104:
105: public boolean equals(Object o) {
106: if (!(o instanceof CertificateID)) {
107: return false;
108: }
109:
110: CertificateID obj = (CertificateID) o;
111:
112: return id.getDERObject().equals(obj.id.getDERObject());
113: }
114:
115: public int hashCode() {
116: return id.getDERObject().hashCode();
117: }
118: }
|