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
#![deny(missing_docs)]
#![deny(warnings)]
// it's not possible to specify required
// bounds with the `async fn` syntax.
#![allow(clippy::manual_async_fn)]

//! secret lair private keystore
//!
//! # Usage
//!
//! ## Communications  Protocol
//!
//! See [docs/protocol.md](./docs/protocol.md)

include!(concat!(env!("OUT_DIR"), "/ver.rs"));

use crate::store::EntryStoreSender;
use lair_keystore_api::*;
use std::io::{BufRead, BufReader, Error, ErrorKind};
use std::sync::Arc;

pub mod internal;

pub mod store;

pub mod ipc;

/// Main loop of lair executable.
pub async fn execute_lair() -> LairResult<()> {
    let mut config = Config::builder();

    if let Some(lair_dir) = std::env::var_os("LAIR_DIR") {
        config = config.set_root_path(lair_dir);
    }

    let config = config.build();

    println!("#lair-keystore-dir:{:?}#", config.get_root_path());

    let internal::pid_check::PidCheckResult {} =
        internal::pid_check::pid_check(&config)?;

    ipc::spawn_bind_server_ipc(config).await?;

    Ok(())
}

/// Gen loop of lair executable with file path.
pub async fn execute_load_ed25519_keypair_from_file(
    load_ed25519_keypair_from_file: std::path::PathBuf,
    passphrase: sodoken::BufRead,
) -> LairResult<()> {
    use std::fs::File;
    let file = File::open(load_ed25519_keypair_from_file)?;
    let encrypted_blob = BufReader::new(file)
        .lines()
        .map(|line| {
            line.and_then(|v| {
                v.parse().map_err(|e| Error::new(ErrorKind::InvalidData, e))
            })
        })
        .collect::<Result<Vec<u8>, Error>>()?;
    execute_load_ed25519_keypair(encrypted_blob.to_vec(), passphrase).await
}

/// Gen loop of lair executable with encrypted blob.
pub async fn execute_load_ed25519_keypair(
    load_ed25519_keypair: Vec<u8>,
    passphrase: sodoken::BufRead,
) -> LairResult<()> {
    let mut config = Config::builder();

    if let Some(lair_dir) = std::env::var_os("LAIR_DIR") {
        config = config.set_root_path(lair_dir);
    }

    let config = config.build();

    println!("#lair-keystore-dir:{:?}#", config.get_root_path());

    let internal::pid_check::PidCheckResult {} =
        internal::pid_check::pid_check(&config)?;

    let db_key = read_or_generate_db_key(config.clone(), passphrase).await?;

    let store_actor = store::spawn_entry_store_actor(config, db_key).await?;

    let keypair = entry::EntrySignEd25519 {
        priv_key:
            lair_keystore_api::internal::sign_ed25519::SignEd25519PrivKey::from(
                load_ed25519_keypair[64..].to_vec(),
            ),
        pub_key:
            lair_keystore_api::internal::sign_ed25519::SignEd25519PubKey::from(
                load_ed25519_keypair[32..64].to_vec(),
            ),
    };

    store_actor
        .add_initial_sign_ed25519_keypair(keypair)
        .await?;
    Ok(())
}

use sodoken::argon2id::SALTBYTES;
use sodoken::secretstream_xchacha20poly1305::*;

#[derive(Clone)]
pub(crate) struct DbKeyEnc {
    pub salt: sodoken::BufReadSized<SALTBYTES>,
    pub header: sodoken::BufReadSized<SECRETSTREAM_HEADERBYTES>,
    pub cipher: sodoken::BufRead,
}

impl DbKeyEnc {
    pub async fn read(config: &Config) -> LairResult<Self> {
        let db_key_path = config.get_db_key_path();

        let content = tokio::fs::read(db_key_path)
            .await
            .map_err(LairError::other)?;

        let salt: sodoken::BufReadSized<SALTBYTES> =
            (&content[0..SALTBYTES]).into();

        let header: sodoken::BufReadSized<SECRETSTREAM_HEADERBYTES> =
            (&content[SALTBYTES..SALTBYTES + SECRETSTREAM_HEADERBYTES]).into();

        let cipher = sodoken::BufRead::new_no_lock(
            &content[SALTBYTES + SECRETSTREAM_HEADERBYTES..],
        );

        Ok(Self {
            salt,
            header,
            cipher,
        })
    }

