siguldry 0.7.1

A signing server and client.
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
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
// SPDX-License-Identifier: MIT
// Copyright (c) Microsoft Corporation.

//! Functions for encrypting and decrypting key passwords.
//!
//! Keys stored in the database are encrypted using a password generated by Siguldry, and PKCS #11
//! modules are protected by a PIN. Both of these secrets are then encrypted using a combination of
//! user-provided secrets ("user passwords") and keys stored in PKCS#11 modules on the server
//! (examples include a Yubikey or TPM, but anything with a PKCS#11 module will work).
//!
//! This implementation was based of the one in the Sigul project.

use std::{
    io::{Read, Write},
    process::Stdio,
};

use anyhow::Context;
use openssl::pkey::PKey;
use openssl::{
    cms::{CMSOptions, CmsContentInfo},
    hash::MessageDigest,
    stack::Stack,
    symm::Cipher,
    x509::X509,
};
use sequoia_openpgp::{
    crypto::Password,
    parse::{
        Parse,
        stream::{DecryptionHelper, DecryptorBuilder, VerificationHelper},
    },
    policy::StandardPolicy,
    serialize::stream::{Armorer, Encryptor, LiteralWriter, Message},
    types::{AEADAlgorithm, SymmetricAlgorithm},
};
use serde::{Deserialize, Serialize};

use crate::server::{config::Pkcs11Binding, db};

/// The intermediate data format for secrets.
///
/// This structure is serialized and, depending on what type of secret it is, futher encrypted.
/// Private keys which have been encrypted with a server-generated secret can be bound with
/// PKCS11 tokens before being serialized and written to the database.
///
/// The key password used to encrypt the private key can be bound with PKCS11 tokens, then encrypted
/// with the user-provided password before being written to the database.
///
/// The serialized JSON looks like:
///
/// `{"None": {"secret": "my-password"}}`
///
/// or
///
/// `{"Pkcs11WithCMS": {"fingerprint": "hexencodedsha256sum", "secret": "-----BEGIN CMS-----..." `
#[derive(Debug, Clone, Serialize, Deserialize)]
enum BoundSecret {
    /// No binding was used.
    None { secret: String },
    /// Secrets bound by asymmetric keys stored in a device accessible via PKCS#11.
    ///
    /// Secrets of this variant have been encrypted using OpenSSL's CMS interface and the
    /// results are PEM-encoded.
    ///
    /// Examples of key stores include SoftHSMv2 or any HSM that provides a PKCS#11 interface,
    /// Yubikeys via the libykcs11 library, and Trusted Platform Modules (TPMs) via the
    /// libtpm2_pkcs11 library. Creating and managing the key pairs is up to the administrator.
    Pkcs11WithCMS {
        /// The SHA256 digest of the DER-encoded X509 certificate used in this binding.
        fingerprint: String,
        /// The secret that's been encrypted by the X509 certificate identified in
        /// `key_fingerprint`. The string contains a PEM-encoded CMS structure.
        secret: String,
    },
}

impl BoundSecret {
    /// Encrypt some data using a [`Binding`] configuration.
    ///
    /// The data is typically the password used to encrypt a private key, or the private key itself.
    ///
    /// The data is encrypted with the X509 certificate in the provided binding to a CMS (RFC 5652)
    /// structure which is PEM-encoded. AES-256-GCM is used for the cipher.
    ///
    /// If there are no bindings configured, the secret is returned, wrapped in a structure suitable
    /// to be serialized and encrypted with a user-provided password.
    pub(crate) fn bind(bindings: &[Pkcs11Binding], secret: &[u8]) -> anyhow::Result<Vec<Self>> {
        let secret_str = str::from_utf8(secret)
            .map_err(|_| anyhow::anyhow!("Secrets that are bound must be encoded with UTF-8"))?;
        let mut bound_secrets = bindings
            .iter()
            .map(|binding| {
                tracing::info!(certificate=?binding.certificate, "binding secret");

                let certificate = std::fs::read_to_string(&binding.certificate)?;
                let certificate = X509::from_pem(certificate.as_bytes())?;
                let mut cert_stack = Stack::new()?;
                cert_stack.push(certificate)?;
                let encrypted = CmsContentInfo::encrypt(
                    &cert_stack,
                    secret,
                    Cipher::aes_256_gcm(),
                    CMSOptions::empty(),
                )?;
                let pem = encrypted.to_pem()?;
                let certificate = cert_stack.pop().expect("we just pushed a cert");
                let fingerprint = hex::encode_upper(certificate.digest(MessageDigest::sha256())?);
                Ok::<_, anyhow::Error>(BoundSecret::Pkcs11WithCMS {
                    fingerprint,
                    secret: String::from_utf8(pem)?,
                })
            })
            .collect::<Result<Vec<_>, _>>()?;

        if bound_secrets.is_empty() {
            tracing::trace!("binding isn't enabled for this secret");
            bound_secrets.push(BoundSecret::None {
                secret: secret_str.to_string(),
            });
        }

        tracing::debug!(
            tokens_bound = bindings.len(),
            "secret has been successfully bound"
        );

        Ok(bound_secrets)
    }

