ssh1bpp.c 13 KB

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