]> git.ipfire.org Git - people/ms/libloc.git/blame - src/test-signature.c
importer: Drop EDROP as it has been merged into DROP
[people/ms/libloc.git] / src / test-signature.c
CommitLineData
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>
37317e4f 25#include <syslog.h>
e8fd8389 26
c12a1385
MT
27#include <libloc/libloc.h>
28#include <libloc/database.h>
29#include <libloc/writer.h>
e8fd8389
MT
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) {
6cd02f1d 37 fprintf(stderr, "Could not open public key file: %m\n");
e8fd8389
MT
38 exit(EXIT_FAILURE);
39 }
40
41 // Open private key
67c99e7e
MT
42 FILE* private_key1 = fopen(ABS_SRCDIR "/examples/private-key.pem", "r");
43 if (!private_key1) {
6cd02f1d 44 fprintf(stderr, "Could not open private key file: %m\n");
67c99e7e
MT
45 exit(EXIT_FAILURE);
46 }
47
48 FILE* private_key2 = fopen(ABS_SRCDIR "/examples/private-key.pem", "r");
49 if (!private_key2) {
6cd02f1d 50 fprintf(stderr, "Could not open private key file: %m\n");
e8fd8389
MT
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
37317e4f
MT
59 // Enable debug logging
60 loc_set_log_priority(ctx, LOG_DEBUG);
61
e8fd8389
MT
62 // Create an empty database
63 struct loc_writer* writer;
67c99e7e 64 err = loc_writer_new(ctx, &writer, private_key1, private_key2);
e8fd8389
MT
65 if (err < 0)
66 exit(EXIT_FAILURE);
67
6254dca6 68 FILE* f = tmpfile();
e8fd8389 69 if (!f) {
6cd02f1d 70 fprintf(stderr, "Could not open file for writing: %m\n");
e8fd8389
MT
71 exit(EXIT_FAILURE);
72 }
73
22c7b98b 74 err = loc_writer_write(writer, f, LOC_DATABASE_VERSION_UNSET);
e8fd8389 75 if (err) {
198e382c 76 fprintf(stderr, "Could not write database: %m\n");
e8fd8389
MT
77 exit(EXIT_FAILURE);
78 }
79 loc_writer_unref(writer);
80
e8fd8389 81 // And open it again from disk
e8fd8389
MT
82 struct loc_database* db;
83 err = loc_database_new(ctx, &db, f);
84 if (err) {
198e382c 85 fprintf(stderr, "Could not open database: %m\n");
e8fd8389
MT
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
9b3db7b1 97 public_key = freopen(ABS_SRCDIR "/data/signing-key.pem", "r", public_key);
e8fd8389 98 if (!public_key) {
6cd02f1d 99 fprintf(stderr, "Could not open public key file: %m\n");
e8fd8389
MT
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);
e8fd8389 112 loc_unref(ctx);
6254dca6 113 fclose(f);
e8fd8389 114
67c99e7e
MT
115 fclose(private_key1);
116 fclose(private_key2);
e8fd8389
MT
117 fclose(public_key);
118
119 return EXIT_SUCCESS;
120}