netssh.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 <bio.h>
  10. #include "ssh2.h" /* ugh */
  11. #define MYID "SSH-2.0-Plan9"
  12. enum {
  13. Server = 0,
  14. Client,
  15. Maxpktpay = 35000,
  16. /* qid.path components: level (2), type (4), conn (7), chan (7) */
  17. Connshift = 7,
  18. MAXCONN = 1 << Connshift, /* also Maxchan */
  19. Chanmask = MAXCONN - 1,
  20. Connmask = Chanmask,
  21. Qtypeshift = 2 * Connshift, /* conn + chan */
  22. Qroot = 0,
  23. Qclone = 1 << Qtypeshift,
  24. Qctl = 2 << Qtypeshift,
  25. Qdata = 3 << Qtypeshift,
  26. Qlisten = 4 << Qtypeshift,
  27. Qlocal = 5 << Qtypeshift,
  28. Qreqrem = 6 << Qtypeshift, /* request or remote */
  29. Qstatus = 7 << Qtypeshift,
  30. Qtcp = 8 << Qtypeshift,
  31. Qtypemask = 017 << Qtypeshift,
  32. Levshift = Qtypeshift + 4,
  33. /* levels of /net/ssh hierarchy */
  34. Top = 0,
  35. Connection,
  36. Subchannel,
  37. };
  38. /*
  39. * The stylistic anomaly with these names of unbounded length
  40. * is a result of following the RFCs in using the same names for
  41. * these constants. I did that to make it easier to search and
  42. * cross-reference between the code and the RFCs.
  43. */
  44. enum { /* SSH2 Protocol Packet Types */
  45. SSH_MSG_DISCONNECT = 1,
  46. SSH_MSG_IGNORE = 2,
  47. SSH_MSG_UNIMPLEMENTED,
  48. SSH_MSG_DEBUG,
  49. SSH_MSG_SERVICE_REQUEST,
  50. SSH_MSG_SERVICE_ACCEPT,
  51. SSH_MSG_KEXINIT = 20,
  52. SSH_MSG_NEWKEYS,
  53. SSH_MSG_KEXDH_INIT = 30,
  54. SSH_MSG_KEXDH_REPLY,
  55. SSH_MSG_USERAUTH_REQUEST = 50,
  56. SSH_MSG_USERAUTH_FAILURE,
  57. SSH_MSG_USERAUTH_SUCCESS,
  58. SSH_MSG_USERAUTH_BANNER,
  59. SSH_MSG_USERAUTH_PK_OK = 60,
  60. SSH_MSG_USERAUTH_PASSWD_CHANGEREQ = 60,
  61. SSH_MSG_GLOBAL_REQUEST = 80,
  62. SSH_MSG_REQUEST_SUCCESS,
  63. SSH_MSG_REQUEST_FAILURE,
  64. SSH_MSG_CHANNEL_OPEN = 90,
  65. SSH_MSG_CHANNEL_OPEN_CONFIRMATION,
  66. SSH_MSG_CHANNEL_OPEN_FAILURE,
  67. SSH_MSG_CHANNEL_WINDOW_ADJUST,
  68. SSH_MSG_CHANNEL_DATA,
  69. SSH_MSG_CHANNEL_EXTENDED_DATA,
  70. SSH_MSG_CHANNEL_EOF,
  71. SSH_MSG_CHANNEL_CLOSE,
  72. SSH_MSG_CHANNEL_REQUEST,
  73. SSH_MSG_CHANNEL_SUCCESS,
  74. SSH_MSG_CHANNEL_FAILURE,
  75. };
  76. enum { /* SSH2 reason codes */
  77. SSH_DISCONNECT_HOST_NOT_ALLOWED_TO_CONNECT = 1,
  78. SSH_DISCONNECT_PROTOCOL_ERROR,
  79. SSH_DISCONNECT_KEY_EXCHANGE_FAILED,
  80. SSH_DISCONNECT_RESERVED,
  81. SSH_DISCONNECT_MAC_ERROR,
  82. SSH_DISCONNECT_COMPRESSION_ERROR,
  83. SSH_DISCONNECT_SERVICE_NOT_AVAILABLE,
  84. SSH_DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED,
  85. SSH_DISCONNECT_HOST_KEY_NOT_VERIFIABLE,
  86. SSH_DISCONNECT_CONNECTION_LOST,
  87. SSH_DISCONNECT_BY_APPLICATION,
  88. SSH_DISCONNECT_TOO_MANY_CONNECTIONS,
  89. SSH_DISCONNECT_AUTH_CANCELLED_BY_USER,
  90. SSH_DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE,
  91. SSH_DISCONNECT_ILLEGAL_USR_NAME,
  92. SSH_OPEN_ADMINISTRATIVELY_PROHIBITED = 1,
  93. SSH_OPEN_CONNECT_FAILED,
  94. SSH_OPEN_UNKNOWN_CHANNEL_TYPE,
  95. SSH_OPEN_RESOURCE_SHORTAGE,
  96. };
  97. enum { /* SSH2 type code */
  98. SSH_EXTENDED_DATA_STDERR = 1,
  99. };
  100. enum { /* connection and channel states */
  101. Empty = 0,
  102. Allocated,
  103. Initting,
  104. Listening,
  105. Opening,
  106. Negotiating,
  107. Authing,
  108. Established,
  109. Eof,
  110. Closing,
  111. Closed,
  112. };
  113. enum {
  114. NoKeyFile,
  115. NoKey,
  116. KeyWrong,
  117. KeyOk,
  118. };
  119. typedef struct Cipher Cipher;
  120. typedef struct CipherState CipherState;
  121. typedef struct Conn Conn;
  122. typedef struct Kex Kex;
  123. typedef struct MBox MBox;
  124. typedef struct PKA PKA;
  125. typedef struct Packet Packet;
  126. typedef struct Plist Plist;
  127. typedef struct SSHChan SSHChan;
  128. struct Plist {
  129. Packet *pack;
  130. uchar *st;
  131. int rem;
  132. Plist *next;
  133. };
  134. struct SSHChan {
  135. Rendez r; /* awaiting input? */
  136. int id;
  137. int otherid;
  138. int state;
  139. int waker;
  140. int conn;
  141. uint32_t rwindow;
  142. uint32_t twindow;
  143. uint32_t sent;
  144. uint32_t inrqueue;
  145. char *ann;
  146. Req *lreq;
  147. /* File* for each Qid type */
  148. File *dir;
  149. File *ctl;
  150. File *data;
  151. File *listen;
  152. File *request;
  153. File *status;
  154. File *tcp;
  155. Plist *dataq;
  156. Plist *datatl;
  157. Plist *reqq;
  158. Plist *reqtl;
  159. Channel *inchan;
  160. Channel *reqchan;
  161. QLock xmtlock;
  162. Rendez xmtrendez;
  163. };
  164. struct Conn {
  165. QLock l;
  166. Rendez r; /* awaiting input? */
  167. Ioproc *dio;
  168. Ioproc *cio;
  169. Ioproc *rio;
  170. int state;
  171. int role;
  172. int id;
  173. char *remote;
  174. char *user;
  175. char *password;
  176. char *service;
  177. char *cap;
  178. char *authkey;
  179. int nchan;
  180. /* underlying tcp connection */
  181. int datafd;
  182. int ctlfd;
  183. int stifle; /* flag: no i/o between listen and sshsession */
  184. int poisoned;
  185. int tcpconn;
  186. int rpid;
  187. int inseq;
  188. int outseq;
  189. int kexalg;
  190. int pkalg;
  191. int cscrypt;
  192. int ncscrypt;
  193. int sccrypt;
  194. int nsccrypt;
  195. int csmac;
  196. int ncsmac;
  197. int scmac;
  198. int nscmac;
  199. int encrypt;
  200. int decrypt;
  201. int outmac;
  202. int inmac;
  203. /* File* for each Qid type */
  204. File *dir;
  205. File *clonefile;
  206. File *ctlfile;
  207. File *datafile;
  208. File *listenfile;
  209. File *localfile;
  210. File *remotefile;
  211. File *statusfile;
  212. File *tcpfile;
  213. Packet *skexinit;
  214. Packet *rkexinit;
  215. mpint *x;
  216. mpint *e;
  217. int got_sessid;
  218. uchar sessid[SHA1dlen];
  219. uchar c2siv[SHA1dlen*2];
  220. uchar nc2siv[SHA1dlen*2];
  221. uchar s2civ[SHA1dlen*2];
  222. uchar ns2civ[SHA1dlen*2];
  223. uchar c2sek[SHA1dlen*2];
  224. uchar nc2sek[SHA1dlen*2];
  225. uchar s2cek[SHA1dlen*2];
  226. uchar ns2cek[SHA1dlen*2];
  227. uchar c2sik[SHA1dlen*2];
  228. uchar nc2sik[SHA1dlen*2];
  229. uchar s2cik[SHA1dlen*2];
  230. uchar ns2cik[SHA1dlen*2];
  231. char *otherid;
  232. uchar *inik;
  233. uchar *outik;
  234. CipherState *s2ccs;
  235. CipherState *c2scs;
  236. CipherState *enccs;
  237. CipherState *deccs;
  238. SSHChan *chans[MAXCONN];
  239. char idstring[256]; /* max allowed by SSH spec */
  240. };
  241. struct Packet {
  242. Conn *c;
  243. uint32_t rlength;
  244. uint32_t tlength;
  245. uchar nlength[4];
  246. uchar pad_len;
  247. uchar payload[Maxpktpay];
  248. };
  249. struct Cipher {
  250. char *name;
  251. int blklen;
  252. CipherState *(*init)(Conn*, int);
  253. void (*encrypt)(CipherState*, uchar*, int);
  254. void (*decrypt)(CipherState*, uchar*, int);
  255. };
  256. struct Kex {
  257. char *name;
  258. int (*serverkex)(Conn *, Packet *);
  259. int (*clientkex1)(Conn *, Packet *);
  260. int (*clientkex2)(Conn *, Packet *);
  261. };
  262. struct PKA {
  263. char *name;
  264. Packet *(*ks)(Conn *);
  265. Packet *(*sign)(Conn *, uchar *, int);
  266. int (*verify)(Conn *, uchar *, int, char *, char *, int);
  267. };
  268. struct MBox {
  269. Channel *mchan;
  270. char *msg;
  271. int state;
  272. };
  273. extern Cipher cipheraes128, cipheraes192, cipheraes256;
  274. extern Cipher cipherblowfish, cipher3des, cipherrc4;
  275. extern int debug;
  276. extern int sshkeychan[];
  277. extern Kex dh1sha1, dh14sha1;
  278. extern MBox keymbox;
  279. extern PKA rsa_pka, dss_pka, *pkas[];
  280. /* pubkey.c */
  281. int appendkey(char *, char *, RSApub *);
  282. int findkey(char *, char *, RSApub *);
  283. RSApub *readpublickey(Biobuf *, char **);
  284. int replacekey(char *, char *, RSApub *);
  285. /* dh.c */
  286. void dh_init(PKA *[]);
  287. /* transport.c */
  288. void add_block(Packet *, void *, int);
  289. void add_byte(Packet *, char);
  290. void add_mp(Packet *, mpint *);
  291. int add_packet(Packet *, void *, int);
  292. void add_string(Packet *, char *);
  293. void add_uint32(Packet *, uint32_t);
  294. void dump_packet(Packet *);
  295. int finish_packet(Packet *);
  296. mpint *get_mp(uchar *q);
  297. uchar *get_string(Packet *, uchar *, char *, int, int *);
  298. uint32_t get_uint32(Packet *, uchar **);
  299. void init_packet(Packet *);
  300. Packet *new_packet(Conn *);
  301. int undo_packet(Packet *);