did-git-sign 0.5.1

Git commit signing proxy using DID Ed25519 keys via VTA
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
use anyhow::{Context, Result, bail};
use vta_sdk::client::{AutoConnect, ClientIdentity, ConnectedVta, VtaClient};
use zeroize::Zeroize;

use crate::config::{self, SigningConfig, VtaCredentials};

/// Maximum number of authentication retry attempts.
const MAX_AUTH_RETRIES: u32 = 2;

/// Authenticate with VTA, using whichever transport the install captured.
/// Returns an authenticated `VtaClient` and the loaded VTA credentials.
///
/// - **DIDComm transport** (`mediator_did` is `Some`) — opens a fresh
///   DIDComm session as the credential DID against the advertised
///   mediator. The session itself is the authenticator; there is no
///   bearer token to cache, so the keyring token cache is bypassed on
///   this path.
/// - **REST transport** (`mediator_did` is `None`) — original behaviour:
///   try cached token first, fall back to challenge-response auth with
///   retry, cache the new token for next time.
pub async fn authenticate(cfg: &SigningConfig) -> Result<(VtaClient, VtaCredentials)> {
    let creds = config::load_vta_credentials(&cfg.did_key_id)?;
    validate_credentials(&creds)?;

    // REST transport with a cached bearer token: short-circuit the handshake.
    // Token caching stays caller-side — the SDK deliberately leaves it to us.
    if creds.mediator_did.is_none()
        && let Some(token) = config::load_cached_token(&cfg.did_key_id)
    {
        let client = client_with_identity(
            &creds.vta_url,
            &creds.credential_did,
            &creds.private_key_multibase,
            &creds.vta_did,
        );
        client.set_token(token);
        return Ok((client, creds));
    }

    // Let the SDK pick the transport and run the handshake. `connect_auto`
    // encapsulates the DIDComm-vs-REST branch, the `rest_fallback` derivation,
    // and the empty-URL rule we used to hand-roll here and in openvtc-core —
    // that logic is SDK-level knowledge, so it lives there now (R22). We keep
    // the transient-failure retry and (REST) token caching, both of which are
    // application policy.
    let connected = connect_with_retry(&creds).await?;

    // DIDComm sessions carry no bearer token (`rest_token` is `None`); a REST
    // handshake issues one, which we cache for the next invocation.
    if let Some(token) = &connected.rest_token {
        let _ = config::cache_token(
            &cfg.did_key_id,
            &token.access_token,
            token.access_expires_at,
        );
    }

    Ok((connected.client, creds))
}

/// May we speak cleartext HTTP to this URL?
///
/// Only to loopback, and decided by **parsing the host** rather than matching
/// a prefix of the URL. `url.starts_with("http://localhost")` — the test this
/// replaces — also accepts `http://localhost.evil.com`, which is a cleartext
/// VTA session, carrying the credential exchange, to a host an attacker chose.
/// `http://localhostevil.com` passed too.
///
/// The Trust Registry's `validate_public_url` is the same rule applied to a
/// different URL. Note it still carries this bug in its IPv6 arm
/// (`rest.starts_with("[::1]")` admits `http://[::1].evil.com`), so the two
/// are deliberately *not* identical until that is fixed there too.
fn is_loopback_http(url: &str) -> bool {
    let Some(rest) = url.strip_prefix("http://") else {
        return false;
    };
    // A bracketed IPv6 literal contains ':' itself, so its host is delimited
    // by the closing bracket, and what follows must be a port, a path, a
    // query, or nothing — `[::1].evil.com` is a different host entirely.
    if let Some(after) = rest.strip_prefix('[') {
        let Some((host, tail)) = after.split_once(']') else {
            return false;
        };
        return host == "::1" && (tail.is_empty() || tail.starts_with([':', '/', '?']));
    }
    let host = rest.split(['/', ':', '?']).next().unwrap_or("");
    host == "localhost" || host == "127.0.0.1"
}

