| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
#![cfg(all(feature = "derive", feature = "alloc"))] |
| 11 |
#![allow( |
| 12 |
clippy::bool_assert_comparison, |
| 13 |
clippy::needless_question_mark, |
| 14 |
clippy::std_instead_of_core |
| 15 |
)] |
| 16 |
|
| 17 |
|
| 18 |
#[derive(Clone, Copy, Debug)] |
| 19 |
#[allow(dead_code)] |
| 20 |
pub struct CustomError(der::Error); |
| 21 |
|
| 22 |
impl core::error::Error for CustomError {} |
| 23 |
|
| 24 |
impl core::fmt::Display for CustomError { |
| 25 |
fn fmt(&self, _: &mut core::fmt::Formatter) -> core::fmt::Result { |
| 26 |
unimplemented!() |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
impl From<der::Error> for CustomError { |
| 31 |
fn from(value: der::Error) -> Self { |
| 32 |
Self(value) |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
impl From<core::convert::Infallible> for CustomError { |
| 37 |
fn from(_value: std::convert::Infallible) -> Self { |
| 38 |
unreachable!() |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
mod choice { |
| 44 |
use super::CustomError; |
| 45 |
|
| 46 |
|
| 47 |
mod explicit { |
| 48 |
use super::CustomError; |
| 49 |
use der::{ |
| 50 |
Choice, Decode, Encode, SliceWriter, |
| 51 |
asn1::{GeneralizedTime, UtcTime}, |
| 52 |
}; |
| 53 |
use hex_literal::hex; |
| 54 |
use std::time::Duration; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
#[derive(Choice)] |
| 67 |
pub enum Time { |
| 68 |
#[asn1(type = "UTCTime")] |
| 69 |
UtcTime(UtcTime), |
| 70 |
|
| 71 |
#[asn1(type = "GeneralizedTime")] |
| 72 |
GeneralTime(GeneralizedTime), |
| 73 |
} |
| 74 |
|
| 75 |
impl Time { |
| 76 |
fn to_unix_duration(&self) -> Duration { |
| 77 |
match self { |
| 78 |
Time::UtcTime(t) => t.to_unix_duration(), |
| 79 |
Time::GeneralTime(t) => t.to_unix_duration(), |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
#[derive(Choice)] |
| 85 |
#[asn1(error = CustomError)] |
| 86 |
pub enum WithCustomError { |
| 87 |
#[asn1(type = "GeneralizedTime")] |
| 88 |
Foo(GeneralizedTime), |
| 89 |
} |
| 90 |
|
| 91 |
const UTC_TIMESTAMP_DER: &[u8] = &hex!("17 0d 39 31 30 35 30 36 32 33 34 35 34 30 5a"); |
| 92 |
const GENERAL_TIMESTAMP_DER: &[u8] = |
| 93 |
&hex!("18 0f 31 39 39 31 30 35 30 36 32 33 34 35 34 30 5a"); |
| 94 |
|
| 95 |
#[test] |
| 96 |
fn decode() { |
| 97 |
let utc_time = Time::from_der(UTC_TIMESTAMP_DER).unwrap(); |
| 98 |
assert_eq!(utc_time.to_unix_duration().as_secs(), 673573540); |
| 99 |
|
| 100 |
let general_time = Time::from_der(GENERAL_TIMESTAMP_DER).unwrap(); |
| 101 |
assert_eq!(general_time.to_unix_duration().as_secs(), 673573540); |
| 102 |
|
| 103 |
let WithCustomError::Foo(with_custom_error) = |
| 104 |
WithCustomError::from_der(GENERAL_TIMESTAMP_DER).unwrap(); |
| 105 |
assert_eq!(with_custom_error.to_unix_duration().as_secs(), 673573540); |
| 106 |
} |
| 107 |
|
| 108 |
#[test] |
| 109 |
fn encode() { |
| 110 |
let mut buf = [0u8; 128]; |
| 111 |
|
| 112 |
let utc_time = Time::from_der(UTC_TIMESTAMP_DER).unwrap(); |
| 113 |
let mut writer = SliceWriter::new(&mut buf); |
| 114 |
utc_time.encode(&mut writer).unwrap(); |
| 115 |
assert_eq!(UTC_TIMESTAMP_DER, writer.finish().unwrap()); |
| 116 |
|
| 117 |
let general_time = Time::from_der(GENERAL_TIMESTAMP_DER).unwrap(); |
| 118 |
let mut writer = SliceWriter::new(&mut buf); |
| 119 |
general_time.encode(&mut writer).unwrap(); |
| 120 |
assert_eq!(GENERAL_TIMESTAMP_DER, writer.finish().unwrap()); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
#[cfg(feature = "heapless")] |
| 126 |
mod implicit { |
| 127 |
use der::asn1::Null; |
| 128 |
use der::{ |
| 129 |
Choice, Decode, Encode, Sequence, SliceWriter, |
| 130 |
asn1::{BitStringRef, GeneralizedTime, SequenceOf}, |
| 131 |
}; |
| 132 |
use hex_literal::hex; |
| 133 |
|
| 134 |
|
| 135 |
#[derive(Choice, Debug, Eq, PartialEq)] |
| 136 |
#[asn1(tag_mode = "IMPLICIT")] |
| 137 |
pub enum ImplicitChoice<'a> { |
| 138 |
#[asn1(context_specific = "0", type = "BIT STRING")] |
| 139 |
BitString(BitStringRef<'a>), |
| 140 |
|
| 141 |
#[asn1(context_specific = "1", type = "GeneralizedTime")] |
| 142 |
Time(GeneralizedTime), |
| 143 |
|
| 144 |
#[asn1(context_specific = "2", type = "UTF8String")] |
| 145 |
Utf8String(String), |
| 146 |
|
| 147 |
#[asn1(context_specific = "3", constructed = "true")] |
| 148 |
SequenceOfNulls(SequenceOf<Null, 1>), |
| 149 |
} |
| 150 |
|
| 151 |
impl<'a> ImplicitChoice<'a> { |
| 152 |
pub fn bit_string(&self) -> Option<BitStringRef<'a>> { |
| 153 |
match self { |
| 154 |
Self::BitString(bs) => Some(*bs), |
| 155 |
_ => None, |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
pub fn time(&self) -> Option<GeneralizedTime> { |
| 160 |
match self { |
| 161 |
Self::Time(time) => Some(*time), |
| 162 |
_ => None, |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
const BITSTRING_DER: &[u8] = &hex!("80 04 00 01 02 03"); |
| 168 |
const TIME_DER: &[u8] = &hex!("81 0f 31 39 39 31 30 35 30 36 32 33 34 35 34 30 5a"); |
| 169 |
|
| 170 |
#[test] |
| 171 |
fn decode() { |
| 172 |
let cs_bit_string = ImplicitChoice::from_der(BITSTRING_DER).unwrap(); |
| 173 |
assert_eq!( |
| 174 |
cs_bit_string.bit_string().unwrap().as_bytes().unwrap(), |
| 175 |
&[1, 2, 3] |
| 176 |
); |
| 177 |
|
| 178 |
let cs_time = ImplicitChoice::from_der(TIME_DER).unwrap(); |
| 179 |
assert_eq!( |
| 180 |
cs_time.time().unwrap().to_unix_duration().as_secs(), |
| 181 |
673573540 |
| 182 |
); |
| 183 |
} |
| 184 |
|
| 185 |
#[test] |
| 186 |
fn encode() { |
| 187 |
let mut buf = [0u8; 128]; |
| 188 |
|
| 189 |
let cs_bit_string = ImplicitChoice::from_der(BITSTRING_DER).unwrap(); |
| 190 |
let mut writer = SliceWriter::new(&mut buf); |
| 191 |
cs_bit_string.encode(&mut writer).unwrap(); |
| 192 |
assert_eq!(BITSTRING_DER, writer.finish().unwrap()); |
| 193 |
|
| 194 |
let cs_time = ImplicitChoice::from_der(TIME_DER).unwrap(); |
| 195 |
let mut writer = SliceWriter::new(&mut buf); |
| 196 |
cs_time.encode(&mut writer).unwrap(); |
| 197 |
assert_eq!(TIME_DER, writer.finish().unwrap()); |
| 198 |
} |
| 199 |
|
| 200 |
#[test] |
| 201 |
fn roundtrip_implicit_constructed_variant() { |
| 202 |
let mut seq = SequenceOf::new(); |
| 203 |
seq.add(Null).unwrap(); |
| 204 |
let obj = ImplicitChoice::SequenceOfNulls(seq); |
| 205 |
let mut buf = [0u8; 128]; |
| 206 |
|
| 207 |
let mut writer = SliceWriter::new(&mut buf); |
| 208 |
obj.encode(&mut writer).unwrap(); |
| 209 |
|
| 210 |
let encoded = writer.finish().unwrap(); |
| 211 |
println!("encoded: {encoded:02X?}"); |
| 212 |
|
| 213 |
let decoded = ImplicitChoice::from_der(encoded).unwrap(); |
| 214 |
|
| 215 |
assert_eq!(decoded, obj); |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
#[derive(Sequence, Debug, Eq, PartialEq)] |
| 220 |
#[allow(dead_code)] |
| 221 |
pub struct ExplicitChoiceInsideSequence<'a> { |
| 222 |
#[asn1(tag_mode = "EXPLICIT", context_specific = "0")] |
| 223 |
choice_field: ImplicitChoice<'a>, |
| 224 |
} |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
mod enumerated { |
| 230 |
use super::CustomError; |
| 231 |
use der::{Decode, Encode, Enumerated, SliceWriter}; |
| 232 |
use hex_literal::hex; |
| 233 |
|
| 234 |
|
| 235 |
#[derive(Enumerated, Copy, Clone, Debug, Eq, PartialEq)] |
| 236 |
#[repr(u32)] |
| 237 |
pub enum CrlReason { |
| 238 |
Unspecified = 0, |
| 239 |
KeyCompromise = 1, |
| 240 |
CaCompromise = 2, |
| 241 |
AffiliationChanged = 3, |
| 242 |
Superseded = 4, |
| 243 |
CessationOfOperation = 5, |
| 244 |
CertificateHold = 6, |
| 245 |
RemoveFromCrl = 8, |
| 246 |
PrivilegeWithdrawn = 9, |
| 247 |
AaCompromised = 10, |
| 248 |
} |
| 249 |
|
| 250 |
const UNSPECIFIED_DER: &[u8] = &hex!("0a 01 00"); |
| 251 |
const KEY_COMPROMISE_DER: &[u8] = &hex!("0a 01 01"); |
| 252 |
|
| 253 |
#[derive(Enumerated, Copy, Clone, Eq, PartialEq, Debug)] |
| 254 |
#[asn1(error = CustomError)] |
| 255 |
#[repr(u32)] |
| 256 |
pub enum EnumWithCustomError { |
| 257 |
Unspecified = 0, |
| 258 |
Specified = 1, |
| 259 |
} |
| 260 |
|
| 261 |
#[test] |
| 262 |
fn decode() { |
| 263 |
let unspecified = CrlReason::from_der(UNSPECIFIED_DER).unwrap(); |
| 264 |
assert_eq!(CrlReason::Unspecified, unspecified); |
| 265 |
|
| 266 |
let key_compromise = CrlReason::from_der(KEY_COMPROMISE_DER).unwrap(); |
| 267 |
assert_eq!(CrlReason::KeyCompromise, key_compromise); |
| 268 |
|
| 269 |
let custom_error_enum = EnumWithCustomError::from_der(UNSPECIFIED_DER).unwrap(); |
| 270 |
assert_eq!(custom_error_enum, EnumWithCustomError::Unspecified); |
| 271 |
} |
| 272 |
|
| 273 |
#[test] |
| 274 |
fn encode() { |
| 275 |
let mut buf = [0u8; 128]; |
| 276 |
|
| 277 |
let mut writer = SliceWriter::new(&mut buf); |
| 278 |
CrlReason::Unspecified.encode(&mut writer).unwrap(); |
| 279 |
assert_eq!(UNSPECIFIED_DER, writer.finish().unwrap()); |
| 280 |
|
| 281 |
let mut writer = SliceWriter::new(&mut buf); |
| 282 |
CrlReason::KeyCompromise.encode(&mut writer).unwrap(); |
| 283 |
assert_eq!(KEY_COMPROMISE_DER, writer.finish().unwrap()); |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
#[cfg(all(feature = "heapless", feature = "oid"))] |
| 289 |
mod sequence { |
| 290 |
use super::CustomError; |
| 291 |
use core::marker::PhantomData; |
| 292 |
use der::{ |
| 293 |
Decode, Encode, Sequence, ValueOrd, |
| 294 |
asn1::{AnyRef, ObjectIdentifier, SetOf}, |
| 295 |
}; |
| 296 |
use hex_literal::hex; |
| 297 |
|
| 298 |
pub fn default_false_example() -> bool { |
| 299 |
false |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
#[derive(Sequence, Default)] |
| 318 |
pub struct IssuingDistributionPointExample { |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
#[asn1( |
| 326 |
context_specific = "1", |
| 327 |
default = "default_false_example", |
| 328 |
tag_mode = "IMPLICIT" |
| 329 |
)] |
| 330 |
pub only_contains_user_certs: bool, |
| 331 |
|
| 332 |
|
| 333 |
#[asn1( |
| 334 |
context_specific = "2", |
| 335 |
default = "default_false_example", |
| 336 |
tag_mode = "IMPLICIT" |
| 337 |
)] |
| 338 |
pub only_contains_cacerts: bool, |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
#[asn1( |
| 345 |
context_specific = "4", |
| 346 |
default = "default_false_example", |
| 347 |
tag_mode = "IMPLICIT" |
| 348 |
)] |
| 349 |
pub indirect_crl: bool, |
| 350 |
|
| 351 |
|
| 352 |
#[asn1( |
| 353 |
context_specific = "5", |
| 354 |
default = "default_false_example", |
| 355 |
tag_mode = "IMPLICIT" |
| 356 |
)] |
| 357 |
pub only_contains_attribute_certs: bool, |
| 358 |
|
| 359 |
|
| 360 |
pub phantom: PhantomData<()>, |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
#[derive(Clone, Debug, Eq, PartialEq, Sequence)] |
| 380 |
pub struct ExtensionExample<'a> { |
| 381 |
|
| 382 |
pub extn_id: ObjectIdentifier, |
| 383 |
|
| 384 |
|
| 385 |
#[asn1(default = "default_false_example")] |
| 386 |
pub critical: bool, |
| 387 |
|
| 388 |
|
| 389 |
#[asn1(type = "OCTET STRING")] |
| 390 |
pub extn_value: &'a [u8], |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
#[derive(Copy, Clone, Debug, Eq, PartialEq, Sequence, ValueOrd)] |
| 395 |
pub struct AlgorithmIdentifier<'a> { |
| 396 |
pub algorithm: ObjectIdentifier, |
| 397 |
pub parameters: Option<AnyRef<'a>>, |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
#[derive(Copy, Clone, Debug, Eq, PartialEq, Sequence, ValueOrd)] |
| 402 |
pub struct SubjectPublicKeyInfo<'a> { |
| 403 |
pub algorithm: AlgorithmIdentifier<'a>, |
| 404 |
#[asn1(type = "BIT STRING")] |
| 405 |
pub subject_public_key: &'a [u8], |
| 406 |
} |
| 407 |
|
| 408 |
#[test] |
| 409 |
fn decode_spki() { |
| 410 |
let spki_bytes = hex!( |
| 411 |
|
| 412 |
"30 1A |
| 413 |
30 0D |
| 414 |
06 09 |
| 415 |
2A 86 48 86 F7 0D 01 01 01 |
| 416 |
05 00 |
| 417 |
03 09 |
| 418 |
00 A0 A1 A2 A3 A4 A5 A6 A7" |
| 419 |
|
| 420 |
"30 1A |
| 421 |
30 0D |
| 422 |
06 09 |
| 423 |
2A 86 48 86 F7 0D 01 01 01 |
| 424 |
05 00 |
| 425 |
03 09 |
| 426 |
00 B0 B1 B2 B3 B4 B5 B6 B7"); |
| 427 |
|
| 428 |
|
| 429 |
let (spki, remaining) = SubjectPublicKeyInfo::from_der_partial(&spki_bytes).unwrap(); |
| 430 |
assert_eq!(spki.subject_public_key, hex!("A0 A1 A2 A3 A4 A5 A6 A7")); |
| 431 |
|
| 432 |
|
| 433 |
let (spki, _) = SubjectPublicKeyInfo::from_der_partial(remaining).unwrap(); |
| 434 |
assert_eq!(spki.subject_public_key, hex!("B0 B1 B2 B3 B4 B5 B6 B7")); |
| 435 |
} |
| 436 |
|
| 437 |
|
| 438 |
#[derive(Sequence)] |
| 439 |
#[allow(dead_code)] |
| 440 |
pub struct OneAsymmetricKey<'a> { |
| 441 |
pub version: u8, |
| 442 |
pub private_key_algorithm: AlgorithmIdentifier<'a>, |
| 443 |
#[asn1(type = "OCTET STRING")] |
| 444 |
pub private_key: &'a [u8], |
| 445 |
#[asn1(context_specific = "0", extensible = "true", optional = "true")] |
| 446 |
pub attributes: Option<SetOf<AnyRef<'a>, 1>>, |
| 447 |
#[asn1( |
| 448 |
context_specific = "1", |
| 449 |
extensible = "true", |
| 450 |
optional = "true", |
| 451 |
type = "BIT STRING" |
| 452 |
)] |
| 453 |
pub public_key: Option<&'a [u8]>, |
| 454 |
} |
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
#[derive(Clone, Debug, Eq, PartialEq, Sequence, ValueOrd)] |
| 459 |
#[allow(dead_code)] |
| 460 |
pub struct Extension<'a> { |
| 461 |
extn_id: ObjectIdentifier, |
| 462 |
#[asn1(default = "critical_default")] |
| 463 |
critical: bool, |
| 464 |
#[asn1(type = "OCTET STRING")] |
| 465 |
extn_value: &'a [u8], |
| 466 |
} |
| 467 |
|
| 468 |
|
| 469 |
#[allow(dead_code)] |
| 470 |
fn critical_default() -> bool { |
| 471 |
false |
| 472 |
} |
| 473 |
|
| 474 |
const ID_EC_PUBLIC_KEY_OID: ObjectIdentifier = |
| 475 |
ObjectIdentifier::new_unwrap("1.2.840.10045.2.1"); |
| 476 |
|
| 477 |
const PRIME256V1_OID: ObjectIdentifier = ObjectIdentifier::new_unwrap("1.2.840.10045.3.1.7"); |
| 478 |
|
| 479 |
const ALGORITHM_IDENTIFIER_DER: &[u8] = |
| 480 |
&hex!("30 13 06 07 2a 86 48 ce 3d 02 01 06 08 2a 86 48 ce 3d 03 01 07"); |
| 481 |
|
| 482 |
#[derive(Sequence, Default, Eq, PartialEq, Debug)] |
| 483 |
#[asn1(tag_mode = "IMPLICIT")] |
| 484 |
pub struct TypeCheckExpandedSequenceFieldAttributeCombinations<'a> { |
| 485 |
pub simple: bool, |
| 486 |
#[asn1(type = "BIT STRING")] |
| 487 |
pub typed: &'a [u8], |
| 488 |
#[asn1(context_specific = "0")] |
| 489 |
pub context_specific: bool, |
| 490 |
|
| 491 |
#[asn1(optional = "true")] |
| 492 |
pub optional: Option<bool>, |
| 493 |
|
| 494 |
#[asn1(type = "OCTET STRING", optional = "true")] |
| 495 |
pub optional_octet_string: Option<&'a [u8]>, |
| 496 |
|
| 497 |
#[asn1(type = "BIT STRING", optional = "true")] |
| 498 |
pub optional_bit_string: Option<&'a [u8]>, |
| 499 |
|
| 500 |
#[asn1(optional = "true")] |
| 501 |
pub optional_oid: Option<ObjectIdentifier>, |
| 502 |
|
| 503 |
#[asn1(default = "default_false_example")] |
| 504 |
pub default: bool, |
| 505 |
#[asn1(type = "BIT STRING", context_specific = "1")] |
| 506 |
pub typed_context_specific: &'a [u8], |
| 507 |
#[asn1(context_specific = "2", optional = "true")] |
| 508 |
pub context_specific_optional: Option<bool>, |
| 509 |
#[asn1(context_specific = "3", default = "default_false_example")] |
| 510 |
pub context_specific_default: bool, |
| 511 |
#[asn1(type = "BIT STRING", context_specific = "4", optional = "true")] |
| 512 |
pub typed_context_specific_optional_bits: Option<&'a [u8]>, |
| 513 |
#[asn1(type = "OCTET STRING", context_specific = "5", optional = "true")] |
| 514 |
pub typed_context_specific_optional_implicit: Option<&'a [u8]>, |
| 515 |
#[asn1( |
| 516 |
type = "OCTET STRING", |
| 517 |
context_specific = "6", |
| 518 |
optional = "true", |
| 519 |
tag_mode = "EXPLICIT" |
| 520 |
)] |
| 521 |
pub typed_context_specific_optional_explicit: Option<&'a [u8]>, |
| 522 |
} |
| 523 |
|
| 524 |
#[test] |
| 525 |
fn type_combinations_instance() { |
| 526 |
let obj = TypeCheckExpandedSequenceFieldAttributeCombinations { |
| 527 |
optional: Some(true), |
| 528 |
optional_octet_string: Some(&[0xAA, 0xBB]), |
| 529 |
optional_bit_string: Some(&[0xCC, 0xDD]), |
| 530 |
context_specific_optional: Some(true), |
| 531 |
typed_context_specific: &[0, 1], |
| 532 |
typed_context_specific_optional_bits: Some(&[2, 3]), |
| 533 |
typed_context_specific_optional_implicit: Some(&[4, 5, 6]), |
| 534 |
typed_context_specific_optional_explicit: Some(&[7, 8]), |
| 535 |
|
| 536 |
..Default::default() |
| 537 |
}; |
| 538 |
|
| 539 |
let der_encoded = obj.to_der().unwrap(); |
| 540 |
|
| 541 |
let obj_decoded = |
| 542 |
TypeCheckExpandedSequenceFieldAttributeCombinations::from_der(&der_encoded).unwrap(); |
| 543 |
assert_eq!(obj, obj_decoded); |
| 544 |
} |
| 545 |
|
| 546 |
#[derive(Sequence, Default, Eq, PartialEq, Debug)] |
| 547 |
#[asn1(tag_mode = "IMPLICIT")] |
| 548 |
pub struct TypeCheckOwnedSequenceFieldAttributeCombinations { |
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
#[asn1(type = "OCTET STRING", deref = "true")] |
| 553 |
pub owned_bytes: Vec<u8>, |
| 554 |
|
| 555 |
#[asn1(type = "BIT STRING", deref = "true")] |
| 556 |
pub owned_bits: Vec<u8>, |
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
#[asn1(type = "OCTET STRING", context_specific = "0", deref = "true")] |
| 561 |
pub owned_implicit_bytes: Vec<u8>, |
| 562 |
|
| 563 |
|
| 564 |
#[asn1(type = "BIT STRING", context_specific = "1", deref = "true")] |
| 565 |
pub owned_implicit_bits: Vec<u8>, |
| 566 |
|
| 567 |
|
| 568 |
#[asn1( |
| 569 |
type = "OCTET STRING", |
| 570 |
context_specific = "2", |
| 571 |
deref = "true", |
| 572 |
tag_mode = "EXPLICIT" |
| 573 |
)] |
| 574 |
pub owned_explicit_bytes: Vec<u8>, |
| 575 |
|
| 576 |
|
| 577 |
#[asn1( |
| 578 |
type = "BIT STRING", |
| 579 |
context_specific = "3", |
| 580 |
deref = "true", |
| 581 |
tag_mode = "EXPLICIT" |
| 582 |
)] |
| 583 |
pub owned_explicit_bits: Vec<u8>, |
| 584 |
|
| 585 |
|
| 586 |
#[asn1(type = "BIT STRING", context_specific = "4", optional = "true")] |
| 587 |
pub owned_optional_implicit_bits: Option<Vec<u8>>, |
| 588 |
#[asn1(type = "OCTET STRING", context_specific = "5", optional = "true")] |
| 589 |
pub owned_optional_implicit_bytes: Option<Vec<u8>>, |
| 590 |
|
| 591 |
#[asn1( |
| 592 |
type = "BIT STRING", |
| 593 |
context_specific = "6", |
| 594 |
optional = "true", |
| 595 |
tag_mode = "EXPLICIT" |
| 596 |
)] |
| 597 |
pub owned_optional_explicit_bits: Option<Vec<u8>>, |
| 598 |
#[asn1( |
| 599 |
type = "OCTET STRING", |
| 600 |
context_specific = "7", |
| 601 |
optional = "true", |
| 602 |
tag_mode = "EXPLICIT" |
| 603 |
)] |
| 604 |
pub owned_optional_explicit_bytes: Option<Vec<u8>>, |
| 605 |
} |
| 606 |
|
| 607 |
#[test] |
| 608 |
fn type_combinations_alloc_instance() { |
| 609 |
let obj = TypeCheckOwnedSequenceFieldAttributeCombinations { |
| 610 |
owned_bytes: vec![0xAA, 0xBB], |
| 611 |
owned_bits: vec![0xCC, 0xDD], |
| 612 |
|
| 613 |
owned_implicit_bytes: vec![0, 1], |
| 614 |
owned_implicit_bits: vec![2, 3], |
| 615 |
|
| 616 |
owned_explicit_bytes: vec![4, 5], |
| 617 |
owned_explicit_bits: vec![6, 7], |
| 618 |
|
| 619 |
owned_optional_implicit_bits: Some(vec![8, 9]), |
| 620 |
owned_optional_implicit_bytes: Some(vec![10, 11]), |
| 621 |
|
| 622 |
owned_optional_explicit_bits: Some(vec![12, 13]), |
| 623 |
owned_optional_explicit_bytes: Some(vec![14, 15]), |
| 624 |
}; |
| 625 |
|
| 626 |
let der_encoded = obj.to_der().unwrap(); |
| 627 |
let obj_decoded = |
| 628 |
TypeCheckOwnedSequenceFieldAttributeCombinations::from_der(&der_encoded).unwrap(); |
| 629 |
assert_eq!(obj, obj_decoded); |
| 630 |
} |
| 631 |
|
| 632 |
#[derive(Sequence)] |
| 633 |
#[asn1(error = CustomError)] |
| 634 |
pub struct TypeWithCustomError { |
| 635 |
pub simple: bool, |
| 636 |
} |
| 637 |
|
| 638 |
#[test] |
| 639 |
fn idp_test() { |
| 640 |
let idp = IssuingDistributionPointExample::from_der(&hex!("30038101FF")).unwrap(); |
| 641 |
assert_eq!(idp.only_contains_user_certs, true); |
| 642 |
assert_eq!(idp.only_contains_cacerts, false); |
| 643 |
assert_eq!(idp.indirect_crl, false); |
| 644 |
assert_eq!(idp.only_contains_attribute_certs, false); |
| 645 |
|
| 646 |
let idp = IssuingDistributionPointExample::from_der(&hex!("30038201FF")).unwrap(); |
| 647 |
assert_eq!(idp.only_contains_user_certs, false); |
| 648 |
assert_eq!(idp.only_contains_cacerts, true); |
| 649 |
assert_eq!(idp.indirect_crl, false); |
| 650 |
assert_eq!(idp.only_contains_attribute_certs, false); |
| 651 |
|
| 652 |
let idp = IssuingDistributionPointExample::from_der(&hex!("30038401FF")).unwrap(); |
| 653 |
assert_eq!(idp.only_contains_user_certs, false); |
| 654 |
assert_eq!(idp.only_contains_cacerts, false); |
| 655 |
assert_eq!(idp.indirect_crl, true); |
| 656 |
assert_eq!(idp.only_contains_attribute_certs, false); |
| 657 |
|
| 658 |
let idp = IssuingDistributionPointExample::from_der(&hex!("30038501FF")).unwrap(); |
| 659 |
assert_eq!(idp.only_contains_user_certs, false); |
| 660 |
assert_eq!(idp.only_contains_cacerts, false); |
| 661 |
assert_eq!(idp.indirect_crl, false); |
| 662 |
assert_eq!(idp.only_contains_attribute_certs, true); |
| 663 |
} |
| 664 |
|
| 665 |
#[test] |
| 666 |
fn idp_encode_twice() { |
| 667 |
let mut vec_buf = Vec::new(); |
| 668 |
|
| 669 |
IssuingDistributionPointExample { |
| 670 |
only_contains_user_certs: true, |
| 671 |
..Default::default() |
| 672 |
} |
| 673 |
.encode_to_vec(&mut vec_buf) |
| 674 |
.unwrap(); |
| 675 |
|
| 676 |
|
| 677 |
IssuingDistributionPointExample { |
| 678 |
only_contains_cacerts: true, |
| 679 |
..Default::default() |
| 680 |
} |
| 681 |
.encode_to_vec(&mut vec_buf) |
| 682 |
.unwrap(); |
| 683 |
|
| 684 |
assert_eq!(vec_buf, hex!("30038101FF 30038201FF")); |
| 685 |
} |
| 686 |
|
| 687 |
|
| 688 |
#[test] |
| 689 |
fn extension_test() { |
| 690 |
let ext1 = ExtensionExample::from_der(&hex!( |
| 691 |
"300F" |
| 692 |
"0603551D13" |
| 693 |
"0101FF" |
| 694 |
"0405" |
| 695 |
"3003" |
| 696 |
"0101FF" |
| 697 |
)) |
| 698 |
.unwrap(); |
| 699 |
assert_eq!(ext1.critical, true); |
| 700 |
|
| 701 |
let ext2 = ExtensionExample::from_der(&hex!( |
| 702 |
"301F" |
| 703 |
"0603551D23" |
| 704 |
"0418" |
| 705 |
"3016" |
| 706 |
"8014E47D5FD15C9586082C05AEBE75B665A7D95DA866" |
| 707 |
)) |
| 708 |
.unwrap(); |
| 709 |
assert_eq!(ext2.critical, false); |
| 710 |
} |
| 711 |
|
| 712 |
#[test] |
| 713 |
fn decode() { |
| 714 |
let algorithm_identifier = AlgorithmIdentifier::from_der(ALGORITHM_IDENTIFIER_DER).unwrap(); |
| 715 |
|
| 716 |
assert_eq!(ID_EC_PUBLIC_KEY_OID, algorithm_identifier.algorithm); |
| 717 |
assert_eq!( |
| 718 |
PRIME256V1_OID, |
| 719 |
ObjectIdentifier::try_from(algorithm_identifier.parameters.unwrap()).unwrap() |
| 720 |
); |
| 721 |
|
| 722 |
let t = TypeWithCustomError::from_der(&hex!("30030101FF")).unwrap(); |
| 723 |
assert!(t.simple); |
| 724 |
} |
| 725 |
|
| 726 |
#[test] |
| 727 |
fn encode() { |
| 728 |
let parameters_oid = PRIME256V1_OID; |
| 729 |
|
| 730 |
let algorithm_identifier = AlgorithmIdentifier { |
| 731 |
algorithm: ID_EC_PUBLIC_KEY_OID, |
| 732 |
parameters: Some(AnyRef::from(¶meters_oid)), |
| 733 |
}; |
| 734 |
|
| 735 |
assert_eq!( |
| 736 |
ALGORITHM_IDENTIFIER_DER, |
| 737 |
algorithm_identifier.to_der().unwrap() |
| 738 |
); |
| 739 |
} |
| 740 |
} |
| 741 |
|
| 742 |
|
| 743 |
mod encode_value { |
| 744 |
use der::{Encode, EncodeValue, FixedTag, Tag}; |
| 745 |
use hex_literal::hex; |
| 746 |
|
| 747 |
#[derive(EncodeValue, Default, Eq, PartialEq, Debug)] |
| 748 |
#[asn1(tag_mode = "IMPLICIT")] |
| 749 |
pub struct EncodeOnlyCheck<'a> { |
| 750 |
#[asn1(type = "OCTET STRING", context_specific = "5")] |
| 751 |
pub field: &'a [u8], |
| 752 |
} |
| 753 |
impl FixedTag for EncodeOnlyCheck<'_> { |
| 754 |
const TAG: Tag = Tag::Sequence; |
| 755 |
} |
| 756 |
|
| 757 |
#[test] |
| 758 |
fn sequence_encode_only_to_der() { |
| 759 |
let obj = EncodeOnlyCheck { |
| 760 |
field: &[0x33, 0x44], |
| 761 |
}; |
| 762 |
|
| 763 |
let der_encoded = obj.to_der().unwrap(); |
| 764 |
|
| 765 |
assert_eq!(der_encoded, hex!("30 04 85 02 33 44")); |
| 766 |
} |
| 767 |
} |
| 768 |
|
| 769 |
|
| 770 |
mod decode_value { |
| 771 |
use der::{Decode, DecodeValue, FixedTag, Tag}; |
| 772 |
use hex_literal::hex; |
| 773 |
|
| 774 |
#[derive(DecodeValue, Default, Eq, PartialEq, Debug)] |
| 775 |
#[asn1(tag_mode = "IMPLICIT")] |
| 776 |
pub struct DecodeOnlyCheck<'a> { |
| 777 |
#[asn1(type = "OCTET STRING", context_specific = "5")] |
| 778 |
pub field: &'a [u8], |
| 779 |
} |
| 780 |
impl FixedTag for DecodeOnlyCheck<'_> { |
| 781 |
const TAG: Tag = Tag::Sequence; |
| 782 |
} |
| 783 |
|
| 784 |
#[test] |
| 785 |
fn sequence_decode_only_from_der() { |
| 786 |
let obj = DecodeOnlyCheck::from_der(&hex!("30 04 85 02 33 44")).unwrap(); |
| 787 |
|
| 788 |
assert_eq!(obj.field, &[0x33, 0x44]); |
| 789 |
} |
| 790 |
} |
| 791 |
|
| 792 |
|
| 793 |
mod decode_encode_value { |
| 794 |
use der::{DecodeValue, EncodeValue, IsConstructed}; |
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
#[derive(DecodeValue, EncodeValue, Default, Eq, PartialEq, Debug)] |
| 799 |
#[allow(dead_code)] |
| 800 |
struct DecodeEncodeCheck { |
| 801 |
field: bool, |
| 802 |
} |
| 803 |
impl IsConstructed for DecodeEncodeCheck { |
| 804 |
const CONSTRUCTED: bool = true; |
| 805 |
} |
| 806 |
} |
| 807 |
|
| 808 |
#[cfg(all(feature = "derive", feature = "oid"))] |
| 809 |
mod custom_application { |
| 810 |
use const_oid::ObjectIdentifier; |
| 811 |
use der::{Decode, DecodeValue, Encode, EncodeValue, FixedTag, Sequence, Tag, TagNumber}; |
| 812 |
use hex_literal::hex; |
| 813 |
|
| 814 |
const TACHO_CERT_DER: &[u8] = &hex!( |
| 815 |
"7F 21 81 C8" |
| 816 |
|
| 817 |
"7F 4E 81 81" |
| 818 |
|
| 819 |
"5F 29" |
| 820 |
"01 00" |
| 821 |
"42 08" |
| 822 |
"FD 45 43 20 01 FF FF 01" |
| 823 |
"5F 4C 07" |
| 824 |
"FF 53 4D 52 44 54 0E" |
| 825 |
"7F 49 4D" |
| 826 |
"06 08 2A 86 48 CE 3D 03 01 07 86 41 04 |
| 827 |
30 E8 EE D8 05 1D FB 8F 05 BF 4E 34 90 B8 A0 1C |
| 828 |
83 21 37 4E 99 41 67 70 64 28 23 A2 C9 E1 21 16 |
| 829 |
D9 27 46 45 94 DD CB CC 79 42 B5 F3 EE 1A A3 AB |
| 830 |
A2 5C E1 6B 20 92 00 F0 09 70 D9 CF 83 0A 33 4B" |
| 831 |
|
| 832 |
|
| 833 |
"5F 20 08" |
| 834 |
"17 47 52 20 02 FF FF 01" |
| 835 |
"5F 25 04" |
| 836 |
"62 A3 B0 D0" |
| 837 |
"5F 24 04" |
| 838 |
"6F F6 49 50" |
| 839 |
"5F 37 40" |
| 840 |
"6D 3E FD 97 |
| 841 |
BE 83 EC 65 5F 51 4D 8C 47 60 DB FD 9B A2 D1 5D |
| 842 |
3C 1A 21 93 CE D7 EA F2 A2 0D 89 CC 4A 4F 0C 4B |
| 843 |
E5 3F A3 F9 0F 20 B5 74 67 26 DB 19 9E FF DE 0B |
| 844 |
D0 B9 2C B9 D1 5A E2 18 08 6C F0 E2" |
| 845 |
); |
| 846 |
|
| 847 |
|
| 848 |
#[derive(DecodeValue, EncodeValue)] |
| 849 |
#[asn1(tag_mode = "IMPLICIT")] |
| 850 |
pub struct TachographCertificate<'a> { |
| 851 |
|
| 852 |
#[asn1(application = "78")] |
| 853 |
pub body: TachographCertificateBody<'a>, |
| 854 |
|
| 855 |
|
| 856 |
#[asn1(application = "55", type = "OCTET STRING")] |
| 857 |
pub signature: &'a [u8], |
| 858 |
} |
| 859 |
|
| 860 |
impl FixedTag for TachographCertificate<'_> { |
| 861 |
const TAG: Tag = Tag::Application { |
| 862 |
number: TagNumber(33), |
| 863 |
constructed: true, |
| 864 |
}; |
| 865 |
} |
| 866 |
|
| 867 |
|
| 868 |
#[derive(Sequence)] |
| 869 |
#[asn1(tag_mode = "IMPLICIT")] |
| 870 |
pub struct TachographCertificateBody<'a> { |
| 871 |
|
| 872 |
#[asn1(application = "41", type = "OCTET STRING")] |
| 873 |
pub profile_identifier: &'a [u8], |
| 874 |
|
| 875 |
|
| 876 |
#[asn1(application = "2", type = "OCTET STRING")] |
| 877 |
pub authority_reference: &'a [u8], |
| 878 |
|
| 879 |
|
| 880 |
#[asn1(application = "76", type = "OCTET STRING")] |
| 881 |
pub holder_authorisation: &'a [u8], |
| 882 |
|
| 883 |
|
| 884 |
#[asn1(application = "73")] |
| 885 |
pub public_key: CertificatePublicKey<'a>, |
| 886 |
|
| 887 |
|
| 888 |
#[asn1(application = "32", type = "OCTET STRING")] |
| 889 |
pub holder_reference: &'a [u8], |
| 890 |
|
| 891 |
|
| 892 |
#[asn1(application = "37", type = "OCTET STRING")] |
| 893 |
pub effective_date: &'a [u8], |
| 894 |
|
| 895 |
|
| 896 |
#[asn1(application = "36", type = "OCTET STRING")] |
| 897 |
pub expiration_date: &'a [u8], |
| 898 |
} |
| 899 |
|
| 900 |
|
| 901 |
#[derive(Sequence)] |
| 902 |
#[asn1(tag_mode = "IMPLICIT")] |
| 903 |
pub struct CertificatePublicKey<'a> { |
| 904 |
pub domain_parameters: ObjectIdentifier, |
| 905 |
|
| 906 |
#[asn1(context_specific = "6", type = "OCTET STRING")] |
| 907 |
pub public_point: &'a [u8], |
| 908 |
} |
| 909 |
#[test] |
| 910 |
fn decode_tacho_application_tags() { |
| 911 |
let tacho_cert = TachographCertificate::from_der(TACHO_CERT_DER).unwrap(); |
| 912 |
|
| 913 |
let sig = tacho_cert.signature; |
| 914 |
assert_eq!(&sig[..2], hex!("6D 3E")); |
| 915 |
assert_eq!(tacho_cert.body.profile_identifier, &[0x00]); |
| 916 |
assert_eq!( |
| 917 |
tacho_cert.body.authority_reference, |
| 918 |
hex!("FD 45 43 20 01 FF FF 01") |
| 919 |
); |
| 920 |
assert_eq!( |
| 921 |
tacho_cert.body.holder_authorisation, |
| 922 |
hex!("FF 53 4D 52 44 54 0E") |
| 923 |
); |
| 924 |
assert_eq!( |
| 925 |
tacho_cert.body.public_key.domain_parameters, |
| 926 |
ObjectIdentifier::new_unwrap("1.2.840.10045.3.1.7") |
| 927 |
); |
| 928 |
assert_eq!( |
| 929 |
&tacho_cert.body.public_key.public_point[..4], |
| 930 |
hex!("04 30 E8 EE") |
| 931 |
); |
| 932 |
const GREECE: &[u8] = b"GR "; |
| 933 |
assert_eq!(&tacho_cert.body.holder_reference[1..4], GREECE); |
| 934 |
|
| 935 |
|
| 936 |
let mut buf = [0u8; 256]; |
| 937 |
let encoded = tacho_cert.encode_to_slice(&mut buf).unwrap(); |
| 938 |
assert_eq!(encoded, TACHO_CERT_DER); |
| 939 |
} |
| 940 |
} |
| 941 |
|
| 942 |
|
| 943 |
#[cfg(feature = "std")] |
| 944 |
mod bitstring { |
| 945 |
use der::BitString; |
| 946 |
use der::Decode; |
| 947 |
use der::Encode; |
| 948 |
use hex_literal::hex; |
| 949 |
|
| 950 |
const BITSTRING_EXAMPLE: &[u8] = &hex!("03 03 06 03 80"); |
| 951 |
|
| 952 |
|
| 953 |
#[derive(BitString)] |
| 954 |
pub struct MyBitStringTest { |
| 955 |
pub first_bit: bool, |
| 956 |
pub second_bit: bool, |
| 957 |
pub third_bit: bool, |
| 958 |
pub fourth_bit: bool, |
| 959 |
pub a: bool, |
| 960 |
pub b: bool, |
| 961 |
pub almost_least_significant: bool, |
| 962 |
pub least_significant_bit: bool, |
| 963 |
|
| 964 |
|
| 965 |
pub second_byte_bit: bool, |
| 966 |
pub second_byte_bit2: bool, |
| 967 |
} |
| 968 |
|
| 969 |
#[test] |
| 970 |
fn decode_bitstring() { |
| 971 |
let test_flags = MyBitStringTest::from_der(BITSTRING_EXAMPLE).unwrap(); |
| 972 |
|
| 973 |
assert!(!test_flags.first_bit); |
| 974 |
|
| 975 |
assert!(test_flags.almost_least_significant); |
| 976 |
assert!(test_flags.least_significant_bit); |
| 977 |
assert!(test_flags.second_byte_bit); |
| 978 |
assert!(!test_flags.second_byte_bit2); |
| 979 |
|
| 980 |
let reencoded = test_flags.to_der().unwrap(); |
| 981 |
|
| 982 |
assert_eq!(reencoded, BITSTRING_EXAMPLE); |
| 983 |
} |
| 984 |
|
| 985 |
|
| 986 |
#[derive(BitString)] |
| 987 |
pub struct MyBitString3or4 { |
| 988 |
pub bit_0: bool, |
| 989 |
pub bit_1: bool, |
| 990 |
pub bit_2: bool, |
| 991 |
|
| 992 |
#[asn1(optional = "true")] |
| 993 |
pub bit_3: bool, |
| 994 |
} |
| 995 |
|
| 996 |
#[test] |
| 997 |
fn decode_bitstring_3_used_first_lit() { |
| 998 |
|
| 999 |
let bits_3 = MyBitString3or4::from_der(&hex!("03 02 05 80")).unwrap(); |
| 1000 |
|
| 1001 |
assert!(bits_3.bit_0); |
| 1002 |
assert!(!bits_3.bit_1); |
| 1003 |
assert!(!bits_3.bit_2); |
| 1004 |
assert!(!bits_3.bit_3); |
| 1005 |
} |
| 1006 |
#[test] |
| 1007 |
fn decode_bitstring_3_used_all_lit() { |
| 1008 |
|
| 1009 |
let bits_3 = MyBitString3or4::from_der(&hex!("03 02 05 FF")).unwrap(); |
| 1010 |
|
| 1011 |
assert!(bits_3.bit_0); |
| 1012 |
assert!(bits_3.bit_1); |
| 1013 |
assert!(bits_3.bit_2); |
| 1014 |
assert!(!bits_3.bit_3); |
| 1015 |
} |
| 1016 |
|
| 1017 |
#[test] |
| 1018 |
fn decode_bitstring_4_used_all_lit() { |
| 1019 |
|
| 1020 |
let bits_3 = MyBitString3or4::from_der(&hex!("03 02 04 FF")).unwrap(); |
| 1021 |
|
| 1022 |
assert!(bits_3.bit_0); |
| 1023 |
assert!(bits_3.bit_1); |
| 1024 |
assert!(bits_3.bit_2); |
| 1025 |
assert!(bits_3.bit_3); |
| 1026 |
} |
| 1027 |
|
| 1028 |
#[test] |
| 1029 |
fn decode_invalid_bitstring_5_used() { |
| 1030 |
|
| 1031 |
assert!(MyBitString3or4::from_der(&hex!("03 02 03 FF")).is_err()); |
| 1032 |
} |
| 1033 |
|
| 1034 |
#[test] |
| 1035 |
fn decode_invalid_bitstring_2_used() { |
| 1036 |
|
| 1037 |
assert!(MyBitString3or4::from_der(&hex!("03 02 06 FF")).is_err()); |
| 1038 |
} |
| 1039 |
|
| 1040 |
#[test] |
| 1041 |
fn encode_3_zero_bits() { |
| 1042 |
let encoded_3_zeros = MyBitString3or4 { |
| 1043 |
bit_0: false, |
| 1044 |
bit_1: false, |
| 1045 |
bit_2: false, |
| 1046 |
bit_3: false, |
| 1047 |
} |
| 1048 |
.to_der() |
| 1049 |
.unwrap(); |
| 1050 |
|
| 1051 |
|
| 1052 |
assert_eq!(encoded_3_zeros, hex!("03 02 04 00")); |
| 1053 |
} |
| 1054 |
|
| 1055 |
#[test] |
| 1056 |
fn encode_3_one_bits() { |
| 1057 |
let encoded_3_zeros = MyBitString3or4 { |
| 1058 |
bit_0: true, |
| 1059 |
bit_1: true, |
| 1060 |
bit_2: true, |
| 1061 |
bit_3: false, |
| 1062 |
} |
| 1063 |
.to_der() |
| 1064 |
.unwrap(); |
| 1065 |
|
| 1066 |
|
| 1067 |
assert_eq!(encoded_3_zeros, hex!("03 02 04 E0")); |
| 1068 |
} |
| 1069 |
|
| 1070 |
#[test] |
| 1071 |
fn encode_4_one_bits() { |
| 1072 |
let encoded_4_zeros = MyBitString3or4 { |
| 1073 |
bit_0: true, |
| 1074 |
bit_1: true, |
| 1075 |
bit_2: true, |
| 1076 |
bit_3: true, |
| 1077 |
} |
| 1078 |
.to_der() |
| 1079 |
.unwrap(); |
| 1080 |
|
| 1081 |
|
| 1082 |
assert_eq!(encoded_4_zeros, hex!("03 02 04 F0")); |
| 1083 |
} |
| 1084 |
|
| 1085 |
#[test] |
| 1086 |
fn encode_optional_one_4_used() { |
| 1087 |
let encoded_4_zeros = MyBitString3or4 { |
| 1088 |
bit_0: false, |
| 1089 |
bit_1: false, |
| 1090 |
bit_2: false, |
| 1091 |
bit_3: true, |
| 1092 |
} |
| 1093 |
.to_der() |
| 1094 |
.unwrap(); |
| 1095 |
|
| 1096 |
|
| 1097 |
assert_eq!(encoded_4_zeros, hex!("03 02 04 10")); |
| 1098 |
} |
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
#[derive(Clone, Debug, Eq, PartialEq, BitString)] |
| 1121 |
pub struct PasswordFlags { |
| 1122 |
|
| 1123 |
pub case_sensitive: bool, |
| 1124 |
|
| 1125 |
|
| 1126 |
pub local: bool, |
| 1127 |
|
| 1128 |
|
| 1129 |
pub change_disabled: bool, |
| 1130 |
|
| 1131 |
|
| 1132 |
pub unblock_disabled: bool, |
| 1133 |
|
| 1134 |
|
| 1135 |
pub initialized: bool, |
| 1136 |
|
| 1137 |
|
| 1138 |
pub needs_padding: bool, |
| 1139 |
|
| 1140 |
|
| 1141 |
pub unblocking_password: bool, |
| 1142 |
|
| 1143 |
|
| 1144 |
pub so_password: bool, |
| 1145 |
|
| 1146 |
|
| 1147 |
pub disable_allowed: bool, |
| 1148 |
|
| 1149 |
|
| 1150 |
pub integrity_protected: bool, |
| 1151 |
|
| 1152 |
|
| 1153 |
pub confidentiality_protected: bool, |
| 1154 |
|
| 1155 |
|
| 1156 |
pub exchange_ref_data: bool, |
| 1157 |
|
| 1158 |
|
| 1159 |
|
| 1160 |
#[asn1(optional = "true")] |
| 1161 |
pub reset_retry_counter1: bool, |
| 1162 |
|
| 1163 |
|
| 1164 |
#[asn1(optional = "true")] |
| 1165 |
pub reset_retry_counter2: bool, |
| 1166 |
|
| 1167 |
|
| 1168 |
#[asn1(optional = "true")] |
| 1169 |
pub context_dependent: bool, |
| 1170 |
|
| 1171 |
|
| 1172 |
#[asn1(optional = "true")] |
| 1173 |
pub multi_step_protocol: bool, |
| 1174 |
|
| 1175 |
|
| 1176 |
#[asn1(optional = "true")] |
| 1177 |
pub fake_bit_for_testing: bool, |
| 1178 |
} |
| 1179 |
|
| 1180 |
const PASS_FLAGS_EXAMPLE_IN: &[u8] = &hex!("03 03 04 FF FF"); |
| 1181 |
const PASS_FLAGS_EXAMPLE_OUT: &[u8] = &hex!("03 04 07 FF F0 00"); |
| 1182 |
|
| 1183 |
#[test] |
| 1184 |
fn decode_short_bitstring_2_bytes() { |
| 1185 |
let pass_flags = PasswordFlags::from_der(PASS_FLAGS_EXAMPLE_IN).unwrap(); |
| 1186 |
|
| 1187 |
|
| 1188 |
assert!(pass_flags.case_sensitive); |
| 1189 |
|
| 1190 |
|
| 1191 |
assert!(pass_flags.exchange_ref_data); |
| 1192 |
|
| 1193 |
|
| 1194 |
assert!(!pass_flags.reset_retry_counter1); |
| 1195 |
|
| 1196 |
let reencoded = pass_flags.to_der().unwrap(); |
| 1197 |
|
| 1198 |
assert_eq!(reencoded, PASS_FLAGS_EXAMPLE_OUT); |
| 1199 |
} |
| 1200 |
} |
| 1201 |
mod infer_default { |
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
|
| 1223 |
|
| 1224 |
|
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
use der::Sequence; |
| 1234 |
|
| 1235 |
struct BooleanIsh; |
| 1236 |
|
| 1237 |
impl PartialEq<BooleanIsh> for bool { |
| 1238 |
fn eq(&self, _other: &BooleanIsh) -> bool { |
| 1239 |
unimplemented!("This is only here to mess up the compiler's type inference") |
| 1240 |
} |
| 1241 |
} |
| 1242 |
|
| 1243 |
#[derive(Sequence)] |
| 1244 |
#[allow(dead_code)] |
| 1245 |
struct Foo { |
| 1246 |
#[asn1(default = "Default::default")] |
| 1247 |
pub use_default_default: bool, |
| 1248 |
|
| 1249 |
#[asn1(default = "something_true")] |
| 1250 |
pub use_custom: bool, |
| 1251 |
} |
| 1252 |
|
| 1253 |
#[allow(dead_code)] |
| 1254 |
fn something_true() -> bool { |
| 1255 |
todo!() |
| 1256 |
} |
| 1257 |
} |