clawft-kernel 0.8.3

WeftOS kernel: boot sequence, process table, service registry, and health subsystem
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
//! Seed HTTP transport: certificate pinning and the response-size cap,
//! against real local servers (a rustls server with a self-signed
//! certificate, and a raw TCP server streaming an endless chunked body).

use std::io::{Read, Write};
use std::net::TcpListener;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use std::time::Duration;

use clawft_types::secret::SecretString;
use rustls::pki_types::{CertificateDer, PrivateKeyDer, PrivatePkcs8KeyDer};
use tokio::io::{AsyncReadExt, AsyncWriteExt};

use super::seed_http::{HttpSeedTransport, MAX_RESPONSE_BYTES, Method, SeedTransport};
use super::seed_tls::SeedTls;
use super::types::RuntimeError;

const TOKEN: &str = "seed-token-under-test";

fn pin_of(cert: &CertificateDer<'_>) -> [u8; 32] {
    match SeedTls::pinned(&SeedTls::fingerprint(cert.as_ref())).unwrap() {
        SeedTls::PinnedSha256(p) => p,
        _ => unreachable!(),
    }
}

/// A TLS server with a fresh self-signed certificate. Every request that
/// completes a handshake is recorded (head only) and answered `200 []`.
fn tls_seed() -> (u16, CertificateDer<'static>, Arc<Mutex<Vec<String>>>) {
    let kp = rcgen::KeyPair::generate().unwrap();
    let (port, cert, seen) = tls_seed_with(&kp, 1);
    (port, cert, seen)
}

/// A TLS server whose self-signed certificate (serial `serial`) is issued
/// for `kp`: the same key with another serial is a renewed certificate.
fn tls_seed_with(
    kp: &rcgen::KeyPair,
    serial: u64,
) -> (u16, CertificateDer<'static>, Arc<Mutex<Vec<String>>>) {
    let mut params = rcgen::CertificateParams::new(vec!["seed.local".into()]).unwrap();
    params.serial_number = Some(rcgen::SerialNumber::from(serial));
    let cert = params.self_signed(kp).unwrap().der().clone();
    let key = PrivateKeyDer::Pkcs8(PrivatePkcs8KeyDer::from(kp.serialize_der()));
    let cfg = Arc::new(
        rustls::ServerConfig::builder_with_provider(Arc::new(
            rustls::crypto::ring::default_provider(),
        ))
        .with_safe_default_protocol_versions()
        .unwrap()
        .with_no_client_auth()
        .with_single_cert(vec![cert.clone()], key)
        .unwrap(),
    );
    let listener = TcpListener::bind("127.0.0.1:0").unwrap();
    let port = listener.local_addr().unwrap().port();
    let seen = Arc::new(Mutex::new(Vec::new()));
    let log = seen.clone();
    std::thread::spawn(move || {
        for tcp in listener.incoming().flatten() {
            let _ = tcp.set_read_timeout(Some(Duration::from_secs(5)));
            let conn = rustls::ServerConnection::new(cfg.clone()).unwrap();
            let mut tls = rustls::StreamOwned::new(conn, tcp);
            let mut head = Vec::new();
            let mut buf = [0u8; 1024];
            while !head.windows(4).any(|w| w == b"\r\n\r\n") {
                match tls.read(&mut buf) {
                    Ok(0) | Err(_) => break,
                    Ok(n) => head.extend_from_slice(&buf[..n]),
                }
            }
            if head.is_empty() {
                continue; // handshake refused by the client
            }
            log.lock()
                .unwrap()
                .push(String::from_utf8_lossy(&head).into_owned());
            let _ = tls.write_all(
                b"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\
                  Content-Length: 2\r\nConnection: close\r\n\r\n[]",
            );
            tls.conn.send_close_notify();
            let _ = tls.flush();
        }
    });
    (port, cert, seen)
}

async fn get(t: &HttpSeedTransport) -> Result<(u16, serde_json::Value), RuntimeError> {
    t.request(
        Method::Get,
        "/api/v1/apps",
        None,
        &SecretString::new(TOKEN.to_string()),
        Duration::from_secs(5),
    )
    .await
}

