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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
use std::fmt;
use std::fs;
use std::io::Write;
use std::os::unix::fs::OpenOptionsExt;
use std::path::{Path, PathBuf};
pub const ED25519_PUB_MULTICODEC: [u8; 2] = [0xed, 0x01];
pub const DID_KEY_PREFIX: &str = "did:key:z";
pub const SECRET_HEX_LEN: usize = 64;
#[derive(Debug)]
pub enum IdentityError {
Read(PathBuf, std::io::Error),
Write(PathBuf, std::io::Error),
Encoding(String),
Random(String),
}
impl fmt::Display for IdentityError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
IdentityError::Read(path, err) => write!(f, "read secret {}: {err}", path.display()),
IdentityError::Write(path, err) => write!(f, "write secret {}: {err}", path.display()),
IdentityError::Encoding(msg) => write!(f, "{msg}"),
IdentityError::Random(msg) => write!(f, "generate secret: {msg}"),
}
}
}
impl std::error::Error for IdentityError {}
#[derive(Clone)]
pub struct Identity {
pub secret: iroh::SecretKey,
}
impl Identity {
pub fn from_seed(seed: &[u8; 32]) -> Identity {
Identity {
secret: iroh::SecretKey::from_bytes(seed),
}
}
pub fn public(&self) -> [u8; 32] {
*self.secret.public().as_bytes()
}
pub fn endpoint_id_hex(&self) -> String {
endpoint_id_hex(&self.public())
}
pub fn did_key(&self) -> String {
did_key(&self.public())
}
}
pub fn endpoint_id_hex(public: &[u8; 32]) -> String {
hex::encode(public)
}
pub fn parse_endpoint_id_hex(text: &str) -> Result<[u8; 32], IdentityError> {
let trimmed = text.trim();
if trimmed.len() != 64 {
return Err(IdentityError::Encoding(format!(
"endpoint id {trimmed:?} is {} chars, want 64 hex",
trimmed.len()
)));
}
let mut out = [0u8; 32];
hex::decode_to_slice(trimmed, &mut out)
.map_err(|err| IdentityError::Encoding(format!("endpoint id {trimmed:?}: {err}")))?;
iroh::PublicKey::from_bytes(&out).map_err(|err| {
IdentityError::Encoding(format!(
"endpoint id {trimmed:?} is not an ed25519 point: {err}"
))
})?;
Ok(out)
}
pub fn did_key(public: &[u8; 32]) -> String {
let mut bytes = Vec::with_capacity(34);
bytes.extend_from_slice(&ED25519_PUB_MULTICODEC);
bytes.extend_from_slice(public);
format!("{DID_KEY_PREFIX}{}", bs58::encode(bytes).into_string())
}
pub fn parse_did_key(did: &str) -> Result<[u8; 32], IdentityError> {
let encoded = did
.strip_prefix(DID_KEY_PREFIX)
.ok_or_else(|| IdentityError::Encoding(format!("{did:?} is not a base58btc did:key")))?;
let bytes = bs58::decode(encoded)
.into_vec()
.map_err(|err| IdentityError::Encoding(format!("{did:?}: {err}")))?;
if bytes.len() != 34 || bytes[0..2] != ED25519_PUB_MULTICODEC {
return Err(IdentityError::Encoding(format!(
"{did:?} does not carry an ed25519-pub multicodec key"
)));
}
let mut out = [0u8; 32];
out.copy_from_slice(&bytes[2..]);
Ok(out)
}
pub fn parse_secret_hex(text: &str) -> Result<[u8; 32], IdentityError> {
let trimmed = text.trim();
if trimmed.len() != SECRET_HEX_LEN {
return Err(IdentityError::Encoding(format!(
"secret is {} chars, want {SECRET_HEX_LEN} hex (the IROH_SECRET encoding)",
trimmed.len()
)));
}
let mut out = [0u8; 32];
hex::decode_to_slice(trimmed, &mut out)
.map_err(|err| IdentityError::Encoding(format!("secret is not hex: {err}")))?;
Ok(out)
}
pub enum Loaded {
Existing(Identity),
Created(Identity),
}
impl Loaded {
pub fn identity(&self) -> &Identity {
match self {
Loaded::Existing(identity) | Loaded::Created(identity) => identity,
}
}
}
pub fn load_or_create(path: &Path) -> Result<Loaded, IdentityError> {
match fs::read_to_string(path) {
Ok(text) => {
let seed = parse_secret_hex(&text).map_err(|err| {
IdentityError::Encoding(format!("secret file {}: {err}", path.display()))
})?;
Ok(Loaded::Existing(Identity::from_seed(&seed)))
}
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
let mut seed = [0u8; 32];
getrandom::fill(&mut seed).map_err(|err| IdentityError::Random(err.to_string()))?;
write_secret(path, &seed)?;
Ok(Loaded::Created(Identity::from_seed(&seed)))
}
Err(err) => Err(IdentityError::Read(path.to_path_buf(), err)),
}
}
fn write_secret(path: &Path, seed: &[u8; 32]) -> Result<(), IdentityError> {
let write_err = |err| IdentityError::Write(path.to_path_buf(), err);
let parent = match path.parent() {
Some(parent) if !parent.as_os_str().is_empty() => parent.to_path_buf(),
_ => PathBuf::from("."),
};
fs::create_dir_all(&parent).map_err(write_err)?;
let file_name = path
.file_name()
.map(|name| name.to_string_lossy().into_owned())
.unwrap_or_else(|| "secret".to_string());
let staging = parent.join(format!(".{file_name}.{}.tmp", std::process::id()));
let mut file = fs::OpenOptions::new()
.write(true)
.create_new(true)
.mode(0o600)
.open(&staging)
.map_err(write_err)?;
let body = format!("{}\n", hex::encode(seed));
let written = file
.write_all(body.as_bytes())
.and_then(|()| file.sync_all());
if let Err(err) = written {
let _ = fs::remove_file(&staging);
return Err(write_err(err));
}
fs::rename(&staging, path).map_err(|err| {
let _ = fs::remove_file(&staging);
write_err(err)
})
}
#[cfg(test)]
mod tests {
use super::*;
const SPEC_PUBLIC_KEY_HEX: &str =
"3b6a27bcceb6a42d62a3a8d02a6f0d73653215771de243a63ac048a18b59da29";
const SPEC_DID: &str = "did:key:z6MkiTBz1ymuepAQ4HEHYSF1H8quG5GLVVQR3djdX3mDooWp";
const DUMBPIPE_SECRET_HEX: &str =
"f8555129991792f402f21f6a01b0c027744b0b02c4b5d1f0e1bf979f722991d8";
const DUMBPIPE_ENDPOINT_ID: &str =
"cb6686656591d6e18858e0262a30c92700543f3230d3d483a4976cbfcbfc693c";
fn spec_public() -> [u8; 32] {
let mut out = [0u8; 32];
hex::decode_to_slice(SPEC_PUBLIC_KEY_HEX, &mut out).unwrap();
out
}
#[test]
fn the_did_key_spec_vector_encodes_an_ed25519_public_key() {
assert_eq!(did_key(&spec_public()), SPEC_DID);
}
#[test]
fn the_did_key_spec_vector_decodes_back_to_its_public_key() {
assert_eq!(parse_did_key(SPEC_DID).unwrap(), spec_public());
}
#[test]
fn the_endpoint_id_of_a_dumbpipe_iroh_secret_is_the_seeds_public_key_in_hex() {
let seed = parse_secret_hex(DUMBPIPE_SECRET_HEX).unwrap();
let identity = Identity::from_seed(&seed);
assert_eq!(identity.endpoint_id_hex(), DUMBPIPE_ENDPOINT_ID);
assert_eq!(identity.secret.public().to_string(), DUMBPIPE_ENDPOINT_ID);
}
#[test]
fn the_did_and_the_endpoint_id_name_one_key() {
let seed = parse_secret_hex(DUMBPIPE_SECRET_HEX).unwrap();
let identity = Identity::from_seed(&seed);
let from_did = parse_did_key(&identity.did_key()).unwrap();
assert_eq!(endpoint_id_hex(&from_did), identity.endpoint_id_hex());
assert_eq!(
parse_endpoint_id_hex(DUMBPIPE_ENDPOINT_ID).unwrap(),
from_did
);
}
#[test]
fn a_did_key_of_another_key_type_is_refused() {
let secp256k1_did = "did:key:zQ3shokFTS3brHcDQrn82RUDfCZESWL1ZdCEJwekUDPQiYBme";
assert!(parse_did_key(secp256k1_did).is_err());
assert!(parse_did_key("did:web:example.com").is_err());
}
#[test]
fn a_secret_must_be_sixty_four_hex_chars_and_surrounding_whitespace_is_ignored() {
assert!(parse_secret_hex(&format!(" {DUMBPIPE_SECRET_HEX}\n")).is_ok());
assert!(parse_secret_hex(&DUMBPIPE_SECRET_HEX[..62]).is_err());
assert!(parse_secret_hex(&format!("{}zz", &DUMBPIPE_SECRET_HEX[..62])).is_err());
}
#[test]
fn an_absent_secret_file_is_created_0600_and_read_back_as_the_same_identity() {
use std::os::unix::fs::PermissionsExt;
let dir = std::env::temp_dir().join(format!(
"meshd-identity-test-{}-{}",
std::process::id(),
line!()
));
let _ = fs::remove_dir_all(&dir);
let path = dir.join("nested").join("secret");
let created = match load_or_create(&path).unwrap() {
Loaded::Created(identity) => identity,
Loaded::Existing(_) => panic!("a missing file was reported as existing"),
};
let mode = fs::metadata(&path).unwrap().permissions().mode() & 0o777;
assert_eq!(mode, 0o600);
let text = fs::read_to_string(&path).unwrap();
assert_eq!(text.trim().len(), SECRET_HEX_LEN);
let again = match load_or_create(&path).unwrap() {
Loaded::Existing(identity) => identity,
Loaded::Created(_) => panic!("an existing file was overwritten"),
};
assert_eq!(again.endpoint_id_hex(), created.endpoint_id_hex());
let _ = fs::remove_dir_all(&dir);
}
#[test]
fn a_corrupt_secret_file_is_an_error_and_is_left_alone() {
let dir = std::env::temp_dir().join(format!(
"meshd-identity-test-{}-{}",
std::process::id(),
line!()
));
let _ = fs::remove_dir_all(&dir);
fs::create_dir_all(&dir).unwrap();
let path = dir.join("secret");
fs::write(&path, "not a key\n").unwrap();
assert!(load_or_create(&path).is_err());
assert_eq!(fs::read_to_string(&path).unwrap(), "not a key\n");
let _ = fs::remove_dir_all(&dir);
}
}