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

CA认证

开发平台:

Java

  1. package org.bouncycastle.asn1.pkcs;
  2. import java.util.Enumeration;
  3. import org.bouncycastle.asn1.*;
  4. import org.bouncycastle.asn1.x509.*;
  5. /**
  6.  * a PKCS#7 signed data object.
  7.  */
  8. public class SignedData
  9.     implements DEREncodable, PKCSObjectIdentifiers
  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: " + o);
  29.     }
  30. public SignedData(
  31. DERInteger        _version,
  32. ASN1Set           _digestAlgorithms,
  33. ContentInfo       _contentInfo,
  34. ASN1Set           _certificates,
  35. ASN1Set           _crls,
  36. ASN1Set           _signerInfos)
  37. {
  38. version          = _version;
  39. digestAlgorithms = _digestAlgorithms;
  40. contentInfo      = _contentInfo;
  41. certificates     = _certificates;
  42. crls             = _crls;
  43. signerInfos      = _signerInfos;
  44. }
  45.     public SignedData(
  46.         ASN1Sequence seq)
  47.     {
  48.         Enumeration     e = seq.getObjects();
  49.         version = (DERInteger)e.nextElement();
  50.         digestAlgorithms = ((ASN1Set)e.nextElement());
  51.         contentInfo = ContentInfo.getInstance(e.nextElement());
  52.         while (e.hasMoreElements())
  53.         {
  54.             DERObject o = (DERObject)e.nextElement();
  55.             //
  56.             // an interesting feature of SignedData is that there appear to be varying implementations...
  57.             // for the moment we ignore anything which doesn't fit.
  58.             //
  59.             if (o instanceof DERTaggedObject)
  60.             {
  61.                 DERTaggedObject tagged = (DERTaggedObject)o;
  62.                 switch (tagged.getTagNo())
  63.                 {
  64.                 case 0:
  65.                     certificates = ASN1Set.getInstance(tagged, false);
  66.                     break;
  67.                 case 1:
  68.                     crls = ASN1Set.getInstance(tagged, false);
  69.                     break;
  70.                 default:
  71.                     throw new IllegalArgumentException("unknown tag value " + tagged.getTagNo());
  72.                 }
  73.             }
  74.             else
  75.             {
  76.                 signerInfos = (ASN1Set)o;
  77.             }
  78.         }
  79.     }
  80.     public DERInteger getVersion()
  81.     {
  82.         return version;
  83.     }
  84.     public ASN1Set getDigestAlgorithms()
  85.     {
  86.         return digestAlgorithms;
  87.     }
  88.     public ContentInfo getContentInfo()
  89.     {
  90.         return contentInfo;
  91.     }
  92.     public ASN1Set getCertificates()
  93.     {
  94.         return certificates;
  95.     }
  96.     public ASN1Set getCRLs()
  97.     {
  98.         return crls;
  99.     }
  100.     public ASN1Set getSignerInfos()
  101.     {
  102.         return signerInfos;
  103.     }
  104.     /**
  105.      * <pre>
  106.      *  SignedData ::= SEQUENCE {
  107.      *      version Version,
  108.      *      digestAlgorithms DigestAlgorithmIdentifiers,
  109.      *      contentInfo ContentInfo,
  110.      *      certificates
  111.      *          [0] IMPLICIT ExtendedCertificatesAndCertificates
  112.      *                   OPTIONAL,
  113.      *      crls
  114.      *          [1] IMPLICIT CertificateRevocationLists OPTIONAL,
  115.      *      signerInfos SignerInfos }
  116.      * </pre>
  117.      */
  118.     public DERObject getDERObject()
  119.     {
  120.         ASN1EncodableVector v = new ASN1EncodableVector();
  121.         v.add(version);
  122.         v.add(digestAlgorithms);
  123.         v.add(contentInfo);
  124.         if (certificates != null)
  125.         {
  126.             v.add(new DERTaggedObject(false, 0, certificates));
  127.         }
  128.         if (crls != null)
  129.         {
  130.             v.add(new DERTaggedObject(false, 1, crls));
  131.         }
  132.         v.add(signerInfos);
  133.         return new BERSequence(v);
  134.     }
  135. }