smsg.c 6.0 KB

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