Skip to content

Commit 2aea40c

Browse files
committed
asymmetric: implement the restrictions allowed with the keytype
1 parent bddf0ec commit 2aea40c

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

src/api.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ impl Keyring {
101101
}
102102
}
103103

104-
#[cfg(test)]
105104
pub(crate) fn serial(&self) -> KeyringSerial {
106105
self.id
107106
}
@@ -530,7 +529,6 @@ impl Key {
530529
}
531530
}
532531

533-
#[cfg(test)]
534532
pub(crate) fn serial(&self) -> KeyringSerial {
535533
self.id
536534
}

src/keytypes/asymmetric.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@
2626

2727
//! Asymmetric keys
2828
29+
use std::borrow::Cow;
30+
2931
use crate::keytype::*;
32+
use crate::{Key, Keyring, KeyringSerial};
3033

3134
/// Asymmetric keys support encrypting, decrypting, signing, and verifying data.
3235
///
@@ -54,3 +57,62 @@ impl KeyType for Asymmetric {
5457
"asymmetric"
5558
}
5659
}
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

Comments
 (0)