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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
use crate::{AlgorithmIdentifier, Error, ObjectIdentifier};
use core::convert::{TryFrom, TryInto};
use der::{message, Any, Encodable, Encoder, ErrorKind, Header, Length, OctetString, Result, Tag};
pub const PBE_WITH_MD2_AND_DES_CBC_OID: ObjectIdentifier =
ObjectIdentifier::new("1.2.840.113549.1.5.1");
pub const PBE_WITH_MD2_AND_RC2_CBC_OID: ObjectIdentifier =
ObjectIdentifier::new("1.2.840.113549.1.5.4");
pub const PBE_WITH_MD5_AND_DES_CBC_OID: ObjectIdentifier =
ObjectIdentifier::new("1.2.840.113549.1.5.3");
pub const PBE_WITH_MD5_AND_RC2_CBC_OID: ObjectIdentifier =
ObjectIdentifier::new("1.2.840.113549.1.5.6");
pub const PBE_WITH_SHA1_AND_DES_CBC_OID: ObjectIdentifier =
ObjectIdentifier::new("1.2.840.113549.1.5.10");
pub const PBE_WITH_SHA1_AND_RC2_CBC_OID: ObjectIdentifier =
ObjectIdentifier::new("1.2.840.113549.1.5.11");
pub const SALT_LENGTH: usize = 8;
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Parameters {
pub encryption: EncryptionScheme,
pub salt: [u8; SALT_LENGTH],
pub iteration_count: u16,
}
impl Parameters {
pub fn oid(&self) -> ObjectIdentifier {
self.encryption.oid()
}
}
impl<'a> TryFrom<Any<'a>> for Parameters {
type Error = Error;
fn try_from(any: Any<'a>) -> Result<Self> {
AlgorithmIdentifier::try_from(any).and_then(TryInto::try_into)
}
}
impl<'a> TryFrom<AlgorithmIdentifier<'a>> for Parameters {
type Error = Error;
fn try_from(alg: AlgorithmIdentifier<'a>) -> Result<Self> {
let encryption = EncryptionScheme::try_from(alg.oid).map_err(|_| ErrorKind::Value {
tag: der::Tag::ObjectIdentifier,
})?;
alg.parameters_any()?.sequence(|params| {
let salt =
params
.octet_string()?
.as_bytes()
.try_into()
.map_err(|_| ErrorKind::Value {
tag: der::Tag::OctetString,
})?;
let iteration_count = params.decode()?;
Ok(Self {
encryption,
salt,
iteration_count,
})
})
}
}
impl Encodable for Parameters {
fn encoded_len(&self) -> Result<Length> {
self.header()?.encoded_len()
}
fn encode(&self, encoder: &mut Encoder<'_>) -> Result<()> {
self.header()?.encode(encoder)?;
let inner_len = self.inner_len()?;
encoder.sequence(inner_len, |nested_encoder| {
nested_encoder.oid(self.encryption.oid())?;
nested_encoder.message(&[&self.salt_string()?, &self.iteration_count])?;
Ok(())
})
}
}
impl Parameters {
fn header(&self) -> Result<Header> {
Header::new(Tag::Sequence, self.inner_len()?)
}
fn inner_len(&self) -> Result<Length> {
let oid_len = self.encryption.oid().encoded_len()?;
let params_len = message::encoded_len(&[&self.salt_string()?, &self.iteration_count])?;
oid_len + params_len
}
fn salt_string(&self) -> Result<OctetString<'_>> {
OctetString::new(&self.salt)
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum EncryptionScheme {
PbeWithMd2AndDesCbc,
PbeWithMd2AndRc2Cbc,
PbeWithMd5AndDesCbc,
PbeWithMd5AndRc2Cbc,
PbeWithSha1AndDesCbc,
PbeWithSha1AndRc2Cbc,
}
impl TryFrom<ObjectIdentifier> for EncryptionScheme {
type Error = Error;
fn try_from(oid: ObjectIdentifier) -> Result<Self> {
match oid {
PBE_WITH_MD2_AND_DES_CBC_OID => Ok(Self::PbeWithMd2AndDesCbc),
PBE_WITH_MD2_AND_RC2_CBC_OID => Ok(Self::PbeWithMd2AndRc2Cbc),
PBE_WITH_MD5_AND_DES_CBC_OID => Ok(Self::PbeWithMd5AndDesCbc),
PBE_WITH_MD5_AND_RC2_CBC_OID => Ok(Self::PbeWithMd5AndRc2Cbc),
PBE_WITH_SHA1_AND_DES_CBC_OID => Ok(Self::PbeWithSha1AndDesCbc),
PBE_WITH_SHA1_AND_RC2_CBC_OID => Ok(Self::PbeWithSha1AndRc2Cbc),
_ => Err(ErrorKind::UnknownOid { oid }.into()),
}
}
}
impl EncryptionScheme {
pub fn cipher(self) -> SymmetricCipher {
match self {
Self::PbeWithMd2AndDesCbc => SymmetricCipher::DesCbc,
Self::PbeWithMd2AndRc2Cbc => SymmetricCipher::Rc2Cbc,
Self::PbeWithMd5AndDesCbc => SymmetricCipher::DesCbc,
Self::PbeWithMd5AndRc2Cbc => SymmetricCipher::Rc2Cbc,
Self::PbeWithSha1AndDesCbc => SymmetricCipher::DesCbc,
Self::PbeWithSha1AndRc2Cbc => SymmetricCipher::Rc2Cbc,
}
}
pub fn digest(self) -> DigestAlgorithm {
match self {
Self::PbeWithMd2AndDesCbc => DigestAlgorithm::Md2,
Self::PbeWithMd2AndRc2Cbc => DigestAlgorithm::Md2,
Self::PbeWithMd5AndDesCbc => DigestAlgorithm::Md5,
Self::PbeWithMd5AndRc2Cbc => DigestAlgorithm::Md5,
Self::PbeWithSha1AndDesCbc => DigestAlgorithm::Sha1,
Self::PbeWithSha1AndRc2Cbc => DigestAlgorithm::Sha1,
}
}
pub fn oid(self) -> ObjectIdentifier {
match self {
Self::PbeWithMd2AndDesCbc => PBE_WITH_MD2_AND_DES_CBC_OID,
Self::PbeWithMd2AndRc2Cbc => PBE_WITH_MD2_AND_RC2_CBC_OID,
Self::PbeWithMd5AndDesCbc => PBE_WITH_MD5_AND_DES_CBC_OID,
Self::PbeWithMd5AndRc2Cbc => PBE_WITH_MD5_AND_RC2_CBC_OID,
Self::PbeWithSha1AndDesCbc => PBE_WITH_SHA1_AND_DES_CBC_OID,
Self::PbeWithSha1AndRc2Cbc => PBE_WITH_SHA1_AND_RC2_CBC_OID,
}
}
}
impl Encodable for EncryptionScheme {
fn encoded_len(&self) -> Result<Length> {
self.oid().encoded_len()
}
fn encode(&self, encoder: &mut Encoder<'_>) -> Result<()> {
self.oid().encode(encoder)
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum DigestAlgorithm {
Md2,
Md5,
Sha1,
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum SymmetricCipher {
DesCbc,
Rc2Cbc,
}