ssh2bpp-bare.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 PktOut *ssh2_bare_bpp_new_pktout(int type);
  21. static void ssh2_bare_bpp_format_packet(BinaryPacketProtocol *bpp, PktOut *);
  22. const struct BinaryPacketProtocolVtable ssh2_bare_bpp_vtable = {
  23. ssh2_bare_bpp_free,
  24. ssh2_bare_bpp_handle_input,
  25. ssh2_bare_bpp_new_pktout,
  26. ssh2_bare_bpp_format_packet,
  27. };
  28. BinaryPacketProtocol *ssh2_bare_bpp_new(void)
  29. {
  30. struct ssh2_bare_bpp_state *s = snew(struct ssh2_bare_bpp_state);
  31. memset(s, 0, sizeof(*s));
  32. s->bpp.vt = &ssh2_bare_bpp_vtable;
  33. return &s->bpp;
  34. }
  35. static void ssh2_bare_bpp_free(BinaryPacketProtocol *bpp)
  36. {
  37. struct ssh2_bare_bpp_state *s =
  38. FROMFIELD(bpp, struct ssh2_bare_bpp_state, bpp);
  39. if (s->pktin)
  40. ssh_unref_packet(s->pktin);
  41. sfree(s);
  42. }
  43. static void ssh2_bare_bpp_handle_input(BinaryPacketProtocol *bpp)
  44. {
  45. struct ssh2_bare_bpp_state *s =
  46. FROMFIELD(bpp, struct ssh2_bare_bpp_state, bpp);
  47. crBegin(s->crState);
  48. while (1) {
  49. /* Read the length field. */
  50. {
  51. unsigned char lenbuf[4];
  52. crMaybeWaitUntilV(bufchain_try_fetch_consume(
  53. s->bpp.in_raw, lenbuf, 4));
  54. s->packetlen = toint(GET_32BIT_MSB_FIRST(lenbuf));
  55. }
  56. if (s->packetlen <= 0 || s->packetlen >= (long)OUR_V2_PACKETLIMIT) {
  57. s->bpp.error = dupstr("Invalid packet length received");
  58. crStopV;
  59. }
  60. /*
  61. * Allocate the packet to return, now we know its length.
  62. */
  63. s->pktin = snew_plus(PktIn, s->packetlen);
  64. s->pktin->qnode.prev = s->pktin->qnode.next = NULL;
  65. s->maxlen = 0;
  66. s->pktin->refcount = 1;
  67. s->data = snew_plus_get_aux(s->pktin);
  68. s->pktin->encrypted_len = s->packetlen;
  69. s->pktin->sequence = s->incoming_sequence++;
  70. /*
  71. * Read the remainder of the packet.
  72. */
  73. crMaybeWaitUntilV(bufchain_try_fetch_consume(
  74. s->bpp.in_raw, s->data, s->packetlen));
  75. /*
  76. * The data we just read is precisely the initial type byte
  77. * followed by the packet payload.
  78. */
  79. s->pktin->type = s->data[0];
  80. s->data++;
  81. s->packetlen--;
  82. BinarySource_INIT(s->pktin, s->data, s->packetlen);
  83. /*
  84. * Log incoming packet, possibly omitting sensitive fields.
  85. */
  86. if (s->bpp.logctx) {
  87. logblank_t blanks[MAX_BLANKS];
  88. int nblanks = ssh2_censor_packet(
  89. s->bpp.pls, s->pktin->type, FALSE,
  90. make_ptrlen(s->data, s->packetlen), blanks);
  91. log_packet(s->bpp.logctx, PKT_INCOMING, s->pktin->type,
  92. ssh2_pkt_type(s->bpp.pls->kctx, s->bpp.pls->actx,
  93. s->pktin->type),
  94. get_ptr(s->pktin), get_avail(s->pktin), nblanks, blanks,
  95. &s->pktin->sequence, 0, NULL);
  96. }
  97. pq_push(s->bpp.in_pq, s->pktin);
  98. {
  99. int type = s->pktin->type;
  100. s->pktin = NULL;
  101. if (type == SSH2_MSG_DISCONNECT)
  102. s->bpp.seen_disconnect = TRUE;
  103. }
  104. }
  105. crFinishV;
  106. }
  107. static PktOut *ssh2_bare_bpp_new_pktout(int pkt_type)
  108. {
  109. PktOut *pkt = ssh_new_packet();
  110. pkt->length = 4; /* space for packet length */
  111. pkt->type = pkt_type;
  112. put_byte(pkt, pkt_type);
  113. return pkt;
  114. }
  115. static void ssh2_bare_bpp_format_packet(BinaryPacketProtocol *bpp, PktOut *pkt)
  116. {
  117. struct ssh2_bare_bpp_state *s =
  118. FROMFIELD(bpp, struct ssh2_bare_bpp_state, bpp);
  119. if (s->bpp.logctx) {
  120. ptrlen pktdata = make_ptrlen(pkt->data + 5, pkt->length - 5);
  121. logblank_t blanks[MAX_BLANKS];
  122. int nblanks = ssh2_censor_packet(
  123. s->bpp.pls, pkt->type, TRUE, pktdata, blanks);
  124. log_packet(s->bpp.logctx, PKT_OUTGOING, pkt->type,
  125. ssh2_pkt_type(s->bpp.pls->kctx, s->bpp.pls->actx,
  126. pkt->type),
  127. pktdata.ptr, pktdata.len, nblanks, blanks,
  128. &s->outgoing_sequence,
  129. pkt->downstream_id, pkt->additional_log_text);
  130. }
  131. s->outgoing_sequence++; /* only for diagnostics, really */
  132. PUT_32BIT(pkt->data, pkt->length - 4);
  133. bufchain_add(s->bpp.out_raw, pkt->data, pkt->length);
  134. ssh_free_pktout(pkt);
  135. }