5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2005 - 2007 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; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
21 #include "silcconnauth.h"
23 /************************** Types and definitions ***************************/
25 SILC_FSM_STATE(silc_connauth_st_initiator_start);
26 SILC_FSM_STATE(silc_connauth_st_initiator_auth_send);
27 SILC_FSM_STATE(silc_connauth_st_initiator_result);
28 SILC_FSM_STATE(silc_connauth_st_initiator_failure);
29 SILC_FSM_STATE(silc_connauth_st_responder_start);
30 SILC_FSM_STATE(silc_connauth_st_responder_authenticate);
31 SILC_FSM_STATE(silc_connauth_st_responder_authenticate_pk);
32 SILC_FSM_STATE(silc_connauth_st_responder_success);
33 SILC_FSM_STATE(silc_connauth_st_responder_failure);
35 static SilcBool silc_connauth_packet_receive(SilcPacketEngine engine,
36 SilcPacketStream stream,
38 void *callback_context,
41 /* Connection authentication context */
42 struct SilcConnAuthStruct {
45 SilcAsyncOperationStruct op;
46 SilcAsyncOperation key_op;
47 SilcConnectionType conn_type;
48 SilcAuthMethod auth_method;
50 SilcUInt32 auth_data_len;
51 SilcConnAuthCompletion completion;
52 SilcConnAuthGetAuthData get_auth_data;
54 SilcDList public_keys;
55 SilcSKRStatus skr_status;
56 SilcUInt32 timeout_secs;
58 unsigned int aborted : 1;
59 unsigned int success : 1;
62 /* Packet stream callbacks */
63 static SilcPacketCallbacks silc_connauth_stream_cbs =
65 silc_connauth_packet_receive, NULL, NULL
69 /************************ Static utility functions **************************/
73 static SilcBool silc_connauth_packet_receive(SilcPacketEngine engine,
74 SilcPacketStream stream,
76 void *callback_context,
79 SilcConnAuth connauth = callback_context;
80 connauth->packet = packet;
81 silc_fsm_continue(connauth->fsm);
85 /* Async operation abortion callback */
87 static void silc_connauth_abort(SilcAsyncOperation op, void *context)
89 SilcConnAuth connauth = context;
91 silc_async_abort(connauth->key_op, NULL, NULL);
92 connauth->aborted = TRUE;
95 /* Signature callback */
97 static void silc_connauth_get_signature_cb(SilcBool success,
98 const unsigned char *signature,
99 SilcUInt32 signature_len,
102 SilcConnAuth connauth = context;
104 connauth->key_op = NULL;
107 silc_fsm_next(connauth->fsm, silc_connauth_st_initiator_failure);
108 SILC_FSM_CALL_CONTINUE(connauth->fsm);
112 connauth->auth_data = silc_memdup(signature, signature_len);
113 connauth->auth_data_len = signature_len;
115 SILC_FSM_CALL_CONTINUE(connauth->fsm);
118 /* Generates signature for public key based authentication */
120 static SilcAsyncOperation
121 silc_connauth_get_signature(SilcConnAuth connauth)
123 SilcAsyncOperation op;
125 SilcPrivateKey private_key;
129 SILC_LOG_DEBUG(("Compute signature"));
132 private_key = connauth->auth_data;
134 /* Make the authentication data. Protocol says it is HASH plus
136 len = ske->hash_len + silc_buffer_len(ske->start_payload_copy);
137 auth = silc_buffer_alloc_size(len);
139 silc_connauth_get_signature_cb(FALSE, NULL, 0, connauth);
142 silc_buffer_format(auth,
143 SILC_STR_DATA(ske->hash, ske->hash_len),
144 SILC_STR_DATA(ske->start_payload_copy->data,
145 silc_buffer_len(ske->start_payload_copy)),
148 /* Compute signature */
149 op = silc_pkcs_sign(private_key, auth->data, silc_buffer_len(auth),
150 TRUE, ske->prop->hash, ske->rng,
151 silc_connauth_get_signature_cb, connauth);
153 silc_buffer_free(auth);
158 /* Verify callback */
160 static void silc_connauth_verify_signature_cb(SilcBool success,
163 SilcConnAuth connauth = context;
165 connauth->key_op = NULL;
166 silc_free(connauth->auth_data);
169 SILC_LOG_DEBUG(("Invalid signature"));
170 silc_fsm_next(connauth->fsm, silc_connauth_st_responder_failure);
171 SILC_FSM_CALL_CONTINUE(connauth->fsm);
175 SILC_LOG_DEBUG(("Signature is Ok"));
176 SILC_FSM_CALL_CONTINUE(connauth->fsm);
179 /* Verifies digital signature */
181 static SilcAsyncOperation
182 silc_connauth_verify_signature(SilcConnAuth connauth,
183 SilcPublicKey pub_key,
187 SilcAsyncOperation op;
189 SilcSKE ske = connauth->ske;
192 if (!pub_key || !sign) {
193 silc_connauth_verify_signature_cb(FALSE, connauth);
197 /* Make the authentication data. Protocol says it is HASH plus
199 len = ske->hash_len + silc_buffer_len(ske->start_payload_copy);
200 auth = silc_buffer_alloc_size(len);
202 silc_connauth_verify_signature_cb(FALSE, connauth);
205 silc_buffer_format(auth,
206 SILC_STR_UI_XNSTRING(ske->hash, ske->hash_len),
207 SILC_STR_UI_XNSTRING(
208 ske->start_payload_copy->data,
209 silc_buffer_len(ske->start_payload_copy)),
212 /* Verify signature */
213 op = silc_pkcs_verify(pub_key, sign, sign_len, auth->data,
214 silc_buffer_len(auth), ske->prop->hash,
215 silc_connauth_verify_signature_cb, connauth);
217 silc_buffer_free(auth);
224 SILC_TASK_CALLBACK(silc_connauth_timeout)
226 SilcConnAuth connauth = context;
227 SILC_LOG_DEBUG(("Protocol timeout"));
228 if (connauth->key_op)
229 silc_async_abort(connauth->key_op, NULL, NULL);
230 connauth->aborted = TRUE;
231 silc_fsm_continue_sync(connauth->fsm);
236 static void silc_connauth_skr_callback(SilcSKR skr, SilcSKRFind find,
237 SilcSKRStatus status,
238 SilcDList results, void *context)
240 SilcConnAuth connauth = context;
242 silc_skr_find_free(find);
244 connauth->public_keys = results;
245 connauth->skr_status = status;
247 SILC_FSM_CALL_CONTINUE(connauth->fsm);
252 static void silc_connauth_fsm_destructor(SilcFSM fsm, void *fsm_context,
253 void *destructor_context)
259 /******************************* Protocol API *******************************/
261 /* Allocate connection authentication context */
263 SilcConnAuth silc_connauth_alloc(SilcSchedule schedule,
265 SilcUInt32 timeout_secs)
267 SilcConnAuth connauth;
269 if (!schedule || !ske)
272 connauth = silc_calloc(1, sizeof(*connauth));
276 connauth->fsm = silc_fsm_alloc(connauth, silc_connauth_fsm_destructor,
278 if (!connauth->fsm) {
279 silc_connauth_free(connauth);
283 connauth->timeout_secs = timeout_secs;
290 /* Free connection authentication context */
292 void silc_connauth_free(SilcConnAuth connauth)
294 if (connauth->public_keys)
295 silc_dlist_uninit(connauth->public_keys);
298 silc_ske_free(connauth->ske);
303 /* Return associated SKE context */
305 SilcSKE silc_connauth_get_ske(SilcConnAuth connauth)
307 return connauth->ske;
311 /******************************** Initiator *********************************/
313 SILC_FSM_STATE(silc_connauth_st_initiator_start)
315 SilcConnAuth connauth = fsm_context;
317 SILC_LOG_DEBUG(("Start"));
319 if (connauth->aborted) {
321 silc_fsm_next(fsm, silc_connauth_st_initiator_failure);
322 return SILC_FSM_CONTINUE;
326 if (connauth->timeout_secs)
327 silc_schedule_task_add_timeout(silc_fsm_get_schedule(fsm),
328 silc_connauth_timeout, connauth,
329 connauth->timeout_secs, 0);
331 /** Generate auth data */
332 silc_fsm_next(fsm, silc_connauth_st_initiator_auth_send);
334 /* Get authentication data */
335 switch (connauth->auth_method) {
337 /* No authentication required */
338 connauth->auth_data = NULL;
339 connauth->auth_data_len = 0;
340 return SILC_FSM_CONTINUE;
343 case SILC_AUTH_PASSWORD:
344 /* We have authentication data already */
345 return SILC_FSM_CONTINUE;
348 case SILC_AUTH_PUBLIC_KEY:
349 /* Compute signature */
350 SILC_FSM_CALL(connauth->key_op = silc_connauth_get_signature(connauth));
355 silc_fsm_next(fsm, silc_connauth_st_initiator_failure);
356 return SILC_FSM_CONTINUE;
359 SILC_FSM_STATE(silc_connauth_st_initiator_auth_send)
361 SilcConnAuth connauth = fsm_context;
364 SilcPacketFlags flags = 0;
366 if (connauth->auth_method == SILC_AUTH_PASSWORD)
367 flags |= SILC_PACKET_FLAG_LONG_PAD;
369 payload_len = 4 + connauth->auth_data_len;
370 packet = silc_buffer_alloc_size(payload_len);
373 silc_fsm_next(fsm, silc_connauth_st_initiator_failure);
374 return SILC_FSM_CONTINUE;
377 silc_buffer_format(packet,
378 SILC_STR_UI_SHORT(payload_len),
379 SILC_STR_UI_SHORT(connauth->conn_type),
380 SILC_STR_DATA(connauth->auth_data,
381 connauth->auth_data_len),
384 silc_free(connauth->auth_data);
386 /* Send the packet */
387 if (!silc_packet_send(connauth->ske->stream, SILC_PACKET_CONNECTION_AUTH,
388 flags, packet->data, silc_buffer_len(packet))) {
389 /** Error sending packet */
390 silc_fsm_next(fsm, silc_connauth_st_initiator_failure);
391 return SILC_FSM_CONTINUE;
394 silc_buffer_free(packet);
396 /** Wait for responder */
397 silc_fsm_next(fsm, silc_connauth_st_initiator_result);
398 return SILC_FSM_WAIT;
401 SILC_FSM_STATE(silc_connauth_st_initiator_result)
403 SilcConnAuth connauth = fsm_context;
405 SILC_LOG_DEBUG(("Start"));
407 if (connauth->aborted) {
409 silc_fsm_next(fsm, silc_connauth_st_initiator_failure);
410 return SILC_FSM_CONTINUE;
413 /* Check the status of authentication */
414 if (connauth->packet->type == SILC_PACKET_SUCCESS) {
415 SILC_LOG_DEBUG(("Authentication successful"));
416 connauth->success = TRUE;
418 SILC_LOG_DEBUG(("Authentication failed, packet %s received",
419 silc_get_packet_name(connauth->packet->type)));
420 connauth->success = FALSE;
422 silc_packet_free(connauth->packet);
424 silc_packet_stream_unlink(connauth->ske->stream,
425 &silc_connauth_stream_cbs, connauth);
426 silc_schedule_task_del_by_context(silc_fsm_get_schedule(fsm), connauth);
428 /* Call completion callback */
429 connauth->completion(connauth, connauth->success, connauth->context);
431 return SILC_FSM_FINISH;
434 SILC_FSM_STATE(silc_connauth_st_initiator_failure)
436 SilcConnAuth connauth = fsm_context;
437 unsigned char error[4];
439 SILC_LOG_DEBUG(("Start"));
441 if (!connauth->aborted) {
442 /* Send FAILURE packet */
443 SILC_PUT32_MSB(SILC_AUTH_FAILED, error);
444 silc_packet_send(connauth->ske->stream, SILC_PACKET_FAILURE, 0, error, 4);
446 silc_packet_stream_unlink(connauth->ske->stream,
447 &silc_connauth_stream_cbs, connauth);
448 silc_schedule_task_del_by_context(silc_fsm_get_schedule(fsm), connauth);
450 /* Call completion callback */
451 connauth->completion(connauth, FALSE, connauth->context);
452 return SILC_FSM_FINISH;
455 silc_packet_stream_unlink(connauth->ske->stream,
456 &silc_connauth_stream_cbs, connauth);
457 silc_schedule_task_del_by_context(silc_fsm_get_schedule(fsm), connauth);
459 return SILC_FSM_FINISH;
463 silc_connauth_initiator(SilcConnAuth connauth,
464 SilcConnectionType conn_type,
465 SilcAuthMethod auth_method, void *auth_data,
466 SilcUInt32 auth_data_len,
467 SilcConnAuthCompletion completion,
470 SILC_LOG_DEBUG(("Connection authentication as initiator"));
472 if (auth_method == SILC_AUTH_PASSWORD && !auth_data) {
473 completion(connauth, FALSE, context);
477 if (auth_method == SILC_AUTH_PUBLIC_KEY && !auth_data) {
478 completion(connauth, FALSE, context);
482 connauth->conn_type = conn_type;
483 connauth->auth_method = auth_method;
484 connauth->completion = completion;
485 connauth->context = context;
486 connauth->auth_data = auth_data;
487 connauth->auth_data_len = auth_data_len;
489 if (connauth->auth_method == SILC_AUTH_PASSWORD)
490 connauth->auth_data = silc_memdup(connauth->auth_data,
491 connauth->auth_data_len);
493 /* Link to packet stream to get packets */
494 silc_packet_stream_link(connauth->ske->stream,
495 &silc_connauth_stream_cbs, connauth, 1000000,
497 SILC_PACKET_FAILURE, -1);
499 /* Start the protocol */
500 silc_async_init(&connauth->op, silc_connauth_abort, NULL, connauth);
501 silc_fsm_start(connauth->fsm, silc_connauth_st_initiator_start);
503 return &connauth->op;
507 /******************************** Responder *********************************/
509 SILC_FSM_STATE(silc_connauth_st_responder_start)
511 SilcConnAuth connauth = fsm_context;
513 SILC_LOG_DEBUG(("Start"));
515 if (connauth->aborted) {
517 silc_fsm_next(fsm, silc_connauth_st_responder_failure);
518 return SILC_FSM_CONTINUE;
522 if (connauth->timeout_secs)
523 silc_schedule_task_add_timeout(silc_fsm_get_schedule(fsm),
524 silc_connauth_timeout, connauth,
525 connauth->timeout_secs, 0);
527 /** Wait for initiator */
528 silc_fsm_next(fsm, silc_connauth_st_responder_authenticate);
529 return SILC_FSM_WAIT;
532 SILC_FSM_STATE(silc_connauth_st_responder_authenticate)
534 SilcConnAuth connauth = fsm_context;
535 SilcUInt16 payload_len;
536 SilcUInt16 conn_type;
537 unsigned char *auth_data = NULL, *passphrase = NULL;
538 SilcUInt32 passphrase_len;
539 SilcSKR repository = NULL;
542 SILC_LOG_DEBUG(("Start"));
544 if (connauth->aborted) {
546 if (connauth->packet)
547 silc_packet_free(connauth->packet);
548 silc_fsm_next(fsm, silc_connauth_st_responder_failure);
549 return SILC_FSM_CONTINUE;
552 if (connauth->packet->type != SILC_PACKET_CONNECTION_AUTH) {
553 /** Protocol failure */
554 silc_packet_free(connauth->packet);
555 silc_fsm_next(fsm, silc_connauth_st_responder_failure);
556 return SILC_FSM_CONTINUE;
559 /* Parse the received authentication data packet. The received
560 payload is Connection Auth Payload. */
561 ret = silc_buffer_unformat(&connauth->packet->buffer,
562 SILC_STR_UI_SHORT(&payload_len),
563 SILC_STR_UI_SHORT(&conn_type),
567 SILC_LOG_ERROR(("Bad payload in authentication packet"));
568 silc_packet_free(connauth->packet);
569 silc_fsm_next(fsm, silc_connauth_st_responder_failure);
570 return SILC_FSM_CONTINUE;
573 if (payload_len != silc_buffer_len(&connauth->packet->buffer)) {
574 /** Bad payload length */
575 SILC_LOG_ERROR(("Bad payload length in authentication packet"));
576 silc_packet_free(connauth->packet);
577 silc_fsm_next(fsm, silc_connauth_st_responder_failure);
578 return SILC_FSM_CONTINUE;
583 if (conn_type < SILC_CONN_CLIENT || conn_type > SILC_CONN_ROUTER) {
584 /** Bad connection type */
585 SILC_LOG_ERROR(("Bad connection type (%d) in authentication packet",
587 silc_packet_free(connauth->packet);
588 silc_fsm_next(fsm, silc_connauth_st_responder_failure);
589 return SILC_FSM_CONTINUE;
592 if (payload_len > 0) {
593 /* Get authentication data */
594 ret = silc_buffer_unformat(&connauth->packet->buffer,
596 SILC_STR_UI_XNSTRING(&auth_data,
601 SILC_LOG_DEBUG(("Bad payload in authentication payload"));
602 silc_packet_free(connauth->packet);
603 silc_fsm_next(fsm, silc_connauth_st_responder_failure);
604 return SILC_FSM_CONTINUE;
607 silc_packet_free(connauth->packet);
609 SILC_LOG_DEBUG(("Remote connection type %d", conn_type));
611 /* Get authentication data */
612 if (!connauth->get_auth_data(connauth, conn_type, &passphrase,
613 &passphrase_len, &repository,
614 connauth->context)) {
615 /** Connection not configured */
616 SILC_LOG_ERROR(("Remote connection not configured"));
617 silc_fsm_next(fsm, silc_connauth_st_responder_failure);
618 return SILC_FSM_CONTINUE;
623 /* Passphrase authentication */
624 if (passphrase && passphrase_len) {
625 SILC_LOG_DEBUG(("Passphrase authentication"));
626 if (!auth_data || payload_len != passphrase_len ||
627 memcmp(auth_data, passphrase, passphrase_len)) {
628 /** Authentication failed */
629 silc_fsm_next(fsm, silc_connauth_st_responder_failure);
630 return SILC_FSM_CONTINUE;
632 } else if (repository) {
633 /* Digital signature */
636 SILC_LOG_DEBUG(("Digital signature authentication"));
639 /** Authentication failed */
640 silc_fsm_next(fsm, silc_connauth_st_responder_failure);
641 return SILC_FSM_CONTINUE;
644 connauth->auth_data = silc_memdup(auth_data, payload_len);
645 connauth->auth_data_len = payload_len;
647 /* Allocate search constraints for finding the key */
648 find = silc_skr_find_alloc();
650 if (!find || !connauth->auth_data || !connauth->ske->prop->public_key) {
652 silc_fsm_next(fsm, silc_connauth_st_responder_failure);
653 return SILC_FSM_CONTINUE;
656 silc_skr_find_set_pkcs_type(
657 find, silc_pkcs_get_type(connauth->ske->prop->public_key));
658 silc_skr_find_set_public_key(find, connauth->ske->prop->public_key);
659 silc_skr_find_set_usage(find, (SILC_SKR_USAGE_AUTH |
660 SILC_SKR_USAGE_KEY_AGREEMENT));
662 /** Find public key */
663 silc_fsm_next(fsm, silc_connauth_st_responder_authenticate_pk);
664 SILC_FSM_CALL(connauth->key_op =
665 silc_skr_find(repository, silc_fsm_get_schedule(fsm),
666 find, silc_connauth_skr_callback,
671 /* Passphrase auth Ok, or no authentication required */
673 /** Authentication successful */
674 silc_fsm_next(fsm, silc_connauth_st_responder_success);
675 return SILC_FSM_CONTINUE;
678 SILC_FSM_STATE(silc_connauth_st_responder_authenticate_pk)
680 SilcConnAuth connauth = fsm_context;
683 if (connauth->aborted) {
685 silc_fsm_next(fsm, silc_connauth_st_responder_failure);
686 return SILC_FSM_CONTINUE;
689 if (connauth->skr_status != SILC_SKR_OK) {
690 /** Public key not found */
691 SILC_LOG_DEBUG(("Public key not found, error %d", connauth->skr_status));
692 silc_fsm_next(fsm, silc_connauth_st_responder_failure);
693 return SILC_FSM_CONTINUE;
696 SILC_LOG_DEBUG(("Found %d public keys",
697 silc_dlist_count(connauth->public_keys)));
699 /** Verify signature */
700 key = silc_dlist_get(connauth->public_keys);
701 silc_fsm_next(fsm, silc_connauth_st_responder_success);
702 SILC_FSM_CALL(connauth->key_op =
703 silc_connauth_verify_signature(connauth, key->key,
705 connauth->auth_data_len));
709 SILC_FSM_STATE(silc_connauth_st_responder_success)
711 SilcConnAuth connauth = fsm_context;
712 unsigned char tmp[4];
714 SILC_LOG_DEBUG(("Authentication successful"));
716 /* Send FAILURE packet */
717 SILC_PUT32_MSB(SILC_AUTH_OK, tmp);
718 silc_packet_send(connauth->ske->stream, SILC_PACKET_SUCCESS, 0, tmp, 4);
720 silc_packet_stream_unlink(connauth->ske->stream,
721 &silc_connauth_stream_cbs, connauth);
722 silc_schedule_task_del_by_context(silc_fsm_get_schedule(fsm), connauth);
724 /* Call completion callback */
725 connauth->completion(connauth, TRUE, connauth->context);
727 return SILC_FSM_FINISH;
730 SILC_FSM_STATE(silc_connauth_st_responder_failure)
732 SilcConnAuth connauth = fsm_context;
733 unsigned char error[4];
735 SILC_LOG_ERROR(("Authentication failed"));
737 if (!connauth->aborted) {
738 /* Send FAILURE packet */
739 SILC_PUT32_MSB(SILC_AUTH_FAILED, error);
740 silc_packet_send(connauth->ske->stream, SILC_PACKET_FAILURE, 0, error, 4);
742 silc_packet_stream_unlink(connauth->ske->stream,
743 &silc_connauth_stream_cbs, connauth);
744 silc_schedule_task_del_by_context(silc_fsm_get_schedule(fsm), connauth);
746 /* Call completion callback */
747 connauth->completion(connauth, FALSE, connauth->context);
749 return SILC_FSM_FINISH;
752 silc_packet_stream_unlink(connauth->ske->stream,
753 &silc_connauth_stream_cbs, connauth);
754 silc_schedule_task_del_by_context(silc_fsm_get_schedule(fsm), connauth);
756 return SILC_FSM_FINISH;
760 silc_connauth_responder(SilcConnAuth connauth,
761 SilcConnAuthGetAuthData get_auth_data,
762 SilcConnAuthCompletion completion,
765 SILC_LOG_DEBUG(("Connection authentication as responder"));
767 connauth->get_auth_data = get_auth_data;
768 connauth->completion = completion;
769 connauth->context = context;
771 /* Link to packet stream to get packets */
772 silc_packet_stream_link(connauth->ske->stream,
773 &silc_connauth_stream_cbs, connauth, 1000000,
774 SILC_PACKET_CONNECTION_AUTH,
775 SILC_PACKET_FAILURE, -1);
777 /* Start the protocol */
778 silc_async_init(&connauth->op, silc_connauth_abort, NULL, connauth);
779 silc_fsm_start(connauth->fsm, silc_connauth_st_responder_start);
781 return &connauth->op;