| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
use crate::cbor::{self, Value}; |
| 14 |
use crate::hash::{hash_object, KeyId, ObjectRef, ObjectType}; |
| 15 |
use crate::identity::{verify, Domain, Identity, Scope, SecretKey, Signature}; |
| 16 |
use crate::objects::{array, bytes, check_version, field, map, oref, text, uint, ObjectError}; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
pub const MAX_LIFETIME_MS: u64 = 60 * 60 * 1000; |
| 21 |
|
| 22 |
#[derive(Clone, Debug, PartialEq, Eq)] |
| 23 |
pub struct Certificate { |
| 24 |
|
| 25 |
pub key: Identity, |
| 26 |
|
| 27 |
pub issuer: KeyId, |
| 28 |
|
| 29 |
pub subject: String, |
| 30 |
|
| 31 |
pub claims: Vec<(String, String)>, |
| 32 |
|
| 33 |
pub scope: Scope, |
| 34 |
|
| 35 |
pub not_before: u64, |
| 36 |
pub expires: u64, |
| 37 |
} |
| 38 |
|
| 39 |
impl Certificate { |
| 40 |
pub fn to_value(&self) -> Value { |
| 41 |
let mut claims = self.claims.clone(); |
| 42 |
claims.sort(); |
| 43 |
Value::map([ |
| 44 |
(0, Value::Uint(1)), |
| 45 |
(1, Value::Bytes(self.key.encode())), |
| 46 |
(2, Value::Bytes(self.issuer.0.to_vec())), |
| 47 |
(3, Value::Text(self.subject.clone())), |
| 48 |
( |
| 49 |
4, |
| 50 |
Value::Array( |
| 51 |
claims |
| 52 |
.into_iter() |
| 53 |
.map(|(k, v)| Value::Array(vec![Value::Text(k), Value::Text(v)])) |
| 54 |
.collect(), |
| 55 |
), |
| 56 |
), |
| 57 |
( |
| 58 |
5, |
| 59 |
Value::Array( |
| 60 |
self.scope |
| 61 |
.names() |
| 62 |
.into_iter() |
| 63 |
.map(|n| Value::Text(n.to_string())) |
| 64 |
.collect(), |
| 65 |
), |
| 66 |
), |
| 67 |
(6, Value::Uint(self.not_before)), |
| 68 |
(7, Value::Uint(self.expires)), |
| 69 |
]) |
| 70 |
} |
| 71 |
|
| 72 |
pub fn encode(&self) -> Vec<u8> { |
| 73 |
cbor::encode(&self.to_value()) |
| 74 |
} |
| 75 |
|
| 76 |
pub fn hash(&self) -> ObjectRef { |
| 77 |
hash_object(ObjectType::Certificate, &self.encode()) |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
pub fn key_id(&self) -> KeyId { |
| 82 |
self.key.hash() |
| 83 |
} |
| 84 |
|
| 85 |
pub fn decode(b: &[u8]) -> Result<Certificate, ObjectError> { |
| 86 |
let v = cbor::decode(b)?; |
| 87 |
let m = map(&v, "certificate")?; |
| 88 |
check_version(m)?; |
| 89 |
let key = Identity::decode(bytes(field(m, 1, "key")?, "key")?)?; |
| 90 |
let mut claims = Vec::new(); |
| 91 |
for c in array(field(m, 4, "claims")?, "claims")? { |
| 92 |
let pair = array(c, "claim")?; |
| 93 |
if pair.len() != 2 { |
| 94 |
return Err(ObjectError::Schema("claim: expected [name, value]".into())); |
| 95 |
} |
| 96 |
claims.push(( |
| 97 |
text(&pair[0], "claim name")?.to_owned(), |
| 98 |
text(&pair[1], "claim value")?.to_owned(), |
| 99 |
)); |
| 100 |
} |
| 101 |
let mut scope = Scope::NONE; |
| 102 |
for n in array(field(m, 5, "scope")?, "scope")? { |
| 103 |
let n = text(n, "scope")?; |
| 104 |
scope = scope |
| 105 |
| Scope::named(n).ok_or_else(|| { |
| 106 |
ObjectError::Schema(format!("certificate: unknown scope {n:?}")) |
| 107 |
})?; |
| 108 |
} |
| 109 |
Ok(Certificate { |
| 110 |
key, |
| 111 |
issuer: oref(field(m, 2, "issuer")?, "issuer")?, |
| 112 |
subject: text(field(m, 3, "subject")?, "subject")?.to_owned(), |
| 113 |
claims, |
| 114 |
scope, |
| 115 |
not_before: uint(field(m, 6, "not_before")?, "not_before")?, |
| 116 |
expires: uint(field(m, 7, "expires")?, "expires")?, |
| 117 |
}) |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
pub fn covers(&self, scope: Scope, at_ms: u64) -> bool { |
| 123 |
!scope.is_empty() |
| 124 |
&& self.scope.has(scope) |
| 125 |
&& self.not_before <= at_ms |
| 126 |
&& at_ms <= self.expires |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
pub fn claim(&self, name: &str) -> Option<&str> { |
| 131 |
self.claims |
| 132 |
.iter() |
| 133 |
.find(|(k, _)| k == name) |
| 134 |
.map(|(_, v)| v.as_str()) |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
pub fn certify_challenge(credential: &str) -> Vec<u8> { |
| 142 |
let mut c = b"oxy1 certify ".to_vec(); |
| 143 |
c.extend_from_slice(blake3::hash(credential.as_bytes()).as_bytes()); |
| 144 |
c |
| 145 |
} |
| 146 |
|
| 147 |
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)] |
| 148 |
pub enum CertError { |
| 149 |
#[error("the certificate names issuer {0}, not this one")] |
| 150 |
WrongIssuer(String), |
| 151 |
#[error("the issuer's signature does not verify: {0}")] |
| 152 |
BadSignature(String), |
| 153 |
#[error("the certificate is valid for no time, or for longer than an hour")] |
| 154 |
BadLifetime, |
| 155 |
#[error("the certificate gives no scope")] |
| 156 |
NoScope, |
| 157 |
#[error("{0}")] |
| 158 |
Schema(String), |
| 159 |
} |
| 160 |
|
| 161 |
impl From<ObjectError> for CertError { |
| 162 |
fn from(e: ObjectError) -> Self { |
| 163 |
CertError::Schema(e.to_string()) |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
#[derive(Clone, Debug, PartialEq, Eq)] |
| 169 |
pub struct SignedCertificate { |
| 170 |
pub cert: Certificate, |
| 171 |
pub sig: Signature, |
| 172 |
} |
| 173 |
|
| 174 |
impl SignedCertificate { |
| 175 |
|
| 176 |
pub fn sign(mut cert: Certificate, issuer: &Identity, sk: &SecretKey) -> SignedCertificate { |
| 177 |
|
| 178 |
cert.claims.sort(); |
| 179 |
let sig = sk.sign(Domain::Certificate, &issuer.hash(), &cert.hash()); |
| 180 |
SignedCertificate { cert, sig } |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
pub fn verify(&self, issuer: &Identity) -> Result<(), CertError> { |
| 186 |
let c = &self.cert; |
| 187 |
if issuer.hash() != c.issuer || self.sig.key != c.issuer { |
| 188 |
return Err(CertError::WrongIssuer(c.issuer.short(12))); |
| 189 |
} |
| 190 |
verify(issuer, &self.sig, Domain::Certificate, &c.hash()) |
| 191 |
.map_err(|e| CertError::BadSignature(e.to_string()))?; |
| 192 |
if c.expires <= c.not_before || c.expires - c.not_before > MAX_LIFETIME_MS { |
| 193 |
return Err(CertError::BadLifetime); |
| 194 |
} |
| 195 |
if c.scope.is_empty() { |
| 196 |
return Err(CertError::NoScope); |
| 197 |
} |
| 198 |
Ok(()) |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
pub fn encode(&self) -> Vec<u8> { |
| 203 |
cbor::encode(&Value::Array(vec![ |
| 204 |
Value::Bytes(self.cert.encode()), |
| 205 |
self.sig.to_value(), |
| 206 |
])) |
| 207 |
} |
| 208 |
|
| 209 |
pub fn decode(b: &[u8]) -> Result<SignedCertificate, CertError> { |
| 210 |
let v = cbor::decode(b).map_err(ObjectError::from)?; |
| 211 |
let a = array(&v, "signed certificate")?; |
| 212 |
if a.len() != 2 { |
| 213 |
return Err(CertError::Schema( |
| 214 |
"signed certificate: expected [certificate, signature]".into(), |
| 215 |
)); |
| 216 |
} |
| 217 |
Ok(SignedCertificate { |
| 218 |
cert: Certificate::decode(bytes(&a[0], "certificate")?)?, |
| 219 |
sig: Signature::from_value(&a[1])?, |
| 220 |
}) |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
#[cfg(test)] |
| 225 |
mod tests { |
| 226 |
use super::*; |
| 227 |
|
| 228 |
fn issue( |
| 229 |
issuer_sk: &SecretKey, |
| 230 |
issuer: &Identity, |
| 231 |
minutes: u64, |
| 232 |
) -> (SecretKey, SignedCertificate) { |
| 233 |
let job_sk = SecretKey::generate(); |
| 234 |
let cert = Certificate { |
| 235 |
key: job_sk.identity("job 12 build", "[email protected]", 1_000), |
| 236 |
issuer: issuer.hash(), |
| 237 |
subject: "repo:andy/demo:bookmark:main".into(), |
| 238 |
claims: vec![ |
| 239 |
("run".into(), "12".into()), |
| 240 |
("environment".into(), "prod".into()), |
| 241 |
], |
| 242 |
scope: Scope::CHANGES | Scope::REFS, |
| 243 |
not_before: 1_000, |
| 244 |
expires: 1_000 + minutes * 60_000, |
| 245 |
}; |
| 246 |
(job_sk, SignedCertificate::sign(cert, issuer, issuer_sk)) |
| 247 |
} |
| 248 |
|
| 249 |
#[test] |
| 250 |
fn a_certificate_round_trips_and_verifies_with_its_issuer_only() { |
| 251 |
let hub_sk = SecretKey::generate(); |
| 252 |
let hub = hub_sk.identity("oxygenhub.io", "[email protected]", 1); |
| 253 |
let (_, sc) = issue(&hub_sk, &hub, 10); |
| 254 |
let back = SignedCertificate::decode(&sc.encode()).unwrap(); |
| 255 |
assert_eq!(back, sc); |
| 256 |
assert_eq!(back.cert.claims[0].0, "environment", "claims kept sorted"); |
| 257 |
assert_eq!(back.cert.claim("run"), Some("12")); |
| 258 |
assert!(back.verify(&hub).is_ok()); |
| 259 |
let other_sk = SecretKey::generate(); |
| 260 |
let other = other_sk.identity("elsewhere", "[email protected]", 1); |
| 261 |
assert!(matches!( |
| 262 |
back.verify(&other), |
| 263 |
Err(CertError::WrongIssuer(_)) |
| 264 |
)); |
| 265 |
|
| 266 |
let mut forged = back.clone(); |
| 267 |
forged.cert.subject = "repo:andy/demo:bookmark:release".into(); |
| 268 |
assert!(matches!( |
| 269 |
forged.verify(&hub), |
| 270 |
Err(CertError::BadSignature(_)) |
| 271 |
)); |
| 272 |
|
| 273 |
let mut stolen = back.clone(); |
| 274 |
stolen.sig = other_sk.sign(Domain::Certificate, &hub.hash(), &back.cert.hash()); |
| 275 |
assert!(matches!( |
| 276 |
stolen.verify(&hub), |
| 277 |
Err(CertError::BadSignature(_)) |
| 278 |
)); |
| 279 |
} |
| 280 |
|
| 281 |
#[test] |
| 282 |
fn it_covers_its_scope_and_window_only() { |
| 283 |
let hub_sk = SecretKey::generate(); |
| 284 |
let hub = hub_sk.identity("oxygenhub.io", "[email protected]", 1); |
| 285 |
let (_, sc) = issue(&hub_sk, &hub, 10); |
| 286 |
let c = &sc.cert; |
| 287 |
assert!(c.covers(Scope::CHANGES, 1_000) && c.covers(Scope::REFS, 1_000 + 10 * 60_000)); |
| 288 |
assert!(!c.covers(Scope::SEAL, 2_000), "not a scope it gives"); |
| 289 |
assert!(!c.covers(Scope::CHANGES, 999), "before it"); |
| 290 |
assert!( |
| 291 |
!c.covers(Scope::CHANGES, 1_000 + 10 * 60_000 + 1), |
| 292 |
"after it" |
| 293 |
); |
| 294 |
assert!(!c.covers(Scope::NONE, 2_000)); |
| 295 |
let (_, long) = issue(&hub_sk, &hub, 61); |
| 296 |
assert_eq!(long.verify(&hub), Err(CertError::BadLifetime)); |
| 297 |
} |
| 298 |
|
| 299 |
#[test] |
| 300 |
fn the_proof_of_possession_is_bound_to_its_credential() { |
| 301 |
let sk = SecretKey::generate(); |
| 302 |
let id = sk.identity("job", "[email protected]", 1); |
| 303 |
let proof = sk |
| 304 |
.sign_challenge(&id.hash(), &certify_challenge("flowjob_aaa")) |
| 305 |
.unwrap(); |
| 306 |
assert!( |
| 307 |
crate::identity::verify_challenge(&id, &proof, &certify_challenge("flowjob_aaa")) |
| 308 |
.is_ok() |
| 309 |
); |
| 310 |
assert!( |
| 311 |
crate::identity::verify_challenge(&id, &proof, &certify_challenge("flowjob_bbb")) |
| 312 |
.is_err() |
| 313 |
); |
| 314 |
} |
| 315 |
|
| 316 |
#[test] |
| 317 |
fn a_signature_by_the_short_lived_key_names_it() { |
| 318 |
let hub_sk = SecretKey::generate(); |
| 319 |
let hub = hub_sk.identity("oxygenhub.io", "[email protected]", 1); |
| 320 |
let (job_sk, sc) = issue(&hub_sk, &hub, 10); |
| 321 |
let target = ObjectRef([5; 32]); |
| 322 |
let sig = job_sk.sign(Domain::Change, &sc.cert.key_id(), &target); |
| 323 |
assert_eq!(sig.key, sc.cert.key_id()); |
| 324 |
assert!(verify(&sc.cert.key, &sig, Domain::Change, &target).is_ok()); |
| 325 |
} |
| 326 |
} |