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