1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
use crate::*;
use candid::Principal;
use dmsg_types::{cose::*, user::*, *};
/// Local validation of a device enrollment proposal; no device registration or authorization.
pub trait DeviceInputExt {
/// Check nonzero device/HPKE bytes, 1..5 unique capabilities and a nonweak Ed25519 key.
///
/// This does not verify proof of possession, fully validate an HPKE point or
/// apply account-specific role rules.
///
/// # Errors
/// Invalid identifiers/capabilities return `Error::InvalidInput`; invalid signing
/// keys return `Error::IntegrityFailed`.
fn validate(&self) -> Result<()>;
}
impl DeviceInputExt for DeviceInput {
fn validate(&self) -> Result<()> {
nonzero(self.device_id.as_slice())?;
nonzero(self.hpke_pub.as_slice())?;
ensure_valid(
!self.capabilities.is_empty() && self.capabilities.len() <= 5,
"capabilities",
)?;
// At most five capabilities: a slice scan avoids a tree allocation.
ensure_valid(
self.capabilities
.iter()
.enumerate()
.all(|(i, capability)| !self.capabilities[..i].contains(capability)),
"duplicate capability",
)?;
validate_ed25519_key(self.signing_pub.as_slice())
}
}
/// Convert the typed signing interface into the low-level approved execution contract.
pub trait SignRequestExt {
/// Consume the request and prepare its canonical signing bytes and derived purpose.
///
/// Validates the statement and kid, fixing signing generation to 1. Preserves
/// the supplied approval and fingerprint without verifying either. The executing
/// services check the origin against their deployment environment with
/// [`validate_origin`]. Compute the execution approval via [`ExecuteRequestExt`]
/// after freezing the request.
///
/// # Errors
/// Propagates [`prepare_cose`] validation errors.
fn into_execution(self) -> Result<ExecuteRequest>;
}
impl SignRequestExt for SignRequest {
fn into_execution(self) -> Result<ExecuteRequest> {
ensure(
!matches!(self.statement.content, StatementContent::AppAction(_)),
Error::UnsupportedProtocol,
)?;
let algorithm: Algorithm = self.key.algorithm.into();
let purpose = statement_purpose(&self.statement);
let (_, bytes) = prepare_cose(&self.statement, &algorithm, &self.key.kid)?;
Ok(ExecuteRequest {
account_id: self.account_id,
max_cycles: self.max_cycles,
approval: self.approval,
kind: ExecutionKind::Sign {
key: KeyRequest {
purpose,
algorithm,
generation: 1,
},
to_be_signed: bytes.into(),
public_key_fingerprint: self.key.public_key_fingerprint,
origin: self.origin,
},
})
}
}
/// Validate fixed COSE deployment configuration without accessing ICP master keys.
pub trait CoseInitExt {
/// Check executing canister against id, derivation version 2, user home, namespace,
/// 1..3 distinct algorithms, key names and positive budgets. Production requires
/// key_1 and nonzero master fingerprints; this does not fetch/check actual keys.
///
/// # Errors
/// Invalid configuration returns `Error::InvalidInput`; excluded user-home
/// Principals return `Error::AuthRequired`.
fn validate(&self, id: Principal) -> Result<()>;
}
impl CoseInitExt for CoseInit {
fn validate(&self, id: Principal) -> Result<()> {
ensure_valid(
self.executing_canister == id && self.derivation_version == 2,
"immutable key home/version",
)?;
authenticated(self.initial_home_user)?;
validate_namespace(&self.issuer_namespace)?;
ensure_valid(
!self.masters.is_empty() && self.masters.len() <= 3,
"masters",
)?;
for (i, k) in self.masters.iter().enumerate() {
ensure_valid(
!self.masters[..i].iter().any(|x| x.algorithm == k.algorithm),
"duplicate algorithm",
)?;
if self.environment == Environment::Production {
ensure_valid(k.key_name == "key_1", "production requires key_1")?;
nonzero(k.expected_fingerprint.as_slice())?;
} else {
ensure_valid(
matches!(k.key_name.as_str(), "key_1" | "test_key_1" | "dfx_test_key"),
"key name",
)?;
}
}
ensure_valid(
self.daily_executions > 0 && self.daily_cycles > 0,
"hard budgets",
)
}
}
/// Check supported purpose, algorithm and generation combinations.
pub trait KeyRequestExt {
/// Require a positive generation, vetKD for ContentRoot, generation 1 with
/// Ed25519 or ES256K for formal signing, and Ed25519 with a generation of at
/// most `agent::MAX_CONTROLLER_RECORDS` for agent controllers. Does not check
/// account/root/controller existence.
///
/// # Errors
/// Zero generation returns `Error::InvalidInput`; incompatible combinations
/// return `Error::UnsupportedProtocol`.
fn validate(&self) -> Result<()>;
}
impl KeyRequestExt for KeyRequest {
fn validate(&self) -> Result<()> {
ensure_valid(self.generation > 0, "generation")?;
match self.purpose {
KeyPurpose::ContentRoot => ensure(
self.algorithm == Algorithm::VetKdBls12381,
Error::UnsupportedProtocol,
),
KeyPurpose::Statement | KeyPurpose::FileAttestation | KeyPurpose::AppAction => ensure(
self.generation == 1 && self.algorithm != Algorithm::VetKdBls12381,
Error::UnsupportedProtocol,
),
KeyPurpose::AgentController => ensure(
self.generation <= dmsg_types::agent::MAX_CONTROLLER_RECORDS as u64
&& self.algorithm == Algorithm::Ed25519,
Error::UnsupportedProtocol,
),
}
}
}
/// Build the device approval digest for a frozen signing or root-derivation request.
pub trait ExecuteRequestExt {
/// Bind kind and max_cycles under dmsg/execute/v3, inside dmsg/device-approval/v2.
///
/// Includes the target user home and replay context via [`approval_message`].
/// The signature field is ignored. This constructs a digest, not an authorization
/// check, and performs no request validation or sequence mutation.
fn approval_message(&self, home_user: Principal) -> Hash;
}
impl ExecuteRequestExt for ExecuteRequest {
/// Shared by typed sign/root requests: changing the public Candid interface
/// does not change the approved bytes or key derivation domains.
fn approval_message(&self, home_user: Principal) -> Hash {
approval_message(
home_user,
&self.account_id,
"dmsg/execute/v3",
&(&self.kind, self.max_cycles),
&self.approval,
)
}
}
/// Build the recovery-key reconfirmation digest under `dmsg/recovery-reconfirm/v2`.
///
/// Binds user home, account, recovery nonce, full recovery request and dispute
/// confirmation. Sign the digest with the offline recovery signing key, not a
/// device key. This does not check policy, deadlines, or signature validity.
pub fn recovery_confirmation_message(
home: Principal,
account_id: &AccountId,
nonce: u64,
request: &RecoveryRequest,
confirmation: &RecoveryConfirmation,
) -> Hash {
digest(
"dmsg/recovery-reconfirm/v2",
&(home, account_id, nonce, request, confirmation),
)
}
#[cfg(test)]
mod tests;
/// Prepare exact application execution bytes, without performing authorization.
impl SignRequestExt for AppActionSignRequest {
fn into_execution(self) -> Result<ExecuteRequest> {
// prepare_cose validates the complete action, including its origin.
ensure(
self.account_id == self.action.signing_account,
Error::Forbidden,
)?;
ensure(
self.approval.expires_at <= self.action.expires_at_ms,
Error::Expired,
)?;
let origin = self.action.origin.clone();
let statement = Statement {
issuer: self.issuer,
subject: None,
issued_at: None,
content: StatementContent::AppAction(Box::new(self.action)),
};
let algorithm: Algorithm = self.key.algorithm.into();
let (_, bytes) = prepare_cose(&statement, &algorithm, &self.key.kid)?;
Ok(ExecuteRequest {
account_id: self.account_id,
max_cycles: self.max_cycles,
approval: self.approval,
kind: ExecutionKind::Sign {
key: KeyRequest {
purpose: KeyPurpose::AppAction,
algorithm,
generation: 1,
},
to_be_signed: bytes.into(),
public_key_fingerprint: self.key.public_key_fingerprint,
origin,
},
})
}
}