ssh2bpp.c 33 KB

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