summary refs log tree commit diff
path: root/crypto/src/asn1/cms/CmsAlgorithmProtection.cs
blob: 9d21e53ff23bc65ead70fd94a5e82405dcbd70ac (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using System;

using Org.BouncyCastle.Asn1.X509;

namespace Org.BouncyCastle.Asn1.Cms
{

    /**
     * From RFC 6211
     * <pre>
     * CMSAlgorithmProtection ::= SEQUENCE {
     *    digestAlgorithm         DigestAlgorithmIdentifier,
     *    signatureAlgorithm  [1] SignatureAlgorithmIdentifier OPTIONAL,
     *    macAlgorithm        [2] MessageAuthenticationCodeAlgorithm
     *                                     OPTIONAL
     * }
     * (WITH COMPONENTS { signatureAlgorithm PRESENT,
     *                    macAlgorithm ABSENT } |
     *  WITH COMPONENTS { signatureAlgorithm ABSENT,
     *                    macAlgorithm PRESENT })
     * </pre>
     */
    public class CmsAlgorithmProtection
        : Asn1Encodable
    {
        public static readonly int Signature = 1;
        public static readonly int Mac = 2;

        private readonly AlgorithmIdentifier digestAlgorithm;
        private readonly AlgorithmIdentifier signatureAlgorithm;
        private readonly AlgorithmIdentifier macAlgorithm;

        public CmsAlgorithmProtection(AlgorithmIdentifier digestAlgorithm, int type, AlgorithmIdentifier algorithmIdentifier)
        {
            if (digestAlgorithm == null || algorithmIdentifier == null)
                throw new ArgumentException("AlgorithmIdentifiers cannot be null");

            this.digestAlgorithm = digestAlgorithm;

            if (type == 1)
            {
                this.signatureAlgorithm = algorithmIdentifier;
                this.macAlgorithm = null;
            }
            else if (type == 2)
            {
                this.signatureAlgorithm = null;
                this.macAlgorithm = algorithmIdentifier;
            }
            else
            {
                throw new ArgumentException("Unknown type: " + type);
            }
        }

        private CmsAlgorithmProtection(Asn1Sequence sequence)
        {
            if (sequence.Count != 2)
                throw new ArgumentException("Sequence wrong size: One of signatureAlgorithm or macAlgorithm must be present");

            this.digestAlgorithm = AlgorithmIdentifier.GetInstance(sequence[0]);

            Asn1TaggedObject tagged = Asn1TaggedObject.GetInstance(sequence[1]);
            if (tagged.TagNo == 1)
            {
                this.signatureAlgorithm = AlgorithmIdentifier.GetInstance(tagged, false);
                this.macAlgorithm = null;
            }
            else if (tagged.TagNo == 2)
            {
                this.signatureAlgorithm = null;

                this.macAlgorithm = AlgorithmIdentifier.GetInstance(tagged, false);
            }
            else
            {
                throw new ArgumentException("Unknown tag found: " + tagged.TagNo);
            }
        }

        public static CmsAlgorithmProtection GetInstance(object obj)
        {
            if (obj == null)
                return null;
            if (obj is CmsAlgorithmProtection cmsAlgorithmProtection)
                return cmsAlgorithmProtection;
            return new CmsAlgorithmProtection(Asn1Sequence.GetInstance(obj));
        }

        public AlgorithmIdentifier DigestAlgorithm => digestAlgorithm;

        public AlgorithmIdentifier MacAlgorithm => macAlgorithm;

        public AlgorithmIdentifier SignatureAlgorithm => signatureAlgorithm;

        public override Asn1Object ToAsn1Object()
        {
            Asn1EncodableVector v = new Asn1EncodableVector(3);
            v.Add(digestAlgorithm);
            v.AddOptionalTagged(false, 1, signatureAlgorithm);
            v.AddOptionalTagged(false, 2, macAlgorithm);
            return new DerSequence(v);
        }
    }
}