From 9014578cf4a7fdea31ad32efbe06a5f67fd35443 Mon Sep 17 00:00:00 2001 From: Simon Dieterle Date: Fri, 30 Dec 2022 19:18:41 +0000 Subject: [PATCH 1/3] add test vector for mixed DER/BER encoding in CMS --- pkcs7/tests/content_tests.rs | 26 +++++++++++++++++++++++++- pkcs7/tests/examples/cms_ber.bin | Bin 0 -> 10914 bytes pkcs7/tests/examples/cms_der.bin | Bin 0 -> 10872 bytes 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 pkcs7/tests/examples/cms_ber.bin create mode 100644 pkcs7/tests/examples/cms_der.bin diff --git a/pkcs7/tests/content_tests.rs b/pkcs7/tests/content_tests.rs index 406e4cf73..e496b2c20 100644 --- a/pkcs7/tests/content_tests.rs +++ b/pkcs7/tests/content_tests.rs @@ -2,7 +2,7 @@ use der::{ asn1::{ObjectIdentifier, OctetStringRef, SequenceRef}, - Decode, SliceWriter, + Decode, Encode, Length, SliceWriter, }; use hex_literal::hex; use pkcs7::{ @@ -140,3 +140,27 @@ fn decode_signed_scep_example() { _ => panic!("expected ContentInfo::SignedData(Some(_))"), } } + +#[test] +fn decode_signed_ber() { + let path = "./tests/examples/cms_der.bin"; + let bytes = fs::read(&path).expect(&format!("Failed to read from {}", &path)); + + let content = ContentInfo::from_der(&bytes).expect("expected valid data"); + + match content { + ContentInfo::SignedData(Some(data)) => { + assert_eq!( + data.encap_content_info + .e_content + .unwrap() + .decode_into::() + .unwrap() + .as_bytes() + .len(), + 10034 + ) + } + _ => panic!("expected ContentInfo::SignedData(Some(_))"), + } +} diff --git a/pkcs7/tests/examples/cms_ber.bin b/pkcs7/tests/examples/cms_ber.bin new file mode 100644 index 0000000000000000000000000000000000000000..57057fc6da166c577d11560ce06cd3a0f89a99c7 GIT binary patch literal 10914 zcmXqLVB^$k^Jx3d%gD~OpuwPliILHe-+-5mGoj6cF_oExiIJ7T07((!f(DfamL}#G zo_U#7nfVII`FSO&d8H-!dR&g9U^E0qLtvPN04T2yGcS#LXEX#xLjX$%jOKMLfifyI z8Un*G1V;1vFpShuFO7x(mJk@t>sSJ1RA@8=hG7Vd=JjD1siR&R4FN16Fq+q~1j?w; zXb23$5E#wt!!S}uy)+sESVCYluVV?6QK8Wg7=|G*n%9S6q>g%NGz74O0E-f)AeUQa zUMkWuItDNRF2!qN%mZSP1du7^47k`hfD8A|*|IP* z8SoqO8gR2Qhq5s9u!N)*ml(*2^BP(hm>ZfIni`rL7(@ZNmPlMo4Uq=IZ0un3m>8i3 zGc&R?J29}l&S{%^ZmTSx-O6m`idUnQLx6Tz_3y zC|38NZK&?bFHVIT9~1c?a6;%KL literal 0 HcmV?d00001 diff --git a/pkcs7/tests/examples/cms_der.bin b/pkcs7/tests/examples/cms_der.bin new file mode 100644 index 0000000000000000000000000000000000000000..2a1a3ae484c35905b07291de09959d85d4327e9a GIT binary patch literal 10872 zcmeI&Ur19?90zdEy~jJJPEAFPHogAI*-AavrYSKoOPe&wMN%y*BbF$-q0S)n!LBwU z!bAxrqh=+Q5{CIFwZzgON}?o7q|8z@X%8i$iG)oNUFt*6@w;#yew^Pu{LbgRehDu# zFhZG8X`B%u0cGN4TEfdtQV4O*M8pVFjY_1YF@B}3g2jgw3?Dk{`ku=^Vro4-=5J)HlS-_ zYSeJ8s^`9K8`DxKYz*Ug zbkQPiv_u<;{~Drop5X8Fqv_lX@mh{Q3Te(^xey>CUM_^Tob#&ZA+nWSIVT~TRs}0- ztnN!fS9&q$huIvJx??=cq?u!1efv%q4t{C);O2TP_^Bc}_})}(Um{6m9G2pnLKiBO zQ6Y~XqYidq~@Bwz^YP0i?&Q?4lQsQ=d!@odSjfeLx zzQ#{XLh74>%rC~pUq>!yb`B`-PTXwnAKX|GtxPHI&8Aq#ju=yGTjQ{@obj6r(!Y(r TGO3zQdQunj==?90yrb<8MyLd+ literal 0 HcmV?d00001 From 9e487b981cf5bd7a68042e1c6e98727dba76b18c Mon Sep 17 00:00:00 2001 From: Simon Dieterle Date: Fri, 30 Dec 2022 19:20:11 +0000 Subject: [PATCH 2/3] remove unused imports --- pkcs7/tests/content_tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkcs7/tests/content_tests.rs b/pkcs7/tests/content_tests.rs index e496b2c20..6222be23f 100644 --- a/pkcs7/tests/content_tests.rs +++ b/pkcs7/tests/content_tests.rs @@ -2,7 +2,7 @@ use der::{ asn1::{ObjectIdentifier, OctetStringRef, SequenceRef}, - Decode, Encode, Length, SliceWriter, + Decode, SliceWriter, }; use hex_literal::hex; use pkcs7::{ From ca5495a3d4905154f601686aaaa812f9f8be7563 Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Sun, 8 Jan 2023 20:08:05 -0700 Subject: [PATCH 3/3] pkcs7: refactor BER/DER tests --- pkcs7/tests/content_tests.rs | 60 ++++++++++++++++++++++++------------ 1 file changed, 41 insertions(+), 19 deletions(-) diff --git a/pkcs7/tests/content_tests.rs b/pkcs7/tests/content_tests.rs index 6222be23f..86be1b3ee 100644 --- a/pkcs7/tests/content_tests.rs +++ b/pkcs7/tests/content_tests.rs @@ -141,26 +141,48 @@ fn decode_signed_scep_example() { } } +// TODO(tarcieri): BER support #[test] +#[ignore] fn decode_signed_ber() { - let path = "./tests/examples/cms_der.bin"; - let bytes = fs::read(&path).expect(&format!("Failed to read from {}", &path)); - - let content = ContentInfo::from_der(&bytes).expect("expected valid data"); + let bytes = include_bytes!("examples/cms_ber.bin"); + + let content = match ContentInfo::from_der(bytes) { + Ok(ContentInfo::SignedData(Some(data))) => data, + other => panic!("unexpected result: {:?}", other), + }; + + assert_eq!( + content + .encap_content_info + .e_content + .unwrap() + .decode_as::() + .unwrap() + .as_bytes() + .len(), + 10034 + ); +} - match content { - ContentInfo::SignedData(Some(data)) => { - assert_eq!( - data.encap_content_info - .e_content - .unwrap() - .decode_into::() - .unwrap() - .as_bytes() - .len(), - 10034 - ) - } - _ => panic!("expected ContentInfo::SignedData(Some(_))"), - } +#[test] +fn decode_signed_der() { + let bytes = include_bytes!("examples/cms_der.bin"); + + let content = match ContentInfo::from_der(bytes) { + Ok(ContentInfo::SignedData(Some(data))) => data, + other => panic!("unexpected result: {:?}", other), + }; + + assert_eq!( + content + .encap_content_info + .e_content + .unwrap() + .decode_as::() + .unwrap() + .as_bytes() + .len(), + 10034 + ); }