[domistream] Pairing API: PIN handshake state machine, pure crypto steps, client identity, pinned host cert

This commit is contained in:
2026-11-25 11:45:57 +01:00
parent 0188ffba97
commit 736a8f42b0
4 changed files with 433 additions and 0 deletions
+156
View File
@@ -0,0 +1,156 @@
//! The client's certificate and key, and the host certificate learnt while pairing.
use core::fmt;
use crate::{RandomSource, Result};
/// Maximum size of a certificate in PEM form, in bytes.
pub const MAX_CERT_PEM_BYTES: usize = 16 * 1024;
/// Maximum size of a certificate in DER form, in bytes.
pub const MAX_CERT_DER_BYTES: usize = 12 * 1024;
/// Maximum size of a PKCS#8 private key in DER form, in bytes.
pub const MAX_KEY_DER_BYTES: usize = 8 * 1024;
/// RSA modulus size of keys this client generates.
pub const CLIENT_KEY_BITS: usize = 2048;
/// The client's long-lived identity towards GameStream hosts: a self-signed RSA-2048 / SHA-256 X.509 certificate
/// and its private key. Generated once per device and kept by the platform layer (never in logs; the key belongs
/// in the platform's secret store). `Debug` never prints the key.
#[derive(Clone)]
pub struct ClientIdentity {
cert_der: Vec<u8>,
key_pkcs8_der: Vec<u8>,
}
impl ClientIdentity {
/// Generates a new key and self-signed certificate.
///
/// `common_name` becomes the subject and issuer CN. `not_before_unix` is the validity start in Unix seconds
/// (passed in: no clock reads); validity runs 20 years from it. RSA key generation is slow (hundreds of ms on a
/// TV); call it once per device.
///
/// # Errors
/// [`crate::Error::Malformed`] for an empty or oversized `common_name`. Phase A: always
/// [`crate::Error::Unimplemented`].
pub fn generate(rng: &mut dyn RandomSource, common_name: &str, not_before_unix: i64) -> Result<Self> {
let _ = (rng, common_name, not_before_unix);
Err(crate::Error::Unimplemented)
}
/// Loads a stored identity. Checks that the key matches the certificate.
///
/// # Errors
/// [`crate::Error::TooLarge`] over [`MAX_CERT_DER_BYTES`] / [`MAX_KEY_DER_BYTES`], [`crate::Error::Malformed`]
/// for bad DER or a key that does not match. Phase A: always [`crate::Error::Unimplemented`].
pub fn from_der(cert_der: &[u8], key_pkcs8_der: &[u8]) -> Result<Self> {
let _ = (cert_der, key_pkcs8_der);
Err(crate::Error::Unimplemented)
}
/// The certificate, DER.
#[must_use]
pub fn cert_der(&self) -> &[u8] {
&self.cert_der
}
/// The private key, PKCS#8 DER, for the platform's secret store and its TLS stack.
#[must_use]
pub fn key_pkcs8_der(&self) -> &[u8] {
&self.key_pkcs8_der
}
/// The certificate as PEM text (what pairing step 1 sends, hex-encoded).
///
/// # Errors
/// Phase A: always [`crate::Error::Unimplemented`].
pub fn cert_pem(&self) -> Result<String> {
Err(crate::Error::Unimplemented)
}
/// The raw `signatureValue` bytes of the certificate (input to the pairing proofs).
///
/// # Errors
/// Phase A: always [`crate::Error::Unimplemented`].
pub fn cert_signature(&self) -> Result<Vec<u8>> {
Err(crate::Error::Unimplemented)
}
/// RSASSA-PKCS1-v1_5 / SHA-256 signature over `data` with the client key.
///
/// # Errors
/// Phase A: always [`crate::Error::Unimplemented`].
pub fn sign(&self, data: &[u8]) -> Result<Vec<u8>> {
let _ = data;
Err(crate::Error::Unimplemented)
}
}
impl fmt::Debug for ClientIdentity {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ClientIdentity")
.field("cert_der_len", &self.cert_der.len())
.field("key", &"<redacted>")
.finish()
}
}
/// The host certificate received as `plaincert` in pairing step 1, kept to pin HTTPS connections.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ServerCert {
der: Vec<u8>,
}
impl ServerCert {
/// Parses the PEM the host sent (already hex-decoded). Exactly one `CERTIFICATE` block.
///
/// # Errors
/// [`crate::Error::TooLarge`] over [`MAX_CERT_PEM_BYTES`], [`crate::Error::Malformed`] for anything that is
/// not one well-formed X.509 certificate with an RSA key. Phase A: always [`crate::Error::Unimplemented`].
pub fn from_pem(pem: &[u8]) -> Result<Self> {
let _ = pem;
Err(crate::Error::Unimplemented)
}
/// Loads a stored certificate.
///
/// # Errors
/// [`crate::Error::TooLarge`] over [`MAX_CERT_DER_BYTES`], [`crate::Error::Malformed`] otherwise. Phase A:
/// always [`crate::Error::Unimplemented`].
pub fn from_der(der: &[u8]) -> Result<Self> {
let _ = der;
Err(crate::Error::Unimplemented)
}
/// The certificate, DER.
#[must_use]
pub fn der(&self) -> &[u8] {
&self.der
}
/// The raw `signatureValue` bytes (input to the pairing proofs).
///
/// # Errors
/// Phase A: always [`crate::Error::Unimplemented`].
pub fn signature(&self) -> Result<Vec<u8>> {
Err(crate::Error::Unimplemented)
}
/// Verifies an RSASSA-PKCS1-v1_5 / SHA-256 `signature` over `data` with the certificate's public key.
///
/// # Errors
/// [`crate::Error::Crypto`] when it does not verify. Phase A: always [`crate::Error::Unimplemented`].
pub fn verify(&self, data: &[u8], signature: &[u8]) -> Result<()> {
let _ = (data, signature);
Err(crate::Error::Unimplemented)
}
/// TLS pinning: whether the certificate a host presents over HTTPS (DER) is exactly this one, compared in
/// constant time. The platform's TLS verifier calls this and nothing else to accept the host.
///
/// # Errors
/// [`crate::Error::TooLarge`] over [`MAX_CERT_DER_BYTES`]. Phase A: always [`crate::Error::Unimplemented`].
pub fn matches(&self, presented_der: &[u8]) -> Result<bool> {
let _ = presented_der;
Err(crate::Error::Unimplemented)
}
}