╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ssh.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
493 try req.append(allocator, proto.SSH_MSG_USERAUTH_REQUEST); 494 try proto.writeStringField(&req, allocator, "testuser"); 495 try proto.writeStringField(&req, allocator, "ssh-connection"); 496 try proto.writeStringField(&req, allocator, "publickey"); 497 try req.append(allocator, 1); 498 try proto.writeStringField(&req, allocator, "ssh-ed25519"); 499 try proto.writeStringField(&req, allocator, user_pubkey_blob.items); 500 try proto.writeStringField(&req, allocator, sig_wire.items); 501 try cs_cipher.writePacket(io, cw, req.items); 502 } 503 { 504 const success = try sc_cipher.readPacket(allocator, cr); 505 defer allocator.free(success); 506 try std.testing.expectEqual(@as(u8, proto.SSH_MSG_USERAUTH_SUCCESS), success[0]); 507 } 508 509 // STEP 8: CHANNEL_OPEN (RFC 4254 §5.1) 510 // open a "session" channel — SSH's container for shell/exec/subsystem. 511 // client picks its own channel id and announces an initial receive 512 // window + max-packet size; server replies CHANNEL_OPEN_CONFIRMATION 513 // with its own channel id and c->s limits. each side identifies the 514 // channel by *the other's* id when sending. 515 // 516 // haxy supports exactly one channel per connection — a second 517 // CHANNEL_OPEN here gets CHANNEL_OPEN_FAILURE (see the failure tests). 518 const client_channel_id: u32 = 42; 519 { 520 var req: std.ArrayList(u8) = .empty; 521 defer req.deinit(allocator); 522 try req.append(allocator, proto.SSH_MSG_CHANNEL_OPEN); 523 try proto.writeStringField(&req, allocator, "session"); 524 try proto.writeU32(&req, allocator, client_channel_id); 525 try proto.writeU32(&req, allocator, 1 << 20); // initial window 526 try proto.writeU32(&req, allocator, 32768); // max packet 527 try cs_cipher.writePacket(io, cw, req.items); 528 } 529 var server_channel_id: u32 = undefined; 530 { 531 const conf = try sc_cipher.readPacket(allocator, cr); 532 defer allocator.free(conf); 533 try std.testing.expectEqual(@as(u8, proto.SSH_MSG_CHANNEL_OPEN_CONFIRMATION), conf[0]); 534 var r = std.Io.Reader.fixed(conf[1..]); 535 const recipient = try r.takeInt(u32, .big); 536 try std.testing.expectEqual(client_channel_id, recipient); 537 server_channel_id = try r.takeInt(u32, .big); 538 } 539 540 // STEP 9: CHANNEL_REQUEST exec (RFC 4254 §6.5) 541 // ask the server to run a command on this channel. packet is 542 // addressed to the *server's* channel id. with want_reply=true the 543 // server must respond CHANNEL_SUCCESS or CHANNEL_FAILURE before any 544 // CHANNEL_DATA flows. 545 const exec_command = "git-upload-pack 'demo-repo'"; 546 { 547 var req: std.ArrayList(u8) = .empty; 548 defer req.deinit(allocator); 549 try req.append(allocator, proto.SSH_MSG_CHANNEL_REQUEST); 550 try proto.writeU32(&req, allocator, server_channel_id); 551 try proto.writeStringField(&req, allocator, "exec"); 552 try req.append(allocator, 1); // want_reply 553 try proto.writeStringField(&req, allocator, exec_command); 554 try cs_cipher.writePacket(io, cw, req.items); 555 } 556 { 557 const success = try sc_cipher.readPacket(allocator, cr); 558 defer allocator.free(success); 559 try std.testing.expectEqual(@as(u8, proto.SSH_MSG_CHANNEL_SUCCESS), success[0]); 560 } 561 562 // STEP 10: HANDLER RUNS, CHANNEL TEARDOWN 563 // the server is now in handleSession. our recording handler returns 564 // immediately; runChannelLayer's wrap-up then sends: 565 // - CHANNEL_REQUEST "exit-status" (status=0, want_reply=false) 566 // - CHANNEL_EOF 567 // - CHANNEL_CLOSE 568 // …and waits for our CHANNEL_CLOSE in reply (so the underlying socket 569 // isn't closed-with-unread-data, which would surface as TCP RST). 570 var saw_exit_status = false; 571 var saw_server_close = false; 572 while (!saw_server_close) { 573 const pkt = try sc_cipher.readPacket(allocator, cr); 574 defer allocator.free(pkt); 575 switch (pkt[0]) { 576 proto.SSH_MSG_CHANNEL_REQUEST => { 577 var r = std.Io.Reader.fixed(pkt[1..]); 578 _ = try r.takeInt(u32, .big); 579 const req_type = try proto.takeStringField(allocator, &r, 64); 580 defer allocator.free(req_type); 581 if (std.mem.eql(u8, req_type, "exit-status")) { 582 _ = try r.takeByte(); 583 const status = try r.takeInt(u32, .big); 584 try std.testing.expectEqual(@as(u32, 0), status); 585 saw_exit_status = true; 586 } 587 }, 588 proto.SSH_MSG_CHANNEL_EOF => {}, 589 proto.SSH_MSG_CHANNEL_CLOSE => saw_server_close = true, 590 proto.SSH_MSG_CHANNEL_WINDOW_ADJUST => {}, 591 else => {}, 592 } 593 } 594 try std.testing.expect(saw_exit_status); 595 596 { 597 var pkt: std.ArrayList(u8) = .empty; 598 defer pkt.deinit(allocator); 599 try pkt.append(allocator, proto.SSH_MSG_CHANNEL_CLOSE); 600 try proto.writeU32(&pkt, allocator, server_channel_id); 601 try cs_cipher.writePacket(io, cw, pkt.items); 602 } 603 604 // simulate the client's TCP FIN so the server's teardown drain reads EOF 605 // and returns instead of blocking on a packet that never arrives. 606 c2s.close(io); 607 608 server_future.await(io); 609 try std.testing.expect(server.result == null); 610 611 // FINAL ASSERTIONS 612 // the exec command we sent on the wire should have reached the handler 613 // verbatim, and the server should have recorded our pubkey's SHA256 614 // fingerprint as the authenticated identity for this session. 615 try std.testing.expectEqualStrings(exec_command, server.captured_exec_buf[0..server.captured_exec_len]); 616 const expected_fp = proto.formatFingerprint(user_pubkey_blob.items); 617 try std.testing.expectEqualSlices(u8, &expected_fp, &server.captured_fp); 618 } 619 620 // failure modes against a single live session, in increasing severity: 621 // - publickey signed-data binding (session_id and user_name) — server 622 // replies USERAUTH_FAILURE, connection continues 623 // - second CHANNEL_OPEN on the same connection — server replies 624 // CHANNEL_OPEN_FAILURE, connection continues 625 // - MAC-tampered encrypted packet — server bails out of handleConnection 626 // with MacVerificationFailed (terminal) 627 // running all of these against one negotiated session avoids paying the 628 // KEX cost per failure case and keeps the failure tests visually together. 629 test "auth + channel layer failure modes" { 630 const io = std.testing.io; 631 const allocator = std.testing.allocator; 632 633 const host_key = try newHostKey(); 634 635 var c2s_buf: [4096]u8 = undefined; 636 var c2s = Pipe.init(&c2s_buf); 637 var s2c_buf: [4096]u8 = undefined; 638 var s2c = Pipe.init(&s2c_buf); 639 640 var server_rd_buf: [4096]u8 = undefined; 641 var server_wr_buf: [4096]u8 = undefined; 642 var client_rd_buf: [4096]u8 = undefined; 643 var client_wr_buf: [4096]u8 = undefined; 644 var server_pipe_reader = PipeReader.init(io, &c2s, &server_rd_buf); 645 var server_pipe_writer = PipeWriter.init(io, &s2c, &server_wr_buf); 646 var client_pipe_reader = PipeReader.init(io, &s2c, &client_rd_buf); 647 var client_pipe_writer = PipeWriter.init(io, &c2s, &client_wr_buf); 648 const cr = &client_pipe_reader.interface; 649 const cw = &client_pipe_writer.interface; 650 651 var server: ServerTask = .{ 652 .io = io, 653 .allocator = allocator, 654 .host_key = &host_key, 655 .reader = &server_pipe_reader.interface, 656 .writer = &server_pipe_writer.interface, 657 .outgoing = &s2c, 658 }; 659 var server_future = try std.Io.concurrent(io, ServerTask.run, .{&server}); 660 defer server_future.await(io); 661 defer c2s.close(io); 662 663 // bring the connection up to a post-NEWKEYS, ready-to-encrypt state: 664 // banner exchange, KEXINIT exchange, ECDH, NEWKEYS, key derivation. 665 var cs: proto.Cipher = undefined; 666 var sc: proto.Cipher = undefined; 667 var session_id: [Sha256.digest_length]u8 = undefined; 668 { 669 try cw.writeAll(client_version ++ "\r\n"); 670 try cw.flush(); 671 const v_s_line = (try cr.takeDelimiter('\n')) orelse return error.UnexpectedEof; 672 const v_s_trimmed = if (v_s_line.len > 0 and v_s_line[v_s_line.len - 1] == '\r') v_s_line[0 .. v_s_line.len - 1] else v_s_line; 673 const v_s = try allocator.dupe(u8, v_s_trimmed); 674 defer allocator.free(v_s); 675 676 const ckex = try proto.buildKexInit(io, allocator, &.{"curve25519-sha256"}, &proto.our_host_key_algos, &proto.our_ciphers); 677 defer allocator.free(ckex); 678 try proto.writePlainPacket(io, cw, ckex); 679 const skex = try proto.readPlainPacket(allocator, cr); 680 defer allocator.free(skex); 681 682 const eph = try newClientEphemeral(); 683 { 684 var pkt: std.ArrayList(u8) = .empty; 685 defer pkt.deinit(allocator); 686 try pkt.append(allocator, proto.SSH_MSG_KEX_ECDH_INIT); 687 try proto.writeStringField(&pkt, allocator, &eph.public_key); 688 try proto.writePlainPacket(io, cw, pkt.items); 689 } 690 const reply = try proto.readPlainPacket(allocator, cr); 691 defer allocator.free(reply); 692 var rr = std.Io.Reader.fixed(reply[1..]); 693 const k_s = try proto.takeStringField(allocator, &rr, 4096); 694 defer allocator.free(k_s); 695 const q_s = try proto.takeStringField(allocator, &rr, X25519.public_length); 696 defer allocator.free(q_s); 697 var server_pub: [X25519.public_length]u8 = undefined; 698 @memcpy(&server_pub, q_s); 699 const sig_blob = try proto.takeStringField(allocator, &rr, 1024); 700 defer allocator.free(sig_blob); 701 const k = try X25519.scalarmult(eph.secret_key, server_pub); 702 const exchange = try proto.computeExchangeHash(allocator, client_version, v_s, ckex, skex, k_s, &eph.public_key, &server_pub, &k, false); 703 704 try proto.writePlainPacket(io, cw, &[_]u8{proto.SSH_MSG_NEWKEYS}); 705 { 706 const server_newkeys = try proto.readPlainPacket(allocator, cr); 707 defer allocator.free(server_newkeys); 708 try std.testing.expectEqual(@as(u8, proto.SSH_MSG_NEWKEYS), server_newkeys[0]); 709 } 710 711 var keys: proto.SessionKeys = undefined; 712 try proto.deriveSessionKeys(allocator, &k, &exchange, &exchange, &keys, false); 713 cs = proto.Cipher.init(&keys.cs_enc, 3); 714 sc = proto.Cipher.init(&keys.sc_enc, 3); 715 session_id = exchange; 716 } 717 718 // SERVICE_REQUEST → SERVICE_ACCEPT, then USERAUTH_REQUEST publickey 719 // probe → PK_OK, then three signed USERAUTH_REQUEST attempts that 720 // exercise the cryptographic-binding properties of publickey auth: 721 // 1. signed with a different session_id → USERAUTH_FAILURE 722 // (replay-across-sessions defense — H is bound into the signature) 723 // 2. signed with one user_name but the request carries another → 724 // USERAUTH_FAILURE (user_name is bound into the signature) 725 // 3. legit (correct session_id and user_name) → USERAUTH_SUCCESS 726 // each USERAUTH_FAILURE is non-terminal: the server's auth loop just 727 // increments attempts and waits for the next request. 728 const user_kp = try newUserKeypair(); 729 { 730 { 731 var req: std.ArrayList(u8) = .empty; 732 defer req.deinit(allocator); 733 try req.append(allocator, proto.SSH_MSG_SERVICE_REQUEST); 734 try proto.writeStringField(&req, allocator, "ssh-userauth"); 735 try cs.writePacket(io, cw, req.items); 736 } 737 { 738 const accept = try sc.readPacket(allocator, cr); 739 defer allocator.free(accept); 740 try std.testing.expectEqual(@as(u8, proto.SSH_MSG_SERVICE_ACCEPT), accept[0]); 741 var r = std.Io.Reader.fixed(accept[1..]); 742 const service = try proto.takeStringField(allocator, &r, 64); 743 defer allocator.free(service); 744 try std.testing.expectEqualStrings("ssh-userauth", service); 745 } 746 747 var pubkey_blob: std.ArrayList(u8) = .empty; 748 defer pubkey_blob.deinit(allocator); 749 try proto.writeStringField(&pubkey_blob, allocator, "ssh-ed25519"); 750 try proto.writeStringField(&pubkey_blob, allocator, &user_kp.public_key.bytes); 751 752 { 753 var req: std.ArrayList(u8) = .empty; 754 defer req.deinit(allocator); 755 try req.append(allocator, proto.SSH_MSG_USERAUTH_REQUEST); 756 try proto.writeStringField(&req, allocator, "testuser"); 757 try proto.writeStringField(&req, allocator, "ssh-connection"); 758 try proto.writeStringField(&req, allocator, "publickey"); 759 try req.append(allocator, 0); 760 try proto.writeStringField(&req, allocator, "ssh-ed25519"); 761 try proto.writeStringField(&req, allocator, pubkey_blob.items); 762 try cs.writePacket(io, cw, req.items); 763 } 764 { 765 const pk_ok = try sc.readPacket(allocator, cr); 766 defer allocator.free(pk_ok); 767 try std.testing.expectEqual(@as(u8, proto.SSH_MSG_USERAUTH_PK_OK), pk_ok[0]); 768 var r = std.Io.Reader.fixed(pk_ok[1..]); 769 const algo = try proto.takeStringField(allocator, &r, 64); 770 defer allocator.free(algo); 771 try std.testing.expectEqualStrings("ssh-ed25519", algo); 772 const accepted_blob = try proto.takeStringField(allocator, &r, 4096); 773 defer allocator.free(accepted_blob); 774 try std.testing.expectEqualSlices(u8, pubkey_blob.items, accepted_blob); 775 } 776 777 const wrong_session = [_]u8{0xFF} ** Sha256.digest_length; 778 for ([_]struct { 779 sign_session: []const u8, 780 sign_user: []const u8, 781 send_user: []const u8, 782 expect: u8, 783 }{ 784 .{ .sign_session = &wrong_session, .sign_user = "testuser", .send_user = "testuser", .expect = proto.SSH_MSG_USERAUTH_FAILURE }, 785 .{ .sign_session = &session_id, .sign_user = "alice", .send_user = "bob", .expect = proto.SSH_MSG_USERAUTH_FAILURE }, 786 .{ .sign_session = &session_id, .sign_user = "testuser", .send_user = "testuser", .expect = proto.SSH_MSG_USERAUTH_SUCCESS }, 787 }) |attempt| { 788 var signed: std.ArrayList(u8) = .empty; 789 defer signed.deinit(allocator); 790 try proto.appendPublickeySignedData(&signed, allocator, attempt.sign_session, attempt.sign_user, "ssh-connection", "ssh-ed25519", pubkey_blob.items); 791 const sig = try user_kp.sign(signed.items, null); 792 const sig_bytes = sig.toBytes(); 793 var sig_wire: std.ArrayList(u8) = .empty; 794 defer sig_wire.deinit(allocator); 795 try proto.writeStringField(&sig_wire, allocator, "ssh-ed25519"); 796 try proto.writeStringField(&sig_wire, allocator, &sig_bytes); 797 798 var req: std.ArrayList(u8) = .empty; 799 defer req.deinit(allocator); 800 try req.append(allocator, proto.SSH_MSG_USERAUTH_REQUEST); 801 try proto.writeStringField(&req, allocator, attempt.send_user); 802 try proto.writeStringField(&req, allocator, "ssh-connection"); 803 try proto.writeStringField(&req, allocator, "publickey"); 804 try req.append(allocator, 1); 805 try proto.writeStringField(&req, allocator, "ssh-ed25519"); 806 try proto.writeStringField(&req, allocator, pubkey_blob.items); 807 try proto.writeStringField(&req, allocator, sig_wire.items); 808 try cs.writePacket(io, cw, req.items); 809 810 const reply = try sc.readPacket(allocator, cr); 811 defer allocator.free(reply); 812 try std.testing.expectEqual(attempt.expect, reply[0]); 813 } 814 } 815 816 // open two channels back to back: server confirms the first, then 817 // refuses the second with OPEN_FAILURE and stays running. 818 for ([_]struct { id: u32, expect: u8 }{ 819 .{ .id = 7, .expect = proto.SSH_MSG_CHANNEL_OPEN_CONFIRMATION }, 820 .{ .id = 8, .expect = proto.SSH_MSG_CHANNEL_OPEN_FAILURE }, 821 }) |case| { 822 var req: std.ArrayList(u8) = .empty; 823 defer req.deinit(allocator); 824 try req.append(allocator, proto.SSH_MSG_CHANNEL_OPEN); 825 try proto.writeStringField(&req, allocator, "session"); 826 try proto.writeU32(&req, allocator, case.id); 827 try proto.writeU32(&req, allocator, 1 << 20); 828 try proto.writeU32(&req, allocator, 32768); 829 try cs.writePacket(io, cw, req.items); 830 831 const reply = try sc.readPacket(allocator, cr); 832 defer allocator.free(reply); 833 try std.testing.expectEqual(case.expect, reply[0]); 834 } 835 836 // now break the connection: build any encrypted packet, flip one bit of 837 // its trailing Poly1305 tag, and ship the corrupted bytes. server must 838 // surface MacVerificationFailed out of handleConnection. 839 var req: std.ArrayList(u8) = .empty; 840 defer req.deinit(allocator); 841 try req.append(allocator, proto.SSH_MSG_CHANNEL_REQUEST); 842 try proto.writeU32(&req, allocator, 0); 843 try proto.writeStringField(&req, allocator, "shell"); 844 try req.append(allocator, 0); 845 846 var tmp_buf: [512]u8 = undefined; 847 var sink = std.Io.Writer.fixed(&tmp_buf); 848 try cs.writePacket(io, &sink, req.items); 849 const written = sink.buffered(); 850 tmp_buf[written.len - 1] ^= 0x01; 851 try cw.writeAll(tmp_buf[0..written.len]); 852 try cw.flush(); 853 854 server_future.await(io); 855 try std.testing.expectEqual(@as(?anyerror, error.MacVerificationFailed), server.result); 856 } 857 858 const client_version = "SSH-2.0-haxy_test"; 859 860 // in-memory bidirectional pipe — drives the server side of handleConnection 861 // without a socket. one Pipe carries bytes in one direction. 862 863 const Pipe = std.Io.Queue(u8); 864 865 const PipeReader = struct { 866 interface: std.Io.Reader, 867 io: std.Io, 868 pipe: *Pipe, 869 870 fn init(io: std.Io, pipe: *Pipe, buf: []u8) PipeReader { 871 return .{ 872 .interface = .{ 873 .vtable = &.{ .readVec = readVec, .stream = stream }, 874 .buffer = buf, 875 .seek = 0, 876 .end = 0, 877 }, 878 .io = io, 879 .pipe = pipe, 880 }; 881 } 882 883 fn stream(r: *std.Io.Reader, w: *std.Io.Writer, limit: std.Io.Limit) std.Io.Reader.StreamError!usize { 884 // tests don't drive any code path that calls stream, but the vtable 885 // requires a non-default impl. read into a small staging buffer and 886 // forward, so the implementation is correct if it ever fires. 887 const self: *PipeReader = @fieldParentPtr("interface", r); 888 var stage: [256]u8 = undefined; 889 const cap = limit.minInt(stage.len); 890 if (cap == 0) return 0; 891 const n = self.pipe.get(self.io, stage[0..cap], 1) catch |err| switch (err) { 892 error.Closed => return error.EndOfStream, 893 error.Canceled => return error.ReadFailed, 894 }; 895 return try w.write(stage[0..n]); 896 } 897 898 fn readVec(r: *std.Io.Reader, data: [][]u8) std.Io.Reader.Error!usize { 899 const self: *PipeReader = @fieldParentPtr("interface", r); 900 // vtable contract: if data[0] is empty, fill r.buffer[r.end..] instead. 901 const dest = if (data.len == 0 or data[0].len == 0) r.buffer[r.end..] else data[0]; 902 if (dest.len == 0) return 0; 903 const n = self.pipe.get(self.io, dest, 1) catch |err| switch (err) { 904 error.Closed => return error.EndOfStream, 905 error.Canceled => return error.ReadFailed, 906 }; 907 if (data.len == 0 or data[0].len == 0) { 908 r.end += n; 909 return 0; 910 } 911 return n; 912 } 913 }; 914 915 const PipeWriter = struct { 916 interface: std.Io.Writer, 917 io: std.Io, 918 pipe: *Pipe, 919 920 fn init(io: std.Io, pipe: *Pipe, buf: []u8) PipeWriter { 921 return .{ 922 .interface = .{ 923 .vtable = &.{ .drain = drain }, 924 .buffer = buf, 925 }, 926 .io = io, 927 .pipe = pipe, 928 }; 929 } 930 931 fn drain(w: *std.Io.Writer, data: []const []const u8, splat: usize) std.Io.Writer.Error!usize { 932 const self: *PipeWriter = @fieldParentPtr("interface", w); 933 const buffered = w.buffered(); 934 if (buffered.len > 0) self.pipe.putAll(self.io, buffered) catch return error.WriteFailed; 935 936 var extra: usize = 0; 937 if (data.len > 1) { 938 for (data[0 .. data.len - 1]) |chunk| { 939 if (chunk.len == 0) continue; 940 self.pipe.putAll(self.io, chunk) catch return error.WriteFailed; 941 extra += chunk.len; 942 } 943 } 944 if (data.len > 0) { 945 const pattern = data[data.len - 1]; 946 if (pattern.len > 0) { 947 var i: usize = 0; 948 while (i < splat) : (i += 1) { 949 self.pipe.putAll(self.io, pattern) catch return error.WriteFailed; 950 extra += pattern.len; 951 } 952 } 953 } 954 return w.consume(buffered.len + extra); 955 } 956 }; 957 958 // runs proto.handleConnection in a concurrent task (spawned via std.Io.concurrent). 959 // also acts as the session handler — captures the authenticated key's 960 // fingerprint and returns immediately, leaving runChannelLayer's wrap-up to 961 // send exit-status / EOF / CLOSE. records any error so the test can assert 962 // on it after awaiting the future. 963 const ServerTask = struct { 964 io: std.Io, 965 allocator: std.mem.Allocator, 966 host_key: *const proto.HostKey, 967 reader: *std.Io.Reader, 968 writer: *std.Io.Writer, 969 outgoing: *Pipe, // closed on exit so the client's reads return EOF 970 captured_fp: [proto.fingerprint_len]u8 = undefined, 971 captured_exec_buf: [256]u8 = undefined, 972 captured_exec_len: usize = 0, 973 result: ?anyerror = null, 974 975 pub fn handleSession(self: *ServerTask, sess: *proto.SessionCtx, request: proto.Request) !void { 976 @memcpy(&self.captured_fp, &sess.fingerprint); 977 switch (request) { 978 .exec => |cmd| { 979 // copy out of the protocol's stack-scoped buffer so the 980 // test can assert on it after the handler returns. 981 @memcpy(self.captured_exec_buf[0..cmd.len], cmd); 982 self.captured_exec_len = cmd.len; 983 }, 984 .shell => {}, 985 } 986 } 987 988 fn run(self: *ServerTask) void { 989 defer self.outgoing.close(self.io); 990 proto.handleConnection( 991 self.io, 992 self.allocator, 993 self.reader, 994 self.writer, 995 self.host_key, 996 null, 997 self, 998 ) catch |e| { 999 self.result = e; 1000 }; 1001 } 1002 }; 1003 1004 // deterministic keys so failures don't depend on RNG state. 1005 1006 fn newHostKey() !proto.HostKey { 1007 const seed = [_]u8{0xAA} ** Ed25519.KeyPair.seed_length; 1008 return .{ .keypair = try Ed25519.KeyPair.generateDeterministic(seed) }; 1009 } 1010 1011 fn newUserKeypair() !Ed25519.KeyPair { 1012 const seed = [_]u8{0xBB} ** Ed25519.KeyPair.seed_length; 1013 return try Ed25519.KeyPair.generateDeterministic(seed); 1014 } 1015 1016 fn newClientEphemeral() !X25519.KeyPair { 1017 const seed = [_]u8{0xCC} ** X25519.seed_length; 1018 return try X25519.KeyPair.generateDeterministic(seed); 1019 } 1020