ssh1bpp.c 13 KB

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