1 /* $OpenBSD: sshconnect2.c,v 1.172 2009/10/23 01:57:11 djm Exp $ */
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 * Copyright (c) 2008 Damien Miller. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/types.h>
30 #include <sys/socket.h>
42 #if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H)
46 #include "openbsd-compat/sys-queue.h"
57 #include "myproposal.h"
58 #include "sshconnect.h"
69 #include "pathnames.h"
79 extern char *client_version_string
;
80 extern char *server_version_string
;
81 extern Options options
;
87 u_char
*session_id2
= NULL
;
88 u_int session_id2_len
= 0;
91 struct sockaddr
*xxx_hostaddr
;
96 verify_host_key_callback(Key
*hostkey
)
98 if (verify_host_key(xxx_host
, xxx_hostaddr
, hostkey
) == -1)
99 fatal("Host key verification failed.");
104 ssh_kex2(char *host
, struct sockaddr
*hostaddr
)
109 xxx_hostaddr
= hostaddr
;
111 if (options
.ciphers
== (char *)-1) {
112 logit("No valid ciphers for protocol version 2 given, using defaults.");
113 options
.ciphers
= NULL
;
115 if (options
.ciphers
!= NULL
) {
116 myproposal
[PROPOSAL_ENC_ALGS_CTOS
] =
117 myproposal
[PROPOSAL_ENC_ALGS_STOC
] = options
.ciphers
;
119 myproposal
[PROPOSAL_ENC_ALGS_CTOS
] =
120 compat_cipher_proposal(myproposal
[PROPOSAL_ENC_ALGS_CTOS
]);
121 myproposal
[PROPOSAL_ENC_ALGS_STOC
] =
122 compat_cipher_proposal(myproposal
[PROPOSAL_ENC_ALGS_STOC
]);
123 if (options
.compression
) {
124 myproposal
[PROPOSAL_COMP_ALGS_CTOS
] =
125 myproposal
[PROPOSAL_COMP_ALGS_STOC
] = "zlib@openssh.com,zlib,none";
127 myproposal
[PROPOSAL_COMP_ALGS_CTOS
] =
128 myproposal
[PROPOSAL_COMP_ALGS_STOC
] = "none,zlib@openssh.com,zlib";
130 if (options
.macs
!= NULL
) {
131 myproposal
[PROPOSAL_MAC_ALGS_CTOS
] =
132 myproposal
[PROPOSAL_MAC_ALGS_STOC
] = options
.macs
;
134 if (options
.hostkeyalgorithms
!= NULL
)
135 myproposal
[PROPOSAL_SERVER_HOST_KEY_ALGS
] =
136 options
.hostkeyalgorithms
;
138 if (options
.rekey_limit
)
139 packet_set_rekey_limit((u_int32_t
)options
.rekey_limit
);
141 /* start key exchange */
142 kex
= kex_setup(myproposal
);
143 kex
->kex
[KEX_DH_GRP1_SHA1
] = kexdh_client
;
144 kex
->kex
[KEX_DH_GRP14_SHA1
] = kexdh_client
;
145 kex
->kex
[KEX_DH_GEX_SHA1
] = kexgex_client
;
146 kex
->kex
[KEX_DH_GEX_SHA256
] = kexgex_client
;
147 kex
->client_version_string
=client_version_string
;
148 kex
->server_version_string
=server_version_string
;
149 kex
->verify_host_key
=&verify_host_key_callback
;
153 dispatch_run(DISPATCH_BLOCK
, &kex
->done
, kex
);
155 session_id2
= kex
->session_id
;
156 session_id2_len
= kex
->session_id_len
;
159 /* send 1st encrypted/maced/compressed message */
160 packet_start(SSH2_MSG_IGNORE
);
161 packet_put_cstring("markus");
171 typedef struct Authctxt Authctxt
;
172 typedef struct Authmethod Authmethod
;
173 typedef struct identity Identity
;
174 typedef struct idlist Idlist
;
177 TAILQ_ENTRY(identity
) next
;
178 AuthenticationConnection
*ac
; /* set if agent supports key */
179 Key
*key
; /* public/private key */
180 char *filename
; /* comment for agent-only keys */
182 int isprivate
; /* key points to the private key */
184 TAILQ_HEAD(idlist
, identity
);
187 const char *server_user
;
188 const char *local_user
;
196 AuthenticationConnection
*agent
;
198 Sensitive
*sensitive
;
199 /* kbd-interactive */
205 char *name
; /* string to compare against server's list */
206 int (*userauth
)(Authctxt
*authctxt
);
207 void (*cleanup
)(Authctxt
*authctxt
);
208 int *enabled
; /* flag in option struct that enables method */
209 int *batch_flag
; /* flag in option struct that disables method */
212 void input_userauth_success(int, u_int32_t
, void *);
213 void input_userauth_success_unexpected(int, u_int32_t
, void *);
214 void input_userauth_failure(int, u_int32_t
, void *);
215 void input_userauth_banner(int, u_int32_t
, void *);
216 void input_userauth_error(int, u_int32_t
, void *);
217 void input_userauth_info_req(int, u_int32_t
, void *);
218 void input_userauth_pk_ok(int, u_int32_t
, void *);
219 void input_userauth_passwd_changereq(int, u_int32_t
, void *);
220 void input_userauth_jpake_server_step1(int, u_int32_t
, void *);
221 void input_userauth_jpake_server_step2(int, u_int32_t
, void *);
222 void input_userauth_jpake_server_confirm(int, u_int32_t
, void *);
224 int userauth_none(Authctxt
*);
225 int userauth_pubkey(Authctxt
*);
226 int userauth_passwd(Authctxt
*);
227 int userauth_kbdint(Authctxt
*);
228 int userauth_hostbased(Authctxt
*);
229 int userauth_jpake(Authctxt
*);
231 void userauth_jpake_cleanup(Authctxt
*);
234 int userauth_gssapi(Authctxt
*authctxt
);
235 void input_gssapi_response(int type
, u_int32_t
, void *);
236 void input_gssapi_token(int type
, u_int32_t
, void *);
237 void input_gssapi_hash(int type
, u_int32_t
, void *);
238 void input_gssapi_error(int, u_int32_t
, void *);
239 void input_gssapi_errtok(int, u_int32_t
, void *);
242 void userauth(Authctxt
*, char *);
244 static int sign_and_send_pubkey(Authctxt
*, Identity
*);
245 static void pubkey_prepare(Authctxt
*);
246 static void pubkey_cleanup(Authctxt
*);
247 static Key
*load_identity_file(char *);
249 static Authmethod
*authmethod_get(char *authlist
);
250 static Authmethod
*authmethod_lookup(const char *name
);
251 static char *authmethods_get(void);
253 Authmethod authmethods
[] = {
258 &options
.gss_authentication
,
264 &options
.hostbased_authentication
,
269 &options
.pubkey_authentication
,
272 {"jpake-01@openssh.com",
274 userauth_jpake_cleanup
,
275 &options
.zero_knowledge_password_authentication
,
276 &options
.batch_mode
},
278 {"keyboard-interactive",
281 &options
.kbd_interactive_authentication
,
282 &options
.batch_mode
},
286 &options
.password_authentication
,
287 &options
.batch_mode
},
293 {NULL
, NULL
, NULL
, NULL
, NULL
}
297 ssh_userauth2(const char *local_user
, const char *server_user
, char *host
,
298 Sensitive
*sensitive
)
303 if (options
.challenge_response_authentication
)
304 options
.kbd_interactive_authentication
= 1;
306 packet_start(SSH2_MSG_SERVICE_REQUEST
);
307 packet_put_cstring("ssh-userauth");
309 debug("SSH2_MSG_SERVICE_REQUEST sent");
311 type
= packet_read();
312 if (type
!= SSH2_MSG_SERVICE_ACCEPT
)
313 fatal("Server denied authentication request: %d", type
);
314 if (packet_remaining() > 0) {
315 char *reply
= packet_get_string(NULL
);
316 debug2("service_accept: %s", reply
);
319 debug2("buggy server: service_accept w/o service");
322 debug("SSH2_MSG_SERVICE_ACCEPT received");
324 if (options
.preferred_authentications
== NULL
)
325 options
.preferred_authentications
= authmethods_get();
327 /* setup authentication context */
328 memset(&authctxt
, 0, sizeof(authctxt
));
329 pubkey_prepare(&authctxt
);
330 authctxt
.server_user
= server_user
;
331 authctxt
.local_user
= local_user
;
332 authctxt
.host
= host
;
333 authctxt
.service
= "ssh-connection"; /* service name */
334 authctxt
.success
= 0;
335 authctxt
.method
= authmethod_lookup("none");
336 authctxt
.authlist
= NULL
;
337 authctxt
.methoddata
= NULL
;
338 authctxt
.sensitive
= sensitive
;
339 authctxt
.info_req_seen
= 0;
340 if (authctxt
.method
== NULL
)
341 fatal("ssh_userauth2: internal error: cannot send userauth none request");
343 /* initial userauth request */
344 userauth_none(&authctxt
);
346 dispatch_init(&input_userauth_error
);
347 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS
, &input_userauth_success
);
348 dispatch_set(SSH2_MSG_USERAUTH_FAILURE
, &input_userauth_failure
);
349 dispatch_set(SSH2_MSG_USERAUTH_BANNER
, &input_userauth_banner
);
350 dispatch_run(DISPATCH_BLOCK
, &authctxt
.success
, &authctxt
); /* loop until success */
352 pubkey_cleanup(&authctxt
);
353 dispatch_range(SSH2_MSG_USERAUTH_MIN
, SSH2_MSG_USERAUTH_MAX
, NULL
);
355 debug("Authentication succeeded (%s).", authctxt
.method
->name
);
359 userauth(Authctxt
*authctxt
, char *authlist
)
361 if (authctxt
->method
!= NULL
&& authctxt
->method
->cleanup
!= NULL
)
362 authctxt
->method
->cleanup(authctxt
);
364 if (authctxt
->methoddata
) {
365 xfree(authctxt
->methoddata
);
366 authctxt
->methoddata
= NULL
;
368 if (authlist
== NULL
) {
369 authlist
= authctxt
->authlist
;
371 if (authctxt
->authlist
)
372 xfree(authctxt
->authlist
);
373 authctxt
->authlist
= authlist
;
376 Authmethod
*method
= authmethod_get(authlist
);
378 fatal("Permission denied (%s).", authlist
);
379 authctxt
->method
= method
;
381 /* reset the per method handler */
382 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN
,
383 SSH2_MSG_USERAUTH_PER_METHOD_MAX
, NULL
);
385 /* and try new method */
386 if (method
->userauth(authctxt
) != 0) {
387 debug2("we sent a %s packet, wait for reply", method
->name
);
390 debug2("we did not send a packet, disable method");
391 method
->enabled
= NULL
;
398 input_userauth_error(int type
, u_int32_t seq
, void *ctxt
)
400 fatal("input_userauth_error: bad message during authentication: "
406 input_userauth_banner(int type
, u_int32_t seq
, void *ctxt
)
408 char *msg
, *raw
, *lang
;
411 debug3("input_userauth_banner");
412 raw
= packet_get_string(&len
);
413 lang
= packet_get_string(NULL
);
414 if (len
> 0 && options
.log_level
>= SYSLOG_LEVEL_INFO
) {
417 msg
= xmalloc(len
* 4 + 1); /* max expansion from strnvis() */
418 strnvis(msg
, raw
, len
* 4 + 1, VIS_SAFE
|VIS_OCTAL
);
419 fprintf(stderr
, "%s", msg
);
428 input_userauth_success(int type
, u_int32_t seq
, void *ctxt
)
430 Authctxt
*authctxt
= ctxt
;
432 if (authctxt
== NULL
)
433 fatal("input_userauth_success: no authentication context");
434 if (authctxt
->authlist
) {
435 xfree(authctxt
->authlist
);
436 authctxt
->authlist
= NULL
;
438 if (authctxt
->method
!= NULL
&& authctxt
->method
->cleanup
!= NULL
)
439 authctxt
->method
->cleanup(authctxt
);
440 if (authctxt
->methoddata
) {
441 xfree(authctxt
->methoddata
);
442 authctxt
->methoddata
= NULL
;
444 authctxt
->success
= 1; /* break out */
448 input_userauth_success_unexpected(int type
, u_int32_t seq
, void *ctxt
)
450 Authctxt
*authctxt
= ctxt
;
452 if (authctxt
== NULL
)
453 fatal("%s: no authentication context", __func__
);
455 fatal("Unexpected authentication success during %s.",
456 authctxt
->method
->name
);
461 input_userauth_failure(int type
, u_int32_t seq
, void *ctxt
)
463 Authctxt
*authctxt
= ctxt
;
464 char *authlist
= NULL
;
467 if (authctxt
== NULL
)
468 fatal("input_userauth_failure: no authentication context");
470 authlist
= packet_get_string(NULL
);
471 partial
= packet_get_char();
475 logit("Authenticated with partial success.");
476 debug("Authentications that can continue: %s", authlist
);
478 userauth(authctxt
, authlist
);
483 input_userauth_pk_ok(int type
, u_int32_t seq
, void *ctxt
)
485 Authctxt
*authctxt
= ctxt
;
489 int pktype
, sent
= 0;
494 if (authctxt
== NULL
)
495 fatal("input_userauth_pk_ok: no authentication context");
496 if (datafellows
& SSH_BUG_PKOK
) {
497 /* this is similar to SSH_BUG_PKAUTH */
498 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
499 pkblob
= packet_get_string(&blen
);
501 buffer_append(&b
, pkblob
, blen
);
502 pkalg
= buffer_get_string(&b
, &alen
);
505 pkalg
= packet_get_string(&alen
);
506 pkblob
= packet_get_string(&blen
);
510 debug("Server accepts key: pkalg %s blen %u", pkalg
, blen
);
512 if ((pktype
= key_type_from_name(pkalg
)) == KEY_UNSPEC
) {
513 debug("unknown pkalg %s", pkalg
);
516 if ((key
= key_from_blob(pkblob
, blen
)) == NULL
) {
517 debug("no key from blob. pkalg %s", pkalg
);
520 if (key
->type
!= pktype
) {
521 error("input_userauth_pk_ok: type mismatch "
522 "for decoded key (received %d, expected %d)",
526 fp
= key_fingerprint(key
, SSH_FP_MD5
, SSH_FP_HEX
);
527 debug2("input_userauth_pk_ok: fp %s", fp
);
531 * search keys in the reverse order, because last candidate has been
532 * moved to the end of the queue. this also avoids confusion by
535 TAILQ_FOREACH_REVERSE(id
, &authctxt
->keys
, idlist
, next
) {
536 if (key_equal(key
, id
->key
)) {
537 sent
= sign_and_send_pubkey(authctxt
, id
);
547 /* try another method if we did not send a packet */
549 userauth(authctxt
, NULL
);
554 userauth_gssapi(Authctxt
*authctxt
)
556 Gssctxt
*gssctxt
= NULL
;
557 static gss_OID_set gss_supported
= NULL
;
558 static u_int mech
= 0;
562 /* Try one GSSAPI method at a time, rather than sending them all at
565 if (gss_supported
== NULL
)
566 gss_indicate_mechs(&min
, &gss_supported
);
568 /* Check to see if the mechanism is usable before we offer it */
569 while (mech
< gss_supported
->count
&& !ok
) {
570 /* My DER encoding requires length<128 */
571 if (gss_supported
->elements
[mech
].length
< 128 &&
572 ssh_gssapi_check_mechanism(&gssctxt
,
573 &gss_supported
->elements
[mech
], authctxt
->host
)) {
574 ok
= 1; /* Mechanism works */
583 authctxt
->methoddata
=(void *)gssctxt
;
585 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
586 packet_put_cstring(authctxt
->server_user
);
587 packet_put_cstring(authctxt
->service
);
588 packet_put_cstring(authctxt
->method
->name
);
592 packet_put_int((gss_supported
->elements
[mech
].length
) + 2);
593 packet_put_char(SSH_GSS_OIDTYPE
);
594 packet_put_char(gss_supported
->elements
[mech
].length
);
595 packet_put_raw(gss_supported
->elements
[mech
].elements
,
596 gss_supported
->elements
[mech
].length
);
600 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE
, &input_gssapi_response
);
601 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN
, &input_gssapi_token
);
602 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR
, &input_gssapi_error
);
603 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK
, &input_gssapi_errtok
);
605 mech
++; /* Move along to next candidate */
611 process_gssapi_token(void *ctxt
, gss_buffer_t recv_tok
)
613 Authctxt
*authctxt
= ctxt
;
614 Gssctxt
*gssctxt
= authctxt
->methoddata
;
615 gss_buffer_desc send_tok
= GSS_C_EMPTY_BUFFER
;
616 gss_buffer_desc mic
= GSS_C_EMPTY_BUFFER
;
617 gss_buffer_desc gssbuf
;
618 OM_uint32 status
, ms
, flags
;
621 status
= ssh_gssapi_init_ctx(gssctxt
, options
.gss_deleg_creds
,
622 recv_tok
, &send_tok
, &flags
);
624 if (send_tok
.length
> 0) {
625 if (GSS_ERROR(status
))
626 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK
);
628 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN
);
630 packet_put_string(send_tok
.value
, send_tok
.length
);
632 gss_release_buffer(&ms
, &send_tok
);
635 if (status
== GSS_S_COMPLETE
) {
636 /* send either complete or MIC, depending on mechanism */
637 if (!(flags
& GSS_C_INTEG_FLAG
)) {
638 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE
);
641 ssh_gssapi_buildmic(&b
, authctxt
->server_user
,
642 authctxt
->service
, "gssapi-with-mic");
644 gssbuf
.value
= buffer_ptr(&b
);
645 gssbuf
.length
= buffer_len(&b
);
647 status
= ssh_gssapi_sign(gssctxt
, &gssbuf
, &mic
);
649 if (!GSS_ERROR(status
)) {
650 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC
);
651 packet_put_string(mic
.value
, mic
.length
);
657 gss_release_buffer(&ms
, &mic
);
666 input_gssapi_response(int type
, u_int32_t plen
, void *ctxt
)
668 Authctxt
*authctxt
= ctxt
;
673 if (authctxt
== NULL
)
674 fatal("input_gssapi_response: no authentication context");
675 gssctxt
= authctxt
->methoddata
;
678 oidv
= packet_get_string(&oidlen
);
681 oidv
[0] != SSH_GSS_OIDTYPE
||
682 oidv
[1] != oidlen
- 2) {
684 debug("Badly encoded mechanism OID received");
685 userauth(authctxt
, NULL
);
689 if (!ssh_gssapi_check_oid(gssctxt
, oidv
+ 2, oidlen
- 2))
690 fatal("Server returned different OID than expected");
696 if (GSS_ERROR(process_gssapi_token(ctxt
, GSS_C_NO_BUFFER
))) {
697 /* Start again with next method on list */
698 debug("Trying to start again");
699 userauth(authctxt
, NULL
);
706 input_gssapi_token(int type
, u_int32_t plen
, void *ctxt
)
708 Authctxt
*authctxt
= ctxt
;
709 gss_buffer_desc recv_tok
;
713 if (authctxt
== NULL
)
714 fatal("input_gssapi_response: no authentication context");
716 recv_tok
.value
= packet_get_string(&slen
);
717 recv_tok
.length
= slen
; /* safe typecast */
721 status
= process_gssapi_token(ctxt
, &recv_tok
);
723 xfree(recv_tok
.value
);
725 if (GSS_ERROR(status
)) {
726 /* Start again with the next method in the list */
727 userauth(authctxt
, NULL
);
734 input_gssapi_errtok(int type
, u_int32_t plen
, void *ctxt
)
736 Authctxt
*authctxt
= ctxt
;
738 gss_buffer_desc send_tok
= GSS_C_EMPTY_BUFFER
;
739 gss_buffer_desc recv_tok
;
740 OM_uint32 status
, ms
;
743 if (authctxt
== NULL
)
744 fatal("input_gssapi_response: no authentication context");
745 gssctxt
= authctxt
->methoddata
;
747 recv_tok
.value
= packet_get_string(&len
);
748 recv_tok
.length
= len
;
752 /* Stick it into GSSAPI and see what it says */
753 status
= ssh_gssapi_init_ctx(gssctxt
, options
.gss_deleg_creds
,
754 &recv_tok
, &send_tok
, NULL
);
756 xfree(recv_tok
.value
);
757 gss_release_buffer(&ms
, &send_tok
);
759 /* Server will be returning a failed packet after this one */
764 input_gssapi_error(int type
, u_int32_t plen
, void *ctxt
)
770 maj
=packet_get_int();
771 min
=packet_get_int();
772 msg
=packet_get_string(NULL
);
773 lang
=packet_get_string(NULL
);
777 debug("Server GSSAPI Error:\n%s", msg
);
784 userauth_none(Authctxt
*authctxt
)
786 /* initial userauth request */
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
);
796 userauth_passwd(Authctxt
*authctxt
)
798 static int attempt
= 0;
802 if (attempt
++ >= options
.number_of_password_prompts
)
806 error("Permission denied, please try again.");
808 snprintf(prompt
, sizeof(prompt
), "%.30s@%.128s's password: ",
809 authctxt
->server_user
, authctxt
->host
);
810 password
= read_passphrase(prompt
, 0);
811 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
812 packet_put_cstring(authctxt
->server_user
);
813 packet_put_cstring(authctxt
->service
);
814 packet_put_cstring(authctxt
->method
->name
);
816 packet_put_cstring(password
);
817 memset(password
, 0, strlen(password
));
819 packet_add_padding(64);
822 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ
,
823 &input_userauth_passwd_changereq
);
829 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
833 input_userauth_passwd_changereq(int type
, u_int32_t seqnr
, void *ctxt
)
835 Authctxt
*authctxt
= ctxt
;
836 char *info
, *lang
, *password
= NULL
, *retype
= NULL
;
839 debug2("input_userauth_passwd_changereq");
841 if (authctxt
== NULL
)
842 fatal("input_userauth_passwd_changereq: "
843 "no authentication context");
845 info
= packet_get_string(NULL
);
846 lang
= packet_get_string(NULL
);
847 if (strlen(info
) > 0)
851 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
852 packet_put_cstring(authctxt
->server_user
);
853 packet_put_cstring(authctxt
->service
);
854 packet_put_cstring(authctxt
->method
->name
);
855 packet_put_char(1); /* additional info */
856 snprintf(prompt
, sizeof(prompt
),
857 "Enter %.30s@%.128s's old password: ",
858 authctxt
->server_user
, authctxt
->host
);
859 password
= read_passphrase(prompt
, 0);
860 packet_put_cstring(password
);
861 memset(password
, 0, strlen(password
));
864 while (password
== NULL
) {
865 snprintf(prompt
, sizeof(prompt
),
866 "Enter %.30s@%.128s's new password: ",
867 authctxt
->server_user
, authctxt
->host
);
868 password
= read_passphrase(prompt
, RP_ALLOW_EOF
);
869 if (password
== NULL
) {
873 snprintf(prompt
, sizeof(prompt
),
874 "Retype %.30s@%.128s's new password: ",
875 authctxt
->server_user
, authctxt
->host
);
876 retype
= read_passphrase(prompt
, 0);
877 if (strcmp(password
, retype
) != 0) {
878 memset(password
, 0, strlen(password
));
880 logit("Mismatch; try again, EOF to quit.");
883 memset(retype
, 0, strlen(retype
));
886 packet_put_cstring(password
);
887 memset(password
, 0, strlen(password
));
889 packet_add_padding(64);
892 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ
,
893 &input_userauth_passwd_changereq
);
898 pw_encrypt(const char *password
, const char *crypt_scheme
, const char *salt
)
900 /* OpenBSD crypt(3) handles all of these */
901 if (strcmp(crypt_scheme
, "crypt") == 0 ||
902 strcmp(crypt_scheme
, "bcrypt") == 0 ||
903 strcmp(crypt_scheme
, "md5crypt") == 0 ||
904 strcmp(crypt_scheme
, "crypt-extended") == 0)
905 return xstrdup(crypt(password
, salt
));
906 error("%s: unsupported password encryption scheme \"%.100s\"",
907 __func__
, crypt_scheme
);
912 jpake_password_to_secret(Authctxt
*authctxt
, const char *crypt_scheme
,
915 char prompt
[256], *password
, *crypted
;
920 snprintf(prompt
, sizeof(prompt
), "%.30s@%.128s's password (JPAKE): ",
921 authctxt
->server_user
, authctxt
->host
);
922 password
= read_passphrase(prompt
, 0);
924 if ((crypted
= pw_encrypt(password
, crypt_scheme
, salt
)) == NULL
) {
925 logit("Disabling %s authentication", authctxt
->method
->name
);
926 authctxt
->method
->enabled
= NULL
;
927 /* Continue with an empty password to fail gracefully */
928 crypted
= xstrdup("");
932 debug3("%s: salt = %s", __func__
, salt
);
933 debug3("%s: scheme = %s", __func__
, crypt_scheme
);
934 debug3("%s: crypted = %s", __func__
, crypted
);
937 if (hash_buffer(crypted
, strlen(crypted
), EVP_sha256(),
938 &secret
, &secret_len
) != 0)
939 fatal("%s: hash_buffer", __func__
);
941 bzero(password
, strlen(password
));
942 bzero(crypted
, strlen(crypted
));
946 if ((ret
= BN_bin2bn(secret
, secret_len
, NULL
)) == NULL
)
947 fatal("%s: BN_bin2bn (secret)", __func__
);
948 bzero(secret
, secret_len
);
956 input_userauth_jpake_server_step1(int type
, u_int32_t seq
, void *ctxt
)
958 Authctxt
*authctxt
= ctxt
;
959 struct jpake_ctx
*pctx
= authctxt
->methoddata
;
960 u_char
*x3_proof
, *x4_proof
, *x2_s_proof
;
961 u_int x3_proof_len
, x4_proof_len
, x2_s_proof_len
;
962 char *crypt_scheme
, *salt
;
964 /* Disable this message */
965 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1
, NULL
);
967 if ((pctx
->g_x3
= BN_new()) == NULL
||
968 (pctx
->g_x4
= BN_new()) == NULL
)
969 fatal("%s: BN_new", __func__
);
971 /* Fetch step 1 values */
972 crypt_scheme
= packet_get_string(NULL
);
973 salt
= packet_get_string(NULL
);
974 pctx
->server_id
= packet_get_string(&pctx
->server_id_len
);
975 packet_get_bignum2(pctx
->g_x3
);
976 packet_get_bignum2(pctx
->g_x4
);
977 x3_proof
= packet_get_string(&x3_proof_len
);
978 x4_proof
= packet_get_string(&x4_proof_len
);
981 JPAKE_DEBUG_CTX((pctx
, "step 1 received in %s", __func__
));
983 /* Obtain password and derive secret */
984 pctx
->s
= jpake_password_to_secret(authctxt
, crypt_scheme
, salt
);
985 bzero(crypt_scheme
, strlen(crypt_scheme
));
986 bzero(salt
, strlen(salt
));
989 JPAKE_DEBUG_BN((pctx
->s
, "%s: s = ", __func__
));
991 /* Calculate step 2 values */
992 jpake_step2(pctx
->grp
, pctx
->s
, pctx
->g_x1
,
993 pctx
->g_x3
, pctx
->g_x4
, pctx
->x2
,
994 pctx
->server_id
, pctx
->server_id_len
,
995 pctx
->client_id
, pctx
->client_id_len
,
996 x3_proof
, x3_proof_len
,
997 x4_proof
, x4_proof_len
,
999 &x2_s_proof
, &x2_s_proof_len
);
1001 bzero(x3_proof
, x3_proof_len
);
1002 bzero(x4_proof
, x4_proof_len
);
1006 JPAKE_DEBUG_CTX((pctx
, "step 2 sending in %s", __func__
));
1008 /* Send values for step 2 */
1009 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP2
);
1010 packet_put_bignum2(pctx
->a
);
1011 packet_put_string(x2_s_proof
, x2_s_proof_len
);
1014 bzero(x2_s_proof
, x2_s_proof_len
);
1017 /* Expect step 2 packet from peer */
1018 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2
,
1019 input_userauth_jpake_server_step2
);
1024 input_userauth_jpake_server_step2(int type
, u_int32_t seq
, void *ctxt
)
1026 Authctxt
*authctxt
= ctxt
;
1027 struct jpake_ctx
*pctx
= authctxt
->methoddata
;
1029 u_int x4_s_proof_len
;
1031 /* Disable this message */
1032 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2
, NULL
);
1034 if ((pctx
->b
= BN_new()) == NULL
)
1035 fatal("%s: BN_new", __func__
);
1037 /* Fetch step 2 values */
1038 packet_get_bignum2(pctx
->b
);
1039 x4_s_proof
= packet_get_string(&x4_s_proof_len
);
1042 JPAKE_DEBUG_CTX((pctx
, "step 2 received in %s", __func__
));
1044 /* Derive shared key and calculate confirmation hash */
1045 jpake_key_confirm(pctx
->grp
, pctx
->s
, pctx
->b
,
1046 pctx
->x2
, pctx
->g_x1
, pctx
->g_x2
, pctx
->g_x3
, pctx
->g_x4
,
1047 pctx
->client_id
, pctx
->client_id_len
,
1048 pctx
->server_id
, pctx
->server_id_len
,
1049 session_id2
, session_id2_len
,
1050 x4_s_proof
, x4_s_proof_len
,
1052 &pctx
->h_k_cid_sessid
, &pctx
->h_k_cid_sessid_len
);
1054 bzero(x4_s_proof
, x4_s_proof_len
);
1057 JPAKE_DEBUG_CTX((pctx
, "confirm sending in %s", __func__
));
1059 /* Send key confirmation proof */
1060 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_CONFIRM
);
1061 packet_put_string(pctx
->h_k_cid_sessid
, pctx
->h_k_cid_sessid_len
);
1064 /* Expect confirmation from peer */
1065 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM
,
1066 input_userauth_jpake_server_confirm
);
1071 input_userauth_jpake_server_confirm(int type
, u_int32_t seq
, void *ctxt
)
1073 Authctxt
*authctxt
= ctxt
;
1074 struct jpake_ctx
*pctx
= authctxt
->methoddata
;
1076 /* Disable this message */
1077 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM
, NULL
);
1079 pctx
->h_k_sid_sessid
= packet_get_string(&pctx
->h_k_sid_sessid_len
);
1082 JPAKE_DEBUG_CTX((pctx
, "confirm received in %s", __func__
));
1084 /* Verify expected confirmation hash */
1085 if (jpake_check_confirm(pctx
->k
,
1086 pctx
->server_id
, pctx
->server_id_len
,
1087 session_id2
, session_id2_len
,
1088 pctx
->h_k_sid_sessid
, pctx
->h_k_sid_sessid_len
) == 1)
1089 debug("%s: %s success", __func__
, authctxt
->method
->name
);
1091 debug("%s: confirmation mismatch", __func__
);
1092 /* XXX stash this so if auth succeeds then we can warn/kill */
1095 userauth_jpake_cleanup(authctxt
);
1100 identity_sign(Identity
*id
, u_char
**sigp
, u_int
*lenp
,
1101 u_char
*data
, u_int datalen
)
1106 /* the agent supports this key */
1108 return (ssh_agent_sign(id
->ac
, id
->key
, sigp
, lenp
,
1111 * we have already loaded the private key or
1112 * the private key is stored in external hardware
1114 if (id
->isprivate
|| (id
->key
->flags
& KEY_FLAG_EXT
))
1115 return (key_sign(id
->key
, sigp
, lenp
, data
, datalen
));
1116 /* load the private key from the file */
1117 if ((prv
= load_identity_file(id
->filename
)) == NULL
)
1119 ret
= key_sign(prv
, sigp
, lenp
, data
, datalen
);
1125 sign_and_send_pubkey(Authctxt
*authctxt
, Identity
*id
)
1128 u_char
*blob
, *signature
;
1129 u_int bloblen
, slen
;
1134 debug3("sign_and_send_pubkey");
1136 if (key_to_blob(id
->key
, &blob
, &bloblen
) == 0) {
1137 /* we cannot handle this key */
1138 debug3("sign_and_send_pubkey: cannot handle key");
1141 /* data to be signed */
1143 if (datafellows
& SSH_OLD_SESSIONID
) {
1144 buffer_append(&b
, session_id2
, session_id2_len
);
1145 skip
= session_id2_len
;
1147 buffer_put_string(&b
, session_id2
, session_id2_len
);
1148 skip
= buffer_len(&b
);
1150 buffer_put_char(&b
, SSH2_MSG_USERAUTH_REQUEST
);
1151 buffer_put_cstring(&b
, authctxt
->server_user
);
1152 buffer_put_cstring(&b
,
1153 datafellows
& SSH_BUG_PKSERVICE
?
1156 if (datafellows
& SSH_BUG_PKAUTH
) {
1157 buffer_put_char(&b
, have_sig
);
1159 buffer_put_cstring(&b
, authctxt
->method
->name
);
1160 buffer_put_char(&b
, have_sig
);
1161 buffer_put_cstring(&b
, key_ssh_name(id
->key
));
1163 buffer_put_string(&b
, blob
, bloblen
);
1165 /* generate signature */
1166 ret
= identity_sign(id
, &signature
, &slen
,
1167 buffer_ptr(&b
), buffer_len(&b
));
1176 if (datafellows
& SSH_BUG_PKSERVICE
) {
1178 buffer_append(&b
, session_id2
, session_id2_len
);
1179 skip
= session_id2_len
;
1180 buffer_put_char(&b
, SSH2_MSG_USERAUTH_REQUEST
);
1181 buffer_put_cstring(&b
, authctxt
->server_user
);
1182 buffer_put_cstring(&b
, authctxt
->service
);
1183 buffer_put_cstring(&b
, authctxt
->method
->name
);
1184 buffer_put_char(&b
, have_sig
);
1185 if (!(datafellows
& SSH_BUG_PKAUTH
))
1186 buffer_put_cstring(&b
, key_ssh_name(id
->key
));
1187 buffer_put_string(&b
, blob
, bloblen
);
1191 /* append signature */
1192 buffer_put_string(&b
, signature
, slen
);
1195 /* skip session id and packet type */
1196 if (buffer_len(&b
) < skip
+ 1)
1197 fatal("userauth_pubkey: internal error");
1198 buffer_consume(&b
, skip
+ 1);
1200 /* put remaining data from buffer into packet */
1201 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
1202 packet_put_raw(buffer_ptr(&b
), buffer_len(&b
));
1210 send_pubkey_test(Authctxt
*authctxt
, Identity
*id
)
1213 u_int bloblen
, have_sig
= 0;
1215 debug3("send_pubkey_test");
1217 if (key_to_blob(id
->key
, &blob
, &bloblen
) == 0) {
1218 /* we cannot handle this key */
1219 debug3("send_pubkey_test: cannot handle key");
1222 /* register callback for USERAUTH_PK_OK message */
1223 dispatch_set(SSH2_MSG_USERAUTH_PK_OK
, &input_userauth_pk_ok
);
1225 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
1226 packet_put_cstring(authctxt
->server_user
);
1227 packet_put_cstring(authctxt
->service
);
1228 packet_put_cstring(authctxt
->method
->name
);
1229 packet_put_char(have_sig
);
1230 if (!(datafellows
& SSH_BUG_PKAUTH
))
1231 packet_put_cstring(key_ssh_name(id
->key
));
1232 packet_put_string(blob
, bloblen
);
1239 load_identity_file(char *filename
)
1242 char prompt
[300], *passphrase
;
1243 int perm_ok
, quit
, i
;
1246 if (stat(filename
, &st
) < 0) {
1247 debug3("no such identity: %s", filename
);
1250 private = key_load_private_type(KEY_UNSPEC
, filename
, "", NULL
, &perm_ok
);
1253 if (private == NULL
) {
1254 if (options
.batch_mode
)
1256 snprintf(prompt
, sizeof prompt
,
1257 "Enter passphrase for key '%.100s': ", filename
);
1258 for (i
= 0; i
< options
.number_of_password_prompts
; i
++) {
1259 passphrase
= read_passphrase(prompt
, 0);
1260 if (strcmp(passphrase
, "") != 0) {
1261 private = key_load_private_type(KEY_UNSPEC
,
1262 filename
, passphrase
, NULL
, NULL
);
1265 debug2("no passphrase given, try next key");
1268 memset(passphrase
, 0, strlen(passphrase
));
1270 if (private != NULL
|| quit
)
1272 debug2("bad passphrase given, try again...");
1279 * try keys in the following order:
1280 * 1. agent keys that are found in the config file
1281 * 2. other agent keys
1282 * 3. keys that are only listed in the config file
1285 pubkey_prepare(Authctxt
*authctxt
)
1288 Idlist agent
, files
, *preferred
;
1290 AuthenticationConnection
*ac
;
1294 TAILQ_INIT(&agent
); /* keys from the agent */
1295 TAILQ_INIT(&files
); /* keys from the config file */
1296 preferred
= &authctxt
->keys
;
1297 TAILQ_INIT(preferred
); /* preferred order of keys */
1299 /* list of keys stored in the filesystem */
1300 for (i
= 0; i
< options
.num_identity_files
; i
++) {
1301 key
= options
.identity_keys
[i
];
1302 if (key
&& key
->type
== KEY_RSA1
)
1304 options
.identity_keys
[i
] = NULL
;
1305 id
= xcalloc(1, sizeof(*id
));
1307 id
->filename
= xstrdup(options
.identity_files
[i
]);
1308 TAILQ_INSERT_TAIL(&files
, id
, next
);
1310 /* list of keys supported by the agent */
1311 if ((ac
= ssh_get_authentication_connection())) {
1312 for (key
= ssh_get_first_identity(ac
, &comment
, 2);
1314 key
= ssh_get_next_identity(ac
, &comment
, 2)) {
1316 TAILQ_FOREACH(id
, &files
, next
) {
1317 /* agent keys from the config file are preferred */
1318 if (key_equal(key
, id
->key
)) {
1321 TAILQ_REMOVE(&files
, id
, next
);
1322 TAILQ_INSERT_TAIL(preferred
, id
, next
);
1328 if (!found
&& !options
.identities_only
) {
1329 id
= xcalloc(1, sizeof(*id
));
1331 id
->filename
= comment
;
1333 TAILQ_INSERT_TAIL(&agent
, id
, next
);
1336 /* append remaining agent keys */
1337 for (id
= TAILQ_FIRST(&agent
); id
; id
= TAILQ_FIRST(&agent
)) {
1338 TAILQ_REMOVE(&agent
, id
, next
);
1339 TAILQ_INSERT_TAIL(preferred
, id
, next
);
1341 authctxt
->agent
= ac
;
1343 /* append remaining keys from the config file */
1344 for (id
= TAILQ_FIRST(&files
); id
; id
= TAILQ_FIRST(&files
)) {
1345 TAILQ_REMOVE(&files
, id
, next
);
1346 TAILQ_INSERT_TAIL(preferred
, id
, next
);
1348 TAILQ_FOREACH(id
, preferred
, next
) {
1349 debug2("key: %s (%p)", id
->filename
, id
->key
);
1354 pubkey_cleanup(Authctxt
*authctxt
)
1358 if (authctxt
->agent
!= NULL
)
1359 ssh_close_authentication_connection(authctxt
->agent
);
1360 for (id
= TAILQ_FIRST(&authctxt
->keys
); id
;
1361 id
= TAILQ_FIRST(&authctxt
->keys
)) {
1362 TAILQ_REMOVE(&authctxt
->keys
, id
, next
);
1366 xfree(id
->filename
);
1372 userauth_pubkey(Authctxt
*authctxt
)
1377 while ((id
= TAILQ_FIRST(&authctxt
->keys
))) {
1380 /* move key to the end of the queue */
1381 TAILQ_REMOVE(&authctxt
->keys
, id
, next
);
1382 TAILQ_INSERT_TAIL(&authctxt
->keys
, id
, next
);
1384 * send a test message if we have the public key. for
1385 * encrypted keys we cannot do this and have to load the
1386 * private key instead
1388 if (id
->key
&& id
->key
->type
!= KEY_RSA1
) {
1389 debug("Offering public key: %s", id
->filename
);
1390 sent
= send_pubkey_test(authctxt
, id
);
1391 } else if (id
->key
== NULL
) {
1392 debug("Trying private key: %s", id
->filename
);
1393 id
->key
= load_identity_file(id
->filename
);
1394 if (id
->key
!= NULL
) {
1396 sent
= sign_and_send_pubkey(authctxt
, id
);
1408 * Send userauth request message specifying keyboard-interactive method.
1411 userauth_kbdint(Authctxt
*authctxt
)
1413 static int attempt
= 0;
1415 if (attempt
++ >= options
.number_of_password_prompts
)
1417 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1418 if (attempt
> 1 && !authctxt
->info_req_seen
) {
1419 debug3("userauth_kbdint: disable: no info_req_seen");
1420 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST
, NULL
);
1424 debug2("userauth_kbdint");
1425 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
1426 packet_put_cstring(authctxt
->server_user
);
1427 packet_put_cstring(authctxt
->service
);
1428 packet_put_cstring(authctxt
->method
->name
);
1429 packet_put_cstring(""); /* lang */
1430 packet_put_cstring(options
.kbd_interactive_devices
?
1431 options
.kbd_interactive_devices
: "");
1434 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST
, &input_userauth_info_req
);
1439 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1442 input_userauth_info_req(int type
, u_int32_t seq
, void *ctxt
)
1444 Authctxt
*authctxt
= ctxt
;
1445 char *name
, *inst
, *lang
, *prompt
, *response
;
1446 u_int num_prompts
, i
;
1449 debug2("input_userauth_info_req");
1451 if (authctxt
== NULL
)
1452 fatal("input_userauth_info_req: no authentication context");
1454 authctxt
->info_req_seen
= 1;
1456 name
= packet_get_string(NULL
);
1457 inst
= packet_get_string(NULL
);
1458 lang
= packet_get_string(NULL
);
1459 if (strlen(name
) > 0)
1461 if (strlen(inst
) > 0)
1467 num_prompts
= packet_get_int();
1469 * Begin to build info response packet based on prompts requested.
1470 * We commit to providing the correct number of responses, so if
1471 * further on we run into a problem that prevents this, we have to
1472 * be sure and clean this up and send a correct error response.
1474 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE
);
1475 packet_put_int(num_prompts
);
1477 debug2("input_userauth_info_req: num_prompts %d", num_prompts
);
1478 for (i
= 0; i
< num_prompts
; i
++) {
1479 prompt
= packet_get_string(NULL
);
1480 echo
= packet_get_char();
1482 response
= read_passphrase(prompt
, echo
? RP_ECHO
: 0);
1484 packet_put_cstring(response
);
1485 memset(response
, 0, strlen(response
));
1489 packet_check_eom(); /* done with parsing incoming message. */
1491 packet_add_padding(64);
1496 ssh_keysign(Key
*key
, u_char
**sigp
, u_int
*lenp
,
1497 u_char
*data
, u_int datalen
)
1502 int to
[2], from
[2], status
, version
= 2;
1504 debug2("ssh_keysign called");
1506 if (stat(_PATH_SSH_KEY_SIGN
, &st
) < 0) {
1507 error("ssh_keysign: no installed: %s", strerror(errno
));
1510 if (fflush(stdout
) != 0)
1511 error("ssh_keysign: fflush: %s", strerror(errno
));
1513 error("ssh_keysign: pipe: %s", strerror(errno
));
1516 if (pipe(from
) < 0) {
1517 error("ssh_keysign: pipe: %s", strerror(errno
));
1520 if ((pid
= fork()) < 0) {
1521 error("ssh_keysign: fork: %s", strerror(errno
));
1525 permanently_drop_suid(getuid());
1527 if (dup2(from
[1], STDOUT_FILENO
) < 0)
1528 fatal("ssh_keysign: dup2: %s", strerror(errno
));
1530 if (dup2(to
[0], STDIN_FILENO
) < 0)
1531 fatal("ssh_keysign: dup2: %s", strerror(errno
));
1534 execl(_PATH_SSH_KEY_SIGN
, _PATH_SSH_KEY_SIGN
, (char *) 0);
1535 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN
,
1542 buffer_put_int(&b
, packet_get_connection_in()); /* send # of socket */
1543 buffer_put_string(&b
, data
, datalen
);
1544 if (ssh_msg_send(to
[1], version
, &b
) == -1)
1545 fatal("ssh_keysign: couldn't send request");
1547 if (ssh_msg_recv(from
[0], &b
) < 0) {
1548 error("ssh_keysign: no reply");
1555 while (waitpid(pid
, &status
, 0) < 0)
1559 if (buffer_get_char(&b
) != version
) {
1560 error("ssh_keysign: bad version");
1564 *sigp
= buffer_get_string(&b
, lenp
);
1571 userauth_hostbased(Authctxt
*authctxt
)
1573 Key
*private = NULL
;
1574 Sensitive
*sensitive
= authctxt
->sensitive
;
1576 u_char
*signature
, *blob
;
1577 char *chost
, *pkalg
, *p
, myname
[NI_MAXHOST
];
1578 const char *service
;
1580 int ok
, i
, len
, found
= 0;
1582 /* check for a useful key */
1583 for (i
= 0; i
< sensitive
->nkeys
; i
++) {
1584 private = sensitive
->keys
[i
];
1585 if (private && private->type
!= KEY_RSA1
) {
1587 /* we take and free the key */
1588 sensitive
->keys
[i
] = NULL
;
1593 debug("No more client hostkeys for hostbased authentication.");
1596 if (key_to_blob(private, &blob
, &blen
) == 0) {
1600 /* figure out a name for the client host */
1602 if (packet_connection_is_on_socket())
1603 p
= get_local_name(packet_get_connection_in());
1605 if (gethostname(myname
, sizeof(myname
)) == -1) {
1606 verbose("userauth_hostbased: gethostname: %s",
1609 p
= xstrdup(myname
);
1612 error("userauth_hostbased: cannot get local ipaddr/name");
1617 len
= strlen(p
) + 2;
1618 xasprintf(&chost
, "%s.", p
);
1619 debug2("userauth_hostbased: chost %s", chost
);
1622 service
= datafellows
& SSH_BUG_HBSERVICE
? "ssh-userauth" :
1624 pkalg
= xstrdup(key_ssh_name(private));
1626 /* construct data */
1627 buffer_put_string(&b
, session_id2
, session_id2_len
);
1628 buffer_put_char(&b
, SSH2_MSG_USERAUTH_REQUEST
);
1629 buffer_put_cstring(&b
, authctxt
->server_user
);
1630 buffer_put_cstring(&b
, service
);
1631 buffer_put_cstring(&b
, authctxt
->method
->name
);
1632 buffer_put_cstring(&b
, pkalg
);
1633 buffer_put_string(&b
, blob
, blen
);
1634 buffer_put_cstring(&b
, chost
);
1635 buffer_put_cstring(&b
, authctxt
->local_user
);
1639 if (sensitive
->external_keysign
)
1640 ok
= ssh_keysign(private, &signature
, &slen
,
1641 buffer_ptr(&b
), buffer_len(&b
));
1643 ok
= key_sign(private, &signature
, &slen
,
1644 buffer_ptr(&b
), buffer_len(&b
));
1648 error("key_sign failed");
1654 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
1655 packet_put_cstring(authctxt
->server_user
);
1656 packet_put_cstring(authctxt
->service
);
1657 packet_put_cstring(authctxt
->method
->name
);
1658 packet_put_cstring(pkalg
);
1659 packet_put_string(blob
, blen
);
1660 packet_put_cstring(chost
);
1661 packet_put_cstring(authctxt
->local_user
);
1662 packet_put_string(signature
, slen
);
1663 memset(signature
, 's', slen
);
1675 userauth_jpake(Authctxt
*authctxt
)
1677 struct jpake_ctx
*pctx
;
1678 u_char
*x1_proof
, *x2_proof
;
1679 u_int x1_proof_len
, x2_proof_len
;
1680 static int attempt
= 0; /* XXX share with userauth_password's? */
1682 if (attempt
++ >= options
.number_of_password_prompts
)
1685 error("Permission denied, please try again.");
1687 if (authctxt
->methoddata
!= NULL
)
1688 fatal("%s: authctxt->methoddata already set (%p)",
1689 __func__
, authctxt
->methoddata
);
1691 authctxt
->methoddata
= pctx
= jpake_new();
1694 * Send request immediately, to get the protocol going while
1695 * we do the initial computations.
1697 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
1698 packet_put_cstring(authctxt
->server_user
);
1699 packet_put_cstring(authctxt
->service
);
1700 packet_put_cstring(authctxt
->method
->name
);
1702 packet_write_wait();
1704 jpake_step1(pctx
->grp
,
1705 &pctx
->client_id
, &pctx
->client_id_len
,
1706 &pctx
->x1
, &pctx
->x2
, &pctx
->g_x1
, &pctx
->g_x2
,
1707 &x1_proof
, &x1_proof_len
,
1708 &x2_proof
, &x2_proof_len
);
1710 JPAKE_DEBUG_CTX((pctx
, "step 1 sending in %s", __func__
));
1712 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP1
);
1713 packet_put_string(pctx
->client_id
, pctx
->client_id_len
);
1714 packet_put_bignum2(pctx
->g_x1
);
1715 packet_put_bignum2(pctx
->g_x2
);
1716 packet_put_string(x1_proof
, x1_proof_len
);
1717 packet_put_string(x2_proof
, x2_proof_len
);
1720 bzero(x1_proof
, x1_proof_len
);
1721 bzero(x2_proof
, x2_proof_len
);
1725 /* Expect step 1 packet from peer */
1726 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1
,
1727 input_userauth_jpake_server_step1
);
1728 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS
,
1729 &input_userauth_success_unexpected
);
1735 userauth_jpake_cleanup(Authctxt
*authctxt
)
1737 debug3("%s: clean up", __func__
);
1738 if (authctxt
->methoddata
!= NULL
) {
1739 jpake_free(authctxt
->methoddata
);
1740 authctxt
->methoddata
= NULL
;
1742 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS
, &input_userauth_success
);
1746 /* find auth method */
1749 * given auth method name, if configurable options permit this method fill
1750 * in auth_ident field and return true, otherwise return false.
1753 authmethod_is_enabled(Authmethod
*method
)
1757 /* return false if options indicate this method is disabled */
1758 if (method
->enabled
== NULL
|| *method
->enabled
== 0)
1760 /* return false if batch mode is enabled but method needs interactive mode */
1761 if (method
->batch_flag
!= NULL
&& *method
->batch_flag
!= 0)
1767 authmethod_lookup(const char *name
)
1769 Authmethod
*method
= NULL
;
1771 for (method
= authmethods
; method
->name
!= NULL
; method
++)
1772 if (strcmp(name
, method
->name
) == 0)
1774 debug2("Unrecognized authentication method name: %s", name
? name
: "NULL");
1778 /* XXX internal state */
1779 static Authmethod
*current
= NULL
;
1780 static char *supported
= NULL
;
1781 static char *preferred
= NULL
;
1784 * Given the authentication method list sent by the server, return the
1785 * next method we should try. If the server initially sends a nil list,
1786 * use a built-in default list.
1789 authmethod_get(char *authlist
)
1794 /* Use a suitable default if we're passed a nil list. */
1795 if (authlist
== NULL
|| strlen(authlist
) == 0)
1796 authlist
= options
.preferred_authentications
;
1798 if (supported
== NULL
|| strcmp(authlist
, supported
) != 0) {
1799 debug3("start over, passed a different list %s", authlist
);
1800 if (supported
!= NULL
)
1802 supported
= xstrdup(authlist
);
1803 preferred
= options
.preferred_authentications
;
1804 debug3("preferred %s", preferred
);
1806 } else if (current
!= NULL
&& authmethod_is_enabled(current
))
1810 if ((name
= match_list(preferred
, supported
, &next
)) == NULL
) {
1811 debug("No more authentication methods to try.");
1816 debug3("authmethod_lookup %s", name
);
1817 debug3("remaining preferred: %s", preferred
);
1818 if ((current
= authmethod_lookup(name
)) != NULL
&&
1819 authmethod_is_enabled(current
)) {
1820 debug3("authmethod_is_enabled %s", name
);
1821 debug("Next authentication method: %s", name
);
1828 authmethods_get(void)
1830 Authmethod
*method
= NULL
;
1835 for (method
= authmethods
; method
->name
!= NULL
; method
++) {
1836 if (authmethod_is_enabled(method
)) {
1837 if (buffer_len(&b
) > 0)
1838 buffer_append(&b
, ",", 1);
1839 buffer_append(&b
, method
->name
, strlen(method
->name
));
1842 buffer_append(&b
, "\0", 1);
1843 list
= xstrdup(buffer_ptr(&b
));