ssh2bpp.c 26 KB

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