Skip to content

Commit f6c912d

Browse files
committed
der: clarify: add OneAsymmetricKey example
1 parent f73c805 commit f6c912d

File tree

4 files changed

+102
-11
lines changed

4 files changed

+102
-11
lines changed

der/src/writer/clarify.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,18 @@ impl Clarifier {
263263
}
264264

265265
/// Writes hex bytes to debug output, for example "30 04 "
266-
pub fn write_clarify_hex(&mut self, slice: &[u8]) {
266+
pub fn write_clarify_hex(&mut self, bytes: &[u8]) {
267267
let indent = self.indent_str();
268-
write!(&mut self.clarify_buf, "{}", HexDisplayLines(&slice, indent)).ok();
268+
write!(
269+
&mut self.clarify_buf,
270+
"{}",
271+
HexDisplayLines {
272+
bytes,
273+
indent,
274+
space: self.comment_writer.needs_newline_space()
275+
}
276+
)
277+
.ok();
269278
}
270279

271280
/// Writes string to debug output, for example a comment "// SEQUENCE"
@@ -418,9 +427,9 @@ impl<'a> Writer for ClarifySliceWriter<'a> {
418427
fn strip_transparent_types(mut type_name: &str) -> Cow<'_, str> {
419428
let prefixes = [
420429
"EncodeValueRef<",
421-
"ApplicationRef<",
422-
"ContextSpecificRef<",
423-
"PrivateRef<",
430+
// "ApplicationRef<",
431+
// "ContextSpecificRef<",
432+
// "PrivateRef<",
424433
];
425434

426435
for prefix in prefixes {

der/src/writer/clarify/commentwriter.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ pub trait CommentWriter {
66
fn before_new_line(&mut self, w: &mut dyn Write);
77

88
fn start_new_line(&mut self, _w: &mut dyn Write) {}
9+
10+
fn needs_newline_space(&self) -> bool {
11+
false
12+
}
913
}
1014

1115
#[derive(Default)]
@@ -72,8 +76,14 @@ impl CommentWriter for RustHexWriter {
7276
self.buf.clear();
7377
self.started_newline = false;
7478
}
79+
7580
fn start_new_line(&mut self, w: &mut dyn Write) {
7681
self.started_newline = true;
7782
w.write_all(b"\"").ok();
7883
}
84+
85+
fn needs_newline_space(&self) -> bool {
86+
// because '"' is in first line, next lines need to be moved by 1 character
87+
true
88+
}
7989
}

der/src/writer/clarify/hexdisplaylines.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
1+
use core::fmt::Write;
12
use std::fmt;
23

3-
/// (bytes, indent)
4-
pub struct HexDisplayLines<'a, 'i>(pub &'a [u8], pub &'i str);
4+
/// (bytes, indent, nl_ident)
5+
pub struct HexDisplayLines<'a, 'i> {
6+
pub bytes: &'a [u8],
7+
pub indent: &'i str,
8+
pub space: bool,
9+
}
510

611
impl fmt::Display for HexDisplayLines<'_, '_> {
712
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
813
let mut first = true;
9-
for chunk in self.0.chunks(16) {
14+
for chunk in self.bytes.chunks(16) {
1015
if !first {
11-
write!(f, "\n{}", self.1)?;
16+
write!(f, "\n{}", self.indent)?;
17+
if self.space {
18+
f.write_char(' ').ok();
19+
}
1220
} else {
1321
first = false;
1422
}

der/tests/clarify.rs

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
#![cfg(all(feature = "derive", feature = "alloc", feature = "clarify"))]
33

44
pub mod sequence {
5-
use std::println;
5+
use std::{println, str::FromStr};
66

77
use const_oid::ObjectIdentifier;
88
use der::{
9-
AnyRef, ClarifyFlavor, Decode, EncodeClarifyExt, Sequence, ValueOrd,
9+
AnyRef, ClarifyFlavor, Decode, EncodeClarifyExt, Sequence, TagNumber, ValueOrd,
1010
asn1::{OctetString, SetOf},
1111
};
1212
use hex_literal::hex;
@@ -84,4 +84,68 @@ pub mod sequence {
8484
"\n\"04 11\" // tag: OCTET STRING len: 17 type: OctetString \n\t\"00 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF \n\t01\"\n\"\" // end: OctetString "
8585
);
8686
}
87+
#[test]
88+
fn clarify_one_assymetric_key_rusthex() {
89+
let obj = OneAsymmetricKey {
90+
version: 1,
91+
private_key_algorithm: AlgorithmIdentifier {
92+
algorithm: ObjectIdentifier::from_str("1.2.3.4.5.6.7.8").expect("valid oid"),
93+
parameters: Some(
94+
AnyRef::new(
95+
der::Tag::ContextSpecific {
96+
constructed: true,
97+
number: TagNumber(0),
98+
},
99+
&[0xAA, 0xBB],
100+
)
101+
.expect("valid length"),
102+
),
103+
},
104+
private_key: &[
105+
0x00, 0x11, 0x22, 0x33, 0x00, 0x11, 0x22, 0x33, 0x00, 0x11, 0x22, 0x33, 0x00, 0x11,
106+
0x22, 0x33, 0x00, 0x11, 0x22, 0x33, 0x00, 0x11, 0x22, 0x33, 0x00, 0x11, 0x22, 0x33,
107+
],
108+
attributes: None,
109+
public_key: Some(&[
110+
0x44, 0x55, 0x66, 0x77, 0x44, 0x55, 0x66, 0x77, 0x44, 0x55, 0x66, 0x77, 0x44, 0x55,
111+
0x66, 0x77, 0x44, 0x55, 0x66, 0x77, 0x44, 0x55, 0x66, 0x77, 0x44, 0x55, 0x66, 0x77,
112+
]),
113+
};
114+
let clarified = obj
115+
.to_der_clarify(ClarifyFlavor::RustHex)
116+
.expect("encoded DER");
117+
118+
//println!("clarified: {clarified}");
119+
120+
assert!(clarified.contains("type: OneAsymmetricKey"));
121+
assert!(clarified.contains("tag: CONTEXT-SPECIFIC [0] (constructed)"));
122+
assert!(clarified.contains("tag: CONTEXT-SPECIFIC [1] (constructed)"));
123+
assert!(clarified.contains("type: AlgorithmIdentifier"));
124+
assert!(clarified.contains("tag: OBJECT IDENTIFIER"));
125+
assert!(clarified.contains("type: ObjectIdentifier"));
126+
assert!(clarified.contains("end: OneAsymmetricKey"));
127+
128+
hex!(
129+
"30 51" // tag: SEQUENCE len: 81 type: OneAsymmetricKey
130+
"02 01" // tag: INTEGER type: u8
131+
"01"
132+
"30 0D" // tag: SEQUENCE len: 13 type: AlgorithmIdentifier
133+
"06 07" // tag: OBJECT IDENTIFIER type: ObjectIdentifier
134+
"2A 03 04 05 06 07 08"
135+
"A0 02" // tag: CONTEXT-SPECIFIC [0] (constructed) type: AnyRef
136+
"AA BB"
137+
"04 1C" // tag: OCTET STRING len: 28 type: OctetStringRef
138+
"00 11 22 33 00 11 22 33 00 11 22 33 00 11 22 33
139+
00 11 22 33 00 11 22 33 00 11 22 33"
140+
"" // end: OctetStringRef
141+
"A1 1F" // tag: CONTEXT-SPECIFIC [1] (constructed) len: 31 type: ContextSpecificRef<BitStringRef>
142+
"03 1D" // tag: BIT STRING len: 29 type: BitStringRef
143+
"00"
144+
"44 55 66 77 44 55 66 77 44 55 66 77 44 55 66 77
145+
44 55 66 77 44 55 66 77 44 55 66 77"
146+
"" // end: BitStringRef
147+
"" // end: ContextSpecificRef<BitStringRef>
148+
"" // end: OneAsymmetricKey
149+
);
150+
}
87151
}

0 commit comments

Comments
 (0)