aboutsummaryrefslogtreecommitdiff
path: root/src/abd/delegate_misc.c
diff options
context:
space:
mode:
authorAndreas Ebner <a.e.bner@web.de>2019-10-07 11:48:07 +0200
committerSchanzenbach, Martin <mschanzenbach@posteo.de>2019-10-07 12:18:42 +0200
commit1d468ecabd6c2ee5c0eae672292efa0f51bc9e48 (patch)
tree6b527980752f9603945e070c8187bfbb06232b6f /src/abd/delegate_misc.c
parent5cc45c7ee6a3ac522e5a1f58010d4efdf4fd102f (diff)
downloadgnunet-1d468ecabd6c2ee5c0eae672292efa0f51bc9e48.tar.gz
gnunet-1d468ecabd6c2ee5c0eae672292efa0f51bc9e48.zip
Renamed credential service to abd, replaced all related functions, parameters, etc
Diffstat (limited to 'src/abd/delegate_misc.c')
-rw-r--r--src/abd/delegate_misc.c274
1 files changed, 274 insertions, 0 deletions
diff --git a/src/abd/delegate_misc.c b/src/abd/delegate_misc.c
new file mode 100644
index 000000000..ecc7f7669
--- /dev/null
+++ b/src/abd/delegate_misc.c
@@ -0,0 +1,274 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2009-2013, 2016 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 SPDX-License-Identifier: AGPL3.0-or-later
19*/
20
21
22/**
23 * @file abd/delegate_misc.c
24 * @brief Misc API for delegate
25 *
26 * @author Martin Schanzenbach
27 */
28#include "platform.h"
29#include "gnunet_util_lib.h"
30#include "gnunet_constants.h"
31#include "gnunet_abd_service.h"
32#include "gnunet_signatures.h"
33#include "abd.h"
34#include <inttypes.h>
35
36char *
37GNUNET_ABD_delegate_to_string (
38 const struct GNUNET_ABD_Delegate *cred)
39{
40 char *cred_str;
41 char *subject_pkey;
42 char *issuer_pkey;
43 char *signature;
44
45 subject_pkey = GNUNET_CRYPTO_ecdsa_public_key_to_string (&cred->subject_key);
46 issuer_pkey = GNUNET_CRYPTO_ecdsa_public_key_to_string (&cred->issuer_key);
47 GNUNET_STRINGS_base64_encode ((char *) &cred->signature,
48 sizeof (struct GNUNET_CRYPTO_EcdsaSignature),
49 &signature);
50 if (0 == cred->subject_attribute_len)
51 {
52 GNUNET_asprintf (&cred_str,
53 "%s.%s -> %s | %s | %" SCNu64,
54 issuer_pkey,
55 cred->issuer_attribute,
56 subject_pkey,
57 signature,
58 cred->expiration.abs_value_us);
59 }
60 else
61 {
62 GNUNET_asprintf (&cred_str,
63 "%s.%s -> %s.%s | %s | %" SCNu64,
64 issuer_pkey,
65 cred->issuer_attribute,
66 subject_pkey,
67 cred->subject_attribute,
68 signature,
69 cred->expiration.abs_value_us);
70 }
71 GNUNET_free (subject_pkey);
72 GNUNET_free (issuer_pkey);
73 GNUNET_free (signature);
74
75 return cred_str;
76}
77
78struct GNUNET_ABD_Delegate *
79GNUNET_ABD_delegate_from_string (const char *s)
80{
81 struct GNUNET_ABD_Delegate *dele;
82 size_t enclen = (sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)) * 8;
83 if (enclen % 5 > 0)
84 enclen += 5 - enclen % 5;
85 enclen /= 5; /* 260/5 = 52 */
86 char subject_pkey[enclen + 1];
87 char issuer_pkey[enclen + 1];
88 char iss_attr[253 + 1];
89 // Needs to be initialized, in case of Type 1 credential (A.a <- B)
90 char sub_attr[253 + 1] = "";
91 char signature[256]; //TODO max payload size
92
93 struct GNUNET_CRYPTO_EcdsaSignature *sig;
94 struct GNUNET_TIME_Absolute etime_abs;
95
96 // If it's A.a <- B.b...
97 if (6 != SSCANF (s,
98 "%52s.%253s -> %52s.%253s | %s | %" SCNu64,
99 issuer_pkey,
100 iss_attr,
101 subject_pkey,
102 sub_attr,
103 signature,
104 &etime_abs.abs_value_us))
105 {
106 // Try if it's A.a <- B
107 if (5 != SSCANF (s,
108 "%52s.%253s -> %52s | %s | %" SCNu64,
109 issuer_pkey,
110 iss_attr,
111 subject_pkey,
112 signature,
113 &etime_abs.abs_value_us))
114 {
115 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
116 "Unable to parse DEL record string `%s'\n",
117 s);
118 return NULL;
119 }
120 }
121
122 // +1 for \0
123 int attr_len;
124 if (strcmp (sub_attr, "") == 0)
125 {
126 attr_len = strlen (iss_attr) + 1;
127 }
128 else
129 {
130 attr_len = strlen (iss_attr) + strlen (sub_attr) + 2;
131 }
132 dele = GNUNET_malloc (sizeof (struct GNUNET_ABD_Delegate) + attr_len);
133
134 char tmp_str[attr_len];
135 GNUNET_memcpy (tmp_str, iss_attr, strlen (iss_attr));
136 if (strcmp (sub_attr, "") != 0)
137 {
138 tmp_str[strlen (iss_attr)] = '\0';
139 GNUNET_memcpy (tmp_str + strlen (iss_attr) + 1,
140 sub_attr,
141 strlen (sub_attr));
142 }
143 tmp_str[attr_len - 1] = '\0';
144
145 GNUNET_CRYPTO_ecdsa_public_key_from_string (subject_pkey,
146 strlen (subject_pkey),
147 &dele->subject_key);
148 GNUNET_CRYPTO_ecdsa_public_key_from_string (issuer_pkey,
149 strlen (issuer_pkey),
150 &dele->issuer_key);
151 GNUNET_assert (sizeof (struct GNUNET_CRYPTO_EcdsaSignature) ==
152 GNUNET_STRINGS_base64_decode (signature,
153 strlen (signature),
154 (void **) &sig));
155 dele->signature = *sig;
156 dele->expiration = etime_abs;
157 GNUNET_free (sig);
158
159 GNUNET_memcpy (&dele[1], tmp_str, attr_len);
160
161 dele->issuer_attribute = (char *) &dele[1];
162 dele->issuer_attribute_len = strlen (iss_attr);
163 if (strcmp (sub_attr, "") == 0)
164 {
165 dele->subject_attribute = NULL;
166 dele->subject_attribute_len = 0;
167 }
168 else
169 {
170 dele->subject_attribute = (char *) &dele[1] + strlen (iss_attr) + 1;
171 dele->subject_attribute_len = strlen (sub_attr);
172 }
173
174 return dele;
175}
176
177/**
178 * Issue an attribute to a subject
179 *
180 * @param issuer the ego that should be used to issue the attribute
181 * @param subject the subject of the attribute
182 * @param iss_attr the name of the attribute
183 * @return handle to the queued request
184 */
185
186struct GNUNET_ABD_Delegate *
187GNUNET_ABD_delegate_issue (
188 const struct GNUNET_CRYPTO_EcdsaPrivateKey *issuer,
189 struct GNUNET_CRYPTO_EcdsaPublicKey *subject,
190 const char *iss_attr,
191 const char *sub_attr,
192 struct GNUNET_TIME_Absolute *expiration)
193{
194 struct DelegateEntry *del;
195 struct GNUNET_ABD_Delegate *dele;
196 size_t size;
197 int attr_len;
198
199 if (NULL == sub_attr)
200 {
201 // +1 for \0
202 attr_len = strlen (iss_attr) + 1;
203 }
204 else
205 {
206 // +2 for both strings need to be terminated with \0
207 attr_len = strlen (iss_attr) + strlen (sub_attr) + 2;
208 }
209 size = sizeof (struct DelegateEntry) + attr_len;
210
211 char tmp_str[attr_len];
212 GNUNET_memcpy (tmp_str, iss_attr, strlen (iss_attr));
213 if (NULL != sub_attr)
214 {
215 tmp_str[strlen (iss_attr)] = '\0';
216 GNUNET_memcpy (tmp_str + strlen (iss_attr) + 1,
217 sub_attr,
218 strlen (sub_attr));
219 }
220 tmp_str[attr_len - 1] = '\0';
221
222 del = GNUNET_malloc (size);
223 del->purpose.size =
224 htonl (size - sizeof (struct GNUNET_CRYPTO_EcdsaSignature));
225 del->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_DELEGATE);
226 GNUNET_CRYPTO_ecdsa_key_get_public (issuer, &del->issuer_key);
227 del->subject_key = *subject;
228 del->expiration = GNUNET_htonll (expiration->abs_value_us);
229 del->issuer_attribute_len = htonl (strlen (iss_attr) + 1);
230 if (NULL == sub_attr)
231 {
232 del->subject_attribute_len = htonl (0);
233 }
234 else
235 {
236 del->subject_attribute_len = htonl (strlen (sub_attr) + 1);
237 }
238
239 GNUNET_memcpy (&del[1], tmp_str, attr_len);
240
241 if (GNUNET_OK !=
242 GNUNET_CRYPTO_ecdsa_sign (issuer, &del->purpose, &del->signature))
243 {
244 GNUNET_break (0);
245 GNUNET_free (del);
246 return NULL;
247 }
248
249 dele = GNUNET_malloc (sizeof (struct GNUNET_ABD_Delegate) + attr_len);
250 dele->signature = del->signature;
251 dele->expiration = *expiration;
252 GNUNET_CRYPTO_ecdsa_key_get_public (issuer, &dele->issuer_key);
253
254 dele->subject_key = *subject;
255
256 // Copy the combined string at the part in the memory where the struct ends
257 GNUNET_memcpy (&dele[1], tmp_str, attr_len);
258
259 dele->issuer_attribute = (char *) &dele[1];
260 dele->issuer_attribute_len = strlen (iss_attr);
261 if (NULL == sub_attr)
262 {
263 dele->subject_attribute = NULL;
264 dele->subject_attribute_len = 0;
265 }
266 else
267 {
268 dele->subject_attribute = (char *) &dele[1] + strlen (iss_attr) + 1;
269 dele->subject_attribute_len = strlen (sub_attr);
270 }
271
272 GNUNET_free (del);
273 return dele;
274}