bpp2.c 37 KB

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