    pub(crate) fn unbind(&self, bindings: &[Pkcs11Binding]) -> anyhow::Result<String> {
        match self {
            BoundSecret::None { secret } => return Ok(secret.clone()),
            BoundSecret::Pkcs11WithCMS {
                fingerprint,
                secret,
            } => {
                for binding in bindings.iter().filter(|binding| binding.can_unbind()) {
                    if let Ok(secret) =
                        binding_decrypt(binding.clone(), secret.clone().into_bytes())
                            .map(String::from_utf8)
                    {
                        let secret = secret.map_err(|e| {
                            anyhow::anyhow!("Unbound key material is not valid UTF-8: {e}")
                        })?;
                        return Ok(secret);
                    } else {
                        tracing::debug!(
                            certificate = fingerprint,
                            key_uri = binding.private_key,
                            "Failed to unbind key password"
                        );
                    }
                }
            }
        }

        Err(anyhow::anyhow!("Unable to unbind key password"))
    }
}

/// Decrypt a bound password using a PIN-protected private key in a PKCS11 token.
fn binding_decrypt(binding: Pkcs11Binding, data: Vec<u8>) -> anyhow::Result<Vec<u8>> {
    let private_key = binding.private_key.as_ref().ok_or_else(|| {
        anyhow::anyhow!(
            "Binding configuration is missing the 'private_key' field and can't be used to decrypt"
        )
    })?;
    // In the future maybe we'll get openssl provider APIs? Alternatively, if Sequioa gets
    // PKCS#11 support, we could switch to using it in a migration.
    let mut command = std::process::Command::new("openssl");
    let mut child = command
        .args([
            "cms",
            "-decrypt",
            "-inform",
            "pem",
            "-provider",
            "pkcs11",
            "-passin",
            "stdin",
            "-inkey",
        ])
        .arg(private_key)
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()?;
    let mut stdin = child
        .stdin
        .take()
        .ok_or_else(|| anyhow::anyhow!("openssl-cms command missing stdin"))?;

    binding
        .pin
        .ok_or_else(|| anyhow::anyhow!("Binding must include a PIN"))?
        .map(|pin| stdin.write_all(pin))?;
    stdin.write_all(b"\n")?;
    stdin.write_all(&data)?;
    drop(stdin);

    let output = child.wait_with_output()?;
    let stderr = String::from_utf8_lossy(&output.stderr);
    if !output.status.success() {
        return Err(anyhow::anyhow!(
            "Failed to decrypt data via PKCS#11 using openssl-cms (exited {:?}): {stderr}",
            output.status.code()
        ));
    }

    Ok(output.stdout)
}

/// Decrypt a key password to enable access to the key itself.
pub async fn decrypt_key_password(
    bindings: &[Pkcs11Binding],
    user_password: Password,
    data: &[u8],
) -> anyhow::Result<Password> {
    let key_bindings: Vec<BoundSecret> = serde_json::from_slice(data)
        .map_err(|_e| anyhow::anyhow!("JSON content in database could not be deserialized!"))?;
    let user_encrypted_key_password = key_bindings
        .iter()
        .map(|s| s.unbind(bindings).ok())
        .find(|result| result.is_some())
        .flatten()
        .ok_or_else(|| anyhow::anyhow!("Unable to unbind key password"))?;

    let key_password = symmetric_decrypt(user_password, user_encrypted_key_password.as_bytes())
        .map(Password::from)
        .context("User password is invalid")?;

    Ok(key_password)
}

