001: package org.bouncycastle.asn1.ess;
002:
003: import org.bouncycastle.asn1.ASN1Encodable;
004: import org.bouncycastle.asn1.ASN1EncodableVector;
005: import org.bouncycastle.asn1.ASN1OctetString;
006: import org.bouncycastle.asn1.ASN1Sequence;
007: import org.bouncycastle.asn1.DERObject;
008: import org.bouncycastle.asn1.DERSequence;
009: import org.bouncycastle.asn1.x509.IssuerSerial;
010: import org.bouncycastle.asn1.x509.DigestInfo;
011: import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
012:
013: public class OtherCertID extends ASN1Encodable {
014: private ASN1Encodable otherCertHash;
015: private IssuerSerial issuerSerial;
016:
017: public static OtherCertID getInstance(Object o) {
018: if (o == null || o instanceof OtherCertID) {
019: return (OtherCertID) o;
020: } else if (o instanceof ASN1Sequence) {
021: return new OtherCertID((ASN1Sequence) o);
022: }
023:
024: throw new IllegalArgumentException(
025: "unknown object in 'OtherCertID' factory : "
026: + o.getClass().getName() + ".");
027: }
028:
029: /**
030: * constructor
031: */
032: public OtherCertID(ASN1Sequence seq) {
033: if (seq.size() < 1 || seq.size() > 2) {
034: throw new IllegalArgumentException("Bad sequence size: "
035: + seq.size());
036: }
037:
038: if (seq.getObjectAt(0).getDERObject() instanceof ASN1OctetString) {
039: otherCertHash = ASN1OctetString.getInstance(seq
040: .getObjectAt(0));
041: } else {
042: otherCertHash = DigestInfo.getInstance(seq.getObjectAt(0));
043:
044: }
045:
046: if (seq.size() > 1) {
047: issuerSerial = new IssuerSerial(ASN1Sequence
048: .getInstance(seq.getObjectAt(1)));
049: }
050: }
051:
052: public OtherCertID(AlgorithmIdentifier algId, byte[] digest) {
053: this .otherCertHash = new DigestInfo(algId, digest);
054: }
055:
056: public OtherCertID(AlgorithmIdentifier algId, byte[] digest,
057: IssuerSerial issuerSerial) {
058: this .otherCertHash = new DigestInfo(algId, digest);
059: this .issuerSerial = issuerSerial;
060: }
061:
062: public AlgorithmIdentifier getAlgorithmHash() {
063: if (otherCertHash.getDERObject() instanceof ASN1OctetString) {
064: // SHA-1
065: return new AlgorithmIdentifier("1.3.14.3.2.26");
066: } else {
067: return DigestInfo.getInstance(otherCertHash)
068: .getAlgorithmId();
069: }
070: }
071:
072: public byte[] getCertHash() {
073: if (otherCertHash.getDERObject() instanceof ASN1OctetString) {
074: // SHA-1
075: return ((ASN1OctetString) otherCertHash.getDERObject())
076: .getOctets();
077: } else {
078: return DigestInfo.getInstance(otherCertHash).getDigest();
079: }
080: }
081:
082: public IssuerSerial getIssuerSerial() {
083: return issuerSerial;
084: }
085:
086: /**
087: * <pre>
088: * OtherCertID ::= SEQUENCE {
089: * otherCertHash OtherHash,
090: * issuerSerial IssuerSerial OPTIONAL }
091: *
092: * OtherHash ::= CHOICE {
093: * sha1Hash OCTET STRING,
094: * otherHash OtherHashAlgAndValue }
095: *
096: * OtherHashAlgAndValue ::= SEQUENCE {
097: * hashAlgorithm AlgorithmIdentifier,
098: * hashValue OCTET STRING }
099: *
100: * </pre>
101: */
102: public DERObject toASN1Object() {
103: ASN1EncodableVector v = new ASN1EncodableVector();
104:
105: v.add(otherCertHash);
106:
107: if (issuerSerial != null) {
108: v.add(issuerSerial);
109: }
110:
111: return new DERSequence(v);
112: }
113: }
|