8 int main(int argc, char *argv[]) {
9 // Open two new meshlink instance.
11 meshlink_handle_t *mesh1 = meshlink_open("sign_verify_conf.1", "foo", "sign-verify");
13 fprintf(stderr, "Could not initialize configuration for foo\n");
17 meshlink_handle_t *mesh2 = meshlink_open("sign_verify_conf.2", "bar", "sign-verify");
19 fprintf(stderr, "Could not initialize configuration for bar\n");
23 // Import and export both side's data
25 meshlink_add_address(mesh1, "localhost");
26 meshlink_add_address(mesh2, "localhost");
28 char *data = meshlink_export(mesh1);
30 if(!meshlink_import(mesh2, data)) {
31 fprintf(stderr, "Bar could not import data from foo\n");
37 data = meshlink_export(mesh2);
39 if(!meshlink_import(mesh1, data)) {
40 fprintf(stderr, "Foo could not import data from bar\n");
46 // Verify that a signature made on one node can be verified by its peer.
48 static const char testdata1[] = "Test data 1.";
49 static const char testdata2[] = "Test data 2.";
51 char sig[MESHLINK_SIGLEN * 2];
52 size_t siglen = sizeof sig * 2;
54 if(!meshlink_sign(mesh1, testdata1, sizeof testdata1, sig, &siglen)) {
55 fprintf(stderr, "Signing failed\n");
58 if(siglen != MESHLINK_SIGLEN) {
59 fprintf(stderr, "Signature has unexpected length %zu != %zu\n", siglen, MESHLINK_SIGLEN);
63 meshlink_node_t *foo = meshlink_get_node(mesh2, "foo");
65 fprintf(stderr, "Bar did not know about node foo\n");
69 meshlink_node_t *bar = meshlink_get_node(mesh2, "bar");
71 fprintf(stderr, "Bar did not know about node bar\n");
75 if(!meshlink_verify(mesh2, foo, testdata1, sizeof testdata1, sig, siglen)) {
76 fprintf(stderr, "False negative verification\n");
80 // Check that bad signatures are revoked
82 if(meshlink_verify(mesh2, foo, testdata1, sizeof testdata1, sig, siglen / 2)) {
83 fprintf(stderr, "False positive verification with half sized signature\n");
86 if(meshlink_verify(mesh2, foo, testdata1, sizeof testdata1, sig, siglen * 2)) {
87 fprintf(stderr, "False positive verification with double sized signature\n");
90 if(meshlink_verify(mesh2, foo, testdata2, sizeof testdata2, sig, siglen)) {
91 fprintf(stderr, "False positive verification with wrong data\n");
94 if(meshlink_verify(mesh2, bar, testdata1, sizeof testdata1, sig, siglen)) {
95 fprintf(stderr, "False positive verification with wrong signer\n");
101 meshlink_close(mesh2);
102 meshlink_close(mesh1);