zerokms_protocol/
lib.rs

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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
mod base64_array;
mod base64_vec;
mod error;

use async_trait::async_trait;
use cipherstash_config::DatasetConfig;
use serde::{Deserialize, Serialize};
use std::{borrow::Cow, ops::Deref};
use uuid::Uuid;
use zeroize::{Zeroize, ZeroizeOnDrop};

pub use cipherstash_config;
/// Re-exports
pub use error::*;
pub mod testing;

#[async_trait]
pub trait ViturConnection {
    async fn send<Request: ViturRequest>(
        &self,
        request: Request,
        access_token: &str,
    ) -> Result<Request::Response, ViturRequestError>;
}

pub trait ViturResponse: Serialize + for<'de> Deserialize<'de> + Send {}

#[async_trait]
pub trait ViturRequest: Serialize + for<'de> Deserialize<'de> + Sized + Send {
    type Response: ViturResponse;

    const SCOPE: &'static str;
    const ENDPOINT: &'static str;
}

/// Request message to create a new [Dataset] with the given name and description.
///
/// Requies the `dataset:create` scope.
#[derive(Debug, Serialize, Deserialize)]
pub struct CreateDatasetRequest<'a> {
    pub name: Cow<'a, str>,
    pub description: Cow<'a, str>,
}

impl ViturRequest for CreateDatasetRequest<'_> {
    type Response = Dataset;

    const ENDPOINT: &'static str = "create-dataset";
    const SCOPE: &'static str = "dataset:create";
}

/// Request message to list all [Dataset]s.
///
/// Requires the `dataset:list` scope.
/// Response is a vector of [Dataset]s.
#[derive(Default, Debug, Serialize, Deserialize)]
pub struct ListDatasetRequest {
    #[serde(default)]
    pub show_disabled: bool,
}

impl ViturRequest for ListDatasetRequest {
    type Response = Vec<Dataset>;

    const ENDPOINT: &'static str = "list-datasets";
    const SCOPE: &'static str = "dataset:list";
}

/// Struct representing a dataset.
/// This is the response to a [CreateDatasetRequest] and a in a vector in the response to a [ListDatasetRequest].
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct Dataset {
    pub id: Uuid,
    pub name: String,
    pub description: String,
}

impl ViturResponse for Dataset {}
impl ViturResponse for Vec<Dataset> {}

/// Represents an empty response for requests that don't return any data.
#[derive(Default, Debug, Serialize, Deserialize)]
pub struct EmptyResponse {}

impl ViturResponse for EmptyResponse {}

/// Request message to create a new client with the given name, description and dataset_id.
///
/// Requires the `client:create` scope.
/// Response is a [CreateClientResponse].
#[derive(Debug, Serialize, Deserialize)]
pub struct CreateClientRequest<'a> {
    pub dataset_id: Uuid,
    pub name: Cow<'a, str>,
    pub description: Cow<'a, str>,
}

impl<'a> ViturRequest for CreateClientRequest<'a> {
    type Response = CreateClientResponse;

    const ENDPOINT: &'static str = "create-client";
    const SCOPE: &'static str = "client:create";
}

/// Response message to a [CreateClientRequest].
///
/// Contains the `client_id`` and the `client_key, the latter being a base64 encoded 32 byte key.
/// The `client_key` should be considered sensitive and should be stored securely.
#[derive(Debug, Serialize, Deserialize)]
pub struct CreateClientResponse {
    pub id: Uuid,
    pub dataset_id: Uuid,
    pub name: String,
    pub description: String,
    pub client_key: ViturKeyMaterial,
}

impl ViturResponse for CreateClientResponse {}

/// Request message to list all clients.
///
/// Requires the `client:list` scope.
/// Response is a vector of [DatasetClient]s.
#[derive(Debug, Serialize, Deserialize)]
pub struct ListClientRequest;

impl ViturRequest for ListClientRequest {
    type Response = Vec<DatasetClient>;

    const ENDPOINT: &'static str = "list-clients";
    const SCOPE: &'static str = "client:list";
}

/// Struct representing the dataset ids associated with a client
/// which could be a single dataset or multiple datasets.
#[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
#[serde(untagged)]
pub enum ClientDatasetId {
    Single(Uuid),
    Multiple(Vec<Uuid>),
}

/// A `Uuid` is comparable with `ClientDatasetId` if the `ClientDatasetId` is a `Single` variant.
impl PartialEq<Uuid> for ClientDatasetId {
    fn eq(&self, other: &Uuid) -> bool {
        if let ClientDatasetId::Single(id) = self {
            id == other
        } else {
            false
        }
    }
}

/// Response type for a [ListClientRequest].
#[derive(Debug, Serialize, Deserialize)]
pub struct DatasetClient {
    pub id: Uuid,
    pub dataset_id: ClientDatasetId,
    pub name: String,
    pub description: String,
}

impl ViturResponse for Vec<DatasetClient> {}

