1 /* $OpenBSD: sshconnect2.c,v 1.171 2009/03/05 07:18:19 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_failure(int, u_int32_t
, void *);
214 void input_userauth_banner(int, u_int32_t
, void *);
215 void input_userauth_error(int, u_int32_t
, void *);
216 void input_userauth_info_req(int, u_int32_t
, void *);
217 void input_userauth_pk_ok(int, u_int32_t
, void *);
218 void input_userauth_passwd_changereq(int, u_int32_t
, void *);
219 void input_userauth_jpake_server_step1(int, u_int32_t
, void *);
220 void input_userauth_jpake_server_step2(int, u_int32_t
, void *);
221 void input_userauth_jpake_server_confirm(int, u_int32_t
, void *);
223 int userauth_none(Authctxt
*);
224 int userauth_pubkey(Authctxt
*);
225 int userauth_passwd(Authctxt
*);
226 int userauth_kbdint(Authctxt
*);
227 int userauth_hostbased(Authctxt
*);
228 int userauth_jpake(Authctxt
*);
230 void userauth_jpake_cleanup(Authctxt
*);
233 int userauth_gssapi(Authctxt
*authctxt
);
234 void input_gssapi_response(int type
, u_int32_t
, void *);
235 void input_gssapi_token(int type
, u_int32_t
, void *);
236 void input_gssapi_hash(int type
, u_int32_t
, void *);
237 void input_gssapi_error(int, u_int32_t
, void *);
238 void input_gssapi_errtok(int, u_int32_t
, void *);
241 void userauth(Authctxt
*, char *);
243 static int sign_and_send_pubkey(Authctxt
*, Identity
*);
244 static void pubkey_prepare(Authctxt
*);
245 static void pubkey_cleanup(Authctxt
*);
246 static Key
*load_identity_file(char *);
248 static Authmethod
*authmethod_get(char *authlist
);
249 static Authmethod
*authmethod_lookup(const char *name
);
250 static char *authmethods_get(void);
252 Authmethod authmethods
[] = {
257 &options
.gss_authentication
,
263 &options
.hostbased_authentication
,
268 &options
.pubkey_authentication
,
271 {"jpake-01@openssh.com",
273 userauth_jpake_cleanup
,
274 &options
.zero_knowledge_password_authentication
,
275 &options
.batch_mode
},
277 {"keyboard-interactive",
280 &options
.kbd_interactive_authentication
,
281 &options
.batch_mode
},
285 &options
.password_authentication
,
286 &options
.batch_mode
},
292 {NULL
, NULL
, NULL
, NULL
, NULL
}
296 ssh_userauth2(const char *local_user
, const char *server_user
, char *host
,
297 Sensitive
*sensitive
)
302 if (options
.challenge_response_authentication
)
303 options
.kbd_interactive_authentication
= 1;
305 packet_start(SSH2_MSG_SERVICE_REQUEST
);
306 packet_put_cstring("ssh-userauth");
308 debug("SSH2_MSG_SERVICE_REQUEST sent");
310 type
= packet_read();
311 if (type
!= SSH2_MSG_SERVICE_ACCEPT
)
312 fatal("Server denied authentication request: %d", type
);
313 if (packet_remaining() > 0) {
314 char *reply
= packet_get_string(NULL
);
315 debug2("service_accept: %s", reply
);
318 debug2("buggy server: service_accept w/o service");
321 debug("SSH2_MSG_SERVICE_ACCEPT received");
323 if (options
.preferred_authentications
== NULL
)
324 options
.preferred_authentications
= authmethods_get();
326 /* setup authentication context */
327 memset(&authctxt
, 0, sizeof(authctxt
));
328 pubkey_prepare(&authctxt
);
329 authctxt
.server_user
= server_user
;
330 authctxt
.local_user
= local_user
;
331 authctxt
.host
= host
;
332 authctxt
.service
= "ssh-connection"; /* service name */
333 authctxt
.success
= 0;
334 authctxt
.method
= authmethod_lookup("none");
335 authctxt
.authlist
= NULL
;
336 authctxt
.methoddata
= NULL
;
337 authctxt
.sensitive
= sensitive
;
338 authctxt
.info_req_seen
= 0;
339 if (authctxt
.method
== NULL
)
340 fatal("ssh_userauth2: internal error: cannot send userauth none request");
342 /* initial userauth request */
343 userauth_none(&authctxt
);
345 dispatch_init(&input_userauth_error
);
346 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS
, &input_userauth_success
);
347 dispatch_set(SSH2_MSG_USERAUTH_FAILURE
, &input_userauth_failure
);
348 dispatch_set(SSH2_MSG_USERAUTH_BANNER
, &input_userauth_banner
);
349 dispatch_run(DISPATCH_BLOCK
, &authctxt
.success
, &authctxt
); /* loop until success */
351 pubkey_cleanup(&authctxt
);
352 dispatch_range(SSH2_MSG_USERAUTH_MIN
, SSH2_MSG_USERAUTH_MAX
, NULL
);
354 debug("Authentication succeeded (%s).", authctxt
.method
->name
);
358 userauth(Authctxt
*authctxt
, char *authlist
)
360 if (authctxt
->method
!= NULL
&& authctxt
->method
->cleanup
!= NULL
)
361 authctxt
->method
->cleanup(authctxt
);
363 if (authctxt
->methoddata
) {
364 xfree(authctxt
->methoddata
);
365 authctxt
->methoddata
= NULL
;
367 if (authlist
== NULL
) {
368 authlist
= authctxt
->authlist
;
370 if (authctxt
->authlist
)
371 xfree(authctxt
->authlist
);
372 authctxt
->authlist
= authlist
;
375 Authmethod
*method
= authmethod_get(authlist
);
377 fatal("Permission denied (%s).", authlist
);
378 authctxt
->method
= method
;
380 /* reset the per method handler */
381 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN
,
382 SSH2_MSG_USERAUTH_PER_METHOD_MAX
, NULL
);
384 /* and try new method */
385 if (method
->userauth(authctxt
) != 0) {
386 debug2("we sent a %s packet, wait for reply", method
->name
);
389 debug2("we did not send a packet, disable method");
390 method
->enabled
= NULL
;
397 input_userauth_error(int type
, u_int32_t seq
, void *ctxt
)
399 fatal("input_userauth_error: bad message during authentication: "
405 input_userauth_banner(int type
, u_int32_t seq
, void *ctxt
)
407 char *msg
, *raw
, *lang
;
410 debug3("input_userauth_banner");
411 raw
= packet_get_string(&len
);
412 lang
= packet_get_string(NULL
);
413 if (len
> 0 && options
.log_level
>= SYSLOG_LEVEL_INFO
) {
416 msg
= xmalloc(len
* 4 + 1); /* max expansion from strnvis() */
417 strnvis(msg
, raw
, len
* 4 + 1, VIS_SAFE
|VIS_OCTAL
);
418 fprintf(stderr
, "%s", msg
);
427 input_userauth_success(int type
, u_int32_t seq
, void *ctxt
)
429 Authctxt
*authctxt
= ctxt
;
430 if (authctxt
== NULL
)
431 fatal("input_userauth_success: no authentication context");
432 if (authctxt
->authlist
) {
433 xfree(authctxt
->authlist
);
434 authctxt
->authlist
= NULL
;
436 if (authctxt
->methoddata
) {
437 xfree(authctxt
->methoddata
);
438 authctxt
->methoddata
= NULL
;
440 authctxt
->success
= 1; /* break out */
445 input_userauth_failure(int type
, u_int32_t seq
, void *ctxt
)
447 Authctxt
*authctxt
= ctxt
;
448 char *authlist
= NULL
;
451 if (authctxt
== NULL
)
452 fatal("input_userauth_failure: no authentication context");
454 authlist
= packet_get_string(NULL
);
455 partial
= packet_get_char();
459 logit("Authenticated with partial success.");
460 debug("Authentications that can continue: %s", authlist
);
462 userauth(authctxt
, authlist
);
467 input_userauth_pk_ok(int type
, u_int32_t seq
, void *ctxt
)
469 Authctxt
*authctxt
= ctxt
;
473 int pktype
, sent
= 0;
478 if (authctxt
== NULL
)
479 fatal("input_userauth_pk_ok: no authentication context");
480 if (datafellows
& SSH_BUG_PKOK
) {
481 /* this is similar to SSH_BUG_PKAUTH */
482 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
483 pkblob
= packet_get_string(&blen
);
485 buffer_append(&b
, pkblob
, blen
);
486 pkalg
= buffer_get_string(&b
, &alen
);
489 pkalg
= packet_get_string(&alen
);
490 pkblob
= packet_get_string(&blen
);
494 debug("Server accepts key: pkalg %s blen %u", pkalg
, blen
);
496 if ((pktype
= key_type_from_name(pkalg
)) == KEY_UNSPEC
) {
497 debug("unknown pkalg %s", pkalg
);
500 if ((key
= key_from_blob(pkblob
, blen
)) == NULL
) {
501 debug("no key from blob. pkalg %s", pkalg
);
504 if (key
->type
!= pktype
) {
505 error("input_userauth_pk_ok: type mismatch "
506 "for decoded key (received %d, expected %d)",
510 fp
= key_fingerprint(key
, SSH_FP_MD5
, SSH_FP_HEX
);
511 debug2("input_userauth_pk_ok: fp %s", fp
);
515 * search keys in the reverse order, because last candidate has been
516 * moved to the end of the queue. this also avoids confusion by
519 TAILQ_FOREACH_REVERSE(id
, &authctxt
->keys
, idlist
, next
) {
520 if (key_equal(key
, id
->key
)) {
521 sent
= sign_and_send_pubkey(authctxt
, id
);
531 /* try another method if we did not send a packet */
533 userauth(authctxt
, NULL
);
538 userauth_gssapi(Authctxt
*authctxt
)
540 Gssctxt
*gssctxt
= NULL
;
541 static gss_OID_set gss_supported
= NULL
;
542 static u_int mech
= 0;
546 /* Try one GSSAPI method at a time, rather than sending them all at
549 if (gss_supported
== NULL
)
550 gss_indicate_mechs(&min
, &gss_supported
);
552 /* Check to see if the mechanism is usable before we offer it */
553 while (mech
< gss_supported
->count
&& !ok
) {
554 /* My DER encoding requires length<128 */
555 if (gss_supported
->elements
[mech
].length
< 128 &&
556 ssh_gssapi_check_mechanism(&gssctxt
,
557 &gss_supported
->elements
[mech
], authctxt
->host
)) {
558 ok
= 1; /* Mechanism works */
567 authctxt
->methoddata
=(void *)gssctxt
;
569 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
570 packet_put_cstring(authctxt
->server_user
);
571 packet_put_cstring(authctxt
->service
);
572 packet_put_cstring(authctxt
->method
->name
);
576 packet_put_int((gss_supported
->elements
[mech
].length
) + 2);
577 packet_put_char(SSH_GSS_OIDTYPE
);
578 packet_put_char(gss_supported
->elements
[mech
].length
);
579 packet_put_raw(gss_supported
->elements
[mech
].elements
,
580 gss_supported
->elements
[mech
].length
);
584 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE
, &input_gssapi_response
);
585 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN
, &input_gssapi_token
);
586 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR
, &input_gssapi_error
);
587 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK
, &input_gssapi_errtok
);
589 mech
++; /* Move along to next candidate */
595 process_gssapi_token(void *ctxt
, gss_buffer_t recv_tok
)
597 Authctxt
*authctxt
= ctxt
;
598 Gssctxt
*gssctxt
= authctxt
->methoddata
;
599 gss_buffer_desc send_tok
= GSS_C_EMPTY_BUFFER
;
600 gss_buffer_desc mic
= GSS_C_EMPTY_BUFFER
;
601 gss_buffer_desc gssbuf
;
602 OM_uint32 status
, ms
, flags
;
605 status
= ssh_gssapi_init_ctx(gssctxt
, options
.gss_deleg_creds
,
606 recv_tok
, &send_tok
, &flags
);
608 if (send_tok
.length
> 0) {
609 if (GSS_ERROR(status
))
610 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK
);
612 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN
);
614 packet_put_string(send_tok
.value
, send_tok
.length
);
616 gss_release_buffer(&ms
, &send_tok
);
619 if (status
== GSS_S_COMPLETE
) {
620 /* send either complete or MIC, depending on mechanism */
621 if (!(flags
& GSS_C_INTEG_FLAG
)) {
622 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE
);
625 ssh_gssapi_buildmic(&b
, authctxt
->server_user
,
626 authctxt
->service
, "gssapi-with-mic");
628 gssbuf
.value
= buffer_ptr(&b
);
629 gssbuf
.length
= buffer_len(&b
);
631 status
= ssh_gssapi_sign(gssctxt
, &gssbuf
, &mic
);
633 if (!GSS_ERROR(status
)) {
634 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC
);
635 packet_put_string(mic
.value
, mic
.length
);
641 gss_release_buffer(&ms
, &mic
);
650 input_gssapi_response(int type
, u_int32_t plen
, void *ctxt
)
652 Authctxt
*authctxt
= ctxt
;
657 if (authctxt
== NULL
)
658 fatal("input_gssapi_response: no authentication context");
659 gssctxt
= authctxt
->methoddata
;
662 oidv
= packet_get_string(&oidlen
);
665 oidv
[0] != SSH_GSS_OIDTYPE
||
666 oidv
[1] != oidlen
- 2) {
668 debug("Badly encoded mechanism OID received");
669 userauth(authctxt
, NULL
);
673 if (!ssh_gssapi_check_oid(gssctxt
, oidv
+ 2, oidlen
- 2))
674 fatal("Server returned different OID than expected");
680 if (GSS_ERROR(process_gssapi_token(ctxt
, GSS_C_NO_BUFFER
))) {
681 /* Start again with next method on list */
682 debug("Trying to start again");
683 userauth(authctxt
, NULL
);
690 input_gssapi_token(int type
, u_int32_t plen
, void *ctxt
)
692 Authctxt
*authctxt
= ctxt
;
693 gss_buffer_desc recv_tok
;
697 if (authctxt
== NULL
)
698 fatal("input_gssapi_response: no authentication context");
700 recv_tok
.value
= packet_get_string(&slen
);
701 recv_tok
.length
= slen
; /* safe typecast */
705 status
= process_gssapi_token(ctxt
, &recv_tok
);
707 xfree(recv_tok
.value
);
709 if (GSS_ERROR(status
)) {
710 /* Start again with the next method in the list */
711 userauth(authctxt
, NULL
);
718 input_gssapi_errtok(int type
, u_int32_t plen
, void *ctxt
)
720 Authctxt
*authctxt
= ctxt
;
722 gss_buffer_desc send_tok
= GSS_C_EMPTY_BUFFER
;
723 gss_buffer_desc recv_tok
;
724 OM_uint32 status
, ms
;
727 if (authctxt
== NULL
)
728 fatal("input_gssapi_response: no authentication context");
729 gssctxt
= authctxt
->methoddata
;
731 recv_tok
.value
= packet_get_string(&len
);
732 recv_tok
.length
= len
;
736 /* Stick it into GSSAPI and see what it says */
737 status
= ssh_gssapi_init_ctx(gssctxt
, options
.gss_deleg_creds
,
738 &recv_tok
, &send_tok
, NULL
);
740 xfree(recv_tok
.value
);
741 gss_release_buffer(&ms
, &send_tok
);
743 /* Server will be returning a failed packet after this one */
748 input_gssapi_error(int type
, u_int32_t plen
, void *ctxt
)
754 maj
=packet_get_int();
755 min
=packet_get_int();
756 msg
=packet_get_string(NULL
);
757 lang
=packet_get_string(NULL
);
761 debug("Server GSSAPI Error:\n%s", msg
);
768 userauth_none(Authctxt
*authctxt
)
770 /* initial userauth request */
771 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
772 packet_put_cstring(authctxt
->server_user
);
773 packet_put_cstring(authctxt
->service
);
774 packet_put_cstring(authctxt
->method
->name
);
780 userauth_passwd(Authctxt
*authctxt
)
782 static int attempt
= 0;
786 if (attempt
++ >= options
.number_of_password_prompts
)
790 error("Permission denied, please try again.");
792 snprintf(prompt
, sizeof(prompt
), "%.30s@%.128s's password: ",
793 authctxt
->server_user
, authctxt
->host
);
794 password
= read_passphrase(prompt
, 0);
795 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
796 packet_put_cstring(authctxt
->server_user
);
797 packet_put_cstring(authctxt
->service
);
798 packet_put_cstring(authctxt
->method
->name
);
800 packet_put_cstring(password
);
801 memset(password
, 0, strlen(password
));
803 packet_add_padding(64);
806 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ
,
807 &input_userauth_passwd_changereq
);
813 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
817 input_userauth_passwd_changereq(int type
, u_int32_t seqnr
, void *ctxt
)
819 Authctxt
*authctxt
= ctxt
;
820 char *info
, *lang
, *password
= NULL
, *retype
= NULL
;
823 debug2("input_userauth_passwd_changereq");
825 if (authctxt
== NULL
)
826 fatal("input_userauth_passwd_changereq: "
827 "no authentication context");
829 info
= packet_get_string(NULL
);
830 lang
= packet_get_string(NULL
);
831 if (strlen(info
) > 0)
835 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
836 packet_put_cstring(authctxt
->server_user
);
837 packet_put_cstring(authctxt
->service
);
838 packet_put_cstring(authctxt
->method
->name
);
839 packet_put_char(1); /* additional info */
840 snprintf(prompt
, sizeof(prompt
),
841 "Enter %.30s@%.128s's old password: ",
842 authctxt
->server_user
, authctxt
->host
);
843 password
= read_passphrase(prompt
, 0);
844 packet_put_cstring(password
);
845 memset(password
, 0, strlen(password
));
848 while (password
== NULL
) {
849 snprintf(prompt
, sizeof(prompt
),
850 "Enter %.30s@%.128s's new password: ",
851 authctxt
->server_user
, authctxt
->host
);
852 password
= read_passphrase(prompt
, RP_ALLOW_EOF
);
853 if (password
== NULL
) {
857 snprintf(prompt
, sizeof(prompt
),
858 "Retype %.30s@%.128s's new password: ",
859 authctxt
->server_user
, authctxt
->host
);
860 retype
= read_passphrase(prompt
, 0);
861 if (strcmp(password
, retype
) != 0) {
862 memset(password
, 0, strlen(password
));
864 logit("Mismatch; try again, EOF to quit.");
867 memset(retype
, 0, strlen(retype
));
870 packet_put_cstring(password
);
871 memset(password
, 0, strlen(password
));
873 packet_add_padding(64);
876 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ
,
877 &input_userauth_passwd_changereq
);
882 pw_encrypt(const char *password
, const char *crypt_scheme
, const char *salt
)
884 /* OpenBSD crypt(3) handles all of these */
885 if (strcmp(crypt_scheme
, "crypt") == 0 ||
886 strcmp(crypt_scheme
, "bcrypt") == 0 ||
887 strcmp(crypt_scheme
, "md5crypt") == 0 ||
888 strcmp(crypt_scheme
, "crypt-extended") == 0)
889 return xstrdup(crypt(password
, salt
));
890 error("%s: unsupported password encryption scheme \"%.100s\"",
891 __func__
, crypt_scheme
);
896 jpake_password_to_secret(Authctxt
*authctxt
, const char *crypt_scheme
,
899 char prompt
[256], *password
, *crypted
;
904 snprintf(prompt
, sizeof(prompt
), "%.30s@%.128s's password (JPAKE): ",
905 authctxt
->server_user
, authctxt
->host
);
906 password
= read_passphrase(prompt
, 0);
908 if ((crypted
= pw_encrypt(password
, crypt_scheme
, salt
)) == NULL
) {
909 logit("Disabling %s authentication", authctxt
->method
->name
);
910 authctxt
->method
->enabled
= NULL
;
911 /* Continue with an empty password to fail gracefully */
912 crypted
= xstrdup("");
916 debug3("%s: salt = %s", __func__
, salt
);
917 debug3("%s: scheme = %s", __func__
, crypt_scheme
);
918 debug3("%s: crypted = %s", __func__
, crypted
);
921 if (hash_buffer(crypted
, strlen(crypted
), EVP_sha256(),
922 &secret
, &secret_len
) != 0)
923 fatal("%s: hash_buffer", __func__
);
925 bzero(password
, strlen(password
));
926 bzero(crypted
, strlen(crypted
));
930 if ((ret
= BN_bin2bn(secret
, secret_len
, NULL
)) == NULL
)
931 fatal("%s: BN_bin2bn (secret)", __func__
);
932 bzero(secret
, secret_len
);
940 input_userauth_jpake_server_step1(int type
, u_int32_t seq
, void *ctxt
)
942 Authctxt
*authctxt
= ctxt
;
943 struct jpake_ctx
*pctx
= authctxt
->methoddata
;
944 u_char
*x3_proof
, *x4_proof
, *x2_s_proof
;
945 u_int x3_proof_len
, x4_proof_len
, x2_s_proof_len
;
946 char *crypt_scheme
, *salt
;
948 /* Disable this message */
949 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1
, NULL
);
951 if ((pctx
->g_x3
= BN_new()) == NULL
||
952 (pctx
->g_x4
= BN_new()) == NULL
)
953 fatal("%s: BN_new", __func__
);
955 /* Fetch step 1 values */
956 crypt_scheme
= packet_get_string(NULL
);
957 salt
= packet_get_string(NULL
);
958 pctx
->server_id
= packet_get_string(&pctx
->server_id_len
);
959 packet_get_bignum2(pctx
->g_x3
);
960 packet_get_bignum2(pctx
->g_x4
);
961 x3_proof
= packet_get_string(&x3_proof_len
);
962 x4_proof
= packet_get_string(&x4_proof_len
);
965 JPAKE_DEBUG_CTX((pctx
, "step 1 received in %s", __func__
));
967 /* Obtain password and derive secret */
968 pctx
->s
= jpake_password_to_secret(authctxt
, crypt_scheme
, salt
);
969 bzero(crypt_scheme
, strlen(crypt_scheme
));
970 bzero(salt
, strlen(salt
));
973 JPAKE_DEBUG_BN((pctx
->s
, "%s: s = ", __func__
));
975 /* Calculate step 2 values */
976 jpake_step2(pctx
->grp
, pctx
->s
, pctx
->g_x1
,
977 pctx
->g_x3
, pctx
->g_x4
, pctx
->x2
,
978 pctx
->server_id
, pctx
->server_id_len
,
979 pctx
->client_id
, pctx
->client_id_len
,
980 x3_proof
, x3_proof_len
,
981 x4_proof
, x4_proof_len
,
983 &x2_s_proof
, &x2_s_proof_len
);
985 bzero(x3_proof
, x3_proof_len
);
986 bzero(x4_proof
, x4_proof_len
);
990 JPAKE_DEBUG_CTX((pctx
, "step 2 sending in %s", __func__
));
992 /* Send values for step 2 */
993 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP2
);
994 packet_put_bignum2(pctx
->a
);
995 packet_put_string(x2_s_proof
, x2_s_proof_len
);
998 bzero(x2_s_proof
, x2_s_proof_len
);
1001 /* Expect step 2 packet from peer */
1002 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2
,
1003 input_userauth_jpake_server_step2
);
1008 input_userauth_jpake_server_step2(int type
, u_int32_t seq
, void *ctxt
)
1010 Authctxt
*authctxt
= ctxt
;
1011 struct jpake_ctx
*pctx
= authctxt
->methoddata
;
1013 u_int x4_s_proof_len
;
1015 /* Disable this message */
1016 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2
, NULL
);
1018 if ((pctx
->b
= BN_new()) == NULL
)
1019 fatal("%s: BN_new", __func__
);
1021 /* Fetch step 2 values */
1022 packet_get_bignum2(pctx
->b
);
1023 x4_s_proof
= packet_get_string(&x4_s_proof_len
);
1026 JPAKE_DEBUG_CTX((pctx
, "step 2 received in %s", __func__
));
1028 /* Derive shared key and calculate confirmation hash */
1029 jpake_key_confirm(pctx
->grp
, pctx
->s
, pctx
->b
,
1030 pctx
->x2
, pctx
->g_x1
, pctx
->g_x2
, pctx
->g_x3
, pctx
->g_x4
,
1031 pctx
->client_id
, pctx
->client_id_len
,
1032 pctx
->server_id
, pctx
->server_id_len
,
1033 session_id2
, session_id2_len
,
1034 x4_s_proof
, x4_s_proof_len
,
1036 &pctx
->h_k_cid_sessid
, &pctx
->h_k_cid_sessid_len
);
1038 bzero(x4_s_proof
, x4_s_proof_len
);
1041 JPAKE_DEBUG_CTX((pctx
, "confirm sending in %s", __func__
));
1043 /* Send key confirmation proof */
1044 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_CONFIRM
);
1045 packet_put_string(pctx
->h_k_cid_sessid
, pctx
->h_k_cid_sessid_len
);
1048 /* Expect confirmation from peer */
1049 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM
,
1050 input_userauth_jpake_server_confirm
);
1055 input_userauth_jpake_server_confirm(int type
, u_int32_t seq
, void *ctxt
)
1057 Authctxt
*authctxt
= ctxt
;
1058 struct jpake_ctx
*pctx
= authctxt
->methoddata
;
1060 /* Disable this message */
1061 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM
, NULL
);
1063 pctx
->h_k_sid_sessid
= packet_get_string(&pctx
->h_k_sid_sessid_len
);
1066 JPAKE_DEBUG_CTX((pctx
, "confirm received in %s", __func__
));
1068 /* Verify expected confirmation hash */
1069 if (jpake_check_confirm(pctx
->k
,
1070 pctx
->server_id
, pctx
->server_id_len
,
1071 session_id2
, session_id2_len
,
1072 pctx
->h_k_sid_sessid
, pctx
->h_k_sid_sessid_len
) == 1)
1073 debug("%s: %s success", __func__
, authctxt
->method
->name
);
1075 debug("%s: confirmation mismatch", __func__
);
1076 /* XXX stash this so if auth succeeds then we can warn/kill */
1079 userauth_jpake_cleanup(authctxt
);
1084 identity_sign(Identity
*id
, u_char
**sigp
, u_int
*lenp
,
1085 u_char
*data
, u_int datalen
)
1090 /* the agent supports this key */
1092 return (ssh_agent_sign(id
->ac
, id
->key
, sigp
, lenp
,
1095 * we have already loaded the private key or
1096 * the private key is stored in external hardware
1098 if (id
->isprivate
|| (id
->key
->flags
& KEY_FLAG_EXT
))
1099 return (key_sign(id
->key
, sigp
, lenp
, data
, datalen
));
1100 /* load the private key from the file */
1101 if ((prv
= load_identity_file(id
->filename
)) == NULL
)
1103 ret
= key_sign(prv
, sigp
, lenp
, data
, datalen
);
1109 sign_and_send_pubkey(Authctxt
*authctxt
, Identity
*id
)
1112 u_char
*blob
, *signature
;
1113 u_int bloblen
, slen
;
1118 debug3("sign_and_send_pubkey");
1120 if (key_to_blob(id
->key
, &blob
, &bloblen
) == 0) {
1121 /* we cannot handle this key */
1122 debug3("sign_and_send_pubkey: cannot handle key");
1125 /* data to be signed */
1127 if (datafellows
& SSH_OLD_SESSIONID
) {
1128 buffer_append(&b
, session_id2
, session_id2_len
);
1129 skip
= session_id2_len
;
1131 buffer_put_string(&b
, session_id2
, session_id2_len
);
1132 skip
= buffer_len(&b
);
1134 buffer_put_char(&b
, SSH2_MSG_USERAUTH_REQUEST
);
1135 buffer_put_cstring(&b
, authctxt
->server_user
);
1136 buffer_put_cstring(&b
,
1137 datafellows
& SSH_BUG_PKSERVICE
?
1140 if (datafellows
& SSH_BUG_PKAUTH
) {
1141 buffer_put_char(&b
, have_sig
);
1143 buffer_put_cstring(&b
, authctxt
->method
->name
);
1144 buffer_put_char(&b
, have_sig
);
1145 buffer_put_cstring(&b
, key_ssh_name(id
->key
));
1147 buffer_put_string(&b
, blob
, bloblen
);
1149 /* generate signature */
1150 ret
= identity_sign(id
, &signature
, &slen
,
1151 buffer_ptr(&b
), buffer_len(&b
));
1160 if (datafellows
& SSH_BUG_PKSERVICE
) {
1162 buffer_append(&b
, session_id2
, session_id2_len
);
1163 skip
= session_id2_len
;
1164 buffer_put_char(&b
, SSH2_MSG_USERAUTH_REQUEST
);
1165 buffer_put_cstring(&b
, authctxt
->server_user
);
1166 buffer_put_cstring(&b
, authctxt
->service
);
1167 buffer_put_cstring(&b
, authctxt
->method
->name
);
1168 buffer_put_char(&b
, have_sig
);
1169 if (!(datafellows
& SSH_BUG_PKAUTH
))
1170 buffer_put_cstring(&b
, key_ssh_name(id
->key
));
1171 buffer_put_string(&b
, blob
, bloblen
);
1175 /* append signature */
1176 buffer_put_string(&b
, signature
, slen
);
1179 /* skip session id and packet type */
1180 if (buffer_len(&b
) < skip
+ 1)
1181 fatal("userauth_pubkey: internal error");
1182 buffer_consume(&b
, skip
+ 1);
1184 /* put remaining data from buffer into packet */
1185 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
1186 packet_put_raw(buffer_ptr(&b
), buffer_len(&b
));
1194 send_pubkey_test(Authctxt
*authctxt
, Identity
*id
)
1197 u_int bloblen
, have_sig
= 0;
1199 debug3("send_pubkey_test");
1201 if (key_to_blob(id
->key
, &blob
, &bloblen
) == 0) {
1202 /* we cannot handle this key */
1203 debug3("send_pubkey_test: cannot handle key");
1206 /* register callback for USERAUTH_PK_OK message */
1207 dispatch_set(SSH2_MSG_USERAUTH_PK_OK
, &input_userauth_pk_ok
);
1209 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
1210 packet_put_cstring(authctxt
->server_user
);
1211 packet_put_cstring(authctxt
->service
);
1212 packet_put_cstring(authctxt
->method
->name
);
1213 packet_put_char(have_sig
);
1214 if (!(datafellows
& SSH_BUG_PKAUTH
))
1215 packet_put_cstring(key_ssh_name(id
->key
));
1216 packet_put_string(blob
, bloblen
);
1223 load_identity_file(char *filename
)
1226 char prompt
[300], *passphrase
;
1227 int perm_ok
, quit
, i
;
1230 if (stat(filename
, &st
) < 0) {
1231 debug3("no such identity: %s", filename
);
1234 private = key_load_private_type(KEY_UNSPEC
, filename
, "", NULL
, &perm_ok
);
1237 if (private == NULL
) {
1238 if (options
.batch_mode
)
1240 snprintf(prompt
, sizeof prompt
,
1241 "Enter passphrase for key '%.100s': ", filename
);
1242 for (i
= 0; i
< options
.number_of_password_prompts
; i
++) {
1243 passphrase
= read_passphrase(prompt
, 0);
1244 if (strcmp(passphrase
, "") != 0) {
1245 private = key_load_private_type(KEY_UNSPEC
,
1246 filename
, passphrase
, NULL
, NULL
);
1249 debug2("no passphrase given, try next key");
1252 memset(passphrase
, 0, strlen(passphrase
));
1254 if (private != NULL
|| quit
)
1256 debug2("bad passphrase given, try again...");
1263 * try keys in the following order:
1264 * 1. agent keys that are found in the config file
1265 * 2. other agent keys
1266 * 3. keys that are only listed in the config file
1269 pubkey_prepare(Authctxt
*authctxt
)
1272 Idlist agent
, files
, *preferred
;
1274 AuthenticationConnection
*ac
;
1278 TAILQ_INIT(&agent
); /* keys from the agent */
1279 TAILQ_INIT(&files
); /* keys from the config file */
1280 preferred
= &authctxt
->keys
;
1281 TAILQ_INIT(preferred
); /* preferred order of keys */
1283 /* list of keys stored in the filesystem */
1284 for (i
= 0; i
< options
.num_identity_files
; i
++) {
1285 key
= options
.identity_keys
[i
];
1286 if (key
&& key
->type
== KEY_RSA1
)
1288 options
.identity_keys
[i
] = NULL
;
1289 id
= xcalloc(1, sizeof(*id
));
1291 id
->filename
= xstrdup(options
.identity_files
[i
]);
1292 TAILQ_INSERT_TAIL(&files
, id
, next
);
1294 /* list of keys supported by the agent */
1295 if ((ac
= ssh_get_authentication_connection())) {
1296 for (key
= ssh_get_first_identity(ac
, &comment
, 2);
1298 key
= ssh_get_next_identity(ac
, &comment
, 2)) {
1300 TAILQ_FOREACH(id
, &files
, next
) {
1301 /* agent keys from the config file are preferred */
1302 if (key_equal(key
, id
->key
)) {
1305 TAILQ_REMOVE(&files
, id
, next
);
1306 TAILQ_INSERT_TAIL(preferred
, id
, next
);
1312 if (!found
&& !options
.identities_only
) {
1313 id
= xcalloc(1, sizeof(*id
));
1315 id
->filename
= comment
;
1317 TAILQ_INSERT_TAIL(&agent
, id
, next
);
1320 /* append remaining agent keys */
1321 for (id
= TAILQ_FIRST(&agent
); id
; id
= TAILQ_FIRST(&agent
)) {
1322 TAILQ_REMOVE(&agent
, id
, next
);
1323 TAILQ_INSERT_TAIL(preferred
, id
, next
);
1325 authctxt
->agent
= ac
;
1327 /* append remaining keys from the config file */
1328 for (id
= TAILQ_FIRST(&files
); id
; id
= TAILQ_FIRST(&files
)) {
1329 TAILQ_REMOVE(&files
, id
, next
);
1330 TAILQ_INSERT_TAIL(preferred
, id
, next
);
1332 TAILQ_FOREACH(id
, preferred
, next
) {
1333 debug2("key: %s (%p)", id
->filename
, id
->key
);
1338 pubkey_cleanup(Authctxt
*authctxt
)
1342 if (authctxt
->agent
!= NULL
)
1343 ssh_close_authentication_connection(authctxt
->agent
);
1344 for (id
= TAILQ_FIRST(&authctxt
->keys
); id
;
1345 id
= TAILQ_FIRST(&authctxt
->keys
)) {
1346 TAILQ_REMOVE(&authctxt
->keys
, id
, next
);
1350 xfree(id
->filename
);
1356 userauth_pubkey(Authctxt
*authctxt
)
1361 while ((id
= TAILQ_FIRST(&authctxt
->keys
))) {
1364 /* move key to the end of the queue */
1365 TAILQ_REMOVE(&authctxt
->keys
, id
, next
);
1366 TAILQ_INSERT_TAIL(&authctxt
->keys
, id
, next
);
1368 * send a test message if we have the public key. for
1369 * encrypted keys we cannot do this and have to load the
1370 * private key instead
1372 if (id
->key
&& id
->key
->type
!= KEY_RSA1
) {
1373 debug("Offering public key: %s", id
->filename
);
1374 sent
= send_pubkey_test(authctxt
, id
);
1375 } else if (id
->key
== NULL
) {
1376 debug("Trying private key: %s", id
->filename
);
1377 id
->key
= load_identity_file(id
->filename
);
1378 if (id
->key
!= NULL
) {
1380 sent
= sign_and_send_pubkey(authctxt
, id
);
1392 * Send userauth request message specifying keyboard-interactive method.
1395 userauth_kbdint(Authctxt
*authctxt
)
1397 static int attempt
= 0;
1399 if (attempt
++ >= options
.number_of_password_prompts
)
1401 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1402 if (attempt
> 1 && !authctxt
->info_req_seen
) {
1403 debug3("userauth_kbdint: disable: no info_req_seen");
1404 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST
, NULL
);
1408 debug2("userauth_kbdint");
1409 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
1410 packet_put_cstring(authctxt
->server_user
);
1411 packet_put_cstring(authctxt
->service
);
1412 packet_put_cstring(authctxt
->method
->name
);
1413 packet_put_cstring(""); /* lang */
1414 packet_put_cstring(options
.kbd_interactive_devices
?
1415 options
.kbd_interactive_devices
: "");
1418 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST
, &input_userauth_info_req
);
1423 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1426 input_userauth_info_req(int type
, u_int32_t seq
, void *ctxt
)
1428 Authctxt
*authctxt
= ctxt
;
1429 char *name
, *inst
, *lang
, *prompt
, *response
;
1430 u_int num_prompts
, i
;
1433 debug2("input_userauth_info_req");
1435 if (authctxt
== NULL
)
1436 fatal("input_userauth_info_req: no authentication context");
1438 authctxt
->info_req_seen
= 1;
1440 name
= packet_get_string(NULL
);
1441 inst
= packet_get_string(NULL
);
1442 lang
= packet_get_string(NULL
);
1443 if (strlen(name
) > 0)
1445 if (strlen(inst
) > 0)
1451 num_prompts
= packet_get_int();
1453 * Begin to build info response packet based on prompts requested.
1454 * We commit to providing the correct number of responses, so if
1455 * further on we run into a problem that prevents this, we have to
1456 * be sure and clean this up and send a correct error response.
1458 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE
);
1459 packet_put_int(num_prompts
);
1461 debug2("input_userauth_info_req: num_prompts %d", num_prompts
);
1462 for (i
= 0; i
< num_prompts
; i
++) {
1463 prompt
= packet_get_string(NULL
);
1464 echo
= packet_get_char();
1466 response
= read_passphrase(prompt
, echo
? RP_ECHO
: 0);
1468 packet_put_cstring(response
);
1469 memset(response
, 0, strlen(response
));
1473 packet_check_eom(); /* done with parsing incoming message. */
1475 packet_add_padding(64);
1480 ssh_keysign(Key
*key
, u_char
**sigp
, u_int
*lenp
,
1481 u_char
*data
, u_int datalen
)
1486 int to
[2], from
[2], status
, version
= 2;
1488 debug2("ssh_keysign called");
1490 if (stat(_PATH_SSH_KEY_SIGN
, &st
) < 0) {
1491 error("ssh_keysign: no installed: %s", strerror(errno
));
1494 if (fflush(stdout
) != 0)
1495 error("ssh_keysign: fflush: %s", strerror(errno
));
1497 error("ssh_keysign: pipe: %s", strerror(errno
));
1500 if (pipe(from
) < 0) {
1501 error("ssh_keysign: pipe: %s", strerror(errno
));
1504 if ((pid
= fork()) < 0) {
1505 error("ssh_keysign: fork: %s", strerror(errno
));
1509 permanently_drop_suid(getuid());
1511 if (dup2(from
[1], STDOUT_FILENO
) < 0)
1512 fatal("ssh_keysign: dup2: %s", strerror(errno
));
1514 if (dup2(to
[0], STDIN_FILENO
) < 0)
1515 fatal("ssh_keysign: dup2: %s", strerror(errno
));
1518 execl(_PATH_SSH_KEY_SIGN
, _PATH_SSH_KEY_SIGN
, (char *) 0);
1519 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN
,
1526 buffer_put_int(&b
, packet_get_connection_in()); /* send # of socket */
1527 buffer_put_string(&b
, data
, datalen
);
1528 if (ssh_msg_send(to
[1], version
, &b
) == -1)
1529 fatal("ssh_keysign: couldn't send request");
1531 if (ssh_msg_recv(from
[0], &b
) < 0) {
1532 error("ssh_keysign: no reply");
1539 while (waitpid(pid
, &status
, 0) < 0)
1543 if (buffer_get_char(&b
) != version
) {
1544 error("ssh_keysign: bad version");
1548 *sigp
= buffer_get_string(&b
, lenp
);
1555 userauth_hostbased(Authctxt
*authctxt
)
1557 Key
*private = NULL
;
1558 Sensitive
*sensitive
= authctxt
->sensitive
;
1560 u_char
*signature
, *blob
;
1561 char *chost
, *pkalg
, *p
, myname
[NI_MAXHOST
];
1562 const char *service
;
1564 int ok
, i
, len
, found
= 0;
1566 /* check for a useful key */
1567 for (i
= 0; i
< sensitive
->nkeys
; i
++) {
1568 private = sensitive
->keys
[i
];
1569 if (private && private->type
!= KEY_RSA1
) {
1571 /* we take and free the key */
1572 sensitive
->keys
[i
] = NULL
;
1577 debug("No more client hostkeys for hostbased authentication.");
1580 if (key_to_blob(private, &blob
, &blen
) == 0) {
1584 /* figure out a name for the client host */
1586 if (packet_connection_is_on_socket())
1587 p
= get_local_name(packet_get_connection_in());
1589 if (gethostname(myname
, sizeof(myname
)) == -1) {
1590 verbose("userauth_hostbased: gethostname: %s",
1593 p
= xstrdup(myname
);
1596 error("userauth_hostbased: cannot get local ipaddr/name");
1601 len
= strlen(p
) + 2;
1602 xasprintf(&chost
, "%s.", p
);
1603 debug2("userauth_hostbased: chost %s", chost
);
1606 service
= datafellows
& SSH_BUG_HBSERVICE
? "ssh-userauth" :
1608 pkalg
= xstrdup(key_ssh_name(private));
1610 /* construct data */
1611 buffer_put_string(&b
, session_id2
, session_id2_len
);
1612 buffer_put_char(&b
, SSH2_MSG_USERAUTH_REQUEST
);
1613 buffer_put_cstring(&b
, authctxt
->server_user
);
1614 buffer_put_cstring(&b
, service
);
1615 buffer_put_cstring(&b
, authctxt
->method
->name
);
1616 buffer_put_cstring(&b
, pkalg
);
1617 buffer_put_string(&b
, blob
, blen
);
1618 buffer_put_cstring(&b
, chost
);
1619 buffer_put_cstring(&b
, authctxt
->local_user
);
1623 if (sensitive
->external_keysign
)
1624 ok
= ssh_keysign(private, &signature
, &slen
,
1625 buffer_ptr(&b
), buffer_len(&b
));
1627 ok
= key_sign(private, &signature
, &slen
,
1628 buffer_ptr(&b
), buffer_len(&b
));
1632 error("key_sign failed");
1638 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
1639 packet_put_cstring(authctxt
->server_user
);
1640 packet_put_cstring(authctxt
->service
);
1641 packet_put_cstring(authctxt
->method
->name
);
1642 packet_put_cstring(pkalg
);
1643 packet_put_string(blob
, blen
);
1644 packet_put_cstring(chost
);
1645 packet_put_cstring(authctxt
->local_user
);
1646 packet_put_string(signature
, slen
);
1647 memset(signature
, 's', slen
);
1659 userauth_jpake(Authctxt
*authctxt
)
1661 struct jpake_ctx
*pctx
;
1662 u_char
*x1_proof
, *x2_proof
;
1663 u_int x1_proof_len
, x2_proof_len
;
1664 static int attempt
= 0; /* XXX share with userauth_password's? */
1666 if (attempt
++ >= options
.number_of_password_prompts
)
1669 error("Permission denied, please try again.");
1671 if (authctxt
->methoddata
!= NULL
)
1672 fatal("%s: authctxt->methoddata already set (%p)",
1673 __func__
, authctxt
->methoddata
);
1675 authctxt
->methoddata
= pctx
= jpake_new();
1678 * Send request immediately, to get the protocol going while
1679 * we do the initial computations.
1681 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
1682 packet_put_cstring(authctxt
->server_user
);
1683 packet_put_cstring(authctxt
->service
);
1684 packet_put_cstring(authctxt
->method
->name
);
1686 packet_write_wait();
1688 jpake_step1(pctx
->grp
,
1689 &pctx
->client_id
, &pctx
->client_id_len
,
1690 &pctx
->x1
, &pctx
->x2
, &pctx
->g_x1
, &pctx
->g_x2
,
1691 &x1_proof
, &x1_proof_len
,
1692 &x2_proof
, &x2_proof_len
);
1694 JPAKE_DEBUG_CTX((pctx
, "step 1 sending in %s", __func__
));
1696 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP1
);
1697 packet_put_string(pctx
->client_id
, pctx
->client_id_len
);
1698 packet_put_bignum2(pctx
->g_x1
);
1699 packet_put_bignum2(pctx
->g_x2
);
1700 packet_put_string(x1_proof
, x1_proof_len
);
1701 packet_put_string(x2_proof
, x2_proof_len
);
1704 bzero(x1_proof
, x1_proof_len
);
1705 bzero(x2_proof
, x2_proof_len
);
1709 /* Expect step 1 packet from peer */
1710 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1
,
1711 input_userauth_jpake_server_step1
);
1717 userauth_jpake_cleanup(Authctxt
*authctxt
)
1719 debug3("%s: clean up", __func__
);
1720 if (authctxt
->methoddata
!= NULL
) {
1721 jpake_free(authctxt
->methoddata
);
1722 authctxt
->methoddata
= NULL
;
1727 /* find auth method */
1730 * given auth method name, if configurable options permit this method fill
1731 * in auth_ident field and return true, otherwise return false.
1734 authmethod_is_enabled(Authmethod
*method
)
1738 /* return false if options indicate this method is disabled */
1739 if (method
->enabled
== NULL
|| *method
->enabled
== 0)
1741 /* return false if batch mode is enabled but method needs interactive mode */
1742 if (method
->batch_flag
!= NULL
&& *method
->batch_flag
!= 0)
1748 authmethod_lookup(const char *name
)
1750 Authmethod
*method
= NULL
;
1752 for (method
= authmethods
; method
->name
!= NULL
; method
++)
1753 if (strcmp(name
, method
->name
) == 0)
1755 debug2("Unrecognized authentication method name: %s", name
? name
: "NULL");
1759 /* XXX internal state */
1760 static Authmethod
*current
= NULL
;
1761 static char *supported
= NULL
;
1762 static char *preferred
= NULL
;
1765 * Given the authentication method list sent by the server, return the
1766 * next method we should try. If the server initially sends a nil list,
1767 * use a built-in default list.
1770 authmethod_get(char *authlist
)
1775 /* Use a suitable default if we're passed a nil list. */
1776 if (authlist
== NULL
|| strlen(authlist
) == 0)
1777 authlist
= options
.preferred_authentications
;
1779 if (supported
== NULL
|| strcmp(authlist
, supported
) != 0) {
1780 debug3("start over, passed a different list %s", authlist
);
1781 if (supported
!= NULL
)
1783 supported
= xstrdup(authlist
);
1784 preferred
= options
.preferred_authentications
;
1785 debug3("preferred %s", preferred
);
1787 } else if (current
!= NULL
&& authmethod_is_enabled(current
))
1791 if ((name
= match_list(preferred
, supported
, &next
)) == NULL
) {
1792 debug("No more authentication methods to try.");
1797 debug3("authmethod_lookup %s", name
);
1798 debug3("remaining preferred: %s", preferred
);
1799 if ((current
= authmethod_lookup(name
)) != NULL
&&
1800 authmethod_is_enabled(current
)) {
1801 debug3("authmethod_is_enabled %s", name
);
1802 debug("Next authentication method: %s", name
);
1809 authmethods_get(void)
1811 Authmethod
*method
= NULL
;
1816 for (method
= authmethods
; method
->name
!= NULL
; method
++) {
1817 if (authmethod_is_enabled(method
)) {
1818 if (buffer_len(&b
) > 0)
1819 buffer_append(&b
, ",", 1);
1820 buffer_append(&b
, method
->name
, strlen(method
->name
));
1823 buffer_append(&b
, "\0", 1);
1824 list
= xstrdup(buffer_ptr(&b
));