Repository navigation
Expand file tree
/
Copy pathcert.rs
More file actions
361 lines (321 loc) · 10.6 KB
/
Copy pathcert.rs
File metadata and controls
361 lines (321 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
//! Ref-update certificates — the consensus mechanism for decentralized branch state.
//!
//! A certificate authorizes a change to a git ref (branch/tag). It must be
//! signed by the pusher and optionally countersigned by a threshold of maintainers.
//!
//! The schema is frozen at v1. All fields are mandatory for forward compatibility.
//! Nodes that receive a certificate with an unknown version MUST reject it.
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use crate::did::Did;
use crate::identity::{verify, Keypair};
use crate::{Error, Result};
/// The certificate type discriminant. Always `"gitlawb/ref-update/v1"`.
pub const CERT_TYPE: &str = "gitlawb/ref-update/v1";
/// A signature on a ref-update certificate.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CertSignature {
pub signer: Did,
/// base64url-encoded Ed25519 signature over the certificate body JSON.
pub sig: String,
}
/// The body of a ref-update certificate (what gets signed).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RefUpdateBody {
/// Always `"gitlawb/ref-update/v1"`.
#[serde(rename = "type")]
pub type_: String,
/// The repository DID.
pub repo: Did,
/// The git ref being updated (e.g. `"refs/heads/main"`).
pub ref_name: String,
/// The previous commit hash (SHA-256 hex). Use all-zeros for new refs.
pub from: String,
/// The new commit hash (SHA-256 hex).
pub to: String,
/// Monotonically increasing sequence number. Prevents replay.
pub seq: u64,
/// RFC 3339 timestamp of this update.
pub timestamp: DateTime<Utc>,
/// Random nonce for deduplication.
pub nonce: String,
}
impl RefUpdateBody {
/// Serialize to canonical JSON bytes for signing.
pub fn to_signing_bytes(&self) -> Result<Vec<u8>> {
Ok(serde_json::to_vec(self)?)
}
}
/// A complete ref-update certificate with at least one signature.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RefUpdateCert {
#[serde(flatten)]
pub body: RefUpdateBody,
pub signatures: Vec<CertSignature>,
}
impl RefUpdateCert {
/// Create a new certificate and sign it with the given keypair.
pub fn new(
repo: Did,
ref_name: String,
from: String,
to: String,
seq: u64,
keypair: &Keypair,
) -> Result<Self> {
let body = RefUpdateBody {
type_: CERT_TYPE.to_string(),
repo,
ref_name,
from,
to,
seq,
timestamp: Utc::now(),
nonce: Uuid::new_v4().to_string(),
};
let signing_bytes = body.to_signing_bytes()?;
let sig = keypair.sign_b64(&signing_bytes);
Ok(Self {
body,
signatures: vec![CertSignature {
signer: keypair.did(),
sig,
}],
})
}
/// Add a countersignature from a maintainer keypair.
pub fn countersign(&mut self, keypair: &Keypair) -> Result<()> {
let signing_bytes = self.body.to_signing_bytes()?;
let sig = keypair.sign_b64(&signing_bytes);
self.signatures.push(CertSignature {
signer: keypair.did(),
sig,
});
Ok(())
}
/// Verify all signatures on this certificate.
///
/// Returns the list of DIDs whose signatures are valid.
pub fn verify_all(&self) -> Result<Vec<Did>> {
use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine};
let signing_bytes = self.body.to_signing_bytes()?;
let mut valid = Vec::new();
for cert_sig in &self.signatures {
// Resolve the verifying key from the DID
let vk = cert_sig.signer.to_verifying_key()?;
let sig_bytes_vec = URL_SAFE_NO_PAD
.decode(&cert_sig.sig)
.map_err(|e| Error::RefCert(format!("invalid base64 sig: {e}")))?;
let sig_bytes: [u8; 64] = sig_bytes_vec
.try_into()
.map_err(|_| Error::RefCert("signature must be 64 bytes".to_string()))?;
verify(&vk, &signing_bytes, &sig_bytes)?;
valid.push(cert_sig.signer.clone());
}
Ok(valid)
}
/// Check if this certificate satisfies a threshold of valid signatures
/// from the provided set of authorized maintainer DIDs.
pub fn satisfies_threshold(&self, maintainers: &[Did], threshold: usize) -> Result<bool> {
let valid = self.verify_all()?;
let count = valid.iter().filter(|d| maintainers.contains(d)).count();
Ok(count >= threshold)
}
/// Validate the certificate structure (not signatures).
pub fn validate_structure(&self) -> Result<()> {
if self.body.type_ != CERT_TYPE {
return Err(Error::RefCert(format!(
"unknown cert type: {}",
self.body.type_
)));
}
if self.body.from.len() != 64 || !self.body.from.chars().all(|c| c.is_ascii_hexdigit()) {
// Allow all-zeros for new refs
if self.body.from != "0".repeat(64) {
return Err(Error::RefCert("invalid 'from' hash".to_string()));
}
}
if self.body.to.len() != 64 || !self.body.to.chars().all(|c| c.is_ascii_hexdigit()) {
return Err(Error::RefCert("invalid 'to' hash".to_string()));
}
if self.signatures.is_empty() {
return Err(Error::RefCert("certificate has no signatures".to_string()));
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::identity::Keypair;
fn dummy_hash(c: char) -> String {
c.to_string().repeat(64)
}
#[test]
fn create_and_verify() {
let kp = Keypair::generate();
let repo_did = kp.did();
let cert = RefUpdateCert::new(
repo_did,
"refs/heads/main".to_string(),
dummy_hash('0'),
dummy_hash('a'),
1,
&kp,
)
.unwrap();
cert.validate_structure().unwrap();
let valid = cert.verify_all().unwrap();
assert_eq!(valid.len(), 1);
assert_eq!(valid[0], kp.did());
}
#[test]
fn countersign() {
let kp1 = Keypair::generate();
let kp2 = Keypair::generate();
let repo_did = kp1.did();
let mut cert = RefUpdateCert::new(
repo_did,
"refs/heads/main".to_string(),
dummy_hash('0'),
dummy_hash('a'),
1,
&kp1,
)
.unwrap();
cert.countersign(&kp2).unwrap();
let valid = cert.verify_all().unwrap();
assert_eq!(valid.len(), 2);
}
#[test]
fn threshold_check() {
let kp1 = Keypair::generate();
let kp2 = Keypair::generate();
let repo_did = kp1.did();
let mut cert = RefUpdateCert::new(
repo_did.clone(),
"refs/heads/main".to_string(),
dummy_hash('0'),
dummy_hash('a'),
1,
&kp1,
)
.unwrap();
cert.countersign(&kp2).unwrap();
let maintainers = vec![kp1.did(), kp2.did()];
assert!(cert.satisfies_threshold(&maintainers, 2).unwrap());
assert!(cert.satisfies_threshold(&maintainers, 1).unwrap());
}
#[test]
fn serializes_to_json() {
let kp = Keypair::generate();
let cert = RefUpdateCert::new(
kp.did(),
"refs/heads/main".to_string(),
dummy_hash('0'),
dummy_hash('b'),
42,
&kp,
)
.unwrap();
let json = serde_json::to_string_pretty(&cert).unwrap();
assert!(json.contains("gitlawb/ref-update/v1"));
assert!(json.contains("refs/heads/main"));
// Round-trip
let _: RefUpdateCert = serde_json::from_str(&json).unwrap();
}
#[test]
fn validate_structure_rejects_wrong_cert_type() {
let kp = Keypair::generate();
let mut cert = RefUpdateCert::new(
kp.did(),
"refs/heads/main".to_string(),
dummy_hash('0'),
dummy_hash('a'),
1,
&kp,
)
.unwrap();
cert.body.type_ = "gitlawb/ref-update/v0".to_string();
assert!(cert.validate_structure().is_err());
}
#[test]
fn validate_structure_rejects_invalid_to_hash() {
let kp = Keypair::generate();
let mut cert = RefUpdateCert::new(
kp.did(),
"refs/heads/main".to_string(),
dummy_hash('0'),
dummy_hash('a'),
1,
&kp,
)
.unwrap();
cert.body.to = "not-a-valid-sha256".to_string();
assert!(cert.validate_structure().is_err());
}
#[test]
fn validate_structure_rejects_empty_signatures() {
let kp = Keypair::generate();
let mut cert = RefUpdateCert::new(
kp.did(),
"refs/heads/main".to_string(),
dummy_hash('0'),
dummy_hash('a'),
1,
&kp,
)
.unwrap();
cert.signatures.clear();
assert!(cert.validate_structure().is_err());
}
#[test]
fn tampered_signature_fails_verify_all() {
let kp = Keypair::generate();
let kp2 = Keypair::generate();
let mut cert = RefUpdateCert::new(
kp.did(),
"refs/heads/main".to_string(),
dummy_hash('0'),
dummy_hash('a'),
1,
&kp,
)
.unwrap();
// Replace kp's signature with a signature from kp2 over different bytes.
// The signer DID still points to kp, so verification must fail.
cert.signatures[0].sig = kp2.sign_b64(b"unrelated bytes");
assert!(cert.verify_all().is_err());
}
#[test]
fn threshold_not_met_when_signer_outside_maintainer_list() {
let kp_pusher = Keypair::generate();
let kp_maintainer = Keypair::generate();
let cert = RefUpdateCert::new(
kp_pusher.did(),
"refs/heads/main".to_string(),
dummy_hash('0'),
dummy_hash('a'),
1,
&kp_pusher,
)
.unwrap();
let maintainers = vec![kp_maintainer.did()];
assert!(!cert.satisfies_threshold(&maintainers, 1).unwrap());
}
#[test]
fn threshold_zero_is_always_satisfied() {
let kp = Keypair::generate();
let cert = RefUpdateCert::new(
kp.did(),
"refs/heads/main".to_string(),
dummy_hash('0'),
dummy_hash('a'),
1,
&kp,
)
.unwrap();
assert!(cert.satisfies_threshold(&[], 0).unwrap());
}
}