1 /* $OpenBSD: sshconnect2.c,v 1.170 2008/11/04 08:22:13 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"
78 extern char *client_version_string
;
79 extern char *server_version_string
;
80 extern Options options
;
86 u_char
*session_id2
= NULL
;
87 u_int session_id2_len
= 0;
90 struct sockaddr
*xxx_hostaddr
;
95 verify_host_key_callback(Key
*hostkey
)
97 if (verify_host_key(xxx_host
, xxx_hostaddr
, hostkey
) == -1)
98 fatal("Host key verification failed.");
103 ssh_kex2(char *host
, struct sockaddr
*hostaddr
)
108 xxx_hostaddr
= hostaddr
;
110 if (options
.ciphers
== (char *)-1) {
111 logit("No valid ciphers for protocol version 2 given, using defaults.");
112 options
.ciphers
= NULL
;
114 if (options
.ciphers
!= NULL
) {
115 myproposal
[PROPOSAL_ENC_ALGS_CTOS
] =
116 myproposal
[PROPOSAL_ENC_ALGS_STOC
] = options
.ciphers
;
118 myproposal
[PROPOSAL_ENC_ALGS_CTOS
] =
119 compat_cipher_proposal(myproposal
[PROPOSAL_ENC_ALGS_CTOS
]);
120 myproposal
[PROPOSAL_ENC_ALGS_STOC
] =
121 compat_cipher_proposal(myproposal
[PROPOSAL_ENC_ALGS_STOC
]);
122 if (options
.compression
) {
123 myproposal
[PROPOSAL_COMP_ALGS_CTOS
] =
124 myproposal
[PROPOSAL_COMP_ALGS_STOC
] = "zlib@openssh.com,zlib,none";
126 myproposal
[PROPOSAL_COMP_ALGS_CTOS
] =
127 myproposal
[PROPOSAL_COMP_ALGS_STOC
] = "none,zlib@openssh.com,zlib";
129 if (options
.macs
!= NULL
) {
130 myproposal
[PROPOSAL_MAC_ALGS_CTOS
] =
131 myproposal
[PROPOSAL_MAC_ALGS_STOC
] = options
.macs
;
133 if (options
.hostkeyalgorithms
!= NULL
)
134 myproposal
[PROPOSAL_SERVER_HOST_KEY_ALGS
] =
135 options
.hostkeyalgorithms
;
137 if (options
.rekey_limit
)
138 packet_set_rekey_limit((u_int32_t
)options
.rekey_limit
);
140 /* start key exchange */
141 kex
= kex_setup(myproposal
);
142 kex
->kex
[KEX_DH_GRP1_SHA1
] = kexdh_client
;
143 kex
->kex
[KEX_DH_GRP14_SHA1
] = kexdh_client
;
144 kex
->kex
[KEX_DH_GEX_SHA1
] = kexgex_client
;
145 kex
->kex
[KEX_DH_GEX_SHA256
] = kexgex_client
;
146 kex
->client_version_string
=client_version_string
;
147 kex
->server_version_string
=server_version_string
;
148 kex
->verify_host_key
=&verify_host_key_callback
;
152 dispatch_run(DISPATCH_BLOCK
, &kex
->done
, kex
);
154 session_id2
= kex
->session_id
;
155 session_id2_len
= kex
->session_id_len
;
158 /* send 1st encrypted/maced/compressed message */
159 packet_start(SSH2_MSG_IGNORE
);
160 packet_put_cstring("markus");
170 typedef struct Authctxt Authctxt
;
171 typedef struct Authmethod Authmethod
;
172 typedef struct identity Identity
;
173 typedef struct idlist Idlist
;
176 TAILQ_ENTRY(identity
) next
;
177 AuthenticationConnection
*ac
; /* set if agent supports key */
178 Key
*key
; /* public/private key */
179 char *filename
; /* comment for agent-only keys */
181 int isprivate
; /* key points to the private key */
183 TAILQ_HEAD(idlist
, identity
);
186 const char *server_user
;
187 const char *local_user
;
195 AuthenticationConnection
*agent
;
197 Sensitive
*sensitive
;
198 /* kbd-interactive */
204 char *name
; /* string to compare against server's list */
205 int (*userauth
)(Authctxt
*authctxt
);
206 void (*cleanup
)(Authctxt
*authctxt
);
207 int *enabled
; /* flag in option struct that enables method */
208 int *batch_flag
; /* flag in option struct that disables method */
211 void input_userauth_success(int, u_int32_t
, void *);
212 void input_userauth_failure(int, u_int32_t
, void *);
213 void input_userauth_banner(int, u_int32_t
, void *);
214 void input_userauth_error(int, u_int32_t
, void *);
215 void input_userauth_info_req(int, u_int32_t
, void *);
216 void input_userauth_pk_ok(int, u_int32_t
, void *);
217 void input_userauth_passwd_changereq(int, u_int32_t
, void *);
218 void input_userauth_jpake_server_step1(int, u_int32_t
, void *);
219 void input_userauth_jpake_server_step2(int, u_int32_t
, void *);
220 void input_userauth_jpake_server_confirm(int, u_int32_t
, void *);
222 int userauth_none(Authctxt
*);
223 int userauth_pubkey(Authctxt
*);
224 int userauth_passwd(Authctxt
*);
225 int userauth_kbdint(Authctxt
*);
226 int userauth_hostbased(Authctxt
*);
227 int userauth_jpake(Authctxt
*);
229 void userauth_jpake_cleanup(Authctxt
*);
232 int userauth_gssapi(Authctxt
*authctxt
);
233 void input_gssapi_response(int type
, u_int32_t
, void *);
234 void input_gssapi_token(int type
, u_int32_t
, void *);
235 void input_gssapi_hash(int type
, u_int32_t
, void *);
236 void input_gssapi_error(int, u_int32_t
, void *);
237 void input_gssapi_errtok(int, u_int32_t
, void *);
240 void userauth(Authctxt
*, char *);
242 static int sign_and_send_pubkey(Authctxt
*, Identity
*);
243 static void pubkey_prepare(Authctxt
*);
244 static void pubkey_cleanup(Authctxt
*);
245 static Key
*load_identity_file(char *);
247 static Authmethod
*authmethod_get(char *authlist
);
248 static Authmethod
*authmethod_lookup(const char *name
);
249 static char *authmethods_get(void);
251 Authmethod authmethods
[] = {
256 &options
.gss_authentication
,
262 &options
.hostbased_authentication
,
267 &options
.pubkey_authentication
,
270 {"jpake-01@openssh.com",
272 userauth_jpake_cleanup
,
273 &options
.zero_knowledge_password_authentication
,
274 &options
.batch_mode
},
276 {"keyboard-interactive",
279 &options
.kbd_interactive_authentication
,
280 &options
.batch_mode
},
284 &options
.password_authentication
,
285 &options
.batch_mode
},
291 {NULL
, NULL
, NULL
, NULL
, NULL
}
295 ssh_userauth2(const char *local_user
, const char *server_user
, char *host
,
296 Sensitive
*sensitive
)
301 if (options
.challenge_response_authentication
)
302 options
.kbd_interactive_authentication
= 1;
304 packet_start(SSH2_MSG_SERVICE_REQUEST
);
305 packet_put_cstring("ssh-userauth");
307 debug("SSH2_MSG_SERVICE_REQUEST sent");
309 type
= packet_read();
310 if (type
!= SSH2_MSG_SERVICE_ACCEPT
)
311 fatal("Server denied authentication request: %d", type
);
312 if (packet_remaining() > 0) {
313 char *reply
= packet_get_string(NULL
);
314 debug2("service_accept: %s", reply
);
317 debug2("buggy server: service_accept w/o service");
320 debug("SSH2_MSG_SERVICE_ACCEPT received");
322 if (options
.preferred_authentications
== NULL
)
323 options
.preferred_authentications
= authmethods_get();
325 /* setup authentication context */
326 memset(&authctxt
, 0, sizeof(authctxt
));
327 pubkey_prepare(&authctxt
);
328 authctxt
.server_user
= server_user
;
329 authctxt
.local_user
= local_user
;
330 authctxt
.host
= host
;
331 authctxt
.service
= "ssh-connection"; /* service name */
332 authctxt
.success
= 0;
333 authctxt
.method
= authmethod_lookup("none");
334 authctxt
.authlist
= NULL
;
335 authctxt
.methoddata
= NULL
;
336 authctxt
.sensitive
= sensitive
;
337 authctxt
.info_req_seen
= 0;
338 if (authctxt
.method
== NULL
)
339 fatal("ssh_userauth2: internal error: cannot send userauth none request");
341 /* initial userauth request */
342 userauth_none(&authctxt
);
344 dispatch_init(&input_userauth_error
);
345 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS
, &input_userauth_success
);
346 dispatch_set(SSH2_MSG_USERAUTH_FAILURE
, &input_userauth_failure
);
347 dispatch_set(SSH2_MSG_USERAUTH_BANNER
, &input_userauth_banner
);
348 dispatch_run(DISPATCH_BLOCK
, &authctxt
.success
, &authctxt
); /* loop until success */
350 pubkey_cleanup(&authctxt
);
351 dispatch_range(SSH2_MSG_USERAUTH_MIN
, SSH2_MSG_USERAUTH_MAX
, NULL
);
353 debug("Authentication succeeded (%s).", authctxt
.method
->name
);
357 userauth(Authctxt
*authctxt
, char *authlist
)
359 if (authctxt
->method
!= NULL
&& authctxt
->method
->cleanup
!= NULL
)
360 authctxt
->method
->cleanup(authctxt
);
362 if (authctxt
->methoddata
) {
363 xfree(authctxt
->methoddata
);
364 authctxt
->methoddata
= NULL
;
366 if (authlist
== NULL
) {
367 authlist
= authctxt
->authlist
;
369 if (authctxt
->authlist
)
370 xfree(authctxt
->authlist
);
371 authctxt
->authlist
= authlist
;
374 Authmethod
*method
= authmethod_get(authlist
);
376 fatal("Permission denied (%s).", authlist
);
377 authctxt
->method
= method
;
379 /* reset the per method handler */
380 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN
,
381 SSH2_MSG_USERAUTH_PER_METHOD_MAX
, NULL
);
383 /* and try new method */
384 if (method
->userauth(authctxt
) != 0) {
385 debug2("we sent a %s packet, wait for reply", method
->name
);
388 debug2("we did not send a packet, disable method");
389 method
->enabled
= NULL
;
396 input_userauth_error(int type
, u_int32_t seq
, void *ctxt
)
398 fatal("input_userauth_error: bad message during authentication: "
404 input_userauth_banner(int type
, u_int32_t seq
, void *ctxt
)
406 char *msg
, *raw
, *lang
;
409 debug3("input_userauth_banner");
410 raw
= packet_get_string(&len
);
411 lang
= packet_get_string(NULL
);
412 if (len
> 0 && options
.log_level
>= SYSLOG_LEVEL_INFO
) {
415 msg
= xmalloc(len
* 4 + 1); /* max expansion from strnvis() */
416 strnvis(msg
, raw
, len
* 4 + 1, VIS_SAFE
|VIS_OCTAL
);
417 fprintf(stderr
, "%s", msg
);
426 input_userauth_success(int type
, u_int32_t seq
, void *ctxt
)
428 Authctxt
*authctxt
= ctxt
;
429 if (authctxt
== NULL
)
430 fatal("input_userauth_success: no authentication context");
431 if (authctxt
->authlist
) {
432 xfree(authctxt
->authlist
);
433 authctxt
->authlist
= NULL
;
435 if (authctxt
->methoddata
) {
436 xfree(authctxt
->methoddata
);
437 authctxt
->methoddata
= NULL
;
439 authctxt
->success
= 1; /* break out */
444 input_userauth_failure(int type
, u_int32_t seq
, void *ctxt
)
446 Authctxt
*authctxt
= ctxt
;
447 char *authlist
= NULL
;
450 if (authctxt
== NULL
)
451 fatal("input_userauth_failure: no authentication context");
453 authlist
= packet_get_string(NULL
);
454 partial
= packet_get_char();
458 logit("Authenticated with partial success.");
459 debug("Authentications that can continue: %s", authlist
);
461 userauth(authctxt
, authlist
);
466 input_userauth_pk_ok(int type
, u_int32_t seq
, void *ctxt
)
468 Authctxt
*authctxt
= ctxt
;
472 int pktype
, sent
= 0;
477 if (authctxt
== NULL
)
478 fatal("input_userauth_pk_ok: no authentication context");
479 if (datafellows
& SSH_BUG_PKOK
) {
480 /* this is similar to SSH_BUG_PKAUTH */
481 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
482 pkblob
= packet_get_string(&blen
);
484 buffer_append(&b
, pkblob
, blen
);
485 pkalg
= buffer_get_string(&b
, &alen
);
488 pkalg
= packet_get_string(&alen
);
489 pkblob
= packet_get_string(&blen
);
493 debug("Server accepts key: pkalg %s blen %u", pkalg
, blen
);
495 if ((pktype
= key_type_from_name(pkalg
)) == KEY_UNSPEC
) {
496 debug("unknown pkalg %s", pkalg
);
499 if ((key
= key_from_blob(pkblob
, blen
)) == NULL
) {
500 debug("no key from blob. pkalg %s", pkalg
);
503 if (key
->type
!= pktype
) {
504 error("input_userauth_pk_ok: type mismatch "
505 "for decoded key (received %d, expected %d)",
509 fp
= key_fingerprint(key
, SSH_FP_MD5
, SSH_FP_HEX
);
510 debug2("input_userauth_pk_ok: fp %s", fp
);
514 * search keys in the reverse order, because last candidate has been
515 * moved to the end of the queue. this also avoids confusion by
518 TAILQ_FOREACH_REVERSE(id
, &authctxt
->keys
, idlist
, next
) {
519 if (key_equal(key
, id
->key
)) {
520 sent
= sign_and_send_pubkey(authctxt
, id
);
530 /* try another method if we did not send a packet */
532 userauth(authctxt
, NULL
);
537 userauth_gssapi(Authctxt
*authctxt
)
539 Gssctxt
*gssctxt
= NULL
;
540 static gss_OID_set gss_supported
= NULL
;
541 static u_int mech
= 0;
545 /* Try one GSSAPI method at a time, rather than sending them all at
548 if (gss_supported
== NULL
)
549 gss_indicate_mechs(&min
, &gss_supported
);
551 /* Check to see if the mechanism is usable before we offer it */
552 while (mech
< gss_supported
->count
&& !ok
) {
553 /* My DER encoding requires length<128 */
554 if (gss_supported
->elements
[mech
].length
< 128 &&
555 ssh_gssapi_check_mechanism(&gssctxt
,
556 &gss_supported
->elements
[mech
], authctxt
->host
)) {
557 ok
= 1; /* Mechanism works */
566 authctxt
->methoddata
=(void *)gssctxt
;
568 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
569 packet_put_cstring(authctxt
->server_user
);
570 packet_put_cstring(authctxt
->service
);
571 packet_put_cstring(authctxt
->method
->name
);
575 packet_put_int((gss_supported
->elements
[mech
].length
) + 2);
576 packet_put_char(SSH_GSS_OIDTYPE
);
577 packet_put_char(gss_supported
->elements
[mech
].length
);
578 packet_put_raw(gss_supported
->elements
[mech
].elements
,
579 gss_supported
->elements
[mech
].length
);
583 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE
, &input_gssapi_response
);
584 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN
, &input_gssapi_token
);
585 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR
, &input_gssapi_error
);
586 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK
, &input_gssapi_errtok
);
588 mech
++; /* Move along to next candidate */
594 process_gssapi_token(void *ctxt
, gss_buffer_t recv_tok
)
596 Authctxt
*authctxt
= ctxt
;
597 Gssctxt
*gssctxt
= authctxt
->methoddata
;
598 gss_buffer_desc send_tok
= GSS_C_EMPTY_BUFFER
;
599 gss_buffer_desc mic
= GSS_C_EMPTY_BUFFER
;
600 gss_buffer_desc gssbuf
;
601 OM_uint32 status
, ms
, flags
;
604 status
= ssh_gssapi_init_ctx(gssctxt
, options
.gss_deleg_creds
,
605 recv_tok
, &send_tok
, &flags
);
607 if (send_tok
.length
> 0) {
608 if (GSS_ERROR(status
))
609 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK
);
611 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN
);
613 packet_put_string(send_tok
.value
, send_tok
.length
);
615 gss_release_buffer(&ms
, &send_tok
);
618 if (status
== GSS_S_COMPLETE
) {
619 /* send either complete or MIC, depending on mechanism */
620 if (!(flags
& GSS_C_INTEG_FLAG
)) {
621 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE
);
624 ssh_gssapi_buildmic(&b
, authctxt
->server_user
,
625 authctxt
->service
, "gssapi-with-mic");
627 gssbuf
.value
= buffer_ptr(&b
);
628 gssbuf
.length
= buffer_len(&b
);
630 status
= ssh_gssapi_sign(gssctxt
, &gssbuf
, &mic
);
632 if (!GSS_ERROR(status
)) {
633 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC
);
634 packet_put_string(mic
.value
, mic
.length
);
640 gss_release_buffer(&ms
, &mic
);
649 input_gssapi_response(int type
, u_int32_t plen
, void *ctxt
)
651 Authctxt
*authctxt
= ctxt
;
656 if (authctxt
== NULL
)
657 fatal("input_gssapi_response: no authentication context");
658 gssctxt
= authctxt
->methoddata
;
661 oidv
= packet_get_string(&oidlen
);
664 oidv
[0] != SSH_GSS_OIDTYPE
||
665 oidv
[1] != oidlen
- 2) {
667 debug("Badly encoded mechanism OID received");
668 userauth(authctxt
, NULL
);
672 if (!ssh_gssapi_check_oid(gssctxt
, oidv
+ 2, oidlen
- 2))
673 fatal("Server returned different OID than expected");
679 if (GSS_ERROR(process_gssapi_token(ctxt
, GSS_C_NO_BUFFER
))) {
680 /* Start again with next method on list */
681 debug("Trying to start again");
682 userauth(authctxt
, NULL
);
689 input_gssapi_token(int type
, u_int32_t plen
, void *ctxt
)
691 Authctxt
*authctxt
= ctxt
;
692 gss_buffer_desc recv_tok
;
696 if (authctxt
== NULL
)
697 fatal("input_gssapi_response: no authentication context");
699 recv_tok
.value
= packet_get_string(&slen
);
700 recv_tok
.length
= slen
; /* safe typecast */
704 status
= process_gssapi_token(ctxt
, &recv_tok
);
706 xfree(recv_tok
.value
);
708 if (GSS_ERROR(status
)) {
709 /* Start again with the next method in the list */
710 userauth(authctxt
, NULL
);
717 input_gssapi_errtok(int type
, u_int32_t plen
, void *ctxt
)
719 Authctxt
*authctxt
= ctxt
;
721 gss_buffer_desc send_tok
= GSS_C_EMPTY_BUFFER
;
722 gss_buffer_desc recv_tok
;
723 OM_uint32 status
, ms
;
726 if (authctxt
== NULL
)
727 fatal("input_gssapi_response: no authentication context");
728 gssctxt
= authctxt
->methoddata
;
730 recv_tok
.value
= packet_get_string(&len
);
731 recv_tok
.length
= len
;
735 /* Stick it into GSSAPI and see what it says */
736 status
= ssh_gssapi_init_ctx(gssctxt
, options
.gss_deleg_creds
,
737 &recv_tok
, &send_tok
, NULL
);
739 xfree(recv_tok
.value
);
740 gss_release_buffer(&ms
, &send_tok
);
742 /* Server will be returning a failed packet after this one */
747 input_gssapi_error(int type
, u_int32_t plen
, void *ctxt
)
753 maj
=packet_get_int();
754 min
=packet_get_int();
755 msg
=packet_get_string(NULL
);
756 lang
=packet_get_string(NULL
);
760 debug("Server GSSAPI Error:\n%s", msg
);
767 userauth_none(Authctxt
*authctxt
)
769 /* initial userauth request */
770 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
771 packet_put_cstring(authctxt
->server_user
);
772 packet_put_cstring(authctxt
->service
);
773 packet_put_cstring(authctxt
->method
->name
);
779 userauth_passwd(Authctxt
*authctxt
)
781 static int attempt
= 0;
785 if (attempt
++ >= options
.number_of_password_prompts
)
789 error("Permission denied, please try again.");
791 snprintf(prompt
, sizeof(prompt
), "%.30s@%.128s's password: ",
792 authctxt
->server_user
, authctxt
->host
);
793 password
= read_passphrase(prompt
, 0);
794 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
795 packet_put_cstring(authctxt
->server_user
);
796 packet_put_cstring(authctxt
->service
);
797 packet_put_cstring(authctxt
->method
->name
);
799 packet_put_cstring(password
);
800 memset(password
, 0, strlen(password
));
802 packet_add_padding(64);
805 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ
,
806 &input_userauth_passwd_changereq
);
812 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
816 input_userauth_passwd_changereq(int type
, u_int32_t seqnr
, void *ctxt
)
818 Authctxt
*authctxt
= ctxt
;
819 char *info
, *lang
, *password
= NULL
, *retype
= NULL
;
822 debug2("input_userauth_passwd_changereq");
824 if (authctxt
== NULL
)
825 fatal("input_userauth_passwd_changereq: "
826 "no authentication context");
828 info
= packet_get_string(NULL
);
829 lang
= packet_get_string(NULL
);
830 if (strlen(info
) > 0)
834 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
835 packet_put_cstring(authctxt
->server_user
);
836 packet_put_cstring(authctxt
->service
);
837 packet_put_cstring(authctxt
->method
->name
);
838 packet_put_char(1); /* additional info */
839 snprintf(prompt
, sizeof(prompt
),
840 "Enter %.30s@%.128s's old password: ",
841 authctxt
->server_user
, authctxt
->host
);
842 password
= read_passphrase(prompt
, 0);
843 packet_put_cstring(password
);
844 memset(password
, 0, strlen(password
));
847 while (password
== NULL
) {
848 snprintf(prompt
, sizeof(prompt
),
849 "Enter %.30s@%.128s's new password: ",
850 authctxt
->server_user
, authctxt
->host
);
851 password
= read_passphrase(prompt
, RP_ALLOW_EOF
);
852 if (password
== NULL
) {
856 snprintf(prompt
, sizeof(prompt
),
857 "Retype %.30s@%.128s's new password: ",
858 authctxt
->server_user
, authctxt
->host
);
859 retype
= read_passphrase(prompt
, 0);
860 if (strcmp(password
, retype
) != 0) {
861 memset(password
, 0, strlen(password
));
863 logit("Mismatch; try again, EOF to quit.");
866 memset(retype
, 0, strlen(retype
));
869 packet_put_cstring(password
);
870 memset(password
, 0, strlen(password
));
872 packet_add_padding(64);
875 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ
,
876 &input_userauth_passwd_changereq
);
881 pw_encrypt(const char *password
, const char *crypt_scheme
, const char *salt
)
883 /* OpenBSD crypt(3) handles all of these */
884 if (strcmp(crypt_scheme
, "crypt") == 0 ||
885 strcmp(crypt_scheme
, "bcrypt") == 0 ||
886 strcmp(crypt_scheme
, "md5crypt") == 0 ||
887 strcmp(crypt_scheme
, "crypt-extended") == 0)
888 return xstrdup(crypt(password
, salt
));
889 error("%s: unsupported password encryption scheme \"%.100s\"",
890 __func__
, crypt_scheme
);
895 jpake_password_to_secret(Authctxt
*authctxt
, const char *crypt_scheme
,
898 char prompt
[256], *password
, *crypted
;
903 snprintf(prompt
, sizeof(prompt
), "%.30s@%.128s's password (JPAKE): ",
904 authctxt
->server_user
, authctxt
->host
);
905 password
= read_passphrase(prompt
, 0);
907 if ((crypted
= pw_encrypt(password
, crypt_scheme
, salt
)) == NULL
) {
908 logit("Disabling %s authentication", authctxt
->method
->name
);
909 authctxt
->method
->enabled
= NULL
;
910 /* Continue with an empty password to fail gracefully */
911 crypted
= xstrdup("");
915 debug3("%s: salt = %s", __func__
, salt
);
916 debug3("%s: scheme = %s", __func__
, crypt_scheme
);
917 debug3("%s: crypted = %s", __func__
, crypted
);
920 if (hash_buffer(crypted
, strlen(crypted
), EVP_sha256(),
921 &secret
, &secret_len
) != 0)
922 fatal("%s: hash_buffer", __func__
);
924 bzero(password
, strlen(password
));
925 bzero(crypted
, strlen(crypted
));
929 if ((ret
= BN_bin2bn(secret
, secret_len
, NULL
)) == NULL
)
930 fatal("%s: BN_bin2bn (secret)", __func__
);
931 bzero(secret
, secret_len
);
939 input_userauth_jpake_server_step1(int type
, u_int32_t seq
, void *ctxt
)
941 Authctxt
*authctxt
= ctxt
;
942 struct jpake_ctx
*pctx
= authctxt
->methoddata
;
943 u_char
*x3_proof
, *x4_proof
, *x2_s_proof
;
944 u_int x3_proof_len
, x4_proof_len
, x2_s_proof_len
;
945 char *crypt_scheme
, *salt
;
947 /* Disable this message */
948 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1
, NULL
);
950 if ((pctx
->g_x3
= BN_new()) == NULL
||
951 (pctx
->g_x4
= BN_new()) == NULL
)
952 fatal("%s: BN_new", __func__
);
954 /* Fetch step 1 values */
955 crypt_scheme
= packet_get_string(NULL
);
956 salt
= packet_get_string(NULL
);
957 pctx
->server_id
= packet_get_string(&pctx
->server_id_len
);
958 packet_get_bignum2(pctx
->g_x3
);
959 packet_get_bignum2(pctx
->g_x4
);
960 x3_proof
= packet_get_string(&x3_proof_len
);
961 x4_proof
= packet_get_string(&x4_proof_len
);
964 JPAKE_DEBUG_CTX((pctx
, "step 1 received in %s", __func__
));
966 /* Obtain password and derive secret */
967 pctx
->s
= jpake_password_to_secret(authctxt
, crypt_scheme
, salt
);
968 bzero(crypt_scheme
, strlen(crypt_scheme
));
969 bzero(salt
, strlen(salt
));
972 JPAKE_DEBUG_BN((pctx
->s
, "%s: s = ", __func__
));
974 /* Calculate step 2 values */
975 jpake_step2(pctx
->grp
, pctx
->s
, pctx
->g_x1
,
976 pctx
->g_x3
, pctx
->g_x4
, pctx
->x2
,
977 pctx
->server_id
, pctx
->server_id_len
,
978 pctx
->client_id
, pctx
->client_id_len
,
979 x3_proof
, x3_proof_len
,
980 x4_proof
, x4_proof_len
,
982 &x2_s_proof
, &x2_s_proof_len
);
984 bzero(x3_proof
, x3_proof_len
);
985 bzero(x4_proof
, x4_proof_len
);
989 JPAKE_DEBUG_CTX((pctx
, "step 2 sending in %s", __func__
));
991 /* Send values for step 2 */
992 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP2
);
993 packet_put_bignum2(pctx
->a
);
994 packet_put_string(x2_s_proof
, x2_s_proof_len
);
997 bzero(x2_s_proof
, x2_s_proof_len
);
1000 /* Expect step 2 packet from peer */
1001 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2
,
1002 input_userauth_jpake_server_step2
);
1007 input_userauth_jpake_server_step2(int type
, u_int32_t seq
, void *ctxt
)
1009 Authctxt
*authctxt
= ctxt
;
1010 struct jpake_ctx
*pctx
= authctxt
->methoddata
;
1012 u_int x4_s_proof_len
;
1014 /* Disable this message */
1015 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2
, NULL
);
1017 if ((pctx
->b
= BN_new()) == NULL
)
1018 fatal("%s: BN_new", __func__
);
1020 /* Fetch step 2 values */
1021 packet_get_bignum2(pctx
->b
);
1022 x4_s_proof
= packet_get_string(&x4_s_proof_len
);
1025 JPAKE_DEBUG_CTX((pctx
, "step 2 received in %s", __func__
));
1027 /* Derive shared key and calculate confirmation hash */
1028 jpake_key_confirm(pctx
->grp
, pctx
->s
, pctx
->b
,
1029 pctx
->x2
, pctx
->g_x1
, pctx
->g_x2
, pctx
->g_x3
, pctx
->g_x4
,
1030 pctx
->client_id
, pctx
->client_id_len
,
1031 pctx
->server_id
, pctx
->server_id_len
,
1032 session_id2
, session_id2_len
,
1033 x4_s_proof
, x4_s_proof_len
,
1035 &pctx
->h_k_cid_sessid
, &pctx
->h_k_cid_sessid_len
);
1037 bzero(x4_s_proof
, x4_s_proof_len
);
1040 JPAKE_DEBUG_CTX((pctx
, "confirm sending in %s", __func__
));
1042 /* Send key confirmation proof */
1043 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_CONFIRM
);
1044 packet_put_string(pctx
->h_k_cid_sessid
, pctx
->h_k_cid_sessid_len
);
1047 /* Expect confirmation from peer */
1048 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM
,
1049 input_userauth_jpake_server_confirm
);
1054 input_userauth_jpake_server_confirm(int type
, u_int32_t seq
, void *ctxt
)
1056 Authctxt
*authctxt
= ctxt
;
1057 struct jpake_ctx
*pctx
= authctxt
->methoddata
;
1059 /* Disable this message */
1060 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM
, NULL
);
1062 pctx
->h_k_sid_sessid
= packet_get_string(&pctx
->h_k_sid_sessid_len
);
1065 JPAKE_DEBUG_CTX((pctx
, "confirm received in %s", __func__
));
1067 /* Verify expected confirmation hash */
1068 if (jpake_check_confirm(pctx
->k
,
1069 pctx
->server_id
, pctx
->server_id_len
,
1070 session_id2
, session_id2_len
,
1071 pctx
->h_k_sid_sessid
, pctx
->h_k_sid_sessid_len
) == 1)
1072 debug("%s: %s success", __func__
, authctxt
->method
->name
);
1074 debug("%s: confirmation mismatch", __func__
);
1075 /* XXX stash this so if auth succeeds then we can warn/kill */
1078 userauth_jpake_cleanup(authctxt
);
1083 identity_sign(Identity
*id
, u_char
**sigp
, u_int
*lenp
,
1084 u_char
*data
, u_int datalen
)
1089 /* the agent supports this key */
1091 return (ssh_agent_sign(id
->ac
, id
->key
, sigp
, lenp
,
1094 * we have already loaded the private key or
1095 * the private key is stored in external hardware
1097 if (id
->isprivate
|| (id
->key
->flags
& KEY_FLAG_EXT
))
1098 return (key_sign(id
->key
, sigp
, lenp
, data
, datalen
));
1099 /* load the private key from the file */
1100 if ((prv
= load_identity_file(id
->filename
)) == NULL
)
1102 ret
= key_sign(prv
, sigp
, lenp
, data
, datalen
);
1108 sign_and_send_pubkey(Authctxt
*authctxt
, Identity
*id
)
1111 u_char
*blob
, *signature
;
1112 u_int bloblen
, slen
;
1117 debug3("sign_and_send_pubkey");
1119 if (key_to_blob(id
->key
, &blob
, &bloblen
) == 0) {
1120 /* we cannot handle this key */
1121 debug3("sign_and_send_pubkey: cannot handle key");
1124 /* data to be signed */
1126 if (datafellows
& SSH_OLD_SESSIONID
) {
1127 buffer_append(&b
, session_id2
, session_id2_len
);
1128 skip
= session_id2_len
;
1130 buffer_put_string(&b
, session_id2
, session_id2_len
);
1131 skip
= buffer_len(&b
);
1133 buffer_put_char(&b
, SSH2_MSG_USERAUTH_REQUEST
);
1134 buffer_put_cstring(&b
, authctxt
->server_user
);
1135 buffer_put_cstring(&b
,
1136 datafellows
& SSH_BUG_PKSERVICE
?
1139 if (datafellows
& SSH_BUG_PKAUTH
) {
1140 buffer_put_char(&b
, have_sig
);
1142 buffer_put_cstring(&b
, authctxt
->method
->name
);
1143 buffer_put_char(&b
, have_sig
);
1144 buffer_put_cstring(&b
, key_ssh_name(id
->key
));
1146 buffer_put_string(&b
, blob
, bloblen
);
1148 /* generate signature */
1149 ret
= identity_sign(id
, &signature
, &slen
,
1150 buffer_ptr(&b
), buffer_len(&b
));
1159 if (datafellows
& SSH_BUG_PKSERVICE
) {
1161 buffer_append(&b
, session_id2
, session_id2_len
);
1162 skip
= session_id2_len
;
1163 buffer_put_char(&b
, SSH2_MSG_USERAUTH_REQUEST
);
1164 buffer_put_cstring(&b
, authctxt
->server_user
);
1165 buffer_put_cstring(&b
, authctxt
->service
);
1166 buffer_put_cstring(&b
, authctxt
->method
->name
);
1167 buffer_put_char(&b
, have_sig
);
1168 if (!(datafellows
& SSH_BUG_PKAUTH
))
1169 buffer_put_cstring(&b
, key_ssh_name(id
->key
));
1170 buffer_put_string(&b
, blob
, bloblen
);
1174 /* append signature */
1175 buffer_put_string(&b
, signature
, slen
);
1178 /* skip session id and packet type */
1179 if (buffer_len(&b
) < skip
+ 1)
1180 fatal("userauth_pubkey: internal error");
1181 buffer_consume(&b
, skip
+ 1);
1183 /* put remaining data from buffer into packet */
1184 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
1185 packet_put_raw(buffer_ptr(&b
), buffer_len(&b
));
1193 send_pubkey_test(Authctxt
*authctxt
, Identity
*id
)
1196 u_int bloblen
, have_sig
= 0;
1198 debug3("send_pubkey_test");
1200 if (key_to_blob(id
->key
, &blob
, &bloblen
) == 0) {
1201 /* we cannot handle this key */
1202 debug3("send_pubkey_test: cannot handle key");
1205 /* register callback for USERAUTH_PK_OK message */
1206 dispatch_set(SSH2_MSG_USERAUTH_PK_OK
, &input_userauth_pk_ok
);
1208 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
1209 packet_put_cstring(authctxt
->server_user
);
1210 packet_put_cstring(authctxt
->service
);
1211 packet_put_cstring(authctxt
->method
->name
);
1212 packet_put_char(have_sig
);
1213 if (!(datafellows
& SSH_BUG_PKAUTH
))
1214 packet_put_cstring(key_ssh_name(id
->key
));
1215 packet_put_string(blob
, bloblen
);
1222 load_identity_file(char *filename
)
1225 char prompt
[300], *passphrase
;
1226 int perm_ok
, quit
, i
;
1229 if (stat(filename
, &st
) < 0) {
1230 debug3("no such identity: %s", filename
);
1233 private = key_load_private_type(KEY_UNSPEC
, filename
, "", NULL
, &perm_ok
);
1236 if (private == NULL
) {
1237 if (options
.batch_mode
)
1239 snprintf(prompt
, sizeof prompt
,
1240 "Enter passphrase for key '%.100s': ", filename
);
1241 for (i
= 0; i
< options
.number_of_password_prompts
; i
++) {
1242 passphrase
= read_passphrase(prompt
, 0);
1243 if (strcmp(passphrase
, "") != 0) {
1244 private = key_load_private_type(KEY_UNSPEC
,
1245 filename
, passphrase
, NULL
, NULL
);
1248 debug2("no passphrase given, try next key");
1251 memset(passphrase
, 0, strlen(passphrase
));
1253 if (private != NULL
|| quit
)
1255 debug2("bad passphrase given, try again...");
1262 * try keys in the following order:
1263 * 1. agent keys that are found in the config file
1264 * 2. other agent keys
1265 * 3. keys that are only listed in the config file
1268 pubkey_prepare(Authctxt
*authctxt
)
1271 Idlist agent
, files
, *preferred
;
1273 AuthenticationConnection
*ac
;
1277 TAILQ_INIT(&agent
); /* keys from the agent */
1278 TAILQ_INIT(&files
); /* keys from the config file */
1279 preferred
= &authctxt
->keys
;
1280 TAILQ_INIT(preferred
); /* preferred order of keys */
1282 /* list of keys stored in the filesystem */
1283 for (i
= 0; i
< options
.num_identity_files
; i
++) {
1284 key
= options
.identity_keys
[i
];
1285 if (key
&& key
->type
== KEY_RSA1
)
1287 options
.identity_keys
[i
] = NULL
;
1288 id
= xcalloc(1, sizeof(*id
));
1290 id
->filename
= xstrdup(options
.identity_files
[i
]);
1291 TAILQ_INSERT_TAIL(&files
, id
, next
);
1293 /* list of keys supported by the agent */
1294 if ((ac
= ssh_get_authentication_connection())) {
1295 for (key
= ssh_get_first_identity(ac
, &comment
, 2);
1297 key
= ssh_get_next_identity(ac
, &comment
, 2)) {
1299 TAILQ_FOREACH(id
, &files
, next
) {
1300 /* agent keys from the config file are preferred */
1301 if (key_equal(key
, id
->key
)) {
1304 TAILQ_REMOVE(&files
, id
, next
);
1305 TAILQ_INSERT_TAIL(preferred
, id
, next
);
1311 if (!found
&& !options
.identities_only
) {
1312 id
= xcalloc(1, sizeof(*id
));
1314 id
->filename
= comment
;
1316 TAILQ_INSERT_TAIL(&agent
, id
, next
);
1319 /* append remaining agent keys */
1320 for (id
= TAILQ_FIRST(&agent
); id
; id
= TAILQ_FIRST(&agent
)) {
1321 TAILQ_REMOVE(&agent
, id
, next
);
1322 TAILQ_INSERT_TAIL(preferred
, id
, next
);
1324 authctxt
->agent
= ac
;
1326 /* append remaining keys from the config file */
1327 for (id
= TAILQ_FIRST(&files
); id
; id
= TAILQ_FIRST(&files
)) {
1328 TAILQ_REMOVE(&files
, id
, next
);
1329 TAILQ_INSERT_TAIL(preferred
, id
, next
);
1331 TAILQ_FOREACH(id
, preferred
, next
) {
1332 debug2("key: %s (%p)", id
->filename
, id
->key
);
1337 pubkey_cleanup(Authctxt
*authctxt
)
1341 if (authctxt
->agent
!= NULL
)
1342 ssh_close_authentication_connection(authctxt
->agent
);
1343 for (id
= TAILQ_FIRST(&authctxt
->keys
); id
;
1344 id
= TAILQ_FIRST(&authctxt
->keys
)) {
1345 TAILQ_REMOVE(&authctxt
->keys
, id
, next
);
1349 xfree(id
->filename
);
1355 userauth_pubkey(Authctxt
*authctxt
)
1360 while ((id
= TAILQ_FIRST(&authctxt
->keys
))) {
1363 /* move key to the end of the queue */
1364 TAILQ_REMOVE(&authctxt
->keys
, id
, next
);
1365 TAILQ_INSERT_TAIL(&authctxt
->keys
, id
, next
);
1367 * send a test message if we have the public key. for
1368 * encrypted keys we cannot do this and have to load the
1369 * private key instead
1371 if (id
->key
&& id
->key
->type
!= KEY_RSA1
) {
1372 debug("Offering public key: %s", id
->filename
);
1373 sent
= send_pubkey_test(authctxt
, id
);
1374 } else if (id
->key
== NULL
) {
1375 debug("Trying private key: %s", id
->filename
);
1376 id
->key
= load_identity_file(id
->filename
);
1377 if (id
->key
!= NULL
) {
1379 sent
= sign_and_send_pubkey(authctxt
, id
);
1391 * Send userauth request message specifying keyboard-interactive method.
1394 userauth_kbdint(Authctxt
*authctxt
)
1396 static int attempt
= 0;
1398 if (attempt
++ >= options
.number_of_password_prompts
)
1400 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1401 if (attempt
> 1 && !authctxt
->info_req_seen
) {
1402 debug3("userauth_kbdint: disable: no info_req_seen");
1403 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST
, NULL
);
1407 debug2("userauth_kbdint");
1408 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
1409 packet_put_cstring(authctxt
->server_user
);
1410 packet_put_cstring(authctxt
->service
);
1411 packet_put_cstring(authctxt
->method
->name
);
1412 packet_put_cstring(""); /* lang */
1413 packet_put_cstring(options
.kbd_interactive_devices
?
1414 options
.kbd_interactive_devices
: "");
1417 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST
, &input_userauth_info_req
);
1422 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1425 input_userauth_info_req(int type
, u_int32_t seq
, void *ctxt
)
1427 Authctxt
*authctxt
= ctxt
;
1428 char *name
, *inst
, *lang
, *prompt
, *response
;
1429 u_int num_prompts
, i
;
1432 debug2("input_userauth_info_req");
1434 if (authctxt
== NULL
)
1435 fatal("input_userauth_info_req: no authentication context");
1437 authctxt
->info_req_seen
= 1;
1439 name
= packet_get_string(NULL
);
1440 inst
= packet_get_string(NULL
);
1441 lang
= packet_get_string(NULL
);
1442 if (strlen(name
) > 0)
1444 if (strlen(inst
) > 0)
1450 num_prompts
= packet_get_int();
1452 * Begin to build info response packet based on prompts requested.
1453 * We commit to providing the correct number of responses, so if
1454 * further on we run into a problem that prevents this, we have to
1455 * be sure and clean this up and send a correct error response.
1457 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE
);
1458 packet_put_int(num_prompts
);
1460 debug2("input_userauth_info_req: num_prompts %d", num_prompts
);
1461 for (i
= 0; i
< num_prompts
; i
++) {
1462 prompt
= packet_get_string(NULL
);
1463 echo
= packet_get_char();
1465 response
= read_passphrase(prompt
, echo
? RP_ECHO
: 0);
1467 packet_put_cstring(response
);
1468 memset(response
, 0, strlen(response
));
1472 packet_check_eom(); /* done with parsing incoming message. */
1474 packet_add_padding(64);
1479 ssh_keysign(Key
*key
, u_char
**sigp
, u_int
*lenp
,
1480 u_char
*data
, u_int datalen
)
1485 int to
[2], from
[2], status
, version
= 2;
1487 debug2("ssh_keysign called");
1489 if (stat(_PATH_SSH_KEY_SIGN
, &st
) < 0) {
1490 error("ssh_keysign: no installed: %s", strerror(errno
));
1493 if (fflush(stdout
) != 0)
1494 error("ssh_keysign: fflush: %s", strerror(errno
));
1496 error("ssh_keysign: pipe: %s", strerror(errno
));
1499 if (pipe(from
) < 0) {
1500 error("ssh_keysign: pipe: %s", strerror(errno
));
1503 if ((pid
= fork()) < 0) {
1504 error("ssh_keysign: fork: %s", strerror(errno
));
1508 permanently_drop_suid(getuid());
1510 if (dup2(from
[1], STDOUT_FILENO
) < 0)
1511 fatal("ssh_keysign: dup2: %s", strerror(errno
));
1513 if (dup2(to
[0], STDIN_FILENO
) < 0)
1514 fatal("ssh_keysign: dup2: %s", strerror(errno
));
1517 execl(_PATH_SSH_KEY_SIGN
, _PATH_SSH_KEY_SIGN
, (char *) 0);
1518 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN
,
1525 buffer_put_int(&b
, packet_get_connection_in()); /* send # of socket */
1526 buffer_put_string(&b
, data
, datalen
);
1527 if (ssh_msg_send(to
[1], version
, &b
) == -1)
1528 fatal("ssh_keysign: couldn't send request");
1530 if (ssh_msg_recv(from
[0], &b
) < 0) {
1531 error("ssh_keysign: no reply");
1538 while (waitpid(pid
, &status
, 0) < 0)
1542 if (buffer_get_char(&b
) != version
) {
1543 error("ssh_keysign: bad version");
1547 *sigp
= buffer_get_string(&b
, lenp
);
1554 userauth_hostbased(Authctxt
*authctxt
)
1556 Key
*private = NULL
;
1557 Sensitive
*sensitive
= authctxt
->sensitive
;
1559 u_char
*signature
, *blob
;
1560 char *chost
, *pkalg
, *p
, myname
[NI_MAXHOST
];
1561 const char *service
;
1563 int ok
, i
, len
, found
= 0;
1565 /* check for a useful key */
1566 for (i
= 0; i
< sensitive
->nkeys
; i
++) {
1567 private = sensitive
->keys
[i
];
1568 if (private && private->type
!= KEY_RSA1
) {
1570 /* we take and free the key */
1571 sensitive
->keys
[i
] = NULL
;
1576 debug("No more client hostkeys for hostbased authentication.");
1579 if (key_to_blob(private, &blob
, &blen
) == 0) {
1583 /* figure out a name for the client host */
1585 if (packet_connection_is_on_socket())
1586 p
= get_local_name(packet_get_connection_in());
1588 if (gethostname(myname
, sizeof(myname
)) == -1) {
1589 verbose("userauth_hostbased: gethostname: %s",
1592 p
= xstrdup(myname
);
1595 error("userauth_hostbased: cannot get local ipaddr/name");
1600 len
= strlen(p
) + 2;
1601 xasprintf(&chost
, "%s.", p
);
1602 debug2("userauth_hostbased: chost %s", chost
);
1605 service
= datafellows
& SSH_BUG_HBSERVICE
? "ssh-userauth" :
1607 pkalg
= xstrdup(key_ssh_name(private));
1609 /* construct data */
1610 buffer_put_string(&b
, session_id2
, session_id2_len
);
1611 buffer_put_char(&b
, SSH2_MSG_USERAUTH_REQUEST
);
1612 buffer_put_cstring(&b
, authctxt
->server_user
);
1613 buffer_put_cstring(&b
, service
);
1614 buffer_put_cstring(&b
, authctxt
->method
->name
);
1615 buffer_put_cstring(&b
, pkalg
);
1616 buffer_put_string(&b
, blob
, blen
);
1617 buffer_put_cstring(&b
, chost
);
1618 buffer_put_cstring(&b
, authctxt
->local_user
);
1622 if (sensitive
->external_keysign
)
1623 ok
= ssh_keysign(private, &signature
, &slen
,
1624 buffer_ptr(&b
), buffer_len(&b
));
1626 ok
= key_sign(private, &signature
, &slen
,
1627 buffer_ptr(&b
), buffer_len(&b
));
1631 error("key_sign failed");
1637 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
1638 packet_put_cstring(authctxt
->server_user
);
1639 packet_put_cstring(authctxt
->service
);
1640 packet_put_cstring(authctxt
->method
->name
);
1641 packet_put_cstring(pkalg
);
1642 packet_put_string(blob
, blen
);
1643 packet_put_cstring(chost
);
1644 packet_put_cstring(authctxt
->local_user
);
1645 packet_put_string(signature
, slen
);
1646 memset(signature
, 's', slen
);
1658 userauth_jpake(Authctxt
*authctxt
)
1660 struct jpake_ctx
*pctx
;
1661 u_char
*x1_proof
, *x2_proof
;
1662 u_int x1_proof_len
, x2_proof_len
;
1663 static int attempt
= 0; /* XXX share with userauth_password's? */
1665 if (attempt
++ >= options
.number_of_password_prompts
)
1668 error("Permission denied, please try again.");
1670 if (authctxt
->methoddata
!= NULL
)
1671 fatal("%s: authctxt->methoddata already set (%p)",
1672 __func__
, authctxt
->methoddata
);
1674 authctxt
->methoddata
= pctx
= jpake_new();
1677 * Send request immediately, to get the protocol going while
1678 * we do the initial computations.
1680 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
1681 packet_put_cstring(authctxt
->server_user
);
1682 packet_put_cstring(authctxt
->service
);
1683 packet_put_cstring(authctxt
->method
->name
);
1685 packet_write_wait();
1687 jpake_step1(pctx
->grp
,
1688 &pctx
->client_id
, &pctx
->client_id_len
,
1689 &pctx
->x1
, &pctx
->x2
, &pctx
->g_x1
, &pctx
->g_x2
,
1690 &x1_proof
, &x1_proof_len
,
1691 &x2_proof
, &x2_proof_len
);
1693 JPAKE_DEBUG_CTX((pctx
, "step 1 sending in %s", __func__
));
1695 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP1
);
1696 packet_put_string(pctx
->client_id
, pctx
->client_id_len
);
1697 packet_put_bignum2(pctx
->g_x1
);
1698 packet_put_bignum2(pctx
->g_x2
);
1699 packet_put_string(x1_proof
, x1_proof_len
);
1700 packet_put_string(x2_proof
, x2_proof_len
);
1703 bzero(x1_proof
, x1_proof_len
);
1704 bzero(x2_proof
, x2_proof_len
);
1708 /* Expect step 1 packet from peer */
1709 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1
,
1710 input_userauth_jpake_server_step1
);
1716 userauth_jpake_cleanup(Authctxt
*authctxt
)
1718 debug3("%s: clean up", __func__
);
1719 if (authctxt
->methoddata
!= NULL
) {
1720 jpake_free(authctxt
->methoddata
);
1721 authctxt
->methoddata
= NULL
;
1726 /* find auth method */
1729 * given auth method name, if configurable options permit this method fill
1730 * in auth_ident field and return true, otherwise return false.
1733 authmethod_is_enabled(Authmethod
*method
)
1737 /* return false if options indicate this method is disabled */
1738 if (method
->enabled
== NULL
|| *method
->enabled
== 0)
1740 /* return false if batch mode is enabled but method needs interactive mode */
1741 if (method
->batch_flag
!= NULL
&& *method
->batch_flag
!= 0)
1747 authmethod_lookup(const char *name
)
1749 Authmethod
*method
= NULL
;
1751 for (method
= authmethods
; method
->name
!= NULL
; method
++)
1752 if (strcmp(name
, method
->name
) == 0)
1754 debug2("Unrecognized authentication method name: %s", name
? name
: "NULL");
1758 /* XXX internal state */
1759 static Authmethod
*current
= NULL
;
1760 static char *supported
= NULL
;
1761 static char *preferred
= NULL
;
1764 * Given the authentication method list sent by the server, return the
1765 * next method we should try. If the server initially sends a nil list,
1766 * use a built-in default list.
1769 authmethod_get(char *authlist
)
1774 /* Use a suitable default if we're passed a nil list. */
1775 if (authlist
== NULL
|| strlen(authlist
) == 0)
1776 authlist
= options
.preferred_authentications
;
1778 if (supported
== NULL
|| strcmp(authlist
, supported
) != 0) {
1779 debug3("start over, passed a different list %s", authlist
);
1780 if (supported
!= NULL
)
1782 supported
= xstrdup(authlist
);
1783 preferred
= options
.preferred_authentications
;
1784 debug3("preferred %s", preferred
);
1786 } else if (current
!= NULL
&& authmethod_is_enabled(current
))
1790 if ((name
= match_list(preferred
, supported
, &next
)) == NULL
) {
1791 debug("No more authentication methods to try.");
1796 debug3("authmethod_lookup %s", name
);
1797 debug3("remaining preferred: %s", preferred
);
1798 if ((current
= authmethod_lookup(name
)) != NULL
&&
1799 authmethod_is_enabled(current
)) {
1800 debug3("authmethod_is_enabled %s", name
);
1801 debug("Next authentication method: %s", name
);
1808 authmethods_get(void)
1810 Authmethod
*method
= NULL
;
1815 for (method
= authmethods
; method
->name
!= NULL
; method
++) {
1816 if (authmethod_is_enabled(method
)) {
1817 if (buffer_len(&b
) > 0)
1818 buffer_append(&b
, ",", 1);
1819 buffer_append(&b
, method
->name
, strlen(method
->name
));
1822 buffer_append(&b
, "\0", 1);
1823 list
= xstrdup(buffer_ptr(&b
));