]> git.ipfire.org Git - location/debian/libloc.git/blame - src/test-signature.c
Update upstream source from tag 'upstream/0.9.16'
[location/debian/libloc.git] / src / test-signature.c
CommitLineData
1f2c3ccb
JS
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#include <syslog.h>
26
27#include <libloc/libloc.h>
28#include <libloc/database.h>
29#include <libloc/writer.h>
30
31int main(int argc, char** argv) {
32 int err;
33
34 // Open public key
35 FILE* public_key = fopen(ABS_SRCDIR "/examples/public-key.pem", "r");
36 if (!public_key) {
aa9346d8 37 fprintf(stderr, "Could not open public key file: %m\n");
1f2c3ccb
JS
38 exit(EXIT_FAILURE);
39 }
40
41 // Open private key
42 FILE* private_key1 = fopen(ABS_SRCDIR "/examples/private-key.pem", "r");
43 if (!private_key1) {
aa9346d8 44 fprintf(stderr, "Could not open private key file: %m\n");
1f2c3ccb
JS
45 exit(EXIT_FAILURE);
46 }
47
48 FILE* private_key2 = fopen(ABS_SRCDIR "/examples/private-key.pem", "r");
49 if (!private_key2) {
aa9346d8 50 fprintf(stderr, "Could not open private key file: %m\n");
1f2c3ccb
JS
51 exit(EXIT_FAILURE);
52 }
53
54 struct loc_ctx* ctx;
55 err = loc_new(&ctx);
56 if (err < 0)
57 exit(EXIT_FAILURE);
58
59 // Enable debug logging
60 loc_set_log_priority(ctx, LOG_DEBUG);
61
62 // Create an empty database
63 struct loc_writer* writer;
64 err = loc_writer_new(ctx, &writer, private_key1, private_key2);
65 if (err < 0)
66 exit(EXIT_FAILURE);
67
68 FILE* f = tmpfile();
69 if (!f) {
aa9346d8 70 fprintf(stderr, "Could not open file for writing: %m\n");
1f2c3ccb
JS
71 exit(EXIT_FAILURE);
72 }
73
74 err = loc_writer_write(writer, f, LOC_DATABASE_VERSION_UNSET);
75 if (err) {
aa9346d8 76 fprintf(stderr, "Could not write database: %m\n");
1f2c3ccb
JS
77 exit(EXIT_FAILURE);
78 }
79 loc_writer_unref(writer);
80
81 // And open it again from disk
82 struct loc_database* db;
83 err = loc_database_new(ctx, &db, f);
84 if (err) {
aa9346d8 85 fprintf(stderr, "Could not open database: %m\n");
1f2c3ccb
JS
86 exit(EXIT_FAILURE);
87 }
88
89 // Verify the database signature
90 err = loc_database_verify(db, public_key);
91 if (err) {
92 fprintf(stderr, "Could not verify the database: %d\n", err);
93 exit(EXIT_FAILURE);
94 }
95
96 // Open another public key
b1863b64 97 public_key = freopen(ABS_SRCDIR "/data/signing-key.pem", "r", public_key);
1f2c3ccb 98 if (!public_key) {
aa9346d8 99 fprintf(stderr, "Could not open public key file: %m\n");
1f2c3ccb
JS
100 exit(EXIT_FAILURE);
101 }
102
103 // Verify with an incorrect key
104 err = loc_database_verify(db, public_key);
105 if (err == 0) {
106 fprintf(stderr, "Database was verified with an incorrect key: %d\n", err);
107 exit(EXIT_FAILURE);
108 }
109
110 // Close the database
111 loc_database_unref(db);
112 loc_unref(ctx);
113 fclose(f);
114
115 fclose(private_key1);
116 fclose(private_key2);
117 fclose(public_key);
118
119 return EXIT_SUCCESS;
120}