Skip to content

PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust

test(sign): a small RSA key's signature verifies with the short-key note

oracola91737f2f725fc0
diff --git a/crates/sign/tests/crypto.rs b/crates/sign/tests/crypto.rs
index a33a13648..c6935ddc6 100644
--- a/crates/sign/tests/crypto.rs
+++ b/crates/sign/tests/crypto.rs
@@ -569,6 +569,28 @@ fn small_rsa_keys_say_so_and_others_do_not() {
assert_eq!(id.certificate.public_key.weakness(), None, "the 2048-bit test identity");
}
+/// A synthetic 1024-bit key made for this test (`openssl genrsa 1024`, e = 65537) and its
+/// PKCS #1 v1.5 SHA-256 signature of `hello`.
+const RSA_1024_N: &str = "ae561814194f0a06475c002fc7633b81e83a738ad3f35ba1ded959be6f6b62302028d58707403f44b7bda959e446f13849896711985ac4815e58d7d323aee664771c35bf2dc3db1bcdc49ac61ae7282a24ec7a5af9f6ebe98945e6a71c4998ed31c41bbb997c4873fb4b8271fd3cf76648efcbff76972ab7bcaf831b9e203d47";
+const RSA_1024_SIG: &str = "0de5110137e768a6b00201cf8340688c81a901fe309489f2c8186b7dc56e67f7e64188970a9bea926b9bd99b19985adfb0489956ffd5e0264468b678c4fd49a155a895fcb9ab127d0c3742b7a07866b3deec20a5b2f61af4cbfd2901517dc268e4d6edf4e905e0f4e05cdc740c3180971c1ec1470c1476f90ea988e2ab4cbb18";
+
+#[test]
+fn a_signature_made_with_a_small_rsa_key_verifies_with_the_note() {
+ use pdfcraft_sign::keys::Scheme;
+ let key = PublicKey::Rsa { n: unhex(RSA_1024_N), e: vec![1, 0, 1] };
+ let weak = key.weakness().expect("a 1024-bit key is short");
+ let sig = unhex(RSA_1024_SIG);
+ let mut notes = Vec::new();
+ let digest = DigestAlg::Sha256.digest(&[b"hello"]);
+ assert!(key.verify_noting(Scheme::RsaPkcs1, DigestAlg::Sha256, &digest, &sig, &mut notes).unwrap());
+ assert_eq!(notes, [weak]);
+ // A signature that doesn't verify says nothing about the key.
+ let mut notes = Vec::new();
+ let other = DigestAlg::Sha256.digest(&[b"hullo"]);
+ assert!(!key.verify_noting(Scheme::RsaPkcs1, DigestAlg::Sha256, &other, &sig, &mut notes).unwrap());
+ assert!(!notes.contains(&weak), "{notes:?}");
+}
+
#[test]
fn an_rsa_signature_value_not_below_the_modulus_is_refused() {
use pdfcraft_sign::keys::{DigestAlg, PssParams, Scheme};