Added SILC Thread Queue API
[crypto.git] / lib / silccore / tests / test_silcmessage.c
index 47cdf2495309b99d644fff2b990d8428caeb848a..1f7d505229b8f0956bd3c9405801a82b392cd647 100644 (file)
@@ -16,11 +16,10 @@ int main(int argc, char **argv)
   SilcMessagePayload message;
   SilcBuffer buf;
   const char *msg = "FOOBAR MESSAGE";
-  unsigned char *data, tmp[1023];
+  unsigned char *data, tmp[1023], *tmp2;
   SilcUInt32 data_len;
   SilcUInt16 flags;
   int i, n;
-  SilcMessageSignedPayload sig;
 
   if (argc > 1 && !strcmp(argv[1], "-d")) {
     silc_log_debug(TRUE);
@@ -78,7 +77,7 @@ int main(int argc, char **argv)
   SILC_LOG_DEBUG(("Parsing private messsage (static key)"));
   message = silc_message_payload_parse(silc_buffer_data(buf),
                                       silc_buffer_len(buf), TRUE, TRUE,
-                                      key, hmac);
+                                      key, hmac, NULL, FALSE, NULL);
   if (!message)
     goto err;
   flags = silc_message_get_flags(message);
@@ -111,7 +110,7 @@ int main(int argc, char **argv)
   SILC_LOG_DEBUG(("Parsing private messsage (static key)"));
   message = silc_message_payload_parse(silc_buffer_data(buf),
                                       silc_buffer_len(buf), TRUE, TRUE,
-                                      key, hmac);
+                                      key, hmac, NULL, FALSE, NULL);
   if (!message)
     goto err;
   flags = silc_message_get_flags(message);
@@ -144,7 +143,7 @@ int main(int argc, char **argv)
   SILC_LOG_DEBUG(("Parsing private messsage (static key)"));
   message = silc_message_payload_parse(silc_buffer_data(buf),
                                       silc_buffer_len(buf), TRUE, TRUE,
-                                      key, hmac);
+                                      key, hmac, NULL, FALSE, NULL);
   if (!message)
     goto err;
   flags = silc_message_get_flags(message);
@@ -179,7 +178,7 @@ int main(int argc, char **argv)
   SILC_LOG_DEBUG(("Parsing private messsage (static key)"));
   message = silc_message_payload_parse(silc_buffer_data(buf),
                                       silc_buffer_len(buf), TRUE, TRUE,
-                                      key, hmac);
+                                      key, hmac, NULL, FALSE, NULL);
   if (!message)
     goto err;
   flags = silc_message_get_flags(message);
@@ -216,7 +215,7 @@ int main(int argc, char **argv)
   SILC_LOG_DEBUG(("Parsing private messsage (static key)"));
   message = silc_message_payload_parse(silc_buffer_data(buf),
                                       silc_buffer_len(buf), TRUE, TRUE,
-                                      key, hmac);
+                                      key, hmac, NULL, FALSE, NULL);
   if (!message)
     goto err;
   flags = silc_message_get_flags(message);
@@ -235,17 +234,13 @@ int main(int argc, char **argv)
     goto err;
   SILC_LOG_HEXDUMP(("MAC"), silc_message_get_mac(message),
                   silc_hmac_len(hmac));
-  SILC_LOG_DEBUG(("Get signature"));
-  sig = silc_message_get_signature(message);
-  if (!sig)
-    goto err;
   SILC_LOG_DEBUG(("Verifying signature"));
-  if (silc_message_signed_verify(sig, message, public_key, hash) !=
+  if (silc_message_signed_verify(message, public_key, hash) !=
       SILC_AUTH_OK)
     goto err;
   SILC_LOG_DEBUG(("Signature Ok"));
   SILC_LOG_DEBUG(("Get public key"));
-  pk2 = silc_message_signed_get_public_key(sig, NULL, NULL);
+  pk2 = silc_message_signed_get_public_key(message, NULL, NULL);
   if (!pk2)
     goto err;
   SILC_LOG_DEBUG(("Verify public key"));
@@ -273,7 +268,7 @@ int main(int argc, char **argv)
   SILC_LOG_DEBUG(("Parsing channel messsage (static key)"));
   message = silc_message_payload_parse(silc_buffer_data(buf),
                                       silc_buffer_len(buf), FALSE, TRUE,
-                                      key, hmac);
+                                      key, hmac, NULL, FALSE, NULL);
   if (!message)
     goto err;
   flags = silc_message_get_flags(message);
@@ -292,17 +287,13 @@ int main(int argc, char **argv)
     goto err;
   SILC_LOG_HEXDUMP(("MAC"), silc_message_get_mac(message),
                   silc_hmac_len(hmac));
-  SILC_LOG_DEBUG(("Get signature"));
-  sig = silc_message_get_signature(message);
-  if (!sig)
-    goto err;
   SILC_LOG_DEBUG(("Verifying signature"));
