2021-07-15 10:15:50 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2020-04-17 12:11:25 +00:00
|
|
|
use anyhow::{bail, format_err, Error};
|
2019-01-29 16:21:58 +00:00
|
|
|
use lazy_static::lazy_static;
|
2021-09-29 09:05:26 +00:00
|
|
|
use openssl::pkey::{PKey, Private, Public};
|
2021-07-15 10:15:50 +00:00
|
|
|
use openssl::rsa::Rsa;
|
2019-01-29 16:41:45 +00:00
|
|
|
use openssl::sha;
|
2019-01-29 16:21:58 +00:00
|
|
|
|
2021-10-08 09:19:37 +00:00
|
|
|
use proxmox_lang::try_block;
|
2022-04-14 12:03:46 +00:00
|
|
|
use proxmox_sys::fs::{file_get_contents, replace_file, CreateOptions};
|
2019-08-03 11:05:38 +00:00
|
|
|
|
2021-07-15 10:15:50 +00:00
|
|
|
use pbs_api_types::Userid;
|
2022-04-14 12:03:46 +00:00
|
|
|
use pbs_buildcfg::configdir;
|
2020-06-10 10:02:56 +00:00
|
|
|
|
2022-04-14 12:03:46 +00:00
|
|
|
fn compute_csrf_secret_digest(timestamp: i64, secret: &[u8], userid: &Userid) -> String {
|
2019-01-29 16:41:45 +00:00
|
|
|
let mut hasher = sha::Sha256::new();
|
2020-08-06 13:46:01 +00:00
|
|
|
let data = format!("{:08X}:{}:", timestamp, userid);
|
2019-01-29 16:50:03 +00:00
|
|
|
hasher.update(data.as_bytes());
|
2019-01-29 16:41:45 +00:00
|
|
|
hasher.update(secret);
|
|
|
|
|
2019-02-16 14:52:55 +00:00
|
|
|
base64::encode_config(&hasher.finish(), base64::STANDARD_NO_PAD)
|
|
|
|
}
|
|
|
|
|
2022-04-14 12:03:46 +00:00
|
|
|
pub fn assemble_csrf_prevention_token(secret: &[u8], userid: &Userid) -> String {
|
2021-10-08 09:19:37 +00:00
|
|
|
let epoch = proxmox_time::epoch_i64();
|
2019-02-16 14:52:55 +00:00
|
|
|
|
2020-08-06 13:46:01 +00:00
|
|
|
let digest = compute_csrf_secret_digest(epoch, secret, userid);
|
2019-01-29 16:41:45 +00:00
|
|
|
|
2019-01-29 16:50:03 +00:00
|
|
|
format!("{:08X}:{}", epoch, digest)
|
2019-01-29 16:41:45 +00:00
|
|
|
}
|
|
|
|
|
2019-02-16 14:52:55 +00:00
|
|
|
pub fn verify_csrf_prevention_token(
|
|
|
|
secret: &[u8],
|
2020-08-06 13:46:01 +00:00
|
|
|
userid: &Userid,
|
2019-02-16 14:52:55 +00:00
|
|
|
token: &str,
|
|
|
|
min_age: i64,
|
|
|
|
max_age: i64,
|
|
|
|
) -> Result<i64, Error> {
|
|
|
|
use std::collections::VecDeque;
|
|
|
|
|
|
|
|
let mut parts: VecDeque<&str> = token.split(':').collect();
|
|
|
|
|
|
|
|
try_block!({
|
|
|
|
if parts.len() != 2 {
|
|
|
|
bail!("format error - wrong number of parts.");
|
|
|
|
}
|
|
|
|
|
|
|
|
let timestamp = parts.pop_front().unwrap();
|
|
|
|
let sig = parts.pop_front().unwrap();
|
|
|
|
|
2022-04-14 12:03:46 +00:00
|
|
|
let ttime = i64::from_str_radix(timestamp, 16)
|
|
|
|
.map_err(|err| format_err!("timestamp format error - {}", err))?;
|
2019-02-16 14:52:55 +00:00
|
|
|
|
2020-08-06 13:46:01 +00:00
|
|
|
let digest = compute_csrf_secret_digest(ttime, secret, userid);
|
2019-02-16 14:52:55 +00:00
|
|
|
|
|
|
|
if digest != sig {
|
|
|
|
bail!("invalid signature.");
|
|
|
|
}
|
|
|
|
|
2021-10-08 09:19:37 +00:00
|
|
|
let now = proxmox_time::epoch_i64();
|
2019-02-16 14:52:55 +00:00
|
|
|
|
|
|
|
let age = now - ttime;
|
|
|
|
if age < min_age {
|
|
|
|
bail!("timestamp newer than expected.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if age > max_age {
|
|
|
|
bail!("timestamp too old.");
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(age)
|
2022-04-14 12:03:46 +00:00
|
|
|
})
|
|
|
|
.map_err(|err| format_err!("invalid csrf token - {}", err))
|
2019-02-16 14:52:55 +00:00
|
|
|
}
|
|
|
|
|
2019-01-29 15:55:49 +00:00
|
|
|
pub fn generate_csrf_key() -> Result<(), Error> {
|
2019-02-04 14:13:03 +00:00
|
|
|
let path = PathBuf::from(configdir!("/csrf.key"));
|
2019-01-29 15:55:49 +00:00
|
|
|
|
2022-04-14 12:03:46 +00:00
|
|
|
if path.exists() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
2019-01-29 15:55:49 +00:00
|
|
|
|
|
|
|
let rsa = Rsa::generate(2048).unwrap();
|
|
|
|
|
|
|
|
let pem = rsa.private_key_to_pem()?;
|
|
|
|
|
|
|
|
use nix::sys::stat::Mode;
|
|
|
|
|
2021-09-02 10:47:11 +00:00
|
|
|
let backup_user = pbs_config::backup_user()?;
|
2019-02-16 08:29:04 +00:00
|
|
|
|
2019-12-18 10:05:30 +00:00
|
|
|
replace_file(
|
|
|
|
&path,
|
|
|
|
&pem,
|
|
|
|
CreateOptions::new()
|
|
|
|
.perm(Mode::from_bits_truncate(0o0640))
|
|
|
|
.owner(nix::unistd::ROOT)
|
2019-12-19 09:20:13 +00:00
|
|
|
.group(backup_user.gid),
|
2021-10-20 12:56:15 +00:00
|
|
|
true,
|
2019-12-18 10:05:30 +00:00
|
|
|
)?;
|
2019-01-29 15:55:49 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn generate_auth_key() -> Result<(), Error> {
|
2019-02-04 14:13:03 +00:00
|
|
|
let priv_path = PathBuf::from(configdir!("/authkey.key"));
|
2019-01-29 15:55:49 +00:00
|
|
|
|
|
|
|
let mut public_path = priv_path.clone();
|
|
|
|
public_path.set_extension("pub");
|
|
|
|
|
2022-04-14 12:03:46 +00:00
|
|
|
if priv_path.exists() && public_path.exists() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
2019-01-29 15:55:49 +00:00
|
|
|
|
|
|
|
let rsa = Rsa::generate(4096).unwrap();
|
|
|
|
|
|
|
|
let priv_pem = rsa.private_key_to_pem()?;
|
|
|
|
|
|
|
|
use nix::sys::stat::Mode;
|
|
|
|
|
2019-12-18 10:05:30 +00:00
|
|
|
replace_file(
|
2021-10-20 12:56:15 +00:00
|
|
|
&priv_path,
|
|
|
|
&priv_pem,
|
|
|
|
CreateOptions::new().perm(Mode::from_bits_truncate(0o0600)),
|
|
|
|
true,
|
|
|
|
)?;
|
2019-01-29 15:55:49 +00:00
|
|
|
|
|
|
|
let public_pem = rsa.public_key_to_pem()?;
|
|
|
|
|
2021-09-02 10:47:11 +00:00
|
|
|
let backup_user = pbs_config::backup_user()?;
|
2019-12-17 08:53:21 +00:00
|
|
|
|
2019-12-18 10:05:30 +00:00
|
|
|
replace_file(
|
|
|
|
&public_path,
|
|
|
|
&public_pem,
|
|
|
|
CreateOptions::new()
|
|
|
|
.perm(Mode::from_bits_truncate(0o0640))
|
|
|
|
.owner(nix::unistd::ROOT)
|
2019-12-19 09:20:13 +00:00
|
|
|
.group(backup_user.gid),
|
2021-10-20 12:56:15 +00:00
|
|
|
true,
|
2019-12-18 10:05:30 +00:00
|
|
|
)?;
|
2019-01-29 15:55:49 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2019-01-29 16:21:58 +00:00
|
|
|
|
|
|
|
pub fn csrf_secret() -> &'static [u8] {
|
|
|
|
lazy_static! {
|
2022-04-14 12:03:46 +00:00
|
|
|
static ref SECRET: Vec<u8> = file_get_contents(configdir!("/csrf.key")).unwrap();
|
2019-01-29 16:21:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
&SECRET
|
|
|
|
}
|
|
|
|
|
|
|
|
fn load_public_auth_key() -> Result<PKey<Public>, Error> {
|
2019-08-03 11:05:38 +00:00
|
|
|
let pem = file_get_contents(configdir!("/authkey.pub"))?;
|
2019-01-29 16:21:58 +00:00
|
|
|
let rsa = Rsa::public_key_from_pem(&pem)?;
|
|
|
|
let key = PKey::from_rsa(rsa)?;
|
|
|
|
|
|
|
|
Ok(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn public_auth_key() -> &'static PKey<Public> {
|
|
|
|
lazy_static! {
|
|
|
|
static ref KEY: PKey<Public> = load_public_auth_key().unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
&KEY
|
|
|
|
}
|
2021-09-29 09:05:26 +00:00
|
|
|
|
|
|
|
fn load_private_auth_key() -> Result<PKey<Private>, Error> {
|
|
|
|
let pem = file_get_contents(configdir!("/authkey.key"))?;
|
|
|
|
let rsa = Rsa::private_key_from_pem(&pem)?;
|
|
|
|
let key = PKey::from_rsa(rsa)?;
|
|
|
|
|
|
|
|
Ok(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn private_auth_key() -> &'static PKey<Private> {
|
|
|
|
lazy_static! {
|
|
|
|
static ref KEY: PKey<Private> = load_private_auth_key().unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
&KEY
|
|
|
|
}
|