|
9 | 9 | //! - [`TestCa::new`] makes a CA that only the test knows, so nothing trusts it |
10 | 10 | //! unless the test hands over [`TestCa::cert_pem`]. |
11 | 11 | //! - [`TestCa::from_env`] loads the environment's additional CA, which the |
12 | | -//! gateway is configured to trust (`additional_ca_bundle`). The environment |
| 12 | +//! supervisor trusts for sandbox egress (`additional_ca_bundle`). The environment |
13 | 13 | //! publishes the paths of its PEM certificate and key in |
14 | 14 | //! `OPENSHELL_TEST_ADDITIONAL_CA_CERT` and `OPENSHELL_TEST_ADDITIONAL_CA_KEY`. |
15 | 15 |
|
@@ -82,9 +82,23 @@ impl TestCa { |
82 | 82 | "parse the CA certificate {}", |
83 | 83 | cert.display() |
84 | 84 | )))?; |
85 | | - let key = KeyPair::from_pem(&key_pem) |
| 85 | + let key_pair = KeyPair::from_pem(&key_pem) |
86 | 86 | .map_err(failed(format!("parse the CA key {}", key.display())))?; |
87 | | - let issuer = pki::issuer_from_der(&der, key) |
| 87 | + // issuer_from_der reads the certificate's metadata only, so check the |
| 88 | + // key here rather than at the first client handshake. |
| 89 | + let key_der = key_pair |
| 90 | + .serialize_der() |
| 91 | + .map_err(failed(format!("export the CA key {}", key.display())))?; |
| 92 | + openshell_crypto::tls::try_server_builder() |
| 93 | + .map_err(failed("build a TLS server configuration"))? |
| 94 | + .with_no_client_auth() |
| 95 | + .with_single_cert(vec![der.clone()], PrivateKeyDer::Pkcs8(key_der.into())) |
| 96 | + .map_err(failed(format!( |
| 97 | + "the CA key {} does not match the CA certificate {}", |
| 98 | + key.display(), |
| 99 | + cert.display() |
| 100 | + )))?; |
| 101 | + let issuer = pki::issuer_from_der(&der, key_pair) |
88 | 102 | .map_err(failed(format!("load the CA {}", cert.display())))?; |
89 | 103 | Ok(Self { cert_pem, issuer }) |
90 | 104 | } |
@@ -301,6 +315,14 @@ mod tests { |
301 | 315 | ); |
302 | 316 | } |
303 | 317 |
|
| 318 | + #[test] |
| 319 | + fn from_files_rejects_a_key_from_another_ca() { |
| 320 | + let (cert, _, _) = write_ca("host-server-mismatch-a"); |
| 321 | + let (_, other_key, _) = write_ca("host-server-mismatch-b"); |
| 322 | + let error = TestCa::from_files(&cert, &other_key).err().unwrap(); |
| 323 | + assert!(error.to_string().contains("does not match"), "{error}"); |
| 324 | + } |
| 325 | + |
304 | 326 | #[test] |
305 | 327 | fn leaf_needs_a_name() { |
306 | 328 | assert!(TestCa::new().unwrap().leaf(&[]).is_err()); |
|
0 commit comments