summaryrefslogtreecommitdiffstats
path: root/secret/src/lib.rs
blob: 02fec9a07b711a0662a1400071daf3d5c61937b9 (plain)
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
//! Handling secret keys.

extern crate capnp;
#[macro_use]
extern crate capnp_rpc;
#[macro_use]
extern crate failure;
extern crate futures;
extern crate rusqlite;
extern crate time;
extern crate tokio_core;
extern crate tokio_io;

use capnp::capability::Promise;
use capnp_rpc::rpc_twoparty_capnp::Side;
use futures::Future;
use std::cell::RefCell;
use std::fmt;
use std::io;
use std::rc::Rc;
use tokio_core::reactor::Core;

extern crate openpgp;
#[allow(unused_imports)]
#[macro_use]
extern crate sequoia_core;
use sequoia_core as core;
extern crate sequoia_net;
use sequoia_net as net;

/// Macros managing requests and responses.
#[macro_use] mod macros;

use openpgp::Fingerprint;
use openpgp::tpk::TPK;
use core::Context;
use net::ipc;

#[allow(dead_code)] pub /*XXX*/ mod secret_protocol_capnp;
use secret_protocol_capnp::node;

/// Dummy TSK implementation.
pub struct TSK(TPK);

impl TSK {
    pub fn new(tpk: TPK) -> TSK {
        TSK(tpk)
    }

    pub fn from_bytes(b: &[u8]) -> Result<TSK> {
        TPK::from_bytes(b).map(|x| TSK(x))
    }

    pub fn serialize<W: io::Write>(&self, o: &mut W) -> Result<()> {
        self.0.serialize(o)
    }

    pub fn tpk(&self) -> TPK {
        self.0.clone()
    }

    pub fn fingerprint(&self) -> Fingerprint {
        self.0.fingerprint()
    }
}

trait SecretKeyObject {
    fn fingerprint(&self) -> &Fingerprint;
    fn tpk(&self) -> Result<TPK>;
    fn unlock(&self, passphrase: &str) -> Result<()>;
    fn lock(&self) -> Result<()>;
    fn decrypt(&self, sk: &[u8]) -> Result<Vec<u8>>;
}

/// Storage backend.
mod backend;

/// Returns the service descriptor.
#[doc(hidden)]
pub fn descriptor(c: &Context) -> ipc::Descriptor {
    ipc::Descriptor::new(
        c,
        c.home().join("S.secret"),
        c.lib().join("secret-store"),
        backend::factory,
    )
}

/// A secret key object.
pub struct Key {
    core: Rc<RefCell<Core>>,
    fp: Fingerprint,
    key: node::key::Client,
}

impl fmt::Debug for Key {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Key {{ {} }}", self.fp)
    }
}

impl Key {
    fn connect(c: &Context) -> Result<(Core, node::Client)> {
        let descriptor = descriptor(c);
        let core = Core::new()?;
        let handle = core.handle();

        let mut rpc_system
            = match descriptor.connect_with_policy(&handle,
                                                   core::IPCPolicy::External) {
                Ok(r) => r,
                Err(e) => return Err(e.into()),
            };

        let client: node::Client = rpc_system.bootstrap(Side::Server);
        handle.spawn(rpc_system.map_err(|_e| ()));

        Ok((core, client))
    }

    pub fn open(c: &Context, fp: &Fingerprint) -> Result<Self> {
        let (mut core, client) = Self::connect(c)?;

        let mut request = client.open_request();
        request.get().set_fingerprint(&fp.to_hex());

        let key = make_request!(&mut core, request)?;
        Ok(Self::new(Rc::new(RefCell::new(core)), fp, key))
    }

    fn new(core: Rc<RefCell<Core>>, fp: &Fingerprint,
           key: node::key::Client) -> Self {
        Key{core: core, fp: fp.clone(), key: key}
    }

    pub fn import(c: &Context, tsk: &TSK) -> Result<Key> {
        let mut blob = vec![];
        tsk.serialize(&mut blob)?;
        let (mut core, client) = Self::connect(c)?;
        let mut request = client.import_request();
        request.get().set_key(&blob);
        let key = make_request!(core, request)?;
        Ok(Self::new(Rc::new(RefCell::new(core)), &tsk.fingerprint(), key))
    }

    /// Lists all keys.
    pub fn list(c: &Context) -> Result<KeyIter> {
        let (mut core, client) = Self::connect(c)?;
        let request = client.iter_request();
        let iter = make_request!(&mut core, request)?;
        Ok(KeyIter{core: Rc::new(RefCell::new(core)), iter: iter})
    }
}

impl SecretKeyObject for Key {
    /// Returns the fingerprint.
    fn fingerprint(&self) -> &Fingerprint {
        &self.fp
    }

    /// Returns the TPK.
    fn tpk(&self) -> Result<TPK> {
        make_request_map!(self.core.borrow_mut(),
                          self.key.tpk_request(),
                          |tpk| TPK::from_bytes(tpk).map_err(|e| e.into()))
    }

    /// Unlocks this secret key.
    fn unlock(&self, passphrase: &str) -> Result<()> {
        let mut request = self.key.unlock_request();
        request.get().set_passphrase(passphrase.into());
        make_request_map!(self.core.borrow_mut(), request,
                          |_| Ok(()))
    }

    /// Locks this secret key.
    fn lock(&self) -> Result<()> {
        make_request_map!(self.core.borrow_mut(),
                          self.key.lock_request(),
                          |_| Ok(()))
    }