vela-cli 0.940.2

The vela command-line tool: build, check, sign, replay, and publish scientific frontier state.
Documentation
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
//! Standard repository-authority signer providers.
//!
//! The first provider uses only the standard OpenSSH agent
//! request-identities and sign-request messages. It never reads a private-key
//! file and receives only the canonical authority-record payload after the
//! writer has completed every authentication, authorization, and semantic
//! check. Fresh authority initialization and ordinary Era-1 transactions use
//! the same provider boundary.

use std::env;
use std::io::{Read, Write};
use std::path::Path;

use base64::Engine as _;
use base64::engine::general_purpose::{STANDARD as BASE64_STANDARD, STANDARD_NO_PAD};
use ed25519_dalek::{Signature, Verifier, VerifyingKey};
use sha2::{Digest, Sha256};
use vela_protocol::authority::{AUTHORITY_PAYLOAD_TYPE_V1, DsseSignatureV1, dsse_pae};

use crate::authority_transaction::RepositoryAuthoritySigner;

const SSH_AGENT_FAILURE: u8 = 5;
const SSH2_AGENTC_REQUEST_IDENTITIES: u8 = 11;
const SSH2_AGENT_IDENTITIES_ANSWER: u8 = 12;
const SSH2_AGENTC_SIGN_REQUEST: u8 = 13;
const SSH2_AGENT_SIGN_RESPONSE: u8 = 14;
const SSH_ED25519: &[u8] = b"ssh-ed25519";
const MAX_AGENT_MESSAGE: usize = 1024 * 1024;

trait AgentIo: Read + Write {}
impl<T: Read + Write> AgentIo for T {}

struct AgentConnection {
    stream: Box<dyn AgentIo>,
}

impl AgentConnection {
    fn connect(path: &Path) -> Result<Self, String> {
        #[cfg(unix)]
        {
            let stream = std::os::unix::net::UnixStream::connect(path)
                .map_err(|error| format!("connect to SSH agent: {error}"))?;
            return Ok(Self {
                stream: Box::new(stream),
            });
        }
        #[cfg(windows)]
        {
            let stream = std::fs::OpenOptions::new()
                .read(true)
                .write(true)
                .open(path)
                .map_err(|error| format!("connect to SSH agent named pipe: {error}"))?;
            return Ok(Self {
                stream: Box::new(stream),
            });
        }
        #[allow(unreachable_code)]
        Err("SSH agent transport is unsupported on this platform".into())
    }

    fn exchange(&mut self, message: &[u8]) -> Result<Vec<u8>, String> {
        if message.len() > MAX_AGENT_MESSAGE {
            return Err("SSH agent request exceeds the bounded message size".into());
        }
        self.stream
            .write_all(&(message.len() as u32).to_be_bytes())
            .and_then(|()| self.stream.write_all(message))
            .and_then(|()| self.stream.flush())
            .map_err(|error| format!("write SSH agent request: {error}"))?;
        let mut length = [0_u8; 4];
        self.stream
            .read_exact(&mut length)
            .map_err(|error| format!("read SSH agent response length: {error}"))?;
        let length = u32::from_be_bytes(length) as usize;
        if length == 0 || length > MAX_AGENT_MESSAGE {
            return Err(format!("SSH agent response length {length} is invalid"));
        }
        let mut response = vec![0_u8; length];
        self.stream
            .read_exact(&mut response)
            .map_err(|error| format!("read SSH agent response: {error}"))?;
        if response[0] == SSH_AGENT_FAILURE {
            return Err("SSH agent refused the request".into());
        }
        Ok(response)
    }

