summaryrefslogtreecommitdiff
path: root/repackaged_platform/bcprov/src/main/java/com/android/internal/org/bouncycastle/asn1/cms/CMSAlgorithmProtection.java
blob: 9c6427b03414c90feead2cadbef57d76d74596b0 (plain)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* GENERATED SOURCE. DO NOT MODIFY. */
package com.android.internal.org.bouncycastle.asn1.cms;

import com.android.internal.org.bouncycastle.asn1.ASN1EncodableVector;
import com.android.internal.org.bouncycastle.asn1.ASN1Object;
import com.android.internal.org.bouncycastle.asn1.ASN1Primitive;
import com.android.internal.org.bouncycastle.asn1.ASN1Sequence;
import com.android.internal.org.bouncycastle.asn1.ASN1TaggedObject;
import com.android.internal.org.bouncycastle.asn1.DERSequence;
import com.android.internal.org.bouncycastle.asn1.DERTaggedObject;
import com.android.internal.org.bouncycastle.asn1.x509.AlgorithmIdentifier;

/**
 * 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>
 * @hide This class is not part of the Android public SDK API
 */
public class CMSAlgorithmProtection
    extends ASN1Object
{
    public static final int SIGNATURE = 1;
    public static final int MAC = 2;

    private final AlgorithmIdentifier digestAlgorithm;
    private final AlgorithmIdentifier signatureAlgorithm;
    private final AlgorithmIdentifier macAlgorithm;

    public CMSAlgorithmProtection(AlgorithmIdentifier digestAlgorithm, int type, AlgorithmIdentifier algorithmIdentifier)
    {
        if (digestAlgorithm == null || algorithmIdentifier == null)
        {
            throw new NullPointerException("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 IllegalArgumentException("Unknown type: " + type);
        }
    }

    private CMSAlgorithmProtection(ASN1Sequence sequence)
    {
        if (sequence.size() != 2)
        {
            throw new IllegalArgumentException("Sequence wrong size: One of signatureAlgorithm or macAlgorithm must be present");
        }

        this.digestAlgorithm = AlgorithmIdentifier.getInstance(sequence.getObjectAt(0));

        ASN1TaggedObject tagged = ASN1TaggedObject.getInstance(sequence.getObjectAt(1));
        if (tagged.getTagNo() == 1)
        {
            this.signatureAlgorithm = AlgorithmIdentifier.getInstance(tagged, false);
            this.macAlgorithm = null;
        }
        else if (tagged.getTagNo() == 2)
        {
            this.signatureAlgorithm = null;

            this.macAlgorithm = AlgorithmIdentifier.getInstance(tagged, false);
        }
        else
        {
            throw new IllegalArgumentException("Unknown tag found: " + tagged.getTagNo());
        }
    }

    public static CMSAlgorithmProtection getInstance(
        Object obj)
    {
        if (obj instanceof CMSAlgorithmProtection)
        {
            return (CMSAlgorithmProtection)obj;
        }
        else if (obj != null)
        {
            return new CMSAlgorithmProtection(ASN1Sequence.getInstance(obj));
        }

        return null;
    }


    public AlgorithmIdentifier getDigestAlgorithm()
    {
        return digestAlgorithm;
    }

    public AlgorithmIdentifier getMacAlgorithm()
    {
        return macAlgorithm;
    }

    public AlgorithmIdentifier getSignatureAlgorithm()
    {
        return signatureAlgorithm;
    }

    public ASN1Primitive toASN1Primitive()
    {
        ASN1EncodableVector v = new ASN1EncodableVector(3);

        v.add(digestAlgorithm);
        if (signatureAlgorithm != null)
        {
            v.add(new DERTaggedObject(false, 1, signatureAlgorithm));
        }
        if (macAlgorithm != null)
        {
            v.add(new DERTaggedObject(false, 2, macAlgorithm));
        }

        return new DERSequence(v);
    }
}