summaryrefslogtreecommitdiff
path: root/src/hello/hello-ng.c
blob: 2d60b04f3de53f8e30a3019ddb02d33fa367ee36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
     This file is part of GNUnet.
     Copyright (C) 2018 GNUnet e.V.

     GNUnet is free software: you can redistribute it and/or modify it
     under the terms of the GNU Affero General Public License as published
     by the Free Software Foundation, either version 3 of the License,
     or (at your option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     Affero General Public License for more details.

     You should have received a copy of the GNU Affero General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>.

     SPDX-License-Identifier: AGPL3.0-or-later
*/

/**
 * @file hello/hello-ng.c
 * @brief helper library for handling HELLOs
 * @author Christian Grothoff
 */
#include "platform.h"
#include "gnunet_signatures.h"
#include "gnunet_hello_lib.h"
#include "gnunet_protocols.h"
#include "gnunet_util_lib.h"
#include "gnunet_ats_service.h"

/**
 * Binary block we sign when we sign an address.
 */
struct SignedAddress
{
  /**
   * Purpose must be #GNUNET_SIGNATURE_PURPOSE_TRANSPORT_ADDRESS
   */
  struct GNUNET_CRYPTO_EccSignaturePurpose purpose;

  /**
   * When does the address expire.
   */
  struct GNUNET_TIME_AbsoluteNBO expiration;

  /**
   * Hash of the address.
   */
  struct GNUNET_HashCode h_addr;
};


/**
 * Build address record by signing raw information with private key.
 *
 * @param address text address at @a communicator to sign
 * @param nt network type of @a address
 * @param expiration how long is @a address valid
 * @param private_key signing key to use
 * @param result[out] where to write address record (allocated)
 * @param result_size[out] set to size of @a result
 */
void
GNUNET_HELLO_sign_address (const char *address,
			   enum GNUNET_NetworkType nt,
			   struct GNUNET_TIME_Absolute expiration,
			   const struct GNUNET_CRYPTO_EddsaPrivateKey *private_key,
			   void **result,
			   size_t *result_size)
{
  struct SignedAddress sa;
  struct GNUNET_CRYPTO_EddsaSignature sig;
  char *sig_str;

  sa.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_ADDRESS);
  sa.purpose.size = htonl (sizeof (sa));
  sa.expiration = GNUNET_TIME_absolute_hton (expiration);
  GNUNET_CRYPTO_hash (address,
		      strlen (address),
		      &sa.h_addr);
  GNUNET_assert (GNUNET_YES ==
		 GNUNET_CRYPTO_eddsa_sign (private_key,
					   &sa.purpose,
					   &sig));
  sig_str = NULL;
  (void) GNUNET_STRINGS_base64_encode (&sig,
				       sizeof (sig),
				       &sig_str);
  *result_size = 1 + GNUNET_asprintf ((char **) result,
				      "%s;%llu;%u;%s",
				      sig_str,
				      (unsigned long long) expiration.abs_value_us,
				      (unsigned int) nt,
				      address);
  GNUNET_free (sig_str);
}


/**
 * Check signature and extract address record.
 *
 * @param raw raw signed address
 * @param raw_size size of @a raw
 * @param pid public key to use for signature verification
 * @param nt[out] set to network type
 * @param expiration[out] how long is the address valid
 * @return NULL on error, otherwise the address
 */
char *
GNUNET_HELLO_extract_address (const void *raw,
			      size_t raw_size,
			      const struct GNUNET_PeerIdentity *pid,
			      enum GNUNET_NetworkType *nt,
			      struct GNUNET_TIME_Absolute *expiration)
{
  const struct GNUNET_CRYPTO_EddsaPublicKey *public_key = &pid->public_key;
  const char *raws = raw;
  unsigned long long raw_us;
  unsigned int raw_nt;
  const char *sc;
  const char *sc2;
  const char *sc3;
  const char *raw_addr;
  struct GNUNET_TIME_Absolute raw_expiration;
  struct SignedAddress sa;
  struct GNUNET_CRYPTO_EddsaSignature *sig;

  if ('\0' != raws[raw_size])
  {
    GNUNET_break_op (0);
    return NULL;
  }
  if (NULL == (sc = strchr (raws,
			    ';')))
  {
    GNUNET_break_op (0);
    return NULL;
  }
  if (NULL == (sc2 = strchr (sc + 1,
			     ';')))
  {
    GNUNET_break_op (0);
    return NULL;
  }
  if (NULL == (sc3 = strchr (sc2 + 1,
			     ';')))
  {
    GNUNET_break_op (0);
    return NULL;
  }
  if (1 != sscanf (sc + 1,
		   "%llu;%u;",
		   &raw_us,
		   &raw_nt))
  {
    GNUNET_break_op (0);
    return NULL;
  }
  raw_expiration.abs_value_us = raw_us;
  if (0 == GNUNET_TIME_absolute_get_remaining (raw_expiration).rel_value_us)
    return NULL; /* expired */
  sig = NULL;
  if (sizeof (struct GNUNET_CRYPTO_EddsaSignature) !=
      GNUNET_STRINGS_base64_decode (raws,
				    sc - raws,
				    (void **) &sig))
  {
    GNUNET_break_op (0);
    GNUNET_free_non_null (sig);
    return NULL;
  }
  raw_addr = sc3 + 1;

  sa.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_ADDRESS);
  sa.purpose.size = htonl (sizeof (sa));
  sa.expiration = GNUNET_TIME_absolute_hton (raw_expiration);
  GNUNET_CRYPTO_hash (raw_addr,
		      strlen (raw_addr),
		      &sa.h_addr);
  if (GNUNET_YES !=
      GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_ADDRESS,
				  &sa.purpose,
				  sig,
				  public_key))
  {
    GNUNET_break_op (0);
    GNUNET_free (sig);
    return NULL;
  }
  GNUNET_free (sig);
  *expiration = raw_expiration;
  *nt = (enum GNUNET_NetworkType) raw_nt;
  return GNUNET_strdup (raw_addr);
}


/**
 * Given an address as a string, extract the prefix that identifies
 * the communicator offering transmissions to that address.
 *
 * @param address a peer's address
 * @return NULL if the address is mal-formed, otherwise the prefix
 */ 
char *
GNUNET_HELLO_address_to_prefix (const char *address)
{
  const char *dash;

  dash = strchr (address, '-');
  if (NULL == dash)
    return NULL;
  return GNUNET_strndup (address,
			 dash - address);
}