Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions core-interface/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ sha2 = { version = "0.10", default-features = false }
# and the client keys are X25519, the device key ID a BLAKE2s-256 hash.
x25519-dalek = { version = "2.0", default-features = false, features = ["static_secrets", "zeroize"] }
blake2 = { version = "0.10", default-features = false }
chacha20poly1305 = { version = "0.10", default-features = false, features = ["alloc"] }
zeroize = { version = "1", default-features = false }

[features]
# `CommandAuthorization::for_tests`, for vehicle crate tests. Only enable it in
Expand All @@ -34,6 +36,7 @@ toml = { workspace = true }
[dev-dependencies]
# Reads the secure channel's test vectors from the contracts.
serde_json = "1"
x25519-dalek = { version = "2.0", default-features = false, features = ["static_secrets"] }
# The integration tests use the `test-utils` helpers.
core-interface = { path = ".", features = ["test-utils"] }
critical-section = { workspace = true }
Expand Down
107 changes: 107 additions & 0 deletions core-interface/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ fn main() -> Result<()> {

generate_ble_transport_constants()?;
generate_phone_validation_constants()?;
generate_secure_channel_constants()?;

Ok(())
}
Expand Down Expand Up @@ -198,3 +199,109 @@ fn parse_uuid(uuid: &str, field: &str) -> u128 {
u128::from_str_radix(&hex, 16)
.unwrap_or_else(|_| panic!("transport.toml {field} is not valid hex: {uuid}"))
}

