ssh2bpp-bare.c 5.6 KB

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