2 * Dropbear - a SSH2 server
4 * Copyright (c) 2005 Matt Johnston
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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
27 #ifdef ENABLE_CLI_AGENTFWD
33 #include "chansession.h"
44 /* The protocol implemented to talk to OpenSSH's SSH2 agent is documented in
45 PROTOCOL.agent in recent OpenSSH source distributions (5.1p1 has it). */
47 static int new_agent_chan(struct Channel
* channel
);
49 const struct ChanType cli_chan_agent
= {
51 "auth-agent@openssh.com",
58 static int connect_agent() {
61 char* agent_sock
= NULL
;
63 agent_sock
= getenv("SSH_AUTH_SOCK");
64 if (agent_sock
== NULL
)
67 fd
= connect_unix(agent_sock
);
70 dropbear_log(LOG_INFO
, "Failed to connect to agent");
76 // handle a request for a connection to the locally running ssh-agent
78 static int new_agent_chan(struct Channel
* channel
) {
82 if (!cli_opts
.agent_fwd
)
83 return SSH_OPEN_ADMINISTRATIVELY_PROHIBITED
;
86 if (cli_opts
.agent_fd
< 0) {
87 return SSH_OPEN_CONNECT_FAILED
;
92 ses
.maxfd
= MAX(ses
.maxfd
, fd
);
95 channel
->writefd
= fd
;
101 /* Sends a request to the agent, returning a newly allocated buffer
102 * with the response */
103 /* This function will block waiting for a response - it will
104 * only be used by client authentication (not for forwarded requests)
105 * won't cause problems for interactivity. */
106 /* Packet format (from draft-ylonen)
107 4 bytes Length, msb first. Does not include length itself.
108 1 byte Packet type. The value 255 is reserved for future extensions.
109 data Any data, depending on packet type. Encoding as in the ssh packet
112 static buffer
* agent_request(unsigned char type
, buffer
*data
) {
114 buffer
* payload
= NULL
;
115 buffer
* inbuf
= NULL
;
118 const int fd
= cli_opts
.agent_fd
;
119 unsigned int data_len
= 0;
122 data_len
= data
->len
;
125 payload
= buf_new(4 + 1 + data_len
);
127 buf_putint(payload
, 1 + data_len
);
128 buf_putbyte(payload
, type
);
130 buf_putbytes(payload
, data
->data
, data
->len
);
132 buf_setpos(payload
, 0);
134 ret
= atomicio(write
, fd
, buf_getptr(payload
, payload
->len
), payload
->len
);
135 if ((size_t)ret
!= payload
->len
) {
136 TRACE(("write failed fd %d for agent_request, %s", fd
, strerror(errno
)))
142 TRACE(("Wrote out bytes for agent_request"))
143 /* Now we read the response */
145 ret
= atomicio(read
, fd
, buf_getwriteptr(inbuf
, 4), 4);
147 TRACE(("read of length failed for agent_request"))
150 buf_setpos(inbuf
, 0);
151 buf_setlen(inbuf
, ret
);
153 readlen
= buf_getint(inbuf
);
154 if (readlen
> MAX_AGENT_REPLY
) {
155 TRACE(("agent reply is too big"));
159 TRACE(("agent_request readlen is %d", readlen
))
161 buf_resize(inbuf
, readlen
);
162 buf_setpos(inbuf
, 0);
163 ret
= atomicio(read
, fd
, buf_getwriteptr(inbuf
, readlen
), readlen
);
164 if ((size_t)ret
!= readlen
) {
165 TRACE(("read of data failed for agent_request"))
168 buf_incrwritepos(inbuf
, readlen
);
169 buf_setpos(inbuf
, 0);
170 TRACE(("agent_request success, length %d", readlen
))
179 static void agent_get_key_list(m_list
* ret_list
)
181 buffer
* inbuf
= NULL
;
182 unsigned int num
= 0;
183 unsigned char packet_type
;
187 inbuf
= agent_request(SSH2_AGENTC_REQUEST_IDENTITIES
, NULL
);
189 TRACE(("agent_request failed returning identities"))
193 /* The reply has a format of:
194 byte SSH2_AGENT_IDENTITIES_ANSWER
196 Followed by zero or more consecutive keys, encoded as:
200 packet_type
= buf_getbyte(inbuf
);
201 if (packet_type
!= SSH2_AGENT_IDENTITIES_ANSWER
) {
205 num
= buf_getint(inbuf
);
206 for (i
= 0; i
< num
; i
++) {
207 sign_key
* pubkey
= NULL
;
208 int key_type
= DROPBEAR_SIGNKEY_ANY
;
211 /* each public key is encoded as a string */
212 key_buf
= buf_getstringbuf(inbuf
);
213 pubkey
= new_sign_key();
214 ret
= buf_get_pub_key(key_buf
, pubkey
, &key_type
);
216 if (ret
!= DROPBEAR_SUCCESS
) {
217 /* This is slack, properly would cleanup vars etc */
218 dropbear_exit("Bad pubkey received from agent");
220 pubkey
->type
= key_type
;
221 pubkey
->source
= SIGNKEY_SOURCE_AGENT
;
223 list_append(ret_list
, pubkey
);
225 /* We'll ignore the comment for now. might want it later.*/
226 buf_eatstring(inbuf
);
236 void cli_setup_agent(struct Channel
*channel
) {
237 if (!getenv("SSH_AUTH_SOCK")) {
241 cli_start_send_channel_request(channel
, "auth-agent-req@openssh.com");
242 /* Don't want replies */
243 buf_putbyte(ses
.writepayload
, 0);
247 /* Returned keys are prepended to ret_list, which will
249 void cli_load_agent_keys(m_list
*ret_list
) {
250 /* agent_fd will be closed after successful auth */
251 cli_opts
.agent_fd
= connect_agent();
252 if (cli_opts
.agent_fd
< 0) {
256 agent_get_key_list(ret_list
);
259 void agent_buf_sign(buffer
*sigblob
, sign_key
*key
,
260 const unsigned char *data
, unsigned int len
) {
261 buffer
*request_data
= buf_new(MAX_PUBKEY_SIZE
+ len
+ 12);
263 unsigned int keylen
, siglen
;
267 byte SSH2_AGENTC_SIGN_REQUEST
272 /* We write the key, then figure how long it was and write that */
273 //buf_putint(request_data, 0);
274 buf_put_pub_key(request_data
, key
, key
->type
);
275 keylen
= request_data
->len
- 4;
276 //buf_setpos(request_data, 0);
277 //buf_putint(request_data, keylen);
279 //buf_setpos(request_data, request_data->len);
280 buf_putstring(request_data
, data
, len
);
281 buf_putint(request_data
, 0);
283 response
= agent_request(SSH2_AGENTC_SIGN_REQUEST
, request_data
);
284 buf_free(request_data
);
290 packet_type
= buf_getbyte(response
);
291 if (packet_type
!= SSH2_AGENT_SIGN_RESPONSE
) {
296 byte SSH2_AGENT_SIGN_RESPONSE
297 string signature_blob
299 siglen
= buf_getint(response
);
300 buf_putbytes(sigblob
, buf_getptr(response
, siglen
), siglen
);
305 /* XXX don't fail badly here. instead propagate a failure code back up to
306 the cli auth pubkey code, and just remove this key from the list of
308 dropbear_exit("Agent failed signing key");