use alloc::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 Public {
Rsa(rsa::PublicKey),
Ec(EcPublic),
Okp(OkpPublic),
}
impl crate::sealed::Sealed for Public {}
impl Thumbprint for Public {
fn thumbprint_prehashed(&self) -> String {
match self {
Public::Rsa(key) => key.thumbprint_prehashed(),
Public::Ec(key) => key.thumbprint_prehashed(),
Public::Okp(key) => key.thumbprint_prehashed(),
}
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Serialize, PartialEq, Eq, Hash)]
#[serde(untagged)]
pub enum EcPublic {
P256(ec::P256PublicKey),
P384(ec::P384PublicKey),
P521(ec::P521PublicKey),
Secp256k1(ec::Secp256k1PublicKey),
}
impl crate::sealed::Sealed for EcPublic {}
impl Thumbprint for EcPublic {
fn thumbprint_prehashed(&self) -> String {
match self {
EcPublic::P256(key) => key.thumbprint_prehashed(),
EcPublic::P384(key) => key.thumbprint_prehashed(),
EcPublic::P521(key) => key.thumbprint_prehashed(),
EcPublic::Secp256k1(key) => key.thumbprint_prehashed(),
}
}
}
impl From<EcPublic> for super::JsonWebKeyType {
fn from(x: EcPublic) -> Self {
super::JsonWebKeyType::Asymmetric(alloc::boxed::Box::new(
super::AsymmetricJsonWebKey::Public(super::Public::Ec(x)),
))
}
}
impl_internally_tagged_deserialize!(EcPublic, "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 OkpPublic {
Ed25519(okp::Ed25519PublicKey),
Ed448(okp::Ed448PublicKey),
}
impl crate::sealed::Sealed for OkpPublic {}
impl Thumbprint for OkpPublic {
fn thumbprint_prehashed(&self) -> String {
match self {
OkpPublic::Ed25519(key) => key.thumbprint_prehashed(),
OkpPublic::Ed448(key) => key.thumbprint_prehashed(),
}
}
}
impl From<OkpPublic> for super::JsonWebKeyType {
fn from(x: OkpPublic) -> Self {
super::JsonWebKeyType::Asymmetric(alloc::boxed::Box::new(
super::AsymmetricJsonWebKey::Public(super::Public::Okp(x)),
))
}
}
impl_internally_tagged_deserialize!(OkpPublic, "crv", "OkpCurve", [
"Ed25519" => Ed25519,
"Ed448" => Ed448,
]);