ssh1bpp.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. ssh1_cipher *cipher;
  17. struct crcda_ctx *crcda_ctx;
  18. ssh_compressor *compctx;
  19. ssh_decompressor *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. ssh1_cipher_free(s->cipher);
  44. if (s->compctx)
  45. ssh_compressor_free(s->compctx);
  46. if (s->decompctx)
  47. ssh_decompressor_free(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 ssh1_cipheralg *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. if (cipher) {
  63. s->cipher = ssh1_cipher_new(cipher);
  64. ssh1_cipher_sesskey(s->cipher, session_key);
  65. assert(!s->crcda_ctx);
  66. s->crcda_ctx = crcda_make_context();
  67. }
  68. }
  69. void ssh1_bpp_start_compression(BinaryPacketProtocol *bpp)
  70. {
  71. struct ssh1_bpp_state *s;
  72. assert(bpp->vt == &ssh1_bpp_vtable);
  73. s = FROMFIELD(bpp, struct ssh1_bpp_state, bpp);
  74. assert(!s->compctx);
  75. assert(!s->decompctx);
  76. s->compctx = ssh_compressor_new(&ssh_zlib);
  77. s->decompctx = ssh_decompressor_new(&ssh_zlib);
  78. }
  79. static void ssh1_bpp_handle_input(BinaryPacketProtocol *bpp)
  80. {
  81. struct ssh1_bpp_state *s = FROMFIELD(bpp, struct ssh1_bpp_state, bpp);
  82. crBegin(s->crState);
  83. while (1) {
  84. s->maxlen = 0;
  85. s->length = 0;
  86. {
  87. unsigned char lenbuf[4];
  88. crMaybeWaitUntilV(bufchain_try_fetch_consume(
  89. bpp->in_raw, lenbuf, 4));
  90. s->len = toint(GET_32BIT_MSB_FIRST(lenbuf));
  91. }
  92. if (s->len < 0 || s->len > 262144) { /* SSH1.5-mandated max size */
  93. s->bpp.error = dupprintf(
  94. "Extremely large packet length from server suggests"
  95. " data stream corruption");
  96. crStopV;
  97. }
  98. s->pad = 8 - (s->len % 8);
  99. s->biglen = s->len + s->pad;
  100. s->length = s->len - 5;
  101. /*
  102. * Allocate the packet to return, now we know its length.
  103. */
  104. s->pktin = snew_plus(PktIn, s->biglen);
  105. s->pktin->qnode.prev = s->pktin->qnode.next = NULL;
  106. s->pktin->refcount = 1;
  107. s->pktin->type = 0;
  108. s->maxlen = s->biglen;
  109. s->data = snew_plus_get_aux(s->pktin);
  110. crMaybeWaitUntilV(bufchain_try_fetch_consume(
  111. bpp->in_raw, s->data, s->biglen));
  112. if (s->cipher && detect_attack(s->crcda_ctx,
  113. s->data, s->biglen, NULL)) {
  114. s->bpp.error = dupprintf(
  115. "Network attack (CRC compensation) detected!");
  116. crStopV;
  117. }
  118. if (s->cipher)
  119. ssh1_cipher_decrypt(s->cipher, s->data, s->biglen);
  120. s->realcrc = crc32_compute(s->data, s->biglen - 4);
  121. s->gotcrc = GET_32BIT(s->data + s->biglen - 4);
  122. if (s->gotcrc != s->realcrc) {
  123. s->bpp.error = dupprintf(
  124. "Incorrect CRC received on packet");
  125. crStopV;
  126. }
  127. if (s->decompctx) {
  128. unsigned char *decompblk;
  129. int decomplen;
  130. if (!ssh_decompressor_decompress(
  131. s->decompctx, s->data + s->pad, s->length + 1,
  132. &decompblk, &decomplen)) {
  133. s->bpp.error = dupprintf(
  134. "Zlib decompression encountered invalid data");
  135. crStopV;
  136. }
  137. if (s->maxlen < s->pad + decomplen) {
  138. PktIn *old_pktin = s->pktin;
  139. s->maxlen = s->pad + decomplen;
  140. s->pktin = snew_plus(PktIn, s->maxlen);
  141. *s->pktin = *old_pktin; /* structure copy */
  142. s->data = snew_plus_get_aux(s->pktin);
  143. smemclr(old_pktin, s->biglen);
  144. sfree(old_pktin);
  145. }
  146. memcpy(s->data + s->pad, decompblk, decomplen);
  147. sfree(decompblk);
  148. s->length = decomplen - 1;
  149. }
  150. /*
  151. * Now we can find the bounds of the semantic content of the
  152. * packet, and the initial type byte.
  153. */
  154. s->data += s->pad;
  155. s->pktin->type = *s->data++;
  156. BinarySource_INIT(s->pktin, s->data, s->length);
  157. if (s->bpp.logctx) {
  158. logblank_t blanks[MAX_BLANKS];
  159. int nblanks = ssh1_censor_packet(
  160. s->bpp.pls, s->pktin->type, FALSE,
  161. make_ptrlen(s->data, s->length), blanks);
  162. log_packet(s->bpp.logctx, PKT_INCOMING, s->pktin->type,
  163. ssh1_pkt_type(s->pktin->type),
  164. get_ptr(s->pktin), get_avail(s->pktin), nblanks, blanks,
  165. NULL, 0, NULL);
  166. }
  167. pq_push(s->bpp.in_pq, s->pktin);
  168. {
  169. int type = s->pktin->type;
  170. s->pktin = NULL;
  171. if (type == SSH1_MSG_DISCONNECT)
  172. s->bpp.seen_disconnect = TRUE;
  173. }
  174. }
  175. crFinishV;
  176. }
  177. static PktOut *ssh1_bpp_new_pktout(int pkt_type)
  178. {
  179. PktOut *pkt = ssh_new_packet();
  180. pkt->length = 4 + 8; /* space for length + max padding */
  181. put_byte(pkt, pkt_type);
  182. pkt->prefix = pkt->length;
  183. pkt->type = pkt_type;
  184. pkt->downstream_id = 0;
  185. pkt->additional_log_text = NULL;
  186. return pkt;
  187. }
  188. static void ssh1_bpp_format_packet(BinaryPacketProtocol *bpp, PktOut *pkt)
  189. {
  190. struct ssh1_bpp_state *s = FROMFIELD(bpp, struct ssh1_bpp_state, bpp);
  191. int pad, biglen, i, pktoffs;
  192. unsigned long crc;
  193. int len;
  194. if (s->bpp.logctx) {
  195. ptrlen pktdata = make_ptrlen(pkt->data + pkt->prefix,
  196. pkt->length - pkt->prefix);
  197. logblank_t blanks[MAX_BLANKS];
  198. int nblanks = ssh1_censor_packet(
  199. s->bpp.pls, pkt->type, TRUE, pktdata, blanks);
  200. log_packet(s->bpp.logctx, PKT_OUTGOING, pkt->type,
  201. ssh1_pkt_type(pkt->type),
  202. pktdata.ptr, pktdata.len, nblanks, blanks,
  203. NULL, 0, NULL);
  204. }
  205. if (s->compctx) {
  206. unsigned char *compblk;
  207. int complen;
  208. ssh_compressor_compress(s->compctx, pkt->data + 12, pkt->length - 12,
  209. &compblk, &complen, 0);
  210. /* Replace the uncompressed packet data with the compressed
  211. * version. */
  212. pkt->length = 12;
  213. put_data(pkt, compblk, complen);
  214. sfree(compblk);
  215. }
  216. put_uint32(pkt, 0); /* space for CRC */
  217. len = pkt->length - 4 - 8; /* len(type+data+CRC) */
  218. pad = 8 - (len % 8);
  219. pktoffs = 8 - pad;
  220. biglen = len + pad; /* len(padding+type+data+CRC) */
  221. for (i = pktoffs; i < 4+8; i++)
  222. pkt->data[i] = random_byte();
  223. crc = crc32_compute(pkt->data + pktoffs + 4,
  224. biglen - 4); /* all ex len */
  225. PUT_32BIT(pkt->data + pktoffs + 4 + biglen - 4, crc);
  226. PUT_32BIT(pkt->data + pktoffs, len);
  227. if (s->cipher)
  228. ssh1_cipher_encrypt(s->cipher, pkt->data + pktoffs + 4, biglen);
  229. bufchain_add(s->bpp.out_raw, pkt->data + pktoffs,
  230. biglen + 4); /* len(length+padding+type+data+CRC) */
  231. ssh_free_pktout(pkt);
  232. }
  233. #ifdef MPEXT
  234. const ssh1_cipher * ssh1_bpp_get_cipher(BinaryPacketProtocol *bpp)
  235. {
  236. return FROMFIELD(bpp, struct ssh1_bpp_state, bpp)->cipher;
  237. }
  238. #endif