    fn identities(&mut self) -> Result<Vec<AgentIdentity>, String> {
        let response = self.exchange(&[SSH2_AGENTC_REQUEST_IDENTITIES])?;
        let mut cursor = SshCursor::new(&response);
        cursor.expect_byte(SSH2_AGENT_IDENTITIES_ANSWER, "identities response")?;
        let count = cursor.read_u32("identity count")? as usize;
        if count > 4096 {
            return Err("SSH agent identity count exceeds the bounded maximum".into());
        }
        let mut identities = Vec::with_capacity(count);
        for _ in 0..count {
            let key_blob = cursor.read_string("identity key")?.to_vec();
            let _comment = cursor.read_string("identity comment")?;
            identities.push(AgentIdentity::parse(key_blob)?);
        }
        cursor.finish("identities response")?;
        Ok(identities)
    }

    fn sign(&mut self, key_blob: &[u8], message: &[u8]) -> Result<[u8; 64], String> {
        let mut request = vec![SSH2_AGENTC_SIGN_REQUEST];
        push_string(&mut request, key_blob)?;
        push_string(&mut request, message)?;
        request.extend_from_slice(&0_u32.to_be_bytes());
        let response = self.exchange(&request)?;
        let mut cursor = SshCursor::new(&response);
        cursor.expect_byte(SSH2_AGENT_SIGN_RESPONSE, "signature response")?;
        let signature_blob = cursor.read_string("signature blob")?;
        cursor.finish("signature response")?;

        let mut signature = SshCursor::new(signature_blob);
        if signature.read_string("signature algorithm")? != SSH_ED25519 {
            return Err("SSH agent returned a non-Ed25519 signature".into());
        }
        let bytes = signature.read_string("Ed25519 signature")?;
        let bytes: [u8; 64] = bytes
            .try_into()
            .map_err(|_| "SSH agent returned a non-64-byte Ed25519 signature".to_string())?;
        signature.finish("Ed25519 signature blob")?;
        Ok(bytes)
    }
}

#[derive(Clone)]
struct AgentIdentity {
    key_blob: Vec<u8>,
    ed25519_public_key: Option<[u8; 32]>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct RepositoryAuthorityIdentity {
    pub(crate) key_id: String,
    pub(crate) fingerprint: String,
    pub(crate) public_key: String,
}

impl AgentIdentity {
    fn parse(key_blob: Vec<u8>) -> Result<Self, String> {
        let mut cursor = SshCursor::new(&key_blob);
        let algorithm = cursor.read_string("identity algorithm")?;
        let ed25519_public_key = if algorithm == SSH_ED25519 {
            let key = cursor.read_string("Ed25519 public key")?;
            Some(
                key.try_into()
                    .map_err(|_| "SSH agent returned a malformed Ed25519 key".to_string())?,
            )
        } else {
            None
        };
        if ed25519_public_key.is_some() {
            cursor.finish("Ed25519 identity")?;
        }
        Ok(Self {
            key_blob,
            ed25519_public_key,
        })
    }
}

struct SshCursor<'a> {
    bytes: &'a [u8],
    position: usize,
}

impl<'a> SshCursor<'a> {
    fn new(bytes: &'a [u8]) -> Self {
        Self { bytes, position: 0 }
    }

    fn expect_byte(&mut self, expected: u8, label: &str) -> Result<(), String> {
        let actual = *self
            .bytes
            .get(self.position)
            .ok_or_else(|| format!("SSH agent {label} is truncated"))?;
        self.position += 1;
        if actual != expected {
            return Err(format!(
                "SSH agent {label} has message type {actual}, expected {expected}"
            ));
        }
        Ok(())
    }

    fn read_u32(&mut self, label: &str) -> Result<u32, String> {
        let end = self
            .position
            .checked_add(4)
            .ok_or_else(|| format!("SSH agent {label} length overflow"))?;
        let bytes: [u8; 4] = self
            .bytes
            .get(self.position..end)
            .ok_or_else(|| format!("SSH agent {label} is truncated"))?
            .try_into()
            .expect("four-byte slice");
        self.position = end;
        Ok(u32::from_be_bytes(bytes))
    }

