Skip to main content

bitwarden_fido/
crypto.rs

1use bitwarden_crypto_primitives::p256 as p256_primitives;
2use coset::{CoseKey, iana};
3use p256::{SecretKey, elliptic_curve::zeroize::Zeroizing};
4use passkey::authenticator::{CoseKeyPair, private_key_from_cose_key};
5use thiserror::Error;
6
7/// Length of a big-endian P-256 secret scalar.
8const P256_SCALAR_LEN: usize = 32;
9
10#[derive(Debug, Error)]
11pub enum CoseKeyToPkcs8Error {
12    #[error("Failed to extract private key from cose_key")]
13    FailedToExtractPrivateKeyFromCoseKey,
14    #[error("Failed to convert P256 private key to PKC8")]
15    FailedToConvertP256PrivateKeyToPkcs8,
16}
17
18pub(crate) fn cose_key_to_pkcs8(cose_key: &CoseKey) -> Result<Vec<u8>, CoseKeyToPkcs8Error> {
19    // cose_key.
20    let secret_key = private_key_from_cose_key(cose_key).map_err(|error| {
21        tracing::error!(?error, "Failed to extract private key from cose_key.");
22        CoseKeyToPkcs8Error::FailedToExtractPrivateKeyFromCoseKey
23    })?;
24
25    // passkey hands out a p256 0.13 key; cross into primitives via the raw scalar.
26    let scalar: Zeroizing<[u8; P256_SCALAR_LEN]> = Zeroizing::new(secret_key.to_bytes().into());
27    let vec = p256_primitives::SecretKey::from_bytes(&scalar)
28        .and_then(|key| key.to_pkcs8_der())
29        .map_err(|error| {
30            tracing::error!(%error, "Failed to convert P256 private key to PKC8.");
31            CoseKeyToPkcs8Error::FailedToConvertP256PrivateKeyToPkcs8
32        })?
33        .to_vec();
34
35    Ok(vec)
36}
37
38#[derive(Debug, Error)]
39#[error("Failed to extract private key from secret_key")]
40pub struct PrivateKeyFromSecretKeyError;
41
42pub fn pkcs8_to_cose_key(secret_key: &[u8]) -> Result<CoseKey, PrivateKeyFromSecretKeyError> {
43    let secret_key = p256_primitives::SecretKey::from_pkcs8_der(secret_key).map_err(|error| {
44        tracing::error!(%error, "Failed to extract private key from secret_key.");
45        PrivateKeyFromSecretKeyError
46    })?;
47
48    // passkey expects a p256 0.13 key; cross from primitives via the raw scalar.
49    let scalar = secret_key.to_bytes();
50    let secret_key = SecretKey::from_slice(scalar.as_slice()).map_err(|error| {
51        tracing::error!(%error, "Failed to convert P256 scalar to secret key.");
52        PrivateKeyFromSecretKeyError
53    })?;
54
55    let cose_key_pair = CoseKeyPair::from_secret_key(&secret_key, iana::Algorithm::ES256);
56    Ok(cose_key_pair.private)
57}
58
59#[cfg(test)]
60mod tests {
61    use super::*;
62
63    fn private_key_for_testing() -> CoseKey {
64        // Hardcoded CoseKey for testing purposes
65        let bytes = vec![
66            166, 1, 2, 3, 38, 32, 1, 33, 88, 32, 200, 30, 161, 146, 196, 121, 165, 149, 92, 232,
67            49, 48, 245, 253, 73, 234, 204, 3, 209, 153, 166, 77, 59, 232, 70, 16, 206, 77, 84,
68            156, 28, 77, 34, 88, 32, 82, 141, 165, 28, 241, 82, 31, 33, 183, 206, 29, 91, 93, 111,
69            216, 216, 26, 62, 211, 49, 191, 86, 238, 118, 241, 124, 131, 106, 214, 95, 170, 160,
70            35, 88, 32, 147, 171, 4, 49, 68, 170, 47, 51, 74, 211, 94, 40, 212, 244, 95, 55, 154,
71            92, 171, 241, 0, 55, 84, 151, 79, 244, 151, 198, 135, 45, 97, 238,
72        ];
73
74        <CoseKey as coset::CborSerializable>::from_slice(bytes.as_slice()).unwrap()
75    }
76
77    #[test]
78    fn test_cose_key_to_pkcs8_and_back() {
79        let cose_key = private_key_for_testing();
80
81        let pkcs8 = cose_key_to_pkcs8(&cose_key).expect("CoseKey to PKCS8 failed");
82        let cose_key2 = pkcs8_to_cose_key(&pkcs8).expect("PKCS8 to CoseKey failed");
83
84        assert_eq!(cose_key, cose_key2);
85    }
86
87    fn pkcs8_key_for_testing() -> Vec<u8> {
88        vec![
89            0x30, 0x81, 0x87, 0x02, 0x01, 0x00, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce,
90            0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x04,
91            0x6d, 0x30, 0x6b, 0x02, 0x01, 0x01, 0x04, 0x20, 0x06, 0x76, 0x5e, 0x85, 0xe0, 0x7f,
92            0xef, 0x43, 0xaa, 0x17, 0xe0, 0x7a, 0xd7, 0x85, 0x63, 0x01, 0x80, 0x70, 0x8c, 0x6c,
93            0x61, 0x43, 0x7d, 0xc3, 0xb1, 0xe6, 0xf9, 0x09, 0x24, 0xeb, 0x1f, 0xf5, 0xa1, 0x44,
94            0x03, 0x42, 0x00, 0x04, 0x35, 0x9a, 0x52, 0xf3, 0x82, 0x44, 0x66, 0x5f, 0x3f, 0xe2,
95            0xc4, 0x0b, 0x1c, 0x16, 0x34, 0xc5, 0x60, 0x07, 0x3a, 0x25, 0xfe, 0x7e, 0x7f, 0x7f,
96            0xda, 0xd4, 0x1c, 0x36, 0x90, 0x00, 0xee, 0xb1, 0x8e, 0x92, 0xb3, 0xac, 0x91, 0x7f,
97            0xb1, 0x8c, 0xa4, 0x85, 0xe7, 0x03, 0x07, 0xd1, 0xf5, 0x5b, 0xd3, 0x7b, 0xc3, 0x56,
98            0x11, 0xdf, 0xbc, 0x7a, 0x97, 0x70, 0x32, 0x4b, 0x3c, 0x84, 0x05, 0x71,
99        ]
100    }
101
102    #[test]
103    fn test_pkcs8_to_cose_key_and_back() {
104        let pkcs8 = pkcs8_key_for_testing();
105
106        let cose_key = pkcs8_to_cose_key(&pkcs8).expect("PKCS8 to CoseKey failed");
107        let pkcs8_2 = cose_key_to_pkcs8(&cose_key).expect("CoseKey to PKCS8 failed");
108
109        assert_eq!(pkcs8, pkcs8_2);
110    }
111}