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
use argonautica::{self, Hasher};
use crev_common::serde::{as_base64, from_base64};
use miscreant;
use rand::{self, Rng};
use serde_yaml;
use std::{
    self, fmt,
    io::{Read, Write},
    path::Path,
};
use crate::Result;
use crev_data::id::{OwnId, PubId};
const CURRENT_LOCKED_ID_SERIALIZATION_VERSION: i64 = -1;
#[derive(Serialize, Deserialize, Debug)]
pub struct PassConfig {
    version: u32,
    variant: String,
    iterations: u32,
    #[serde(rename = "memory-size")]
    memory_size: u32,
    #[serde(serialize_with = "as_base64", deserialize_with = "from_base64")]
    salt: Vec<u8>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct LockedId {
    version: i64,
    #[serde(flatten)]
    pub url: crev_data::Url,
    #[serde(serialize_with = "as_base64", deserialize_with = "from_base64")]
    #[serde(rename = "public-key")]
    pub public_key: Vec<u8>,
    #[serde(serialize_with = "as_base64", deserialize_with = "from_base64")]
    #[serde(rename = "sealed-secret-key")]
    sealed_secret_key: Vec<u8>,
    #[serde(serialize_with = "as_base64", deserialize_with = "from_base64")]
    #[serde(rename = "seal-nonce")]
    seal_nonce: Vec<u8>,
    pass: PassConfig,
}
impl fmt::Display for LockedId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        
        f.write_str(&serde_yaml::to_string(self).map_err(|_| fmt::Error)?)
    }
}
impl LockedId {
    pub fn from_own_id(own_id: &OwnId, passphrase: &str) -> Result<LockedId> {
        use miscreant::aead::Algorithm;
        let mut hasher = Hasher::default();
        hasher
            .configure_memory_size(4096)
            .configure_hash_len(64)
            .opt_out_of_secret_key(true);
        let pwhash = hasher.with_password(passphrase).hash_raw()?;
        let mut siv = miscreant::aead::Aes256Siv::new(pwhash.raw_hash_bytes());
        let seal_nonce: Vec<u8> = rand::thread_rng()
            .sample_iter(&rand::distributions::Standard)
            .take(32)
            .collect();
        let hasher_config = hasher.config();
        assert_eq!(hasher_config.version(), argonautica::config::Version::_0x13);
        Ok(LockedId {
            version: CURRENT_LOCKED_ID_SERIALIZATION_VERSION,
            public_key: own_id.keypair.public.to_bytes().to_vec(),
            sealed_secret_key: siv.seal(&seal_nonce, &[], own_id.keypair.secret.as_bytes()),
            seal_nonce,
            url: own_id.id.url.clone(),
            pass: PassConfig {
                salt: pwhash.raw_salt_bytes().to_vec(),
                iterations: hasher_config.iterations(),
                memory_size: hasher_config.memory_size(),
                version: 0x13,
                variant: hasher_config.variant().as_str().to_string(),
            },
        })
    }
    pub fn to_pubid(&self) -> PubId {
        PubId::new_from_pubkey(self.public_key.to_owned(), self.url.clone())
    }
    pub fn pub_key_as_base64(&self) -> String {
        crev_common::base64_encode(&self.public_key)
    }
    pub fn save_to(&self, path: &Path) -> Result<()> {
        let mut file = std::fs::OpenOptions::new()
            .create(true)
            .append(true)
            .open(path)?;
        write!(file, "{}", self)?;
        Ok(())
    }
    pub fn read_from_yaml_file(path: &Path) -> Result<Self> {
        let mut file = std::fs::File::open(path)?;
        let mut content = String::new();
        file.read_to_string(&mut content)?;
        Ok(serde_yaml::from_str::<LockedId>(&content)?)
    }
    pub fn to_unlocked(&self, passphrase: &str) -> Result<OwnId> {
        let LockedId {
            ref version,
            ref url,
            ref public_key,
            ref sealed_secret_key,
            ref seal_nonce,
            ref pass,
        } = self;
        {
            if *version > CURRENT_LOCKED_ID_SERIALIZATION_VERSION {
                bail!("Unsupported version: {}", *version);
            }
            use miscreant::aead::Algorithm;
            let mut hasher = Hasher::default();
            hasher
                .configure_memory_size(pass.memory_size)
                .configure_version(argonautica::config::Version::from_u32(pass.version)?)
                .configure_iterations(pass.iterations)
                .configure_variant(std::str::FromStr::from_str(&pass.variant)?)
                .with_salt(&pass.salt)
                .configure_hash_len(64)
                .opt_out_of_secret_key(true);
            let pwhash = hasher.with_password(passphrase).hash_raw()?;
            let mut siv = miscreant::aead::Aes256Siv::new(pwhash.raw_hash_bytes());
            let sec_key = siv.open(&seal_nonce, &[], &sealed_secret_key)?;
            let res = OwnId::new(url.to_owned(), sec_key)?;
            if public_key != &res.keypair.public.to_bytes() {
                bail!("PubKey mismatch");
            }
            Ok(res)
        }
    }
}