ssh2bpp.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. /*
  2. * Binary packet protocol for SSH-2.
  3. */
  4. #include <assert.h>
  5. #include "putty.h"
  6. #include "ssh.h"
  7. #include "sshbpp.h"
  8. #include "sshcr.h"
  9. struct ssh2_bpp_direction {
  10. unsigned long sequence;
  11. ssh2_cipher *cipher;
  12. ssh2_mac *mac;
  13. int etm_mode;
  14. const struct ssh_compression_alg *pending_compression;
  15. };
  16. struct ssh2_bpp_state {
  17. int crState;
  18. long len, pad, payload, packetlen, maclen, length, maxlen;
  19. unsigned char *buf;
  20. size_t bufsize;
  21. unsigned char *data;
  22. unsigned cipherblk;
  23. PktIn *pktin;
  24. struct DataTransferStats *stats;
  25. int cbc_ignore_workaround;
  26. struct ssh2_bpp_direction in, out;
  27. /* comp and decomp logically belong in the per-direction
  28. * substructure, except that they have different types */
  29. ssh_decompressor *in_decomp;
  30. ssh_compressor *out_comp;
  31. int pending_newkeys;
  32. int pending_compression, seen_userauth_success;
  33. BinaryPacketProtocol bpp;
  34. };
  35. static void ssh2_bpp_free(BinaryPacketProtocol *bpp);
  36. static void ssh2_bpp_handle_input(BinaryPacketProtocol *bpp);
  37. static void ssh2_bpp_handle_output(BinaryPacketProtocol *bpp);
  38. static PktOut *ssh2_bpp_new_pktout(int type);
  39. static const struct BinaryPacketProtocolVtable ssh2_bpp_vtable = {
  40. ssh2_bpp_free,
  41. ssh2_bpp_handle_input,
  42. ssh2_bpp_handle_output,
  43. ssh2_bpp_new_pktout,
  44. ssh2_bpp_queue_disconnect, /* in sshcommon.c */
  45. };
  46. BinaryPacketProtocol *ssh2_bpp_new(
  47. Frontend *frontend, struct DataTransferStats *stats)
  48. {
  49. struct ssh2_bpp_state *s = snew(struct ssh2_bpp_state);
  50. memset(s, 0, sizeof(*s));
  51. s->bpp.vt = &ssh2_bpp_vtable;
  52. s->bpp.frontend = frontend;
  53. s->stats = stats;
  54. ssh_bpp_common_setup(&s->bpp);
  55. return &s->bpp;
  56. }
  57. static void ssh2_bpp_free(BinaryPacketProtocol *bpp)
  58. {
  59. struct ssh2_bpp_state *s = container_of(bpp, struct ssh2_bpp_state, bpp);
  60. sfree(s->buf);
  61. if (s->out.cipher)
  62. ssh2_cipher_free(s->out.cipher);
  63. if (s->out.mac)
  64. ssh2_mac_free(s->out.mac);
  65. if (s->out_comp)
  66. ssh_compressor_free(s->out_comp);
  67. if (s->in.cipher)
  68. ssh2_cipher_free(s->in.cipher);
  69. if (s->in.mac)
  70. ssh2_mac_free(s->in.mac);
  71. if (s->in_decomp)
  72. ssh_decompressor_free(s->in_decomp);
  73. sfree(s->pktin);
  74. sfree(s);
  75. }
  76. #define bpp_logevent(printf_args) \
  77. logevent_and_free(s->bpp.frontend, dupprintf printf_args)
  78. void ssh2_bpp_new_outgoing_crypto(
  79. BinaryPacketProtocol *bpp,
  80. const struct ssh2_cipheralg *cipher, const void *ckey, const void *iv,
  81. const struct ssh2_macalg *mac, int etm_mode, const void *mac_key,
  82. const struct ssh_compression_alg *compression, int delayed_compression)
  83. {
  84. struct ssh2_bpp_state *s;
  85. assert(bpp->vt == &ssh2_bpp_vtable);
  86. s = container_of(bpp, struct ssh2_bpp_state, bpp);
  87. if (s->out.cipher)
  88. ssh2_cipher_free(s->out.cipher);
  89. if (s->out.mac)
  90. ssh2_mac_free(s->out.mac);
  91. if (s->out_comp)
  92. ssh_compressor_free(s->out_comp);
  93. if (cipher) {
  94. s->out.cipher = ssh2_cipher_new(cipher);
  95. ssh2_cipher_setkey(s->out.cipher, ckey);
  96. ssh2_cipher_setiv(s->out.cipher, iv);
  97. s->cbc_ignore_workaround = (
  98. (ssh2_cipher_alg(s->out.cipher)->flags & SSH_CIPHER_IS_CBC) &&
  99. !(s->bpp.remote_bugs & BUG_CHOKES_ON_SSH2_IGNORE));
  100. bpp_logevent(("Initialised %.200s client->server encryption",
  101. ssh2_cipher_alg(s->out.cipher)->text_name));
  102. } else {
  103. s->out.cipher = NULL;
  104. s->cbc_ignore_workaround = FALSE;
  105. }
  106. s->out.etm_mode = etm_mode;
  107. if (mac) {
  108. s->out.mac = ssh2_mac_new(mac, s->out.cipher);
  109. mac->setkey(s->out.mac, mac_key);
  110. bpp_logevent(("Initialised %.200s client->server"
  111. " MAC algorithm%s%s",
  112. ssh2_mac_alg(s->out.mac)->text_name,
  113. etm_mode ? " (in ETM mode)" : "",
  114. (s->out.cipher &&
  115. ssh2_cipher_alg(s->out.cipher)->required_mac ?
  116. " (required by cipher)" : "")));
  117. } else {
  118. s->out.mac = NULL;
  119. }
  120. if (delayed_compression && !s->seen_userauth_success) {
  121. s->out.pending_compression = compression;
  122. s->out_comp = NULL;
  123. bpp_logevent(("Will enable %s compression after user authentication",
  124. s->out.pending_compression->text_name));
  125. } else {
  126. s->out.pending_compression = NULL;
  127. /* 'compression' is always non-NULL, because no compression is
  128. * indicated by ssh_comp_none. But this setup call may return a
  129. * null out_comp. */
  130. s->out_comp = ssh_compressor_new(compression);
  131. if (s->out_comp)
  132. bpp_logevent(("Initialised %s compression",
  133. ssh_compressor_alg(s->out_comp)->text_name));
  134. }
  135. }
  136. void ssh2_bpp_new_incoming_crypto(
  137. BinaryPacketProtocol *bpp,
  138. const struct ssh2_cipheralg *cipher, const void *ckey, const void *iv,
  139. const struct ssh2_macalg *mac, int etm_mode, const void *mac_key,
  140. const struct ssh_compression_alg *compression, int delayed_compression)
  141. {
  142. struct ssh2_bpp_state *s;
  143. assert(bpp->vt == &ssh2_bpp_vtable);
  144. s = container_of(bpp, struct ssh2_bpp_state, bpp);
  145. if (s->in.cipher)
  146. ssh2_cipher_free(s->in.cipher);
  147. if (s->in.mac)
  148. ssh2_mac_free(s->in.mac);
  149. if (s->in_decomp)
  150. ssh_decompressor_free(s->in_decomp);
  151. if (cipher) {
  152. s->in.cipher = ssh2_cipher_new(cipher);
  153. ssh2_cipher_setkey(s->in.cipher, ckey);
  154. ssh2_cipher_setiv(s->in.cipher, iv);
  155. bpp_logevent(("Initialised %.200s server->client encryption",
  156. ssh2_cipher_alg(s->in.cipher)->text_name));
  157. } else {
  158. s->in.cipher = NULL;
  159. }
  160. s->in.etm_mode = etm_mode;
  161. if (mac) {
  162. s->in.mac = ssh2_mac_new(mac, s->in.cipher);
  163. mac->setkey(s->in.mac, mac_key);
  164. bpp_logevent(("Initialised %.200s server->client MAC algorithm%s%s",
  165. ssh2_mac_alg(s->in.mac)->text_name,
  166. etm_mode ? " (in ETM mode)" : "",
  167. (s->in.cipher &&
  168. ssh2_cipher_alg(s->in.cipher)->required_mac ?
  169. " (required by cipher)" : "")));
  170. } else {
  171. s->in.mac = NULL;
  172. }
  173. if (delayed_compression && !s->seen_userauth_success) {
  174. s->in.pending_compression = compression;
  175. s->in_decomp = NULL;
  176. bpp_logevent(("Will enable %s decompression after user authentication",
  177. s->in.pending_compression->text_name));
  178. } else {
  179. s->in.pending_compression = NULL;
  180. /* 'compression' is always non-NULL, because no compression is
  181. * indicated by ssh_comp_none. But this setup call may return a
  182. * null in_decomp. */
  183. s->in_decomp = ssh_decompressor_new(compression);
  184. if (s->in_decomp)
  185. bpp_logevent(("Initialised %s decompression",
  186. ssh_decompressor_alg(s->in_decomp)->text_name));
  187. }
  188. /* Clear the pending_newkeys flag, so that handle_input below will
  189. * start consuming the input data again. */
  190. s->pending_newkeys = FALSE;
  191. }
  192. int ssh2_bpp_rekey_inadvisable(BinaryPacketProtocol *bpp)
  193. {
  194. struct ssh2_bpp_state *s;
  195. assert(bpp->vt == &ssh2_bpp_vtable);
  196. s = container_of(bpp, struct ssh2_bpp_state, bpp);
  197. return s->pending_compression;
  198. }
  199. #define BPP_READ(ptr, len) do \
  200. { \
  201. crMaybeWaitUntilV(s->bpp.input_eof || \
  202. bufchain_try_fetch_consume( \
  203. s->bpp.in_raw, ptr, len)); \
  204. if (s->bpp.input_eof) \
  205. goto eof; \
  206. } while (0)
  207. #define userauth_range(pkttype) ((unsigned)((pkttype) - 50) < 20)
  208. static void ssh2_bpp_handle_input(BinaryPacketProtocol *bpp)
  209. {
  210. struct ssh2_bpp_state *s = container_of(bpp, struct ssh2_bpp_state, bpp);
  211. crBegin(s->crState);
  212. while (1) {
  213. s->maxlen = 0;
  214. s->length = 0;
  215. if (s->in.cipher)
  216. s->cipherblk = ssh2_cipher_alg(s->in.cipher)->blksize;
  217. else
  218. s->cipherblk = 8;
  219. if (s->cipherblk < 8)
  220. s->cipherblk = 8;
  221. s->maclen = s->in.mac ? ssh2_mac_alg(s->in.mac)->len : 0;
  222. if (s->in.cipher &&
  223. (ssh2_cipher_alg(s->in.cipher)->flags & SSH_CIPHER_IS_CBC) &&
  224. s->in.mac && !s->in.etm_mode) {
  225. /*
  226. * When dealing with a CBC-mode cipher, we want to avoid the
  227. * possibility of an attacker's tweaking the ciphertext stream
  228. * so as to cause us to feed the same block to the block
  229. * cipher more than once and thus leak information
  230. * (VU#958563). The way we do this is not to take any
  231. * decisions on the basis of anything we've decrypted until
  232. * we've verified it with a MAC. That includes the packet
  233. * length, so we just read data and check the MAC repeatedly,
  234. * and when the MAC passes, see if the length we've got is
  235. * plausible.
  236. *
  237. * This defence is unnecessary in OpenSSH ETM mode, because
  238. * the whole point of ETM mode is that the attacker can't
  239. * tweak the ciphertext stream at all without the MAC
  240. * detecting it before we decrypt anything.
  241. */
  242. /*
  243. * Make sure we have buffer space for a maximum-size packet.
  244. */
  245. unsigned buflimit = OUR_V2_PACKETLIMIT + s->maclen;
  246. if (s->bufsize < buflimit) {
  247. s->bufsize = buflimit;
  248. s->buf = sresize(s->buf, s->bufsize, unsigned char);
  249. }
  250. /* Read an amount corresponding to the MAC. */
  251. BPP_READ(s->buf, s->maclen);
  252. s->packetlen = 0;
  253. ssh2_mac_start(s->in.mac);
  254. put_uint32(s->in.mac, s->in.sequence);
  255. for (;;) { /* Once around this loop per cipher block. */
  256. /* Read another cipher-block's worth, and tack it on to
  257. * the end. */
  258. BPP_READ(s->buf + (s->packetlen + s->maclen), s->cipherblk);
  259. /* Decrypt one more block (a little further back in
  260. * the stream). */
  261. ssh2_cipher_decrypt(s->in.cipher,
  262. s->buf + s->packetlen, s->cipherblk);
  263. /* Feed that block to the MAC. */
  264. put_data(s->in.mac,
  265. s->buf + s->packetlen, s->cipherblk);
  266. s->packetlen += s->cipherblk;
  267. /* See if that gives us a valid packet. */
  268. if (ssh2_mac_verresult(s->in.mac, s->buf + s->packetlen) &&
  269. ((s->len = toint(GET_32BIT(s->buf))) ==
  270. s->packetlen-4))
  271. break;
  272. if (s->packetlen >= (long)OUR_V2_PACKETLIMIT) {
  273. ssh_sw_abort(s->bpp.ssh,
  274. "No valid incoming packet found");
  275. crStopV;
  276. }
  277. }
  278. s->maxlen = s->packetlen + s->maclen;
  279. /*
  280. * Now transfer the data into an output packet.
  281. */
  282. s->pktin = snew_plus(PktIn, s->maxlen);
  283. s->pktin->qnode.prev = s->pktin->qnode.next = NULL;
  284. s->pktin->type = 0;
  285. s->pktin->qnode.on_free_queue = FALSE;
  286. s->data = snew_plus_get_aux(s->pktin);
  287. memcpy(s->data, s->buf, s->maxlen);
  288. } else if (s->in.mac && s->in.etm_mode) {
  289. if (s->bufsize < 4) {
  290. s->bufsize = 4;
  291. s->buf = sresize(s->buf, s->bufsize, unsigned char);
  292. }
  293. /*
  294. * OpenSSH encrypt-then-MAC mode: the packet length is
  295. * unencrypted, unless the cipher supports length encryption.
  296. */
  297. BPP_READ(s->buf, 4);
  298. /* Cipher supports length decryption, so do it */
  299. if (s->in.cipher && (ssh2_cipher_alg(s->in.cipher)->flags &
  300. SSH_CIPHER_SEPARATE_LENGTH)) {
  301. /* Keep the packet the same though, so the MAC passes */
  302. unsigned char len[4];
  303. memcpy(len, s->buf, 4);
  304. ssh2_cipher_decrypt_length(
  305. s->in.cipher, len, 4, s->in.sequence);
  306. s->len = toint(GET_32BIT(len));
  307. } else {
  308. s->len = toint(GET_32BIT(s->buf));
  309. }
  310. /*
  311. * _Completely_ silly lengths should be stomped on before they
  312. * do us any more damage.
  313. */
  314. if (s->len < 0 || s->len > (long)OUR_V2_PACKETLIMIT ||
  315. s->len % s->cipherblk != 0) {
  316. ssh_sw_abort(s->bpp.ssh,
  317. "Incoming packet length field was garbled");
  318. crStopV;
  319. }
  320. /*
  321. * So now we can work out the total packet length.
  322. */
  323. s->packetlen = s->len + 4;
  324. /*
  325. * Allocate the packet to return, now we know its length.
  326. */
  327. s->pktin = snew_plus(PktIn, OUR_V2_PACKETLIMIT + s->maclen);
  328. s->pktin->qnode.prev = s->pktin->qnode.next = NULL;
  329. s->pktin->type = 0;
  330. s->pktin->qnode.on_free_queue = FALSE;
  331. s->data = snew_plus_get_aux(s->pktin);
  332. memcpy(s->data, s->buf, 4);
  333. /*
  334. * Read the remainder of the packet.
  335. */
  336. BPP_READ(s->data + 4, s->packetlen + s->maclen - 4);
  337. /*
  338. * Check the MAC.
  339. */
  340. if (s->in.mac && !ssh2_mac_verify(
  341. s->in.mac, s->data, s->len + 4, s->in.sequence)) {
  342. ssh_sw_abort(s->bpp.ssh, "Incorrect MAC received on packet");
  343. crStopV;
  344. }
  345. /* Decrypt everything between the length field and the MAC. */
  346. if (s->in.cipher)
  347. ssh2_cipher_decrypt(
  348. s->in.cipher, s->data + 4, s->packetlen - 4);
  349. } else {
  350. if (s->bufsize < s->cipherblk) {
  351. s->bufsize = s->cipherblk;
  352. s->buf = sresize(s->buf, s->bufsize, unsigned char);
  353. }
  354. /*
  355. * Acquire and decrypt the first block of the packet. This will
  356. * contain the length and padding details.
  357. */
  358. BPP_READ(s->buf, s->cipherblk);
  359. if (s->in.cipher)
  360. ssh2_cipher_decrypt(
  361. s->in.cipher, s->buf, s->cipherblk);
  362. /*
  363. * Now get the length figure.
  364. */
  365. s->len = toint(GET_32BIT(s->buf));
  366. /*
  367. * _Completely_ silly lengths should be stomped on before they
  368. * do us any more damage.
  369. */
  370. if (s->len < 0 || s->len > (long)OUR_V2_PACKETLIMIT ||
  371. (s->len + 4) % s->cipherblk != 0) {
  372. ssh_sw_abort(s->bpp.ssh,
  373. "Incoming packet was garbled on decryption");
  374. crStopV;
  375. }
  376. /*
  377. * So now we can work out the total packet length.
  378. */
  379. s->packetlen = s->len + 4;
  380. /*
  381. * Allocate the packet to return, now we know its length.
  382. */
  383. s->maxlen = s->packetlen + s->maclen;
  384. s->pktin = snew_plus(PktIn, s->maxlen);
  385. s->pktin->qnode.prev = s->pktin->qnode.next = NULL;
  386. s->pktin->type = 0;
  387. s->pktin->qnode.on_free_queue = FALSE;
  388. s->data = snew_plus_get_aux(s->pktin);
  389. memcpy(s->data, s->buf, s->cipherblk);
  390. /*
  391. * Read and decrypt the remainder of the packet.
  392. */
  393. BPP_READ(s->data + s->cipherblk,
  394. s->packetlen + s->maclen - s->cipherblk);
  395. /* Decrypt everything _except_ the MAC. */
  396. if (s->in.cipher)
  397. ssh2_cipher_decrypt(
  398. s->in.cipher,
  399. s->data + s->cipherblk, s->packetlen - s->cipherblk);
  400. /*
  401. * Check the MAC.
  402. */
  403. if (s->in.mac && !ssh2_mac_verify(
  404. s->in.mac, s->data, s->len + 4, s->in.sequence)) {
  405. ssh_sw_abort(s->bpp.ssh, "Incorrect MAC received on packet");
  406. crStopV;
  407. }
  408. }
  409. /* Get and sanity-check the amount of random padding. */
  410. s->pad = s->data[4];
  411. if (s->pad < 4 || s->len - s->pad < 1) {
  412. ssh_sw_abort(s->bpp.ssh,
  413. "Invalid padding length on received packet");
  414. crStopV;
  415. }
  416. /*
  417. * This enables us to deduce the payload length.
  418. */
  419. s->payload = s->len - s->pad - 1;
  420. s->length = s->payload + 5;
  421. DTS_CONSUME(s->stats, in, s->packetlen);
  422. s->pktin->sequence = s->in.sequence++;
  423. s->length = s->packetlen - s->pad;
  424. assert(s->length >= 0);
  425. /*
  426. * Decompress packet payload.
  427. */
  428. {
  429. unsigned char *newpayload;
  430. int newlen;
  431. if (s->in_decomp && ssh_decompressor_decompress(
  432. s->in_decomp, s->data + 5, s->length - 5,
  433. &newpayload, &newlen)) {
  434. if (s->maxlen < newlen + 5) {
  435. PktIn *old_pktin = s->pktin;
  436. s->maxlen = newlen + 5;
  437. s->pktin = snew_plus(PktIn, s->maxlen);
  438. *s->pktin = *old_pktin; /* structure copy */
  439. s->data = snew_plus_get_aux(s->pktin);
  440. smemclr(old_pktin, s->packetlen + s->maclen);
  441. sfree(old_pktin);
  442. }
  443. s->length = 5 + newlen;
  444. memcpy(s->data + 5, newpayload, newlen);
  445. sfree(newpayload);
  446. }
  447. }
  448. /*
  449. * Now we can identify the semantic content of the packet,
  450. * and also the initial type byte.
  451. */
  452. if (s->length <= 5) { /* == 5 we hope, but robustness */
  453. /*
  454. * RFC 4253 doesn't explicitly say that completely empty
  455. * packets with no type byte are forbidden. We handle them
  456. * here by giving them a type code larger than 0xFF, which
  457. * will be picked up at the next layer and trigger
  458. * SSH_MSG_UNIMPLEMENTED.
  459. */
  460. s->pktin->type = SSH_MSG_NO_TYPE_CODE;
  461. s->data += 5;
  462. s->length = 0;
  463. } else {
  464. s->pktin->type = s->data[5];
  465. s->data += 6;
  466. s->length -= 6;
  467. }
  468. BinarySource_INIT(s->pktin, s->data, s->length);
  469. if (s->bpp.logctx) {
  470. logblank_t blanks[MAX_BLANKS];
  471. int nblanks = ssh2_censor_packet(
  472. s->bpp.pls, s->pktin->type, FALSE,
  473. make_ptrlen(s->data, s->length), blanks);
  474. log_packet(s->bpp.logctx, PKT_INCOMING, s->pktin->type,
  475. ssh2_pkt_type(s->bpp.pls->kctx, s->bpp.pls->actx,
  476. s->pktin->type),
  477. s->data, s->length, nblanks, blanks,
  478. &s->pktin->sequence, 0, NULL);
  479. }
  480. if (ssh2_bpp_check_unimplemented(&s->bpp, s->pktin)) {
  481. sfree(s->pktin);
  482. s->pktin = NULL;
  483. continue;
  484. }
  485. pq_push(&s->bpp.in_pq, s->pktin);
  486. {
  487. int type = s->pktin->type;
  488. s->pktin = NULL;
  489. if (type == SSH2_MSG_NEWKEYS) {
  490. /*
  491. * Mild layer violation: in this situation we must
  492. * suspend processing of the input byte stream until
  493. * the transport layer has initialised the new keys by
  494. * calling ssh2_bpp_new_incoming_crypto above.
  495. */
  496. s->pending_newkeys = TRUE;
  497. crWaitUntilV(!s->pending_newkeys);
  498. continue;
  499. }
  500. if (type == SSH2_MSG_USERAUTH_SUCCESS) {
  501. /*
  502. * Another one: if we were configured with OpenSSH's
  503. * deferred compression which is triggered on receipt
  504. * of USERAUTH_SUCCESS, then this is the moment to
  505. * turn on compression.
  506. */
  507. if (s->in.pending_compression) {
  508. s->in_decomp =
  509. ssh_decompressor_new(s->in.pending_compression);
  510. bpp_logevent(("Initialised delayed %s decompression",
  511. ssh_decompressor_alg(
  512. s->in_decomp)->text_name));
  513. s->in.pending_compression = NULL;
  514. }
  515. if (s->out.pending_compression) {
  516. s->out_comp =
  517. ssh_compressor_new(s->out.pending_compression);
  518. bpp_logevent(("Initialised delayed %s compression",
  519. ssh_compressor_alg(
  520. s->out_comp)->text_name));
  521. s->out.pending_compression = NULL;
  522. }
  523. /*
  524. * Whether or not we were doing delayed compression in
  525. * _this_ set of crypto parameters, we should set a
  526. * flag indicating that we're now authenticated, so
  527. * that a delayed compression method enabled in any
  528. * future rekey will be treated as un-delayed.
  529. */
  530. s->seen_userauth_success = TRUE;
  531. }
  532. if (s->pending_compression && userauth_range(type)) {
  533. /*
  534. * Receiving any userauth message at all indicates
  535. * that we're not about to turn on delayed compression
  536. * - either because we just _have_ done, or because
  537. * this message is a USERAUTH_FAILURE or some kind of
  538. * intermediate 'please send more data' continuation
  539. * message. Either way, we turn off the outgoing
  540. * packet blockage for now, and release any queued
  541. * output packets, so that we can make another attempt
  542. * to authenticate. The next userauth packet we send
  543. * will re-block the output direction.
  544. */
  545. s->pending_compression = FALSE;
  546. queue_idempotent_callback(&s->bpp.ic_out_pq);
  547. }
  548. }
  549. }
  550. eof:
  551. if (!s->bpp.expect_close) {
  552. ssh_remote_error(s->bpp.ssh,
  553. "Server unexpectedly closed network connection");
  554. } else {
  555. ssh_remote_eof(s->bpp.ssh, "Server closed network connection");
  556. }
  557. return; /* avoid touching s now it's been freed */
  558. crFinishV;
  559. }
  560. static PktOut *ssh2_bpp_new_pktout(int pkt_type)
  561. {
  562. PktOut *pkt = ssh_new_packet();
  563. pkt->length = 5; /* space for packet length + padding length */
  564. pkt->minlen = 0;
  565. pkt->type = pkt_type;
  566. put_byte(pkt, pkt_type);
  567. pkt->prefix = pkt->length;
  568. return pkt;
  569. }
  570. static void ssh2_bpp_format_packet_inner(struct ssh2_bpp_state *s, PktOut *pkt)
  571. {
  572. int origlen, cipherblk, maclen, padding, unencrypted_prefix, i;
  573. if (s->bpp.logctx) {
  574. ptrlen pktdata = make_ptrlen(pkt->data + pkt->prefix,
  575. pkt->length - pkt->prefix);
  576. logblank_t blanks[MAX_BLANKS];
  577. int nblanks = ssh2_censor_packet(
  578. s->bpp.pls, pkt->type, TRUE, pktdata, blanks);
  579. log_packet(s->bpp.logctx, PKT_OUTGOING, pkt->type,
  580. ssh2_pkt_type(s->bpp.pls->kctx, s->bpp.pls->actx,
  581. pkt->type),
  582. pktdata.ptr, pktdata.len, nblanks, blanks, &s->out.sequence,
  583. pkt->downstream_id, pkt->additional_log_text);
  584. }
  585. cipherblk = s->out.cipher ? ssh2_cipher_alg(s->out.cipher)->blksize : 8;
  586. cipherblk = cipherblk < 8 ? 8 : cipherblk; /* or 8 if blksize < 8 */
  587. if (s->out_comp) {
  588. unsigned char *newpayload;
  589. int minlen, newlen;
  590. /*
  591. * Compress packet payload.
  592. */
  593. minlen = pkt->minlen;
  594. if (minlen) {
  595. /*
  596. * Work out how much compressed data we need (at least) to
  597. * make the overall packet length come to pkt->minlen.
  598. */
  599. if (s->out.mac)
  600. minlen -= ssh2_mac_alg(s->out.mac)->len;
  601. minlen -= 8; /* length field + min padding */
  602. }
  603. ssh_compressor_compress(s->out_comp, pkt->data + 5, pkt->length - 5,
  604. &newpayload, &newlen, minlen);
  605. pkt->length = 5;
  606. put_data(pkt, newpayload, newlen);
  607. sfree(newpayload);
  608. }
  609. /*
  610. * Add padding. At least four bytes, and must also bring total
  611. * length (minus MAC) up to a multiple of the block size.
  612. * If pkt->forcepad is set, make sure the packet is at least that size
  613. * after padding.
  614. */
  615. padding = 4;
  616. unencrypted_prefix = (s->out.mac && s->out.etm_mode) ? 4 : 0;
  617. padding +=
  618. (cipherblk - (pkt->length - unencrypted_prefix + padding) % cipherblk)
  619. % cipherblk;
  620. assert(padding <= 255);
  621. maclen = s->out.mac ? ssh2_mac_alg(s->out.mac)->len : 0;
  622. origlen = pkt->length;
  623. for (i = 0; i < padding; i++)
  624. put_byte(pkt, random_byte());
  625. pkt->data[4] = padding;
  626. PUT_32BIT(pkt->data, origlen + padding - 4);
  627. /* Encrypt length if the scheme requires it */
  628. if (s->out.cipher &&
  629. (ssh2_cipher_alg(s->out.cipher)->flags & SSH_CIPHER_SEPARATE_LENGTH)) {
  630. ssh2_cipher_encrypt_length(s->out.cipher, pkt->data, 4,
  631. s->out.sequence);
  632. }
  633. put_padding(pkt, maclen, 0);
  634. if (s->out.mac && s->out.etm_mode) {
  635. /*
  636. * OpenSSH-defined encrypt-then-MAC protocol.
  637. */
  638. if (s->out.cipher)
  639. ssh2_cipher_encrypt(s->out.cipher,
  640. pkt->data + 4, origlen + padding - 4);
  641. ssh2_mac_generate(s->out.mac, pkt->data, origlen + padding,
  642. s->out.sequence);
  643. } else {
  644. /*
  645. * SSH-2 standard protocol.
  646. */
  647. if (s->out.mac)
  648. ssh2_mac_generate(s->out.mac, pkt->data, origlen + padding,
  649. s->out.sequence);
  650. if (s->out.cipher)
  651. ssh2_cipher_encrypt(s->out.cipher, pkt->data, origlen + padding);
  652. }
  653. s->out.sequence++; /* whether or not we MACed */
  654. DTS_CONSUME(s->stats, out, origlen + padding);
  655. }
  656. static void ssh2_bpp_format_packet(struct ssh2_bpp_state *s, PktOut *pkt)
  657. {
  658. if (pkt->minlen > 0 && !s->out_comp) {
  659. /*
  660. * If we've been told to pad the packet out to a given minimum
  661. * length, but we're not compressing (and hence can't get the
  662. * compression to do the padding by pointlessly opening and
  663. * closing zlib blocks), then our other strategy is to precede
  664. * this message with an SSH_MSG_IGNORE that makes it up to the
  665. * right length.
  666. *
  667. * A third option in principle, and the most obviously
  668. * sensible, would be to set the explicit padding field in the
  669. * packet to more than its minimum value. Sadly, that turns
  670. * out to break some servers (our institutional memory thinks
  671. * Cisco in particular) and so we abandoned that idea shortly
  672. * after trying it.
  673. */
  674. /*
  675. * Calculate the length we expect the real packet to have.
  676. */
  677. int block, length;
  678. PktOut *ignore_pkt;
  679. block = s->out.cipher ? ssh2_cipher_alg(s->out.cipher)->blksize : 0;
  680. if (block < 8)
  681. block = 8;
  682. length = pkt->length;
  683. length += 4; /* minimum 4 byte padding */
  684. length += block-1;
  685. length -= (length % block);
  686. if (s->out.mac)
  687. length += ssh2_mac_alg(s->out.mac)->len;
  688. if (length < pkt->minlen) {
  689. /*
  690. * We need an ignore message. Calculate its length.
  691. */
  692. length = pkt->minlen - length;
  693. /*
  694. * And work backwards from that to the length of the
  695. * contained string.
  696. */
  697. if (s->out.mac)
  698. length -= ssh2_mac_alg(s->out.mac)->len;
  699. length -= 8; /* length field + min padding */
  700. length -= 5; /* type code + string length prefix */
  701. if (length < 0)
  702. length = 0;
  703. ignore_pkt = ssh2_bpp_new_pktout(SSH2_MSG_IGNORE);
  704. put_uint32(ignore_pkt, length);
  705. while (length-- > 0)
  706. put_byte(ignore_pkt, random_byte());
  707. ssh2_bpp_format_packet_inner(s, ignore_pkt);
  708. bufchain_add(s->bpp.out_raw, ignore_pkt->data, ignore_pkt->length);
  709. ssh_free_pktout(ignore_pkt);
  710. }
  711. }
  712. ssh2_bpp_format_packet_inner(s, pkt);
  713. bufchain_add(s->bpp.out_raw, pkt->data, pkt->length);
  714. }
  715. static void ssh2_bpp_handle_output(BinaryPacketProtocol *bpp)
  716. {
  717. struct ssh2_bpp_state *s = container_of(bpp, struct ssh2_bpp_state, bpp);
  718. PktOut *pkt;
  719. int n_userauth;
  720. /*
  721. * Count the userauth packets in the queue.
  722. */
  723. n_userauth = 0;
  724. for (pkt = pq_first(&s->bpp.out_pq); pkt != NULL;
  725. pkt = pq_next(&s->bpp.out_pq, pkt))
  726. if (userauth_range(pkt->type))
  727. n_userauth++;
  728. if (s->pending_compression && !n_userauth) {
  729. /*
  730. * We're currently blocked from sending any outgoing packets
  731. * until the other end tells us whether we're going to have to
  732. * enable compression or not.
  733. *
  734. * If our end has pushed a userauth packet on the queue, that
  735. * must mean it knows that a USERAUTH_SUCCESS is not
  736. * immediately forthcoming, so we unblock ourselves and send
  737. * up to and including that packet. But in this if statement,
  738. * there aren't any, so we're still blocked.
  739. */
  740. return;
  741. }
  742. if (s->cbc_ignore_workaround) {
  743. /*
  744. * When using a CBC-mode cipher in SSH-2, it's necessary to
  745. * ensure that an attacker can't provide data to be encrypted
  746. * using an IV that they know. We ensure this by inserting an
  747. * SSH_MSG_IGNORE if the last cipher block of the previous
  748. * packet has already been sent to the network (which we
  749. * approximate conservatively by checking if it's vanished
  750. * from out_raw).
  751. */
  752. if (bufchain_size(s->bpp.out_raw) <
  753. (ssh2_cipher_alg(s->out.cipher)->blksize +
  754. ssh2_mac_alg(s->out.mac)->len)) {
  755. /*
  756. * There's less data in out_raw than the MAC size plus the
  757. * cipher block size, which means at least one byte of
  758. * that cipher block must already have left. Add an
  759. * IGNORE.
  760. */
  761. pkt = ssh_bpp_new_pktout(&s->bpp, SSH2_MSG_IGNORE);
  762. put_stringz(pkt, "");
  763. ssh2_bpp_format_packet(s, pkt);
  764. }
  765. }
  766. while ((pkt = pq_pop(&s->bpp.out_pq)) != NULL) {
  767. if (userauth_range(pkt->type))
  768. n_userauth--;
  769. ssh2_bpp_format_packet(s, pkt);
  770. ssh_free_pktout(pkt);
  771. if (n_userauth == 0 && s->out.pending_compression) {
  772. /*
  773. * This is the last userauth packet in the queue, so
  774. * unless our side decides to send another one in future,
  775. * we have to assume will potentially provoke
  776. * USERAUTH_SUCCESS. Block (non-userauth) outgoing packets
  777. * until we see the reply.
  778. */
  779. s->pending_compression = TRUE;
  780. return;
  781. }
  782. }
  783. }