identity_storage/key_storage/
memstore.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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
// Copyright 2020-2023 IOTA Stiftung, Fondazione Links
// SPDX-License-Identifier: Apache-2.0

use core::fmt::Debug;
use std::collections::HashMap;
use std::fmt::Display;
use std::str::FromStr;

use async_trait::async_trait;
use crypto::signatures::ed25519::SecretKey;
use identity_verification::jose::jwk::EdCurve;
use identity_verification::jose::jwk::Jwk;
use identity_verification::jose::jwk::JwkType;
use identity_verification::jose::jws::JwsAlgorithm;
use identity_verification::jwk::BlsCurve;
use rand::distributions::DistString;
use shared::Shared;
use tokio::sync::RwLockReadGuard;
use tokio::sync::RwLockWriteGuard;

use super::ed25519::encode_jwk;
use super::ed25519::expand_secret_jwk;
use super::jwk_gen_output::JwkGenOutput;
use super::KeyId;
use super::KeyStorageError;
use super::KeyStorageErrorKind;
use super::KeyStorageResult;
use super::KeyType;
use crate::key_storage::JwkStorage;

/// The map from key ids to JWKs.
type JwkKeyStore = HashMap<KeyId, Jwk>;

/// An insecure, in-memory [`JwkStorage`] implementation that serves as an example and may be used in tests.
#[derive(Debug)]
pub struct JwkMemStore {
  jwk_store: Shared<JwkKeyStore>,
}

impl JwkMemStore {
  /// Creates a new, empty `JwkMemStore` instance.
  pub fn new() -> Self {
    Self {
      jwk_store: Shared::new(HashMap::new()),
    }
  }

  /// Returns the number of items contained in the [`JwkMemStore`].
  pub async fn count(&self) -> usize {
    self.jwk_store.read().await.keys().count()
  }
}

// Refer to the `JwkStorage` interface docs for high-level documentation of the individual methods.
#[cfg_attr(not(feature = "send-sync-storage"), async_trait(?Send))]
#[cfg_attr(feature = "send-sync-storage", async_trait)]
impl JwkStorage for JwkMemStore {
  async fn generate(&self, key_type: KeyType, alg: JwsAlgorithm) -> KeyStorageResult<JwkGenOutput> {
    let key_type: MemStoreKeyType = MemStoreKeyType::try_from(&key_type)?;

    check_key_alg_compatibility(key_type, &alg)?;

    let (private_key, public_key) = match key_type {
      MemStoreKeyType::Ed25519 => {
        let private_key = SecretKey::generate()
          .map_err(|err| KeyStorageError::new(KeyStorageErrorKind::RetryableIOFailure).with_source(err))?;
        let public_key = private_key.public_key();
        (private_key, public_key)
      }
      other => {
        return Err(
          KeyStorageError::new(KeyStorageErrorKind::UnsupportedKeyType)
            .with_custom_message(format!("{other} is not supported")),
        );
      }
    };

    let kid: KeyId = random_key_id();

    let mut jwk: Jwk = encode_jwk(&private_key, &public_key);
    jwk.set_alg(alg.name());
    jwk.set_kid(jwk.thumbprint_sha256_b64());
    let public_jwk: Jwk = jwk.to_public().expect("should only panic if kty == oct");

    let mut jwk_store: RwLockWriteGuard<'_, JwkKeyStore> = self.jwk_store.write().await;
    jwk_store.insert(kid.clone(), jwk);

    Ok(JwkGenOutput::new(kid, public_jwk))
  }

  async fn insert(&self, jwk: Jwk) -> KeyStorageResult<KeyId> {
    let key_type = MemStoreKeyType::try_from(&jwk)?;

    if !jwk.is_private() {
      return Err(
        KeyStorageError::new(KeyStorageErrorKind::Unspecified)
          .with_custom_message("expected a Jwk with all private key components set"),
      );
    }

    match jwk.alg() {
      Some(alg) => {
        let alg: JwsAlgorithm = JwsAlgorithm::from_str(alg)
          .map_err(|err| KeyStorageError::new(KeyStorageErrorKind::UnsupportedSignatureAlgorithm).with_source(err))?;
        check_key_alg_compatibility(key_type, &alg)?;
      }
      None => {
        return Err(
          KeyStorageError::new(KeyStorageErrorKind::UnsupportedSignatureAlgorithm)
            .with_custom_message("expected a Jwk with an `alg` parameter"),
        );
      }
    }

    if jwk.alg().is_none() {
      return Err(
        KeyStorageError::new(KeyStorageErrorKind::UnsupportedSignatureAlgorithm)
          .with_custom_message("expected a Jwk with an `alg` parameter"),
      );
    }

    let key_id: KeyId = random_key_id();

    let mut jwk_store: RwLockWriteGuard<'_, JwkKeyStore> = self.jwk_store.write().await;

    jwk_store.insert(key_id.clone(), jwk);

    Ok(key_id)
  }

