certs.rs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. //! `test(mtls)` certificate fixtures.
  2. //!
  3. //! Short-lived RSA-2048 fixtures minted with `openssl` for task 21 only:
  4. //! a TLS server identity, a self-signed client, a client CA plus a
  5. //! CA-signed client, and an unregistered rogue client. They expire days
  6. //! after minting; if a fixture expires, re-mint with the same subjects and
  7. //! update the pinned `x5t#S256` vectors in `tests/mtls.rs`.
  8. use base64::{Engine as _, engine::general_purpose::STANDARD};
  9. use rustls::pki_types::{CertificateDer, PrivateKeyDer, pem::PemObject};
  10. /// TLS server certificate (`CN=localhost`, SAN `localhost`/127.0.0.1).
  11. pub const SERVER_CERT_PEM: &str = include_str!("certs/server-cert.pem");
  12. /// TLS server private key (PKCS#8).
  13. pub const SERVER_KEY_PEM: &str = include_str!("certs/server-key.pem");
  14. /// Self-signed client certificate (`CN=mtls-test-client, O=Example`).
  15. pub const CLIENT_SELF_CERT_PEM: &str = include_str!("certs/client-self-cert.pem");
  16. /// Self-signed client private key (PKCS#8).
  17. pub const CLIENT_SELF_KEY_PEM: &str = include_str!("certs/client-self-key.pem");
  18. /// Client CA bundle (single self-signed CA, `CN=mtls-test-ca`).
  19. pub const CA_CERT_PEM: &str = include_str!("certs/ca-cert.pem");
  20. /// CA-signed client certificate (`CN=mtls-pki-client, O=Example`).
  21. pub const CLIENT_PKI_CERT_PEM: &str = include_str!("certs/client-pki-cert.pem");
  22. /// CA-signed client private key (PKCS#8).
  23. pub const CLIENT_PKI_KEY_PEM: &str = include_str!("certs/client-pki-key.pem");
  24. /// Unregistered self-signed client certificate (`CN=rogue, O=Example`).
  25. pub const CLIENT_ROGUE_CERT_PEM: &str = include_str!("certs/client-rogue-cert.pem");
  26. /// Fixture errors.
  27. #[derive(Debug, thiserror::Error)]
  28. pub enum FixtureError {
  29. /// A PEM block did not decode.
  30. #[error("fixture PEM error: {0}")]
  31. Pem(#[from] rustls_pki_types::pem::Error),
  32. }
  33. /// Decode the first certificate from a PEM fixture.
  34. pub fn cert_der(pem: &str) -> Result<CertificateDer<'static>, FixtureError> {
  35. Ok(CertificateDer::from_pem_slice(pem.as_bytes())?)
  36. }
  37. /// Decode the first private key from a PEM fixture.
  38. pub fn private_key(pem: &str) -> Result<PrivateKeyDer<'static>, FixtureError> {
  39. Ok(PrivateKeyDer::from_pem_slice(pem.as_bytes())?)
  40. }
  41. /// Standard-base64 DER of the first certificate in a PEM fixture (the
  42. /// trusted-proxy header encoding).
  43. pub fn der_b64(pem: &str) -> Result<String, FixtureError> {
  44. Ok(STANDARD.encode(cert_der(pem)?.as_ref()))
  45. }