A LOT updates. Cannot separate. :)
[silc.git] / lib / silcske / silcske.c
index 2ee18f78f51bc2beb4e78c30adb20b368e117c76..0dac063376bdd3490cf231c9c9faefc128fd3afa 100644 (file)
   GNU General Public License for more details.
 
 */
-/*
- * $Id$
- * $Log$
- * Revision 1.6  2000/07/19 07:04:37  priikone
- *     Added version detection support to SKE. Minor bugfixes.
- *
- * Revision 1.5  2000/07/10 05:34:22  priikone
- *     Added mp binary encoding as protocols defines.
- *
- * Revision 1.4  2000/07/07 06:46:43  priikone
- *     Removed ske_verify_public_key function as it is not needed
- *     anymore. Added support to the public key verification as callback
- *     function. Other minor changes and bug fixes.
- *
- * Revision 1.3  2000/07/06 07:12:39  priikone
- *     Support for SILC style public keys added.
- *
- * Revision 1.2  2000/07/05 06:05:15  priikone
- *     Global cosmetic change.
- *
- * Revision 1.1.1.1  2000/06/27 11:36:56  priikone
- *     Imported from internal CVS/Added Log headers.
- *
- *
- */
+/* $Id$ */
 
 #include "silcincludes.h"
 #include "payload_internal.h"
@@ -56,6 +32,7 @@ SilcSKE silc_ske_alloc()
   SILC_LOG_DEBUG(("Allocating new Key Exchange object"));
 
   ske = silc_calloc(1, sizeof(*ske));
+  ske->status = SILC_SKE_STATUS_OK;
 
   return ske;
 }
@@ -224,6 +201,7 @@ SilcSKEStatus silc_ske_initiator_phase_1(SilcSKE ske,
   if (status == SILC_SKE_STATUS_OK)
     return SILC_SKE_STATUS_ERROR;
 
+  ske->status = status;
   return status;
 }
 
@@ -392,6 +370,7 @@ SilcSKEStatus silc_ske_initiator_finish(SilcSKE ske,
   if (status == SILC_SKE_STATUS_OK)
     return SILC_SKE_STATUS_ERROR;
 
+  ske->status = status;
   return status;
 }
 
@@ -449,6 +428,7 @@ SilcSKEStatus silc_ske_responder_start(SilcSKE ske, SilcRng rng,
   if (status == SILC_SKE_STATUS_OK)
     return SILC_SKE_STATUS_ERROR;
 
+  ske->status = status;
   return status;
 }
 
@@ -523,6 +503,7 @@ SilcSKEStatus silc_ske_responder_phase_1(SilcSKE ske,
   if (status == SILC_SKE_STATUS_OK)
     return SILC_SKE_STATUS_ERROR;
 
+  ske->status = status;
   return status;
 }
 
@@ -664,6 +645,7 @@ SilcSKEStatus silc_ske_responder_finish(SilcSKE ske,
   if (status == SILC_SKE_STATUS_OK)
     return SILC_SKE_STATUS_ERROR;
 
+  ske->status = status;
   return status;
 }
 
@@ -675,18 +657,22 @@ SilcSKEStatus silc_ske_end(SilcSKE ske,
                           SilcSKESendPacketCb send_packet,
                           void *context)
 {
-  SilcSKEStatus status = SILC_SKE_STATUS_OK;
   SilcBuffer packet;
 
   SILC_LOG_DEBUG(("Start"));
 
-  packet = silc_buffer_alloc(1);
-  packet->len = 0;
+  packet = silc_buffer_alloc(4);
+  silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
+  silc_buffer_format(packet,
+                    SILC_STR_UI_SHORT(SILC_SKE_STATUS_OK),
+                    SILC_STR_END);
 
   if (send_packet)
     (*send_packet)(ske, packet, SILC_PACKET_SUCCESS, context);
 
-  return status;
+  silc_buffer_free(packet);
+
+  return SILC_SKE_STATUS_OK;
 }
 
 /* Aborts the Key Exchange protocol. This is called if error occurs
@@ -724,24 +710,25 @@ SilcSKEStatus silc_ske_abort(SilcSKE ske, SilcSKEStatus status,
 
 SilcSKEStatus 
 silc_ske_assemble_security_properties(SilcSKE ske,
+                                     unsigned char flags,
                                      char *version,
                                      SilcSKEStartPayload **return_payload)
 {
   SilcSKEStartPayload *rp;
+  int i;
 
   SILC_LOG_DEBUG(("Assembling KE Start Payload"));
 
   rp = silc_calloc(1, sizeof(*rp));
 
-  /* XXX */
   /* Set flags */
-  rp->flags = 0;
+  rp->flags = flags;
 
-  /* XXX */
-  /* Cookie */
-  rp->cookie = silc_calloc(SILC_SKE_COOKIE_LEN, sizeof(unsigned char));
+  /* Set random cookie */
+  rp->cookie = silc_calloc(SILC_SKE_COOKIE_LEN, sizeof(*rp->cookie));
+  for (i = 0; i < SILC_SKE_COOKIE_LEN; i++)
+    rp->cookie[i] = silc_rng_get_byte(ske->rng);
   rp->cookie_len = SILC_SKE_COOKIE_LEN;
-  memcpy(rp->cookie, "1234567890123456", SILC_SKE_COOKIE_LEN);
 
   /* Put version */
   rp->version = strdup(version);
@@ -804,7 +791,8 @@ silc_ske_select_security_properties(SilcSKE ske,
   payload->cookie_len = SILC_SKE_COOKIE_LEN;
   memcpy(payload->cookie, rp->cookie, SILC_SKE_COOKIE_LEN);
 
-  /* XXX Do version check */
+  /* Check version string */
+  silc_ske_check_version(ske, rp->version, rp->version_len);
 
   /* Put our version to our reply */
   payload->version = strdup(version);