  async fn sign(&self, key_id: &KeyId, data: &[u8], public_key: &Jwk) -> KeyStorageResult<Vec<u8>> {
    let jwk_store: RwLockReadGuard<'_, JwkKeyStore> = self.jwk_store.read().await;

    // Extract the required alg from the given public key
    let alg = public_key
      .alg()
      .ok_or(KeyStorageErrorKind::UnsupportedSignatureAlgorithm)
      .and_then(|alg_str| {
        JwsAlgorithm::from_str(alg_str).map_err(|_| KeyStorageErrorKind::UnsupportedSignatureAlgorithm)
      })?;

    // Check that `kty` is `Okp` and `crv = Ed25519`.
    match alg {
      JwsAlgorithm::EdDSA => {
        let okp_params = public_key.try_okp_params().map_err(|err| {
          KeyStorageError::new(KeyStorageErrorKind::Unspecified)
            .with_custom_message(format!("expected a Jwk with Okp params in order to sign with {alg}"))
            .with_source(err)
        })?;
        if okp_params.crv != EdCurve::Ed25519.name() {
          return Err(
            KeyStorageError::new(KeyStorageErrorKind::Unspecified).with_custom_message(format!(
              "expected Jwk with Okp {} crv in order to sign with {alg}",
              EdCurve::Ed25519
            )),
          );
        }
      }
      other => {
        return Err(
          KeyStorageError::new(KeyStorageErrorKind::UnsupportedSignatureAlgorithm)
            .with_custom_message(format!("{other} is not supported")),
        );
      }
    };

    // Obtain the corresponding private key and sign `data`.
    let jwk: &Jwk = jwk_store
      .get(key_id)
      .ok_or_else(|| KeyStorageError::new(KeyStorageErrorKind::KeyNotFound))?;
    let secret_key = expand_secret_jwk(jwk)?;
    Ok(secret_key.sign(data).to_bytes().to_vec())
  }

  async fn delete(&self, key_id: &KeyId) -> KeyStorageResult<()> {
    let mut jwk_store: RwLockWriteGuard<'_, JwkKeyStore> = self.jwk_store.write().await;

    jwk_store
      .remove(key_id)
      .map(|_| ())
      .ok_or_else(|| KeyStorageError::new(KeyStorageErrorKind::KeyNotFound))
  }

  async fn exists(&self, key_id: &KeyId) -> KeyStorageResult<bool> {
    let jwk_store: RwLockReadGuard<'_, JwkKeyStore> = self.jwk_store.read().await;
    Ok(jwk_store.contains_key(key_id))
  }
}

#[derive(Debug, Copy, Clone)]
enum MemStoreKeyType {
  Ed25519,
  BLS12381G2,
}

impl JwkMemStore {
  const ED25519_KEY_TYPE_STR: &'static str = "Ed25519";
  /// The Ed25519 key type.
  pub const ED25519_KEY_TYPE: KeyType = KeyType::from_static_str(Self::ED25519_KEY_TYPE_STR);

  const BLS12381G2_KEY_TYPE_STR: &'static str = "BLS12381G2";
  /// The BLS12381G2 key type
  pub const BLS12381G2_KEY_TYPE: KeyType = KeyType::from_static_str(Self::BLS12381G2_KEY_TYPE_STR);
}

impl MemStoreKeyType {
  const fn name(&self) -> &'static str {
    match self {
      MemStoreKeyType::Ed25519 => JwkMemStore::ED25519_KEY_TYPE_STR,
      MemStoreKeyType::BLS12381G2 => JwkMemStore::BLS12381G2_KEY_TYPE_STR,
    }
  }
}

