ssh1bpp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. int pending_compression_request;
  19. ssh_compressor *compctx;
  20. ssh_decompressor *decompctx;
  21. BinaryPacketProtocol bpp;
  22. };
  23. static void ssh1_bpp_free(BinaryPacketProtocol *bpp);
  24. static void ssh1_bpp_handle_input(BinaryPacketProtocol *bpp);
  25. static void ssh1_bpp_handle_output(BinaryPacketProtocol *bpp);
  26. static void ssh1_bpp_queue_disconnect(BinaryPacketProtocol *bpp,
  27. const char *msg, int category);
  28. static PktOut *ssh1_bpp_new_pktout(int type);
  29. static const struct BinaryPacketProtocolVtable ssh1_bpp_vtable = {
  30. ssh1_bpp_free,
  31. ssh1_bpp_handle_input,
  32. ssh1_bpp_handle_output,
  33. ssh1_bpp_new_pktout,
  34. ssh1_bpp_queue_disconnect,
  35. };
  36. BinaryPacketProtocol *ssh1_bpp_new(Frontend *frontend)
  37. {
  38. struct ssh1_bpp_state *s = snew(struct ssh1_bpp_state);
  39. memset(s, 0, sizeof(*s));
  40. s->bpp.vt = &ssh1_bpp_vtable;
  41. s->bpp.frontend = frontend;
  42. ssh_bpp_common_setup(&s->bpp);
  43. return &s->bpp;
  44. }
  45. static void ssh1_bpp_free(BinaryPacketProtocol *bpp)
  46. {
  47. struct ssh1_bpp_state *s = FROMFIELD(bpp, struct ssh1_bpp_state, bpp);
  48. if (s->cipher)
  49. ssh1_cipher_free(s->cipher);
  50. if (s->compctx)
  51. ssh_compressor_free(s->compctx);
  52. if (s->decompctx)
  53. ssh_decompressor_free(s->decompctx);
  54. if (s->crcda_ctx)
  55. crcda_free_context(s->crcda_ctx);
  56. sfree(s->pktin);
  57. sfree(s);
  58. }
  59. void ssh1_bpp_new_cipher(BinaryPacketProtocol *bpp,
  60. const struct ssh1_cipheralg *cipher,
  61. const void *session_key)
  62. {
  63. struct ssh1_bpp_state *s;
  64. assert(bpp->vt == &ssh1_bpp_vtable);
  65. s = FROMFIELD(bpp, struct ssh1_bpp_state, bpp);
  66. assert(!s->cipher);
  67. if (cipher) {
  68. s->cipher = ssh1_cipher_new(cipher);
  69. ssh1_cipher_sesskey(s->cipher, session_key);
  70. assert(!s->crcda_ctx);
  71. s->crcda_ctx = crcda_make_context();
  72. }
  73. }
  74. #define BPP_READ(ptr, len) do \
  75. { \
  76. crMaybeWaitUntilV(bufchain_try_fetch_consume( \
  77. s->bpp.in_raw, ptr, len)); \
  78. } while (0)
  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. BPP_READ(lenbuf, 4);
  89. s->len = toint(GET_32BIT_MSB_FIRST(lenbuf));
  90. }
  91. if (s->len < 0 || s->len > 262144) { /* SSH1.5-mandated max size */
  92. s->bpp.error = dupprintf(
  93. "Extremely large packet length from server suggests"
  94. " data stream corruption");
  95. crStopV;
  96. }
  97. s->pad = 8 - (s->len % 8);
  98. s->biglen = s->len + s->pad;
  99. s->length = s->len - 5;
  100. /*
  101. * Allocate the packet to return, now we know its length.
  102. */
  103. s->pktin = snew_plus(PktIn, s->biglen);
  104. s->pktin->qnode.prev = s->pktin->qnode.next = NULL;
  105. s->pktin->qnode.on_free_queue = FALSE;
  106. s->pktin->type = 0;
  107. s->maxlen = s->biglen;
  108. s->data = snew_plus_get_aux(s->pktin);
  109. BPP_READ(s->data, s->biglen);
  110. if (s->cipher && detect_attack(s->crcda_ctx,
  111. s->data, s->biglen, NULL)) {
  112. s->bpp.error = dupprintf(
  113. "Network attack (CRC compensation) detected!");
  114. crStopV;
  115. }
  116. if (s->cipher)
  117. ssh1_cipher_decrypt(s->cipher, s->data, s->biglen);
  118. s->realcrc = crc32_compute(s->data, s->biglen - 4);
  119. s->gotcrc = GET_32BIT(s->data + s->biglen - 4);
  120. if (s->gotcrc != s->realcrc) {
  121. s->bpp.error = dupprintf(
  122. "Incorrect CRC received on packet");
  123. crStopV;
  124. }
  125. if (s->decompctx) {
  126. unsigned char *decompblk;
  127. int decomplen;
  128. if (!ssh_decompressor_decompress(
  129. s->decompctx, s->data + s->pad, s->length + 1,
  130. &decompblk, &decomplen)) {
  131. s->bpp.error = dupprintf(
  132. "Zlib decompression encountered invalid data");
  133. crStopV;
  134. }
  135. if (s->maxlen < s->pad + decomplen) {
  136. PktIn *old_pktin = s->pktin;
  137. s->maxlen = s->pad + decomplen;
  138. s->pktin = snew_plus(PktIn, s->maxlen);
  139. *s->pktin = *old_pktin; /* structure copy */
  140. s->data = snew_plus_get_aux(s->pktin);
  141. smemclr(old_pktin, s->biglen);
  142. sfree(old_pktin);
  143. }
  144. memcpy(s->data + s->pad, decompblk, decomplen);
  145. sfree(decompblk);
  146. s->length = decomplen - 1;
  147. }
  148. /*
  149. * Now we can find the bounds of the semantic content of the
  150. * packet, and the initial type byte.
  151. */
  152. s->data += s->pad;
  153. s->pktin->type = *s->data++;
  154. BinarySource_INIT(s->pktin, s->data, s->length);
  155. if (s->bpp.logctx) {
  156. logblank_t blanks[MAX_BLANKS];
  157. int nblanks = ssh1_censor_packet(
  158. s->bpp.pls, s->pktin->type, FALSE,
  159. make_ptrlen(s->data, s->length), blanks);
  160. log_packet(s->bpp.logctx, PKT_INCOMING, s->pktin->type,
  161. ssh1_pkt_type(s->pktin->type),
  162. get_ptr(s->pktin), get_avail(s->pktin), nblanks, blanks,
  163. NULL, 0, NULL);
  164. }
  165. pq_push(&s->bpp.in_pq, s->pktin);
  166. {
  167. int type = s->pktin->type;
  168. s->pktin = NULL;
  169. switch (type) {
  170. case SSH1_MSG_DISCONNECT:
  171. s->bpp.seen_disconnect = TRUE;
  172. break;
  173. case SSH1_SMSG_SUCCESS:
  174. case SSH1_SMSG_FAILURE:
  175. if (s->pending_compression_request) {
  176. /*
  177. * This is the response to
  178. * SSH1_CMSG_REQUEST_COMPRESSION.
  179. */
  180. if (type == SSH1_SMSG_SUCCESS) {
  181. /*
  182. * If the response was positive, start
  183. * compression.
  184. */
  185. assert(!s->compctx);
  186. assert(!s->decompctx);
  187. s->compctx = ssh_compressor_new(&ssh_zlib);
  188. s->decompctx = ssh_decompressor_new(&ssh_zlib);
  189. }
  190. /*
  191. * Either way, cancel the pending flag, and
  192. * schedule a run of our output side in case we
  193. * had any packets queued up in the meantime.
  194. */
  195. s->pending_compression_request = FALSE;
  196. queue_idempotent_callback(&s->bpp.ic_out_pq);
  197. }
  198. break;
  199. }
  200. }
  201. }
  202. crFinishV;
  203. }
  204. static PktOut *ssh1_bpp_new_pktout(int pkt_type)
  205. {
  206. PktOut *pkt = ssh_new_packet();
  207. pkt->length = 4 + 8; /* space for length + max padding */
  208. put_byte(pkt, pkt_type);
  209. pkt->prefix = pkt->length;
  210. pkt->type = pkt_type;
  211. pkt->downstream_id = 0;
  212. pkt->additional_log_text = NULL;
  213. return pkt;
  214. }
  215. static void ssh1_bpp_format_packet(struct ssh1_bpp_state *s, PktOut *pkt)
  216. {
  217. int pad, biglen, i, pktoffs;
  218. unsigned long crc;
  219. int len;
  220. if (s->bpp.logctx) {
  221. ptrlen pktdata = make_ptrlen(pkt->data + pkt->prefix,
  222. pkt->length - pkt->prefix);
  223. logblank_t blanks[MAX_BLANKS];
  224. int nblanks = ssh1_censor_packet(
  225. s->bpp.pls, pkt->type, TRUE, pktdata, blanks);
  226. log_packet(s->bpp.logctx, PKT_OUTGOING, pkt->type,
  227. ssh1_pkt_type(pkt->type),
  228. pktdata.ptr, pktdata.len, nblanks, blanks,
  229. NULL, 0, NULL);
  230. }
  231. if (s->compctx) {
  232. unsigned char *compblk;
  233. int complen;
  234. ssh_compressor_compress(s->compctx, pkt->data + 12, pkt->length - 12,
  235. &compblk, &complen, 0);
  236. /* Replace the uncompressed packet data with the compressed
  237. * version. */
  238. pkt->length = 12;
  239. put_data(pkt, compblk, complen);
  240. sfree(compblk);
  241. }
  242. put_uint32(pkt, 0); /* space for CRC */
  243. len = pkt->length - 4 - 8; /* len(type+data+CRC) */
  244. pad = 8 - (len % 8);
  245. pktoffs = 8 - pad;
  246. biglen = len + pad; /* len(padding+type+data+CRC) */
  247. for (i = pktoffs; i < 4+8; i++)
  248. pkt->data[i] = random_byte();
  249. crc = crc32_compute(pkt->data + pktoffs + 4,
  250. biglen - 4); /* all ex len */
  251. PUT_32BIT(pkt->data + pktoffs + 4 + biglen - 4, crc);
  252. PUT_32BIT(pkt->data + pktoffs, len);
  253. if (s->cipher)
  254. ssh1_cipher_encrypt(s->cipher, pkt->data + pktoffs + 4, biglen);
  255. bufchain_add(s->bpp.out_raw, pkt->data + pktoffs,
  256. biglen + 4); /* len(length+padding+type+data+CRC) */
  257. }
  258. static void ssh1_bpp_handle_output(BinaryPacketProtocol *bpp)
  259. {
  260. struct ssh1_bpp_state *s = FROMFIELD(bpp, struct ssh1_bpp_state, bpp);
  261. PktOut *pkt;
  262. if (s->pending_compression_request) {
  263. /*
  264. * Don't send any output packets while we're awaiting a
  265. * response to SSH1_CMSG_REQUEST_COMPRESSION, because if they
  266. * cross over in transit with the responding SSH1_CMSG_SUCCESS
  267. * then the other end could decode them with the wrong
  268. * compression settings.
  269. */
  270. return;
  271. }
  272. while ((pkt = pq_pop(&s->bpp.out_pq)) != NULL) {
  273. int type = pkt->type;
  274. ssh1_bpp_format_packet(s, pkt);
  275. ssh_free_pktout(pkt);
  276. if (type == SSH1_CMSG_REQUEST_COMPRESSION) {
  277. /*
  278. * When we see the actual compression request go past, set
  279. * the pending flag, and stop processing packets this
  280. * time.
  281. */
  282. s->pending_compression_request = TRUE;
  283. break;
  284. }
  285. }
  286. }
  287. static void ssh1_bpp_queue_disconnect(BinaryPacketProtocol *bpp,
  288. const char *msg, int category)
  289. {
  290. PktOut *pkt = ssh_bpp_new_pktout(bpp, SSH1_MSG_DISCONNECT);
  291. put_stringz(pkt, msg);
  292. pq_push(&bpp->out_pq, pkt);
  293. }
  294. #ifdef MPEXT
  295. const ssh1_cipher * ssh1_bpp_get_cipher(BinaryPacketProtocol *bpp)
  296. {
  297. return FROMFIELD(bpp, struct ssh1_bpp_state, bpp)->cipher;
  298. }
  299. #endif