5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2006 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
21 #include "silcclient.h"
22 #include "client_internal.h"
24 /************************ Static utility functions **************************/
26 /* Callback called after connected to remote host */
28 static void silc_client_connect_callback(SilcNetStatus status,
29 SilcStream stream, void *context)
31 SilcFSMThread fsm = context;
32 SilcClientConnection conn = silc_fsm_get_context(fsm);
33 SilcClient client = conn->client;
35 conn->internal->op = NULL;
36 if (conn->internal->verbose) {
40 case SILC_NET_UNKNOWN_IP:
41 client->internal->ops->say(
42 client, conn, SILC_CLIENT_MESSAGE_ERROR,
43 "Could not connect to host %s: unknown IP address",
46 case SILC_NET_UNKNOWN_HOST:
47 client->internal->ops->say(
48 client, conn, SILC_CLIENT_MESSAGE_ERROR,
49 "Could not connect to host %s: unknown host name",
52 case SILC_NET_HOST_UNREACHABLE:
53 client->internal->ops->say(
54 client, conn, SILC_CLIENT_MESSAGE_ERROR,
55 "Could not connect to host %s: network unreachable",
58 case SILC_NET_CONNECTION_REFUSED:
59 client->internal->ops->say(
60 client, conn, SILC_CLIENT_MESSAGE_ERROR,
61 "Could not connect to host %s: connection refused",
64 case SILC_NET_CONNECTION_TIMEOUT:
65 client->internal->ops->say(
66 client, conn, SILC_CLIENT_MESSAGE_ERROR,
67 "Could not connect to host %s: connection timeout",
71 client->internal->ops->say(
72 client, conn, SILC_CLIENT_MESSAGE_ERROR,
73 "Could not connect to host %s",
79 if (status != SILC_NET_OK) {
80 /* Notify application of failure */
81 SILC_LOG_DEBUG(("Connecting failed"));
82 conn->callback(client, conn, SILC_CLIENT_CONN_ERROR, 0,
83 NULL, conn->callback_context);
84 silc_fsm_next(fsm, silc_client_st_connect_error);
85 SILC_FSM_CALL_CONTINUE(fsm);
89 /* Connection created successfully */
90 SILC_LOG_DEBUG(("Connected"));
91 conn->stream = (void *)stream;
92 SILC_FSM_CALL_CONTINUE(fsm);
95 /* Called after application has verified remote host's public key */
97 static void silc_client_ke_verify_key_cb(SilcBool success, void *context)
99 VerifyKeyContext verify = (VerifyKeyContext)context;
101 SILC_LOG_DEBUG(("Start"));
103 /* Call the completion callback back to the SKE */
104 verify->completion(verify->ske, success ? SILC_SKE_STATUS_OK :
105 SILC_SKE_STATUS_UNSUPPORTED_PUBLIC_KEY,
106 verify->completion_context);
111 /* Verify remote host's public key */
113 static void silc_client_ke_verify_key(SilcSKE ske,
114 SilcPublicKey public_key,
116 SilcSKEVerifyCbCompletion completion,
117 void *completion_context)
119 SilcFSMThread fsm = context;
120 SilcClientConnection conn = silc_fsm_get_context(fsm);
121 SilcClient client = conn->client;
122 VerifyKeyContext verify;
124 /* If we provided repository for SKE and we got here the key was not
125 found from the repository. */
126 if (conn->internal->params.repository &&
127 !conn->internal->params.verify_notfound) {
128 completion(ske, SILC_SKE_STATUS_UNSUPPORTED_PUBLIC_KEY,
133 SILC_LOG_DEBUG(("Verify remote public key"));
135 verify = silc_calloc(1, sizeof(*verify));
137 completion(ske, SILC_SKE_STATUS_UNSUPPORTED_PUBLIC_KEY,
142 verify->completion = completion;
143 verify->completion_context = completion_context;
145 /* Verify public key in application */
146 client->internal->ops->verify_public_key(client, conn,
147 conn->type, public_key,
148 silc_client_ke_verify_key_cb,
152 /* Key exchange protocol completion callback */
154 static void silc_client_ke_completion(SilcSKE ske,
155 SilcSKEStatus status,
156 SilcSKESecurityProperties prop,
157 SilcSKEKeyMaterial keymat,
158 SilcSKERekeyMaterial rekey,
161 SilcFSMThread fsm = context;
162 SilcClientConnection conn = silc_fsm_get_context(fsm);
163 SilcClient client = conn->client;
164 SilcCipher send_key, receive_key;
165 SilcHmac hmac_send, hmac_receive;
167 conn->internal->op = NULL;
168 if (status != SILC_SKE_STATUS_OK) {
169 /* Key exchange failed */
170 SILC_LOG_DEBUG(("Error during key exchange with %s: %s (%d)",
171 conn->remote_host, silc_ske_map_status(status), status));
173 if (conn->internal->verbose)
174 client->internal->ops->say(client, conn, SILC_CLIENT_MESSAGE_ERROR,
175 "Error during key exchange with %s: %s",
177 silc_ske_map_status(status));
179 silc_ske_free_rekey_material(rekey);
181 silc_fsm_next(fsm, silc_client_st_connect_error);
182 SILC_FSM_CALL_CONTINUE(fsm);
186 SILC_LOG_DEBUG(("Setting keys into use"));
188 /* Allocate the cipher and HMAC contexts */
189 if (!silc_ske_set_keys(ske, keymat, prop, &send_key, &receive_key,
190 &hmac_send, &hmac_receive, &conn->internal->hash)) {
191 /* Error setting keys */
192 SILC_LOG_DEBUG(("Could not set keys into use"));
194 if (conn->internal->verbose)
195 client->internal->ops->say(
196 client, conn, SILC_CLIENT_MESSAGE_ERROR,
197 "Error during key exchange with %s: cannot use keys",
200 conn->callback(client, conn, SILC_CLIENT_CONN_ERROR_KE, 0, NULL,
201 conn->callback_context);
203 silc_ske_free_rekey_material(rekey);
205 silc_fsm_next(fsm, silc_client_st_connect_error);
206 SILC_FSM_CALL_CONTINUE(fsm);
210 /* Set the keys into the packet stream. After this call packets will be
211 encrypted with these keys. */
212 if (!silc_packet_set_keys(conn->stream, send_key, receive_key, hmac_send,
213 hmac_receive, FALSE)) {
214 /* Error setting keys */
215 SILC_LOG_DEBUG(("Could not set keys into use"));
217 if (conn->internal->verbose)
218 client->internal->ops->say(
219 client, conn, SILC_CLIENT_MESSAGE_ERROR,
220 "Error during key exchange with %s: cannot use keys",
223 conn->callback(client, conn, SILC_CLIENT_CONN_ERROR_KE, 0, NULL,
224 conn->callback_context);
226 silc_ske_free_rekey_material(rekey);
228 silc_fsm_next(fsm, silc_client_st_connect_error);
229 SILC_FSM_CALL_CONTINUE(fsm);
233 conn->internal->rekey = rekey;
235 SILC_LOG_DEBUG(("Key Exchange completed"));
237 /* Key exchange done */
238 SILC_FSM_CALL_CONTINUE(fsm);
241 /* Rekey protocol completion callback */
243 static void silc_client_rekey_completion(SilcSKE ske,
244 SilcSKEStatus status,
245 SilcSKESecurityProperties prop,
246 SilcSKEKeyMaterial keymat,
247 SilcSKERekeyMaterial rekey,
250 SilcFSMThread fsm = context;
251 SilcClientConnection conn = silc_fsm_get_context(fsm);
252 SilcClient client = conn->client;
254 conn->internal->op = NULL;
255 if (status != SILC_SKE_STATUS_OK) {
257 SILC_LOG_DEBUG(("Error during rekey with %s: %s (%d)",
258 conn->remote_host, silc_ske_map_status(status), status));
260 if (conn->internal->verbose)
261 client->internal->ops->say(client, conn, SILC_CLIENT_MESSAGE_ERROR,
262 "Error during rekey with %s: %s",
264 silc_ske_map_status(status));
266 silc_fsm_finish(fsm);
270 silc_ske_free_rekey_material(conn->internal->rekey);
271 conn->internal->rekey = rekey;
273 SILC_LOG_DEBUG(("Rekey completed"));
276 silc_fsm_finish(fsm);
279 /* Callback called by application to return authentication data */
281 static void silc_client_connect_auth_method(SilcBool success,
282 SilcAuthMethod auth_meth,
283 void *auth, SilcUInt32 auth_len,
286 SilcFSMThread fsm = context;
287 SilcClientConnection conn = silc_fsm_get_context(fsm);
289 conn->internal->params.auth_method = SILC_AUTH_NONE;
292 conn->internal->params.auth_method = auth_meth;
293 conn->internal->params.auth = auth;
294 conn->internal->params.auth_len = auth_len;
297 SILC_FSM_CALL_CONTINUE(fsm);
300 /* Connection authentication completion callback */
302 static void silc_client_connect_auth_completion(SilcConnAuth connauth,
306 SilcFSMThread fsm = context;
307 SilcClientConnection conn = silc_fsm_get_context(fsm);
308 SilcClient client = conn->client;
310 silc_connauth_free(connauth);
313 if (conn->internal->verbose)
314 client->internal->ops->say(
315 client, conn, SILC_CLIENT_MESSAGE_ERROR,
316 "Authentication failed");
318 conn->callback(client, conn, SILC_CLIENT_CONN_ERROR_AUTH, 0, NULL,
319 conn->callback_context);
320 silc_fsm_next(fsm, silc_client_st_connect_error);
323 SILC_FSM_CALL_CONTINUE(fsm);
326 /*************************** Connect remote host ****************************/
328 /* Connection timeout callback */
330 SILC_TASK_CALLBACK(silc_client_connect_timeout)
332 SilcClientConnection conn = context;
333 SilcClient client = conn->client;
335 SILC_LOG_DEBUG(("Connection timeout"));
337 conn->callback(client, conn, SILC_CLIENT_CONN_ERROR_TIMEOUT, 0, NULL,
338 conn->callback_context);
340 silc_fsm_next(&conn->internal->event_thread, silc_client_st_connect_error);
341 silc_fsm_continue_sync(&conn->internal->event_thread);
344 /* Creates a connection to remote host */
346 SILC_FSM_STATE(silc_client_st_connect)
348 SilcClientConnection conn = fsm_context;
349 SilcClient client = conn->client;
351 SILC_LOG_DEBUG(("Connecting to %s:%d", conn->remote_host,
355 silc_fsm_next(fsm, silc_client_st_connect_set_stream);
357 /* Add connection timeout */
358 if (conn->internal->params.timeout_secs)
359 silc_schedule_task_add_timeout(conn->internal->schedule,
360 silc_client_connect_timeout, conn,
361 conn->internal->params.timeout_secs, 0);
363 if (conn->internal->params.udp) {
366 if (!conn->internal->params.local_ip) {
367 /** IP address not given */
368 SILC_LOG_ERROR(("Local UDP IP address not specified"));
369 conn->callback(client, conn, SILC_CLIENT_CONN_ERROR, 0, NULL,
370 conn->callback_context);
371 silc_fsm_next(fsm, silc_client_st_connect_error);
372 return SILC_FSM_CONTINUE;
376 stream = silc_net_udp_connect(conn->internal->params.bind_ip ?
377 conn->internal->params.bind_ip :
378 conn->internal->params.local_ip,
379 conn->internal->params.local_port,
380 conn->remote_host, conn->remote_port,
381 conn->internal->schedule);
383 SILC_FSM_CALL(silc_client_connect_callback(stream ? SILC_NET_OK :
384 SILC_NET_HOST_UNREACHABLE,
388 SILC_FSM_CALL(conn->internal->op = silc_net_tcp_connect(
389 NULL, conn->remote_host,
391 conn->internal->schedule,
392 silc_client_connect_callback, fsm));
396 /* Sets the new connection stream into use and creates packet stream */
398 SILC_FSM_STATE(silc_client_st_connect_set_stream)
400 SilcClientConnection conn = fsm_context;
401 SilcClient client = conn->client;
403 /* Create packet stream */
404 conn->stream = silc_packet_stream_create(client->internal->packet_engine,
405 conn->internal->schedule,
406 (SilcStream)conn->stream);
408 /** Cannot create packet stream */
409 SILC_LOG_DEBUG(("Could not create packet stream"));
410 conn->callback(client, conn, SILC_CLIENT_CONN_ERROR, 0, NULL,
411 conn->callback_context);
412 silc_fsm_next(fsm, silc_client_st_connect_error);
413 return SILC_FSM_CONTINUE;
416 silc_packet_set_context(conn->stream, conn);
418 /** Start key exchange */
419 silc_fsm_next(fsm, silc_client_st_connect_key_exchange);
420 return SILC_FSM_CONTINUE;
423 /* Starts key exchange protocol with remote host */
425 SILC_FSM_STATE(silc_client_st_connect_key_exchange)
427 SilcClientConnection conn = fsm_context;
428 SilcClient client = conn->client;
429 SilcSKEParamsStruct params;
431 SILC_LOG_DEBUG(("Starting key exchange protocol"));
434 conn->internal->ske =
435 silc_ske_alloc(client->rng, conn->internal->schedule,
436 conn->internal->params.repository,
437 conn->public_key, conn->private_key, fsm);
438 if (!conn->internal->ske) {
440 conn->callback(client, conn, SILC_CLIENT_CONN_ERROR_KE, 0, NULL,
441 conn->callback_context);
442 silc_fsm_next(fsm, silc_client_st_connect_error);
443 return SILC_FSM_CONTINUE;
446 /* Set SKE callbacks */
447 silc_ske_set_callbacks(conn->internal->ske, silc_client_ke_verify_key,
448 silc_client_ke_completion, fsm);
450 /* Set up key exchange parameters */
451 params.version = client->internal->silc_client_version;
452 params.flags = SILC_SKE_SP_FLAG_MUTUAL;
453 if (conn->internal->params.pfs)
454 params.flags |= SILC_SKE_SP_FLAG_PFS;
455 if (conn->internal->params.udp) {
456 params.flags |= SILC_SKE_SP_FLAG_IV_INCLUDED;
457 params.session_port = conn->internal->params.local_port;
460 if (conn->internal->params.no_authentication)
461 /** Run key exchange (no auth) */
462 silc_fsm_next(fsm, silc_client_st_connected);
463 else if (conn->internal->params.udp)
464 /** Run key exchange (UDP)*/
465 silc_fsm_next(fsm, silc_client_st_connect_setup_udp);
467 /** Run key exchange (TCP) */
468 silc_fsm_next(fsm, silc_client_st_connect_auth);
470 SILC_FSM_CALL(conn->internal->op = silc_ske_initiator(conn->internal->ske,
475 /* For UDP/IP connections, set up the UDP session after successful key
478 SILC_FSM_STATE(silc_client_st_connect_setup_udp)
480 SilcClientConnection conn = fsm_context;
481 SilcClient client = conn->client;
482 SilcStream stream, old;
483 SilcSKESecurityProperties prop;
485 SILC_LOG_DEBUG(("Setup UDP SILC session"));
487 /* Create new UDP stream */
488 prop = silc_ske_get_security_properties(conn->internal->ske);
489 stream = silc_net_udp_connect(conn->internal->params.local_ip,
490 conn->internal->params.local_port,
491 conn->remote_host, prop->remote_port,
492 conn->internal->schedule);
494 /** Cannot create UDP stream */
495 conn->callback(client, conn, SILC_CLIENT_CONN_ERROR, 0, NULL,
496 conn->callback_context);
497 silc_fsm_next(fsm, silc_client_st_connect_error);
498 return SILC_FSM_CONTINUE;
501 /* Set the new stream to packet stream */
502 old = silc_packet_stream_get_stream(conn->stream);
503 silc_packet_stream_set_stream(conn->stream, stream,
504 conn->internal->schedule);
505 silc_packet_stream_set_iv_included(conn->stream);
506 silc_packet_set_sid(conn->stream, 0);
508 /* Delete the old stream */
509 silc_stream_destroy(old);
511 /** Start authentication */
512 silc_fsm_next(fsm, silc_client_st_connect_auth);
513 return SILC_FSM_CONTINUE;
516 /* Get authentication method to be used in authentication protocol */
518 SILC_FSM_STATE(silc_client_st_connect_auth)
520 SilcClientConnection conn = fsm_context;
521 SilcClient client = conn->client;
523 SILC_LOG_DEBUG(("Get authentication data"));
525 silc_fsm_next(fsm, silc_client_st_connect_auth_start);
527 /* If authentication data not provided, ask from application */
528 if (!conn->internal->params.auth_set)
529 SILC_FSM_CALL(client->internal->ops->get_auth_method(
533 silc_client_connect_auth_method, fsm));
535 if (conn->internal->params.auth_method == SILC_AUTH_PUBLIC_KEY)
536 conn->internal->params.auth = conn->private_key;
538 /* We have authentication data */
539 return SILC_FSM_CONTINUE;
542 /* Start connection authentication with remote host */
544 SILC_FSM_STATE(silc_client_st_connect_auth_start)
546 SilcClientConnection conn = fsm_context;
547 SilcClient client = conn->client;
548 SilcConnAuth connauth;
550 SILC_LOG_DEBUG(("Starting connection authentication protocol"));
552 /* Allocate connection authentication protocol */
553 connauth = silc_connauth_alloc(conn->internal->schedule,
555 conn->internal->params.rekey_secs);
558 conn->callback(client, conn, SILC_CLIENT_CONN_ERROR_AUTH, 0, NULL,
559 conn->callback_context);
560 silc_fsm_next(fsm, silc_client_st_connect_error);
561 return SILC_FSM_CONTINUE;
564 /** Start connection authentication */
565 silc_fsm_next(fsm, silc_client_st_connected);
566 SILC_FSM_CALL(conn->internal->op = silc_connauth_initiator(
567 connauth, SILC_CONN_CLIENT,
568 conn->internal->params.auth_method,
569 conn->internal->params.auth,
570 conn->internal->params.auth_len,
571 silc_client_connect_auth_completion,
575 /* Connection fully established */
577 SILC_FSM_STATE(silc_client_st_connected)
579 SilcClientConnection conn = fsm_context;
580 SilcClient client = conn->client;
582 SILC_LOG_DEBUG(("Connection established"));
584 silc_ske_free(conn->internal->ske);
585 conn->internal->ske = NULL;
587 /* Install rekey timer */
588 silc_schedule_task_add_timeout(conn->internal->schedule,
589 silc_client_rekey_timer, conn,
590 conn->internal->params.rekey_secs, 0);
592 /* If we connected to server, now register to network. */
593 if (conn->type == SILC_CONN_SERVER &&
594 !conn->internal->params.no_authentication) {
596 /* If detach data is provided, resume the session. */
597 if (conn->internal->params.detach_data &&
598 conn->internal->params.detach_data_len) {
599 /** Resume detached session */
600 silc_fsm_next(fsm, silc_client_st_resume);
602 /** Register to network */
603 silc_fsm_next(fsm, silc_client_st_register);
606 return SILC_FSM_CONTINUE;
609 silc_schedule_task_del_by_all(conn->internal->schedule, 0,
610 silc_client_connect_timeout, conn);
612 /* Call connection callback */
613 conn->callback(client, conn, SILC_CLIENT_CONN_SUCCESS, 0, NULL,
614 conn->callback_context);
616 return SILC_FSM_FINISH;
619 /* Error during connecting */
621 SILC_FSM_STATE(silc_client_st_connect_error)
623 SilcClientConnection conn = fsm_context;
625 if (conn->internal->ske) {
626 silc_ske_free(conn->internal->ske);
627 conn->internal->ske = NULL;
630 /* Signal to close connection */
631 if (!conn->internal->disconnected) {
632 conn->internal->disconnected = TRUE;
633 SILC_FSM_SEMA_POST(&conn->internal->wait_event);
636 silc_schedule_task_del_by_all(conn->internal->schedule, 0,
637 silc_client_connect_timeout, conn);
639 return SILC_FSM_FINISH;
642 /****************************** Connect rekey *******************************/
644 /* Connection rekey timer callback */
646 SILC_TASK_CALLBACK(silc_client_rekey_timer)
648 SilcClientConnection conn = context;
650 /* Signal to start rekey */
651 conn->internal->rekeying = TRUE;
652 SILC_FSM_SEMA_POST(&conn->internal->wait_event);
654 /* Reinstall rekey timer */
655 silc_schedule_task_add_timeout(conn->internal->schedule,
656 silc_client_rekey_timer, conn,
657 conn->internal->params.rekey_secs, 0);
662 SILC_FSM_STATE(silc_client_st_rekey)
664 SilcClientConnection conn = fsm_context;
665 SilcClient client = conn->client;
667 SILC_LOG_DEBUG(("Rekey"));
670 conn->internal->ske =
671 silc_ske_alloc(client->rng, conn->internal->schedule,
672 conn->internal->params.repository,
673 conn->public_key, conn->private_key, fsm);
674 if (!conn->internal->ske)
675 return SILC_FSM_FINISH;
677 /* Set SKE callbacks */
678 silc_ske_set_callbacks(conn->internal->ske, NULL,
679 silc_client_rekey_completion, fsm);
682 if (!conn->internal->rekey_responder)
683 SILC_FSM_CALL(conn->internal->op = silc_ske_rekey_initiator(
686 conn->internal->rekey));
688 SILC_FSM_CALL(conn->internal->op = silc_ske_rekey_responder(
691 conn->internal->rekey));