diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2023-11-15 13:33:30 +0100 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2023-11-15 13:33:30 +0100 |
commit | b5f0f06f41d0bdc1800e9b8796b71c2d42296bf1 (patch) | |
tree | 40c4838f9fcb6cb689b003b6e55749f91ebc5a86 | |
parent | ebcbb15cba6d9b668a789034955e458f6f7c8177 (diff) |
openpgp: Add Key<P, R> conversion for mutable references.
- This is safe, as it is not possible to change the shape of the
data through a mutable reference (e.g. it is not possible to take
the secret key material away).
-rw-r--r-- | openpgp/src/packet/key/conversions.rs | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/openpgp/src/packet/key/conversions.rs b/openpgp/src/packet/key/conversions.rs index 150e1e9a..7c8b8c8c 100644 --- a/openpgp/src/packet/key/conversions.rs +++ b/openpgp/src/packet/key/conversions.rs @@ -113,6 +113,14 @@ macro_rules! create_part_conversions { convert_ref!(p) } } + + impl<$($l, )* $($g, )* > From<&$($l)* mut $Key<$($l, )* $from_parts, $($g, )* >> for &$($l)* mut $Key<$($l, )* $to_parts, $($g, )* > + where $($w: $c ),* + { + fn from(p: &$($l)* mut $Key<$($l, )* $from_parts, $($g, )* >) -> Self { + convert_ref!(p) + } + } } } @@ -141,6 +149,20 @@ macro_rules! create_part_conversions { } } } + + impl<$($l, )* $($g, )* > TryFrom<&$($l)* mut $Key<$($l, )* $from_parts, $($g, )* >> for &$($l)* mut $Key<$($l, )* $to_parts, $($g, )* > + where $($w: $c ),* + { + type Error = anyhow::Error; + fn try_from(p: &$($l)* mut $Key<$($l, )* $from_parts, $($g, )* >) -> Result<Self> { + if p.has_secret() { + Ok(convert_ref!(p)) + } else { + Err(Error::InvalidArgument("No secret key".into()) + .into()) + } + } + } } } @@ -187,6 +209,11 @@ macro_rules! create_part_conversions { convert_ref!(self) } + /// Changes the key's parts tag to `PublicParts`. + pub fn parts_as_public_mut(&$($l)* mut self) -> &$($l)* mut $Key<$($l, )* PublicParts, $($g, )*> { + convert_ref!(self) + } + /// Changes the key's parts tag to `SecretParts`. pub fn parts_into_secret(self) -> Result<$Key<$($l, )* SecretParts, $($g, )*>> { if self.has_secret() { @@ -206,6 +233,16 @@ macro_rules! create_part_conversions { } } + /// Changes the key's parts tag to `SecretParts`. + pub fn parts_as_secret_mut(&$($l)* mut self) -> Result<&$($l)* mut $Key<$($l, )* SecretParts, $($g, )*>> + { + if self.has_secret() { + Ok(convert_ref!(self)) + } else { + Err(Error::InvalidArgument("No secret key".into()).into()) + } + } + /// Changes the key's parts tag to `UnspecifiedParts`. pub fn parts_into_unspecified(self) -> $Key<$($l, )* UnspecifiedParts, $($g, )*> { convert!(self) @@ -215,6 +252,11 @@ macro_rules! create_part_conversions { pub fn parts_as_unspecified(&$($l)* self) -> &$Key<$($l, )* UnspecifiedParts, $($g, )*> { convert_ref!(self) } + + /// Changes the key's parts tag to `UnspecifiedParts`. + pub fn parts_as_unspecified_mut(&$($l)* mut self) -> &mut $Key<$($l, )* UnspecifiedParts, $($g, )*> { + convert_ref!(self) + } } } } @@ -240,6 +282,14 @@ macro_rules! create_role_conversions { convert_ref!(p) } } + + impl<$($l, )* P> From<&$($l)* mut $Key<$($l, )* P, $from_role>> for &$($l)* mut $Key<$($l, )* P, $to_role> + where P: KeyParts + { + fn from(p: &$($l)* mut $Key<$($l, )* P, $from_role>) -> Self { + convert_ref!(p) + } + } } } @@ -277,6 +327,13 @@ macro_rules! create_conversions { convert_ref!(p) } } + + impl<$($l ),*> From<&$($l)* mut $Key<$($l, )* $from_parts, $from_role>> for &$($l)* mut $Key<$($l, )* $to_parts, $to_role> + { + fn from(p: &$($l)* mut $Key<$from_parts, $from_role>) -> Self { + convert_ref!(p) + } + } } } @@ -387,6 +444,11 @@ macro_rules! create_conversions { convert_ref!(self) } + /// Changes the key's role tag to `PrimaryRole`. + pub fn role_as_primary_mut(&$($l)* mut self) -> &$($l)* mut $Key<$($l, )* P, PrimaryRole> { + convert_ref!(self) + } + /// Changes the key's role tag to `SubordinateRole`. pub fn role_into_subordinate(self) -> $Key<$($l, )* P, SubordinateRole> { @@ -399,6 +461,12 @@ macro_rules! create_conversions { convert_ref!(self) } + /// Changes the key's role tag to `SubordinateRole`. + pub fn role_as_subordinate_mut(&$($l)* mut self) -> &$($l)* mut $Key<$($l, )* P, SubordinateRole> + { + convert_ref!(self) + } + /// Changes the key's role tag to `UnspecifiedRole`. pub fn role_into_unspecified(self) -> $Key<$($l, )* P, UnspecifiedRole> { @@ -410,6 +478,12 @@ macro_rules! create_conversions { { convert_ref!(self) } + + /// Changes the key's role tag to `UnspecifiedRole`. + pub fn role_as_unspecified_mut(&$($l)* mut self) -> &$($l)* mut $Key<$($l, )* P, UnspecifiedRole> + { + convert_ref!(self) + } } } } |