2 libloc - A library to determine the location of someone on the Internet
4 Copyright (C) 2017 IPFire Development Team <info@ipfire.org>
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 This library 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 GNU
14 Lesser General Public License for more details.
22 #include <sys/queue.h>
29 #include <openssl/bio.h>
30 #include <openssl/err.h>
31 #include <openssl/evp.h>
32 #include <openssl/pem.h>
34 #include <libloc/libloc.h>
35 #include <libloc/as.h>
36 #include <libloc/as-list.h>
37 #include <libloc/compat.h>
38 #include <libloc/country.h>
39 #include <libloc/database.h>
40 #include <libloc/format.h>
41 #include <libloc/network.h>
42 #include <libloc/private.h>
43 #include <libloc/writer.h>
49 struct loc_stringpool
* pool
;
54 // Private keys to sign any databases
55 EVP_PKEY
* private_key1
;
56 EVP_PKEY
* private_key2
;
59 char signature1
[LOC_SIGNATURE_MAX_LENGTH
];
60 size_t signature1_length
;
61 char signature2
[LOC_SIGNATURE_MAX_LENGTH
];
62 size_t signature2_length
;
64 struct loc_country
** countries
;
65 size_t countries_count
;
67 struct loc_network_tree
* networks
;
69 struct loc_as_list
* as_list
;
72 static int parse_private_key(struct loc_writer
* writer
, EVP_PKEY
** private_key
, FILE* f
) {
73 // Free any previously loaded keys
75 EVP_PKEY_free(*private_key
);
78 *private_key
= PEM_read_PrivateKey(f
, NULL
, NULL
, NULL
);
82 char* error
= ERR_error_string(ERR_get_error(), NULL
);
83 ERROR(writer
->ctx
, "Could not parse private key: %s\n", error
);
91 LOC_EXPORT
int loc_writer_new(struct loc_ctx
* ctx
, struct loc_writer
** writer
,
92 FILE* fkey1
, FILE* fkey2
) {
93 struct loc_writer
* w
= calloc(1, sizeof(*w
));
97 w
->ctx
= loc_ref(ctx
);
100 int r
= loc_stringpool_new(ctx
, &w
->pool
);
106 // Initialize the network tree
107 r
= loc_network_tree_new(ctx
, &w
->networks
);
113 // Initialize AS list
114 r
= loc_as_list_new(ctx
, &w
->as_list
);
120 // Load the private keys to sign databases
122 r
= parse_private_key(w
, &w
->private_key1
, fkey1
);
130 r
= parse_private_key(w
, &w
->private_key2
, fkey2
);
141 LOC_EXPORT
struct loc_writer
* loc_writer_ref(struct loc_writer
* writer
) {
147 static void loc_writer_free(struct loc_writer
* writer
) {
148 DEBUG(writer
->ctx
, "Releasing writer at %p\n", writer
);
151 if (writer
->private_key1
)
152 EVP_PKEY_free(writer
->private_key1
);
153 if (writer
->private_key2
)
154 EVP_PKEY_free(writer
->private_key2
);
158 loc_as_list_unref(writer
->as_list
);
160 // Unref all countries
161 if (writer
->countries
) {
162 for (unsigned int i
= 0; i
< writer
->countries_count
; i
++) {
163 loc_country_unref(writer
->countries
[i
]);
165 free(writer
->countries
);
168 // Release network tree
169 if (writer
->networks
)
170 loc_network_tree_unref(writer
->networks
);
172 // Unref the string pool
173 loc_stringpool_unref(writer
->pool
);
175 loc_unref(writer
->ctx
);
179 LOC_EXPORT
struct loc_writer
* loc_writer_unref(struct loc_writer
* writer
) {
180 if (--writer
->refcount
> 0)
183 loc_writer_free(writer
);
188 LOC_EXPORT
const char* loc_writer_get_vendor(struct loc_writer
* writer
) {
189 return loc_stringpool_get(writer
->pool
, writer
->vendor
);
192 LOC_EXPORT
int loc_writer_set_vendor(struct loc_writer
* writer
, const char* vendor
) {
193 // Add the string to the string pool
194 off_t offset
= loc_stringpool_add(writer
->pool
, vendor
);
198 writer
->vendor
= offset
;
202 LOC_EXPORT
const char* loc_writer_get_description(struct loc_writer
* writer
) {
203 return loc_stringpool_get(writer
->pool
, writer
->description
);
206 LOC_EXPORT
int loc_writer_set_description(struct loc_writer
* writer
, const char* description
) {
207 // Add the string to the string pool
208 off_t offset
= loc_stringpool_add(writer
->pool
, description
);
212 writer
->description
= offset
;
216 LOC_EXPORT
const char* loc_writer_get_license(struct loc_writer
* writer
) {
217 return loc_stringpool_get(writer
->pool
, writer
->license
);
220 LOC_EXPORT
int loc_writer_set_license(struct loc_writer
* writer
, const char* license
) {
221 // Add the string to the string pool
222 off_t offset
= loc_stringpool_add(writer
->pool
, license
);
226 writer
->license
= offset
;
230 LOC_EXPORT
int loc_writer_add_as(struct loc_writer
* writer
, struct loc_as
** as
, uint32_t number
) {
231 // Create a new AS object
232 int r
= loc_as_new(writer
->ctx
, as
, number
);
236 // Append it to the list
237 return loc_as_list_append(writer
->as_list
, *as
);
240 LOC_EXPORT
int loc_writer_add_network(struct loc_writer
* writer
, struct loc_network
** network
, const char* string
) {
243 // Create a new network object
244 r
= loc_network_new_from_string(writer
->ctx
, network
, string
);
248 // Add it to the local tree
249 return loc_network_tree_add_network(writer
->networks
, *network
);
252 static int __loc_country_cmp(const void* country1
, const void* country2
) {
253 return loc_country_cmp(*(struct loc_country
**)country1
, *(struct loc_country
**)country2
);
256 LOC_EXPORT
int loc_writer_add_country(struct loc_writer
* writer
, struct loc_country
** country
, const char* country_code
) {
257 int r
= loc_country_new(writer
->ctx
, country
, country_code
);
261 // We have a new country to add
262 writer
->countries_count
++;
265 writer
->countries
= realloc(writer
->countries
, sizeof(*writer
->countries
) * writer
->countries_count
);
266 if (!writer
->countries
)
269 // Add as last element
270 writer
->countries
[writer
->countries_count
- 1] = loc_country_ref(*country
);
273 qsort(writer
->countries
, writer
->countries_count
, sizeof(*writer
->countries
), __loc_country_cmp
);
278 static void make_magic(struct loc_writer
* writer
, struct loc_database_magic
* magic
,
279 enum loc_database_version version
) {
281 for (unsigned int i
= 0; i
< strlen(LOC_DATABASE_MAGIC
); i
++)
282 magic
->magic
[i
] = LOC_DATABASE_MAGIC
[i
];
285 magic
->version
= version
;
288 static void align_page_boundary(off_t
* offset
, FILE* f
) {
289 // Move to next page boundary
290 while (*offset
% LOC_DATABASE_PAGE_SIZE
> 0)
291 *offset
+= fwrite("", 1, 1, f
);
294 static int loc_database_write_pool(struct loc_writer
* writer
,
295 struct loc_database_header_v1
* header
, off_t
* offset
, FILE* f
) {
296 // Save the offset of the pool section
297 DEBUG(writer
->ctx
, "Pool starts at %jd bytes\n", (intmax_t)*offset
);
298 header
->pool_offset
= htobe32(*offset
);
301 size_t pool_length
= loc_stringpool_write(writer
->pool
, f
);
302 *offset
+= pool_length
;
304 DEBUG(writer
->ctx
, "Pool has a length of %zu bytes\n", pool_length
);
305 header
->pool_length
= htobe32(pool_length
);
310 static int loc_database_write_as_section(struct loc_writer
* writer
,
311 struct loc_database_header_v1
* header
, off_t
* offset
, FILE* f
) {
312 DEBUG(writer
->ctx
, "AS section starts at %jd bytes\n", (intmax_t)*offset
);
313 header
->as_offset
= htobe32(*offset
);
315 // Sort the AS list first
316 loc_as_list_sort(writer
->as_list
);
318 const size_t as_count
= loc_as_list_size(writer
->as_list
);
320 struct loc_database_as_v1 block
;
321 size_t block_length
= 0;
323 for (unsigned int i
= 0; i
< as_count
; i
++) {
324 struct loc_as
* as
= loc_as_list_get(writer
->as_list
, i
);
328 // Convert AS into database format
329 loc_as_to_database_v1(as
, writer
->pool
, &block
);
332 *offset
+= fwrite(&block
, 1, sizeof(block
), f
);
333 block_length
+= sizeof(block
);
339 DEBUG(writer
->ctx
, "AS section has a length of %zu bytes\n", block_length
);
340 header
->as_length
= htobe32(block_length
);
342 align_page_boundary(offset
, f
);
348 TAILQ_ENTRY(node
) nodes
;
350 struct loc_network_tree_node
* node
;
352 // Indices of the child nodes
357 static struct node
* make_node(struct loc_network_tree_node
* node
) {
358 struct node
* n
= malloc(sizeof(*n
));
362 n
->node
= loc_network_tree_node_ref(node
);
363 n
->index_zero
= n
->index_one
= 0;
368 static void free_node(struct node
* node
) {
369 loc_network_tree_node_unref(node
->node
);
375 TAILQ_ENTRY(network
) networks
;
377 struct loc_network
* network
;
380 static struct network
* make_network(struct loc_network
* network
) {
381 struct network
* n
= malloc(sizeof(*n
));
385 n
->network
= loc_network_ref(network
);
390 static void free_network(struct network
* network
) {
391 loc_network_unref(network
->network
);
396 static int loc_database_write_networks(struct loc_writer
* writer
,
397 struct loc_database_header_v1
* header
, off_t
* offset
, FILE* f
) {
398 // Write the network tree
399 DEBUG(writer
->ctx
, "Network tree starts at %jd bytes\n", (intmax_t)*offset
);
400 header
->network_tree_offset
= htobe32(*offset
);
402 size_t network_tree_length
= 0;
403 size_t network_data_length
= 0;
406 struct node
* child_node
;
409 uint32_t network_index
= 0;
411 struct loc_database_network_v1 db_network
;
412 struct loc_database_network_node_v1 db_node
;
414 // Initialize queue for nodes
415 TAILQ_HEAD(node_t
, node
) nodes
;
418 // Initialize queue for networks
419 TAILQ_HEAD(network_t
, network
) networks
;
420 TAILQ_INIT(&networks
);
423 struct loc_network_tree_node
* root
= loc_network_tree_get_root(writer
->networks
);
424 node
= make_node(root
);
428 TAILQ_INSERT_TAIL(&nodes
, node
, nodes
);
430 while (!TAILQ_EMPTY(&nodes
)) {
431 // Pop first node in list
432 node
= TAILQ_FIRST(&nodes
);
433 TAILQ_REMOVE(&nodes
, node
, nodes
);
435 DEBUG(writer
->ctx
, "Processing node %p\n", node
);
438 struct loc_network_tree_node
* node_zero
= loc_network_tree_node_get(node
->node
, 0);
440 node
->index_zero
= ++index
;
442 child_node
= make_node(node_zero
);
443 loc_network_tree_node_unref(node_zero
);
445 TAILQ_INSERT_TAIL(&nodes
, child_node
, nodes
);
448 struct loc_network_tree_node
* node_one
= loc_network_tree_node_get(node
->node
, 1);
450 node
->index_one
= ++index
;
452 child_node
= make_node(node_one
);
453 loc_network_tree_node_unref(node_one
);
455 TAILQ_INSERT_TAIL(&nodes
, child_node
, nodes
);
458 // Prepare what we are writing to disk
459 db_node
.zero
= htobe32(node
->index_zero
);
460 db_node
.one
= htobe32(node
->index_one
);
462 if (loc_network_tree_node_is_leaf(node
->node
)) {
463 struct loc_network
* network
= loc_network_tree_node_get_network(node
->node
);
465 // Append network to be written out later
466 struct network
* nw
= make_network(network
);
471 TAILQ_INSERT_TAIL(&networks
, nw
, networks
);
473 db_node
.network
= htobe32(network_index
++);
474 loc_network_unref(network
);
476 db_node
.network
= htobe32(0xffffffff);
479 // Write the current node
480 DEBUG(writer
->ctx
, "Writing node %p (0 = %d, 1 = %d)\n",
481 node
, node
->index_zero
, node
->index_one
);
483 *offset
+= fwrite(&db_node
, 1, sizeof(db_node
), f
);
484 network_tree_length
+= sizeof(db_node
);
489 loc_network_tree_node_unref(root
);
491 header
->network_tree_length
= htobe32(network_tree_length
);
493 align_page_boundary(offset
, f
);
495 DEBUG(writer
->ctx
, "Networks data section starts at %jd bytes\n", (intmax_t)*offset
);
496 header
->network_data_offset
= htobe32(*offset
);
498 // We have now written the entire tree and have all networks
499 // in a queue in order as they are indexed
500 while (!TAILQ_EMPTY(&networks
)) {
501 struct network
* nw
= TAILQ_FIRST(&networks
);
502 TAILQ_REMOVE(&networks
, nw
, networks
);
504 // Prepare what we are writing to disk
505 int r
= loc_network_to_database_v1(nw
->network
, &db_network
);
509 *offset
+= fwrite(&db_network
, 1, sizeof(db_network
), f
);
510 network_data_length
+= sizeof(db_network
);
515 header
->network_data_length
= htobe32(network_data_length
);
517 align_page_boundary(offset
, f
);
522 static int loc_database_write_countries(struct loc_writer
* writer
,
523 struct loc_database_header_v1
* header
, off_t
* offset
, FILE* f
) {
524 DEBUG(writer
->ctx
, "Countries section starts at %jd bytes\n", (intmax_t)*offset
);
525 header
->countries_offset
= htobe32(*offset
);
527 size_t countries_length
= 0;
529 struct loc_database_country_v1 country
;
530 for (unsigned int i
= 0; i
< writer
->countries_count
; i
++) {
531 // Convert country into database format
532 loc_country_to_database_v1(writer
->countries
[i
], writer
->pool
, &country
);
535 *offset
+= fwrite(&country
, 1, sizeof(country
), f
);
536 countries_length
+= sizeof(country
);
539 DEBUG(writer
->ctx
, "Countries section has a length of %zu bytes\n", countries_length
);
540 header
->countries_length
= htobe32(countries_length
);
542 align_page_boundary(offset
, f
);
547 static int loc_writer_create_signature(struct loc_writer
* writer
,
548 struct loc_database_header_v1
* header
, FILE* f
, EVP_PKEY
* private_key
,
549 char* signature
, size_t* length
) {
550 DEBUG(writer
->ctx
, "Creating signature...\n");
552 // Read file from the beginning
555 // Create a new context for signing
556 EVP_MD_CTX
* mdctx
= EVP_MD_CTX_new();
558 // Initialise the context
559 int r
= EVP_DigestSignInit(mdctx
, NULL
, NULL
, NULL
, private_key
);
561 ERROR(writer
->ctx
, "%s\n", ERR_error_string(ERR_get_error(), NULL
));
566 struct loc_database_magic magic
;
567 fread(&magic
, 1, sizeof(magic
), f
);
569 hexdump(writer
->ctx
, &magic
, sizeof(magic
));
571 // Feed magic into the signature
572 r
= EVP_DigestSignUpdate(mdctx
, &magic
, sizeof(magic
));
574 ERROR(writer
->ctx
, "%s\n", ERR_error_string(ERR_get_error(), NULL
));
578 hexdump(writer
->ctx
, header
, sizeof(*header
));
580 // Feed the header into the signature
581 r
= EVP_DigestSignUpdate(mdctx
, header
, sizeof(*header
));
583 ERROR(writer
->ctx
, "%s\n", ERR_error_string(ERR_get_error(), NULL
));
588 fseek(f
, sizeof(*header
), SEEK_CUR
);
590 // Walk through the file in chunks of 64kB
591 char buffer
[64 * 1024];
593 size_t bytes_read
= fread(buffer
, 1, sizeof(buffer
), f
);
596 ERROR(writer
->ctx
, "Error reading from file: %s\n", strerror(errno
));
601 hexdump(writer
->ctx
, buffer
, bytes_read
);
603 r
= EVP_DigestSignUpdate(mdctx
, buffer
, bytes_read
);
605 ERROR(writer
->ctx
, "%s\n", ERR_error_string(ERR_get_error(), NULL
));
611 // Compute the signature
612 r
= EVP_DigestSignFinal(mdctx
,
613 (unsigned char*)signature
, length
);
615 ERROR(writer
->ctx
, "%s\n", ERR_error_string(ERR_get_error(), NULL
));
620 DEBUG(writer
->ctx
, "Successfully generated signature of %zu bytes\n", *length
);
624 hexdump(writer
->ctx
, signature
, *length
);
627 EVP_MD_CTX_free(mdctx
);
632 LOC_EXPORT
int loc_writer_write(struct loc_writer
* writer
, FILE* f
, enum loc_database_version version
) {
635 case LOC_DATABASE_VERSION_UNSET
:
636 version
= LOC_DATABASE_VERSION_LATEST
;
639 case LOC_DATABASE_VERSION_1
:
643 ERROR(writer
->ctx
, "Invalid database version: %d\n", version
);
647 DEBUG(writer
->ctx
, "Writing database in version %d\n", version
);
649 struct loc_database_magic magic
;
650 make_magic(writer
, &magic
, version
);
653 struct loc_database_header_v1 header
;
654 header
.vendor
= htobe32(writer
->vendor
);
655 header
.description
= htobe32(writer
->description
);
656 header
.license
= htobe32(writer
->license
);
658 time_t now
= time(NULL
);
659 header
.created_at
= htobe64(now
);
661 // Clear the signatures
662 memset(header
.signature1
, '\0', sizeof(header
.signature1
));
663 header
.signature1_length
= 0;
664 memset(header
.signature2
, '\0', sizeof(header
.signature2
));
665 header
.signature2_length
= 0;
668 memset(header
.padding
, '\0', sizeof(header
.padding
));
673 // Start writing at the beginning of the file
674 r
= fseek(f
, 0, SEEK_SET
);
679 offset
+= fwrite(&magic
, 1, sizeof(magic
), f
);
681 // Skip the space we need to write the header later
682 r
= fseek(f
, sizeof(header
), SEEK_CUR
);
684 DEBUG(writer
->ctx
, "Could not seek to position after header\n");
687 offset
+= sizeof(header
);
689 align_page_boundary(&offset
, f
);
692 r
= loc_database_write_as_section(writer
, &header
, &offset
, f
);
696 // Write all networks
697 r
= loc_database_write_networks(writer
, &header
, &offset
, f
);
702 r
= loc_database_write_countries(writer
, &header
, &offset
, f
);
707 r
= loc_database_write_pool(writer
, &header
, &offset
, f
);
711 // Create the signatures
712 if (writer
->private_key1
) {
713 DEBUG(writer
->ctx
, "Creating signature with first private key\n");
715 writer
->signature1_length
= sizeof(writer
->signature1
);
717 r
= loc_writer_create_signature(writer
, &header
, f
,
718 writer
->private_key1
, writer
->signature1
, &writer
->signature1_length
);
723 if (writer
->private_key2
) {
724 DEBUG(writer
->ctx
, "Creating signature with second private key\n");
726 writer
->signature2_length
= sizeof(writer
->signature2
);
728 r
= loc_writer_create_signature(writer
, &header
, f
,
729 writer
->private_key2
, writer
->signature2
, &writer
->signature2_length
);
734 // Copy the signatures into the header
735 if (writer
->signature1_length
) {
736 DEBUG(writer
->ctx
, "Copying first signature of %zu byte(s)\n",
737 writer
->signature1_length
);
739 memcpy(header
.signature1
, writer
->signature1
, writer
->signature1_length
);
740 header
.signature1_length
= htobe16(writer
->signature1_length
);
743 if (writer
->signature2_length
) {
744 DEBUG(writer
->ctx
, "Copying second signature of %zu byte(s)\n",
745 writer
->signature1_length
);
747 memcpy(header
.signature2
, writer
->signature2
, writer
->signature2_length
);
748 header
.signature2_length
= htobe16(writer
->signature2_length
);
752 r
= fseek(f
, sizeof(magic
), SEEK_SET
);
756 fwrite(&header
, 1, sizeof(header
), f
);