Added SILC Thread Queue API
[crypto.git] / lib / silccore / tests / test_silcmessage.c
index e6feb3aedf2f00def644432cdfc32466f342aaa0..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;
-  SilcMessageSignedPayload sig;
+  int i, n;
 
   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);
@@ -98,11 +97,12 @@ int main(int argc, char **argv)
   silc_message_payload_free(message);
 
   /* Simple private message */
-  SILC_LOG_DEBUG(("Encoding private message len %d (static key)", 10));
+  n = 10;
+  SILC_LOG_DEBUG(("Encoding private message len %d (static key)", n));
   buf = silc_message_payload_encode(SILC_MESSAGE_FLAG_ACTION |
                                    SILC_MESSAGE_FLAG_UTF8 |
                                    SILC_MESSAGE_FLAG_ACK,
-                                   msg, 10, TRUE, TRUE,
+                                   msg, n, TRUE, TRUE,
                                    key, hmac, rng, NULL, NULL, NULL, buf);
   if (!buf)
     goto err;
@@ -110,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);
@@ -123,18 +123,19 @@ int main(int argc, char **argv)
     goto err;
   data = silc_message_get_data(message, &data_len);
   SILC_LOG_HEXDUMP(("Data"), data, data_len);
-  if (data_len != 10 || memcmp(data, msg, 10))
+  if (data_len != n || memcmp(data, msg, n))
     goto err;
   SILC_LOG_HEXDUMP(("MAC"), silc_message_get_mac(message),
                   silc_hmac_len(hmac));
   silc_message_payload_free(message);
 
   /* Simple private message */
-  SILC_LOG_DEBUG(("Encoding private message len %d (static key)", 1));
+  n = 1;
+  SILC_LOG_DEBUG(("Encoding private message len %d (static key)", n));
   buf = silc_message_payload_encode(SILC_MESSAGE_FLAG_ACTION |
                                    SILC_MESSAGE_FLAG_UTF8 |
                                    SILC_MESSAGE_FLAG_ACK,
-                                   msg, 1, TRUE, TRUE,
+                                   msg, n, TRUE, TRUE,
                                    key, hmac, rng, NULL, NULL, NULL, buf);
   if (!buf)
     goto err;
@@ -142,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);
@@ -155,7 +156,7 @@ int main(int argc, char **argv)
     goto err;
   data = silc_message_get_data(message, &data_len);
   SILC_LOG_HEXDUMP(("Data"), data, data_len);
-  if (data_len != 1 || memcmp(data, msg, 1))
+  if (data_len != n || memcmp(data, msg, n))
     goto err;
   SILC_LOG_HEXDUMP(("MAC"), silc_message_get_mac(message),
                   silc_hmac_len(hmac));
@@ -177,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);
@@ -214,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);
@@ -233,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"));
@@ -271,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);
@@ -290,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"));
@@ -328,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);
@@ -345,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"));
@@ -364,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;