diff options
Diffstat (limited to 'tests/name.rs')
-rw-r--r-- | tests/name.rs | 138 |
1 files changed, 86 insertions, 52 deletions
diff --git a/tests/name.rs b/tests/name.rs index e52b2c7..27af2b5 100644 --- a/tests/name.rs +++ b/tests/name.rs @@ -2,7 +2,7 @@ use const_oid::ObjectIdentifier; use der::asn1::{Ia5StringRef, OctetStringRef, PrintableStringRef, SetOfVec, Utf8StringRef}; -use der::{AnyRef, Decode, Encode, Tag, Tagged}; +use der::{Any, Decode, Encode, Tag, Tagged}; use hex_literal::hex; use x509_cert::attr::AttributeTypeAndValue; use x509_cert::name::{Name, RdnSequence, RelativeDistinguishedName}; @@ -40,23 +40,53 @@ fn decode_name() { for atav in i1 { if 0 == counter { assert_eq!(atav.oid.to_string(), "2.5.4.6"); - assert_eq!(atav.value.printable_string().unwrap().to_string(), "US"); + assert_eq!( + PrintableStringRef::try_from(&atav.value) + .unwrap() + .to_string(), + "US" + ); } else if 1 == counter { assert_eq!(atav.oid.to_string(), "2.5.4.10"); assert_eq!( - atav.value.printable_string().unwrap().to_string(), + PrintableStringRef::try_from(&atav.value) + .unwrap() + .to_string(), "Test Certificates 2011" ); } else if 2 == counter { assert_eq!(atav.oid.to_string(), "2.5.4.3"); assert_eq!( - atav.value.printable_string().unwrap().to_string(), + PrintableStringRef::try_from(&atav.value) + .unwrap() + .to_string(), "Good CA" ); } counter += 1; } } + + #[cfg(feature = "std")] + { + // https://datatracker.ietf.org/doc/html/rfc4514.html#section-2.1 + // If the RDNSequence is an empty sequence, the result is the empty or + // zero-length string. + // Otherwise, the output consists of the string encodings of each + // RelativeDistinguishedName in the RDNSequence (according to Section 2.2), + // starting with the last element of the sequence and moving backwards + // toward the first. + // The encodings of adjoining RelativeDistinguishedNames are separated by + // a comma (',' U+002C) character. + let name = rdn1a.to_string(); + assert_eq!(name, "CN=Good CA,O=Test Certificates 2011,C=US"); + + // https://github.com/RustCrypto/formats/issues/1121 + let rdn1 = Name::from_der(&hex!("3081c0310b30090603550406130255533113301106035504080c0a43616c69666f726e69613116301406035504070c0d4d6f756e7461696e205669657731133011060355040a0c0a476f6f676c65204c4c43311e301c06035504030c154f51464176444e4457732e676f6f676c652e636f6d31243022060355040b0c1b6d616e6167656d656e743a64732e67726f75702e3338393131313131293027060a0992268993f22c6401010c196964656e746974793a64732e67726f75702e33383931313131")[..]); + let rdn1a = rdn1.unwrap(); + let name = rdn1a.to_string(); + assert_eq!(name, "UID=identity:ds.group.3891111,OU=management:ds.group.3891111,CN=OQFAvDNDWs.google.com,O=Google LLC,L=Mountain View,ST=California,C=US"); + } } #[test] @@ -73,9 +103,9 @@ fn decode_rdn() { for atav in i { let oid = atav.oid; assert_eq!(oid.to_string(), "2.5.4.6"); - let value = atav.value; + let value = &atav.value; assert_eq!(value.tag(), Tag::PrintableString); - let ps = value.printable_string().unwrap(); + let ps = PrintableStringRef::try_from(value).unwrap(); assert_eq!(ps.to_string(), "US"); } @@ -99,32 +129,32 @@ fn decode_rdn() { let atav1a = i.next().unwrap(); let oid2 = atav1a.oid; assert_eq!(oid2.to_string(), "2.5.4.10"); - let value2 = atav1a.value; + let value2 = &atav1a.value; assert_eq!(value2.tag(), Tag::Utf8String); - let utf8b = value2.utf8_string().unwrap(); + let utf8b = Utf8StringRef::try_from(value2).unwrap(); assert_eq!(utf8b.to_string(), "123"); let atav2a = i.next().unwrap(); let oid1 = atav2a.oid; assert_eq!(oid1.to_string(), "2.5.4.3"); - let value1 = atav2a.value; + let value1 = &atav2a.value; assert_eq!(value1.tag(), Tag::Utf8String); - let utf8a = value1.utf8_string().unwrap(); + let utf8a = Utf8StringRef::try_from(value1).unwrap(); assert_eq!(utf8a.to_string(), "JOHN SMITH"); let mut from_scratch = RelativeDistinguishedName::default(); - assert!(from_scratch.0.add(*atav1a).is_ok()); - assert!(from_scratch.0.add(*atav2a).is_ok()); - let reencoded = from_scratch.to_vec().unwrap(); + assert!(from_scratch.0.insert(atav1a.clone()).is_ok()); + assert!(from_scratch.0.insert(atav2a.clone()).is_ok()); + let reencoded = from_scratch.to_der().unwrap(); assert_eq!( reencoded, &hex!("311F300A060355040A0C03313233301106035504030C0A4A4F484E20534D495448") ); let mut from_scratch2 = RelativeDistinguishedName::default(); - assert!(from_scratch2.0.add(*atav2a).is_ok()); + assert!(from_scratch2.0.insert_ordered(atav2a.clone()).is_ok()); // fails when caller adds items not in DER lexicographical order - assert!(from_scratch2.0.add(*atav1a).is_err()); + assert!(from_scratch2.0.insert_ordered(atav1a.clone()).is_err()); // allow out-of-order RDNs (see: RustCrypto/formats#625) assert!(RelativeDistinguishedName::from_der( @@ -196,24 +226,24 @@ fn rdns_serde() { ], "CN=foo,SN=bar,C=baz+L=bat", &[ - &[AttributeTypeAndValue { - oid: const_oid::db::rfc4519::CN, - value: AnyRef::from(Utf8StringRef::new("foo").unwrap()), - }], - &[AttributeTypeAndValue { - oid: const_oid::db::rfc4519::SN, - value: AnyRef::from(Utf8StringRef::new("bar").unwrap()), - }], &[ AttributeTypeAndValue { oid: const_oid::db::rfc4519::C, - value: AnyRef::from(PrintableStringRef::new("baz").unwrap()), + value: Any::from(PrintableStringRef::new("baz").unwrap()), }, AttributeTypeAndValue { oid: const_oid::db::rfc4519::L, - value: AnyRef::from(Utf8StringRef::new("bat").unwrap()), + value: Any::from(Utf8StringRef::new("bat").unwrap()), }, ], + &[AttributeTypeAndValue { + oid: const_oid::db::rfc4519::SN, + value: Any::from(Utf8StringRef::new("bar").unwrap()), + }], + &[AttributeTypeAndValue { + oid: const_oid::db::rfc4519::CN, + value: Any::from(Utf8StringRef::new("foo").unwrap()), + }], ], ), ( @@ -221,16 +251,16 @@ fn rdns_serde() { "UID=jsmith,DC=example,DC=net", &[ &[AttributeTypeAndValue { - oid: const_oid::db::rfc4519::UID, - value: AnyRef::from(Utf8StringRef::new("jsmith").unwrap()), + oid: const_oid::db::rfc4519::DC, + value: Any::from(Ia5StringRef::new("net").unwrap()), }], &[AttributeTypeAndValue { oid: const_oid::db::rfc4519::DC, - value: AnyRef::from(Ia5StringRef::new("example").unwrap()), + value: Any::from(Ia5StringRef::new("example").unwrap()), }], &[AttributeTypeAndValue { - oid: const_oid::db::rfc4519::DC, - value: AnyRef::from(Ia5StringRef::new("net").unwrap()), + oid: const_oid::db::rfc4519::UID, + value: Any::from(Utf8StringRef::new("jsmith").unwrap()), }], ], ), @@ -238,24 +268,24 @@ fn rdns_serde() { &["OU=Sales+CN=J. Smith,DC=example,DC=net"], "OU=Sales+CN=J. Smith,DC=example,DC=net", &[ + &[AttributeTypeAndValue { + oid: const_oid::db::rfc4519::DC, + value: Any::from(Ia5StringRef::new("net").unwrap()), + }], + &[AttributeTypeAndValue { + oid: const_oid::db::rfc4519::DC, + value: Any::from(Ia5StringRef::new("example").unwrap()), + }], &[ AttributeTypeAndValue { oid: const_oid::db::rfc4519::OU, - value: AnyRef::from(Utf8StringRef::new("Sales").unwrap()), + value: Any::from(Utf8StringRef::new("Sales").unwrap()), }, AttributeTypeAndValue { oid: const_oid::db::rfc4519::CN, - value: AnyRef::from(Utf8StringRef::new("J. Smith").unwrap()), + value: Any::from(Utf8StringRef::new("J. Smith").unwrap()), }, ], - &[AttributeTypeAndValue { - oid: const_oid::db::rfc4519::DC, - value: AnyRef::from(Ia5StringRef::new("example").unwrap()), - }], - &[AttributeTypeAndValue { - oid: const_oid::db::rfc4519::DC, - value: AnyRef::from(Ia5StringRef::new("net").unwrap()), - }], ], ), ( @@ -263,16 +293,16 @@ fn rdns_serde() { "CN=James \\\"Jim\\\" Smith\\, III,DC=example,DC=net", &[ &[AttributeTypeAndValue { - oid: const_oid::db::rfc4519::CN, - value: AnyRef::from(Utf8StringRef::new(r#"James "Jim" Smith, III"#).unwrap()), + oid: const_oid::db::rfc4519::DC, + value: Any::from(Ia5StringRef::new("net").unwrap()), }], &[AttributeTypeAndValue { oid: const_oid::db::rfc4519::DC, - value: AnyRef::from(Ia5StringRef::new("example").unwrap()), + value: Any::from(Ia5StringRef::new("example").unwrap()), }], &[AttributeTypeAndValue { - oid: const_oid::db::rfc4519::DC, - value: AnyRef::from(Ia5StringRef::new("net").unwrap()), + oid: const_oid::db::rfc4519::CN, + value: Any::from(Utf8StringRef::new(r#"James "Jim" Smith, III"#).unwrap()), }], ], ), @@ -281,16 +311,16 @@ fn rdns_serde() { "CN=Before\\0dAfter,DC=example,DC=net", &[ &[AttributeTypeAndValue { - oid: const_oid::db::rfc4519::CN, - value: AnyRef::from(Utf8StringRef::new("Before\rAfter").unwrap()), + oid: const_oid::db::rfc4519::DC, + value: Any::from(Ia5StringRef::new("net").unwrap()), }], &[AttributeTypeAndValue { oid: const_oid::db::rfc4519::DC, - value: AnyRef::from(Ia5StringRef::new("example").unwrap()), + value: Any::from(Ia5StringRef::new("example").unwrap()), }], &[AttributeTypeAndValue { - oid: const_oid::db::rfc4519::DC, - value: AnyRef::from(Ia5StringRef::new("net").unwrap()), + oid: const_oid::db::rfc4519::CN, + value: Any::from(Utf8StringRef::new("Before\rAfter").unwrap()), }], ], ), @@ -299,7 +329,7 @@ fn rdns_serde() { "1.3.6.1.4.1.1466.0=#04024869", &[&[AttributeTypeAndValue { oid: ObjectIdentifier::new("1.3.6.1.4.1.1466.0").unwrap(), - value: AnyRef::from(OctetStringRef::new(&[b'H', b'i']).unwrap()), + value: Any::from(OctetStringRef::new(&[b'H', b'i']).unwrap()), }]], ), ]; @@ -319,7 +349,11 @@ fn rdns_serde() { for input in inputs.iter() { eprintln!("input: {}", input); - let der = RdnSequence::encode_from_string(input).unwrap(); + let der = input + .parse::<RdnSequence>() + .and_then(|rdn| rdn.to_der()) + .unwrap(); + let rdns = RdnSequence::from_der(&der).unwrap(); for (l, r) in brdns.0.iter().zip(rdns.0.iter()) { |