5 Author: Pekka Riikonen <priikone@silcnet.org>
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);
30 SILC_TASK_CALLBACK(silc_server_protocol_rekey);
32 extern char *silc_version_string;
35 * Key Exhange protocol functions
39 silc_verify_public_key_internal(SilcServer server, SilcSocketConnection sock,
40 SilcSocketType conn_type,
41 unsigned char *pk, uint32 pk_len,
42 SilcSKEPKType pk_type)
44 char file[256], filename[256], *fingerprint;
47 if (pk_type != SILC_SKE_PK_TYPE_SILC) {
48 SILC_LOG_WARNING(("We don't support %s (%s) port %d public key type %d",
49 sock->hostname, sock->ip, sock->port, pk_type));
53 /* Accept client keys without verification */
54 if (conn_type == SILC_SOCKET_TYPE_CLIENT) {
55 SILC_LOG_DEBUG(("Accepting client public key without verification"));
59 /* XXX For now, accept server keys without verification too. We are
60 currently always doing mutual authentication so the proof of posession
61 of the private key is verified, and if server is authenticated in
62 conn auth protocol with public key we MUST have the key already. */
64 /* Rest is unreachable code! */
66 memset(filename, 0, sizeof(filename));
67 memset(file, 0, sizeof(file));
68 snprintf(file, sizeof(file) - 1, "serverkey_%s_%d.pub", sock->hostname,
70 snprintf(filename, sizeof(filename) - 1, SILC_ETCDIR "/serverkeys/%s",
73 /* Create serverkeys directory if it doesn't exist. */
74 if (stat(SILC_ETCDIR "/serverkeys", &st) < 0) {
75 /* If dir doesn't exist */
76 if (errno == ENOENT) {
77 if (mkdir(SILC_ETCDIR "/serverkeys", 0755) < 0) {
78 SILC_LOG_ERROR(("Couldn't create `%s' directory\n",
79 SILC_ETCDIR "/serverkeys"));
83 SILC_LOG_ERROR(("%s\n", strerror(errno)));
88 /* Take fingerprint of the public key */
89 fingerprint = silc_hash_fingerprint(NULL, pk, pk_len);
90 SILC_LOG_DEBUG(("Received server %s (%s) port %d public key (%s)",
91 sock->hostname, sock->ip, sock->port, fingerprint));
92 silc_free(fingerprint);
94 /* Check whether this key already exists */
95 if (stat(filename, &st) < 0) {
96 /* We don't have it, then cache it. */
97 SILC_LOG_DEBUG(("New public key from server"));
99 silc_pkcs_save_public_key_data(filename, pk, pk_len,
103 /* The key already exists, verify it. */
104 SilcPublicKey public_key;
105 unsigned char *encpk;
108 SILC_LOG_DEBUG(("We have the public key saved locally"));
110 /* Load the key file */
111 if (!silc_pkcs_load_public_key(filename, &public_key,
113 if (!silc_pkcs_load_public_key(filename, &public_key,
114 SILC_PKCS_FILE_BIN)) {
115 SILC_LOG_WARNING(("Could not load local copy of the %s (%s) port %d "
116 "server public key", sock->hostname, sock->ip,
119 /* Save the key for future checking */
121 silc_pkcs_save_public_key_data(filename, pk, pk_len,
126 /* Encode the key data */
127 encpk = silc_pkcs_public_key_encode(public_key, &encpk_len);
129 SILC_LOG_WARNING(("Local copy of the server %s (%s) port %d public key "
130 "is malformed", sock->hostname, sock->ip, sock->port));
132 /* Save the key for future checking */
134 silc_pkcs_save_public_key_data(filename, pk, pk_len, SILC_PKCS_FILE_PEM);
138 if (memcmp(pk, encpk, encpk_len)) {
139 SILC_LOG_WARNING(("%s (%s) port %d server public key does not match "
140 "with local copy", sock->hostname, sock->ip,
142 SILC_LOG_WARNING(("It is possible that the key has expired or changed"));
143 SILC_LOG_WARNING(("It is also possible that some one is performing "
144 "man-in-the-middle attack"));
145 SILC_LOG_WARNING(("Will not accept the server %s (%s) port %d public "
147 sock->hostname, sock->ip, sock->port));
151 /* Local copy matched */
156 /* Callback that is called when we have received KE2 payload from
157 responder. We try to verify the public key now. */
160 silc_server_protocol_ke_verify_key(SilcSKE ske,
161 unsigned char *pk_data,
163 SilcSKEPKType pk_type,
165 SilcSKEVerifyCbCompletion completion,
166 void *completion_context)
168 SilcProtocol protocol = (SilcProtocol)context;
169 SilcServerKEInternalContext *ctx =
170 (SilcServerKEInternalContext *)protocol->context;
171 SilcServer server = (SilcServer)ctx->server;
173 SILC_LOG_DEBUG(("Start"));
175 if (silc_verify_public_key_internal(server, ctx->sock,
176 (ctx->responder == FALSE ?
177 SILC_SOCKET_TYPE_ROUTER:
178 ctx->sconfig ? SILC_SOCKET_TYPE_SERVER :
179 ctx->rconfig ? SILC_SOCKET_TYPE_ROUTER :
180 SILC_SOCKET_TYPE_CLIENT),
181 pk_data, pk_len, pk_type))
182 completion(ske, SILC_SKE_STATUS_OK, completion_context);
184 completion(ske, SILC_SKE_STATUS_UNSUPPORTED_PUBLIC_KEY,
188 /* Packet sending callback. This function is provided as packet sending
189 routine to the Key Exchange functions. */
191 static void silc_server_protocol_ke_send_packet(SilcSKE ske,
196 SilcProtocol protocol = (SilcProtocol)context;
197 SilcServerKEInternalContext *ctx =
198 (SilcServerKEInternalContext *)protocol->context;
199 SilcServer server = (SilcServer)ctx->server;
201 /* Send the packet immediately */
202 silc_server_packet_send(server, ske->sock,
203 type, 0, packet->data, packet->len, TRUE);
206 /* Sets the negotiated key material into use for particular connection. */
208 int silc_server_protocol_ke_set_keys(SilcServer server,
210 SilcSocketConnection sock,
211 SilcSKEKeyMaterial *keymat,
216 SilcSKEDiffieHellmanGroup group,
219 SilcUnknownEntry conn_data;
220 SilcIDListData idata;
222 SILC_LOG_DEBUG(("Setting new key into use"));
224 conn_data = silc_calloc(1, sizeof(*conn_data));
225 idata = (SilcIDListData)conn_data;
227 /* Allocate cipher to be used in the communication */
228 if (!silc_cipher_alloc(cipher->cipher->name, &idata->send_key)) {
229 silc_free(conn_data);
232 if (!silc_cipher_alloc(cipher->cipher->name, &idata->receive_key)) {
233 silc_free(conn_data);
237 if (!silc_hmac_alloc((char *)silc_hmac_get_name(hmac), NULL,
238 &idata->hmac_send)) {
239 silc_cipher_free(idata->send_key);
240 silc_cipher_free(idata->receive_key);
241 silc_free(conn_data);
245 if (!silc_hmac_alloc((char *)silc_hmac_get_name(hmac), NULL,
246 &idata->hmac_receive)) {
247 silc_cipher_free(idata->send_key);
248 silc_cipher_free(idata->receive_key);
249 silc_hmac_free(idata->hmac_send);
250 silc_free(conn_data);
254 if (is_responder == TRUE) {
255 silc_cipher_set_key(idata->send_key, keymat->receive_enc_key,
256 keymat->enc_key_len);
257 silc_cipher_set_iv(idata->send_key, keymat->receive_iv);
258 silc_cipher_set_key(idata->receive_key, keymat->send_enc_key,
259 keymat->enc_key_len);
260 silc_cipher_set_iv(idata->receive_key, keymat->send_iv);
261 silc_hmac_set_key(idata->hmac_send, keymat->receive_hmac_key,
262 keymat->hmac_key_len);
263 silc_hmac_set_key(idata->hmac_receive, keymat->send_hmac_key,
264 keymat->hmac_key_len);
266 silc_cipher_set_key(idata->send_key, keymat->send_enc_key,
267 keymat->enc_key_len);
268 silc_cipher_set_iv(idata->send_key, keymat->send_iv);
269 silc_cipher_set_key(idata->receive_key, keymat->receive_enc_key,
270 keymat->enc_key_len);
271 silc_cipher_set_iv(idata->receive_key, keymat->receive_iv);
272 silc_hmac_set_key(idata->hmac_send, keymat->send_hmac_key,
273 keymat->hmac_key_len);
274 silc_hmac_set_key(idata->hmac_receive, keymat->receive_hmac_key,
275 keymat->hmac_key_len);
278 idata->rekey = silc_calloc(1, sizeof(*idata->rekey));
279 idata->rekey->send_enc_key =
280 silc_calloc(keymat->enc_key_len / 8,
281 sizeof(*idata->rekey->send_enc_key));
282 memcpy(idata->rekey->send_enc_key,
283 keymat->send_enc_key, keymat->enc_key_len / 8);
284 idata->rekey->enc_key_len = keymat->enc_key_len / 8;
286 if (ske->start_payload->flags & SILC_SKE_SP_FLAG_PFS)
287 idata->rekey->pfs = TRUE;
288 idata->rekey->ske_group = silc_ske_group_get_number(group);
291 if (!silc_hash_alloc(hash->hash->name, &idata->hash)) {
292 silc_cipher_free(idata->send_key);
293 silc_cipher_free(idata->receive_key);
294 silc_hmac_free(idata->hmac_send);
295 silc_hmac_free(idata->hmac_receive);
296 silc_free(conn_data);
300 /* Save the remote host's public key */
301 silc_pkcs_public_key_decode(ske->ke1_payload->pk_data,
302 ske->ke1_payload->pk_len, &idata->public_key);
303 if (ske->prop->flags & SILC_SKE_SP_FLAG_MUTUAL)
304 silc_hash_make(server->sha1hash, ske->ke1_payload->pk_data,
305 ske->ke1_payload->pk_len, idata->fingerprint);
307 sock->user_data = (void *)conn_data;
309 SILC_LOG_INFO(("%s (%s) security properties: %s %s %s",
310 sock->hostname, sock->ip,
311 idata->send_key->cipher->name,
312 (char *)silc_hmac_get_name(idata->hmac_send),
313 idata->hash->hash->name));
318 /* Check remote host version string */
320 SilcSKEStatus silc_ske_check_version(SilcSKE ske, unsigned char *version,
321 uint32 len, void *context)
323 SilcSKEStatus status = SILC_SKE_STATUS_OK;
325 int maj = 0, min = 0, build = 0, maj2 = 0, min2 = 0, build2 = 0;
327 SILC_LOG_INFO(("%s (%s) is version %s", ske->sock->hostname,
328 ske->sock->ip, version));
330 /* Check for initial version string */
331 if (!strstr(version, "SILC-1.0-"))
332 status = SILC_SKE_STATUS_BAD_VERSION;
334 /* Check software version */
338 status = SILC_SKE_STATUS_BAD_VERSION;
341 cp = strchr(cp, '.');
347 cp = strchr(cp, '.');
349 build = atoi(cp + 1);
352 cp = silc_version_string + 9;
354 status = SILC_SKE_STATUS_BAD_VERSION;
357 cp = strchr(cp, '.');
363 cp = strchr(cp, '.');
365 build2 = atoi(cp + 1);
369 status = SILC_SKE_STATUS_BAD_VERSION;
372 status = SILC_SKE_STATUS_BAD_VERSION;
375 /* XXX < 0.6 is not allowed */
376 if (maj == 0 && min < 5)
377 status = SILC_SKE_STATUS_BAD_VERSION;
379 /* XXX backward support for 0.6.1 */
380 if (maj == 0 && min == 6 && build < 2)
381 ske->backward_version = 1;
386 /* Callback that is called by the SKE to indicate that it is safe to
387 continue the execution of the protocol. This is used only if we are
388 initiator. Is given as argument to the silc_ske_initiator_finish or
389 silc_ske_responder_phase_2 functions. This is called due to the fact
390 that the public key verification process is asynchronous and we must
391 not continue the protocl until the public key has been verified and
392 this callback is called. */
394 static void silc_server_protocol_ke_continue(SilcSKE ske, void *context)
396 SilcProtocol protocol = (SilcProtocol)context;
397 SilcServerKEInternalContext *ctx =
398 (SilcServerKEInternalContext *)protocol->context;
399 SilcServer server = (SilcServer)ctx->server;
401 SILC_LOG_DEBUG(("Start"));
403 if (ske->status != SILC_SKE_STATUS_OK) {
404 SILC_LOG_WARNING(("Error (%s) during Key Exchange protocol",
405 silc_ske_map_status(ske->status)));
406 SILC_LOG_DEBUG(("Error (%s) during Key Exchange protocol",
407 silc_ske_map_status(ske->status)));
409 protocol->state = SILC_PROTOCOL_STATE_ERROR;
410 silc_protocol_execute(protocol, server->schedule, 0, 300000);
414 /* Send Ok to the other end. We will end the protocol as responder
415 sends Ok to us when we will take the new keys into use. */
416 if (ctx->responder == FALSE) {
417 silc_ske_end(ctx->ske);
419 /* End the protocol on the next round */
420 protocol->state = SILC_PROTOCOL_STATE_END;
423 /* Advance protocol state and call the next state if we are responder.
424 This happens when this callback was sent to silc_ske_responder_phase_2
426 if (ctx->responder == TRUE) {
428 silc_protocol_execute(protocol, server->schedule, 0, 100000);
432 /* Performs key exchange protocol. This is used for both initiator
433 and responder key exchange. This is performed always when accepting
434 new connection to the server. This may be called recursively. */
436 SILC_TASK_CALLBACK(silc_server_protocol_key_exchange)
438 SilcProtocol protocol = (SilcProtocol)context;
439 SilcServerKEInternalContext *ctx =
440 (SilcServerKEInternalContext *)protocol->context;
441 SilcServer server = (SilcServer)ctx->server;
442 SilcSKEStatus status = SILC_SKE_STATUS_OK;
444 SILC_LOG_DEBUG(("Start"));
446 if (protocol->state == SILC_PROTOCOL_STATE_UNKNOWN)
447 protocol->state = SILC_PROTOCOL_STATE_START;
449 SILC_LOG_DEBUG(("State=%d", protocol->state));
451 switch(protocol->state) {
452 case SILC_PROTOCOL_STATE_START:
459 /* Allocate Key Exchange object */
460 ske = silc_ske_alloc();
462 ske->rng = server->rng;
464 silc_ske_set_callbacks(ske, silc_server_protocol_ke_send_packet, NULL,
465 silc_server_protocol_ke_verify_key,
466 silc_server_protocol_ke_continue,
467 silc_ske_check_version, context);
469 if (ctx->responder == TRUE) {
470 /* Start the key exchange by processing the received security
471 properties packet from initiator. */
472 status = silc_ske_responder_start(ske, ctx->rng, ctx->sock,
474 ctx->packet->buffer, TRUE);
476 SilcSKEStartPayload *start_payload;
478 /* Assemble security properties. */
479 silc_ske_assemble_security_properties(ske, SILC_SKE_SP_FLAG_MUTUAL,
483 /* Start the key exchange by sending our security properties
484 to the remote end. */
485 status = silc_ske_initiator_start(ske, ctx->rng, ctx->sock,
489 /* Return now if the procedure is pending. */
490 if (status == SILC_SKE_STATUS_PENDING)
493 if (status != SILC_SKE_STATUS_OK) {
494 SILC_LOG_WARNING(("Error (%s) during Key Exchange protocol",
495 silc_ske_map_status(status)));
496 SILC_LOG_DEBUG(("Error (%s) during Key Exchange protocol",
497 silc_ske_map_status(status)));
499 protocol->state = SILC_PROTOCOL_STATE_ERROR;
500 silc_protocol_execute(protocol, server->schedule, 0, 300000);
504 /* Advance protocol state and call the next state if we are responder */
506 if (ctx->responder == TRUE)
507 silc_protocol_execute(protocol, server->schedule, 0, 100000);
515 if (ctx->responder == TRUE) {
516 /* Sends the selected security properties to the initiator. */
517 status = silc_ske_responder_phase_1(ctx->ske,
518 ctx->ske->start_payload);
520 /* Call Phase-1 function. This processes the Key Exchange Start
521 paylaod reply we just got from the responder. The callback
522 function will receive the processed payload where we will
524 status = silc_ske_initiator_phase_1(ctx->ske, ctx->packet->buffer);
527 /* Return now if the procedure is pending. */
528 if (status == SILC_SKE_STATUS_PENDING)
531 if (status != SILC_SKE_STATUS_OK) {
532 SILC_LOG_WARNING(("Error (%s) during Key Exchange protocol",
533 silc_ske_map_status(status)));
534 SILC_LOG_DEBUG(("Error (%s) during Key Exchange protocol",
535 silc_ske_map_status(status)));
537 protocol->state = SILC_PROTOCOL_STATE_ERROR;
538 silc_protocol_execute(protocol, server->schedule, 0, 300000);
542 /* Advance protocol state and call next state if we are initiator */
544 if (ctx->responder == FALSE)
545 silc_protocol_execute(protocol, server->schedule, 0, 100000);
553 if (ctx->responder == TRUE) {
554 /* Process the received Key Exchange 1 Payload packet from
555 the initiator. This also creates our parts of the Diffie
556 Hellman algorithm. The silc_server_protocol_ke_continue
557 will be called after the public key has been verified. */
558 status = silc_ske_responder_phase_2(ctx->ske, ctx->packet->buffer);
560 /* Call the Phase-2 function. This creates Diffie Hellman
561 key exchange parameters and sends our public part inside
562 Key Exhange 1 Payload to the responder. */
563 status = silc_ske_initiator_phase_2(ctx->ske,
565 server->private_key);
569 /* Return now if the procedure is pending. */
570 if (status == SILC_SKE_STATUS_PENDING)
573 if (status != SILC_SKE_STATUS_OK) {
574 SILC_LOG_WARNING(("Error (%s) during Key Exchange protocol",
575 silc_ske_map_status(status)));
576 SILC_LOG_DEBUG(("Error (%s) during Key Exchange protocol",
577 silc_ske_map_status(status)));
579 protocol->state = SILC_PROTOCOL_STATE_ERROR;
580 silc_protocol_execute(protocol, server->schedule, 0, 300000);
590 if (ctx->responder == TRUE) {
591 /* This creates the key exchange material and sends our
592 public parts to the initiator inside Key Exchange 2 Payload. */
593 status = silc_ske_responder_finish(ctx->ske,
596 SILC_SKE_PK_TYPE_SILC);
598 /* End the protocol on the next round */
599 protocol->state = SILC_PROTOCOL_STATE_END;
601 /* Finish the protocol. This verifies the Key Exchange 2 payload
602 sent by responder. The silc_server_protocol_ke_continue will
603 be called after the public key has been verified. */
604 status = silc_ske_initiator_finish(ctx->ske, ctx->packet->buffer);
607 /* Return now if the procedure is pending. */
608 if (status == SILC_SKE_STATUS_PENDING)
611 if (status != SILC_SKE_STATUS_OK) {
612 SILC_LOG_WARNING(("Error (%s) during Key Exchange protocol",
613 silc_ske_map_status(status)));
614 SILC_LOG_DEBUG(("Error (%s) during Key Exchange protocol",
615 silc_ske_map_status(status)));
617 protocol->state = SILC_PROTOCOL_STATE_ERROR;
618 silc_protocol_execute(protocol, server->schedule, 0, 300000);
624 case SILC_PROTOCOL_STATE_END:
629 SilcSKEKeyMaterial *keymat;
630 int key_len = silc_cipher_get_key_len(ctx->ske->prop->cipher);
631 int hash_len = ctx->ske->prop->hash->hash->hash_len;
633 /* Process the key material */
634 keymat = silc_calloc(1, sizeof(*keymat));
635 status = silc_ske_process_key_material(ctx->ske, 16, key_len, hash_len,
637 if (status != SILC_SKE_STATUS_OK) {
638 protocol->state = SILC_PROTOCOL_STATE_ERROR;
639 silc_protocol_execute(protocol, server->schedule, 0, 300000);
640 silc_ske_free_key_material(keymat);
643 ctx->keymat = keymat;
645 /* Send Ok to the other end if we are responder. If we are initiator
646 we have sent this already. */
647 if (ctx->responder == TRUE)
648 silc_ske_end(ctx->ske);
650 /* Unregister the timeout task since the protocol has ended.
651 This was the timeout task to be executed if the protocol is
652 not completed fast enough. */
653 if (ctx->timeout_task)
654 silc_schedule_task_del(server->schedule, ctx->timeout_task);
656 /* Assure that after calling final callback there cannot be pending
657 executions for this protocol anymore. This just unregisters any
658 timeout callbacks for this protocol. */
659 silc_protocol_cancel(protocol, server->schedule);
661 /* Call the final callback */
662 if (protocol->final_callback)
663 silc_protocol_execute_final(protocol, server->schedule);
665 silc_protocol_free(protocol);
669 case SILC_PROTOCOL_STATE_ERROR:
674 /* Send abort notification */
675 silc_ske_abort(ctx->ske, ctx->ske->status);
677 /* Unregister the timeout task since the protocol has ended.
678 This was the timeout task to be executed if the protocol is
679 not completed fast enough. */
680 if (ctx->timeout_task)
681 silc_schedule_task_del(server->schedule, ctx->timeout_task);
683 /* Assure that after calling final callback there cannot be pending
684 executions for this protocol anymore. This just unregisters any
685 timeout callbacks for this protocol. */
686 silc_protocol_cancel(protocol, server->schedule);
688 /* On error the final callback is always called. */
689 if (protocol->final_callback)
690 silc_protocol_execute_final(protocol, server->schedule);
692 silc_protocol_free(protocol);
695 case SILC_PROTOCOL_STATE_FAILURE:
697 * We have received failure from remote
700 /* Unregister the timeout task since the protocol has ended.
701 This was the timeout task to be executed if the protocol is
702 not completed fast enough. */
703 if (ctx->timeout_task)
704 silc_schedule_task_del(server->schedule, ctx->timeout_task);
706 /* Assure that after calling final callback there cannot be pending
707 executions for this protocol anymore. This just unregisters any
708 timeout callbacks for this protocol. */
709 silc_protocol_cancel(protocol, server->schedule);
711 /* On error the final callback is always called. */
712 if (protocol->final_callback)
713 silc_protocol_execute_final(protocol, server->schedule);
715 silc_protocol_free(protocol);
718 case SILC_PROTOCOL_STATE_UNKNOWN:
724 * Connection Authentication protocol functions
728 silc_server_password_authentication(SilcServer server, char *remote_auth,
731 if (!remote_auth || !local_auth)
734 if (!memcmp(remote_auth, local_auth, strlen(local_auth)))
741 silc_server_public_key_authentication(SilcServer server,
742 SilcPublicKey pub_key,
751 if (!pub_key || !sign)
754 silc_pkcs_alloc(pub_key->name, &pkcs);
755 if (!silc_pkcs_public_key_set(pkcs, pub_key)) {
756 silc_pkcs_free(pkcs);
760 /* Make the authentication data. Protocol says it is HASH plus
762 len = ske->hash_len + ske->start_payload_copy->len;
763 auth = silc_buffer_alloc(len);
764 silc_buffer_pull_tail(auth, len);
765 silc_buffer_format(auth,
766 SILC_STR_UI_XNSTRING(ske->hash, ske->hash_len),
767 SILC_STR_UI_XNSTRING(ske->start_payload_copy->data,
768 ske->start_payload_copy->len),
771 /* Verify signature */
772 if (silc_pkcs_verify_with_hash(pkcs, ske->prop->hash, sign, sign_len,
773 auth->data, auth->len)) {
774 silc_pkcs_free(pkcs);
775 silc_buffer_free(auth);
779 silc_pkcs_free(pkcs);
780 silc_buffer_free(auth);
785 silc_server_get_public_key_auth(SilcServer server,
786 unsigned char *auth_data,
787 uint32 *auth_data_len,
796 /* Make the authentication data. Protocol says it is HASH plus
798 len = ske->hash_len + ske->start_payload_copy->len;
799 auth = silc_buffer_alloc(len);
800 silc_buffer_pull_tail(auth, len);
801 silc_buffer_format(auth,
802 SILC_STR_UI_XNSTRING(ske->hash, ske->hash_len),
803 SILC_STR_UI_XNSTRING(ske->start_payload_copy->data,
804 ske->start_payload_copy->len),
807 if (silc_pkcs_sign_with_hash(pkcs, ske->prop->hash, auth->data,
808 auth->len, auth_data, auth_data_len)) {
809 silc_buffer_free(auth);
813 silc_buffer_free(auth);
817 /* Function that actually performs the authentication to the remote. This
818 supports both passphrase and public key authentication. */
821 silc_server_get_authentication(SilcServerConnAuthInternalContext *ctx,
822 char *local_passphrase,
823 void *local_publickey,
824 unsigned char *remote_auth,
825 uint32 remote_auth_len)
827 SilcServer server = (SilcServer)ctx->server;
828 SilcSKE ske = ctx->ske;
831 /* If we don't have authentication data set at all we do not require
832 authentication at all */
833 if (!local_passphrase && !local_publickey) {
834 SILC_LOG_DEBUG(("No authentication required"));
838 /* If both passphrase and public key is provided then we'll try both of
839 them and see which one of them authenticates. If only one of them is
840 set, then try only that. */
842 /* Try first passphrase (as it is faster to check) */
843 if (local_passphrase) {
844 SILC_LOG_DEBUG(("Password authentication"));
845 result = silc_server_password_authentication(server, local_passphrase,
849 /* Try public key authenetication */
850 if (!result && local_publickey) {
851 SILC_LOG_DEBUG(("Public key authentication"));
852 result = silc_server_public_key_authentication(server,
862 /* Performs connection authentication protocol. If responder, we
863 authenticate the remote data received. If initiator, we will send
864 authentication data to the remote end. */
866 SILC_TASK_CALLBACK(silc_server_protocol_connection_auth)
868 SilcProtocol protocol = (SilcProtocol)context;
869 SilcServerConnAuthInternalContext *ctx =
870 (SilcServerConnAuthInternalContext *)protocol->context;
871 SilcServer server = (SilcServer)ctx->server;
873 SILC_LOG_DEBUG(("Start"));
875 if (protocol->state == SILC_PROTOCOL_STATE_UNKNOWN)
876 protocol->state = SILC_PROTOCOL_STATE_START;
878 SILC_LOG_DEBUG(("State=%d", protocol->state));
880 switch(protocol->state) {
881 case SILC_PROTOCOL_STATE_START:
887 if (ctx->responder == TRUE) {
889 * We are receiving party
894 unsigned char *auth_data = NULL;
896 SILC_LOG_INFO(("Performing authentication protocol for %s (%s)",
897 ctx->sock->hostname, ctx->sock->ip));
899 /* Parse the received authentication data packet. The received
900 payload is Connection Auth Payload. */
901 ret = silc_buffer_unformat(ctx->packet->buffer,
902 SILC_STR_UI_SHORT(&payload_len),
903 SILC_STR_UI_SHORT(&conn_type),
906 SILC_LOG_DEBUG(("Bad payload in authentication packet"));
907 protocol->state = SILC_PROTOCOL_STATE_ERROR;
908 silc_protocol_execute(protocol, server->schedule, 0, 300000);
912 if (payload_len != ctx->packet->buffer->len) {
913 SILC_LOG_DEBUG(("Bad payload in authentication packet"));
914 protocol->state = SILC_PROTOCOL_STATE_ERROR;
915 silc_protocol_execute(protocol, server->schedule, 0, 300000);
921 if (conn_type < SILC_SOCKET_TYPE_CLIENT ||
922 conn_type > SILC_SOCKET_TYPE_ROUTER) {
923 SILC_LOG_ERROR(("Bad connection type %d", conn_type));
924 protocol->state = SILC_PROTOCOL_STATE_ERROR;
925 silc_protocol_execute(protocol, server->schedule, 0, 300000);
929 if (payload_len > 0) {
930 /* Get authentication data */
931 silc_buffer_pull(ctx->packet->buffer, 4);
932 ret = silc_buffer_unformat(ctx->packet->buffer,
933 SILC_STR_UI_XNSTRING_ALLOC(&auth_data,
937 SILC_LOG_DEBUG(("Bad payload in authentication packet"));
938 protocol->state = SILC_PROTOCOL_STATE_ERROR;
939 silc_protocol_execute(protocol, server->schedule, 0, 300000);
945 * Check the remote connection type and make sure that we have
946 * configured this connection. If we haven't allowed this connection
947 * the authentication must be failed.
950 SILC_LOG_DEBUG(("Remote connection type %d", conn_type));
952 /* Remote end is client */
953 if (conn_type == SILC_SOCKET_TYPE_CLIENT) {
954 SilcServerConfigSectionClient *client = ctx->cconfig;
957 ret = silc_server_get_authentication(ctx, client->passphrase,
959 auth_data, payload_len);
961 /* Authentication failed */
962 SILC_LOG_ERROR(("Authentication failed"));
963 SILC_LOG_DEBUG(("Authentication failed"));
964 silc_free(auth_data);
965 protocol->state = SILC_PROTOCOL_STATE_ERROR;
966 silc_protocol_execute(protocol, server->schedule, 0, 300000);
970 SILC_LOG_DEBUG(("No configuration for remote client connection"));
971 SILC_LOG_ERROR(("Remote client connection not configured"));
972 SILC_LOG_ERROR(("Authentication failed"));
973 silc_free(auth_data);
974 protocol->state = SILC_PROTOCOL_STATE_ERROR;
975 silc_protocol_execute(protocol, server->schedule,
981 /* Remote end is server */
982 if (conn_type == SILC_SOCKET_TYPE_SERVER) {
983 SilcServerConfigSectionServer *serv = ctx->sconfig;
986 ret = silc_server_get_authentication(ctx, serv->passphrase,
988 auth_data, payload_len);
990 /* Authentication failed */
991 SILC_LOG_ERROR(("Authentication failed"));
992 SILC_LOG_DEBUG(("Authentication failed"));
993 silc_free(auth_data);
994 protocol->state = SILC_PROTOCOL_STATE_ERROR;
995 silc_protocol_execute(protocol, server->schedule, 0, 300000);
999 SILC_LOG_DEBUG(("No configuration for remote server connection"));
1000 SILC_LOG_ERROR(("Remote server connection not configured"));
1001 SILC_LOG_ERROR(("Authentication failed"));
1002 protocol->state = SILC_PROTOCOL_STATE_ERROR;
1003 silc_protocol_execute(protocol, server->schedule,
1005 silc_free(auth_data);
1010 /* Remote end is router */
1011 if (conn_type == SILC_SOCKET_TYPE_ROUTER) {
1012 SilcServerConfigSectionRouter *serv = ctx->rconfig;
1015 ret = silc_server_get_authentication(ctx, serv->passphrase,
1017 auth_data, payload_len);
1019 /* Authentication failed */
1020 SILC_LOG_ERROR(("Authentication failed"));
1021 SILC_LOG_DEBUG(("Authentication failed"));
1022 silc_free(auth_data);
1023 protocol->state = SILC_PROTOCOL_STATE_ERROR;
1024 silc_protocol_execute(protocol, server->schedule, 0, 300000);
1028 SILC_LOG_DEBUG(("No configuration for remote router connection"));
1029 SILC_LOG_ERROR(("Remote router connection not configured"));
1030 SILC_LOG_ERROR(("Authentication failed"));
1031 silc_free(auth_data);
1032 protocol->state = SILC_PROTOCOL_STATE_ERROR;
1033 silc_protocol_execute(protocol, server->schedule,
1039 silc_free(auth_data);
1041 /* Save connection type. This is later used to create the
1042 ID for the connection. */
1043 ctx->conn_type = conn_type;
1045 /* Advance protocol state. */
1046 protocol->state = SILC_PROTOCOL_STATE_END;
1047 silc_protocol_execute(protocol, server->schedule, 0, 0);
1051 * We are initiator. We are authenticating ourselves to a
1052 * remote server. We will send the authentication data to the
1053 * other end for verify.
1056 int payload_len = 0;
1057 unsigned char *auth_data = NULL;
1058 uint32 auth_data_len = 0;
1060 switch(ctx->auth_meth) {
1061 case SILC_AUTH_NONE:
1062 /* No authentication required */
1065 case SILC_AUTH_PASSWORD:
1066 /* Password authentication */
1067 if (ctx->auth_data && ctx->auth_data_len) {
1068 auth_data = strdup(ctx->auth_data);
1069 auth_data_len = ctx->auth_data_len;
1074 case SILC_AUTH_PUBLIC_KEY:
1076 unsigned char sign[1024];
1078 /* Public key authentication */
1079 silc_server_get_public_key_auth(server, sign, &auth_data_len,
1081 auth_data = silc_calloc(auth_data_len, sizeof(*auth_data));
1082 memcpy(auth_data, sign, auth_data_len);
1087 payload_len = 4 + auth_data_len;
1088 packet = silc_buffer_alloc(payload_len);
1089 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
1090 silc_buffer_format(packet,
1091 SILC_STR_UI_SHORT(payload_len),
1092 SILC_STR_UI_SHORT(server->server_type
1094 SILC_SOCKET_TYPE_SERVER :
1095 SILC_SOCKET_TYPE_ROUTER),
1096 SILC_STR_UI_XNSTRING(auth_data, auth_data_len),
1099 /* Send the packet to server */
1100 silc_server_packet_send(server, ctx->sock,
1101 SILC_PACKET_CONNECTION_AUTH, 0,
1102 packet->data, packet->len, TRUE);
1105 memset(auth_data, 0, auth_data_len);
1106 silc_free(auth_data);
1108 silc_buffer_free(packet);
1110 /* Next state is end of protocol */
1111 protocol->state = SILC_PROTOCOL_STATE_END;
1116 case SILC_PROTOCOL_STATE_END:
1121 unsigned char ok[4];
1123 SILC_PUT32_MSB(SILC_AUTH_OK, ok);
1125 /* Authentication successful */
1126 silc_server_packet_send(server, ctx->sock, SILC_PACKET_SUCCESS,
1129 /* Unregister the timeout task since the protocol has ended.
1130 This was the timeout task to be executed if the protocol is
1131 not completed fast enough. */
1132 if (ctx->timeout_task)
1133 silc_schedule_task_del(server->schedule, ctx->timeout_task);
1135 /* Assure that after calling final callback there cannot be pending
1136 executions for this protocol anymore. This just unregisters any
1137 timeout callbacks for this protocol. */
1138 silc_protocol_cancel(protocol, server->schedule);
1140 /* Protocol has ended, call the final callback */
1141 if (protocol->final_callback)
1142 silc_protocol_execute_final(protocol, server->schedule);
1144 silc_protocol_free(protocol);
1147 case SILC_PROTOCOL_STATE_ERROR:
1150 * Error. Send notify to remote.
1152 unsigned char error[4];
1154 SILC_PUT32_MSB(SILC_AUTH_FAILED, error);
1156 /* Authentication failed */
1157 silc_server_packet_send(server, ctx->sock, SILC_PACKET_FAILURE,
1160 /* Unregister the timeout task since the protocol has ended.
1161 This was the timeout task to be executed if the protocol is
1162 not completed fast enough. */
1163 if (ctx->timeout_task)
1164 silc_schedule_task_del(server->schedule, ctx->timeout_task);
1166 /* Assure that after calling final callback there cannot be pending
1167 executions for this protocol anymore. This just unregisters any
1168 timeout callbacks for this protocol. */
1169 silc_protocol_cancel(protocol, server->schedule);
1171 /* On error the final callback is always called. */
1172 if (protocol->final_callback)
1173 silc_protocol_execute_final(protocol, server->schedule);
1175 silc_protocol_free(protocol);
1179 case SILC_PROTOCOL_STATE_FAILURE:
1181 * We have received failure from remote
1184 /* Unregister the timeout task since the protocol has ended.
1185 This was the timeout task to be executed if the protocol is
1186 not completed fast enough. */
1187 if (ctx->timeout_task)
1188 silc_schedule_task_del(server->schedule, ctx->timeout_task);
1190 /* Assure that after calling final callback there cannot be pending
1191 executions for this protocol anymore. This just unregisters any
1192 timeout callbacks for this protocol. */
1193 silc_protocol_cancel(protocol, server->schedule);
1195 /* On error the final callback is always called. */
1196 if (protocol->final_callback)
1197 silc_protocol_execute_final(protocol, server->schedule);
1199 silc_protocol_free(protocol);
1202 case SILC_PROTOCOL_STATE_UNKNOWN:
1208 * Re-key protocol routines
1211 /* Actually takes the new keys into use. */
1214 silc_server_protocol_rekey_validate(SilcServer server,
1215 SilcServerRekeyInternalContext *ctx,
1216 SilcIDListData idata,
1217 SilcSKEKeyMaterial *keymat,
1220 if (ctx->responder == TRUE) {
1222 silc_cipher_set_key(idata->send_key, keymat->receive_enc_key,
1223 keymat->enc_key_len);
1224 silc_cipher_set_iv(idata->send_key, keymat->receive_iv);
1225 silc_hmac_set_key(idata->hmac_send, keymat->receive_hmac_key,
1226 keymat->hmac_key_len);
1228 silc_cipher_set_key(idata->receive_key, keymat->send_enc_key,
1229 keymat->enc_key_len);
1230 silc_cipher_set_iv(idata->receive_key, keymat->send_iv);
1231 silc_hmac_set_key(idata->hmac_receive, keymat->send_hmac_key,
1232 keymat->hmac_key_len);
1236 silc_cipher_set_key(idata->send_key, keymat->send_enc_key,
1237 keymat->enc_key_len);
1238 silc_cipher_set_iv(idata->send_key, keymat->send_iv);
1239 silc_hmac_set_key(idata->hmac_send, keymat->send_hmac_key,
1240 keymat->hmac_key_len);
1242 silc_cipher_set_key(idata->receive_key, keymat->receive_enc_key,
1243 keymat->enc_key_len);
1244 silc_cipher_set_iv(idata->receive_key, keymat->receive_iv);
1245 silc_hmac_set_key(idata->hmac_receive, keymat->receive_hmac_key,
1246 keymat->hmac_key_len);
1250 /* Save the current sending encryption key */
1252 memset(idata->rekey->send_enc_key, 0, idata->rekey->enc_key_len);
1253 silc_free(idata->rekey->send_enc_key);
1254 idata->rekey->send_enc_key =
1255 silc_calloc(keymat->enc_key_len / 8,
1256 sizeof(*idata->rekey->send_enc_key));
1257 memcpy(idata->rekey->send_enc_key, keymat->send_enc_key,
1258 keymat->enc_key_len / 8);
1259 idata->rekey->enc_key_len = keymat->enc_key_len / 8;
1263 /* This function actually re-generates (when not using PFS) the keys and
1264 takes them into use. */
1266 void silc_server_protocol_rekey_generate(SilcServer server,
1267 SilcServerRekeyInternalContext *ctx,
1270 SilcIDListData idata = (SilcIDListData)ctx->sock->user_data;
1271 SilcSKEKeyMaterial *keymat;
1272 uint32 key_len = silc_cipher_get_key_len(idata->send_key);
1273 uint32 hash_len = idata->hash->hash->hash_len;
1275 SILC_LOG_DEBUG(("Generating new %s session keys (no PFS)",
1276 send ? "sending" : "receiving"));
1278 /* Generate the new key */
1279 keymat = silc_calloc(1, sizeof(*keymat));
1280 silc_ske_process_key_material_data(idata->rekey->send_enc_key,
1281 idata->rekey->enc_key_len,
1282 16, key_len, hash_len,
1283 idata->hash, keymat);
1285 /* Set the keys into use */
1286 silc_server_protocol_rekey_validate(server, ctx, idata, keymat, send);
1288 silc_ske_free_key_material(keymat);
1291 /* This function actually re-generates (with PFS) the keys and
1292 takes them into use. */
1295 silc_server_protocol_rekey_generate_pfs(SilcServer server,
1296 SilcServerRekeyInternalContext *ctx,
1299 SilcIDListData idata = (SilcIDListData)ctx->sock->user_data;
1300 SilcSKEKeyMaterial *keymat;
1301 uint32 key_len = silc_cipher_get_key_len(idata->send_key);
1302 uint32 hash_len = idata->hash->hash->hash_len;
1303 unsigned char *tmpbuf;
1306 SILC_LOG_DEBUG(("Generating new %s session keys (with PFS)",
1307 send ? "sending" : "receiving"));
1309 /* Encode KEY to binary data */
1310 tmpbuf = silc_mp_mp2bin(ctx->ske->KEY, 0, &klen);
1312 /* Generate the new key */
1313 keymat = silc_calloc(1, sizeof(*keymat));
1314 silc_ske_process_key_material_data(tmpbuf, klen, 16, key_len, hash_len,
1315 idata->hash, keymat);
1317 /* Set the keys into use */
1318 silc_server_protocol_rekey_validate(server, ctx, idata, keymat, send);
1320 memset(tmpbuf, 0, klen);
1322 silc_ske_free_key_material(keymat);
1325 /* Packet sending callback. This function is provided as packet sending
1326 routine to the Key Exchange functions. */
1329 silc_server_protocol_rekey_send_packet(SilcSKE ske,
1331 SilcPacketType type,
1334 SilcProtocol protocol = (SilcProtocol)context;
1335 SilcServerRekeyInternalContext *ctx =
1336 (SilcServerRekeyInternalContext *)protocol->context;
1337 SilcServer server = (SilcServer)ctx->server;
1339 /* Send the packet immediately */
1340 silc_server_packet_send(server, ctx->sock,
1341 type, 0, packet->data, packet->len, FALSE);
1344 /* Performs re-key as defined in the SILC protocol specification. */
1346 SILC_TASK_CALLBACK(silc_server_protocol_rekey)
1348 SilcProtocol protocol = (SilcProtocol)context;
1349 SilcServerRekeyInternalContext *ctx =
1350 (SilcServerRekeyInternalContext *)protocol->context;
1351 SilcServer server = (SilcServer)ctx->server;
1352 SilcIDListData idata = (SilcIDListData)ctx->sock->user_data;
1353 SilcSKEStatus status;
1355 SILC_LOG_DEBUG(("Start"));
1357 if (protocol->state == SILC_PROTOCOL_STATE_UNKNOWN)
1358 protocol->state = SILC_PROTOCOL_STATE_START;
1360 SILC_LOG_DEBUG(("State=%d", protocol->state));
1362 switch(protocol->state) {
1363 case SILC_PROTOCOL_STATE_START:
1369 if (ctx->responder == TRUE) {
1371 * We are receiving party
1374 if (ctx->pfs == TRUE) {
1376 * Use Perfect Forward Secrecy, ie. negotiate the key material
1377 * using the SKE protocol.
1380 if (ctx->packet->type != SILC_PACKET_KEY_EXCHANGE_1) {
1381 /* Error in protocol */
1382 protocol->state = SILC_PROTOCOL_STATE_ERROR;
1383 silc_protocol_execute(protocol, server->schedule, 0, 300000);
1387 ctx->ske = silc_ske_alloc();
1388 ctx->ske->rng = server->rng;
1389 ctx->ske->prop = silc_calloc(1, sizeof(*ctx->ske->prop));
1390 silc_ske_group_get_by_number(idata->rekey->ske_group,
1391 &ctx->ske->prop->group);
1393 silc_ske_set_callbacks(ctx->ske,
1394 silc_server_protocol_rekey_send_packet,
1395 NULL, NULL, NULL, silc_ske_check_version,
1398 status = silc_ske_responder_phase_2(ctx->ske, ctx->packet->buffer);
1399 if (status != SILC_SKE_STATUS_OK) {
1400 SILC_LOG_WARNING(("Error (%s) during Re-key (PFS)",
1401 silc_ske_map_status(status)));
1403 protocol->state = SILC_PROTOCOL_STATE_ERROR;
1404 silc_protocol_execute(protocol, server->schedule, 0, 300000);
1408 /* Advance the protocol state */
1410 silc_protocol_execute(protocol, server->schedule, 0, 0);
1413 * Do normal and simple re-key.
1416 /* Send the REKEY_DONE to indicate we will take new keys into use */
1417 silc_server_packet_send(server, ctx->sock, SILC_PACKET_REKEY_DONE,
1420 /* After we send REKEY_DONE we must set the sending encryption
1421 key to the new key since all packets after this packet must
1422 encrypted with the new key. */
1423 silc_server_protocol_rekey_generate(server, ctx, TRUE);
1425 /* The protocol ends in next stage. */
1426 protocol->state = SILC_PROTOCOL_STATE_END;
1431 * We are the initiator of this protocol
1434 /* Start the re-key by sending the REKEY packet */
1435 silc_server_packet_send(server, ctx->sock, SILC_PACKET_REKEY,
1438 if (ctx->pfs == TRUE) {
1440 * Use Perfect Forward Secrecy, ie. negotiate the key material
1441 * using the SKE protocol.
1443 ctx->ske = silc_ske_alloc();
1444 ctx->ske->rng = server->rng;
1445 ctx->ske->prop = silc_calloc(1, sizeof(*ctx->ske->prop));
1446 silc_ske_group_get_by_number(idata->rekey->ske_group,
1447 &ctx->ske->prop->group);
1449 silc_ske_set_callbacks(ctx->ske,
1450 silc_server_protocol_rekey_send_packet,
1451 NULL, NULL, NULL, silc_ske_check_version,
1454 status = silc_ske_initiator_phase_2(ctx->ske, NULL, NULL);
1455 if (status != SILC_SKE_STATUS_OK) {
1456 SILC_LOG_WARNING(("Error (%s) during Re-key (PFS)",
1457 silc_ske_map_status(status)));
1459 protocol->state = SILC_PROTOCOL_STATE_ERROR;
1460 silc_protocol_execute(protocol, server->schedule, 0, 300000);
1464 /* Advance the protocol state */
1468 * Do normal and simple re-key.
1471 /* Send the REKEY_DONE to indicate we will take new keys into use
1473 silc_server_packet_send(server, ctx->sock, SILC_PACKET_REKEY_DONE,
1476 /* After we send REKEY_DONE we must set the sending encryption
1477 key to the new key since all packets after this packet must
1478 encrypted with the new key. */
1479 silc_server_protocol_rekey_generate(server, ctx, TRUE);
1481 /* The protocol ends in next stage. */
1482 protocol->state = SILC_PROTOCOL_STATE_END;
1490 * Second state, used only when oding re-key with PFS.
1492 if (ctx->responder == TRUE) {
1493 if (ctx->pfs == TRUE) {
1495 * Send our KE packe to the initiator now that we've processed
1496 * the initiator's KE packet.
1498 status = silc_ske_responder_finish(ctx->ske, NULL, NULL,
1499 SILC_SKE_PK_TYPE_SILC);
1500 if (status != SILC_SKE_STATUS_OK) {
1501 SILC_LOG_WARNING(("Error (%s) during Re-key (PFS)",
1502 silc_ske_map_status(status)));
1504 protocol->state = SILC_PROTOCOL_STATE_ERROR;
1505 silc_protocol_execute(protocol, server->schedule, 0, 300000);
1511 if (ctx->pfs == TRUE) {
1513 * The packet type must be KE packet
1515 if (ctx->packet->type != SILC_PACKET_KEY_EXCHANGE_2) {
1516 /* Error in protocol */
1517 protocol->state = SILC_PROTOCOL_STATE_ERROR;
1518 silc_protocol_execute(protocol, server->schedule, 0, 300000);
1522 status = silc_ske_initiator_finish(ctx->ske, ctx->packet->buffer);
1523 if (status != SILC_SKE_STATUS_OK) {
1524 SILC_LOG_WARNING(("Error (%s) during Re-key (PFS)",
1525 silc_ske_map_status(status)));
1527 protocol->state = SILC_PROTOCOL_STATE_ERROR;
1528 silc_protocol_execute(protocol, server->schedule, 0, 300000);
1534 /* Send the REKEY_DONE to indicate we will take new keys into use
1536 silc_server_packet_send(server, ctx->sock, SILC_PACKET_REKEY_DONE,
1539 /* After we send REKEY_DONE we must set the sending encryption
1540 key to the new key since all packets after this packet must
1541 encrypted with the new key. */
1542 silc_server_protocol_rekey_generate_pfs(server, ctx, TRUE);
1544 /* The protocol ends in next stage. */
1545 protocol->state = SILC_PROTOCOL_STATE_END;
1548 case SILC_PROTOCOL_STATE_END:
1553 if (ctx->packet->type != SILC_PACKET_REKEY_DONE) {
1554 /* Error in protocol */
1555 protocol->state = SILC_PROTOCOL_STATE_ERROR;
1556 silc_protocol_execute(protocol, server->schedule, 0, 300000);
1560 /* We received the REKEY_DONE packet and all packets after this is
1561 encrypted with the new key so set the decryption key to the new key */
1562 silc_server_protocol_rekey_generate(server, ctx, FALSE);
1564 /* Assure that after calling final callback there cannot be pending
1565 executions for this protocol anymore. This just unregisters any
1566 timeout callbacks for this protocol. */
1567 silc_protocol_cancel(protocol, server->schedule);
1569 /* Protocol has ended, call the final callback */
1570 if (protocol->final_callback)
1571 silc_protocol_execute_final(protocol, server->schedule);
1573 silc_protocol_free(protocol);
1576 case SILC_PROTOCOL_STATE_ERROR:
1581 if (ctx->pfs == TRUE) {
1582 /* Send abort notification */
1583 silc_ske_abort(ctx->ske, ctx->ske->status);
1586 /* Assure that after calling final callback there cannot be pending
1587 executions for this protocol anymore. This just unregisters any
1588 timeout callbacks for this protocol. */
1589 silc_protocol_cancel(protocol, server->schedule);
1591 /* On error the final callback is always called. */
1592 if (protocol->final_callback)
1593 silc_protocol_execute_final(protocol, server->schedule);
1595 silc_protocol_free(protocol);
1598 case SILC_PROTOCOL_STATE_FAILURE:
1600 * We have received failure from remote
1603 /* Assure that after calling final callback there cannot be pending
1604 executions for this protocol anymore. This just unregisters any
1605 timeout callbacks for this protocol. */
1606 silc_protocol_cancel(protocol, server->schedule);
1608 /* On error the final callback is always called. */
1609 if (protocol->final_callback)
1610 silc_protocol_execute_final(protocol, server->schedule);
1612 silc_protocol_free(protocol);
1615 case SILC_PROTOCOL_STATE_UNKNOWN:
1621 /* Registers protocols used in server. */
1623 void silc_server_protocols_register(void)
1625 silc_protocol_register(SILC_PROTOCOL_SERVER_CONNECTION_AUTH,
1626 silc_server_protocol_connection_auth);
1627 silc_protocol_register(SILC_PROTOCOL_SERVER_KEY_EXCHANGE,
1628 silc_server_protocol_key_exchange);
1629 silc_protocol_register(SILC_PROTOCOL_SERVER_REKEY,
1630 silc_server_protocol_rekey);
1631 silc_protocol_register(SILC_PROTOCOL_SERVER_BACKUP,
1632 silc_server_protocol_backup);
1635 /* Unregisters protocols */
1637 void silc_server_protocols_unregister(void)
1639 silc_protocol_unregister(SILC_PROTOCOL_SERVER_CONNECTION_AUTH,
1640 silc_server_protocol_connection_auth);
1641 silc_protocol_unregister(SILC_PROTOCOL_SERVER_KEY_EXCHANGE,
1642 silc_server_protocol_key_exchange);
1643 silc_protocol_unregister(SILC_PROTOCOL_SERVER_REKEY,
1644 silc_server_protocol_rekey);
1645 silc_protocol_unregister(SILC_PROTOCOL_SERVER_BACKUP,
1646 silc_server_protocol_backup);