1 /* $OpenBSD: sshconnect2.c,v 1.180 2010/02/26 20:29:54 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>
43 #if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H)
47 #include "openbsd-compat/sys-queue.h"
58 #include "myproposal.h"
59 #include "sshconnect.h"
70 #include "pathnames.h"
80 extern char *client_version_string
;
81 extern char *server_version_string
;
82 extern Options options
;
88 u_char
*session_id2
= NULL
;
89 u_int session_id2_len
= 0;
92 struct sockaddr
*xxx_hostaddr
;
97 verify_host_key_callback(Key
*hostkey
)
99 if (verify_host_key(xxx_host
, xxx_hostaddr
, hostkey
) == -1)
100 fatal("Host key verification failed.");
105 ssh_kex2(char *host
, struct sockaddr
*hostaddr
)
110 xxx_hostaddr
= hostaddr
;
112 if (options
.ciphers
== (char *)-1) {
113 logit("No valid ciphers for protocol version 2 given, using defaults.");
114 options
.ciphers
= NULL
;
116 if (options
.ciphers
!= NULL
) {
117 myproposal
[PROPOSAL_ENC_ALGS_CTOS
] =
118 myproposal
[PROPOSAL_ENC_ALGS_STOC
] = options
.ciphers
;
120 myproposal
[PROPOSAL_ENC_ALGS_CTOS
] =
121 compat_cipher_proposal(myproposal
[PROPOSAL_ENC_ALGS_CTOS
]);
122 myproposal
[PROPOSAL_ENC_ALGS_STOC
] =
123 compat_cipher_proposal(myproposal
[PROPOSAL_ENC_ALGS_STOC
]);
124 if (options
.compression
) {
125 myproposal
[PROPOSAL_COMP_ALGS_CTOS
] =
126 myproposal
[PROPOSAL_COMP_ALGS_STOC
] = "zlib@openssh.com,zlib,none";
128 myproposal
[PROPOSAL_COMP_ALGS_CTOS
] =
129 myproposal
[PROPOSAL_COMP_ALGS_STOC
] = "none,zlib@openssh.com,zlib";
131 if (options
.macs
!= NULL
) {
132 myproposal
[PROPOSAL_MAC_ALGS_CTOS
] =
133 myproposal
[PROPOSAL_MAC_ALGS_STOC
] = options
.macs
;
135 if (options
.hostkeyalgorithms
!= NULL
)
136 myproposal
[PROPOSAL_SERVER_HOST_KEY_ALGS
] =
137 options
.hostkeyalgorithms
;
139 if (options
.rekey_limit
)
140 packet_set_rekey_limit((u_int32_t
)options
.rekey_limit
);
142 /* start key exchange */
143 kex
= kex_setup(myproposal
);
144 kex
->kex
[KEX_DH_GRP1_SHA1
] = kexdh_client
;
145 kex
->kex
[KEX_DH_GRP14_SHA1
] = kexdh_client
;
146 kex
->kex
[KEX_DH_GEX_SHA1
] = kexgex_client
;
147 kex
->kex
[KEX_DH_GEX_SHA256
] = kexgex_client
;
148 kex
->client_version_string
=client_version_string
;
149 kex
->server_version_string
=server_version_string
;
150 kex
->verify_host_key
=&verify_host_key_callback
;
154 dispatch_run(DISPATCH_BLOCK
, &kex
->done
, kex
);
156 if (options
.use_roaming
&& !kex
->roaming
) {
157 debug("Roaming not allowed by server");
158 options
.use_roaming
= 0;
161 session_id2
= kex
->session_id
;
162 session_id2_len
= kex
->session_id_len
;
165 /* send 1st encrypted/maced/compressed message */
166 packet_start(SSH2_MSG_IGNORE
);
167 packet_put_cstring("markus");
177 typedef struct Authctxt Authctxt
;
178 typedef struct Authmethod Authmethod
;
179 typedef struct identity Identity
;
180 typedef struct idlist Idlist
;
183 TAILQ_ENTRY(identity
) next
;
184 AuthenticationConnection
*ac
; /* set if agent supports key */
185 Key
*key
; /* public/private key */
186 char *filename
; /* comment for agent-only keys */
188 int isprivate
; /* key points to the private key */
190 TAILQ_HEAD(idlist
, identity
);
193 const char *server_user
;
194 const char *local_user
;
202 AuthenticationConnection
*agent
;
204 Sensitive
*sensitive
;
205 /* kbd-interactive */
211 char *name
; /* string to compare against server's list */
212 int (*userauth
)(Authctxt
*authctxt
);
213 void (*cleanup
)(Authctxt
*authctxt
);
214 int *enabled
; /* flag in option struct that enables method */
215 int *batch_flag
; /* flag in option struct that disables method */
218 void input_userauth_success(int, u_int32_t
, void *);
219 void input_userauth_success_unexpected(int, u_int32_t
, void *);
220 void input_userauth_failure(int, u_int32_t
, void *);
221 void input_userauth_banner(int, u_int32_t
, void *);
222 void input_userauth_error(int, u_int32_t
, void *);
223 void input_userauth_info_req(int, u_int32_t
, void *);
224 void input_userauth_pk_ok(int, u_int32_t
, void *);
225 void input_userauth_passwd_changereq(int, u_int32_t
, void *);
226 void input_userauth_jpake_server_step1(int, u_int32_t
, void *);
227 void input_userauth_jpake_server_step2(int, u_int32_t
, void *);
228 void input_userauth_jpake_server_confirm(int, u_int32_t
, void *);
230 int userauth_none(Authctxt
*);
231 int userauth_pubkey(Authctxt
*);
232 int userauth_passwd(Authctxt
*);
233 int userauth_kbdint(Authctxt
*);
234 int userauth_hostbased(Authctxt
*);
235 int userauth_jpake(Authctxt
*);
237 void userauth_jpake_cleanup(Authctxt
*);
240 int userauth_gssapi(Authctxt
*authctxt
);
241 void input_gssapi_response(int type
, u_int32_t
, void *);
242 void input_gssapi_token(int type
, u_int32_t
, void *);
243 void input_gssapi_hash(int type
, u_int32_t
, void *);
244 void input_gssapi_error(int, u_int32_t
, void *);
245 void input_gssapi_errtok(int, u_int32_t
, void *);
248 void userauth(Authctxt
*, char *);
250 static int sign_and_send_pubkey(Authctxt
*, Identity
*);
251 static void pubkey_prepare(Authctxt
*);
252 static void pubkey_cleanup(Authctxt
*);
253 static Key
*load_identity_file(char *);
255 static Authmethod
*authmethod_get(char *authlist
);
256 static Authmethod
*authmethod_lookup(const char *name
);
257 static char *authmethods_get(void);
259 Authmethod authmethods
[] = {
264 &options
.gss_authentication
,
270 &options
.hostbased_authentication
,
275 &options
.pubkey_authentication
,
278 {"jpake-01@openssh.com",
280 userauth_jpake_cleanup
,
281 &options
.zero_knowledge_password_authentication
,
282 &options
.batch_mode
},
284 {"keyboard-interactive",
287 &options
.kbd_interactive_authentication
,
288 &options
.batch_mode
},
292 &options
.password_authentication
,
293 &options
.batch_mode
},
299 {NULL
, NULL
, NULL
, NULL
, NULL
}
303 ssh_userauth2(const char *local_user
, const char *server_user
, char *host
,
304 Sensitive
*sensitive
)
309 if (options
.challenge_response_authentication
)
310 options
.kbd_interactive_authentication
= 1;
312 packet_start(SSH2_MSG_SERVICE_REQUEST
);
313 packet_put_cstring("ssh-userauth");
315 debug("SSH2_MSG_SERVICE_REQUEST sent");
317 type
= packet_read();
318 if (type
!= SSH2_MSG_SERVICE_ACCEPT
)
319 fatal("Server denied authentication request: %d", type
);
320 if (packet_remaining() > 0) {
321 char *reply
= packet_get_string(NULL
);
322 debug2("service_accept: %s", reply
);
325 debug2("buggy server: service_accept w/o service");
328 debug("SSH2_MSG_SERVICE_ACCEPT received");
330 if (options
.preferred_authentications
== NULL
)
331 options
.preferred_authentications
= authmethods_get();
333 /* setup authentication context */
334 memset(&authctxt
, 0, sizeof(authctxt
));
335 pubkey_prepare(&authctxt
);
336 authctxt
.server_user
= server_user
;
337 authctxt
.local_user
= local_user
;
338 authctxt
.host
= host
;
339 authctxt
.service
= "ssh-connection"; /* service name */
340 authctxt
.success
= 0;
341 authctxt
.method
= authmethod_lookup("none");
342 authctxt
.authlist
= NULL
;
343 authctxt
.methoddata
= NULL
;
344 authctxt
.sensitive
= sensitive
;
345 authctxt
.info_req_seen
= 0;
346 if (authctxt
.method
== NULL
)
347 fatal("ssh_userauth2: internal error: cannot send userauth none request");
349 /* initial userauth request */
350 userauth_none(&authctxt
);
352 dispatch_init(&input_userauth_error
);
353 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS
, &input_userauth_success
);
354 dispatch_set(SSH2_MSG_USERAUTH_FAILURE
, &input_userauth_failure
);
355 dispatch_set(SSH2_MSG_USERAUTH_BANNER
, &input_userauth_banner
);
356 dispatch_run(DISPATCH_BLOCK
, &authctxt
.success
, &authctxt
); /* loop until success */
358 pubkey_cleanup(&authctxt
);
359 dispatch_range(SSH2_MSG_USERAUTH_MIN
, SSH2_MSG_USERAUTH_MAX
, NULL
);
361 debug("Authentication succeeded (%s).", authctxt
.method
->name
);
365 userauth(Authctxt
*authctxt
, char *authlist
)
367 if (authctxt
->method
!= NULL
&& authctxt
->method
->cleanup
!= NULL
)
368 authctxt
->method
->cleanup(authctxt
);
370 if (authctxt
->methoddata
) {
371 xfree(authctxt
->methoddata
);
372 authctxt
->methoddata
= NULL
;
374 if (authlist
== NULL
) {
375 authlist
= authctxt
->authlist
;
377 if (authctxt
->authlist
)
378 xfree(authctxt
->authlist
);
379 authctxt
->authlist
= authlist
;
382 Authmethod
*method
= authmethod_get(authlist
);
384 fatal("Permission denied (%s).", authlist
);
385 authctxt
->method
= method
;
387 /* reset the per method handler */
388 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN
,
389 SSH2_MSG_USERAUTH_PER_METHOD_MAX
, NULL
);
391 /* and try new method */
392 if (method
->userauth(authctxt
) != 0) {
393 debug2("we sent a %s packet, wait for reply", method
->name
);
396 debug2("we did not send a packet, disable method");
397 method
->enabled
= NULL
;
404 input_userauth_error(int type
, u_int32_t seq
, void *ctxt
)
406 fatal("input_userauth_error: bad message during authentication: "
412 input_userauth_banner(int type
, u_int32_t seq
, void *ctxt
)
414 char *msg
, *raw
, *lang
;
417 debug3("input_userauth_banner");
418 raw
= packet_get_string(&len
);
419 lang
= packet_get_string(NULL
);
420 if (len
> 0 && options
.log_level
>= SYSLOG_LEVEL_INFO
) {
423 msg
= xmalloc(len
* 4 + 1); /* max expansion from strnvis() */
424 strnvis(msg
, raw
, len
* 4 + 1, VIS_SAFE
|VIS_OCTAL
|VIS_NOSLASH
);
425 fprintf(stderr
, "%s", msg
);
434 input_userauth_success(int type
, u_int32_t seq
, void *ctxt
)
436 Authctxt
*authctxt
= ctxt
;
438 if (authctxt
== NULL
)
439 fatal("input_userauth_success: no authentication context");
440 if (authctxt
->authlist
) {
441 xfree(authctxt
->authlist
);
442 authctxt
->authlist
= NULL
;
444 if (authctxt
->method
!= NULL
&& authctxt
->method
->cleanup
!= NULL
)
445 authctxt
->method
->cleanup(authctxt
);
446 if (authctxt
->methoddata
) {
447 xfree(authctxt
->methoddata
);
448 authctxt
->methoddata
= NULL
;
450 authctxt
->success
= 1; /* break out */
454 input_userauth_success_unexpected(int type
, u_int32_t seq
, void *ctxt
)
456 Authctxt
*authctxt
= ctxt
;
458 if (authctxt
== NULL
)
459 fatal("%s: no authentication context", __func__
);
461 fatal("Unexpected authentication success during %s.",
462 authctxt
->method
->name
);
467 input_userauth_failure(int type
, u_int32_t seq
, void *ctxt
)
469 Authctxt
*authctxt
= ctxt
;
470 char *authlist
= NULL
;
473 if (authctxt
== NULL
)
474 fatal("input_userauth_failure: no authentication context");
476 authlist
= packet_get_string(NULL
);
477 partial
= packet_get_char();
481 logit("Authenticated with partial success.");
482 debug("Authentications that can continue: %s", authlist
);
484 userauth(authctxt
, authlist
);
489 input_userauth_pk_ok(int type
, u_int32_t seq
, void *ctxt
)
491 Authctxt
*authctxt
= ctxt
;
495 int pktype
, sent
= 0;
500 if (authctxt
== NULL
)
501 fatal("input_userauth_pk_ok: no authentication context");
502 if (datafellows
& SSH_BUG_PKOK
) {
503 /* this is similar to SSH_BUG_PKAUTH */
504 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
505 pkblob
= packet_get_string(&blen
);
507 buffer_append(&b
, pkblob
, blen
);
508 pkalg
= buffer_get_string(&b
, &alen
);
511 pkalg
= packet_get_string(&alen
);
512 pkblob
= packet_get_string(&blen
);
516 debug("Server accepts key: pkalg %s blen %u", pkalg
, blen
);
518 if ((pktype
= key_type_from_name(pkalg
)) == KEY_UNSPEC
) {
519 debug("unknown pkalg %s", pkalg
);
522 if ((key
= key_from_blob(pkblob
, blen
)) == NULL
) {
523 debug("no key from blob. pkalg %s", pkalg
);
526 if (key
->type
!= pktype
) {
527 error("input_userauth_pk_ok: type mismatch "
528 "for decoded key (received %d, expected %d)",
532 fp
= key_fingerprint(key
, SSH_FP_MD5
, SSH_FP_HEX
);
533 debug2("input_userauth_pk_ok: fp %s", fp
);
537 * search keys in the reverse order, because last candidate has been
538 * moved to the end of the queue. this also avoids confusion by
541 TAILQ_FOREACH_REVERSE(id
, &authctxt
->keys
, idlist
, next
) {
542 if (key_equal(key
, id
->key
)) {
543 sent
= sign_and_send_pubkey(authctxt
, id
);
553 /* try another method if we did not send a packet */
555 userauth(authctxt
, NULL
);
560 userauth_gssapi(Authctxt
*authctxt
)
562 Gssctxt
*gssctxt
= NULL
;
563 static gss_OID_set gss_supported
= NULL
;
564 static u_int mech
= 0;
568 /* Try one GSSAPI method at a time, rather than sending them all at
571 if (gss_supported
== NULL
)
572 gss_indicate_mechs(&min
, &gss_supported
);
574 /* Check to see if the mechanism is usable before we offer it */
575 while (mech
< gss_supported
->count
&& !ok
) {
576 /* My DER encoding requires length<128 */
577 if (gss_supported
->elements
[mech
].length
< 128 &&
578 ssh_gssapi_check_mechanism(&gssctxt
,
579 &gss_supported
->elements
[mech
], authctxt
->host
)) {
580 ok
= 1; /* Mechanism works */
589 authctxt
->methoddata
=(void *)gssctxt
;
591 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
592 packet_put_cstring(authctxt
->server_user
);
593 packet_put_cstring(authctxt
->service
);
594 packet_put_cstring(authctxt
->method
->name
);
598 packet_put_int((gss_supported
->elements
[mech
].length
) + 2);
599 packet_put_char(SSH_GSS_OIDTYPE
);
600 packet_put_char(gss_supported
->elements
[mech
].length
);
601 packet_put_raw(gss_supported
->elements
[mech
].elements
,
602 gss_supported
->elements
[mech
].length
);
606 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE
, &input_gssapi_response
);
607 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN
, &input_gssapi_token
);
608 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR
, &input_gssapi_error
);
609 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK
, &input_gssapi_errtok
);
611 mech
++; /* Move along to next candidate */
617 process_gssapi_token(void *ctxt
, gss_buffer_t recv_tok
)
619 Authctxt
*authctxt
= ctxt
;
620 Gssctxt
*gssctxt
= authctxt
->methoddata
;
621 gss_buffer_desc send_tok
= GSS_C_EMPTY_BUFFER
;
622 gss_buffer_desc mic
= GSS_C_EMPTY_BUFFER
;
623 gss_buffer_desc gssbuf
;
624 OM_uint32 status
, ms
, flags
;
627 status
= ssh_gssapi_init_ctx(gssctxt
, options
.gss_deleg_creds
,
628 recv_tok
, &send_tok
, &flags
);
630 if (send_tok
.length
> 0) {
631 if (GSS_ERROR(status
))
632 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK
);
634 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN
);
636 packet_put_string(send_tok
.value
, send_tok
.length
);
638 gss_release_buffer(&ms
, &send_tok
);
641 if (status
== GSS_S_COMPLETE
) {
642 /* send either complete or MIC, depending on mechanism */
643 if (!(flags
& GSS_C_INTEG_FLAG
)) {
644 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE
);
647 ssh_gssapi_buildmic(&b
, authctxt
->server_user
,
648 authctxt
->service
, "gssapi-with-mic");
650 gssbuf
.value
= buffer_ptr(&b
);
651 gssbuf
.length
= buffer_len(&b
);
653 status
= ssh_gssapi_sign(gssctxt
, &gssbuf
, &mic
);
655 if (!GSS_ERROR(status
)) {
656 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC
);
657 packet_put_string(mic
.value
, mic
.length
);
663 gss_release_buffer(&ms
, &mic
);
672 input_gssapi_response(int type
, u_int32_t plen
, void *ctxt
)
674 Authctxt
*authctxt
= ctxt
;
679 if (authctxt
== NULL
)
680 fatal("input_gssapi_response: no authentication context");
681 gssctxt
= authctxt
->methoddata
;
684 oidv
= packet_get_string(&oidlen
);
687 oidv
[0] != SSH_GSS_OIDTYPE
||
688 oidv
[1] != oidlen
- 2) {
690 debug("Badly encoded mechanism OID received");
691 userauth(authctxt
, NULL
);
695 if (!ssh_gssapi_check_oid(gssctxt
, oidv
+ 2, oidlen
- 2))
696 fatal("Server returned different OID than expected");
702 if (GSS_ERROR(process_gssapi_token(ctxt
, GSS_C_NO_BUFFER
))) {
703 /* Start again with next method on list */
704 debug("Trying to start again");
705 userauth(authctxt
, NULL
);
712 input_gssapi_token(int type
, u_int32_t plen
, void *ctxt
)
714 Authctxt
*authctxt
= ctxt
;
715 gss_buffer_desc recv_tok
;
719 if (authctxt
== NULL
)
720 fatal("input_gssapi_response: no authentication context");
722 recv_tok
.value
= packet_get_string(&slen
);
723 recv_tok
.length
= slen
; /* safe typecast */
727 status
= process_gssapi_token(ctxt
, &recv_tok
);
729 xfree(recv_tok
.value
);
731 if (GSS_ERROR(status
)) {
732 /* Start again with the next method in the list */
733 userauth(authctxt
, NULL
);
740 input_gssapi_errtok(int type
, u_int32_t plen
, void *ctxt
)
742 Authctxt
*authctxt
= ctxt
;
744 gss_buffer_desc send_tok
= GSS_C_EMPTY_BUFFER
;
745 gss_buffer_desc recv_tok
;
746 OM_uint32 status
, ms
;
749 if (authctxt
== NULL
)
750 fatal("input_gssapi_response: no authentication context");
751 gssctxt
= authctxt
->methoddata
;
753 recv_tok
.value
= packet_get_string(&len
);
754 recv_tok
.length
= len
;
758 /* Stick it into GSSAPI and see what it says */
759 status
= ssh_gssapi_init_ctx(gssctxt
, options
.gss_deleg_creds
,
760 &recv_tok
, &send_tok
, NULL
);
762 xfree(recv_tok
.value
);
763 gss_release_buffer(&ms
, &send_tok
);
765 /* Server will be returning a failed packet after this one */
770 input_gssapi_error(int type
, u_int32_t plen
, void *ctxt
)
776 maj
=packet_get_int();
777 min
=packet_get_int();
778 msg
=packet_get_string(NULL
);
779 lang
=packet_get_string(NULL
);
783 debug("Server GSSAPI Error:\n%s", msg
);
790 userauth_none(Authctxt
*authctxt
)
792 /* initial userauth request */
793 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
794 packet_put_cstring(authctxt
->server_user
);
795 packet_put_cstring(authctxt
->service
);
796 packet_put_cstring(authctxt
->method
->name
);
802 userauth_passwd(Authctxt
*authctxt
)
804 static int attempt
= 0;
807 const char *host
= options
.host_key_alias
? options
.host_key_alias
:
810 if (attempt
++ >= options
.number_of_password_prompts
)
814 error("Permission denied, please try again.");
816 snprintf(prompt
, sizeof(prompt
), "%.30s@%.128s's password: ",
817 authctxt
->server_user
, host
);
818 password
= read_passphrase(prompt
, 0);
819 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
820 packet_put_cstring(authctxt
->server_user
);
821 packet_put_cstring(authctxt
->service
);
822 packet_put_cstring(authctxt
->method
->name
);
824 packet_put_cstring(password
);
825 memset(password
, 0, strlen(password
));
827 packet_add_padding(64);
830 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ
,
831 &input_userauth_passwd_changereq
);
837 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
841 input_userauth_passwd_changereq(int type
, u_int32_t seqnr
, void *ctxt
)
843 Authctxt
*authctxt
= ctxt
;
844 char *info
, *lang
, *password
= NULL
, *retype
= NULL
;
846 const char *host
= options
.host_key_alias
? options
.host_key_alias
:
849 debug2("input_userauth_passwd_changereq");
851 if (authctxt
== NULL
)
852 fatal("input_userauth_passwd_changereq: "
853 "no authentication context");
855 info
= packet_get_string(NULL
);
856 lang
= packet_get_string(NULL
);
857 if (strlen(info
) > 0)
861 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
862 packet_put_cstring(authctxt
->server_user
);
863 packet_put_cstring(authctxt
->service
);
864 packet_put_cstring(authctxt
->method
->name
);
865 packet_put_char(1); /* additional info */
866 snprintf(prompt
, sizeof(prompt
),
867 "Enter %.30s@%.128s's old password: ",
868 authctxt
->server_user
, host
);
869 password
= read_passphrase(prompt
, 0);
870 packet_put_cstring(password
);
871 memset(password
, 0, strlen(password
));
874 while (password
== NULL
) {
875 snprintf(prompt
, sizeof(prompt
),
876 "Enter %.30s@%.128s's new password: ",
877 authctxt
->server_user
, host
);
878 password
= read_passphrase(prompt
, RP_ALLOW_EOF
);
879 if (password
== NULL
) {
883 snprintf(prompt
, sizeof(prompt
),
884 "Retype %.30s@%.128s's new password: ",
885 authctxt
->server_user
, host
);
886 retype
= read_passphrase(prompt
, 0);
887 if (strcmp(password
, retype
) != 0) {
888 memset(password
, 0, strlen(password
));
890 logit("Mismatch; try again, EOF to quit.");
893 memset(retype
, 0, strlen(retype
));
896 packet_put_cstring(password
);
897 memset(password
, 0, strlen(password
));
899 packet_add_padding(64);
902 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ
,
903 &input_userauth_passwd_changereq
);
908 pw_encrypt(const char *password
, const char *crypt_scheme
, const char *salt
)
910 /* OpenBSD crypt(3) handles all of these */
911 if (strcmp(crypt_scheme
, "crypt") == 0 ||
912 strcmp(crypt_scheme
, "bcrypt") == 0 ||
913 strcmp(crypt_scheme
, "md5crypt") == 0 ||
914 strcmp(crypt_scheme
, "crypt-extended") == 0)
915 return xstrdup(crypt(password
, salt
));
916 error("%s: unsupported password encryption scheme \"%.100s\"",
917 __func__
, crypt_scheme
);
922 jpake_password_to_secret(Authctxt
*authctxt
, const char *crypt_scheme
,
925 char prompt
[256], *password
, *crypted
;
930 snprintf(prompt
, sizeof(prompt
), "%.30s@%.128s's password (JPAKE): ",
931 authctxt
->server_user
, authctxt
->host
);
932 password
= read_passphrase(prompt
, 0);
934 if ((crypted
= pw_encrypt(password
, crypt_scheme
, salt
)) == NULL
) {
935 logit("Disabling %s authentication", authctxt
->method
->name
);
936 authctxt
->method
->enabled
= NULL
;
937 /* Continue with an empty password to fail gracefully */
938 crypted
= xstrdup("");
942 debug3("%s: salt = %s", __func__
, salt
);
943 debug3("%s: scheme = %s", __func__
, crypt_scheme
);
944 debug3("%s: crypted = %s", __func__
, crypted
);
947 if (hash_buffer(crypted
, strlen(crypted
), EVP_sha256(),
948 &secret
, &secret_len
) != 0)
949 fatal("%s: hash_buffer", __func__
);
951 bzero(password
, strlen(password
));
952 bzero(crypted
, strlen(crypted
));
956 if ((ret
= BN_bin2bn(secret
, secret_len
, NULL
)) == NULL
)
957 fatal("%s: BN_bin2bn (secret)", __func__
);
958 bzero(secret
, secret_len
);
966 input_userauth_jpake_server_step1(int type
, u_int32_t seq
, void *ctxt
)
968 Authctxt
*authctxt
= ctxt
;
969 struct jpake_ctx
*pctx
= authctxt
->methoddata
;
970 u_char
*x3_proof
, *x4_proof
, *x2_s_proof
;
971 u_int x3_proof_len
, x4_proof_len
, x2_s_proof_len
;
972 char *crypt_scheme
, *salt
;
974 /* Disable this message */
975 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1
, NULL
);
977 if ((pctx
->g_x3
= BN_new()) == NULL
||
978 (pctx
->g_x4
= BN_new()) == NULL
)
979 fatal("%s: BN_new", __func__
);
981 /* Fetch step 1 values */
982 crypt_scheme
= packet_get_string(NULL
);
983 salt
= packet_get_string(NULL
);
984 pctx
->server_id
= packet_get_string(&pctx
->server_id_len
);
985 packet_get_bignum2(pctx
->g_x3
);
986 packet_get_bignum2(pctx
->g_x4
);
987 x3_proof
= packet_get_string(&x3_proof_len
);
988 x4_proof
= packet_get_string(&x4_proof_len
);
991 JPAKE_DEBUG_CTX((pctx
, "step 1 received in %s", __func__
));
993 /* Obtain password and derive secret */
994 pctx
->s
= jpake_password_to_secret(authctxt
, crypt_scheme
, salt
);
995 bzero(crypt_scheme
, strlen(crypt_scheme
));
996 bzero(salt
, strlen(salt
));
999 JPAKE_DEBUG_BN((pctx
->s
, "%s: s = ", __func__
));
1001 /* Calculate step 2 values */
1002 jpake_step2(pctx
->grp
, pctx
->s
, pctx
->g_x1
,
1003 pctx
->g_x3
, pctx
->g_x4
, pctx
->x2
,
1004 pctx
->server_id
, pctx
->server_id_len
,
1005 pctx
->client_id
, pctx
->client_id_len
,
1006 x3_proof
, x3_proof_len
,
1007 x4_proof
, x4_proof_len
,
1009 &x2_s_proof
, &x2_s_proof_len
);
1011 bzero(x3_proof
, x3_proof_len
);
1012 bzero(x4_proof
, x4_proof_len
);
1016 JPAKE_DEBUG_CTX((pctx
, "step 2 sending in %s", __func__
));
1018 /* Send values for step 2 */
1019 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP2
);
1020 packet_put_bignum2(pctx
->a
);
1021 packet_put_string(x2_s_proof
, x2_s_proof_len
);
1024 bzero(x2_s_proof
, x2_s_proof_len
);
1027 /* Expect step 2 packet from peer */
1028 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2
,
1029 input_userauth_jpake_server_step2
);
1034 input_userauth_jpake_server_step2(int type
, u_int32_t seq
, void *ctxt
)
1036 Authctxt
*authctxt
= ctxt
;
1037 struct jpake_ctx
*pctx
= authctxt
->methoddata
;
1039 u_int x4_s_proof_len
;
1041 /* Disable this message */
1042 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2
, NULL
);
1044 if ((pctx
->b
= BN_new()) == NULL
)
1045 fatal("%s: BN_new", __func__
);
1047 /* Fetch step 2 values */
1048 packet_get_bignum2(pctx
->b
);
1049 x4_s_proof
= packet_get_string(&x4_s_proof_len
);
1052 JPAKE_DEBUG_CTX((pctx
, "step 2 received in %s", __func__
));
1054 /* Derive shared key and calculate confirmation hash */
1055 jpake_key_confirm(pctx
->grp
, pctx
->s
, pctx
->b
,
1056 pctx
->x2
, pctx
->g_x1
, pctx
->g_x2
, pctx
->g_x3
, pctx
->g_x4
,
1057 pctx
->client_id
, pctx
->client_id_len
,
1058 pctx
->server_id
, pctx
->server_id_len
,
1059 session_id2
, session_id2_len
,
1060 x4_s_proof
, x4_s_proof_len
,
1062 &pctx
->h_k_cid_sessid
, &pctx
->h_k_cid_sessid_len
);
1064 bzero(x4_s_proof
, x4_s_proof_len
);
1067 JPAKE_DEBUG_CTX((pctx
, "confirm sending in %s", __func__
));
1069 /* Send key confirmation proof */
1070 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_CONFIRM
);
1071 packet_put_string(pctx
->h_k_cid_sessid
, pctx
->h_k_cid_sessid_len
);
1074 /* Expect confirmation from peer */
1075 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM
,
1076 input_userauth_jpake_server_confirm
);
1081 input_userauth_jpake_server_confirm(int type
, u_int32_t seq
, void *ctxt
)
1083 Authctxt
*authctxt
= ctxt
;
1084 struct jpake_ctx
*pctx
= authctxt
->methoddata
;
1086 /* Disable this message */
1087 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM
, NULL
);
1089 pctx
->h_k_sid_sessid
= packet_get_string(&pctx
->h_k_sid_sessid_len
);
1092 JPAKE_DEBUG_CTX((pctx
, "confirm received in %s", __func__
));
1094 /* Verify expected confirmation hash */
1095 if (jpake_check_confirm(pctx
->k
,
1096 pctx
->server_id
, pctx
->server_id_len
,
1097 session_id2
, session_id2_len
,
1098 pctx
->h_k_sid_sessid
, pctx
->h_k_sid_sessid_len
) == 1)
1099 debug("%s: %s success", __func__
, authctxt
->method
->name
);
1101 debug("%s: confirmation mismatch", __func__
);
1102 /* XXX stash this so if auth succeeds then we can warn/kill */
1105 userauth_jpake_cleanup(authctxt
);
1110 identity_sign(Identity
*id
, u_char
**sigp
, u_int
*lenp
,
1111 u_char
*data
, u_int datalen
)
1116 /* the agent supports this key */
1118 return (ssh_agent_sign(id
->ac
, id
->key
, sigp
, lenp
,
1121 * we have already loaded the private key or
1122 * the private key is stored in external hardware
1124 if (id
->isprivate
|| (id
->key
->flags
& KEY_FLAG_EXT
))
1125 return (key_sign(id
->key
, sigp
, lenp
, data
, datalen
));
1126 /* load the private key from the file */
1127 if ((prv
= load_identity_file(id
->filename
)) == NULL
)
1129 ret
= key_sign(prv
, sigp
, lenp
, data
, datalen
);
1135 sign_and_send_pubkey(Authctxt
*authctxt
, Identity
*id
)
1138 u_char
*blob
, *signature
;
1139 u_int bloblen
, slen
;
1144 debug3("sign_and_send_pubkey");
1146 if (key_to_blob(id
->key
, &blob
, &bloblen
) == 0) {
1147 /* we cannot handle this key */
1148 debug3("sign_and_send_pubkey: cannot handle key");
1151 /* data to be signed */
1153 if (datafellows
& SSH_OLD_SESSIONID
) {
1154 buffer_append(&b
, session_id2
, session_id2_len
);
1155 skip
= session_id2_len
;
1157 buffer_put_string(&b
, session_id2
, session_id2_len
);
1158 skip
= buffer_len(&b
);
1160 buffer_put_char(&b
, SSH2_MSG_USERAUTH_REQUEST
);
1161 buffer_put_cstring(&b
, authctxt
->server_user
);
1162 buffer_put_cstring(&b
,
1163 datafellows
& SSH_BUG_PKSERVICE
?
1166 if (datafellows
& SSH_BUG_PKAUTH
) {
1167 buffer_put_char(&b
, have_sig
);
1169 buffer_put_cstring(&b
, authctxt
->method
->name
);
1170 buffer_put_char(&b
, have_sig
);
1171 buffer_put_cstring(&b
, key_ssh_name(id
->key
));
1173 buffer_put_string(&b
, blob
, bloblen
);
1175 /* generate signature */
1176 ret
= identity_sign(id
, &signature
, &slen
,
1177 buffer_ptr(&b
), buffer_len(&b
));
1186 if (datafellows
& SSH_BUG_PKSERVICE
) {
1188 buffer_append(&b
, session_id2
, session_id2_len
);
1189 skip
= session_id2_len
;
1190 buffer_put_char(&b
, SSH2_MSG_USERAUTH_REQUEST
);
1191 buffer_put_cstring(&b
, authctxt
->server_user
);
1192 buffer_put_cstring(&b
, authctxt
->service
);
1193 buffer_put_cstring(&b
, authctxt
->method
->name
);
1194 buffer_put_char(&b
, have_sig
);
1195 if (!(datafellows
& SSH_BUG_PKAUTH
))
1196 buffer_put_cstring(&b
, key_ssh_name(id
->key
));
1197 buffer_put_string(&b
, blob
, bloblen
);
1201 /* append signature */
1202 buffer_put_string(&b
, signature
, slen
);
1205 /* skip session id and packet type */
1206 if (buffer_len(&b
) < skip
+ 1)
1207 fatal("userauth_pubkey: internal error");
1208 buffer_consume(&b
, skip
+ 1);
1210 /* put remaining data from buffer into packet */
1211 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
1212 packet_put_raw(buffer_ptr(&b
), buffer_len(&b
));
1220 send_pubkey_test(Authctxt
*authctxt
, Identity
*id
)
1223 u_int bloblen
, have_sig
= 0;
1225 debug3("send_pubkey_test");
1227 if (key_to_blob(id
->key
, &blob
, &bloblen
) == 0) {
1228 /* we cannot handle this key */
1229 debug3("send_pubkey_test: cannot handle key");
1232 /* register callback for USERAUTH_PK_OK message */
1233 dispatch_set(SSH2_MSG_USERAUTH_PK_OK
, &input_userauth_pk_ok
);
1235 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
1236 packet_put_cstring(authctxt
->server_user
);
1237 packet_put_cstring(authctxt
->service
);
1238 packet_put_cstring(authctxt
->method
->name
);
1239 packet_put_char(have_sig
);
1240 if (!(datafellows
& SSH_BUG_PKAUTH
))
1241 packet_put_cstring(key_ssh_name(id
->key
));
1242 packet_put_string(blob
, bloblen
);
1249 load_identity_file(char *filename
)
1252 char prompt
[300], *passphrase
;
1253 int perm_ok
= 0, quit
, i
;
1256 if (stat(filename
, &st
) < 0) {
1257 debug3("no such identity: %s", filename
);
1260 private = key_load_private_type(KEY_UNSPEC
, filename
, "", NULL
, &perm_ok
);
1263 if (private == NULL
) {
1264 if (options
.batch_mode
)
1266 snprintf(prompt
, sizeof prompt
,
1267 "Enter passphrase for key '%.100s': ", filename
);
1268 for (i
= 0; i
< options
.number_of_password_prompts
; i
++) {
1269 passphrase
= read_passphrase(prompt
, 0);
1270 if (strcmp(passphrase
, "") != 0) {
1271 private = key_load_private_type(KEY_UNSPEC
,
1272 filename
, passphrase
, NULL
, NULL
);
1275 debug2("no passphrase given, try next key");
1278 memset(passphrase
, 0, strlen(passphrase
));
1280 if (private != NULL
|| quit
)
1282 debug2("bad passphrase given, try again...");
1289 * try keys in the following order:
1290 * 1. agent keys that are found in the config file
1291 * 2. other agent keys
1292 * 3. keys that are only listed in the config file
1295 pubkey_prepare(Authctxt
*authctxt
)
1298 Idlist agent
, files
, *preferred
;
1300 AuthenticationConnection
*ac
;
1304 TAILQ_INIT(&agent
); /* keys from the agent */
1305 TAILQ_INIT(&files
); /* keys from the config file */
1306 preferred
= &authctxt
->keys
;
1307 TAILQ_INIT(preferred
); /* preferred order of keys */
1309 /* list of keys stored in the filesystem */
1310 for (i
= 0; i
< options
.num_identity_files
; i
++) {
1311 key
= options
.identity_keys
[i
];
1312 if (key
&& key
->type
== KEY_RSA1
)
1314 if (key
&& key
->cert
&& key
->cert
->type
!= SSH2_CERT_TYPE_USER
)
1316 options
.identity_keys
[i
] = NULL
;
1317 id
= xcalloc(1, sizeof(*id
));
1319 id
->filename
= xstrdup(options
.identity_files
[i
]);
1320 TAILQ_INSERT_TAIL(&files
, id
, next
);
1322 /* list of keys supported by the agent */
1323 if ((ac
= ssh_get_authentication_connection())) {
1324 for (key
= ssh_get_first_identity(ac
, &comment
, 2);
1326 key
= ssh_get_next_identity(ac
, &comment
, 2)) {
1328 TAILQ_FOREACH(id
, &files
, next
) {
1329 /* agent keys from the config file are preferred */
1330 if (key_equal(key
, id
->key
)) {
1333 TAILQ_REMOVE(&files
, id
, next
);
1334 TAILQ_INSERT_TAIL(preferred
, id
, next
);
1340 if (!found
&& !options
.identities_only
) {
1341 id
= xcalloc(1, sizeof(*id
));
1343 id
->filename
= comment
;
1345 TAILQ_INSERT_TAIL(&agent
, id
, next
);
1348 /* append remaining agent keys */
1349 for (id
= TAILQ_FIRST(&agent
); id
; id
= TAILQ_FIRST(&agent
)) {
1350 TAILQ_REMOVE(&agent
, id
, next
);
1351 TAILQ_INSERT_TAIL(preferred
, id
, next
);
1353 authctxt
->agent
= ac
;
1355 /* append remaining keys from the config file */
1356 for (id
= TAILQ_FIRST(&files
); id
; id
= TAILQ_FIRST(&files
)) {
1357 TAILQ_REMOVE(&files
, id
, next
);
1358 TAILQ_INSERT_TAIL(preferred
, id
, next
);
1360 TAILQ_FOREACH(id
, preferred
, next
) {
1361 debug2("key: %s (%p)", id
->filename
, id
->key
);
1366 pubkey_cleanup(Authctxt
*authctxt
)
1370 if (authctxt
->agent
!= NULL
)
1371 ssh_close_authentication_connection(authctxt
->agent
);
1372 for (id
= TAILQ_FIRST(&authctxt
->keys
); id
;
1373 id
= TAILQ_FIRST(&authctxt
->keys
)) {
1374 TAILQ_REMOVE(&authctxt
->keys
, id
, next
);
1378 xfree(id
->filename
);
1384 userauth_pubkey(Authctxt
*authctxt
)
1389 while ((id
= TAILQ_FIRST(&authctxt
->keys
))) {
1392 /* move key to the end of the queue */
1393 TAILQ_REMOVE(&authctxt
->keys
, id
, next
);
1394 TAILQ_INSERT_TAIL(&authctxt
->keys
, id
, next
);
1396 * send a test message if we have the public key. for
1397 * encrypted keys we cannot do this and have to load the
1398 * private key instead
1400 if (id
->key
&& id
->key
->type
!= KEY_RSA1
) {
1401 debug("Offering public key: %s", id
->filename
);
1402 sent
= send_pubkey_test(authctxt
, id
);
1403 } else if (id
->key
== NULL
) {
1404 debug("Trying private key: %s", id
->filename
);
1405 id
->key
= load_identity_file(id
->filename
);
1406 if (id
->key
!= NULL
) {
1408 sent
= sign_and_send_pubkey(authctxt
, id
);
1420 * Send userauth request message specifying keyboard-interactive method.
1423 userauth_kbdint(Authctxt
*authctxt
)
1425 static int attempt
= 0;
1427 if (attempt
++ >= options
.number_of_password_prompts
)
1429 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1430 if (attempt
> 1 && !authctxt
->info_req_seen
) {
1431 debug3("userauth_kbdint: disable: no info_req_seen");
1432 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST
, NULL
);
1436 debug2("userauth_kbdint");
1437 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
1438 packet_put_cstring(authctxt
->server_user
);
1439 packet_put_cstring(authctxt
->service
);
1440 packet_put_cstring(authctxt
->method
->name
);
1441 packet_put_cstring(""); /* lang */
1442 packet_put_cstring(options
.kbd_interactive_devices
?
1443 options
.kbd_interactive_devices
: "");
1446 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST
, &input_userauth_info_req
);
1451 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1454 input_userauth_info_req(int type
, u_int32_t seq
, void *ctxt
)
1456 Authctxt
*authctxt
= ctxt
;
1457 char *name
, *inst
, *lang
, *prompt
, *response
;
1458 u_int num_prompts
, i
;
1461 debug2("input_userauth_info_req");
1463 if (authctxt
== NULL
)
1464 fatal("input_userauth_info_req: no authentication context");
1466 authctxt
->info_req_seen
= 1;
1468 name
= packet_get_string(NULL
);
1469 inst
= packet_get_string(NULL
);
1470 lang
= packet_get_string(NULL
);
1471 if (strlen(name
) > 0)
1473 if (strlen(inst
) > 0)
1479 num_prompts
= packet_get_int();
1481 * Begin to build info response packet based on prompts requested.
1482 * We commit to providing the correct number of responses, so if
1483 * further on we run into a problem that prevents this, we have to
1484 * be sure and clean this up and send a correct error response.
1486 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE
);
1487 packet_put_int(num_prompts
);
1489 debug2("input_userauth_info_req: num_prompts %d", num_prompts
);
1490 for (i
= 0; i
< num_prompts
; i
++) {
1491 prompt
= packet_get_string(NULL
);
1492 echo
= packet_get_char();
1494 response
= read_passphrase(prompt
, echo
? RP_ECHO
: 0);
1496 packet_put_cstring(response
);
1497 memset(response
, 0, strlen(response
));
1501 packet_check_eom(); /* done with parsing incoming message. */
1503 packet_add_padding(64);
1508 ssh_keysign(Key
*key
, u_char
**sigp
, u_int
*lenp
,
1509 u_char
*data
, u_int datalen
)
1514 int to
[2], from
[2], status
, version
= 2;
1516 debug2("ssh_keysign called");
1518 if (stat(_PATH_SSH_KEY_SIGN
, &st
) < 0) {
1519 error("ssh_keysign: not installed: %s", strerror(errno
));
1522 if (fflush(stdout
) != 0)
1523 error("ssh_keysign: fflush: %s", strerror(errno
));
1525 error("ssh_keysign: pipe: %s", strerror(errno
));
1528 if (pipe(from
) < 0) {
1529 error("ssh_keysign: pipe: %s", strerror(errno
));
1532 if ((pid
= fork()) < 0) {
1533 error("ssh_keysign: fork: %s", strerror(errno
));
1537 /* keep the socket on exec */
1538 fcntl(packet_get_connection_in(), F_SETFD
, 0);
1539 permanently_drop_suid(getuid());
1541 if (dup2(from
[1], STDOUT_FILENO
) < 0)
1542 fatal("ssh_keysign: dup2: %s", strerror(errno
));
1544 if (dup2(to
[0], STDIN_FILENO
) < 0)
1545 fatal("ssh_keysign: dup2: %s", strerror(errno
));
1548 execl(_PATH_SSH_KEY_SIGN
, _PATH_SSH_KEY_SIGN
, (char *) 0);
1549 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN
,
1556 buffer_put_int(&b
, packet_get_connection_in()); /* send # of socket */
1557 buffer_put_string(&b
, data
, datalen
);
1558 if (ssh_msg_send(to
[1], version
, &b
) == -1)
1559 fatal("ssh_keysign: couldn't send request");
1561 if (ssh_msg_recv(from
[0], &b
) < 0) {
1562 error("ssh_keysign: no reply");
1569 while (waitpid(pid
, &status
, 0) < 0)
1573 if (buffer_get_char(&b
) != version
) {
1574 error("ssh_keysign: bad version");
1578 *sigp
= buffer_get_string(&b
, lenp
);
1585 userauth_hostbased(Authctxt
*authctxt
)
1587 Key
*private = NULL
;
1588 Sensitive
*sensitive
= authctxt
->sensitive
;
1590 u_char
*signature
, *blob
;
1591 char *chost
, *pkalg
, *p
;
1592 const char *service
;
1594 int ok
, i
, found
= 0;
1596 /* check for a useful key */
1597 for (i
= 0; i
< sensitive
->nkeys
; i
++) {
1598 private = sensitive
->keys
[i
];
1599 if (private && private->type
!= KEY_RSA1
) {
1601 /* we take and free the key */
1602 sensitive
->keys
[i
] = NULL
;
1607 debug("No more client hostkeys for hostbased authentication.");
1610 if (key_to_blob(private, &blob
, &blen
) == 0) {
1614 /* figure out a name for the client host */
1615 p
= get_local_name(packet_get_connection_in());
1617 error("userauth_hostbased: cannot get local ipaddr/name");
1622 xasprintf(&chost
, "%s.", p
);
1623 debug2("userauth_hostbased: chost %s", chost
);
1626 service
= datafellows
& SSH_BUG_HBSERVICE
? "ssh-userauth" :
1628 pkalg
= xstrdup(key_ssh_name(private));
1630 /* construct data */
1631 buffer_put_string(&b
, session_id2
, session_id2_len
);
1632 buffer_put_char(&b
, SSH2_MSG_USERAUTH_REQUEST
);
1633 buffer_put_cstring(&b
, authctxt
->server_user
);
1634 buffer_put_cstring(&b
, service
);
1635 buffer_put_cstring(&b
, authctxt
->method
->name
);
1636 buffer_put_cstring(&b
, pkalg
);
1637 buffer_put_string(&b
, blob
, blen
);
1638 buffer_put_cstring(&b
, chost
);
1639 buffer_put_cstring(&b
, authctxt
->local_user
);
1643 if (sensitive
->external_keysign
)
1644 ok
= ssh_keysign(private, &signature
, &slen
,
1645 buffer_ptr(&b
), buffer_len(&b
));
1647 ok
= key_sign(private, &signature
, &slen
,
1648 buffer_ptr(&b
), buffer_len(&b
));
1652 error("key_sign failed");
1658 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
1659 packet_put_cstring(authctxt
->server_user
);
1660 packet_put_cstring(authctxt
->service
);
1661 packet_put_cstring(authctxt
->method
->name
);
1662 packet_put_cstring(pkalg
);
1663 packet_put_string(blob
, blen
);
1664 packet_put_cstring(chost
);
1665 packet_put_cstring(authctxt
->local_user
);
1666 packet_put_string(signature
, slen
);
1667 memset(signature
, 's', slen
);
1679 userauth_jpake(Authctxt
*authctxt
)
1681 struct jpake_ctx
*pctx
;
1682 u_char
*x1_proof
, *x2_proof
;
1683 u_int x1_proof_len
, x2_proof_len
;
1684 static int attempt
= 0; /* XXX share with userauth_password's? */
1686 if (attempt
++ >= options
.number_of_password_prompts
)
1689 error("Permission denied, please try again.");
1691 if (authctxt
->methoddata
!= NULL
)
1692 fatal("%s: authctxt->methoddata already set (%p)",
1693 __func__
, authctxt
->methoddata
);
1695 authctxt
->methoddata
= pctx
= jpake_new();
1698 * Send request immediately, to get the protocol going while
1699 * we do the initial computations.
1701 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
1702 packet_put_cstring(authctxt
->server_user
);
1703 packet_put_cstring(authctxt
->service
);
1704 packet_put_cstring(authctxt
->method
->name
);
1706 packet_write_wait();
1708 jpake_step1(pctx
->grp
,
1709 &pctx
->client_id
, &pctx
->client_id_len
,
1710 &pctx
->x1
, &pctx
->x2
, &pctx
->g_x1
, &pctx
->g_x2
,
1711 &x1_proof
, &x1_proof_len
,
1712 &x2_proof
, &x2_proof_len
);
1714 JPAKE_DEBUG_CTX((pctx
, "step 1 sending in %s", __func__
));
1716 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP1
);
1717 packet_put_string(pctx
->client_id
, pctx
->client_id_len
);
1718 packet_put_bignum2(pctx
->g_x1
);
1719 packet_put_bignum2(pctx
->g_x2
);
1720 packet_put_string(x1_proof
, x1_proof_len
);
1721 packet_put_string(x2_proof
, x2_proof_len
);
1724 bzero(x1_proof
, x1_proof_len
);
1725 bzero(x2_proof
, x2_proof_len
);
1729 /* Expect step 1 packet from peer */
1730 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1
,
1731 input_userauth_jpake_server_step1
);
1732 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS
,
1733 &input_userauth_success_unexpected
);
1739 userauth_jpake_cleanup(Authctxt
*authctxt
)
1741 debug3("%s: clean up", __func__
);
1742 if (authctxt
->methoddata
!= NULL
) {
1743 jpake_free(authctxt
->methoddata
);
1744 authctxt
->methoddata
= NULL
;
1746 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS
, &input_userauth_success
);
1750 /* find auth method */
1753 * given auth method name, if configurable options permit this method fill
1754 * in auth_ident field and return true, otherwise return false.
1757 authmethod_is_enabled(Authmethod
*method
)
1761 /* return false if options indicate this method is disabled */
1762 if (method
->enabled
== NULL
|| *method
->enabled
== 0)
1764 /* return false if batch mode is enabled but method needs interactive mode */
1765 if (method
->batch_flag
!= NULL
&& *method
->batch_flag
!= 0)
1771 authmethod_lookup(const char *name
)
1773 Authmethod
*method
= NULL
;
1775 for (method
= authmethods
; method
->name
!= NULL
; method
++)
1776 if (strcmp(name
, method
->name
) == 0)
1778 debug2("Unrecognized authentication method name: %s", name
? name
: "NULL");
1782 /* XXX internal state */
1783 static Authmethod
*current
= NULL
;
1784 static char *supported
= NULL
;
1785 static char *preferred
= NULL
;
1788 * Given the authentication method list sent by the server, return the
1789 * next method we should try. If the server initially sends a nil list,
1790 * use a built-in default list.
1793 authmethod_get(char *authlist
)
1798 /* Use a suitable default if we're passed a nil list. */
1799 if (authlist
== NULL
|| strlen(authlist
) == 0)
1800 authlist
= options
.preferred_authentications
;
1802 if (supported
== NULL
|| strcmp(authlist
, supported
) != 0) {
1803 debug3("start over, passed a different list %s", authlist
);
1804 if (supported
!= NULL
)
1806 supported
= xstrdup(authlist
);
1807 preferred
= options
.preferred_authentications
;
1808 debug3("preferred %s", preferred
);
1810 } else if (current
!= NULL
&& authmethod_is_enabled(current
))
1814 if ((name
= match_list(preferred
, supported
, &next
)) == NULL
) {
1815 debug("No more authentication methods to try.");
1820 debug3("authmethod_lookup %s", name
);
1821 debug3("remaining preferred: %s", preferred
);
1822 if ((current
= authmethod_lookup(name
)) != NULL
&&
1823 authmethod_is_enabled(current
)) {
1824 debug3("authmethod_is_enabled %s", name
);
1825 debug("Next authentication method: %s", name
);
1832 authmethods_get(void)
1834 Authmethod
*method
= NULL
;
1839 for (method
= authmethods
; method
->name
!= NULL
; method
++) {
1840 if (authmethod_is_enabled(method
)) {
1841 if (buffer_len(&b
) > 0)
1842 buffer_append(&b
, ",", 1);
1843 buffer_append(&b
, method
->name
, strlen(method
->name
));
1846 buffer_append(&b
, "\0", 1);
1847 list
= xstrdup(buffer_ptr(&b
));