|
26 | 26 |
|
27 | 27 | //! Asymmetric keys
|
28 | 28 |
|
| 29 | +use std::borrow::Cow; |
| 30 | + |
29 | 31 | use crate::keytype::*;
|
| 32 | +use crate::{Key, Keyring, KeyringSerial}; |
30 | 33 |
|
31 | 34 | /// Asymmetric keys support encrypting, decrypting, signing, and verifying data.
|
32 | 35 | ///
|
@@ -54,3 +57,62 @@ impl KeyType for Asymmetric {
|
54 | 57 | "asymmetric"
|
55 | 58 | }
|
56 | 59 | }
|
| 60 | + |
| 61 | +/// A restriction that may be placed onto a keyring using an asymmetric key. |
| 62 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 63 | +pub enum AsymmetricRestriction { |
| 64 | + /// Only allow keys which have been signed by a key on the builtin trusted keyring. |
| 65 | + BuiltinTrusted, |
| 66 | + /// Only allow keys which have been signed by a key on the builtin or secondary trusted |
| 67 | + /// keyrings. |
| 68 | + BuiltinAndSecondaryTrusted, |
| 69 | + /// Only allow keys which have been signed by the given key. |
| 70 | + Key { |
| 71 | + /// The signing key. |
| 72 | + key: Key, |
| 73 | + /// Whether or not chaining should be used (see `Chained`). |
| 74 | + chained: bool, |
| 75 | + }, |
| 76 | + /// Only allow keys which have been signed by a key on the given keyring. |
| 77 | + Keyring { |
| 78 | + /// The keyring with permitted signing keys. |
| 79 | + keyring: Keyring, |
| 80 | + /// Whether or not chaining should be used (see `Chained`). |
| 81 | + chained: bool, |
| 82 | + }, |
| 83 | + /// When chaining the destination keyring is also searched for signing keys. |
| 84 | + /// |
| 85 | + /// This allows building up a chain of trust in the destination keyring. |
| 86 | + Chained, |
| 87 | +} |
| 88 | + |
| 89 | +impl AsymmetricRestriction { |
| 90 | + fn restriction_str(id: KeyringSerial, chained: bool) -> String { |
| 91 | + let chain_suffix = if chained { ":chain" } else { "" }; |
| 92 | + format!("key_or_keyring:{}{}", id, chain_suffix) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +impl KeyRestriction for AsymmetricRestriction { |
| 97 | + fn restriction(&self) -> Cow<str> { |
| 98 | + match self { |
| 99 | + AsymmetricRestriction::BuiltinTrusted => "builtin_trusted".into(), |
| 100 | + AsymmetricRestriction::BuiltinAndSecondaryTrusted => { |
| 101 | + "builtin_and_secondary_trusted".into() |
| 102 | + }, |
| 103 | + AsymmetricRestriction::Key { |
| 104 | + key, |
| 105 | + chained, |
| 106 | + } => Self::restriction_str(key.serial(), *chained).into(), |
| 107 | + AsymmetricRestriction::Keyring { |
| 108 | + keyring, |
| 109 | + chained, |
| 110 | + } => Self::restriction_str(keyring.serial(), *chained).into(), |
| 111 | + AsymmetricRestriction::Chained => "key_or_keyring:0:chain".into(), |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +impl RestrictableKeyType for Asymmetric { |
| 117 | + type Restriction = AsymmetricRestriction; |
| 118 | +} |
0 commit comments