ssh1bpp.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * Binary packet protocol for SSH-1.
  3. */
  4. #include <assert.h>
  5. #include "putty.h"
  6. #include "ssh.h"
  7. #include "sshbpp.h"
  8. #include "sshcr.h"
  9. struct ssh1_bpp_state {
  10. int crState;
  11. long len, pad, biglen, length, maxlen;
  12. unsigned char *data;
  13. unsigned long realcrc, gotcrc;
  14. int chunk;
  15. PktIn *pktin;
  16. const struct ssh_cipher *cipher;
  17. void *cipher_ctx;
  18. void *crcda_ctx;
  19. void *compctx, *decompctx;
  20. BinaryPacketProtocol bpp;
  21. };
  22. static void ssh1_bpp_free(BinaryPacketProtocol *bpp);
  23. static void ssh1_bpp_handle_input(BinaryPacketProtocol *bpp);
  24. static PktOut *ssh1_bpp_new_pktout(int type);
  25. static void ssh1_bpp_format_packet(BinaryPacketProtocol *bpp, PktOut *pkt);
  26. const struct BinaryPacketProtocolVtable ssh1_bpp_vtable = {
  27. ssh1_bpp_free,
  28. ssh1_bpp_handle_input,
  29. ssh1_bpp_new_pktout,
  30. ssh1_bpp_format_packet,
  31. };
  32. BinaryPacketProtocol *ssh1_bpp_new(void)
  33. {
  34. struct ssh1_bpp_state *s = snew(struct ssh1_bpp_state);
  35. memset(s, 0, sizeof(*s));
  36. s->bpp.vt = &ssh1_bpp_vtable;
  37. return &s->bpp;
  38. }
  39. static void ssh1_bpp_free(BinaryPacketProtocol *bpp)
  40. {
  41. struct ssh1_bpp_state *s = FROMFIELD(bpp, struct ssh1_bpp_state, bpp);
  42. if (s->cipher)
  43. s->cipher->free_context(s->cipher_ctx);
  44. if (s->compctx)
  45. zlib_compress_cleanup(s->compctx);
  46. if (s->decompctx)
  47. zlib_decompress_cleanup(s->decompctx);
  48. if (s->crcda_ctx)
  49. crcda_free_context(s->crcda_ctx);
  50. if (s->pktin)
  51. ssh_unref_packet(s->pktin);
  52. sfree(s);
  53. }
  54. void ssh1_bpp_new_cipher(BinaryPacketProtocol *bpp,
  55. const struct ssh_cipher *cipher,
  56. const void *session_key)
  57. {
  58. struct ssh1_bpp_state *s;
  59. assert(bpp->vt == &ssh1_bpp_vtable);
  60. s = FROMFIELD(bpp, struct ssh1_bpp_state, bpp);
  61. assert(!s->cipher);
  62. s->cipher = cipher;
  63. if (s->cipher) {
  64. s->cipher_ctx = cipher->make_context();
  65. cipher->sesskey(s->cipher_ctx, session_key);
  66. assert(!s->crcda_ctx);
  67. s->crcda_ctx = crcda_make_context();
  68. }
  69. }
  70. void ssh1_bpp_start_compression(BinaryPacketProtocol *bpp)
  71. {
  72. struct ssh1_bpp_state *s;
  73. assert(bpp->vt == &ssh1_bpp_vtable);
  74. s = FROMFIELD(bpp, struct ssh1_bpp_state, bpp);
  75. assert(!s->compctx);
  76. assert(!s->decompctx);
  77. s->compctx = zlib_compress_init();
  78. s->decompctx = zlib_decompress_init();
  79. }
  80. static void ssh1_bpp_handle_input(BinaryPacketProtocol *bpp)
  81. {
  82. struct ssh1_bpp_state *s = FROMFIELD(bpp, struct ssh1_bpp_state, bpp);
  83. crBegin(s->crState);
  84. while (1) {
  85. s->maxlen = 0;
  86. s->length = 0;
  87. {
  88. unsigned char lenbuf[4];
  89. crMaybeWaitUntilV(bufchain_try_fetch_consume(
  90. bpp->in_raw, lenbuf, 4));
  91. s->len = toint(GET_32BIT_MSB_FIRST(lenbuf));
  92. }
  93. if (s->len < 0 || s->len > 262144) { /* SSH1.5-mandated max size */
  94. s->bpp.error = dupprintf(
  95. "Extremely large packet length from server suggests"
  96. " data stream corruption");
  97. crStopV;
  98. }
  99. s->pad = 8 - (s->len % 8);
  100. s->biglen = s->len + s->pad;
  101. s->length = s->len - 5;
  102. /*
  103. * Allocate the packet to return, now we know its length.
  104. */
  105. s->pktin = snew_plus(PktIn, s->biglen);
  106. s->pktin->qnode.prev = s->pktin->qnode.next = NULL;
  107. s->pktin->refcount = 1;
  108. s->pktin->type = 0;
  109. s->maxlen = s->biglen;
  110. s->data = snew_plus_get_aux(s->pktin);
  111. crMaybeWaitUntilV(bufchain_try_fetch_consume(
  112. bpp->in_raw, s->data, s->biglen));
  113. if (s->cipher && detect_attack(s->crcda_ctx,
  114. s->data, s->biglen, NULL)) {
  115. s->bpp.error = dupprintf(
  116. "Network attack (CRC compensation) detected!");
  117. crStopV;
  118. }
  119. if (s->cipher)
  120. s->cipher->decrypt(s->cipher_ctx, s->data, s->biglen);
  121. s->realcrc = crc32_compute(s->data, s->biglen - 4);
  122. s->gotcrc = GET_32BIT(s->data + s->biglen - 4);
  123. if (s->gotcrc != s->realcrc) {
  124. s->bpp.error = dupprintf(
  125. "Incorrect CRC received on packet");
  126. crStopV;
  127. }
  128. if (s->decompctx) {
  129. unsigned char *decompblk;
  130. int decomplen;
  131. if (!zlib_decompress_block(s->decompctx,
  132. s->data + s->pad, s->length + 1,
  133. &decompblk, &decomplen)) {
  134. s->bpp.error = dupprintf(
  135. "Zlib decompression encountered invalid data");
  136. crStopV;
  137. }
  138. if (s->maxlen < s->pad + decomplen) {
  139. PktIn *old_pktin = s->pktin;
  140. s->maxlen = s->pad + decomplen;
  141. s->pktin = snew_plus(PktIn, s->maxlen);
  142. *s->pktin = *old_pktin; /* structure copy */
  143. s->data = snew_plus_get_aux(s->pktin);
  144. smemclr(old_pktin, s->biglen);
  145. sfree(old_pktin);
  146. }
  147. memcpy(s->data + s->pad, decompblk, decomplen);
  148. sfree(decompblk);
  149. s->length = decomplen - 1;
  150. }
  151. /*
  152. * Now we can find the bounds of the semantic content of the
  153. * packet, and the initial type byte.
  154. */
  155. s->data += s->pad;
  156. s->pktin->type = *s->data++;
  157. BinarySource_INIT(s->pktin, s->data, s->length);
  158. if (s->bpp.logctx) {
  159. logblank_t blanks[MAX_BLANKS];
  160. int nblanks = ssh1_censor_packet(
  161. s->bpp.pls, s->pktin->type, FALSE,
  162. make_ptrlen(s->data, s->length), blanks);
  163. log_packet(s->bpp.logctx, PKT_INCOMING, s->pktin->type,
  164. ssh1_pkt_type(s->pktin->type),
  165. get_ptr(s->pktin), get_avail(s->pktin), nblanks, blanks,
  166. NULL, 0, NULL);
  167. }
  168. pq_push(s->bpp.in_pq, s->pktin);
  169. {
  170. int type = s->pktin->type;
  171. s->pktin = NULL;
  172. if (type == SSH1_MSG_DISCONNECT)
  173. s->bpp.seen_disconnect = TRUE;
  174. }
  175. }
  176. crFinishV;
  177. }
  178. static PktOut *ssh1_bpp_new_pktout(int pkt_type)
  179. {
  180. PktOut *pkt = ssh_new_packet();
  181. pkt->length = 4 + 8; /* space for length + max padding */
  182. put_byte(pkt, pkt_type);
  183. pkt->prefix = pkt->length;
  184. pkt->type = pkt_type;
  185. pkt->downstream_id = 0;
  186. pkt->additional_log_text = NULL;
  187. return pkt;
  188. }
  189. static void ssh1_bpp_format_packet(BinaryPacketProtocol *bpp, PktOut *pkt)
  190. {
  191. struct ssh1_bpp_state *s = FROMFIELD(bpp, struct ssh1_bpp_state, bpp);
  192. int pad, biglen, i, pktoffs;
  193. unsigned long crc;
  194. int len;
  195. if (s->bpp.logctx) {
  196. ptrlen pktdata = make_ptrlen(pkt->data + pkt->prefix,
  197. pkt->length - pkt->prefix);
  198. logblank_t blanks[MAX_BLANKS];
  199. int nblanks = ssh1_censor_packet(
  200. s->bpp.pls, pkt->type, TRUE, pktdata, blanks);
  201. log_packet(s->bpp.logctx, PKT_OUTGOING, pkt->type,
  202. ssh1_pkt_type(pkt->type),
  203. pktdata.ptr, pktdata.len, nblanks, blanks,
  204. NULL, 0, NULL);
  205. }
  206. if (s->compctx) {
  207. unsigned char *compblk;
  208. int complen;
  209. zlib_compress_block(s->compctx, pkt->data + 12, pkt->length - 12,
  210. &compblk, &complen, 0);
  211. /* Replace the uncompressed packet data with the compressed
  212. * version. */
  213. pkt->length = 12;
  214. put_data(pkt, compblk, complen);
  215. sfree(compblk);
  216. }
  217. put_uint32(pkt, 0); /* space for CRC */
  218. len = pkt->length - 4 - 8; /* len(type+data+CRC) */
  219. pad = 8 - (len % 8);
  220. pktoffs = 8 - pad;
  221. biglen = len + pad; /* len(padding+type+data+CRC) */
  222. for (i = pktoffs; i < 4+8; i++)
  223. pkt->data[i] = random_byte();
  224. crc = crc32_compute(pkt->data + pktoffs + 4,
  225. biglen - 4); /* all ex len */
  226. PUT_32BIT(pkt->data + pktoffs + 4 + biglen - 4, crc);
  227. PUT_32BIT(pkt->data + pktoffs, len);
  228. if (s->cipher)
  229. s->cipher->encrypt(s->cipher_ctx, pkt->data + pktoffs + 4, biglen);
  230. bufchain_add(s->bpp.out_raw, pkt->data + pktoffs,
  231. biglen + 4); /* len(length+padding+type+data+CRC) */
  232. ssh_free_pktout(pkt);
  233. }
  234. #ifdef MPEXT
  235. int ssh1_bpp_get_compressing(BinaryPacketProtocol *bpp)
  236. {
  237. return FROMFIELD(bpp, struct ssh1_bpp_state, bpp)->compctx != NULL;
  238. }
  239. const struct ssh_cipher * ssh1_bpp_get_cipher(BinaryPacketProtocol *bpp)
  240. {
  241. return FROMFIELD(bpp, struct ssh1_bpp_state, bpp)->cipher;
  242. }
  243. #endif