145f398f1a3916d9b9dd72a9a9ec2f579c9aa931
[silc.git] / lib / silcclient / client_connect.c
1 /*
2
3   client_st_connect.c
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2006 - 2007 Pekka Riikonen
8
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.
12
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.
17
18 */
19
20 #include "silc.h"
21 #include "silcclient.h"
22 #include "client_internal.h"
23
24 /************************ Static utility functions **************************/
25
26 /* Callback called after connected to remote host */
27
28 static void silc_client_connect_callback(SilcNetStatus status,
29                                          SilcStream stream, void *context)
30 {
31   SilcFSMThread fsm = context;
32   SilcClientConnection conn = silc_fsm_get_context(fsm);
33   SilcClient client = conn->client;
34
35   conn->internal->op = NULL;
36   if (conn->internal->verbose) {
37     switch (status) {
38     case SILC_NET_OK:
39       break;
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",
44                    conn->remote_host);
45       break;
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",
50                    conn->remote_host);
51       break;
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",
56                    conn->remote_host);
57       break;
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",
62                    conn->remote_host);
63       break;
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",
68                    conn->remote_host);
69       break;
70     default:
71       client->internal->ops->say(
72                    client, conn, SILC_CLIENT_MESSAGE_ERROR,
73                    "Could not connect to host %s",
74                    conn->remote_host);
75       break;
76     }
77   }
78
79   if (status != SILC_NET_OK) {
80     /* Notify application of failure */
81     SILC_LOG_DEBUG(("Connecting failed"));
82     conn->internal->status = SILC_CLIENT_CONN_ERROR;
83     silc_fsm_next(fsm, silc_client_st_connect_error);
84     SILC_FSM_CALL_CONTINUE(fsm);
85     return;
86   }
87
88   /* Connection created successfully */
89   SILC_LOG_DEBUG(("Connected"));
90   conn->internal->user_stream = stream;
91   SILC_FSM_CALL_CONTINUE(fsm);
92 }
93
94 /* Called after application has verified remote host's public key */
95
96 static void silc_client_ke_verify_key_cb(SilcBool success, void *context)
97 {
98   SilcVerifyKeyContext verify = context;
99
100   SILC_LOG_DEBUG(("Start"));
101
102   /* Call the completion callback back to the SKE */
103   verify->completion(verify->ske, success ? SILC_SKE_STATUS_OK :
104                      SILC_SKE_STATUS_UNSUPPORTED_PUBLIC_KEY,
105                      verify->completion_context);
106
107   silc_free(verify);
108 }
109
110 /* Verify remote host's public key */
111
112 static void silc_client_ke_verify_key(SilcSKE ske,
113                                       SilcPublicKey public_key,
114                                       void *context,
115                                       SilcSKEVerifyCbCompletion completion,
116                                       void *completion_context)
117 {
118   SilcFSMThread fsm = context;
119   SilcClientConnection conn = silc_fsm_get_context(fsm);
120   SilcClient client = conn->client;
121   SilcVerifyKeyContext verify;
122
123   /* If we provided repository for SKE and we got here the key was not
124      found from the repository. */
125   if (conn->internal->params.repository &&
126       !conn->internal->params.verify_notfound) {
127     completion(ske, SILC_SKE_STATUS_UNSUPPORTED_PUBLIC_KEY,
128                completion_context);
129     return;
130   }
131
132   SILC_LOG_DEBUG(("Verify remote public key"));
133
134   verify = silc_calloc(1, sizeof(*verify));
135   if (!verify) {
136     completion(ske, SILC_SKE_STATUS_UNSUPPORTED_PUBLIC_KEY,
137                completion_context);
138     return;
139   }
140   verify->ske = ske;
141   verify->completion = completion;
142   verify->completion_context = completion_context;
143
144   /* Verify public key in application */
145   client->internal->ops->verify_public_key(client, conn,
146                                            conn->type, public_key,
147                                            silc_client_ke_verify_key_cb,
148                                            verify);
149 }
150
151 /* Key exchange protocol completion callback */
152
153 static void silc_client_ke_completion(SilcSKE ske,
154                                       SilcSKEStatus status,
155                                       SilcSKESecurityProperties prop,
156                                       SilcSKEKeyMaterial keymat,
157                                       SilcSKERekeyMaterial rekey,
158                                       void *context)
159 {
160   SilcFSMThread fsm = context;
161   SilcClientConnection conn = silc_fsm_get_context(fsm);
162   SilcClient client = conn->client;
163   SilcCipher send_key, receive_key;
164   SilcHmac hmac_send, hmac_receive;
165
166   conn->internal->op = NULL;
167   if (status != SILC_SKE_STATUS_OK) {
168     /* Key exchange failed */
169     SILC_LOG_DEBUG(("Error during key exchange with %s: %s (%d)",
170                     conn->remote_host, silc_ske_map_status(status), status));
171
172     if (conn->internal->verbose)
173       client->internal->ops->say(client, conn, SILC_CLIENT_MESSAGE_ERROR,
174                                  "Error during key exchange with %s: %s",
175                                  conn->remote_host,
176                                  silc_ske_map_status(status));
177
178     conn->internal->status = SILC_CLIENT_CONN_ERROR_KE;
179     silc_ske_free_rekey_material(rekey);
180
181     silc_fsm_next(fsm, silc_client_st_connect_error);
182     SILC_FSM_CALL_CONTINUE_SYNC(fsm);
183     return;
184   }
185
186   SILC_LOG_DEBUG(("Setting keys into use"));
187
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"));
193
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",
198                        conn->remote_host);
199
200     conn->internal->status = SILC_CLIENT_CONN_ERROR_KE;
201     silc_ske_free_rekey_material(rekey);
202
203     silc_fsm_next(fsm, silc_client_st_connect_error);
204     SILC_FSM_CALL_CONTINUE_SYNC(fsm);
205     return;
206   }
207
208   /* Set the keys into the packet stream.  After this call packets will be
209      encrypted with these keys. */
210   if (!silc_packet_set_keys(conn->stream, send_key, receive_key, hmac_send,
211                             hmac_receive, FALSE)) {
212     /* Error setting keys */
213     SILC_LOG_DEBUG(("Could not set keys into use"));
214
215     if (conn->internal->verbose)
216       client->internal->ops->say(
217                        client, conn, SILC_CLIENT_MESSAGE_ERROR,
218                        "Error during key exchange with %s: cannot use keys",
219                        conn->remote_host);
220
221     conn->internal->status = SILC_CLIENT_CONN_ERROR_KE;
222     silc_ske_free_rekey_material(rekey);
223
224     silc_fsm_next(fsm, silc_client_st_connect_error);
225     SILC_FSM_CALL_CONTINUE_SYNC(fsm);
226     return;
227   }
228
229   conn->internal->rekey = rekey;
230
231   SILC_LOG_DEBUG(("Key Exchange completed"));
232
233   /* Key exchange done */
234   SILC_FSM_CALL_CONTINUE_SYNC(fsm);
235 }
236
237 /* Rekey protocol completion callback */
238
239 static void silc_client_rekey_completion(SilcSKE ske,
240                                          SilcSKEStatus status,
241                                          SilcSKESecurityProperties prop,
242                                          SilcSKEKeyMaterial keymat,
243                                          SilcSKERekeyMaterial rekey,
244                                          void *context)
245 {
246   SilcFSMThread fsm = context;
247   SilcClientConnection conn = silc_fsm_get_context(fsm);
248   SilcClient client = conn->client;
249
250   conn->internal->op = NULL;
251   if (status != SILC_SKE_STATUS_OK) {
252     /* Rekey failed */
253     SILC_LOG_DEBUG(("Error during rekey with %s: %s (%d)",
254                     conn->remote_host, silc_ske_map_status(status), status));
255
256     if (conn->internal->verbose)
257       client->internal->ops->say(client, conn, SILC_CLIENT_MESSAGE_ERROR,
258                                  "Error during rekey with %s: %s",
259                                  conn->remote_host,
260                                  silc_ske_map_status(status));
261
262     silc_ske_free(conn->internal->ske);
263     conn->internal->ske = NULL;
264     silc_fsm_finish(fsm);
265
266     /* Close connection */
267     silc_client_close_connection(client, conn);
268     return;
269   }
270
271   silc_ske_free_rekey_material(conn->internal->rekey);
272   conn->internal->rekey = rekey;
273
274   silc_ske_free(conn->internal->ske);
275   conn->internal->ske = NULL;
276
277   SILC_LOG_DEBUG(("Rekey completed conn %p", conn));
278
279   /* Rekey done */
280   silc_fsm_finish(fsm);
281 }
282
283 /* Callback called by application to return authentication data */
284
285 static void silc_client_connect_auth_method(SilcAuthMethod auth_meth,
286                                             const void *auth,
287                                             SilcUInt32 auth_len,
288                                             void *context)
289 {
290   SilcFSMThread fsm = context;
291   SilcClientConnection conn = silc_fsm_get_context(fsm);
292
293   conn->internal->params.auth_method = auth_meth;
294   if (auth_meth == SILC_AUTH_PASSWORD)
295     conn->internal->params.auth = silc_memdup(auth, auth_len);
296   else
297     conn->internal->params.auth = (void *)auth;
298   conn->internal->params.auth_len = auth_len;
299
300   SILC_FSM_CALL_CONTINUE(fsm);
301 }
302
303 /* Connection authentication completion callback */
304
305 static void silc_client_connect_auth_completion(SilcConnAuth connauth,
306                                                 SilcBool success,
307                                                 void *context)
308 {
309   SilcFSMThread fsm = context;
310   SilcClientConnection conn = silc_fsm_get_context(fsm);
311   SilcClient client = conn->client;
312
313   conn->internal->op = NULL;
314   silc_connauth_free(connauth);
315
316   if (!success) {
317     if (conn->internal->verbose)
318         client->internal->ops->say(
319                         client, conn, SILC_CLIENT_MESSAGE_ERROR,
320                         "Authentication failed");
321
322     conn->internal->status = SILC_CLIENT_CONN_ERROR_AUTH;
323     conn->internal->error = SILC_STATUS_ERR_AUTH_FAILED;
324     silc_fsm_next(fsm, silc_client_st_connect_error);
325   }
326
327   SILC_FSM_CALL_CONTINUE_SYNC(fsm);
328 }
329
330 /********************** CONNECTION_AUTH_REQUEST packet **********************/
331
332 /* Received connection authentication request packet.  We get the
333    required authentication method here. */
334
335 SILC_FSM_STATE(silc_client_connect_auth_request)
336 {
337   SilcClientConnection conn = fsm_context;
338   SilcPacket packet = state_context;
339   SilcUInt16 conn_type, auth_meth;
340
341   if (!conn->internal->auth_request) {
342     silc_packet_free(packet);
343     return SILC_FSM_FINISH;
344   }
345
346   /* Parse the payload */
347   if (silc_buffer_unformat(&packet->buffer,
348                            SILC_STR_UI_SHORT(&conn_type),
349                            SILC_STR_UI_SHORT(&auth_meth),
350                            SILC_STR_END) < 0)
351     auth_meth = SILC_AUTH_NONE;
352
353   silc_packet_free(packet);
354
355   SILC_LOG_DEBUG(("Resolved authentication method: %s",
356                   (auth_meth == SILC_AUTH_NONE ? "none" :
357                    auth_meth == SILC_AUTH_PASSWORD ? "passphrase" :
358                    "public key")));
359   conn->internal->params.auth_method = auth_meth;
360
361   /* Continue authentication */
362   silc_fsm_continue_sync(&conn->internal->event_thread);
363   return SILC_FSM_FINISH;
364 }
365
366 /*************************** Connect remote host ****************************/
367
368 /* Connection timeout callback */
369
370 SILC_TASK_CALLBACK(silc_client_connect_timeout)
371 {
372   SilcClientConnection conn = context;
373
374   SILC_LOG_DEBUG(("Connection timeout"));
375
376   conn->internal->status = SILC_CLIENT_CONN_ERROR_TIMEOUT;
377   conn->internal->error = SILC_STATUS_ERR_TIMEDOUT;
378
379   silc_fsm_next(&conn->internal->event_thread, silc_client_st_connect_error);
380   silc_fsm_continue_sync(&conn->internal->event_thread);
381 }
382
383 /* Creates a connection to remote host */
384
385 SILC_FSM_STATE(silc_client_st_connect)
386 {
387   SilcClientConnection conn = fsm_context;
388
389   SILC_LOG_DEBUG(("Connecting to %s:%d", conn->remote_host,
390                   conn->remote_port));
391
392   /** Connect */
393   silc_fsm_next(fsm, silc_client_st_connect_set_stream);
394
395   /* Add connection timeout */
396   if (conn->internal->params.timeout_secs)
397     silc_schedule_task_add_timeout(conn->internal->schedule,
398                                    silc_client_connect_timeout, conn,
399                                    conn->internal->params.timeout_secs, 0);
400
401   if (conn->internal->params.udp) {
402     SilcStream stream;
403
404     if (!conn->internal->params.local_ip) {
405       /** IP address not given */
406       SILC_LOG_ERROR(("Local UDP IP address not specified"));
407       conn->internal->status = SILC_CLIENT_CONN_ERROR;
408       silc_fsm_next(fsm, silc_client_st_connect_error);
409       return SILC_FSM_CONTINUE;
410     }
411
412     /* Connect (UDP) */
413     stream = silc_net_udp_connect(conn->internal->params.bind_ip ?
414                                   conn->internal->params.bind_ip :
415                                   conn->internal->params.local_ip,
416                                   conn->internal->params.local_port,
417                                   conn->remote_host, conn->remote_port,
418                                   conn->internal->schedule);
419
420     SILC_FSM_CALL(silc_client_connect_callback(stream ? SILC_NET_OK :
421                                                SILC_NET_HOST_UNREACHABLE,
422                                                stream, fsm));
423   } else {
424     /* Connect (TCP) */
425     SILC_FSM_CALL(conn->internal->op = silc_net_tcp_connect(
426                                        NULL, conn->remote_host,
427                                        conn->remote_port,
428                                        conn->internal->schedule,
429                                        silc_client_connect_callback, fsm));
430   }
431 }
432
433 /* Sets the new connection stream into use and creates packet stream */
434
435 SILC_FSM_STATE(silc_client_st_connect_set_stream)
436 {
437   SilcClientConnection conn = fsm_context;
438   SilcClient client = conn->client;
439
440   if (conn->internal->disconnected) {
441     /** Disconnected */
442     silc_fsm_next(fsm, silc_client_st_connect_error);
443     return SILC_FSM_CONTINUE;
444   }
445
446   /* Create packet stream */
447   conn->stream = silc_packet_stream_create(client->internal->packet_engine,
448                                            conn->internal->schedule,
449                                            conn->internal->user_stream);
450   if (!conn->stream) {
451     /** Cannot create packet stream */
452     SILC_LOG_DEBUG(("Could not create packet stream"));
453     conn->internal->status = SILC_CLIENT_CONN_ERROR;
454     silc_fsm_next(fsm, silc_client_st_connect_error);
455     return SILC_FSM_CONTINUE;
456   }
457
458   silc_packet_set_context(conn->stream, conn);
459
460   /** Start key exchange */
461   silc_fsm_next(fsm, silc_client_st_connect_key_exchange);
462   return SILC_FSM_CONTINUE;
463 }
464
465 /* Starts key exchange protocol with remote host */
466
467 SILC_FSM_STATE(silc_client_st_connect_key_exchange)
468 {
469   SilcClientConnection conn = fsm_context;
470   SilcClient client = conn->client;
471   SilcSKEParamsStruct params;
472   SilcClientID cid;
473
474   SILC_LOG_DEBUG(("Starting key exchange protocol"));
475
476   /* Allocate SKE */
477   conn->internal->ske =
478     silc_ske_alloc(client->rng, conn->internal->schedule,
479                    conn->internal->params.repository,
480                    conn->public_key, conn->private_key, fsm);
481   if (!conn->internal->ske) {
482     /** Out of memory */
483     conn->internal->status = SILC_CLIENT_CONN_ERROR_KE;
484     silc_fsm_next(fsm, silc_client_st_connect_error);
485     return SILC_FSM_CONTINUE;
486   }
487
488   /* Set SKE callbacks */
489   silc_ske_set_callbacks(conn->internal->ske, silc_client_ke_verify_key,
490                          silc_client_ke_completion, fsm);
491
492   /* Set up key exchange parameters */
493   params.version = client->internal->silc_client_version;
494   params.timeout_secs = conn->internal->params.timeout_secs;
495   params.flags = SILC_SKE_SP_FLAG_MUTUAL;
496   if (conn->internal->params.pfs)
497     params.flags |= SILC_SKE_SP_FLAG_PFS;
498   if (conn->internal->params.udp) {
499     params.flags |= SILC_SKE_SP_FLAG_IV_INCLUDED;
500     params.session_port = conn->internal->params.local_port;
501   }
502
503   if (conn->internal->params.no_authentication)
504     /** Run key exchange (no auth) */
505     silc_fsm_next(fsm, silc_client_st_connected);
506   else if (conn->internal->params.udp)
507     /** Run key exchange (UDP)*/
508     silc_fsm_next(fsm, silc_client_st_connect_setup_udp);
509   else
510     /** Run key exchange (TCP) */
511     silc_fsm_next(fsm, silc_client_st_connect_auth_resolve);
512
513   /* Old server version requires empty Client ID in packets.  Remove this
514      backwards support somepoint after 1.1 server is released. */
515   memset(&cid, 0, sizeof(cid));
516   cid.ip.data_len = 4;
517   silc_packet_set_ids(conn->stream, SILC_ID_CLIENT, &cid, 0, NULL);
518
519   SILC_FSM_CALL(conn->internal->op = silc_ske_initiator(conn->internal->ske,
520                                                         conn->stream,
521                                                         &params, NULL));
522 }
523
524 /* For UDP/IP connections, set up the UDP session after successful key
525    exchange protocol */
526
527 SILC_FSM_STATE(silc_client_st_connect_setup_udp)
528 {
529   SilcClientConnection conn = fsm_context;
530   SilcStream stream, old;
531   SilcSKESecurityProperties prop;
532
533   SILC_LOG_DEBUG(("Setup UDP SILC session"));
534
535   if (conn->internal->disconnected) {
536     /** Disconnected */
537     silc_fsm_next(fsm, silc_client_st_connect_error);
538     return SILC_FSM_CONTINUE;
539   }
540
541   /* Create new UDP stream */
542   prop = silc_ske_get_security_properties(conn->internal->ske);
543   stream = silc_net_udp_connect(conn->internal->params.local_ip,
544                                 conn->internal->params.local_port,
545                                 conn->remote_host, prop->remote_port,
546                                 conn->internal->schedule);
547   if (!stream) {
548     /** Cannot create UDP stream */
549     conn->internal->status = SILC_CLIENT_CONN_ERROR;
550     silc_fsm_next(fsm, silc_client_st_connect_error);
551     return SILC_FSM_CONTINUE;
552   }
553
554   /* Set the new stream to packet stream */
555   old = silc_packet_stream_get_stream(conn->stream);
556   silc_packet_stream_set_stream(conn->stream, stream);
557   silc_packet_stream_set_iv_included(conn->stream);
558   silc_packet_set_sid(conn->stream, 0);
559
560   /* Delete the old stream */
561   silc_stream_destroy(old);
562
563   /** Start authentication */
564   silc_fsm_next(fsm, silc_client_st_connect_auth_resolve);
565   return SILC_FSM_CONTINUE;
566 }
567
568 /* Resolve authentication method to be used in authentication protocol */
569
570 SILC_FSM_STATE(silc_client_st_connect_auth_resolve)
571 {
572   SilcClientConnection conn = fsm_context;
573
574   SILC_LOG_DEBUG(("Resolve authentication method"));
575
576   if (conn->internal->disconnected) {
577     /** Disconnected */
578     silc_fsm_next(fsm, silc_client_st_connect_error);
579     return SILC_FSM_CONTINUE;
580   }
581
582   /* If authentication method and data is set, use them */
583   if (conn->internal->params.auth_set) {
584     /** Got authentication data */
585     silc_fsm_next(fsm, silc_client_st_connect_auth_start);
586     return SILC_FSM_CONTINUE;
587   }
588
589   /* Send connection authentication request packet */
590   silc_packet_send_va(conn->stream,
591                       SILC_PACKET_CONNECTION_AUTH_REQUEST, 0,
592                       SILC_STR_UI_SHORT(SILC_CONN_CLIENT),
593                       SILC_STR_UI_SHORT(SILC_AUTH_NONE),
594                       SILC_STR_END);
595
596   /** Wait for authentication method */
597   conn->internal->auth_request = TRUE;
598   conn->internal->params.auth_method = SILC_AUTH_NONE;
599   silc_fsm_next_later(fsm, silc_client_st_connect_auth_data, 2, 0);
600   return SILC_FSM_WAIT;
601 }
602
603 /* Get authentication data to be used in authentication protocol */
604
605 SILC_FSM_STATE(silc_client_st_connect_auth_data)
606 {
607   SilcClientConnection conn = fsm_context;
608   SilcClient client = conn->client;
609
610   SILC_LOG_DEBUG(("Get authentication data"));
611
612   if (conn->internal->disconnected) {
613     /** Disconnected */
614     silc_fsm_next(fsm, silc_client_st_connect_error);
615     return SILC_FSM_CONTINUE;
616   }
617
618   conn->internal->auth_request = FALSE;
619
620   /** Get authentication data */
621   silc_fsm_next(fsm, silc_client_st_connect_auth_start);
622   SILC_FSM_CALL(client->internal->ops->get_auth_method(
623                                     client, conn,
624                                     conn->remote_host,
625                                     conn->remote_port,
626                                     conn->internal->params.auth_method,
627                                     silc_client_connect_auth_method, fsm));
628 }
629
630 /* Start connection authentication with remote host */
631
632 SILC_FSM_STATE(silc_client_st_connect_auth_start)
633 {
634   SilcClientConnection conn = fsm_context;
635   SilcConnAuth connauth;
636
637   SILC_LOG_DEBUG(("Starting connection authentication protocol"));
638
639   if (conn->internal->disconnected) {
640     /** Disconnected */
641     silc_fsm_next(fsm, silc_client_st_connect_error);
642     return SILC_FSM_CONTINUE;
643   }
644
645   /* We always use the same key for connection authentication and SKE */
646   if (conn->internal->params.auth_method == SILC_AUTH_PUBLIC_KEY)
647     conn->internal->params.auth = conn->private_key;
648
649   /* Allocate connection authentication protocol */
650   connauth = silc_connauth_alloc(conn->internal->schedule,
651                                  conn->internal->ske,
652                                  conn->internal->params.rekey_secs);
653   if (!connauth) {
654     /** Out of memory */
655     conn->internal->status = SILC_CLIENT_CONN_ERROR_AUTH;
656     conn->internal->error = SILC_STATUS_ERR_AUTH_FAILED;
657     silc_fsm_next(fsm, silc_client_st_connect_error);
658     return SILC_FSM_CONTINUE;
659   }
660
661   /** Start connection authentication */
662   silc_fsm_next(fsm, silc_client_st_connected);
663   SILC_FSM_CALL(conn->internal->op = silc_connauth_initiator(
664                                         connauth, SILC_CONN_CLIENT,
665                                         conn->internal->params.auth_method,
666                                         conn->internal->params.auth,
667                                         conn->internal->params.auth_len,
668                                         silc_client_connect_auth_completion,
669                                         fsm));
670 }
671
672 /* Connection fully established */
673
674 SILC_FSM_STATE(silc_client_st_connected)
675 {
676   SilcClientConnection conn = fsm_context;
677   SilcClient client = conn->client;
678
679   /* Get SILC protocol version remote supports */
680   silc_ske_parse_version(conn->internal->ske, &conn->internal->remote_version,
681                          NULL, NULL, NULL, NULL);
682
683   silc_ske_free(conn->internal->ske);
684   conn->internal->ske = NULL;
685
686   if (!conn->internal->params.auth_set &&
687       conn->internal->params.auth_method == SILC_AUTH_PASSWORD &&
688       conn->internal->params.auth) {
689     silc_free(conn->internal->params.auth);
690     conn->internal->params.auth = NULL;
691   }
692
693   if (conn->internal->disconnected) {
694     /** Disconnected */
695     silc_fsm_next(fsm, silc_client_st_connect_error);
696     return SILC_FSM_CONTINUE;
697   }
698
699   SILC_LOG_DEBUG(("Connection established"));
700
701   /* Install rekey timer */
702   if (conn->type != SILC_CONN_CLIENT)
703     silc_schedule_task_add_timeout(conn->internal->schedule,
704                                    silc_client_rekey_timer, conn,
705                                    conn->internal->params.rekey_secs, 0);
706
707   /* If we connected to server, now register to network. */
708   if (conn->type == SILC_CONN_SERVER &&
709       !conn->internal->params.no_authentication) {
710
711     /* If detach data is provided, resume the session. */
712     if (conn->internal->params.detach_data &&
713         conn->internal->params.detach_data_len) {
714       /** Resume detached session */
715       silc_fsm_next(fsm, silc_client_st_resume);
716     } else {
717       /** Register to network */
718       silc_fsm_next(fsm, silc_client_st_register);
719     }
720
721     return SILC_FSM_CONTINUE;
722   }
723
724   silc_schedule_task_del_by_all(conn->internal->schedule, 0,
725                                 silc_client_connect_timeout, conn);
726
727   /* Call connection callback */
728   conn->callback(client, conn, SILC_CLIENT_CONN_SUCCESS, 0, NULL,
729                  conn->callback_context);
730
731   silc_async_free(conn->internal->cop);
732   conn->internal->cop = NULL;
733
734   return SILC_FSM_FINISH;
735 }
736
737 /* Error during connecting */
738
739 SILC_FSM_STATE(silc_client_st_connect_error)
740 {
741   SilcClientConnection conn = fsm_context;
742
743   if (conn->internal->ske) {
744     silc_ske_free(conn->internal->ske);
745     conn->internal->ske = NULL;
746   }
747
748   /* Signal to close connection */
749   if (!conn->internal->disconnected) {
750     conn->internal->disconnected = TRUE;
751     SILC_FSM_EVENT_SIGNAL(&conn->internal->wait_event);
752   }
753
754   silc_schedule_task_del_by_all(conn->internal->schedule, 0,
755                                 silc_client_connect_timeout, conn);
756
757   return SILC_FSM_FINISH;
758 }
759
760 /****************************** Connect rekey *******************************/
761
762 /* Connection rekey timer callback */
763
764 SILC_TASK_CALLBACK(silc_client_rekey_timer)
765 {
766   SilcClientConnection conn = context;
767
768   /* Signal to start rekey */
769   if (!silc_fsm_is_started(&conn->internal->event_thread)) {
770     conn->internal->rekey_responder = FALSE;
771     conn->internal->rekeying = TRUE;
772     SILC_FSM_EVENT_SIGNAL(&conn->internal->wait_event);
773   }
774
775   /* Reinstall rekey timer */
776   silc_schedule_task_add_timeout(conn->internal->schedule,
777                                  silc_client_rekey_timer, conn,
778                                  conn->internal->params.rekey_secs, 0);
779 }
780
781 /* Performs rekey */
782
783 SILC_FSM_STATE(silc_client_st_rekey)
784 {
785   SilcClientConnection conn = fsm_context;
786   SilcClient client = conn->client;
787
788   SILC_LOG_DEBUG(("Rekey conn %p", conn));
789
790   if (conn->internal->disconnected)
791     return SILC_FSM_FINISH;
792
793   /* Allocate SKE */
794   conn->internal->ske =
795     silc_ske_alloc(client->rng, conn->internal->schedule, NULL,
796                    conn->public_key, NULL, fsm);
797   if (!conn->internal->ske)
798     return SILC_FSM_FINISH;
799
800   /* Set SKE callbacks */
801   silc_ske_set_callbacks(conn->internal->ske, NULL,
802                          silc_client_rekey_completion, fsm);
803
804   /** Perform rekey */
805   if (!conn->internal->rekey_responder)
806     SILC_FSM_CALL(conn->internal->op = silc_ske_rekey_initiator(
807                                                     conn->internal->ske,
808                                                     conn->stream,
809                                                     conn->internal->rekey));
810   else
811     SILC_FSM_CALL(conn->internal->op = silc_ske_rekey_responder(
812                                                     conn->internal->ske,
813                                                     conn->stream,
814                                                     conn->internal->rekey,
815                                                     NULL));
816 }