certs.rs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. //! `test(mtls)` certificate fixtures.
  2. //!
  3. //! RSA-2048 fixtures minted with `openssl` for task 21 only: a TLS server
  4. //! identity, a self-signed client, a client CA plus a CA-signed client, an
  5. //! unregistered rogue client, and an already-expired client. The live
  6. //! fixtures expire years after minting; if one expires, re-mint with the
  7. //! same subjects and update the pinned `x5t#S256` vectors in
  8. //! `tests/mtls.rs`. The expired fixture is deliberately outside its
  9. //! validity window and must stay that way.
  10. use rustls::pki_types::{CertificateDer, PrivateKeyDer, pem::PemObject};
  11. /// TLS server certificate (`CN=localhost`, SAN `localhost`/127.0.0.1).
  12. pub const SERVER_CERT_PEM: &str = include_str!("certs/server-cert.pem");
  13. /// TLS server private key (PKCS#8).
  14. pub const SERVER_KEY_PEM: &str = include_str!("certs/server-key.pem");
  15. /// Self-signed client certificate (`CN=mtls-test-client, O=Example`).
  16. pub const CLIENT_SELF_CERT_PEM: &str = include_str!("certs/client-self-cert.pem");
  17. /// Self-signed client private key (PKCS#8).
  18. pub const CLIENT_SELF_KEY_PEM: &str = include_str!("certs/client-self-key.pem");
  19. /// Client CA bundle (single self-signed CA, `CN=mtls-test-ca`).
  20. pub const CA_CERT_PEM: &str = include_str!("certs/ca-cert.pem");
  21. /// CA-signed client certificate (`CN=mtls-pki-client, O=Example`).
  22. pub const CLIENT_PKI_CERT_PEM: &str = include_str!("certs/client-pki-cert.pem");
  23. /// CA-signed client private key (PKCS#8).
  24. pub const CLIENT_PKI_KEY_PEM: &str = include_str!("certs/client-pki-key.pem");
  25. /// Unregistered self-signed client certificate (`CN=rogue, O=Example`).
  26. pub const CLIENT_ROGUE_CERT_PEM: &str = include_str!("certs/client-rogue-cert.pem");
  27. /// Expired self-signed client certificate (`CN=mtls-expired-client,
  28. /// O=Example`; valid 2020-01-01 to 2020-12-31 only).
  29. pub const CLIENT_EXPIRED_CERT_PEM: &str = include_str!("certs/client-expired-cert.pem");
  30. /// Fixture errors.
  31. #[derive(Debug, thiserror::Error)]
  32. pub enum FixtureError {
  33. /// A PEM block did not decode.
  34. #[error("fixture PEM error: {0}")]
  35. Pem(#[from] rustls_pki_types::pem::Error),
  36. }
  37. /// Decode the first certificate from a PEM fixture.
  38. pub fn cert_der(pem: &str) -> Result<CertificateDer<'static>, FixtureError> {
  39. Ok(CertificateDer::from_pem_slice(pem.as_bytes())?)
  40. }
  41. /// Decode the first private key from a PEM fixture.
  42. pub fn private_key(pem: &str) -> Result<PrivateKeyDer<'static>, FixtureError> {
  43. Ok(PrivateKeyDer::from_pem_slice(pem.as_bytes())?)
  44. }