sec1/
private_key.rs

1//! SEC1 elliptic curve private key support.
2//!
3//! Support for ASN.1 DER-encoded elliptic curve private keys as described in
4//! SEC1: Elliptic Curve Cryptography (Version 2.0) Appendix C.4 (p.108):
5//!
6//! <https://www.secg.org/sec1-v2.pdf>
7
8#[cfg(feature = "alloc")]
9pub(crate) mod document;
10
11use crate::{EcParameters, Error};
12use core::fmt;
13use der::{
14    asn1::{BitString, ContextSpecific, OctetString},
15    Decodable, Decoder, Encodable, Sequence, Tag, TagMode, TagNumber,
16};
17
18/// `ECPrivateKey` version.
19///
20/// From [RFC5913 Section 3]:
21/// > version specifies the syntax version number of the elliptic curve
22/// > private key structure.  For this version of the document, it SHALL
23/// > be set to ecPrivkeyVer1, which is of type INTEGER and whose value
24/// > is one (1).
25///
26/// [RFC5915 Section 3]: https://datatracker.ietf.org/doc/html/rfc5915#section-3
27const VERSION: u8 = 1;
28
29/// Context-specific tag number for the elliptic curve parameters.
30const EC_PARAMETERS_TAG: TagNumber = TagNumber::new(0);
31
32/// Context-specific tag number for the public key.
33const PUBLIC_KEY_TAG: TagNumber = TagNumber::new(1);
34
35/// SEC1 elliptic curve private key.
36///
37/// Described in [SEC1: Elliptic Curve Cryptography (Version 2.0)]
38/// Appendix C.4 (p.108) and also [RFC5915 Section 3]:
39///
40/// ```text
41/// ECPrivateKey ::= SEQUENCE {
42///   version        INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
43///   privateKey     OCTET STRING,
44///   parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
45///   publicKey  [1] BIT STRING OPTIONAL
46/// }
47/// ```
48///
49/// When encoded as PEM (text), keys in this format begin with the following:
50///
51/// ```text
52/// -----BEGIN EC PRIVATE KEY-----
53/// ```
54///
55/// [SEC1: Elliptic Curve Cryptography (Version 2.0)]: https://www.secg.org/sec1-v2.pdf
56/// [RFC5915 Section 3]: https://datatracker.ietf.org/doc/html/rfc5915#section-3
57#[derive(Clone)]
58pub struct EcPrivateKey<'a> {
59    /// Private key data.
60    pub private_key: &'a [u8],
61
62    /// Elliptic curve parameters.
63    pub parameters: Option<EcParameters>,
64
65    /// Public key data, optionally available if version is V2.
66    pub public_key: Option<&'a [u8]>,
67}
68
69impl<'a> Decodable<'a> for EcPrivateKey<'a> {
70    fn decode(decoder: &mut Decoder<'a>) -> der::Result<Self> {
71        decoder.sequence(|decoder| {
72            if decoder.uint8()? != VERSION {
73                return Err(der::Tag::Integer.value_error());
74            }
75
76            let private_key = decoder.octet_string()?.as_bytes();
77            let parameters = decoder.context_specific(EC_PARAMETERS_TAG, TagMode::Explicit)?;
78            let public_key = decoder
79                .context_specific::<BitString<'_>>(PUBLIC_KEY_TAG, TagMode::Explicit)?
80                .map(|bs| bs.as_bytes().ok_or_else(|| Tag::BitString.value_error()))
81                .transpose()?;
82
83            Ok(EcPrivateKey {
84                private_key,
85                parameters,
86                public_key,
87            })
88        })
89    }
90}
91
92impl<'a> Sequence<'a> for EcPrivateKey<'a> {
93    fn fields<F, T>(&self, f: F) -> der::Result<T>
94    where
95        F: FnOnce(&[&dyn Encodable]) -> der::Result<T>,
96    {
97        f(&[
98            &VERSION,
99            &OctetString::new(self.private_key)?,
100            &self.parameters.as_ref().map(|params| ContextSpecific {
101                tag_number: EC_PARAMETERS_TAG,
102                tag_mode: TagMode::Explicit,
103                value: *params,
104            }),
105            &self
106                .public_key
107                .map(|pk| {
108                    BitString::from_bytes(pk).map(|value| ContextSpecific {
109                        tag_number: PUBLIC_KEY_TAG,
110                        tag_mode: TagMode::Explicit,
111                        value,
112                    })
113                })
114                .transpose()?,
115        ])
116    }
117}
118
119impl<'a> TryFrom<&'a [u8]> for EcPrivateKey<'a> {
120    type Error = Error;
121
122    fn try_from(bytes: &'a [u8]) -> Result<EcPrivateKey<'a>, Error> {
123        Ok(Self::from_der(bytes)?)
124    }
125}
126
127impl<'a> fmt::Debug for EcPrivateKey<'a> {
128    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
129        f.debug_struct("EcPrivateKey")
130            .field("parameters", &self.parameters)
131            .field("public_key", &self.public_key)
132            .finish_non_exhaustive()
133    }
134}