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
);
64 FILE* f
= fopen("test.db", "w+");
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
);
80 // And open it again from disk
81 f
= fopen("test.db", "r");
83 fprintf(stderr
, "Could not open file for reading: %s\n", strerror(errno
));
87 struct loc_database
* db
;
88 err
= loc_database_new(ctx
, &db
, f
);
90 fprintf(stderr
, "Could not open database: %s\n", strerror(-err
));
94 // Verify the database signature
95 err
= loc_database_verify(db
, public_key
);
97 fprintf(stderr
, "Could not verify the database: %d\n", err
);
101 // Open another public key
102 public_key
= freopen(ABS_SRCDIR
"/src/signing-key.pem", "r", public_key
);
104 fprintf(stderr
, "Could not open public key file: %s\n", strerror(errno
));
108 // Verify with an incorrect key
109 err
= loc_database_verify(db
, public_key
);
111 fprintf(stderr
, "Database was verified with an incorrect key: %d\n", err
);
115 // Close the database
116 loc_database_unref(db
);
120 fclose(private_key1
);
121 fclose(private_key2
);