netssh.h 7.0 KB

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