parent
f24116ded9
commit
b8d549bebb
@ -0,0 +1,187 @@
|
|||||||
|
//! RSA Private key serialisation
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
/// A container of RSA private key
|
||||||
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
pub struct RsaPrivateBody
|
||||||
|
{
|
||||||
|
garbage: [u8; PADDING_SZ],
|
||||||
|
key: rsa::RsaPrivateKey,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl RsaPrivateBody
|
||||||
|
{
|
||||||
|
/// Create a new body container from this RSA private key
|
||||||
|
pub fn new_key(key: rsa::RsaPrivateKey) -> Self
|
||||||
|
{
|
||||||
|
Self {
|
||||||
|
garbage: new_padding(),
|
||||||
|
key,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create an instace of public body from the public parts of this private key
|
||||||
|
pub fn get_public(&self) -> RsaPublicBody
|
||||||
|
{
|
||||||
|
RsaPublicBody::new_key(self.key.get_public_parts())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create a new instance with new padding
|
||||||
|
pub fn with_padding(self) -> Self
|
||||||
|
{
|
||||||
|
Self {
|
||||||
|
garbage: new_padding(),
|
||||||
|
..self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// Give this body new padding
|
||||||
|
pub fn give_padding(&mut self)
|
||||||
|
{
|
||||||
|
self.garbage = new_padding();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Consume into the internal key
|
||||||
|
pub fn into_key(self) -> rsa::RsaPrivateKey
|
||||||
|
{
|
||||||
|
self.key
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The internal key
|
||||||
|
pub fn key(&self) -> &rsa::RsaPrivateKey
|
||||||
|
{
|
||||||
|
&self.key
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Mutable reference to the internal key
|
||||||
|
pub fn key_mut(&mut self) -> &mut rsa::RsaPrivateKey
|
||||||
|
{
|
||||||
|
&mut self.key
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Write this body's key to a PEM string
|
||||||
|
pub fn to_pem_string(&self, pw: Option<&Password>) -> eyre::Result<String>
|
||||||
|
{
|
||||||
|
self.key.to_pem(pw)
|
||||||
|
.wrap_err_with(|| eyre!("Failed to write private key body to PEM string"))
|
||||||
|
.with_section(|| format!("{:?}", self.key).header("Key body data was"))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Read this body from a PEM string encoded key
|
||||||
|
///
|
||||||
|
/// # Notes
|
||||||
|
/// This creates new padding for the returned body
|
||||||
|
pub fn from_pem<F: FnOnce() -> Option<Password>>(pem: impl AsRef<str>, pw: F) -> eyre::Result<Self>
|
||||||
|
{
|
||||||
|
let pem = pem.as_ref();
|
||||||
|
Ok(Self::new_key(rsa::RsaPrivateKey::from_pem(pem, pw)
|
||||||
|
.wrap_err_with(|| eyre!("Failed to read private key body from PEM string"))
|
||||||
|
.with_section(|| pem.to_owned().header("PEM string was"))?))
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Write RSA private body to a binary stream
|
||||||
|
#[instrument(skip(out), err)]
|
||||||
|
pub async fn write_bytes<T: AsyncWrite+?Sized+Unpin>(&self, out: &mut T, ses_enc: &AesKey) -> Result<usize, eyre::Report>
|
||||||
|
{
|
||||||
|
if self.garbage == [0u8; PADDING_SZ] {
|
||||||
|
warn!("Writing empty-padded body");
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut buf: Vec<u8> = self.key.to_bytes();
|
||||||
|
buf.extend_from_slice(&self.garbage[..]);
|
||||||
|
|
||||||
|
let mut tbuf = Vec::with_capacity(buf.len()); //eh, i need to do calculating instead of these dumb temp buffers...
|
||||||
|
aes::encrypt_stream(ses_enc, &mut &buf[..], &mut tbuf).await
|
||||||
|
.wrap_err_with(|| eyre!("Failed to encrypt body stream with header key"))
|
||||||
|
.with_section(|| format!("{}", buf.fmt_view()).header("Body stream was"))
|
||||||
|
.with_section(|| ses_enc.to_string().header("Header key was"))?;
|
||||||
|
|
||||||
|
out.write_u64(try_usize!(<- tbuf.len())?).await?;
|
||||||
|
out.write_all(&tbuf[..]).await?;
|
||||||
|
Ok(8 + tbuf.len())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Read an RSA private body from a binary stream
|
||||||
|
#[instrument(skip(input), err)]
|
||||||
|
pub async fn read_bytes<T: AsyncRead+?Sized+Unpin>(input: &mut T, ses_enc: &AesKey) -> Result<Self, eyre::Report>
|
||||||
|
{
|
||||||
|
let sz = try_usize!(-> input.read_u64().await?)?;
|
||||||
|
let mut tbuf = vec![0u8; sz];
|
||||||
|
input.read_exact(&mut tbuf[..]).await?;
|
||||||
|
|
||||||
|
let mut output = Vec::with_capacity(tbuf.len());
|
||||||
|
|
||||||
|
aes::decrypt_stream(ses_enc, &mut &tbuf[..], &mut output).await
|
||||||
|
.wrap_err_with(|| eyre!("Failed to decrypt body stream with header key"))
|
||||||
|
.with_section(|| format!("{}", tbuf.fmt_view()).header("Body stream was"))
|
||||||
|
.with_section(|| ses_enc.to_string().header("Header key was"))?;
|
||||||
|
|
||||||
|
if output.len() < PADDING_SZ {
|
||||||
|
return Err(eyre!("Padding is invalid"))
|
||||||
|
.with_section(|| tbuf.to_view_string().header("Decrypted body stream was"));
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut garbage = [0u8; PADDING_SZ];
|
||||||
|
bytes::copy_slice(&mut garbage[..], &output[output.len()-PADDING_SZ..]);
|
||||||
|
|
||||||
|
let key = rsa::RsaPrivateKey::from_bytes(&output[..output.len()-PADDING_SZ])
|
||||||
|
.wrap_err_with(|| eyre!("Failed to instantiate key from body"))
|
||||||
|
.with_section(|| tbuf.to_view_string().header("Decrypted body stream was"))?;
|
||||||
|
|
||||||
|
Ok(Self {
|
||||||
|
key, garbage
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Write an RSA private body to a text stream
|
||||||
|
#[instrument(err, skip(out))]
|
||||||
|
pub async fn write_text<T: AsyncWrite+Unpin+?Sized>(&self, out: &mut T, ses_enc: &AesKey) -> Result<usize, eyre::Report>
|
||||||
|
{
|
||||||
|
let text = serialise::into_text_with_key_async(self, ses_enc).await
|
||||||
|
.wrap_err_with(|| eyre!("Failed to serialise body to text"))
|
||||||
|
.with_section(|| format!("{:?}", self.key).header("Body key was"))
|
||||||
|
.with_section(|| ses_enc.to_string().header("Header key was"))?;
|
||||||
|
|
||||||
|
Ok(write_chunked_text!(out, text))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Read an RSA private body from a text stream
|
||||||
|
#[instrument(err, skip(input))]
|
||||||
|
pub async fn read_text<T: AsyncBufRead+Unpin+?Sized>(input: &mut T, ses_enc: &AesKey) -> Result<Self, eyre::Report>
|
||||||
|
{
|
||||||
|
let whole = read_chunked_text!(input);
|
||||||
|
Ok(serialise::from_text_with_key_async(&whole[..], ses_enc).await
|
||||||
|
.wrap_err_with(|| eyre!("Failed to deserialise body from text"))
|
||||||
|
.with_section(move|| whole.header("Body was"))
|
||||||
|
.with_section(|| ses_enc.to_string().header("Header key was"))?)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
impl KeyBodySerialisable for RsaPrivateBody
|
||||||
|
{
|
||||||
|
#[inline] fn serialise_text<'a, 'b: 'a, T: AsyncWrite+Unpin+?Sized>(&'a self, out: &'b mut T, ses_enc: &'b AesKey) -> LocalBoxFuture<'a, Result<usize, eyre::Report>>
|
||||||
|
{
|
||||||
|
self.write_text(out, ses_enc).boxed_local()
|
||||||
|
}
|
||||||
|
#[inline] fn serialise_bytes<'a, 'b: 'a, T: AsyncWrite+Unpin+?Sized>(&'a self, out: &'b mut T, ses_enc: &'b AesKey) -> LocalBoxFuture<'a, Result<usize, eyre::Report>>
|
||||||
|
{
|
||||||
|
self.write_bytes(out, ses_enc).boxed_local()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl KeyBodyDeserialisable for RsaPrivateBody
|
||||||
|
{
|
||||||
|
#[inline] fn deserialise_bytes<'a, T: AsyncBufRead+Unpin+?Sized>(input: &'a mut T, ses_enc: &'a AesKey) -> LocalBoxFuture<'a, Result<Self, eyre::Report>>
|
||||||
|
where Self: 'a
|
||||||
|
{
|
||||||
|
Self::read_bytes(input, ses_enc).boxed_local()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline] fn deserialise_text<'a, T: AsyncBufRead+Unpin+?Sized>(input: &'a mut T, ses_enc: &'a AesKey) -> LocalBoxFuture<'a, Result<Self, eyre::Report>>
|
||||||
|
where Self: 'a
|
||||||
|
{
|
||||||
|
Self::read_text(input, ses_enc).boxed_local()
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue