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 <loc/libloc.h>
36 #include <loc/compat.h>
37 #include <loc/country.h>
38 #include <loc/database.h>
39 #include <loc/format.h>
40 #include <loc/network.h>
41 #include <loc/private.h>
42 #include <loc/writer.h>
48 struct loc_stringpool
* pool
;
53 // Private keys to sign any databases
54 EVP_PKEY
* private_key1
;
55 EVP_PKEY
* private_key2
;
58 char signature1
[LOC_SIGNATURE_MAX_LENGTH
];
59 size_t signature1_length
;
60 char signature2
[LOC_SIGNATURE_MAX_LENGTH
];
61 size_t signature2_length
;
66 struct loc_country
** countries
;
67 size_t countries_count
;
69 struct loc_network_tree
* networks
;
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 // Load the private keys to sign databases
115 r
= parse_private_key(w
, &w
->private_key1
, fkey1
);
123 r
= parse_private_key(w
, &w
->private_key2
, fkey2
);
134 LOC_EXPORT
struct loc_writer
* loc_writer_ref(struct loc_writer
* writer
) {
140 static void loc_writer_free(struct loc_writer
* writer
) {
141 DEBUG(writer
->ctx
, "Releasing writer at %p\n", writer
);
144 if (writer
->private_key1
)
145 EVP_PKEY_free(writer
->private_key1
);
146 if (writer
->private_key2
)
147 EVP_PKEY_free(writer
->private_key2
);
150 for (unsigned int i
= 0; i
< writer
->as_count
; i
++) {
151 loc_as_unref(writer
->as
[i
]);
154 // Release network tree
155 if (writer
->networks
)
156 loc_network_tree_unref(writer
->networks
);
158 // Unref the string pool
159 loc_stringpool_unref(writer
->pool
);
161 loc_unref(writer
->ctx
);
165 LOC_EXPORT
struct loc_writer
* loc_writer_unref(struct loc_writer
* writer
) {
166 if (--writer
->refcount
> 0)
169 loc_writer_free(writer
);
174 LOC_EXPORT
const char* loc_writer_get_vendor(struct loc_writer
* writer
) {
175 return loc_stringpool_get(writer
->pool
, writer
->vendor
);
178 LOC_EXPORT
int loc_writer_set_vendor(struct loc_writer
* writer
, const char* vendor
) {
179 // Add the string to the string pool
180 off_t offset
= loc_stringpool_add(writer
->pool
, vendor
);
184 writer
->vendor
= offset
;
188 LOC_EXPORT
const char* loc_writer_get_description(struct loc_writer
* writer
) {
189 return loc_stringpool_get(writer
->pool
, writer
->description
);
192 LOC_EXPORT
int loc_writer_set_description(struct loc_writer
* writer
, const char* description
) {
193 // Add the string to the string pool
194 off_t offset
= loc_stringpool_add(writer
->pool
, description
);
198 writer
->description
= offset
;
202 LOC_EXPORT
const char* loc_writer_get_license(struct loc_writer
* writer
) {
203 return loc_stringpool_get(writer
->pool
, writer
->license
);
206 LOC_EXPORT
int loc_writer_set_license(struct loc_writer
* writer
, const char* license
) {
207 // Add the string to the string pool
208 off_t offset
= loc_stringpool_add(writer
->pool
, license
);
212 writer
->license
= offset
;
216 static int __loc_as_cmp(const void* as1
, const void* as2
) {
217 return loc_as_cmp(*(struct loc_as
**)as1
, *(struct loc_as
**)as2
);
220 LOC_EXPORT
int loc_writer_add_as(struct loc_writer
* writer
, struct loc_as
** as
, uint32_t number
) {
221 int r
= loc_as_new(writer
->ctx
, as
, number
);
225 // We have a new AS to add
229 writer
->as
= realloc(writer
->as
, sizeof(*writer
->as
) * writer
->as_count
);
233 // Add as last element
234 writer
->as
[writer
->as_count
- 1] = loc_as_ref(*as
);
237 qsort(writer
->as
, writer
->as_count
, sizeof(*writer
->as
), __loc_as_cmp
);
242 LOC_EXPORT
int loc_writer_add_network(struct loc_writer
* writer
, struct loc_network
** network
, const char* string
) {
245 // Create a new network object
246 r
= loc_network_new_from_string(writer
->ctx
, network
, string
);
250 // Add it to the local tree
251 return loc_network_tree_add_network(writer
->networks
, *network
);
254 static int __loc_country_cmp(const void* country1
, const void* country2
) {
255 return loc_country_cmp(*(struct loc_country
**)country1
, *(struct loc_country
**)country2
);
258 LOC_EXPORT
int loc_writer_add_country(struct loc_writer
* writer
, struct loc_country
** country
, const char* country_code
) {
259 int r
= loc_country_new(writer
->ctx
, country
, country_code
);
263 // We have a new country to add
264 writer
->countries_count
++;
267 writer
->countries
= realloc(writer
->countries
, sizeof(*writer
->countries
) * writer
->countries_count
);
268 if (!writer
->countries
)
271 // Add as last element
272 writer
->countries
[writer
->countries_count
- 1] = loc_country_ref(*country
);
275 qsort(writer
->countries
, writer
->countries_count
, sizeof(*writer
->countries
), __loc_country_cmp
);
280 static void make_magic(struct loc_writer
* writer
, struct loc_database_magic
* magic
,
281 enum loc_database_version version
) {
283 for (unsigned int i
= 0; i
< strlen(LOC_DATABASE_MAGIC
); i
++)
284 magic
->magic
[i
] = LOC_DATABASE_MAGIC
[i
];
287 magic
->version
= version
;
290 static void align_page_boundary(off_t
* offset
, FILE* f
) {
291 // Move to next page boundary
292 while (*offset
% LOC_DATABASE_PAGE_SIZE
> 0)
293 *offset
+= fwrite("", 1, 1, f
);
296 static int loc_database_write_pool(struct loc_writer
* writer
,
297 struct loc_database_header_v1
* header
, off_t
* offset
, FILE* f
) {
298 // Save the offset of the pool section
299 DEBUG(writer
->ctx
, "Pool starts at %jd bytes\n", (intmax_t)*offset
);
300 header
->pool_offset
= htobe32(*offset
);
303 size_t pool_length
= loc_stringpool_write(writer
->pool
, f
);
304 *offset
+= pool_length
;
306 DEBUG(writer
->ctx
, "Pool has a length of %zu bytes\n", pool_length
);
307 header
->pool_length
= htobe32(pool_length
);
312 static int loc_database_write_as_section(struct loc_writer
* writer
,
313 struct loc_database_header_v1
* header
, off_t
* offset
, FILE* f
) {
314 DEBUG(writer
->ctx
, "AS section starts at %jd bytes\n", (intmax_t)*offset
);
315 header
->as_offset
= htobe32(*offset
);
317 size_t as_length
= 0;
319 struct loc_database_as_v1 as
;
320 for (unsigned int i
= 0; i
< writer
->as_count
; i
++) {
321 // Convert AS into database format
322 loc_as_to_database_v1(writer
->as
[i
], writer
->pool
, &as
);
325 *offset
+= fwrite(&as
, 1, sizeof(as
), f
);
326 as_length
+= sizeof(as
);
329 DEBUG(writer
->ctx
, "AS section has a length of %zu bytes\n", as_length
);
330 header
->as_length
= htobe32(as_length
);
332 align_page_boundary(offset
, f
);
338 TAILQ_ENTRY(node
) nodes
;
340 struct loc_network_tree_node
* node
;
342 // Indices of the child nodes
347 static struct node
* make_node(struct loc_network_tree_node
* node
) {
348 struct node
* n
= malloc(sizeof(*n
));
352 n
->node
= loc_network_tree_node_ref(node
);
353 n
->index_zero
= n
->index_one
= 0;
358 static void free_node(struct node
* node
) {
359 loc_network_tree_node_unref(node
->node
);
365 TAILQ_ENTRY(network
) networks
;
367 struct loc_network
* network
;
370 static struct network
* make_network(struct loc_network
* network
) {
371 struct network
* n
= malloc(sizeof(*n
));
375 n
->network
= loc_network_ref(network
);
380 static void free_network(struct network
* network
) {
381 loc_network_unref(network
->network
);
386 static int loc_database_write_networks(struct loc_writer
* writer
,
387 struct loc_database_header_v1
* header
, off_t
* offset
, FILE* f
) {
388 // Write the network tree
389 DEBUG(writer
->ctx
, "Network tree starts at %jd bytes\n", (intmax_t)*offset
);
390 header
->network_tree_offset
= htobe32(*offset
);
392 size_t network_tree_length
= 0;
393 size_t network_data_length
= 0;
396 struct node
* child_node
;
399 uint32_t network_index
= 0;
401 struct loc_database_network_v1 db_network
;
402 struct loc_database_network_node_v1 db_node
;
404 // Initialize queue for nodes
405 TAILQ_HEAD(node_t
, node
) nodes
;
408 // Initialize queue for networks
409 TAILQ_HEAD(network_t
, network
) networks
;
410 TAILQ_INIT(&networks
);
413 struct loc_network_tree_node
* root
= loc_network_tree_get_root(writer
->networks
);
414 node
= make_node(root
);
416 TAILQ_INSERT_TAIL(&nodes
, node
, nodes
);
418 while (!TAILQ_EMPTY(&nodes
)) {
419 // Pop first node in list
420 node
= TAILQ_FIRST(&nodes
);
421 TAILQ_REMOVE(&nodes
, node
, nodes
);
423 DEBUG(writer
->ctx
, "Processing node %p\n", node
);
426 struct loc_network_tree_node
* node_zero
= loc_network_tree_node_get(node
->node
, 0);
428 node
->index_zero
= ++index
;
430 child_node
= make_node(node_zero
);
431 loc_network_tree_node_unref(node_zero
);
433 TAILQ_INSERT_TAIL(&nodes
, child_node
, nodes
);
436 struct loc_network_tree_node
* node_one
= loc_network_tree_node_get(node
->node
, 1);
438 node
->index_one
= ++index
;
440 child_node
= make_node(node_one
);
441 loc_network_tree_node_unref(node_one
);
443 TAILQ_INSERT_TAIL(&nodes
, child_node
, nodes
);
446 // Prepare what we are writing to disk
447 db_node
.zero
= htobe32(node
->index_zero
);
448 db_node
.one
= htobe32(node
->index_one
);
450 if (loc_network_tree_node_is_leaf(node
->node
)) {
451 struct loc_network
* network
= loc_network_tree_node_get_network(node
->node
);
453 // Append network to be written out later
454 struct network
* nw
= make_network(network
);
455 TAILQ_INSERT_TAIL(&networks
, nw
, networks
);
457 db_node
.network
= htobe32(network_index
++);
458 loc_network_unref(network
);
460 db_node
.network
= htobe32(0xffffffff);
463 // Write the current node
464 DEBUG(writer
->ctx
, "Writing node %p (0 = %d, 1 = %d)\n",
465 node
, node
->index_zero
, node
->index_one
);
467 *offset
+= fwrite(&db_node
, 1, sizeof(db_node
), f
);
468 network_tree_length
+= sizeof(db_node
);
473 loc_network_tree_node_unref(root
);
475 header
->network_tree_length
= htobe32(network_tree_length
);
477 align_page_boundary(offset
, f
);
479 DEBUG(writer
->ctx
, "Networks data section starts at %jd bytes\n", (intmax_t)*offset
);
480 header
->network_data_offset
= htobe32(*offset
);
482 // We have now written the entire tree and have all networks
483 // in a queue in order as they are indexed
484 while (!TAILQ_EMPTY(&networks
)) {
485 struct network
* nw
= TAILQ_FIRST(&networks
);
486 TAILQ_REMOVE(&networks
, nw
, networks
);
488 // Prepare what we are writing to disk
489 int r
= loc_network_to_database_v1(nw
->network
, &db_network
);
493 *offset
+= fwrite(&db_network
, 1, sizeof(db_network
), f
);
494 network_data_length
+= sizeof(db_network
);
499 header
->network_data_length
= htobe32(network_data_length
);
501 align_page_boundary(offset
, f
);
506 static int loc_database_write_countries(struct loc_writer
* writer
,
507 struct loc_database_header_v1
* header
, off_t
* offset
, FILE* f
) {
508 DEBUG(writer
->ctx
, "Countries section starts at %jd bytes\n", (intmax_t)*offset
);
509 header
->countries_offset
= htobe32(*offset
);
511 size_t countries_length
= 0;
513 struct loc_database_country_v1 country
;
514 for (unsigned int i
= 0; i
< writer
->countries_count
; i
++) {
515 // Convert country into database format
516 loc_country_to_database_v1(writer
->countries
[i
], writer
->pool
, &country
);
519 *offset
+= fwrite(&country
, 1, sizeof(country
), f
);
520 countries_length
+= sizeof(country
);
523 DEBUG(writer
->ctx
, "Countries section has a length of %zu bytes\n", countries_length
);
524 header
->countries_length
= htobe32(countries_length
);
526 align_page_boundary(offset
, f
);
531 static int loc_writer_create_signature(struct loc_writer
* writer
,
532 struct loc_database_header_v1
* header
, FILE* f
, EVP_PKEY
* private_key
,
533 char* signature
, size_t* length
) {
534 DEBUG(writer
->ctx
, "Creating signature...\n");
536 // Read file from the beginning
539 // Create a new context for signing
540 EVP_MD_CTX
* mdctx
= EVP_MD_CTX_new();
542 // Initialise the context
543 int r
= EVP_DigestSignInit(mdctx
, NULL
, NULL
, NULL
, private_key
);
545 ERROR(writer
->ctx
, "%s\n", ERR_error_string(ERR_get_error(), NULL
));
550 struct loc_database_magic magic
;
551 fread(&magic
, 1, sizeof(magic
), f
);
553 hexdump(writer
->ctx
, &magic
, sizeof(magic
));
555 // Feed magic into the signature
556 r
= EVP_DigestSignUpdate(mdctx
, &magic
, sizeof(magic
));
558 ERROR(writer
->ctx
, "%s\n", ERR_error_string(ERR_get_error(), NULL
));
562 hexdump(writer
->ctx
, header
, sizeof(*header
));
564 // Feed the header into the signature
565 r
= EVP_DigestSignUpdate(mdctx
, header
, sizeof(*header
));
567 ERROR(writer
->ctx
, "%s\n", ERR_error_string(ERR_get_error(), NULL
));
572 fseek(f
, sizeof(*header
), SEEK_CUR
);
574 // Walk through the file in chunks of 64kB
575 char buffer
[64 * 1024];
577 size_t bytes_read
= fread(buffer
, 1, sizeof(buffer
), f
);
580 ERROR(writer
->ctx
, "Error reading from file: %s\n", strerror(errno
));
585 hexdump(writer
->ctx
, buffer
, bytes_read
);
587 r
= EVP_DigestSignUpdate(mdctx
, buffer
, bytes_read
);
589 ERROR(writer
->ctx
, "%s\n", ERR_error_string(ERR_get_error(), NULL
));
595 // Compute the signature
596 r
= EVP_DigestSignFinal(mdctx
,
597 (unsigned char*)signature
, length
);
599 ERROR(writer
->ctx
, "%s\n", ERR_error_string(ERR_get_error(), NULL
));
604 DEBUG(writer
->ctx
, "Successfully generated signature of %lu bytes\n", *length
);
608 hexdump(writer
->ctx
, signature
, *length
);
611 EVP_MD_CTX_free(mdctx
);
616 LOC_EXPORT
int loc_writer_write(struct loc_writer
* writer
, FILE* f
, enum loc_database_version version
) {
619 case LOC_DATABASE_VERSION_UNSET
:
620 version
= LOC_DATABASE_VERSION_LATEST
;
623 case LOC_DATABASE_VERSION_1
:
627 ERROR(writer
->ctx
, "Invalid database version: %d\n", version
);
631 DEBUG(writer
->ctx
, "Writing database in version %d\n", version
);
633 struct loc_database_magic magic
;
634 make_magic(writer
, &magic
, version
);
637 struct loc_database_header_v1 header
;
638 header
.vendor
= htobe32(writer
->vendor
);
639 header
.description
= htobe32(writer
->description
);
640 header
.license
= htobe32(writer
->license
);
642 time_t now
= time(NULL
);
643 header
.created_at
= htobe64(now
);
645 // Clear the signatures
646 memset(header
.signature1
, '\0', sizeof(header
.signature1
));
647 header
.signature1_length
= 0;
648 memset(header
.signature2
, '\0', sizeof(header
.signature2
));
649 header
.signature2_length
= 0;
652 memset(header
.padding
, '\0', sizeof(header
.padding
));
657 // Start writing at the beginning of the file
658 r
= fseek(f
, 0, SEEK_SET
);
663 offset
+= fwrite(&magic
, 1, sizeof(magic
), f
);
665 // Skip the space we need to write the header later
666 r
= fseek(f
, sizeof(header
), SEEK_CUR
);
668 DEBUG(writer
->ctx
, "Could not seek to position after header\n");
671 offset
+= sizeof(header
);
673 align_page_boundary(&offset
, f
);
676 r
= loc_database_write_as_section(writer
, &header
, &offset
, f
);
680 // Write all networks
681 r
= loc_database_write_networks(writer
, &header
, &offset
, f
);
686 r
= loc_database_write_countries(writer
, &header
, &offset
, f
);
691 r
= loc_database_write_pool(writer
, &header
, &offset
, f
);
695 // Create the signatures
696 if (writer
->private_key1
) {
697 DEBUG(writer
->ctx
, "Creating signature with first private key\n");
699 writer
->signature1_length
= sizeof(writer
->signature1
);
701 r
= loc_writer_create_signature(writer
, &header
, f
,
702 writer
->private_key1
, writer
->signature1
, &writer
->signature1_length
);
707 if (writer
->private_key2
) {
708 DEBUG(writer
->ctx
, "Creating signature with second private key\n");
710 writer
->signature2_length
= sizeof(writer
->signature2
);
712 r
= loc_writer_create_signature(writer
, &header
, f
,
713 writer
->private_key2
, writer
->signature2
, &writer
->signature2_length
);
718 // Copy the signatures into the header
719 if (writer
->signature1_length
) {
720 DEBUG(writer
->ctx
, "Copying first signature of %zu byte(s)\n",
721 writer
->signature1_length
);
723 memcpy(header
.signature1
, writer
->signature1
, writer
->signature1_length
);
724 header
.signature1_length
= htobe16(writer
->signature1_length
);
727 if (writer
->signature2_length
) {
728 DEBUG(writer
->ctx
, "Copying second signature of %zu byte(s)\n",
729 writer
->signature1_length
);
731 memcpy(header
.signature2
, writer
->signature2
, writer
->signature2_length
);
732 header
.signature2_length
= htobe16(writer
->signature2_length
);
736 r
= fseek(f
, sizeof(magic
), SEEK_SET
);
740 fwrite(&header
, 1, sizeof(header
), f
);