Compare commits
4 Commits
warning-cl
...
fix/13
| Author | SHA1 | Date | |
|---|---|---|---|
| 64244ab27e | |||
|
|
7e4eeb67f2 | ||
|
|
924f79f6a3 | ||
|
|
b414834c62 |
61
src/lib.rs
61
src/lib.rs
@@ -401,7 +401,7 @@ pub enum ASN1EncodeErr {
|
|||||||
|
|
||||||
impl fmt::Display for ASN1EncodeErr {
|
impl fmt::Display for ASN1EncodeErr {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
f.write_str(self.description())
|
f.write_str(&self.to_string())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1642,4 +1642,63 @@ mod tests {
|
|||||||
assert_eq!(raw_oid, &expected[6..(expected.len() - 4)]);
|
assert_eq!(raw_oid, &expected[6..(expected.len() - 4)]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn openssl_ec_key_test() {
|
||||||
|
use ASN1Block::{Sequence, Explicit, Integer, ObjectIdentifier,
|
||||||
|
OctetString};
|
||||||
|
|
||||||
|
// Create openssl ec key
|
||||||
|
let private_key = vec![0, 1, 2, 3, 4, 5, 6, 7];
|
||||||
|
let der = to_der(
|
||||||
|
&ASN1Block::Sequence(0, vec![
|
||||||
|
// Version
|
||||||
|
ASN1Block::Integer(0, 1.into()),
|
||||||
|
// Private key
|
||||||
|
ASN1Block::OctetString(0, private_key.clone()),
|
||||||
|
// Parameters
|
||||||
|
// Explicitely tagged oid
|
||||||
|
// Oid: 1, 2, 840, 10045, 3, 1, 7,
|
||||||
|
ASN1Block::Explicit(ASN1Class::ContextSpecific, 0, 0u8.into(),
|
||||||
|
Box::new(ASN1Block::ObjectIdentifier(0,
|
||||||
|
OID::new(vec![
|
||||||
|
1u8.into(), 2u8.into(), 840u16.into(),
|
||||||
|
10045u16.into(), 3u8.into(), 1u8.into(), 7u8.into(),
|
||||||
|
])
|
||||||
|
)))
|
||||||
|
])
|
||||||
|
).unwrap();
|
||||||
|
let der_data = vec![48, 25, 2, 1, 1, 4, 8, 0, 1, 2, 3, 4, 5, 6, 7, 160,
|
||||||
|
10, 6, 8, 42, 134, 72, 206, 61, 3, 1, 7];
|
||||||
|
|
||||||
|
assert_eq!(der, der_data);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
from_der(&der_data),
|
||||||
|
Ok(vec![Sequence(0, vec![Integer(2, 1u8.into()),
|
||||||
|
OctetString(5, private_key),
|
||||||
|
Explicit(ASN1Class::ContextSpecific, 15, 0u8.into(),
|
||||||
|
Box::new(ObjectIdentifier(17, OID(vec![1u8.into(),
|
||||||
|
2u8.into(), 840u16.into(), 10045u16.into(), 3u8.into(),
|
||||||
|
1u8.into(), 7u8.into(),
|
||||||
|
]))))])]),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn encode_unknowns() {
|
||||||
|
fn encode_structure(bufs: &[Vec<u8>]) -> Vec<u8> {
|
||||||
|
let mut body = Vec::new();
|
||||||
|
for (i, buf) in bufs.iter().enumerate() {
|
||||||
|
let mut der = to_der(&ASN1Block::Unknown(ASN1Class::ContextSpecific, false, 0, BigUint::from_usize(i).unwrap(), buf.to_vec())).unwrap();
|
||||||
|
body.append(&mut der);
|
||||||
|
}
|
||||||
|
let block = ASN1Block::Unknown(ASN1Class::ContextSpecific, true, 0, BigUint::from_u8(0).unwrap(), body);
|
||||||
|
to_der(&block).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
let decoded = from_der(&encode_structure(&vec![vec![0]])).unwrap();
|
||||||
|
let expected = [ASN1Block::Unknown(ASN1Class::ContextSpecific, true, 0, BigUint::from_u8(0).unwrap(), vec![128, 1, 0])];
|
||||||
|
assert_eq!(decoded, expected);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user