]>
Commit | Line | Data |
---|---|---|
e8fd8389 MT |
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 | |
67c99e7e MT |
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) { | |
e8fd8389 MT |
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; | |
67c99e7e | 60 | err = loc_writer_new(ctx, &writer, private_key1, private_key2); |
e8fd8389 MT |
61 | if (err < 0) |
62 | exit(EXIT_FAILURE); | |
63 | ||
6254dca6 | 64 | FILE* f = tmpfile(); |
e8fd8389 MT |
65 | if (!f) { |
66 | fprintf(stderr, "Could not open file for writing: %s\n", strerror(errno)); | |
67 | exit(EXIT_FAILURE); | |
68 | } | |
69 | ||
22c7b98b | 70 | err = loc_writer_write(writer, f, LOC_DATABASE_VERSION_UNSET); |
e8fd8389 MT |
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 | ||
e8fd8389 | 77 | // And open it again from disk |
e8fd8389 MT |
78 | struct loc_database* db; |
79 | err = loc_database_new(ctx, &db, f); | |
80 | if (err) { | |
81 | fprintf(stderr, "Could not open database: %s\n", strerror(-err)); | |
82 | exit(EXIT_FAILURE); | |
83 | } | |
84 | ||
85 | // Verify the database signature | |
86 | err = loc_database_verify(db, public_key); | |
87 | if (err) { | |
88 | fprintf(stderr, "Could not verify the database: %d\n", err); | |
89 | exit(EXIT_FAILURE); | |
90 | } | |
91 | ||
92 | // Open another public key | |
0600eac2 | 93 | public_key = freopen(ABS_SRCDIR "/src/signing-key.pem", "r", public_key); |
e8fd8389 MT |
94 | if (!public_key) { |
95 | fprintf(stderr, "Could not open public key file: %s\n", strerror(errno)); | |
96 | exit(EXIT_FAILURE); | |
97 | } | |
98 | ||
99 | // Verify with an incorrect key | |
100 | err = loc_database_verify(db, public_key); | |
101 | if (err == 0) { | |
102 | fprintf(stderr, "Database was verified with an incorrect key: %d\n", err); | |
103 | exit(EXIT_FAILURE); | |
104 | } | |
105 | ||
106 | // Close the database | |
107 | loc_database_unref(db); | |
e8fd8389 | 108 | loc_unref(ctx); |
6254dca6 | 109 | fclose(f); |
e8fd8389 | 110 | |
67c99e7e MT |
111 | fclose(private_key1); |
112 | fclose(private_key2); | |
e8fd8389 MT |
113 | fclose(public_key); |
114 | ||
115 | return EXIT_SUCCESS; | |
116 | } |