identity-kit 0.1.0

DID, key management and verifiable credential primitives
Documentation
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
//! The behavioural suite every [`KeyStore`] must pass.
//!
//! The suite checks the contract of [`KeyStore`] against a live backend: that
//! [`capabilities`](KeyStore::capabilities) is truthful, that every advertised algorithm signs
//! and the signature verifies, that signatures are in their JWS form, and that the error
//! vocabulary (`NotFound`, `AlgorithmMismatch`, `Unsupported`, `AlreadyExists`, `InvalidSpec`)
//! is used as documented. It panics with a message naming the violated requirement.
//!
//! A backend that generates keys runs [`run`]; one whose keys are provisioned out of band (an
//! HSM, a KMS) runs [`run_with_keys`] with one key per advertised algorithm. Every run also
//! covers [`KeySigner`] and [`encode_compact`].
//!
//! Signatures are verified with [`DefaultVerifier`]; an algorithm it cannot verify in this build
//! (ES384, or ES256K and the RSA algorithms without their features) is only format-checked.
//!
//! # Examples
//!
//! ```
//! # #[cfg(feature = "in-memory")]
//! # tokio::runtime::Builder::new_current_thread().build()?.block_on(async {
//! use identity_kit::keys::{InMemoryKeyStore, testsuite};
//!
//! testsuite::run(&InMemoryKeyStore::new()).await;
//! # });
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```

use super::{KeyId, KeySigner, KeySpec, KeyStore, KeyStoreError};
use crate::jose::{
    CompactJws, DefaultVerifier, JwsAlgorithm, JwsHeader, JwsVerifier as _, KeyType, Signer as _,
    VerificationError, encode_compact,
};

/// Runs the suite against a store that generates keys.
///
/// One key per advertised algorithm is generated, checked as [`run_with_keys`] does, and deleted
/// when the store can delete. The generate and delete contract is checked too: specs whose
/// algorithm does not fit, labels, duplicate labels, unsupported key types.
///
/// # Panics
///
/// When the store does not advertise generation, or on the first violated requirement.
pub async fn run(store: &dyn KeyStore) {
    let capabilities = store.capabilities();
    assert!(
        capabilities.can_generate,
        "testsuite::run needs a store that generates keys; use run_with_keys for provisioned keys"
    );
    check_capabilities(store);

    let mut keys = Vec::new();
    for &alg in &capabilities.algorithms {
        let key_type = key_type_for(alg).unwrap_or_else(|| {
            panic!("{alg} is advertised but no KeyType of this crate generates it")
        });
        let spec = KeySpec::new(key_type, alg);
        match store.generate(spec).await {
            Ok(id) => keys.push((id, alg)),
            Err(e) => panic!("generate for advertised algorithm {alg} failed: {e}"),
        }
    }

    // A label becomes the key id, and a taken label is `AlreadyExists`.
    let (first_id, first_alg) = keys[0].clone();
    let label = format!(
        "identity-kit-testsuite-{}",
        first_alg.as_str().to_lowercase()
    );
    let labelled = store
        .generate(
            KeySpec::new(key_type_for(first_alg).expect("checked above"), first_alg)
                .with_label(&label),
        )
        .await
        .expect("generate with a label");
    assert_eq!(
        labelled.as_str(),
        label,
        "the key id of a labelled key is the label"
    );
    let duplicate = store
        .generate(
            KeySpec::new(key_type_for(first_alg).expect("checked above"), first_alg)
                .with_label(&label),
        )
        .await;
    assert!(
        matches!(duplicate, Err(KeyStoreError::AlreadyExists(ref id)) if id.as_str() == label),
        "generating a taken label must be AlreadyExists, got {duplicate:?}"
    );
    keys.push((labelled, first_alg));

    // A spec whose algorithm does not fit its key type is `InvalidSpec`.
    let wrong_type = match key_type_for(first_alg).expect("checked above") {
        KeyType::Ed25519 => KeyType::P256,
        _ => KeyType::Ed25519,
    };
    let invalid = store.generate(KeySpec::new(wrong_type, first_alg)).await;
    assert!(
        matches!(invalid, Err(KeyStoreError::InvalidSpec { .. })),
        "generating {first_alg} with a {wrong_type:?} key must be InvalidSpec, got {invalid:?}"
    );

    // An algorithm that is not advertised is refused.
    if let Some(alg) = JwsAlgorithm::ALL
        .iter()
        .copied()
        .find(|alg| !capabilities.supports(*alg) && key_type_for(*alg).is_some())
    {
        let key_type = key_type_for(alg).expect("filtered above");
        let refused = store.generate(KeySpec::new(key_type, alg)).await;
        assert!(
            matches!(
                refused,
                Err(KeyStoreError::UnsupportedAlgorithm(_) | KeyStoreError::UnsupportedKeyType(_))
            ),
            "generating unadvertised {alg} must be UnsupportedAlgorithm or UnsupportedKeyType, got {refused:?}"
        );
    }

    run_with_keys(store, &keys).await;

    if capabilities.can_delete {
        for (id, _) in &keys {
            store
                .delete(id)
                .await
                .unwrap_or_else(|e| panic!("delete of `{id}` failed: {e}"));
            assert!(
                !store.exists(id).await.expect("exists after delete"),
                "`{id}` still exists after delete"
            );
            let again = store.delete(id).await;
            assert!(
                matches!(again, Err(KeyStoreError::NotFound(ref missing)) if missing == id),
                "deleting `{id}` twice must be NotFound, got {again:?}"
            );
            let gone = store.public_jwk(id).await;
            assert!(
                matches!(gone, Err(KeyStoreError::NotFound(ref missing)) if missing == id),
                "public_jwk of deleted `{id}` must be NotFound, got {gone:?}"
            );
        }
    }
    let _ = first_id;
}

/// Runs the suite against provisioned keys: `keys` names one key per advertised algorithm.
///
/// Checks, for every key: `exists`, `public_jwk` (public only, `alg` consistent, key type
/// consistent with the algorithm), `sign` (JWS signature form, verifies with
/// [`DefaultVerifier`], `AlgorithmMismatch` for any other algorithm), and the JWS round trip
/// through [`KeySigner`]. Checks `NotFound` for an unknown id, and that `generate` and `delete`
/// are `Unsupported` when not advertised.
///
/// # Panics
///
/// When an advertised algorithm has no key in `keys`, or on the first violated requirement.
pub async fn run_with_keys(store: &dyn KeyStore, keys: &[(KeyId, JwsAlgorithm)]) {
    let capabilities = check_capabilities(store);
    for &alg in &capabilities.algorithms {
        assert!(
            keys.iter().any(|(_, key_alg)| *key_alg == alg),
            "no key for advertised algorithm {alg}"
        );
    }

    for (id, alg) in keys {
        assert!(
            capabilities.supports(*alg),
            "key `{id}` signs with {alg}, which the store does not advertise"
        );
        check_key(store, id, *alg).await;
    }

    check_unknown_key(store).await;

    if !capabilities.can_generate {
        let refused = store
            .generate(KeySpec::new(KeyType::Ed25519, JwsAlgorithm::EdDSA))
            .await;
        assert!(
            matches!(refused, Err(KeyStoreError::Unsupported("generate"))),
            "a store that does not advertise generation must return Unsupported(\"generate\"), got {refused:?}"
        );
    }
    if !capabilities.can_delete {
        let (id, _) = &keys[0];
        let refused = store.delete(id).await;
        assert!(
            matches!(refused, Err(KeyStoreError::Unsupported("delete"))),
            "a store that does not advertise deletion must return Unsupported(\"delete\"), got {refused:?}"
        );
        assert!(
            store.exists(id).await.expect("exists"),
            "`{id}` vanished after a refused delete"
        );
    }
}

fn check_capabilities(store: &dyn KeyStore) -> super::KeyStoreCapabilities {
    let capabilities = store.capabilities();
    assert!(
        !capabilities.algorithms.is_empty(),
        "a key store must advertise at least one algorithm"
    );
    for (i, alg) in capabilities.algorithms.iter().enumerate() {
        assert!(
            !capabilities.algorithms[..i].contains(alg),
            "{alg} is advertised twice"
        );
    }
    assert_eq!(
        store.capabilities(),
        capabilities,
        "capabilities must be stable between calls"
    );
    capabilities
}

async fn check_key(store: &dyn KeyStore, id: &KeyId, alg: JwsAlgorithm) {
    assert!(
        store
            .exists(id)
            .await
            .unwrap_or_else(|e| panic!("exists for `{id}` failed: {e}")),
        "`{id}` was provisioned or generated but exists is false"
    );

    let public = store
        .public_jwk(id)
        .await
        .unwrap_or_else(|e| panic!("public_jwk for `{id}` failed: {e}"));
    assert!(
        !public.is_private(),
        "public_jwk for `{id}` carries private parameters"
    );
    if let Some(key_alg) = public.alg() {
        assert_eq!(
            key_alg,
            alg.as_str(),
            "public_jwk for `{id}` names `alg` {key_alg}, but the key signs with {alg}"
        );
    }
    let (kty, crv) = expected_key_type(alg);
    assert_eq!(
        (public.kty(), public.crv()),
        (kty, crv),
        "public_jwk for `{id}` ({alg}) has the wrong key type"
    );
    assert!(
        public.thumbprint_sha256().is_ok(),
        "public_jwk for `{id}` lacks the members its key type requires"
    );

    let data = b"identity-kit key store test suite";
    let signature = store
        .sign(id, alg, data)
        .await
        .unwrap_or_else(|e| panic!("sign with `{id}` ({alg}) failed: {e}"));
    check_signature_form(alg, &signature, &public, id);
    verify(alg, data, &signature, &public, id);

    // Signing twice must give two valid signatures (equal or not; RFC 6979 and random nonces
    // are both fine).
    let second = store
        .sign(id, alg, data)
        .await
        .unwrap_or_else(|e| panic!("second sign with `{id}` failed: {e}"));
    verify(alg, data, &second, &public, id);

    // Any other algorithm is a mismatch, whether or not the store supports it.
    for &other in JwsAlgorithm::ALL {
        if other == alg {
            continue;
        }
        let refused = store.sign(id, other, data).await;
        assert!(
            matches!(
                refused,
                Err(KeyStoreError::AlgorithmMismatch { ref key, key_alg, requested })
                    if key == id && key_alg == alg && requested == other
            ),
            "signing with `{id}` ({alg}) under {other} must be AlgorithmMismatch, got {refused:?}"
        );
    }

    // The JWS round trip through `KeySigner`.
    let signer = KeySigner::new(store, id.clone(), alg)
        .await
        .unwrap_or_else(|e| panic!("KeySigner::new for `{id}` failed: {e}"));
    assert_eq!(signer.kid(), Some(id.as_str()));
    assert_eq!(signer.public_jwk(), &public);
    let header = JwsHeader::new(alg).with_kid(id.as_str());
    let token = encode_compact(&header, b"{\"iss\":\"did:example:123\"}", &signer)
        .await
        .unwrap_or_else(|e| panic!("encode_compact with `{id}` failed: {e}"));
    let jws = CompactJws::decode(&token).expect("the encoded JWS decodes");
    assert_eq!(jws.kid(), Some(id.as_str()));
    check_signature_form(alg, jws.signature(), &public, id);
    verify(alg, jws.signing_input(), jws.signature(), &public, id);

    for &other in JwsAlgorithm::ALL {
        if other == alg {
            continue;
        }
        let mismatch = KeySigner::new(store, id.clone(), other).await;
        assert!(
            matches!(
                mismatch,
                Err(KeyStoreError::AlgorithmMismatch { .. }
                    | KeyStoreError::UnsupportedAlgorithm(_))
            ),
            "KeySigner::new for `{id}` ({alg}) under {other} must fail, got {mismatch:?}"
        );
    }
}

async fn check_unknown_key(store: &dyn KeyStore) {
    let unknown = KeyId::new("identity-kit-testsuite-no-such-key");
    assert!(
        !store
            .exists(&unknown)
            .await
            .expect("exists for an unknown key is Ok(false), not an error"),
        "exists reports an unknown key as present"
    );
    let public = store.public_jwk(&unknown).await;
    assert!(
        matches!(public, Err(KeyStoreError::NotFound(ref id)) if *id == unknown),
        "public_jwk of an unknown key must be NotFound, got {public:?}"
    );
    let alg = store.capabilities().algorithms[0];
    let signature = store.sign(&unknown, alg, b"data").await;
    assert!(
        matches!(signature, Err(KeyStoreError::NotFound(ref id)) if *id == unknown),
        "sign with an unknown key must be NotFound, got {signature:?}"
    );
    let signer = KeySigner::new(store, unknown.clone(), alg).await;
    assert!(
        matches!(signer, Err(KeyStoreError::NotFound(ref id)) if *id == unknown),
        "KeySigner::new for an unknown key must be NotFound, got {signer:?}"
    );
    if store.capabilities().can_delete {
        let deleted = store.delete(&unknown).await;
        assert!(
            matches!(deleted, Err(KeyStoreError::NotFound(ref id)) if *id == unknown),
            "delete of an unknown key must be NotFound, got {deleted:?}"
        );
    }
}

/// The JWS form of a signature under `alg`: 64 bytes for EdDSA, raw `r || s` of twice the
/// field size for ECDSA, the modulus size for RSA.
fn check_signature_form(
    alg: JwsAlgorithm,
    signature: &[u8],
    public: &crate::jose::Jwk,
    id: &KeyId,
) {
    let expected = match alg {
        JwsAlgorithm::EdDSA | JwsAlgorithm::ES256 | JwsAlgorithm::ES256K => Some(64),
        JwsAlgorithm::ES384 => Some(96),
        JwsAlgorithm::RS256 | JwsAlgorithm::PS256 => {
            public.rsa_public_components().ok().map(|(n, _)| n.len())
        }
    };
    if let Some(expected) = expected {
        assert_eq!(
            signature.len(),
            expected,
            "signature of `{id}` ({alg}) must be {expected} bytes (the JWS form, not DER or PEM)"
        );
    }
    if alg.is_ecdsa() {
        assert!(
            !(signature.first() == Some(&0x30) && signature.len() != 64 && signature.len() != 96),
            "signature of `{id}` ({alg}) looks DER-encoded; ECDSA signatures must be raw `r || s`"
        );
    }
}

fn verify(alg: JwsAlgorithm, data: &[u8], signature: &[u8], public: &crate::jose::Jwk, id: &KeyId) {
    match DefaultVerifier.verify(alg, data, signature, public) {
        Ok(()) | Err(VerificationError::UnsupportedAlgorithm(_)) => {}
        Err(e) => panic!("signature of `{id}` ({alg}) does not verify with its public JWK: {e}"),
    }
}

/// The key type that signs with `alg`, for generation; `None` for ES384, which no [`KeyType`]
/// of this crate produces, and for the RSA algorithms, where the 2048-bit key stands in.
fn key_type_for(alg: JwsAlgorithm) -> Option<KeyType> {
    match alg {
        JwsAlgorithm::EdDSA => Some(KeyType::Ed25519),
        JwsAlgorithm::ES256 => Some(KeyType::P256),
        JwsAlgorithm::ES256K => Some(KeyType::Secp256k1),
        JwsAlgorithm::RS256 | JwsAlgorithm::PS256 => Some(KeyType::Rsa2048),
        JwsAlgorithm::ES384 => None,
    }
}

/// The `kty` and `crv` of a public JWK for `alg`.
fn expected_key_type(alg: JwsAlgorithm) -> (&'static str, Option<&'static str>) {
    match alg {
        JwsAlgorithm::EdDSA => ("OKP", Some("Ed25519")),
        JwsAlgorithm::ES256 => ("EC", Some("P-256")),
        JwsAlgorithm::ES256K => ("EC", Some("secp256k1")),
        JwsAlgorithm::ES384 => ("EC", Some("P-384")),
        JwsAlgorithm::RS256 | JwsAlgorithm::PS256 => ("RSA", None),
    }
}