/// Request message to delete a client and all associated authority keys.
///
/// Requires the `client:revoke` scope.
/// Response is an [DeleteClientResponse].
#[derive(Debug, Serialize, Deserialize)]
pub struct DeleteClientRequest {
    pub client_id: Uuid,
}

impl ViturRequest for DeleteClientRequest {
    type Response = DeleteClientResponse;

    const ENDPOINT: &'static str = "delete-client";
    const SCOPE: &'static str = "client:delete";
}

#[derive(Default, Debug, Serialize, Deserialize)]
pub struct DeleteClientResponse {}

impl ViturResponse for DeleteClientResponse {}

/// Key material type used in [GenerateKeyRequest] and [RetrieveKeyRequest] as well as [CreateClientResponse].
#[derive(Serialize, Deserialize, Zeroize, ZeroizeOnDrop)]
pub struct ViturKeyMaterial(#[serde(with = "base64_vec")] Vec<u8>);
opaque_debug::implement!(ViturKeyMaterial);

impl From<Vec<u8>> for ViturKeyMaterial {
    fn from(inner: Vec<u8>) -> Self {
        Self(inner)
    }
}

impl Deref for ViturKeyMaterial {
    type Target = [u8];

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

/// Represents generated data key material which is used by the client to derive data keys with its own key material.
///
/// Returned in the response to a [GenerateKeyRequest].
#[derive(Debug, Serialize, Deserialize)]
pub struct GeneratedKey {
    pub key_material: ViturKeyMaterial,
    // FIXME: Use Vitamin C Equatable type
    #[serde(with = "base64_vec")]
    pub tag: Vec<u8>,
}

/// Response to a [GenerateKeyRequest].
#[derive(Debug, Serialize, Deserialize)]
pub struct GenerateKeyResponse {
    pub keys: Vec<GeneratedKey>,
}

impl ViturResponse for GenerateKeyResponse {}

/// A specification for generating a data key used in a [GenerateKeyRequest].
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct GenerateKeySpec<'a> {
    // FIXME: Remove IV and have the server generate it instead
    #[serde(with = "base64_array")]
    pub iv: [u8; 16],
    pub descriptor: Cow<'a, str>,
}

/// A request message to generate a data key made on behalf of a client
/// in the given dataset.
///
/// Requires the `data_key:generate` scope.
/// Response is a [GenerateKeyResponse].
///
/// See also [GenerateKeySpec].
#[derive(Debug, Serialize, Deserialize)]
pub struct GenerateKeyRequest<'a> {
    pub client_id: Uuid,
    pub dataset_id: Option<Uuid>,
    pub keys: Cow<'a, [GenerateKeySpec<'a>]>,
}

impl ViturRequest for GenerateKeyRequest<'_> {
    type Response = GenerateKeyResponse;

    const ENDPOINT: &'static str = "generate-data-key";
    const SCOPE: &'static str = "data_key:generate";
}

/// Returned type from a [RetrieveKeyRequest].
#[derive(Debug, Serialize, Deserialize)]
pub struct RetrievedKey {
    pub key_material: ViturKeyMaterial,
}

/// Response to a [RetrieveKeyRequest].
/// Contains a list of [RetrievedKey]s.
#[derive(Debug, Serialize, Deserialize)]
pub struct RetrieveKeyResponse {
    pub keys: Vec<RetrievedKey>,
}

impl ViturResponse for RetrieveKeyResponse {}

/// A specification for retrieving a data key used in a [RetrieveKeyRequest].
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct RetrieveKeySpec<'a> {
    #[serde(with = "base64_array")]
    pub iv: [u8; 16],
    pub descriptor: Cow<'a, str>,
    pub tag: Cow<'a, [u8]>,

    // Since this field will be removed in the future allow older versions of Vitur to be able to
    // parse a RetrieveKeySpec that doesn't include the tag_version.
    #[serde(default)]
    pub tag_version: usize,
}

/// Request to retrieve a data key on behalf of a client in the given dataset.
/// Requires the `data_key:retrieve` scope.
/// Response is a [RetrieveKeyResponse].
///
/// See also [RetrieveKeySpec].
#[derive(Debug, Serialize, Deserialize)]
pub struct RetrieveKeyRequest<'a> {
    pub client_id: Uuid,
    pub dataset_id: Option<Uuid>,
    pub keys: Cow<'a, [RetrieveKeySpec<'a>]>,
}

impl ViturRequest for RetrieveKeyRequest<'_> {
    type Response = RetrieveKeyResponse;

    const ENDPOINT: &'static str = "retrieve-data-key";
    const SCOPE: &'static str = "data_key:retrieve";
}

#[derive(Debug, Serialize, Deserialize)]
pub struct SaveConfigRequest<'a> {
    pub client_id: Uuid,
    #[serde(with = "base64_vec")]
    pub encrypted_index_root_key: Vec<u8>,
    pub dataset_config: Cow<'a, DatasetConfig>,
    pub dataset_id: Option<Uuid>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct SaveConfigResponse {}

