Merge branch 'topic/mm-fixes' of git://208.110.73.182/silc into silc.1.1.branch
[silc.git] / lib / silcske / silcconnauth.c
index ecd515f084363d3f097dcfa442e60fba27a19132..3e92d0ed86a50ca24b08748836f17651d1b3255e 100644 (file)
@@ -357,7 +357,8 @@ SILC_FSM_STATE(silc_connauth_st_initiator_result)
     SILC_LOG_DEBUG(("Authentication successful"));
     connauth->success = TRUE;
   } else {
-    SILC_LOG_DEBUG(("Authentication failed"));
+    SILC_LOG_DEBUG(("Authentication failed, packet %s received",
+                   silc_get_packet_name(connauth->packet->type)));
     connauth->success = FALSE;
   }
   silc_packet_free(connauth->packet);
@@ -384,8 +385,13 @@ SILC_FSM_STATE(silc_connauth_st_initiator_failure)
     SILC_PUT32_MSB(SILC_AUTH_FAILED, error);
     silc_packet_send(connauth->ske->stream, SILC_PACKET_FAILURE, 0, error, 4);
 
+    silc_packet_stream_unlink(connauth->ske->stream,
+                             &silc_connauth_stream_cbs, connauth);
+    silc_schedule_task_del_by_context(silc_fsm_get_schedule(fsm), connauth);
+
     /* Call completion callback */
     connauth->completion(connauth, FALSE, connauth->context);
+    return SILC_FSM_FINISH;
   }
 
   silc_packet_stream_unlink(connauth->ske->stream,
@@ -481,7 +487,8 @@ SILC_FSM_STATE(silc_connauth_st_responder_authenticate)
 
   if (connauth->aborted) {
     /** Aborted */
-    silc_packet_free(connauth->packet);
+    if (connauth->packet)
+      silc_packet_free(connauth->packet);
     silc_fsm_next(fsm, silc_connauth_st_responder_failure);
     return SILC_FSM_CONTINUE;
   }
@@ -560,7 +567,8 @@ SILC_FSM_STATE(silc_connauth_st_responder_authenticate)
   /* Passphrase authentication */
   if (passphrase && passphrase_len) {
     SILC_LOG_DEBUG(("Passphrase authentication"));
-    if (!memcmp(auth_data, passphrase, passphrase_len)) {
+    if (!auth_data || payload_len != passphrase_len ||
+       memcmp(auth_data, passphrase, passphrase_len)) {
       /** Authentication failed */
       silc_fsm_next(fsm, silc_connauth_st_responder_failure);
       return SILC_FSM_CONTINUE;
@@ -571,26 +579,34 @@ SILC_FSM_STATE(silc_connauth_st_responder_authenticate)
 
     SILC_LOG_DEBUG(("Digital signature authentication"));
 
+    if (!auth_data) {
+      /** Authentication failed */
+      silc_fsm_next(fsm, silc_connauth_st_responder_failure);
+      return SILC_FSM_CONTINUE;
+    }
+
     connauth->auth_data = silc_memdup(auth_data, payload_len);
     connauth->auth_data_len = payload_len;
 
     /* Allocate search constraints for finding the key */
     find = silc_skr_find_alloc();
 
-    if (!find || !connauth->auth_data) {
+    if (!find || !connauth->auth_data || !connauth->ske->prop->public_key) {
       /** Out of memory */
       silc_fsm_next(fsm, silc_connauth_st_responder_failure);
       return SILC_FSM_CONTINUE;
     }
 
-    silc_skr_find_set_pkcs_type(find, connauth->ske->pk_type);
-    silc_skr_find_set_public_key(find, connauth->ske->public_key);
+    silc_skr_find_set_pkcs_type(
+                 find, silc_pkcs_get_type(connauth->ske->prop->public_key));
+    silc_skr_find_set_public_key(find, connauth->ske->prop->public_key);
     silc_skr_find_set_usage(find, (SILC_SKR_USAGE_AUTH |
                                   SILC_SKR_USAGE_KEY_AGREEMENT));
 
     /** Find public key */
     silc_fsm_next(fsm, silc_connauth_st_responder_authenticate_pk);
-    SILC_FSM_CALL(silc_skr_find(repository, find, silc_connauth_skr_callback,
+    SILC_FSM_CALL(silc_skr_find(repository, silc_fsm_get_schedule(fsm),
+                               find, silc_connauth_skr_callback,
                                connauth));
     /* NOT REACHED */
   }
@@ -637,6 +653,8 @@ SILC_FSM_STATE(silc_connauth_st_responder_authenticate_pk)
 
   silc_free(connauth->auth_data);
 
+  SILC_LOG_DEBUG(("Signature is Ok"));
+
   /** Authentication successful */
   silc_fsm_next(fsm, silc_connauth_st_responder_success);
   return SILC_FSM_CONTINUE;
@@ -653,13 +671,13 @@ SILC_FSM_STATE(silc_connauth_st_responder_success)
   SILC_PUT32_MSB(SILC_AUTH_OK, tmp);
   silc_packet_send(connauth->ske->stream, SILC_PACKET_SUCCESS, 0, tmp, 4);
 
-  /* Call completion callback */
-  connauth->completion(connauth, TRUE, connauth->context);
-
   silc_packet_stream_unlink(connauth->ske->stream,
                            &silc_connauth_stream_cbs, connauth);
   silc_schedule_task_del_by_context(silc_fsm_get_schedule(fsm), connauth);
 
+  /* Call completion callback */
+  connauth->completion(connauth, TRUE, connauth->context);
+
   return SILC_FSM_FINISH;
 }
 
@@ -675,8 +693,14 @@ SILC_FSM_STATE(silc_connauth_st_responder_failure)
     SILC_PUT32_MSB(SILC_AUTH_FAILED, error);
     silc_packet_send(connauth->ske->stream, SILC_PACKET_FAILURE, 0, error, 4);
 
+    silc_packet_stream_unlink(connauth->ske->stream,
+                             &silc_connauth_stream_cbs, connauth);
+    silc_schedule_task_del_by_context(silc_fsm_get_schedule(fsm), connauth);
+
     /* Call completion callback */
     connauth->completion(connauth, FALSE, connauth->context);
+
+    return SILC_FSM_FINISH;
   }
 
   silc_packet_stream_unlink(connauth->ske->stream,