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