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