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
31 extern char *silc_version_string;
32
33 /*
34  * Key Exhange protocol functions
35  */
36
37 /* Packet sending callback. This function is provided as packet sending
38    routine to the Key Exchange functions. */
39
40 static void silc_server_protocol_ke_send_packet(SilcSKE ske,
41                                                 SilcBuffer packet,
42                                                 SilcPacketType type,
43                                                 void *context)
44 {
45   SilcProtocol protocol = (SilcProtocol)context;
46   SilcServerKEInternalContext *ctx = 
47     (SilcServerKEInternalContext *)protocol->context;
48   SilcServer server = (SilcServer)ctx->server;
49
50   /* Send the packet immediately */
51   silc_server_packet_send(server, ske->sock,
52                           type, 0, packet->data, packet->len, TRUE);
53 }
54
55 /* Sets the negotiated key material into use for particular connection. */
56
57 static int silc_server_protocol_ke_set_keys(SilcSKE ske,
58                                             SilcSocketConnection sock,
59                                             SilcSKEKeyMaterial *keymat,
60                                             SilcCipher cipher,
61                                             SilcPKCS pkcs,
62                                             SilcHash hash,
63                                             int is_responder)
64 {
65   SilcUnknownEntry conn_data;
66   SilcIDListData idata;
67   SilcHash nhash;
68
69   SILC_LOG_DEBUG(("Setting new key into use"));
70
71   conn_data = silc_calloc(1, sizeof(*conn_data));
72   idata = (SilcIDListData)conn_data;
73
74   /* Allocate cipher to be used in the communication */
75   if (!silc_cipher_alloc(cipher->cipher->name, &idata->send_key)) {
76     silc_free(conn_data);
77     return FALSE;
78   }
79   if (!silc_cipher_alloc(cipher->cipher->name, &idata->receive_key)) {
80     silc_free(conn_data);
81     return FALSE;
82   }
83   
84   if (is_responder == TRUE) {
85     idata->send_key->cipher->set_key(idata->send_key->context, 
86                                      keymat->receive_enc_key, 
87                                      keymat->enc_key_len);
88     idata->send_key->set_iv(idata->send_key, keymat->receive_iv);
89     idata->receive_key->cipher->set_key(idata->receive_key->context, 
90                                         keymat->send_enc_key, 
91                                         keymat->enc_key_len);
92     idata->receive_key->set_iv(idata->receive_key, keymat->send_iv);
93     
94   } else {
95     idata->send_key->cipher->set_key(idata->send_key->context, 
96                                      keymat->send_enc_key, 
97                                      keymat->enc_key_len);
98     idata->send_key->set_iv(idata->send_key, keymat->send_iv);
99     idata->receive_key->cipher->set_key(idata->receive_key->context, 
100                                         keymat->receive_enc_key, 
101                                         keymat->enc_key_len);
102     idata->receive_key->set_iv(idata->receive_key, keymat->receive_iv);
103   }
104
105   /* Allocate PKCS to be used */
106 #if 0
107   /* XXX Do we ever need to allocate PKCS for the connection??
108      If yes, we need to change KE protocol to get the initiators
109      public key. */
110   silc_pkcs_alloc(pkcs->pkcs->name, &idata->pkcs);
111   idata->public_key = silc_pkcs_public_key_alloc(XXX);
112   silc_pkcs_set_public_key(idata->pkcs, ske->ke2_payload->pk_data, 
113                            ske->ke2_payload->pk_len);
114 #endif
115
116   /* Save HMAC key to be used in the communication. */
117   if (!silc_hash_alloc(hash->hash->name, &nhash)) {
118     silc_cipher_free(idata->send_key);
119     silc_cipher_free(idata->receive_key);
120     silc_free(conn_data);
121     return FALSE;
122   }
123   silc_hmac_alloc(nhash, &idata->hmac);
124   silc_hmac_set_key(idata->hmac, keymat->hmac_key, keymat->hmac_key_len);
125
126   sock->user_data = (void *)conn_data;
127
128   return TRUE;
129 }
130
131 /* Check remote host version string */
132
133 SilcSKEStatus silc_ske_check_version(SilcSKE ske, unsigned char *version,
134                                      unsigned int len)
135 {
136   SilcSKEStatus status = SILC_SKE_STATUS_OK;
137
138   /* Check for initial version string */
139   if (!strstr(version, "SILC-1.0-"))
140     status = SILC_SKE_STATUS_BAD_VERSION;
141
142   /* Check software version */
143
144   if (len < strlen(silc_version_string))
145     status = SILC_SKE_STATUS_BAD_VERSION;
146
147   /* XXX for now there is no other tests due to the abnormal version
148      string that is used */
149
150   return status;
151 }
152
153 /* Performs key exchange protocol. This is used for both initiator
154    and responder key exchange. This is performed always when accepting
155    new connection to the server. This may be called recursively. */
156
157 SILC_TASK_CALLBACK(silc_server_protocol_key_exchange)
158 {
159   SilcProtocol protocol = (SilcProtocol)context;
160   SilcServerKEInternalContext *ctx = 
161     (SilcServerKEInternalContext *)protocol->context;
162   SilcServer server = (SilcServer)ctx->server;
163   SilcSKEStatus status = 0;
164
165   SILC_LOG_DEBUG(("Start"));
166
167   if (protocol->state == SILC_PROTOCOL_STATE_UNKNOWN)
168     protocol->state = SILC_PROTOCOL_STATE_START;
169
170   SILC_LOG_DEBUG(("State=%d", protocol->state));
171
172   switch(protocol->state) {
173   case SILC_PROTOCOL_STATE_START:
174     {
175       /*
176        * Start protocol
177        */
178       SilcSKE ske;
179
180       /* Allocate Key Exchange object */
181       ske = silc_ske_alloc();
182       ctx->ske = ske;
183       ske->rng = server->rng;
184       
185       if (ctx->responder == TRUE) {
186         /* Start the key exchange by processing the received security
187            properties packet from initiator. */
188         status = silc_ske_responder_start(ske, ctx->rng, ctx->sock,
189                                           silc_version_string,
190                                           ctx->packet->buffer, NULL, NULL);
191       } else {
192         SilcSKEStartPayload *start_payload;
193
194         /* Assemble security properties. */
195         silc_ske_assemble_security_properties(ske, SILC_SKE_SP_FLAG_NONE, 
196                                               silc_version_string,
197                                               &start_payload);
198
199         /* Start the key exchange by sending our security properties
200            to the remote end. */
201         status = silc_ske_initiator_start(ske, ctx->rng, ctx->sock,
202                                           start_payload,
203                                           silc_server_protocol_ke_send_packet,
204                                           context);
205       }
206
207       if (status != SILC_SKE_STATUS_OK) {
208         SILC_LOG_WARNING(("Error (type %d) during Key Exchange protocol",
209                           status));
210         SILC_LOG_DEBUG(("Error (type %d) during Key Exchange protocol",
211                         status));
212
213         protocol->state = SILC_PROTOCOL_STATE_ERROR;
214         protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 300000);
215         return;
216       }
217
218       /* Advance protocol state and call the next state if we are responder */
219       protocol->state++;
220       if (ctx->responder == TRUE)
221         protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 100000);
222     }
223     break;
224   case 2:
225     {
226       /* 
227        * Phase 1 
228        */
229       if (ctx->responder == TRUE) {
230         /* Sends the selected security properties to the initiator. */
231         status = 
232           silc_ske_responder_phase_1(ctx->ske, 
233                                      ctx->ske->start_payload,
234                                      silc_server_protocol_ke_send_packet,
235                                      context);
236       } else {
237         /* Call Phase-1 function. This processes the Key Exchange Start
238            paylaod reply we just got from the responder. The callback
239            function will receive the processed payload where we will
240            save it. */
241         status = silc_ske_initiator_phase_1(ctx->ske, ctx->packet->buffer,
242                                             NULL, NULL);
243       }
244
245       if (status != SILC_SKE_STATUS_OK) {
246         SILC_LOG_WARNING(("Error (type %d) during Key Exchange protocol",
247                           status));
248         SILC_LOG_DEBUG(("Error (type %d) during Key Exchange protocol",
249                         status));
250
251         protocol->state = SILC_PROTOCOL_STATE_ERROR;
252         protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 300000);
253         return;
254       }
255
256       /* Advance protocol state and call next state if we are initiator */
257       protocol->state++;
258       if (ctx->responder == FALSE)
259         protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 100000);
260     }
261     break;
262   case 3:
263     {
264       /* 
265        * Phase 2 
266        */
267       if (ctx->responder == TRUE) {
268         /* Process the received Key Exchange 1 Payload packet from
269            the initiator. This also creates our parts of the Diffie
270            Hellman algorithm. */
271         status = silc_ske_responder_phase_2(ctx->ske, ctx->packet->buffer, 
272                                             NULL, NULL);
273       } else {
274         /* Call the Phase-2 function. This creates Diffie Hellman
275            key exchange parameters and sends our public part inside
276            Key Exhange 1 Payload to the responder. */
277         status = 
278           silc_ske_initiator_phase_2(ctx->ske,
279                                      server->public_key,
280                                      silc_server_protocol_ke_send_packet,
281                                      context);
282       }
283
284       if (status != SILC_SKE_STATUS_OK) {
285         SILC_LOG_WARNING(("Error (type %d) during Key Exchange protocol",
286                           status));
287         SILC_LOG_DEBUG(("Error (type %d) during Key Exchange protocol",
288                         status));
289
290         protocol->state = SILC_PROTOCOL_STATE_ERROR;
291         protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 300000);
292         return;
293       }
294
295       /* Advance protocol state and call the next state if we are responder */
296       protocol->state++;
297       if (ctx->responder == TRUE)
298         protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 100000);
299     }
300     break;
301   case 4:
302     {
303       /* 
304        * Finish protocol
305        */
306       if (ctx->responder == TRUE) {
307         /* This creates the key exchange material and sends our
308            public parts to the initiator inside Key Exchange 2 Payload. */
309         status = 
310           silc_ske_responder_finish(ctx->ske, 
311                                     server->public_key, server->private_key,
312                                     SILC_SKE_PK_TYPE_SILC,
313                                     silc_server_protocol_ke_send_packet,
314                                     context);
315       } else {
316         /* Finish the protocol. This verifies the Key Exchange 2 payload
317            sent by responder. */
318         status = silc_ske_initiator_finish(ctx->ske, ctx->packet->buffer, 
319                                            NULL, NULL, NULL, NULL);
320       }
321
322       if (status != SILC_SKE_STATUS_OK) {
323         SILC_LOG_WARNING(("Error (type %d) during Key Exchange protocol",
324                           status));
325         SILC_LOG_DEBUG(("Error (type %d) during Key Exchange protocol",
326                         status));
327
328         protocol->state = SILC_PROTOCOL_STATE_ERROR;
329         protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 300000);
330         return;
331       }
332
333       /* Send Ok to the other end. We will end the protocol as responder
334          sends Ok to us when we will take the new keys into use. */
335       if (ctx->responder == FALSE)
336         silc_ske_end(ctx->ske, silc_server_protocol_ke_send_packet, context);
337
338       /* End the protocol on the next round */
339       protocol->state = SILC_PROTOCOL_STATE_END;
340     }
341     break;
342
343   case SILC_PROTOCOL_STATE_END:
344     {
345       /* 
346        * End protocol
347        */
348       SilcSKEKeyMaterial *keymat;
349
350       /* Send Ok to the other end if we are responder. If we are 
351          initiator we have sent this already. */
352       if (ctx->responder == TRUE)
353         silc_ske_end(ctx->ske, silc_server_protocol_ke_send_packet, context);
354
355       /* Process the key material */
356       keymat = silc_calloc(1, sizeof(*keymat));
357       silc_ske_process_key_material(ctx->ske, 16, (16 * 8), 16, keymat);
358
359       /* Take the new keys into use. */
360       if (!silc_server_protocol_ke_set_keys(ctx->ske, ctx->sock, keymat,
361                                             ctx->ske->prop->cipher,
362                                             ctx->ske->prop->pkcs,
363                                             ctx->ske->prop->hash,
364                                             ctx->responder)) {
365         protocol->state = SILC_PROTOCOL_STATE_ERROR;
366         protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 300000);
367         return;
368       }
369
370       /* Unregister the timeout task since the protocol has ended. 
371          This was the timeout task to be executed if the protocol is
372          not completed fast enough. */
373       if (ctx->timeout_task)
374         silc_task_unregister(server->timeout_queue, ctx->timeout_task);
375
376       /* Call the final callback */
377       if (protocol->final_callback)
378         protocol->execute_final(server->timeout_queue, 0, protocol, fd);
379       else
380         silc_protocol_free(protocol);
381     }
382     break;
383
384   case SILC_PROTOCOL_STATE_ERROR:
385     /*
386      * Error occured
387      */
388
389     /* Send abort notification */
390     silc_ske_abort(ctx->ske, ctx->ske->status, 
391                    silc_server_protocol_ke_send_packet,
392                    context);
393
394     /* Unregister the timeout task since the protocol has ended. 
395        This was the timeout task to be executed if the protocol is
396        not completed fast enough. */
397     if (ctx->timeout_task)
398       silc_task_unregister(server->timeout_queue, ctx->timeout_task);
399
400     /* On error the final callback is always called. */
401     if (protocol->final_callback)
402       protocol->execute_final(server->timeout_queue, 0, protocol, fd);
403     else
404       silc_protocol_free(protocol);
405     break;
406
407   case SILC_PROTOCOL_STATE_FAILURE:
408     /*
409      * We have received failure from remote
410      */
411
412     /* Unregister the timeout task since the protocol has ended. 
413        This was the timeout task to be executed if the protocol is
414        not completed fast enough. */
415     if (ctx->timeout_task)
416       silc_task_unregister(server->timeout_queue, ctx->timeout_task);
417
418     /* On error the final callback is always called. */
419     if (protocol->final_callback)
420       protocol->execute_final(server->timeout_queue, 0, protocol, fd);
421     else
422       silc_protocol_free(protocol);
423     break;
424
425   case SILC_PROTOCOL_STATE_UNKNOWN:
426     break;
427   }
428 }
429
430 /*
431  * Connection Authentication protocol functions
432  */
433
434 /* XXX move these to somehwere else */
435
436 int silc_server_password_authentication(SilcServer server, char *auth1, 
437                                         char *auth2)
438 {
439   if (!auth1 || !auth2)
440     return FALSE;
441
442   if (!memcmp(auth1, auth2, strlen(auth1)))
443     return TRUE;
444
445   return FALSE;
446 }
447
448 int silc_server_public_key_authentication(SilcServer server,
449                                           char *pkfile,
450                                           unsigned char *sign,
451                                           unsigned int sign_len,
452                                           SilcSKE ske)
453 {
454   SilcPublicKey pub_key;
455   SilcPKCS pkcs;
456   int len;
457   SilcBuffer auth;
458
459   if (!pkfile || !sign)
460     return FALSE;
461
462   /* Load public key from file */
463   if (!silc_pkcs_load_public_key(pkfile, &pub_key, SILC_PKCS_FILE_PEM))
464     if (!silc_pkcs_load_public_key(pkfile, &pub_key, SILC_PKCS_FILE_BIN))
465       return FALSE;
466
467   silc_pkcs_alloc(pub_key->name, &pkcs);
468   if (!silc_pkcs_public_key_set(pkcs, pub_key)) {
469     silc_pkcs_free(pkcs);
470     return FALSE;
471   }
472
473   /* Make the authentication data. Protocol says it is HASH plus
474      KE Start Payload. */
475   len = ske->hash_len + ske->start_payload_copy->len;
476   auth = silc_buffer_alloc(len);
477   silc_buffer_pull_tail(auth, len);
478   silc_buffer_format(auth,
479                      SILC_STR_UI_XNSTRING(ske->hash, ske->hash_len),
480                      SILC_STR_UI_XNSTRING(ske->start_payload_copy->data,
481                                           ske->start_payload_copy->len),
482                      SILC_STR_END);
483
484   /* Verify signature */
485   if (pkcs->pkcs->verify(pkcs->context, sign, sign_len,
486                          auth->data, auth->len))
487     {
488       silc_pkcs_free(pkcs);
489       silc_pkcs_public_key_free(pub_key);
490       silc_buffer_free(auth);
491       return TRUE;
492     }
493
494   silc_pkcs_free(pkcs);
495   silc_pkcs_public_key_free(pub_key);
496   silc_buffer_free(auth);
497   return FALSE;
498 }
499
500 /* Performs connection authentication protocol. If responder, we 
501    authenticate the remote data received. If initiator, we will send
502    authentication data to the remote end. */
503
504 SILC_TASK_CALLBACK(silc_server_protocol_connection_auth)
505 {
506   SilcProtocol protocol = (SilcProtocol)context;
507   SilcServerConnAuthInternalContext *ctx = 
508     (SilcServerConnAuthInternalContext *)protocol->context;
509   SilcServer server = (SilcServer)ctx->server;
510
511   SILC_LOG_DEBUG(("Start"));
512
513   if (protocol->state == SILC_PROTOCOL_STATE_UNKNOWN)
514     protocol->state = SILC_PROTOCOL_STATE_START;
515
516   SILC_LOG_DEBUG(("State=%d", protocol->state));
517
518   switch(protocol->state) {
519   case SILC_PROTOCOL_STATE_START:
520     {
521       /* 
522        * Start protocol.
523        */
524
525       if (ctx->responder == TRUE) {
526         /*
527          * We are receiving party
528          */
529         int ret;
530         unsigned short payload_len;
531         unsigned short conn_type;
532         unsigned char *auth_data;
533
534         SILC_LOG_INFO(("Performing authentication protocol for %s (%s)",
535                        ctx->sock->hostname, ctx->sock->ip));
536
537         /* Parse the received authentication data packet. The received
538            payload is Connection Auth Payload. */
539         ret = silc_buffer_unformat(ctx->packet->buffer,
540                                    SILC_STR_UI_SHORT(&payload_len),
541                                    SILC_STR_UI_SHORT(&conn_type),
542                                    SILC_STR_END);
543         if (ret == -1) {
544           SILC_LOG_DEBUG(("Bad payload in authentication packet"));
545           protocol->state = SILC_PROTOCOL_STATE_ERROR;
546           protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 300000);
547           return;
548         }
549         
550         if (payload_len != ctx->packet->buffer->len) {
551           SILC_LOG_DEBUG(("Bad payload in authentication packet"));
552           protocol->state = SILC_PROTOCOL_STATE_ERROR;
553           protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 300000);
554           return;
555         }
556         
557         payload_len -= 4;
558         
559         if (conn_type < SILC_SOCKET_TYPE_CLIENT || 
560             conn_type > SILC_SOCKET_TYPE_ROUTER) {
561           SILC_LOG_ERROR(("Bad connection type %d", conn_type));
562           protocol->state = SILC_PROTOCOL_STATE_ERROR;
563           protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 300000);
564           return;
565         }
566         
567         if (payload_len > 0) {
568           /* Get authentication data */
569           silc_buffer_pull(ctx->packet->buffer, 4);
570           ret = silc_buffer_unformat(ctx->packet->buffer,
571                                      SILC_STR_UI_XNSTRING_ALLOC(&auth_data, 
572                                                                 payload_len),
573                                      SILC_STR_END);
574           if (ret == -1) {
575             SILC_LOG_DEBUG(("Bad payload in authentication packet"));
576             protocol->state = SILC_PROTOCOL_STATE_ERROR;
577             protocol->execute(server->timeout_queue, 0, 
578                               protocol, fd, 0, 300000);
579             return;
580           }
581         } else {
582           auth_data = NULL;
583         }
584
585         /* 
586          * Check the remote connection type and make sure that we have
587          * configured this connection. If we haven't allowed this connection
588          * the authentication must be failed.
589          */
590
591         SILC_LOG_DEBUG(("Remote connection type %d", conn_type));
592
593         /* Remote end is client */
594         if (conn_type == SILC_SOCKET_TYPE_CLIENT) {
595           SilcConfigServerSectionClientConnection *client = NULL;
596           client = 
597             silc_config_server_find_client_conn(server->config,
598                                                 ctx->sock->ip,
599                                                 ctx->sock->port);
600           if (!client)
601             client = 
602               silc_config_server_find_client_conn(server->config,
603                                                   ctx->sock->hostname,
604                                                   ctx->sock->port);
605           
606           if (client) {
607             switch(client->auth_meth) {
608             case SILC_AUTH_NONE:
609               /* No authentication required */
610               SILC_LOG_DEBUG(("No authentication required"));
611               break;
612               
613             case SILC_AUTH_PASSWORD:
614               /* Password authentication */
615               SILC_LOG_DEBUG(("Password authentication"));
616               ret = silc_server_password_authentication(server, auth_data,
617                                                         client->auth_data);
618
619               if (ret) {
620                 memset(auth_data, 0, payload_len);
621                 silc_free(auth_data);
622                 auth_data = NULL;
623                 break;
624               }
625
626               /* Authentication failed */
627               SILC_LOG_ERROR(("Authentication failed"));
628               SILC_LOG_DEBUG(("Authentication failed"));
629               protocol->state = SILC_PROTOCOL_STATE_ERROR;
630               protocol->execute(server->timeout_queue, 0, 
631                                 protocol, fd, 0, 300000);
632               return;
633               break;
634               
635             case SILC_AUTH_PUBLIC_KEY:
636               /* Public key authentication */
637               SILC_LOG_DEBUG(("Public key authentication"));
638               ret = silc_server_public_key_authentication(server, 
639                                                           client->auth_data,
640                                                           auth_data,
641                                                           payload_len, 
642                                                           ctx->ske);
643                                                           
644               if (ret) {
645                 memset(auth_data, 0, payload_len);
646                 silc_free(auth_data);
647                 auth_data = NULL;
648                 break;
649               }
650
651               SILC_LOG_ERROR(("Authentication failed"));
652               SILC_LOG_DEBUG(("Authentication failed"));
653               protocol->state = SILC_PROTOCOL_STATE_ERROR;
654               protocol->execute(server->timeout_queue, 0, 
655                                 protocol, fd, 0, 300000);
656               return;
657             }
658           } else {
659             SILC_LOG_DEBUG(("No configuration for remote connection"));
660             SILC_LOG_ERROR(("Remote connection not configured"));
661             SILC_LOG_ERROR(("Authentication failed"));
662             memset(auth_data, 0, payload_len);
663             silc_free(auth_data);
664             auth_data = NULL;
665             protocol->state = SILC_PROTOCOL_STATE_ERROR;
666             protocol->execute(server->timeout_queue, 0, 
667                               protocol, fd, 0, 300000);
668             return;
669           }
670         }
671         
672         /* Remote end is server */
673         if (conn_type == SILC_SOCKET_TYPE_SERVER) {
674           SilcConfigServerSectionServerConnection *serv = NULL;
675           serv = 
676             silc_config_server_find_server_conn(server->config,
677                                                 ctx->sock->ip,
678                                                 ctx->sock->port);
679           if (!serv)
680             serv = 
681               silc_config_server_find_server_conn(server->config,
682                                                   ctx->sock->hostname,
683                                                   ctx->sock->port);
684           
685           if (serv) {
686             switch(serv->auth_meth) {
687             case SILC_AUTH_NONE:
688               /* No authentication required */
689               SILC_LOG_DEBUG(("No authentication required"));
690               break;
691               
692             case SILC_AUTH_PASSWORD:
693               /* Password authentication */
694               SILC_LOG_DEBUG(("Password authentication"));
695               ret = silc_server_password_authentication(server, auth_data,
696                                                         serv->auth_data);
697
698               if (ret) {
699                 memset(auth_data, 0, payload_len);
700                 silc_free(auth_data);
701                 auth_data = NULL;
702                 break;
703               }
704               
705               /* Authentication failed */
706               SILC_LOG_ERROR(("Authentication failed"));
707               SILC_LOG_DEBUG(("Authentication failed"));
708               protocol->state = SILC_PROTOCOL_STATE_ERROR;
709               protocol->execute(server->timeout_queue, 0, 
710                                 protocol, fd, 0, 300000);
711               return;
712               break;
713               
714             case SILC_AUTH_PUBLIC_KEY:
715               /* Public key authentication */
716               SILC_LOG_DEBUG(("Public key authentication"));
717               ret = silc_server_public_key_authentication(server, 
718                                                           serv->auth_data,
719                                                           auth_data,
720                                                           payload_len, 
721                                                           ctx->ske);
722                                                           
723               if (ret) {
724                 memset(auth_data, 0, payload_len);
725                 silc_free(auth_data);
726                 auth_data = NULL;
727                 break;
728               }
729
730               SILC_LOG_ERROR(("Authentication failed"));
731               SILC_LOG_DEBUG(("Authentication failed"));
732               protocol->state = SILC_PROTOCOL_STATE_ERROR;
733               protocol->execute(server->timeout_queue, 0, 
734                                 protocol, fd, 0, 300000);
735               return;
736             }
737           } else {
738             SILC_LOG_DEBUG(("No configuration for remote connection"));
739             SILC_LOG_ERROR(("Remote connection not configured"));
740             SILC_LOG_ERROR(("Authentication failed"));
741             memset(auth_data, 0, payload_len);
742             silc_free(auth_data);
743             auth_data = NULL;
744             protocol->state = SILC_PROTOCOL_STATE_ERROR;
745             protocol->execute(server->timeout_queue, 0, 
746                               protocol, fd, 0, 300000);
747             return;
748           }
749         }
750         
751         /* Remote end is router */
752         if (conn_type == SILC_SOCKET_TYPE_ROUTER) {
753           SilcConfigServerSectionServerConnection *serv = NULL;
754           serv = 
755             silc_config_server_find_router_conn(server->config,
756                                                 ctx->sock->ip,
757                                                 ctx->sock->port);
758           if (!serv)
759             serv = 
760               silc_config_server_find_router_conn(server->config,
761                                                   ctx->sock->hostname,
762                                                   ctx->sock->port);
763           
764           if (serv) {
765             switch(serv->auth_meth) {
766             case SILC_AUTH_NONE:
767               /* No authentication required */
768               SILC_LOG_DEBUG(("No authentication required"));
769               break;
770               
771             case SILC_AUTH_PASSWORD:
772               /* Password authentication */
773               SILC_LOG_DEBUG(("Password authentication"));
774               ret = silc_server_password_authentication(server, auth_data,
775                                                         serv->auth_data);
776
777               if (ret) {
778                 memset(auth_data, 0, payload_len);
779                 silc_free(auth_data);
780                 auth_data = NULL;
781                 break;
782               }
783               
784               /* Authentication failed */
785               SILC_LOG_ERROR(("Authentication failed"));
786               SILC_LOG_DEBUG(("Authentication failed"));
787               protocol->state = SILC_PROTOCOL_STATE_ERROR;
788               protocol->execute(server->timeout_queue, 0, 
789                                 protocol, fd, 0, 300000);
790               return;
791               break;
792               
793             case SILC_AUTH_PUBLIC_KEY:
794               /* Public key authentication */
795               SILC_LOG_DEBUG(("Public key authentication"));
796               ret = silc_server_public_key_authentication(server, 
797                                                           serv->auth_data,
798                                                           auth_data,
799                                                           payload_len, 
800                                                           ctx->ske);
801                                                           
802               if (ret) {
803                 memset(auth_data, 0, payload_len);
804                 silc_free(auth_data);
805                 auth_data = NULL;
806                 break;
807               }
808
809               SILC_LOG_ERROR(("Authentication failed"));
810               SILC_LOG_DEBUG(("Authentication failed"));
811               protocol->state = SILC_PROTOCOL_STATE_ERROR;
812               protocol->execute(server->timeout_queue, 0, 
813                                 protocol, fd, 0, 300000);
814               return;
815             }
816           } else {
817             SILC_LOG_DEBUG(("No configuration for remote connection"));
818             SILC_LOG_ERROR(("Remote connection not configured"));
819             SILC_LOG_ERROR(("Authentication failed"));
820             memset(auth_data, 0, payload_len);
821             silc_free(auth_data);
822             auth_data = NULL;
823             protocol->state = SILC_PROTOCOL_STATE_ERROR;
824             protocol->execute(server->timeout_queue, 0, 
825                               protocol, fd, 0, 300000);
826             return;
827           }
828         }
829         
830         if (auth_data) {
831           memset(auth_data, 0, payload_len);
832           silc_free(auth_data);
833         }
834         
835         /* Save connection type. This is later used to create the
836            ID for the connection. */
837         ctx->conn_type = conn_type;
838           
839         /* Advance protocol state. */
840         protocol->state = SILC_PROTOCOL_STATE_END;
841         protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 0);
842
843       } else {
844         /* 
845          * We are initiator. We are authenticating ourselves to a
846          * remote server. We will send the authentication data to the
847          * other end for verify. 
848          */
849         SilcBuffer packet;
850         int payload_len = 0;
851         unsigned char *auth_data = NULL;
852         unsigned int auth_data_len = 0;
853         
854         switch(ctx->auth_meth) {
855         case SILC_AUTH_NONE:
856           /* No authentication required */
857           break;
858           
859         case SILC_AUTH_PASSWORD:
860           /* Password authentication */
861           if (ctx->auth_data && ctx->auth_data_len) {
862             auth_data = ctx->auth_data;
863             auth_data_len = ctx->auth_data_len;
864             break;
865           }
866
867           /* No authentication data exits. Ask interactively from user. */
868           /* XXX */
869
870           break;
871           
872         case SILC_AUTH_PUBLIC_KEY:
873           /* Public key authentication */
874           /* XXX TODO */
875           break;
876         }
877         
878         payload_len = 4 + auth_data_len;
879         packet = silc_buffer_alloc(payload_len);
880         silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
881         silc_buffer_format(packet,
882                            SILC_STR_UI_SHORT(payload_len),
883                            SILC_STR_UI_SHORT(server->server_type 
884                                               == SILC_SERVER ?
885                                               SILC_SOCKET_TYPE_SERVER :
886                                               SILC_SOCKET_TYPE_ROUTER),
887                            SILC_STR_UI_XNSTRING(auth_data, auth_data_len),
888                            SILC_STR_END);
889         
890         /* Send the packet to server */
891         silc_server_packet_send(server, ctx->sock,
892                                 SILC_PACKET_CONNECTION_AUTH, 0, 
893                                 packet->data, packet->len, TRUE);
894         
895         if (auth_data) {
896           memset(auth_data, 0, auth_data_len);
897           silc_free(auth_data);
898         }
899         silc_buffer_free(packet);
900         
901         /* Next state is end of protocol */
902         protocol->state = SILC_PROTOCOL_STATE_END;
903       }
904     }
905     break;
906
907   case SILC_PROTOCOL_STATE_END:
908     {
909       /* 
910        * End protocol
911        */
912       unsigned char ok[4];
913
914       SILC_PUT32_MSB(SILC_AUTH_OK, ok);
915
916       /* Authentication failed */
917       silc_server_packet_send(server, ctx->sock, SILC_PACKET_SUCCESS,
918                               0, ok, 4, TRUE);
919
920       /* Unregister the timeout task since the protocol has ended. 
921          This was the timeout task to be executed if the protocol is
922          not completed fast enough. */
923       if (ctx->timeout_task)
924         silc_task_unregister(server->timeout_queue, ctx->timeout_task);
925
926       /* Protocol has ended, call the final callback */
927       if (protocol->final_callback)
928         protocol->execute_final(server->timeout_queue, 0, protocol, fd);
929       else
930         silc_protocol_free(protocol);
931     }
932     break;
933   case SILC_PROTOCOL_STATE_ERROR:
934     {
935       /*
936        * Error. Send notify to remote.
937        */
938       unsigned char error[4];
939
940       SILC_PUT32_MSB(SILC_AUTH_FAILED, error);
941
942       /* Authentication failed */
943       silc_server_packet_send(server, ctx->sock, SILC_PACKET_FAILURE,
944                               0, error, 4, TRUE);
945
946       /* Unregister the timeout task since the protocol has ended. 
947          This was the timeout task to be executed if the protocol is
948          not completed fast enough. */
949       if (ctx->timeout_task)
950         silc_task_unregister(server->timeout_queue, ctx->timeout_task);
951
952       /* On error the final callback is always called. */
953       if (protocol->final_callback)
954         protocol->execute_final(server->timeout_queue, 0, protocol, fd);
955       else
956         silc_protocol_free(protocol);
957     }
958     break;
959
960   case SILC_PROTOCOL_STATE_FAILURE:
961     /*
962      * We have received failure from remote
963      */
964
965     /* Unregister the timeout task since the protocol has ended. 
966        This was the timeout task to be executed if the protocol is
967        not completed fast enough. */
968     if (ctx->timeout_task)
969       silc_task_unregister(server->timeout_queue, ctx->timeout_task);
970
971     /* On error the final callback is always called. */
972     if (protocol->final_callback)
973       protocol->execute_final(server->timeout_queue, 0, protocol, fd);
974     else
975       silc_protocol_free(protocol);
976     break;
977
978   case SILC_PROTOCOL_STATE_UNKNOWN:
979     break;
980   }
981 }
982
983 /* Registers protocols used in server. */
984
985 void silc_server_protocols_register(void)
986 {
987   silc_protocol_register(SILC_PROTOCOL_SERVER_CONNECTION_AUTH,
988                          silc_server_protocol_connection_auth);
989   silc_protocol_register(SILC_PROTOCOL_SERVER_KEY_EXCHANGE,
990                          silc_server_protocol_key_exchange);
991 }
992
993 /* Unregisters protocols */
994
995 void silc_server_protocols_unregister(void)
996 {
997   silc_protocol_unregister(SILC_PROTOCOL_SERVER_CONNECTION_AUTH,
998                            silc_server_protocol_connection_auth);
999   silc_protocol_unregister(SILC_PROTOCOL_SERVER_KEY_EXCHANGE,
1000                            silc_server_protocol_key_exchange);
1001 }