ssh1bpp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. } while (0)
  105. static void ssh1_bpp_handle_input(BinaryPacketProtocol *bpp)
  106. {
  107. struct ssh1_bpp_state *s = container_of(bpp, struct ssh1_bpp_state, bpp);
  108. crBegin(s->crState);
  109. while (1) {
  110. s->maxlen = 0;
  111. s->length = 0;
  112. {
  113. unsigned char lenbuf[4];
  114. BPP_READ(lenbuf, 4);
  115. s->len = toint(GET_32BIT_MSB_FIRST(lenbuf));
  116. }
  117. if (s->len < 0 || s->len > 262144) { /* SSH1.5-mandated max size */
  118. ssh_sw_abort(s->bpp.ssh,
  119. "Extremely large packet length from remote suggests"
  120. " data stream corruption");
  121. crStopV;
  122. }
  123. s->pad = 8 - (s->len % 8);
  124. s->biglen = s->len + s->pad;
  125. s->length = s->len - 5;
  126. /*
  127. * Allocate the packet to return, now we know its length.
  128. */
  129. s->pktin = snew_plus(PktIn, s->biglen);
  130. s->pktin->qnode.prev = s->pktin->qnode.next = NULL;
  131. s->pktin->qnode.on_free_queue = false;
  132. s->pktin->type = 0;
  133. s->maxlen = s->biglen;
  134. s->data = snew_plus_get_aux(s->pktin);
  135. BPP_READ(s->data, s->biglen);
  136. if (s->cipher_in && detect_attack(s->crcda_ctx,
  137. s->data, s->biglen, s->iv)) {
  138. ssh_sw_abort(s->bpp.ssh,
  139. "Network attack (CRC compensation) detected!");
  140. crStopV;
  141. }
  142. /* Save the last cipher block, to be passed to the next call
  143. * to detect_attack */
  144. assert(s->biglen >= 8);
  145. memcpy(s->iv, s->data + s->biglen - 8, sizeof(s->iv));
  146. if (s->cipher_in)
  147. ssh_cipher_decrypt(s->cipher_in, s->data, s->biglen);
  148. s->realcrc = crc32_ssh1(make_ptrlen(s->data, s->biglen - 4));
  149. s->gotcrc = GET_32BIT(s->data + s->biglen - 4);
  150. if (s->gotcrc != s->realcrc) {
  151. ssh_sw_abort(s->bpp.ssh, "Incorrect CRC received on packet");
  152. crStopV;
  153. }
  154. if (s->decompctx) {
  155. unsigned char *decompblk;
  156. int decomplen;
  157. if (!ssh_decompressor_decompress(
  158. s->decompctx, s->data + s->pad, s->length + 1,
  159. &decompblk, &decomplen)) {
  160. ssh_sw_abort(s->bpp.ssh,
  161. "Zlib decompression encountered invalid data");
  162. crStopV;
  163. }
  164. if (s->maxlen < s->pad + decomplen) {
  165. PktIn *old_pktin = s->pktin;
  166. s->maxlen = s->pad + decomplen;
  167. s->pktin = snew_plus(PktIn, s->maxlen);
  168. *s->pktin = *old_pktin; /* structure copy */
  169. s->data = snew_plus_get_aux(s->pktin);
  170. smemclr(old_pktin, s->biglen);
  171. sfree(old_pktin);
  172. }
  173. memcpy(s->data + s->pad, decompblk, decomplen);
  174. sfree(decompblk);
  175. s->length = decomplen - 1;
  176. }
  177. /*
  178. * Now we can find the bounds of the semantic content of the
  179. * packet, and the initial type byte.
  180. */
  181. s->data += s->pad;
  182. s->pktin->type = *s->data++;
  183. BinarySource_INIT(s->pktin, s->data, s->length);
  184. if (s->bpp.logctx) {
  185. logblank_t blanks[MAX_BLANKS];
  186. int nblanks = ssh1_censor_packet(
  187. s->bpp.pls, s->pktin->type, false,
  188. make_ptrlen(s->data, s->length), blanks);
  189. log_packet(s->bpp.logctx, PKT_INCOMING, s->pktin->type,
  190. ssh1_pkt_type(s->pktin->type),
  191. get_ptr(s->pktin), get_avail(s->pktin), nblanks, blanks,
  192. NULL, 0, NULL);
  193. }
  194. pq_push(&s->bpp.in_pq, s->pktin);
  195. {
  196. int type = s->pktin->type;
  197. s->pktin = NULL;
  198. switch (type) {
  199. case SSH1_SMSG_SUCCESS:
  200. case SSH1_SMSG_FAILURE:
  201. if (s->pending_compression_request) {
  202. /*
  203. * This is the response to
  204. * SSH1_CMSG_REQUEST_COMPRESSION.
  205. */
  206. if (type == SSH1_SMSG_SUCCESS) {
  207. /*
  208. * If the response was positive, start
  209. * compression.
  210. */
  211. ssh1_bpp_start_compression(&s->bpp);
  212. }
  213. /*
  214. * Either way, cancel the pending flag, and
  215. * schedule a run of our output side in case we
  216. * had any packets queued up in the meantime.
  217. */
  218. s->pending_compression_request = false;
  219. queue_idempotent_callback(&s->bpp.ic_out_pq);
  220. }
  221. break;
  222. }
  223. }
  224. }
  225. eof:
  226. if (!s->bpp.expect_close) {
  227. ssh_remote_error(s->bpp.ssh,
  228. "Remote side unexpectedly closed network connection");
  229. } else {
  230. ssh_remote_eof(s->bpp.ssh, "Remote side closed network connection");
  231. }
  232. return; /* avoid touching s now it's been freed */
  233. crFinishV;
  234. }
  235. static PktOut *ssh1_bpp_new_pktout(int pkt_type)
  236. {
  237. PktOut *pkt = ssh_new_packet();
  238. pkt->length = 4 + 8; /* space for length + max padding */
  239. put_byte(pkt, pkt_type);
  240. pkt->prefix = pkt->length;
  241. pkt->type = pkt_type;
  242. pkt->downstream_id = 0;
  243. pkt->additional_log_text = NULL;
  244. return pkt;
  245. }
  246. static void ssh1_bpp_format_packet(struct ssh1_bpp_state *s, PktOut *pkt)
  247. {
  248. int pad, biglen, pktoffs;
  249. uint32_t crc;
  250. int len;
  251. if (s->bpp.logctx) {
  252. ptrlen pktdata = make_ptrlen(pkt->data + pkt->prefix,
  253. pkt->length - pkt->prefix);
  254. logblank_t blanks[MAX_BLANKS];
  255. int nblanks = ssh1_censor_packet(
  256. s->bpp.pls, pkt->type, true, pktdata, blanks);
  257. log_packet(s->bpp.logctx, PKT_OUTGOING, pkt->type,
  258. ssh1_pkt_type(pkt->type),
  259. pktdata.ptr, pktdata.len, nblanks, blanks,
  260. NULL, 0, NULL);
  261. }
  262. if (s->compctx) {
  263. unsigned char *compblk;
  264. int complen;
  265. ssh_compressor_compress(s->compctx, pkt->data + 12, pkt->length - 12,
  266. &compblk, &complen, 0);
  267. /* Replace the uncompressed packet data with the compressed
  268. * version. */
  269. pkt->length = 12;
  270. put_data(pkt, compblk, complen);
  271. sfree(compblk);
  272. }
  273. put_uint32(pkt, 0); /* space for CRC */
  274. len = pkt->length - 4 - 8; /* len(type+data+CRC) */
  275. pad = 8 - (len % 8);
  276. pktoffs = 8 - pad;
  277. biglen = len + pad; /* len(padding+type+data+CRC) */
  278. random_read(pkt->data + pktoffs, 4+8 - pktoffs);
  279. crc = crc32_ssh1(
  280. make_ptrlen(pkt->data + pktoffs + 4, biglen - 4)); /* all ex len */
  281. PUT_32BIT(pkt->data + pktoffs + 4 + biglen - 4, crc);
  282. PUT_32BIT(pkt->data + pktoffs, len);
  283. if (s->cipher_out)
  284. ssh_cipher_encrypt(s->cipher_out, pkt->data + pktoffs + 4, biglen);
  285. bufchain_add(s->bpp.out_raw, pkt->data + pktoffs,
  286. biglen + 4); /* len(length+padding+type+data+CRC) */
  287. }
  288. static void ssh1_bpp_handle_output(BinaryPacketProtocol *bpp)
  289. {
  290. struct ssh1_bpp_state *s = container_of(bpp, struct ssh1_bpp_state, bpp);
  291. PktOut *pkt;
  292. if (s->pending_compression_request) {
  293. /*
  294. * Don't send any output packets while we're awaiting a
  295. * response to SSH1_CMSG_REQUEST_COMPRESSION, because if they
  296. * cross over in transit with the responding SSH1_CMSG_SUCCESS
  297. * then the other end could decode them with the wrong
  298. * compression settings.
  299. */
  300. return;
  301. }
  302. while ((pkt = pq_pop(&s->bpp.out_pq)) != NULL) {
  303. int type = pkt->type;
  304. ssh1_bpp_format_packet(s, pkt);
  305. ssh_free_pktout(pkt);
  306. if (type == SSH1_CMSG_REQUEST_COMPRESSION) {
  307. /*
  308. * When we see the actual compression request go past, set
  309. * the pending flag, and stop processing packets this
  310. * time.
  311. */
  312. s->pending_compression_request = true;
  313. break;
  314. }
  315. }
  316. }
  317. static void ssh1_bpp_queue_disconnect(BinaryPacketProtocol *bpp,
  318. const char *msg, int category)
  319. {
  320. PktOut *pkt = ssh_bpp_new_pktout(bpp, SSH1_MSG_DISCONNECT);
  321. put_stringz(pkt, msg);
  322. pq_push(&bpp->out_pq, pkt);
  323. }