1 /* $NetBSD: sshconnect2.c,v 1.1.1.2 2009/12/27 01:07:15 christos Exp $ */
2 /* $OpenBSD: sshconnect2.c,v 1.171 2009/03/05 07:18:19 djm Exp $ */
4 * Copyright (c) 2000 Markus Friedl. All rights reserved.
5 * Copyright (c) 2008 Damien Miller. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 __RCSID("$NetBSD: sshconnect2.c,v 1.3 2009/06/09 13:32:46 stacktic Exp $");
30 #include <sys/types.h>
31 #include <sys/socket.h>
33 #include <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
;
85 /* tty_flag is set in ssh.c. use this in ssh_userauth2 */
86 /* if it is set then prevent the switch to the null cipher */
94 u_char
*session_id2
= NULL
;
95 u_int session_id2_len
= 0;
98 struct sockaddr
*xxx_hostaddr
;
103 verify_host_key_callback(Key
*hostkey
)
105 if (verify_host_key(xxx_host
, xxx_hostaddr
, hostkey
) == -1)
106 fatal("Host key verification failed.");
111 ssh_kex2(char *host
, struct sockaddr
*hostaddr
)
116 xxx_hostaddr
= hostaddr
;
118 if (options
.ciphers
== (char *)-1) {
119 logit("No valid ciphers for protocol version 2 given, using defaults.");
120 options
.ciphers
= NULL
;
122 if (options
.ciphers
!= NULL
) {
123 myproposal
[PROPOSAL_ENC_ALGS_CTOS
] =
124 myproposal
[PROPOSAL_ENC_ALGS_STOC
] = options
.ciphers
;
126 myproposal
[PROPOSAL_ENC_ALGS_CTOS
] =
127 compat_cipher_proposal(myproposal
[PROPOSAL_ENC_ALGS_CTOS
]);
128 myproposal
[PROPOSAL_ENC_ALGS_STOC
] =
129 compat_cipher_proposal(myproposal
[PROPOSAL_ENC_ALGS_STOC
]);
130 if (options
.compression
) {
131 myproposal
[PROPOSAL_COMP_ALGS_CTOS
] =
132 myproposal
[PROPOSAL_COMP_ALGS_STOC
] = "zlib@openssh.com,zlib,none";
134 myproposal
[PROPOSAL_COMP_ALGS_CTOS
] =
135 myproposal
[PROPOSAL_COMP_ALGS_STOC
] = "none,zlib@openssh.com,zlib";
137 if (options
.macs
!= NULL
) {
138 myproposal
[PROPOSAL_MAC_ALGS_CTOS
] =
139 myproposal
[PROPOSAL_MAC_ALGS_STOC
] = options
.macs
;
141 if (options
.hostkeyalgorithms
!= NULL
)
142 myproposal
[PROPOSAL_SERVER_HOST_KEY_ALGS
] =
143 options
.hostkeyalgorithms
;
145 if (options
.rekey_limit
)
146 packet_set_rekey_limit((u_int32_t
)options
.rekey_limit
);
148 /* start key exchange */
149 kex
= kex_setup(myproposal
);
150 kex
->kex
[KEX_DH_GRP1_SHA1
] = kexdh_client
;
151 kex
->kex
[KEX_DH_GRP14_SHA1
] = kexdh_client
;
152 kex
->kex
[KEX_DH_GEX_SHA1
] = kexgex_client
;
153 kex
->kex
[KEX_DH_GEX_SHA256
] = kexgex_client
;
154 kex
->client_version_string
=client_version_string
;
155 kex
->server_version_string
=server_version_string
;
156 kex
->verify_host_key
=&verify_host_key_callback
;
160 dispatch_run(DISPATCH_BLOCK
, &kex
->done
, kex
);
162 session_id2
= kex
->session_id
;
163 session_id2_len
= kex
->session_id_len
;
166 /* send 1st encrypted/maced/compressed message */
167 packet_start(SSH2_MSG_IGNORE
);
168 packet_put_cstring("markus");
178 typedef struct Authctxt Authctxt
;
179 typedef struct Authmethod Authmethod
;
180 typedef struct identity Identity
;
181 typedef struct idlist Idlist
;
184 TAILQ_ENTRY(identity
) next
;
185 AuthenticationConnection
*ac
; /* set if agent supports key */
186 Key
*key
; /* public/private key */
187 char *filename
; /* comment for agent-only keys */
189 int isprivate
; /* key points to the private key */
191 TAILQ_HEAD(idlist
, identity
);
194 const char *server_user
;
195 const char *local_user
;
199 sig_atomic_t success
;
203 AuthenticationConnection
*agent
;
205 Sensitive
*sensitive
;
206 /* kbd-interactive */
212 char *name
; /* string to compare against server's list */
213 int (*userauth
)(Authctxt
*authctxt
);
214 void (*cleanup
)(Authctxt
*authctxt
);
215 int *enabled
; /* flag in option struct that enables method */
216 int *batch_flag
; /* flag in option struct that disables method */
219 void input_userauth_success(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_kerberos(Authctxt
*);
236 int userauth_jpake(Authctxt
*);
238 void userauth_jpake_cleanup(Authctxt
*);
241 int userauth_gssapi(Authctxt
*authctxt
);
242 void input_gssapi_response(int type
, u_int32_t
, void *);
243 void input_gssapi_token(int type
, u_int32_t
, void *);
244 void input_gssapi_hash(int type
, u_int32_t
, void *);
245 void input_gssapi_error(int, u_int32_t
, void *);
246 void input_gssapi_errtok(int, u_int32_t
, void *);
249 void userauth(Authctxt
*, char *);
251 static int sign_and_send_pubkey(Authctxt
*, Identity
*);
252 static void pubkey_prepare(Authctxt
*);
253 static void pubkey_cleanup(Authctxt
*);
254 static Key
*load_identity_file(char *);
256 static Authmethod
*authmethod_get(char *authlist
);
257 static Authmethod
*authmethod_lookup(const char *name
);
258 static char *authmethods_get(void);
260 Authmethod authmethods
[] = {
265 &options
.gss_authentication
,
271 &options
.hostbased_authentication
,
274 {"kerberos-2@ssh.com",
277 &options
.kerberos_authentication
,
283 &options
.pubkey_authentication
,
286 {"jpake-01@openssh.com",
288 userauth_jpake_cleanup
,
289 &options
.zero_knowledge_password_authentication
,
290 &options
.batch_mode
},
292 {"keyboard-interactive",
295 &options
.kbd_interactive_authentication
,
296 &options
.batch_mode
},
300 &options
.password_authentication
,
301 &options
.batch_mode
},
307 {NULL
, NULL
, NULL
, NULL
, NULL
}
311 ssh_userauth2(const char *local_user
, const char *server_user
, char *host
,
312 Sensitive
*sensitive
)
317 if (options
.challenge_response_authentication
)
318 options
.kbd_interactive_authentication
= 1;
320 packet_start(SSH2_MSG_SERVICE_REQUEST
);
321 packet_put_cstring("ssh-userauth");
323 debug("SSH2_MSG_SERVICE_REQUEST sent");
325 type
= packet_read();
326 if (type
!= SSH2_MSG_SERVICE_ACCEPT
)
327 fatal("Server denied authentication request: %d", type
);
328 if (packet_remaining() > 0) {
329 char *reply
= packet_get_string(NULL
);
330 debug2("service_accept: %s", reply
);
333 debug2("buggy server: service_accept w/o service");
336 debug("SSH2_MSG_SERVICE_ACCEPT received");
338 if (options
.preferred_authentications
== NULL
)
339 options
.preferred_authentications
= authmethods_get();
341 /* setup authentication context */
342 memset(&authctxt
, 0, sizeof(authctxt
));
343 pubkey_prepare(&authctxt
);
344 authctxt
.server_user
= server_user
;
345 authctxt
.local_user
= local_user
;
346 authctxt
.host
= host
;
347 authctxt
.service
= "ssh-connection"; /* service name */
348 authctxt
.success
= 0;
349 authctxt
.method
= authmethod_lookup("none");
350 authctxt
.authlist
= NULL
;
351 authctxt
.methoddata
= NULL
;
352 authctxt
.sensitive
= sensitive
;
353 authctxt
.info_req_seen
= 0;
354 if (authctxt
.method
== NULL
)
355 fatal("ssh_userauth2: internal error: cannot send userauth none request");
357 /* initial userauth request */
358 userauth_none(&authctxt
);
360 dispatch_init(&input_userauth_error
);
361 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS
, &input_userauth_success
);
362 dispatch_set(SSH2_MSG_USERAUTH_FAILURE
, &input_userauth_failure
);
363 dispatch_set(SSH2_MSG_USERAUTH_BANNER
, &input_userauth_banner
);
364 dispatch_run(DISPATCH_BLOCK
, &authctxt
.success
, &authctxt
); /* loop until success */
366 pubkey_cleanup(&authctxt
);
367 dispatch_range(SSH2_MSG_USERAUTH_MIN
, SSH2_MSG_USERAUTH_MAX
, NULL
);
369 /* if the user wants to use the none cipher do it */
370 /* post authentication and only if the right conditions are met */
371 /* both of the NONE commands must be true and there must be no */
373 if ((options
.none_switch
== 1) && (options
.none_enabled
== 1))
375 if (!tty_flag
) /* no null on tty sessions */
377 debug("Requesting none rekeying...");
378 myproposal
[PROPOSAL_ENC_ALGS_STOC
] = "none";
379 myproposal
[PROPOSAL_ENC_ALGS_CTOS
] = "none";
380 kex_prop2buf(&xxx_kex
->my
,myproposal
);
381 packet_request_rekeying();
382 fprintf(stderr
, "WARNING: ENABLED NONE CIPHER\n");
386 /* requested NONE cipher when in a tty */
387 debug("Cannot switch to NONE cipher with tty allocated");
388 fprintf(stderr
, "NONE cipher switch disabled when a TTY is allocated\n");
391 debug("Authentication succeeded (%s).", authctxt
.method
->name
);
395 userauth(Authctxt
*authctxt
, char *authlist
)
397 if (authctxt
->method
!= NULL
&& authctxt
->method
->cleanup
!= NULL
)
398 authctxt
->method
->cleanup(authctxt
);
400 if (authctxt
->methoddata
) {
401 xfree(authctxt
->methoddata
);
402 authctxt
->methoddata
= NULL
;
404 if (authlist
== NULL
) {
405 authlist
= authctxt
->authlist
;
407 if (authctxt
->authlist
)
408 xfree(authctxt
->authlist
);
409 authctxt
->authlist
= authlist
;
412 Authmethod
*method
= authmethod_get(authlist
);
414 fatal("Permission denied (%s).", authlist
);
415 authctxt
->method
= method
;
417 /* reset the per method handler */
418 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN
,
419 SSH2_MSG_USERAUTH_PER_METHOD_MAX
, NULL
);
421 /* and try new method */
422 if (method
->userauth(authctxt
) != 0) {
423 debug2("we sent a %s packet, wait for reply", method
->name
);
426 debug2("we did not send a packet, disable method");
427 method
->enabled
= NULL
;
434 input_userauth_error(int type
, u_int32_t seq
, void *ctxt
)
436 fatal("input_userauth_error: bad message during authentication: "
442 input_userauth_banner(int type
, u_int32_t seq
, void *ctxt
)
444 char *msg
, *raw
, *lang
;
447 debug3("input_userauth_banner");
448 raw
= packet_get_string(&len
);
449 lang
= packet_get_string(NULL
);
450 if (len
> 0 && options
.log_level
>= SYSLOG_LEVEL_INFO
) {
453 msg
= xmalloc(len
* 4 + 1); /* max expansion from strnvis() */
454 strvisx(msg
, raw
, len
, VIS_SAFE
|VIS_OCTAL
);
455 fprintf(stderr
, "%s", msg
);
464 input_userauth_success(int type
, u_int32_t seq
, void *ctxt
)
466 Authctxt
*authctxt
= ctxt
;
467 if (authctxt
== NULL
)
468 fatal("input_userauth_success: no authentication context");
469 if (authctxt
->authlist
) {
470 xfree(authctxt
->authlist
);
471 authctxt
->authlist
= NULL
;
473 if (authctxt
->methoddata
) {
474 xfree(authctxt
->methoddata
);
475 authctxt
->methoddata
= NULL
;
477 authctxt
->success
= 1; /* break out */
482 input_userauth_failure(int type
, u_int32_t seq
, void *ctxt
)
484 Authctxt
*authctxt
= ctxt
;
485 char *authlist
= NULL
;
488 if (authctxt
== NULL
)
489 fatal("input_userauth_failure: no authentication context");
491 authlist
= packet_get_string(NULL
);
492 partial
= packet_get_char();
496 logit("Authenticated with partial success.");
497 debug("Authentications that can continue: %s", authlist
);
499 userauth(authctxt
, authlist
);
504 input_userauth_pk_ok(int type
, u_int32_t seq
, void *ctxt
)
506 Authctxt
*authctxt
= ctxt
;
510 int pktype
, sent
= 0;
515 if (authctxt
== NULL
)
516 fatal("input_userauth_pk_ok: no authentication context");
517 if (datafellows
& SSH_BUG_PKOK
) {
518 /* this is similar to SSH_BUG_PKAUTH */
519 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
520 pkblob
= packet_get_string(&blen
);
522 buffer_append(&b
, pkblob
, blen
);
523 pkalg
= buffer_get_string(&b
, &alen
);
526 pkalg
= packet_get_string(&alen
);
527 pkblob
= packet_get_string(&blen
);
531 debug("Server accepts key: pkalg %s blen %u", pkalg
, blen
);
533 if ((pktype
= key_type_from_name(pkalg
)) == KEY_UNSPEC
) {
534 debug("unknown pkalg %s", pkalg
);
537 if ((key
= key_from_blob(pkblob
, blen
)) == NULL
) {
538 debug("no key from blob. pkalg %s", pkalg
);
541 if (key
->type
!= pktype
) {
542 error("input_userauth_pk_ok: type mismatch "
543 "for decoded key (received %d, expected %d)",
547 fp
= key_fingerprint(key
, SSH_FP_MD5
, SSH_FP_HEX
);
548 debug2("input_userauth_pk_ok: fp %s", fp
);
552 * search keys in the reverse order, because last candidate has been
553 * moved to the end of the queue. this also avoids confusion by
556 TAILQ_FOREACH_REVERSE(id
, &authctxt
->keys
, idlist
, next
) {
557 if (key_equal(key
, id
->key
)) {
558 sent
= sign_and_send_pubkey(authctxt
, id
);
568 /* try another method if we did not send a packet */
570 userauth(authctxt
, NULL
);
575 userauth_gssapi(Authctxt
*authctxt
)
577 Gssctxt
*gssctxt
= NULL
;
578 static gss_OID_set gss_supported
= NULL
;
579 static u_int mech
= 0;
583 /* Try one GSSAPI method at a time, rather than sending them all at
586 if (gss_supported
== NULL
)
587 gss_indicate_mechs(&min
, &gss_supported
);
589 /* Check to see if the mechanism is usable before we offer it */
590 while (mech
< gss_supported
->count
&& !ok
) {
591 /* My DER encoding requires length<128 */
592 if (gss_supported
->elements
[mech
].length
< 128 &&
593 ssh_gssapi_check_mechanism(&gssctxt
,
594 &gss_supported
->elements
[mech
], authctxt
->host
)) {
595 ok
= 1; /* Mechanism works */
602 ssh_gssapi_delete_ctx(&gssctxt
);
606 authctxt
->methoddata
=(void *)gssctxt
;
608 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
609 packet_put_cstring(authctxt
->server_user
);
610 packet_put_cstring(authctxt
->service
);
611 packet_put_cstring(authctxt
->method
->name
);
615 packet_put_int((gss_supported
->elements
[mech
].length
) + 2);
616 packet_put_char(SSH_GSS_OIDTYPE
);
617 packet_put_char(gss_supported
->elements
[mech
].length
);
618 packet_put_raw(gss_supported
->elements
[mech
].elements
,
619 gss_supported
->elements
[mech
].length
);
623 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE
, &input_gssapi_response
);
624 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN
, &input_gssapi_token
);
625 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR
, &input_gssapi_error
);
626 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK
, &input_gssapi_errtok
);
628 mech
++; /* Move along to next candidate */
634 process_gssapi_token(void *ctxt
, gss_buffer_t recv_tok
)
636 Authctxt
*authctxt
= ctxt
;
637 Gssctxt
*gssctxt
= authctxt
->methoddata
;
638 gss_buffer_desc send_tok
= GSS_C_EMPTY_BUFFER
;
639 gss_buffer_desc mic
= GSS_C_EMPTY_BUFFER
;
640 gss_buffer_desc gssbuf
;
641 OM_uint32 status
, ms
, flags
;
644 status
= ssh_gssapi_init_ctx(gssctxt
, options
.gss_deleg_creds
,
645 recv_tok
, &send_tok
, &flags
);
647 if (send_tok
.length
> 0) {
648 if (GSS_ERROR(status
))
649 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK
);
651 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN
);
653 packet_put_string(send_tok
.value
, send_tok
.length
);
655 gss_release_buffer(&ms
, &send_tok
);
658 if (status
== GSS_S_COMPLETE
) {
659 /* send either complete or MIC, depending on mechanism */
660 if (!(flags
& GSS_C_INTEG_FLAG
)) {
661 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE
);
664 ssh_gssapi_buildmic(&b
, authctxt
->server_user
,
665 authctxt
->service
, "gssapi-with-mic");
667 gssbuf
.value
= buffer_ptr(&b
);
668 gssbuf
.length
= buffer_len(&b
);
670 status
= ssh_gssapi_sign(gssctxt
, &gssbuf
, &mic
);
672 if (!GSS_ERROR(status
)) {
673 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC
);
674 packet_put_string(mic
.value
, mic
.length
);
680 gss_release_buffer(&ms
, &mic
);
689 input_gssapi_response(int type
, u_int32_t plen
, void *ctxt
)
691 Authctxt
*authctxt
= ctxt
;
696 if (authctxt
== NULL
)
697 fatal("input_gssapi_response: no authentication context");
698 gssctxt
= authctxt
->methoddata
;
701 oidv
= packet_get_string(&oidlen
);
704 oidv
[0] != SSH_GSS_OIDTYPE
||
705 oidv
[1] != oidlen
- 2) {
707 debug("Badly encoded mechanism OID received");
708 userauth(authctxt
, NULL
);
712 if (!ssh_gssapi_check_oid(gssctxt
, oidv
+ 2, oidlen
- 2))
713 fatal("Server returned different OID than expected");
719 if (GSS_ERROR(process_gssapi_token(ctxt
, GSS_C_NO_BUFFER
))) {
720 /* Start again with next method on list */
721 debug("Trying to start again");
722 userauth(authctxt
, NULL
);
729 input_gssapi_token(int type
, u_int32_t plen
, void *ctxt
)
731 Authctxt
*authctxt
= ctxt
;
732 gss_buffer_desc recv_tok
;
736 if (authctxt
== NULL
)
737 fatal("input_gssapi_response: no authentication context");
739 recv_tok
.value
= packet_get_string(&slen
);
740 recv_tok
.length
= slen
; /* safe typecast */
744 status
= process_gssapi_token(ctxt
, &recv_tok
);
746 xfree(recv_tok
.value
);
748 if (GSS_ERROR(status
)) {
749 /* Start again with the next method in the list */
750 userauth(authctxt
, NULL
);
757 input_gssapi_errtok(int type
, u_int32_t plen
, void *ctxt
)
759 Authctxt
*authctxt
= ctxt
;
761 gss_buffer_desc send_tok
= GSS_C_EMPTY_BUFFER
;
762 gss_buffer_desc recv_tok
;
763 OM_uint32 status
, ms
;
766 if (authctxt
== NULL
)
767 fatal("input_gssapi_response: no authentication context");
768 gssctxt
= authctxt
->methoddata
;
770 recv_tok
.value
= packet_get_string(&len
);
771 recv_tok
.length
= len
;
775 /* Stick it into GSSAPI and see what it says */
776 status
= ssh_gssapi_init_ctx(gssctxt
, options
.gss_deleg_creds
,
777 &recv_tok
, &send_tok
, NULL
);
779 xfree(recv_tok
.value
);
780 gss_release_buffer(&ms
, &send_tok
);
782 /* Server will be returning a failed packet after this one */
787 input_gssapi_error(int type
, u_int32_t plen
, void *ctxt
)
793 maj
=packet_get_int();
794 min
=packet_get_int();
795 msg
=packet_get_string(NULL
);
796 lang
=packet_get_string(NULL
);
800 debug("Server GSSAPI Error:\n%s", msg
);
807 userauth_none(Authctxt
*authctxt
)
809 /* initial userauth request */
810 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
811 packet_put_cstring(authctxt
->server_user
);
812 packet_put_cstring(authctxt
->service
);
813 packet_put_cstring(authctxt
->method
->name
);
819 userauth_passwd(Authctxt
*authctxt
)
821 static int attempt
= 0;
825 if (attempt
++ >= options
.number_of_password_prompts
)
829 error("Permission denied, please try again.");
831 snprintf(prompt
, sizeof(prompt
), "%.30s@%.128s's password: ",
832 authctxt
->server_user
, authctxt
->host
);
833 password
= read_passphrase(prompt
, 0);
834 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
835 packet_put_cstring(authctxt
->server_user
);
836 packet_put_cstring(authctxt
->service
);
837 packet_put_cstring(authctxt
->method
->name
);
839 packet_put_cstring(password
);
840 memset(password
, 0, strlen(password
));
842 packet_add_padding(64);
845 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ
,
846 &input_userauth_passwd_changereq
);
852 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
856 input_userauth_passwd_changereq(int type
, u_int32_t seqnr
, void *ctxt
)
858 Authctxt
*authctxt
= ctxt
;
859 char *info
, *lang
, *password
= NULL
, *retype
= NULL
;
862 debug2("input_userauth_passwd_changereq");
864 if (authctxt
== NULL
)
865 fatal("input_userauth_passwd_changereq: "
866 "no authentication context");
868 info
= packet_get_string(NULL
);
869 lang
= packet_get_string(NULL
);
870 if (strlen(info
) > 0)
874 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
875 packet_put_cstring(authctxt
->server_user
);
876 packet_put_cstring(authctxt
->service
);
877 packet_put_cstring(authctxt
->method
->name
);
878 packet_put_char(1); /* additional info */
879 snprintf(prompt
, sizeof(prompt
),
880 "Enter %.30s@%.128s's old password: ",
881 authctxt
->server_user
, authctxt
->host
);
882 password
= read_passphrase(prompt
, 0);
883 packet_put_cstring(password
);
884 memset(password
, 0, strlen(password
));
887 while (password
== NULL
) {
888 snprintf(prompt
, sizeof(prompt
),
889 "Enter %.30s@%.128s's new password: ",
890 authctxt
->server_user
, authctxt
->host
);
891 password
= read_passphrase(prompt
, RP_ALLOW_EOF
);
892 if (password
== NULL
) {
896 snprintf(prompt
, sizeof(prompt
),
897 "Retype %.30s@%.128s's new password: ",
898 authctxt
->server_user
, authctxt
->host
);
899 retype
= read_passphrase(prompt
, 0);
900 if (strcmp(password
, retype
) != 0) {
901 memset(password
, 0, strlen(password
));
903 logit("Mismatch; try again, EOF to quit.");
906 memset(retype
, 0, strlen(retype
));
909 packet_put_cstring(password
);
910 memset(password
, 0, strlen(password
));
912 packet_add_padding(64);
915 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ
,
916 &input_userauth_passwd_changereq
);
921 pw_encrypt(const char *password
, const char *crypt_scheme
, const char *salt
)
923 /* OpenBSD crypt(3) handles all of these */
924 if (strcmp(crypt_scheme
, "crypt") == 0 ||
925 strcmp(crypt_scheme
, "bcrypt") == 0 ||
926 strcmp(crypt_scheme
, "md5crypt") == 0 ||
927 strcmp(crypt_scheme
, "crypt-extended") == 0)
928 return xstrdup(crypt(password
, salt
));
929 error("%s: unsupported password encryption scheme \"%.100s\"",
930 __func__
, crypt_scheme
);
935 jpake_password_to_secret(Authctxt
*authctxt
, const char *crypt_scheme
,
938 char prompt
[256], *password
, *crypted
;
943 snprintf(prompt
, sizeof(prompt
), "%.30s@%.128s's password (JPAKE): ",
944 authctxt
->server_user
, authctxt
->host
);
945 password
= read_passphrase(prompt
, 0);
947 if ((crypted
= pw_encrypt(password
, crypt_scheme
, salt
)) == NULL
) {
948 logit("Disabling %s authentication", authctxt
->method
->name
);
949 authctxt
->method
->enabled
= NULL
;
950 /* Continue with an empty password to fail gracefully */
951 crypted
= xstrdup("");
955 debug3("%s: salt = %s", __func__
, salt
);
956 debug3("%s: scheme = %s", __func__
, crypt_scheme
);
957 debug3("%s: crypted = %s", __func__
, crypted
);
960 if (hash_buffer(crypted
, strlen(crypted
), EVP_sha256(),
961 &secret
, &secret_len
) != 0)
962 fatal("%s: hash_buffer", __func__
);
964 bzero(password
, strlen(password
));
965 bzero(crypted
, strlen(crypted
));
969 if ((ret
= BN_bin2bn(secret
, secret_len
, NULL
)) == NULL
)
970 fatal("%s: BN_bin2bn (secret)", __func__
);
971 bzero(secret
, secret_len
);
979 input_userauth_jpake_server_step1(int type
, u_int32_t seq
, void *ctxt
)
981 Authctxt
*authctxt
= ctxt
;
982 struct jpake_ctx
*pctx
= authctxt
->methoddata
;
983 u_char
*x3_proof
, *x4_proof
, *x2_s_proof
;
984 u_int x3_proof_len
, x4_proof_len
, x2_s_proof_len
;
985 char *crypt_scheme
, *salt
;
987 /* Disable this message */
988 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1
, NULL
);
990 if ((pctx
->g_x3
= BN_new()) == NULL
||
991 (pctx
->g_x4
= BN_new()) == NULL
)
992 fatal("%s: BN_new", __func__
);
994 /* Fetch step 1 values */
995 crypt_scheme
= packet_get_string(NULL
);
996 salt
= packet_get_string(NULL
);
997 pctx
->server_id
= packet_get_string(&pctx
->server_id_len
);
998 packet_get_bignum2(pctx
->g_x3
);
999 packet_get_bignum2(pctx
->g_x4
);
1000 x3_proof
= packet_get_string(&x3_proof_len
);
1001 x4_proof
= packet_get_string(&x4_proof_len
);
1004 JPAKE_DEBUG_CTX((pctx
, "step 1 received in %s", __func__
));
1006 /* Obtain password and derive secret */
1007 pctx
->s
= jpake_password_to_secret(authctxt
, crypt_scheme
, salt
);
1008 bzero(crypt_scheme
, strlen(crypt_scheme
));
1009 bzero(salt
, strlen(salt
));
1010 xfree(crypt_scheme
);
1012 JPAKE_DEBUG_BN((pctx
->s
, "%s: s = ", __func__
));
1014 /* Calculate step 2 values */
1015 jpake_step2(pctx
->grp
, pctx
->s
, pctx
->g_x1
,
1016 pctx
->g_x3
, pctx
->g_x4
, pctx
->x2
,
1017 pctx
->server_id
, pctx
->server_id_len
,
1018 pctx
->client_id
, pctx
->client_id_len
,
1019 x3_proof
, x3_proof_len
,
1020 x4_proof
, x4_proof_len
,
1022 &x2_s_proof
, &x2_s_proof_len
);
1024 bzero(x3_proof
, x3_proof_len
);
1025 bzero(x4_proof
, x4_proof_len
);
1029 JPAKE_DEBUG_CTX((pctx
, "step 2 sending in %s", __func__
));
1031 /* Send values for step 2 */
1032 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP2
);
1033 packet_put_bignum2(pctx
->a
);
1034 packet_put_string(x2_s_proof
, x2_s_proof_len
);
1037 bzero(x2_s_proof
, x2_s_proof_len
);
1040 /* Expect step 2 packet from peer */
1041 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2
,
1042 input_userauth_jpake_server_step2
);
1047 input_userauth_jpake_server_step2(int type
, u_int32_t seq
, void *ctxt
)
1049 Authctxt
*authctxt
= ctxt
;
1050 struct jpake_ctx
*pctx
= authctxt
->methoddata
;
1052 u_int x4_s_proof_len
;
1054 /* Disable this message */
1055 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2
, NULL
);
1057 if ((pctx
->b
= BN_new()) == NULL
)
1058 fatal("%s: BN_new", __func__
);
1060 /* Fetch step 2 values */
1061 packet_get_bignum2(pctx
->b
);
1062 x4_s_proof
= packet_get_string(&x4_s_proof_len
);
1065 JPAKE_DEBUG_CTX((pctx
, "step 2 received in %s", __func__
));
1067 /* Derive shared key and calculate confirmation hash */
1068 jpake_key_confirm(pctx
->grp
, pctx
->s
, pctx
->b
,
1069 pctx
->x2
, pctx
->g_x1
, pctx
->g_x2
, pctx
->g_x3
, pctx
->g_x4
,
1070 pctx
->client_id
, pctx
->client_id_len
,
1071 pctx
->server_id
, pctx
->server_id_len
,
1072 session_id2
, session_id2_len
,
1073 x4_s_proof
, x4_s_proof_len
,
1075 &pctx
->h_k_cid_sessid
, &pctx
->h_k_cid_sessid_len
);
1077 bzero(x4_s_proof
, x4_s_proof_len
);
1080 JPAKE_DEBUG_CTX((pctx
, "confirm sending in %s", __func__
));
1082 /* Send key confirmation proof */
1083 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_CONFIRM
);
1084 packet_put_string(pctx
->h_k_cid_sessid
, pctx
->h_k_cid_sessid_len
);
1087 /* Expect confirmation from peer */
1088 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM
,
1089 input_userauth_jpake_server_confirm
);
1094 input_userauth_jpake_server_confirm(int type
, u_int32_t seq
, void *ctxt
)
1096 Authctxt
*authctxt
= ctxt
;
1097 struct jpake_ctx
*pctx
= authctxt
->methoddata
;
1099 /* Disable this message */
1100 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM
, NULL
);
1102 pctx
->h_k_sid_sessid
= packet_get_string(&pctx
->h_k_sid_sessid_len
);
1105 JPAKE_DEBUG_CTX((pctx
, "confirm received in %s", __func__
));
1107 /* Verify expected confirmation hash */
1108 if (jpake_check_confirm(pctx
->k
,
1109 pctx
->server_id
, pctx
->server_id_len
,
1110 session_id2
, session_id2_len
,
1111 pctx
->h_k_sid_sessid
, pctx
->h_k_sid_sessid_len
) == 1)
1112 debug("%s: %s success", __func__
, authctxt
->method
->name
);
1114 debug("%s: confirmation mismatch", __func__
);
1115 /* XXX stash this so if auth succeeds then we can warn/kill */
1118 userauth_jpake_cleanup(authctxt
);
1123 identity_sign(Identity
*id
, u_char
**sigp
, u_int
*lenp
,
1124 u_char
*data
, u_int datalen
)
1129 /* the agent supports this key */
1131 return (ssh_agent_sign(id
->ac
, id
->key
, sigp
, lenp
,
1134 * we have already loaded the private key or
1135 * the private key is stored in external hardware
1137 if (id
->isprivate
|| (id
->key
->flags
& KEY_FLAG_EXT
))
1138 return (key_sign(id
->key
, sigp
, lenp
, data
, datalen
));
1139 /* load the private key from the file */
1140 if ((prv
= load_identity_file(id
->filename
)) == NULL
)
1142 ret
= key_sign(prv
, sigp
, lenp
, data
, datalen
);
1148 sign_and_send_pubkey(Authctxt
*authctxt
, Identity
*id
)
1151 u_char
*blob
, *signature
;
1152 u_int bloblen
, slen
;
1157 debug3("sign_and_send_pubkey");
1159 if (key_to_blob(id
->key
, &blob
, &bloblen
) == 0) {
1160 /* we cannot handle this key */
1161 debug3("sign_and_send_pubkey: cannot handle key");
1164 /* data to be signed */
1166 if (datafellows
& SSH_OLD_SESSIONID
) {
1167 buffer_append(&b
, session_id2
, session_id2_len
);
1168 skip
= session_id2_len
;
1170 buffer_put_string(&b
, session_id2
, session_id2_len
);
1171 skip
= buffer_len(&b
);
1173 buffer_put_char(&b
, SSH2_MSG_USERAUTH_REQUEST
);
1174 buffer_put_cstring(&b
, authctxt
->server_user
);
1175 buffer_put_cstring(&b
,
1176 datafellows
& SSH_BUG_PKSERVICE
?
1179 if (datafellows
& SSH_BUG_PKAUTH
) {
1180 buffer_put_char(&b
, have_sig
);
1182 buffer_put_cstring(&b
, authctxt
->method
->name
);
1183 buffer_put_char(&b
, have_sig
);
1184 buffer_put_cstring(&b
, key_ssh_name(id
->key
));
1186 buffer_put_string(&b
, blob
, bloblen
);
1188 /* generate signature */
1189 ret
= identity_sign(id
, &signature
, &slen
,
1190 buffer_ptr(&b
), buffer_len(&b
));
1199 if (datafellows
& SSH_BUG_PKSERVICE
) {
1201 buffer_append(&b
, session_id2
, session_id2_len
);
1202 skip
= session_id2_len
;
1203 buffer_put_char(&b
, SSH2_MSG_USERAUTH_REQUEST
);
1204 buffer_put_cstring(&b
, authctxt
->server_user
);
1205 buffer_put_cstring(&b
, authctxt
->service
);
1206 buffer_put_cstring(&b
, authctxt
->method
->name
);
1207 buffer_put_char(&b
, have_sig
);
1208 if (!(datafellows
& SSH_BUG_PKAUTH
))
1209 buffer_put_cstring(&b
, key_ssh_name(id
->key
));
1210 buffer_put_string(&b
, blob
, bloblen
);
1214 /* append signature */
1215 buffer_put_string(&b
, signature
, slen
);
1218 /* skip session id and packet type */
1219 if (buffer_len(&b
) < skip
+ 1)
1220 fatal("userauth_pubkey: internal error");
1221 buffer_consume(&b
, skip
+ 1);
1223 /* put remaining data from buffer into packet */
1224 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
1225 packet_put_raw(buffer_ptr(&b
), buffer_len(&b
));
1233 send_pubkey_test(Authctxt
*authctxt
, Identity
*id
)
1236 u_int bloblen
, have_sig
= 0;
1238 debug3("send_pubkey_test");
1240 if (key_to_blob(id
->key
, &blob
, &bloblen
) == 0) {
1241 /* we cannot handle this key */
1242 debug3("send_pubkey_test: cannot handle key");
1245 /* register callback for USERAUTH_PK_OK message */
1246 dispatch_set(SSH2_MSG_USERAUTH_PK_OK
, &input_userauth_pk_ok
);
1248 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
1249 packet_put_cstring(authctxt
->server_user
);
1250 packet_put_cstring(authctxt
->service
);
1251 packet_put_cstring(authctxt
->method
->name
);
1252 packet_put_char(have_sig
);
1253 if (!(datafellows
& SSH_BUG_PKAUTH
))
1254 packet_put_cstring(key_ssh_name(id
->key
));
1255 packet_put_string(blob
, bloblen
);
1262 load_identity_file(char *filename
)
1265 char prompt
[300], *passphrase
;
1266 int perm_ok
, quit
, i
;
1269 if (stat(filename
, &st
) < 0) {
1270 debug3("no such identity: %s", filename
);
1273 private = key_load_private_type(KEY_UNSPEC
, filename
, "", NULL
, &perm_ok
);
1276 if (private == NULL
) {
1277 if (options
.batch_mode
)
1279 snprintf(prompt
, sizeof prompt
,
1280 "Enter passphrase for key '%.100s': ", filename
);
1281 for (i
= 0; i
< options
.number_of_password_prompts
; i
++) {
1282 passphrase
= read_passphrase(prompt
, 0);
1283 if (strcmp(passphrase
, "") != 0) {
1284 private = key_load_private_type(KEY_UNSPEC
,
1285 filename
, passphrase
, NULL
, NULL
);
1288 debug2("no passphrase given, try next key");
1291 memset(passphrase
, 0, strlen(passphrase
));
1293 if (private != NULL
|| quit
)
1295 debug2("bad passphrase given, try again...");
1302 * try keys in the following order:
1303 * 1. agent keys that are found in the config file
1304 * 2. other agent keys
1305 * 3. keys that are only listed in the config file
1308 pubkey_prepare(Authctxt
*authctxt
)
1311 Idlist agent
, files
, *preferred
;
1313 AuthenticationConnection
*ac
;
1317 TAILQ_INIT(&agent
); /* keys from the agent */
1318 TAILQ_INIT(&files
); /* keys from the config file */
1319 preferred
= &authctxt
->keys
;
1320 TAILQ_INIT(preferred
); /* preferred order of keys */
1322 /* list of keys stored in the filesystem */
1323 for (i
= 0; i
< options
.num_identity_files
; i
++) {
1324 key
= options
.identity_keys
[i
];
1325 if (key
&& key
->type
== KEY_RSA1
)
1327 options
.identity_keys
[i
] = NULL
;
1328 id
= xcalloc(1, sizeof(*id
));
1330 id
->filename
= xstrdup(options
.identity_files
[i
]);
1331 TAILQ_INSERT_TAIL(&files
, id
, next
);
1333 /* list of keys supported by the agent */
1334 if ((ac
= ssh_get_authentication_connection())) {
1335 for (key
= ssh_get_first_identity(ac
, &comment
, 2);
1337 key
= ssh_get_next_identity(ac
, &comment
, 2)) {
1339 TAILQ_FOREACH(id
, &files
, next
) {
1340 /* agent keys from the config file are preferred */
1341 if (key_equal(key
, id
->key
)) {
1344 TAILQ_REMOVE(&files
, id
, next
);
1345 TAILQ_INSERT_TAIL(preferred
, id
, next
);
1351 if (!found
&& !options
.identities_only
) {
1352 id
= xcalloc(1, sizeof(*id
));
1354 id
->filename
= comment
;
1356 TAILQ_INSERT_TAIL(&agent
, id
, next
);
1359 /* append remaining agent keys */
1360 for (id
= TAILQ_FIRST(&agent
); id
; id
= TAILQ_FIRST(&agent
)) {
1361 TAILQ_REMOVE(&agent
, id
, next
);
1362 TAILQ_INSERT_TAIL(preferred
, id
, next
);
1364 authctxt
->agent
= ac
;
1366 /* append remaining keys from the config file */
1367 for (id
= TAILQ_FIRST(&files
); id
; id
= TAILQ_FIRST(&files
)) {
1368 TAILQ_REMOVE(&files
, id
, next
);
1369 TAILQ_INSERT_TAIL(preferred
, id
, next
);
1371 TAILQ_FOREACH(id
, preferred
, next
) {
1372 debug2("key: %s (%p)", id
->filename
, id
->key
);
1377 pubkey_cleanup(Authctxt
*authctxt
)
1381 if (authctxt
->agent
!= NULL
)
1382 ssh_close_authentication_connection(authctxt
->agent
);
1383 for (id
= TAILQ_FIRST(&authctxt
->keys
); id
;
1384 id
= TAILQ_FIRST(&authctxt
->keys
)) {
1385 TAILQ_REMOVE(&authctxt
->keys
, id
, next
);
1389 xfree(id
->filename
);
1395 userauth_pubkey(Authctxt
*authctxt
)
1400 while ((id
= TAILQ_FIRST(&authctxt
->keys
))) {
1403 /* move key to the end of the queue */
1404 TAILQ_REMOVE(&authctxt
->keys
, id
, next
);
1405 TAILQ_INSERT_TAIL(&authctxt
->keys
, id
, next
);
1407 * send a test message if we have the public key. for
1408 * encrypted keys we cannot do this and have to load the
1409 * private key instead
1411 if (id
->key
&& id
->key
->type
!= KEY_RSA1
) {
1412 debug("Offering public key: %s", id
->filename
);
1413 sent
= send_pubkey_test(authctxt
, id
);
1414 } else if (id
->key
== NULL
) {
1415 debug("Trying private key: %s", id
->filename
);
1416 id
->key
= load_identity_file(id
->filename
);
1417 if (id
->key
!= NULL
) {
1419 sent
= sign_and_send_pubkey(authctxt
, id
);
1431 * Send userauth request message specifying keyboard-interactive method.
1434 userauth_kbdint(Authctxt
*authctxt
)
1436 static int attempt
= 0;
1438 if (attempt
++ >= options
.number_of_password_prompts
)
1440 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1441 if (attempt
> 1 && !authctxt
->info_req_seen
) {
1442 debug3("userauth_kbdint: disable: no info_req_seen");
1443 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST
, NULL
);
1447 debug2("userauth_kbdint");
1448 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
1449 packet_put_cstring(authctxt
->server_user
);
1450 packet_put_cstring(authctxt
->service
);
1451 packet_put_cstring(authctxt
->method
->name
);
1452 packet_put_cstring(""); /* lang */
1453 packet_put_cstring(options
.kbd_interactive_devices
?
1454 options
.kbd_interactive_devices
: "");
1457 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST
, &input_userauth_info_req
);
1462 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1465 input_userauth_info_req(int type
, u_int32_t seq
, void *ctxt
)
1467 Authctxt
*authctxt
= ctxt
;
1468 char *name
, *inst
, *lang
, *prompt
, *response
;
1469 u_int num_prompts
, i
;
1472 debug2("input_userauth_info_req");
1474 if (authctxt
== NULL
)
1475 fatal("input_userauth_info_req: no authentication context");
1477 authctxt
->info_req_seen
= 1;
1479 name
= packet_get_string(NULL
);
1480 inst
= packet_get_string(NULL
);
1481 lang
= packet_get_string(NULL
);
1482 if (strlen(name
) > 0)
1484 if (strlen(inst
) > 0)
1490 num_prompts
= packet_get_int();
1492 * Begin to build info response packet based on prompts requested.
1493 * We commit to providing the correct number of responses, so if
1494 * further on we run into a problem that prevents this, we have to
1495 * be sure and clean this up and send a correct error response.
1497 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE
);
1498 packet_put_int(num_prompts
);
1500 debug2("input_userauth_info_req: num_prompts %d", num_prompts
);
1501 for (i
= 0; i
< num_prompts
; i
++) {
1502 prompt
= packet_get_string(NULL
);
1503 echo
= packet_get_char();
1505 response
= read_passphrase(prompt
, echo
? RP_ECHO
: 0);
1507 packet_put_cstring(response
);
1508 memset(response
, 0, strlen(response
));
1512 packet_check_eom(); /* done with parsing incoming message. */
1514 packet_add_padding(64);
1519 ssh_keysign(Key
*key
, u_char
**sigp
, u_int
*lenp
,
1520 u_char
*data
, u_int datalen
)
1525 int to
[2], from
[2], status
, version
= 2;
1527 debug2("ssh_keysign called");
1529 if (stat(_PATH_SSH_KEY_SIGN
, &st
) < 0) {
1530 error("ssh_keysign: no installed: %s", strerror(errno
));
1533 if (fflush(stdout
) != 0)
1534 error("ssh_keysign: fflush: %s", strerror(errno
));
1536 error("ssh_keysign: pipe: %s", strerror(errno
));
1539 if (pipe(from
) < 0) {
1540 error("ssh_keysign: pipe: %s", strerror(errno
));
1543 if ((pid
= fork()) < 0) {
1544 error("ssh_keysign: fork: %s", strerror(errno
));
1548 permanently_drop_suid(getuid());
1550 if (dup2(from
[1], STDOUT_FILENO
) < 0)
1551 fatal("ssh_keysign: dup2: %s", strerror(errno
));
1553 if (dup2(to
[0], STDIN_FILENO
) < 0)
1554 fatal("ssh_keysign: dup2: %s", strerror(errno
));
1557 execl(_PATH_SSH_KEY_SIGN
, _PATH_SSH_KEY_SIGN
, (char *) 0);
1558 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN
,
1565 buffer_put_int(&b
, packet_get_connection_in()); /* send # of socket */
1566 buffer_put_string(&b
, data
, datalen
);
1567 if (ssh_msg_send(to
[1], version
, &b
) == -1)
1568 fatal("ssh_keysign: couldn't send request");
1570 if (ssh_msg_recv(from
[0], &b
) < 0) {
1571 error("ssh_keysign: no reply");
1578 while (waitpid(pid
, &status
, 0) < 0)
1582 if (buffer_get_char(&b
) != version
) {
1583 error("ssh_keysign: bad version");
1587 *sigp
= buffer_get_string(&b
, lenp
);
1594 userauth_hostbased(Authctxt
*authctxt
)
1596 Key
*private = NULL
;
1597 Sensitive
*sensitive
= authctxt
->sensitive
;
1599 u_char
*signature
, *blob
;
1600 char *chost
, *pkalg
, *p
, myname
[NI_MAXHOST
];
1601 const char *service
;
1603 int ok
, i
, len
, found
= 0;
1605 /* check for a useful key */
1606 for (i
= 0; i
< sensitive
->nkeys
; i
++) {
1607 private = sensitive
->keys
[i
];
1608 if (private && private->type
!= KEY_RSA1
) {
1610 /* we take and free the key */
1611 sensitive
->keys
[i
] = NULL
;
1616 debug("No more client hostkeys for hostbased authentication.");
1619 if (key_to_blob(private, &blob
, &blen
) == 0) {
1623 /* figure out a name for the client host */
1625 if (packet_connection_is_on_socket())
1626 p
= get_local_name(packet_get_connection_in());
1628 if (gethostname(myname
, sizeof(myname
)) == -1) {
1629 verbose("userauth_hostbased: gethostname: %s",
1632 p
= xstrdup(myname
);
1635 error("userauth_hostbased: cannot get local ipaddr/name");
1640 len
= strlen(p
) + 2;
1641 xasprintf(&chost
, "%s.", p
);
1642 debug2("userauth_hostbased: chost %s", chost
);
1645 service
= datafellows
& SSH_BUG_HBSERVICE
? "ssh-userauth" :
1647 pkalg
= xstrdup(key_ssh_name(private));
1649 /* construct data */
1650 buffer_put_string(&b
, session_id2
, session_id2_len
);
1651 buffer_put_char(&b
, SSH2_MSG_USERAUTH_REQUEST
);
1652 buffer_put_cstring(&b
, authctxt
->server_user
);
1653 buffer_put_cstring(&b
, service
);
1654 buffer_put_cstring(&b
, authctxt
->method
->name
);
1655 buffer_put_cstring(&b
, pkalg
);
1656 buffer_put_string(&b
, blob
, blen
);
1657 buffer_put_cstring(&b
, chost
);
1658 buffer_put_cstring(&b
, authctxt
->local_user
);
1662 if (sensitive
->external_keysign
)
1663 ok
= ssh_keysign(private, &signature
, &slen
,
1664 buffer_ptr(&b
), buffer_len(&b
));
1666 ok
= key_sign(private, &signature
, &slen
,
1667 buffer_ptr(&b
), buffer_len(&b
));
1671 error("key_sign failed");
1677 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
1678 packet_put_cstring(authctxt
->server_user
);
1679 packet_put_cstring(authctxt
->service
);
1680 packet_put_cstring(authctxt
->method
->name
);
1681 packet_put_cstring(pkalg
);
1682 packet_put_string(blob
, blen
);
1683 packet_put_cstring(chost
);
1684 packet_put_cstring(authctxt
->local_user
);
1685 packet_put_string(signature
, slen
);
1686 memset(signature
, 's', slen
);
1698 userauth_jpake(Authctxt
*authctxt
)
1700 struct jpake_ctx
*pctx
;
1701 u_char
*x1_proof
, *x2_proof
;
1702 u_int x1_proof_len
, x2_proof_len
;
1703 static int attempt
= 0; /* XXX share with userauth_password's? */
1705 if (attempt
++ >= options
.number_of_password_prompts
)
1708 error("Permission denied, please try again.");
1710 if (authctxt
->methoddata
!= NULL
)
1711 fatal("%s: authctxt->methoddata already set (%p)",
1712 __func__
, authctxt
->methoddata
);
1714 authctxt
->methoddata
= pctx
= jpake_new();
1717 * Send request immediately, to get the protocol going while
1718 * we do the initial computations.
1720 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
1721 packet_put_cstring(authctxt
->server_user
);
1722 packet_put_cstring(authctxt
->service
);
1723 packet_put_cstring(authctxt
->method
->name
);
1725 packet_write_wait();
1727 jpake_step1(pctx
->grp
,
1728 &pctx
->client_id
, &pctx
->client_id_len
,
1729 &pctx
->x1
, &pctx
->x2
, &pctx
->g_x1
, &pctx
->g_x2
,
1730 &x1_proof
, &x1_proof_len
,
1731 &x2_proof
, &x2_proof_len
);
1733 JPAKE_DEBUG_CTX((pctx
, "step 1 sending in %s", __func__
));
1735 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP1
);
1736 packet_put_string(pctx
->client_id
, pctx
->client_id_len
);
1737 packet_put_bignum2(pctx
->g_x1
);
1738 packet_put_bignum2(pctx
->g_x2
);
1739 packet_put_string(x1_proof
, x1_proof_len
);
1740 packet_put_string(x2_proof
, x2_proof_len
);
1743 bzero(x1_proof
, x1_proof_len
);
1744 bzero(x2_proof
, x2_proof_len
);
1748 /* Expect step 1 packet from peer */
1749 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1
,
1750 input_userauth_jpake_server_step1
);
1756 userauth_jpake_cleanup(Authctxt
*authctxt
)
1758 debug3("%s: clean up", __func__
);
1759 if (authctxt
->methoddata
!= NULL
) {
1760 jpake_free(authctxt
->methoddata
);
1761 authctxt
->methoddata
= NULL
;
1768 ssh_krb5_helper(krb5_data
*ap
)
1770 krb5_context xcontext
= NULL
; /* XXX share with ssh1 */
1771 krb5_auth_context xauth_context
= NULL
;
1773 krb5_context
*context
;
1774 krb5_auth_context
*auth_context
;
1775 krb5_error_code problem
;
1778 krb5_ccache ccache
= NULL
;
1779 const char *remotehost
;
1782 memset(ap
, 0, sizeof(*ap
));
1784 context
= &xcontext
;
1785 auth_context
= &xauth_context
;
1787 problem
= krb5_init_context(context
);
1789 debug("Kerberos v5: krb5_init_context failed");
1794 tkfile
= krb5_cc_default_name(*context
);
1795 if (strncmp(tkfile
, "FILE:", 5) == 0)
1798 if (stat(tkfile
, &buf
) == 0 && getuid() != buf
.st_uid
) {
1799 debug("Kerberos v5: could not get default ccache (permission denied).");
1804 problem
= krb5_cc_default(*context
, &ccache
);
1806 debug("Kerberos v5: krb5_cc_default failed: %s",
1807 krb5_get_err_text(*context
, problem
));
1812 remotehost
= get_canonical_hostname(1);
1814 problem
= krb5_mk_req(*context
, auth_context
, AP_OPTS_MUTUAL_REQUIRED
,
1815 "host", remotehost
, NULL
, ccache
, ap
);
1817 debug("Kerberos v5: krb5_mk_req failed: %s",
1818 krb5_get_err_text(*context
, problem
));
1826 krb5_cc_close(*context
, ccache
);
1828 krb5_auth_con_free(*context
, *auth_context
);
1830 krb5_free_context(*context
);
1835 userauth_kerberos(Authctxt
*authctxt
)
1839 if (ssh_krb5_helper(&ap
) == 0)
1842 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
1843 packet_put_cstring(authctxt
->server_user
);
1844 packet_put_cstring(authctxt
->service
);
1845 packet_put_cstring(authctxt
->method
->name
);
1846 packet_put_string(ap
.data
, ap
.length
);
1849 krb5_data_free(&ap
);
1854 /* find auth method */
1857 * given auth method name, if configurable options permit this method fill
1858 * in auth_ident field and return true, otherwise return false.
1861 authmethod_is_enabled(Authmethod
*method
)
1865 /* return false if options indicate this method is disabled */
1866 if (method
->enabled
== NULL
|| *method
->enabled
== 0)
1868 /* return false if batch mode is enabled but method needs interactive mode */
1869 if (method
->batch_flag
!= NULL
&& *method
->batch_flag
!= 0)
1875 authmethod_lookup(const char *name
)
1877 Authmethod
*method
= NULL
;
1879 for (method
= authmethods
; method
->name
!= NULL
; method
++)
1880 if (strcmp(name
, method
->name
) == 0)
1882 debug2("Unrecognized authentication method name: %s", name
? name
: "NULL");
1886 /* XXX internal state */
1887 static Authmethod
*current
= NULL
;
1888 static char *supported
= NULL
;
1889 static char *preferred
= NULL
;
1892 * Given the authentication method list sent by the server, return the
1893 * next method we should try. If the server initially sends a nil list,
1894 * use a built-in default list.
1897 authmethod_get(char *authlist
)
1902 /* Use a suitable default if we're passed a nil list. */
1903 if (authlist
== NULL
|| strlen(authlist
) == 0)
1904 authlist
= options
.preferred_authentications
;
1906 if (supported
== NULL
|| strcmp(authlist
, supported
) != 0) {
1907 debug3("start over, passed a different list %s", authlist
);
1908 if (supported
!= NULL
)
1910 supported
= xstrdup(authlist
);
1911 preferred
= options
.preferred_authentications
;
1912 debug3("preferred %s", preferred
);
1914 } else if (current
!= NULL
&& authmethod_is_enabled(current
))
1918 if ((name
= match_list(preferred
, supported
, &next
)) == NULL
) {
1919 debug("No more authentication methods to try.");
1924 debug3("authmethod_lookup %s", name
);
1925 debug3("remaining preferred: %s", preferred
);
1926 if ((current
= authmethod_lookup(name
)) != NULL
&&
1927 authmethod_is_enabled(current
)) {
1928 debug3("authmethod_is_enabled %s", name
);
1929 debug("Next authentication method: %s", name
);
1936 authmethods_get(void)
1938 Authmethod
*method
= NULL
;
1943 for (method
= authmethods
; method
->name
!= NULL
; method
++) {
1944 if (authmethod_is_enabled(method
)) {
1945 if (buffer_len(&b
) > 0)
1946 buffer_append(&b
, ",", 1);
1947 buffer_append(&b
, method
->name
, strlen(method
->name
));
1950 buffer_append(&b
, "\0", 1);
1951 list
= xstrdup(buffer_ptr(&b
));