forked from RustCrypto/formats
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsequence.rs
More file actions
411 lines (352 loc) · 14.2 KB
/
sequence.rs
File metadata and controls
411 lines (352 loc) · 14.2 KB
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//! Support for deriving the `Sequence` trait on structs for the purposes of
//! decoding/encoding ASN.1 `SEQUENCE` types as mapped to struct fields.
mod field;
use crate::{ErrorType, TypeAttrs, default_lifetime};
use field::SequenceField;
use proc_macro2::TokenStream;
use quote::{ToTokens, quote};
use syn::{DeriveInput, GenericParam, Generics, Ident, Lifetime, LifetimeParam};
/// Derive the `Sequence` trait for a struct
pub(crate) struct DeriveSequence {
/// Name of the sequence struct.
ident: Ident,
/// Generics of the struct.
generics: Generics,
/// Fields of the struct.
fields: Vec<SequenceField>,
/// Error type for `DecodeValue` implementation.
error: ErrorType,
}
impl DeriveSequence {
/// Parse [`DeriveInput`].
pub fn new(input: DeriveInput) -> syn::Result<Self> {
let data = match input.data {
syn::Data::Struct(data) => data,
_ => abort!(
input.ident,
"can't derive `Sequence` on this type: only `struct` types are allowed",
),
};
let type_attrs = TypeAttrs::parse(&input.attrs)?;
let fields = data
.fields
.iter()
.map(|field| SequenceField::new(field, &type_attrs))
.collect::<syn::Result<_>>()?;
Ok(Self {
ident: input.ident,
generics: input.generics.clone(),
fields,
error: type_attrs.error.clone(),
})
}
/// Use the first lifetime parameter as lifetime for Decode/Encode lifetime
/// if none found, add one.
fn calc_lifetime(&self) -> (Generics, Lifetime) {
let mut generics = self.generics.clone();
let lifetime = generics
.lifetimes()
.next()
.map(|lt| lt.lifetime.clone())
.unwrap_or_else(|| {
let lt = default_lifetime();
generics
.params
.insert(0, GenericParam::Lifetime(LifetimeParam::new(lt.clone())));
lt
});
// We may or may not have inserted a lifetime.
(generics, lifetime)
}
/// Lower the derived output into a [`TokenStream`] for Sequence trait impl.
pub fn to_tokens_sequence_trait(&self) -> TokenStream {
let ident = &self.ident;
let (der_generics, lifetime) = self.calc_lifetime();
let (_, ty_generics, where_clause) = self.generics.split_for_impl();
let (impl_generics, _, _) = der_generics.split_for_impl();
quote! {
impl #impl_generics ::der::Sequence<#lifetime> for #ident #ty_generics #where_clause {}
}
}
/// Lower the derived output into a [`TokenStream`] for DecodeValue trait impl.
pub fn to_tokens_decode(&self) -> TokenStream {
let ident = &self.ident;
let (der_generics, lifetime) = self.calc_lifetime();
let (_, ty_generics, where_clause) = self.generics.split_for_impl();
let (impl_generics, _, _) = der_generics.split_for_impl();
let mut decode_body = Vec::new();
let mut decode_result = Vec::new();
for field in &self.fields {
decode_body.push(field.to_decode_tokens());
decode_result.push(&field.ident);
}
let error = self.error.to_token_stream();
quote! {
impl #impl_generics ::der::DecodeValue<#lifetime> for #ident #ty_generics #where_clause {
type Error = #error;
fn decode_value<R: ::der::Reader<#lifetime>>(
reader: &mut R,
header: ::der::Header,
) -> ::core::result::Result<Self, #error> {
use ::der::{Decode as _, DecodeValue as _, Reader as _};
reader.read_nested(header.length, |reader| {
#(#decode_body)*
Ok(Self {
#(#decode_result),*
})
})
}
}
}
}
/// Lower the derived output into a [`TokenStream`] for EncodeValue trait impl.
pub fn to_tokens_encode(&self) -> TokenStream {
let ident = &self.ident;
let (_, ty_generics, where_clause) = self.generics.split_for_impl();
let (impl_generics, _, _) = self.generics.split_for_impl();
let mut encoded_lengths = Vec::new();
let mut encode_fields = Vec::new();
for field in &self.fields {
let field = field.to_encode_tokens();
encoded_lengths.push(quote!(#field.encoded_len()?));
encode_fields.push(quote!(#field.encode(writer)?;));
}
quote! {
impl #impl_generics ::der::EncodeValue for #ident #ty_generics #where_clause {
fn value_len(&self) -> ::der::Result<::der::Length> {
use ::der::Encode as _;
[
#(#encoded_lengths),*
]
.into_iter()
.try_fold(::der::Length::ZERO, |acc, len| acc + len)
}
fn encode_value(&self, writer: &mut impl ::der::Writer) -> ::der::Result<()> {
use ::der::Encode as _;
#(#encode_fields)*
Ok(())
}
}
}
}
/// Lower the derived output into a [`TokenStream`] for trait impls:
/// - EncodeValue
/// - DecodeValue
/// - Sequence
pub fn to_tokens_all(&self) -> TokenStream {
let decode_tokens = self.to_tokens_decode();
let encode_tokens = self.to_tokens_encode();
let sequence_trait_tokens = self.to_tokens_sequence_trait();
quote! {
#decode_tokens
#encode_tokens
#sequence_trait_tokens
}
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
#[allow(clippy::bool_assert_comparison)]
mod tests {
use super::DeriveSequence;
use crate::{Asn1Type, TagMode};
use syn::parse_quote;
/// X.509 SPKI `AlgorithmIdentifier`.
#[test]
fn algorithm_identifier_example() {
let input = parse_quote! {
#[derive(Sequence)]
pub struct AlgorithmIdentifier<'a> {
pub algorithm: ObjectIdentifier,
pub parameters: Option<Any<'a>>,
}
};
let ir = DeriveSequence::new(input).unwrap();
assert_eq!(ir.ident, "AlgorithmIdentifier");
assert_eq!(
ir.generics.lifetimes().next().unwrap().lifetime.to_string(),
"'a"
);
assert_eq!(ir.fields.len(), 2);
let algorithm_field = &ir.fields[0];
assert_eq!(algorithm_field.ident, "algorithm");
assert_eq!(algorithm_field.attrs.asn1_type, None);
assert_eq!(algorithm_field.attrs.context_specific, None);
assert_eq!(algorithm_field.attrs.tag_mode, TagMode::Explicit);
let parameters_field = &ir.fields[1];
assert_eq!(parameters_field.ident, "parameters");
assert_eq!(parameters_field.attrs.asn1_type, None);
assert_eq!(parameters_field.attrs.context_specific, None);
assert_eq!(parameters_field.attrs.tag_mode, TagMode::Explicit);
}
/// X.509 `SubjectPublicKeyInfo`.
#[test]
fn spki_example() {
let input = parse_quote! {
#[derive(Sequence)]
pub struct SubjectPublicKeyInfo<'a> {
pub algorithm: AlgorithmIdentifier<'a>,
#[asn1(type = "BIT STRING")]
pub subject_public_key: &'a [u8],
}
};
let ir = DeriveSequence::new(input).unwrap();
assert_eq!(ir.ident, "SubjectPublicKeyInfo");
assert_eq!(
ir.generics.lifetimes().next().unwrap().lifetime.to_string(),
"'a"
);
assert_eq!(ir.fields.len(), 2);
let algorithm_field = &ir.fields[0];
assert_eq!(algorithm_field.ident, "algorithm");
assert_eq!(algorithm_field.attrs.asn1_type, None);
assert_eq!(algorithm_field.attrs.context_specific, None);
assert_eq!(algorithm_field.attrs.tag_mode, TagMode::Explicit);
let subject_public_key_field = &ir.fields[1];
assert_eq!(subject_public_key_field.ident, "subject_public_key");
assert_eq!(
subject_public_key_field.attrs.asn1_type,
Some(Asn1Type::BitString)
);
assert_eq!(subject_public_key_field.attrs.context_specific, None);
assert_eq!(subject_public_key_field.attrs.tag_mode, TagMode::Explicit);
}
/// PKCS#8v2 `OneAsymmetricKey`.
///
/// ```text
/// OneAsymmetricKey ::= SEQUENCE {
/// version Version,
/// privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
/// privateKey PrivateKey,
/// attributes [0] Attributes OPTIONAL,
/// ...,
/// [[2: publicKey [1] PublicKey OPTIONAL ]],
/// ...
/// }
///
/// Version ::= INTEGER { v1(0), v2(1) } (v1, ..., v2)
///
/// PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
///
/// PrivateKey ::= OCTET STRING
///
/// Attributes ::= SET OF Attribute
///
/// PublicKey ::= BIT STRING
/// ```
#[test]
fn pkcs8_example() {
let input = parse_quote! {
#[derive(Sequence)]
pub struct OneAsymmetricKey<'a> {
pub version: u8,
pub private_key_algorithm: AlgorithmIdentifier<'a>,
#[asn1(type = "OCTET STRING")]
pub private_key: &'a [u8],
#[asn1(context_specific = "0", extensible = "true", optional = "true")]
pub attributes: Option<SetOf<Any<'a>, 1>>,
#[asn1(
context_specific = "1",
extensible = "true",
optional = "true",
type = "BIT STRING"
)]
pub public_key: Option<&'a [u8]>,
}
};
let ir = DeriveSequence::new(input).unwrap();
assert_eq!(ir.ident, "OneAsymmetricKey");
assert_eq!(
ir.generics.lifetimes().next().unwrap().lifetime.to_string(),
"'a"
);
assert_eq!(ir.fields.len(), 5);
let version_field = &ir.fields[0];
assert_eq!(version_field.ident, "version");
assert_eq!(version_field.attrs.asn1_type, None);
assert_eq!(version_field.attrs.context_specific, None);
assert_eq!(version_field.attrs.extensible, false);
assert_eq!(version_field.attrs.optional, false);
assert_eq!(version_field.attrs.tag_mode, TagMode::Explicit);
let algorithm_field = &ir.fields[1];
assert_eq!(algorithm_field.ident, "private_key_algorithm");
assert_eq!(algorithm_field.attrs.asn1_type, None);
assert_eq!(algorithm_field.attrs.context_specific, None);
assert_eq!(algorithm_field.attrs.extensible, false);
assert_eq!(algorithm_field.attrs.optional, false);
assert_eq!(algorithm_field.attrs.tag_mode, TagMode::Explicit);
let private_key_field = &ir.fields[2];
assert_eq!(private_key_field.ident, "private_key");
assert_eq!(
private_key_field.attrs.asn1_type,
Some(Asn1Type::OctetString)
);
assert_eq!(private_key_field.attrs.context_specific, None);
assert_eq!(private_key_field.attrs.extensible, false);
assert_eq!(private_key_field.attrs.optional, false);
assert_eq!(private_key_field.attrs.tag_mode, TagMode::Explicit);
let attributes_field = &ir.fields[3];
assert_eq!(attributes_field.ident, "attributes");
assert_eq!(attributes_field.attrs.asn1_type, None);
assert_eq!(
attributes_field.attrs.context_specific,
Some("0".parse().unwrap())
);
assert_eq!(attributes_field.attrs.extensible, true);
assert_eq!(attributes_field.attrs.optional, true);
assert_eq!(attributes_field.attrs.tag_mode, TagMode::Explicit);
let public_key_field = &ir.fields[4];
assert_eq!(public_key_field.ident, "public_key");
assert_eq!(public_key_field.attrs.asn1_type, Some(Asn1Type::BitString));
assert_eq!(
public_key_field.attrs.context_specific,
Some("1".parse().unwrap())
);
assert_eq!(public_key_field.attrs.extensible, true);
assert_eq!(public_key_field.attrs.optional, true);
assert_eq!(public_key_field.attrs.tag_mode, TagMode::Explicit);
}
/// `IMPLICIT` tagged example
#[test]
fn implicit_example() {
let input = parse_quote! {
#[asn1(tag_mode = "IMPLICIT")]
pub struct ImplicitSequence<'a> {
#[asn1(context_specific = "0", type = "BIT STRING")]
bit_string: BitString<'a>,
#[asn1(context_specific = "1", type = "GeneralizedTime")]
time: GeneralizedTime,
#[asn1(context_specific = "2", type = "UTF8String")]
utf8_string: String,
}
};
let ir = DeriveSequence::new(input).unwrap();
assert_eq!(ir.ident, "ImplicitSequence");
assert_eq!(
ir.generics.lifetimes().next().unwrap().lifetime.to_string(),
"'a"
);
assert_eq!(ir.fields.len(), 3);
let bit_string = &ir.fields[0];
assert_eq!(bit_string.ident, "bit_string");
assert_eq!(bit_string.attrs.asn1_type, Some(Asn1Type::BitString));
assert_eq!(
bit_string.attrs.context_specific,
Some("0".parse().unwrap())
);
assert_eq!(bit_string.attrs.tag_mode, TagMode::Implicit);
let time = &ir.fields[1];
assert_eq!(time.ident, "time");
assert_eq!(time.attrs.asn1_type, Some(Asn1Type::GeneralizedTime));
assert_eq!(time.attrs.context_specific, Some("1".parse().unwrap()));
assert_eq!(time.attrs.tag_mode, TagMode::Implicit);
let utf8_string = &ir.fields[2];
assert_eq!(utf8_string.ident, "utf8_string");
assert_eq!(utf8_string.attrs.asn1_type, Some(Asn1Type::Utf8String));
assert_eq!(
utf8_string.attrs.context_specific,
Some("2".parse().unwrap())
);
assert_eq!(utf8_string.attrs.tag_mode, TagMode::Implicit);
}
}