/// Encrypt a key password for storage.
pub fn encrypt_key_password(
    bindings: &[Pkcs11Binding],
    user_password: Password,
    key_password: Password,
) -> anyhow::Result<Vec<u8>> {
    let encrypted_key_password = key_password
        .map(|key_password| symmetric_encrypt(user_password.clone(), key_password))
        .context("Failed to PGP-encrypt the key password")?;
    let bound_passwords = BoundSecret::bind(bindings, &encrypted_key_password)?;

    tracing::debug!(
        tokens_bound = bindings.len(),
        "Key password has been successfully bound"
    );
    Ok(serde_json::to_vec(&bound_passwords)?)
}

/// Bind a string with the provided [`Pkcs11Binding`] and serialize it to JSON for database storage.
///
/// If there are no bindings (e.g. an empty slice) the string is wrapped in [`BoundSecret::None`].
pub(crate) fn bind_with_pkcs11(bindings: &[Pkcs11Binding], secret: &str) -> anyhow::Result<String> {
    let bound = BoundSecret::bind(bindings, secret.as_bytes())?;
    serde_json::to_string(&bound).context("Failed to serialize bound secret")
}

/// Unbind key material to recover the encrypted PEM string.
///
/// This is the inverse of [`bind_key_material`].
pub(crate) fn unbind_with_pkcs11(
    bindings: &[Pkcs11Binding],
    bound_secret: &str,
) -> anyhow::Result<String> {
    let bound_secrets: Vec<BoundSecret> =
        serde_json::from_str(bound_secret).context("Failed to deserialize bound key material")?;
    bound_secrets
        .iter()
        .find_map(|s| s.unbind(bindings).ok())
        .ok_or_else(|| anyhow::anyhow!("Unable to unbind key material"))
}

/// Decrypt a soft key's private key material.
pub(crate) async fn decrypt_private_key(
    key: &db::Key,
    encrypted_password: &[u8],
    bindings: &[Pkcs11Binding],
    user_password: Password,
) -> anyhow::Result<PKey<openssl::pkey::Private>> {
    let key_material = if let Some(material) = &key.key_material {
        material
    } else {
        return Err(anyhow::anyhow!(
            "Can't decrypt private key for a PKCS#11 token"
        ));
    };
    let key_password = decrypt_key_password(bindings, user_password, encrypted_password).await?;
    let encrypted_pem = unbind_with_pkcs11(bindings, key_material)?;
    Ok(key_password.map(|passphrase| {
        PKey::private_key_from_pem_passphrase(encrypted_pem.as_bytes(), passphrase)
    })?)
}

/// Implement a helper for unsigned, symmetrically encrypted data for Sequoia.
struct SymmetricHelper {
    password: Password,
}

// Decrypt exclusively via symmetrically encrypted session keys.
impl DecryptionHelper for SymmetricHelper {
    fn decrypt(
        &mut self,
        _pkesks: &[sequoia_openpgp::packet::PKESK],
        symmetric_session_keys: &[sequoia_openpgp::packet::SKESK],
        _sym_algo: Option<sequoia_openpgp::types::SymmetricAlgorithm>,
        decrypt: &mut dyn FnMut(
            Option<sequoia_openpgp::types::SymmetricAlgorithm>,
            &sequoia_openpgp::crypto::SessionKey,
        ) -> bool,
    ) -> sequoia_openpgp::Result<Option<sequoia_openpgp::Cert>> {
        for session_key in symmetric_session_keys {
            if session_key
                .decrypt(&self.password)
                .is_ok_and(|(algorithm, session_key)| decrypt(algorithm, &session_key))
            {
                return Ok(None);
            }
        }
        Err(anyhow::anyhow!("Bad passphrase"))
    }
}

// A no-op verification helper implementation since the data is not expected to be signed.
impl VerificationHelper for SymmetricHelper {
    fn get_certs(
        &mut self,
        _ids: &[sequoia_openpgp::KeyHandle],
    ) -> sequoia_openpgp::Result<Vec<sequoia_openpgp::Cert>> {
        Ok(vec![])
    }

