summaryrefslogtreecommitdiffstats
path: root/vendor/sec1/src/private_key.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-18 02:49:50 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-18 02:49:50 +0000
commit9835e2ae736235810b4ea1c162ca5e65c547e770 (patch)
tree3fcebf40ed70e581d776a8a4c65923e8ec20e026 /vendor/sec1/src/private_key.rs
parentReleasing progress-linux version 1.70.0+dfsg2-1~progress7.99u1. (diff)
downloadrustc-9835e2ae736235810b4ea1c162ca5e65c547e770.tar.xz
rustc-9835e2ae736235810b4ea1c162ca5e65c547e770.zip
Merging upstream version 1.71.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/sec1/src/private_key.rs')
-rw-r--r--vendor/sec1/src/private_key.rs75
1 files changed, 44 insertions, 31 deletions
diff --git a/vendor/sec1/src/private_key.rs b/vendor/sec1/src/private_key.rs
index 236243231..531579936 100644
--- a/vendor/sec1/src/private_key.rs
+++ b/vendor/sec1/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";
}