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.
27 #include <loc/libloc.h>
28 #include <loc/database.h>
29 #include <loc/writer.h>
31 int main(int argc
, char** argv
) {
35 FILE* public_key
= fopen(ABS_SRCDIR
"/examples/public-key.pem", "r");
37 fprintf(stderr
, "Could not open public key file: %s\n", strerror(errno
));
42 FILE* private_key1
= fopen(ABS_SRCDIR
"/examples/private-key.pem", "r");
44 fprintf(stderr
, "Could not open private key file: %s\n", strerror(errno
));
48 FILE* private_key2
= fopen(ABS_SRCDIR
"/examples/private-key.pem", "r");
50 fprintf(stderr
, "Could not open private key file: %s\n", strerror(errno
));
59 // Enable debug logging
60 loc_set_log_priority(ctx
, LOG_DEBUG
);
62 // Create an empty database
63 struct loc_writer
* writer
;
64 err
= loc_writer_new(ctx
, &writer
, private_key1
, private_key2
);
70 fprintf(stderr
, "Could not open file for writing: %s\n", strerror(errno
));
74 err
= loc_writer_write(writer
, f
, LOC_DATABASE_VERSION_UNSET
);
76 fprintf(stderr
, "Could not write database: %s\n", strerror(err
));
79 loc_writer_unref(writer
);
81 // And open it again from disk
82 struct loc_database
* db
;
83 err
= loc_database_new(ctx
, &db
, f
);
85 fprintf(stderr
, "Could not open database: %s\n", strerror(-err
));
89 // Verify the database signature
90 err
= loc_database_verify(db
, public_key
);
92 fprintf(stderr
, "Could not verify the database: %d\n", err
);
96 // Open another public key
97 public_key
= freopen(ABS_SRCDIR
"/src/signing-key.pem", "r", public_key
);
99 fprintf(stderr
, "Could not open public key file: %s\n", strerror(errno
));
103 // Verify with an incorrect key
104 err
= loc_database_verify(db
, public_key
);
106 fprintf(stderr
, "Database was verified with an incorrect key: %d\n", err
);
110 // Close the database
111 loc_database_unref(db
);
115 fclose(private_key1
);
116 fclose(private_key2
);