impl Display for MemStoreKeyType {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    f.write_str(self.name())
  }
}

impl TryFrom<&KeyType> for MemStoreKeyType {
  type Error = KeyStorageError;

  fn try_from(value: &KeyType) -> Result<Self, Self::Error> {
    match value.as_str() {
      JwkMemStore::ED25519_KEY_TYPE_STR => Ok(MemStoreKeyType::Ed25519),
      JwkMemStore::BLS12381G2_KEY_TYPE_STR => Ok(MemStoreKeyType::BLS12381G2),
      _ => Err(KeyStorageError::new(KeyStorageErrorKind::UnsupportedKeyType)),
    }
  }
}

impl TryFrom<&Jwk> for MemStoreKeyType {
  type Error = KeyStorageError;

  fn try_from(jwk: &Jwk) -> Result<Self, Self::Error> {
    match jwk.kty() {
      JwkType::Okp => {
        let okp_params = jwk.try_okp_params().map_err(|err| {
          KeyStorageError::new(KeyStorageErrorKind::UnsupportedKeyType)
            .with_custom_message("expected Okp parameters for a JWK with `kty` Okp")
            .with_source(err)
        })?;
        match okp_params.try_ed_curve().map_err(|err| {
          KeyStorageError::new(KeyStorageErrorKind::UnsupportedKeyType)
            .with_custom_message("only Ed curves are supported for signing")
            .with_source(err)
        })? {
          EdCurve::Ed25519 => Ok(MemStoreKeyType::Ed25519),
          curve => Err(
            KeyStorageError::new(KeyStorageErrorKind::UnsupportedKeyType)
              .with_custom_message(format!("{curve} not supported")),
          ),
        }
      }
      JwkType::Ec => {
        let ec_params = jwk.try_ec_params().map_err(|err| {
          KeyStorageError::new(KeyStorageErrorKind::UnsupportedKeyType)
            .with_custom_message("expected EC parameters for a JWK with `kty` Ec")
            .with_source(err)
        })?;
        match ec_params.try_bls_curve().map_err(|err| {
          KeyStorageError::new(KeyStorageErrorKind::UnsupportedKeyType)
            .with_custom_message("only Ed curves are supported for signing")
            .with_source(err)
        })? {
          BlsCurve::BLS12381G2 => Ok(MemStoreKeyType::BLS12381G2),
          curve => Err(
            KeyStorageError::new(KeyStorageErrorKind::UnsupportedKeyType)
              .with_custom_message(format!("{curve} not supported")),
          ),
        }
      }
      other => Err(
        KeyStorageError::new(KeyStorageErrorKind::UnsupportedKeyType)
          .with_custom_message(format!("Jwk `kty` {other} not supported")),
      ),
    }
  }
}

impl Default for JwkMemStore {
  fn default() -> Self {
    Self::new()
  }
}

/// Generate a random alphanumeric string of len 32.
fn random_key_id() -> KeyId {
  KeyId::new(rand::distributions::Alphanumeric.sample_string(&mut rand::thread_rng(), 32))
}

/// Check that the key type can be used with the algorithm.
fn check_key_alg_compatibility(key_type: MemStoreKeyType, alg: &JwsAlgorithm) -> KeyStorageResult<()> {
  match (key_type, alg) {
    (MemStoreKeyType::Ed25519, JwsAlgorithm::EdDSA) => Ok(()),
    (key_type, alg) => Err(
      KeyStorageError::new(crate::key_storage::KeyStorageErrorKind::KeyAlgorithmMismatch)
        .with_custom_message(format!("`cannot use key type `{key_type}` with algorithm `{alg}`")),
    ),
  }
}

#[cfg(feature = "jpt-bbs-plus")]
mod bbs_plus_impl {
  use std::str::FromStr as _;

  use crate::key_storage::bls::encode_bls_jwk;
  use crate::key_storage::bls::expand_bls_jwk;
  use crate::key_storage::bls::generate_bbs_keypair;
  use crate::key_storage::bls::sign_bbs;
  use crate::key_storage::bls::update_bbs_signature;
  use crate::JwkGenOutput;
  use crate::JwkMemStore;
  use crate::JwkStorageBbsPlusExt;
  use crate::KeyId;
  use crate::KeyStorageError;
  use crate::KeyStorageErrorKind;
  use crate::KeyStorageResult;
  use crate::KeyType;
  use crate::ProofUpdateCtx;
  use async_trait::async_trait;
  use identity_verification::jwk::BlsCurve;
  use identity_verification::jwk::Jwk;
  use jsonprooftoken::jpa::algs::ProofAlgorithm;

