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