smsg.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 "ssh.h"
  10. #include <bio.h>
  11. static void
  12. send_ssh_smsg_public_key(Conn *c)
  13. {
  14. int i;
  15. Msg *m;
  16. m = allocmsg(c, SSH_SMSG_PUBLIC_KEY, 2048);
  17. putbytes(m, c->cookie, COOKIELEN);
  18. putRSApub(m, c->serverkey);
  19. putRSApub(m, c->hostkey);
  20. putlong(m, c->flags);
  21. for(i=0; i<c->nokcipher; i++)
  22. c->ciphermask |= 1<<c->okcipher[i]->id;
  23. putlong(m, c->ciphermask);
  24. for(i=0; i<c->nokauthsrv; i++)
  25. c->authmask |= 1<<c->okauthsrv[i]->id;
  26. putlong(m, c->authmask);
  27. sendmsg(m);
  28. }
  29. static mpint*
  30. rpcdecrypt(AuthRpc *rpc, mpint *b)
  31. {
  32. mpint *a;
  33. char *p;
  34. p = mptoa(b, 16, nil, 0);
  35. if(auth_rpc(rpc, "write", p, strlen(p)) != ARok)
  36. sysfatal("factotum rsa write: %r");
  37. free(p);
  38. if(auth_rpc(rpc, "read", nil, 0) != ARok)
  39. sysfatal("factotum rsa read: %r");
  40. a = strtomp(rpc->arg, nil, 16, nil);
  41. mpfree(b);
  42. return a;
  43. }
  44. static void
  45. recv_ssh_cmsg_session_key(Conn *c, AuthRpc *rpc)
  46. {
  47. int i, id, n, serverkeylen, hostkeylen;
  48. mpint *a, *b;
  49. uint8_t *buf;
  50. Msg *m;
  51. RSApriv *ksmall, *kbig;
  52. m = recvmsg(c, SSH_CMSG_SESSION_KEY);
  53. id = getbyte(m);
  54. c->cipher = nil;
  55. for(i=0; i<c->nokcipher; i++)
  56. if(c->okcipher[i]->id == id)
  57. c->cipher = c->okcipher[i];
  58. if(c->cipher == nil)
  59. sysfatal("invalid cipher selected");
  60. if(memcmp(getbytes(m, COOKIELEN), c->cookie, COOKIELEN) != 0)
  61. sysfatal("bad cookie");
  62. serverkeylen = mpsignif(c->serverkey->n);
  63. hostkeylen = mpsignif(c->hostkey->n);
  64. ksmall = kbig = nil;
  65. if(serverkeylen+128 <= hostkeylen){
  66. ksmall = c->serverpriv;
  67. kbig = nil;
  68. }else if(hostkeylen+128 <= serverkeylen){
  69. ksmall = nil;
  70. kbig = c->serverpriv;
  71. }else
  72. sysfatal("server session and host keys do not differ by at least 128 bits");
  73. b = getmpint(m);
  74. debug(DBG_CRYPTO, "encrypted with kbig is %B\n", b);
  75. if(kbig){
  76. a = rsadecrypt(kbig, b, nil);
  77. mpfree(b);
  78. b = a;
  79. }else
  80. b = rpcdecrypt(rpc, b);
  81. a = rsaunpad(b);
  82. mpfree(b);
  83. b = a;
  84. debug(DBG_CRYPTO, "encrypted with ksmall is %B\n", b);
  85. if(ksmall){
  86. a = rsadecrypt(ksmall, b, nil);
  87. mpfree(b);
  88. b = a;
  89. }else
  90. b = rpcdecrypt(rpc, b);
  91. a = rsaunpad(b);
  92. mpfree(b);
  93. b = a;
  94. debug(DBG_CRYPTO, "munged is %B\n", b);
  95. n = (mpsignif(b)+7)/8;
  96. if(n > SESSKEYLEN)
  97. sysfatal("client sent short session key");
  98. buf = emalloc(SESSKEYLEN);
  99. mptoberjust(b, buf, SESSKEYLEN);
  100. mpfree(b);
  101. for(i=0; i<SESSIDLEN; i++)
  102. buf[i] ^= c->sessid[i];
  103. memmove(c->sesskey, buf, SESSKEYLEN);
  104. debug(DBG_CRYPTO, "unmunged is %.*H\n", SESSKEYLEN, buf);
  105. c->flags = getlong(m);
  106. free(m);
  107. }
  108. static AuthInfo*
  109. responselogin(char *user, char *resp)
  110. {
  111. Chalstate *c;
  112. AuthInfo *ai;
  113. if((c = auth_challenge("proto=p9cr user=%q role=server", user)) == nil){
  114. sshlog("auth_challenge failed for %s", user);
  115. return nil;
  116. }
  117. c->resp = resp;
  118. c->nresp = strlen(resp);
  119. ai = auth_response(c);
  120. auth_freechal(c);
  121. return ai;
  122. }
  123. static AuthInfo*
  124. authusername(Conn *c)
  125. {
  126. char *p;
  127. AuthInfo *ai;
  128. /*
  129. * hack for sam users: 'name numbers' gets tried as securid login.
  130. */
  131. if(p = strchr(c->user, ' ')){
  132. *p++ = '\0';
  133. if((ai=responselogin(c->user, p)) != nil)
  134. return ai;
  135. *--p = ' ';
  136. sshlog("bad response: %s", c->user);
  137. }
  138. return nil;
  139. }
  140. static void
  141. authsrvuser(Conn *c)
  142. {
  143. int i;
  144. char *ns, *user;
  145. AuthInfo *ai;
  146. Msg *m;
  147. m = recvmsg(c, SSH_CMSG_USER);
  148. user = getstring(m);
  149. c->user = emalloc(strlen(user)+1);
  150. strcpy(c->user, user);
  151. free(m);
  152. ai = authusername(c);
  153. while(ai == nil){
  154. /*
  155. * clumsy: if the client aborted the auth_tis early
  156. * we don't send a new failure. we check this by
  157. * looking at c->unget, which is only used in that
  158. * case.
  159. */
  160. if(c->unget != nil)
  161. goto skipfailure;
  162. sendmsg(allocmsg(c, SSH_SMSG_FAILURE, 0));
  163. skipfailure:
  164. m = recvmsg(c, -1);
  165. for(i=0; i<c->nokauthsrv; i++)
  166. if(c->okauthsrv[i]->firstmsg == m->type){
  167. ai = (*c->okauthsrv[i]->fn)(c, m);
  168. break;
  169. }
  170. if(i==c->nokauthsrv)
  171. badmsg(m, 0);
  172. }
  173. sendmsg(allocmsg(c, SSH_SMSG_SUCCESS, 0));
  174. if(noworld(ai->cuid))
  175. ns = "/lib/namespace.noworld";
  176. else
  177. ns = nil;
  178. if(auth_chuid(ai, ns) < 0){
  179. sshlog("auth_chuid to %s: %r", ai->cuid);
  180. sysfatal("auth_chuid: %r");
  181. }
  182. sshlog("logged in as %s", ai->cuid);
  183. auth_freeAI(ai);
  184. }
  185. void
  186. sshserverhandshake(Conn *c)
  187. {
  188. char *p, buf[128];
  189. Biobuf *b;
  190. Attr *a;
  191. int i, afd;
  192. mpint *m;
  193. AuthRpc *rpc;
  194. RSApub *key;
  195. /*
  196. * BUG: should use `attr' to get the key attributes
  197. * after the read, but that's not implemented yet.
  198. */
  199. if((b = Bopen("/mnt/factotum/ctl", OREAD)) == nil)
  200. sysfatal("open /mnt/factotum/ctl: %r");
  201. while((p = Brdline(b, '\n')) != nil){
  202. p[Blinelen(b)-1] = '\0';
  203. if(strstr(p, " proto=rsa ") && strstr(p, " service=sshserve "))
  204. break;
  205. }
  206. if(p == nil)
  207. sysfatal("no sshserve keys found in /mnt/factotum/ctl");
  208. a = _parseattr(p);
  209. Bterm(b);
  210. key = emalloc(sizeof(*key));
  211. if((p = _strfindattr(a, "n")) == nil)
  212. sysfatal("no n in sshserve key");
  213. if((key->n = strtomp(p, &p, 16, nil)) == nil || *p != 0)
  214. sysfatal("bad n in sshserve key");
  215. if((p = _strfindattr(a, "ek")) == nil)
  216. sysfatal("no ek in sshserve key");
  217. if((key->ek = strtomp(p, &p, 16, nil)) == nil || *p != 0)
  218. sysfatal("bad ek in sshserve key");
  219. _freeattr(a);
  220. if((afd = open("/mnt/factotum/rpc", ORDWR)) < 0)
  221. sysfatal("open /mnt/factotum/rpc: %r");
  222. if((rpc = auth_allocrpc(afd)) == nil)
  223. sysfatal("auth_allocrpc: %r");
  224. p = "proto=rsa role=client service=sshserve";
  225. if(auth_rpc(rpc, "start", p, strlen(p)) != ARok)
  226. sysfatal("auth_rpc start %s: %r", p);
  227. if(auth_rpc(rpc, "read", nil, 0) != ARok)
  228. sysfatal("auth_rpc read: %r");
  229. m = strtomp(rpc->arg, nil, 16, nil);
  230. if(mpcmp(m, key->n) != 0)
  231. sysfatal("key in /mnt/factotum/ctl does not match rpc key");
  232. mpfree(m);
  233. c->hostkey = key;
  234. /* send id string */
  235. fprint(c->fd[0], "SSH-1.5-Plan9\n");
  236. /* receive id string */
  237. if(readstrnl(c->fd[0], buf, sizeof buf) < 0)
  238. sysfatal("reading server version: %r");
  239. /* id string is "SSH-m.n-comment". We need m=1, n>=5. */
  240. if(strncmp(buf, "SSH-", 4) != 0
  241. || strtol(buf+4, &p, 10) != 1
  242. || *p != '.'
  243. || strtol(p+1, &p, 10) < 5
  244. || *p != '-')
  245. sysfatal("protocol mismatch; got %s, need SSH-1.x for x>=5", buf);
  246. for(i=0; i<COOKIELEN; i++)
  247. c->cookie[i] = fastrand();
  248. calcsessid(c);
  249. send_ssh_smsg_public_key(c);
  250. recv_ssh_cmsg_session_key(c, rpc);
  251. auth_freerpc(rpc);
  252. close(afd);
  253. c->cstate = (*c->cipher->init)(c, 1); /* turns on encryption */
  254. sendmsg(allocmsg(c, SSH_SMSG_SUCCESS, 0));
  255. authsrvuser(c);
  256. }