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