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_key
= fopen(ABS_SRCDIR
"/examples/private-key.pem", "r");
43 fprintf(stderr
, "Could not open private key file: %s\n", strerror(errno
));
52 // Create an empty database
53 struct loc_writer
* writer
;
54 err
= loc_writer_new(ctx
, &writer
, private_key
);
58 FILE* f
= fopen("test.db", "w+");
60 fprintf(stderr
, "Could not open file for writing: %s\n", strerror(errno
));
64 err
= loc_writer_write(writer
, f
, LOC_DATABASE_VERSION_UNSET
);
66 fprintf(stderr
, "Could not write database: %s\n", strerror(err
));
69 loc_writer_unref(writer
);
74 // And open it again from disk
75 f
= fopen("test.db", "r");
77 fprintf(stderr
, "Could not open file for reading: %s\n", strerror(errno
));
81 struct loc_database
* db
;
82 err
= loc_database_new(ctx
, &db
, f
);
84 fprintf(stderr
, "Could not open database: %s\n", strerror(-err
));
88 // Verify the database signature
89 err
= loc_database_verify(db
, public_key
);
91 fprintf(stderr
, "Could not verify the database: %d\n", err
);
95 // Open another public key
96 public_key
= freopen(ABS_SRCDIR
"/src/signing-key.pem", "r", public_key
);
98 fprintf(stderr
, "Could not open public key file: %s\n", strerror(errno
));
102 // Verify with an incorrect key
103 err
= loc_database_verify(db
, public_key
);
105 fprintf(stderr
, "Database was verified with an incorrect key: %d\n", err
);
109 // Close the database
110 loc_database_unref(db
);