bpp-bare.c 6.9 KB

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