2 libloc - A library to determine the location of someone on the Internet
4 Copyright (C) 2019 IPFire Development Team <info@ipfire.org>
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.
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.
26 #include <loc/libloc.h>
27 #include <loc/database.h>
28 #include <loc/writer.h>
30 int main(int argc
, char** argv
) {
34 FILE* public_key
= fopen(ABS_SRCDIR
"/examples/public-key.pem", "r");
36 fprintf(stderr
, "Could not open public key file: %s\n", strerror(errno
));
41 FILE* private_key1
= fopen(ABS_SRCDIR
"/examples/private-key.pem", "r");
43 fprintf(stderr
, "Could not open private key file: %s\n", strerror(errno
));
47 FILE* private_key2
= fopen(ABS_SRCDIR
"/examples/private-key.pem", "r");
49 fprintf(stderr
, "Could not open private key file: %s\n", strerror(errno
));
58 // Create an empty database
59 struct loc_writer
* writer
;
60 err
= loc_writer_new(ctx
, &writer
, private_key1
, private_key2
);
66 fprintf(stderr
, "Could not open file for writing: %s\n", strerror(errno
));
70 err
= loc_writer_write(writer
, f
, LOC_DATABASE_VERSION_UNSET
);
72 fprintf(stderr
, "Could not write database: %s\n", strerror(err
));
75 loc_writer_unref(writer
);
77 // And open it again from disk
78 struct loc_database
* db
;
79 err
= loc_database_new(ctx
, &db
, f
);
81 fprintf(stderr
, "Could not open database: %s\n", strerror(-err
));
85 // Verify the database signature
86 err
= loc_database_verify(db
, public_key
);
88 fprintf(stderr
, "Could not verify the database: %d\n", err
);
92 // Open another public key
93 public_key
= freopen(ABS_SRCDIR
"/src/signing-key.pem", "r", public_key
);
95 fprintf(stderr
, "Could not open public key file: %s\n", strerror(errno
));
99 // Verify with an incorrect key
100 err
= loc_database_verify(db
, public_key
);
102 fprintf(stderr
, "Database was verified with an incorrect key: %d\n", err
);
106 // Close the database
107 loc_database_unref(db
);
111 fclose(private_key1
);
112 fclose(private_key2
);