]> git.meshlink.io Git - meshlink/blob - test/sign-verify.c
028d8021cc76683d593c992853bc3ed6a6c6ea31
[meshlink] / test / sign-verify.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 #include "meshlink.h"
7
8 int main(int argc, char *argv[]) {
9         // Open two new meshlink instance.
10
11         meshlink_handle_t *mesh1 = meshlink_open("sign_verify_conf.1", "foo", "sign-verify", DEV_CLASS_BACKBONE);
12         if(!mesh1) {
13                 fprintf(stderr, "Could not initialize configuration for foo\n");
14                 return 1;
15         }
16
17         meshlink_handle_t *mesh2 = meshlink_open("sign_verify_conf.2", "bar", "sign-verify", DEV_CLASS_BACKBONE);
18         if(!mesh2) {
19                 fprintf(stderr, "Could not initialize configuration for bar\n");
20                 return 1;
21         }
22
23         // Import and export both side's data
24
25         meshlink_add_address(mesh1, "localhost");
26         meshlink_add_address(mesh2, "localhost");
27
28         char *data = meshlink_export(mesh1);
29
30         if(!meshlink_import(mesh2, data)) {
31                 fprintf(stderr, "Bar could not import data from foo\n");
32                 return 1;
33         }
34
35         free(data);
36
37         data = meshlink_export(mesh2);
38
39         if(!meshlink_import(mesh1, data)) {
40                 fprintf(stderr, "Foo could not import data from bar\n");
41                 return 1;
42         }
43
44         free(data);
45
46         // Verify that a signature made on one node can be verified by its peer.
47
48         static const char testdata1[] = "Test data 1.";
49         static const char testdata2[] = "Test data 2.";
50
51         char sig[MESHLINK_SIGLEN * 2];
52         size_t siglen = sizeof sig * 2;
53
54         if(!meshlink_sign(mesh1, testdata1, sizeof testdata1, sig, &siglen)) {
55                 fprintf(stderr, "Signing failed\n");
56                 return 1;
57         }
58         if(siglen != MESHLINK_SIGLEN) {
59                 fprintf(stderr, "Signature has unexpected length %zu != %zu\n", siglen, MESHLINK_SIGLEN);
60                 return 1;
61         }
62
63         meshlink_node_t *foo = meshlink_get_node(mesh2, "foo");
64         if(!foo) {
65                 fprintf(stderr, "Bar did not know about node foo\n");
66                 return 1;
67         }
68
69         meshlink_node_t *bar = meshlink_get_node(mesh2, "bar");
70         if(!bar) {
71                 fprintf(stderr, "Bar did not know about node bar\n");
72                 return 1;
73         }
74
75         if(!meshlink_verify(mesh2, foo, testdata1, sizeof testdata1, sig, siglen)) {
76                 fprintf(stderr, "False negative verification\n");
77                 return 1;
78         }
79
80         // Check that bad signatures are revoked
81
82         if(meshlink_verify(mesh2, foo, testdata1, sizeof testdata1, sig, siglen / 2)) {
83                 fprintf(stderr, "False positive verification with half sized signature\n");
84                 return 1;
85         }
86         if(meshlink_verify(mesh2, foo, testdata1, sizeof testdata1, sig, siglen * 2)) {
87                 fprintf(stderr, "False positive verification with double sized signature\n");
88                 return 1;
89         }
90         if(meshlink_verify(mesh2, foo, testdata2, sizeof testdata2, sig, siglen)) {
91                 fprintf(stderr, "False positive verification with wrong data\n");
92                 return 1;
93         }
94         if(meshlink_verify(mesh2, bar, testdata1, sizeof testdata1, sig, siglen)) {
95                 fprintf(stderr, "False positive verification with wrong signer\n");
96                 return 1;
97         }
98
99         // Clean up.
100
101         meshlink_close(mesh2);
102         meshlink_close(mesh1);
103
104         return 0;
105 }