use crate::{AlgorithmIdentifier, Error, Result, Version};
use core::fmt;
use der::{
asn1::{Any, BitString, ContextSpecific, OctetString},
Decodable, Decoder, Encodable, Sequence, TagMode, TagNumber,
};
#[cfg(feature = "alloc")]
use crate::PrivateKeyDocument;
#[cfg(feature = "encryption")]
use {
crate::EncryptedPrivateKeyDocument,
rand_core::{CryptoRng, RngCore},
};
#[cfg(feature = "pem")]
use {
crate::{EncodePrivateKey, LineEnding},
alloc::string::String,
zeroize::Zeroizing,
};
#[cfg(feature = "subtle")]
use subtle::{Choice, ConstantTimeEq};
const PUBLIC_KEY_TAG: TagNumber = TagNumber::new(1);
#[derive(Clone)]
pub struct PrivateKeyInfo<'a> {
pub algorithm: AlgorithmIdentifier<'a>,
pub private_key: &'a [u8],
pub public_key: Option<&'a [u8]>,
}
impl<'a> PrivateKeyInfo<'a> {
pub fn new(algorithm: AlgorithmIdentifier<'a>, private_key: &'a [u8]) -> Self {
Self {
algorithm,
private_key,
public_key: None,
}
}
pub fn version(&self) -> Version {
if self.public_key.is_some() {
Version::V2
} else {
Version::V1
}
}
#[cfg(feature = "encryption")]
#[cfg_attr(docsrs, doc(cfg(feature = "encryption")))]
pub fn encrypt(
&self,
rng: impl CryptoRng + RngCore,
password: impl AsRef<[u8]>,
) -> Result<EncryptedPrivateKeyDocument> {
self.to_der()?.encrypt(rng, password)
}
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
pub fn to_der(&self) -> Result<PrivateKeyDocument> {
self.try_into()
}
#[cfg(feature = "pem")]
#[cfg_attr(docsrs, doc(cfg(feature = "pem")))]
pub fn to_pem(&self, line_ending: LineEnding) -> Result<Zeroizing<String>> {
self.to_der()?.to_pkcs8_pem(line_ending)
}
}
impl<'a> Decodable<'a> for PrivateKeyInfo<'a> {
fn decode(decoder: &mut Decoder<'a>) -> der::Result<PrivateKeyInfo<'a>> {
decoder.sequence(|decoder| {
let version = Version::decode(decoder)?;
let algorithm = decoder.decode()?;
let private_key = decoder.octet_string()?.into();
let public_key = decoder
.context_specific::<BitString<'_>>(PUBLIC_KEY_TAG, TagMode::Implicit)?
.map(|bs| {
bs.as_bytes()
.ok_or_else(|| der::Tag::BitString.value_error())
})
.transpose()?;
if version.has_public_key() != public_key.is_some() {
return Err(decoder.value_error(der::Tag::ContextSpecific {
constructed: true,
number: PUBLIC_KEY_TAG,
}));
}
while !decoder.is_finished() {
decoder.decode::<ContextSpecific<Any<'_>>>()?;
}
Ok(Self {
algorithm,
private_key,
public_key,
})
})
}
}
impl<'a> Sequence<'a> for PrivateKeyInfo<'a> {
fn fields<F, T>(&self, f: F) -> der::Result<T>
where
F: FnOnce(&[&dyn Encodable]) -> der::Result<T>,
{
f(&[
&u8::from(self.version()),
&self.algorithm,
&OctetString::new(self.private_key)?,
&self
.public_key
.map(|pk| {
BitString::from_bytes(pk).map(|value| ContextSpecific {
tag_number: PUBLIC_KEY_TAG,
tag_mode: TagMode::Implicit,
value,
})
})
.transpose()?,
])
}
}
impl<'a> TryFrom<&'a [u8]> for PrivateKeyInfo<'a> {
type Error = Error;
fn try_from(bytes: &'a [u8]) -> Result<Self> {
Ok(Self::from_der(bytes)?)
}
}
impl<'a> fmt::Debug for PrivateKeyInfo<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("PrivateKeyInfo")
.field("version", &self.version())
.field("algorithm", &self.algorithm)
.field("public_key", &self.public_key)
.finish() }
}
#[cfg(feature = "subtle")]
#[cfg_attr(docsrs, doc(cfg(feature = "subtle")))]
impl<'a> ConstantTimeEq for PrivateKeyInfo<'a> {
fn ct_eq(&self, other: &Self) -> Choice {
let public_fields_eq =
self.algorithm == other.algorithm && self.public_key == other.public_key;
self.private_key.ct_eq(other.private_key) & Choice::from(public_fields_eq as u8)
}
}
#[cfg(feature = "subtle")]
#[cfg_attr(docsrs, doc(cfg(feature = "subtle")))]
impl<'a> PartialEq for PrivateKeyInfo<'a> {
fn eq(&self, other: &Self) -> bool {
self.ct_eq(other).into()
}
}
#[cfg(feature = "subtle")]
#[cfg_attr(docsrs, doc(cfg(feature = "subtle")))]
impl<'a> Eq for PrivateKeyInfo<'a> {}