diff options
author | Christian Grothoff <christian@grothoff.org> | 2018-11-22 17:29:01 +0100 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2018-11-22 17:29:01 +0100 |
commit | ca90313490f4233ce9d209abbdcc2d78d16b8326 (patch) | |
tree | ae0743a71f00cd744ba2368cb45c9a4f3949752c /src/hello | |
parent | ae82c19b277ae3f5f43379ed6fc384aba4fdea83 (diff) | |
download | gnunet-ca90313490f4233ce9d209abbdcc2d78d16b8326.tar.gz gnunet-ca90313490f4233ce9d209abbdcc2d78d16b8326.zip |
add new hello generation support logic to tng
Diffstat (limited to 'src/hello')
-rw-r--r-- | src/hello/Makefile.am | 4 | ||||
-rw-r--r-- | src/hello/hello-ng.c | 178 |
2 files changed, 181 insertions, 1 deletions
diff --git a/src/hello/Makefile.am b/src/hello/Makefile.am index 79003301b..00357f9e1 100644 --- a/src/hello/Makefile.am +++ b/src/hello/Makefile.am | |||
@@ -13,7 +13,9 @@ endif | |||
13 | lib_LTLIBRARIES = libgnunethello.la | 13 | lib_LTLIBRARIES = libgnunethello.la |
14 | 14 | ||
15 | libgnunethello_la_SOURCES = \ | 15 | libgnunethello_la_SOURCES = \ |
16 | hello.c address.c | 16 | hello.c \ |
17 | address.c \ | ||
18 | hello-ng.c | ||
17 | libgnunethello_la_LIBADD = \ | 19 | libgnunethello_la_LIBADD = \ |
18 | $(top_builddir)/src/util/libgnunetutil.la $(XLIB) \ | 20 | $(top_builddir)/src/util/libgnunetutil.la $(XLIB) \ |
19 | $(LTLIBINTL) | 21 | $(LTLIBINTL) |
diff --git a/src/hello/hello-ng.c b/src/hello/hello-ng.c new file mode 100644 index 000000000..425095f9c --- /dev/null +++ b/src/hello/hello-ng.c | |||
@@ -0,0 +1,178 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2018 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | */ | ||
18 | |||
19 | /** | ||
20 | * @file hello/hello-ng.c | ||
21 | * @brief helper library for handling HELLOs | ||
22 | * @author Christian Grothoff | ||
23 | */ | ||
24 | #include "platform.h" | ||
25 | #include "gnunet_signatures.h" | ||
26 | #include "gnunet_hello_lib.h" | ||
27 | #include "gnunet_protocols.h" | ||
28 | #include "gnunet_util_lib.h" | ||
29 | |||
30 | /** | ||
31 | * Binary block we sign when we sign an address. | ||
32 | */ | ||
33 | struct SignedAddress | ||
34 | { | ||
35 | /** | ||
36 | * Purpose must be #GNUNET_SIGNATURE_PURPOSE_TRANSPORT_ADDRESS | ||
37 | */ | ||
38 | struct GNUNET_CRYPTO_EccSignaturePurpose purpose; | ||
39 | |||
40 | /** | ||
41 | * When does the address expire. | ||
42 | */ | ||
43 | struct GNUNET_TIME_AbsoluteNBO expiration; | ||
44 | |||
45 | /** | ||
46 | * Hash of the address. | ||
47 | */ | ||
48 | struct GNUNET_HashCode h_addr; | ||
49 | }; | ||
50 | |||
51 | |||
52 | /** | ||
53 | * Build address record by signing raw information with private key. | ||
54 | * | ||
55 | * @param address text address at @a communicator to sign | ||
56 | * @param expiration how long is @a address valid | ||
57 | * @param private_key signing key to use | ||
58 | * @param result[out] where to write address record (allocated) | ||
59 | * @param result_size[out] set to size of @a result | ||
60 | */ | ||
61 | void | ||
62 | GNUNET_HELLO_sign_address (const char *address, | ||
63 | struct GNUNET_TIME_Absolute expiration, | ||
64 | const struct GNUNET_CRYPTO_EddsaPrivateKey *private_key, | ||
65 | void **result, | ||
66 | size_t *result_size) | ||
67 | { | ||
68 | struct SignedAddress sa; | ||
69 | struct GNUNET_CRYPTO_EddsaSignature sig; | ||
70 | char *sig_str; | ||
71 | |||
72 | sa.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_ADDRESS); | ||
73 | sa.purpose.size = htonl (sizeof (sa)); | ||
74 | sa.expiration = GNUNET_TIME_absolute_hton (expiration); | ||
75 | GNUNET_CRYPTO_hash (address, | ||
76 | strlen (address), | ||
77 | &sa.h_addr); | ||
78 | GNUNET_assert (GNUNET_YES == | ||
79 | GNUNET_CRYPTO_eddsa_sign (private_key, | ||
80 | &sa.purpose, | ||
81 | &sig)); | ||
82 | sig_str = NULL; | ||
83 | (void) GNUNET_STRINGS_base64_encode (&sig, | ||
84 | sizeof (sig), | ||
85 | &sig_str); | ||
86 | *result_size = 1 + GNUNET_asprintf ((char **) result, | ||
87 | "%s;%llu;%s", | ||
88 | sig_str, | ||
89 | (unsigned long long) expiration.abs_value_us, | ||
90 | address); | ||
91 | GNUNET_free (sig_str); | ||
92 | } | ||
93 | |||
94 | |||
95 | /** | ||
96 | * Check signature and extract address record. | ||
97 | * | ||
98 | * @param raw raw signed address | ||
99 | * @param raw_size size of @a raw | ||
100 | * @param public_key public key to use for signature verification | ||
101 | * @param expiration[out] how long is the address valid | ||
102 | * @return NULL on error, otherwise the address | ||
103 | */ | ||
104 | char * | ||
105 | GNUNET_HELLO_extract_address (const void *raw, | ||
106 | size_t raw_size, | ||
107 | const struct GNUNET_CRYPTO_EddsaPublicKey *public_key, | ||
108 | struct GNUNET_TIME_Absolute *expiration) | ||
109 | { | ||
110 | const char *raws = raw; | ||
111 | unsigned long long raw_us; | ||
112 | const char *sc; | ||
113 | const char *sc2; | ||
114 | const char *raw_addr; | ||
115 | struct GNUNET_TIME_Absolute raw_expiration; | ||
116 | struct SignedAddress sa; | ||
117 | struct GNUNET_CRYPTO_EddsaSignature *sig; | ||
118 | |||
119 | if ('\0' != raws[raw_size]) | ||
120 | { | ||
121 | GNUNET_break_op (0); | ||
122 | return NULL; | ||
123 | } | ||
124 | if (NULL == (sc = strchr (raws, | ||
125 | ';'))) | ||
126 | { | ||
127 | GNUNET_break_op (0); | ||
128 | return NULL; | ||
129 | } | ||
130 | if (NULL == (sc2 = strchr (sc + 1, | ||
131 | ';'))) | ||
132 | { | ||
133 | GNUNET_break_op (0); | ||
134 | return NULL; | ||
135 | } | ||
136 | if (1 != sscanf (sc + 1, | ||
137 | "%llu;", | ||
138 | &raw_us)) | ||
139 | { | ||
140 | GNUNET_break_op (0); | ||
141 | return NULL; | ||
142 | } | ||
143 | raw_expiration.abs_value_us = raw_us; | ||
144 | if (0 == GNUNET_TIME_absolute_get_remaining (raw_expiration).rel_value_us) | ||
145 | return NULL; /* expired */ | ||
146 | sig = NULL; | ||
147 | if (sizeof (struct GNUNET_CRYPTO_EddsaSignature) != | ||
148 | GNUNET_STRINGS_base64_decode (raws, | ||
149 | sc - raws, | ||
150 | (void **) &sig)) | ||
151 | { | ||
152 | GNUNET_break_op (0); | ||
153 | GNUNET_free_non_null (sig); | ||
154 | return NULL; | ||
155 | } | ||
156 | raw_addr = sc2 + 1; | ||
157 | |||
158 | sa.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_ADDRESS); | ||
159 | sa.purpose.size = htonl (sizeof (sa)); | ||
160 | sa.expiration = GNUNET_TIME_absolute_hton (raw_expiration); | ||
161 | GNUNET_CRYPTO_hash (raw_addr, | ||
162 | strlen (raw_addr), | ||
163 | &sa.h_addr); | ||
164 | if (GNUNET_YES != | ||
165 | GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_ADDRESS, | ||
166 | &sa.purpose, | ||
167 | sig, | ||
168 | public_key)) | ||
169 | { | ||
170 | GNUNET_break_op (0); | ||
171 | GNUNET_free (sig); | ||
172 | return NULL; | ||
173 | } | ||
174 | GNUNET_free (sig); | ||
175 | return GNUNET_strdup (raw_addr); | ||
176 | } | ||
177 | |||
178 | |||