/// Is this a URL we may carry VTA credentials over?
fn vta_url_is_secure(url: &str) -> bool {
    url.starts_with("https://") || is_loopback_http(url)
}

/// Validate VTA credentials before use.
///
/// REST transport requires a non-empty HTTPS URL. DIDComm transport
/// (`mediator_did` set) treats `vta_url` as optional — an empty value is
/// fine for VTAs that publish no `#vta-rest` service at all.
fn validate_credentials(creds: &VtaCredentials) -> Result<()> {
    if creds.credential_did.is_empty() {
        bail!("credential DID is empty");
    }
    if creds.key_id.is_empty() {
        bail!("signing key ID is empty");
    }

    if creds.mediator_did.is_some() {
        // DIDComm transport — the URL is optional. If it *is* set, hold
        // it to the same HTTPS rule (it'll be passed through as a /health
        // fallback so we don't want to risk leaking creds over plain HTTP).
        if !creds.vta_url.is_empty() && !vta_url_is_secure(&creds.vta_url) {
            bail!(
                "VTA URL must use HTTPS (got: {}). Cleartext http:// is allowed only to \
                 loopback (localhost, 127.0.0.1, [::1]) for local development.",
                creds.vta_url
            );
        }
        return Ok(());
    }

    // REST transport — URL is required.
    if creds.vta_url.is_empty() {
        bail!("VTA URL is empty");
    }
    if !vta_url_is_secure(&creds.vta_url) {
        bail!(
            "VTA URL must use HTTPS (got: {}). Cleartext http:// is allowed only to \
             loopback (localhost, 127.0.0.1, [::1]) for local development.",
            creds.vta_url
        );
    }
    Ok(())
}

/// Connect via [`VtaClient::connect_auto`] with retry on transient failures.
///
/// The transport (DIDComm vs REST) is chosen by the SDK from `creds`. Retry
/// covers both paths uniformly — a transient mediator or network hiccup is
/// worth a second attempt regardless of transport.
async fn connect_with_retry(creds: &VtaCredentials) -> Result<ConnectedVta> {
    let mut last_err = None;
    for attempt in 1..=MAX_AUTH_RETRIES {
        let result = VtaClient::connect_auto(AutoConnect {
            vta_url: &creds.vta_url,
            vta_did: &creds.vta_did,
            credential_did: &creds.credential_did,
            private_key_multibase: &creds.private_key_multibase,
            mediator_did: creds.mediator_did.as_deref(),
        })
        .await;
        match result {
            Ok(connected) => {
                // A REST handshake must yield a non-empty bearer token; DIDComm
                // carries none (`rest_token` is `None`), so this skips it.
                if let Some(token) = &connected.rest_token
                    && token.access_token.is_empty()
                {
                    bail!("VTA returned an empty access token");
                }
                return Ok(connected);
            }
            Err(e) => {
                let err_msg = format!("{e}");
                if attempt < MAX_AUTH_RETRIES {
                    eprintln!(
                        "VTA connect attempt {attempt}/{MAX_AUTH_RETRIES} failed: {err_msg}, retrying..."
                    );
                }
                last_err = Some(err_msg);
            }
        }
    }
    bail!(
        "VTA connection failed after {MAX_AUTH_RETRIES} attempts: {}",
        last_err.unwrap_or_else(|| "unknown error".to_string())
    )
}

/// A client speaking as `client_did`, with the token left to the caller.
///
/// The identity is not optional. `keys/export-secret` — like every
/// proof-bearing trust task — names an in-band recipient and signs the
/// request, and the SDK refuses to build that document from an identity-less
/// client before any I/O happens. `init` shipped exactly that regression
/// once: a `VtaClient::new` + `set_token` client whose first
/// [`get_signing_key`] failed with "carries no ClientIdentity".
///
/// What the client may *do* is the VTA's ACL's business, not this
/// function's: `init` passes its freshly provisioned admin credential, but
/// nothing here checks or confers a role.
pub fn client_with_identity(
    vta_url: &str,
    client_did: &str,
    private_key_mb: &str,
    vta_did: &str,
) -> VtaClient {
    let identity = ClientIdentity::did_key(client_did, private_key_mb, vta_did);
    VtaClient::new(vta_url).with_identity(identity)
}

