use crate::{Error, Result, Version};
use core::fmt;
use der::{
Decode, DecodeValue, Encode, EncodeValue, FixedTag, Header, Length, Reader, Sequence, TagMode,
TagNumber, Writer,
asn1::{AnyRef, BitStringRef, ContextSpecific, OctetStringRef, SequenceRef},
};
use spki::AlgorithmIdentifier;
#[cfg(feature = "ctutils")]
use ctutils::{Choice, CtEq};
#[cfg(feature = "pem")]
use der::pem::PemLabel;
#[cfg(feature = "alloc")]
use der::{
SecretDocument,
asn1::{Any, BitString, OctetString},
};
#[cfg(feature = "encryption")]
use {
crate::EncryptedPrivateKeyInfoRef, der::zeroize::Zeroizing, pkcs5::pbes2,
rand_core::TryCryptoRng,
};
const ATTRIBUTES_TAG: TagNumber = TagNumber(0);
const PUBLIC_KEY_TAG: TagNumber = TagNumber(1);
#[derive(Clone)]
pub struct PrivateKeyInfo<Params, Key, PubKey> {
pub algorithm: AlgorithmIdentifier<Params>,
pub private_key: Key,
pub public_key: Option<PubKey>,
}
impl<Params, Key, PubKey> PrivateKeyInfo<Params, Key, PubKey> {
pub fn new(algorithm: AlgorithmIdentifier<Params>, private_key: Key) -> Self {
Self {
algorithm,
private_key,
public_key: None,
}
}
pub fn version(&self) -> Version {
if self.public_key.is_some() {
Version::V2
} else {
Version::V1
}
}
}
impl<'a, Params, Key, PubKey> PrivateKeyInfo<Params, Key, PubKey>
where
Params: der::Choice<'a, Error = der::Error> + Encode,
Key: DecodeValue<'a, Error = der::Error> + FixedTag + 'a,
Key: EncodeValue,
PubKey: DecodeValue<'a, Error = der::Error> + FixedTag + 'a,
PubKey: BitStringLike,
{
#[cfg(feature = "getrandom")]
pub fn encrypt(&self, password: impl AsRef<[u8]>) -> Result<SecretDocument> {
let der = Zeroizing::new(self.to_der()?);
EncryptedPrivateKeyInfoRef::encrypt(password, der.as_ref())
}
#[cfg(feature = "encryption")]
pub fn encrypt_with_rng<R: TryCryptoRng>(
&self,
rng: &mut R,
password: impl AsRef<[u8]>,
) -> Result<SecretDocument> {
let der = Zeroizing::new(self.to_der()?);
EncryptedPrivateKeyInfoRef::encrypt_with_rng(rng, password, der.as_ref())
}
#[cfg(feature = "encryption")]
pub fn encrypt_with_params(
&self,
pbes2_params: pbes2::Parameters,
password: impl AsRef<[u8]>,
) -> Result<SecretDocument> {
let der = Zeroizing::new(self.to_der()?);
EncryptedPrivateKeyInfoRef::encrypt_with_params(pbes2_params, password, der.as_ref())
}
}
impl<'a, Params, Key, PubKey> PrivateKeyInfo<Params, Key, PubKey>
where
Params: der::Choice<'a> + Encode,
PubKey: BitStringLike,
{
fn public_key_bit_string(&self) -> Option<ContextSpecific<BitStringRef<'_>>> {
self.public_key.as_ref().map(|pk| {
let value = pk.as_bit_string();
ContextSpecific {
tag_number: PUBLIC_KEY_TAG,
tag_mode: TagMode::Implicit,
value,
}
})
}
}
impl<'a, Params, Key, PubKey> DecodeValue<'a> for PrivateKeyInfo<Params, Key, PubKey>
where
Params: der::Choice<'a, Error = der::Error> + Encode,
Key: DecodeValue<'a, Error = der::Error> + FixedTag + 'a,
PubKey: DecodeValue<'a, Error = der::Error> + FixedTag + 'a,
{
type Error = der::Error;
fn decode_value<R: Reader<'a>>(reader: &mut R, _header: Header) -> der::Result<Self> {
let version = Version::decode(reader)?;
let algorithm = reader.decode()?;
let private_key = Key::decode(reader)?;
let _attributes =
reader.context_specific::<&SequenceRef>(ATTRIBUTES_TAG, TagMode::Implicit)?;
let public_key = reader.context_specific::<PubKey>(PUBLIC_KEY_TAG, TagMode::Implicit)?;
if version.has_public_key() != public_key.is_some() {
return Err(reader.error(
der::Tag::ContextSpecific {
constructed: true,
number: PUBLIC_KEY_TAG,
}
.value_error(),
));
}
while !reader.is_finished() {
reader.decode::<ContextSpecific<AnyRef<'_>>>()?;
}
Ok(Self {
algorithm,
private_key,
public_key,
})
}
}
impl<'a, Params, Key, PubKey> EncodeValue for PrivateKeyInfo<Params, Key, PubKey>
where
Params: der::Choice<'a, Error = der::Error> + Encode,
Key: EncodeValue + FixedTag,
PubKey: BitStringLike,
{
fn value_len(&self) -> der::Result<Length> {
self.version().encoded_len()?
+ self.algorithm.encoded_len()?
+ self.private_key.encoded_len()?
+ self.public_key_bit_string().encoded_len()?
}
fn encode_value(&self, writer: &mut impl Writer) -> der::Result<()> {
self.version().encode(writer)?;
self.algorithm.encode(writer)?;
self.private_key.encode(writer)?;
self.public_key_bit_string().encode(writer)?;
Ok(())
}
}
impl<'a, Params, Key, PubKey> Sequence<'a> for PrivateKeyInfo<Params, Key, PubKey>
where
Params: der::Choice<'a, Error = der::Error> + Encode,
Key: DecodeValue<'a, Error = der::Error> + FixedTag + 'a,
Key: EncodeValue,
PubKey: DecodeValue<'a, Error = der::Error> + FixedTag + 'a,
PubKey: BitStringLike,
{
}
impl<'a, Params, Key, PubKey> TryFrom<&'a [u8]> for PrivateKeyInfo<Params, Key, PubKey>
where
Params: der::Choice<'a, Error = der::Error> + Encode,
Key: DecodeValue<'a, Error = der::Error> + FixedTag + 'a,
Key: EncodeValue,
PubKey: DecodeValue<'a, Error = der::Error> + FixedTag + 'a,
PubKey: BitStringLike,
{
type Error = Error;
fn try_from(bytes: &'a [u8]) -> Result<Self> {
Ok(Self::from_der(bytes)?)
}
}
impl<Params, Key, PubKey> fmt::Debug for PrivateKeyInfo<Params, Key, PubKey>
where
Params: fmt::Debug,
PubKey: fmt::Debug,
{
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_non_exhaustive()
}
}
#[cfg(feature = "alloc")]
impl<'a, Params, Key, PubKey> TryFrom<PrivateKeyInfo<Params, Key, PubKey>> for SecretDocument
where
Params: der::Choice<'a, Error = der::Error> + Encode,
Key: DecodeValue<'a, Error = der::Error> + FixedTag + 'a,
Key: EncodeValue,
PubKey: DecodeValue<'a, Error = der::Error> + FixedTag + 'a,
PubKey: BitStringLike,
{
type Error = Error;
fn try_from(private_key: PrivateKeyInfo<Params, Key, PubKey>) -> Result<SecretDocument> {
SecretDocument::try_from(&private_key)
}
}
#[cfg(feature = "alloc")]
impl<'a, Params, Key, PubKey> TryFrom<&PrivateKeyInfo<Params, Key, PubKey>> for SecretDocument
where
Params: der::Choice<'a, Error = der::Error> + Encode,
Key: DecodeValue<'a, Error = der::Error> + FixedTag + 'a,
Key: EncodeValue,
PubKey: DecodeValue<'a, Error = der::Error> + FixedTag + 'a,
PubKey: BitStringLike,
{
type Error = Error;
fn try_from(private_key: &PrivateKeyInfo<Params, Key, PubKey>) -> Result<SecretDocument> {
Ok(Self::encode_msg(private_key)?)
}
}
#[cfg(feature = "pem")]
impl<Params, Key, PubKey> PemLabel for PrivateKeyInfo<Params, Key, PubKey> {
const PEM_LABEL: &'static str = "PRIVATE KEY";
}
#[cfg(feature = "ctutils")]
impl<Params, Key, PubKey> CtEq for PrivateKeyInfo<Params, Key, PubKey>
where
Params: Eq,
Key: PartialEq + AsRef<[u8]>,
PubKey: PartialEq,
{
fn ct_eq(&self, other: &Self) -> Choice {
let public_fields_eq =
self.algorithm == other.algorithm && self.public_key == other.public_key;
self.private_key.as_ref().ct_eq(other.private_key.as_ref())
& Choice::from(u8::from(public_fields_eq))
}
}
#[cfg(feature = "ctutils")]
impl<Params, Key, PubKey> Eq for PrivateKeyInfo<Params, Key, PubKey>
where
Params: Eq,
Key: AsRef<[u8]> + Eq,
PubKey: Eq,
{
}
#[cfg(feature = "ctutils")]
impl<Params, Key, PubKey> PartialEq for PrivateKeyInfo<Params, Key, PubKey>
where
Params: Eq,
Key: PartialEq + AsRef<[u8]>,
PubKey: PartialEq,
{
fn eq(&self, other: &Self) -> bool {
self.ct_eq(other).into()
}
}
pub type PrivateKeyInfoRef<'a> = PrivateKeyInfo<AnyRef<'a>, &'a OctetStringRef, BitStringRef<'a>>;
pub trait BitStringLike {
fn as_bit_string(&self) -> BitStringRef<'_>;
}
impl BitStringLike for BitStringRef<'_> {
fn as_bit_string(&self) -> BitStringRef<'_> {
BitStringRef::from(self)
}
}
#[cfg(feature = "alloc")]
pub(crate) mod allocating {
use super::*;
use crate::{DecodePrivateKey, EncodePrivateKey};
use alloc::borrow::ToOwned;
use core::borrow::Borrow;
use der::referenced::*;
#[cfg(feature = "pem")]
use der::DecodePem;
pub type PrivateKeyInfoOwned = PrivateKeyInfo<Any, OctetString, BitString>;
impl DecodePrivateKey for PrivateKeyInfoOwned {
fn from_pkcs8_der(bytes: &[u8]) -> Result<Self> {
Ok(Self::from_der(bytes)?)
}
#[cfg(feature = "pem")]
fn from_pkcs8_pem(pem: &str) -> Result<Self> {
Ok(Self::from_pem(pem)?)
}
}
impl EncodePrivateKey for PrivateKeyInfoOwned {
fn to_pkcs8_der(&self) -> Result<SecretDocument> {
self.try_into()
}
}
impl EncodePrivateKey for PrivateKeyInfoRef<'_> {
fn to_pkcs8_der(&self) -> Result<SecretDocument> {
self.try_into()
}
}
impl<'a> RefToOwned<'a> for PrivateKeyInfoRef<'a> {
type Owned = PrivateKeyInfoOwned;
fn ref_to_owned(&self) -> Self::Owned {
PrivateKeyInfoOwned {
algorithm: self.algorithm.ref_to_owned(),
private_key: self.private_key.to_owned(),
public_key: self.public_key.ref_to_owned(),
}
}
}
impl OwnedToRef for PrivateKeyInfoOwned {
type Borrowed<'a> = PrivateKeyInfoRef<'a>;
fn owned_to_ref(&self) -> Self::Borrowed<'_> {
PrivateKeyInfoRef {
algorithm: self.algorithm.owned_to_ref(),
private_key: self.private_key.borrow(),
public_key: self.public_key.owned_to_ref(),
}
}
}
impl BitStringLike for BitString {
fn as_bit_string(&self) -> BitStringRef<'_> {
BitStringRef::from(self)
}
}
}