Merge commit 'origin/silc.1.1.branch'
[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 - 2008 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(SilcResult 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_OK:
39       break;
40     case SILC_ERR_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_ERR_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_ERR_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_ERR_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_ERR_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_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     return;
266   }
267
268   silc_ske_free_rekey_material(conn->internal->rekey);
269   conn->internal->rekey = rekey;
270
271   silc_ske_free(conn->internal->ske);
272   conn->internal->ske = NULL;
273
274   SILC_LOG_DEBUG(("Rekey completed conn %p", conn));
275
276   /* Rekey done */
277   silc_fsm_finish(fsm);
278 }
279
280 /* Callback called by application to return authentication data */
281
282 static void silc_client_connect_auth_method(SilcAuthMethod auth_meth,
283                                             const void *auth,
284                                             SilcUInt32 auth_len,
285                                             void *context)
286 {
287   SilcFSMThread fsm = context;
288   SilcClientConnection conn = silc_fsm_get_context(fsm);
289
290   conn->internal->params.auth_method = auth_meth;
291   if (auth_meth == SILC_AUTH_PASSWORD)
292     conn->internal->params.auth = silc_memdup(auth, auth_len);
293   else
294     conn->internal->params.auth = (void *)auth;
295   conn->internal->params.auth_len = auth_len;
296
297   SILC_FSM_CALL_CONTINUE(fsm);
298 }
299
300 /* Connection authentication completion callback */
301
302 static void silc_client_connect_auth_completion(SilcConnAuth connauth,
303                                                 SilcBool success,
304                                                 void *context)
305 {
306   SilcFSMThread fsm = context;
307   SilcClientConnection conn = silc_fsm_get_context(fsm);
308   SilcClient client = conn->client;
309
310   conn->internal->op = NULL;
311   silc_connauth_free(connauth);
312
313   if (!success) {
314     if (conn->internal->verbose)
315         client->internal->ops->say(
316                         client, conn, SILC_CLIENT_MESSAGE_ERROR,
317                         "Authentication failed");
318
319     conn->internal->status = SILC_CLIENT_CONN_ERROR_AUTH;
320     conn->internal->error = SILC_STATUS_ERR_AUTH_FAILED;
321     silc_fsm_next(fsm, silc_client_st_connect_error);
322   }
323
324   SILC_FSM_CALL_CONTINUE_SYNC(fsm);
325 }
326
327 /********************** CONNECTION_AUTH_REQUEST packet **********************/
328
329 /* Received connection authentication request packet.  We get the
330    required authentication method here. */
331
332 SILC_FSM_STATE(silc_client_connect_auth_request)
333 {
334   SilcClientConnection conn = fsm_context;
335   SilcPacket packet = state_context;
336   SilcUInt16 conn_type, auth_meth;
337
338   if (!conn->internal->auth_request) {
339     silc_packet_free(packet);
340     return SILC_FSM_FINISH;
341   }
342
343   /* Parse the payload */
344   if (silc_buffer_unformat(&packet->buffer,
345                            SILC_STR_UI_SHORT(&conn_type),
346                            SILC_STR_UI_SHORT(&auth_meth),
347                            SILC_STR_END) < 0)
348     auth_meth = SILC_AUTH_NONE;
349
350   silc_packet_free(packet);
351
352   SILC_LOG_DEBUG(("Resolved authentication method: %s",
353                   (auth_meth == SILC_AUTH_NONE ? "none" :
354                    auth_meth == SILC_AUTH_PASSWORD ? "passphrase" :
355                    "public key")));
356   conn->internal->params.auth_method = auth_meth;
357
358   /* Continue authentication */
359   silc_fsm_continue_sync(&conn->internal->event_thread);
360   return SILC_FSM_FINISH;
361 }
362
363 /*************************** Connect remote host ****************************/
364
365 /* Connection timeout callback */
366
367 SILC_TASK_CALLBACK(silc_client_connect_timeout)
368 {
369   SilcClientConnection conn = context;
370
371   SILC_LOG_DEBUG(("Connection timeout"));
372
373   conn->internal->status = SILC_CLIENT_CONN_ERROR_TIMEOUT;
374   conn->internal->error = SILC_STATUS_ERR_TIMEDOUT;
375
376   silc_fsm_next(&conn->internal->event_thread, silc_client_st_connect_error);
377   silc_fsm_continue_sync(&conn->internal->event_thread);
378 }
379
380 /* Creates a connection to remote host */
381
382 SILC_FSM_STATE(silc_client_st_connect)
383 {
384   SilcClientConnection conn = fsm_context;
385
386   SILC_LOG_DEBUG(("Connecting to %s:%d", conn->remote_host,
387                   conn->remote_port));
388
389   /** Connect */
390   silc_fsm_next(fsm, silc_client_st_connect_set_stream);
391
392   /* Add connection timeout */
393   if (conn->internal->params.timeout_secs)
394     silc_schedule_task_add_timeout(conn->internal->schedule,
395                                    silc_client_connect_timeout, conn,
396                                    conn->internal->params.timeout_secs, 0);
397
398   if (conn->internal->params.udp) {
399     SilcStream stream;
400
401     if (!conn->internal->params.local_ip) {
402       /** IP address not given */
403       SILC_LOG_ERROR(("Local UDP IP address not specified"));
404       conn->internal->status = SILC_CLIENT_CONN_ERROR;
405       silc_fsm_next(fsm, silc_client_st_connect_error);
406       return SILC_FSM_CONTINUE;
407     }
408
409     /* Connect (UDP) */
410     stream = silc_net_udp_connect(conn->internal->params.bind_ip ?
411                                   conn->internal->params.bind_ip :
412                                   conn->internal->params.local_ip,
413                                   conn->internal->params.local_port,
414                                   conn->remote_host, conn->remote_port,
415                                   conn->internal->schedule);
416
417     SILC_FSM_CALL(silc_client_connect_callback(stream ? SILC_OK :
418                                                SILC_ERR_UNREACHABLE,
419                                                stream, fsm));
420   } else {
421     /* Connect (TCP) */
422     SILC_FSM_CALL(conn->internal->op = silc_net_tcp_connect(
423                                        NULL, conn->remote_host,
424                                        conn->remote_port,
425                                        conn->internal->schedule,
426                                        silc_client_connect_callback, fsm));
427   }
428 }
429
430 /* Sets the new connection stream into use and creates packet stream */
431
432 SILC_FSM_STATE(silc_client_st_connect_set_stream)
433 {
434   SilcClientConnection conn = fsm_context;
435   SilcClient client = conn->client;
436
437   if (conn->internal->disconnected) {
438     /** Disconnected */
439     silc_fsm_next(fsm, silc_client_st_connect_error);
440     return SILC_FSM_CONTINUE;
441   }
442
443   /* Create packet stream */
444   conn->stream = silc_packet_stream_create(client->internal->packet_engine,
445                                            conn->internal->schedule,
446                                            conn->internal->user_stream);
447   if (!conn->stream) {
448     /** Cannot create packet stream */
449     SILC_LOG_DEBUG(("Could not create packet stream"));
450     conn->internal->status = SILC_CLIENT_CONN_ERROR;
451     silc_fsm_next(fsm, silc_client_st_connect_error);
452     return SILC_FSM_CONTINUE;
453   }
454
455   silc_packet_set_context(conn->stream, conn);
456
457   /* Save socket stream and socket into connection context */
458   conn->socket_stream = silc_packet_stream_get_stream(conn->stream);
459   silc_socket_stream_get_info(conn->socket_stream, &conn->sock, NULL,
460                               NULL, NULL);
461
462   /** Start key exchange */
463   silc_fsm_next(fsm, silc_client_st_connect_key_exchange);
464   return SILC_FSM_CONTINUE;
465 }
466
467 /* Starts key exchange protocol with remote host */
468
469 SILC_FSM_STATE(silc_client_st_connect_key_exchange)
470 {
471   SilcClientConnection conn = fsm_context;
472   SilcClient client = conn->client;
473   SilcSKEParamsStruct params;
474   SilcClientID cid;
475
476   SILC_LOG_DEBUG(("Starting key exchange protocol"));
477
478   /* Allocate SKE */
479   conn->internal->ske =
480     silc_ske_alloc(client->rng, conn->internal->schedule,
481                    conn->internal->params.repository,
482                    conn->public_key, conn->private_key, fsm);
483   if (!conn->internal->ske) {
484     /** Out of memory */
485     conn->internal->status = SILC_CLIENT_CONN_ERROR_KE;
486     silc_fsm_next(fsm, silc_client_st_connect_error);
487     return SILC_FSM_CONTINUE;
488   }
489
490   /* Set SKE callbacks */
491   silc_ske_set_callbacks(conn->internal->ske, silc_client_ke_verify_key,
492                          silc_client_ke_completion, fsm);
493
494   /* Set up key exchange parameters */
495   params.version = client->internal->silc_client_version;
496   params.timeout_secs = conn->internal->params.timeout_secs;
497   params.flags = SILC_SKE_SP_FLAG_MUTUAL;
498   if (conn->internal->params.pfs)
499     params.flags |= SILC_SKE_SP_FLAG_PFS;
500   if (conn->internal->params.udp) {
501     params.flags |= SILC_SKE_SP_FLAG_IV_INCLUDED;
502     params.session_port = conn->internal->params.local_port;
503   }
504
505   if (conn->internal->params.no_authentication)
506     /** Run key exchange (no auth) */
507     silc_fsm_next(fsm, silc_client_st_connected);
508   else if (conn->internal->params.udp)
509     /** Run key exchange (UDP)*/
510     silc_fsm_next(fsm, silc_client_st_connect_setup_udp);
511   else
512     /** Run key exchange (TCP) */
513     silc_fsm_next(fsm, silc_client_st_connect_auth_resolve);
514
515   /* Old server version requires empty Client ID in packets.  Remove this
516      backwards support somepoint after 1.1 server is released. */
517   memset(&cid, 0, sizeof(cid));
518   cid.ip.data_len = 4;
519   silc_packet_set_ids(conn->stream, SILC_ID_CLIENT, &cid, 0, NULL);
520
521   SILC_FSM_CALL(conn->internal->op = silc_ske_initiator(conn->internal->ske,
522                                                         conn->stream,
523                                                         &params, NULL));
524 }
525
526 /* For UDP/IP connections, set up the UDP session after successful key
527    exchange protocol */
528
529 SILC_FSM_STATE(silc_client_st_connect_setup_udp)
530 {
531   SilcClientConnection conn = fsm_context;
532   SilcStream stream, old;
533   SilcSKESecurityProperties prop;
534
535   SILC_LOG_DEBUG(("Setup UDP SILC session"));
536
537   if (conn->internal->disconnected) {
538     /** Disconnected */
539     silc_fsm_next(fsm, silc_client_st_connect_error);
540     return SILC_FSM_CONTINUE;
541   }
542
543   /* Create new UDP stream */
544   prop = silc_ske_get_security_properties(conn->internal->ske);
545   stream = silc_net_udp_connect(conn->internal->params.local_ip,
546                                 conn->internal->params.local_port,
547                                 conn->remote_host, prop->remote_port,
548                                 conn->internal->schedule);
549   if (!stream) {
550     /** Cannot create UDP stream */
551     conn->internal->status = SILC_CLIENT_CONN_ERROR;
552     silc_fsm_next(fsm, silc_client_st_connect_error);
553     return SILC_FSM_CONTINUE;
554   }
555
556   /* Set the new stream to packet stream */
557   old = silc_packet_stream_get_stream(conn->stream);
558   silc_packet_stream_set_stream(conn->stream, stream);
559   conn->socket_stream = stream;
560   silc_socket_stream_get_info(conn->socket_stream, &conn->sock, NULL,
561                               NULL, NULL);
562   silc_packet_stream_set_iv_included(conn->stream);
563   silc_packet_set_sid(conn->stream, 0);
564
565   /* Delete the old stream */
566   silc_stream_destroy(old);
567
568   /** Start authentication */
569   silc_fsm_next(fsm, silc_client_st_connect_auth_resolve);
570   return SILC_FSM_CONTINUE;
571 }
572
573 /* Resolve authentication method to be used in authentication protocol */
574
575 SILC_FSM_STATE(silc_client_st_connect_auth_resolve)
576 {
577   SilcClientConnection conn = fsm_context;
578
579   SILC_LOG_DEBUG(("Resolve authentication method"));
580
581   if (conn->internal->disconnected) {
582     /** Disconnected */
583     silc_fsm_next(fsm, silc_client_st_connect_error);
584     return SILC_FSM_CONTINUE;
585   }
586
587   /* If authentication method and data is set, use them */
588   if (conn->internal->params.auth_set) {
589     /** Got authentication data */
590     silc_fsm_next(fsm, silc_client_st_connect_auth_start);
591     return SILC_FSM_CONTINUE;
592   }
593
594   /* Send connection authentication request packet */
595   silc_packet_send_va(conn->stream,
596                       SILC_PACKET_CONNECTION_AUTH_REQUEST, 0,
597                       SILC_STR_UI_SHORT(SILC_CONN_CLIENT),
598                       SILC_STR_UI_SHORT(SILC_AUTH_NONE),
599                       SILC_STR_END);
600
601   /** Wait for authentication method */
602   conn->internal->auth_request = TRUE;
603   conn->internal->params.auth_method = SILC_AUTH_NONE;
604   silc_fsm_next_later(fsm, silc_client_st_connect_auth_data, 2, 0);
605   return SILC_FSM_WAIT;
606 }
607
608 /* Get authentication data to be used in authentication protocol */
609
610 SILC_FSM_STATE(silc_client_st_connect_auth_data)
611 {
612   SilcClientConnection conn = fsm_context;
613   SilcClient client = conn->client;
614
615   SILC_LOG_DEBUG(("Get authentication data"));
616
617   if (conn->internal->disconnected) {
618     /** Disconnected */
619     silc_fsm_next(fsm, silc_client_st_connect_error);
620     return SILC_FSM_CONTINUE;
621   }
622
623   conn->internal->auth_request = FALSE;
624
625   /** Get authentication data */
626   silc_fsm_next(fsm, silc_client_st_connect_auth_start);
627   SILC_FSM_CALL(client->internal->ops->get_auth_method(
628                                     client, conn,
629                                     conn->remote_host,
630                                     conn->remote_port,
631                                     conn->internal->params.auth_method,
632                                     silc_client_connect_auth_method, fsm));
633 }
634
635 /* Start connection authentication with remote host */
636
637 SILC_FSM_STATE(silc_client_st_connect_auth_start)
638 {
639   SilcClientConnection conn = fsm_context;
640   SilcConnAuth connauth;
641
642   SILC_LOG_DEBUG(("Starting connection authentication protocol"));
643
644   if (conn->internal->disconnected) {
645     /** Disconnected */
646     silc_fsm_next(fsm, silc_client_st_connect_error);
647     return SILC_FSM_CONTINUE;
648   }
649
650   /* We always use the same key for connection authentication and SKE */
651   if (conn->internal->params.auth_method == SILC_AUTH_PUBLIC_KEY)
652     conn->internal->params.auth = conn->private_key;
653
654   /* Allocate connection authentication protocol */
655   connauth = silc_connauth_alloc(conn->internal->schedule,
656                                  conn->internal->ske,
657                                  conn->internal->params.rekey_secs);
658   if (!connauth) {
659     /** Out of memory */
660     conn->internal->status = SILC_CLIENT_CONN_ERROR_AUTH;
661     conn->internal->error = SILC_STATUS_ERR_AUTH_FAILED;
662     silc_fsm_next(fsm, silc_client_st_connect_error);
663     return SILC_FSM_CONTINUE;
664   }
665
666   /** Start connection authentication */
667   silc_fsm_next(fsm, silc_client_st_connected);
668   SILC_FSM_CALL(conn->internal->op = silc_connauth_initiator(
669                                         connauth, SILC_CONN_CLIENT,
670                                         conn->internal->params.auth_method,
671                                         conn->internal->params.auth,
672                                         conn->internal->params.auth_len,
673                                         silc_client_connect_auth_completion,
674                                         fsm));
675 }
676
677 /* Connection fully established */
678
679 SILC_FSM_STATE(silc_client_st_connected)
680 {
681   SilcClientConnection conn = fsm_context;
682   SilcClient client = conn->client;
683
684   /* Get SILC protocol version remote supports */
685   silc_ske_parse_version(conn->internal->ske, &conn->internal->remote_version,
686                          NULL, NULL, NULL, NULL);
687
688   silc_ske_free(conn->internal->ske);
689   conn->internal->ske = NULL;
690
691   if (!conn->internal->params.auth_set &&
692       conn->internal->params.auth_method == SILC_AUTH_PASSWORD &&
693       conn->internal->params.auth) {
694     silc_free(conn->internal->params.auth);
695     conn->internal->params.auth = NULL;
696   }
697
698   if (conn->internal->disconnected) {
699     /** Disconnected */
700     silc_fsm_next(fsm, silc_client_st_connect_error);
701     return SILC_FSM_CONTINUE;
702   }
703
704   SILC_LOG_DEBUG(("Connection established"));
705
706   /* Install rekey timer */
707   if (conn->type != SILC_CONN_CLIENT)
708     silc_schedule_task_add_timeout(conn->internal->schedule,
709                                    silc_client_rekey_timer, conn,
710                                    conn->internal->params.rekey_secs, 0);
711
712   /* If we connected to server, now register to network. */
713   if (conn->type == SILC_CONN_SERVER &&
714       !conn->internal->params.no_authentication) {
715
716     /* If detach data is provided, resume the session. */
717     if (conn->internal->params.detach_data &&
718         conn->internal->params.detach_data_len) {
719       /** Resume detached session */
720       silc_fsm_next(fsm, silc_client_st_resume);
721     } else {
722       /** Register to network */
723       silc_fsm_next(fsm, silc_client_st_register);
724     }
725
726     return SILC_FSM_CONTINUE;
727   }
728
729   silc_schedule_task_del_by_all(conn->internal->schedule, 0,
730                                 silc_client_connect_timeout, conn);
731
732   /* Call connection callback */
733   conn->callback(client, conn, SILC_CLIENT_CONN_SUCCESS, 0, NULL,
734                  conn->callback_context);
735
736   silc_async_free(conn->internal->cop);
737   conn->internal->cop = NULL;
738
739   return SILC_FSM_FINISH;
740 }
741
742 /* Error during connecting */
743
744 SILC_FSM_STATE(silc_client_st_connect_error)
745 {
746   SilcClientConnection conn = fsm_context;
747
748   if (conn->internal->ske) {
749     silc_ske_free(conn->internal->ske);
750     conn->internal->ske = NULL;
751   }
752
753   /* Signal to close connection */
754   if (!conn->internal->disconnected) {
755     conn->internal->disconnected = TRUE;
756     SILC_FSM_EVENT_SIGNAL(&conn->internal->wait_event);
757   }
758
759   silc_schedule_task_del_by_all(conn->internal->schedule, 0,
760                                 silc_client_connect_timeout, conn);
761
762   return SILC_FSM_FINISH;
763 }
764
765 /****************************** Connect rekey *******************************/
766
767 /* Connection rekey timer callback */
768
769 SILC_TASK_CALLBACK(silc_client_rekey_timer)
770 {
771   SilcClientConnection conn = context;
772
773   /* Signal to start rekey */
774   if (!silc_fsm_is_started(&conn->internal->event_thread)) {
775     conn->internal->rekey_responder = FALSE;
776     conn->internal->rekeying = TRUE;
777     SILC_FSM_EVENT_SIGNAL(&conn->internal->wait_event);
778   }
779
780   /* Reinstall rekey timer */
781   silc_schedule_task_add_timeout(conn->internal->schedule,
782                                  silc_client_rekey_timer, conn,
783                                  conn->internal->params.rekey_secs, 0);
784 }
785
786 /* Performs rekey */
787
788 SILC_FSM_STATE(silc_client_st_rekey)
789 {
790   SilcClientConnection conn = fsm_context;
791   SilcClient client = conn->client;
792
793   SILC_LOG_DEBUG(("Rekey conn %p", conn));
794
795   if (conn->internal->disconnected)
796     return SILC_FSM_FINISH;
797
798   /* Allocate SKE */
799   conn->internal->ske =
800     silc_ske_alloc(client->rng, conn->internal->schedule, NULL,
801                    conn->public_key, NULL, fsm);
802   if (!conn->internal->ske)
803     return SILC_FSM_FINISH;
804
805   /* Set SKE callbacks */
806   silc_ske_set_callbacks(conn->internal->ske, NULL,
807                          silc_client_rekey_completion, fsm);
808
809   /** Perform rekey */
810   if (!conn->internal->rekey_responder)
811     SILC_FSM_CALL(conn->internal->op = silc_ske_rekey_initiator(
812                                                     conn->internal->ske,
813                                                     conn->stream,
814                                                     conn->internal->rekey));
815   else
816     SILC_FSM_CALL(conn->internal->op = silc_ske_rekey_responder(
817                                                     conn->internal->ske,
818                                                     conn->stream,
819                                                     conn->internal->rekey,
820                                                     NULL));
821 }