@ -186,7 +186,8 @@ impl<V: ?Sized + MessageValue> Message<V>
let sign = if let Some ( sig ) = & serial . sig {
let sign = if let Some ( sig ) = & serial . sig {
// Validate signature
// Validate signature
assert_valid ! ( recv_with . verify_data ( & data [ .. ] , sig ) . ok_or ( eyre ! ( "Message was signed, but receiver doesn't support signature verification" ) ) ? . wrap_err ( "Failed to verify signature" ) ? ,
assert_valid ! ( recv_with . verify_data ( & data [ .. ] , sig ) . ok_or ( eyre ! ( "Message was signed, but receiver doesn't support signature verification" ) ) ? . wrap_err ( "Failed to verify signature" ) ? ,
"Non-matching signature" ) ? ;
"Non-matching signature" )
. with_section ( move | | format! ( "Embedded sig was: {}" , sig ) ) ? ;
true
true
} else {
} else {
false
false
@ -225,7 +226,8 @@ impl<V: ?Sized + MessageValue> SerializedMessage<V>
} ;
} ;
( : $ser :expr ) = > {
( : $ser :expr ) = > {
{
{
let v = serde_cbor ::to_vec ( $ser ) ? ;
let mut v = StackVec ::new ( ) ;
serde_cbor ::to_writer ( & mut v , $ser ) ? ;
write! ( & v [ .. ] ) ;
write! ( & v [ .. ] ) ;
}
}
}
}
@ -260,7 +262,8 @@ impl<V: ?Sized + MessageValue> SerializedMessage<V>
} ;
} ;
( : $ser :expr ) = > {
( : $ser :expr ) = > {
{
{
let ser = serde_cbor ::to_vec ( $ser ) ? ;
let mut ser = StackVec ::new ( ) ;
serde_cbor ::to_writer ( & mut ser , $ser ) ? ;
write! ( u64 ::try_from ( ser . len ( ) ) ? . to_be_bytes ( ) ) ;
write! ( u64 ::try_from ( ser . len ( ) ) ? . to_be_bytes ( ) ) ;
write! ( ser ) ;
write! ( ser ) ;
/*
/*
@ -285,8 +288,12 @@ impl<V: ?Sized + MessageValue> SerializedMessage<V>
pub fn from_reader ( mut reader : impl io ::Read ) -> eyre ::Result < Self >
pub fn from_reader ( mut reader : impl io ::Read ) -> eyre ::Result < Self >
{
{
macro_rules! read {
macro_rules! read {
( $b :expr ; $fmt :literal $( $tt :tt ) * ) = > {
read_all ( $b , & mut reader ) . wrap_err ( eyre ! ( $fmt $( $tt ) * ) )
} ;
( $b :expr ) = > {
( $b :expr ) = > {
read_all ( $b , & mut reader ) . wrap_err ( eyre ! ( "Failed to read from stream" ) ) ?
read ! ( $b ; "Failed to read from stream" ) ? ;
} ;
} ;
( ? $ot :expr ) = > {
( ? $ot :expr ) = > {
{
{
@ -314,11 +321,14 @@ impl<V: ?Sized + MessageValue> SerializedMessage<V>
let len = usize ::try_from ( u64 ::from_be_bytes ( len ) ) ? ;
let len = usize ::try_from ( u64 ::from_be_bytes ( len ) ) ? ;
//TODO: Find realistic max size for `$ser`.
//TODO: Find realistic max size for `$ser`.
if len > MAX_ALLOC_SIZE {
if len > MAX_ALLOC_SIZE {
return Err ( eyre ! ( "Invalid length read: {}" , len ) ) ;
return Err ( eyre ! ( "Invalid length read: {}" , len )
. with_section ( | | format! ( "Max length read: {}" , MAX_ALLOC_SIZE ) ) )
}
}
let mut de = Vec ::with_capacity ( len ) ;
alloc_local_bytes ( len , | de | {
read ! ( & mut de , len ) ;
read ! ( & mut de [ .. ] ) ;
serde_cbor ::from_slice ::< $ser > ( & de [ .. ] ) . wrap_err ( eyre ! ( "Failed to deserialise {} from reader" , std ::any ::type_name ::< $ser > ( ) ) ) ?
serde_cbor ::from_slice ::< $ser > ( & de [ .. ] ) . wrap_err ( eyre ! ( "Failed to deserialise {} from reader" , std ::any ::type_name ::< $ser > ( ) ) )
} ) ?
}
}
} ;
} ;
( $into :expr , $num :expr ) = > {
( $into :expr , $num :expr ) = > {
@ -380,7 +390,7 @@ mod tests
D : MessageReceiver
D : MessageReceiver
{
{
eprintln! ( "=== Message serialisation with tc, rc: S: {}, D: {}" , std ::any ::type_name ::< S > ( ) , std ::any ::type_name ::< D > ( ) ) ;
eprintln! ( "=== Message serialisation with tc, rc: S: {}, D: {}" , std ::any ::type_name ::< S > ( ) , std ::any ::type_name ::< D > ( ) ) ;
let message = MessageBuilder ::new ( )
let message = MessageBuilder ::for_sender::< S > ( )
. create ( format! ( "This is a string, and some random data: {:?}" , aes ::AesKey ::generate ( ) . unwrap ( ) ) )
. create ( format! ( "This is a string, and some random data: {:?}" , aes ::AesKey ::generate ( ) . unwrap ( ) ) )
. expect ( "Failed to create message" ) ;
. expect ( "Failed to create message" ) ;
println! ( ">> Created message: {:?}" , message ) ;
println! ( ">> Created message: {:?}" , message ) ;
@ -404,5 +414,89 @@ mod tests
message_serial_generic ( ( ) , ( ) ) ;
message_serial_generic ( ( ) , ( ) ) ;
}
}
#[ test ]
fn message_serial_sign ( )
{
let rsa_priv = rsa ::RsaPrivateKey ::generate ( ) . unwrap ( ) ;
struct Sign ( rsa ::RsaPrivateKey ) ;
struct Verify ( rsa ::RsaPublicKey ) ;
impl MessageSender for Sign
{
const CAP_SIGN : bool = true ;
fn sign_data ( & self , data : & [ u8 ] ) -> Option < rsa ::Signature > {
Some ( rsa ::sign_slice ( data , & self . 0 ) . expect ( "Failed to sign" ) )
}
}
impl MessageReceiver for Verify
{
fn verify_data ( & self , data : & [ u8 ] , sig : & rsa ::Signature ) -> Option < eyre ::Result < bool > > {
Some ( sig . verify_slice ( data , & self . 0 ) . map_err ( Into ::into ) )
}
}
let verify = Verify ( rsa_priv . get_public_parts ( ) ) ;
println! ( "Signing priv-key: {:?}" , rsa_priv ) ;
println! ( "Verifying pub-key: {:?}" , verify . 0 ) ;
message_serial_generic ( Sign ( rsa_priv ) , verify ) ;
}
#[ test ]
fn message_serial_encrypt ( )
{
color_eyre ::install ( ) . unwrap ( ) ;
let rsa_priv = rsa ::RsaPrivateKey ::generate ( ) . unwrap ( ) ;
struct Dec ( rsa ::RsaPrivateKey ) ;
struct Enc ( rsa ::RsaPublicKey ) ;
impl MessageSender for Enc
{
const CAP_ENCRYPT : bool = true ;
fn encrypt_key ( & self , key : & aes ::AesKey ) -> Option < [ u8 ; RSA_BLOCK_SIZE ] > {
let mut output = [ 0 u8 ; RSA_BLOCK_SIZE ] ;
use rsa ::HasPublicComponents ;
let w = rsa ::encrypt_slice_sync ( key , & self . 0 , & mut & mut output [ .. ] ) . expect ( "Failed to encrypt session key" ) ;
assert_eq! ( w , output . len ( ) ) ;
Some ( output )
}
}
impl MessageReceiver for Dec
{
fn decrypt_key ( & self , enc_key : & [ u8 ; RSA_BLOCK_SIZE ] ) -> Option < eyre ::Result < aes ::AesKey > > {
let mut output = aes ::AesKey ::empty ( ) ;
match rsa ::decrypt_slice_sync ( enc_key , & self . 0 , & mut output . as_mut ( ) ) {
Ok ( sz ) = > assert_eq! ( sz ,
output . as_ref ( ) . len ( ) ) ,
Err ( err ) = > return Some ( Err ( err . into ( ) ) ) ,
}
Some ( Ok ( output ) )
}
}
let enc = Enc ( rsa_priv . get_public_parts ( ) ) ;
println! ( "Encrypting pub-key: {:?}" , enc . 0 ) ;
println! ( "Decrypting priv-key: {:?}" , rsa_priv ) ;
message_serial_generic ( enc , Dec ( rsa_priv ) ) ;
}
#[ test ]
fn rsa_bullshit ( )
{
use rsa ::HasPublicComponents ;
let rsa = rsa ::openssl ::rsa ::Rsa ::generate ( cryptohelpers ::consts ::RSA_KEY_BITS ) . unwrap ( ) ;
eprintln! ( "rn: {}, re: {}," , rsa . n ( ) , rsa . e ( ) ) ;
let rsa_comp : rsa ::RsaPrivateKey = rsa . clone ( ) . into ( ) ;
eprintln! ( "n: {:?}, e: {:?}" , rsa_comp . n ( ) , rsa_comp . e ( ) ) ;
let rsa2 : rsa ::openssl ::rsa ::Rsa < _ > = rsa_comp . get_rsa_pub ( ) . unwrap ( ) ;
eprintln! ( "n2: {}, e2: {}" , rsa2 . n ( ) , rsa2 . e ( ) ) ;
let mut data = Vec ::new ( ) ;
data . extend_from_slice ( aes ::AesKey ::generate ( ) . unwrap ( ) . as_ref ( ) ) ;
let rend = rsa ::encrypt_slice_to_vec ( data , & rsa2 ) . unwrap ( ) ;
assert_eq! ( rend . len ( ) , RSA_BLOCK_SIZE ) ;
}
//TODO: message_serial_sign(), message_serial_encrypted(), message_serial_encrypted_sign()
//TODO: message_serial_sign(), message_serial_encrypted(), message_serial_encrypted_sign()
}
}