1
0
mirror of https://github.com/BitskiCo/jwk-rs synced 2024-11-24 21:09:22 +00:00
jwk-rs/src/utils.rs

90 lines
2.7 KiB
Rust
Raw Normal View History

2020-07-12 18:57:57 +00:00
use serde::{
de::{self, Deserialize, Deserializer},
ser::{Serialize, Serializer},
};
use zeroize::Zeroizing;
fn base64_config() -> base64::Config {
base64::Config::new(base64::CharacterSet::UrlSafe, true /* pad */)
}
fn base64_encode(bytes: impl AsRef<[u8]>) -> String {
base64::encode_config(bytes, base64_config())
}
fn base64_decode(b64: impl AsRef<[u8]>) -> Result<Vec<u8>, base64::DecodeError> {
base64::decode_config(b64, base64_config())
}
2021-01-25 17:11:01 +00:00
pub(crate) mod serde_base64 {
use super::*;
pub(crate) fn serialize<S: Serializer>(
bytes: impl AsRef<[u8]>,
s: S,
) -> Result<S::Ok, S::Error> {
base64_encode(bytes).serialize(s)
}
2020-07-12 18:57:57 +00:00
2021-01-25 17:11:01 +00:00
pub(crate) fn deserialize<'de, D: Deserializer<'de>>(d: D) -> Result<Vec<u8>, D::Error> {
let base64_str = Zeroizing::new(String::deserialize(d)?);
base64_decode(&*base64_str).map_err(|e| {
#[cfg(debug_assertions)]
let err_msg = e.to_string().to_lowercase();
#[cfg(not(debug_assertions))]
let err_msg = "invalid base64";
de::Error::custom(err_msg.strip_suffix(".").unwrap_or(&err_msg))
})
}
2020-07-12 18:57:57 +00:00
}
2020-07-13 20:46:42 +00:00
2020-07-16 02:32:11 +00:00
#[cfg(feature = "pkcs-convert")]
2021-01-25 17:11:01 +00:00
pub(crate) mod pkcs8 {
2020-07-13 20:46:42 +00:00
use yasna::{
models::{ObjectIdentifier, TaggedDerValue},
DERWriter, DERWriterSeq,
};
2021-01-25 17:11:01 +00:00
fn write_oids(writer: &mut DERWriterSeq<'_>, oids: &[Option<&ObjectIdentifier>]) {
2020-07-13 20:46:42 +00:00
for oid in oids {
match oid {
Some(oid) => writer.next().write_oid(oid),
None => writer.next().write_null(),
}
}
}
2021-01-25 17:11:01 +00:00
pub(crate) fn write_private(
2020-07-13 20:46:42 +00:00
oids: &[Option<&ObjectIdentifier>],
2021-01-25 17:11:01 +00:00
body_writer: impl FnOnce(&mut DERWriterSeq<'_>),
2020-07-13 20:46:42 +00:00
) -> Vec<u8> {
yasna::construct_der(|writer| {
writer.write_sequence(|writer| {
writer.next().write_i8(0); // version
writer
.next()
.write_sequence(|writer| write_oids(writer, oids));
let body = yasna::construct_der(|writer| writer.write_sequence(body_writer));
writer
.next()
.write_tagged_der(&TaggedDerValue::from_octetstring(body));
})
})
}
2021-01-25 17:11:01 +00:00
pub(crate) fn write_public(
2020-07-13 20:46:42 +00:00
oids: &[Option<&ObjectIdentifier>],
2021-01-25 17:11:01 +00:00
body_writer: impl FnOnce(DERWriter<'_>),
2020-07-13 20:46:42 +00:00
) -> Vec<u8> {
yasna::construct_der(|writer| {
writer.write_sequence(|writer| {
writer
.next()
.write_sequence(|writer| write_oids(writer, oids));
body_writer(writer.next());
})
})
}
}