/
delegation.rs
217 lines (187 loc) · 7.12 KB
/
delegation.rs
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
use crate::ii_domain::IIDomain;
use crate::state::persistent_state_mut;
use crate::{hash, state, update_root_hash, DAY_NS, MINUTE_NS};
use candid::Principal;
use canister_sig_util::signature_map::SignatureMap;
use canister_sig_util::CanisterSigPublicKey;
use ic_cdk::api::time;
use ic_cdk::{id, trap};
use ic_certification::Hash;
use internet_identity_interface::internet_identity::types::*;
use serde_bytes::ByteBuf;
use std::collections::HashMap;
use std::net::IpAddr;
// The expiration used for delegations if none is specified
// (calculated as now() + this)
const DEFAULT_EXPIRATION_PERIOD_NS: u64 = 30 * MINUTE_NS;
// The maximum expiration time for delegation
// (calculated as now() + this)
const MAX_EXPIRATION_PERIOD_NS: u64 = 30 * DAY_NS;
pub async fn prepare_delegation(
anchor_number: AnchorNumber,
frontend: FrontendHostname,
session_key: SessionKey,
max_time_to_live: Option<u64>,
ii_domain: &Option<IIDomain>,
) -> (UserKey, Timestamp) {
state::ensure_salt_set().await;
check_frontend_length(&frontend);
let delta = u64::min(
max_time_to_live.unwrap_or(DEFAULT_EXPIRATION_PERIOD_NS),
MAX_EXPIRATION_PERIOD_NS,
);
let expiration = time().saturating_add(delta);
let seed = calculate_seed(anchor_number, &frontend);
state::signature_map_mut(|sigs| {
add_delegation_signature(sigs, session_key, seed.as_ref(), expiration);
});
update_root_hash();
delegation_bookkeeping(frontend, ii_domain);
(
ByteBuf::from(der_encode_canister_sig_key(seed.to_vec())),
expiration,
)
}
/// Update metrics and the list of latest front-end origins.
fn delegation_bookkeeping(frontend: FrontendHostname, ii_domain: &Option<IIDomain>) {
state::usage_metrics_mut(|metrics| {
metrics.delegation_counter += 1;
});
if ii_domain.is_some() && !is_dev_frontend(&frontend) {
update_latest_delegation_origins(frontend);
}
}
/// Filter out derivation origins that most likely point to development setups.
/// This is not bullet proof but given the data we collected so far it should be good for now.
fn is_dev_frontend(frontend: &FrontendHostname) -> bool {
if frontend.starts_with("http://") || frontend.contains("localhost") {
// we don't care about insecure origins or localhost
return true;
}
// lets check for local IP addresses
if let Some(hostname) = frontend
.strip_prefix("https://")
.and_then(|s| s.split(':').next())
{
return match hostname.parse::<IpAddr>() {
Ok(IpAddr::V4(addr)) => addr.is_private() || addr.is_loopback(),
Ok(IpAddr::V6(addr)) => addr.is_loopback(),
Err(_) => false,
};
}
false
}
/// Add the current front-end to the list of latest used front-end origins.
fn update_latest_delegation_origins(frontend: FrontendHostname) {
let now_ns = time();
persistent_state_mut(|persistent_state| {
let latest_delegation_origins = &mut persistent_state.latest_delegation_origins;
if let Some(timestamp_ns) = latest_delegation_origins.get_mut(&frontend) {
*timestamp_ns = now_ns;
} else {
latest_delegation_origins.insert(frontend, now_ns);
};
// drop entries older than 30 days
latest_delegation_origins.retain(|_, timestamp_ns| now_ns - *timestamp_ns < 30 * DAY_NS);
// if we still have too many entries, drop the oldest
if latest_delegation_origins.len() as u64
> persistent_state.max_num_latest_delegation_origins
{
// if this case is hit often (i.e. we routinely have more than 1000 entries), we should
// consider using a more efficient data structure
let mut values: Vec<_> = latest_delegation_origins.clone().into_iter().collect();
values.sort_by(|(_, timestamp_1), (_, timestamp_2)| timestamp_1.cmp(timestamp_2));
latest_delegation_origins.remove(&values[0].0);
};
});
}
pub fn get_delegation(
anchor_number: AnchorNumber,
frontend: FrontendHostname,
session_key: SessionKey,
expiration: Timestamp,
) -> GetDelegationResponse {
check_frontend_length(&frontend);
state::assets_and_signatures(|certified_assets, sigs| {
let message_hash = delegation_signature_msg_hash(&Delegation {
pubkey: session_key.clone(),
expiration,
targets: None,
});
match sigs.get_signature_as_cbor(
&calculate_seed(anchor_number, &frontend),
message_hash,
Some(certified_assets.root_hash()),
) {
Ok(signature) => GetDelegationResponse::SignedDelegation(SignedDelegation {
delegation: Delegation {
pubkey: session_key,
expiration,
targets: None,
},
signature: ByteBuf::from(signature),
}),
Err(_) => GetDelegationResponse::NoSuchDelegation,
}
})
}
pub fn get_principal(anchor_number: AnchorNumber, frontend: FrontendHostname) -> Principal {
check_frontend_length(&frontend);
let seed = calculate_seed(anchor_number, &frontend);
let public_key = der_encode_canister_sig_key(seed.to_vec());
Principal::self_authenticating(public_key)
}
fn calculate_seed(anchor_number: AnchorNumber, frontend: &FrontendHostname) -> Hash {
let salt = state::salt();
let mut blob: Vec<u8> = vec![];
blob.push(salt.len() as u8);
blob.extend_from_slice(&salt);
let anchor_number_str = anchor_number.to_string();
let anchor_number_blob = anchor_number_str.bytes();
blob.push(anchor_number_blob.len() as u8);
blob.extend(anchor_number_blob);
blob.push(frontend.bytes().len() as u8);
blob.extend(frontend.bytes());
hash::hash_bytes(blob)
}
pub(crate) fn der_encode_canister_sig_key(seed: Vec<u8>) -> Vec<u8> {
let my_canister_id = id();
CanisterSigPublicKey::new(my_canister_id, seed).to_der()
}
fn delegation_signature_msg_hash(d: &Delegation) -> Hash {
use hash::Value;
let mut m = HashMap::new();
m.insert("pubkey", Value::Bytes(d.pubkey.as_slice()));
m.insert("expiration", Value::U64(d.expiration));
if let Some(targets) = d.targets.as_ref() {
let mut arr = Vec::with_capacity(targets.len());
for t in targets.iter() {
arr.push(Value::Bytes(t.as_ref()));
}
m.insert("targets", Value::Array(arr));
}
let map_hash = hash::hash_of_map(m);
hash::hash_with_domain(b"ic-request-auth-delegation", &map_hash)
}
fn add_delegation_signature(
sigs: &mut SignatureMap,
pk: PublicKey,
seed: &[u8],
expiration: Timestamp,
) {
let msg_hash = delegation_signature_msg_hash(&Delegation {
pubkey: pk,
expiration,
targets: None,
});
sigs.add_signature(seed, msg_hash);
}
pub(crate) fn check_frontend_length(frontend: &FrontendHostname) {
const FRONTEND_HOSTNAME_LIMIT: usize = 255;
let n = frontend.len();
if frontend.len() > FRONTEND_HOSTNAME_LIMIT {
trap(&format!(
"frontend hostname {n} exceeds the limit of {FRONTEND_HOSTNAME_LIMIT} bytes",
));
}
}