Coverage Report

Created: 2024-01-26 01:52

/work/toxcore/group_onion_announce.c
Line
Count
Source (jump to first uncovered line)
1
/* SPDX-License-Identifier: GPL-3.0-or-later
2
 * Copyright © 2016-2020 The TokTok team.
3
 * Copyright © 2015 Tox project.
4
 */
5
6
#include "group_onion_announce.h"
7
8
#include <assert.h>
9
#include <string.h>
10
11
#include "ccompat.h"
12
#include "crypto_core.h"
13
#include "group_announce.h"
14
#include "logger.h"
15
#include "mono_time.h"
16
#include "network.h"
17
#include "onion_announce.h"
18
19
static_assert(GCA_ANNOUNCE_MAX_SIZE <= ONION_MAX_EXTRA_DATA_SIZE,
20
              "GC_Announce does not fit into the onion packet extra data");
21
22
static pack_extra_data_cb pack_group_announces;
23
non_null()
24
static int pack_group_announces(void *object, const Logger *logger, const Mono_Time *mono_time,
25
                                uint8_t num_nodes, uint8_t *plain, uint16_t plain_size,
26
                                uint8_t *response, uint16_t response_size, uint16_t offset)
27
1.61k
{
28
1.61k
    GC_Announces_List *gc_announces_list = (GC_Announces_List *)object;
29
1.61k
    GC_Public_Announce public_announce;
30
31
1.61k
    if (gca_unpack_public_announce(logger, plain, plain_size,
32
1.61k
                                   &public_announce) == -1) {
33
13
        LOGGER_WARNING(logger, "Failed to unpack public group announce");
34
13
        return -1;
35
13
    }
36
37
1.60k
    const GC_Peer_Announce *new_announce = gca_add_announce(mono_time, gc_announces_list, &public_announce);
38
39
1.60k
    if (new_announce == nullptr) {
40
0
        LOGGER_ERROR(logger, "Failed to add group announce");
41
0
        return -1;
42
0
    }
43
44
1.60k
    GC_Announce gc_announces[GCA_MAX_SENT_ANNOUNCES];
45
1.60k
    const int num_ann = gca_get_announces(gc_announces_list,
46
1.60k
                                          gc_announces,
47
1.60k
                                          GCA_MAX_SENT_ANNOUNCES,
48
1.60k
                                          public_announce.chat_public_key,
49
1.60k
                                          new_announce->base_announce.peer_public_key);
50
51
1.60k
    if (num_ann < 0) {
52
0
        LOGGER_ERROR(logger, "failed to get group announce");
53
0
        return -1;
54
0
    }
55
56
1.60k
    assert(num_ann <= UINT8_MAX);
57
58
1.60k
    size_t announces_length = 0;
59
60
1.60k
    if (gca_pack_announces_list(logger, response + offset, response_size - offset, gc_announces, (uint8_t)num_ann,
61
1.60k
                                &announces_length) != num_ann) {
62
0
        LOGGER_WARNING(logger, "Failed to pack group announces list");
63
0
        return -1;
64
0
    }
65
66
1.60k
    return announces_length;
67
1.60k
}
68
69
void gca_onion_init(GC_Announces_List *group_announce, Onion_Announce *onion_a)
70
3.68k
{
71
3.68k
    onion_announce_extra_data_callback(onion_a, GCA_MAX_SENT_ANNOUNCES * sizeof(GC_Announce), pack_group_announces,
72
3.68k
                                       group_announce);
73
3.68k
}
74
75
int create_gca_announce_request(
76
    const Random *rng, uint8_t *packet, uint16_t max_packet_length, const uint8_t *dest_client_id,
77
    const uint8_t *public_key, const uint8_t *secret_key, const uint8_t *ping_id,
78
    const uint8_t *client_id, const uint8_t *data_public_key, uint64_t sendback_data,
79
    const uint8_t *gc_data, uint16_t gc_data_length)
80
1.60k
{
81
1.60k
    if (max_packet_length < ONION_ANNOUNCE_REQUEST_MAX_SIZE || gc_data_length == 0) {
82
0
        return -1;
83
0
    }
84
85
1.60k
    uint8_t plain[ONION_PING_ID_SIZE + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_PUBLIC_KEY_SIZE +
86
1.60k
                                     ONION_ANNOUNCE_SENDBACK_DATA_LENGTH + GCA_ANNOUNCE_MAX_SIZE];
87
1.60k
    uint8_t *position_in_plain = plain;
88
1.60k
    const size_t encrypted_size = sizeof(plain) - GCA_ANNOUNCE_MAX_SIZE + gc_data_length;
89
90
1.60k
    memcpy(plain, ping_id, ONION_PING_ID_SIZE);
91
1.60k
    position_in_plain += ONION_PING_ID_SIZE;
92
93
1.60k
    memcpy(position_in_plain, client_id, CRYPTO_PUBLIC_KEY_SIZE);
94
1.60k
    position_in_plain += CRYPTO_PUBLIC_KEY_SIZE;
95
96
1.60k
    memcpy(position_in_plain, data_public_key, CRYPTO_PUBLIC_KEY_SIZE);
97
1.60k
    position_in_plain += CRYPTO_PUBLIC_KEY_SIZE;
98
99
1.60k
    memcpy(position_in_plain, &sendback_data, sizeof(sendback_data));
100
1.60k
    position_in_plain += sizeof(sendback_data);
101
102
1.60k
    memcpy(position_in_plain, gc_data, gc_data_length);
103
104
1.60k
    packet[0] = NET_PACKET_ANNOUNCE_REQUEST;
105
1.60k
    random_nonce(rng, packet + 1);
106
1.60k
    memcpy(packet + 1 + CRYPTO_NONCE_SIZE, public_key, CRYPTO_PUBLIC_KEY_SIZE);
107
108
1.60k
    const int len = encrypt_data(dest_client_id, secret_key, packet + 1, plain,
109
1.60k
                                 encrypted_size, packet + 1 + CRYPTO_NONCE_SIZE + CRYPTO_PUBLIC_KEY_SIZE);
110
111
1.60k
    const uint32_t full_length = (uint32_t)len + 1 + CRYPTO_NONCE_SIZE + CRYPTO_PUBLIC_KEY_SIZE;
112
113
1.60k
    if (full_length != ONION_ANNOUNCE_REQUEST_MIN_SIZE + gc_data_length) {
114
0
        return -1;
115
0
    }
116
117
1.60k
    return full_length;
118
1.60k
}