updates.
[silc.git] / apps / silcd / protocol.c
1 /*
2
3   protocol.c
4
5   Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
6
7   Copyright (C) 1997 - 2001 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; either version 2 of the License, or
12   (at your option) any later version.
13   
14   This program is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License for more details.
18
19 */
20 /*
21  * Server side of the protocols.
22  */
23 /* $Id$ */
24
25 #include "serverincludes.h"
26 #include "server_internal.h"
27
28 SILC_TASK_CALLBACK(silc_server_protocol_connection_auth);
29 SILC_TASK_CALLBACK(silc_server_protocol_key_exchange);
30 SILC_TASK_CALLBACK(silc_server_protocol_rekey);
31
32 extern char *silc_version_string;
33
34 /*
35  * Key Exhange protocol functions
36  */
37
38 /* Packet sending callback. This function is provided as packet sending
39    routine to the Key Exchange functions. */
40
41 static void silc_server_protocol_ke_send_packet(SilcSKE ske,
42                                                 SilcBuffer packet,
43                                                 SilcPacketType type,
44                                                 void *context)
45 {
46   SilcProtocol protocol = (SilcProtocol)context;
47   SilcServerKEInternalContext *ctx = 
48     (SilcServerKEInternalContext *)protocol->context;
49   SilcServer server = (SilcServer)ctx->server;
50
51   /* Send the packet immediately */
52   silc_server_packet_send(server, ske->sock,
53                           type, 0, packet->data, packet->len, TRUE);
54 }
55
56 /* Sets the negotiated key material into use for particular connection. */
57
58 int silc_server_protocol_ke_set_keys(SilcSKE ske,
59                                      SilcSocketConnection sock,
60                                      SilcSKEKeyMaterial *keymat,
61                                      SilcCipher cipher,
62                                      SilcPKCS pkcs,
63                                      SilcHash hash,
64                                      SilcHmac hmac,
65                                      SilcSKEDiffieHellmanGroup group,
66                                      bool is_responder)
67 {
68   SilcUnknownEntry conn_data;
69   SilcIDListData idata;
70
71   SILC_LOG_DEBUG(("Setting new key into use"));
72
73   conn_data = silc_calloc(1, sizeof(*conn_data));
74   idata = (SilcIDListData)conn_data;
75
76   /* Allocate cipher to be used in the communication */
77   if (!silc_cipher_alloc(cipher->cipher->name, &idata->send_key)) {
78     silc_free(conn_data);
79     return FALSE;
80   }
81   if (!silc_cipher_alloc(cipher->cipher->name, &idata->receive_key)) {
82     silc_free(conn_data);
83     return FALSE;
84   }
85   
86   if (is_responder == TRUE) {
87     silc_cipher_set_key(idata->send_key, keymat->receive_enc_key, 
88                         keymat->enc_key_len);
89     silc_cipher_set_iv(idata->send_key, keymat->receive_iv);
90     silc_cipher_set_key(idata->receive_key, keymat->send_enc_key, 
91                         keymat->enc_key_len);
92     silc_cipher_set_iv(idata->receive_key, keymat->send_iv);
93   } else {
94     silc_cipher_set_key(idata->send_key, keymat->send_enc_key, 
95                         keymat->enc_key_len);
96     silc_cipher_set_iv(idata->send_key, keymat->send_iv);
97     silc_cipher_set_key(idata->receive_key, keymat->receive_enc_key, 
98                         keymat->enc_key_len);
99     silc_cipher_set_iv(idata->receive_key, keymat->receive_iv);
100   }
101
102   idata->rekey = silc_calloc(1, sizeof(*idata->rekey));
103   idata->rekey->send_enc_key = 
104     silc_calloc(keymat->enc_key_len / 8,
105                 sizeof(*idata->rekey->send_enc_key));
106   memcpy(idata->rekey->send_enc_key, 
107          keymat->send_enc_key, keymat->enc_key_len / 8);
108   idata->rekey->enc_key_len = keymat->enc_key_len / 8;
109
110   if (ske->start_payload->flags & SILC_SKE_SP_FLAG_PFS)
111     idata->rekey->pfs = TRUE;
112   idata->rekey->ske_group = silc_ske_group_get_number(group);
113
114   /* Save the remote host's public key */
115   silc_pkcs_public_key_decode(ske->ke1_payload->pk_data, 
116                               ske->ke1_payload->pk_len, &idata->public_key);
117
118   /* Save the hash */
119   if (!silc_hash_alloc(hash->hash->name, &idata->hash)) {
120     silc_cipher_free(idata->send_key);
121     silc_cipher_free(idata->receive_key);
122     silc_free(conn_data);
123     return FALSE;
124   }
125
126   /* Save HMAC key to be used in the communication. */
127   if (!silc_hmac_alloc(hmac->hmac->name, NULL, &idata->hmac)) {
128     silc_cipher_free(idata->send_key);
129     silc_cipher_free(idata->receive_key);
130     silc_hash_free(idata->hash);
131     silc_free(conn_data);
132     return FALSE;
133   }
134   silc_hmac_set_key(idata->hmac, keymat->hmac_key, keymat->hmac_key_len);
135
136   sock->user_data = (void *)conn_data;
137
138   return TRUE;
139 }
140
141 /* Check remote host version string */
142
143 SilcSKEStatus silc_ske_check_version(SilcSKE ske, unsigned char *version,
144                                      uint32 len)
145 {
146   SilcSKEStatus status = SILC_SKE_STATUS_OK;
147
148   SILC_LOG_INFO(("%s (%s) is version %s", ske->sock->hostname,
149                  ske->sock->ip, version));
150
151   /* Check for initial version string */
152   if (!strstr(version, "SILC-1.0-"))
153     status = SILC_SKE_STATUS_BAD_VERSION;
154
155   /* Check software version */
156
157   if (len < strlen(silc_version_string))
158     status = SILC_SKE_STATUS_BAD_VERSION;
159
160   /* XXX for now there is no other tests due to the abnormal version
161      string that is used */
162
163   return status;
164 }
165
166 /* Performs key exchange protocol. This is used for both initiator
167    and responder key exchange. This is performed always when accepting
168    new connection to the server. This may be called recursively. */
169
170 SILC_TASK_CALLBACK(silc_server_protocol_key_exchange)
171 {
172   SilcProtocol protocol = (SilcProtocol)context;
173   SilcServerKEInternalContext *ctx = 
174     (SilcServerKEInternalContext *)protocol->context;
175   SilcServer server = (SilcServer)ctx->server;
176   SilcSKEStatus status = 0;
177
178   SILC_LOG_DEBUG(("Start"));
179
180   if (protocol->state == SILC_PROTOCOL_STATE_UNKNOWN)
181     protocol->state = SILC_PROTOCOL_STATE_START;
182
183   SILC_LOG_DEBUG(("State=%d", protocol->state));
184
185   switch(protocol->state) {
186   case SILC_PROTOCOL_STATE_START:
187     {
188       /*
189        * Start protocol
190        */
191       SilcSKE ske;
192
193       /* Allocate Key Exchange object */
194       ske = silc_ske_alloc();
195       ctx->ske = ske;
196       ske->rng = server->rng;
197       
198       if (ctx->responder == TRUE) {
199         /* Start the key exchange by processing the received security
200            properties packet from initiator. */
201         status = silc_ske_responder_start(ske, ctx->rng, ctx->sock,
202                                           silc_version_string,
203                                           ctx->packet->buffer, FALSE,
204                                           NULL, NULL);
205       } else {
206         SilcSKEStartPayload *start_payload;
207
208         /* Assemble security properties. */
209         silc_ske_assemble_security_properties(ske, SILC_SKE_SP_FLAG_NONE, 
210                                               silc_version_string,
211                                               &start_payload);
212
213         /* Start the key exchange by sending our security properties
214            to the remote end. */
215         status = silc_ske_initiator_start(ske, ctx->rng, ctx->sock,
216                                           start_payload,
217                                           silc_server_protocol_ke_send_packet,
218                                           context);
219       }
220
221       if (status != SILC_SKE_STATUS_OK) {
222         SILC_LOG_WARNING(("Error (type %d) during Key Exchange protocol",
223                           status));
224         SILC_LOG_DEBUG(("Error (type %d) during Key Exchange protocol",
225                         status));
226
227         protocol->state = SILC_PROTOCOL_STATE_ERROR;
228         protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 300000);
229         return;
230       }
231
232       /* Advance protocol state and call the next state if we are responder */
233       protocol->state++;
234       if (ctx->responder == TRUE)
235         protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 100000);
236     }
237     break;
238   case 2:
239     {
240       /* 
241        * Phase 1 
242        */
243       if (ctx->responder == TRUE) {
244         /* Sends the selected security properties to the initiator. */
245         status = 
246           silc_ske_responder_phase_1(ctx->ske, 
247                                      ctx->ske->start_payload,
248                                      silc_server_protocol_ke_send_packet,
249                                      context);
250       } else {
251         /* Call Phase-1 function. This processes the Key Exchange Start
252            paylaod reply we just got from the responder. The callback
253            function will receive the processed payload where we will
254            save it. */
255         status = silc_ske_initiator_phase_1(ctx->ske, ctx->packet->buffer,
256                                             NULL, NULL);
257       }
258
259       if (status != SILC_SKE_STATUS_OK) {
260         SILC_LOG_WARNING(("Error (type %d) during Key Exchange protocol",
261                           status));
262         SILC_LOG_DEBUG(("Error (type %d) during Key Exchange protocol",
263                         status));
264
265         protocol->state = SILC_PROTOCOL_STATE_ERROR;
266         protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 300000);
267         return;
268       }
269
270       /* Advance protocol state and call next state if we are initiator */
271       protocol->state++;
272       if (ctx->responder == FALSE)
273         protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 100000);
274     }
275     break;
276   case 3:
277     {
278       /* 
279        * Phase 2 
280        */
281       if (ctx->responder == TRUE) {
282         /* Process the received Key Exchange 1 Payload packet from
283            the initiator. This also creates our parts of the Diffie
284            Hellman algorithm. */
285         status = silc_ske_responder_phase_2(ctx->ske, ctx->packet->buffer, 
286                                             NULL, NULL, NULL, NULL);
287       } else {
288         /* Call the Phase-2 function. This creates Diffie Hellman
289            key exchange parameters and sends our public part inside
290            Key Exhange 1 Payload to the responder. */
291         status = 
292           silc_ske_initiator_phase_2(ctx->ske,
293                                      server->public_key,
294                                      server->private_key,
295                                      silc_server_protocol_ke_send_packet,
296                                      context);
297       }
298
299       if (status != SILC_SKE_STATUS_OK) {
300         SILC_LOG_WARNING(("Error (type %d) during Key Exchange protocol",
301                           status));
302         SILC_LOG_DEBUG(("Error (type %d) during Key Exchange protocol",
303                         status));
304
305         protocol->state = SILC_PROTOCOL_STATE_ERROR;
306         protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 300000);
307         return;
308       }
309
310       /* Advance protocol state and call the next state if we are responder */
311       protocol->state++;
312       if (ctx->responder == TRUE)
313         protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 100000);
314     }
315     break;
316   case 4:
317     {
318       /* 
319        * Finish protocol
320        */
321       if (ctx->responder == TRUE) {
322         /* This creates the key exchange material and sends our
323            public parts to the initiator inside Key Exchange 2 Payload. */
324         status = 
325           silc_ske_responder_finish(ctx->ske, 
326                                     server->public_key, server->private_key,
327                                     SILC_SKE_PK_TYPE_SILC,
328                                     silc_server_protocol_ke_send_packet,
329                                     context);
330       } else {
331         /* Finish the protocol. This verifies the Key Exchange 2 payload
332            sent by responder. */
333         status = silc_ske_initiator_finish(ctx->ske, ctx->packet->buffer, 
334                                            NULL, NULL, NULL, NULL);
335       }
336
337       if (status != SILC_SKE_STATUS_OK) {
338         SILC_LOG_WARNING(("Error (type %d) during Key Exchange protocol",
339                           status));
340         SILC_LOG_DEBUG(("Error (type %d) during Key Exchange protocol",
341                         status));
342
343         protocol->state = SILC_PROTOCOL_STATE_ERROR;
344         protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 300000);
345         return;
346       }
347
348       /* Send Ok to the other end. We will end the protocol as responder
349          sends Ok to us when we will take the new keys into use. */
350       if (ctx->responder == FALSE)
351         silc_ske_end(ctx->ske, silc_server_protocol_ke_send_packet, context);
352
353       /* End the protocol on the next round */
354       protocol->state = SILC_PROTOCOL_STATE_END;
355     }
356     break;
357
358   case SILC_PROTOCOL_STATE_END:
359     {
360       /* 
361        * End protocol
362        */
363       SilcSKEKeyMaterial *keymat;
364       int key_len = silc_cipher_get_key_len(ctx->ske->prop->cipher);
365       int hash_len = ctx->ske->prop->hash->hash->hash_len;
366
367       /* Process the key material */
368       keymat = silc_calloc(1, sizeof(*keymat));
369       status = silc_ske_process_key_material(ctx->ske, 16, key_len, hash_len,
370                                              keymat);
371       if (status != SILC_SKE_STATUS_OK) {
372         protocol->state = SILC_PROTOCOL_STATE_ERROR;
373         protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 300000);
374         silc_ske_free_key_material(keymat);
375         return;
376       }
377       ctx->keymat = keymat;
378
379       /* Send Ok to the other end if we are responder. If we are initiator
380          we have sent this already. */
381       if (ctx->responder == TRUE)
382         silc_ske_end(ctx->ske, silc_server_protocol_ke_send_packet, context);
383
384       /* Unregister the timeout task since the protocol has ended. 
385          This was the timeout task to be executed if the protocol is
386          not completed fast enough. */
387       if (ctx->timeout_task)
388         silc_task_unregister(server->timeout_queue, ctx->timeout_task);
389
390       /* Call the final callback */
391       if (protocol->final_callback)
392         protocol->execute_final(server->timeout_queue, 0, protocol, fd);
393       else
394         silc_protocol_free(protocol);
395     }
396     break;
397
398   case SILC_PROTOCOL_STATE_ERROR:
399     /*
400      * Error occured
401      */
402
403     /* Send abort notification */
404     silc_ske_abort(ctx->ske, ctx->ske->status, 
405                    silc_server_protocol_ke_send_packet,
406                    context);
407
408     /* Unregister the timeout task since the protocol has ended. 
409        This was the timeout task to be executed if the protocol is
410        not completed fast enough. */
411     if (ctx->timeout_task)
412       silc_task_unregister(server->timeout_queue, ctx->timeout_task);
413
414     /* On error the final callback is always called. */
415     if (protocol->final_callback)
416       protocol->execute_final(server->timeout_queue, 0, protocol, fd);
417     else
418       silc_protocol_free(protocol);
419     break;
420
421   case SILC_PROTOCOL_STATE_FAILURE:
422     /*
423      * We have received failure from remote
424      */
425
426     /* Unregister the timeout task since the protocol has ended. 
427        This was the timeout task to be executed if the protocol is
428        not completed fast enough. */
429     if (ctx->timeout_task)
430       silc_task_unregister(server->timeout_queue, ctx->timeout_task);
431
432     /* On error the final callback is always called. */
433     if (protocol->final_callback)
434       protocol->execute_final(server->timeout_queue, 0, protocol, fd);
435     else
436       silc_protocol_free(protocol);
437     break;
438
439   case SILC_PROTOCOL_STATE_UNKNOWN:
440     break;
441   }
442 }
443
444 /*
445  * Connection Authentication protocol functions
446  */
447
448 static int 
449 silc_server_password_authentication(SilcServer server, char *auth1, 
450                                     char *auth2)
451 {
452   if (!auth1 || !auth2)
453     return FALSE;
454
455   if (!memcmp(auth1, auth2, strlen(auth1)))
456     return TRUE;
457
458   return FALSE;
459 }
460
461 static int
462 silc_server_public_key_authentication(SilcServer server,
463                                       SilcPublicKey pub_key,
464                                       unsigned char *sign,
465                                       uint32 sign_len,
466                                       SilcSKE ske)
467 {
468   SilcPKCS pkcs;
469   int len;
470   SilcBuffer auth;
471
472   if (!pub_key || !sign)
473     return FALSE;
474
475   silc_pkcs_alloc(pub_key->name, &pkcs);
476   if (!silc_pkcs_public_key_set(pkcs, pub_key)) {
477     silc_pkcs_free(pkcs);
478     return FALSE;
479   }
480
481   /* Make the authentication data. Protocol says it is HASH plus
482      KE Start Payload. */
483   len = ske->hash_len + ske->start_payload_copy->len;
484   auth = silc_buffer_alloc(len);
485   silc_buffer_pull_tail(auth, len);
486   silc_buffer_format(auth,
487                      SILC_STR_UI_XNSTRING(ske->hash, ske->hash_len),
488                      SILC_STR_UI_XNSTRING(ske->start_payload_copy->data,
489                                           ske->start_payload_copy->len),
490                      SILC_STR_END);
491
492   /* Verify signature */
493   if (silc_pkcs_verify(pkcs, sign, sign_len, auth->data, auth->len)) {
494     silc_pkcs_free(pkcs);
495     silc_buffer_free(auth);
496     return TRUE;
497   }
498
499   silc_pkcs_free(pkcs);
500   silc_buffer_free(auth);
501   return FALSE;
502 }
503
504 static int
505 silc_server_get_public_key_auth(SilcServer server,
506                                 SilcPublicKey pub_key,
507                                 unsigned char *auth_data,
508                                 uint32 *auth_data_len,
509                                 SilcSKE ske)
510 {
511   int len;
512   SilcPKCS pkcs;
513   SilcBuffer auth;
514
515   if (!pub_key)
516     return FALSE;
517
518   silc_pkcs_alloc(pub_key->name, &pkcs);
519   if (!silc_pkcs_public_key_set(pkcs, pub_key)) {
520     silc_pkcs_free(pkcs);
521     return FALSE;
522   }
523
524   /* Make the authentication data. Protocol says it is HASH plus
525      KE Start Payload. */
526   len = ske->hash_len + ske->start_payload_copy->len;
527   auth = silc_buffer_alloc(len);
528   silc_buffer_pull_tail(auth, len);
529   silc_buffer_format(auth,
530                      SILC_STR_UI_XNSTRING(ske->hash, ske->hash_len),
531                      SILC_STR_UI_XNSTRING(ske->start_payload_copy->data,
532                                           ske->start_payload_copy->len),
533                      SILC_STR_END);
534
535   if (silc_pkcs_sign(pkcs, auth->data, auth->len, auth_data, auth_data_len)) {
536     silc_pkcs_free(pkcs);
537     silc_buffer_free(auth);
538     return TRUE;
539   }
540
541   silc_pkcs_free(pkcs);
542   silc_buffer_free(auth);
543   return FALSE;
544 }
545
546 /* Performs connection authentication protocol. If responder, we 
547    authenticate the remote data received. If initiator, we will send
548    authentication data to the remote end. */
549
550 SILC_TASK_CALLBACK(silc_server_protocol_connection_auth)
551 {
552   SilcProtocol protocol = (SilcProtocol)context;
553   SilcServerConnAuthInternalContext *ctx = 
554     (SilcServerConnAuthInternalContext *)protocol->context;
555   SilcServer server = (SilcServer)ctx->server;
556
557   SILC_LOG_DEBUG(("Start"));
558
559   if (protocol->state == SILC_PROTOCOL_STATE_UNKNOWN)
560     protocol->state = SILC_PROTOCOL_STATE_START;
561
562   SILC_LOG_DEBUG(("State=%d", protocol->state));
563
564   switch(protocol->state) {
565   case SILC_PROTOCOL_STATE_START:
566     {
567       /* 
568        * Start protocol.
569        */
570
571       if (ctx->responder == TRUE) {
572         /*
573          * We are receiving party
574          */
575         int ret;
576         uint16 payload_len;
577         uint16 conn_type;
578         unsigned char *auth_data = NULL;
579
580         SILC_LOG_INFO(("Performing authentication protocol for %s (%s)",
581                        ctx->sock->hostname, ctx->sock->ip));
582
583         /* Parse the received authentication data packet. The received
584            payload is Connection Auth Payload. */
585         ret = silc_buffer_unformat(ctx->packet->buffer,
586                                    SILC_STR_UI_SHORT(&payload_len),
587                                    SILC_STR_UI_SHORT(&conn_type),
588                                    SILC_STR_END);
589         if (ret == -1) {
590           SILC_LOG_DEBUG(("Bad payload in authentication packet"));
591           protocol->state = SILC_PROTOCOL_STATE_ERROR;
592           protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 300000);
593           return;
594         }
595         
596         if (payload_len != ctx->packet->buffer->len) {
597           SILC_LOG_DEBUG(("Bad payload in authentication packet"));
598           protocol->state = SILC_PROTOCOL_STATE_ERROR;
599           protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 300000);
600           return;
601         }
602         
603         payload_len -= 4;
604         
605         if (conn_type < SILC_SOCKET_TYPE_CLIENT || 
606             conn_type > SILC_SOCKET_TYPE_ROUTER) {
607           SILC_LOG_ERROR(("Bad connection type %d", conn_type));
608           protocol->state = SILC_PROTOCOL_STATE_ERROR;
609           protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 300000);
610           return;
611         }
612         
613         if (payload_len > 0) {
614           /* Get authentication data */
615           silc_buffer_pull(ctx->packet->buffer, 4);
616           ret = silc_buffer_unformat(ctx->packet->buffer,
617                                      SILC_STR_UI_XNSTRING_ALLOC(&auth_data, 
618                                                                 payload_len),
619                                      SILC_STR_END);
620           if (ret == -1) {
621             SILC_LOG_DEBUG(("Bad payload in authentication packet"));
622             protocol->state = SILC_PROTOCOL_STATE_ERROR;
623             protocol->execute(server->timeout_queue, 0, 
624                               protocol, fd, 0, 300000);
625             return;
626           }
627         }
628
629         /* 
630          * Check the remote connection type and make sure that we have
631          * configured this connection. If we haven't allowed this connection
632          * the authentication must be failed.
633          */
634
635         SILC_LOG_DEBUG(("Remote connection type %d", conn_type));
636
637         /* Remote end is client */
638         if (conn_type == SILC_SOCKET_TYPE_CLIENT) {
639           SilcServerConfigSectionClientConnection *client = NULL;
640           client = silc_server_config_find_client_conn(server->config,
641                                                        ctx->sock->ip,
642                                                        ctx->sock->port);
643           if (!client)
644             client = silc_server_config_find_client_conn(server->config,
645                                                          ctx->sock->hostname,
646                                                          ctx->sock->port);
647           
648           if (client) {
649             switch(client->auth_meth) {
650             case SILC_AUTH_NONE:
651               /* No authentication required */
652               SILC_LOG_DEBUG(("No authentication required"));
653               break;
654               
655             case SILC_AUTH_PASSWORD:
656               /* Password authentication */
657               SILC_LOG_DEBUG(("Password authentication"));
658               ret = silc_server_password_authentication(server, auth_data,
659                                                         client->auth_data);
660
661               if (ret)
662                 break;
663
664               /* Authentication failed */
665               SILC_LOG_ERROR(("Authentication failed"));
666               SILC_LOG_DEBUG(("Authentication failed"));
667               silc_free(auth_data);
668               protocol->state = SILC_PROTOCOL_STATE_ERROR;
669               protocol->execute(server->timeout_queue, 0, 
670                                 protocol, fd, 0, 300000);
671               return;
672               break;
673               
674             case SILC_AUTH_PUBLIC_KEY:
675               /* Public key authentication */
676               SILC_LOG_DEBUG(("Public key authentication"));
677               ret = silc_server_public_key_authentication(server, 
678                                                           client->auth_data,
679                                                           auth_data,
680                                                           payload_len, 
681                                                           ctx->ske);
682
683               if (ret)
684                 break;
685
686               SILC_LOG_ERROR(("Authentication failed"));
687               SILC_LOG_DEBUG(("Authentication failed"));
688               silc_free(auth_data);
689               protocol->state = SILC_PROTOCOL_STATE_ERROR;
690               protocol->execute(server->timeout_queue, 0, 
691                                 protocol, fd, 0, 300000);
692               return;
693             }
694           } else {
695             SILC_LOG_DEBUG(("No configuration for remote connection"));
696             SILC_LOG_ERROR(("Remote connection not configured"));
697             SILC_LOG_ERROR(("Authentication failed"));
698             silc_free(auth_data);
699             protocol->state = SILC_PROTOCOL_STATE_ERROR;
700             protocol->execute(server->timeout_queue, 0, 
701                               protocol, fd, 0, 300000);
702             return;
703           }
704         }
705         
706         /* Remote end is server */
707         if (conn_type == SILC_SOCKET_TYPE_SERVER) {
708           SilcServerConfigSectionServerConnection *serv = NULL;
709           serv = silc_server_config_find_server_conn(server->config,
710                                                      ctx->sock->ip,
711                                                      ctx->sock->port);
712           if (!serv)
713             serv = silc_server_config_find_server_conn(server->config,
714                                                        ctx->sock->hostname,
715                                                        ctx->sock->port);
716
717           if (serv) {
718             switch(serv->auth_meth) {
719             case SILC_AUTH_NONE:
720               /* No authentication required */
721               SILC_LOG_DEBUG(("No authentication required"));
722               break;
723               
724             case SILC_AUTH_PASSWORD:
725               /* Password authentication */
726               SILC_LOG_DEBUG(("Password authentication"));
727               ret = silc_server_password_authentication(server, auth_data,
728                                                         serv->auth_data);
729
730               if (ret)
731                 break;
732               
733               /* Authentication failed */
734               SILC_LOG_ERROR(("Authentication failed"));
735               SILC_LOG_DEBUG(("Authentication failed"));
736               silc_free(auth_data);
737               protocol->state = SILC_PROTOCOL_STATE_ERROR;
738               protocol->execute(server->timeout_queue, 0, 
739                                 protocol, fd, 0, 300000);
740               return;
741               break;
742
743             case SILC_AUTH_PUBLIC_KEY:
744               /* Public key authentication */
745               SILC_LOG_DEBUG(("Public key authentication"));
746               ret = silc_server_public_key_authentication(server, 
747                                                           serv->auth_data,
748                                                           auth_data,
749                                                           payload_len, 
750                                                           ctx->ske);
751                                                           
752               if (ret)
753                 break;
754
755               SILC_LOG_ERROR(("Authentication failed"));
756               SILC_LOG_DEBUG(("Authentication failed"));
757               silc_free(auth_data);
758               protocol->state = SILC_PROTOCOL_STATE_ERROR;
759               protocol->execute(server->timeout_queue, 0, 
760                                 protocol, fd, 0, 300000);
761               return;
762             }
763           } else {
764             SILC_LOG_DEBUG(("No configuration for remote connection"));
765             SILC_LOG_ERROR(("Remote connection not configured"));
766             SILC_LOG_ERROR(("Authentication failed"));
767             protocol->state = SILC_PROTOCOL_STATE_ERROR;
768             protocol->execute(server->timeout_queue, 0, 
769                               protocol, fd, 0, 300000);
770             silc_free(auth_data);
771             return;
772           }
773         }
774         
775         /* Remote end is router */
776         if (conn_type == SILC_SOCKET_TYPE_ROUTER) {
777           SilcServerConfigSectionServerConnection *serv = NULL;
778           serv = silc_server_config_find_router_conn(server->config,
779                                                      ctx->sock->ip,
780                                                      ctx->sock->port);
781           if (!serv)
782             serv = silc_server_config_find_router_conn(server->config,
783                                                        ctx->sock->hostname,
784                                                        ctx->sock->port);
785           
786           if (serv) {
787             switch(serv->auth_meth) {
788             case SILC_AUTH_NONE:
789               /* No authentication required */
790               SILC_LOG_DEBUG(("No authentication required"));
791               break;
792               
793             case SILC_AUTH_PASSWORD:
794               /* Password authentication */
795               SILC_LOG_DEBUG(("Password authentication"));
796               ret = silc_server_password_authentication(server, auth_data,
797                                                         serv->auth_data);
798
799               if (ret)
800                 break;
801               
802               /* Authentication failed */
803               SILC_LOG_ERROR(("Authentication failed"));
804               SILC_LOG_DEBUG(("Authentication failed"));
805               silc_free(auth_data);
806               protocol->state = SILC_PROTOCOL_STATE_ERROR;
807               protocol->execute(server->timeout_queue, 0, 
808                                 protocol, fd, 0, 300000);
809               return;
810               break;
811               
812             case SILC_AUTH_PUBLIC_KEY:
813               /* Public key authentication */
814               SILC_LOG_DEBUG(("Public key authentication"));
815               ret = silc_server_public_key_authentication(server, 
816                                                           serv->auth_data,
817                                                           auth_data,
818                                                           payload_len, 
819                                                           ctx->ske);
820                                                           
821               if (ret)
822                 break;
823               
824               SILC_LOG_ERROR(("Authentication failed"));
825               SILC_LOG_DEBUG(("Authentication failed"));
826               silc_free(auth_data);
827               protocol->state = SILC_PROTOCOL_STATE_ERROR;
828               protocol->execute(server->timeout_queue, 0, 
829                                 protocol, fd, 0, 300000);
830               return;
831             }
832           } else {
833             SILC_LOG_DEBUG(("No configuration for remote connection"));
834             SILC_LOG_ERROR(("Remote connection not configured"));
835             SILC_LOG_ERROR(("Authentication failed"));
836             silc_free(auth_data);
837             protocol->state = SILC_PROTOCOL_STATE_ERROR;
838             protocol->execute(server->timeout_queue, 0, 
839                               protocol, fd, 0, 300000);
840             return;
841           }
842         }
843         
844         silc_free(auth_data);
845
846         /* Save connection type. This is later used to create the
847            ID for the connection. */
848         ctx->conn_type = conn_type;
849           
850         /* Advance protocol state. */
851         protocol->state = SILC_PROTOCOL_STATE_END;
852         protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 0);
853
854       } else {
855         /* 
856          * We are initiator. We are authenticating ourselves to a
857          * remote server. We will send the authentication data to the
858          * other end for verify. 
859          */
860         SilcBuffer packet;
861         int payload_len = 0;
862         unsigned char *auth_data = NULL;
863         uint32 auth_data_len = 0;
864         
865         switch(ctx->auth_meth) {
866         case SILC_AUTH_NONE:
867           /* No authentication required */
868           break;
869           
870         case SILC_AUTH_PASSWORD:
871           /* Password authentication */
872           if (ctx->auth_data && ctx->auth_data_len) {
873             auth_data = strdup(ctx->auth_data);
874             auth_data_len = ctx->auth_data_len;
875             break;
876           }
877           break;
878           
879         case SILC_AUTH_PUBLIC_KEY:
880           {
881             unsigned char sign[1024];
882
883             /* Public key authentication */
884             silc_server_get_public_key_auth(server, ctx->auth_data,
885                                             sign, &auth_data_len,
886                                             ctx->ske);
887             auth_data = silc_calloc(auth_data_len, sizeof(*auth_data));
888             memcpy(auth_data, sign, auth_data_len);
889             break;
890           }
891         }
892         
893         payload_len = 4 + auth_data_len;
894         packet = silc_buffer_alloc(payload_len);
895         silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
896         silc_buffer_format(packet,
897                            SILC_STR_UI_SHORT(payload_len),
898                            SILC_STR_UI_SHORT(server->server_type 
899                                               == SILC_SERVER ?
900                                               SILC_SOCKET_TYPE_SERVER :
901                                               SILC_SOCKET_TYPE_ROUTER),
902                            SILC_STR_UI_XNSTRING(auth_data, auth_data_len),
903                            SILC_STR_END);
904         
905         /* Send the packet to server */
906         silc_server_packet_send(server, ctx->sock,
907                                 SILC_PACKET_CONNECTION_AUTH, 0, 
908                                 packet->data, packet->len, TRUE);
909         
910         if (auth_data) {
911           memset(auth_data, 0, auth_data_len);
912           silc_free(auth_data);
913         }
914         silc_buffer_free(packet);
915         
916         /* Next state is end of protocol */
917         protocol->state = SILC_PROTOCOL_STATE_END;
918       }
919     }
920     break;
921
922   case SILC_PROTOCOL_STATE_END:
923     {
924       /* 
925        * End protocol
926        */
927       unsigned char ok[4];
928
929       SILC_PUT32_MSB(SILC_AUTH_OK, ok);
930
931       /* Authentication successful */
932       silc_server_packet_send(server, ctx->sock, SILC_PACKET_SUCCESS,
933                               0, ok, 4, TRUE);
934
935       /* Unregister the timeout task since the protocol has ended. 
936          This was the timeout task to be executed if the protocol is
937          not completed fast enough. */
938       if (ctx->timeout_task)
939         silc_task_unregister(server->timeout_queue, ctx->timeout_task);
940
941       /* Protocol has ended, call the final callback */
942       if (protocol->final_callback)
943         protocol->execute_final(server->timeout_queue, 0, protocol, fd);
944       else
945         silc_protocol_free(protocol);
946     }
947     break;
948   case SILC_PROTOCOL_STATE_ERROR:
949     {
950       /*
951        * Error. Send notify to remote.
952        */
953       unsigned char error[4];
954
955       SILC_PUT32_MSB(SILC_AUTH_FAILED, error);
956
957       /* Authentication failed */
958       silc_server_packet_send(server, ctx->sock, SILC_PACKET_FAILURE,
959                               0, error, 4, TRUE);
960
961       /* Unregister the timeout task since the protocol has ended. 
962          This was the timeout task to be executed if the protocol is
963          not completed fast enough. */
964       if (ctx->timeout_task)
965         silc_task_unregister(server->timeout_queue, ctx->timeout_task);
966
967       /* On error the final callback is always called. */
968       if (protocol->final_callback)
969         protocol->execute_final(server->timeout_queue, 0, protocol, fd);
970       else
971         silc_protocol_free(protocol);
972     }
973     break;
974
975   case SILC_PROTOCOL_STATE_FAILURE:
976     /*
977      * We have received failure from remote
978      */
979
980     /* Unregister the timeout task since the protocol has ended. 
981        This was the timeout task to be executed if the protocol is
982        not completed fast enough. */
983     if (ctx->timeout_task)
984       silc_task_unregister(server->timeout_queue, ctx->timeout_task);
985
986     /* On error the final callback is always called. */
987     if (protocol->final_callback)
988       protocol->execute_final(server->timeout_queue, 0, protocol, fd);
989     else
990       silc_protocol_free(protocol);
991     break;
992
993   case SILC_PROTOCOL_STATE_UNKNOWN:
994     break;
995   }
996 }
997
998 /*
999  * Re-key protocol routines
1000  */
1001
1002 /* Actually takes the new keys into use. */
1003
1004 static void 
1005 silc_server_protocol_rekey_validate(SilcServer server,
1006                                     SilcServerRekeyInternalContext *ctx,
1007                                     SilcIDListData idata,
1008                                     SilcSKEKeyMaterial *keymat)
1009 {
1010   if (ctx->responder == TRUE) {
1011     silc_cipher_set_key(idata->send_key, keymat->receive_enc_key, 
1012                         keymat->enc_key_len);
1013     silc_cipher_set_iv(idata->send_key, keymat->receive_iv);
1014     silc_cipher_set_key(idata->receive_key, keymat->send_enc_key, 
1015                         keymat->enc_key_len);
1016     silc_cipher_set_iv(idata->receive_key, keymat->send_iv);
1017   } else {
1018     silc_cipher_set_key(idata->send_key, keymat->send_enc_key, 
1019                         keymat->enc_key_len);
1020     silc_cipher_set_iv(idata->send_key, keymat->send_iv);
1021     silc_cipher_set_key(idata->receive_key, keymat->receive_enc_key, 
1022                         keymat->enc_key_len);
1023     silc_cipher_set_iv(idata->receive_key, keymat->receive_iv);
1024   }
1025
1026   silc_hmac_set_key(idata->hmac, keymat->hmac_key, keymat->hmac_key_len);
1027
1028   /* Save the current sending encryption key */
1029   memset(idata->rekey->send_enc_key, 0, idata->rekey->enc_key_len);
1030   silc_free(idata->rekey->send_enc_key);
1031   idata->rekey->send_enc_key = 
1032     silc_calloc(keymat->enc_key_len / 8,
1033                 sizeof(*idata->rekey->send_enc_key));
1034   memcpy(idata->rekey->send_enc_key, keymat->send_enc_key, 
1035          keymat->enc_key_len / 8);
1036   idata->rekey->enc_key_len = keymat->enc_key_len / 8;
1037 }
1038
1039 /* This function actually re-generates (when not using PFS) the keys and
1040    takes them into use. */
1041
1042 void silc_server_protocol_rekey_generate(SilcServer server,
1043                                          SilcServerRekeyInternalContext *ctx)
1044 {
1045   SilcIDListData idata = (SilcIDListData)ctx->sock->user_data;
1046   SilcSKEKeyMaterial *keymat;
1047   uint32 key_len = silc_cipher_get_key_len(idata->send_key);
1048   uint32 hash_len = idata->hash->hash->hash_len;
1049
1050   SILC_LOG_DEBUG(("Generating new session keys (no PFS)"));
1051
1052   /* Generate the new key */
1053   keymat = silc_calloc(1, sizeof(*keymat));
1054   silc_ske_process_key_material_data(idata->rekey->send_enc_key,
1055                                      idata->rekey->enc_key_len,
1056                                      16, key_len, hash_len, 
1057                                      idata->hash, keymat);
1058
1059   /* Set the keys into use */
1060   silc_server_protocol_rekey_validate(server, ctx, idata, keymat);
1061
1062   silc_ske_free_key_material(keymat);
1063 }
1064
1065 /* This function actually re-generates (with PFS) the keys and
1066    takes them into use. */
1067
1068 void 
1069 silc_server_protocol_rekey_generate_pfs(SilcServer server,
1070                                         SilcServerRekeyInternalContext *ctx)
1071 {
1072   SilcIDListData idata = (SilcIDListData)ctx->sock->user_data;
1073   SilcSKEKeyMaterial *keymat;
1074   uint32 key_len = silc_cipher_get_key_len(idata->send_key);
1075   uint32 hash_len = idata->hash->hash->hash_len;
1076   unsigned char *tmpbuf;
1077   uint32 klen;
1078
1079   SILC_LOG_DEBUG(("Generating new session keys (with PFS)"));
1080
1081   /* Encode KEY to binary data */
1082   tmpbuf = silc_mp_mp2bin(ctx->ske->KEY, 0, &klen);
1083
1084   /* Generate the new key */
1085   keymat = silc_calloc(1, sizeof(*keymat));
1086   silc_ske_process_key_material_data(tmpbuf, klen, 16, key_len, hash_len, 
1087                                      idata->hash, keymat);
1088
1089   /* Set the keys into use */
1090   silc_server_protocol_rekey_validate(server, ctx, idata, keymat);
1091
1092   memset(tmpbuf, 0, klen);
1093   silc_free(tmpbuf);
1094   silc_ske_free_key_material(keymat);
1095 }
1096
1097 /* Packet sending callback. This function is provided as packet sending
1098    routine to the Key Exchange functions. */
1099
1100 static void 
1101 silc_server_protocol_rekey_send_packet(SilcSKE ske,
1102                                        SilcBuffer packet,
1103                                        SilcPacketType type,
1104                                        void *context)
1105 {
1106   SilcProtocol protocol = (SilcProtocol)context;
1107   SilcServerRekeyInternalContext *ctx = 
1108     (SilcServerRekeyInternalContext *)protocol->context;
1109   SilcServer server = (SilcServer)ctx->server;
1110
1111   /* Send the packet immediately */
1112   silc_server_packet_send(server, ctx->sock,
1113                           type, 0, packet->data, packet->len, TRUE);
1114 }
1115
1116 /* Performs re-key as defined in the SILC protocol specification. */
1117
1118 SILC_TASK_CALLBACK(silc_server_protocol_rekey)
1119 {
1120   SilcProtocol protocol = (SilcProtocol)context;
1121   SilcServerRekeyInternalContext *ctx = 
1122     (SilcServerRekeyInternalContext *)protocol->context;
1123   SilcServer server = (SilcServer)ctx->server;
1124   SilcIDListData idata = (SilcIDListData)ctx->sock->user_data;
1125   SilcSKEStatus status;
1126
1127   SILC_LOG_DEBUG(("Start"));
1128
1129   if (protocol->state == SILC_PROTOCOL_STATE_UNKNOWN)
1130     protocol->state = SILC_PROTOCOL_STATE_START;
1131
1132   SILC_LOG_DEBUG(("State=%d", protocol->state));
1133
1134   switch(protocol->state) {
1135   case SILC_PROTOCOL_STATE_START:
1136     {
1137       /* 
1138        * Start protocol.
1139        */
1140
1141       if (ctx->responder == TRUE) {
1142         /*
1143          * We are receiving party
1144          */
1145
1146         if (ctx->pfs == TRUE) {
1147           /* 
1148            * Use Perfect Forward Secrecy, ie. negotiate the key material
1149            * using the SKE protocol.
1150            */
1151
1152           if (ctx->packet->type != SILC_PACKET_KEY_EXCHANGE_1) {
1153             /* Error in protocol */
1154             protocol->state = SILC_PROTOCOL_STATE_ERROR;
1155             protocol->execute(server->timeout_queue, 0, protocol, fd, 
1156                               0, 300000);
1157           }
1158
1159           ctx->ske = silc_ske_alloc();
1160           ctx->ske->rng = server->rng;
1161           ctx->ske->prop = silc_calloc(1, sizeof(*ctx->ske->prop));
1162           silc_ske_get_group_by_number(idata->rekey->ske_group,
1163                                        &ctx->ske->prop->group);
1164
1165           status = silc_ske_responder_phase_2(ctx->ske, ctx->packet->buffer,
1166                                               NULL, NULL, NULL, NULL);
1167           if (status != SILC_SKE_STATUS_OK) {
1168             SILC_LOG_WARNING(("Error (type %d) during Re-key (PFS)",
1169                               status));
1170             
1171             protocol->state = SILC_PROTOCOL_STATE_ERROR;
1172             protocol->execute(server->timeout_queue, 0, 
1173                               protocol, fd, 0, 300000);
1174             return;
1175           }
1176
1177           /* Advance the protocol state */
1178           protocol->state++;
1179           protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 0);
1180         } else {
1181           /*
1182            * Do normal and simple re-key.
1183            */
1184
1185           /* Send the REKEY_DONE to indicate we will take new keys into use */
1186           silc_server_packet_send(server, ctx->sock, SILC_PACKET_REKEY_DONE,
1187                                   0, NULL, 0, FALSE);
1188
1189           /* The protocol ends in next stage. */
1190           protocol->state = SILC_PROTOCOL_STATE_END;
1191         }
1192       
1193       } else {
1194         /*
1195          * We are the initiator of this protocol
1196          */
1197
1198         /* Start the re-key by sending the REKEY packet */
1199         silc_server_packet_send(server, ctx->sock, SILC_PACKET_REKEY,
1200                                 0, NULL, 0, TRUE);
1201
1202         if (ctx->pfs == TRUE) {
1203           /* 
1204            * Use Perfect Forward Secrecy, ie. negotiate the key material
1205            * using the SKE protocol.
1206            */
1207           ctx->ske = silc_ske_alloc();
1208           ctx->ske->rng = server->rng;
1209           ctx->ske->prop = silc_calloc(1, sizeof(*ctx->ske->prop));
1210           silc_ske_get_group_by_number(idata->rekey->ske_group,
1211                                        &ctx->ske->prop->group);
1212
1213           status = 
1214             silc_ske_initiator_phase_2(ctx->ske, NULL, NULL,
1215                                        silc_server_protocol_rekey_send_packet,
1216                                        context);
1217
1218           if (status != SILC_SKE_STATUS_OK) {
1219             SILC_LOG_WARNING(("Error (type %d) during Re-key (PFS)",
1220                               status));
1221             
1222             protocol->state = SILC_PROTOCOL_STATE_ERROR;
1223             protocol->execute(server->timeout_queue, 0, 
1224                               protocol, fd, 0, 300000);
1225             return;
1226           }
1227
1228           /* Advance the protocol state */
1229           protocol->state++;
1230         } else {
1231           /*
1232            * Do normal and simple re-key.
1233            */
1234
1235           /* The protocol ends in next stage. We have sent the REKEY packet
1236              and now we just wait that the responder send REKEY_DONE and
1237              the we'll generate the new key, simple. */
1238           protocol->state = SILC_PROTOCOL_STATE_END;
1239         }
1240       }
1241     }
1242     break;
1243
1244   case 2:
1245     /*
1246      * Second state, used only when oding re-key with PFS.
1247      */
1248     if (ctx->responder == TRUE) {
1249       if (ctx->pfs == TRUE) {
1250         /*
1251          * Send our KE packe to the initiator now that we've processed
1252          * the initiator's KE packet.
1253          */
1254         status = 
1255           silc_ske_responder_finish(ctx->ske, NULL, NULL, 
1256                                     SILC_SKE_PK_TYPE_SILC,
1257                                     silc_server_protocol_rekey_send_packet,
1258                                     context);
1259
1260           if (status != SILC_SKE_STATUS_OK) {
1261             SILC_LOG_WARNING(("Error (type %d) during Re-key (PFS)",
1262                               status));
1263             
1264             protocol->state = SILC_PROTOCOL_STATE_ERROR;
1265             protocol->execute(server->timeout_queue, 0, 
1266                               protocol, fd, 0, 300000);
1267             return;
1268           }
1269       }
1270
1271     } else {
1272       if (ctx->pfs == TRUE) {
1273         /*
1274          * The packet type must be KE packet
1275          */
1276         if (ctx->packet->type != SILC_PACKET_KEY_EXCHANGE_2) {
1277           /* Error in protocol */
1278           protocol->state = SILC_PROTOCOL_STATE_ERROR;
1279           protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 300000);
1280         }
1281         
1282         status = silc_ske_initiator_finish(ctx->ske, ctx->packet->buffer,
1283                                            NULL, NULL, NULL, NULL);
1284         if (status != SILC_SKE_STATUS_OK) {
1285           SILC_LOG_WARNING(("Error (type %d) during Re-key (PFS)",
1286                             status));
1287           
1288           protocol->state = SILC_PROTOCOL_STATE_ERROR;
1289           protocol->execute(server->timeout_queue, 0, 
1290                             protocol, fd, 0, 300000);
1291           return;
1292         }
1293       }
1294     }
1295
1296     /* Send the REKEY_DONE to indicate we will take new keys into use 
1297        now. */ 
1298     silc_server_packet_send(server, ctx->sock, SILC_PACKET_REKEY_DONE,
1299                             0, NULL, 0, FALSE);
1300     
1301     /* The protocol ends in next stage. */
1302     protocol->state = SILC_PROTOCOL_STATE_END;
1303     break;
1304
1305   case SILC_PROTOCOL_STATE_END:
1306     /* 
1307      * End protocol
1308      */
1309
1310     if (ctx->packet->type != SILC_PACKET_REKEY_DONE) {
1311       /* Error in protocol */
1312       protocol->state = SILC_PROTOCOL_STATE_ERROR;
1313       protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 0);
1314     }
1315
1316     if (ctx->responder == FALSE) {
1317       if (ctx->pfs == FALSE) {
1318         /* Send the REKEY_DONE to indicate we will take new keys into use 
1319            now. */ 
1320         silc_server_packet_send(server, ctx->sock, SILC_PACKET_REKEY_DONE,
1321                                 0, NULL, 0, FALSE);
1322       }
1323     }
1324
1325     /* Protocol has ended, call the final callback */
1326     if (protocol->final_callback)
1327       protocol->execute_final(server->timeout_queue, 0, protocol, fd);
1328     else
1329       silc_protocol_free(protocol);
1330     break;
1331
1332   case SILC_PROTOCOL_STATE_ERROR:
1333     /*
1334      * Error occured
1335      */
1336
1337     if (ctx->pfs == TRUE) {
1338       /* Send abort notification */
1339       silc_ske_abort(ctx->ske, ctx->ske->status, 
1340                      silc_server_protocol_ke_send_packet,
1341                      context);
1342     }
1343
1344     /* On error the final callback is always called. */
1345     if (protocol->final_callback)
1346       protocol->execute_final(server->timeout_queue, 0, protocol, fd);
1347     else
1348       silc_protocol_free(protocol);
1349     break;
1350
1351   case SILC_PROTOCOL_STATE_FAILURE:
1352     /*
1353      * We have received failure from remote
1354      */
1355
1356     /* On error the final callback is always called. */
1357     if (protocol->final_callback)
1358       protocol->execute_final(server->timeout_queue, 0, protocol, fd);
1359     else
1360       silc_protocol_free(protocol);
1361     break;
1362
1363   case SILC_PROTOCOL_STATE_UNKNOWN:
1364     break;
1365   }
1366
1367 }
1368
1369 /* Registers protocols used in server. */
1370
1371 void silc_server_protocols_register(void)
1372 {
1373   silc_protocol_register(SILC_PROTOCOL_SERVER_CONNECTION_AUTH,
1374                          silc_server_protocol_connection_auth);
1375   silc_protocol_register(SILC_PROTOCOL_SERVER_KEY_EXCHANGE,
1376                          silc_server_protocol_key_exchange);
1377   silc_protocol_register(SILC_PROTOCOL_SERVER_REKEY,
1378                          silc_server_protocol_rekey);
1379 }
1380
1381 /* Unregisters protocols */
1382
1383 void silc_server_protocols_unregister(void)
1384 {
1385   silc_protocol_unregister(SILC_PROTOCOL_SERVER_CONNECTION_AUTH,
1386                            silc_server_protocol_connection_auth);
1387   silc_protocol_unregister(SILC_PROTOCOL_SERVER_KEY_EXCHANGE,
1388                            silc_server_protocol_key_exchange);
1389   silc_protocol_unregister(SILC_PROTOCOL_SERVER_REKEY,
1390                            silc_server_protocol_rekey);
1391 }