001: package org.bouncycastle.asn1.pkcs;
002:
003: import java.util.Enumeration;
004:
005: import org.bouncycastle.asn1.*;
006: import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
007:
008: /**
009: * a PKCS#7 signer info object.
010: */
011: public class SignerInfo extends ASN1Encodable {
012: private DERInteger version;
013: private IssuerAndSerialNumber issuerAndSerialNumber;
014: private AlgorithmIdentifier digAlgorithm;
015: private ASN1Set authenticatedAttributes;
016: private AlgorithmIdentifier digEncryptionAlgorithm;
017: private ASN1OctetString encryptedDigest;
018: private ASN1Set unauthenticatedAttributes;
019:
020: public static SignerInfo getInstance(Object o) {
021: if (o instanceof SignerInfo) {
022: return (SignerInfo) o;
023: } else if (o instanceof ASN1Sequence) {
024: return new SignerInfo((ASN1Sequence) o);
025: }
026:
027: throw new IllegalArgumentException("unknown object in factory");
028: }
029:
030: public SignerInfo(DERInteger version,
031: IssuerAndSerialNumber issuerAndSerialNumber,
032: AlgorithmIdentifier digAlgorithm,
033: ASN1Set authenticatedAttributes,
034: AlgorithmIdentifier digEncryptionAlgorithm,
035: ASN1OctetString encryptedDigest,
036: ASN1Set unauthenticatedAttributes) {
037: this .version = version;
038: this .issuerAndSerialNumber = issuerAndSerialNumber;
039: this .digAlgorithm = digAlgorithm;
040: this .authenticatedAttributes = authenticatedAttributes;
041: this .digEncryptionAlgorithm = digEncryptionAlgorithm;
042: this .encryptedDigest = encryptedDigest;
043: this .unauthenticatedAttributes = unauthenticatedAttributes;
044: }
045:
046: public SignerInfo(ASN1Sequence seq) {
047: Enumeration e = seq.getObjects();
048:
049: version = (DERInteger) e.nextElement();
050: issuerAndSerialNumber = IssuerAndSerialNumber.getInstance(e
051: .nextElement());
052: digAlgorithm = AlgorithmIdentifier.getInstance(e.nextElement());
053:
054: Object obj = e.nextElement();
055:
056: if (obj instanceof ASN1TaggedObject) {
057: authenticatedAttributes = ASN1Set.getInstance(
058: (ASN1TaggedObject) obj, false);
059:
060: digEncryptionAlgorithm = AlgorithmIdentifier.getInstance(e
061: .nextElement());
062: } else {
063: authenticatedAttributes = null;
064: digEncryptionAlgorithm = AlgorithmIdentifier
065: .getInstance(obj);
066: }
067:
068: encryptedDigest = DEROctetString.getInstance(e.nextElement());
069:
070: if (e.hasMoreElements()) {
071: unauthenticatedAttributes = ASN1Set.getInstance(
072: (ASN1TaggedObject) e.nextElement(), false);
073: } else {
074: unauthenticatedAttributes = null;
075: }
076: }
077:
078: public DERInteger getVersion() {
079: return version;
080: }
081:
082: public IssuerAndSerialNumber getIssuerAndSerialNumber() {
083: return issuerAndSerialNumber;
084: }
085:
086: public ASN1Set getAuthenticatedAttributes() {
087: return authenticatedAttributes;
088: }
089:
090: public AlgorithmIdentifier getDigestAlgorithm() {
091: return digAlgorithm;
092: }
093:
094: public ASN1OctetString getEncryptedDigest() {
095: return encryptedDigest;
096: }
097:
098: public AlgorithmIdentifier getDigestEncryptionAlgorithm() {
099: return digEncryptionAlgorithm;
100: }
101:
102: public ASN1Set getUnauthenticatedAttributes() {
103: return unauthenticatedAttributes;
104: }
105:
106: /**
107: * Produce an object suitable for an ASN1OutputStream.
108: * <pre>
109: * SignerInfo ::= SEQUENCE {
110: * version Version,
111: * issuerAndSerialNumber IssuerAndSerialNumber,
112: * digestAlgorithm DigestAlgorithmIdentifier,
113: * authenticatedAttributes [0] IMPLICIT Attributes OPTIONAL,
114: * digestEncryptionAlgorithm DigestEncryptionAlgorithmIdentifier,
115: * encryptedDigest EncryptedDigest,
116: * unauthenticatedAttributes [1] IMPLICIT Attributes OPTIONAL
117: * }
118: *
119: * EncryptedDigest ::= OCTET STRING
120: *
121: * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
122: *
123: * DigestEncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
124: * </pre>
125: */
126: public DERObject toASN1Object() {
127: ASN1EncodableVector v = new ASN1EncodableVector();
128:
129: v.add(version);
130: v.add(issuerAndSerialNumber);
131: v.add(digAlgorithm);
132:
133: if (authenticatedAttributes != null) {
134: v
135: .add(new DERTaggedObject(false, 0,
136: authenticatedAttributes));
137: }
138:
139: v.add(digEncryptionAlgorithm);
140: v.add(encryptedDigest);
141:
142: if (unauthenticatedAttributes != null) {
143: v.add(new DERTaggedObject(false, 1,
144: unauthenticatedAttributes));
145: }
146:
147: return new DERSequence(v);
148: }
149: }
|