impl ViturResponse for SaveConfigResponse {}

impl ViturRequest for SaveConfigRequest<'_> {
    type Response = SaveConfigResponse;

    const ENDPOINT: &'static str = "save-config";
    const SCOPE: &'static str = "config:write";
}

#[derive(Debug, Serialize, Deserialize)]
pub struct LoadConfigRequest {
    pub client_id: Uuid,
    pub dataset_id: Option<Uuid>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct LoadConfigResponse {
    #[serde(with = "base64_vec")]
    pub encrypted_index_root_key: Vec<u8>,
    pub dataset_config: DatasetConfig,
}

impl ViturResponse for LoadConfigResponse {}

impl ViturRequest for LoadConfigRequest {
    type Response = LoadConfigResponse;

    const ENDPOINT: &'static str = "load-config";
    const SCOPE: &'static str = "config:read";
}

/// Request message to disable a dataset.
/// Requires the `dataset:disable` scope.
/// Response is an [EmptyResponse].
#[derive(Debug, Serialize, Deserialize)]
pub struct DisableDatasetRequest {
    pub dataset_id: Uuid,
}

impl ViturRequest for DisableDatasetRequest {
    type Response = EmptyResponse;

    const ENDPOINT: &'static str = "disable-dataset";
    const SCOPE: &'static str = "dataset:disable";
}

/// Request message to enable a dataset that has was previously disabled.
/// Requires the `dataset:enable` scope.
/// Response is an [EmptyResponse].
#[derive(Debug, Serialize, Deserialize)]
pub struct EnableDatasetRequest {
    pub dataset_id: Uuid,
}

impl ViturRequest for EnableDatasetRequest {
    type Response = EmptyResponse;

    const ENDPOINT: &'static str = "enable-dataset";
    const SCOPE: &'static str = "dataset:enable";
}

/// Request message to modify a dataset with the given dataset_id.
/// `name` and `description` are optional and will be updated if provided.
///
/// Requires the `dataset:modify` scope.
/// Response is an [EmptyResponse].
#[derive(Debug, Serialize, Deserialize)]
pub struct ModifyDatasetRequest<'a> {
    pub dataset_id: Uuid,

    pub name: Option<Cow<'a, str>>,
    pub description: Option<Cow<'a, str>>,
}

impl ViturRequest for ModifyDatasetRequest<'_> {
    type Response = EmptyResponse;

    const ENDPOINT: &'static str = "modify-dataset";
    const SCOPE: &'static str = "dataset:modify";
}

/// Request message to grant a client access to a dataset.
/// Requires the `dataset:grant` scope.
///
/// Response is an [EmptyResponse].
#[derive(Debug, Serialize, Deserialize)]
pub struct GrantDatasetRequest {
    pub client_id: Uuid,
    pub dataset_id: Uuid,
}

impl ViturRequest for GrantDatasetRequest {
    type Response = EmptyResponse;

    const ENDPOINT: &'static str = "grant-dataset";
    const SCOPE: &'static str = "dataset:grant";
}

/// Request message to revoke a client's access to a dataset.
/// Requires the `dataset:revoke` scope.
/// Response is an [EmptyResponse].
#[derive(Debug, Serialize, Deserialize)]
pub struct RevokeDatasetRequest {
    pub client_id: Uuid,
    pub dataset_id: Uuid,
}

impl ViturRequest for RevokeDatasetRequest {
    type Response = EmptyResponse;

    const ENDPOINT: &'static str = "revoke-dataset";
    const SCOPE: &'static str = "dataset:revoke";
}

/// Request to load a dataset on behalf of a client.
/// This is used by clients before indexing or querying data and includes
/// key material which can be derived by the client to generate encrypted index terms.
///
/// If a dataset_id is not provided the client's default dataset will be loaded.
///
/// Requires the `data_key:retrieve` scope (though this may change in the future).
/// Response is a [LoadDatasetResponse].
#[derive(Debug, Serialize, Deserialize)]
pub struct LoadDatasetRequest {
    pub client_id: Uuid,
    pub dataset_id: Option<Uuid>,
}

impl ViturRequest for LoadDatasetRequest {
    type Response = LoadDatasetResponse;

    const ENDPOINT: &'static str = "load-dataset";
    // NOTE: We don't currently support the ability to allow an operation
    // based on any one of several possible scopes so we'll just use `data_key:retrieve` for now.
    // This should probably be allowed for any operation that requires indexing or querying.
    const SCOPE: &'static str = "data_key:retrieve";
}

/// Response to a [LoadDatasetRequest].
/// The response includes the key material required to derive data keys.
/// It is analogous to a [RetrieveKeyResponse] but where the server generated the key.
#[derive(Debug, Serialize, Deserialize)]
pub struct LoadDatasetResponse {
    pub partial_index_key: RetrievedKey,
    pub dataset: Dataset,
}

impl ViturResponse for LoadDatasetResponse {}