╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_ssh_protocol.zig ───────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1319 if (!ok) { 1320 auth_attempts += 1; 1321 if (auth_attempts >= max_auth_attempts) return error.TooManyAuthAttempts; 1322 try sendUserauthFailure(io, allocator, sc_cipher, writer); 1323 continue; 1324 } 1325 1326 try sc_cipher.writePacket(io, allocator, writer, &[_]u8{SSH_MSG_USERAUTH_SUCCESS}); 1327 return formatFingerprint(pubkey_blob); 1328 } 1329 } 1330 1331 fn sendUserauthFailure( 1332 io: std.Io, 1333 allocator: std.mem.Allocator, 1334 sc_cipher: *Cipher, 1335 writer: *std.Io.Writer, 1336 ) !void { 1337 var buf: std.ArrayList(u8) = .empty; 1338 defer buf.deinit(allocator); 1339 try buf.append(allocator, SSH_MSG_USERAUTH_FAILURE); 1340 try writeNameList(&buf, allocator, &.{"publickey"}); // allowed methods 1341 try buf.append(allocator, 0); // partial_success = false 1342 try sc_cipher.writePacket(io, allocator, writer, buf.items); 1343 } 1344 1345 fn sendDisconnect( 1346 io: std.Io, 1347 allocator: std.mem.Allocator, 1348 sc_cipher: *Cipher, 1349 writer: *std.Io.Writer, 1350 description: []const u8, 1351 ) !void { 1352 var buf: std.ArrayList(u8) = .empty; 1353 defer buf.deinit(allocator); 1354 try buf.append(allocator, SSH_MSG_DISCONNECT); 1355 try writeU32(&buf, allocator, SSH_DISCONNECT_PROTOCOL_ERROR); 1356 try writeStringField(&buf, allocator, description); 1357 try writeStringField(&buf, allocator, ""); 1358 try sc_cipher.writePacket(io, allocator, writer, buf.items); 1359 } 1360 1361 /// Append the canonical publickey-signed-data bytes (RFC 4252 §7) to `buf`. 1362 /// Used by the server to recompute the signed input for verification 1363 pub fn appendPublickeySignedData( 1364 buf: *std.ArrayList(u8), 1365 allocator: std.mem.Allocator, 1366 session_id: []const u8, 1367 user_name: []const u8, 1368 service_name: []const u8, 1369 algo: []const u8, 1370 pubkey_blob: []const u8, 1371 ) !void { 1372 try writeStringField(buf, allocator, session_id); 1373 try buf.append(allocator, SSH_MSG_USERAUTH_REQUEST); 1374 try writeStringField(buf, allocator, user_name); 1375 try writeStringField(buf, allocator, service_name); 1376 try writeStringField(buf, allocator, "publickey"); 1377 try buf.append(allocator, 1); 1378 try writeStringField(buf, allocator, algo); 1379 try writeStringField(buf, allocator, pubkey_blob); 1380 } 1381 1382 // build the bytes a publickey signature is computed over (RFC 4252 §7) and 1383 // verify the supplied signature against them. 1384 fn verifyUserauthSignature( 1385 allocator: std.mem.Allocator, 1386 session_id: *const [Sha256.digest_length]u8, 1387 user_name: []const u8, 1388 service_name: []const u8, 1389 algo: []const u8, 1390 pubkey_blob: []const u8, 1391 signature_blob: []const u8, 1392 ) !bool { 1393 var signed: std.ArrayList(u8) = .empty; 1394 defer signed.deinit(allocator); 1395 try appendPublickeySignedData(&signed, allocator, session_id, user_name, service_name, algo, pubkey_blob); 1396 1397 // parse pubkey_blob: string "ssh-ed25519" || string raw_pubkey 1398 var pubkey_reader = std.Io.Reader.fixed(pubkey_blob); 1399 const pk_algo = try takeStringField(allocator, &pubkey_reader, 64); 1400 defer allocator.free(pk_algo); 1401 if (!std.mem.eql(u8, pk_algo, "ssh-ed25519")) return false; 1402 const raw_pubkey = try takeStringField(allocator, &pubkey_reader, 64); 1403 defer allocator.free(raw_pubkey); 1404 if (raw_pubkey.len != Ed25519.PublicKey.encoded_length) return false; 1405 1406 // parse signature_blob: string "ssh-ed25519" || string raw_signature 1407 var sig_reader = std.Io.Reader.fixed(signature_blob); 1408 const sig_algo = try takeStringField(allocator, &sig_reader, 64); 1409 defer allocator.free(sig_algo); 1410 if (!std.mem.eql(u8, sig_algo, "ssh-ed25519")) return false; 1411 const raw_sig = try takeStringField(allocator, &sig_reader, Ed25519.Signature.encoded_length); 1412 defer allocator.free(raw_sig); 1413 if (raw_sig.len != Ed25519.Signature.encoded_length) return false; 1414 1415 var pubkey_bytes: [Ed25519.PublicKey.encoded_length]u8 = undefined; 1416 @memcpy(&pubkey_bytes, raw_pubkey); 1417 var sig_bytes: [Ed25519.Signature.encoded_length]u8 = undefined; 1418 @memcpy(&sig_bytes, raw_sig); 1419 1420 const pk = Ed25519.PublicKey.fromBytes(pubkey_bytes) catch return false; 1421 const sig = Ed25519.Signature.fromBytes(sig_bytes); 1422 sig.verify(signed.items, pk) catch return false; 1423 return true; 1424 } 1425 1426 // --------------------------------------------------------------------------- 1427 // connection layer (RFC 4254): channels, requests, data, flow control 1428 // --------------------------------------------------------------------------- 1429 1430 const Channel = struct { 1431 local_id: u32, 1432 remote_id: u32, 1433 local_window: u32, // bytes peer may still send us before we adjust 1434 remote_window: u32, // bytes we may still send to peer before they adjust 1435 max_packet: u32, // max CHANNEL_DATA payload the peer accepts in one packet 1436 eof_received: bool = false, 1437 pty: ?PtySize = null, 1438 }; 1439 1440 pub const PtySize = struct { width_cells: u16, height_cells: u16 }; 1441 1442 fn runChannelLayer( 1443 io: std.Io, 1444 allocator: std.mem.Allocator, 1445 reader: *std.Io.Reader, 1446 writer: *std.Io.Writer, 1447 cs_cipher: *Cipher, 1448 sc_cipher: *Cipher, 1449 fingerprint: *const [fingerprint_len]u8, 1450 handler: anytype, 1451 ) !void { 1452 var channel: ?Channel = null; 1453 var pending_exec: ?[]u8 = null; 1454 defer if (pending_exec) |s| allocator.free(s); 1455 1456 while (true) { 1457 const packet = try cs_cipher.readPacket(allocator, reader); 1458 defer allocator.free(packet); 1459 if (packet.len < 1) return error.EmptyPacket; 1460 const msg_type = packet[0]; 1461 1462 switch (msg_type) { 1463 SSH_MSG_GLOBAL_REQUEST => try handleGlobalRequest(io, allocator, sc_cipher, writer, packet), 1464 1465 SSH_MSG_KEXINIT => { 1466 try sendDisconnect(io, allocator, sc_cipher, writer, "rekey is not supported"); 1467 return error.RekeyUnsupported; 1468 }, 1469 1470 SSH_MSG_CHANNEL_OPEN => { 1471 if (channel != null) { 1472 try sendChannelOpenFailure(io, allocator, sc_cipher, writer, packet, SSH_OPEN_RESOURCE_SHORTAGE, "only one channel per connection"); 1473 continue; 1474 } 1475 channel = try handleChannelOpen(io, allocator, sc_cipher, writer, packet) orelse continue; 1476 }, 1477 1478 SSH_MSG_CHANNEL_REQUEST => { 1479 const ch = if (channel) |*c| c else continue; 1480 const start_request = try handleChannelRequest(io, allocator, sc_cipher, writer, ch, packet, &pending_exec); 1481 if (start_request) |req_kind| { 1482 const request: Request = switch (req_kind) { 1483 .shell => .{ .shell = ch.pty }, 1484 .exec => .{ .exec = pending_exec.? }, 1485 }; 1486 var sess = SessionCtx{ 1487 .io = io, 1488 .allocator = allocator, 1489 .reader = reader, 1490 .writer = writer, 1491 .cs_cipher = cs_cipher, 1492 .sc_cipher = sc_cipher, 1493 .channel = ch, 1494 .fingerprint = fingerprint.*, 1495 }; 1496 defer sess.deinit(); 1497 handler.handleSession(&sess, request) catch |session_err| { 1498 // try to send a non-zero exit status before closing 1499 sess.exit(1) catch {}; 1500 return session_err; 1501 }; 1502 if (!sess.closed) try sess.exit(0); 1503 return; 1504 } 1505 }, 1506 1507 SSH_MSG_CHANNEL_WINDOW_ADJUST => { 1508 const ch = if (channel) |*c| c else continue; 1509 try handleWindowAdjust(ch, packet); 1510 }, 1511 1512 SSH_MSG_CHANNEL_DATA, SSH_MSG_CHANNEL_EXTENDED_DATA => { 1513 const ch = if (channel) |*c| c else continue; 1514 // before shell/exec the client shouldn't be sending data, but 1515 // some clients are chatty; discard and refill window. 1516 try discardChannelData(io, allocator, sc_cipher, writer, ch, packet, msg_type == SSH_MSG_CHANNEL_EXTENDED_DATA); 1517 }, 1518 1519 SSH_MSG_CHANNEL_EOF => { 1520 if (channel) |*c| { 1521 try parseChannelId(packet, c.local_id); 1522 c.eof_received = true; 1523 } 1524 }, 1525 1526 SSH_MSG_CHANNEL_CLOSE => { 1527 if (channel) |*c| try parseChannelId(packet, c.local_id); 1528 return; 1529 }, 1530 1531 else => {}, // ignore unknown messages 1532 } 1533 } 1534 } 1535 1536 fn handleGlobalRequest( 1537 io: std.Io, 1538 allocator: std.mem.Allocator, 1539 sc_cipher: *Cipher, 1540 writer: *std.Io.Writer, 1541 packet: []const u8, 1542 ) !void { 1543 // byte SSH_MSG_GLOBAL_REQUEST 1544 // string request_name 1545 // boolean want_reply 1546 // …request-specific data we ignore 1547 var r = std.Io.Reader.fixed(packet[1..]); 1548 const name = try takeStringField(allocator, &r, 256); 1549 defer allocator.free(name); 1550 const want_reply = (try r.takeByte()) != 0; 1551 if (want_reply) { 1552 try sc_cipher.writePacket(io, allocator, writer, &[_]u8{SSH_MSG_REQUEST_FAILURE}); 1553 } 1554 } 1555 1556 /// Parse a CHANNEL_OPEN. On a session channel, send CHANNEL_OPEN_CONFIRMATION 1557 /// and return the new Channel. On other types, send CHANNEL_OPEN_FAILURE and 1558 /// return null. 1559 fn handleChannelOpen( 1560 io: std.Io, 1561 allocator: std.mem.Allocator, 1562 sc_cipher: *Cipher, 1563 writer: *std.Io.Writer, 1564 packet: []const u8, 1565 ) !?Channel { 1566 // byte SSH_MSG_CHANNEL_OPEN 1567 // string channel_type 1568 // uint32 sender_channel (peer's id) 1569 // uint32 initial_window 1570 // uint32 max_packet 1571 var r = std.Io.Reader.fixed(packet[1..]); 1572 const ch_type = try takeStringField(allocator, &r, 64); 1573 defer allocator.free(ch_type); 1574 const remote_id = try r.takeInt(u32, .big); 1575 const initial_window = try r.takeInt(u32, .big); 1576 const max_packet = try r.takeInt(u32, .big); 1577 1578 if (!std.mem.eql(u8, ch_type, "session")) { 1579 try sendChannelOpenFailure(io, allocator, sc_cipher, writer, packet, SSH_OPEN_UNKNOWN_CHANNEL_TYPE, "only session channels are supported"); 1580 return null; 1581 } 1582 1583 const local_id: u32 = 0; // single-channel connection — id is always 0 from our side 1584 var reply: std.ArrayList(u8) = .empty; 1585 defer reply.deinit(allocator); 1586 try reply.append(allocator, SSH_MSG_CHANNEL_OPEN_CONFIRMATION); 1587 try writeU32(&reply, allocator, remote_id); 1588 try writeU32(&reply, allocator, local_id); 1589 try writeU32(&reply, allocator, initial_recv_window); 1590 try writeU32(&reply, allocator, max_packet_size); 1591 try sc_cipher.writePacket(io, allocator, writer, reply.items); 1592 1593 return .{ 1594 .local_id = local_id, 1595 .remote_id = remote_id, 1596 .local_window = initial_recv_window, 1597 .remote_window = initial_window, 1598 .max_packet = max_packet, 1599 }; 1600 } 1601 1602 fn sendChannelOpenFailure( 1603 io: std.Io, 1604 allocator: std.mem.Allocator, 1605 sc_cipher: *Cipher, 1606 writer: *std.Io.Writer, 1607 open_packet: []const u8, 1608 reason: u32, 1609 description: []const u8, 1610 ) !void { 1611 // recover the sender_channel field from the CHANNEL_OPEN packet so we 1612 // address the failure to the correct id 1613 var r = std.Io.Reader.fixed(open_packet[1..]); 1614 const ch_type_len = try r.takeInt(u32, .big); 1615 try r.discardAll(ch_type_len); 1616 const remote_id = try r.takeInt(u32, .big); 1617 1618 var reply: std.ArrayList(u8) = .empty; 1619 defer reply.deinit(allocator); 1620 try reply.append(allocator, SSH_MSG_CHANNEL_OPEN_FAILURE); 1621 try writeU32(&reply, allocator, remote_id); 1622 try writeU32(&reply, allocator, reason); 1623 try writeStringField(&reply, allocator, description); 1624 try writeStringField(&reply, allocator, ""); // language tag 1625 try sc_cipher.writePacket(io, allocator, writer, reply.items); 1626 } 1627 1628 const RequestKind = enum { shell, exec }; 1629 1630 /// Returns the kind of session request if this triggered one, else null. 1631 fn handleChannelRequest( 1632 io: std.Io, 1633 allocator: std.mem.Allocator, 1634 sc_cipher: *Cipher, 1635 writer: *std.Io.Writer, 1636 ch: *Channel, 1637 packet: []const u8, 1638 pending_exec: *?[]u8, 1639 ) !?RequestKind { 1640 // byte SSH_MSG_CHANNEL_REQUEST 1641 // uint32 recipient_channel (== our local_id) 1642 // string request_type 1643 // boolean want_reply 1644 // …request-specific data 1645 var r = std.Io.Reader.fixed(packet[1..]); 1646 const recipient_channel = try r.takeInt(u32, .big); 1647 if (recipient_channel != ch.local_id) return error.UnknownChannel; 1648 const req_type = try takeStringField(allocator, &r, 64); 1649 defer allocator.free(req_type); 1650 const want_reply = (try r.takeByte()) != 0; 1651 1652 if (std.mem.eql(u8, req_type, "pty-req")) { 1653 // string TERM, uint32 width chars, uint32 height rows, uint32 width px, 1654 // uint32 height px, string modes 1655 const term = try takeStringField(allocator, &r, 64); 1656 defer allocator.free(term); 1657 const width = try r.takeInt(u32, .big); 1658 const height = try r.takeInt(u32, .big); 1659 ch.pty = .{ .width_cells = @intCast(@min(width, 0xFFFF)), .height_cells = @intCast(@min(height, 0xFFFF)) }; 1660 try replyChannelRequest(io, allocator, sc_cipher, writer, ch, want_reply, true); 1661 return null; 1662 } 1663 1664 if (std.mem.eql(u8, req_type, "env")) { 1665 // ignore environment variables silently (we don't pass them anywhere) 1666 try replyChannelRequest(io, allocator, sc_cipher, writer, ch, want_reply, true); 1667 return null; 1668 } 1669 1670 if (std.mem.eql(u8, req_type, "window-change")) { 1671 // uint32 width cells, uint32 height rows, uint32 width px, uint32 height px 1672 const width = try r.takeInt(u32, .big); 1673 const height = try r.takeInt(u32, .big); 1674 if (ch.pty != null) { 1675 ch.pty = .{ .width_cells = @intCast(@min(width, 0xFFFF)), .height_cells = @intCast(@min(height, 0xFFFF)) }; 1676 } 1677 // spec: window-change MUST NOT request a reply, but accept either 1678 try replyChannelRequest(io, allocator, sc_cipher, writer, ch, want_reply, true); 1679 return null; 1680 } 1681 1682 if (std.mem.eql(u8, req_type, "shell")) { 1683 try replyChannelRequest(io, allocator, sc_cipher, writer, ch, want_reply, true); 1684 return .shell; 1685 } 1686 1687 if (std.mem.eql(u8, req_type, "exec")) { 1688 // capture the command string for the handler to inspect 1689 const cmd = try takeStringField(allocator, &r, 4096); 1690 if (pending_exec.*) |old| allocator.free(old); 1691 pending_exec.* = cmd; 1692 try replyChannelRequest(io, allocator, sc_cipher, writer, ch, want_reply, true); 1693 return .exec; 1694 } 1695 1696 // unknown request kind — fail it 1697 try replyChannelRequest(io, allocator, sc_cipher, writer, ch, want_reply, false); 1698 return null; 1699 } 1700 1701 fn replyChannelRequest( 1702 io: std.Io, 1703 allocator: std.mem.Allocator, 1704 sc_cipher: *Cipher, 1705 writer: *std.Io.Writer, 1706 ch: *Channel, 1707 want_reply: bool, 1708 success: bool, 1709 ) !void { 1710 if (!want_reply) return; 1711 var reply: std.ArrayList(u8) = .empty; 1712 defer reply.deinit(allocator); 1713 try reply.append(allocator, if (success) SSH_MSG_CHANNEL_SUCCESS else SSH_MSG_CHANNEL_FAILURE); 1714 try writeU32(&reply, allocator, ch.remote_id); 1715 try sc_cipher.writePacket(io, allocator, writer, reply.items); 1716 } 1717 1718 fn handleWindowAdjust(ch: *Channel, packet: []const u8) !void { 1719 // byte SSH_MSG_CHANNEL_WINDOW_ADJUST 1720 // uint32 recipient_channel 1721 // uint32 bytes_to_add 1722 var r = std.Io.Reader.fixed(packet[1..]); 1723 const recipient_channel = try r.takeInt(u32, .big); 1724 if (recipient_channel != ch.local_id) return error.UnknownChannel; 1725 const add = try r.takeInt(u32, .big); 1726 ch.remote_window +|= add; 1727 } 1728 1729 fn discardChannelData( 1730 io: std.Io, 1731 allocator: std.mem.Allocator, 1732 sc_cipher: *Cipher, 1733 writer: *std.Io.Writer, 1734 ch: *Channel, 1735 packet: []const u8, 1736 extended: bool, 1737 ) !void { 1738 // byte SSH_MSG_CHANNEL_DATA/EXTENDED_DATA 1739 // uint32 recipient_channel 1740 // [if extended: uint32 type_code] 1741 // string data 1742 const payload = try parseChannelData(packet, extended, ch.local_id); 1743 const data_len: u32 = @intCast(payload.len); 1744 1745 if (data_len > ch.local_window) return error.WindowExceeded; 1746 ch.local_window -= data_len; 1747 try maybeRefillRecvWindow(io, allocator, sc_cipher, writer, ch); 1748 } 1749 1750 pub fn parseChannelData(packet: []const u8, extended: bool, expected_channel: u32) ![]const u8 { 1751 if (packet.len < 1) return error.EmptyPacket; 1752 var r = std.Io.Reader.fixed(packet[1..]); 1753 const recipient_channel = try r.takeInt(u32, .big); 1754 if (recipient_channel != expected_channel) return error.UnknownChannel; 1755 if (extended) { 1756 const data_type = try r.takeInt(u32, .big); 1757 if (data_type != SSH_EXTENDED_DATA_STDERR) return error.UnsupportedExtendedData; 1758 } 1759 const data_len = try r.takeInt(u32, .big); 1760 // catches both data_len > remaining (would otherwise be a bounds-check 1761 // panic on the slice) and trailing junk after the declared payload. 1762 if (data_len != r.buffered().len) return error.InvalidChannelData; 1763 return r.buffered(); 1764 } 1765 1766 fn parseChannelId(packet: []const u8, expected_channel: u32) !void { 1767 if (packet.len < 1) return error.EmptyPacket; 1768 var r = std.Io.Reader.fixed(packet[1..]); 1769 const recipient_channel = try r.takeInt(u32, .big); 1770 if (recipient_channel != expected_channel) return error.UnknownChannel; 1771 if (r.buffered().len != 0) return error.InvalidChannelMessage; 1772 } 1773 1774 /// Send a CHANNEL_WINDOW_ADJUST if our receive window has dipped below half 1775 /// of the initial value. Used both for the pre-session discard path and by 1776 /// SessionCtx after it consumes a CHANNEL_DATA payload. 1777 fn maybeRefillRecvWindow( 1778 io: std.Io, 1779 allocator: std.mem.Allocator, 1780 sc_cipher: *Cipher, 1781 writer: *std.Io.Writer, 1782 ch: *Channel, 1783 ) !void { 1784 if (ch.local_window >= initial_recv_window / 2) return; 1785 const add = initial_recv_window - ch.local_window; 1786 ch.local_window += add; 1787 var adj: std.ArrayList(u8) = .empty; 1788 defer adj.deinit(allocator); 1789 try adj.append(allocator, SSH_MSG_CHANNEL_WINDOW_ADJUST); 1790 try writeU32(&adj, allocator, ch.remote_id); 1791 try writeU32(&adj, allocator, add); 1792 try sc_cipher.writePacket(io, allocator, writer, adj.items); 1793 } 1794 1795 pub fn writeU32(buf: *std.ArrayList(u8), allocator: std.mem.Allocator, value: u32) !void { 1796 var bytes: [4]u8 = undefined; 1797 std.mem.writeInt(u32, &bytes, value, .big); 1798 try buf.appendSlice(allocator, &bytes); 1799 } 1800