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
// This file is part of radicle-link
// <https://github.com/radicle-dev/radicle-link>
//
// Copyright (C) 2019-2020 The Radicle Team <dev@radicle.xyz>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 3 or
// later as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use std::{
    fmt::{self, Debug, Display},
    fs::File,
    io,
    marker::PhantomData,
    path::{Path, PathBuf},
};

use serde::{de::DeserializeOwned, Deserialize, Serialize};

use crate::{crypto::Crypto, Keypair, Keystore, SecretKeyExt};

/// [`Keystore`] implementation which stores the encrypted key in a file on the
/// local filesystem.
#[derive(Clone)]
pub struct FileStorage<C, PK, SK, M> {
    key_file_path: PathBuf,
    crypto: C,

    _marker: PhantomData<(PK, SK, M)>,
}

impl<C, PK, SK, M> FileStorage<C, PK, SK, M> {
    /// Construct a new [`FileStorage`] with the given [`Crypto`]
    /// implementation.
    ///
    /// The [`Path`] given by `key_file_path` must be an actual file path, not a
    /// directory.
    pub fn new(key_file_path: &Path, crypto: C) -> Self {
        Self {
            key_file_path: key_file_path.to_path_buf(),
            crypto,

            _marker: PhantomData,
        }
    }

    /// [`Path`] to the file where the encrypted key is stored.
    pub fn key_file_path(&self) -> &Path {
        self.key_file_path.as_path()
    }
}

#[derive(Serialize, Deserialize)]
struct Stored<PK, S, M> {
    public_key: PK,
    secret_key: S,
    metadata: M,
}

#[derive(Debug)]
pub enum Error<Crypto, Conversion> {
    KeyExists(PathBuf),
    NoSuchKey(PathBuf),
    Crypto(Crypto),
    Conversion(Conversion),
    Serde(serde_cbor::error::Error),
    Io(io::Error),
}

impl<Crypto, Conversion> std::error::Error for Error<Crypto, Conversion>
where
    Crypto: Display + Debug,
    Conversion: Display + Debug,
{
}

impl<Crypto, Conversion> Display for Error<Crypto, Conversion>
where
    Crypto: Display + Debug,
    Conversion: Display + Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::KeyExists(path) => {
                write!(
                    f,
                    "Key exists at file path {}, refusing to overwrite",
                    path.display()
                )
            },
            Self::NoSuchKey(path) => write!(f, "No key found at file path: {}", path.display()),
            Self::Conversion(e) => write!(f, "Error reconstructing sealed key: {}", e),
            Self::Crypto(e) => write!(f, "Error unsealing key: {}", e),
            Self::Serde(e) => write!(f, "{}", e),
            Self::Io(e) => write!(f, "{}", e),
        }
    }
}

impl<Crypto, Conversion> From<io::Error> for Error<Crypto, Conversion> {
    fn from(e: io::Error) -> Self {
        Self::Io(e)
    }
}

impl<Crypto, Conversion> From<serde_cbor::error::Error> for Error<Crypto, Conversion> {
    fn from(e: serde_cbor::error::Error) -> Self {
        Self::Serde(e)
    }
}

impl<C, PK, SK, M> Keystore for FileStorage<C, PK, SK, M>
where
    C: Crypto,
    C::Error: Display + Debug,
    C::SecretBox: Serialize + DeserializeOwned,

    SK: AsRef<[u8]> + SecretKeyExt<Metadata = M>,
    <SK as SecretKeyExt>::Error: Display + Debug,

    PK: Clone + From<SK> + Serialize + DeserializeOwned,
    M: Clone + Serialize + DeserializeOwned,
{
    type PublicKey = PK;
    type SecretKey = SK;
    type Metadata = M;
    type Error = Error<C::Error, <SK as SecretKeyExt>::Error>;

    fn put_key(&mut self, key: Self::SecretKey) -> Result<(), Self::Error> {
        if self.key_file_path().exists() {
            return Err(Error::KeyExists(self.key_file_path.clone()));
        }

        let metadata = key.metadata();
        let sealed_key = self.crypto.seal(&key).map_err(Error::Crypto)?;

        let key_file = File::create(self.key_file_path())?;
        serde_cbor::to_writer(
            &key_file,
            &Stored {
                public_key: Self::PublicKey::from(key),
                secret_key: sealed_key,
                metadata,
            },
        )?;
        key_file.sync_all()?;

        Ok(())
    }

    fn get_key(&self) -> Result<Keypair<Self::PublicKey, Self::SecretKey>, Self::Error> {
        if !self.key_file_path().exists() {
            return Err(Error::NoSuchKey(self.key_file_path.clone()));
        }

        let stored: Stored<Self::PublicKey, <C as Crypto>::SecretBox, Self::Metadata> =
            serde_cbor::from_reader(File::open(self.key_file_path())?)?;

        let secret_key = {
            let sbox = stored.secret_key;
            let meta = stored.metadata;

            self.crypto
                .unseal(sbox)
                .map_err(Error::Crypto)
                .and_then(|sec| {
                    Self::SecretKey::from_bytes_and_meta(sec, &meta).map_err(Error::Conversion)
                })
        }?;

        Ok(Keypair {
            public_key: stored.public_key,
            secret_key,
        })
    }

    fn show_key(&self) -> Result<(Self::PublicKey, Self::Metadata), Self::Error> {
        if !self.key_file_path().exists() {
            return Err(Error::NoSuchKey(self.key_file_path.clone()));
        }

        let stored: Stored<Self::PublicKey, <C as Crypto>::SecretBox, Self::Metadata> =
            serde_cbor::from_reader(File::open(self.key_file_path())?)?;

        Ok((stored.public_key, stored.metadata))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        crypto::{self, Pwhash, SecretBoxError},
        pinentry::Pinentry,
        test::*,
    };
    use tempfile::tempdir;

    fn with_fs_store<F, P>(pin: P, f: F)
    where
        F: FnOnce(FileStorage<Pwhash<P>, PublicKey, SecretKey, ()>),
        P: Pinentry,
    {
        let tmp = tempdir().expect("Can't get tempdir");
        f(FileStorage::new(
            &tmp.path().join("test.key"),
            Pwhash::new(pin, *crypto::KDF_PARAMS_TEST),
        ))
    }

    #[test]
    fn test_get_after_put() {
        with_fs_store(default_passphrase(), get_after_put)
    }

    #[test]
    fn test_put_twice() {
        with_fs_store(default_passphrase(), |store| {
            let path = store.key_file_path().to_path_buf();
            put_twice(store, Error::KeyExists(path))
        })
    }

    #[test]
    fn test_get_empty() {
        with_fs_store(default_passphrase(), |store| {
            let path = store.key_file_path().to_path_buf();
            get_empty(store, Error::NoSuchKey(path))
        })
    }

    #[test]
    fn test_passphrase_mismatch() {
        with_fs_store(PinCycle::new(&["right".into(), "wrong".into()]), |store| {
            passphrase_mismatch(store, Error::Crypto(SecretBoxError::InvalidKey))
        })
    }
}