refactor: replace OpenSSL transports and certificate imports

This commit is contained in:
Kevin Yue committed 2026-11-01 23:35:58 +08:00
1 parent 37c88c7adb
commit 14c4e24e0e
59 files changed
+1072 -1151

No files matched your search

+139 -188
View File
@@ -1,4 +1,4 @@
//! Public identity compatibility and native import behavior.
//! Public identity compatibility and owned material lifecycle.
use std::{fs, path::PathBuf};
use gpapi::utils::request::{ClientIdentity, RequestIdentityError};
@@ -31,23 +31,9 @@ fn rsa_pkcs8_loads_without_a_password() {
}
#[test]
fn ec_pkcs8_records_native_import_compatibility() {
let result = load("ec-cert.pem", "ec-pkcs8.pem", None);
if cfg!(target_os = "macos") {
// Apple native-tls import currently rejects this P-256 PKCS#8 fixture.
assert!(
result
.unwrap_err()
.downcast_ref::<reqwest::Error>()
.unwrap()
.is_builder()
);
} else {
assert_eq!(
result.unwrap().key_data().unwrap(),
fs::read(fixture("ec-pkcs8.pem")).unwrap()
);
}
fn ec_pkcs8_loads_on_every_platform() {
let identity = load("ec-cert.pem", "ec-pkcs8.pem", None).unwrap();
assert_eq!(identity.key_data().unwrap(), fs::read(fixture("ec-pkcs8.pem")).unwrap());
}
#[test]
@@ -63,23 +49,13 @@ fn encrypted_pkcs8_keys_normalize_to_the_same_private_key() {
("ec-pbes2.pem", "ec-pkcs8.pem", "ec-cert.pem"),
] {
let result = load(certificate, key, Some(PASSWORD));
if cfg!(target_os = "macos") && certificate == "ec-cert.pem" {
assert!(
result
.unwrap_err()
.downcast_ref::<reqwest::Error>()
.unwrap()
.is_builder()
);
} else {
let identity = result.unwrap_or_else(|error| panic!("{key}: {error}"));
assert_eq!(
identity.key_data().unwrap(),
fs::read(fixture(expected)).unwrap(),
"{key}"
);
assert!(identity.key_password().is_none());
}
let identity = result.unwrap_or_else(|error| panic!("{key}: {error}"));
assert_eq!(
identity.key_data().unwrap(),
fs::read(fixture(expected)).unwrap(),
"{key}"
);
assert!(identity.key_password().is_none());
for (password, missing) in [(None, true), (Some("incorrect"), false)] {
let error = load(certificate, key, password).unwrap_err();
match error.downcast_ref::<RequestIdentityError>() {
@@ -128,7 +104,7 @@ fn rsa_pkcs1_normalizes_to_pkcs8_in_separate_and_combined_files() {
}
#[test]
fn malformed_rsa_pkcs1_is_rejected_by_native_import() {
fn malformed_rsa_pkcs1_is_rejected_before_connection() {
let directory = tempfile::tempdir().unwrap();
let source = directory.path().join("invalid-key.pem");
fs::write(&source, pem::encode(&pem::Pem::new("RSA PRIVATE KEY", vec![0x30, 0]))).unwrap();
@@ -138,7 +114,11 @@ fn malformed_rsa_pkcs1_is_rejected_by_native_import() {
None,
)
.unwrap_err();
assert!(error.downcast_ref::<reqwest::Error>().is_some());
assert!(
error
.to_string()
.contains("Check that the private key matches the certificate")
);
}
fn check_archive(name: &str, password: &str) {
@@ -211,15 +191,12 @@ fn pkcs12_password_errors_explain_how_to_recover() {
assert!(message.contains("PKCS#12"), "{message}");
assert!(message.contains("password") && message.contains("file"), "{message}");
assert!(!format!("{error:?}").contains("incorrect"));
assert!(error.chain().any(|cause| cause.is::<reqwest::Error>()));
assert!(error.chain().any(|cause| cause.is::<p12_keystore::error::Error>()));
}
}
}
#[test]
// Apple's native importer rejects this empty-password fixture; its native
// compatibility decision is covered by archive_snapshot_and_pfx_preserve_native_import_contract.
#[cfg(not(target_os = "macos"))]
fn empty_pkcs12_password_survives_loading_and_transport() {
let archive = fs::read(fixture("empty-password.p12")).unwrap();
for password in [None, Some("")] {
@@ -239,26 +216,6 @@ fn pkcs12_accepts_non_ascii_passwords() {
#[test]
fn legacy_rc2_pkcs12_checks_password_and_archive_integrity() {
#[cfg(not(target_os = "macos"))]
if std::env::var_os("GP_IDENTITY_LEGACY_TEST_CHILD").is_none() {
// OpenSSL providers are process-global. Exercise the existing legacy-mode
// configuration in an isolated process, without affecting parallel tests.
let directory = tempfile::tempdir().unwrap();
let config = directory.path().join("openssl.cnf");
fs::write(&config, gpapi::utils::openssl::openssl_conf()).unwrap();
let status = std::process::Command::new(std::env::current_exe().unwrap())
.args([
"--exact",
"legacy_rc2_pkcs12_checks_password_and_archive_integrity",
"--nocapture",
])
.env("OPENSSL_CONF", config)
.env("GP_IDENTITY_LEGACY_TEST_CHILD", "1")
.status()
.unwrap();
assert!(status.success());
return;
}
check_archive("legacy-rc2.p12", PASSWORD);
}
@@ -307,7 +264,12 @@ fn pbes2_legacy_ciphers_remain_supported() {
}
#[test]
#[cfg(feature = "desktop")]
#[cfg(any(
target_os = "macos",
target_os = "linux",
target_os = "freebsd",
target_os = "openbsd"
))]
fn legacy_pkcs8_conversion_preserves_the_key_and_password_errors() {
let identity = load("rsa-cert.pem", "rsa-pbes1-3des.pem", Some(PASSWORD)).unwrap();
assert_eq!(
@@ -326,121 +288,65 @@ fn legacy_pkcs8_conversion_preserves_the_key_and_password_errors() {
}
#[test]
#[cfg(not(target_os = "macos"))]
#[cfg(feature = "desktop")]
fn legacy_pkcs8_matches_previous_linux_openssl_processing() {
let encrypted = fs::read(fixture("rsa-pbes1-3des.pem")).unwrap();
let previous = openssl::pkey::PKey::private_key_from_pem_passphrase(&encrypted, PASSWORD.as_bytes())
.unwrap()
.private_key_to_pem_pkcs8()
.unwrap();
let identity = load("rsa-cert.pem", "rsa-pbes1-3des.pem", Some(PASSWORD)).unwrap();
assert_eq!(identity.key_data().unwrap(), previous);
assert!(openssl::pkey::PKey::private_key_from_pem_passphrase(&encrypted, b"incorrect").is_err());
}
#[test]
#[cfg(feature = "desktop")]
fn legacy_pkcs8_converter_failures_are_terminal_and_modern_keys_need_no_executable() {
use std::os::unix::fs::PermissionsExt;
const CHILD: &str = "GP_LEGACY_CONVERTER_TEST";
if let Ok(case) = std::env::var(CHILD) {
let started = std::time::Instant::now();
let error = load("rsa-cert.pem", "rsa-pbes1-3des.pem", Some(PASSWORD)).unwrap_err();
assert!(
started.elapsed() < std::time::Duration::from_secs(12),
"conversion exceeded its deadline"
);
assert!(
matches!(
error.downcast_ref::<RequestIdentityError>(),
Some(RequestIdentityError::LegacyKeyConversion(_))
),
"{error:?}"
);
let message = format!("{error:?}");
assert!(message.contains("Convert the key to modern PKCS#8"));
assert!(!message.contains(PASSWORD));
assert!(!message.contains("PRIVATE KEY"));
if case == "missing" {
assert!(message.contains("unavailable"));
} else if case.starts_with("timeout") {
assert!(message.contains("timed out"));
}
for key in ["rsa-pkcs8.pem", "rsa-pbes2-sha256.pem"] {
let identity = load("rsa-cert.pem", key, Some(PASSWORD)).unwrap();
assert_eq!(
identity.key_data().unwrap(),
fs::read(fixture("rsa-pkcs8.pem")).unwrap()
);
}
return;
}
// PATH is process-global; isolate executable discovery from parallel tests.
for (case, script) in [
("missing", None),
(
"unsupported",
Some("#!/bin/sh\nprintf 'unsupported: fixture-password PRIVATE KEY' >&2\nexit 1\n"),
),
("invalid-output", Some("#!/bin/sh\nprintf 'invalid converted key'\n")),
("timeout-wrapper", Some("#!/bin/sh\n/bin/sleep 14\n")),
("timeout-exited-wrapper", Some("#!/bin/sh\n/bin/sleep 14 &\nexit 0\n")),
] {
#[cfg(any(
target_os = "macos",
target_os = "linux",
target_os = "freebsd",
target_os = "openbsd"
))]
fn identity_import_does_not_use_converter_executables() {
const CHILD: &str = "GP_IDENTITY_NO_COMMANDS";
if std::env::var_os(CHILD).is_none() {
let directory = tempfile::tempdir().unwrap();
if let Some(script) = script {
let executable = directory.path().join("openssl");
fs::write(&executable, script).unwrap();
fs::set_permissions(executable, fs::Permissions::from_mode(0o700)).unwrap();
}
let status = std::process::Command::new(std::env::current_exe().unwrap())
.args([
"--exact",
"legacy_pkcs8_converter_failures_are_terminal_and_modern_keys_need_no_executable",
"--nocapture",
])
.args(["--exact", "identity_import_does_not_use_converter_executables"])
.env(CHILD, "1")
.env("PATH", directory.path())
.env(CHILD, case)
.status()
.unwrap();
assert!(status.success(), "converter case: {case}");
assert!(status.success());
return;
}
for key in ["rsa-pkcs8.pem", "rsa-traditional-aes256.pem", "rsa-pbes1-3des.pem"] {
let identity = load("rsa-cert.pem", key, Some(PASSWORD)).unwrap();
assert_eq!(
identity.key_data().unwrap(),
fs::read(fixture("rsa-pkcs8.pem")).unwrap()
);
}
check_archive("legacy-rc2.p12", PASSWORD);
}
#[test]
#[cfg(any(
target_os = "macos",
target_os = "linux",
target_os = "freebsd",
target_os = "openbsd"
))]
fn encrypted_key_import_accepts_long_passwords() {
for length in [1023, 1024] {
let identity = load(
"rsa-cert.pem",
&format!("rsa-pbes1-3des-password-{length}.pem"),
Some(&"x".repeat(length)),
)
.unwrap();
assert_eq!(
identity.key_data().unwrap(),
fs::read(fixture("rsa-pkcs8.pem")).unwrap()
);
}
}
#[test]
#[cfg(feature = "desktop")]
fn legacy_converter_reports_its_password_length_limit_without_requesting_a_retry() {
let password = "x".repeat(1024);
let encrypted = fs::read(fixture("rsa-pbes1-3des-password-1024.pem")).unwrap();
// The previous linked API accepted this boundary. The CLI cannot carry it;
// report that limitation rather than calling the correct password wrong.
openssl::pkey::PKey::private_key_from_pem_passphrase(&encrypted, password.as_bytes()).unwrap();
let error = load("rsa-cert.pem", "rsa-pbes1-3des-password-1024.pem", Some(&password)).unwrap_err();
assert!(
matches!(
error.downcast_ref::<RequestIdentityError>(),
Some(RequestIdentityError::LegacyKeyConversion(_))
),
"{error:?}"
);
assert!(error.to_string().contains("1023-byte"));
let accepted = load(
"rsa-cert.pem",
"rsa-pbes1-3des-password-1023.pem",
Some(&"x".repeat(1023)),
)
.unwrap();
assert_eq!(
accepted.key_data().unwrap(),
fs::read(fixture("rsa-pkcs8.pem")).unwrap()
);
}
#[test]
#[cfg(feature = "desktop")]
fn legacy_conversion_keeps_original_files_and_rejects_line_breaks_in_passwords() {
#[cfg(any(
target_os = "macos",
target_os = "linux",
target_os = "freebsd",
target_os = "openbsd"
))]
fn encrypted_key_import_keeps_original_files_and_redacts_passwords() {
let original = fs::read(fixture("rsa-pbes1-3des.pem")).unwrap();
for password in [
PASSWORD,
@@ -452,16 +358,15 @@ fn legacy_conversion_keeps_original_files_and_rejects_line_breaks_in_passwords()
let result = load("rsa-cert.pem", "rsa-pbes1-3des.pem", Some(password));
if password == PASSWORD {
result.unwrap();
} else if password != "incorrect" {
assert!(matches!(
result.unwrap_err().downcast_ref::<RequestIdentityError>(),
Some(RequestIdentityError::LegacyKeyConversion(_))
));
} else {
assert!(matches!(
result.unwrap_err().downcast_ref::<RequestIdentityError>(),
Some(RequestIdentityError::DecryptError("PEM"))
));
let error = result.unwrap_err();
assert!(!format!("{error:?}").contains(password));
if !password.contains('\0') {
assert!(matches!(
error.downcast_ref::<RequestIdentityError>(),
Some(RequestIdentityError::DecryptError("PEM"))
));
}
}
assert_eq!(fs::read(fixture("rsa-pbes1-3des.pem")).unwrap(), original);
}
@@ -535,7 +440,7 @@ fn prepared_pem_round_trip_retains_snapshot_and_private_file_lifecycle() {
}
#[test]
fn archive_snapshot_and_pfx_preserve_native_import_contract() {
fn archive_snapshot_and_pfx_preserve_prepared_identity() {
use zeroize::Zeroizing;
let directory = tempfile::tempdir().unwrap();
let path = directory.path().join("identity.pfx");
@@ -554,12 +459,7 @@ fn archive_snapshot_and_pfx_preserve_native_import_contract() {
assert!(restored.key_data().is_none());
assert!(!format!("{restored:?}").contains(PASSWORD));
let empty = fs::read(fixture("empty-password.p12")).unwrap();
// Preserve the native backend's existing empty-password decision.
let expected = reqwest::Identity::from_pkcs12_der(&empty, "").is_ok();
assert_eq!(
ClientIdentity::from_data(Zeroizing::new(empty), None, Some("")).is_ok(),
expected
);
assert!(ClientIdentity::from_data(Zeroizing::new(empty), None, Some("")).is_ok());
}
#[test]
@@ -712,11 +612,62 @@ fn mismatched_certificate_key_is_rejected_before_request_client_creation() {
);
}
#[cfg(not(feature = "desktop"))]
#[test]
fn portable_legacy_pkcs8_is_explicitly_unsupported() {
for password in [None, Some(PASSWORD)] {
let error = load("rsa-cert.pem", "rsa-pbes1-3des.pem", password).unwrap_err();
fn pkcs12_without_bag_ids_or_mac_matches_the_key_instead_of_bag_order() {
let data = fs::read(fixture("no-bag-ids.p12")).unwrap();
let archive = p12_keystore::Pkcs12Archive::from_pkcs12(&data, "").unwrap();
assert!(archive.keys[0].local_key_id.is_none());
assert!(archive.certs.iter().all(|bag| bag.local_key_id.is_none()));
assert_eq!(
archive.certs[0].cert.as_der(),
pem::parse(fs::read(fixture("root.pem")).unwrap()).unwrap().contents()
);
for password in [None, Some("")] {
let identity = ClientIdentity::from_data(data.clone().into(), None, password).unwrap();
assert_eq!(identity.certificate_data(), data);
assert!(identity.key_data().is_none());
}
}
#[test]
fn pkcs12_rejects_mismatched_and_multiple_identities() {
for (name, ambiguous) in [("mismatched.p12", false), ("multiple-identities.p12", true)] {
let error = ClientIdentity::load(fixture(name).to_str().unwrap(), None, None).unwrap_err();
match error.downcast_ref::<RequestIdentityError>() {
Some(RequestIdentityError::AmbiguousPkcs12) => assert!(ambiguous),
Some(RequestIdentityError::InvalidCertificate) => assert!(!ambiguous),
other => panic!("{name}: unexpected archive error: {other:?}"),
}
}
}
#[test]
fn owned_identity_accepts_certificate_pem_without_a_final_newline() {
let certificate = fs::read(fixture("rsa-cert.pem")).unwrap();
let certificate = certificate.trim_ascii_end().to_vec();
let key = fs::read(fixture("rsa-pkcs8.pem")).unwrap();
let identity = ClientIdentity::from_data(
zeroize::Zeroizing::new(certificate.clone()),
Some(zeroize::Zeroizing::new(key)),
None,
)
.unwrap();
assert_eq!(identity.certificate_data(), certificate);
}
#[test]
fn retained_pkcs12_password_rejects_nul_before_native_tunnel_setup() {
let password = "private\0password";
let error = ClientIdentity::load(fixture("no-bag-ids.p12").to_str().unwrap(), None, Some(password)).unwrap_err();
assert!(error.to_string().contains("NUL"));
assert!(!format!("{error:?}").contains(password));
}
#[cfg(target_os = "ios")]
#[test]
fn ios_reports_unsupported_legacy_encryption_without_a_native_importer() {
for key in ["rsa-pbes1-3des.pem", "rsa-traditional-aes256.pem"] {
let error = load("rsa-cert.pem", key, Some(PASSWORD)).unwrap_err();
assert!(matches!(
error.downcast_ref::<RequestIdentityError>(),
Some(RequestIdentityError::UnsupportedEncryption)
@@ -12,9 +12,9 @@ OPENSSL_BIN=/path/to/openssl bash generate.sh
```
Generation is random; it replaces the keys, certificates, and archives together.
The test runner reads checked-in files. Legacy standalone conversion tests
require an `openssl` executable on PATH (OpenSSL or compatible LibreSSL).
No fixture generation is needed to run the tests.
The test runner reads checked-in files and needs no converter executable.
No fixture generation is needed to run the tests. OpenSSL is used only by the
optional fixture-generation script, not identity import.
| Files | Contents |
| --- | --- |
@@ -24,38 +24,32 @@ No fixture generation is needed to run the tests.
| `{rsa,ec}-traditional-{aes128,aes192,aes256,des3}.pem` | Traditional encrypted PEM with AES-CBC or 3DES-CBC; password `fixture-password` |
| `rsa-pbes2-sha1.pem`, `rsa-pbes2-sha256.pem` | Encrypted PKCS#8, AES256-CBC/PBKDF2 with SHA1 or SHA256 PRF |
| `rsa-pbes2-aes-{128,192}-cbc-{sha1,sha256}.pem` | AES128/192-CBC with PBKDF2 SHA1/SHA256 |
| `rsa-pbes2-des3-{sha1,sha256}.pem` | PBES2 3DES-CBC with PBKDF2 SHA1/SHA256; distinct from the legacy PBE scheme handled by optional conversion |
| `rsa-pbes2-des3-{sha1,sha256}.pem` | PBES2 3DES-CBC with PBKDF2 SHA1/SHA256; distinct from the PKCS#12 PBE scheme handled by GnuTLS |
| `rsa-pbes2-des.pem` | PBES2 DES-CBC/PBKDF2-SHA256; Rust processing requires no native legacy provider |
| `rsa-pbes2-scrypt.pem` | PBES2 AES256-CBC with scrypt |
| `rsa-pbes1-3des-password-{1023,1024}.pem` | Same legacy encryption with passwords at the CLI input boundary; the linked importer accepts 1024 bytes, while the converter reports its 1023-byte limit explicitly |
| `rsa-pbes1-3des.pem` | Legacy PKCS#12 SHA1/3DES scheme inside standalone encrypted PKCS#8; optional OpenSSL executable conversion case |
| `empty-password.p12` | RSA leaf/key/root with an empty password, preserving native backend acceptance |
| `rsa-pbes1-3des-password-{1023,1024}.pem` | Same encryption with 1023- and 1024-byte passwords; both import without executable line limits |
| `rsa-pbes1-3des.pem` | Legacy PKCS#12 SHA1/3DES scheme inside standalone encrypted PKCS#8; private GnuTLS import case |
| `empty-password.p12` | RSA leaf/key/root with an empty password, accepted with omitted or empty password |
| `ec-pbes2.pem` | Encrypted EC PKCS#8, AES256-CBC/PBKDF2-SHA256 |
| `modern.p12`, `unicode-password.p12` | Leaf/key/root, AES256-CBC/PBKDF2-SHA256, SHA256 MAC; RSA and EC respectively |
| `certificate-only.pfx`, `key-only.pfx` | Incomplete PKCS#12 archives: certificate without key, and key without certificate; password `fixture-password` |
| `legacy-rc2.p12` | Issue #437 case: RC2-40 certificate bags, SHA1/3DES key bag, SHA1 MAC |
`tests/client_identity.rs` records native import compatibility and the owned
identity lifecycle. `tests/identity_encoding.rs` covers container normalization:
DER certificates and keys, RSA PKCS#1, Linux/BSD EC SEC1 and traditional encrypted
PEM, first-key selection, malformed encryption headers, and archive extension
capitalization. macOS rejects SEC1 and traditional encrypted PEM before password
handling. Its native importer also rejects the P-256 PKCS#8 PEM fixture, although
its native PKCS#12 import accepts the EC archive. Legacy-provider setup is isolated
in a child process on Linux/BSD.
`tests/client_identity.rs` covers all supported key families, password failures,
archive integrity, certificate/key matching, retained snapshots, private-file
permissions and cleanup. `tests/identity_encoding.rs` covers PEM/DER, RSA PKCS#1,
EC SEC1, traditional encrypted PEM, first-key selection, malformed encryption
headers and archive extension capitalization. Rustls validates RSA and EC keys
on all supported targets; desktop legacy encrypted-key imports use GnuTLS.
PBES2, including scrypt, stays in Rust. The no-executable child test runs with an
empty PATH and imports both legacy key families and RC2 PKCS#12.
Standalone PKCS#8 PBE-SHA1-3DES uses optional OpenSSL executable conversion,
with missing/wrong passwords preserving the old Linux prompt/retry contract.
Other unsupported schemes retain explicit conversion guidance. PBES2 DES/3DES
use Rust processing and need no executable. Child-process tests isolate PATH
for absent/unsupported/stalled converters, invalid output, and secret-free errors.
Timeout coverage includes wrappers whose descendants retain pipes after the
wrapper exits or is killed; all pipe I/O shares the process deadline.
Linux/BSD additionally compare the result with the former linked OpenSSL importer.
Archive integrity checks change the UTF-16 friendly name without changing the
MAC. Decrypted output is compared with independent plaintext fixture keys.
Unit tests in `utils/request.rs` cover missing keys, large local files and request
client construction.
The tests check archive integrity by changing the UTF-16 friendly name without
changing the MAC, and compare normalized PEM output against the independent
checked-in plaintext key. Public integration tests cover first-key selection,
retained snapshots, private-file permissions and cleanup, `.pfx`, empty-password
backend behavior, and chain retention. Unit tests in `utils/request.rs` cover
missing keys, large local files, and native request-client construction.
The unencrypted, MAC-less `no-bag-ids.p12`, `mismatched.p12`, and
`multiple-identities.p12` contain only the same public test keys/certificates.
They exercise PKCS#12 bags without optional attributes, reversed certificate
order/unrelated certificates, and terminal mismatched/ambiguous identities.
@@ -0,0 +1,53 @@
"""Assemble public, unencrypted PKCS#12 fixtures; no cryptography is performed."""
import base64
from pathlib import Path
ROOT = Path(__file__).resolve().parent
def pem(name):
lines = (ROOT / name).read_text().splitlines()
return base64.b64decode("".join(line for line in lines if not line.startswith("-----")))
def tlv(tag, content):
length = len(content)
encoded = length.to_bytes(max(1, (length.bit_length() + 7) // 8), "big")
prefix = bytes([length]) if length < 128 else bytes([128 + len(encoded)]) + encoded
return bytes([tag]) + prefix + content
def sequence(*items):
return tlv(0x30, b"".join(items))
def oid(value):
return tlv(6, bytes.fromhex(value))
def explicit(content):
return tlv(0xA0, content)
def data(content):
return sequence(oid("2a864886f70d010701"), explicit(tlv(4, content)))
def key(name):
return sequence(oid("2a864886f70d010c0a0101"), explicit(pem(name)))
def certificate(name):
cert_bag = sequence(oid("2a864886f70d01091601"), explicit(tlv(4, pem(name))))
return sequence(oid("2a864886f70d010c0a0103"), explicit(cert_bag))
def archive(name, *bags):
(ROOT / name).write_bytes(sequence(tlv(2, b"\x03"), data(sequence(data(sequence(*bags))))))
archive("no-bag-ids.p12", certificate("root.pem"), certificate("ec-cert.pem"),
key("rsa-pkcs8.pem"), certificate("rsa-cert.pem"))
archive("mismatched.p12", key("rsa-pkcs8.pem"), certificate("ec-cert.pem"), certificate("root.pem"))
archive("multiple-identities.p12", key("rsa-pkcs8.pem"), key("ec-pkcs8.pem"),
certificate("rsa-cert.pem"), certificate("ec-cert.pem"))
@@ -75,3 +75,6 @@ done
-name fixture -passout pass:fixture-password -out certificate-only.pfx
"$openssl_bin" pkcs12 -export -nocerts -inkey rsa-pkcs8.pem \
-name fixture -passout pass:fixture-password -out key-only.pfx
# Plain PKCS#12 bags let the import tests omit optional IDs and reorder bags.
python3 "$(dirname "$0")/generate-plain-pfx.py"
+37 -38
View File
@@ -65,41 +65,40 @@ fn encrypted_der_preserves_password_errors() {
}
#[test]
fn ec_sec1_normalizes_on_linux_and_bsd_and_is_terminal_on_macos() {
fn ec_sec1_pem_and_der_are_portable() {
let directory = tempfile::tempdir().unwrap();
let key = directory.path().join("ec.der");
fs::write(&key, pem::parse(bytes("ec-sec1.pem")).unwrap().contents()).unwrap();
for source in [fixture("ec-sec1.pem"), key] {
for password in [None, Some(PASSWORD)] {
let result = load(&fixture("ec-cert.pem"), &source, password);
if cfg!(target_os = "macos") {
assert!(matches!(
result.unwrap_err().downcast_ref::<RequestIdentityError>(),
Some(RequestIdentityError::UnsupportedKeyFormat)
));
} else {
assert_eq!(result.unwrap().key_data().unwrap(), bytes("ec-pkcs8.pem"));
}
let identity = load(&fixture("ec-cert.pem"), &source, password).unwrap();
assert_eq!(identity.key_data().unwrap(), bytes("ec-sec1.pem"));
}
}
}
#[test]
fn traditional_encrypted_pem_has_platform_specific_support_and_password_errors() {
#[cfg(any(
target_os = "macos",
target_os = "linux",
target_os = "freebsd",
target_os = "openbsd"
))]
fn traditional_encrypted_pem_preserves_password_errors() {
for algorithm in ["rsa", "ec"] {
for cipher in ["aes128", "aes192", "aes256", "des3"] {
let certificate = fixture(&format!("{algorithm}-cert.pem"));
let key = fixture(&format!("{algorithm}-traditional-{cipher}.pem"));
for password in [None, Some("incorrect"), Some(PASSWORD)] {
let result = load(&certificate, &key, password);
if cfg!(target_os = "macos") {
assert!(matches!(
result.unwrap_err().downcast_ref::<RequestIdentityError>(),
Some(RequestIdentityError::UnsupportedEncryption)
));
} else if password == Some(PASSWORD) {
if password == Some(PASSWORD) {
let identity = result.unwrap_or_else(|error| panic!("{key:?}: {error}"));
assert_eq!(identity.key_data().unwrap(), bytes(&format!("{algorithm}-pkcs8.pem")));
let actual = openssl::pkey::PKey::private_key_from_pem(identity.key_data().unwrap()).unwrap();
let expected = openssl::pkey::PKey::private_key_from_pem(&bytes(&format!("{algorithm}-pkcs8.pem"))).unwrap();
assert!(
actual.public_eq(&expected),
"{key:?}: imported key differs from the fixture"
);
assert!(identity.key_password().is_none());
} else {
let error = result.unwrap_err();
@@ -115,7 +114,12 @@ fn traditional_encrypted_pem_has_platform_specific_support_and_password_errors()
}
#[test]
#[cfg(not(target_os = "macos"))]
#[cfg(any(
target_os = "macos",
target_os = "linux",
target_os = "freebsd",
target_os = "openbsd"
))]
fn traditional_passphrase_with_nul_returns_a_redacted_terminal_error() {
let error = load(
&fixture("rsa-cert.pem"),
@@ -130,11 +134,16 @@ fn traditional_passphrase_with_nul_returns_a_redacted_terminal_error() {
for diagnostic in [error.to_string(), format!("{error:?}")] {
assert!(!diagnostic.contains("secret-password"));
assert!(!diagnostic.contains("do-not-log"));
assert!(!diagnostic.contains("115, 101, 99, 114, 101, 116"));
}
}
#[test]
#[cfg(any(
target_os = "macos",
target_os = "linux",
target_os = "freebsd",
target_os = "openbsd"
))]
fn first_traditional_encrypted_key_controls_combined_pem_loading() {
let directory = tempfile::tempdir().unwrap();
let source = directory.path().join("combined.pem");
@@ -144,24 +153,14 @@ fn first_traditional_encrypted_key_controls_combined_pem_loading() {
combined.extend(bytes("rsa-traditional-aes256.pem"));
combined.extend(bytes("rsa-pkcs8.pem"));
fs::write(&source, &combined).unwrap();
for password in [None, Some(PASSWORD)] {
let result = ClientIdentity::load(source.to_str().unwrap(), None, password);
if cfg!(target_os = "macos") {
assert!(matches!(
result.unwrap_err().downcast_ref::<RequestIdentityError>(),
Some(RequestIdentityError::UnsupportedEncryption)
));
} else if password.is_none() {
assert!(matches!(
result.unwrap_err().downcast_ref::<RequestIdentityError>(),
Some(RequestIdentityError::NoPassphrase("PEM"))
));
} else {
let identity = result.unwrap();
assert_eq!(identity.certificate_data(), chain);
assert_eq!(identity.key_data().unwrap(), bytes("rsa-pkcs8.pem"));
}
}
let error = ClientIdentity::load(source.to_str().unwrap(), None, None).unwrap_err();
assert!(matches!(
error.downcast_ref::<RequestIdentityError>(),
Some(RequestIdentityError::NoPassphrase("PEM"))
));
let identity = ClientIdentity::load(source.to_str().unwrap(), None, Some(PASSWORD)).unwrap();
assert_eq!(identity.certificate_data(), chain);
assert_eq!(identity.key_data().unwrap(), bytes("rsa-pkcs8.pem"));
assert_eq!(fs::read(source).unwrap(), combined);
}