    fn check(
        &mut self,
        _structure: sequoia_openpgp::parse::stream::MessageStructure<'_>,
    ) -> sequoia_openpgp::Result<()> {
        Ok(())
    }
}

/// Encrypts some data with the given [`Password`] using OpenPGP.
///
/// Returns the ASCII-armored, encrypted `key_passphrase`.
fn symmetric_encrypt(password: Password, data: &[u8]) -> anyhow::Result<Vec<u8>> {
    let mut buffer = vec![];
    {
        let message = Armorer::new(Message::new(&mut buffer)).build()?;
        let encryptor = Encryptor::with_passwords(message, Some(password))
            .symmetric_algo(SymmetricAlgorithm::AES256)
            .aead_algo(AEADAlgorithm::GCM)
            .build()?;
        let mut message = LiteralWriter::new(encryptor).build()?;
        message.write_all(data)?;
        message.finalize()?;
    }

    Ok(buffer)
}

/// Decrypt data using GPG.
///
/// This is the inverse of [`symmetric_encrypt`]. Data is not expected to be signed and signatures are not checked.
fn symmetric_decrypt(password: Password, data: &[u8]) -> anyhow::Result<Vec<u8>> {
    let policy = StandardPolicy::new();
    let helper = SymmetricHelper { password };
    let mut decryptor = DecryptorBuilder::from_bytes(&data)?.with_policy(&policy, None, helper)?;
    let mut user_passphrase = vec![];
    decryptor.read_to_end(&mut user_passphrase)?;

    Ok(user_passphrase)
}

/// This module contains functions for working with Sigul-bound keys.
///
/// It should only be used when accessing a Sigul database for the purpose of
/// migrating it to Siguldry.
pub mod sigul {
    use super::*;

    /// The format, serialized as JSON, that Sigul uses to store secrets bound with PKCS11.
    #[derive(Debug, Clone, Serialize, Deserialize)]
    #[allow(dead_code)]
    struct SigulPkcs11BoundPassword {
        method: String,
        /// The encrypted password (or, if nesting is being used, the encrypted output of some other
        /// binding).
        ///
        /// Sigul used openssl-smime to encrypt the password with the X509 certificate for a private
        /// key accessible via PKCS#11.
        value: String,
        /// A string to identify the token; we don't bother using this and just try to decrypt with
        /// any tokens we have.
        token: String,
    }

    // Shell out to pgp to decrypt the key password.
    //
    // This is done rather than use Sequioa because the OpenPGP data structure written by
    // sigul is considered malformed to Sequoia.
    fn gpg_symmetric_decrypt(password: Password, data: &[u8]) -> anyhow::Result<Vec<u8>> {
        let mut child = std::process::Command::new("gpg")
            .args([
                "--batch",
                "--yes",
                "--quiet",
                "--decrypt",
                "--passphrase-fd",
                "0",
            ])
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
            .context("Failed to spawn pgp process")?;

        let mut stdin = child
            .stdin
            .take()
            .ok_or_else(|| anyhow::anyhow!("Failed to open pgp stdin"))?;
        password.map(|p| {
            stdin.write_all(p)?;
            stdin.write_all(b"\n")?;
            stdin.write_all(data)
        })?;
        drop(stdin);

        let output = child
            .wait_with_output()
            .context("Failed to wait for pgp process")?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            return Err(anyhow::anyhow!(
                "gpg decryption failed (exit code {:?}): {}",
                output.status.code(),
                stderr
            ));
        }

