5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
7 Copyright (C) 1997 - 2001 Pekka Riikonen
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.
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.
21 * Server side of the protocols.
25 #include "serverincludes.h"
26 #include "server_internal.h"
28 SILC_TASK_CALLBACK(silc_server_protocol_connection_auth);
29 SILC_TASK_CALLBACK(silc_server_protocol_key_exchange);
31 extern char *silc_version_string;
34 * Key Exhange protocol functions
37 /* Packet sending callback. This function is provided as packet sending
38 routine to the Key Exchange functions. */
40 static void silc_server_protocol_ke_send_packet(SilcSKE ske,
45 SilcProtocol protocol = (SilcProtocol)context;
46 SilcServerKEInternalContext *ctx =
47 (SilcServerKEInternalContext *)protocol->context;
48 SilcServer server = (SilcServer)ctx->server;
50 /* Send the packet immediately */
51 silc_server_packet_send(server, ske->sock,
52 type, 0, packet->data, packet->len, TRUE);
55 /* Sets the negotiated key material into use for particular connection. */
57 int silc_server_protocol_ke_set_keys(SilcSKE ske,
58 SilcSocketConnection sock,
59 SilcSKEKeyMaterial *keymat,
66 SilcUnknownEntry conn_data;
69 SILC_LOG_DEBUG(("Setting new key into use"));
71 conn_data = silc_calloc(1, sizeof(*conn_data));
72 idata = (SilcIDListData)conn_data;
74 /* Allocate cipher to be used in the communication */
75 if (!silc_cipher_alloc(cipher->cipher->name, &idata->send_key)) {
79 if (!silc_cipher_alloc(cipher->cipher->name, &idata->receive_key)) {
84 if (is_responder == TRUE) {
85 silc_cipher_set_key(idata->send_key, keymat->receive_enc_key,
87 silc_cipher_set_iv(idata->send_key, keymat->receive_iv);
88 silc_cipher_set_key(idata->receive_key, keymat->send_enc_key,
90 silc_cipher_set_iv(idata->receive_key, keymat->send_iv);
92 silc_cipher_set_key(idata->send_key, keymat->send_enc_key,
94 silc_cipher_set_iv(idata->send_key, keymat->send_iv);
95 silc_cipher_set_key(idata->receive_key, keymat->receive_enc_key,
97 silc_cipher_set_iv(idata->receive_key, keymat->receive_iv);
100 /* Note that for responder the initiator's sending key is receiving key */
101 idata->rekey = silc_calloc(1, sizeof(*idata->rekey));
102 idata->rekey->send_enc_key =
103 silc_calloc(keymat->enc_key_len / 8,
104 sizeof(*idata->rekey->send_enc_key));
105 memcpy(idata->rekey->send_enc_key,
106 keymat->send_enc_key, keymat->enc_key_len / 8);
107 idata->rekey->enc_key_len = keymat->enc_key_len / 8;
109 if (ske->start_payload->flags & SILC_SKE_SP_FLAG_PFS)
110 idata->rekey->pfs = TRUE;
112 /* Save the remote host's public key */
113 silc_pkcs_public_key_decode(ske->ke1_payload->pk_data,
114 ske->ke1_payload->pk_len, &idata->public_key);
117 if (!silc_hash_alloc(hash->hash->name, &idata->hash)) {
118 silc_cipher_free(idata->send_key);
119 silc_cipher_free(idata->receive_key);
120 silc_free(conn_data);
124 /* Save HMAC key to be used in the communication. */
125 if (!silc_hmac_alloc(hmac->hmac->name, NULL, &idata->hmac)) {
126 silc_cipher_free(idata->send_key);
127 silc_cipher_free(idata->receive_key);
128 silc_hash_free(idata->hash);
129 silc_free(conn_data);
132 silc_hmac_set_key(idata->hmac, keymat->hmac_key, keymat->hmac_key_len);
134 sock->user_data = (void *)conn_data;
139 /* Check remote host version string */
141 SilcSKEStatus silc_ske_check_version(SilcSKE ske, unsigned char *version,
144 SilcSKEStatus status = SILC_SKE_STATUS_OK;
146 SILC_LOG_INFO(("%s (%s) is version %s", ske->sock->hostname,
147 ske->sock->ip, version));
149 /* Check for initial version string */
150 if (!strstr(version, "SILC-1.0-"))
151 status = SILC_SKE_STATUS_BAD_VERSION;
153 /* Check software version */
155 if (len < strlen(silc_version_string))
156 status = SILC_SKE_STATUS_BAD_VERSION;
158 /* XXX for now there is no other tests due to the abnormal version
159 string that is used */
164 /* Performs key exchange protocol. This is used for both initiator
165 and responder key exchange. This is performed always when accepting
166 new connection to the server. This may be called recursively. */
168 SILC_TASK_CALLBACK(silc_server_protocol_key_exchange)
170 SilcProtocol protocol = (SilcProtocol)context;
171 SilcServerKEInternalContext *ctx =
172 (SilcServerKEInternalContext *)protocol->context;
173 SilcServer server = (SilcServer)ctx->server;
174 SilcSKEStatus status = 0;
176 SILC_LOG_DEBUG(("Start"));
178 if (protocol->state == SILC_PROTOCOL_STATE_UNKNOWN)
179 protocol->state = SILC_PROTOCOL_STATE_START;
181 SILC_LOG_DEBUG(("State=%d", protocol->state));
183 switch(protocol->state) {
184 case SILC_PROTOCOL_STATE_START:
191 /* Allocate Key Exchange object */
192 ske = silc_ske_alloc();
194 ske->rng = server->rng;
196 if (ctx->responder == TRUE) {
197 /* Start the key exchange by processing the received security
198 properties packet from initiator. */
199 status = silc_ske_responder_start(ske, ctx->rng, ctx->sock,
201 ctx->packet->buffer, FALSE,
204 SilcSKEStartPayload *start_payload;
206 /* Assemble security properties. */
207 silc_ske_assemble_security_properties(ske, SILC_SKE_SP_FLAG_NONE,
211 /* Start the key exchange by sending our security properties
212 to the remote end. */
213 status = silc_ske_initiator_start(ske, ctx->rng, ctx->sock,
215 silc_server_protocol_ke_send_packet,
219 if (status != SILC_SKE_STATUS_OK) {
220 SILC_LOG_WARNING(("Error (type %d) during Key Exchange protocol",
222 SILC_LOG_DEBUG(("Error (type %d) during Key Exchange protocol",
225 protocol->state = SILC_PROTOCOL_STATE_ERROR;
226 protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 300000);
230 /* Advance protocol state and call the next state if we are responder */
232 if (ctx->responder == TRUE)
233 protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 100000);
241 if (ctx->responder == TRUE) {
242 /* Sends the selected security properties to the initiator. */
244 silc_ske_responder_phase_1(ctx->ske,
245 ctx->ske->start_payload,
246 silc_server_protocol_ke_send_packet,
249 /* Call Phase-1 function. This processes the Key Exchange Start
250 paylaod reply we just got from the responder. The callback
251 function will receive the processed payload where we will
253 status = silc_ske_initiator_phase_1(ctx->ske, ctx->packet->buffer,
257 if (status != SILC_SKE_STATUS_OK) {
258 SILC_LOG_WARNING(("Error (type %d) during Key Exchange protocol",
260 SILC_LOG_DEBUG(("Error (type %d) during Key Exchange protocol",
263 protocol->state = SILC_PROTOCOL_STATE_ERROR;
264 protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 300000);
268 /* Advance protocol state and call next state if we are initiator */
270 if (ctx->responder == FALSE)
271 protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 100000);
279 if (ctx->responder == TRUE) {
280 /* Process the received Key Exchange 1 Payload packet from
281 the initiator. This also creates our parts of the Diffie
282 Hellman algorithm. */
283 status = silc_ske_responder_phase_2(ctx->ske, ctx->packet->buffer,
284 NULL, NULL, NULL, NULL);
286 /* Call the Phase-2 function. This creates Diffie Hellman
287 key exchange parameters and sends our public part inside
288 Key Exhange 1 Payload to the responder. */
290 silc_ske_initiator_phase_2(ctx->ske,
293 silc_server_protocol_ke_send_packet,
297 if (status != SILC_SKE_STATUS_OK) {
298 SILC_LOG_WARNING(("Error (type %d) during Key Exchange protocol",
300 SILC_LOG_DEBUG(("Error (type %d) during Key Exchange protocol",
303 protocol->state = SILC_PROTOCOL_STATE_ERROR;
304 protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 300000);
308 /* Advance protocol state and call the next state if we are responder */
310 if (ctx->responder == TRUE)
311 protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 100000);
319 if (ctx->responder == TRUE) {
320 /* This creates the key exchange material and sends our
321 public parts to the initiator inside Key Exchange 2 Payload. */
323 silc_ske_responder_finish(ctx->ske,
324 server->public_key, server->private_key,
325 SILC_SKE_PK_TYPE_SILC,
326 silc_server_protocol_ke_send_packet,
329 /* Finish the protocol. This verifies the Key Exchange 2 payload
330 sent by responder. */
331 status = silc_ske_initiator_finish(ctx->ske, ctx->packet->buffer,
332 NULL, NULL, NULL, NULL);
335 if (status != SILC_SKE_STATUS_OK) {
336 SILC_LOG_WARNING(("Error (type %d) during Key Exchange protocol",
338 SILC_LOG_DEBUG(("Error (type %d) during Key Exchange protocol",
341 protocol->state = SILC_PROTOCOL_STATE_ERROR;
342 protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 300000);
346 /* Send Ok to the other end. We will end the protocol as responder
347 sends Ok to us when we will take the new keys into use. */
348 if (ctx->responder == FALSE)
349 silc_ske_end(ctx->ske, silc_server_protocol_ke_send_packet, context);
351 /* End the protocol on the next round */
352 protocol->state = SILC_PROTOCOL_STATE_END;
356 case SILC_PROTOCOL_STATE_END:
361 SilcSKEKeyMaterial *keymat;
362 int key_len = silc_cipher_get_key_len(ctx->ske->prop->cipher);
363 int hash_len = ctx->ske->prop->hash->hash->hash_len;
365 /* Process the key material */
366 keymat = silc_calloc(1, sizeof(*keymat));
367 status = silc_ske_process_key_material(ctx->ske, 16, key_len, hash_len,
369 if (status != SILC_SKE_STATUS_OK) {
370 protocol->state = SILC_PROTOCOL_STATE_ERROR;
371 protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 300000);
372 silc_ske_free_key_material(keymat);
375 ctx->keymat = keymat;
377 /* Send Ok to the other end if we are responder. If we are initiator
378 we have sent this already. */
379 if (ctx->responder == TRUE)
380 silc_ske_end(ctx->ske, silc_server_protocol_ke_send_packet, context);
382 /* Unregister the timeout task since the protocol has ended.
383 This was the timeout task to be executed if the protocol is
384 not completed fast enough. */
385 if (ctx->timeout_task)
386 silc_task_unregister(server->timeout_queue, ctx->timeout_task);
388 /* Call the final callback */
389 if (protocol->final_callback)
390 protocol->execute_final(server->timeout_queue, 0, protocol, fd);
392 silc_protocol_free(protocol);
396 case SILC_PROTOCOL_STATE_ERROR:
401 /* Send abort notification */
402 silc_ske_abort(ctx->ske, ctx->ske->status,
403 silc_server_protocol_ke_send_packet,
406 /* Unregister the timeout task since the protocol has ended.
407 This was the timeout task to be executed if the protocol is
408 not completed fast enough. */
409 if (ctx->timeout_task)
410 silc_task_unregister(server->timeout_queue, ctx->timeout_task);
412 /* On error the final callback is always called. */
413 if (protocol->final_callback)
414 protocol->execute_final(server->timeout_queue, 0, protocol, fd);
416 silc_protocol_free(protocol);
419 case SILC_PROTOCOL_STATE_FAILURE:
421 * We have received failure from remote
424 /* Unregister the timeout task since the protocol has ended.
425 This was the timeout task to be executed if the protocol is
426 not completed fast enough. */
427 if (ctx->timeout_task)
428 silc_task_unregister(server->timeout_queue, ctx->timeout_task);
430 /* On error the final callback is always called. */
431 if (protocol->final_callback)
432 protocol->execute_final(server->timeout_queue, 0, protocol, fd);
434 silc_protocol_free(protocol);
437 case SILC_PROTOCOL_STATE_UNKNOWN:
443 * Connection Authentication protocol functions
447 silc_server_password_authentication(SilcServer server, char *auth1,
450 if (!auth1 || !auth2)
453 if (!memcmp(auth1, auth2, strlen(auth1)))
460 silc_server_public_key_authentication(SilcServer server,
461 SilcPublicKey pub_key,
470 if (!pub_key || !sign)
473 silc_pkcs_alloc(pub_key->name, &pkcs);
474 if (!silc_pkcs_public_key_set(pkcs, pub_key)) {
475 silc_pkcs_free(pkcs);
479 /* Make the authentication data. Protocol says it is HASH plus
481 len = ske->hash_len + ske->start_payload_copy->len;
482 auth = silc_buffer_alloc(len);
483 silc_buffer_pull_tail(auth, len);
484 silc_buffer_format(auth,
485 SILC_STR_UI_XNSTRING(ske->hash, ske->hash_len),
486 SILC_STR_UI_XNSTRING(ske->start_payload_copy->data,
487 ske->start_payload_copy->len),
490 /* Verify signature */
491 if (silc_pkcs_verify(pkcs, sign, sign_len, auth->data, auth->len)) {
492 silc_pkcs_free(pkcs);
493 silc_buffer_free(auth);
497 silc_pkcs_free(pkcs);
498 silc_buffer_free(auth);
503 silc_server_get_public_key_auth(SilcServer server,
504 SilcPublicKey pub_key,
505 unsigned char *auth_data,
506 uint32 *auth_data_len,
516 silc_pkcs_alloc(pub_key->name, &pkcs);
517 if (!silc_pkcs_public_key_set(pkcs, pub_key)) {
518 silc_pkcs_free(pkcs);
522 /* Make the authentication data. Protocol says it is HASH plus
524 len = ske->hash_len + ske->start_payload_copy->len;
525 auth = silc_buffer_alloc(len);
526 silc_buffer_pull_tail(auth, len);
527 silc_buffer_format(auth,
528 SILC_STR_UI_XNSTRING(ske->hash, ske->hash_len),
529 SILC_STR_UI_XNSTRING(ske->start_payload_copy->data,
530 ske->start_payload_copy->len),
533 if (silc_pkcs_sign(pkcs, auth->data, auth->len, auth_data, auth_data_len)) {
534 silc_pkcs_free(pkcs);
535 silc_buffer_free(auth);
539 silc_pkcs_free(pkcs);
540 silc_buffer_free(auth);
544 /* Performs connection authentication protocol. If responder, we
545 authenticate the remote data received. If initiator, we will send
546 authentication data to the remote end. */
548 SILC_TASK_CALLBACK(silc_server_protocol_connection_auth)
550 SilcProtocol protocol = (SilcProtocol)context;
551 SilcServerConnAuthInternalContext *ctx =
552 (SilcServerConnAuthInternalContext *)protocol->context;
553 SilcServer server = (SilcServer)ctx->server;
555 SILC_LOG_DEBUG(("Start"));
557 if (protocol->state == SILC_PROTOCOL_STATE_UNKNOWN)
558 protocol->state = SILC_PROTOCOL_STATE_START;
560 SILC_LOG_DEBUG(("State=%d", protocol->state));
562 switch(protocol->state) {
563 case SILC_PROTOCOL_STATE_START:
569 if (ctx->responder == TRUE) {
571 * We are receiving party
576 unsigned char *auth_data = NULL;
578 SILC_LOG_INFO(("Performing authentication protocol for %s (%s)",
579 ctx->sock->hostname, ctx->sock->ip));
581 /* Parse the received authentication data packet. The received
582 payload is Connection Auth Payload. */
583 ret = silc_buffer_unformat(ctx->packet->buffer,
584 SILC_STR_UI_SHORT(&payload_len),
585 SILC_STR_UI_SHORT(&conn_type),
588 SILC_LOG_DEBUG(("Bad payload in authentication packet"));
589 protocol->state = SILC_PROTOCOL_STATE_ERROR;
590 protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 300000);
594 if (payload_len != ctx->packet->buffer->len) {
595 SILC_LOG_DEBUG(("Bad payload in authentication packet"));
596 protocol->state = SILC_PROTOCOL_STATE_ERROR;
597 protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 300000);
603 if (conn_type < SILC_SOCKET_TYPE_CLIENT ||
604 conn_type > SILC_SOCKET_TYPE_ROUTER) {
605 SILC_LOG_ERROR(("Bad connection type %d", conn_type));
606 protocol->state = SILC_PROTOCOL_STATE_ERROR;
607 protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 300000);
611 if (payload_len > 0) {
612 /* Get authentication data */
613 silc_buffer_pull(ctx->packet->buffer, 4);
614 ret = silc_buffer_unformat(ctx->packet->buffer,
615 SILC_STR_UI_XNSTRING_ALLOC(&auth_data,
619 SILC_LOG_DEBUG(("Bad payload in authentication packet"));
620 protocol->state = SILC_PROTOCOL_STATE_ERROR;
621 protocol->execute(server->timeout_queue, 0,
622 protocol, fd, 0, 300000);
628 * Check the remote connection type and make sure that we have
629 * configured this connection. If we haven't allowed this connection
630 * the authentication must be failed.
633 SILC_LOG_DEBUG(("Remote connection type %d", conn_type));
635 /* Remote end is client */
636 if (conn_type == SILC_SOCKET_TYPE_CLIENT) {
637 SilcServerConfigSectionClientConnection *client = NULL;
638 client = silc_server_config_find_client_conn(server->config,
642 client = silc_server_config_find_client_conn(server->config,
647 switch(client->auth_meth) {
649 /* No authentication required */
650 SILC_LOG_DEBUG(("No authentication required"));
653 case SILC_AUTH_PASSWORD:
654 /* Password authentication */
655 SILC_LOG_DEBUG(("Password authentication"));
656 ret = silc_server_password_authentication(server, auth_data,
662 /* Authentication failed */
663 SILC_LOG_ERROR(("Authentication failed"));
664 SILC_LOG_DEBUG(("Authentication failed"));
665 silc_free(auth_data);
666 protocol->state = SILC_PROTOCOL_STATE_ERROR;
667 protocol->execute(server->timeout_queue, 0,
668 protocol, fd, 0, 300000);
672 case SILC_AUTH_PUBLIC_KEY:
673 /* Public key authentication */
674 SILC_LOG_DEBUG(("Public key authentication"));
675 ret = silc_server_public_key_authentication(server,
684 SILC_LOG_ERROR(("Authentication failed"));
685 SILC_LOG_DEBUG(("Authentication failed"));
686 silc_free(auth_data);
687 protocol->state = SILC_PROTOCOL_STATE_ERROR;
688 protocol->execute(server->timeout_queue, 0,
689 protocol, fd, 0, 300000);
693 SILC_LOG_DEBUG(("No configuration for remote connection"));
694 SILC_LOG_ERROR(("Remote connection not configured"));
695 SILC_LOG_ERROR(("Authentication failed"));
696 silc_free(auth_data);
697 protocol->state = SILC_PROTOCOL_STATE_ERROR;
698 protocol->execute(server->timeout_queue, 0,
699 protocol, fd, 0, 300000);
704 /* Remote end is server */
705 if (conn_type == SILC_SOCKET_TYPE_SERVER) {
706 SilcServerConfigSectionServerConnection *serv = NULL;
707 serv = silc_server_config_find_server_conn(server->config,
711 serv = silc_server_config_find_server_conn(server->config,
716 switch(serv->auth_meth) {
718 /* No authentication required */
719 SILC_LOG_DEBUG(("No authentication required"));
722 case SILC_AUTH_PASSWORD:
723 /* Password authentication */
724 SILC_LOG_DEBUG(("Password authentication"));
725 ret = silc_server_password_authentication(server, auth_data,
731 /* Authentication failed */
732 SILC_LOG_ERROR(("Authentication failed"));
733 SILC_LOG_DEBUG(("Authentication failed"));
734 silc_free(auth_data);
735 protocol->state = SILC_PROTOCOL_STATE_ERROR;
736 protocol->execute(server->timeout_queue, 0,
737 protocol, fd, 0, 300000);
741 case SILC_AUTH_PUBLIC_KEY:
742 /* Public key authentication */
743 SILC_LOG_DEBUG(("Public key authentication"));
744 ret = silc_server_public_key_authentication(server,
753 SILC_LOG_ERROR(("Authentication failed"));
754 SILC_LOG_DEBUG(("Authentication failed"));
755 silc_free(auth_data);
756 protocol->state = SILC_PROTOCOL_STATE_ERROR;
757 protocol->execute(server->timeout_queue, 0,
758 protocol, fd, 0, 300000);
762 SILC_LOG_DEBUG(("No configuration for remote connection"));
763 SILC_LOG_ERROR(("Remote connection not configured"));
764 SILC_LOG_ERROR(("Authentication failed"));
765 protocol->state = SILC_PROTOCOL_STATE_ERROR;
766 protocol->execute(server->timeout_queue, 0,
767 protocol, fd, 0, 300000);
768 silc_free(auth_data);
773 /* Remote end is router */
774 if (conn_type == SILC_SOCKET_TYPE_ROUTER) {
775 SilcServerConfigSectionServerConnection *serv = NULL;
776 serv = silc_server_config_find_router_conn(server->config,
780 serv = silc_server_config_find_router_conn(server->config,
785 switch(serv->auth_meth) {
787 /* No authentication required */
788 SILC_LOG_DEBUG(("No authentication required"));
791 case SILC_AUTH_PASSWORD:
792 /* Password authentication */
793 SILC_LOG_DEBUG(("Password authentication"));
794 ret = silc_server_password_authentication(server, auth_data,
800 /* Authentication failed */
801 SILC_LOG_ERROR(("Authentication failed"));
802 SILC_LOG_DEBUG(("Authentication failed"));
803 silc_free(auth_data);
804 protocol->state = SILC_PROTOCOL_STATE_ERROR;
805 protocol->execute(server->timeout_queue, 0,
806 protocol, fd, 0, 300000);
810 case SILC_AUTH_PUBLIC_KEY:
811 /* Public key authentication */
812 SILC_LOG_DEBUG(("Public key authentication"));
813 ret = silc_server_public_key_authentication(server,
822 SILC_LOG_ERROR(("Authentication failed"));
823 SILC_LOG_DEBUG(("Authentication failed"));
824 silc_free(auth_data);
825 protocol->state = SILC_PROTOCOL_STATE_ERROR;
826 protocol->execute(server->timeout_queue, 0,
827 protocol, fd, 0, 300000);
831 SILC_LOG_DEBUG(("No configuration for remote connection"));
832 SILC_LOG_ERROR(("Remote connection not configured"));
833 SILC_LOG_ERROR(("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);
842 silc_free(auth_data);
844 /* Save connection type. This is later used to create the
845 ID for the connection. */
846 ctx->conn_type = conn_type;
848 /* Advance protocol state. */
849 protocol->state = SILC_PROTOCOL_STATE_END;
850 protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 0);
854 * We are initiator. We are authenticating ourselves to a
855 * remote server. We will send the authentication data to the
856 * other end for verify.
860 unsigned char *auth_data = NULL;
861 uint32 auth_data_len = 0;
863 switch(ctx->auth_meth) {
865 /* No authentication required */
868 case SILC_AUTH_PASSWORD:
869 /* Password authentication */
870 if (ctx->auth_data && ctx->auth_data_len) {
871 auth_data = strdup(ctx->auth_data);
872 auth_data_len = ctx->auth_data_len;
877 case SILC_AUTH_PUBLIC_KEY:
879 unsigned char sign[1024];
881 /* Public key authentication */
882 silc_server_get_public_key_auth(server, ctx->auth_data,
883 sign, &auth_data_len,
885 auth_data = silc_calloc(auth_data_len, sizeof(*auth_data));
886 memcpy(auth_data, sign, auth_data_len);
891 payload_len = 4 + auth_data_len;
892 packet = silc_buffer_alloc(payload_len);
893 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
894 silc_buffer_format(packet,
895 SILC_STR_UI_SHORT(payload_len),
896 SILC_STR_UI_SHORT(server->server_type
898 SILC_SOCKET_TYPE_SERVER :
899 SILC_SOCKET_TYPE_ROUTER),
900 SILC_STR_UI_XNSTRING(auth_data, auth_data_len),
903 /* Send the packet to server */
904 silc_server_packet_send(server, ctx->sock,
905 SILC_PACKET_CONNECTION_AUTH, 0,
906 packet->data, packet->len, TRUE);
909 memset(auth_data, 0, auth_data_len);
910 silc_free(auth_data);
912 silc_buffer_free(packet);
914 /* Next state is end of protocol */
915 protocol->state = SILC_PROTOCOL_STATE_END;
920 case SILC_PROTOCOL_STATE_END:
927 SILC_PUT32_MSB(SILC_AUTH_OK, ok);
929 /* Authentication successful */
930 silc_server_packet_send(server, ctx->sock, SILC_PACKET_SUCCESS,
933 /* Unregister the timeout task since the protocol has ended.
934 This was the timeout task to be executed if the protocol is
935 not completed fast enough. */
936 if (ctx->timeout_task)
937 silc_task_unregister(server->timeout_queue, ctx->timeout_task);
939 /* Protocol has ended, call the final callback */
940 if (protocol->final_callback)
941 protocol->execute_final(server->timeout_queue, 0, protocol, fd);
943 silc_protocol_free(protocol);
946 case SILC_PROTOCOL_STATE_ERROR:
949 * Error. Send notify to remote.
951 unsigned char error[4];
953 SILC_PUT32_MSB(SILC_AUTH_FAILED, error);
955 /* Authentication failed */
956 silc_server_packet_send(server, ctx->sock, SILC_PACKET_FAILURE,
959 /* Unregister the timeout task since the protocol has ended.
960 This was the timeout task to be executed if the protocol is
961 not completed fast enough. */
962 if (ctx->timeout_task)
963 silc_task_unregister(server->timeout_queue, ctx->timeout_task);
965 /* On error the final callback is always called. */
966 if (protocol->final_callback)
967 protocol->execute_final(server->timeout_queue, 0, protocol, fd);
969 silc_protocol_free(protocol);
973 case SILC_PROTOCOL_STATE_FAILURE:
975 * We have received failure from remote
978 /* Unregister the timeout task since the protocol has ended.
979 This was the timeout task to be executed if the protocol is
980 not completed fast enough. */
981 if (ctx->timeout_task)
982 silc_task_unregister(server->timeout_queue, ctx->timeout_task);
984 /* On error the final callback is always called. */
985 if (protocol->final_callback)
986 protocol->execute_final(server->timeout_queue, 0, protocol, fd);
988 silc_protocol_free(protocol);
991 case SILC_PROTOCOL_STATE_UNKNOWN:
997 * Re-key protocol routines
1000 /* This function actually re-generates (when not using PFS) the keys and
1001 takes them into use. */
1003 void silc_server_protocol_rekey_generate(SilcServer server,
1004 SilcServerRekeyInternalContext *ctx)
1006 SilcIDListData idata = (SilcIDListData)ctx->sock->user_data;
1007 SilcSKEKeyMaterial *keymat;
1008 uint32 key_len = silc_cipher_get_key_len(idata->send_key);
1009 uint32 hash_len = idata->hash->hash->hash_len;
1011 SILC_LOG_DEBUG(("Generating new session keys (no PFS)"));
1013 /* Generate the new key */
1014 keymat = silc_calloc(1, sizeof(*keymat));
1015 silc_ske_process_key_material_data(idata->rekey->send_enc_key,
1016 idata->rekey->enc_key_len,
1017 16, key_len, hash_len,
1018 idata->hash, keymat);
1020 /* Set the keys into use */
1022 if (ctx->responder == TRUE) {
1023 silc_cipher_set_key(idata->send_key, keymat->receive_enc_key,
1024 keymat->enc_key_len);
1025 silc_cipher_set_iv(idata->send_key, keymat->receive_iv);
1026 silc_cipher_set_key(idata->receive_key, keymat->send_enc_key,
1027 keymat->enc_key_len);
1028 silc_cipher_set_iv(idata->receive_key, keymat->send_iv);
1030 silc_cipher_set_key(idata->send_key, keymat->send_enc_key,
1031 keymat->enc_key_len);
1032 silc_cipher_set_iv(idata->send_key, keymat->send_iv);
1033 silc_cipher_set_key(idata->receive_key, keymat->receive_enc_key,
1034 keymat->enc_key_len);
1035 silc_cipher_set_iv(idata->receive_key, keymat->receive_iv);
1038 silc_hmac_set_key(idata->hmac, keymat->hmac_key, keymat->hmac_key_len);
1040 /* Save the current sending encryption key */
1041 memset(idata->rekey->send_enc_key, 0, idata->rekey->enc_key_len);
1042 silc_free(idata->rekey->send_enc_key);
1043 idata->rekey->send_enc_key =
1044 silc_calloc(keymat->enc_key_len / 8,
1045 sizeof(*idata->rekey->send_enc_key));
1046 memcpy(idata->rekey->send_enc_key, keymat->send_enc_key,
1047 keymat->enc_key_len / 8);
1048 idata->rekey->enc_key_len = keymat->enc_key_len / 8;
1050 silc_ske_free_key_material(keymat);
1053 /* Performs re-key as defined the SILC protocol specification. */
1055 SILC_TASK_CALLBACK(silc_server_protocol_rekey)
1057 SilcProtocol protocol = (SilcProtocol)context;
1058 SilcServerRekeyInternalContext *ctx =
1059 (SilcServerRekeyInternalContext *)protocol->context;
1060 SilcServer server = (SilcServer)ctx->server;
1062 SILC_LOG_DEBUG(("Start"));
1064 if (protocol->state == SILC_PROTOCOL_STATE_UNKNOWN)
1065 protocol->state = SILC_PROTOCOL_STATE_START;
1067 SILC_LOG_DEBUG(("State=%d", protocol->state));
1069 switch(protocol->state) {
1070 case SILC_PROTOCOL_STATE_START:
1076 if (ctx->responder == TRUE) {
1078 * We are receiving party
1081 if (ctx->pfs == TRUE) {
1083 * Use Perfect Forward Secrecy, ie. negotiate the key material
1084 * using the SKE protocol.
1089 * Do normal and simple re-key.
1092 /* Send the REKEY_DONE to indicate we will take new keys into use */
1093 silc_server_packet_send(server, ctx->sock, SILC_PACKET_REKEY_DONE,
1096 /* The protocol ends in next stage. */
1097 protocol->state = SILC_PROTOCOL_STATE_END;
1102 * We are the initiator of this protocol
1105 if (ctx->pfs == TRUE) {
1107 * Use Perfect Forward Secrecy, ie. negotiate the key material
1108 * using the SKE protocol.
1113 * Do normal and simple re-key.
1116 /* Start the re-key by sending the REKEY packet */
1117 silc_server_packet_send(server, ctx->sock, SILC_PACKET_REKEY,
1120 /* The protocol ends in next stage. */
1121 protocol->state = SILC_PROTOCOL_STATE_END;
1128 case SILC_PROTOCOL_STATE_END:
1133 if (ctx->responder == TRUE) {
1135 if (ctx->pfs == TRUE) {
1142 * We must have received the REKEY_DONE from the initiator.
1145 if (ctx->packet->type != SILC_PACKET_REKEY_DONE) {
1146 /* Error in protocol */
1147 protocol->state = SILC_PROTOCOL_STATE_ERROR;
1148 protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 0);
1154 if (ctx->pfs == TRUE) {
1161 * We must have received the REKEY_DONE from the responder.
1164 if (ctx->packet->type != SILC_PACKET_REKEY_DONE) {
1165 /* Error in protocol */
1166 protocol->state = SILC_PROTOCOL_STATE_ERROR;
1167 protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 0);
1170 /* Send the REKEY_DONE to indicate we will take new keys into use
1172 silc_server_packet_send(server, ctx->sock, SILC_PACKET_REKEY_DONE,
1177 /* Protocol has ended, call the final callback */
1178 if (protocol->final_callback)
1179 protocol->execute_final(server->timeout_queue, 0, protocol, fd);
1181 silc_protocol_free(protocol);
1184 case SILC_PROTOCOL_STATE_ERROR:
1189 if (ctx->pfs == TRUE) {
1190 /* Send abort notification */
1191 silc_ske_abort(ctx->ske, ctx->ske->status,
1192 silc_server_protocol_ke_send_packet,
1196 /* On error the final callback is always called. */
1197 if (protocol->final_callback)
1198 protocol->execute_final(server->timeout_queue, 0, protocol, fd);
1200 silc_protocol_free(protocol);
1203 case SILC_PROTOCOL_STATE_FAILURE:
1205 * We have received failure from remote
1208 /* On error the final callback is always called. */
1209 if (protocol->final_callback)
1210 protocol->execute_final(server->timeout_queue, 0, protocol, fd);
1212 silc_protocol_free(protocol);
1215 case SILC_PROTOCOL_STATE_UNKNOWN:
1221 /* Registers protocols used in server. */
1223 void silc_server_protocols_register(void)
1225 silc_protocol_register(SILC_PROTOCOL_SERVER_CONNECTION_AUTH,
1226 silc_server_protocol_connection_auth);
1227 silc_protocol_register(SILC_PROTOCOL_SERVER_KEY_EXCHANGE,
1228 silc_server_protocol_key_exchange);
1229 silc_protocol_register(SILC_PROTOCOL_SERVER_REKEY,
1230 silc_server_protocol_rekey);
1233 /* Unregisters protocols */
1235 void silc_server_protocols_unregister(void)
1237 silc_protocol_unregister(SILC_PROTOCOL_SERVER_CONNECTION_AUTH,
1238 silc_server_protocol_connection_auth);
1239 silc_protocol_unregister(SILC_PROTOCOL_SERVER_KEY_EXCHANGE,
1240 silc_server_protocol_key_exchange);
1241 silc_protocol_unregister(SILC_PROTOCOL_SERVER_REKEY,
1242 silc_server_protocol_rekey);