1 /* $OpenBSD: sshconnect2.c,v 1.186 2010/11/29 23:45:51 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"
81 extern char *client_version_string
;
82 extern char *server_version_string
;
83 extern Options options
;
89 u_char
*session_id2
= NULL
;
90 u_int session_id2_len
= 0;
93 struct sockaddr
*xxx_hostaddr
;
98 verify_host_key_callback(Key
*hostkey
)
100 if (verify_host_key(xxx_host
, xxx_hostaddr
, hostkey
) == -1)
101 fatal("Host key verification failed.");
106 order_hostkeyalgs(char *host
, struct sockaddr
*hostaddr
, u_short port
)
108 char *oavail
, *avail
, *first
, *last
, *alg
, *hostname
, *ret
;
110 struct hostkeys
*hostkeys
;
113 /* Find all hostkeys for this hostname */
114 get_hostfile_hostname_ipaddr(host
, hostaddr
, port
, &hostname
, NULL
);
115 hostkeys
= init_hostkeys();
116 load_hostkeys(hostkeys
, hostname
, options
.user_hostfile2
);
117 load_hostkeys(hostkeys
, hostname
, options
.system_hostfile2
);
118 load_hostkeys(hostkeys
, hostname
, options
.user_hostfile
);
119 load_hostkeys(hostkeys
, hostname
, options
.system_hostfile
);
121 oavail
= avail
= xstrdup(KEX_DEFAULT_PK_ALG
);
122 maxlen
= strlen(avail
) + 1;
123 first
= xmalloc(maxlen
);
124 last
= xmalloc(maxlen
);
125 *first
= *last
= '\0';
127 #define ALG_APPEND(to, from) \
130 strlcat(to, ",", maxlen); \
131 strlcat(to, from, maxlen); \
134 while ((alg
= strsep(&avail
, ",")) && *alg
!= '\0') {
135 if ((ktype
= key_type_from_name(alg
)) == KEY_UNSPEC
)
136 fatal("%s: unknown alg %s", __func__
, alg
);
137 if (lookup_key_in_hostkeys_by_type(hostkeys
,
138 key_type_plain(ktype
), NULL
))
139 ALG_APPEND(first
, alg
);
141 ALG_APPEND(last
, alg
);
144 xasprintf(&ret
, "%s%s%s", first
, *first
== '\0' ? "" : ",", last
);
146 debug3("%s: prefer hostkeyalgs: %s", __func__
, first
);
152 free_hostkeys(hostkeys
);
158 ssh_kex2(char *host
, struct sockaddr
*hostaddr
, u_short port
)
163 xxx_hostaddr
= hostaddr
;
165 if (options
.ciphers
== (char *)-1) {
166 logit("No valid ciphers for protocol version 2 given, using defaults.");
167 options
.ciphers
= NULL
;
169 if (options
.ciphers
!= NULL
) {
170 myproposal
[PROPOSAL_ENC_ALGS_CTOS
] =
171 myproposal
[PROPOSAL_ENC_ALGS_STOC
] = options
.ciphers
;
173 myproposal
[PROPOSAL_ENC_ALGS_CTOS
] =
174 compat_cipher_proposal(myproposal
[PROPOSAL_ENC_ALGS_CTOS
]);
175 myproposal
[PROPOSAL_ENC_ALGS_STOC
] =
176 compat_cipher_proposal(myproposal
[PROPOSAL_ENC_ALGS_STOC
]);
177 if (options
.compression
) {
178 myproposal
[PROPOSAL_COMP_ALGS_CTOS
] =
179 myproposal
[PROPOSAL_COMP_ALGS_STOC
] = "zlib@openssh.com,zlib,none";
181 myproposal
[PROPOSAL_COMP_ALGS_CTOS
] =
182 myproposal
[PROPOSAL_COMP_ALGS_STOC
] = "none,zlib@openssh.com,zlib";
184 if (options
.macs
!= NULL
) {
185 myproposal
[PROPOSAL_MAC_ALGS_CTOS
] =
186 myproposal
[PROPOSAL_MAC_ALGS_STOC
] = options
.macs
;
188 if (options
.hostkeyalgorithms
!= NULL
)
189 myproposal
[PROPOSAL_SERVER_HOST_KEY_ALGS
] =
190 options
.hostkeyalgorithms
;
192 /* Prefer algorithms that we already have keys for */
193 myproposal
[PROPOSAL_SERVER_HOST_KEY_ALGS
] =
194 order_hostkeyalgs(host
, hostaddr
, port
);
196 if (options
.kex_algorithms
!= NULL
)
197 myproposal
[PROPOSAL_KEX_ALGS
] = options
.kex_algorithms
;
199 if (options
.rekey_limit
)
200 packet_set_rekey_limit((u_int32_t
)options
.rekey_limit
);
202 /* start key exchange */
203 kex
= kex_setup(myproposal
);
204 kex
->kex
[KEX_DH_GRP1_SHA1
] = kexdh_client
;
205 kex
->kex
[KEX_DH_GRP14_SHA1
] = kexdh_client
;
206 kex
->kex
[KEX_DH_GEX_SHA1
] = kexgex_client
;
207 kex
->kex
[KEX_DH_GEX_SHA256
] = kexgex_client
;
208 kex
->kex
[KEX_ECDH_SHA2
] = kexecdh_client
;
209 kex
->client_version_string
=client_version_string
;
210 kex
->server_version_string
=server_version_string
;
211 kex
->verify_host_key
=&verify_host_key_callback
;
215 dispatch_run(DISPATCH_BLOCK
, &kex
->done
, kex
);
217 if (options
.use_roaming
&& !kex
->roaming
) {
218 debug("Roaming not allowed by server");
219 options
.use_roaming
= 0;
222 session_id2
= kex
->session_id
;
223 session_id2_len
= kex
->session_id_len
;
226 /* send 1st encrypted/maced/compressed message */
227 packet_start(SSH2_MSG_IGNORE
);
228 packet_put_cstring("markus");
238 typedef struct Authctxt Authctxt
;
239 typedef struct Authmethod Authmethod
;
240 typedef struct identity Identity
;
241 typedef struct idlist Idlist
;
244 TAILQ_ENTRY(identity
) next
;
245 AuthenticationConnection
*ac
; /* set if agent supports key */
246 Key
*key
; /* public/private key */
247 char *filename
; /* comment for agent-only keys */
249 int isprivate
; /* key points to the private key */
251 TAILQ_HEAD(idlist
, identity
);
254 const char *server_user
;
255 const char *local_user
;
259 sig_atomic_t success
;
263 AuthenticationConnection
*agent
;
265 Sensitive
*sensitive
;
266 /* kbd-interactive */
272 char *name
; /* string to compare against server's list */
273 int (*userauth
)(Authctxt
*authctxt
);
274 void (*cleanup
)(Authctxt
*authctxt
);
275 int *enabled
; /* flag in option struct that enables method */
276 int *batch_flag
; /* flag in option struct that disables method */
279 void input_userauth_success(int, u_int32_t
, void *);
280 void input_userauth_success_unexpected(int, u_int32_t
, void *);
281 void input_userauth_failure(int, u_int32_t
, void *);
282 void input_userauth_banner(int, u_int32_t
, void *);
283 void input_userauth_error(int, u_int32_t
, void *);
284 void input_userauth_info_req(int, u_int32_t
, void *);
285 void input_userauth_pk_ok(int, u_int32_t
, void *);
286 void input_userauth_passwd_changereq(int, u_int32_t
, void *);
287 void input_userauth_jpake_server_step1(int, u_int32_t
, void *);
288 void input_userauth_jpake_server_step2(int, u_int32_t
, void *);
289 void input_userauth_jpake_server_confirm(int, u_int32_t
, void *);
291 int userauth_none(Authctxt
*);
292 int userauth_pubkey(Authctxt
*);
293 int userauth_passwd(Authctxt
*);
294 int userauth_kbdint(Authctxt
*);
295 int userauth_hostbased(Authctxt
*);
296 int userauth_jpake(Authctxt
*);
298 void userauth_jpake_cleanup(Authctxt
*);
301 int userauth_gssapi(Authctxt
*authctxt
);
302 void input_gssapi_response(int type
, u_int32_t
, void *);
303 void input_gssapi_token(int type
, u_int32_t
, void *);
304 void input_gssapi_hash(int type
, u_int32_t
, void *);
305 void input_gssapi_error(int, u_int32_t
, void *);
306 void input_gssapi_errtok(int, u_int32_t
, void *);
309 void userauth(Authctxt
*, char *);
311 static int sign_and_send_pubkey(Authctxt
*, Identity
*);
312 static void pubkey_prepare(Authctxt
*);
313 static void pubkey_cleanup(Authctxt
*);
314 static Key
*load_identity_file(char *);
316 static Authmethod
*authmethod_get(char *authlist
);
317 static Authmethod
*authmethod_lookup(const char *name
);
318 static char *authmethods_get(void);
320 Authmethod authmethods
[] = {
325 &options
.gss_authentication
,
331 &options
.hostbased_authentication
,
336 &options
.pubkey_authentication
,
339 {"jpake-01@openssh.com",
341 userauth_jpake_cleanup
,
342 &options
.zero_knowledge_password_authentication
,
343 &options
.batch_mode
},
345 {"keyboard-interactive",
348 &options
.kbd_interactive_authentication
,
349 &options
.batch_mode
},
353 &options
.password_authentication
,
354 &options
.batch_mode
},
360 {NULL
, NULL
, NULL
, NULL
, NULL
}
364 ssh_userauth2(const char *local_user
, const char *server_user
, char *host
,
365 Sensitive
*sensitive
)
370 if (options
.challenge_response_authentication
)
371 options
.kbd_interactive_authentication
= 1;
373 packet_start(SSH2_MSG_SERVICE_REQUEST
);
374 packet_put_cstring("ssh-userauth");
376 debug("SSH2_MSG_SERVICE_REQUEST sent");
378 type
= packet_read();
379 if (type
!= SSH2_MSG_SERVICE_ACCEPT
)
380 fatal("Server denied authentication request: %d", type
);
381 if (packet_remaining() > 0) {
382 char *reply
= packet_get_string(NULL
);
383 debug2("service_accept: %s", reply
);
386 debug2("buggy server: service_accept w/o service");
389 debug("SSH2_MSG_SERVICE_ACCEPT received");
391 if (options
.preferred_authentications
== NULL
)
392 options
.preferred_authentications
= authmethods_get();
394 /* setup authentication context */
395 memset(&authctxt
, 0, sizeof(authctxt
));
396 pubkey_prepare(&authctxt
);
397 authctxt
.server_user
= server_user
;
398 authctxt
.local_user
= local_user
;
399 authctxt
.host
= host
;
400 authctxt
.service
= "ssh-connection"; /* service name */
401 authctxt
.success
= 0;
402 authctxt
.method
= authmethod_lookup("none");
403 authctxt
.authlist
= NULL
;
404 authctxt
.methoddata
= NULL
;
405 authctxt
.sensitive
= sensitive
;
406 authctxt
.info_req_seen
= 0;
407 if (authctxt
.method
== NULL
)
408 fatal("ssh_userauth2: internal error: cannot send userauth none request");
410 /* initial userauth request */
411 userauth_none(&authctxt
);
413 dispatch_init(&input_userauth_error
);
414 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS
, &input_userauth_success
);
415 dispatch_set(SSH2_MSG_USERAUTH_FAILURE
, &input_userauth_failure
);
416 dispatch_set(SSH2_MSG_USERAUTH_BANNER
, &input_userauth_banner
);
417 dispatch_run(DISPATCH_BLOCK
, &authctxt
.success
, &authctxt
); /* loop until success */
419 pubkey_cleanup(&authctxt
);
420 dispatch_range(SSH2_MSG_USERAUTH_MIN
, SSH2_MSG_USERAUTH_MAX
, NULL
);
422 debug("Authentication succeeded (%s).", authctxt
.method
->name
);
426 userauth(Authctxt
*authctxt
, char *authlist
)
428 if (authctxt
->method
!= NULL
&& authctxt
->method
->cleanup
!= NULL
)
429 authctxt
->method
->cleanup(authctxt
);
431 if (authctxt
->methoddata
) {
432 xfree(authctxt
->methoddata
);
433 authctxt
->methoddata
= NULL
;
435 if (authlist
== NULL
) {
436 authlist
= authctxt
->authlist
;
438 if (authctxt
->authlist
)
439 xfree(authctxt
->authlist
);
440 authctxt
->authlist
= authlist
;
443 Authmethod
*method
= authmethod_get(authlist
);
445 fatal("Permission denied (%s).", authlist
);
446 authctxt
->method
= method
;
448 /* reset the per method handler */
449 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN
,
450 SSH2_MSG_USERAUTH_PER_METHOD_MAX
, NULL
);
452 /* and try new method */
453 if (method
->userauth(authctxt
) != 0) {
454 debug2("we sent a %s packet, wait for reply", method
->name
);
457 debug2("we did not send a packet, disable method");
458 method
->enabled
= NULL
;
465 input_userauth_error(int type
, u_int32_t seq
, void *ctxt
)
467 fatal("input_userauth_error: bad message during authentication: "
473 input_userauth_banner(int type
, u_int32_t seq
, void *ctxt
)
475 char *msg
, *raw
, *lang
;
478 debug3("input_userauth_banner");
479 raw
= packet_get_string(&len
);
480 lang
= packet_get_string(NULL
);
481 if (len
> 0 && options
.log_level
>= SYSLOG_LEVEL_INFO
) {
484 msg
= xmalloc(len
* 4 + 1); /* max expansion from strnvis() */
485 strnvis(msg
, raw
, len
* 4 + 1, VIS_SAFE
|VIS_OCTAL
|VIS_NOSLASH
);
486 fprintf(stderr
, "%s", msg
);
495 input_userauth_success(int type
, u_int32_t seq
, void *ctxt
)
497 Authctxt
*authctxt
= ctxt
;
499 if (authctxt
== NULL
)
500 fatal("input_userauth_success: no authentication context");
501 if (authctxt
->authlist
) {
502 xfree(authctxt
->authlist
);
503 authctxt
->authlist
= NULL
;
505 if (authctxt
->method
!= NULL
&& authctxt
->method
->cleanup
!= NULL
)
506 authctxt
->method
->cleanup(authctxt
);
507 if (authctxt
->methoddata
) {
508 xfree(authctxt
->methoddata
);
509 authctxt
->methoddata
= NULL
;
511 authctxt
->success
= 1; /* break out */
515 input_userauth_success_unexpected(int type
, u_int32_t seq
, void *ctxt
)
517 Authctxt
*authctxt
= ctxt
;
519 if (authctxt
== NULL
)
520 fatal("%s: no authentication context", __func__
);
522 fatal("Unexpected authentication success during %s.",
523 authctxt
->method
->name
);
528 input_userauth_failure(int type
, u_int32_t seq
, void *ctxt
)
530 Authctxt
*authctxt
= ctxt
;
531 char *authlist
= NULL
;
534 if (authctxt
== NULL
)
535 fatal("input_userauth_failure: no authentication context");
537 authlist
= packet_get_string(NULL
);
538 partial
= packet_get_char();
542 logit("Authenticated with partial success.");
543 debug("Authentications that can continue: %s", authlist
);
545 userauth(authctxt
, authlist
);
550 input_userauth_pk_ok(int type
, u_int32_t seq
, void *ctxt
)
552 Authctxt
*authctxt
= ctxt
;
556 int pktype
, sent
= 0;
561 if (authctxt
== NULL
)
562 fatal("input_userauth_pk_ok: no authentication context");
563 if (datafellows
& SSH_BUG_PKOK
) {
564 /* this is similar to SSH_BUG_PKAUTH */
565 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
566 pkblob
= packet_get_string(&blen
);
568 buffer_append(&b
, pkblob
, blen
);
569 pkalg
= buffer_get_string(&b
, &alen
);
572 pkalg
= packet_get_string(&alen
);
573 pkblob
= packet_get_string(&blen
);
577 debug("Server accepts key: pkalg %s blen %u", pkalg
, blen
);
579 if ((pktype
= key_type_from_name(pkalg
)) == KEY_UNSPEC
) {
580 debug("unknown pkalg %s", pkalg
);
583 if ((key
= key_from_blob(pkblob
, blen
)) == NULL
) {
584 debug("no key from blob. pkalg %s", pkalg
);
587 if (key
->type
!= pktype
) {
588 error("input_userauth_pk_ok: type mismatch "
589 "for decoded key (received %d, expected %d)",
593 fp
= key_fingerprint(key
, SSH_FP_MD5
, SSH_FP_HEX
);
594 debug2("input_userauth_pk_ok: fp %s", fp
);
598 * search keys in the reverse order, because last candidate has been
599 * moved to the end of the queue. this also avoids confusion by
602 TAILQ_FOREACH_REVERSE(id
, &authctxt
->keys
, idlist
, next
) {
603 if (key_equal(key
, id
->key
)) {
604 sent
= sign_and_send_pubkey(authctxt
, id
);
614 /* try another method if we did not send a packet */
616 userauth(authctxt
, NULL
);
621 userauth_gssapi(Authctxt
*authctxt
)
623 Gssctxt
*gssctxt
= NULL
;
624 static gss_OID_set gss_supported
= NULL
;
625 static u_int mech
= 0;
629 /* Try one GSSAPI method at a time, rather than sending them all at
632 if (gss_supported
== NULL
)
633 gss_indicate_mechs(&min
, &gss_supported
);
635 /* Check to see if the mechanism is usable before we offer it */
636 while (mech
< gss_supported
->count
&& !ok
) {
637 /* My DER encoding requires length<128 */
638 if (gss_supported
->elements
[mech
].length
< 128 &&
639 ssh_gssapi_check_mechanism(&gssctxt
,
640 &gss_supported
->elements
[mech
], authctxt
->host
)) {
641 ok
= 1; /* Mechanism works */
650 authctxt
->methoddata
=(void *)gssctxt
;
652 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
653 packet_put_cstring(authctxt
->server_user
);
654 packet_put_cstring(authctxt
->service
);
655 packet_put_cstring(authctxt
->method
->name
);
659 packet_put_int((gss_supported
->elements
[mech
].length
) + 2);
660 packet_put_char(SSH_GSS_OIDTYPE
);
661 packet_put_char(gss_supported
->elements
[mech
].length
);
662 packet_put_raw(gss_supported
->elements
[mech
].elements
,
663 gss_supported
->elements
[mech
].length
);
667 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE
, &input_gssapi_response
);
668 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN
, &input_gssapi_token
);
669 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR
, &input_gssapi_error
);
670 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK
, &input_gssapi_errtok
);
672 mech
++; /* Move along to next candidate */
678 process_gssapi_token(void *ctxt
, gss_buffer_t recv_tok
)
680 Authctxt
*authctxt
= ctxt
;
681 Gssctxt
*gssctxt
= authctxt
->methoddata
;
682 gss_buffer_desc send_tok
= GSS_C_EMPTY_BUFFER
;
683 gss_buffer_desc mic
= GSS_C_EMPTY_BUFFER
;
684 gss_buffer_desc gssbuf
;
685 OM_uint32 status
, ms
, flags
;
688 status
= ssh_gssapi_init_ctx(gssctxt
, options
.gss_deleg_creds
,
689 recv_tok
, &send_tok
, &flags
);
691 if (send_tok
.length
> 0) {
692 if (GSS_ERROR(status
))
693 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK
);
695 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN
);
697 packet_put_string(send_tok
.value
, send_tok
.length
);
699 gss_release_buffer(&ms
, &send_tok
);
702 if (status
== GSS_S_COMPLETE
) {
703 /* send either complete or MIC, depending on mechanism */
704 if (!(flags
& GSS_C_INTEG_FLAG
)) {
705 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE
);
708 ssh_gssapi_buildmic(&b
, authctxt
->server_user
,
709 authctxt
->service
, "gssapi-with-mic");
711 gssbuf
.value
= buffer_ptr(&b
);
712 gssbuf
.length
= buffer_len(&b
);
714 status
= ssh_gssapi_sign(gssctxt
, &gssbuf
, &mic
);
716 if (!GSS_ERROR(status
)) {
717 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC
);
718 packet_put_string(mic
.value
, mic
.length
);
724 gss_release_buffer(&ms
, &mic
);
733 input_gssapi_response(int type
, u_int32_t plen
, void *ctxt
)
735 Authctxt
*authctxt
= ctxt
;
740 if (authctxt
== NULL
)
741 fatal("input_gssapi_response: no authentication context");
742 gssctxt
= authctxt
->methoddata
;
745 oidv
= packet_get_string(&oidlen
);
748 oidv
[0] != SSH_GSS_OIDTYPE
||
749 oidv
[1] != oidlen
- 2) {
751 debug("Badly encoded mechanism OID received");
752 userauth(authctxt
, NULL
);
756 if (!ssh_gssapi_check_oid(gssctxt
, oidv
+ 2, oidlen
- 2))
757 fatal("Server returned different OID than expected");
763 if (GSS_ERROR(process_gssapi_token(ctxt
, GSS_C_NO_BUFFER
))) {
764 /* Start again with next method on list */
765 debug("Trying to start again");
766 userauth(authctxt
, NULL
);
773 input_gssapi_token(int type
, u_int32_t plen
, void *ctxt
)
775 Authctxt
*authctxt
= ctxt
;
776 gss_buffer_desc recv_tok
;
780 if (authctxt
== NULL
)
781 fatal("input_gssapi_response: no authentication context");
783 recv_tok
.value
= packet_get_string(&slen
);
784 recv_tok
.length
= slen
; /* safe typecast */
788 status
= process_gssapi_token(ctxt
, &recv_tok
);
790 xfree(recv_tok
.value
);
792 if (GSS_ERROR(status
)) {
793 /* Start again with the next method in the list */
794 userauth(authctxt
, NULL
);
801 input_gssapi_errtok(int type
, u_int32_t plen
, void *ctxt
)
803 Authctxt
*authctxt
= ctxt
;
805 gss_buffer_desc send_tok
= GSS_C_EMPTY_BUFFER
;
806 gss_buffer_desc recv_tok
;
807 OM_uint32 status
, ms
;
810 if (authctxt
== NULL
)
811 fatal("input_gssapi_response: no authentication context");
812 gssctxt
= authctxt
->methoddata
;
814 recv_tok
.value
= packet_get_string(&len
);
815 recv_tok
.length
= len
;
819 /* Stick it into GSSAPI and see what it says */
820 status
= ssh_gssapi_init_ctx(gssctxt
, options
.gss_deleg_creds
,
821 &recv_tok
, &send_tok
, NULL
);
823 xfree(recv_tok
.value
);
824 gss_release_buffer(&ms
, &send_tok
);
826 /* Server will be returning a failed packet after this one */
831 input_gssapi_error(int type
, u_int32_t plen
, void *ctxt
)
837 maj
=packet_get_int();
838 min
=packet_get_int();
839 msg
=packet_get_string(NULL
);
840 lang
=packet_get_string(NULL
);
844 debug("Server GSSAPI Error:\n%s", msg
);
851 userauth_none(Authctxt
*authctxt
)
853 /* initial userauth request */
854 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
855 packet_put_cstring(authctxt
->server_user
);
856 packet_put_cstring(authctxt
->service
);
857 packet_put_cstring(authctxt
->method
->name
);
863 userauth_passwd(Authctxt
*authctxt
)
865 static int attempt
= 0;
868 const char *host
= options
.host_key_alias
? options
.host_key_alias
:
871 if (attempt
++ >= options
.number_of_password_prompts
)
875 error("Permission denied, please try again.");
877 snprintf(prompt
, sizeof(prompt
), "%.30s@%.128s's password: ",
878 authctxt
->server_user
, host
);
879 password
= read_passphrase(prompt
, 0);
880 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
881 packet_put_cstring(authctxt
->server_user
);
882 packet_put_cstring(authctxt
->service
);
883 packet_put_cstring(authctxt
->method
->name
);
885 packet_put_cstring(password
);
886 memset(password
, 0, strlen(password
));
888 packet_add_padding(64);
891 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ
,
892 &input_userauth_passwd_changereq
);
898 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
902 input_userauth_passwd_changereq(int type
, u_int32_t seqnr
, void *ctxt
)
904 Authctxt
*authctxt
= ctxt
;
905 char *info
, *lang
, *password
= NULL
, *retype
= NULL
;
907 const char *host
= options
.host_key_alias
? options
.host_key_alias
:
910 debug2("input_userauth_passwd_changereq");
912 if (authctxt
== NULL
)
913 fatal("input_userauth_passwd_changereq: "
914 "no authentication context");
916 info
= packet_get_string(NULL
);
917 lang
= packet_get_string(NULL
);
918 if (strlen(info
) > 0)
922 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
923 packet_put_cstring(authctxt
->server_user
);
924 packet_put_cstring(authctxt
->service
);
925 packet_put_cstring(authctxt
->method
->name
);
926 packet_put_char(1); /* additional info */
927 snprintf(prompt
, sizeof(prompt
),
928 "Enter %.30s@%.128s's old password: ",
929 authctxt
->server_user
, host
);
930 password
= read_passphrase(prompt
, 0);
931 packet_put_cstring(password
);
932 memset(password
, 0, strlen(password
));
935 while (password
== NULL
) {
936 snprintf(prompt
, sizeof(prompt
),
937 "Enter %.30s@%.128s's new password: ",
938 authctxt
->server_user
, host
);
939 password
= read_passphrase(prompt
, RP_ALLOW_EOF
);
940 if (password
== NULL
) {
944 snprintf(prompt
, sizeof(prompt
),
945 "Retype %.30s@%.128s's new password: ",
946 authctxt
->server_user
, host
);
947 retype
= read_passphrase(prompt
, 0);
948 if (strcmp(password
, retype
) != 0) {
949 memset(password
, 0, strlen(password
));
951 logit("Mismatch; try again, EOF to quit.");
954 memset(retype
, 0, strlen(retype
));
957 packet_put_cstring(password
);
958 memset(password
, 0, strlen(password
));
960 packet_add_padding(64);
963 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ
,
964 &input_userauth_passwd_changereq
);
969 pw_encrypt(const char *password
, const char *crypt_scheme
, const char *salt
)
971 /* OpenBSD crypt(3) handles all of these */
972 if (strcmp(crypt_scheme
, "crypt") == 0 ||
973 strcmp(crypt_scheme
, "bcrypt") == 0 ||
974 strcmp(crypt_scheme
, "md5crypt") == 0 ||
975 strcmp(crypt_scheme
, "crypt-extended") == 0)
976 return xstrdup(crypt(password
, salt
));
977 error("%s: unsupported password encryption scheme \"%.100s\"",
978 __func__
, crypt_scheme
);
983 jpake_password_to_secret(Authctxt
*authctxt
, const char *crypt_scheme
,
986 char prompt
[256], *password
, *crypted
;
991 snprintf(prompt
, sizeof(prompt
), "%.30s@%.128s's password (JPAKE): ",
992 authctxt
->server_user
, authctxt
->host
);
993 password
= read_passphrase(prompt
, 0);
995 if ((crypted
= pw_encrypt(password
, crypt_scheme
, salt
)) == NULL
) {
996 logit("Disabling %s authentication", authctxt
->method
->name
);
997 authctxt
->method
->enabled
= NULL
;
998 /* Continue with an empty password to fail gracefully */
999 crypted
= xstrdup("");
1003 debug3("%s: salt = %s", __func__
, salt
);
1004 debug3("%s: scheme = %s", __func__
, crypt_scheme
);
1005 debug3("%s: crypted = %s", __func__
, crypted
);
1008 if (hash_buffer(crypted
, strlen(crypted
), EVP_sha256(),
1009 &secret
, &secret_len
) != 0)
1010 fatal("%s: hash_buffer", __func__
);
1012 bzero(password
, strlen(password
));
1013 bzero(crypted
, strlen(crypted
));
1017 if ((ret
= BN_bin2bn(secret
, secret_len
, NULL
)) == NULL
)
1018 fatal("%s: BN_bin2bn (secret)", __func__
);
1019 bzero(secret
, secret_len
);
1027 input_userauth_jpake_server_step1(int type
, u_int32_t seq
, void *ctxt
)
1029 Authctxt
*authctxt
= ctxt
;
1030 struct jpake_ctx
*pctx
= authctxt
->methoddata
;
1031 u_char
*x3_proof
, *x4_proof
, *x2_s_proof
;
1032 u_int x3_proof_len
, x4_proof_len
, x2_s_proof_len
;
1033 char *crypt_scheme
, *salt
;
1035 /* Disable this message */
1036 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1
, NULL
);
1038 if ((pctx
->g_x3
= BN_new()) == NULL
||
1039 (pctx
->g_x4
= BN_new()) == NULL
)
1040 fatal("%s: BN_new", __func__
);
1042 /* Fetch step 1 values */
1043 crypt_scheme
= packet_get_string(NULL
);
1044 salt
= packet_get_string(NULL
);
1045 pctx
->server_id
= packet_get_string(&pctx
->server_id_len
);
1046 packet_get_bignum2(pctx
->g_x3
);
1047 packet_get_bignum2(pctx
->g_x4
);
1048 x3_proof
= packet_get_string(&x3_proof_len
);
1049 x4_proof
= packet_get_string(&x4_proof_len
);
1052 JPAKE_DEBUG_CTX((pctx
, "step 1 received in %s", __func__
));
1054 /* Obtain password and derive secret */
1055 pctx
->s
= jpake_password_to_secret(authctxt
, crypt_scheme
, salt
);
1056 bzero(crypt_scheme
, strlen(crypt_scheme
));
1057 bzero(salt
, strlen(salt
));
1058 xfree(crypt_scheme
);
1060 JPAKE_DEBUG_BN((pctx
->s
, "%s: s = ", __func__
));
1062 /* Calculate step 2 values */
1063 jpake_step2(pctx
->grp
, pctx
->s
, pctx
->g_x1
,
1064 pctx
->g_x3
, pctx
->g_x4
, pctx
->x2
,
1065 pctx
->server_id
, pctx
->server_id_len
,
1066 pctx
->client_id
, pctx
->client_id_len
,
1067 x3_proof
, x3_proof_len
,
1068 x4_proof
, x4_proof_len
,
1070 &x2_s_proof
, &x2_s_proof_len
);
1072 bzero(x3_proof
, x3_proof_len
);
1073 bzero(x4_proof
, x4_proof_len
);
1077 JPAKE_DEBUG_CTX((pctx
, "step 2 sending in %s", __func__
));
1079 /* Send values for step 2 */
1080 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP2
);
1081 packet_put_bignum2(pctx
->a
);
1082 packet_put_string(x2_s_proof
, x2_s_proof_len
);
1085 bzero(x2_s_proof
, x2_s_proof_len
);
1088 /* Expect step 2 packet from peer */
1089 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2
,
1090 input_userauth_jpake_server_step2
);
1095 input_userauth_jpake_server_step2(int type
, u_int32_t seq
, void *ctxt
)
1097 Authctxt
*authctxt
= ctxt
;
1098 struct jpake_ctx
*pctx
= authctxt
->methoddata
;
1100 u_int x4_s_proof_len
;
1102 /* Disable this message */
1103 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2
, NULL
);
1105 if ((pctx
->b
= BN_new()) == NULL
)
1106 fatal("%s: BN_new", __func__
);
1108 /* Fetch step 2 values */
1109 packet_get_bignum2(pctx
->b
);
1110 x4_s_proof
= packet_get_string(&x4_s_proof_len
);
1113 JPAKE_DEBUG_CTX((pctx
, "step 2 received in %s", __func__
));
1115 /* Derive shared key and calculate confirmation hash */
1116 jpake_key_confirm(pctx
->grp
, pctx
->s
, pctx
->b
,
1117 pctx
->x2
, pctx
->g_x1
, pctx
->g_x2
, pctx
->g_x3
, pctx
->g_x4
,
1118 pctx
->client_id
, pctx
->client_id_len
,
1119 pctx
->server_id
, pctx
->server_id_len
,
1120 session_id2
, session_id2_len
,
1121 x4_s_proof
, x4_s_proof_len
,
1123 &pctx
->h_k_cid_sessid
, &pctx
->h_k_cid_sessid_len
);
1125 bzero(x4_s_proof
, x4_s_proof_len
);
1128 JPAKE_DEBUG_CTX((pctx
, "confirm sending in %s", __func__
));
1130 /* Send key confirmation proof */
1131 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_CONFIRM
);
1132 packet_put_string(pctx
->h_k_cid_sessid
, pctx
->h_k_cid_sessid_len
);
1135 /* Expect confirmation from peer */
1136 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM
,
1137 input_userauth_jpake_server_confirm
);
1142 input_userauth_jpake_server_confirm(int type
, u_int32_t seq
, void *ctxt
)
1144 Authctxt
*authctxt
= ctxt
;
1145 struct jpake_ctx
*pctx
= authctxt
->methoddata
;
1147 /* Disable this message */
1148 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM
, NULL
);
1150 pctx
->h_k_sid_sessid
= packet_get_string(&pctx
->h_k_sid_sessid_len
);
1153 JPAKE_DEBUG_CTX((pctx
, "confirm received in %s", __func__
));
1155 /* Verify expected confirmation hash */
1156 if (jpake_check_confirm(pctx
->k
,
1157 pctx
->server_id
, pctx
->server_id_len
,
1158 session_id2
, session_id2_len
,
1159 pctx
->h_k_sid_sessid
, pctx
->h_k_sid_sessid_len
) == 1)
1160 debug("%s: %s success", __func__
, authctxt
->method
->name
);
1162 debug("%s: confirmation mismatch", __func__
);
1163 /* XXX stash this so if auth succeeds then we can warn/kill */
1166 userauth_jpake_cleanup(authctxt
);
1171 identity_sign(Identity
*id
, u_char
**sigp
, u_int
*lenp
,
1172 u_char
*data
, u_int datalen
)
1177 /* the agent supports this key */
1179 return (ssh_agent_sign(id
->ac
, id
->key
, sigp
, lenp
,
1182 * we have already loaded the private key or
1183 * the private key is stored in external hardware
1185 if (id
->isprivate
|| (id
->key
->flags
& KEY_FLAG_EXT
))
1186 return (key_sign(id
->key
, sigp
, lenp
, data
, datalen
));
1187 /* load the private key from the file */
1188 if ((prv
= load_identity_file(id
->filename
)) == NULL
)
1190 ret
= key_sign(prv
, sigp
, lenp
, data
, datalen
);
1196 sign_and_send_pubkey(Authctxt
*authctxt
, Identity
*id
)
1199 u_char
*blob
, *signature
;
1200 u_int bloblen
, slen
;
1206 fp
= key_fingerprint(id
->key
, SSH_FP_MD5
, SSH_FP_HEX
);
1207 debug3("sign_and_send_pubkey: %s %s", key_type(id
->key
), fp
);
1210 if (key_to_blob(id
->key
, &blob
, &bloblen
) == 0) {
1211 /* we cannot handle this key */
1212 debug3("sign_and_send_pubkey: cannot handle key");
1215 /* data to be signed */
1217 if (datafellows
& SSH_OLD_SESSIONID
) {
1218 buffer_append(&b
, session_id2
, session_id2_len
);
1219 skip
= session_id2_len
;
1221 buffer_put_string(&b
, session_id2
, session_id2_len
);
1222 skip
= buffer_len(&b
);
1224 buffer_put_char(&b
, SSH2_MSG_USERAUTH_REQUEST
);
1225 buffer_put_cstring(&b
, authctxt
->server_user
);
1226 buffer_put_cstring(&b
,
1227 datafellows
& SSH_BUG_PKSERVICE
?
1230 if (datafellows
& SSH_BUG_PKAUTH
) {
1231 buffer_put_char(&b
, have_sig
);
1233 buffer_put_cstring(&b
, authctxt
->method
->name
);
1234 buffer_put_char(&b
, have_sig
);
1235 buffer_put_cstring(&b
, key_ssh_name(id
->key
));
1237 buffer_put_string(&b
, blob
, bloblen
);
1239 /* generate signature */
1240 ret
= identity_sign(id
, &signature
, &slen
,
1241 buffer_ptr(&b
), buffer_len(&b
));
1250 if (datafellows
& SSH_BUG_PKSERVICE
) {
1252 buffer_append(&b
, session_id2
, session_id2_len
);
1253 skip
= session_id2_len
;
1254 buffer_put_char(&b
, SSH2_MSG_USERAUTH_REQUEST
);
1255 buffer_put_cstring(&b
, authctxt
->server_user
);
1256 buffer_put_cstring(&b
, authctxt
->service
);
1257 buffer_put_cstring(&b
, authctxt
->method
->name
);
1258 buffer_put_char(&b
, have_sig
);
1259 if (!(datafellows
& SSH_BUG_PKAUTH
))
1260 buffer_put_cstring(&b
, key_ssh_name(id
->key
));
1261 buffer_put_string(&b
, blob
, bloblen
);
1265 /* append signature */
1266 buffer_put_string(&b
, signature
, slen
);
1269 /* skip session id and packet type */
1270 if (buffer_len(&b
) < skip
+ 1)
1271 fatal("userauth_pubkey: internal error");
1272 buffer_consume(&b
, skip
+ 1);
1274 /* put remaining data from buffer into packet */
1275 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
1276 packet_put_raw(buffer_ptr(&b
), buffer_len(&b
));
1284 send_pubkey_test(Authctxt
*authctxt
, Identity
*id
)
1287 u_int bloblen
, have_sig
= 0;
1289 debug3("send_pubkey_test");
1291 if (key_to_blob(id
->key
, &blob
, &bloblen
) == 0) {
1292 /* we cannot handle this key */
1293 debug3("send_pubkey_test: cannot handle key");
1296 /* register callback for USERAUTH_PK_OK message */
1297 dispatch_set(SSH2_MSG_USERAUTH_PK_OK
, &input_userauth_pk_ok
);
1299 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
1300 packet_put_cstring(authctxt
->server_user
);
1301 packet_put_cstring(authctxt
->service
);
1302 packet_put_cstring(authctxt
->method
->name
);
1303 packet_put_char(have_sig
);
1304 if (!(datafellows
& SSH_BUG_PKAUTH
))
1305 packet_put_cstring(key_ssh_name(id
->key
));
1306 packet_put_string(blob
, bloblen
);
1313 load_identity_file(char *filename
)
1316 char prompt
[300], *passphrase
;
1317 int perm_ok
= 0, quit
, i
;
1320 if (stat(filename
, &st
) < 0) {
1321 debug3("no such identity: %s", filename
);
1324 private = key_load_private_type(KEY_UNSPEC
, filename
, "", NULL
, &perm_ok
);
1327 if (private == NULL
) {
1328 if (options
.batch_mode
)
1330 snprintf(prompt
, sizeof prompt
,
1331 "Enter passphrase for key '%.100s': ", filename
);
1332 for (i
= 0; i
< options
.number_of_password_prompts
; i
++) {
1333 passphrase
= read_passphrase(prompt
, 0);
1334 if (strcmp(passphrase
, "") != 0) {
1335 private = key_load_private_type(KEY_UNSPEC
,
1336 filename
, passphrase
, NULL
, NULL
);
1339 debug2("no passphrase given, try next key");
1342 memset(passphrase
, 0, strlen(passphrase
));
1344 if (private != NULL
|| quit
)
1346 debug2("bad passphrase given, try again...");
1353 * try keys in the following order:
1354 * 1. agent keys that are found in the config file
1355 * 2. other agent keys
1356 * 3. keys that are only listed in the config file
1359 pubkey_prepare(Authctxt
*authctxt
)
1362 Idlist agent
, files
, *preferred
;
1364 AuthenticationConnection
*ac
;
1368 TAILQ_INIT(&agent
); /* keys from the agent */
1369 TAILQ_INIT(&files
); /* keys from the config file */
1370 preferred
= &authctxt
->keys
;
1371 TAILQ_INIT(preferred
); /* preferred order of keys */
1373 /* list of keys stored in the filesystem */
1374 for (i
= 0; i
< options
.num_identity_files
; i
++) {
1375 key
= options
.identity_keys
[i
];
1376 if (key
&& key
->type
== KEY_RSA1
)
1378 if (key
&& key
->cert
&& key
->cert
->type
!= SSH2_CERT_TYPE_USER
)
1380 options
.identity_keys
[i
] = NULL
;
1381 id
= xcalloc(1, sizeof(*id
));
1383 id
->filename
= xstrdup(options
.identity_files
[i
]);
1384 TAILQ_INSERT_TAIL(&files
, id
, next
);
1386 /* list of keys supported by the agent */
1387 if ((ac
= ssh_get_authentication_connection())) {
1388 for (key
= ssh_get_first_identity(ac
, &comment
, 2);
1390 key
= ssh_get_next_identity(ac
, &comment
, 2)) {
1392 TAILQ_FOREACH(id
, &files
, next
) {
1393 /* agent keys from the config file are preferred */
1394 if (key_equal(key
, id
->key
)) {
1397 TAILQ_REMOVE(&files
, id
, next
);
1398 TAILQ_INSERT_TAIL(preferred
, id
, next
);
1404 if (!found
&& !options
.identities_only
) {
1405 id
= xcalloc(1, sizeof(*id
));
1407 id
->filename
= comment
;
1409 TAILQ_INSERT_TAIL(&agent
, id
, next
);
1412 /* append remaining agent keys */
1413 for (id
= TAILQ_FIRST(&agent
); id
; id
= TAILQ_FIRST(&agent
)) {
1414 TAILQ_REMOVE(&agent
, id
, next
);
1415 TAILQ_INSERT_TAIL(preferred
, id
, next
);
1417 authctxt
->agent
= ac
;
1419 /* append remaining keys from the config file */
1420 for (id
= TAILQ_FIRST(&files
); id
; id
= TAILQ_FIRST(&files
)) {
1421 TAILQ_REMOVE(&files
, id
, next
);
1422 TAILQ_INSERT_TAIL(preferred
, id
, next
);
1424 TAILQ_FOREACH(id
, preferred
, next
) {
1425 debug2("key: %s (%p)", id
->filename
, id
->key
);
1430 pubkey_cleanup(Authctxt
*authctxt
)
1434 if (authctxt
->agent
!= NULL
)
1435 ssh_close_authentication_connection(authctxt
->agent
);
1436 for (id
= TAILQ_FIRST(&authctxt
->keys
); id
;
1437 id
= TAILQ_FIRST(&authctxt
->keys
)) {
1438 TAILQ_REMOVE(&authctxt
->keys
, id
, next
);
1442 xfree(id
->filename
);
1448 userauth_pubkey(Authctxt
*authctxt
)
1453 while ((id
= TAILQ_FIRST(&authctxt
->keys
))) {
1456 /* move key to the end of the queue */
1457 TAILQ_REMOVE(&authctxt
->keys
, id
, next
);
1458 TAILQ_INSERT_TAIL(&authctxt
->keys
, id
, next
);
1460 * send a test message if we have the public key. for
1461 * encrypted keys we cannot do this and have to load the
1462 * private key instead
1464 if (id
->key
&& id
->key
->type
!= KEY_RSA1
) {
1465 debug("Offering %s public key: %s", key_type(id
->key
),
1467 sent
= send_pubkey_test(authctxt
, id
);
1468 } else if (id
->key
== NULL
) {
1469 debug("Trying private key: %s", id
->filename
);
1470 id
->key
= load_identity_file(id
->filename
);
1471 if (id
->key
!= NULL
) {
1473 sent
= sign_and_send_pubkey(authctxt
, id
);
1485 * Send userauth request message specifying keyboard-interactive method.
1488 userauth_kbdint(Authctxt
*authctxt
)
1490 static int attempt
= 0;
1492 if (attempt
++ >= options
.number_of_password_prompts
)
1494 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1495 if (attempt
> 1 && !authctxt
->info_req_seen
) {
1496 debug3("userauth_kbdint: disable: no info_req_seen");
1497 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST
, NULL
);
1501 debug2("userauth_kbdint");
1502 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
1503 packet_put_cstring(authctxt
->server_user
);
1504 packet_put_cstring(authctxt
->service
);
1505 packet_put_cstring(authctxt
->method
->name
);
1506 packet_put_cstring(""); /* lang */
1507 packet_put_cstring(options
.kbd_interactive_devices
?
1508 options
.kbd_interactive_devices
: "");
1511 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST
, &input_userauth_info_req
);
1516 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1519 input_userauth_info_req(int type
, u_int32_t seq
, void *ctxt
)
1521 Authctxt
*authctxt
= ctxt
;
1522 char *name
, *inst
, *lang
, *prompt
, *response
;
1523 u_int num_prompts
, i
;
1526 debug2("input_userauth_info_req");
1528 if (authctxt
== NULL
)
1529 fatal("input_userauth_info_req: no authentication context");
1531 authctxt
->info_req_seen
= 1;
1533 name
= packet_get_string(NULL
);
1534 inst
= packet_get_string(NULL
);
1535 lang
= packet_get_string(NULL
);
1536 if (strlen(name
) > 0)
1538 if (strlen(inst
) > 0)
1544 num_prompts
= packet_get_int();
1546 * Begin to build info response packet based on prompts requested.
1547 * We commit to providing the correct number of responses, so if
1548 * further on we run into a problem that prevents this, we have to
1549 * be sure and clean this up and send a correct error response.
1551 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE
);
1552 packet_put_int(num_prompts
);
1554 debug2("input_userauth_info_req: num_prompts %d", num_prompts
);
1555 for (i
= 0; i
< num_prompts
; i
++) {
1556 prompt
= packet_get_string(NULL
);
1557 echo
= packet_get_char();
1559 response
= read_passphrase(prompt
, echo
? RP_ECHO
: 0);
1561 packet_put_cstring(response
);
1562 memset(response
, 0, strlen(response
));
1566 packet_check_eom(); /* done with parsing incoming message. */
1568 packet_add_padding(64);
1573 ssh_keysign(Key
*key
, u_char
**sigp
, u_int
*lenp
,
1574 u_char
*data
, u_int datalen
)
1579 int to
[2], from
[2], status
, version
= 2;
1581 debug2("ssh_keysign called");
1583 if (stat(_PATH_SSH_KEY_SIGN
, &st
) < 0) {
1584 error("ssh_keysign: not installed: %s", strerror(errno
));
1587 if (fflush(stdout
) != 0)
1588 error("ssh_keysign: fflush: %s", strerror(errno
));
1590 error("ssh_keysign: pipe: %s", strerror(errno
));
1593 if (pipe(from
) < 0) {
1594 error("ssh_keysign: pipe: %s", strerror(errno
));
1597 if ((pid
= fork()) < 0) {
1598 error("ssh_keysign: fork: %s", strerror(errno
));
1602 /* keep the socket on exec */
1603 fcntl(packet_get_connection_in(), F_SETFD
, 0);
1604 permanently_drop_suid(getuid());
1606 if (dup2(from
[1], STDOUT_FILENO
) < 0)
1607 fatal("ssh_keysign: dup2: %s", strerror(errno
));
1609 if (dup2(to
[0], STDIN_FILENO
) < 0)
1610 fatal("ssh_keysign: dup2: %s", strerror(errno
));
1613 execl(_PATH_SSH_KEY_SIGN
, _PATH_SSH_KEY_SIGN
, (char *) 0);
1614 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN
,
1621 buffer_put_int(&b
, packet_get_connection_in()); /* send # of socket */
1622 buffer_put_string(&b
, data
, datalen
);
1623 if (ssh_msg_send(to
[1], version
, &b
) == -1)
1624 fatal("ssh_keysign: couldn't send request");
1626 if (ssh_msg_recv(from
[0], &b
) < 0) {
1627 error("ssh_keysign: no reply");
1634 while (waitpid(pid
, &status
, 0) < 0)
1638 if (buffer_get_char(&b
) != version
) {
1639 error("ssh_keysign: bad version");
1643 *sigp
= buffer_get_string(&b
, lenp
);
1650 userauth_hostbased(Authctxt
*authctxt
)
1652 Key
*private = NULL
;
1653 Sensitive
*sensitive
= authctxt
->sensitive
;
1655 u_char
*signature
, *blob
;
1656 char *chost
, *pkalg
, *p
;
1657 const char *service
;
1659 int ok
, i
, found
= 0;
1661 /* check for a useful key */
1662 for (i
= 0; i
< sensitive
->nkeys
; i
++) {
1663 private = sensitive
->keys
[i
];
1664 if (private && private->type
!= KEY_RSA1
) {
1666 /* we take and free the key */
1667 sensitive
->keys
[i
] = NULL
;
1672 debug("No more client hostkeys for hostbased authentication.");
1675 if (key_to_blob(private, &blob
, &blen
) == 0) {
1679 /* figure out a name for the client host */
1680 p
= get_local_name(packet_get_connection_in());
1682 error("userauth_hostbased: cannot get local ipaddr/name");
1687 xasprintf(&chost
, "%s.", p
);
1688 debug2("userauth_hostbased: chost %s", chost
);
1691 service
= datafellows
& SSH_BUG_HBSERVICE
? "ssh-userauth" :
1693 pkalg
= xstrdup(key_ssh_name(private));
1695 /* construct data */
1696 buffer_put_string(&b
, session_id2
, session_id2_len
);
1697 buffer_put_char(&b
, SSH2_MSG_USERAUTH_REQUEST
);
1698 buffer_put_cstring(&b
, authctxt
->server_user
);
1699 buffer_put_cstring(&b
, service
);
1700 buffer_put_cstring(&b
, authctxt
->method
->name
);
1701 buffer_put_cstring(&b
, pkalg
);
1702 buffer_put_string(&b
, blob
, blen
);
1703 buffer_put_cstring(&b
, chost
);
1704 buffer_put_cstring(&b
, authctxt
->local_user
);
1708 if (sensitive
->external_keysign
)
1709 ok
= ssh_keysign(private, &signature
, &slen
,
1710 buffer_ptr(&b
), buffer_len(&b
));
1712 ok
= key_sign(private, &signature
, &slen
,
1713 buffer_ptr(&b
), buffer_len(&b
));
1717 error("key_sign failed");
1723 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
1724 packet_put_cstring(authctxt
->server_user
);
1725 packet_put_cstring(authctxt
->service
);
1726 packet_put_cstring(authctxt
->method
->name
);
1727 packet_put_cstring(pkalg
);
1728 packet_put_string(blob
, blen
);
1729 packet_put_cstring(chost
);
1730 packet_put_cstring(authctxt
->local_user
);
1731 packet_put_string(signature
, slen
);
1732 memset(signature
, 's', slen
);
1744 userauth_jpake(Authctxt
*authctxt
)
1746 struct jpake_ctx
*pctx
;
1747 u_char
*x1_proof
, *x2_proof
;
1748 u_int x1_proof_len
, x2_proof_len
;
1749 static int attempt
= 0; /* XXX share with userauth_password's? */
1751 if (attempt
++ >= options
.number_of_password_prompts
)
1754 error("Permission denied, please try again.");
1756 if (authctxt
->methoddata
!= NULL
)
1757 fatal("%s: authctxt->methoddata already set (%p)",
1758 __func__
, authctxt
->methoddata
);
1760 authctxt
->methoddata
= pctx
= jpake_new();
1763 * Send request immediately, to get the protocol going while
1764 * we do the initial computations.
1766 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
1767 packet_put_cstring(authctxt
->server_user
);
1768 packet_put_cstring(authctxt
->service
);
1769 packet_put_cstring(authctxt
->method
->name
);
1771 packet_write_wait();
1773 jpake_step1(pctx
->grp
,
1774 &pctx
->client_id
, &pctx
->client_id_len
,
1775 &pctx
->x1
, &pctx
->x2
, &pctx
->g_x1
, &pctx
->g_x2
,
1776 &x1_proof
, &x1_proof_len
,
1777 &x2_proof
, &x2_proof_len
);
1779 JPAKE_DEBUG_CTX((pctx
, "step 1 sending in %s", __func__
));
1781 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP1
);
1782 packet_put_string(pctx
->client_id
, pctx
->client_id_len
);
1783 packet_put_bignum2(pctx
->g_x1
);
1784 packet_put_bignum2(pctx
->g_x2
);
1785 packet_put_string(x1_proof
, x1_proof_len
);
1786 packet_put_string(x2_proof
, x2_proof_len
);
1789 bzero(x1_proof
, x1_proof_len
);
1790 bzero(x2_proof
, x2_proof_len
);
1794 /* Expect step 1 packet from peer */
1795 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1
,
1796 input_userauth_jpake_server_step1
);
1797 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS
,
1798 &input_userauth_success_unexpected
);
1804 userauth_jpake_cleanup(Authctxt
*authctxt
)
1806 debug3("%s: clean up", __func__
);
1807 if (authctxt
->methoddata
!= NULL
) {
1808 jpake_free(authctxt
->methoddata
);
1809 authctxt
->methoddata
= NULL
;
1811 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS
, &input_userauth_success
);
1815 /* find auth method */
1818 * given auth method name, if configurable options permit this method fill
1819 * in auth_ident field and return true, otherwise return false.
1822 authmethod_is_enabled(Authmethod
*method
)
1826 /* return false if options indicate this method is disabled */
1827 if (method
->enabled
== NULL
|| *method
->enabled
== 0)
1829 /* return false if batch mode is enabled but method needs interactive mode */
1830 if (method
->batch_flag
!= NULL
&& *method
->batch_flag
!= 0)
1836 authmethod_lookup(const char *name
)
1838 Authmethod
*method
= NULL
;
1840 for (method
= authmethods
; method
->name
!= NULL
; method
++)
1841 if (strcmp(name
, method
->name
) == 0)
1843 debug2("Unrecognized authentication method name: %s", name
? name
: "NULL");
1847 /* XXX internal state */
1848 static Authmethod
*current
= NULL
;
1849 static char *supported
= NULL
;
1850 static char *preferred
= NULL
;
1853 * Given the authentication method list sent by the server, return the
1854 * next method we should try. If the server initially sends a nil list,
1855 * use a built-in default list.
1858 authmethod_get(char *authlist
)
1863 /* Use a suitable default if we're passed a nil list. */
1864 if (authlist
== NULL
|| strlen(authlist
) == 0)
1865 authlist
= options
.preferred_authentications
;
1867 if (supported
== NULL
|| strcmp(authlist
, supported
) != 0) {
1868 debug3("start over, passed a different list %s", authlist
);
1869 if (supported
!= NULL
)
1871 supported
= xstrdup(authlist
);
1872 preferred
= options
.preferred_authentications
;
1873 debug3("preferred %s", preferred
);
1875 } else if (current
!= NULL
&& authmethod_is_enabled(current
))
1879 if ((name
= match_list(preferred
, supported
, &next
)) == NULL
) {
1880 debug("No more authentication methods to try.");
1885 debug3("authmethod_lookup %s", name
);
1886 debug3("remaining preferred: %s", preferred
);
1887 if ((current
= authmethod_lookup(name
)) != NULL
&&
1888 authmethod_is_enabled(current
)) {
1889 debug3("authmethod_is_enabled %s", name
);
1890 debug("Next authentication method: %s", name
);
1897 authmethods_get(void)
1899 Authmethod
*method
= NULL
;
1904 for (method
= authmethods
; method
->name
!= NULL
; method
++) {
1905 if (authmethod_is_enabled(method
)) {
1906 if (buffer_len(&b
) > 0)
1907 buffer_append(&b
, ",", 1);
1908 buffer_append(&b
, method
->name
, strlen(method
->name
));
1911 buffer_append(&b
, "\0", 1);
1912 list
= xstrdup(buffer_ptr(&b
));