Home | History | Annotate | Download | only in cms
      1 package org.bouncycastle.asn1.cms;
      2 
      3 import org.bouncycastle.asn1.ASN1EncodableVector;
      4 import org.bouncycastle.asn1.ASN1Object;
      5 import org.bouncycastle.asn1.ASN1Primitive;
      6 import org.bouncycastle.asn1.ASN1Sequence;
      7 import org.bouncycastle.asn1.ASN1TaggedObject;
      8 import org.bouncycastle.asn1.DERSequence;
      9 import org.bouncycastle.asn1.DERTaggedObject;
     10 import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
     11 
     12 /**
     13  * From RFC 6211
     14  * <pre>
     15  * CMSAlgorithmProtection ::= SEQUENCE {
     16  *    digestAlgorithm         DigestAlgorithmIdentifier,
     17  *    signatureAlgorithm  [1] SignatureAlgorithmIdentifier OPTIONAL,
     18  *    macAlgorithm        [2] MessageAuthenticationCodeAlgorithm
     19  *                                     OPTIONAL
     20  * }
     21  * (WITH COMPONENTS { signatureAlgorithm PRESENT,
     22  *                    macAlgorithm ABSENT } |
     23  *  WITH COMPONENTS { signatureAlgorithm ABSENT,
     24  *                    macAlgorithm PRESENT })
     25  * </pre>
     26  */
     27 public class CMSAlgorithmProtection
     28     extends ASN1Object
     29 {
     30     public static final int SIGNATURE = 1;
     31     public static final int MAC = 2;
     32 
     33     private final AlgorithmIdentifier digestAlgorithm;
     34     private final AlgorithmIdentifier signatureAlgorithm;
     35     private final AlgorithmIdentifier macAlgorithm;
     36 
     37     public CMSAlgorithmProtection(AlgorithmIdentifier digestAlgorithm, int type, AlgorithmIdentifier algorithmIdentifier)
     38     {
     39         if (digestAlgorithm == null || algorithmIdentifier == null)
     40         {
     41             throw new NullPointerException("AlgorithmIdentifiers cannot be null");
     42         }
     43 
     44         this.digestAlgorithm = digestAlgorithm;
     45 
     46         if (type == 1)
     47         {
     48             this.signatureAlgorithm = algorithmIdentifier;
     49             this.macAlgorithm = null;
     50         }
     51         else if (type == 2)
     52         {
     53             this.signatureAlgorithm = null;
     54             this.macAlgorithm = algorithmIdentifier;
     55         }
     56         else
     57         {
     58             throw new IllegalArgumentException("Unknown type: " + type);
     59         }
     60     }
     61 
     62     private CMSAlgorithmProtection(ASN1Sequence sequence)
     63     {
     64         if (sequence.size() != 2)
     65         {
     66             throw new IllegalArgumentException("Sequence wrong size: One of signatureAlgorithm or macAlgorithm must be present");
     67         }
     68 
     69         this.digestAlgorithm = AlgorithmIdentifier.getInstance(sequence.getObjectAt(0));
     70 
     71         ASN1TaggedObject tagged = ASN1TaggedObject.getInstance(sequence.getObjectAt(1));
     72         if (tagged.getTagNo() == 1)
     73         {
     74             this.signatureAlgorithm = AlgorithmIdentifier.getInstance(tagged, false);
     75             this.macAlgorithm = null;
     76         }
     77         else if (tagged.getTagNo() == 2)
     78         {
     79             this.signatureAlgorithm = null;
     80 
     81             this.macAlgorithm = AlgorithmIdentifier.getInstance(tagged, false);
     82         }
     83         else
     84         {
     85             throw new IllegalArgumentException("Unknown tag found: " + tagged.getTagNo());
     86         }
     87     }
     88 
     89     public static CMSAlgorithmProtection getInstance(
     90         Object obj)
     91     {
     92         if (obj instanceof CMSAlgorithmProtection)
     93         {
     94             return (CMSAlgorithmProtection)obj;
     95         }
     96         else if (obj != null)
     97         {
     98             return new CMSAlgorithmProtection(ASN1Sequence.getInstance(obj));
     99         }
    100 
    101         return null;
    102     }
    103 
    104 
    105     public AlgorithmIdentifier getDigestAlgorithm()
    106     {
    107         return digestAlgorithm;
    108     }
    109 
    110     public AlgorithmIdentifier getMacAlgorithm()
    111     {
    112         return macAlgorithm;
    113     }
    114 
    115     public AlgorithmIdentifier getSignatureAlgorithm()
    116     {
    117         return signatureAlgorithm;
    118     }
    119 
    120     public ASN1Primitive toASN1Primitive()
    121     {
    122         ASN1EncodableVector v = new ASN1EncodableVector();
    123 
    124         v.add(digestAlgorithm);
    125         if (signatureAlgorithm != null)
    126         {
    127             v.add(new DERTaggedObject(false, 1, signatureAlgorithm));
    128         }
    129         if (macAlgorithm != null)
    130         {
    131             v.add(new DERTaggedObject(false, 2, macAlgorithm));
    132         }
    133 
    134         return new DERSequence(v);
    135     }
    136 }
    137