ssh2bpp-bare.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 void ssh2_bare_bpp_handle_output(BinaryPacketProtocol *bpp);
  21. static PktOut *ssh2_bare_bpp_new_pktout(int type);
  22. static const struct BinaryPacketProtocolVtable ssh2_bare_bpp_vtable = {
  23. ssh2_bare_bpp_free,
  24. ssh2_bare_bpp_handle_input,
  25. ssh2_bare_bpp_handle_output,
  26. ssh2_bare_bpp_new_pktout,
  27. ssh2_bpp_queue_disconnect, /* in sshcommon.c */
  28. };
  29. BinaryPacketProtocol *ssh2_bare_bpp_new(LogContext *logctx)
  30. {
  31. struct ssh2_bare_bpp_state *s = snew(struct ssh2_bare_bpp_state);
  32. memset(s, 0, sizeof(*s));
  33. s->bpp.vt = &ssh2_bare_bpp_vtable;
  34. s->bpp.logctx = logctx;
  35. ssh_bpp_common_setup(&s->bpp);
  36. return &s->bpp;
  37. }
  38. static void ssh2_bare_bpp_free(BinaryPacketProtocol *bpp)
  39. {
  40. struct ssh2_bare_bpp_state *s =
  41. container_of(bpp, struct ssh2_bare_bpp_state, bpp);
  42. sfree(s->pktin);
  43. sfree(s);
  44. }
  45. #define BPP_READ(ptr, len) do \
  46. { \
  47. bool success; \
  48. crMaybeWaitUntilV((success = bufchain_try_fetch_consume( \
  49. s->bpp.in_raw, ptr, len)) || \
  50. s->bpp.input_eof); \
  51. if (!success) \
  52. goto eof; \
  53. ssh_check_frozen(s->bpp.ssh); \
  54. } while (0)
  55. static void ssh2_bare_bpp_handle_input(BinaryPacketProtocol *bpp)
  56. {
  57. struct ssh2_bare_bpp_state *s =
  58. container_of(bpp, struct ssh2_bare_bpp_state, bpp);
  59. crBegin(s->crState);
  60. while (1) {
  61. /* Read the length field. */
  62. {
  63. unsigned char lenbuf[4];
  64. BPP_READ(lenbuf, 4);
  65. s->packetlen = toint(GET_32BIT_MSB_FIRST(lenbuf));
  66. }
  67. if (s->packetlen <= 0 || s->packetlen >= (long)OUR_V2_PACKETLIMIT) {
  68. ssh_sw_abort(s->bpp.ssh, "Invalid packet length received");
  69. crStopV;
  70. }
  71. /*
  72. * Allocate the packet to return, now we know its length.
  73. */
  74. s->pktin = snew_plus(PktIn, s->packetlen);
  75. s->pktin->qnode.prev = s->pktin->qnode.next = NULL;
  76. s->pktin->qnode.on_free_queue = false;
  77. s->maxlen = 0;
  78. s->data = snew_plus_get_aux(s->pktin);
  79. s->pktin->sequence = s->incoming_sequence++;
  80. /*
  81. * Read the remainder of the packet.
  82. */
  83. BPP_READ(s->data, s->packetlen);
  84. /*
  85. * The data we just read is precisely the initial type byte
  86. * followed by the packet payload.
  87. */
  88. s->pktin->type = s->data[0];
  89. s->data++;
  90. s->packetlen--;
  91. BinarySource_INIT(s->pktin, s->data, s->packetlen);
  92. /*
  93. * Log incoming packet, possibly omitting sensitive fields.
  94. */
  95. if (s->bpp.logctx) {
  96. logblank_t blanks[MAX_BLANKS];
  97. int nblanks = ssh2_censor_packet(
  98. s->bpp.pls, s->pktin->type, false,
  99. make_ptrlen(s->data, s->packetlen), blanks);
  100. log_packet(s->bpp.logctx, PKT_INCOMING, s->pktin->type,
  101. ssh2_pkt_type(s->bpp.pls->kctx, s->bpp.pls->actx,
  102. s->pktin->type),
  103. get_ptr(s->pktin), get_avail(s->pktin), nblanks, blanks,
  104. &s->pktin->sequence, 0, NULL);
  105. }
  106. if (ssh2_bpp_check_unimplemented(&s->bpp, s->pktin)) {
  107. sfree(s->pktin);
  108. s->pktin = NULL;
  109. continue;
  110. }
  111. pq_push(&s->bpp.in_pq, s->pktin);
  112. s->pktin = NULL;
  113. }
  114. eof:
  115. if (!s->bpp.expect_close) {
  116. ssh_remote_error(s->bpp.ssh,
  117. "Remote side unexpectedly closed network connection");
  118. } else {
  119. ssh_remote_eof(s->bpp.ssh, "Remote side closed network connection");
  120. }
  121. return; /* avoid touching s now it's been freed */
  122. crFinishV;
  123. }
  124. static PktOut *ssh2_bare_bpp_new_pktout(int pkt_type)
  125. {
  126. PktOut *pkt = ssh_new_packet();
  127. pkt->length = 4; /* space for packet length */
  128. pkt->type = pkt_type;
  129. put_byte(pkt, pkt_type);
  130. return pkt;
  131. }
  132. static void ssh2_bare_bpp_format_packet(struct ssh2_bare_bpp_state *s,
  133. PktOut *pkt)
  134. {
  135. if (s->bpp.logctx) {
  136. ptrlen pktdata = make_ptrlen(pkt->data + 5, pkt->length - 5);
  137. logblank_t blanks[MAX_BLANKS];
  138. int nblanks = ssh2_censor_packet(
  139. s->bpp.pls, pkt->type, true, pktdata, blanks);
  140. log_packet(s->bpp.logctx, PKT_OUTGOING, pkt->type,
  141. ssh2_pkt_type(s->bpp.pls->kctx, s->bpp.pls->actx,
  142. pkt->type),
  143. pktdata.ptr, pktdata.len, nblanks, blanks,
  144. &s->outgoing_sequence,
  145. pkt->downstream_id, pkt->additional_log_text);
  146. }
  147. s->outgoing_sequence++; /* only for diagnostics, really */
  148. PUT_32BIT_MSB_FIRST(pkt->data, pkt->length - 4);
  149. bufchain_add(s->bpp.out_raw, pkt->data, pkt->length);
  150. }
  151. static void ssh2_bare_bpp_handle_output(BinaryPacketProtocol *bpp)
  152. {
  153. struct ssh2_bare_bpp_state *s =
  154. container_of(bpp, struct ssh2_bare_bpp_state, bpp);
  155. PktOut *pkt;
  156. while ((pkt = pq_pop(&s->bpp.out_pq)) != NULL) {
  157. ssh2_bare_bpp_format_packet(s, pkt);
  158. ssh_free_pktout(pkt);
  159. }
  160. }