SignerInfo.java revision e6bf3e8dfa2804891a82075cb469b736321b4827
1package org.bouncycastle.asn1.cms; 2 3import java.util.Enumeration; 4 5import org.bouncycastle.asn1.ASN1EncodableVector; 6import org.bouncycastle.asn1.ASN1Integer; 7import org.bouncycastle.asn1.ASN1Object; 8import org.bouncycastle.asn1.ASN1OctetString; 9import org.bouncycastle.asn1.ASN1Primitive; 10import org.bouncycastle.asn1.ASN1Sequence; 11import org.bouncycastle.asn1.ASN1Set; 12import org.bouncycastle.asn1.ASN1TaggedObject; 13import org.bouncycastle.asn1.DEROctetString; 14import org.bouncycastle.asn1.DERSequence; 15import org.bouncycastle.asn1.DERTaggedObject; 16import org.bouncycastle.asn1.x509.AlgorithmIdentifier; 17 18public class SignerInfo 19 extends ASN1Object 20{ 21 private ASN1Integer version; 22 private SignerIdentifier sid; 23 private AlgorithmIdentifier digAlgorithm; 24 private ASN1Set authenticatedAttributes; 25 private AlgorithmIdentifier digEncryptionAlgorithm; 26 private ASN1OctetString encryptedDigest; 27 private ASN1Set unauthenticatedAttributes; 28 29 public static SignerInfo getInstance( 30 Object o) 31 throws IllegalArgumentException 32 { 33 if (o == null || o instanceof SignerInfo) 34 { 35 return (SignerInfo)o; 36 } 37 else if (o instanceof ASN1Sequence) 38 { 39 return new SignerInfo((ASN1Sequence)o); 40 } 41 42 throw new IllegalArgumentException("unknown object in factory: " + o.getClass().getName()); 43 } 44 45 public SignerInfo( 46 SignerIdentifier sid, 47 AlgorithmIdentifier digAlgorithm, 48 ASN1Set authenticatedAttributes, 49 AlgorithmIdentifier digEncryptionAlgorithm, 50 ASN1OctetString encryptedDigest, 51 ASN1Set unauthenticatedAttributes) 52 { 53 if (sid.isTagged()) 54 { 55 this.version = new ASN1Integer(3); 56 } 57 else 58 { 59 this.version = new ASN1Integer(1); 60 } 61 62 this.sid = sid; 63 this.digAlgorithm = digAlgorithm; 64 this.authenticatedAttributes = authenticatedAttributes; 65 this.digEncryptionAlgorithm = digEncryptionAlgorithm; 66 this.encryptedDigest = encryptedDigest; 67 this.unauthenticatedAttributes = unauthenticatedAttributes; 68 } 69 70 public SignerInfo( 71 ASN1Sequence seq) 72 { 73 Enumeration e = seq.getObjects(); 74 75 version = (ASN1Integer)e.nextElement(); 76 sid = SignerIdentifier.getInstance(e.nextElement()); 77 digAlgorithm = AlgorithmIdentifier.getInstance(e.nextElement()); 78 79 Object obj = e.nextElement(); 80 81 if (obj instanceof ASN1TaggedObject) 82 { 83 authenticatedAttributes = ASN1Set.getInstance((ASN1TaggedObject)obj, false); 84 85 digEncryptionAlgorithm = AlgorithmIdentifier.getInstance(e.nextElement()); 86 } 87 else 88 { 89 authenticatedAttributes = null; 90 digEncryptionAlgorithm = AlgorithmIdentifier.getInstance(obj); 91 } 92 93 encryptedDigest = DEROctetString.getInstance(e.nextElement()); 94 95 if (e.hasMoreElements()) 96 { 97 unauthenticatedAttributes = ASN1Set.getInstance((ASN1TaggedObject)e.nextElement(), false); 98 } 99 else 100 { 101 unauthenticatedAttributes = null; 102 } 103 } 104 105 public ASN1Integer getVersion() 106 { 107 return version; 108 } 109 110 public SignerIdentifier getSID() 111 { 112 return sid; 113 } 114 115 public ASN1Set getAuthenticatedAttributes() 116 { 117 return authenticatedAttributes; 118 } 119 120 public AlgorithmIdentifier getDigestAlgorithm() 121 { 122 return digAlgorithm; 123 } 124 125 public ASN1OctetString getEncryptedDigest() 126 { 127 return encryptedDigest; 128 } 129 130 public AlgorithmIdentifier getDigestEncryptionAlgorithm() 131 { 132 return digEncryptionAlgorithm; 133 } 134 135 public ASN1Set getUnauthenticatedAttributes() 136 { 137 return unauthenticatedAttributes; 138 } 139 140 /** 141 * Produce an object suitable for an ASN1OutputStream. 142 * <pre> 143 * SignerInfo ::= SEQUENCE { 144 * version Version, 145 * SignerIdentifier sid, 146 * digestAlgorithm DigestAlgorithmIdentifier, 147 * authenticatedAttributes [0] IMPLICIT Attributes OPTIONAL, 148 * digestEncryptionAlgorithm DigestEncryptionAlgorithmIdentifier, 149 * encryptedDigest EncryptedDigest, 150 * unauthenticatedAttributes [1] IMPLICIT Attributes OPTIONAL 151 * } 152 * 153 * EncryptedDigest ::= OCTET STRING 154 * 155 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier 156 * 157 * DigestEncryptionAlgorithmIdentifier ::= AlgorithmIdentifier 158 * </pre> 159 */ 160 public ASN1Primitive toASN1Primitive() 161 { 162 ASN1EncodableVector v = new ASN1EncodableVector(); 163 164 v.add(version); 165 v.add(sid); 166 v.add(digAlgorithm); 167 168 if (authenticatedAttributes != null) 169 { 170 v.add(new DERTaggedObject(false, 0, authenticatedAttributes)); 171 } 172 173 v.add(digEncryptionAlgorithm); 174 v.add(encryptedDigest); 175 176 if (unauthenticatedAttributes != null) 177 { 178 v.add(new DERTaggedObject(false, 1, unauthenticatedAttributes)); 179 } 180 181 return new DERSequence(v); 182 } 183} 184