ssh2bpp-bare.c 6.0 KB

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