Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.bouncycastle.asn1.cms;
  
  
RFC 5652:

A signed data object containing multitude of SignerInfos.

 SignedData ::= SEQUENCE {
     version CMSVersion,
     digestAlgorithms DigestAlgorithmIdentifiers,
     encapContentInfo EncapsulatedContentInfo,
     certificates [0] IMPLICIT CertificateSet OPTIONAL,
     crls [1] IMPLICIT CertificateRevocationLists OPTIONAL,
     signerInfos SignerInfos
   }
 
 DigestAlgorithmIdentifiers ::= SET OF DigestAlgorithmIdentifier
 
 SignerInfos ::= SET OF SignerInfo
 

The version calculation uses following ruleset from RFC 3852 section 5.1:

 IF ((certificates is present) AND
    (any certificates with a type of other are present)) OR
    ((crls is present) AND
    (any crls with a type of other are present))
 THEN version MUST be 5
 ELSE
    IF (certificates is present) AND
       (any version 2 attribute certificates are present)
    THEN version MUST be 4
    ELSE
       IF ((certificates is present) AND
          (any version 1 attribute certificates are present)) OR
          (any SignerInfo structures are version 3) OR
          (encapContentInfo eContentType is other than id-data)
       THEN version MUST be 3
       ELSE version MUST be 1
 

Todo:
Check possible update for this to RFC 5652 level
 
 public class SignedData
     extends ASN1Object
 {
     private static final ASN1Integer VERSION_1 = new ASN1Integer(1);
     private static final ASN1Integer VERSION_3 = new ASN1Integer(3);
     private static final ASN1Integer VERSION_4 = new ASN1Integer(4);
     private static final ASN1Integer VERSION_5 = new ASN1Integer(5);
 
     private ASN1Integer version;
     private ASN1Set     digestAlgorithms;
     private ContentInfo contentInfo;
     private ASN1Set     certificates;
     private ASN1Set     crls;
     private ASN1Set     signerInfos;
     private boolean certsBer;
     private boolean        crlsBer;

    
Return a SignedData object from the given object.

Accepted inputs:

  • null → null
  • SignedData object
  • ASN1Sequence input formats with SignedData structure inside

Parameters:
o the object we want converted.
Throws:
java.lang.IllegalArgumentException if the object cannot be converted.
 
     public static SignedData getInstance(
         Object  o)
     {
         if (o instanceof SignedData)
         {
             return (SignedData)o;
         }
         else if (o != null)
         {
             return new SignedData(ASN1Sequence.getInstance(o));
         }
        return null;
    }
    public SignedData(
        ASN1Set     digestAlgorithms,
        ContentInfo contentInfo,
        ASN1Set     certificates,
        ASN1Set     crls,
        ASN1Set     signerInfos)
    {
        this. = calculateVersion(contentInfo.getContentType(), certificatescrlssignerInfos);
        this. = digestAlgorithms;
        this. = contentInfo;
        this. = certificates;
        this. = crls;
        this. = signerInfos;
        this. = crls instanceof BERSet;
        this. = certificates instanceof BERSet;
    }
    private ASN1Integer calculateVersion(
        ASN1ObjectIdentifier contentOid,
        ASN1Set certs,
        ASN1Set crls,
        ASN1Set signerInfs)
    {
        boolean otherCert = false;
        boolean otherCrl = false;
        boolean attrCertV1Found = false;
        boolean attrCertV2Found = false;
        if (certs != null)
        {
            for (Enumeration en = certs.getObjects(); en.hasMoreElements();)
            {
                Object obj = en.nextElement();
                if (obj instanceof ASN1TaggedObject)
                {
                    ASN1TaggedObject tagged = ASN1TaggedObject.getInstance(obj);
                    if (tagged.getTagNo() == 1)
                    {
                        attrCertV1Found = true;
                    }
                    else if (tagged.getTagNo() == 2)
                    {
                        attrCertV2Found = true;
                    }
                    else if (tagged.getTagNo() == 3)
                    {
                        otherCert = true;
                    }
                }
            }
        }
        if (otherCert)
        {
            return new ASN1Integer(5);
        }
        if (crls != null)         // no need to check if otherCert is true
        {
            for (Enumeration en = crls.getObjects(); en.hasMoreElements();)
            {
                Object obj = en.nextElement();
                if (obj instanceof ASN1TaggedObject)
                {
                    otherCrl = true;
                }
            }
        }
        if (otherCrl)
        {
            return ;
        }
        if (attrCertV2Found)
        {
            return ;
        }
        if (attrCertV1Found)
        {
            return ;
        }
        if (checkForVersion3(signerInfs))
        {
            return ;
        }
        if (!..equals(contentOid))
        {
            return ;
        }
        return ;
    }
    private boolean checkForVersion3(ASN1Set signerInfs)
    {
        for (Enumeration e = signerInfs.getObjects(); e.hasMoreElements();)
        {
            SignerInfo s = SignerInfo.getInstance(e.nextElement());
            if (s.getVersion().getValue().intValue() == 3)
            {
                return true;
            }
        }
        return false;
    }
    private SignedData(
        ASN1Sequence seq)
    {
        Enumeration     e = seq.getObjects();
         = ASN1Integer.getInstance(e.nextElement());
         = ((ASN1Set)e.nextElement());
         = ContentInfo.getInstance(e.nextElement());
        while (e.hasMoreElements())
        {
            ASN1Primitive o = (ASN1Primitive)e.nextElement();
            //
            // an interesting feature of SignedData is that there appear
            // to be varying implementations...
            // for the moment we ignore anything which doesn't fit.
            //
            if (o instanceof ASN1TaggedObject)
            {
                ASN1TaggedObject tagged = (ASN1TaggedObject)o;
                switch (tagged.getTagNo())
                {
                case 0:
                     = tagged instanceof BERTaggedObject;
                     = ASN1Set.getInstance(taggedfalse);
                    break;
                case 1:
                     = tagged instanceof BERTaggedObject;
                     = ASN1Set.getInstance(taggedfalse);
                    break;
                default:
                    throw new IllegalArgumentException("unknown tag value " + tagged.getTagNo());
                }
            }
            else
            {
                 = (ASN1Set)o;
            }
        }
    }
    public ASN1Integer getVersion()
    {
        return ;
    }
    public ASN1Set getDigestAlgorithms()
    {
        return ;
    }
    {
        return ;
    }
    public ASN1Set getCertificates()
    {
        return ;
    }
    public ASN1Set getCRLs()
    {
        return ;
    }
    public ASN1Set getSignerInfos()
    {
        return ;
    }

    
Produce an object suitable for an ASN1OutputStream.
    {
        ASN1EncodableVector  v = new ASN1EncodableVector();
        v.add();
        v.add();
        v.add();
        if ( != null)
        {
            if ()
            {
                v.add(new BERTaggedObject(false, 0, ));
            }
            else
            {
                v.add(new DERTaggedObject(false, 0, ));
            }
        }
        if ( != null)
        {
            if ()
            {
                v.add(new BERTaggedObject(false, 1, ));
            }
            else
            {
                v.add(new DERTaggedObject(false, 1, ));
            }
        }
        v.add();
        return new BERSequence(v);
    }
New to GrepCode? Check out our FAQ X