]> git.ipfire.org Git - location/libloc.git/blob - src/test-signature.c
daebef2d5123e02a8423b8d895c6689fbd4c85b2
[location/libloc.git] / src / test-signature.c
1 /*
2 libloc - A library to determine the location of someone on the Internet
3
4 Copyright (C) 2019 IPFire Development Team <info@ipfire.org>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15 */
16
17 #include <stdio.h>
18 #include <stddef.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <fcntl.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #include <unistd.h>
25
26 #include <loc/libloc.h>
27 #include <loc/database.h>
28 #include <loc/writer.h>
29
30 int main(int argc, char** argv) {
31 int err;
32
33 // Open public key
34 FILE* public_key = fopen(ABS_SRCDIR "/examples/public-key.pem", "r");
35 if (!public_key) {
36 fprintf(stderr, "Could not open public key file: %s\n", strerror(errno));
37 exit(EXIT_FAILURE);
38 }
39
40 // Open private key
41 FILE* private_key1 = fopen(ABS_SRCDIR "/examples/private-key.pem", "r");
42 if (!private_key1) {
43 fprintf(stderr, "Could not open private key file: %s\n", strerror(errno));
44 exit(EXIT_FAILURE);
45 }
46
47 FILE* private_key2 = fopen(ABS_SRCDIR "/examples/private-key.pem", "r");
48 if (!private_key2) {
49 fprintf(stderr, "Could not open private key file: %s\n", strerror(errno));
50 exit(EXIT_FAILURE);
51 }
52
53 struct loc_ctx* ctx;
54 err = loc_new(&ctx);
55 if (err < 0)
56 exit(EXIT_FAILURE);
57
58 // Create an empty database
59 struct loc_writer* writer;
60 err = loc_writer_new(ctx, &writer, private_key1, private_key2);
61 if (err < 0)
62 exit(EXIT_FAILURE);
63
64 FILE* f = fopen("test.db", "w+");
65 if (!f) {
66 fprintf(stderr, "Could not open file for writing: %s\n", strerror(errno));
67 exit(EXIT_FAILURE);
68 }
69
70 err = loc_writer_write(writer, f, LOC_DATABASE_VERSION_UNSET);
71 if (err) {
72 fprintf(stderr, "Could not write database: %s\n", strerror(err));
73 exit(EXIT_FAILURE);
74 }
75 loc_writer_unref(writer);
76
77 // Close the file
78 fclose(f);
79
80 // And open it again from disk
81 f = fopen("test.db", "r");
82 if (!f) {
83 fprintf(stderr, "Could not open file for reading: %s\n", strerror(errno));
84 exit(EXIT_FAILURE);
85 }
86
87 struct loc_database* db;
88 err = loc_database_new(ctx, &db, f);
89 if (err) {
90 fprintf(stderr, "Could not open database: %s\n", strerror(-err));
91 exit(EXIT_FAILURE);
92 }
93
94 // Verify the database signature
95 err = loc_database_verify(db, public_key);
96 if (err) {
97 fprintf(stderr, "Could not verify the database: %d\n", err);
98 exit(EXIT_FAILURE);
99 }
100
101 // Open another public key
102 public_key = freopen(ABS_SRCDIR "/src/signing-key.pem", "r", public_key);
103 if (!public_key) {
104 fprintf(stderr, "Could not open public key file: %s\n", strerror(errno));
105 exit(EXIT_FAILURE);
106 }
107
108 // Verify with an incorrect key
109 err = loc_database_verify(db, public_key);
110 if (err == 0) {
111 fprintf(stderr, "Database was verified with an incorrect key: %d\n", err);
112 exit(EXIT_FAILURE);
113 }
114
115 // Close the database
116 loc_database_unref(db);
117
118 loc_unref(ctx);
119
120 fclose(private_key1);
121 fclose(private_key2);
122 fclose(public_key);
123
124 return EXIT_SUCCESS;
125 }