SignedData.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. /**
  6.  * a signed data object.
  7.  */
  8. public class SignedData
  9.     implements DEREncodable, CMSObjectIdentifiers
  10. {
  11.     private DERInteger  version;
  12.     private ASN1Set     digestAlgorithms;
  13.     private ContentInfo contentInfo;
  14.     private ASN1Set     certificates;
  15.     private ASN1Set     crls;
  16.     private ASN1Set     signerInfos;
  17.     public static SignedData getInstance(
  18.         Object  o)
  19.     {
  20.         if (o instanceof SignedData)
  21.         {
  22.             return (SignedData)o;
  23.         }
  24.         else if (o instanceof ASN1Sequence)
  25.         {
  26.             return new SignedData((ASN1Sequence)o);
  27.         }
  28.         throw new IllegalArgumentException("unknown object in factory");
  29.     }
  30. public SignedData(
  31. ASN1Set     digestAlgorithms,
  32. ContentInfo contentInfo,
  33. ASN1Set     certificates,
  34. ASN1Set     crls,
  35. ASN1Set     signerInfos)
  36. {
  37.         if (contentInfo.getContentType().equals(CMSObjectIdentifiers.data))
  38.         {
  39.             //
  40.             // we should also be looking for attribute certificates here,
  41.             // later.
  42.             //
  43.             Enumeration e = signerInfos.getObjects();
  44.             boolean     v3Found = false;
  45.             while (e.hasMoreElements())
  46.             {
  47.                 SignerInfo  s = SignerInfo.getInstance(e.nextElement());
  48.                 if (s.getVersion().getValue().intValue() == 3)
  49.                 {
  50.                     v3Found = true;
  51.                 }
  52.             }
  53.             if (v3Found)
  54.             {
  55.                 this.version = new DERInteger(3);
  56.             }
  57.             else
  58.             {
  59.                 this.version = new DERInteger(1);
  60.             }
  61.         }
  62.         else
  63.         {
  64.             this.version = new DERInteger(3);
  65.         }
  66. this.digestAlgorithms = digestAlgorithms;
  67. this.contentInfo = contentInfo;
  68. this.certificates = certificates;
  69. this.crls = crls;
  70. this.signerInfos = signerInfos;
  71. }
  72.     public SignedData(
  73.         ASN1Sequence seq)
  74.     {
  75.         Enumeration     e = seq.getObjects();
  76.         version = (DERInteger)e.nextElement();
  77.         digestAlgorithms = ((ASN1Set)e.nextElement());
  78.         contentInfo = ContentInfo.getInstance(e.nextElement());
  79.         while (e.hasMoreElements())
  80.         {
  81.             DERObject o = (DERObject)e.nextElement();
  82.             //
  83.             // an interesting feature of SignedData is that there appear
  84.             // to be varying implementations...
  85.             // for the moment we ignore anything which doesn't fit.
  86.             //
  87.             if (o instanceof DERTaggedObject)
  88.             {
  89.                 DERTaggedObject tagged = (DERTaggedObject)o;
  90.                 switch (tagged.getTagNo())
  91.                 {
  92.                 case 0:
  93.                     certificates = ASN1Set.getInstance(tagged, false);
  94.                     break;
  95.                 case 1:
  96.                     crls = ASN1Set.getInstance(tagged, false);
  97.                     break;
  98.                 default:
  99.                     throw new IllegalArgumentException("unknown tag value " + tagged.getTagNo());
  100.                 }
  101.             }
  102.             else
  103.             {
  104.                 signerInfos = (ASN1Set)o;
  105.             }
  106.         }
  107.     }
  108.     public DERInteger getVersion()
  109.     {
  110.         return version;
  111.     }
  112.     public ASN1Set getDigestAlgorithms()
  113.     {
  114.         return digestAlgorithms;
  115.     }
  116.     public ContentInfo getEncapContentInfo()
  117.     {
  118.         return contentInfo;
  119.     }
  120.     public ASN1Set getCertificates()
  121.     {
  122.         return certificates;
  123.     }
  124.     public ASN1Set getCRLs()
  125.     {
  126.         return crls;
  127.     }
  128.     public ASN1Set getSignerInfos()
  129.     {
  130.         return signerInfos;
  131.     }
  132.     /**
  133.      * <pre>
  134.      * SignedData ::= SEQUENCE {
  135.      *     version CMSVersion,
  136.      *     digestAlgorithms DigestAlgorithmIdentifiers,
  137.      *     encapContentInfo EncapsulatedContentInfo,
  138.      *     certificates [0] IMPLICIT CertificateSet OPTIONAL,
  139.      *     crls [1] IMPLICIT CertificateRevocationLists OPTIONAL,
  140.      *     signerInfos SignerInfos
  141.      *   }
  142.      * </pre>
  143.      */
  144.     public DERObject getDERObject()
  145.     {
  146.         ASN1EncodableVector  v = new ASN1EncodableVector();
  147.         v.add(version);
  148.         v.add(digestAlgorithms);
  149.         v.add(contentInfo);
  150.         if (certificates != null)
  151.         {
  152.             v.add(new DERTaggedObject(false, 0, certificates));
  153.         }
  154.         if (crls != null)
  155.         {
  156.             v.add(new DERTaggedObject(false, 1, crls));
  157.         }
  158.         v.add(signerInfos);
  159.         return new BERSequence(v);
  160.     }
  161. }