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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/* Copyright (c) Fortanix, Inc.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use derives::*;
use oid;

/* An algorithm identifier is a sequence containing OID
 * Followed by optional components based on the algorithm
 * If nothing beyond OID is required, we need to write_null()
*/
macro_rules! define_algorithm_identifier {
    (
        $($variant:ident = $val:expr),*,
    ) => {
        enum_oid! {
            AlgorithmIdentifierType {
                $($variant = $val),*,
            }
        }

        impl_content_with_associated_type! {
            AlgorithmIdentifier : Sequence => AlgorithmIdentifierType {
                $($variant),*,
            }
        }
    }
}

// These are identifiers without furthur description
macro_rules! impl_null_desc_algorithm_identifier {
    (
        $($variant:ident),*,
    ) => {
        $(
            #[derive(Clone, Debug, Eq, PartialEq, Hash)]
            #[allow(non_camel_case_types)]
            pub struct $variant{}
            impl DerWrite for $variant {
                fn write(&self, writer: DERWriter) {
                    writer.write_null();
                }
            }
            impl BERDecodable for $variant {
                fn decode_ber(reader: BERReader) -> ASN1Result<Self> {
                    reader.read_null().and_then(|_| Ok($variant{}))
                }
            }
        )*
    }
}

macro_rules! define_algorithm {
    ($name:ident => $Ty:ident {
        $($variant:ident = $val:expr),*,
     }
    ) => {
        enum_oid! {
            $Ty {
                $($variant = $val),*,
            }
        }

        impl_content_with_associated_type! {
            $name : Sequence => $Ty {
                $($variant),*,
            }
        }
    }
}

impl_null_desc_algorithm_identifier! {
    sha256,
    sha1,
}

define_algorithm_identifier! {
    sha256 = oid::sha256,
    sha1 = oid::sha1,
    mgf1 = oid::mgf1,
    aes128_cbc = oid::aes128_cbc,
}

#[derive(Clone, Debug, Eq, PartialEq, Hash)]
#[allow(non_camel_case_types)]
pub struct mgf1 {}

// mfg1's following content is always the AlgorithmIdentifier for sha1.
impl DerWrite for mgf1 {
    fn write(&self, writer: DERWriter) {
        let algo = sha1 {};
        let _ = &algo.write(writer);
    }
}

impl BERDecodable for mgf1 {
    fn decode_ber(reader: BERReader) -> ASN1Result<Self> {
        <sha1 as BERDecodable>::decode_ber(reader).and_then(|_| Ok(mgf1 {}))
    }
}

enum_subtype! {
    ContentEncryptionAlgorithm => AlgorithmIdentifier {
        aes128_cbc,
    }
}

const CMS_128_IV_LEN : usize = 16;
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
#[allow(non_camel_case_types)]
pub struct aes128_cbc {
    pub iv: [u8; 16],
}

impl aes128_cbc {
    pub fn new(iv: &[u8]) -> ASN1Result<Self> {
        if iv.len() != CMS_128_IV_LEN {
            return Err(ASN1Error::new(ASN1ErrorKind::Invalid));
        }
        let mut algo_iv : [u8; CMS_128_IV_LEN] = [0; CMS_128_IV_LEN];
        algo_iv.copy_from_slice(iv);
        Ok(aes128_cbc{iv : algo_iv})
    }
}

impl DerWrite for aes128_cbc {
    fn write(&self, writer: DERWriter) {
        writer.write_bytes(&self.iv);
    }
}

impl BERDecodable for aes128_cbc {
    fn decode_ber(reader: BERReader) -> ASN1Result<Self> {
        reader.read_bytes().and_then(|iv| aes128_cbc::new(&iv))
    }
}

enum_subtype! {
    HashAlgorithm => AlgorithmIdentifier {
        sha256,
    }
}

enum_subtype! {
    MaskGenAlgorithm => AlgorithmIdentifier {
        mgf1,
    }
}

enum_subtype! {
    DigestAlgorithmIdentifier => AlgorithmIdentifier {
        sha256,
    }
}
derive_set_of! {
    DigestAlgorithmIdentifier => DigestAlgorithmIdentifiers
}

//RSAES_OAEP
define_algorithm! {
    KeyEncryptionAlgorithm => KeyEncryptionAlgorithmType {
        RSAES_OAEP = oid::RSAES_OAEP,
    }
}
// rfc3560#section-2.2
//rfc3447#appendix-A.2.1
derive_sequence! {
    RSAES_OAEP {
        hashAlgorithm    :    [0] EXPLICIT : HashAlgorithm,
        maskGenAlgorithm :    [1] EXPLICIT : MaskGenAlgorithm,
    }
}

define_algorithm! {
    SignatureAlgorithm  => SignatureAlgorithmType {
        RSASSA_PSS = oid::RSASSA_PSS,
    }
}

#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct SaltLength(u64);

impl From<u64> for SaltLength {
    fn from(n: u64) -> SaltLength {
        SaltLength(n)
    }
}

impl From<SaltLength> for u64 {
    fn from(s: SaltLength) -> u64 {
        let SaltLength(n) = s;
        n
    }
}

impl BERDecodable for SaltLength {
    fn decode_ber(reader: BERReader) -> ASN1Result<Self> {
        reader.read_u64().and_then(|n| Ok(n.into()))
    }
}

impl DerWrite for SaltLength {
    fn write(&self, writer: DERWriter) {
        // self is essentially an integer, clone should be trivial.
        u64::from(self.clone()).write(writer)
    }
}

// rfc3560#section-2.2
derive_sequence! {
    RSASSA_PSS {
        hashAlgorithm : [0] EXPLICIT : HashAlgorithm,
        maskAlgorithm : [1] EXPLICIT : MaskGenAlgorithm,
        saltLength    : [2] EXPLICIT : SaltLength,
    }
}