Skip to main content

sec1/
traits.rs

1//! Traits for parsing objects from SEC1 encoded documents
2
3use crate::Result;
4
5#[cfg(feature = "alloc")]
6use der::SecretDocument;
7#[cfg(feature = "std")]
8use std::path::Path;
9#[cfg(feature = "pem")]
10use {crate::EcPrivateKey, zeroize::Zeroizing};
11#[cfg(feature = "pem")]
12use {crate::LineEnding, alloc::string::String, der::pem::PemLabel};
13
14/// Parse an [`EcPrivateKey`] from a SEC1-encoded document.
15pub trait DecodeEcPrivateKey: Sized {
16    /// Deserialize SEC1 private key from ASN.1 DER-encoded data
17    /// (binary format).
18    fn from_sec1_der(bytes: &[u8]) -> Result<Self>;
19
20    /// Deserialize SEC1-encoded private key from PEM.
21    ///
22    /// Keys in this format begin with the following:
23    ///
24    /// ```text
25    /// -----BEGIN EC PRIVATE KEY-----
26    /// ```
27    #[cfg(feature = "pem")]
28    fn from_sec1_pem(s: &str) -> Result<Self> {
29        let (label, doc) = SecretDocument::from_pem(s)?;
30        EcPrivateKey::validate_pem_label(label)?;
31        Self::from_sec1_der(doc.as_bytes())
32    }
33
34    /// Load SEC1 private key from an ASN.1 DER-encoded file on the local
35    /// filesystem (binary format).
36    #[cfg(feature = "std")]
37    fn read_sec1_der_file(path: impl AsRef<Path>) -> Result<Self> {
38        Self::from_sec1_der(SecretDocument::read_der_file(path)?.as_bytes())
39    }
40
41    /// Load SEC1 private key from a PEM-encoded file on the local filesystem.
42    #[cfg(all(feature = "pem", feature = "std"))]
43    fn read_sec1_pem_file(path: impl AsRef<Path>) -> Result<Self> {
44        let (label, doc) = SecretDocument::read_pem_file(path)?;
45        EcPrivateKey::validate_pem_label(&label)?;
46        Self::from_sec1_der(doc.as_bytes())
47    }
48}
49
50/// Serialize a [`EcPrivateKey`] to a SEC1 encoded document.
51#[cfg(feature = "alloc")]
52pub trait EncodeEcPrivateKey {
53    /// Serialize a [`SecretDocument`] containing a SEC1-encoded private key.
54    fn to_sec1_der(&self) -> Result<SecretDocument>;
55
56    /// Serialize this private key as PEM-encoded SEC1 with the given [`LineEnding`].
57    ///
58    /// To use the OS's native line endings, pass `Default::default()`.
59    #[cfg(feature = "pem")]
60    fn to_sec1_pem(&self, line_ending: LineEnding) -> Result<Zeroizing<String>> {
61        let doc = self.to_sec1_der()?;
62        Ok(doc.to_pem(EcPrivateKey::PEM_LABEL, line_ending)?)
63    }
64
65    /// Write ASN.1 DER-encoded SEC1 private key to the given path.
66    #[cfg(feature = "std")]
67    fn write_sec1_der_file(&self, path: impl AsRef<Path>) -> Result<()> {
68        Ok(self.to_sec1_der()?.write_der_file(path)?)
69    }
70
71    /// Write ASN.1 PEM-encoded SEC1 private key to the given path.
72    #[cfg(all(feature = "pem", feature = "std"))]
73    fn write_sec1_pem_file(&self, path: impl AsRef<Path>, line_ending: LineEnding) -> Result<()> {
74        let doc = self.to_sec1_der()?;
75        Ok(doc.write_pem_file(path, EcPrivateKey::PEM_LABEL, line_ending)?)
76    }
77}