ssh2bpp.c 26 KB

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