#[test]
fn pins_parse_in_the_usual_fingerprint_forms() {
    let hex = "ab".repeat(32);
    let colon = vec!["AB"; 32].join(":");
    for text in [format!("sha256:{hex}"), format!("sha256:{colon}")] {
        assert_eq!(
            SeedTls::pinned(&text).unwrap(),
            SeedTls::PinnedSha256([0xab; 32])
        );
    }
    let bare = hex.clone();
    let short = format!("sha256:{}", "ab".repeat(31));
    let long = format!("sha256:{}", "ab".repeat(33));
    let nonhex = format!("sha256:{}", "zz".repeat(32));
    for bad in ["", "sha256:", bare.as_str(), &short, &long, &nonhex] {
        assert!(SeedTls::pinned(bad).is_err(), "{bad:?} accepted");
    }
    assert_eq!(SeedTls::fingerprint(b"x").len(), "sha256:".len() + 64);
}

#[tokio::test]
async fn the_pinned_certificate_is_trusted_and_carries_the_token() {
    let (port, cert, seen) = tls_seed();
    let t = HttpSeedTransport::new(
        &format!("https://127.0.0.1:{port}"),
        SeedTls::PinnedSha256(pin_of(&cert)),
    )
    .unwrap();
    let (status, body) = get(&t).await.expect("pinned handshake");
    assert_eq!((status, body), (200, serde_json::json!([])));
    let seen = seen.lock().unwrap();
    assert_eq!(seen.len(), 1);
    assert!(
        seen[0]
            .to_ascii_lowercase()
            .contains(&format!("authorization: bearer {TOKEN}"))
    );
}

#[tokio::test]
async fn any_other_certificate_is_refused_before_the_token_is_sent() {
    let (port, cert, seen) = tls_seed();
    let mut wrong = pin_of(&cert);
    wrong[0] ^= 1;
    let base = format!("https://127.0.0.1:{port}");
    let pinned_wrong = HttpSeedTransport::new(&base, SeedTls::PinnedSha256(wrong)).unwrap();
    let err = get(&pinned_wrong).await.expect_err("wrong pin accepted");
    assert!(!err.to_string().contains(TOKEN));
    // Without a pin, the Seed's self-signed certificate fails WebPKI.
    let webpki = HttpSeedTransport::new(&base, SeedTls::WebPki).unwrap();
    get(&webpki)
        .await
        .expect_err("self-signed accepted without a pin");
    assert!(
        seen.lock().unwrap().is_empty(),
        "a request (and its bearer token) reached an untrusted server"
    );
}

#[test]
fn a_pin_on_a_plain_http_base_is_refused() {
    let r = HttpSeedTransport::new("http://127.0.0.1:1", SeedTls::PinnedSha256([0; 32]));
    assert!(matches!(r, Err(RuntimeError::InvalidConfig(_))));
}

/// A server that answers every request with `head` and then streams
/// `total` bytes of body in `chunk`-sized pieces; returns its port and the
/// number of body bytes it managed to write.
async fn streaming_seed(
    head: &'static str,
    total: usize,
    chunked: bool,
) -> (u16, Arc<AtomicUsize>) {
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let port = listener.local_addr().unwrap().port();
    let sent = Arc::new(AtomicUsize::new(0));
    let count = sent.clone();
    tokio::spawn(async move {
        let (mut s, _) = listener.accept().await.unwrap();
        let mut buf = [0u8; 4096];
        let _ = s.read(&mut buf).await;
        if s.write_all(head.as_bytes()).await.is_err() {
            return;
        }
        let piece = vec![b' '; 64 * 1024];
        while count.load(Ordering::SeqCst) < total {
            let r = if chunked {
                let mut c = format!("{:x}\r\n", piece.len()).into_bytes();
                c.extend_from_slice(&piece);
                c.extend_from_slice(b"\r\n");
                s.write_all(&c).await
            } else {
                s.write_all(&piece).await
            };
            if r.is_err() {
                return; // the client hung up
            }
            count.fetch_add(piece.len(), Ordering::SeqCst);
        }
    });
    (port, sent)
}

