transport.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <mp.h>
  12. #include <fcall.h>
  13. #include <thread.h>
  14. #include <9p.h>
  15. #include <libsec.h>
  16. #include <ip.h>
  17. #include "netssh.h"
  18. extern Cipher *cryptos[];
  19. Packet *
  20. new_packet(Conn *c)
  21. {
  22. Packet *p;
  23. p = emalloc9p(sizeof(Packet));
  24. init_packet(p);
  25. p->c = c;
  26. return p;
  27. }
  28. void
  29. init_packet(Packet *p)
  30. {
  31. memset(p, 0, sizeof(Packet));
  32. p->rlength = 1;
  33. }
  34. void
  35. add_byte(Packet *p, char c)
  36. {
  37. p->payload[p->rlength-1] = c;
  38. p->rlength++;
  39. }
  40. void
  41. add_uint32(Packet *p, uint32_t l)
  42. {
  43. hnputl(p->payload+p->rlength-1, l);
  44. p->rlength += 4;
  45. }
  46. uint32_t
  47. get_uint32(Packet *, uint8_t **data)
  48. {
  49. uint32_t x;
  50. x = nhgetl(*data);
  51. *data += 4;
  52. return x;
  53. }
  54. int
  55. add_packet(Packet *p, void *data, int len)
  56. {
  57. if(p->rlength + len > Maxpayload)
  58. return -1;
  59. memmove(p->payload + p->rlength - 1, data, len);
  60. p->rlength += len;
  61. return 0;
  62. }
  63. void
  64. add_block(Packet *p, void *data, int len)
  65. {
  66. hnputl(p->payload + p->rlength - 1, len);
  67. p->rlength += 4;
  68. add_packet(p, data, len);
  69. }
  70. void
  71. add_string(Packet *p, char *s)
  72. {
  73. uint8_t *q;
  74. int n;
  75. uint8_t nn[4];
  76. n = strlen(s);
  77. hnputl(nn, n);
  78. q = p->payload + p->rlength - 1;
  79. memmove(q, nn, 4);
  80. memmove(q+4, s, n);
  81. p->rlength += n + 4;
  82. }
  83. uint8_t *
  84. get_string(Packet *p, uint8_t *q, char *s, int lim, int *len)
  85. {
  86. int n, m;
  87. if (p && q > p->payload + p->rlength)
  88. s[0] = '\0';
  89. m = nhgetl(q);
  90. q += 4;
  91. if(m < lim)
  92. n = m;
  93. else
  94. n = lim - 1;
  95. memmove(s, q, n);
  96. s[n] = '\0';
  97. q += m;
  98. if(len)
  99. *len = n;
  100. return q;
  101. }
  102. void
  103. add_mp(Packet *p, mpint *x)
  104. {
  105. uint8_t *q;
  106. int n;
  107. q = p->payload + p->rlength - 1;
  108. n = mptobe(x, q + 4, Maxpktpay - p->rlength + 1 - 4, nil);
  109. if(q[4] & 0x80){
  110. memmove(q + 5, q + 4, n);
  111. q[4] = 0;
  112. n++;
  113. }
  114. hnputl(q, n);
  115. p->rlength += n + 4;
  116. }
  117. mpint *
  118. get_mp(uint8_t *q)
  119. {
  120. return betomp(q + 4, nhgetl(q), nil);
  121. }
  122. int
  123. finish_packet(Packet *p)
  124. {
  125. Conn *c;
  126. uint8_t *q, *buf;
  127. int blklen, i, n2, n1, maclen;
  128. c = p->c;
  129. blklen = 8;
  130. if(c && debug > 1)
  131. fprint(2, "%s: in finish_packet: enc %d outmac %d len %ld\n",
  132. argv0, c->encrypt, c->outmac, p->rlength);
  133. if(c && c->encrypt != -1){
  134. blklen = cryptos[c->encrypt]->blklen;
  135. if(blklen < 8)
  136. blklen = 8;
  137. }
  138. n1 = p->rlength - 1;
  139. n2 = blklen - (n1 + 5) % blklen;
  140. if(n2 < 4)
  141. n2 += blklen;
  142. p->pad_len = n2;
  143. for(i = 0, q = p->payload + n1; i < n2; ++i, ++q)
  144. *q = fastrand();
  145. p->rlength = n1 + n2 + 1;
  146. hnputl(p->nlength, p->rlength);
  147. maclen = 0;
  148. if(c && c->outmac != -1){
  149. maclen = SHA1dlen;
  150. buf = emalloc9p(Maxpktpay);
  151. hnputl(buf, c->outseq);
  152. memmove(buf + 4, p->nlength, p->rlength + 4);
  153. hmac_sha1(buf, p->rlength + 8, c->outik, maclen, q, nil);
  154. free(buf);
  155. }
  156. if(c && c->encrypt != -1)
  157. cryptos[c->encrypt]->encrypt(c->enccs, p->nlength, p->rlength + 4);
  158. if (c)
  159. c->outseq++;
  160. if(debug > 1)
  161. fprint(2, "%s: leaving finish packet: len %ld n1 %d n2 %d maclen %d\n",
  162. argv0, p->rlength, n1, n2, maclen);
  163. return p->rlength + 4 + maclen;
  164. }
  165. /*
  166. * The first blklen bytes are already decrypted so we could find the
  167. * length.
  168. */
  169. int
  170. undo_packet(Packet *p)
  171. {
  172. Conn *c;
  173. int32_t nlength;
  174. int nb;
  175. uint8_t rmac[SHA1dlen], *buf;
  176. c = p->c;
  177. nb = 4;
  178. if(c->decrypt != -1)
  179. nb = cryptos[c->decrypt]->blklen;
  180. if(c->inmac != -1)
  181. p->rlength -= SHA1dlen; /* was magic 20 */
  182. nlength = nhgetl(p->nlength);
  183. if(c->decrypt != -1)
  184. cryptos[c->decrypt]->decrypt(c->deccs, p->nlength + nb,
  185. p->rlength + 4 - nb);
  186. if(c->inmac != -1){
  187. buf = emalloc9p(Maxpktpay);
  188. hnputl(buf, c->inseq);
  189. memmove(buf + 4, p->nlength, nlength + 4);
  190. hmac_sha1(buf, nlength + 8, c->inik, SHA1dlen, rmac, nil);
  191. free(buf);
  192. if(memcmp(rmac, p->payload + nlength - 1, SHA1dlen) != 0){
  193. fprint(2, "%s: received MAC verification failed: seq=%d\n",
  194. argv0, c->inseq);
  195. return -1;
  196. }
  197. }
  198. c->inseq++;
  199. p->rlength -= p->pad_len;
  200. p->pad_len = 0;
  201. return p->rlength - 1;
  202. }
  203. void
  204. dump_packet(Packet *p)
  205. {
  206. int i;
  207. char *buf, *q, *e;
  208. fprint(2, "Length: %ld, Padding length: %d\n", p->rlength, p->pad_len);
  209. q = buf = emalloc9p(Copybufsz);
  210. e = buf + Copybufsz;
  211. for(i = 0; i < p->rlength - 1; ++i){
  212. q = seprint(q, e, " %02x", p->payload[i]);
  213. if(i % 16 == 15)
  214. q = seprint(q, e, "\n");
  215. if(q - buf > Copybufsz - 4){
  216. fprint(2, "%s", buf);
  217. q = buf;
  218. }
  219. }
  220. fprint(2, "%s\n", buf);
  221. free(buf);
  222. }