-  if (silc_message_signed_verify(sig, message, public_key, hash) !=
+  if (silc_message_signed_verify(message, public_key, hash) !=
       SILC_AUTH_OK)
     goto err;
   SILC_LOG_DEBUG(("Signature Ok"));
   SILC_LOG_DEBUG(("Get public key"));
-  pk2 = silc_message_signed_get_public_key(sig, NULL, NULL);
+  pk2 = silc_message_signed_get_public_key(message, NULL, NULL);
   if (!pk2)
     goto err;
   SILC_LOG_DEBUG(("Verify public key"));
@@ -330,7 +321,7 @@ int main(int argc, char **argv)
   SILC_LOG_DEBUG(("Parsing private messsage (static key)"));
   message = silc_message_payload_parse(silc_buffer_data(buf),
                                       silc_buffer_len(buf), TRUE, FALSE,
-                                      NULL, NULL);
+                                      NULL, NULL, NULL, FALSE, NULL);
   if (!message)
     goto err;
   flags = silc_message_get_flags(message);
@@ -347,17 +338,70 @@ int main(int argc, char **argv)
   SILC_LOG_HEXDUMP(("Data"), data, data_len);
   if (data_len != sizeof(tmp) / 2 || memcmp(data, tmp, sizeof(tmp) / 2))
     goto err;
-  SILC_LOG_DEBUG(("Get signature"));
-  sig = silc_message_get_signature(message);
-  if (!sig)
+  SILC_LOG_DEBUG(("Verifying signature"));
+  if (silc_message_signed_verify(message, public_key, hash) !=
+      SILC_AUTH_OK)
+    goto err;
+  SILC_LOG_DEBUG(("Signature Ok"));
+  SILC_LOG_DEBUG(("Get public key"));
+  pk2 = silc_message_signed_get_public_key(message, NULL, NULL);
+  if (!pk2)
+    goto err;
+  SILC_LOG_DEBUG(("Verify public key"));
+  if (!silc_pkcs_public_key_compare(public_key, pk2))
+    goto err;
+  SILC_LOG_DEBUG(("Public key Ok"));
+  silc_pkcs_public_key_free(pk2);
+  silc_message_payload_free(message);
+
+  /* Digitally signed channel message (LARGE) */
+  n = 65550;
+  tmp2 = silc_malloc(n);
+  if (!tmp2)
+    goto err;
+  SILC_LOG_DEBUG(("Encoding channel message len %d (static key) SIGNED LARGE",
+                 n));
+  buf = silc_message_payload_encode(SILC_MESSAGE_FLAG_ACTION |
+                                   SILC_MESSAGE_FLAG_UTF8 |
+                                   SILC_MESSAGE_FLAG_ACK |
+                                   SILC_MESSAGE_FLAG_SIGNED,
+                                   tmp2, n, TRUE, FALSE,
+                                   key, hmac, rng,
+                                   public_key, private_key, hash, buf);
+  if (!buf)
+    goto err;
+  SILC_LOG_DEBUG(("Message length: %d", silc_buffer_len(buf)));
+  if (silc_buffer_len(buf) > SILC_PACKET_MAX_LEN)
+    goto err;
+  SILC_LOG_DEBUG(("Parsing channel messsage (static key)"));
+  message = silc_message_payload_parse(silc_buffer_data(buf),
+                                      silc_buffer_len(buf), FALSE, TRUE,
+                                      key, hmac, NULL, FALSE, NULL);
+  if (!message)
+    goto err;
+  flags = silc_message_get_flags(message);
+  SILC_LOG_DEBUG(("Flags: %x", flags));
+  if (!(flags & SILC_MESSAGE_FLAG_ACTION))
+    goto err;
+  if (!(flags & SILC_MESSAGE_FLAG_UTF8))
+    goto err;
+  if (!(flags & SILC_MESSAGE_FLAG_ACK))
     goto err;
+  if (!(flags & SILC_MESSAGE_FLAG_SIGNED))
+    goto err;
+  data = silc_message_get_data(message, &data_len);
+  SILC_LOG_DEBUG(("Data len: %d", data_len));
+  if (silc_buffer_len(buf) > SILC_PACKET_MAX_LEN)
+    goto err;
+  SILC_LOG_HEXDUMP(("MAC"), silc_message_get_mac(message),
+                  silc_hmac_len(hmac));
   SILC_LOG_DEBUG(("Verifying signature"));
-  if (silc_message_signed_verify(sig, message, public_key, hash) !=
+  if (silc_message_signed_verify(message, public_key, hash) !=
       SILC_AUTH_OK)
     goto err;
   SILC_LOG_DEBUG(("Signature Ok"));
   SILC_LOG_DEBUG(("Get public key"));
-  pk2 = silc_message_signed_get_public_key(sig, NULL, NULL);
+  pk2 = silc_message_signed_get_public_key(message, NULL, NULL);
   if (!pk2)
     goto err;
   SILC_LOG_DEBUG(("Verify public key"));
@@ -366,6 +410,7 @@ int main(int argc, char **argv)
   SILC_LOG_DEBUG(("Public key Ok"));
   silc_pkcs_public_key_free(pk2);
   silc_message_payload_free(message);
+  silc_free(tmp2);
 
 
   success = TRUE;