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