  use super::random_key_id;

  /// JwkStorageBbsPlusExt implementation for JwkMemStore
  #[cfg_attr(not(feature = "send-sync-storage"), async_trait(?Send))]
  #[cfg_attr(feature = "send-sync-storage", async_trait)]
  impl JwkStorageBbsPlusExt for JwkMemStore {
    async fn generate_bbs(&self, key_type: KeyType, alg: ProofAlgorithm) -> KeyStorageResult<JwkGenOutput> {
      if key_type != JwkMemStore::BLS12381G2_KEY_TYPE {
        return Err(
          KeyStorageError::new(KeyStorageErrorKind::UnsupportedKeyType)
            .with_custom_message(format!("unsupported key type {key_type}")),
        );
      }

      let (private_key, public_key) = generate_bbs_keypair(alg)?;
      let (jwk, public_jwk) = encode_bls_jwk(&private_key, &public_key, alg);

      let kid: KeyId = random_key_id();
      let mut jwk_store = self.jwk_store.write().await;
      jwk_store.insert(kid.clone(), jwk);

      Ok(JwkGenOutput::new(kid, public_jwk))
    }

    async fn sign_bbs(
      &self,
      key_id: &KeyId,
      data: &[Vec<u8>],
      header: &[u8],
      public_key: &Jwk,
    ) -> KeyStorageResult<Vec<u8>> {
      let jwk_store = self.jwk_store.read().await;

      // Extract the required alg from the given public key
      let alg = public_key
        .alg()
        .and_then(|alg_str| ProofAlgorithm::from_str(alg_str).ok())
        .ok_or(KeyStorageErrorKind::UnsupportedProofAlgorithm)?;

      // Check the provided JWK represents a BLS12381G2 key.
      if !public_key
        .try_ec_params()
        .map(|ec| ec.crv == BlsCurve::BLS12381G2.to_string())
        .unwrap_or(false)
      {
        return Err(
          KeyStorageError::new(KeyStorageErrorKind::UnsupportedKeyType)
            .with_custom_message(format!("expected a key from the {} curve", BlsCurve::BLS12381G2)),
        );
      }

      // Obtain the corresponding private key.
      let jwk: &Jwk = jwk_store.get(key_id).ok_or(KeyStorageErrorKind::KeyNotFound)?;
      let (sk, pk) = expand_bls_jwk(jwk)?;

      sign_bbs(alg, data, &sk.expect("jwk is private"), &pk, header)
    }

    async fn update_signature(
      &self,
      key_id: &KeyId,
      public_key: &Jwk,
      signature: &[u8],
      ctx: ProofUpdateCtx,
    ) -> KeyStorageResult<Vec<u8>> {
      let jwk_store = self.jwk_store.read().await;

      // Extract the required alg from the given public key
      let alg = public_key
        .alg()
        .ok_or(KeyStorageErrorKind::UnsupportedProofAlgorithm)
        .and_then(|alg_str| {
          ProofAlgorithm::from_str(alg_str).map_err(|_| KeyStorageErrorKind::UnsupportedProofAlgorithm)
        })?;

      // Check the provided JWK represents a BLS12381G2 key.
      if !public_key
        .try_ec_params()
        .map(|ec| ec.crv == BlsCurve::BLS12381G2.to_string())
        .unwrap_or(false)
      {
        return Err(
          KeyStorageError::new(KeyStorageErrorKind::UnsupportedKeyType)
            .with_custom_message(format!("expected a key from the {} curve", BlsCurve::BLS12381G2)),
        );
      }

      // Obtain the corresponding private key.
      let jwk = jwk_store.get(key_id).ok_or(KeyStorageErrorKind::KeyNotFound)?;
      let sk = expand_bls_jwk(jwk)?.0.expect("jwk is private");

      // Update the signature.
      update_bbs_signature(alg, signature, &sk, &ctx)
    }
  }
}
pub(crate) mod shared {
  use core::fmt::Debug;
  use core::fmt::Formatter;
  use tokio::sync::RwLock;
  use tokio::sync::RwLockReadGuard;
  use tokio::sync::RwLockWriteGuard;

