ssh_packet/crypto/
signature.rs

1//! Facilities to use some of the _signature algorithms_.
2
3use binrw::binwrite;
4
5use crate::arch;
6
7/// The data that gets _signed_ and _verified_ to prove the possession of the said private key in
8/// the `publickey` authentication method, computed from the concatenation of the following.
9///
10/// see <https://datatracker.ietf.org/doc/html/rfc4252#section-7>.
11#[binwrite]
12#[derive(Debug)]
13#[bw(big)]
14pub struct Publickey<'b> {
15    /// The session identifier issued by the key-exchange.
16    pub session_id: arch::Bytes<'b>,
17
18    #[bw(calc = 50)]
19    magic: u8,
20
21    /// Username for the auth request.
22    pub username: arch::Utf8<'b>,
23
24    /// Service name to query.
25    pub service_name: arch::Ascii<'b>,
26
27    #[bw(calc = "publickey".into())]
28    method: arch::Utf8<'b>,
29
30    #[bw(calc = true.into())]
31    signed: arch::Bool,
32
33    /// Public key algorithm's name.
34    pub algorithm: arch::Bytes<'b>,
35
36    /// Public key blob.
37    pub blob: arch::Bytes<'b>,
38}
39
40impl Publickey<'_> {
41    /// Verify the structure against the provided `signature` with the `key`.
42    #[cfg(feature = "signature")]
43    #[cfg_attr(docsrs, doc(cfg(feature = "signature")))]
44    pub fn verify<S, K: signature::Verifier<S>>(
45        &self,
46        key: &K,
47        signature: &S,
48    ) -> signature::Result<()> {
49        use binrw::BinWrite;
50
51        let mut buffer = Vec::new();
52        self.write(&mut std::io::Cursor::new(&mut buffer))
53            .expect("The binrw structure serialization failed");
54
55        K::verify(key, &buffer, signature)
56    }
57
58    /// Sign the structure with the provided `key` to produce the `signature`.
59    #[cfg(feature = "signature")]
60    #[cfg_attr(docsrs, doc(cfg(feature = "signature")))]
61    pub fn sign<S, K: signature::Signer<S>>(&self, key: &K) -> S {
62        use binrw::BinWrite;
63
64        let mut buffer = Vec::new();
65        self.write(&mut std::io::Cursor::new(&mut buffer))
66            .expect("The binrw structure serialization failed");
67
68        K::sign(key, &buffer)
69    }
70}