ssh2bpp-bare.c 6.3 KB

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