    fn read_string(&mut self, label: &str) -> Result<&'a [u8], String> {
        let length = self.read_u32(label)? as usize;
        let end = self
            .position
            .checked_add(length)
            .ok_or_else(|| format!("SSH agent {label} length overflow"))?;
        let value = self
            .bytes
            .get(self.position..end)
            .ok_or_else(|| format!("SSH agent {label} is truncated"))?;
        self.position = end;
        Ok(value)
    }

    fn finish(&self, label: &str) -> Result<(), String> {
        if self.position != self.bytes.len() {
            return Err(format!("SSH agent {label} has trailing bytes"));
        }
        Ok(())
    }
}

fn push_string(output: &mut Vec<u8>, value: &[u8]) -> Result<(), String> {
    let length =
        u32::try_from(value.len()).map_err(|_| "SSH agent field exceeds u32".to_string())?;
    output.extend_from_slice(&length.to_be_bytes());
    output.extend_from_slice(value);
    Ok(())
}

/// OpenSSH-agent-backed Ed25519 repository authority.
///
/// The configured public key must appear exactly once as a plain Ed25519
/// identity in the selected agent. Certificates, security-key signatures, RSA
/// keys, and algorithm substitution are rejected because
/// `vela.authority-keyset.v1` currently admits only raw Ed25519 verification.
pub(crate) struct SshAgentRepositoryAuthoritySigner {
    connection: AgentConnection,
    identity: AgentIdentity,
    key_id: String,
    expected_public_key: [u8; 32],
}

impl SshAgentRepositoryAuthoritySigner {
    pub(crate) fn connect(
        socket_path: &Path,
        key_id: impl Into<String>,
        expected_public_key_hex: &str,
    ) -> Result<Self, String> {
        let key_id = key_id.into();
        if key_id.trim().is_empty() {
            return Err("repository-authority key ID is empty".into());
        }
        if socket_path.as_os_str().is_empty() {
            return Err("SSH agent socket path is empty".into());
        }
        let expected_public_key = decode_ed25519_public_key(expected_public_key_hex)?;
        let mut connection = AgentConnection::connect(socket_path)?;
        let matches = connection
            .identities()?
            .into_iter()
            .filter(|identity| identity.ed25519_public_key == Some(expected_public_key))
            .collect::<Vec<_>>();
        let [identity] = matches.as_slice() else {
            return Err(format!(
                "SSH agent must expose exactly one plain Ed25519 identity matching key {key_id}; found {}",
                matches.len()
            ));
        };
        Ok(Self {
            connection,
            identity: identity.clone(),
            key_id,
            expected_public_key,
        })
    }

    /// Resolve the platform's standard OpenSSH agent endpoint.
    ///
    /// The endpoint itself is process-local configuration and never enters a
    /// Frontier, an authority record, a journal, or a diagnostic payload.
    pub(crate) fn from_environment(
        key_id: impl Into<String>,
        expected_public_key_hex: &str,
    ) -> Result<Self, String> {
        let socket = env::var_os("SSH_AUTH_SOCK")
            .or({
                #[cfg(windows)]
                {
                    Some(r"\\.\pipe\openssh-ssh-agent".into())
                }
                #[cfg(not(windows))]
                {
                    None
                }
            })
            .ok_or_else(|| "SSH_AUTH_SOCK is not set".to_string())?;
        Self::connect(Path::new(&socket), key_id, expected_public_key_hex)
    }
}