#[tokio::test]
async fn a_body_without_a_length_is_cut_off_at_the_cap() {
    let total = 256 * 1024 * 1024;
    let (port, sent) = streaming_seed(
        "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nTransfer-Encoding: chunked\r\n\r\n",
        total,
        true,
    )
    .await;
    let t = HttpSeedTransport::new(&format!("http://127.0.0.1:{port}"), SeedTls::WebPki).unwrap();
    let err = get(&t).await.expect_err("oversized body accepted");
    assert!(err.to_string().contains("too large"), "{err}");
    // The client stopped reading at the cap: the server could not push the
    // whole body (it only gets as far as the cap plus socket buffers).
    tokio::time::sleep(Duration::from_millis(500)).await;
    let n = sent.load(Ordering::SeqCst);
    assert!(n < 8 * MAX_RESPONSE_BYTES, "server wrote {n} bytes");
}

#[tokio::test]
async fn a_declared_length_over_the_cap_is_refused_before_reading() {
    let (port, sent) = streaming_seed(
        "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: 1073741824\r\n\r\n",
        1024 * 1024 * 1024,
        false,
    )
    .await;
    let t = HttpSeedTransport::new(&format!("http://127.0.0.1:{port}"), SeedTls::WebPki).unwrap();
    let err = get(&t).await.expect_err("oversized body accepted");
    assert!(err.to_string().contains("too large"), "{err}");
    tokio::time::sleep(Duration::from_millis(500)).await;
    assert!(sent.load(Ordering::SeqCst) < 8 * MAX_RESPONSE_BYTES);
}

fn spki_pin(cert: &CertificateDer<'_>) -> SeedTls {
    SeedTls::pinned_spki(&SeedTls::spki_fingerprint(cert.as_ref()).unwrap()).unwrap()
}

#[test]
fn spki_pins_parse_and_reject_junk() {
    let hex = "cd".repeat(32);
    assert_eq!(
        SeedTls::pinned_spki(&format!("spki-sha256:{hex}")).unwrap(),
        SeedTls::PinnedSpki([0xcd; 32])
    );
    for bad in [
        "",
        "sha256:".to_string().as_str(),
        &format!("sha256:{hex}"),
        "spki-sha256:zz",
    ] {
        assert!(SeedTls::pinned_spki(bad).is_err(), "{bad:?} accepted");
    }
    assert!(SeedTls::spki_fingerprint(b"not a certificate").is_none());
}

#[tokio::test]
async fn a_renewed_leaf_certificate_under_the_same_key_still_connects() {
    let kp = rcgen::KeyPair::generate().unwrap();
    let (_, old_cert, _) = tls_seed_with(&kp, 1);
    let key_pin = spki_pin(&old_cert);
    let cert_pin = SeedTls::PinnedSha256(pin_of(&old_cert));

    // The Seed renews: a new certificate (new serial), same device key.
    let (port, new_cert, seen) = tls_seed_with(&kp, 2);
    assert_ne!(old_cert.as_ref(), new_cert.as_ref());
    let base = format!("https://127.0.0.1:{port}");

    // The old leaf-hash pin breaks on rotation (the card's failure) ...
    let t = HttpSeedTransport::new(&base, cert_pin).unwrap();
    get(&t)
        .await
        .expect_err("leaf-hash pin survived a rotation");
    assert!(seen.lock().unwrap().is_empty());
    // ... the public-key pin does not.
    let t = HttpSeedTransport::new(&base, key_pin).unwrap();
    let (status, _) = get(&t).await.expect("key pin across a rotation");
    assert_eq!(status, 200);
    assert_eq!(seen.lock().unwrap().len(), 1);
}

#[tokio::test]
async fn a_certificate_under_another_key_is_refused_by_the_key_pin() {
    let (_, old_cert, _) = tls_seed();
    let key_pin = spki_pin(&old_cert);
    // A different device key (a rekey, or an impostor): refused, and the
    // token is never sent. The operator must re-pin over a trusted path.
    let (port, _, seen) = tls_seed();
    let t = HttpSeedTransport::new(&format!("https://127.0.0.1:{port}"), key_pin).unwrap();
    get(&t).await.expect_err("a different key was accepted");
    assert!(seen.lock().unwrap().is_empty());
}