        Ok(output.stdout)
    }

    // This is based off the bind_passphrase/unbind_passphrase implementations in the utils.py module
    // of Sigul.
    //
    // While Sigul supports several binding methods (such at TPM 1.2), we only handle the PKCS#11 case
    // as that's what is used in Fedora's production deployment.
    pub async fn unbind_key_password(
        user_password: Password,
        encrypted_passphrase: &[u8],
        sigul_binding: &Option<Pkcs11Binding>,
    ) -> anyhow::Result<Password> {
        let bound_key_password = gpg_symmetric_decrypt(user_password, encrypted_passphrase)
            .context("Decryption via the user password failed")?;
        tracing::debug!("User password used to decrypt key access; unbinding...");
        let mut bound_key_password = String::from_utf8(bound_key_password)
            .map_err(|_| anyhow::anyhow!("Passwords are expected to be UTF-8"))?;

        let key_password = loop {
            match bound_key_password.as_str() {
                bound_object if bound_key_password.starts_with("{") => {
                    // Sigul changed its format from being a single object to a list of those objects.
                    if let Some(sigul_binding) = sigul_binding.as_ref() {
                        let pkcs11_binding: SigulPkcs11BoundPassword =
                            serde_json::from_str(bound_object)
                                .context("Key is bound with an unsupported method")?;
                        let inner = super::binding_decrypt(
                            sigul_binding.clone(),
                            pkcs11_binding.value.as_bytes().to_vec(),
                        )?;
                        bound_key_password = String::from_utf8(inner)?;
                    } else {
                        return Err(anyhow::anyhow!(
                            "Sigul key is hardware-bound but no PKCS#11 unbinding key was provided"
                        ));
                    }
                }
                bound_list if bound_key_password.starts_with("[") => {
                    if let Some(sigul_binding) = sigul_binding.as_ref() {
                        let binding_entries: Vec<serde_json::Value> =
                            serde_json::from_str(bound_list)?;
                        for binding in binding_entries {
                            if let Ok(pkcs11_binding) =
                                serde_json::from_value::<SigulPkcs11BoundPassword>(binding)
                            {
                                let inner = super::binding_decrypt(
                                    sigul_binding.clone(),
                                    pkcs11_binding.value.as_bytes().to_vec(),
                                );
                                if let Ok(inner) = inner {
                                    bound_key_password = String::from_utf8(inner)?;
                                    break;
                                } else {
                                    tracing::debug!(
                                        binding_token = pkcs11_binding.token,
                                        "Failed to decrypt the password"
                                    );
                                }
                            } else {
                                tracing::debug!("Unknown binding format found, skipping");
                            }
                        }
                    } else {
                        return Err(anyhow::anyhow!(
                            "Sigul key is hardware-bound but no PKCS#11 unbinding key was provided"
                        ));
                    }
                }
                unbound => break Ok::<_, anyhow::Error>(unbound),
            };
        }?;

        tracing::debug!("Key password successfully unbound");
        Ok(key_password.into())
    }
}

#[cfg(test)]
mod tests {

    use anyhow::Result;
    use tempfile::NamedTempFile;

    use super::*;
    use crate::server::crypto::test_utils::setup_hsm;

    // Encrypting and then decrypting should give us the key back
    #[test]
    fn encrypt_decrypt() -> Result<()> {
        let user_passphrase = Password::from("this grants a user access to the key passphrase");
        let data = "this encrypts the private key";
        let encrypted_data = symmetric_encrypt(user_passphrase.clone(), data.as_bytes())?;
        let decrypted_data = symmetric_decrypt(user_passphrase, &encrypted_data)?;
        assert_eq!(data.as_bytes(), decrypted_data);
        Ok(())
    }

    // Ensure something encrypted with sq's CLI is decrypted by our implementation
    #[test]
    fn encrypt_with_sq_decrypt() -> Result<()> {
        let user_passphrase = "this grants a user access to the key passphrase".to_string();
        let data = "this encrypts the private key";
        let mut password_file = NamedTempFile::new()?;
        let mut message = NamedTempFile::new()?;
        password_file.write_all(user_passphrase.as_bytes())?;
        message.write_all(data.as_bytes())?;

        let mut command = std::process::Command::new("sq");
        let result = command
            .arg("encrypt")
            .arg(format!(
                "--with-password-file={}",
                password_file.path().display()
            ))
            .arg("--without-signature")
            .arg(message.path())
            .output()?;

        let retrieved_key_passphrase =
            symmetric_decrypt(Password::from(user_passphrase), &result.stdout)?;
        assert_eq!(data.as_bytes(), retrieved_key_passphrase);
        Ok(())
    }

