1 /* $OpenBSD: sshconnect2.c,v 1.162 2006/08/30 00:06:51 dtucker Exp $ */
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/types.h>
29 #include <sys/socket.h>
41 #include "openbsd-compat/sys-queue.h"
52 #include "myproposal.h"
53 #include "sshconnect.h"
64 #include "pathnames.h"
72 extern char *client_version_string
;
73 extern char *server_version_string
;
74 extern Options options
;
80 u_char
*session_id2
= NULL
;
81 u_int session_id2_len
= 0;
84 struct sockaddr
*xxx_hostaddr
;
89 verify_host_key_callback(Key
*hostkey
)
91 if (verify_host_key(xxx_host
, xxx_hostaddr
, hostkey
) == -1)
92 fatal("Host key verification failed.");
97 ssh_kex2(char *host
, struct sockaddr
*hostaddr
)
102 xxx_hostaddr
= hostaddr
;
104 if (options
.ciphers
== (char *)-1) {
105 logit("No valid ciphers for protocol version 2 given, using defaults.");
106 options
.ciphers
= NULL
;
108 if (options
.ciphers
!= NULL
) {
109 myproposal
[PROPOSAL_ENC_ALGS_CTOS
] =
110 myproposal
[PROPOSAL_ENC_ALGS_STOC
] = options
.ciphers
;
112 myproposal
[PROPOSAL_ENC_ALGS_CTOS
] =
113 compat_cipher_proposal(myproposal
[PROPOSAL_ENC_ALGS_CTOS
]);
114 myproposal
[PROPOSAL_ENC_ALGS_STOC
] =
115 compat_cipher_proposal(myproposal
[PROPOSAL_ENC_ALGS_STOC
]);
116 if (options
.compression
) {
117 myproposal
[PROPOSAL_COMP_ALGS_CTOS
] =
118 myproposal
[PROPOSAL_COMP_ALGS_STOC
] = "zlib@openssh.com,zlib,none";
120 myproposal
[PROPOSAL_COMP_ALGS_CTOS
] =
121 myproposal
[PROPOSAL_COMP_ALGS_STOC
] = "none,zlib@openssh.com,zlib";
123 if (options
.macs
!= NULL
) {
124 myproposal
[PROPOSAL_MAC_ALGS_CTOS
] =
125 myproposal
[PROPOSAL_MAC_ALGS_STOC
] = options
.macs
;
127 if (options
.hostkeyalgorithms
!= NULL
)
128 myproposal
[PROPOSAL_SERVER_HOST_KEY_ALGS
] =
129 options
.hostkeyalgorithms
;
131 if (options
.rekey_limit
)
132 packet_set_rekey_limit(options
.rekey_limit
);
134 /* start key exchange */
135 kex
= kex_setup(myproposal
);
136 kex
->kex
[KEX_DH_GRP1_SHA1
] = kexdh_client
;
137 kex
->kex
[KEX_DH_GRP14_SHA1
] = kexdh_client
;
138 kex
->kex
[KEX_DH_GEX_SHA1
] = kexgex_client
;
139 kex
->kex
[KEX_DH_GEX_SHA256
] = kexgex_client
;
140 kex
->client_version_string
=client_version_string
;
141 kex
->server_version_string
=server_version_string
;
142 kex
->verify_host_key
=&verify_host_key_callback
;
146 dispatch_run(DISPATCH_BLOCK
, &kex
->done
, kex
);
148 session_id2
= kex
->session_id
;
149 session_id2_len
= kex
->session_id_len
;
152 /* send 1st encrypted/maced/compressed message */
153 packet_start(SSH2_MSG_IGNORE
);
154 packet_put_cstring("markus");
164 typedef struct Authctxt Authctxt
;
165 typedef struct Authmethod Authmethod
;
166 typedef struct identity Identity
;
167 typedef struct idlist Idlist
;
170 TAILQ_ENTRY(identity
) next
;
171 AuthenticationConnection
*ac
; /* set if agent supports key */
172 Key
*key
; /* public/private key */
173 char *filename
; /* comment for agent-only keys */
175 int isprivate
; /* key points to the private key */
177 TAILQ_HEAD(idlist
, identity
);
180 const char *server_user
;
181 const char *local_user
;
189 AuthenticationConnection
*agent
;
191 Sensitive
*sensitive
;
192 /* kbd-interactive */
198 char *name
; /* string to compare against server's list */
199 int (*userauth
)(Authctxt
*authctxt
);
200 int *enabled
; /* flag in option struct that enables method */
201 int *batch_flag
; /* flag in option struct that disables method */
204 void input_userauth_success(int, u_int32_t
, void *);
205 void input_userauth_failure(int, u_int32_t
, void *);
206 void input_userauth_banner(int, u_int32_t
, void *);
207 void input_userauth_error(int, u_int32_t
, void *);
208 void input_userauth_info_req(int, u_int32_t
, void *);
209 void input_userauth_pk_ok(int, u_int32_t
, void *);
210 void input_userauth_passwd_changereq(int, u_int32_t
, void *);
212 int userauth_none(Authctxt
*);
213 int userauth_pubkey(Authctxt
*);
214 int userauth_passwd(Authctxt
*);
215 int userauth_kbdint(Authctxt
*);
216 int userauth_hostbased(Authctxt
*);
217 int userauth_kerberos(Authctxt
*);
220 int userauth_gssapi(Authctxt
*authctxt
);
221 void input_gssapi_response(int type
, u_int32_t
, void *);
222 void input_gssapi_token(int type
, u_int32_t
, void *);
223 void input_gssapi_hash(int type
, u_int32_t
, void *);
224 void input_gssapi_error(int, u_int32_t
, void *);
225 void input_gssapi_errtok(int, u_int32_t
, void *);
228 void userauth(Authctxt
*, char *);
230 static int sign_and_send_pubkey(Authctxt
*, Identity
*);
231 static void pubkey_prepare(Authctxt
*);
232 static void pubkey_cleanup(Authctxt
*);
233 static Key
*load_identity_file(char *);
235 static Authmethod
*authmethod_get(char *authlist
);
236 static Authmethod
*authmethod_lookup(const char *name
);
237 static char *authmethods_get(void);
239 Authmethod authmethods
[] = {
243 &options
.gss_authentication
,
248 &options
.hostbased_authentication
,
252 &options
.pubkey_authentication
,
254 {"keyboard-interactive",
256 &options
.kbd_interactive_authentication
,
257 &options
.batch_mode
},
260 &options
.password_authentication
,
261 &options
.batch_mode
},
266 {NULL
, NULL
, NULL
, NULL
}
270 ssh_userauth2(const char *local_user
, const char *server_user
, char *host
,
271 Sensitive
*sensitive
)
276 if (options
.challenge_response_authentication
)
277 options
.kbd_interactive_authentication
= 1;
279 packet_start(SSH2_MSG_SERVICE_REQUEST
);
280 packet_put_cstring("ssh-userauth");
282 debug("SSH2_MSG_SERVICE_REQUEST sent");
284 type
= packet_read();
285 if (type
!= SSH2_MSG_SERVICE_ACCEPT
)
286 fatal("Server denied authentication request: %d", type
);
287 if (packet_remaining() > 0) {
288 char *reply
= packet_get_string(NULL
);
289 debug2("service_accept: %s", reply
);
292 debug2("buggy server: service_accept w/o service");
295 debug("SSH2_MSG_SERVICE_ACCEPT received");
297 if (options
.preferred_authentications
== NULL
)
298 options
.preferred_authentications
= authmethods_get();
300 /* setup authentication context */
301 memset(&authctxt
, 0, sizeof(authctxt
));
302 pubkey_prepare(&authctxt
);
303 authctxt
.server_user
= server_user
;
304 authctxt
.local_user
= local_user
;
305 authctxt
.host
= host
;
306 authctxt
.service
= "ssh-connection"; /* service name */
307 authctxt
.success
= 0;
308 authctxt
.method
= authmethod_lookup("none");
309 authctxt
.authlist
= NULL
;
310 authctxt
.methoddata
= NULL
;
311 authctxt
.sensitive
= sensitive
;
312 authctxt
.info_req_seen
= 0;
313 if (authctxt
.method
== NULL
)
314 fatal("ssh_userauth2: internal error: cannot send userauth none request");
316 /* initial userauth request */
317 userauth_none(&authctxt
);
319 dispatch_init(&input_userauth_error
);
320 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS
, &input_userauth_success
);
321 dispatch_set(SSH2_MSG_USERAUTH_FAILURE
, &input_userauth_failure
);
322 dispatch_set(SSH2_MSG_USERAUTH_BANNER
, &input_userauth_banner
);
323 dispatch_run(DISPATCH_BLOCK
, &authctxt
.success
, &authctxt
); /* loop until success */
325 pubkey_cleanup(&authctxt
);
326 dispatch_range(SSH2_MSG_USERAUTH_MIN
, SSH2_MSG_USERAUTH_MAX
, NULL
);
328 debug("Authentication succeeded (%s).", authctxt
.method
->name
);
332 userauth(Authctxt
*authctxt
, char *authlist
)
334 if (authctxt
->methoddata
) {
335 xfree(authctxt
->methoddata
);
336 authctxt
->methoddata
= NULL
;
338 if (authlist
== NULL
) {
339 authlist
= authctxt
->authlist
;
341 if (authctxt
->authlist
)
342 xfree(authctxt
->authlist
);
343 authctxt
->authlist
= authlist
;
346 Authmethod
*method
= authmethod_get(authlist
);
348 fatal("Permission denied (%s).", authlist
);
349 authctxt
->method
= method
;
351 /* reset the per method handler */
352 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN
,
353 SSH2_MSG_USERAUTH_PER_METHOD_MAX
, NULL
);
355 /* and try new method */
356 if (method
->userauth(authctxt
) != 0) {
357 debug2("we sent a %s packet, wait for reply", method
->name
);
360 debug2("we did not send a packet, disable method");
361 method
->enabled
= NULL
;
367 input_userauth_error(int type
, u_int32_t seq
, void *ctxt
)
369 fatal("input_userauth_error: bad message during authentication: "
374 input_userauth_banner(int type
, u_int32_t seq
, void *ctxt
)
378 debug3("input_userauth_banner");
379 msg
= packet_get_string(NULL
);
380 lang
= packet_get_string(NULL
);
381 if (options
.log_level
>= SYSLOG_LEVEL_INFO
)
382 fprintf(stderr
, "%s", msg
);
388 input_userauth_success(int type
, u_int32_t seq
, void *ctxt
)
390 Authctxt
*authctxt
= ctxt
;
391 if (authctxt
== NULL
)
392 fatal("input_userauth_success: no authentication context");
393 if (authctxt
->authlist
) {
394 xfree(authctxt
->authlist
);
395 authctxt
->authlist
= NULL
;
397 if (authctxt
->methoddata
) {
398 xfree(authctxt
->methoddata
);
399 authctxt
->methoddata
= NULL
;
401 authctxt
->success
= 1; /* break out */
405 input_userauth_failure(int type
, u_int32_t seq
, void *ctxt
)
407 Authctxt
*authctxt
= ctxt
;
408 char *authlist
= NULL
;
411 if (authctxt
== NULL
)
412 fatal("input_userauth_failure: no authentication context");
414 authlist
= packet_get_string(NULL
);
415 partial
= packet_get_char();
419 logit("Authenticated with partial success.");
420 debug("Authentications that can continue: %s", authlist
);
422 userauth(authctxt
, authlist
);
425 input_userauth_pk_ok(int type
, u_int32_t seq
, void *ctxt
)
427 Authctxt
*authctxt
= ctxt
;
431 int pktype
, sent
= 0;
436 if (authctxt
== NULL
)
437 fatal("input_userauth_pk_ok: no authentication context");
438 if (datafellows
& SSH_BUG_PKOK
) {
439 /* this is similar to SSH_BUG_PKAUTH */
440 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
441 pkblob
= packet_get_string(&blen
);
443 buffer_append(&b
, pkblob
, blen
);
444 pkalg
= buffer_get_string(&b
, &alen
);
447 pkalg
= packet_get_string(&alen
);
448 pkblob
= packet_get_string(&blen
);
452 debug("Server accepts key: pkalg %s blen %u", pkalg
, blen
);
454 if ((pktype
= key_type_from_name(pkalg
)) == KEY_UNSPEC
) {
455 debug("unknown pkalg %s", pkalg
);
458 if ((key
= key_from_blob(pkblob
, blen
)) == NULL
) {
459 debug("no key from blob. pkalg %s", pkalg
);
462 if (key
->type
!= pktype
) {
463 error("input_userauth_pk_ok: type mismatch "
464 "for decoded key (received %d, expected %d)",
468 fp
= key_fingerprint(key
, SSH_FP_MD5
, SSH_FP_HEX
);
469 debug2("input_userauth_pk_ok: fp %s", fp
);
473 * search keys in the reverse order, because last candidate has been
474 * moved to the end of the queue. this also avoids confusion by
477 TAILQ_FOREACH_REVERSE(id
, &authctxt
->keys
, idlist
, next
) {
478 if (key_equal(key
, id
->key
)) {
479 sent
= sign_and_send_pubkey(authctxt
, id
);
489 /* try another method if we did not send a packet */
491 userauth(authctxt
, NULL
);
496 userauth_gssapi(Authctxt
*authctxt
)
498 Gssctxt
*gssctxt
= NULL
;
499 static gss_OID_set gss_supported
= NULL
;
500 static u_int mech
= 0;
504 /* Try one GSSAPI method at a time, rather than sending them all at
507 if (gss_supported
== NULL
)
508 gss_indicate_mechs(&min
, &gss_supported
);
510 /* Check to see if the mechanism is usable before we offer it */
511 while (mech
< gss_supported
->count
&& !ok
) {
512 /* My DER encoding requires length<128 */
513 if (gss_supported
->elements
[mech
].length
< 128 &&
514 ssh_gssapi_check_mechanism(&gssctxt
,
515 &gss_supported
->elements
[mech
], authctxt
->host
)) {
516 ok
= 1; /* Mechanism works */
525 authctxt
->methoddata
=(void *)gssctxt
;
527 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
528 packet_put_cstring(authctxt
->server_user
);
529 packet_put_cstring(authctxt
->service
);
530 packet_put_cstring(authctxt
->method
->name
);
534 packet_put_int((gss_supported
->elements
[mech
].length
) + 2);
535 packet_put_char(SSH_GSS_OIDTYPE
);
536 packet_put_char(gss_supported
->elements
[mech
].length
);
537 packet_put_raw(gss_supported
->elements
[mech
].elements
,
538 gss_supported
->elements
[mech
].length
);
542 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE
, &input_gssapi_response
);
543 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN
, &input_gssapi_token
);
544 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR
, &input_gssapi_error
);
545 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK
, &input_gssapi_errtok
);
547 mech
++; /* Move along to next candidate */
553 process_gssapi_token(void *ctxt
, gss_buffer_t recv_tok
)
555 Authctxt
*authctxt
= ctxt
;
556 Gssctxt
*gssctxt
= authctxt
->methoddata
;
557 gss_buffer_desc send_tok
= GSS_C_EMPTY_BUFFER
;
558 gss_buffer_desc mic
= GSS_C_EMPTY_BUFFER
;
559 gss_buffer_desc gssbuf
;
560 OM_uint32 status
, ms
, flags
;
563 status
= ssh_gssapi_init_ctx(gssctxt
, options
.gss_deleg_creds
,
564 recv_tok
, &send_tok
, &flags
);
566 if (send_tok
.length
> 0) {
567 if (GSS_ERROR(status
))
568 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK
);
570 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN
);
572 packet_put_string(send_tok
.value
, send_tok
.length
);
574 gss_release_buffer(&ms
, &send_tok
);
577 if (status
== GSS_S_COMPLETE
) {
578 /* send either complete or MIC, depending on mechanism */
579 if (!(flags
& GSS_C_INTEG_FLAG
)) {
580 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE
);
583 ssh_gssapi_buildmic(&b
, authctxt
->server_user
,
584 authctxt
->service
, "gssapi-with-mic");
586 gssbuf
.value
= buffer_ptr(&b
);
587 gssbuf
.length
= buffer_len(&b
);
589 status
= ssh_gssapi_sign(gssctxt
, &gssbuf
, &mic
);
591 if (!GSS_ERROR(status
)) {
592 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC
);
593 packet_put_string(mic
.value
, mic
.length
);
599 gss_release_buffer(&ms
, &mic
);
607 input_gssapi_response(int type
, u_int32_t plen
, void *ctxt
)
609 Authctxt
*authctxt
= ctxt
;
614 if (authctxt
== NULL
)
615 fatal("input_gssapi_response: no authentication context");
616 gssctxt
= authctxt
->methoddata
;
619 oidv
= packet_get_string(&oidlen
);
622 oidv
[0] != SSH_GSS_OIDTYPE
||
623 oidv
[1] != oidlen
- 2) {
625 debug("Badly encoded mechanism OID received");
626 userauth(authctxt
, NULL
);
630 if (!ssh_gssapi_check_oid(gssctxt
, oidv
+ 2, oidlen
- 2))
631 fatal("Server returned different OID than expected");
637 if (GSS_ERROR(process_gssapi_token(ctxt
, GSS_C_NO_BUFFER
))) {
638 /* Start again with next method on list */
639 debug("Trying to start again");
640 userauth(authctxt
, NULL
);
646 input_gssapi_token(int type
, u_int32_t plen
, void *ctxt
)
648 Authctxt
*authctxt
= ctxt
;
649 gss_buffer_desc recv_tok
;
653 if (authctxt
== NULL
)
654 fatal("input_gssapi_response: no authentication context");
656 recv_tok
.value
= packet_get_string(&slen
);
657 recv_tok
.length
= slen
; /* safe typecast */
661 status
= process_gssapi_token(ctxt
, &recv_tok
);
663 xfree(recv_tok
.value
);
665 if (GSS_ERROR(status
)) {
666 /* Start again with the next method in the list */
667 userauth(authctxt
, NULL
);
673 input_gssapi_errtok(int type
, u_int32_t plen
, void *ctxt
)
675 Authctxt
*authctxt
= ctxt
;
677 gss_buffer_desc send_tok
= GSS_C_EMPTY_BUFFER
;
678 gss_buffer_desc recv_tok
;
679 OM_uint32 status
, ms
;
682 if (authctxt
== NULL
)
683 fatal("input_gssapi_response: no authentication context");
684 gssctxt
= authctxt
->methoddata
;
686 recv_tok
.value
= packet_get_string(&len
);
687 recv_tok
.length
= len
;
691 /* Stick it into GSSAPI and see what it says */
692 status
= ssh_gssapi_init_ctx(gssctxt
, options
.gss_deleg_creds
,
693 &recv_tok
, &send_tok
, NULL
);
695 xfree(recv_tok
.value
);
696 gss_release_buffer(&ms
, &send_tok
);
698 /* Server will be returning a failed packet after this one */
702 input_gssapi_error(int type
, u_int32_t plen
, void *ctxt
)
708 maj
=packet_get_int();
709 min
=packet_get_int();
710 msg
=packet_get_string(NULL
);
711 lang
=packet_get_string(NULL
);
715 debug("Server GSSAPI Error:\n%s", msg
);
722 userauth_none(Authctxt
*authctxt
)
724 /* initial userauth request */
725 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
726 packet_put_cstring(authctxt
->server_user
);
727 packet_put_cstring(authctxt
->service
);
728 packet_put_cstring(authctxt
->method
->name
);
734 userauth_passwd(Authctxt
*authctxt
)
736 static int attempt
= 0;
740 if (attempt
++ >= options
.number_of_password_prompts
)
744 error("Permission denied, please try again.");
746 snprintf(prompt
, sizeof(prompt
), "%.30s@%.128s's password: ",
747 authctxt
->server_user
, authctxt
->host
);
748 password
= read_passphrase(prompt
, 0);
749 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
750 packet_put_cstring(authctxt
->server_user
);
751 packet_put_cstring(authctxt
->service
);
752 packet_put_cstring(authctxt
->method
->name
);
754 packet_put_cstring(password
);
755 memset(password
, 0, strlen(password
));
757 packet_add_padding(64);
760 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ
,
761 &input_userauth_passwd_changereq
);
766 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
769 input_userauth_passwd_changereq(int type
, u_int32_t seqnr
, void *ctxt
)
771 Authctxt
*authctxt
= ctxt
;
772 char *info
, *lang
, *password
= NULL
, *retype
= NULL
;
775 debug2("input_userauth_passwd_changereq");
777 if (authctxt
== NULL
)
778 fatal("input_userauth_passwd_changereq: "
779 "no authentication context");
781 info
= packet_get_string(NULL
);
782 lang
= packet_get_string(NULL
);
783 if (strlen(info
) > 0)
787 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
788 packet_put_cstring(authctxt
->server_user
);
789 packet_put_cstring(authctxt
->service
);
790 packet_put_cstring(authctxt
->method
->name
);
791 packet_put_char(1); /* additional info */
792 snprintf(prompt
, sizeof(prompt
),
793 "Enter %.30s@%.128s's old password: ",
794 authctxt
->server_user
, authctxt
->host
);
795 password
= read_passphrase(prompt
, 0);
796 packet_put_cstring(password
);
797 memset(password
, 0, strlen(password
));
800 while (password
== NULL
) {
801 snprintf(prompt
, sizeof(prompt
),
802 "Enter %.30s@%.128s's new password: ",
803 authctxt
->server_user
, authctxt
->host
);
804 password
= read_passphrase(prompt
, RP_ALLOW_EOF
);
805 if (password
== NULL
) {
809 snprintf(prompt
, sizeof(prompt
),
810 "Retype %.30s@%.128s's new password: ",
811 authctxt
->server_user
, authctxt
->host
);
812 retype
= read_passphrase(prompt
, 0);
813 if (strcmp(password
, retype
) != 0) {
814 memset(password
, 0, strlen(password
));
816 logit("Mismatch; try again, EOF to quit.");
819 memset(retype
, 0, strlen(retype
));
822 packet_put_cstring(password
);
823 memset(password
, 0, strlen(password
));
825 packet_add_padding(64);
828 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ
,
829 &input_userauth_passwd_changereq
);
833 identity_sign(Identity
*id
, u_char
**sigp
, u_int
*lenp
,
834 u_char
*data
, u_int datalen
)
839 /* the agent supports this key */
841 return (ssh_agent_sign(id
->ac
, id
->key
, sigp
, lenp
,
844 * we have already loaded the private key or
845 * the private key is stored in external hardware
847 if (id
->isprivate
|| (id
->key
->flags
& KEY_FLAG_EXT
))
848 return (key_sign(id
->key
, sigp
, lenp
, data
, datalen
));
849 /* load the private key from the file */
850 if ((prv
= load_identity_file(id
->filename
)) == NULL
)
852 ret
= key_sign(prv
, sigp
, lenp
, data
, datalen
);
858 sign_and_send_pubkey(Authctxt
*authctxt
, Identity
*id
)
861 u_char
*blob
, *signature
;
867 debug3("sign_and_send_pubkey");
869 if (key_to_blob(id
->key
, &blob
, &bloblen
) == 0) {
870 /* we cannot handle this key */
871 debug3("sign_and_send_pubkey: cannot handle key");
874 /* data to be signed */
876 if (datafellows
& SSH_OLD_SESSIONID
) {
877 buffer_append(&b
, session_id2
, session_id2_len
);
878 skip
= session_id2_len
;
880 buffer_put_string(&b
, session_id2
, session_id2_len
);
881 skip
= buffer_len(&b
);
883 buffer_put_char(&b
, SSH2_MSG_USERAUTH_REQUEST
);
884 buffer_put_cstring(&b
, authctxt
->server_user
);
885 buffer_put_cstring(&b
,
886 datafellows
& SSH_BUG_PKSERVICE
?
889 if (datafellows
& SSH_BUG_PKAUTH
) {
890 buffer_put_char(&b
, have_sig
);
892 buffer_put_cstring(&b
, authctxt
->method
->name
);
893 buffer_put_char(&b
, have_sig
);
894 buffer_put_cstring(&b
, key_ssh_name(id
->key
));
896 buffer_put_string(&b
, blob
, bloblen
);
898 /* generate signature */
899 ret
= identity_sign(id
, &signature
, &slen
,
900 buffer_ptr(&b
), buffer_len(&b
));
909 if (datafellows
& SSH_BUG_PKSERVICE
) {
911 buffer_append(&b
, session_id2
, session_id2_len
);
912 skip
= session_id2_len
;
913 buffer_put_char(&b
, SSH2_MSG_USERAUTH_REQUEST
);
914 buffer_put_cstring(&b
, authctxt
->server_user
);
915 buffer_put_cstring(&b
, authctxt
->service
);
916 buffer_put_cstring(&b
, authctxt
->method
->name
);
917 buffer_put_char(&b
, have_sig
);
918 if (!(datafellows
& SSH_BUG_PKAUTH
))
919 buffer_put_cstring(&b
, key_ssh_name(id
->key
));
920 buffer_put_string(&b
, blob
, bloblen
);
924 /* append signature */
925 buffer_put_string(&b
, signature
, slen
);
928 /* skip session id and packet type */
929 if (buffer_len(&b
) < skip
+ 1)
930 fatal("userauth_pubkey: internal error");
931 buffer_consume(&b
, skip
+ 1);
933 /* put remaining data from buffer into packet */
934 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
935 packet_put_raw(buffer_ptr(&b
), buffer_len(&b
));
943 send_pubkey_test(Authctxt
*authctxt
, Identity
*id
)
946 u_int bloblen
, have_sig
= 0;
948 debug3("send_pubkey_test");
950 if (key_to_blob(id
->key
, &blob
, &bloblen
) == 0) {
951 /* we cannot handle this key */
952 debug3("send_pubkey_test: cannot handle key");
955 /* register callback for USERAUTH_PK_OK message */
956 dispatch_set(SSH2_MSG_USERAUTH_PK_OK
, &input_userauth_pk_ok
);
958 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
959 packet_put_cstring(authctxt
->server_user
);
960 packet_put_cstring(authctxt
->service
);
961 packet_put_cstring(authctxt
->method
->name
);
962 packet_put_char(have_sig
);
963 if (!(datafellows
& SSH_BUG_PKAUTH
))
964 packet_put_cstring(key_ssh_name(id
->key
));
965 packet_put_string(blob
, bloblen
);
972 load_identity_file(char *filename
)
975 char prompt
[300], *passphrase
;
976 int perm_ok
, quit
, i
;
979 if (stat(filename
, &st
) < 0) {
980 debug3("no such identity: %s", filename
);
983 private = key_load_private_type(KEY_UNSPEC
, filename
, "", NULL
, &perm_ok
);
986 if (private == NULL
) {
987 if (options
.batch_mode
)
989 snprintf(prompt
, sizeof prompt
,
990 "Enter passphrase for key '%.100s': ", filename
);
991 for (i
= 0; i
< options
.number_of_password_prompts
; i
++) {
992 passphrase
= read_passphrase(prompt
, 0);
993 if (strcmp(passphrase
, "") != 0) {
994 private = key_load_private_type(KEY_UNSPEC
,
995 filename
, passphrase
, NULL
, NULL
);
998 debug2("no passphrase given, try next key");
1001 memset(passphrase
, 0, strlen(passphrase
));
1003 if (private != NULL
|| quit
)
1005 debug2("bad passphrase given, try again...");
1012 * try keys in the following order:
1013 * 1. agent keys that are found in the config file
1014 * 2. other agent keys
1015 * 3. keys that are only listed in the config file
1018 pubkey_prepare(Authctxt
*authctxt
)
1021 Idlist agent
, files
, *preferred
;
1023 AuthenticationConnection
*ac
;
1027 TAILQ_INIT(&agent
); /* keys from the agent */
1028 TAILQ_INIT(&files
); /* keys from the config file */
1029 preferred
= &authctxt
->keys
;
1030 TAILQ_INIT(preferred
); /* preferred order of keys */
1032 /* list of keys stored in the filesystem */
1033 for (i
= 0; i
< options
.num_identity_files
; i
++) {
1034 key
= options
.identity_keys
[i
];
1035 if (key
&& key
->type
== KEY_RSA1
)
1037 options
.identity_keys
[i
] = NULL
;
1038 id
= xcalloc(1, sizeof(*id
));
1040 id
->filename
= xstrdup(options
.identity_files
[i
]);
1041 TAILQ_INSERT_TAIL(&files
, id
, next
);
1043 /* list of keys supported by the agent */
1044 if ((ac
= ssh_get_authentication_connection())) {
1045 for (key
= ssh_get_first_identity(ac
, &comment
, 2);
1047 key
= ssh_get_next_identity(ac
, &comment
, 2)) {
1049 TAILQ_FOREACH(id
, &files
, next
) {
1050 /* agent keys from the config file are preferred */
1051 if (key_equal(key
, id
->key
)) {
1054 TAILQ_REMOVE(&files
, id
, next
);
1055 TAILQ_INSERT_TAIL(preferred
, id
, next
);
1061 if (!found
&& !options
.identities_only
) {
1062 id
= xcalloc(1, sizeof(*id
));
1064 id
->filename
= comment
;
1066 TAILQ_INSERT_TAIL(&agent
, id
, next
);
1069 /* append remaining agent keys */
1070 for (id
= TAILQ_FIRST(&agent
); id
; id
= TAILQ_FIRST(&agent
)) {
1071 TAILQ_REMOVE(&agent
, id
, next
);
1072 TAILQ_INSERT_TAIL(preferred
, id
, next
);
1074 authctxt
->agent
= ac
;
1076 /* append remaining keys from the config file */
1077 for (id
= TAILQ_FIRST(&files
); id
; id
= TAILQ_FIRST(&files
)) {
1078 TAILQ_REMOVE(&files
, id
, next
);
1079 TAILQ_INSERT_TAIL(preferred
, id
, next
);
1081 TAILQ_FOREACH(id
, preferred
, next
) {
1082 debug2("key: %s (%p)", id
->filename
, id
->key
);
1087 pubkey_cleanup(Authctxt
*authctxt
)
1091 if (authctxt
->agent
!= NULL
)
1092 ssh_close_authentication_connection(authctxt
->agent
);
1093 for (id
= TAILQ_FIRST(&authctxt
->keys
); id
;
1094 id
= TAILQ_FIRST(&authctxt
->keys
)) {
1095 TAILQ_REMOVE(&authctxt
->keys
, id
, next
);
1099 xfree(id
->filename
);
1105 userauth_pubkey(Authctxt
*authctxt
)
1110 while ((id
= TAILQ_FIRST(&authctxt
->keys
))) {
1113 /* move key to the end of the queue */
1114 TAILQ_REMOVE(&authctxt
->keys
, id
, next
);
1115 TAILQ_INSERT_TAIL(&authctxt
->keys
, id
, next
);
1117 * send a test message if we have the public key. for
1118 * encrypted keys we cannot do this and have to load the
1119 * private key instead
1121 if (id
->key
&& id
->key
->type
!= KEY_RSA1
) {
1122 debug("Offering public key: %s", id
->filename
);
1123 sent
= send_pubkey_test(authctxt
, id
);
1124 } else if (id
->key
== NULL
) {
1125 debug("Trying private key: %s", id
->filename
);
1126 id
->key
= load_identity_file(id
->filename
);
1127 if (id
->key
!= NULL
) {
1129 sent
= sign_and_send_pubkey(authctxt
, id
);
1141 * Send userauth request message specifying keyboard-interactive method.
1144 userauth_kbdint(Authctxt
*authctxt
)
1146 static int attempt
= 0;
1148 if (attempt
++ >= options
.number_of_password_prompts
)
1150 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1151 if (attempt
> 1 && !authctxt
->info_req_seen
) {
1152 debug3("userauth_kbdint: disable: no info_req_seen");
1153 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST
, NULL
);
1157 debug2("userauth_kbdint");
1158 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
1159 packet_put_cstring(authctxt
->server_user
);
1160 packet_put_cstring(authctxt
->service
);
1161 packet_put_cstring(authctxt
->method
->name
);
1162 packet_put_cstring(""); /* lang */
1163 packet_put_cstring(options
.kbd_interactive_devices
?
1164 options
.kbd_interactive_devices
: "");
1167 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST
, &input_userauth_info_req
);
1172 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1175 input_userauth_info_req(int type
, u_int32_t seq
, void *ctxt
)
1177 Authctxt
*authctxt
= ctxt
;
1178 char *name
, *inst
, *lang
, *prompt
, *response
;
1179 u_int num_prompts
, i
;
1182 debug2("input_userauth_info_req");
1184 if (authctxt
== NULL
)
1185 fatal("input_userauth_info_req: no authentication context");
1187 authctxt
->info_req_seen
= 1;
1189 name
= packet_get_string(NULL
);
1190 inst
= packet_get_string(NULL
);
1191 lang
= packet_get_string(NULL
);
1192 if (strlen(name
) > 0)
1194 if (strlen(inst
) > 0)
1200 num_prompts
= packet_get_int();
1202 * Begin to build info response packet based on prompts requested.
1203 * We commit to providing the correct number of responses, so if
1204 * further on we run into a problem that prevents this, we have to
1205 * be sure and clean this up and send a correct error response.
1207 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE
);
1208 packet_put_int(num_prompts
);
1210 debug2("input_userauth_info_req: num_prompts %d", num_prompts
);
1211 for (i
= 0; i
< num_prompts
; i
++) {
1212 prompt
= packet_get_string(NULL
);
1213 echo
= packet_get_char();
1215 response
= read_passphrase(prompt
, echo
? RP_ECHO
: 0);
1217 packet_put_cstring(response
);
1218 memset(response
, 0, strlen(response
));
1222 packet_check_eom(); /* done with parsing incoming message. */
1224 packet_add_padding(64);
1229 ssh_keysign(Key
*key
, u_char
**sigp
, u_int
*lenp
,
1230 u_char
*data
, u_int datalen
)
1235 int to
[2], from
[2], status
, version
= 2;
1237 debug2("ssh_keysign called");
1239 if (stat(_PATH_SSH_KEY_SIGN
, &st
) < 0) {
1240 error("ssh_keysign: no installed: %s", strerror(errno
));
1243 if (fflush(stdout
) != 0)
1244 error("ssh_keysign: fflush: %s", strerror(errno
));
1246 error("ssh_keysign: pipe: %s", strerror(errno
));
1249 if (pipe(from
) < 0) {
1250 error("ssh_keysign: pipe: %s", strerror(errno
));
1253 if ((pid
= fork()) < 0) {
1254 error("ssh_keysign: fork: %s", strerror(errno
));
1258 permanently_drop_suid(getuid());
1260 if (dup2(from
[1], STDOUT_FILENO
) < 0)
1261 fatal("ssh_keysign: dup2: %s", strerror(errno
));
1263 if (dup2(to
[0], STDIN_FILENO
) < 0)
1264 fatal("ssh_keysign: dup2: %s", strerror(errno
));
1267 execl(_PATH_SSH_KEY_SIGN
, _PATH_SSH_KEY_SIGN
, (char *) 0);
1268 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN
,
1275 buffer_put_int(&b
, packet_get_connection_in()); /* send # of socket */
1276 buffer_put_string(&b
, data
, datalen
);
1277 if (ssh_msg_send(to
[1], version
, &b
) == -1)
1278 fatal("ssh_keysign: couldn't send request");
1280 if (ssh_msg_recv(from
[0], &b
) < 0) {
1281 error("ssh_keysign: no reply");
1288 while (waitpid(pid
, &status
, 0) < 0)
1292 if (buffer_get_char(&b
) != version
) {
1293 error("ssh_keysign: bad version");
1297 *sigp
= buffer_get_string(&b
, lenp
);
1304 userauth_hostbased(Authctxt
*authctxt
)
1306 Key
*private = NULL
;
1307 Sensitive
*sensitive
= authctxt
->sensitive
;
1309 u_char
*signature
, *blob
;
1310 char *chost
, *pkalg
, *p
;
1311 const char *service
;
1313 int ok
, i
, len
, found
= 0;
1315 /* check for a useful key */
1316 for (i
= 0; i
< sensitive
->nkeys
; i
++) {
1317 private = sensitive
->keys
[i
];
1318 if (private && private->type
!= KEY_RSA1
) {
1320 /* we take and free the key */
1321 sensitive
->keys
[i
] = NULL
;
1326 debug("No more client hostkeys for hostbased authentication.");
1329 if (key_to_blob(private, &blob
, &blen
) == 0) {
1333 /* figure out a name for the client host */
1334 p
= get_local_name(packet_get_connection_in());
1336 error("userauth_hostbased: cannot get local ipaddr/name");
1341 len
= strlen(p
) + 2;
1342 xasprintf(&chost
, "%s.", p
);
1343 debug2("userauth_hostbased: chost %s", chost
);
1346 service
= datafellows
& SSH_BUG_HBSERVICE
? "ssh-userauth" :
1348 pkalg
= xstrdup(key_ssh_name(private));
1350 /* construct data */
1351 buffer_put_string(&b
, session_id2
, session_id2_len
);
1352 buffer_put_char(&b
, SSH2_MSG_USERAUTH_REQUEST
);
1353 buffer_put_cstring(&b
, authctxt
->server_user
);
1354 buffer_put_cstring(&b
, service
);
1355 buffer_put_cstring(&b
, authctxt
->method
->name
);
1356 buffer_put_cstring(&b
, pkalg
);
1357 buffer_put_string(&b
, blob
, blen
);
1358 buffer_put_cstring(&b
, chost
);
1359 buffer_put_cstring(&b
, authctxt
->local_user
);
1363 if (sensitive
->external_keysign
)
1364 ok
= ssh_keysign(private, &signature
, &slen
,
1365 buffer_ptr(&b
), buffer_len(&b
));
1367 ok
= key_sign(private, &signature
, &slen
,
1368 buffer_ptr(&b
), buffer_len(&b
));
1372 error("key_sign failed");
1378 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
1379 packet_put_cstring(authctxt
->server_user
);
1380 packet_put_cstring(authctxt
->service
);
1381 packet_put_cstring(authctxt
->method
->name
);
1382 packet_put_cstring(pkalg
);
1383 packet_put_string(blob
, blen
);
1384 packet_put_cstring(chost
);
1385 packet_put_cstring(authctxt
->local_user
);
1386 packet_put_string(signature
, slen
);
1387 memset(signature
, 's', slen
);
1397 /* find auth method */
1400 * given auth method name, if configurable options permit this method fill
1401 * in auth_ident field and return true, otherwise return false.
1404 authmethod_is_enabled(Authmethod
*method
)
1408 /* return false if options indicate this method is disabled */
1409 if (method
->enabled
== NULL
|| *method
->enabled
== 0)
1411 /* return false if batch mode is enabled but method needs interactive mode */
1412 if (method
->batch_flag
!= NULL
&& *method
->batch_flag
!= 0)
1418 authmethod_lookup(const char *name
)
1420 Authmethod
*method
= NULL
;
1422 for (method
= authmethods
; method
->name
!= NULL
; method
++)
1423 if (strcmp(name
, method
->name
) == 0)
1425 debug2("Unrecognized authentication method name: %s", name
? name
: "NULL");
1429 /* XXX internal state */
1430 static Authmethod
*current
= NULL
;
1431 static char *supported
= NULL
;
1432 static char *preferred
= NULL
;
1435 * Given the authentication method list sent by the server, return the
1436 * next method we should try. If the server initially sends a nil list,
1437 * use a built-in default list.
1440 authmethod_get(char *authlist
)
1445 /* Use a suitable default if we're passed a nil list. */
1446 if (authlist
== NULL
|| strlen(authlist
) == 0)
1447 authlist
= options
.preferred_authentications
;
1449 if (supported
== NULL
|| strcmp(authlist
, supported
) != 0) {
1450 debug3("start over, passed a different list %s", authlist
);
1451 if (supported
!= NULL
)
1453 supported
= xstrdup(authlist
);
1454 preferred
= options
.preferred_authentications
;
1455 debug3("preferred %s", preferred
);
1457 } else if (current
!= NULL
&& authmethod_is_enabled(current
))
1461 if ((name
= match_list(preferred
, supported
, &next
)) == NULL
) {
1462 debug("No more authentication methods to try.");
1467 debug3("authmethod_lookup %s", name
);
1468 debug3("remaining preferred: %s", preferred
);
1469 if ((current
= authmethod_lookup(name
)) != NULL
&&
1470 authmethod_is_enabled(current
)) {
1471 debug3("authmethod_is_enabled %s", name
);
1472 debug("Next authentication method: %s", name
);
1479 authmethods_get(void)
1481 Authmethod
*method
= NULL
;
1486 for (method
= authmethods
; method
->name
!= NULL
; method
++) {
1487 if (authmethod_is_enabled(method
)) {
1488 if (buffer_len(&b
) > 0)
1489 buffer_append(&b
, ",", 1);
1490 buffer_append(&b
, method
->name
, strlen(method
->name
));
1493 buffer_append(&b
, "\0", 1);
1494 list
= xstrdup(buffer_ptr(&b
));