#![cfg(feature = "ssh")]
use bc_components::SigningOptions;
use bc_envelope::prelude::*;
use indoc::indoc;
use ssh_key::{Algorithm as SSHAlgorithm, HashAlg};
mod common;
use crate::common::{check_encoding::*, test_data::*};
#[test]
fn test_ssh_signed_plaintext() {
bc_components::register_tags();
let alice_ssh_private_key = alice_private_key()
.ssh_signing_private_key(SSHAlgorithm::Ed25519, "alice@example.com")
.unwrap();
let alice_ssh_public_key = alice_ssh_private_key.public_key().unwrap();
let options = SigningOptions::Ssh {
namespace: "test".to_string(),
hash_alg: HashAlg::Sha256,
};
let envelope = hello_envelope()
.add_signature_opt(&alice_ssh_private_key, Some(options), None)
.check_encoding()
.unwrap();
let ur = envelope.ur();
#[rustfmt::skip]
let expected_format = indoc! {r#"
"Hello." [
'signed': Signature(SshEd25519)
]
"#}.trim();
assert_actual_expected!(envelope.format(), expected_format);
let received_envelope =
Envelope::from_ur(&ur).unwrap().check_encoding().unwrap();
let received_plaintext =
received_envelope.verify_signature_from(&alice_ssh_public_key);
let received_plaintext = received_plaintext
.unwrap()
.extract_subject::<String>()
.unwrap();
assert_eq!(received_plaintext, "Hello.");
let carol_ssh_public_key = carol_private_key()
.ssh_signing_private_key(SSHAlgorithm::Ed25519, "carol@example.com")
.unwrap()
.public_key()
.unwrap();
assert!(
received_envelope
.verify_signature_from(&carol_ssh_public_key)
.is_err()
);
received_envelope
.verify_signatures_from_threshold(
&[&alice_ssh_public_key, &carol_ssh_public_key],
Some(1),
)
.unwrap();
assert!(
received_envelope
.verify_signatures_from_threshold(
&[&alice_ssh_public_key, &carol_ssh_public_key],
Some(2)
)
.is_err()
);
}