certs.rs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 base64::{Engine as _, engine::general_purpose::STANDARD};
  11. use rustls::pki_types::{CertificateDer, PrivateKeyDer, pem::PemObject};
  12. /// TLS server certificate (`CN=localhost`, SAN `localhost`/127.0.0.1).
  13. pub const SERVER_CERT_PEM: &str = include_str!("certs/server-cert.pem");
  14. /// TLS server private key (PKCS#8).
  15. pub const SERVER_KEY_PEM: &str = include_str!("certs/server-key.pem");
  16. /// Self-signed client certificate (`CN=mtls-test-client, O=Example`).
  17. pub const CLIENT_SELF_CERT_PEM: &str = include_str!("certs/client-self-cert.pem");
  18. /// Self-signed client private key (PKCS#8).
  19. pub const CLIENT_SELF_KEY_PEM: &str = include_str!("certs/client-self-key.pem");
  20. /// Client CA bundle (single self-signed CA, `CN=mtls-test-ca`).
  21. pub const CA_CERT_PEM: &str = include_str!("certs/ca-cert.pem");
  22. /// CA-signed client certificate (`CN=mtls-pki-client, O=Example`).
  23. pub const CLIENT_PKI_CERT_PEM: &str = include_str!("certs/client-pki-cert.pem");
  24. /// CA-signed client private key (PKCS#8).
  25. pub const CLIENT_PKI_KEY_PEM: &str = include_str!("certs/client-pki-key.pem");
  26. /// Unregistered self-signed client certificate (`CN=rogue, O=Example`).
  27. pub const CLIENT_ROGUE_CERT_PEM: &str = include_str!("certs/client-rogue-cert.pem");
  28. /// Expired self-signed client certificate (`CN=mtls-expired-client,
  29. /// O=Example`; valid 2020-01-01 to 2020-12-31 only).
  30. pub const CLIENT_EXPIRED_CERT_PEM: &str = include_str!("certs/client-expired-cert.pem");
  31. /// Fixture errors.
  32. #[derive(Debug, thiserror::Error)]
  33. pub enum FixtureError {
  34. /// A PEM block did not decode.
  35. #[error("fixture PEM error: {0}")]
  36. Pem(#[from] rustls_pki_types::pem::Error),
  37. }
  38. /// Decode the first certificate from a PEM fixture.
  39. pub fn cert_der(pem: &str) -> Result<CertificateDer<'static>, FixtureError> {
  40. Ok(CertificateDer::from_pem_slice(pem.as_bytes())?)
  41. }
  42. /// Decode the first private key from a PEM fixture.
  43. pub fn private_key(pem: &str) -> Result<PrivateKeyDer<'static>, FixtureError> {
  44. Ok(PrivateKeyDer::from_pem_slice(pem.as_bytes())?)
  45. }
  46. /// Standard-base64 DER of the first certificate in a PEM fixture (the
  47. /// trusted-proxy header encoding).
  48. pub fn der_b64(pem: &str) -> Result<String, FixtureError> {
  49. Ok(STANDARD.encode(cert_der(pem)?.as_ref()))
  50. }