ssh2bpp.c 33 KB

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