SignerInfo.java
上传用户:lior1029
上传日期:2013-05-07
资源大小:209k
文件大小:5k
源码类别:

CA认证

开发平台:

Java

  1. package org.bouncycastle.asn1.cms;
  2. import java.util.Enumeration;
  3. import org.bouncycastle.asn1.*;
  4. import org.bouncycastle.asn1.x509.*;
  5. public class SignerInfo
  6.     implements DEREncodable
  7. {
  8.     private DERInteger              version;
  9.     private SignerIdentifier        sid;
  10.     private AlgorithmIdentifier     digAlgorithm;
  11.     private ASN1Set                 authenticatedAttributes;
  12.     private AlgorithmIdentifier     digEncryptionAlgorithm;
  13.     private ASN1OctetString         encryptedDigest;
  14.     private ASN1Set                 unauthenticatedAttributes;
  15.     public static SignerInfo getInstance(
  16.         Object  o)
  17.     {
  18.         if (o == null || o instanceof SignerInfo)
  19.         {
  20.             return (SignerInfo)o;
  21.         }
  22.         else if (o instanceof ASN1Sequence)
  23.         {
  24.             return new SignerInfo((ASN1Sequence)o);
  25.         }
  26.         throw new IllegalArgumentException("unknown object in factory");
  27.     }
  28.     public SignerInfo(
  29.         SignerIdentifier        sid,
  30.         AlgorithmIdentifier     digAlgorithm,
  31.         ASN1Set                 authenticatedAttributes,
  32.         AlgorithmIdentifier     digEncryptionAlgorithm,
  33.         ASN1OctetString         encryptedDigest,
  34.         ASN1Set                 unauthenticatedAttributes)
  35.     {
  36.         if (sid.isTagged())
  37.         {
  38.             this.version = new DERInteger(3);
  39.         }
  40.         else
  41.         {
  42.             this.version = new DERInteger(1);
  43.         }
  44.         this.sid = sid;
  45.         this.digAlgorithm = digAlgorithm;
  46.         this.authenticatedAttributes = authenticatedAttributes;
  47.         this.digEncryptionAlgorithm = digEncryptionAlgorithm;
  48.         this.encryptedDigest = encryptedDigest;
  49.         this.unauthenticatedAttributes = unauthenticatedAttributes;
  50.     }
  51.     public SignerInfo(
  52.         ASN1Sequence seq)
  53.     {
  54.         Enumeration     e = seq.getObjects();
  55.         version = (DERInteger)e.nextElement();
  56.         sid = SignerIdentifier.getInstance(e.nextElement());
  57.         digAlgorithm = AlgorithmIdentifier.getInstance(e.nextElement());
  58.         Object obj = e.nextElement();
  59.         if (obj instanceof ASN1TaggedObject)
  60.         {
  61.             authenticatedAttributes = ASN1Set.getInstance((ASN1TaggedObject)obj, false);
  62.             digEncryptionAlgorithm = AlgorithmIdentifier.getInstance(e.nextElement());
  63.         }
  64.         else
  65.         {
  66.             authenticatedAttributes = null;
  67.             digEncryptionAlgorithm = AlgorithmIdentifier.getInstance(obj);
  68.         }
  69.         encryptedDigest = DEROctetString.getInstance(e.nextElement());
  70.         if (e.hasMoreElements())
  71.         {
  72.             unauthenticatedAttributes = ASN1Set.getInstance((ASN1TaggedObject)e.nextElement(), false);
  73.         }
  74.         else
  75.         {
  76.             unauthenticatedAttributes = null;
  77.         }
  78.     }
  79.     public DERInteger getVersion()
  80.     {
  81.         return version;
  82.     }
  83.     public SignerIdentifier getSID()
  84.     {
  85.         return sid;
  86.     }
  87.     public ASN1Set getAuthenticatedAttributes()
  88.     {
  89.         return authenticatedAttributes;
  90.     }
  91.     public AlgorithmIdentifier getDigestAlgorithm()
  92.     {
  93.         return digAlgorithm;
  94.     }
  95.     public ASN1OctetString getEncryptedDigest()
  96.     {
  97.         return encryptedDigest;
  98.     }
  99.     public AlgorithmIdentifier getDigestEncryptionAlgorithm()
  100.     {
  101.         return digEncryptionAlgorithm;
  102.     }
  103.     public ASN1Set getUnauthenticatedAttributes()
  104.     {
  105.         return unauthenticatedAttributes;
  106.     }
  107.     /**
  108.      * <pre>
  109.      *  SignerInfo ::= SEQUENCE {
  110.      *      version Version,
  111.      *      SignerIdentifier sid,
  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 getDERObject()
  127.     {
  128.         ASN1EncodableVector v = new ASN1EncodableVector();
  129.         v.add(version);
  130.         v.add(sid);
  131.         v.add(digAlgorithm);
  132.         if (authenticatedAttributes != null)
  133.         {
  134.             v.add(new DERTaggedObject(false, 0, authenticatedAttributes));
  135.         }
  136.         v.add(digEncryptionAlgorithm);
  137.         v.add(encryptedDigest);
  138.         if (unauthenticatedAttributes != null)
  139.         {
  140.             v.add(new DERTaggedObject(false, 1, unauthenticatedAttributes));
  141.         }
  142.         return new DERSequence(v);
  143.     }
  144. }