#[test]
fn a_key_pin_on_a_plain_http_base_is_refused() {
    let r = HttpSeedTransport::new("http://127.0.0.1:1", SeedTls::PinnedSpki([0; 32]));
    assert!(matches!(r, Err(RuntimeError::InvalidConfig(_))));
}

/// Plain-http Seed (the USB base `http://169.254.42.1`) with the real
/// firmware's keep-alive behaviour: the first request on a connection is
/// answered with `Connection: keep-alive`, and a second request on the
/// same socket is never answered (a stall, not a FIN). A client that pools
/// idle connections reuses the socket and hangs; one that does not opens a
/// fresh connection each time and always gets an answer.
#[tokio::test]
async fn plain_http_never_reuses_a_connection_the_seed_will_stall_on() {
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let port = listener.local_addr().unwrap().port();
    let heads = Arc::new(Mutex::new(Vec::<String>::new()));
    let log = heads.clone();
    tokio::spawn(async move {
        loop {
            let Ok((mut s, _)) = listener.accept().await else {
                return;
            };
            let log = log.clone();
            tokio::spawn(async move {
                let mut buf = [0u8; 4096];
                let n = s.read(&mut buf).await.unwrap_or(0);
                log.lock()
                    .unwrap()
                    .push(String::from_utf8_lossy(&buf[..n]).into_owned());
                let _ = s
                    .write_all(
                        b"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\
                          Content-Length: 2\r\nConnection: keep-alive\r\n\r\n[]",
                    )
                    .await;
                // A second request on this socket is read and ignored.
                let _ = s.read(&mut buf).await;
                tokio::time::sleep(Duration::from_secs(30)).await;
            });
        }
    });
    let t = HttpSeedTransport::new(&format!("http://127.0.0.1:{port}"), SeedTls::WebPki).unwrap();
    for i in 0..3 {
        let (status, body) = t
            .request(
                Method::Get,
                "/api/v1/apps",
                None,
                &SecretString::new(TOKEN.to_string()),
                Duration::from_millis(1500),
            )
            .await
            .unwrap_or_else(|e| panic!("request {i} stalled on a reused connection: {e}"));
        assert_eq!((status, body), (200, serde_json::json!([])));
        tokio::time::sleep(Duration::from_millis(50)).await;
    }
    let heads = heads.lock().unwrap();
    assert_eq!(heads.len(), 3, "one fresh connection per request");
    assert!(
        heads[0]
            .to_ascii_lowercase()
            .contains(&format!("authorization: bearer {TOKEN}"))
    );
}

const PROXY_CHILD: &str = "SEED_TEST_PROXY_CHILD";

/// A proxy in the environment must not capture Seed traffic. The
/// environment is read when a client is built, and mutating it in this
/// process would race the other tests, so the check runs in a child
/// process of this test binary.
#[test]
fn an_environment_proxy_is_not_used_for_a_seed() {
    if std::env::var(PROXY_CHILD).is_ok() {
        return; // the child runs `proxy_child_body` instead
    }
    let out = std::process::Command::new(std::env::current_exe().unwrap())
        .args(["proxy_child_body", "--nocapture"])
        .env(PROXY_CHILD, "1")
        .env("HTTP_PROXY", "http://127.0.0.1:1")
        .env("http_proxy", "http://127.0.0.1:1")
        .env("ALL_PROXY", "http://127.0.0.1:1")
        .env_remove("NO_PROXY")
        .env_remove("no_proxy")
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "child failed:\n{}\n{}",
        String::from_utf8_lossy(&out.stdout),
        String::from_utf8_lossy(&out.stderr)
    );
}

#[tokio::test]
async fn proxy_child_body() {
    if std::env::var(PROXY_CHILD).is_err() {
        return; // only meaningful inside the child process above
    }
    let (port, _sent) = streaming_seed(
        "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: 2\r\n\r\n[]",
        2,
        false,
    )
    .await;
    let t = HttpSeedTransport::new(&format!("http://127.0.0.1:{port}"), SeedTls::WebPki).unwrap();
    let (status, _) = get(&t).await.expect("request went through the env proxy");
    assert_eq!(status, 200);
}