pub(crate) fn select_repository_authority_identity(
    selector: Option<&str>,
) -> Result<RepositoryAuthorityIdentity, String> {
    let socket = env::var_os("SSH_AUTH_SOCK")
        .or({
            #[cfg(windows)]
            {
                Some(r"\\.\pipe\openssh-ssh-agent".into())
            }
            #[cfg(not(windows))]
            {
                None
            }
        })
        .ok_or_else(|| {
            "no standard OpenSSH agent is available; start one and load one dedicated Ed25519 repository-authority key"
                .to_string()
        })?;
    let mut connection = AgentConnection::connect(Path::new(&socket))?;
    let mut identities = connection
        .identities()?
        .into_iter()
        .filter_map(|identity| {
            identity.ed25519_public_key.map(|public_key| {
                let fingerprint = format!(
                    "SHA256:{}",
                    STANDARD_NO_PAD.encode(Sha256::digest(&identity.key_blob))
                );
                RepositoryAuthorityIdentity {
                    key_id: format!("ssh-ed25519:{fingerprint}"),
                    fingerprint,
                    public_key: hex::encode(public_key),
                }
            })
        })
        .collect::<Vec<_>>();
    identities.sort_by(|left, right| left.key_id.cmp(&right.key_id));
    let matches = identities
        .iter()
        .filter(|identity| {
            selector.is_none_or(|selector| {
                selector == identity.key_id
                    || selector == identity.fingerprint
                    || selector == identity.public_key
            })
        })
        .cloned()
        .collect::<Vec<_>>();
    match matches.as_slice() {
        [identity] => Ok(identity.clone()),
        [] if identities.is_empty() => Err(
            "the OpenSSH agent exposes no plain Ed25519 identity; load one dedicated repository-authority key"
                .into(),
        ),
        [] => Err(format!(
            "no loaded Ed25519 identity matches {}; available fingerprints: {}",
            selector.unwrap_or("<automatic selection>"),
            identities
                .iter()
                .map(|identity| identity.fingerprint.as_str())
                .collect::<Vec<_>>()
                .join(", ")
        )),
        _ => Err(format!(
            "the OpenSSH agent exposes multiple Ed25519 identities; select one with --key <fingerprint>: {}",
            matches
                .iter()
                .map(|identity| identity.fingerprint.as_str())
                .collect::<Vec<_>>()
                .join(", ")
        )),
    }
}

impl RepositoryAuthoritySigner for SshAgentRepositoryAuthoritySigner {
    fn sign(
        &mut self,
        payload_type: &str,
        canonical_payload: &[u8],
    ) -> Result<Vec<DsseSignatureV1>, String> {
        if payload_type != AUTHORITY_PAYLOAD_TYPE_V1 {
            return Err(format!(
                "repository authority refuses payload type {payload_type}"
            ));
        }
        let pae = dsse_pae(payload_type, canonical_payload);
        let signature = Signature::from_bytes(
            &self
                .connection
                .sign(&self.identity.key_blob, &pae)
                .map_err(|error| format!("SSH agent signing failed: {error}"))?,
        );
        let verifying_key = VerifyingKey::from_bytes(&self.expected_public_key)
            .map_err(|error| format!("configured repository public key is invalid: {error}"))?;
        verifying_key.verify(&pae, &signature).map_err(|error| {
            format!("SSH agent signature does not match the configured key: {error}")
        })?;
        Ok(vec![DsseSignatureV1 {
            keyid: self.key_id.clone(),
            sig: BASE64_STANDARD.encode(signature.to_bytes()),
        }])
    }
}

fn decode_ed25519_public_key(value: &str) -> Result<[u8; 32], String> {
    let decoded =
        hex::decode(value).map_err(|error| format!("decode repository public key: {error}"))?;
    decoded.try_into().map_err(|bytes: Vec<u8>| {
        format!(
            "repository public key is {} bytes, expected 32",
            bytes.len()
        )
    })
}

#[cfg(all(test, unix))]
mod tests {
    use std::fs;
    use std::process::{Child, Command, Stdio};
    use std::thread;
    use std::time::Duration;

    use tempfile::TempDir;

    use super::*;

    struct AgentGuard {
        child: Child,
        socket: std::path::PathBuf,
        directory: TempDir,
    }

    impl Drop for AgentGuard {
        fn drop(&mut self) {
            let _ = self.child.kill();
            let _ = self.child.wait();
            let _ = fs::remove_file(&self.socket);
        }
    }

