ssh2bpp.c 33 KB

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