  #[derive(Default)]
  pub(crate) struct Shared<T>(RwLock<T>);

  impl<T> Shared<T> {
    pub(crate) fn new(data: T) -> Self {
      Self(RwLock::new(data))
    }

    pub(crate) async fn read(&self) -> RwLockReadGuard<'_, T> {
      self.0.read().await
    }

    pub(crate) async fn write(&self) -> RwLockWriteGuard<'_, T> {
      self.0.write().await
    }
  }

  impl<T: Debug> Debug for Shared<T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
      Debug::fmt(&self.0, f)
    }
  }
}

#[cfg(test)]
mod tests {
  use crate::key_storage::tests::utils::expand_public_jwk;
  use crate::key_storage::tests::utils::generate_ed25519;
  use crypto::signatures::ed25519::PublicKey;
  use crypto::signatures::ed25519::Signature;
  use identity_verification::jose::jwk::EcCurve;
  use identity_verification::jose::jwk::JwkParamsEc;

  use super::*;

  #[tokio::test]
  async fn generate_and_sign() {
    let test_msg: &[u8] = b"test";
    let store: JwkMemStore = JwkMemStore::new();

    let JwkGenOutput { key_id, jwk } = store
      .generate(JwkMemStore::ED25519_KEY_TYPE, JwsAlgorithm::EdDSA)
      .await
      .unwrap();

    let signature = store.sign(&key_id, test_msg, &jwk.to_public().unwrap()).await.unwrap();

    let public_key: PublicKey = expand_public_jwk(&jwk);
    let signature: Signature = Signature::from_bytes(signature.try_into().unwrap());

    assert!(public_key.verify(&signature, test_msg));
    assert!(store.exists(&key_id).await.unwrap());
    store.delete(&key_id).await.unwrap();
  }

  #[tokio::test]
  async fn insert() {
    let store: JwkMemStore = JwkMemStore::new();

    let (private_key, public_key) = generate_ed25519();
    let mut jwk: Jwk = crate::key_storage::ed25519::encode_jwk(&private_key, &public_key);

    // INVALID: Inserting a Jwk without an `alg` parameter should fail.
    let err = store.insert(jwk.clone()).await.unwrap_err();
    assert!(matches!(err.kind(), KeyStorageErrorKind::UnsupportedSignatureAlgorithm));

    // VALID: Inserting a Jwk with all private key components set should succeed.
    jwk.set_alg(JwsAlgorithm::EdDSA.name());
    store.insert(jwk.clone()).await.unwrap();

    // INVALID: Inserting a Jwk with all private key components unset should fail.
    let err = store.insert(jwk.to_public().unwrap()).await.unwrap_err();
    assert!(matches!(err.kind(), KeyStorageErrorKind::Unspecified))
  }

  #[tokio::test]
  async fn exists() {
    let store: JwkMemStore = JwkMemStore::new();
    assert!(!store.exists(&KeyId::new("non-existent-id")).await.unwrap());
  }

  #[tokio::test]
  async fn incompatible_key_type() {
    let store: JwkMemStore = JwkMemStore::new();

    let mut ec_params = JwkParamsEc::new();
    ec_params.crv = EcCurve::P256.name().to_string();
    ec_params.x = String::new();
    ec_params.y = String::new();
    ec_params.d = Some(String::new());
    let jwk_ec = Jwk::from_params(ec_params);

    let err = store.insert(jwk_ec).await.unwrap_err();
    assert!(matches!(err.kind(), KeyStorageErrorKind::UnsupportedKeyType));
  }

  #[tokio::test]
  async fn incompatible_key_alg() {
    let store: JwkMemStore = JwkMemStore::new();

    let (private_key, public_key) = generate_ed25519();
    let mut jwk: Jwk = crate::key_storage::ed25519::encode_jwk(&private_key, &public_key);
    jwk.set_alg(JwsAlgorithm::ES256.name());

    // INVALID: Inserting an Ed25519 key with the ES256 alg is not compatible.
    let err = store.insert(jwk.clone()).await.unwrap_err();
    assert!(matches!(err.kind(), KeyStorageErrorKind::KeyAlgorithmMismatch));
  }
}