| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
use std::fmt::Write as _; |
| 5 |
|
| 6 |
use crate::error::{AppError, Result}; |
| 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 |
pub fn install_default_crypto_provider() { |
| 40 |
let _ = rustls::crypto::ring::default_provider().install_default(); |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
const TOTP_ENC_PREFIX: &str = "enc:v1:"; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
fn totp_encryption_key(signing_secret: &str) -> [u8; 32] { |
| 54 |
use hmac::{Hmac, KeyInit, Mac}; |
| 55 |
use sha2::Sha256; |
| 56 |
|
| 57 |
let mut mac = Hmac::<Sha256>::new_from_slice(signing_secret.as_bytes()) |
| 58 |
.expect("HMAC-SHA256 accepts any key length"); |
| 59 |
mac.update(b"mnw-totp-secret-encryption-v1"); |
| 60 |
mac.finalize().into_bytes().into() |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
pub fn encrypt_totp_secret(plaintext: &str, signing_secret: &str) -> String { |
| 71 |
use base64::Engine; |
| 72 |
use chacha20poly1305::{ChaCha20Poly1305, KeyInit, Nonce, aead::Aead}; |
| 73 |
|
| 74 |
let key = totp_encryption_key(signing_secret); |
| 75 |
let cipher = ChaCha20Poly1305::new((&key).into()); |
| 76 |
|
| 77 |
let mut nonce_bytes = [0u8; 12]; |
| 78 |
rand::Rng::fill_bytes(&mut rand::rng(), &mut nonce_bytes); |
| 79 |
let nonce = Nonce::from(nonce_bytes); |
| 80 |
|
| 81 |
let ciphertext = cipher |
| 82 |
.encrypt(&nonce, plaintext.as_bytes()) |
| 83 |
|
| 84 |
|
| 85 |
.expect("ChaCha20-Poly1305 encryption is infallible here"); |
| 86 |
|
| 87 |
let mut payload = Vec::with_capacity(nonce_bytes.len() + ciphertext.len()); |
| 88 |
payload.extend_from_slice(&nonce_bytes); |
| 89 |
payload.extend_from_slice(&ciphertext); |
| 90 |
|
| 91 |
format!( |
| 92 |
"{TOTP_ENC_PREFIX}{}", |
| 93 |
base64::engine::general_purpose::STANDARD.encode(payload) |
| 94 |
) |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
pub fn decrypt_totp_secret(stored: &str, signing_secret: &str) -> Result<String> { |
| 105 |
use base64::Engine; |
| 106 |
use chacha20poly1305::{ChaCha20Poly1305, KeyInit, Nonce, aead::Aead}; |
| 107 |
|
| 108 |
let Some(b64) = stored.strip_prefix(TOTP_ENC_PREFIX) else { |
| 109 |
return Err(AppError::Internal(anyhow::anyhow!( |
| 110 |
"totp secret is not encrypted (missing {TOTP_ENC_PREFIX} prefix)" |
| 111 |
))); |
| 112 |
}; |
| 113 |
|
| 114 |
let payload = base64::engine::general_purpose::STANDARD |
| 115 |
.decode(b64) |
| 116 |
.map_err(|e| AppError::Internal(anyhow::anyhow!("totp secret base64 decode: {e}")))?; |
| 117 |
if payload.len() < 12 { |
| 118 |
return Err(AppError::Internal(anyhow::anyhow!( |
| 119 |
"totp secret ciphertext too short" |
| 120 |
))); |
| 121 |
} |
| 122 |
let (nonce_bytes, ciphertext) = payload.split_at(12); |
| 123 |
let nonce_arr: [u8; 12] = nonce_bytes |
| 124 |
.try_into() |
| 125 |
.expect("split_at(12) on a >=12-byte payload yields exactly 12 bytes"); |
| 126 |
|
| 127 |
let key = totp_encryption_key(signing_secret); |
| 128 |
let cipher = ChaCha20Poly1305::new((&key).into()); |
| 129 |
let plaintext = cipher |
| 130 |
.decrypt(&Nonce::from(nonce_arr), ciphertext) |
| 131 |
.map_err(|_| AppError::Internal(anyhow::anyhow!("totp secret decryption failed")))?; |
| 132 |
|
| 133 |
String::from_utf8(plaintext) |
| 134 |
.map_err(|e| AppError::Internal(anyhow::anyhow!("totp secret utf8: {e}"))) |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
pub fn constant_time_compare(a: &str, b: &str) -> bool { |
| 147 |
use subtle::ConstantTimeEq; |
| 148 |
let a = a.as_bytes(); |
| 149 |
let b = b.as_bytes(); |
| 150 |
if a.len() != b.len() { |
| 151 |
return false; |
| 152 |
} |
| 153 |
a.ct_eq(b).into() |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
pub fn generate_key_code() -> crate::db::KeyCode { |
| 165 |
use rand::RngExt; |
| 166 |
let mut rng = rand::rng(); |
| 167 |
let words: Vec<&str> = (0..6) |
| 168 |
.map(|_| { |
| 169 |
let idx = rng.random_range(0..crate::wordlist::WORDLIST.len()); |
| 170 |
crate::wordlist::WORDLIST[idx] |
| 171 |
}) |
| 172 |
.collect(); |
| 173 |
crate::db::KeyCode::from_trusted(words.join("-")) |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
pub fn generate_git_token() -> (String, String) { |
| 181 |
let mut bytes = [0u8; 32]; |
| 182 |
rand::Rng::fill_bytes(&mut rand::rng(), &mut bytes); |
| 183 |
let body: String = bytes.iter().fold(String::new(), |mut out, b| { |
| 184 |
let _ = write!(out, "{b:02x}"); |
| 185 |
out |
| 186 |
}); |
| 187 |
let plaintext = format!("mnw_{body}"); |
| 188 |
let hash = git_token_hash(&plaintext); |
| 189 |
(plaintext, hash) |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
pub fn sha256_hex(input: &str) -> String { |
| 196 |
use sha2::{Digest, Sha256}; |
| 197 |
Sha256::digest(input.as_bytes()) |
| 198 |
.iter() |
| 199 |
.fold(String::new(), |mut out, b| { |
| 200 |
let _ = write!(out, "{b:02x}"); |
| 201 |
out |
| 202 |
}) |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
pub fn git_token_hash(token: &str) -> String { |
| 209 |
sha256_hex(token) |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
pub fn invite_code_hash(code: &str) -> String { |
| 216 |
sha256_hex(code) |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
const INTERNAL_ACTOR_DOMAIN: &str = "internal-actor:v1"; |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
pub fn mint_internal_actor_token( |
| 232 |
user_id: crate::db::UserId, |
| 233 |
expiry_unix: i64, |
| 234 |
signing_secret: &str, |
| 235 |
) -> String { |
| 236 |
use hmac::{Hmac, KeyInit, Mac}; |
| 237 |
use sha2::Sha256; |
| 238 |
let message = format!("{INTERNAL_ACTOR_DOMAIN}:{user_id}:{expiry_unix}"); |
| 239 |
let mut mac = Hmac::<Sha256>::new_from_slice(signing_secret.as_bytes()) |
| 240 |
.expect("HMAC-SHA256 accepts any key length"); |
| 241 |
mac.update(message.as_bytes()); |
| 242 |
format!( |
| 243 |
"{user_id}.{expiry_unix}.{}", |
| 244 |
hex::encode(mac.finalize().into_bytes()) |
| 245 |
) |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
pub fn verify_internal_actor_token( |
| 251 |
token: &str, |
| 252 |
signing_secret: &str, |
| 253 |
now_unix: i64, |
| 254 |
) -> Option<crate::db::UserId> { |
| 255 |
use hmac::{Hmac, KeyInit, Mac}; |
| 256 |
use sha2::Sha256; |
| 257 |
|
| 258 |
let (user_part, rest) = token.split_once('.')?; |
| 259 |
let (expiry_part, sig_hex) = rest.split_once('.')?; |
| 260 |
let user_id = crate::db::UserId::from_uuid(user_part.parse().ok()?); |
| 261 |
let expiry: i64 = expiry_part.parse().ok()?; |
| 262 |
if expiry <= now_unix { |
| 263 |
return None; |
| 264 |
} |
| 265 |
let sig = hex::decode(sig_hex).ok()?; |
| 266 |
let message = format!("{INTERNAL_ACTOR_DOMAIN}:{user_id}:{expiry}"); |
| 267 |
let mut mac = Hmac::<Sha256>::new_from_slice(signing_secret.as_bytes()) |
| 268 |
.expect("HMAC-SHA256 accepts any key length"); |
| 269 |
mac.update(message.as_bytes()); |
| 270 |
mac.verify_slice(&sig).ok()?; |
| 271 |
Some(user_id) |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
fn feed_signature(user_id: crate::db::UserId, version: i32, secret: &str) -> String { |
| 276 |
use hmac::{Hmac, KeyInit, Mac}; |
| 277 |
use sha2::Sha256; |
| 278 |
|
| 279 |
let message = format!("feed:{user_id}:{version}"); |
| 280 |
let mut mac = Hmac::<Sha256>::new_from_slice(secret.as_bytes()) |
| 281 |
.expect("HMAC-SHA256 accepts any key length"); |
| 282 |
mac.update(message.as_bytes()); |
| 283 |
hex::encode(mac.finalize().into_bytes()) |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
pub fn generate_feed_url( |
| 294 |
host_url: &str, |
| 295 |
user_id: crate::db::UserId, |
| 296 |
version: i32, |
| 297 |
secret: &str, |
| 298 |
) -> String { |
| 299 |
let sig = feed_signature(user_id, version, secret); |
| 300 |
format!("{host_url}/feed/{user_id}?v={version}&sig={sig}") |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
pub fn verify_feed_signature( |
| 308 |
user_id: crate::db::UserId, |
| 309 |
version: i32, |
| 310 |
signature: &str, |
| 311 |
secret: &str, |
| 312 |
) -> bool { |
| 313 |
let expected = feed_signature(user_id, version, secret); |
| 314 |
constant_time_compare(&expected, signature) |
| 315 |
} |
| 316 |
|
| 317 |
#[cfg(test)] |
| 318 |
mod tests { |
| 319 |
use super::*; |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
#[test] |
| 324 |
fn internal_actor_token_round_trips() { |
| 325 |
let uid = crate::db::UserId::new(); |
| 326 |
let secret = "a-stable-signing-secret-at-least-32c"; |
| 327 |
let tok = mint_internal_actor_token(uid, 10_000_000_000, secret); |
| 328 |
assert_eq!(verify_internal_actor_token(&tok, secret, 1_000), Some(uid)); |
| 329 |
} |
| 330 |
|
| 331 |
#[test] |
| 332 |
fn internal_actor_token_rejects_expired() { |
| 333 |
let uid = crate::db::UserId::new(); |
| 334 |
let secret = "a-stable-signing-secret-at-least-32c"; |
| 335 |
let tok = mint_internal_actor_token(uid, 1_000, secret); |
| 336 |
assert_eq!(verify_internal_actor_token(&tok, secret, 2_000), None); |
| 337 |
} |
| 338 |
|
| 339 |
#[test] |
| 340 |
fn internal_actor_token_rejects_wrong_secret() { |
| 341 |
let uid = crate::db::UserId::new(); |
| 342 |
let tok = |
| 343 |
mint_internal_actor_token(uid, 10_000_000_000, "secret-one-that-is-long-enough!!"); |
| 344 |
assert_eq!( |
| 345 |
verify_internal_actor_token(&tok, "secret-two-that-is-long-enough!!", 1_000), |
| 346 |
None |
| 347 |
); |
| 348 |
} |
| 349 |
|
| 350 |
#[test] |
| 351 |
fn internal_actor_token_rejects_tampered_user() { |
| 352 |
let uid = crate::db::UserId::new(); |
| 353 |
let other = crate::db::UserId::new(); |
| 354 |
let secret = "a-stable-signing-secret-at-least-32c"; |
| 355 |
let tok = mint_internal_actor_token(uid, 10_000_000_000, secret); |
| 356 |
|
| 357 |
let rest = tok.split_once('.').unwrap().1; |
| 358 |
let forged = format!("{other}.{rest}"); |
| 359 |
assert_eq!(verify_internal_actor_token(&forged, secret, 1_000), None); |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
#[test] |
| 371 |
fn totp_secret_matches_independent_known_answer() { |
| 372 |
const STORED: &str = "enc:v1:EBESExQVFhcYGRobIDLGTuOMhI+7NitFN57N5vREAFNm152FpA/N06cmy8o="; |
| 373 |
let signing_secret = "test-signing-secret-for-known-answer"; |
| 374 |
assert_eq!( |
| 375 |
decrypt_totp_secret(STORED, signing_secret).unwrap(), |
| 376 |
"JBSWY3DPEHPK3PXP" |
| 377 |
); |
| 378 |
|
| 379 |
|
| 380 |
assert!(decrypt_totp_secret(STORED, "some-other-signing-secret").is_err()); |
| 381 |
} |
| 382 |
|
| 383 |
#[test] |
| 384 |
fn totp_secret_round_trips() { |
| 385 |
let secret = "JBSWY3DPEHPK3PXP"; |
| 386 |
let key = "a-stable-signing-secret-at-least-32c"; |
| 387 |
let enc = encrypt_totp_secret(secret, key); |
| 388 |
assert!( |
| 389 |
enc.starts_with("enc:v1:"), |
| 390 |
"ciphertext must be version-tagged" |
| 391 |
); |
| 392 |
assert_ne!(enc, secret, "ciphertext must not be the plaintext"); |
| 393 |
assert_eq!(decrypt_totp_secret(&enc, key).unwrap(), secret); |
| 394 |
} |
| 395 |
|
| 396 |
#[test] |
| 397 |
fn totp_secret_nonce_is_random() { |
| 398 |
let secret = "JBSWY3DPEHPK3PXP"; |
| 399 |
let key = "a-stable-signing-secret-at-least-32c"; |
| 400 |
|
| 401 |
assert_ne!( |
| 402 |
encrypt_totp_secret(secret, key), |
| 403 |
encrypt_totp_secret(secret, key) |
| 404 |
); |
| 405 |
} |
| 406 |
|
| 407 |
#[test] |
| 408 |
fn totp_secret_wrong_key_fails_to_decrypt() { |
| 409 |
let secret = "JBSWY3DPEHPK3PXP"; |
| 410 |
let enc = encrypt_totp_secret(secret, "a-stable-signing-secret-at-least-32c"); |
| 411 |
assert!(decrypt_totp_secret(&enc, "a-different-signing-secret-32-chars!").is_err()); |
| 412 |
} |
| 413 |
|
| 414 |
#[test] |
| 415 |
fn totp_secret_tampered_ciphertext_fails() { |
| 416 |
let key = "a-stable-signing-secret-at-least-32c"; |
| 417 |
let enc = encrypt_totp_secret("JBSWY3DPEHPK3PXP", key); |
| 418 |
|
| 419 |
let mut bytes: Vec<char> = enc.chars().collect(); |
| 420 |
let last = bytes.len() - 1; |
| 421 |
bytes[last] = if bytes[last] == 'A' { 'B' } else { 'A' }; |
| 422 |
let tampered: String = bytes.into_iter().collect(); |
| 423 |
assert!(decrypt_totp_secret(&tampered, key).is_err()); |
| 424 |
} |
| 425 |
|
| 426 |
#[test] |
| 427 |
fn totp_secret_unprefixed_plaintext_is_rejected() { |
| 428 |
|
| 429 |
|
| 430 |
let key = "a-stable-signing-secret-at-least-32c"; |
| 431 |
assert!(decrypt_totp_secret("JBSWY3DPEHPK3PXP", key).is_err()); |
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
#[test] |
| 437 |
fn compare_equal_strings() { |
| 438 |
assert!(constant_time_compare("abc123", "abc123")); |
| 439 |
} |
| 440 |
|
| 441 |
#[test] |
| 442 |
fn compare_different_strings() { |
| 443 |
assert!(!constant_time_compare("abc123", "abc124")); |
| 444 |
} |
| 445 |
|
| 446 |
#[test] |
| 447 |
fn compare_different_lengths() { |
| 448 |
assert!(!constant_time_compare("short", "longer")); |
| 449 |
} |
| 450 |
|
| 451 |
#[test] |
| 452 |
fn compare_empty_strings() { |
| 453 |
assert!(constant_time_compare("", "")); |
| 454 |
} |
| 455 |
|
| 456 |
#[test] |
| 457 |
fn adversarial_timing_safety() { |
| 458 |
assert!(!constant_time_compare("a", "b")); |
| 459 |
assert!(!constant_time_compare("a", "aa")); |
| 460 |
assert!(!constant_time_compare("", "x")); |
| 461 |
assert!(constant_time_compare("same", "same")); |
| 462 |
} |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
#[test] |
| 467 |
fn key_code_format() { |
| 468 |
let code = generate_key_code(); |
| 469 |
let parts: Vec<&str> = code.split('-').collect(); |
| 470 |
assert_eq!(parts.len(), 6, "Key code should have 6 words"); |
| 471 |
for word in &parts { |
| 472 |
assert!( |
| 473 |
word.len() >= 3, |
| 474 |
"Each word should be at least 3 chars: {word}" |
| 475 |
); |
| 476 |
assert!( |
| 477 |
word.len() <= 6, |
| 478 |
"Each word should be at most 6 chars: {word}" |
| 479 |
); |
| 480 |
assert!( |
| 481 |
word.chars().all(|c| c.is_ascii_lowercase()), |
| 482 |
"Words should be lowercase: {word}" |
| 483 |
); |
| 484 |
} |
| 485 |
} |
| 486 |
|
| 487 |
#[test] |
| 488 |
fn key_code_uniqueness() { |
| 489 |
let codes: std::collections::HashSet<crate::db::KeyCode> = |
| 490 |
(0..100).map(|_| generate_key_code()).collect(); |
| 491 |
assert_eq!( |
| 492 |
codes.len(), |
| 493 |
100, |
| 494 |
"100 generated key codes should all be unique" |
| 495 |
); |
| 496 |
} |
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
fn sig_of(url: &str) -> &str { |
| 502 |
url.split("sig=").nth(1).unwrap() |
| 503 |
} |
| 504 |
|
| 505 |
#[test] |
| 506 |
fn feed_url_round_trip() { |
| 507 |
let user_id = crate::db::UserId::new(); |
| 508 |
let url = generate_feed_url("https://makenot.work", user_id, 0, "secret"); |
| 509 |
assert!(url.contains(&user_id.to_string())); |
| 510 |
assert!(url.contains("v=0")); |
| 511 |
assert!(url.contains("sig=")); |
| 512 |
assert!(verify_feed_signature(user_id, 0, sig_of(&url), "secret")); |
| 513 |
} |
| 514 |
|
| 515 |
#[test] |
| 516 |
fn feed_url_wrong_secret_rejected() { |
| 517 |
let user_id = crate::db::UserId::new(); |
| 518 |
let url = generate_feed_url("https://makenot.work", user_id, 0, "secret"); |
| 519 |
assert!(!verify_feed_signature( |
| 520 |
user_id, |
| 521 |
0, |
| 522 |
sig_of(&url), |
| 523 |
"wrong-secret" |
| 524 |
)); |
| 525 |
} |
| 526 |
|
| 527 |
#[test] |
| 528 |
fn feed_url_wrong_user_rejected() { |
| 529 |
let user_id = crate::db::UserId::new(); |
| 530 |
let other_id = crate::db::UserId::new(); |
| 531 |
let url = generate_feed_url("https://makenot.work", user_id, 0, "secret"); |
| 532 |
assert!(!verify_feed_signature(other_id, 0, sig_of(&url), "secret")); |
| 533 |
} |
| 534 |
|
| 535 |
#[test] |
| 536 |
fn feed_url_stale_version_rejected() { |
| 537 |
|
| 538 |
|
| 539 |
let user_id = crate::db::UserId::new(); |
| 540 |
let url = generate_feed_url("https://makenot.work", user_id, 0, "secret"); |
| 541 |
assert!(verify_feed_signature(user_id, 0, sig_of(&url), "secret")); |
| 542 |
assert!(!verify_feed_signature(user_id, 1, sig_of(&url), "secret")); |
| 543 |
} |
| 544 |
|
| 545 |
#[test] |
| 546 |
fn feed_signature_empty_string_rejected() { |
| 547 |
let user_id = crate::db::UserId::new(); |
| 548 |
assert!(!verify_feed_signature(user_id, 0, "", "secret")); |
| 549 |
} |
| 550 |
|
| 551 |
#[test] |
| 552 |
fn feed_signature_tampered_rejected() { |
| 553 |
let user_id = crate::db::UserId::new(); |
| 554 |
let url = generate_feed_url("https://makenot.work", user_id, 0, "secret"); |
| 555 |
let sig = sig_of(&url); |
| 556 |
let mut tampered = sig.to_string(); |
| 557 |
let first = tampered.remove(0); |
| 558 |
tampered.insert(0, if first == '0' { '1' } else { '0' }); |
| 559 |
assert!(!verify_feed_signature(user_id, 0, &tampered, "secret")); |
| 560 |
} |
| 561 |
} |
| 562 |
|