/// Fetch the Ed25519 signing key seed from VTA. Returns 32-byte seed.
/// The seed is zeroized on drop via the returned wrapper.
pub async fn get_signing_key(client: &VtaClient, key_id: &str) -> Result<SeedMaterial> {
    let resp = client
        .get_key_secret(key_id)
        .await
        .map_err(|e| anyhow::anyhow!("failed to fetch key secret: {e}"))?;

    if resp.key_type != vta_sdk::keys::KeyType::Ed25519 {
        bail!(
            "signing key {key_id} is {:?}, expected Ed25519",
            resp.key_type
        );
    }

    let seed = vta_sdk::did_key::decode_private_key_multibase(&resp.private_key_multibase)
        .context("failed to decode signing key")?;

    Ok(SeedMaterial(seed))
}

/// Wrapper around a 32-byte Ed25519 seed that zeroizes on drop.
pub struct SeedMaterial([u8; 32]);

impl SeedMaterial {
    pub fn as_bytes(&self) -> &[u8; 32] {
        &self.0
    }
}

impl Drop for SeedMaterial {
    fn drop(&mut self) {
        self.0.zeroize();
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn test_creds() -> VtaCredentials {
        VtaCredentials {
            vta_url: "https://vta.example.com".to_string(),
            vta_did: "did:example:vta".to_string(),
            credential_did: "did:key:z6Mk123".to_string(),
            private_key_multibase: "z...".to_string(),
            key_id: "key-1".to_string(),
            mediator_did: None,
        }
    }

    /// The SDK refuses `keys/export-secret` from an identity-less client
    /// before any I/O — the control for the regression test below. The URL is
    /// unreachable on purpose: nothing here may touch the network.
    #[tokio::test]
    async fn test_get_signing_key_without_identity_refused_before_io() {
        let bare = VtaClient::new("http://127.0.0.1:1");
        bare.set_token("test-token".to_string());
        let err = match get_signing_key(&bare, "key-1").await {
            Err(e) => format!("{e:#}"),
            Ok(_) => panic!("an identity-less client must not fetch a key secret"),
        };
        assert!(
            err.contains("ClientIdentity"),
            "expected the SDK's identity refusal, got: {err}"
        );
    }

    /// The client `init` builds must get past the identity gate: whatever
    /// fails afterwards (here: an undecodable key, still with no I/O), it must
    /// not be the "carries no ClientIdentity" refusal `init` once shipped.
    #[tokio::test]
    async fn test_client_with_identity_passes_identity_gate() {
        let client = client_with_identity(
            "http://127.0.0.1:1",
            "did:key:z6Mk123",
            "zNotARealKey",
            "did:example:vta",
        );
        client.set_token("test-token".to_string());
        let err = match get_signing_key(&client, "key-1").await {
            Err(e) => format!("{e:#}"),
            Ok(_) => panic!("a garbage key against an unreachable VTA must not succeed"),
        };
        assert!(
            !err.contains("ClientIdentity"),
            "the init client lost its identity again: {err}"
        );
    }

    #[test]
    fn test_validate_rejects_empty_url() {
        let mut creds = test_creds();
        creds.vta_url = "".to_string();
        assert!(validate_credentials(&creds).is_err());
    }

    #[test]
    fn test_validate_rejects_http() {
        let mut creds = test_creds();
        creds.vta_url = "http://example.com".to_string();
        assert!(validate_credentials(&creds).is_err());
    }

    #[test]
    fn test_validate_allows_https() {
        assert!(validate_credentials(&test_creds()).is_ok());
    }

    #[test]
    fn test_validate_allows_localhost() {
        let mut creds = test_creds();
        creds.vta_url = "http://localhost:3000".to_string();
        assert!(validate_credentials(&creds).is_ok());
    }

    /// Every loopback form the dev affordance is meant to cover.
    #[test]
    fn cleartext_is_allowed_to_every_loopback_form() {
        for url in [
            "http://localhost",
            "http://localhost:3000",
            "http://localhost/path",
            "http://127.0.0.1:8100",
            "http://[::1]:8100",
        ] {
            let mut creds = test_creds();
            creds.vta_url = url.to_string();
            assert!(
                validate_credentials(&creds).is_ok(),
                "loopback must stay usable for local dev: {url}"
            );
        }
    }

    /// The bug this replaces: `starts_with("http://localhost")` matched any
    /// host merely *beginning* with those characters, so the HTTPS
    /// requirement could be sidestepped by registering a lookalike domain.
    /// The credential exchange would then cross the network in cleartext to
    /// a host the attacker controls.
    #[test]
    fn cleartext_is_rejected_to_hosts_that_only_look_like_loopback() {
        for url in [
            "http://localhost.evil.com",
            "http://localhost.evil.com/vta",
            "http://localhostevil.com",
            "http://127.0.0.1.evil.com",
            "http://[::1].evil.com",
        ] {
            let mut creds = test_creds();
            creds.vta_url = url.to_string();
            assert!(
                validate_credentials(&creds).is_err(),
                "a lookalike host must not pass the HTTPS requirement: {url}"
            );
        }
    }

    /// The DIDComm branch treats the URL as optional but holds a present one
    /// to the same rule — it is passed through as a `/health` fallback, so a
    /// lookalike there leaks just the same.
    #[test]
    fn the_didcomm_branch_applies_the_same_host_rule() {
        let mut creds = test_creds();
        creds.mediator_did = Some("did:web:mediator.example".to_string());

        creds.vta_url = String::new();
        assert!(validate_credentials(&creds).is_ok(), "empty stays allowed");

        creds.vta_url = "http://localhost:3000".to_string();
        assert!(validate_credentials(&creds).is_ok());

        creds.vta_url = "http://localhost.evil.com".to_string();
        assert!(
            validate_credentials(&creds).is_err(),
            "the lookalike must fail on the DIDComm path too"
        );
    }

    #[test]
    fn test_validate_rejects_empty_key_id() {
        let mut creds = test_creds();
        creds.key_id = "".to_string();
        assert!(validate_credentials(&creds).is_err());
    }

    #[test]
    fn test_validate_rejects_empty_credential_did() {
        let mut creds = test_creds();
        creds.credential_did = "".to_string();
        assert!(validate_credentials(&creds).is_err());
    }

    #[test]
    fn test_seed_material_zeroizes_on_drop() {
        let seed = SeedMaterial([0xAB; 32]);
        assert_eq!(seed.as_bytes(), &[0xAB; 32]);
        drop(seed);
    }

    #[test]
    fn test_validate_didcomm_only_accepts_empty_url() {
        let mut creds = test_creds();
        creds.vta_url = "".to_string();
        creds.mediator_did = Some("did:peer:0z6Mkmediator".to_string());
        assert!(validate_credentials(&creds).is_ok());
    }

    #[test]
    fn test_validate_didcomm_with_url_still_requires_https() {
        let mut creds = test_creds();
        creds.vta_url = "http://example.com".to_string();
        creds.mediator_did = Some("did:peer:0z6Mkmediator".to_string());
        assert!(validate_credentials(&creds).is_err());
    }

    #[test]
    fn test_validate_didcomm_still_rejects_empty_credential_did() {
        let mut creds = test_creds();
        creds.vta_url = "".to_string();
        creds.mediator_did = Some("did:peer:0z6Mkmediator".to_string());
        creds.credential_did = "".to_string();
        assert!(validate_credentials(&creds).is_err());
    }
}