Skip to main content

webpki/
rpk_entity.rs

1use pki_types::{SignatureVerificationAlgorithm, SubjectPublicKeyInfoDer};
2
3use crate::error::Error;
4use crate::signed_data::SubjectPublicKeyInfo;
5use crate::{DerTypeId, der, signed_data};
6
7/// A Raw Public Key, used for connections using raw public keys as specified
8/// in [RFC 7250](https://www.rfc-editor.org/rfc/rfc7250).
9#[derive(Debug)]
10pub struct RawPublicKeyEntity<'a> {
11    inner: untrusted::Input<'a>,
12}
13
14impl<'a> TryFrom<&'a SubjectPublicKeyInfoDer<'a>> for RawPublicKeyEntity<'a> {
15    type Error = Error;
16
17    /// Parse the ASN.1 DER-encoded SPKI encoding of the raw public key `spki`.
18    /// Since we are parsing a raw public key, we first strip the outer sequence tag.
19    fn try_from(spki: &'a SubjectPublicKeyInfoDer<'a>) -> Result<Self, Self::Error> {
20        let input = untrusted::Input::from(spki.as_ref());
21        let spki = input.read_all(
22            Error::TrailingData(DerTypeId::SubjectPublicKeyInfo),
23            |reader| {
24                let untagged_spki = der::expect_tag(reader, der::Tag::Sequence)?;
25                der::read_all::<SubjectPublicKeyInfo<'_>>(untagged_spki)?;
26                Ok(untagged_spki)
27            },
28        )?;
29        Ok(Self { inner: spki })
30    }
31}
32
33impl RawPublicKeyEntity<'_> {
34    /// Verifies the signature `signature` of message `msg` using a raw public key,
35    /// supporting RFC 7250.
36    ///
37    /// For more information on `signature_alg` and `signature` see the documentation for [`crate::end_entity::EndEntityCert::verify_signature`].
38    pub fn verify_signature(
39        &self,
40        signature_alg: &dyn SignatureVerificationAlgorithm,
41        msg: &[u8],
42        signature: &[u8],
43    ) -> Result<(), Error> {
44        signed_data::verify_signature(
45            signature_alg,
46            self.inner,
47            untrusted::Input::from(msg),
48            untrusted::Input::from(signature),
49        )
50    }
51}
52
53#[cfg(feature = "alloc")]
54#[cfg(test)]
55mod tests {
56    use super::*;
57
58    #[test]
59    fn test_ee_read_for_rpk() {
60        // Try to read an end entity certificate into a RawPublicKeyEntity.
61        // It will fail to parse the key value since we expect no unused bits.
62        let ee = include_bytes!("../tests/ed25519/ee.der");
63        let ee_der = SubjectPublicKeyInfoDer::from(ee.as_slice());
64        assert_eq!(
65            RawPublicKeyEntity::try_from(&ee_der).expect_err("unexpectedly parsed certificate"),
66            Error::TrailingData(DerTypeId::BitString)
67        );
68    }
69
70    #[test]
71    fn test_spki_read_for_rpk() {
72        let pubkey = include_bytes!("../tests/ed25519/ee-pubkey.der");
73        let spki_der = SubjectPublicKeyInfoDer::from(pubkey.as_slice());
74        let rpk = RawPublicKeyEntity::try_from(&spki_der).expect("failed to parse rpk");
75
76        // Retrieved the SPKI from the pubkey.der using the following commands (as in [`cert::test_spki_read`]):
77        // xxd -plain -cols 1 tests/ed255519/ee-pubkey.der
78        let expected_spki = [
79            0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x70, 0x03, 0x21, 0x00, 0xfe, 0x5a, 0x1e, 0x36,
80            0x6c, 0x17, 0x27, 0x5b, 0xf1, 0x58, 0x1e, 0x3a, 0x0e, 0xe6, 0x56, 0x29, 0x8d, 0x9e,
81            0x1b, 0x3f, 0xd3, 0x3f, 0x96, 0x46, 0xef, 0xbf, 0x04, 0x6b, 0xc7, 0x3d, 0x47, 0x5c,
82        ];
83        assert_eq!(expected_spki, rpk.inner.as_slice_less_safe())
84    }
85}