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