1 /* $OpenBSD: sshconnect2.c,v 1.173 2009/10/24 11:13:54 andreas 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 if (options
.use_roaming
&& !kex
->roaming
) {
156 debug("Roaming not allowed by server");
157 options
.use_roaming
= 0;
160 session_id2
= kex
->session_id
;
161 session_id2_len
= kex
->session_id_len
;
164 /* send 1st encrypted/maced/compressed message */
165 packet_start(SSH2_MSG_IGNORE
);
166 packet_put_cstring("markus");
176 typedef struct Authctxt Authctxt
;
177 typedef struct Authmethod Authmethod
;
178 typedef struct identity Identity
;
179 typedef struct idlist Idlist
;
182 TAILQ_ENTRY(identity
) next
;
183 AuthenticationConnection
*ac
; /* set if agent supports key */
184 Key
*key
; /* public/private key */
185 char *filename
; /* comment for agent-only keys */
187 int isprivate
; /* key points to the private key */
189 TAILQ_HEAD(idlist
, identity
);
192 const char *server_user
;
193 const char *local_user
;
201 AuthenticationConnection
*agent
;
203 Sensitive
*sensitive
;
204 /* kbd-interactive */
210 char *name
; /* string to compare against server's list */
211 int (*userauth
)(Authctxt
*authctxt
);
212 void (*cleanup
)(Authctxt
*authctxt
);
213 int *enabled
; /* flag in option struct that enables method */
214 int *batch_flag
; /* flag in option struct that disables method */
217 void input_userauth_success(int, u_int32_t
, void *);
218 void input_userauth_success_unexpected(int, u_int32_t
, void *);
219 void input_userauth_failure(int, u_int32_t
, void *);
220 void input_userauth_banner(int, u_int32_t
, void *);
221 void input_userauth_error(int, u_int32_t
, void *);
222 void input_userauth_info_req(int, u_int32_t
, void *);
223 void input_userauth_pk_ok(int, u_int32_t
, void *);
224 void input_userauth_passwd_changereq(int, u_int32_t
, void *);
225 void input_userauth_jpake_server_step1(int, u_int32_t
, void *);
226 void input_userauth_jpake_server_step2(int, u_int32_t
, void *);
227 void input_userauth_jpake_server_confirm(int, u_int32_t
, void *);
229 int userauth_none(Authctxt
*);
230 int userauth_pubkey(Authctxt
*);
231 int userauth_passwd(Authctxt
*);
232 int userauth_kbdint(Authctxt
*);
233 int userauth_hostbased(Authctxt
*);
234 int userauth_jpake(Authctxt
*);
236 void userauth_jpake_cleanup(Authctxt
*);
239 int userauth_gssapi(Authctxt
*authctxt
);
240 void input_gssapi_response(int type
, u_int32_t
, void *);
241 void input_gssapi_token(int type
, u_int32_t
, void *);
242 void input_gssapi_hash(int type
, u_int32_t
, void *);
243 void input_gssapi_error(int, u_int32_t
, void *);
244 void input_gssapi_errtok(int, u_int32_t
, void *);
247 void userauth(Authctxt
*, char *);
249 static int sign_and_send_pubkey(Authctxt
*, Identity
*);
250 static void pubkey_prepare(Authctxt
*);
251 static void pubkey_cleanup(Authctxt
*);
252 static Key
*load_identity_file(char *);
254 static Authmethod
*authmethod_get(char *authlist
);
255 static Authmethod
*authmethod_lookup(const char *name
);
256 static char *authmethods_get(void);
258 Authmethod authmethods
[] = {
263 &options
.gss_authentication
,
269 &options
.hostbased_authentication
,
274 &options
.pubkey_authentication
,
277 {"jpake-01@openssh.com",
279 userauth_jpake_cleanup
,
280 &options
.zero_knowledge_password_authentication
,
281 &options
.batch_mode
},
283 {"keyboard-interactive",
286 &options
.kbd_interactive_authentication
,
287 &options
.batch_mode
},
291 &options
.password_authentication
,
292 &options
.batch_mode
},
298 {NULL
, NULL
, NULL
, NULL
, NULL
}
302 ssh_userauth2(const char *local_user
, const char *server_user
, char *host
,
303 Sensitive
*sensitive
)
308 if (options
.challenge_response_authentication
)
309 options
.kbd_interactive_authentication
= 1;
311 packet_start(SSH2_MSG_SERVICE_REQUEST
);
312 packet_put_cstring("ssh-userauth");
314 debug("SSH2_MSG_SERVICE_REQUEST sent");
316 type
= packet_read();
317 if (type
!= SSH2_MSG_SERVICE_ACCEPT
)
318 fatal("Server denied authentication request: %d", type
);
319 if (packet_remaining() > 0) {
320 char *reply
= packet_get_string(NULL
);
321 debug2("service_accept: %s", reply
);
324 debug2("buggy server: service_accept w/o service");
327 debug("SSH2_MSG_SERVICE_ACCEPT received");
329 if (options
.preferred_authentications
== NULL
)
330 options
.preferred_authentications
= authmethods_get();
332 /* setup authentication context */
333 memset(&authctxt
, 0, sizeof(authctxt
));
334 pubkey_prepare(&authctxt
);
335 authctxt
.server_user
= server_user
;
336 authctxt
.local_user
= local_user
;
337 authctxt
.host
= host
;
338 authctxt
.service
= "ssh-connection"; /* service name */
339 authctxt
.success
= 0;
340 authctxt
.method
= authmethod_lookup("none");
341 authctxt
.authlist
= NULL
;
342 authctxt
.methoddata
= NULL
;
343 authctxt
.sensitive
= sensitive
;
344 authctxt
.info_req_seen
= 0;
345 if (authctxt
.method
== NULL
)
346 fatal("ssh_userauth2: internal error: cannot send userauth none request");
348 /* initial userauth request */
349 userauth_none(&authctxt
);
351 dispatch_init(&input_userauth_error
);
352 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS
, &input_userauth_success
);
353 dispatch_set(SSH2_MSG_USERAUTH_FAILURE
, &input_userauth_failure
);
354 dispatch_set(SSH2_MSG_USERAUTH_BANNER
, &input_userauth_banner
);
355 dispatch_run(DISPATCH_BLOCK
, &authctxt
.success
, &authctxt
); /* loop until success */
357 pubkey_cleanup(&authctxt
);
358 dispatch_range(SSH2_MSG_USERAUTH_MIN
, SSH2_MSG_USERAUTH_MAX
, NULL
);
360 debug("Authentication succeeded (%s).", authctxt
.method
->name
);
364 userauth(Authctxt
*authctxt
, char *authlist
)
366 if (authctxt
->method
!= NULL
&& authctxt
->method
->cleanup
!= NULL
)
367 authctxt
->method
->cleanup(authctxt
);
369 if (authctxt
->methoddata
) {
370 xfree(authctxt
->methoddata
);
371 authctxt
->methoddata
= NULL
;
373 if (authlist
== NULL
) {
374 authlist
= authctxt
->authlist
;
376 if (authctxt
->authlist
)
377 xfree(authctxt
->authlist
);
378 authctxt
->authlist
= authlist
;
381 Authmethod
*method
= authmethod_get(authlist
);
383 fatal("Permission denied (%s).", authlist
);
384 authctxt
->method
= method
;
386 /* reset the per method handler */
387 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN
,
388 SSH2_MSG_USERAUTH_PER_METHOD_MAX
, NULL
);
390 /* and try new method */
391 if (method
->userauth(authctxt
) != 0) {
392 debug2("we sent a %s packet, wait for reply", method
->name
);
395 debug2("we did not send a packet, disable method");
396 method
->enabled
= NULL
;
403 input_userauth_error(int type
, u_int32_t seq
, void *ctxt
)
405 fatal("input_userauth_error: bad message during authentication: "
411 input_userauth_banner(int type
, u_int32_t seq
, void *ctxt
)
413 char *msg
, *raw
, *lang
;
416 debug3("input_userauth_banner");
417 raw
= packet_get_string(&len
);
418 lang
= packet_get_string(NULL
);
419 if (len
> 0 && options
.log_level
>= SYSLOG_LEVEL_INFO
) {
422 msg
= xmalloc(len
* 4 + 1); /* max expansion from strnvis() */
423 strnvis(msg
, raw
, len
* 4 + 1, VIS_SAFE
|VIS_OCTAL
);
424 fprintf(stderr
, "%s", msg
);
433 input_userauth_success(int type
, u_int32_t seq
, void *ctxt
)
435 Authctxt
*authctxt
= ctxt
;
437 if (authctxt
== NULL
)
438 fatal("input_userauth_success: no authentication context");
439 if (authctxt
->authlist
) {
440 xfree(authctxt
->authlist
);
441 authctxt
->authlist
= NULL
;
443 if (authctxt
->method
!= NULL
&& authctxt
->method
->cleanup
!= NULL
)
444 authctxt
->method
->cleanup(authctxt
);
445 if (authctxt
->methoddata
) {
446 xfree(authctxt
->methoddata
);
447 authctxt
->methoddata
= NULL
;
449 authctxt
->success
= 1; /* break out */
453 input_userauth_success_unexpected(int type
, u_int32_t seq
, void *ctxt
)
455 Authctxt
*authctxt
= ctxt
;
457 if (authctxt
== NULL
)
458 fatal("%s: no authentication context", __func__
);
460 fatal("Unexpected authentication success during %s.",
461 authctxt
->method
->name
);
466 input_userauth_failure(int type
, u_int32_t seq
, void *ctxt
)
468 Authctxt
*authctxt
= ctxt
;
469 char *authlist
= NULL
;
472 if (authctxt
== NULL
)
473 fatal("input_userauth_failure: no authentication context");
475 authlist
= packet_get_string(NULL
);
476 partial
= packet_get_char();
480 logit("Authenticated with partial success.");
481 debug("Authentications that can continue: %s", authlist
);
483 userauth(authctxt
, authlist
);
488 input_userauth_pk_ok(int type
, u_int32_t seq
, void *ctxt
)
490 Authctxt
*authctxt
= ctxt
;
494 int pktype
, sent
= 0;
499 if (authctxt
== NULL
)
500 fatal("input_userauth_pk_ok: no authentication context");
501 if (datafellows
& SSH_BUG_PKOK
) {
502 /* this is similar to SSH_BUG_PKAUTH */
503 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
504 pkblob
= packet_get_string(&blen
);
506 buffer_append(&b
, pkblob
, blen
);
507 pkalg
= buffer_get_string(&b
, &alen
);
510 pkalg
= packet_get_string(&alen
);
511 pkblob
= packet_get_string(&blen
);
515 debug("Server accepts key: pkalg %s blen %u", pkalg
, blen
);
517 if ((pktype
= key_type_from_name(pkalg
)) == KEY_UNSPEC
) {
518 debug("unknown pkalg %s", pkalg
);
521 if ((key
= key_from_blob(pkblob
, blen
)) == NULL
) {
522 debug("no key from blob. pkalg %s", pkalg
);
525 if (key
->type
!= pktype
) {
526 error("input_userauth_pk_ok: type mismatch "
527 "for decoded key (received %d, expected %d)",
531 fp
= key_fingerprint(key
, SSH_FP_MD5
, SSH_FP_HEX
);
532 debug2("input_userauth_pk_ok: fp %s", fp
);
536 * search keys in the reverse order, because last candidate has been
537 * moved to the end of the queue. this also avoids confusion by
540 TAILQ_FOREACH_REVERSE(id
, &authctxt
->keys
, idlist
, next
) {
541 if (key_equal(key
, id
->key
)) {
542 sent
= sign_and_send_pubkey(authctxt
, id
);
552 /* try another method if we did not send a packet */
554 userauth(authctxt
, NULL
);
559 userauth_gssapi(Authctxt
*authctxt
)
561 Gssctxt
*gssctxt
= NULL
;
562 static gss_OID_set gss_supported
= NULL
;
563 static u_int mech
= 0;
567 /* Try one GSSAPI method at a time, rather than sending them all at
570 if (gss_supported
== NULL
)
571 gss_indicate_mechs(&min
, &gss_supported
);
573 /* Check to see if the mechanism is usable before we offer it */
574 while (mech
< gss_supported
->count
&& !ok
) {
575 /* My DER encoding requires length<128 */
576 if (gss_supported
->elements
[mech
].length
< 128 &&
577 ssh_gssapi_check_mechanism(&gssctxt
,
578 &gss_supported
->elements
[mech
], authctxt
->host
)) {
579 ok
= 1; /* Mechanism works */
588 authctxt
->methoddata
=(void *)gssctxt
;
590 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
591 packet_put_cstring(authctxt
->server_user
);
592 packet_put_cstring(authctxt
->service
);
593 packet_put_cstring(authctxt
->method
->name
);
597 packet_put_int((gss_supported
->elements
[mech
].length
) + 2);
598 packet_put_char(SSH_GSS_OIDTYPE
);
599 packet_put_char(gss_supported
->elements
[mech
].length
);
600 packet_put_raw(gss_supported
->elements
[mech
].elements
,
601 gss_supported
->elements
[mech
].length
);
605 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE
, &input_gssapi_response
);
606 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN
, &input_gssapi_token
);
607 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR
, &input_gssapi_error
);
608 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK
, &input_gssapi_errtok
);
610 mech
++; /* Move along to next candidate */
616 process_gssapi_token(void *ctxt
, gss_buffer_t recv_tok
)
618 Authctxt
*authctxt
= ctxt
;
619 Gssctxt
*gssctxt
= authctxt
->methoddata
;
620 gss_buffer_desc send_tok
= GSS_C_EMPTY_BUFFER
;
621 gss_buffer_desc mic
= GSS_C_EMPTY_BUFFER
;
622 gss_buffer_desc gssbuf
;
623 OM_uint32 status
, ms
, flags
;
626 status
= ssh_gssapi_init_ctx(gssctxt
, options
.gss_deleg_creds
,
627 recv_tok
, &send_tok
, &flags
);
629 if (send_tok
.length
> 0) {
630 if (GSS_ERROR(status
))
631 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK
);
633 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN
);
635 packet_put_string(send_tok
.value
, send_tok
.length
);
637 gss_release_buffer(&ms
, &send_tok
);
640 if (status
== GSS_S_COMPLETE
) {
641 /* send either complete or MIC, depending on mechanism */
642 if (!(flags
& GSS_C_INTEG_FLAG
)) {
643 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE
);
646 ssh_gssapi_buildmic(&b
, authctxt
->server_user
,
647 authctxt
->service
, "gssapi-with-mic");
649 gssbuf
.value
= buffer_ptr(&b
);
650 gssbuf
.length
= buffer_len(&b
);
652 status
= ssh_gssapi_sign(gssctxt
, &gssbuf
, &mic
);
654 if (!GSS_ERROR(status
)) {
655 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC
);
656 packet_put_string(mic
.value
, mic
.length
);
662 gss_release_buffer(&ms
, &mic
);
671 input_gssapi_response(int type
, u_int32_t plen
, void *ctxt
)
673 Authctxt
*authctxt
= ctxt
;
678 if (authctxt
== NULL
)
679 fatal("input_gssapi_response: no authentication context");
680 gssctxt
= authctxt
->methoddata
;
683 oidv
= packet_get_string(&oidlen
);
686 oidv
[0] != SSH_GSS_OIDTYPE
||
687 oidv
[1] != oidlen
- 2) {
689 debug("Badly encoded mechanism OID received");
690 userauth(authctxt
, NULL
);
694 if (!ssh_gssapi_check_oid(gssctxt
, oidv
+ 2, oidlen
- 2))
695 fatal("Server returned different OID than expected");
701 if (GSS_ERROR(process_gssapi_token(ctxt
, GSS_C_NO_BUFFER
))) {
702 /* Start again with next method on list */
703 debug("Trying to start again");
704 userauth(authctxt
, NULL
);
711 input_gssapi_token(int type
, u_int32_t plen
, void *ctxt
)
713 Authctxt
*authctxt
= ctxt
;
714 gss_buffer_desc recv_tok
;
718 if (authctxt
== NULL
)
719 fatal("input_gssapi_response: no authentication context");
721 recv_tok
.value
= packet_get_string(&slen
);
722 recv_tok
.length
= slen
; /* safe typecast */
726 status
= process_gssapi_token(ctxt
, &recv_tok
);
728 xfree(recv_tok
.value
);
730 if (GSS_ERROR(status
)) {
731 /* Start again with the next method in the list */
732 userauth(authctxt
, NULL
);
739 input_gssapi_errtok(int type
, u_int32_t plen
, void *ctxt
)
741 Authctxt
*authctxt
= ctxt
;
743 gss_buffer_desc send_tok
= GSS_C_EMPTY_BUFFER
;
744 gss_buffer_desc recv_tok
;
745 OM_uint32 status
, ms
;
748 if (authctxt
== NULL
)
749 fatal("input_gssapi_response: no authentication context");
750 gssctxt
= authctxt
->methoddata
;
752 recv_tok
.value
= packet_get_string(&len
);
753 recv_tok
.length
= len
;
757 /* Stick it into GSSAPI and see what it says */
758 status
= ssh_gssapi_init_ctx(gssctxt
, options
.gss_deleg_creds
,
759 &recv_tok
, &send_tok
, NULL
);
761 xfree(recv_tok
.value
);
762 gss_release_buffer(&ms
, &send_tok
);
764 /* Server will be returning a failed packet after this one */
769 input_gssapi_error(int type
, u_int32_t plen
, void *ctxt
)
775 maj
=packet_get_int();
776 min
=packet_get_int();
777 msg
=packet_get_string(NULL
);
778 lang
=packet_get_string(NULL
);
782 debug("Server GSSAPI Error:\n%s", msg
);
789 userauth_none(Authctxt
*authctxt
)
791 /* initial userauth request */
792 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
793 packet_put_cstring(authctxt
->server_user
);
794 packet_put_cstring(authctxt
->service
);
795 packet_put_cstring(authctxt
->method
->name
);
801 userauth_passwd(Authctxt
*authctxt
)
803 static int attempt
= 0;
807 if (attempt
++ >= options
.number_of_password_prompts
)
811 error("Permission denied, please try again.");
813 snprintf(prompt
, sizeof(prompt
), "%.30s@%.128s's password: ",
814 authctxt
->server_user
, authctxt
->host
);
815 password
= read_passphrase(prompt
, 0);
816 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
817 packet_put_cstring(authctxt
->server_user
);
818 packet_put_cstring(authctxt
->service
);
819 packet_put_cstring(authctxt
->method
->name
);
821 packet_put_cstring(password
);
822 memset(password
, 0, strlen(password
));
824 packet_add_padding(64);
827 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ
,
828 &input_userauth_passwd_changereq
);
834 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
838 input_userauth_passwd_changereq(int type
, u_int32_t seqnr
, void *ctxt
)
840 Authctxt
*authctxt
= ctxt
;
841 char *info
, *lang
, *password
= NULL
, *retype
= NULL
;
844 debug2("input_userauth_passwd_changereq");
846 if (authctxt
== NULL
)
847 fatal("input_userauth_passwd_changereq: "
848 "no authentication context");
850 info
= packet_get_string(NULL
);
851 lang
= packet_get_string(NULL
);
852 if (strlen(info
) > 0)
856 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
857 packet_put_cstring(authctxt
->server_user
);
858 packet_put_cstring(authctxt
->service
);
859 packet_put_cstring(authctxt
->method
->name
);
860 packet_put_char(1); /* additional info */
861 snprintf(prompt
, sizeof(prompt
),
862 "Enter %.30s@%.128s's old password: ",
863 authctxt
->server_user
, authctxt
->host
);
864 password
= read_passphrase(prompt
, 0);
865 packet_put_cstring(password
);
866 memset(password
, 0, strlen(password
));
869 while (password
== NULL
) {
870 snprintf(prompt
, sizeof(prompt
),
871 "Enter %.30s@%.128s's new password: ",
872 authctxt
->server_user
, authctxt
->host
);
873 password
= read_passphrase(prompt
, RP_ALLOW_EOF
);
874 if (password
== NULL
) {
878 snprintf(prompt
, sizeof(prompt
),
879 "Retype %.30s@%.128s's new password: ",
880 authctxt
->server_user
, authctxt
->host
);
881 retype
= read_passphrase(prompt
, 0);
882 if (strcmp(password
, retype
) != 0) {
883 memset(password
, 0, strlen(password
));
885 logit("Mismatch; try again, EOF to quit.");
888 memset(retype
, 0, strlen(retype
));
891 packet_put_cstring(password
);
892 memset(password
, 0, strlen(password
));
894 packet_add_padding(64);
897 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ
,
898 &input_userauth_passwd_changereq
);
903 pw_encrypt(const char *password
, const char *crypt_scheme
, const char *salt
)
905 /* OpenBSD crypt(3) handles all of these */
906 if (strcmp(crypt_scheme
, "crypt") == 0 ||
907 strcmp(crypt_scheme
, "bcrypt") == 0 ||
908 strcmp(crypt_scheme
, "md5crypt") == 0 ||
909 strcmp(crypt_scheme
, "crypt-extended") == 0)
910 return xstrdup(crypt(password
, salt
));
911 error("%s: unsupported password encryption scheme \"%.100s\"",
912 __func__
, crypt_scheme
);
917 jpake_password_to_secret(Authctxt
*authctxt
, const char *crypt_scheme
,
920 char prompt
[256], *password
, *crypted
;
925 snprintf(prompt
, sizeof(prompt
), "%.30s@%.128s's password (JPAKE): ",
926 authctxt
->server_user
, authctxt
->host
);
927 password
= read_passphrase(prompt
, 0);
929 if ((crypted
= pw_encrypt(password
, crypt_scheme
, salt
)) == NULL
) {
930 logit("Disabling %s authentication", authctxt
->method
->name
);
931 authctxt
->method
->enabled
= NULL
;
932 /* Continue with an empty password to fail gracefully */
933 crypted
= xstrdup("");
937 debug3("%s: salt = %s", __func__
, salt
);
938 debug3("%s: scheme = %s", __func__
, crypt_scheme
);
939 debug3("%s: crypted = %s", __func__
, crypted
);
942 if (hash_buffer(crypted
, strlen(crypted
), EVP_sha256(),
943 &secret
, &secret_len
) != 0)
944 fatal("%s: hash_buffer", __func__
);
946 bzero(password
, strlen(password
));
947 bzero(crypted
, strlen(crypted
));
951 if ((ret
= BN_bin2bn(secret
, secret_len
, NULL
)) == NULL
)
952 fatal("%s: BN_bin2bn (secret)", __func__
);
953 bzero(secret
, secret_len
);
961 input_userauth_jpake_server_step1(int type
, u_int32_t seq
, void *ctxt
)
963 Authctxt
*authctxt
= ctxt
;
964 struct jpake_ctx
*pctx
= authctxt
->methoddata
;
965 u_char
*x3_proof
, *x4_proof
, *x2_s_proof
;
966 u_int x3_proof_len
, x4_proof_len
, x2_s_proof_len
;
967 char *crypt_scheme
, *salt
;
969 /* Disable this message */
970 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1
, NULL
);
972 if ((pctx
->g_x3
= BN_new()) == NULL
||
973 (pctx
->g_x4
= BN_new()) == NULL
)
974 fatal("%s: BN_new", __func__
);
976 /* Fetch step 1 values */
977 crypt_scheme
= packet_get_string(NULL
);
978 salt
= packet_get_string(NULL
);
979 pctx
->server_id
= packet_get_string(&pctx
->server_id_len
);
980 packet_get_bignum2(pctx
->g_x3
);
981 packet_get_bignum2(pctx
->g_x4
);
982 x3_proof
= packet_get_string(&x3_proof_len
);
983 x4_proof
= packet_get_string(&x4_proof_len
);
986 JPAKE_DEBUG_CTX((pctx
, "step 1 received in %s", __func__
));
988 /* Obtain password and derive secret */
989 pctx
->s
= jpake_password_to_secret(authctxt
, crypt_scheme
, salt
);
990 bzero(crypt_scheme
, strlen(crypt_scheme
));
991 bzero(salt
, strlen(salt
));
994 JPAKE_DEBUG_BN((pctx
->s
, "%s: s = ", __func__
));
996 /* Calculate step 2 values */
997 jpake_step2(pctx
->grp
, pctx
->s
, pctx
->g_x1
,
998 pctx
->g_x3
, pctx
->g_x4
, pctx
->x2
,
999 pctx
->server_id
, pctx
->server_id_len
,
1000 pctx
->client_id
, pctx
->client_id_len
,
1001 x3_proof
, x3_proof_len
,
1002 x4_proof
, x4_proof_len
,
1004 &x2_s_proof
, &x2_s_proof_len
);
1006 bzero(x3_proof
, x3_proof_len
);
1007 bzero(x4_proof
, x4_proof_len
);
1011 JPAKE_DEBUG_CTX((pctx
, "step 2 sending in %s", __func__
));
1013 /* Send values for step 2 */
1014 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP2
);
1015 packet_put_bignum2(pctx
->a
);
1016 packet_put_string(x2_s_proof
, x2_s_proof_len
);
1019 bzero(x2_s_proof
, x2_s_proof_len
);
1022 /* Expect step 2 packet from peer */
1023 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2
,
1024 input_userauth_jpake_server_step2
);
1029 input_userauth_jpake_server_step2(int type
, u_int32_t seq
, void *ctxt
)
1031 Authctxt
*authctxt
= ctxt
;
1032 struct jpake_ctx
*pctx
= authctxt
->methoddata
;
1034 u_int x4_s_proof_len
;
1036 /* Disable this message */
1037 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2
, NULL
);
1039 if ((pctx
->b
= BN_new()) == NULL
)
1040 fatal("%s: BN_new", __func__
);
1042 /* Fetch step 2 values */
1043 packet_get_bignum2(pctx
->b
);
1044 x4_s_proof
= packet_get_string(&x4_s_proof_len
);
1047 JPAKE_DEBUG_CTX((pctx
, "step 2 received in %s", __func__
));
1049 /* Derive shared key and calculate confirmation hash */
1050 jpake_key_confirm(pctx
->grp
, pctx
->s
, pctx
->b
,
1051 pctx
->x2
, pctx
->g_x1
, pctx
->g_x2
, pctx
->g_x3
, pctx
->g_x4
,
1052 pctx
->client_id
, pctx
->client_id_len
,
1053 pctx
->server_id
, pctx
->server_id_len
,
1054 session_id2
, session_id2_len
,
1055 x4_s_proof
, x4_s_proof_len
,
1057 &pctx
->h_k_cid_sessid
, &pctx
->h_k_cid_sessid_len
);
1059 bzero(x4_s_proof
, x4_s_proof_len
);
1062 JPAKE_DEBUG_CTX((pctx
, "confirm sending in %s", __func__
));
1064 /* Send key confirmation proof */
1065 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_CONFIRM
);
1066 packet_put_string(pctx
->h_k_cid_sessid
, pctx
->h_k_cid_sessid_len
);
1069 /* Expect confirmation from peer */
1070 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM
,
1071 input_userauth_jpake_server_confirm
);
1076 input_userauth_jpake_server_confirm(int type
, u_int32_t seq
, void *ctxt
)
1078 Authctxt
*authctxt
= ctxt
;
1079 struct jpake_ctx
*pctx
= authctxt
->methoddata
;
1081 /* Disable this message */
1082 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM
, NULL
);
1084 pctx
->h_k_sid_sessid
= packet_get_string(&pctx
->h_k_sid_sessid_len
);
1087 JPAKE_DEBUG_CTX((pctx
, "confirm received in %s", __func__
));
1089 /* Verify expected confirmation hash */
1090 if (jpake_check_confirm(pctx
->k
,
1091 pctx
->server_id
, pctx
->server_id_len
,
1092 session_id2
, session_id2_len
,
1093 pctx
->h_k_sid_sessid
, pctx
->h_k_sid_sessid_len
) == 1)
1094 debug("%s: %s success", __func__
, authctxt
->method
->name
);
1096 debug("%s: confirmation mismatch", __func__
);
1097 /* XXX stash this so if auth succeeds then we can warn/kill */
1100 userauth_jpake_cleanup(authctxt
);
1105 identity_sign(Identity
*id
, u_char
**sigp
, u_int
*lenp
,
1106 u_char
*data
, u_int datalen
)
1111 /* the agent supports this key */
1113 return (ssh_agent_sign(id
->ac
, id
->key
, sigp
, lenp
,
1116 * we have already loaded the private key or
1117 * the private key is stored in external hardware
1119 if (id
->isprivate
|| (id
->key
->flags
& KEY_FLAG_EXT
))
1120 return (key_sign(id
->key
, sigp
, lenp
, data
, datalen
));
1121 /* load the private key from the file */
1122 if ((prv
= load_identity_file(id
->filename
)) == NULL
)
1124 ret
= key_sign(prv
, sigp
, lenp
, data
, datalen
);
1130 sign_and_send_pubkey(Authctxt
*authctxt
, Identity
*id
)
1133 u_char
*blob
, *signature
;
1134 u_int bloblen
, slen
;
1139 debug3("sign_and_send_pubkey");
1141 if (key_to_blob(id
->key
, &blob
, &bloblen
) == 0) {
1142 /* we cannot handle this key */
1143 debug3("sign_and_send_pubkey: cannot handle key");
1146 /* data to be signed */
1148 if (datafellows
& SSH_OLD_SESSIONID
) {
1149 buffer_append(&b
, session_id2
, session_id2_len
);
1150 skip
= session_id2_len
;
1152 buffer_put_string(&b
, session_id2
, session_id2_len
);
1153 skip
= buffer_len(&b
);
1155 buffer_put_char(&b
, SSH2_MSG_USERAUTH_REQUEST
);
1156 buffer_put_cstring(&b
, authctxt
->server_user
);
1157 buffer_put_cstring(&b
,
1158 datafellows
& SSH_BUG_PKSERVICE
?
1161 if (datafellows
& SSH_BUG_PKAUTH
) {
1162 buffer_put_char(&b
, have_sig
);
1164 buffer_put_cstring(&b
, authctxt
->method
->name
);
1165 buffer_put_char(&b
, have_sig
);
1166 buffer_put_cstring(&b
, key_ssh_name(id
->key
));
1168 buffer_put_string(&b
, blob
, bloblen
);
1170 /* generate signature */
1171 ret
= identity_sign(id
, &signature
, &slen
,
1172 buffer_ptr(&b
), buffer_len(&b
));
1181 if (datafellows
& SSH_BUG_PKSERVICE
) {
1183 buffer_append(&b
, session_id2
, session_id2_len
);
1184 skip
= session_id2_len
;
1185 buffer_put_char(&b
, SSH2_MSG_USERAUTH_REQUEST
);
1186 buffer_put_cstring(&b
, authctxt
->server_user
);
1187 buffer_put_cstring(&b
, authctxt
->service
);
1188 buffer_put_cstring(&b
, authctxt
->method
->name
);
1189 buffer_put_char(&b
, have_sig
);
1190 if (!(datafellows
& SSH_BUG_PKAUTH
))
1191 buffer_put_cstring(&b
, key_ssh_name(id
->key
));
1192 buffer_put_string(&b
, blob
, bloblen
);
1196 /* append signature */
1197 buffer_put_string(&b
, signature
, slen
);
1200 /* skip session id and packet type */
1201 if (buffer_len(&b
) < skip
+ 1)
1202 fatal("userauth_pubkey: internal error");
1203 buffer_consume(&b
, skip
+ 1);
1205 /* put remaining data from buffer into packet */
1206 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
1207 packet_put_raw(buffer_ptr(&b
), buffer_len(&b
));
1215 send_pubkey_test(Authctxt
*authctxt
, Identity
*id
)
1218 u_int bloblen
, have_sig
= 0;
1220 debug3("send_pubkey_test");
1222 if (key_to_blob(id
->key
, &blob
, &bloblen
) == 0) {
1223 /* we cannot handle this key */
1224 debug3("send_pubkey_test: cannot handle key");
1227 /* register callback for USERAUTH_PK_OK message */
1228 dispatch_set(SSH2_MSG_USERAUTH_PK_OK
, &input_userauth_pk_ok
);
1230 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
1231 packet_put_cstring(authctxt
->server_user
);
1232 packet_put_cstring(authctxt
->service
);
1233 packet_put_cstring(authctxt
->method
->name
);
1234 packet_put_char(have_sig
);
1235 if (!(datafellows
& SSH_BUG_PKAUTH
))
1236 packet_put_cstring(key_ssh_name(id
->key
));
1237 packet_put_string(blob
, bloblen
);
1244 load_identity_file(char *filename
)
1247 char prompt
[300], *passphrase
;
1248 int perm_ok
, quit
, i
;
1251 if (stat(filename
, &st
) < 0) {
1252 debug3("no such identity: %s", filename
);
1255 private = key_load_private_type(KEY_UNSPEC
, filename
, "", NULL
, &perm_ok
);
1258 if (private == NULL
) {
1259 if (options
.batch_mode
)
1261 snprintf(prompt
, sizeof prompt
,
1262 "Enter passphrase for key '%.100s': ", filename
);
1263 for (i
= 0; i
< options
.number_of_password_prompts
; i
++) {
1264 passphrase
= read_passphrase(prompt
, 0);
1265 if (strcmp(passphrase
, "") != 0) {
1266 private = key_load_private_type(KEY_UNSPEC
,
1267 filename
, passphrase
, NULL
, NULL
);
1270 debug2("no passphrase given, try next key");
1273 memset(passphrase
, 0, strlen(passphrase
));
1275 if (private != NULL
|| quit
)
1277 debug2("bad passphrase given, try again...");
1284 * try keys in the following order:
1285 * 1. agent keys that are found in the config file
1286 * 2. other agent keys
1287 * 3. keys that are only listed in the config file
1290 pubkey_prepare(Authctxt
*authctxt
)
1293 Idlist agent
, files
, *preferred
;
1295 AuthenticationConnection
*ac
;
1299 TAILQ_INIT(&agent
); /* keys from the agent */
1300 TAILQ_INIT(&files
); /* keys from the config file */
1301 preferred
= &authctxt
->keys
;
1302 TAILQ_INIT(preferred
); /* preferred order of keys */
1304 /* list of keys stored in the filesystem */
1305 for (i
= 0; i
< options
.num_identity_files
; i
++) {
1306 key
= options
.identity_keys
[i
];
1307 if (key
&& key
->type
== KEY_RSA1
)
1309 options
.identity_keys
[i
] = NULL
;
1310 id
= xcalloc(1, sizeof(*id
));
1312 id
->filename
= xstrdup(options
.identity_files
[i
]);
1313 TAILQ_INSERT_TAIL(&files
, id
, next
);
1315 /* list of keys supported by the agent */
1316 if ((ac
= ssh_get_authentication_connection())) {
1317 for (key
= ssh_get_first_identity(ac
, &comment
, 2);
1319 key
= ssh_get_next_identity(ac
, &comment
, 2)) {
1321 TAILQ_FOREACH(id
, &files
, next
) {
1322 /* agent keys from the config file are preferred */
1323 if (key_equal(key
, id
->key
)) {
1326 TAILQ_REMOVE(&files
, id
, next
);
1327 TAILQ_INSERT_TAIL(preferred
, id
, next
);
1333 if (!found
&& !options
.identities_only
) {
1334 id
= xcalloc(1, sizeof(*id
));
1336 id
->filename
= comment
;
1338 TAILQ_INSERT_TAIL(&agent
, id
, next
);
1341 /* append remaining agent keys */
1342 for (id
= TAILQ_FIRST(&agent
); id
; id
= TAILQ_FIRST(&agent
)) {
1343 TAILQ_REMOVE(&agent
, id
, next
);
1344 TAILQ_INSERT_TAIL(preferred
, id
, next
);
1346 authctxt
->agent
= ac
;
1348 /* append remaining keys from the config file */
1349 for (id
= TAILQ_FIRST(&files
); id
; id
= TAILQ_FIRST(&files
)) {
1350 TAILQ_REMOVE(&files
, id
, next
);
1351 TAILQ_INSERT_TAIL(preferred
, id
, next
);
1353 TAILQ_FOREACH(id
, preferred
, next
) {
1354 debug2("key: %s (%p)", id
->filename
, id
->key
);
1359 pubkey_cleanup(Authctxt
*authctxt
)
1363 if (authctxt
->agent
!= NULL
)
1364 ssh_close_authentication_connection(authctxt
->agent
);
1365 for (id
= TAILQ_FIRST(&authctxt
->keys
); id
;
1366 id
= TAILQ_FIRST(&authctxt
->keys
)) {
1367 TAILQ_REMOVE(&authctxt
->keys
, id
, next
);
1371 xfree(id
->filename
);
1377 userauth_pubkey(Authctxt
*authctxt
)
1382 while ((id
= TAILQ_FIRST(&authctxt
->keys
))) {
1385 /* move key to the end of the queue */
1386 TAILQ_REMOVE(&authctxt
->keys
, id
, next
);
1387 TAILQ_INSERT_TAIL(&authctxt
->keys
, id
, next
);
1389 * send a test message if we have the public key. for
1390 * encrypted keys we cannot do this and have to load the
1391 * private key instead
1393 if (id
->key
&& id
->key
->type
!= KEY_RSA1
) {
1394 debug("Offering public key: %s", id
->filename
);
1395 sent
= send_pubkey_test(authctxt
, id
);
1396 } else if (id
->key
== NULL
) {
1397 debug("Trying private key: %s", id
->filename
);
1398 id
->key
= load_identity_file(id
->filename
);
1399 if (id
->key
!= NULL
) {
1401 sent
= sign_and_send_pubkey(authctxt
, id
);
1413 * Send userauth request message specifying keyboard-interactive method.
1416 userauth_kbdint(Authctxt
*authctxt
)
1418 static int attempt
= 0;
1420 if (attempt
++ >= options
.number_of_password_prompts
)
1422 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1423 if (attempt
> 1 && !authctxt
->info_req_seen
) {
1424 debug3("userauth_kbdint: disable: no info_req_seen");
1425 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST
, NULL
);
1429 debug2("userauth_kbdint");
1430 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
1431 packet_put_cstring(authctxt
->server_user
);
1432 packet_put_cstring(authctxt
->service
);
1433 packet_put_cstring(authctxt
->method
->name
);
1434 packet_put_cstring(""); /* lang */
1435 packet_put_cstring(options
.kbd_interactive_devices
?
1436 options
.kbd_interactive_devices
: "");
1439 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST
, &input_userauth_info_req
);
1444 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1447 input_userauth_info_req(int type
, u_int32_t seq
, void *ctxt
)
1449 Authctxt
*authctxt
= ctxt
;
1450 char *name
, *inst
, *lang
, *prompt
, *response
;
1451 u_int num_prompts
, i
;
1454 debug2("input_userauth_info_req");
1456 if (authctxt
== NULL
)
1457 fatal("input_userauth_info_req: no authentication context");
1459 authctxt
->info_req_seen
= 1;
1461 name
= packet_get_string(NULL
);
1462 inst
= packet_get_string(NULL
);
1463 lang
= packet_get_string(NULL
);
1464 if (strlen(name
) > 0)
1466 if (strlen(inst
) > 0)
1472 num_prompts
= packet_get_int();
1474 * Begin to build info response packet based on prompts requested.
1475 * We commit to providing the correct number of responses, so if
1476 * further on we run into a problem that prevents this, we have to
1477 * be sure and clean this up and send a correct error response.
1479 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE
);
1480 packet_put_int(num_prompts
);
1482 debug2("input_userauth_info_req: num_prompts %d", num_prompts
);
1483 for (i
= 0; i
< num_prompts
; i
++) {
1484 prompt
= packet_get_string(NULL
);
1485 echo
= packet_get_char();
1487 response
= read_passphrase(prompt
, echo
? RP_ECHO
: 0);
1489 packet_put_cstring(response
);
1490 memset(response
, 0, strlen(response
));
1494 packet_check_eom(); /* done with parsing incoming message. */
1496 packet_add_padding(64);
1501 ssh_keysign(Key
*key
, u_char
**sigp
, u_int
*lenp
,
1502 u_char
*data
, u_int datalen
)
1507 int to
[2], from
[2], status
, version
= 2;
1509 debug2("ssh_keysign called");
1511 if (stat(_PATH_SSH_KEY_SIGN
, &st
) < 0) {
1512 error("ssh_keysign: no installed: %s", strerror(errno
));
1515 if (fflush(stdout
) != 0)
1516 error("ssh_keysign: fflush: %s", strerror(errno
));
1518 error("ssh_keysign: pipe: %s", strerror(errno
));
1521 if (pipe(from
) < 0) {
1522 error("ssh_keysign: pipe: %s", strerror(errno
));
1525 if ((pid
= fork()) < 0) {
1526 error("ssh_keysign: fork: %s", strerror(errno
));
1530 permanently_drop_suid(getuid());
1532 if (dup2(from
[1], STDOUT_FILENO
) < 0)
1533 fatal("ssh_keysign: dup2: %s", strerror(errno
));
1535 if (dup2(to
[0], STDIN_FILENO
) < 0)
1536 fatal("ssh_keysign: dup2: %s", strerror(errno
));
1539 execl(_PATH_SSH_KEY_SIGN
, _PATH_SSH_KEY_SIGN
, (char *) 0);
1540 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN
,
1547 buffer_put_int(&b
, packet_get_connection_in()); /* send # of socket */
1548 buffer_put_string(&b
, data
, datalen
);
1549 if (ssh_msg_send(to
[1], version
, &b
) == -1)
1550 fatal("ssh_keysign: couldn't send request");
1552 if (ssh_msg_recv(from
[0], &b
) < 0) {
1553 error("ssh_keysign: no reply");
1560 while (waitpid(pid
, &status
, 0) < 0)
1564 if (buffer_get_char(&b
) != version
) {
1565 error("ssh_keysign: bad version");
1569 *sigp
= buffer_get_string(&b
, lenp
);
1576 userauth_hostbased(Authctxt
*authctxt
)
1578 Key
*private = NULL
;
1579 Sensitive
*sensitive
= authctxt
->sensitive
;
1581 u_char
*signature
, *blob
;
1582 char *chost
, *pkalg
, *p
, myname
[NI_MAXHOST
];
1583 const char *service
;
1585 int ok
, i
, len
, found
= 0;
1587 /* check for a useful key */
1588 for (i
= 0; i
< sensitive
->nkeys
; i
++) {
1589 private = sensitive
->keys
[i
];
1590 if (private && private->type
!= KEY_RSA1
) {
1592 /* we take and free the key */
1593 sensitive
->keys
[i
] = NULL
;
1598 debug("No more client hostkeys for hostbased authentication.");
1601 if (key_to_blob(private, &blob
, &blen
) == 0) {
1605 /* figure out a name for the client host */
1607 if (packet_connection_is_on_socket())
1608 p
= get_local_name(packet_get_connection_in());
1610 if (gethostname(myname
, sizeof(myname
)) == -1) {
1611 verbose("userauth_hostbased: gethostname: %s",
1614 p
= xstrdup(myname
);
1617 error("userauth_hostbased: cannot get local ipaddr/name");
1622 len
= strlen(p
) + 2;
1623 xasprintf(&chost
, "%s.", p
);
1624 debug2("userauth_hostbased: chost %s", chost
);
1627 service
= datafellows
& SSH_BUG_HBSERVICE
? "ssh-userauth" :
1629 pkalg
= xstrdup(key_ssh_name(private));
1631 /* construct data */
1632 buffer_put_string(&b
, session_id2
, session_id2_len
);
1633 buffer_put_char(&b
, SSH2_MSG_USERAUTH_REQUEST
);
1634 buffer_put_cstring(&b
, authctxt
->server_user
);
1635 buffer_put_cstring(&b
, service
);
1636 buffer_put_cstring(&b
, authctxt
->method
->name
);
1637 buffer_put_cstring(&b
, pkalg
);
1638 buffer_put_string(&b
, blob
, blen
);
1639 buffer_put_cstring(&b
, chost
);
1640 buffer_put_cstring(&b
, authctxt
->local_user
);
1644 if (sensitive
->external_keysign
)
1645 ok
= ssh_keysign(private, &signature
, &slen
,
1646 buffer_ptr(&b
), buffer_len(&b
));
1648 ok
= key_sign(private, &signature
, &slen
,
1649 buffer_ptr(&b
), buffer_len(&b
));
1653 error("key_sign failed");
1659 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
1660 packet_put_cstring(authctxt
->server_user
);
1661 packet_put_cstring(authctxt
->service
);
1662 packet_put_cstring(authctxt
->method
->name
);
1663 packet_put_cstring(pkalg
);
1664 packet_put_string(blob
, blen
);
1665 packet_put_cstring(chost
);
1666 packet_put_cstring(authctxt
->local_user
);
1667 packet_put_string(signature
, slen
);
1668 memset(signature
, 's', slen
);
1680 userauth_jpake(Authctxt
*authctxt
)
1682 struct jpake_ctx
*pctx
;
1683 u_char
*x1_proof
, *x2_proof
;
1684 u_int x1_proof_len
, x2_proof_len
;
1685 static int attempt
= 0; /* XXX share with userauth_password's? */
1687 if (attempt
++ >= options
.number_of_password_prompts
)
1690 error("Permission denied, please try again.");
1692 if (authctxt
->methoddata
!= NULL
)
1693 fatal("%s: authctxt->methoddata already set (%p)",
1694 __func__
, authctxt
->methoddata
);
1696 authctxt
->methoddata
= pctx
= jpake_new();
1699 * Send request immediately, to get the protocol going while
1700 * we do the initial computations.
1702 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
1703 packet_put_cstring(authctxt
->server_user
);
1704 packet_put_cstring(authctxt
->service
);
1705 packet_put_cstring(authctxt
->method
->name
);
1707 packet_write_wait();
1709 jpake_step1(pctx
->grp
,
1710 &pctx
->client_id
, &pctx
->client_id_len
,
1711 &pctx
->x1
, &pctx
->x2
, &pctx
->g_x1
, &pctx
->g_x2
,
1712 &x1_proof
, &x1_proof_len
,
1713 &x2_proof
, &x2_proof_len
);
1715 JPAKE_DEBUG_CTX((pctx
, "step 1 sending in %s", __func__
));
1717 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP1
);
1718 packet_put_string(pctx
->client_id
, pctx
->client_id_len
);
1719 packet_put_bignum2(pctx
->g_x1
);
1720 packet_put_bignum2(pctx
->g_x2
);
1721 packet_put_string(x1_proof
, x1_proof_len
);
1722 packet_put_string(x2_proof
, x2_proof_len
);
1725 bzero(x1_proof
, x1_proof_len
);
1726 bzero(x2_proof
, x2_proof_len
);
1730 /* Expect step 1 packet from peer */
1731 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1
,
1732 input_userauth_jpake_server_step1
);
1733 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS
,
1734 &input_userauth_success_unexpected
);
1740 userauth_jpake_cleanup(Authctxt
*authctxt
)
1742 debug3("%s: clean up", __func__
);
1743 if (authctxt
->methoddata
!= NULL
) {
1744 jpake_free(authctxt
->methoddata
);
1745 authctxt
->methoddata
= NULL
;
1747 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS
, &input_userauth_success
);
1751 /* find auth method */
1754 * given auth method name, if configurable options permit this method fill
1755 * in auth_ident field and return true, otherwise return false.
1758 authmethod_is_enabled(Authmethod
*method
)
1762 /* return false if options indicate this method is disabled */
1763 if (method
->enabled
== NULL
|| *method
->enabled
== 0)
1765 /* return false if batch mode is enabled but method needs interactive mode */
1766 if (method
->batch_flag
!= NULL
&& *method
->batch_flag
!= 0)
1772 authmethod_lookup(const char *name
)
1774 Authmethod
*method
= NULL
;
1776 for (method
= authmethods
; method
->name
!= NULL
; method
++)
1777 if (strcmp(name
, method
->name
) == 0)
1779 debug2("Unrecognized authentication method name: %s", name
? name
: "NULL");
1783 /* XXX internal state */
1784 static Authmethod
*current
= NULL
;
1785 static char *supported
= NULL
;
1786 static char *preferred
= NULL
;
1789 * Given the authentication method list sent by the server, return the
1790 * next method we should try. If the server initially sends a nil list,
1791 * use a built-in default list.
1794 authmethod_get(char *authlist
)
1799 /* Use a suitable default if we're passed a nil list. */
1800 if (authlist
== NULL
|| strlen(authlist
) == 0)
1801 authlist
= options
.preferred_authentications
;
1803 if (supported
== NULL
|| strcmp(authlist
, supported
) != 0) {
1804 debug3("start over, passed a different list %s", authlist
);
1805 if (supported
!= NULL
)
1807 supported
= xstrdup(authlist
);
1808 preferred
= options
.preferred_authentications
;
1809 debug3("preferred %s", preferred
);
1811 } else if (current
!= NULL
&& authmethod_is_enabled(current
))
1815 if ((name
= match_list(preferred
, supported
, &next
)) == NULL
) {
1816 debug("No more authentication methods to try.");
1821 debug3("authmethod_lookup %s", name
);
1822 debug3("remaining preferred: %s", preferred
);
1823 if ((current
= authmethod_lookup(name
)) != NULL
&&
1824 authmethod_is_enabled(current
)) {
1825 debug3("authmethod_is_enabled %s", name
);
1826 debug("Next authentication method: %s", name
);
1833 authmethods_get(void)
1835 Authmethod
*method
= NULL
;
1840 for (method
= authmethods
; method
->name
!= NULL
; method
++) {
1841 if (authmethod_is_enabled(method
)) {
1842 if (buffer_len(&b
) > 0)
1843 buffer_append(&b
, ",", 1);
1844 buffer_append(&b
, method
->name
, strlen(method
->name
));
1847 buffer_append(&b
, "\0", 1);
1848 list
= xstrdup(buffer_ptr(&b
));