cli-agentfwd.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * Dropbear - a SSH2 server
  3. *
  4. * Copyright (c) 2005 Matt Johnston
  5. * All rights reserved.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE. */
  24. #include "includes.h"
  25. #if DROPBEAR_CLI_AGENTFWD
  26. #include "agentfwd.h"
  27. #include "session.h"
  28. #include "ssh.h"
  29. #include "dbutil.h"
  30. #include "chansession.h"
  31. #include "channel.h"
  32. #include "packet.h"
  33. #include "buffer.h"
  34. #include "dbrandom.h"
  35. #include "listener.h"
  36. #include "runopts.h"
  37. #include "atomicio.h"
  38. #include "signkey.h"
  39. #include "auth.h"
  40. /* The protocol implemented to talk to OpenSSH's SSH2 agent is documented in
  41. PROTOCOL.agent in recent OpenSSH source distributions (5.1p1 has it). */
  42. static int new_agent_chan(struct Channel * channel);
  43. const struct ChanType cli_chan_agent = {
  44. "auth-agent@openssh.com",
  45. new_agent_chan,
  46. NULL,
  47. NULL,
  48. NULL,
  49. NULL
  50. };
  51. static int connect_agent() {
  52. int fd = -1;
  53. char* agent_sock = NULL;
  54. agent_sock = getenv("SSH_AUTH_SOCK");
  55. if (agent_sock == NULL)
  56. return -1;
  57. fd = connect_unix(agent_sock);
  58. if (fd < 0) {
  59. dropbear_log(LOG_INFO, "Failed to connect to agent");
  60. }
  61. return fd;
  62. }
  63. /* handle a request for a connection to the locally running ssh-agent
  64. or forward. */
  65. static int new_agent_chan(struct Channel * channel) {
  66. int fd = -1;
  67. if (!cli_opts.agent_fwd)
  68. return SSH_OPEN_ADMINISTRATIVELY_PROHIBITED;
  69. fd = connect_agent();
  70. if (fd < 0) {
  71. return SSH_OPEN_CONNECT_FAILED;
  72. }
  73. setnonblocking(fd);
  74. ses.maxfd = MAX(ses.maxfd, fd);
  75. channel->readfd = fd;
  76. channel->writefd = fd;
  77. channel->bidir_fd = 1;
  78. return 0;
  79. }
  80. /* Sends a request to the agent, returning a newly allocated buffer
  81. * with the response */
  82. /* This function will block waiting for a response - it will
  83. * only be used by client authentication (not for forwarded requests)
  84. * won't cause problems for interactivity. */
  85. /* Packet format (from draft-ylonen)
  86. 4 bytes Length, msb first. Does not include length itself.
  87. 1 byte Packet type. The value 255 is reserved for future extensions.
  88. data Any data, depending on packet type. Encoding as in the ssh packet
  89. protocol.
  90. */
  91. static buffer * agent_request(unsigned char type, const buffer *data) {
  92. buffer * payload = NULL;
  93. buffer * inbuf = NULL;
  94. size_t readlen = 0;
  95. ssize_t ret;
  96. const int fd = cli_opts.agent_fd;
  97. unsigned int data_len = 0;
  98. if (data)
  99. {
  100. data_len = data->len;
  101. }
  102. payload = buf_new(4 + 1 + data_len);
  103. buf_putint(payload, 1 + data_len);
  104. buf_putbyte(payload, type);
  105. if (data) {
  106. buf_putbytes(payload, data->data, data->len);
  107. }
  108. buf_setpos(payload, 0);
  109. ret = atomicio(vwrite, fd, buf_getptr(payload, payload->len), payload->len);
  110. if ((size_t)ret != payload->len) {
  111. TRACE(("write failed fd %d for agent_request, %s", fd, strerror(errno)))
  112. goto out;
  113. }
  114. buf_free(payload);
  115. payload = NULL;
  116. TRACE(("Wrote out bytes for agent_request"))
  117. /* Now we read the response */
  118. inbuf = buf_new(4);
  119. ret = atomicio(read, fd, buf_getwriteptr(inbuf, 4), 4);
  120. if (ret != 4) {
  121. TRACE(("read of length failed for agent_request"))
  122. goto out;
  123. }
  124. buf_setpos(inbuf, 0);
  125. buf_setlen(inbuf, ret);
  126. readlen = buf_getint(inbuf);
  127. if (readlen > MAX_AGENT_REPLY) {
  128. TRACE(("agent reply is too big"));
  129. goto out;
  130. }
  131. inbuf = buf_resize(inbuf, readlen);
  132. buf_setpos(inbuf, 0);
  133. ret = atomicio(read, fd, buf_getwriteptr(inbuf, readlen), readlen);
  134. if ((size_t)ret != readlen) {
  135. TRACE(("read of data failed for agent_request"))
  136. goto out;
  137. }
  138. buf_incrwritepos(inbuf, readlen);
  139. buf_setpos(inbuf, 0);
  140. out:
  141. if (payload)
  142. buf_free(payload);
  143. return inbuf;
  144. }
  145. static void agent_get_key_list(m_list * ret_list)
  146. {
  147. buffer * inbuf = NULL;
  148. unsigned int num = 0;
  149. unsigned char packet_type;
  150. unsigned int i;
  151. int ret;
  152. inbuf = agent_request(SSH2_AGENTC_REQUEST_IDENTITIES, NULL);
  153. if (!inbuf) {
  154. TRACE(("agent_request failed returning identities"))
  155. goto out;
  156. }
  157. /* The reply has a format of:
  158. byte SSH2_AGENT_IDENTITIES_ANSWER
  159. uint32 num_keys
  160. Followed by zero or more consecutive keys, encoded as:
  161. string key_blob
  162. string key_comment
  163. */
  164. packet_type = buf_getbyte(inbuf);
  165. if (packet_type != SSH2_AGENT_IDENTITIES_ANSWER) {
  166. goto out;
  167. }
  168. num = buf_getint(inbuf);
  169. for (i = 0; i < num; i++) {
  170. sign_key * pubkey = NULL;
  171. enum signkey_type key_type = DROPBEAR_SIGNKEY_ANY;
  172. buffer * key_buf;
  173. /* each public key is encoded as a string */
  174. key_buf = buf_getstringbuf(inbuf);
  175. pubkey = new_sign_key();
  176. ret = buf_get_pub_key(key_buf, pubkey, &key_type);
  177. buf_free(key_buf);
  178. if (ret != DROPBEAR_SUCCESS) {
  179. TRACE(("Skipping bad/unknown type pubkey from agent"));
  180. sign_key_free(pubkey);
  181. } else {
  182. pubkey->type = key_type;
  183. pubkey->source = SIGNKEY_SOURCE_AGENT;
  184. list_append(ret_list, pubkey);
  185. }
  186. /* We'll ignore the comment for now. might want it later.*/
  187. buf_eatstring(inbuf);
  188. }
  189. out:
  190. if (inbuf) {
  191. buf_free(inbuf);
  192. inbuf = NULL;
  193. }
  194. }
  195. void cli_setup_agent(const struct Channel *channel) {
  196. if (!getenv("SSH_AUTH_SOCK")) {
  197. return;
  198. }
  199. start_send_channel_request(channel, "auth-agent-req@openssh.com");
  200. /* Don't want replies */
  201. buf_putbyte(ses.writepayload, 0);
  202. encrypt_packet();
  203. }
  204. /* Returned keys are prepended to ret_list, which will
  205. be updated. */
  206. void cli_load_agent_keys(m_list *ret_list) {
  207. /* agent_fd will be closed after successful auth */
  208. cli_opts.agent_fd = connect_agent();
  209. if (cli_opts.agent_fd < 0) {
  210. return;
  211. }
  212. agent_get_key_list(ret_list);
  213. }
  214. void agent_buf_sign(buffer *sigblob, sign_key *key,
  215. const buffer *data_buf, enum signature_type sigtype) {
  216. buffer *request_data = NULL;
  217. buffer *response = NULL;
  218. unsigned int siglen;
  219. int packet_type;
  220. int flags = 0;
  221. /* Request format
  222. byte SSH2_AGENTC_SIGN_REQUEST
  223. string key_blob
  224. string data
  225. uint32 flags
  226. */
  227. request_data = buf_new(MAX_PUBKEY_SIZE + data_buf->len + 12);
  228. buf_put_pub_key(request_data, key, key->type);
  229. buf_putbufstring(request_data, data_buf);
  230. #if DROPBEAR_RSA_SHA256
  231. if (sigtype == DROPBEAR_SIGNATURE_RSA_SHA256) {
  232. flags |= SSH_AGENT_RSA_SHA2_256;
  233. }
  234. #endif
  235. buf_putint(request_data, flags);
  236. response = agent_request(SSH2_AGENTC_SIGN_REQUEST, request_data);
  237. if (!response) {
  238. goto fail;
  239. }
  240. packet_type = buf_getbyte(response);
  241. if (packet_type != SSH2_AGENT_SIGN_RESPONSE) {
  242. goto fail;
  243. }
  244. /* Response format
  245. byte SSH2_AGENT_SIGN_RESPONSE
  246. string signature_blob
  247. */
  248. siglen = buf_getint(response);
  249. buf_putbytes(sigblob, buf_getptr(response, siglen), siglen);
  250. goto cleanup;
  251. fail:
  252. /* XXX don't fail badly here. instead propagate a failure code back up to
  253. the cli auth pubkey code, and just remove this key from the list of
  254. ones to try. */
  255. dropbear_exit("Agent failed signing key");
  256. cleanup:
  257. if (request_data) {
  258. buf_free(request_data);
  259. }
  260. if (response) {
  261. buf_free(response);
  262. }
  263. }
  264. #endif