    // Ensure something encrypted with our implementation is decryptable by sq's CLI
    #[test]
    fn encrypt_decrypt_with_sq() -> Result<()> {
        let user_passphrase = "this grants a user access to the key passphrase".to_string();
        let data = "this encrypts the private key";
        let encrypted_passphrase =
            symmetric_encrypt(Password::from(user_passphrase.as_bytes()), data.as_bytes())?;
        let mut password_file = NamedTempFile::new()?;
        let mut encrypted_message = NamedTempFile::new()?;
        password_file.write_all(user_passphrase.as_bytes())?;
        encrypted_message.write_all(&encrypted_passphrase)?;

        let mut command = std::process::Command::new("sq");
        let result = command
            .arg(format!(
                "--password-file={}",
                password_file.path().display()
            ))
            .arg("decrypt")
            .arg(encrypted_message.path())
            .output()?;
        assert_eq!(data.as_bytes(), result.stdout);

        Ok(())
    }

    /// Assert encrypting and then decrypting for bindings works.
    #[tokio::test]
    async fn encrypt_decrypt_binding() -> Result<()> {
        let softhsm = setup_hsm()?;

        let bound_secrets = BoundSecret::bind(&softhsm.bindings, b"some data")?;
        let bound_password = bound_secrets.first().unwrap();

        assert!(
            matches!(
                &bound_password,
                &BoundSecret::Pkcs11WithCMS {
                    fingerprint: _,
                    secret: _
                }
            ),
            "Expected PKCS11 binding"
        );
        let decrypted_data = bound_password.unbind(&softhsm.bindings)?;
        assert_eq!("some data", decrypted_data);

        Ok(())
    }

    /// Assert the complete encryption/decryption process roundtrips as expected.
    #[tokio::test]
    async fn encrypt_decrypt_key_password() -> Result<()> {
        let softhsm = setup_hsm()?;

        let key_password = Password::from("a secret that never leaves the server");
        let user_password = Password::from("some long password clients provide");
        let blob = encrypt_key_password(
            &softhsm.bindings,
            user_password.clone(),
            key_password.clone(),
        )?;
        let roundtrip_key_password =
            decrypt_key_password(&softhsm.bindings, user_password, &blob).await?;

        assert_eq!(key_password, roundtrip_key_password);

        Ok(())
    }

    // Assert if no bindings include keys, we get an error
    #[tokio::test]
    async fn encrypt_decrypt_key_password_binding_no_key() -> Result<()> {
        let softhsm = setup_hsm()?;

        let key_password = Password::from("a secret that never leaves the server");
        let user_password = Password::from("some long password clients provide");
        let blob = encrypt_key_password(
            &softhsm.bindings,
            user_password.clone(),
            key_password.clone(),
        )?;
        let result =
            decrypt_key_password(softhsm.bindings.get(1..).unwrap(), user_password, &blob).await;
        assert!(result.is_err_and(|err| err.to_string().contains("Unable to unbind key password")));

        Ok(())
    }

    // We should get an error if the user password is incorrect
    #[tokio::test]
    async fn encrypt_decrypt_key_password_wrong_user_password() -> Result<()> {
        let softhsm = setup_hsm()?;

        let key_password = Password::from("a secret that never leaves the server");
        let user_password = Password::from("some long password clients provide");
        let blob = encrypt_key_password(&[], user_password, key_password.clone())?;
        let user_password = Password::from("the wrong password");
        let result =
            decrypt_key_password(softhsm.bindings.get(1..).unwrap(), user_password, &blob).await;
        let err_str = result.map_err(|e| e.to_string()).err().unwrap();
        assert_eq!(err_str, "User password is invalid");

        Ok(())
    }

    // Assert if no bindings are configured, just the user password is sufficient.
    #[tokio::test]
    async fn encrypt_decrypt_key_password_no_bindings() -> Result<()> {
        let key_password = Password::from("a secret that never leaves the server");
        let user_password = Password::from("some long password clients provide");
        let blob = encrypt_key_password(&[], user_password.clone(), key_password.clone())?;
        let roundtrip_key_password = decrypt_key_password(&[], user_password, &blob).await?;
        assert_eq!(key_password, roundtrip_key_password);
        Ok(())
    }
}