    fn start_agent() -> AgentGuard {
        let directory = tempfile::Builder::new()
            .prefix("vela-agent-")
            .tempdir_in("/tmp")
            .unwrap();
        let socket = directory.path().join("agent.sock");
        let child = Command::new("ssh-agent")
            .arg("-D")
            .arg("-a")
            .arg(&socket)
            .stdin(Stdio::null())
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .spawn()
            .expect("OpenSSH ssh-agent is required for the provider qualification");
        let mut guard = AgentGuard {
            child,
            socket,
            directory,
        };
        for _ in 0..100 {
            if guard.socket.exists() {
                return guard;
            }
            if let Some(status) = guard.child.try_wait().unwrap() {
                panic!("ssh-agent exited before creating its socket: {status}");
            }
            thread::sleep(Duration::from_millis(10));
        }
        panic!("ssh-agent did not create its socket");
    }

    fn load_fixture_identity(guard: &AgentGuard) -> [u8; 32] {
        let private_key = guard.directory.path().join("fixture");
        let status = Command::new("ssh-keygen")
            .args(["-q", "-t", "ed25519", "-N", "", "-f"])
            .arg(&private_key)
            .status()
            .unwrap();
        assert!(status.success());
        let status = Command::new("ssh-add")
            .arg(&private_key)
            .env("SSH_AUTH_SOCK", &guard.socket)
            .env("SSH_ASKPASS_REQUIRE", "never")
            .stdin(Stdio::null())
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .status()
            .unwrap();
        assert!(status.success());

        let public = fs::read_to_string(private_key.with_extension("pub")).unwrap();
        let encoded = public.split_whitespace().nth(1).unwrap();
        let key_blob = BASE64_STANDARD.decode(encoded).unwrap();
        AgentIdentity::parse(key_blob)
            .unwrap()
            .ed25519_public_key
            .unwrap()
    }

    #[test]
    fn ssh_agent_provider_signs_only_the_exact_dsse_pae_with_the_bound_key() {
        let guard = start_agent();
        let public_key = load_fixture_identity(&guard);
        let mut provider = SshAgentRepositoryAuthoritySigner::connect(
            &guard.socket,
            "repository-key-1",
            &hex::encode(public_key),
        )
        .unwrap();
        let payload = br#"{"schema":"vela.authority-record.v1"}"#;
        let signatures = provider.sign(AUTHORITY_PAYLOAD_TYPE_V1, payload).unwrap();
        assert_eq!(signatures.len(), 1);
        assert_eq!(signatures[0].keyid, "repository-key-1");
        let signature_bytes = BASE64_STANDARD.decode(&signatures[0].sig).unwrap();
        let signature = Signature::try_from(signature_bytes.as_slice()).unwrap();
        VerifyingKey::from_bytes(&public_key)
            .unwrap()
            .verify(&dsse_pae(AUTHORITY_PAYLOAD_TYPE_V1, payload), &signature)
            .unwrap();

        let error = provider.sign("application/json", payload).unwrap_err();
        assert!(error.contains("payload type"), "{error}");
    }

    #[test]
    fn ssh_agent_provider_rejects_missing_or_substituted_identity() {
        let guard = start_agent();
        let public_key = load_fixture_identity(&guard);
        let error = SshAgentRepositoryAuthoritySigner::connect(
            &guard.socket,
            "repository-key-1",
            &hex::encode([43; 32]),
        )
        .err()
        .expect("wrong identity must fail");
        assert!(error.contains("found 0"), "{error}");

        let provider = SshAgentRepositoryAuthoritySigner::connect(
            &guard.socket,
            "repository-key-1",
            &hex::encode(public_key),
        );
        assert!(provider.is_ok());
    }

    #[test]
    fn ssh_agent_provider_refuses_malformed_configuration_before_agent_use() {
        let error = SshAgentRepositoryAuthoritySigner::connect(
            Path::new("/definitely/missing/ssh-agent.sock"),
            "",
            &"00".repeat(32),
        )
        .err()
        .expect("empty key id must fail");
        assert!(error.contains("key ID"), "{error}");

        let error = SshAgentRepositoryAuthoritySigner::connect(
            Path::new("/definitely/missing/ssh-agent.sock"),
            "repository-key-1",
            "00",
        )
        .err()
        .expect("wrong key length must fail");
        assert!(error.contains("expected 32"), "{error}");
    }
}