1 /* $OpenBSD: sshconnect2.c,v 1.210 2014/07/15 15:54:14 millert 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) && !defined(BROKEN_STRNVIS)
47 #include "openbsd-compat/sys-queue.h"
58 #include "myproposal.h"
59 #include "sshconnect.h"
70 #include "pathnames.h"
79 extern char *client_version_string
;
80 extern char *server_version_string
;
81 extern Options options
;
84 /* tty_flag is set in ssh.c. use this in ssh_userauth2 */
85 /* if it is set then prevent the switch to the null cipher */
93 u_char
*session_id2
= NULL
;
94 u_int session_id2_len
= 0;
97 struct sockaddr
*xxx_hostaddr
;
102 verify_host_key_callback(Key
*hostkey
)
104 if (verify_host_key(xxx_host
, xxx_hostaddr
, hostkey
) == -1)
105 fatal("Host key verification failed.");
110 order_hostkeyalgs(char *host
, struct sockaddr
*hostaddr
, u_short port
)
112 char *oavail
, *avail
, *first
, *last
, *alg
, *hostname
, *ret
;
114 struct hostkeys
*hostkeys
;
118 /* Find all hostkeys for this hostname */
119 get_hostfile_hostname_ipaddr(host
, hostaddr
, port
, &hostname
, NULL
);
120 hostkeys
= init_hostkeys();
121 for (i
= 0; i
< options
.num_user_hostfiles
; i
++)
122 load_hostkeys(hostkeys
, hostname
, options
.user_hostfiles
[i
]);
123 for (i
= 0; i
< options
.num_system_hostfiles
; i
++)
124 load_hostkeys(hostkeys
, hostname
, options
.system_hostfiles
[i
]);
126 oavail
= avail
= xstrdup(KEX_DEFAULT_PK_ALG
);
127 maxlen
= strlen(avail
) + 1;
128 first
= xmalloc(maxlen
);
129 last
= xmalloc(maxlen
);
130 *first
= *last
= '\0';
132 #define ALG_APPEND(to, from) \
135 strlcat(to, ",", maxlen); \
136 strlcat(to, from, maxlen); \
139 while ((alg
= strsep(&avail
, ",")) && *alg
!= '\0') {
140 if ((ktype
= key_type_from_name(alg
)) == KEY_UNSPEC
)
141 fatal("%s: unknown alg %s", __func__
, alg
);
142 if (lookup_key_in_hostkeys_by_type(hostkeys
,
143 key_type_plain(ktype
), NULL
))
144 ALG_APPEND(first
, alg
);
146 ALG_APPEND(last
, alg
);
149 xasprintf(&ret
, "%s%s%s", first
, *first
== '\0' ? "" : ",", last
);
151 debug3("%s: prefer hostkeyalgs: %s", __func__
, first
);
157 free_hostkeys(hostkeys
);
162 static char *myproposal
[PROPOSAL_MAX
];
163 static const char *myproposal_default
[PROPOSAL_MAX
] = { KEX_CLIENT
};
166 ssh_kex2(char *host
, struct sockaddr
*hostaddr
, u_short port
)
170 memcpy(&myproposal
, &myproposal_default
, sizeof(myproposal
));
173 xxx_hostaddr
= hostaddr
;
175 if (options
.ciphers
== (char *)-1) {
176 logit("No valid ciphers for protocol version 2 given, using defaults.");
177 options
.ciphers
= NULL
;
179 if (options
.ciphers
!= NULL
) {
180 myproposal
[PROPOSAL_ENC_ALGS_CTOS
] =
181 myproposal
[PROPOSAL_ENC_ALGS_STOC
] = options
.ciphers
;
183 myproposal
[PROPOSAL_ENC_ALGS_CTOS
] =
184 compat_cipher_proposal(myproposal
[PROPOSAL_ENC_ALGS_CTOS
]);
185 myproposal
[PROPOSAL_ENC_ALGS_STOC
] =
186 compat_cipher_proposal(myproposal
[PROPOSAL_ENC_ALGS_STOC
]);
187 if (options
.compression
) {
188 myproposal
[PROPOSAL_COMP_ALGS_CTOS
] =
189 myproposal
[PROPOSAL_COMP_ALGS_STOC
] = "zlib@openssh.com,zlib,none";
191 myproposal
[PROPOSAL_COMP_ALGS_CTOS
] =
192 myproposal
[PROPOSAL_COMP_ALGS_STOC
] = "none,zlib@openssh.com,zlib";
194 if (options
.macs
!= NULL
) {
195 myproposal
[PROPOSAL_MAC_ALGS_CTOS
] =
196 myproposal
[PROPOSAL_MAC_ALGS_STOC
] = options
.macs
;
198 if (options
.hostkeyalgorithms
!= NULL
)
199 myproposal
[PROPOSAL_SERVER_HOST_KEY_ALGS
] =
200 compat_pkalg_proposal(options
.hostkeyalgorithms
);
202 /* Prefer algorithms that we already have keys for */
203 myproposal
[PROPOSAL_SERVER_HOST_KEY_ALGS
] =
204 compat_pkalg_proposal(
205 order_hostkeyalgs(host
, hostaddr
, port
));
207 if (options
.kex_algorithms
!= NULL
)
208 myproposal
[PROPOSAL_KEX_ALGS
] = options
.kex_algorithms
;
209 myproposal
[PROPOSAL_KEX_ALGS
] = compat_kex_proposal(
210 myproposal
[PROPOSAL_KEX_ALGS
]);
212 if (options
.rekey_limit
|| options
.rekey_interval
)
213 packet_set_rekey_limits((u_int32_t
)options
.rekey_limit
,
214 (time_t)options
.rekey_interval
);
216 /* start key exchange */
217 kex
= kex_setup(myproposal
);
219 kex
->kex
[KEX_DH_GRP1_SHA1
] = kexdh_client
;
220 kex
->kex
[KEX_DH_GRP14_SHA1
] = kexdh_client
;
221 kex
->kex
[KEX_DH_GEX_SHA1
] = kexgex_client
;
222 kex
->kex
[KEX_DH_GEX_SHA256
] = kexgex_client
;
223 kex
->kex
[KEX_ECDH_SHA2
] = kexecdh_client
;
225 kex
->kex
[KEX_C25519_SHA256
] = kexc25519_client
;
226 kex
->client_version_string
=client_version_string
;
227 kex
->server_version_string
=server_version_string
;
228 kex
->verify_host_key
=&verify_host_key_callback
;
232 dispatch_run(DISPATCH_BLOCK
, &kex
->done
, kex
);
234 if (options
.use_roaming
&& !kex
->roaming
) {
235 debug("Roaming not allowed by server");
236 options
.use_roaming
= 0;
239 session_id2
= kex
->session_id
;
240 session_id2_len
= kex
->session_id_len
;
243 /* send 1st encrypted/maced/compressed message */
244 packet_start(SSH2_MSG_IGNORE
);
245 packet_put_cstring("markus");
255 typedef struct Authctxt Authctxt
;
256 typedef struct Authmethod Authmethod
;
257 typedef struct identity Identity
;
258 typedef struct idlist Idlist
;
261 TAILQ_ENTRY(identity
) next
;
262 AuthenticationConnection
*ac
; /* set if agent supports key */
263 Key
*key
; /* public/private key */
264 char *filename
; /* comment for agent-only keys */
266 int isprivate
; /* key points to the private key */
269 TAILQ_HEAD(idlist
, identity
);
272 const char *server_user
;
273 const char *local_user
;
277 sig_atomic_t success
;
281 AuthenticationConnection
*agent
;
283 Sensitive
*sensitive
;
284 /* kbd-interactive */
290 char *name
; /* string to compare against server's list */
291 int (*userauth
)(Authctxt
*authctxt
);
292 void (*cleanup
)(Authctxt
*authctxt
);
293 int *enabled
; /* flag in option struct that enables method */
294 int *batch_flag
; /* flag in option struct that disables method */
297 void input_userauth_success(int, u_int32_t
, void *);
298 void input_userauth_success_unexpected(int, u_int32_t
, void *);
299 void input_userauth_failure(int, u_int32_t
, void *);
300 void input_userauth_banner(int, u_int32_t
, void *);
301 void input_userauth_error(int, u_int32_t
, void *);
302 void input_userauth_info_req(int, u_int32_t
, void *);
303 void input_userauth_pk_ok(int, u_int32_t
, void *);
304 void input_userauth_passwd_changereq(int, u_int32_t
, void *);
306 int userauth_none(Authctxt
*);
307 int userauth_pubkey(Authctxt
*);
308 int userauth_passwd(Authctxt
*);
309 int userauth_kbdint(Authctxt
*);
310 int userauth_hostbased(Authctxt
*);
313 int userauth_gssapi(Authctxt
*authctxt
);
314 void input_gssapi_response(int type
, u_int32_t
, void *);
315 void input_gssapi_token(int type
, u_int32_t
, void *);
316 void input_gssapi_hash(int type
, u_int32_t
, void *);
317 void input_gssapi_error(int, u_int32_t
, void *);
318 void input_gssapi_errtok(int, u_int32_t
, void *);
321 void userauth(Authctxt
*, char *);
323 static int sign_and_send_pubkey(Authctxt
*, Identity
*);
324 static void pubkey_prepare(Authctxt
*);
325 static void pubkey_cleanup(Authctxt
*);
326 static Key
*load_identity_file(char *, int);
328 static Authmethod
*authmethod_get(char *authlist
);
329 static Authmethod
*authmethod_lookup(const char *name
);
330 static char *authmethods_get(void);
332 Authmethod authmethods
[] = {
337 &options
.gss_authentication
,
343 &options
.hostbased_authentication
,
348 &options
.pubkey_authentication
,
350 {"keyboard-interactive",
353 &options
.kbd_interactive_authentication
,
354 &options
.batch_mode
},
358 &options
.password_authentication
,
359 &options
.batch_mode
},
365 {NULL
, NULL
, NULL
, NULL
, NULL
}
369 ssh_userauth2(const char *local_user
, const char *server_user
, char *host
,
370 Sensitive
*sensitive
)
375 if (options
.challenge_response_authentication
)
376 options
.kbd_interactive_authentication
= 1;
378 packet_start(SSH2_MSG_SERVICE_REQUEST
);
379 packet_put_cstring("ssh-userauth");
381 debug("SSH2_MSG_SERVICE_REQUEST sent");
383 type
= packet_read();
384 if (type
!= SSH2_MSG_SERVICE_ACCEPT
)
385 fatal("Server denied authentication request: %d", type
);
386 if (packet_remaining() > 0) {
387 char *reply
= packet_get_string(NULL
);
388 debug2("service_accept: %s", reply
);
391 debug2("buggy server: service_accept w/o service");
394 debug("SSH2_MSG_SERVICE_ACCEPT received");
396 if (options
.preferred_authentications
== NULL
)
397 options
.preferred_authentications
= authmethods_get();
399 /* setup authentication context */
400 memset(&authctxt
, 0, sizeof(authctxt
));
401 pubkey_prepare(&authctxt
);
402 authctxt
.server_user
= server_user
;
403 authctxt
.local_user
= local_user
;
404 authctxt
.host
= host
;
405 authctxt
.service
= "ssh-connection"; /* service name */
406 authctxt
.success
= 0;
407 authctxt
.method
= authmethod_lookup("none");
408 authctxt
.authlist
= NULL
;
409 authctxt
.methoddata
= NULL
;
410 authctxt
.sensitive
= sensitive
;
411 authctxt
.info_req_seen
= 0;
412 if (authctxt
.method
== NULL
)
413 fatal("ssh_userauth2: internal error: cannot send userauth none request");
415 /* initial userauth request */
416 userauth_none(&authctxt
);
418 dispatch_init(&input_userauth_error
);
419 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS
, &input_userauth_success
);
420 dispatch_set(SSH2_MSG_USERAUTH_FAILURE
, &input_userauth_failure
);
421 dispatch_set(SSH2_MSG_USERAUTH_BANNER
, &input_userauth_banner
);
422 dispatch_run(DISPATCH_BLOCK
, &authctxt
.success
, &authctxt
); /* loop until success */
424 pubkey_cleanup(&authctxt
);
425 dispatch_range(SSH2_MSG_USERAUTH_MIN
, SSH2_MSG_USERAUTH_MAX
, NULL
);
427 /* if the user wants to use the none cipher do it */
428 /* post authentication and only if the right conditions are met */
429 /* both of the NONE commands must be true and there must be no */
431 if ((options
.none_switch
== 1) && (options
.none_enabled
== 1))
433 if (!tty_flag
) /* no null on tty sessions */
435 debug("Requesting none rekeying...");
436 myproposal
[PROPOSAL_ENC_ALGS_STOC
] = "none";
437 myproposal
[PROPOSAL_ENC_ALGS_CTOS
] = "none";
438 kex_prop2buf(&xxx_kex
->my
,myproposal
);
439 packet_request_rekeying();
440 fprintf(stderr
, "WARNING: ENABLED NONE CIPHER\n");
444 /* requested NONE cipher when in a tty */
445 debug("Cannot switch to NONE cipher with tty allocated");
446 fprintf(stderr
, "NONE cipher switch disabled when a TTY is allocated\n");
449 /* if we are using aes-ctr there can be issues in either a fork or sandbox
450 * so the initial aes-ctr is defined to point to the original single process
451 * evp. After authentication we'll be past the fork and the sandboxed privsep
452 * so we repoint the define to the multithreaded evp. To start the threads we
455 CipherContext
*ccsend
;
456 ccsend
= (CipherContext
*)packet_get_send_context();
458 /* only do this for the ctr cipher. otherwise gcm mode breaks. Don't know why though */
459 if (strstr(cipher_return_name((Cipher
*)ccsend
->cipher
), "ctr")) {
460 debug ("Single to Multithread CTR cipher swap - client request");
461 cipher_reset_multithreaded();
462 packet_request_rekeying();
465 debug("Authentication succeeded (%s).", authctxt
.method
->name
);
469 userauth(Authctxt
*authctxt
, char *authlist
)
471 if (authctxt
->method
!= NULL
&& authctxt
->method
->cleanup
!= NULL
)
472 authctxt
->method
->cleanup(authctxt
);
474 free(authctxt
->methoddata
);
475 authctxt
->methoddata
= NULL
;
476 if (authlist
== NULL
) {
477 authlist
= authctxt
->authlist
;
479 free(authctxt
->authlist
);
480 authctxt
->authlist
= authlist
;
483 Authmethod
*method
= authmethod_get(authlist
);
485 fatal("Permission denied (%s).", authlist
);
486 authctxt
->method
= method
;
488 /* reset the per method handler */
489 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN
,
490 SSH2_MSG_USERAUTH_PER_METHOD_MAX
, NULL
);
492 /* and try new method */
493 if (method
->userauth(authctxt
) != 0) {
494 debug2("we sent a %s packet, wait for reply", method
->name
);
497 debug2("we did not send a packet, disable method");
498 method
->enabled
= NULL
;
505 input_userauth_error(int type
, u_int32_t seq
, void *ctxt
)
507 fatal("input_userauth_error: bad message during authentication: "
513 input_userauth_banner(int type
, u_int32_t seq
, void *ctxt
)
515 char *msg
, *raw
, *lang
;
518 debug3("input_userauth_banner");
519 raw
= packet_get_string(&len
);
520 lang
= packet_get_string(NULL
);
521 if (len
> 0 && options
.log_level
>= SYSLOG_LEVEL_INFO
) {
524 msg
= xmalloc(len
* 4 + 1); /* max expansion from strnvis() */
525 strnvis(msg
, raw
, len
* 4 + 1, VIS_SAFE
|VIS_OCTAL
|VIS_NOSLASH
);
526 fprintf(stderr
, "%s", msg
);
535 input_userauth_success(int type
, u_int32_t seq
, void *ctxt
)
537 Authctxt
*authctxt
= ctxt
;
539 if (authctxt
== NULL
)
540 fatal("input_userauth_success: no authentication context");
541 free(authctxt
->authlist
);
542 authctxt
->authlist
= NULL
;
543 if (authctxt
->method
!= NULL
&& authctxt
->method
->cleanup
!= NULL
)
544 authctxt
->method
->cleanup(authctxt
);
545 free(authctxt
->methoddata
);
546 authctxt
->methoddata
= NULL
;
547 authctxt
->success
= 1; /* break out */
551 input_userauth_success_unexpected(int type
, u_int32_t seq
, void *ctxt
)
553 Authctxt
*authctxt
= ctxt
;
555 if (authctxt
== NULL
)
556 fatal("%s: no authentication context", __func__
);
558 fatal("Unexpected authentication success during %s.",
559 authctxt
->method
->name
);
564 input_userauth_failure(int type
, u_int32_t seq
, void *ctxt
)
566 Authctxt
*authctxt
= ctxt
;
567 char *authlist
= NULL
;
570 if (authctxt
== NULL
)
571 fatal("input_userauth_failure: no authentication context");
573 authlist
= packet_get_string(NULL
);
574 partial
= packet_get_char();
578 logit("Authenticated with partial success.");
580 pubkey_cleanup(authctxt
);
581 pubkey_prepare(authctxt
);
583 debug("Authentications that can continue: %s", authlist
);
585 userauth(authctxt
, authlist
);
590 input_userauth_pk_ok(int type
, u_int32_t seq
, void *ctxt
)
592 Authctxt
*authctxt
= ctxt
;
596 int pktype
, sent
= 0;
601 if (authctxt
== NULL
)
602 fatal("input_userauth_pk_ok: no authentication context");
603 if (datafellows
& SSH_BUG_PKOK
) {
604 /* this is similar to SSH_BUG_PKAUTH */
605 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
606 pkblob
= packet_get_string(&blen
);
608 buffer_append(&b
, pkblob
, blen
);
609 pkalg
= buffer_get_string(&b
, &alen
);
612 pkalg
= packet_get_string(&alen
);
613 pkblob
= packet_get_string(&blen
);
617 debug("Server accepts key: pkalg %s blen %u", pkalg
, blen
);
619 if ((pktype
= key_type_from_name(pkalg
)) == KEY_UNSPEC
) {
620 debug("unknown pkalg %s", pkalg
);
623 if ((key
= key_from_blob(pkblob
, blen
)) == NULL
) {
624 debug("no key from blob. pkalg %s", pkalg
);
627 if (key
->type
!= pktype
) {
628 error("input_userauth_pk_ok: type mismatch "
629 "for decoded key (received %d, expected %d)",
633 fp
= key_fingerprint(key
, SSH_FP_MD5
, SSH_FP_HEX
);
634 debug2("input_userauth_pk_ok: fp %s", fp
);
638 * search keys in the reverse order, because last candidate has been
639 * moved to the end of the queue. this also avoids confusion by
642 TAILQ_FOREACH_REVERSE(id
, &authctxt
->keys
, idlist
, next
) {
643 if (key_equal(key
, id
->key
)) {
644 sent
= sign_and_send_pubkey(authctxt
, id
);
654 /* try another method if we did not send a packet */
656 userauth(authctxt
, NULL
);
661 userauth_gssapi(Authctxt
*authctxt
)
663 Gssctxt
*gssctxt
= NULL
;
664 static gss_OID_set gss_supported
= NULL
;
665 static u_int mech
= 0;
669 /* Try one GSSAPI method at a time, rather than sending them all at
672 if (gss_supported
== NULL
)
673 gss_indicate_mechs(&min
, &gss_supported
);
675 /* Check to see if the mechanism is usable before we offer it */
676 while (mech
< gss_supported
->count
&& !ok
) {
677 /* My DER encoding requires length<128 */
678 if (gss_supported
->elements
[mech
].length
< 128 &&
679 ssh_gssapi_check_mechanism(&gssctxt
,
680 &gss_supported
->elements
[mech
], authctxt
->host
)) {
681 ok
= 1; /* Mechanism works */
690 authctxt
->methoddata
=(void *)gssctxt
;
692 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
693 packet_put_cstring(authctxt
->server_user
);
694 packet_put_cstring(authctxt
->service
);
695 packet_put_cstring(authctxt
->method
->name
);
699 packet_put_int((gss_supported
->elements
[mech
].length
) + 2);
700 packet_put_char(SSH_GSS_OIDTYPE
);
701 packet_put_char(gss_supported
->elements
[mech
].length
);
702 packet_put_raw(gss_supported
->elements
[mech
].elements
,
703 gss_supported
->elements
[mech
].length
);
707 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE
, &input_gssapi_response
);
708 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN
, &input_gssapi_token
);
709 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR
, &input_gssapi_error
);
710 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK
, &input_gssapi_errtok
);
712 mech
++; /* Move along to next candidate */
718 process_gssapi_token(void *ctxt
, gss_buffer_t recv_tok
)
720 Authctxt
*authctxt
= ctxt
;
721 Gssctxt
*gssctxt
= authctxt
->methoddata
;
722 gss_buffer_desc send_tok
= GSS_C_EMPTY_BUFFER
;
723 gss_buffer_desc mic
= GSS_C_EMPTY_BUFFER
;
724 gss_buffer_desc gssbuf
;
725 OM_uint32 status
, ms
, flags
;
728 status
= ssh_gssapi_init_ctx(gssctxt
, options
.gss_deleg_creds
,
729 recv_tok
, &send_tok
, &flags
);
731 if (send_tok
.length
> 0) {
732 if (GSS_ERROR(status
))
733 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK
);
735 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN
);
737 packet_put_string(send_tok
.value
, send_tok
.length
);
739 gss_release_buffer(&ms
, &send_tok
);
742 if (status
== GSS_S_COMPLETE
) {
743 /* send either complete or MIC, depending on mechanism */
744 if (!(flags
& GSS_C_INTEG_FLAG
)) {
745 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE
);
748 ssh_gssapi_buildmic(&b
, authctxt
->server_user
,
749 authctxt
->service
, "gssapi-with-mic");
751 gssbuf
.value
= buffer_ptr(&b
);
752 gssbuf
.length
= buffer_len(&b
);
754 status
= ssh_gssapi_sign(gssctxt
, &gssbuf
, &mic
);
756 if (!GSS_ERROR(status
)) {
757 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC
);
758 packet_put_string(mic
.value
, mic
.length
);
764 gss_release_buffer(&ms
, &mic
);
773 input_gssapi_response(int type
, u_int32_t plen
, void *ctxt
)
775 Authctxt
*authctxt
= ctxt
;
780 if (authctxt
== NULL
)
781 fatal("input_gssapi_response: no authentication context");
782 gssctxt
= authctxt
->methoddata
;
785 oidv
= packet_get_string(&oidlen
);
788 oidv
[0] != SSH_GSS_OIDTYPE
||
789 oidv
[1] != oidlen
- 2) {
791 debug("Badly encoded mechanism OID received");
792 userauth(authctxt
, NULL
);
796 if (!ssh_gssapi_check_oid(gssctxt
, oidv
+ 2, oidlen
- 2))
797 fatal("Server returned different OID than expected");
803 if (GSS_ERROR(process_gssapi_token(ctxt
, GSS_C_NO_BUFFER
))) {
804 /* Start again with next method on list */
805 debug("Trying to start again");
806 userauth(authctxt
, NULL
);
813 input_gssapi_token(int type
, u_int32_t plen
, void *ctxt
)
815 Authctxt
*authctxt
= ctxt
;
816 gss_buffer_desc recv_tok
;
820 if (authctxt
== NULL
)
821 fatal("input_gssapi_response: no authentication context");
823 recv_tok
.value
= packet_get_string(&slen
);
824 recv_tok
.length
= slen
; /* safe typecast */
828 status
= process_gssapi_token(ctxt
, &recv_tok
);
830 free(recv_tok
.value
);
832 if (GSS_ERROR(status
)) {
833 /* Start again with the next method in the list */
834 userauth(authctxt
, NULL
);
841 input_gssapi_errtok(int type
, u_int32_t plen
, void *ctxt
)
843 Authctxt
*authctxt
= ctxt
;
845 gss_buffer_desc send_tok
= GSS_C_EMPTY_BUFFER
;
846 gss_buffer_desc recv_tok
;
850 if (authctxt
== NULL
)
851 fatal("input_gssapi_response: no authentication context");
852 gssctxt
= authctxt
->methoddata
;
854 recv_tok
.value
= packet_get_string(&len
);
855 recv_tok
.length
= len
;
859 /* Stick it into GSSAPI and see what it says */
860 (void)ssh_gssapi_init_ctx(gssctxt
, options
.gss_deleg_creds
,
861 &recv_tok
, &send_tok
, NULL
);
863 free(recv_tok
.value
);
864 gss_release_buffer(&ms
, &send_tok
);
866 /* Server will be returning a failed packet after this one */
871 input_gssapi_error(int type
, u_int32_t plen
, void *ctxt
)
876 /* maj */(void)packet_get_int();
877 /* min */(void)packet_get_int();
878 msg
=packet_get_string(NULL
);
879 lang
=packet_get_string(NULL
);
883 debug("Server GSSAPI Error:\n%s", msg
);
890 userauth_none(Authctxt
*authctxt
)
892 /* initial userauth request */
893 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
894 packet_put_cstring(authctxt
->server_user
);
895 packet_put_cstring(authctxt
->service
);
896 packet_put_cstring(authctxt
->method
->name
);
902 userauth_passwd(Authctxt
*authctxt
)
904 static int attempt
= 0;
907 const char *host
= options
.host_key_alias
? options
.host_key_alias
:
910 if (attempt
++ >= options
.number_of_password_prompts
)
914 error("Permission denied, please try again.");
916 snprintf(prompt
, sizeof(prompt
), "%.30s@%.128s's password: ",
917 authctxt
->server_user
, host
);
918 password
= read_passphrase(prompt
, 0);
919 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
920 packet_put_cstring(authctxt
->server_user
);
921 packet_put_cstring(authctxt
->service
);
922 packet_put_cstring(authctxt
->method
->name
);
924 packet_put_cstring(password
);
925 explicit_bzero(password
, strlen(password
));
927 packet_add_padding(64);
930 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ
,
931 &input_userauth_passwd_changereq
);
937 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
941 input_userauth_passwd_changereq(int type
, u_int32_t seqnr
, void *ctxt
)
943 Authctxt
*authctxt
= ctxt
;
944 char *info
, *lang
, *password
= NULL
, *retype
= NULL
;
946 const char *host
= options
.host_key_alias
? options
.host_key_alias
:
949 debug2("input_userauth_passwd_changereq");
951 if (authctxt
== NULL
)
952 fatal("input_userauth_passwd_changereq: "
953 "no authentication context");
955 info
= packet_get_string(NULL
);
956 lang
= packet_get_string(NULL
);
957 if (strlen(info
) > 0)
961 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
962 packet_put_cstring(authctxt
->server_user
);
963 packet_put_cstring(authctxt
->service
);
964 packet_put_cstring(authctxt
->method
->name
);
965 packet_put_char(1); /* additional info */
966 snprintf(prompt
, sizeof(prompt
),
967 "Enter %.30s@%.128s's old password: ",
968 authctxt
->server_user
, host
);
969 password
= read_passphrase(prompt
, 0);
970 packet_put_cstring(password
);
971 explicit_bzero(password
, strlen(password
));
974 while (password
== NULL
) {
975 snprintf(prompt
, sizeof(prompt
),
976 "Enter %.30s@%.128s's new password: ",
977 authctxt
->server_user
, host
);
978 password
= read_passphrase(prompt
, RP_ALLOW_EOF
);
979 if (password
== NULL
) {
983 snprintf(prompt
, sizeof(prompt
),
984 "Retype %.30s@%.128s's new password: ",
985 authctxt
->server_user
, host
);
986 retype
= read_passphrase(prompt
, 0);
987 if (strcmp(password
, retype
) != 0) {
988 explicit_bzero(password
, strlen(password
));
990 logit("Mismatch; try again, EOF to quit.");
993 explicit_bzero(retype
, strlen(retype
));
996 packet_put_cstring(password
);
997 explicit_bzero(password
, strlen(password
));
999 packet_add_padding(64);
1002 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ
,
1003 &input_userauth_passwd_changereq
);
1007 identity_sign(Identity
*id
, u_char
**sigp
, u_int
*lenp
,
1008 u_char
*data
, u_int datalen
)
1013 /* the agent supports this key */
1015 return (ssh_agent_sign(id
->ac
, id
->key
, sigp
, lenp
,
1018 * we have already loaded the private key or
1019 * the private key is stored in external hardware
1021 if (id
->isprivate
|| (id
->key
->flags
& SSHKEY_FLAG_EXT
))
1022 return (key_sign(id
->key
, sigp
, lenp
, data
, datalen
));
1023 /* load the private key from the file */
1024 if ((prv
= load_identity_file(id
->filename
, id
->userprovided
)) == NULL
)
1026 ret
= key_sign(prv
, sigp
, lenp
, data
, datalen
);
1032 sign_and_send_pubkey(Authctxt
*authctxt
, Identity
*id
)
1035 u_char
*blob
, *signature
;
1036 u_int bloblen
, slen
;
1042 fp
= key_fingerprint(id
->key
, SSH_FP_MD5
, SSH_FP_HEX
);
1043 debug3("sign_and_send_pubkey: %s %s", key_type(id
->key
), fp
);
1046 if (key_to_blob(id
->key
, &blob
, &bloblen
) == 0) {
1047 /* we cannot handle this key */
1048 debug3("sign_and_send_pubkey: cannot handle key");
1051 /* data to be signed */
1053 if (datafellows
& SSH_OLD_SESSIONID
) {
1054 buffer_append(&b
, session_id2
, session_id2_len
);
1055 skip
= session_id2_len
;
1057 buffer_put_string(&b
, session_id2
, session_id2_len
);
1058 skip
= buffer_len(&b
);
1060 buffer_put_char(&b
, SSH2_MSG_USERAUTH_REQUEST
);
1061 buffer_put_cstring(&b
, authctxt
->server_user
);
1062 buffer_put_cstring(&b
,
1063 datafellows
& SSH_BUG_PKSERVICE
?
1066 if (datafellows
& SSH_BUG_PKAUTH
) {
1067 buffer_put_char(&b
, have_sig
);
1069 buffer_put_cstring(&b
, authctxt
->method
->name
);
1070 buffer_put_char(&b
, have_sig
);
1071 buffer_put_cstring(&b
, key_ssh_name(id
->key
));
1073 buffer_put_string(&b
, blob
, bloblen
);
1075 /* generate signature */
1076 ret
= identity_sign(id
, &signature
, &slen
,
1077 buffer_ptr(&b
), buffer_len(&b
));
1086 if (datafellows
& SSH_BUG_PKSERVICE
) {
1088 buffer_append(&b
, session_id2
, session_id2_len
);
1089 skip
= session_id2_len
;
1090 buffer_put_char(&b
, SSH2_MSG_USERAUTH_REQUEST
);
1091 buffer_put_cstring(&b
, authctxt
->server_user
);
1092 buffer_put_cstring(&b
, authctxt
->service
);
1093 buffer_put_cstring(&b
, authctxt
->method
->name
);
1094 buffer_put_char(&b
, have_sig
);
1095 if (!(datafellows
& SSH_BUG_PKAUTH
))
1096 buffer_put_cstring(&b
, key_ssh_name(id
->key
));
1097 buffer_put_string(&b
, blob
, bloblen
);
1101 /* append signature */
1102 buffer_put_string(&b
, signature
, slen
);
1105 /* skip session id and packet type */
1106 if (buffer_len(&b
) < skip
+ 1)
1107 fatal("userauth_pubkey: internal error");
1108 buffer_consume(&b
, skip
+ 1);
1110 /* put remaining data from buffer into packet */
1111 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
1112 packet_put_raw(buffer_ptr(&b
), buffer_len(&b
));
1120 send_pubkey_test(Authctxt
*authctxt
, Identity
*id
)
1123 u_int bloblen
, have_sig
= 0;
1125 debug3("send_pubkey_test");
1127 if (key_to_blob(id
->key
, &blob
, &bloblen
) == 0) {
1128 /* we cannot handle this key */
1129 debug3("send_pubkey_test: cannot handle key");
1132 /* register callback for USERAUTH_PK_OK message */
1133 dispatch_set(SSH2_MSG_USERAUTH_PK_OK
, &input_userauth_pk_ok
);
1135 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
1136 packet_put_cstring(authctxt
->server_user
);
1137 packet_put_cstring(authctxt
->service
);
1138 packet_put_cstring(authctxt
->method
->name
);
1139 packet_put_char(have_sig
);
1140 if (!(datafellows
& SSH_BUG_PKAUTH
))
1141 packet_put_cstring(key_ssh_name(id
->key
));
1142 packet_put_string(blob
, bloblen
);
1149 load_identity_file(char *filename
, int userprovided
)
1152 char prompt
[300], *passphrase
;
1153 int perm_ok
= 0, quit
, i
;
1156 if (stat(filename
, &st
) < 0) {
1157 (userprovided
? logit
: debug3
)("no such identity: %s: %s",
1158 filename
, strerror(errno
));
1161 private = key_load_private_type(KEY_UNSPEC
, filename
, "", NULL
, &perm_ok
);
1163 if (private != NULL
)
1167 if (private == NULL
) {
1168 if (options
.batch_mode
)
1170 snprintf(prompt
, sizeof prompt
,
1171 "Enter passphrase for key '%.100s': ", filename
);
1172 for (i
= 0; i
< options
.number_of_password_prompts
; i
++) {
1173 passphrase
= read_passphrase(prompt
, 0);
1174 if (strcmp(passphrase
, "") != 0) {
1175 private = key_load_private_type(KEY_UNSPEC
,
1176 filename
, passphrase
, NULL
, NULL
);
1179 debug2("no passphrase given, try next key");
1182 explicit_bzero(passphrase
, strlen(passphrase
));
1184 if (private != NULL
|| quit
)
1186 debug2("bad passphrase given, try again...");
1193 * try keys in the following order:
1194 * 1. agent keys that are found in the config file
1195 * 2. other agent keys
1196 * 3. keys that are only listed in the config file
1199 pubkey_prepare(Authctxt
*authctxt
)
1201 Identity
*id
, *id2
, *tmp
;
1202 Idlist agent
, files
, *preferred
;
1204 AuthenticationConnection
*ac
;
1208 TAILQ_INIT(&agent
); /* keys from the agent */
1209 TAILQ_INIT(&files
); /* keys from the config file */
1210 preferred
= &authctxt
->keys
;
1211 TAILQ_INIT(preferred
); /* preferred order of keys */
1213 /* list of keys stored in the filesystem and PKCS#11 */
1214 for (i
= 0; i
< options
.num_identity_files
; i
++) {
1215 key
= options
.identity_keys
[i
];
1216 if (key
&& key
->type
== KEY_RSA1
)
1218 if (key
&& key
->cert
&& key
->cert
->type
!= SSH2_CERT_TYPE_USER
)
1220 options
.identity_keys
[i
] = NULL
;
1221 id
= xcalloc(1, sizeof(*id
));
1223 id
->filename
= xstrdup(options
.identity_files
[i
]);
1224 id
->userprovided
= options
.identity_file_userprovided
[i
];
1225 TAILQ_INSERT_TAIL(&files
, id
, next
);
1227 /* Prefer PKCS11 keys that are explicitly listed */
1228 TAILQ_FOREACH_SAFE(id
, &files
, next
, tmp
) {
1229 if (id
->key
== NULL
|| (id
->key
->flags
& SSHKEY_FLAG_EXT
) == 0)
1232 TAILQ_FOREACH(id2
, &files
, next
) {
1233 if (id2
->key
== NULL
||
1234 (id2
->key
->flags
& SSHKEY_FLAG_EXT
) == 0)
1236 if (key_equal(id
->key
, id2
->key
)) {
1237 TAILQ_REMOVE(&files
, id
, next
);
1238 TAILQ_INSERT_TAIL(preferred
, id
, next
);
1243 /* If IdentitiesOnly set and key not found then don't use it */
1244 if (!found
&& options
.identities_only
) {
1245 TAILQ_REMOVE(&files
, id
, next
);
1246 explicit_bzero(id
, sizeof(*id
));
1250 /* list of keys supported by the agent */
1251 if ((ac
= ssh_get_authentication_connection())) {
1252 for (key
= ssh_get_first_identity(ac
, &comment
, 2);
1254 key
= ssh_get_next_identity(ac
, &comment
, 2)) {
1256 TAILQ_FOREACH(id
, &files
, next
) {
1257 /* agent keys from the config file are preferred */
1258 if (key_equal(key
, id
->key
)) {
1261 TAILQ_REMOVE(&files
, id
, next
);
1262 TAILQ_INSERT_TAIL(preferred
, id
, next
);
1268 if (!found
&& !options
.identities_only
) {
1269 id
= xcalloc(1, sizeof(*id
));
1271 id
->filename
= comment
;
1273 TAILQ_INSERT_TAIL(&agent
, id
, next
);
1276 /* append remaining agent keys */
1277 for (id
= TAILQ_FIRST(&agent
); id
; id
= TAILQ_FIRST(&agent
)) {
1278 TAILQ_REMOVE(&agent
, id
, next
);
1279 TAILQ_INSERT_TAIL(preferred
, id
, next
);
1281 authctxt
->agent
= ac
;
1283 /* append remaining keys from the config file */
1284 for (id
= TAILQ_FIRST(&files
); id
; id
= TAILQ_FIRST(&files
)) {
1285 TAILQ_REMOVE(&files
, id
, next
);
1286 TAILQ_INSERT_TAIL(preferred
, id
, next
);
1288 TAILQ_FOREACH(id
, preferred
, next
) {
1289 debug2("key: %s (%p),%s", id
->filename
, id
->key
,
1290 id
->userprovided
? " explicit" : "");
1295 pubkey_cleanup(Authctxt
*authctxt
)
1299 if (authctxt
->agent
!= NULL
)
1300 ssh_close_authentication_connection(authctxt
->agent
);
1301 for (id
= TAILQ_FIRST(&authctxt
->keys
); id
;
1302 id
= TAILQ_FIRST(&authctxt
->keys
)) {
1303 TAILQ_REMOVE(&authctxt
->keys
, id
, next
);
1312 userauth_pubkey(Authctxt
*authctxt
)
1317 while ((id
= TAILQ_FIRST(&authctxt
->keys
))) {
1320 /* move key to the end of the queue */
1321 TAILQ_REMOVE(&authctxt
->keys
, id
, next
);
1322 TAILQ_INSERT_TAIL(&authctxt
->keys
, id
, next
);
1324 * send a test message if we have the public key. for
1325 * encrypted keys we cannot do this and have to load the
1326 * private key instead
1328 if (id
->key
!= NULL
) {
1329 if (key_type_plain(id
->key
->type
) == KEY_RSA
&&
1330 (datafellows
& SSH_BUG_RSASIGMD5
) != 0) {
1331 debug("Skipped %s key %s for RSA/MD5 server",
1332 key_type(id
->key
), id
->filename
);
1333 } else if (id
->key
->type
!= KEY_RSA1
) {
1334 debug("Offering %s public key: %s",
1335 key_type(id
->key
), id
->filename
);
1336 sent
= send_pubkey_test(authctxt
, id
);
1339 debug("Trying private key: %s", id
->filename
);
1340 id
->key
= load_identity_file(id
->filename
,
1342 if (id
->key
!= NULL
) {
1344 if (key_type_plain(id
->key
->type
) == KEY_RSA
&&
1345 (datafellows
& SSH_BUG_RSASIGMD5
) != 0) {
1346 debug("Skipped %s key %s for RSA/MD5 "
1347 "server", key_type(id
->key
),
1350 sent
= sign_and_send_pubkey(
1364 * Send userauth request message specifying keyboard-interactive method.
1367 userauth_kbdint(Authctxt
*authctxt
)
1369 static int attempt
= 0;
1371 if (attempt
++ >= options
.number_of_password_prompts
)
1373 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1374 if (attempt
> 1 && !authctxt
->info_req_seen
) {
1375 debug3("userauth_kbdint: disable: no info_req_seen");
1376 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST
, NULL
);
1380 debug2("userauth_kbdint");
1381 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
1382 packet_put_cstring(authctxt
->server_user
);
1383 packet_put_cstring(authctxt
->service
);
1384 packet_put_cstring(authctxt
->method
->name
);
1385 packet_put_cstring(""); /* lang */
1386 packet_put_cstring(options
.kbd_interactive_devices
?
1387 options
.kbd_interactive_devices
: "");
1390 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST
, &input_userauth_info_req
);
1395 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1398 input_userauth_info_req(int type
, u_int32_t seq
, void *ctxt
)
1400 Authctxt
*authctxt
= ctxt
;
1401 char *name
, *inst
, *lang
, *prompt
, *response
;
1402 u_int num_prompts
, i
;
1405 debug2("input_userauth_info_req");
1407 if (authctxt
== NULL
)
1408 fatal("input_userauth_info_req: no authentication context");
1410 authctxt
->info_req_seen
= 1;
1412 name
= packet_get_string(NULL
);
1413 inst
= packet_get_string(NULL
);
1414 lang
= packet_get_string(NULL
);
1415 if (strlen(name
) > 0)
1417 if (strlen(inst
) > 0)
1423 num_prompts
= packet_get_int();
1425 * Begin to build info response packet based on prompts requested.
1426 * We commit to providing the correct number of responses, so if
1427 * further on we run into a problem that prevents this, we have to
1428 * be sure and clean this up and send a correct error response.
1430 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE
);
1431 packet_put_int(num_prompts
);
1433 debug2("input_userauth_info_req: num_prompts %d", num_prompts
);
1434 for (i
= 0; i
< num_prompts
; i
++) {
1435 prompt
= packet_get_string(NULL
);
1436 echo
= packet_get_char();
1438 response
= read_passphrase(prompt
, echo
? RP_ECHO
: 0);
1440 packet_put_cstring(response
);
1441 explicit_bzero(response
, strlen(response
));
1445 packet_check_eom(); /* done with parsing incoming message. */
1447 packet_add_padding(64);
1452 ssh_keysign(Key
*key
, u_char
**sigp
, u_int
*lenp
,
1453 u_char
*data
, u_int datalen
)
1458 int to
[2], from
[2], status
, version
= 2;
1460 debug2("ssh_keysign called");
1462 if (stat(_PATH_SSH_KEY_SIGN
, &st
) < 0) {
1463 error("ssh_keysign: not installed: %s", strerror(errno
));
1466 if (fflush(stdout
) != 0)
1467 error("ssh_keysign: fflush: %s", strerror(errno
));
1469 error("ssh_keysign: pipe: %s", strerror(errno
));
1472 if (pipe(from
) < 0) {
1473 error("ssh_keysign: pipe: %s", strerror(errno
));
1476 if ((pid
= fork()) < 0) {
1477 error("ssh_keysign: fork: %s", strerror(errno
));
1481 /* keep the socket on exec */
1482 fcntl(packet_get_connection_in(), F_SETFD
, 0);
1483 permanently_drop_suid(getuid());
1485 if (dup2(from
[1], STDOUT_FILENO
) < 0)
1486 fatal("ssh_keysign: dup2: %s", strerror(errno
));
1488 if (dup2(to
[0], STDIN_FILENO
) < 0)
1489 fatal("ssh_keysign: dup2: %s", strerror(errno
));
1492 execl(_PATH_SSH_KEY_SIGN
, _PATH_SSH_KEY_SIGN
, (char *) 0);
1493 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN
,
1500 buffer_put_int(&b
, packet_get_connection_in()); /* send # of socket */
1501 buffer_put_string(&b
, data
, datalen
);
1502 if (ssh_msg_send(to
[1], version
, &b
) == -1)
1503 fatal("ssh_keysign: couldn't send request");
1505 if (ssh_msg_recv(from
[0], &b
) < 0) {
1506 error("ssh_keysign: no reply");
1513 while (waitpid(pid
, &status
, 0) < 0)
1517 if (buffer_get_char(&b
) != version
) {
1518 error("ssh_keysign: bad version");
1522 *sigp
= buffer_get_string(&b
, lenp
);
1529 userauth_hostbased(Authctxt
*authctxt
)
1531 Key
*private = NULL
;
1532 Sensitive
*sensitive
= authctxt
->sensitive
;
1534 u_char
*signature
, *blob
;
1535 char *chost
, *pkalg
, *p
;
1536 const char *service
;
1538 int ok
, i
, found
= 0;
1540 /* check for a useful key */
1541 for (i
= 0; i
< sensitive
->nkeys
; i
++) {
1542 private = sensitive
->keys
[i
];
1543 if (private && private->type
!= KEY_RSA1
) {
1545 /* we take and free the key */
1546 sensitive
->keys
[i
] = NULL
;
1551 debug("No more client hostkeys for hostbased authentication.");
1554 if (key_to_blob(private, &blob
, &blen
) == 0) {
1558 /* figure out a name for the client host */
1559 p
= get_local_name(packet_get_connection_in());
1561 error("userauth_hostbased: cannot get local ipaddr/name");
1566 xasprintf(&chost
, "%s.", p
);
1567 debug2("userauth_hostbased: chost %s", chost
);
1570 service
= datafellows
& SSH_BUG_HBSERVICE
? "ssh-userauth" :
1572 pkalg
= xstrdup(key_ssh_name(private));
1574 /* construct data */
1575 buffer_put_string(&b
, session_id2
, session_id2_len
);
1576 buffer_put_char(&b
, SSH2_MSG_USERAUTH_REQUEST
);
1577 buffer_put_cstring(&b
, authctxt
->server_user
);
1578 buffer_put_cstring(&b
, service
);
1579 buffer_put_cstring(&b
, authctxt
->method
->name
);
1580 buffer_put_cstring(&b
, pkalg
);
1581 buffer_put_string(&b
, blob
, blen
);
1582 buffer_put_cstring(&b
, chost
);
1583 buffer_put_cstring(&b
, authctxt
->local_user
);
1587 if (sensitive
->external_keysign
)
1588 ok
= ssh_keysign(private, &signature
, &slen
,
1589 buffer_ptr(&b
), buffer_len(&b
));
1591 ok
= key_sign(private, &signature
, &slen
,
1592 buffer_ptr(&b
), buffer_len(&b
));
1596 error("key_sign failed");
1602 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
1603 packet_put_cstring(authctxt
->server_user
);
1604 packet_put_cstring(authctxt
->service
);
1605 packet_put_cstring(authctxt
->method
->name
);
1606 packet_put_cstring(pkalg
);
1607 packet_put_string(blob
, blen
);
1608 packet_put_cstring(chost
);
1609 packet_put_cstring(authctxt
->local_user
);
1610 packet_put_string(signature
, slen
);
1611 explicit_bzero(signature
, slen
);
1621 /* find auth method */
1624 * given auth method name, if configurable options permit this method fill
1625 * in auth_ident field and return true, otherwise return false.
1628 authmethod_is_enabled(Authmethod
*method
)
1632 /* return false if options indicate this method is disabled */
1633 if (method
->enabled
== NULL
|| *method
->enabled
== 0)
1635 /* return false if batch mode is enabled but method needs interactive mode */
1636 if (method
->batch_flag
!= NULL
&& *method
->batch_flag
!= 0)
1642 authmethod_lookup(const char *name
)
1644 Authmethod
*method
= NULL
;
1646 for (method
= authmethods
; method
->name
!= NULL
; method
++)
1647 if (strcmp(name
, method
->name
) == 0)
1649 debug2("Unrecognized authentication method name: %s", name
? name
: "NULL");
1653 /* XXX internal state */
1654 static Authmethod
*current
= NULL
;
1655 static char *supported
= NULL
;
1656 static char *preferred
= NULL
;
1659 * Given the authentication method list sent by the server, return the
1660 * next method we should try. If the server initially sends a nil list,
1661 * use a built-in default list.
1664 authmethod_get(char *authlist
)
1669 /* Use a suitable default if we're passed a nil list. */
1670 if (authlist
== NULL
|| strlen(authlist
) == 0)
1671 authlist
= options
.preferred_authentications
;
1673 if (supported
== NULL
|| strcmp(authlist
, supported
) != 0) {
1674 debug3("start over, passed a different list %s", authlist
);
1676 supported
= xstrdup(authlist
);
1677 preferred
= options
.preferred_authentications
;
1678 debug3("preferred %s", preferred
);
1680 } else if (current
!= NULL
&& authmethod_is_enabled(current
))
1684 if ((name
= match_list(preferred
, supported
, &next
)) == NULL
) {
1685 debug("No more authentication methods to try.");
1690 debug3("authmethod_lookup %s", name
);
1691 debug3("remaining preferred: %s", preferred
);
1692 if ((current
= authmethod_lookup(name
)) != NULL
&&
1693 authmethod_is_enabled(current
)) {
1694 debug3("authmethod_is_enabled %s", name
);
1695 debug("Next authentication method: %s", name
);
1704 authmethods_get(void)
1706 Authmethod
*method
= NULL
;
1711 for (method
= authmethods
; method
->name
!= NULL
; method
++) {
1712 if (authmethod_is_enabled(method
)) {
1713 if (buffer_len(&b
) > 0)
1714 buffer_append(&b
, ",", 1);
1715 buffer_append(&b
, method
->name
, strlen(method
->name
));
1718 buffer_append(&b
, "\0", 1);
1719 list
= xstrdup(buffer_ptr(&b
));