/// The secure channel's constants (`[secure_channel]`, docs/secure-channel.md), for
/// `secure_channel.rs`, `identity.rs` and `client_keys.rs`.
fn generate_secure_channel_constants() -> Result<()> {
let toml_str = std::fs::read_to_string("../contracts/opencar/core/v1/transport.toml")
.expect("Failed to read contracts/opencar/core/v1/transport.toml");
let transport: toml::Value = toml::from_str(&toml_str).expect("Failed to parse transport.toml");
let section = transport.get("secure_channel").unwrap_or_else(|| {
panic!("transport.toml is missing [secure_channel]; update the contracts submodule")
});
let int = |key: &str| {
section
.get(key)
.and_then(|v| v.as_integer())
.unwrap_or_else(|| panic!("transport.toml is missing integer secure_channel.{key}"))
};
let string = |key: &str| {
section
.get(key)
.and_then(|v| v.as_str())
.unwrap_or_else(|| panic!("transport.toml is missing string secure_channel.{key}"))
.to_string()
};
// The wire layout is implemented by hand: a change has to change the code too.
assert_eq!(
string("protocol"),
"Noise_IK_25519_ChaChaPoly_BLAKE2s",
"secure_channel.protocol changed; update core_interface::noise"
);
assert!(
int("frame_type_handshake_init") == 1
&& int("frame_type_handshake_response") == 2
&& int("frame_type_transport") == 3
&& int("frame_type_unknown_session") == 5
&& int("session_id_bytes") == 4
&& int("counter_bytes") == 8
&& int("stream_length_prefix_bytes") == 2,
"transport.toml secure_channel frame layout changed; update core_interface::secure_channel"
);
let key_id_bytes = int("key_id_bytes");
assert!(
(1..=32).contains(&key_id_bytes),
"secure_channel.key_id_bytes out of range"
);
let replay_window = int("replay_window");
assert!(
(1..=64).contains(&replay_window),
"secure_channel.replay_window must fit a 64-bit bitmap"
);
let max_plaintext_bytes = int("max_plaintext_bytes");
// A frame (13-byte header, 16-byte tag) must fit the 2-byte stream length prefix.
assert!(
max_plaintext_bytes > 0 && max_plaintext_bytes + 29 <= 0xFFFF,
"secure_channel.max_plaintext_bytes out of range"
);
let max_sessions = int("max_sessions");
let max_sessions_per_client = int("max_sessions_per_client");
assert!(
(1..=255).contains(&max_sessions) && (1..=max_sessions).contains(&max_sessions_per_client),
"secure_channel session limits out of range"
);
let idle_timeout_seconds = int("idle_timeout_seconds");
let max_session_seconds = int("max_session_seconds");
let max_session_messages = int("max_session_messages");
let handshake_timeout_ms = int("handshake_timeout_ms");
let max_client_label_bytes = int("max_client_label_bytes");
assert!(
idle_timeout_seconds > 0
&& max_session_seconds > 0
&& max_session_messages > 0
&& handshake_timeout_ms > 0
&& (0..=255).contains(&max_client_label_bytes),
"secure_channel timeouts and limits out of range"
);
let prologue_label = string("prologue_label");

let out_dir = std::env::var("OUT_DIR").unwrap();
std::fs::write(
std::path::Path::new(&out_dir).join("secure_channel.rs"),
format!(
"// @generated — do not edit. Source: contracts/opencar/core/v1/transport.toml\n\
/// Start of the prologue, before a 0x00 byte and the device key ID.\n\
pub const PROLOGUE_LABEL: &[u8] = b{prologue_label:?};\n\
/// Bytes of the device key ID, before hex.\n\
pub const DEVICE_KEY_ID_BYTES: usize = {key_id_bytes};\n\
/// Transport frames accepted behind the highest counter seen.\n\
pub const REPLAY_WINDOW: u64 = {replay_window};\n\
/// Largest plaintext of one transport frame.\n\
pub const MAX_PLAINTEXT_BYTES: usize = {max_plaintext_bytes};\n\
/// Sessions the device keeps at once.\n\
pub const MAX_SESSIONS: usize = {max_sessions};\n\
/// Sessions one client may have at once.\n\
pub const MAX_SESSIONS_PER_CLIENT: usize = {max_sessions_per_client};\n\
/// A session without a valid transport frame for this long ends.\n\
pub const SESSION_IDLE_TIMEOUT_MS: u64 = {idle_timeout_seconds} * 1_000;\n\
/// A session ends this long after its handshake.\n\
pub const MAX_SESSION_MS: u64 = {max_session_seconds} * 1_000;\n\
/// A session ends after this many transport frames in one direction.\n\
pub const MAX_SESSION_MESSAGES: u64 = {max_session_messages};\n\
/// How long a client waits for the handshake response.\n\
pub const HANDSHAKE_TIMEOUT_MS: u64 = {handshake_timeout_ms};\n\
/// Longest client label, in UTF-8 bytes.\n\
pub const MAX_CLIENT_LABEL_BYTES: usize = {max_client_label_bytes};\n"
),
)
}
3 changes: 1 addition & 2 deletions core-interface/src/client_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ use embassy_sync::signal::Signal;

use crate::proto;

/// Longest label, in UTF-8 bytes: `secure_channel.max_client_label_bytes` (transport.toml).
pub const MAX_CLIENT_LABEL_BYTES: usize = 32;
pub use crate::secure_channel::MAX_CLIENT_LABEL_BYTES;

/// Format version of [`encode_client_keys`]; an unknown one reads as no key.
const CLIENT_KEYS_FORMAT_VERSION: u8 = 1;
Expand Down
8 changes: 5 additions & 3 deletions core-interface/src/dedupe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
//! transports, without answering it: the app then reports the result as unknown and
//! re-reads the state.
//!
//! The source is the transport-verified one (the BLE address), never the app's. MQTT
//! has no verified source yet (contracts#8, firmware#16), so its commands are keyed with
//! an empty source and only match other MQTT commands.
//! The source is the transport-verified one, never the app's: the BLE address, or the
//! secure channel client's ID, which for a phone is the same `ble:` ID. So a command
//! resent from BLE to the secure channel (or back) is caught. Plain MQTT has no verified
//! source, so its commands are keyed with an empty source and only match other MQTT
//! commands (plain MQTT commands are refused anyway, see `dispatch.rs`).

use alloc::collections::VecDeque;
use alloc::vec::Vec;
Expand Down
Loading