ssh2bpp-bare.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Trivial binary packet protocol for the 'bare' ssh-connection
  3. * protocol used in PuTTY's SSH-2 connection sharing system.
  4. */
  5. #include <assert.h>
  6. #include "putty.h"
  7. #include "ssh.h"
  8. #include "sshbpp.h"
  9. #include "sshcr.h"
  10. struct ssh2_bare_bpp_state {
  11. int crState;
  12. long packetlen, maxlen;
  13. unsigned char *data;
  14. unsigned long incoming_sequence, outgoing_sequence;
  15. PktIn *pktin;
  16. BinaryPacketProtocol bpp;
  17. };
  18. static void ssh2_bare_bpp_free(BinaryPacketProtocol *bpp);
  19. static void ssh2_bare_bpp_handle_input(BinaryPacketProtocol *bpp);
  20. static PktOut *ssh2_bare_bpp_new_pktout(int type);
  21. static void ssh2_bare_bpp_format_packet(BinaryPacketProtocol *bpp, PktOut *);
  22. static const struct BinaryPacketProtocolVtable ssh2_bare_bpp_vtable = {
  23. ssh2_bare_bpp_free,
  24. ssh2_bare_bpp_handle_input,
  25. ssh2_bare_bpp_new_pktout,
  26. ssh2_bare_bpp_format_packet,
  27. };
  28. BinaryPacketProtocol *ssh2_bare_bpp_new(void)
  29. {
  30. struct ssh2_bare_bpp_state *s = snew(struct ssh2_bare_bpp_state);
  31. memset(s, 0, sizeof(*s));
  32. s->bpp.vt = &ssh2_bare_bpp_vtable;
  33. return &s->bpp;
  34. }
  35. static void ssh2_bare_bpp_free(BinaryPacketProtocol *bpp)
  36. {
  37. struct ssh2_bare_bpp_state *s =
  38. FROMFIELD(bpp, struct ssh2_bare_bpp_state, bpp);
  39. if (s->pktin)
  40. ssh_unref_packet(s->pktin);
  41. sfree(s);
  42. }
  43. static void ssh2_bare_bpp_handle_input(BinaryPacketProtocol *bpp)
  44. {
  45. struct ssh2_bare_bpp_state *s =
  46. FROMFIELD(bpp, struct ssh2_bare_bpp_state, bpp);
  47. crBegin(s->crState);
  48. while (1) {
  49. /* Read the length field. */
  50. {
  51. unsigned char lenbuf[4];
  52. crMaybeWaitUntilV(bufchain_try_fetch_consume(
  53. s->bpp.in_raw, lenbuf, 4));
  54. s->packetlen = toint(GET_32BIT_MSB_FIRST(lenbuf));
  55. }
  56. if (s->packetlen <= 0 || s->packetlen >= (long)OUR_V2_PACKETLIMIT) {
  57. s->bpp.error = dupstr("Invalid packet length received");
  58. crStopV;
  59. }
  60. /*
  61. * Allocate the packet to return, now we know its length.
  62. */
  63. s->pktin = snew_plus(PktIn, s->packetlen);
  64. s->pktin->qnode.prev = s->pktin->qnode.next = NULL;
  65. s->maxlen = 0;
  66. s->pktin->refcount = 1;
  67. s->data = snew_plus_get_aux(s->pktin);
  68. s->pktin->sequence = s->incoming_sequence++;
  69. /*
  70. * Read the remainder of the packet.
  71. */
  72. crMaybeWaitUntilV(bufchain_try_fetch_consume(
  73. s->bpp.in_raw, s->data, s->packetlen));
  74. /*
  75. * The data we just read is precisely the initial type byte
  76. * followed by the packet payload.
  77. */
  78. s->pktin->type = s->data[0];
  79. s->data++;
  80. s->packetlen--;
  81. BinarySource_INIT(s->pktin, s->data, s->packetlen);
  82. /*
  83. * Log incoming packet, possibly omitting sensitive fields.
  84. */
  85. if (s->bpp.logctx) {
  86. logblank_t blanks[MAX_BLANKS];
  87. int nblanks = ssh2_censor_packet(
  88. s->bpp.pls, s->pktin->type, FALSE,
  89. make_ptrlen(s->data, s->packetlen), blanks);
  90. log_packet(s->bpp.logctx, PKT_INCOMING, s->pktin->type,
  91. ssh2_pkt_type(s->bpp.pls->kctx, s->bpp.pls->actx,
  92. s->pktin->type),
  93. get_ptr(s->pktin), get_avail(s->pktin), nblanks, blanks,
  94. &s->pktin->sequence, 0, NULL);
  95. }
  96. pq_push(s->bpp.in_pq, s->pktin);
  97. {
  98. int type = s->pktin->type;
  99. s->pktin = NULL;
  100. if (type == SSH2_MSG_DISCONNECT)
  101. s->bpp.seen_disconnect = TRUE;
  102. }
  103. }
  104. crFinishV;
  105. }
  106. static PktOut *ssh2_bare_bpp_new_pktout(int pkt_type)
  107. {
  108. PktOut *pkt = ssh_new_packet();
  109. pkt->length = 4; /* space for packet length */
  110. pkt->type = pkt_type;
  111. put_byte(pkt, pkt_type);
  112. return pkt;
  113. }
  114. static void ssh2_bare_bpp_format_packet(BinaryPacketProtocol *bpp, PktOut *pkt)
  115. {
  116. struct ssh2_bare_bpp_state *s =
  117. FROMFIELD(bpp, struct ssh2_bare_bpp_state, bpp);
  118. if (s->bpp.logctx) {
  119. ptrlen pktdata = make_ptrlen(pkt->data + 5, pkt->length - 5);
  120. logblank_t blanks[MAX_BLANKS];
  121. int nblanks = ssh2_censor_packet(
  122. s->bpp.pls, pkt->type, TRUE, pktdata, blanks);
  123. log_packet(s->bpp.logctx, PKT_OUTGOING, pkt->type,
  124. ssh2_pkt_type(s->bpp.pls->kctx, s->bpp.pls->actx,
  125. pkt->type),
  126. pktdata.ptr, pktdata.len, nblanks, blanks,
  127. &s->outgoing_sequence,
  128. pkt->downstream_id, pkt->additional_log_text);
  129. }
  130. s->outgoing_sequence++; /* only for diagnostics, really */
  131. PUT_32BIT(pkt->data, pkt->length - 4);
  132. bufchain_add(s->bpp.out_raw, pkt->data, pkt->length);
  133. ssh_free_pktout(pkt);
  134. }