X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=test%2Fsign-verify.c;fp=test%2Fsign-verify.c;h=a7f9a5af5b7e22bf55d9f085eac3a4cb1b4a3a0b;hb=82a9712857c5b640288174493905499093853997;hp=0000000000000000000000000000000000000000;hpb=d1f3e4416636bd46665753f5e1acfb0f6f007871;p=meshlink diff --git a/test/sign-verify.c b/test/sign-verify.c new file mode 100644 index 00000000..a7f9a5af --- /dev/null +++ b/test/sign-verify.c @@ -0,0 +1,105 @@ +#include +#include +#include +#include + +#include "meshlink.h" + +int main(int argc, char *argv[]) { + // Open two new meshlink instance. + + meshlink_handle_t *mesh1 = meshlink_open("sign_verify_conf.1", "foo"); + if(!mesh1) { + fprintf(stderr, "Could not initialize configuration for foo\n"); + return 1; + } + + meshlink_handle_t *mesh2 = meshlink_open("sign_verify_conf.2", "bar"); + if(!mesh2) { + fprintf(stderr, "Could not initialize configuration for bar\n"); + return 1; + } + + // Import and export both side's data + + meshlink_add_address(mesh1, "localhost"); + meshlink_add_address(mesh2, "localhost"); + + char *data = meshlink_export(mesh1); + + if(!meshlink_import(mesh2, data)) { + fprintf(stderr, "Bar could not import data from foo\n"); + return 1; + } + + free(data); + + data = meshlink_export(mesh2); + + if(!meshlink_import(mesh1, data)) { + fprintf(stderr, "Foo could not import data from bar\n"); + return 1; + } + + free(data); + + // Verify that a signature made on one node can be verified by its peer. + + static const char testdata1[] = "Test data 1."; + static const char testdata2[] = "Test data 2."; + + char sig[MESHLINK_SIGLEN * 2]; + size_t siglen = sizeof sig * 2; + + if(!meshlink_sign(mesh1, testdata1, sizeof testdata1, sig, &siglen)) { + fprintf(stderr, "Signing failed\n"); + return 1; + } + if(siglen != MESHLINK_SIGLEN) { + fprintf(stderr, "Signature has unexpected length %zu != %zu\n", siglen, MESHLINK_SIGLEN); + return 1; + } + + meshlink_node_t *foo = meshlink_get_node(mesh2, "foo"); + if(!foo) { + fprintf(stderr, "Bar did not know about node foo\n"); + return 1; + } + + meshlink_node_t *bar = meshlink_get_node(mesh2, "bar"); + if(!bar) { + fprintf(stderr, "Bar did not know about node bar\n"); + return 1; + } + + if(!meshlink_verify(mesh2, foo, testdata1, sizeof testdata1, sig, siglen)) { + fprintf(stderr, "False negative verification\n"); + return 1; + } + + // Check that bad signatures are revoked + + if(meshlink_verify(mesh2, foo, testdata1, sizeof testdata1, sig, siglen / 2)) { + fprintf(stderr, "False positive verification with half sized signature\n"); + return 1; + } + if(meshlink_verify(mesh2, foo, testdata1, sizeof testdata1, sig, siglen * 2)) { + fprintf(stderr, "False positive verification with double sized signature\n"); + return 1; + } + if(meshlink_verify(mesh2, foo, testdata2, sizeof testdata2, sig, siglen)) { + fprintf(stderr, "False positive verification with wrong data\n"); + return 1; + } + if(meshlink_verify(mesh2, bar, testdata1, sizeof testdata1, sig, siglen)) { + fprintf(stderr, "False positive verification with wrong signer\n"); + return 1; + } + + // Clean up. + + meshlink_close(mesh2); + meshlink_close(mesh1); + + return 0; +}