ssh2bpp-bare.c 5.9 KB

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