    /// write the salt and cipher to the db key file
    pub async fn write(&self, config: &Config) -> LairResult<()> {
        let db_key_path = config.get_db_key_path();

        let mut data = Vec::with_capacity(
            self.salt.len() + self.header.len() + self.cipher.len(),
        );

        data.extend_from_slice(&*self.salt.read_lock());
        data.extend_from_slice(&*self.header.read_lock());
        data.extend_from_slice(&*self.cipher.read_lock());

        tokio::fs::write(db_key_path, &data)
            .await
            .map_err(LairError::other)?;

        Ok(())
    }

    async fn calc_pre_key(
        passphrase: sodoken::BufRead,
        salt: sodoken::BufReadSized<SALTBYTES>,
    ) -> LairResult<sodoken::BufReadSized<32>> {
        use once_cell::sync::Lazy;

        // argon is designed to be both cpu and memory hard
        // from a cpu perspective it doesn't make sense to run more
        // argons than the number of cpus we have.
        // from a memory perspective, if we run too many at a time,
        // we end up with horrible slow things like memory virtualization.
        // MODERATE limits with min(num_cpus::get(), 4) seems to work ok.
        static ARGON_LIMIT: Lazy<Arc<tokio::sync::Semaphore>> =
            Lazy::new(|| {
                Arc::new(tokio::sync::Semaphore::new(std::cmp::min(
                    num_cpus::get(),
                    4,
                )))
            });
        let _permit = ARGON_LIMIT
            .clone()
            .acquire_owned()
            .await
            .expect("this semaphore is never closed");

        let pre_key = sodoken::BufWriteSized::new_mem_locked()?;
        sodoken::argon2id::hash(
            pre_key.clone(),
            passphrase,
            salt,
            sodoken::argon2id::OPSLIMIT_MODERATE,
            sodoken::argon2id::MEMLIMIT_MODERATE,
        )
        .await
        .map_err(|e| LairError::from(format!("argon fail: {:?}", e)))?;
        Ok(pre_key.to_read_sized())
    }

    pub async fn calc_db_key(
        &self,
        passphrase: sodoken::BufRead,
    ) -> LairResult<sodoken::BufReadSized<32>> {
        // calculate the pre_key given salt and passphrase
        let pre_key = Self::calc_pre_key(passphrase, self.salt.clone()).await?;

        // decrypt the db key given our calculated pre_key
        let mut dec = SecretStreamDecrypt::new(pre_key, self.header.clone())
            .map_err(|e| {
                LairError::from(format!("decrypt new fail: {:?}", e))
            })?;

        let db_key = sodoken::BufWriteSized::new_mem_locked()?;
        dec.pull(
            self.cipher.clone(),
            <Option<sodoken::BufRead>>::None,
            db_key.clone(),
        )
        .await
        .map_err(|e| LairError::from(format!("decrypt pull fail: {:?}", e)))?;

        Ok(db_key.to_read_sized())
    }

    pub async fn generate(
        passphrase: sodoken::BufRead,
    ) -> LairResult<(Self, sodoken::BufReadSized<32>)> {
        // generate a new random salt
        let salt = sodoken::BufWriteSized::new_no_lock();
        sodoken::random::randombytes_buf(salt.clone()).await?;

        // calculate the pre_key given salt and passphrase
        let pre_key =
            Self::calc_pre_key(passphrase, salt.to_read_sized()).await?;

        // generate a new random db_key
        let db_key = sodoken::BufWriteSized::new_mem_locked()?;
        sodoken::random::randombytes_buf(db_key.clone()).await?;

        let header = sodoken::BufWriteSized::new_no_lock();
        let mut enc = SecretStreamEncrypt::new(pre_key, header.clone())?;

        let cipher = sodoken::BufWrite::new_unbound_no_lock();
        enc.push_final(
            db_key.clone(),
            <Option<sodoken::BufRead>>::None,
            cipher.clone(),
        )
        .await?;

        Ok((
            Self {
                salt: salt.to_read_sized(),
                header: header.to_read_sized(),
                cipher: cipher.to_read(),
            },
            db_key.to_read_sized(),
        ))
    }
}

pub(crate) async fn read_or_generate_db_key(
    config: Arc<Config>,
    passphrase: sodoken::BufRead,
) -> LairResult<sodoken::BufReadSized<32>> {
    match DbKeyEnc::read(&config).await {
        Ok(dbk_enc) => dbk_enc.calc_db_key(passphrase).await,
        Err(_) => {
            let (dbk_enc, db_key) = DbKeyEnc::generate(passphrase).await?;
            dbk_enc.write(&config).await?;
            Ok(db_key)
        }
    }
}