use alloc::{boxed::Box, string::String};
use serde::{Deserialize, Serialize};
use super::Thumbprint;
use crate::crypto::{ec, okp, rsa};
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
#[serde(untagged)]
pub enum Private {
Rsa(Box<rsa::PrivateKey>),
Ec(EcPrivate),
Okp(OkpPrivate),
}
impl From<Private> for super::JsonWebKeyType {
fn from(x: Private) -> Self {
super::JsonWebKeyType::Asymmetric(Box::new(super::AsymmetricJsonWebKey::Private(x)))
}
}
impl crate::sealed::Sealed for Private {}
impl Thumbprint for Private {
fn thumbprint_prehashed(&self) -> String {
match self {
Private::Rsa(key) => key.thumbprint_prehashed(),
Private::Ec(key) => key.thumbprint_prehashed(),
Private::Okp(key) => key.thumbprint_prehashed(),
}
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Serialize, PartialEq, Eq, Hash)]
#[serde(untagged)]
pub enum EcPrivate {
P256(ec::P256PrivateKey),
P384(ec::P384PrivateKey),
P521(ec::P521PrivateKey),
Secp256k1(ec::Secp256k1PrivateKey),
}
impl crate::sealed::Sealed for EcPrivate {}
impl Thumbprint for EcPrivate {
fn thumbprint_prehashed(&self) -> String {
match self {
EcPrivate::P256(key) => key.thumbprint_prehashed(),
EcPrivate::P384(key) => key.thumbprint_prehashed(),
EcPrivate::P521(key) => key.thumbprint_prehashed(),
EcPrivate::Secp256k1(key) => key.thumbprint_prehashed(),
}
}
}
impl From<EcPrivate> for super::JsonWebKeyType {
fn from(x: EcPrivate) -> Self {
super::JsonWebKeyType::Asymmetric(alloc::boxed::Box::new(
super::AsymmetricJsonWebKey::Private(super::Private::Ec(x)),
))
}
}
impl_internally_tagged_deserialize!(EcPrivate, "crv", "EcCurve", [
"P-256" => P256,
"P-384" => P384,
"P-521" => P521,
"secp256k1" => Secp256k1,
]);
#[non_exhaustive]
#[derive(Debug, Clone, Serialize, PartialEq, Eq, Hash)]
#[serde(untagged)]
pub enum OkpPrivate {
Ed25519(okp::Ed25519PrivateKey),
Ed448(okp::Ed448PrivateKey),
}
impl crate::sealed::Sealed for OkpPrivate {}
impl Thumbprint for OkpPrivate {
fn thumbprint_prehashed(&self) -> String {
match self {
OkpPrivate::Ed25519(key) => key.thumbprint_prehashed(),
OkpPrivate::Ed448(key) => key.thumbprint_prehashed(),
}
}
}
impl From<OkpPrivate> for super::JsonWebKeyType {
fn from(x: OkpPrivate) -> Self {
super::JsonWebKeyType::Asymmetric(alloc::boxed::Box::new(
super::AsymmetricJsonWebKey::Private(super::Private::Okp(x)),
))
}
}
impl_internally_tagged_deserialize!(OkpPrivate, "crv", "OkpCurve", [
"Ed25519" => Ed25519,
"Ed448" => Ed448,
]);