libp2p_webrtc/tokio/
fingerprint.rs

1// Copyright 2022 Parity Technologies (UK) Ltd.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the "Software"),
5// to deal in the Software without restriction, including without limitation
6// the rights to use, copy, modify, merge, publish, distribute, sublicense,
7// and/or sell copies of the Software, and to permit persons to whom the
8// Software is furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
14// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19// DEALINGS IN THE SOFTWARE.
20
21use webrtc::dtls_transport::dtls_fingerprint::RTCDtlsFingerprint;
22
23const SHA256: &str = "sha-256";
24
25type Multihash = multihash::Multihash<64>;
26
27/// A certificate fingerprint that is assumed to be created using the SHA256 hash algorithm.
28#[derive(Debug, Eq, PartialEq, Copy, Clone)]
29pub struct Fingerprint(libp2p_webrtc_utils::Fingerprint);
30
31impl Fingerprint {
32    #[cfg(test)]
33    pub fn raw(bytes: [u8; 32]) -> Self {
34        Self(libp2p_webrtc_utils::Fingerprint::raw(bytes))
35    }
36
37    /// Creates a fingerprint from a raw certificate.
38    pub fn from_certificate(bytes: &[u8]) -> Self {
39        Fingerprint(libp2p_webrtc_utils::Fingerprint::from_certificate(bytes))
40    }
41
42    /// Converts [`RTCDtlsFingerprint`] to [`Fingerprint`].
43    pub fn try_from_rtc_dtls(fp: &RTCDtlsFingerprint) -> Option<Self> {
44        if fp.algorithm != SHA256 {
45            return None;
46        }
47
48        let mut buf = [0; 32];
49        hex::decode_to_slice(fp.value.replace(':', ""), &mut buf).ok()?;
50
51        Some(Self(libp2p_webrtc_utils::Fingerprint::raw(buf)))
52    }
53
54    /// Converts [`Multihash`](multihash::Multihash) to [`Fingerprint`].
55    pub fn try_from_multihash(hash: Multihash) -> Option<Self> {
56        Some(Self(libp2p_webrtc_utils::Fingerprint::try_from_multihash(
57            hash,
58        )?))
59    }
60
61    /// Converts this fingerprint to [`Multihash`](multihash::Multihash).
62    pub fn to_multihash(self) -> Multihash {
63        self.0.to_multihash()
64    }
65
66    /// Formats this fingerprint as uppercase hex, separated by colons (`:`).
67    ///
68    /// This is the format described in <https://www.rfc-editor.org/rfc/rfc4572#section-5>.
69    pub fn to_sdp_format(self) -> String {
70        self.0.to_sdp_format()
71    }
72
73    /// Returns the algorithm used (e.g. "sha-256").
74    /// See <https://datatracker.ietf.org/doc/html/rfc8122#section-5>
75    pub fn algorithm(&self) -> String {
76        self.0.algorithm()
77    }
78
79    pub(crate) fn into_inner(self) -> libp2p_webrtc_utils::Fingerprint {
80        self.0
81    }
82}