Lesser General Public License for more details.
*/
-#include <endian.h>
+#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/queue.h>
#include <time.h>
+#ifdef HAVE_ENDIAN_H
+# include <endian.h>
+#endif
+
+#include <openssl/bio.h>
+#include <openssl/err.h>
+#include <openssl/evp.h>
+#include <openssl/pem.h>
+
#include <loc/libloc.h>
#include <loc/as.h>
+#include <loc/compat.h>
#include <loc/country.h>
+#include <loc/database.h>
#include <loc/format.h>
#include <loc/network.h>
#include <loc/private.h>
off_t description;
off_t license;
+ // Private key to sign any databases
+ EVP_PKEY* private_key;
+
struct loc_as** as;
size_t as_count;
struct loc_network_tree* networks;
};
-LOC_EXPORT int loc_writer_new(struct loc_ctx* ctx, struct loc_writer** writer) {
+static int parse_private_key(struct loc_writer* writer, FILE* f) {
+ // Free any previously loaded keys
+ if (writer->private_key)
+ EVP_PKEY_free(writer->private_key);
+
+ // Read the key
+ writer->private_key = PEM_read_PrivateKey(f, NULL, NULL, NULL);
+
+ // Log any errors
+ if (!writer->private_key) {
+ char* error = ERR_error_string(ERR_get_error(), NULL);
+ ERROR(writer->ctx, "Could not parse private key: %s\n", error);
+
+ return -1;
+ }
+
+ return 0;
+}
+
+LOC_EXPORT int loc_writer_new(struct loc_ctx* ctx, struct loc_writer** writer, FILE* fkey) {
struct loc_writer* w = calloc(1, sizeof(*w));
if (!w)
return -ENOMEM;
return r;
}
+ // Load the private key to sign databases
+ if (fkey) {
+ r = parse_private_key(w, fkey);
+ if (r) {
+ loc_writer_unref(w);
+ return r;
+ }
+ }
+
*writer = w;
return 0;
}
static void loc_writer_free(struct loc_writer* writer) {
DEBUG(writer->ctx, "Releasing writer at %p\n", writer);
+ // Free private key
+ if (writer->private_key)
+ EVP_PKEY_free(writer->private_key);
+
// Unref all AS
for (unsigned int i = 0; i < writer->as_count; i++) {
loc_as_unref(writer->as[i]);
static int loc_database_write_pool(struct loc_writer* writer,
struct loc_database_header_v0* header, off_t* offset, FILE* f) {
// Save the offset of the pool section
- DEBUG(writer->ctx, "Pool starts at %jd bytes\n", *offset);
+ DEBUG(writer->ctx, "Pool starts at %jd bytes\n", (intmax_t)*offset);
header->pool_offset = htobe32(*offset);
// Write the pool
static int loc_database_write_as_section(struct loc_writer* writer,
struct loc_database_header_v0* header, off_t* offset, FILE* f) {
- DEBUG(writer->ctx, "AS section starts at %jd bytes\n", *offset);
+ DEBUG(writer->ctx, "AS section starts at %jd bytes\n", (intmax_t)*offset);
header->as_offset = htobe32(*offset);
size_t as_length = 0;
static int loc_database_write_networks(struct loc_writer* writer,
struct loc_database_header_v0* header, off_t* offset, FILE* f) {
// Write the network tree
- DEBUG(writer->ctx, "Network tree starts at %jd bytes\n", *offset);
+ DEBUG(writer->ctx, "Network tree starts at %jd bytes\n", (intmax_t)*offset);
header->network_tree_offset = htobe32(*offset);
size_t network_tree_length = 0;
align_page_boundary(offset, f);
- DEBUG(writer->ctx, "Networks data section starts at %jd bytes\n", *offset);
+ DEBUG(writer->ctx, "Networks data section starts at %jd bytes\n", (intmax_t)*offset);
header->network_data_offset = htobe32(*offset);
// We have now written the entire tree and have all networks
static int loc_database_write_countries(struct loc_writer* writer,
struct loc_database_header_v0* header, off_t* offset, FILE* f) {
- DEBUG(writer->ctx, "Countries section starts at %jd bytes\n", *offset);
+ DEBUG(writer->ctx, "Countries section starts at %jd bytes\n", (intmax_t)*offset);
header->countries_offset = htobe32(*offset);
size_t countries_length = 0;
return 0;
}
+static int loc_writer_create_signature(struct loc_writer* writer,
+ struct loc_database_header_v0* header, FILE* f) {
+ DEBUG(writer->ctx, "Signing database...\n");
+
+ // Read file from the beginning
+ rewind(f);
+
+ // Create a new context for signing
+ EVP_MD_CTX* mdctx = EVP_MD_CTX_new();
+
+ // Initialise the context
+ int r = EVP_DigestSignInit(mdctx, NULL, NULL, NULL, writer->private_key);
+ if (r != 1) {
+ ERROR(writer->ctx, "%s\n", ERR_error_string(ERR_get_error(), NULL));
+ goto END;
+ }
+
+ // Read magic
+ struct loc_database_magic magic;
+ fread(&magic, 1, sizeof(magic), f);
+
+ hexdump(writer->ctx, &magic, sizeof(magic));
+
+ // Feed magic into the signature
+ r = EVP_DigestSignUpdate(mdctx, &magic, sizeof(magic));
+ if (r != 1) {
+ ERROR(writer->ctx, "%s\n", ERR_error_string(ERR_get_error(), NULL));
+ goto END;
+ }
+
+ hexdump(writer->ctx, header, sizeof(*header));
+
+ // Feed the header into the signature
+ r = EVP_DigestSignUpdate(mdctx, header, sizeof(*header));
+ if (r != 1) {
+ ERROR(writer->ctx, "%s\n", ERR_error_string(ERR_get_error(), NULL));
+ goto END;
+ }
+
+ // Skip header
+ fseek(f, sizeof(*header), SEEK_CUR);
+
+ // Walk through the file in chunks of 64kB
+ char buffer[64 * 1024];
+ while (!feof(f)) {
+ size_t bytes_read = fread(buffer, 1, sizeof(buffer), f);
+
+ if (ferror(f)) {
+ ERROR(writer->ctx, "Error reading from file: %s\n", strerror(errno));
+ r = errno;
+ goto END;
+ }
+
+ hexdump(writer->ctx, buffer, bytes_read);
+
+ r = EVP_DigestSignUpdate(mdctx, buffer, bytes_read);
+ if (r != 1) {
+ ERROR(writer->ctx, "%s\n", ERR_error_string(ERR_get_error(), NULL));
+ goto END;
+ }
+ }
+
+ // Compute the signature
+ size_t signature_length = sizeof(header->signature);
+
+ r = EVP_DigestSignFinal(mdctx,
+ (unsigned char*)header->signature, &signature_length);
+ if (r != 1) {
+ ERROR(writer->ctx, "%s\n", ERR_error_string(ERR_get_error(), NULL));
+ goto END;
+ }
+
+ // Save length of the signature
+ header->signature_length = htobe32(signature_length);
+
+ DEBUG(writer->ctx, "Successfully generated signature of %lu bytes\n",
+ signature_length);
+ r = 0;
+
+ // Dump signature
+ hexdump(writer->ctx, header->signature, signature_length);
+
+END:
+ EVP_MD_CTX_free(mdctx);
+
+ return r;
+}
+
LOC_EXPORT int loc_writer_write(struct loc_writer* writer, FILE* f) {
struct loc_database_magic magic;
make_magic(writer, &magic);
time_t now = time(NULL);
header.created_at = htobe64(now);
+ // Clear the signature
+ header.signature_length = 0;
+ for (unsigned int i = 0; i < sizeof(header.signature); i++)
+ header.signature[i] = '\0';
+
+ // Clear the padding
+ for (unsigned int i = 0; i < sizeof(header.padding); i++)
+ header.padding[i] = '\0';
+
int r;
off_t offset = 0;
if (r)
return r;
+ // Create the signature
+ if (writer->private_key) {
+ r = loc_writer_create_signature(writer, &header, f);
+ if (r)
+ return r;
+ }
+
// Write the header
r = fseek(f, sizeof(magic), SEEK_SET);
if (r)
return r;
- offset += fwrite(&header, 1, sizeof(header), f);
+ fwrite(&header, 1, sizeof(header), f);
- return 0;
+ return r;
}