aboutsummaryrefslogtreecommitdiff
path: root/src/private_key.rs
diff options
context:
space:
mode:
authorAndrew Walbran <qwandor@google.com>2023-12-15 16:32:53 +0000
committerAndrew Walbran <qwandor@google.com>2023-12-15 16:32:54 +0000
commit3b65df0b6f6462cbed971f51d0f044ef9e5f5d18 (patch)
tree860d80f94633b4178d21d2f35303896992541d53 /src/private_key.rs
parentae2adecee3b0bd52796c2b3c3cb24f242d5c4ccc (diff)
downloadsec1-3b65df0b6f6462cbed971f51d0f044ef9e5f5d18.tar.gz
Upgrade sec1 to 0.7.3
This project was upgraded with external_updater. Usage: tools/external_updater/updater.sh update rust/crates/sec1 For more info, check https://cs.android.com/android/platform/superproject/+/main:tools/external_updater/README.md Test: TreeHugger Change-Id: I1b58256b44780ea1cb414aa3155f382cbd2816fb
Diffstat (limited to 'src/private_key.rs')
-rw-r--r--src/private_key.rs75
1 files changed, 44 insertions, 31 deletions
diff --git a/src/private_key.rs b/src/private_key.rs
index 2362432..5315799 100644
--- a/src/private_key.rs
+++ b/src/private_key.rs
@@ -8,11 +8,12 @@
use crate::{EcParameters, Error, Result};
use core::fmt;
use der::{
- asn1::{BitStringRef, ContextSpecific, OctetStringRef},
- Decode, DecodeValue, Encode, Header, Reader, Sequence, Tag, TagMode, TagNumber,
+ asn1::{BitStringRef, ContextSpecific, ContextSpecificRef, OctetStringRef},
+ Decode, DecodeValue, Encode, EncodeValue, Header, Length, Reader, Sequence, Tag, TagMode,
+ TagNumber, Writer,
};
-#[cfg(feature = "alloc")]
+#[cfg(all(feature = "alloc", feature = "zeroize"))]
use der::SecretDocument;
#[cfg(feature = "pem")]
@@ -58,7 +59,6 @@ const PUBLIC_KEY_TAG: TagNumber = TagNumber::new(1);
/// [SEC1: Elliptic Curve Cryptography (Version 2.0)]: https://www.secg.org/sec1-v2.pdf
/// [RFC5915 Section 3]: https://datatracker.ietf.org/doc/html/rfc5915#section-3
#[derive(Clone)]
-#[cfg_attr(docsrs, doc(cfg(feature = "der")))]
pub struct EcPrivateKey<'a> {
/// Private key data.
pub private_key: &'a [u8],
@@ -70,6 +70,30 @@ pub struct EcPrivateKey<'a> {
pub public_key: Option<&'a [u8]>,
}
+impl<'a> EcPrivateKey<'a> {
+ fn context_specific_parameters(&self) -> Option<ContextSpecificRef<'_, EcParameters>> {
+ self.parameters.as_ref().map(|params| ContextSpecificRef {
+ tag_number: EC_PARAMETERS_TAG,
+ tag_mode: TagMode::Explicit,
+ value: params,
+ })
+ }
+
+ fn context_specific_public_key(
+ &self,
+ ) -> der::Result<Option<ContextSpecific<BitStringRef<'a>>>> {
+ self.public_key
+ .map(|pk| {
+ BitStringRef::from_bytes(pk).map(|value| ContextSpecific {
+ tag_number: PUBLIC_KEY_TAG,
+ tag_mode: TagMode::Explicit,
+ value,
+ })
+ })
+ .transpose()
+ }
+}
+
impl<'a> DecodeValue<'a> for EcPrivateKey<'a> {
fn decode_value<R: Reader<'a>>(reader: &mut R, header: Header) -> der::Result<Self> {
reader.read_nested(header.length, |reader| {
@@ -93,33 +117,25 @@ impl<'a> DecodeValue<'a> for EcPrivateKey<'a> {
}
}
-impl<'a> Sequence<'a> for EcPrivateKey<'a> {
- fn fields<F, T>(&self, f: F) -> der::Result<T>
- where
- F: FnOnce(&[&dyn Encode]) -> der::Result<T>,
- {
- f(&[
- &VERSION,
- &OctetStringRef::new(self.private_key)?,
- &self.parameters.as_ref().map(|params| ContextSpecific {
- tag_number: EC_PARAMETERS_TAG,
- tag_mode: TagMode::Explicit,
- value: *params,
- }),
- &self
- .public_key
- .map(|pk| {
- BitStringRef::from_bytes(pk).map(|value| ContextSpecific {
- tag_number: PUBLIC_KEY_TAG,
- tag_mode: TagMode::Explicit,
- value,
- })
- })
- .transpose()?,
- ])
+impl EncodeValue for EcPrivateKey<'_> {
+ fn value_len(&self) -> der::Result<Length> {
+ VERSION.encoded_len()?
+ + OctetStringRef::new(self.private_key)?.encoded_len()?
+ + self.context_specific_parameters().encoded_len()?
+ + self.context_specific_public_key()?.encoded_len()?
+ }
+
+ fn encode_value(&self, writer: &mut impl Writer) -> der::Result<()> {
+ VERSION.encode(writer)?;
+ OctetStringRef::new(self.private_key)?.encode(writer)?;
+ self.context_specific_parameters().encode(writer)?;
+ self.context_specific_public_key()?.encode(writer)?;
+ Ok(())
}
}
+impl<'a> Sequence<'a> for EcPrivateKey<'a> {}
+
impl<'a> TryFrom<&'a [u8]> for EcPrivateKey<'a> {
type Error = Error;
@@ -138,7 +154,6 @@ impl<'a> fmt::Debug for EcPrivateKey<'a> {
}
#[cfg(feature = "alloc")]
-#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl TryFrom<EcPrivateKey<'_>> for SecretDocument {
type Error = Error;
@@ -148,7 +163,6 @@ impl TryFrom<EcPrivateKey<'_>> for SecretDocument {
}
#[cfg(feature = "alloc")]
-#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl TryFrom<&EcPrivateKey<'_>> for SecretDocument {
type Error = Error;
@@ -158,7 +172,6 @@ impl TryFrom<&EcPrivateKey<'_>> for SecretDocument {
}
#[cfg(feature = "pem")]
-#[cfg_attr(docsrs, doc(cfg(feature = "pem")))]
impl PemLabel for EcPrivateKey<'_> {
const PEM_LABEL: &'static str = "EC PRIVATE KEY";
}