Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion elliptic-curve/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pem-rfc7468 = { version = "1.0.0-rc.2", optional = true, features = ["alloc"] }
pkcs8 = { version = "0.11.0-rc.6", optional = true, default-features = false }
sec1 = { version = "0.8.0-rc.8", optional = true, features = ["subtle", "zeroize"] }
serdect = { version = "0.3", optional = true, default-features = false, features = ["alloc"] }
der = { version = "0.8.0-rc.8", default-features = false }

[dev-dependencies]
hex-literal = "1"
Expand Down Expand Up @@ -64,7 +65,7 @@ ecdh = ["arithmetic", "digest", "dep:hkdf"]
group = ["dep:group", "ff"]
pkcs8 = ["dep:pkcs8", "sec1"]
pem = ["dep:pem-rfc7468", "alloc", "arithmetic", "pkcs8/pem", "sec1/pem"]
serde = ["dep:serdect", "alloc", "pkcs8", "sec1/serde"]
serde = ["dep:serdect", "pkcs8", "sec1/serde"]

[package.metadata.docs.rs]
features = ["bits", "ecdh", "pem", "std"]
29 changes: 28 additions & 1 deletion elliptic-curve/src/public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,28 @@ where
}
}

#[cfg(feature = "serde")]
impl<C> PublicKey<C>
where
C: AssociatedOid + CurveArithmetic,
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
FieldBytesSize<C>: ModulusSize,
{
/// Encode this [`PublicKey`] as der bytes, placing the result in `output`. This function
/// returns a slice containing the encoded DER bytes.
fn encode_as_der<'buf>(&self, output: &'buf mut [u8]) -> der::Result<&'buf [u8]> {
let public_key_bytes = self.to_encoded_point(false);
let subject_public_key = der::asn1::BitStringRef::new(0, public_key_bytes.as_bytes())?;

let spki = pkcs8::SubjectPublicKeyInfo {
algorithm: Self::ALGORITHM_IDENTIFIER,
subject_public_key,
};

der::Encode::encode_to_slice(&spki, output)
}
}

#[cfg(all(feature = "alloc", feature = "pkcs8"))]
impl<C> EncodePublicKey for PublicKey<C>
where
Expand All @@ -436,6 +458,7 @@ where
let public_key_bytes = self.to_encoded_point(false);
let subject_public_key = der::asn1::BitStringRef::new(0, public_key_bytes.as_bytes())?;

// TODO: use `encode_as_der` here?
Copy link
Author

@datdenkikniet datdenkikniet Aug 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using encode_as_der seemed like a hassle here. Is there a better approach than encode_as_der followed directly by Document::decode().unwrap()?

Also suffers from the encoding length question, and this specific conversion will require alloc anyways (because der::Document contains a Vec<u8>)

pkcs8::SubjectPublicKeyInfo {
algorithm: Self::ALGORITHM_IDENTIFIER,
subject_public_key,
Expand Down Expand Up @@ -483,7 +506,11 @@ where
where
S: ser::Serializer,
{
let der = self.to_public_key_der().map_err(ser::Error::custom)?;
// TODO: can we determine DER encoding length up-front? Using `MockCurve` gives
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to collect this information from some associated const? Was hard to figure out

// 91 bytes of output, but it feels like that depends on the curve that is being
// used here.
let mut buf = [0u8; 91];
let der = self.encode_as_der(&mut buf).map_err(ser::Error::custom)?;
serdect::slice::serialize_hex_upper_or_bin(&der, serializer)
}
}
Expand Down