diff options
Diffstat (limited to 'src/contrib/service/abd/delegate_misc.c')
-rw-r--r-- | src/contrib/service/abd/delegate_misc.c | 279 |
1 files changed, 279 insertions, 0 deletions
diff --git a/src/contrib/service/abd/delegate_misc.c b/src/contrib/service/abd/delegate_misc.c new file mode 100644 index 000000000..446c0b8d2 --- /dev/null +++ b/src/contrib/service/abd/delegate_misc.c | |||
@@ -0,0 +1,279 @@ | |||
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 | |||
36 | char * | ||
37 | GNUNET_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_public_key_to_string (&cred->subject_key); | ||
46 | issuer_pkey = GNUNET_CRYPTO_public_key_to_string (&cred->issuer_key); | ||
47 | GNUNET_STRINGS_base64_encode ((char *) &cred->signature, | ||
48 | sizeof (struct GNUNET_CRYPTO_Signature), | ||
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 | |||
78 | |||
79 | struct GNUNET_ABD_Delegate * | ||
80 | GNUNET_ABD_delegate_from_string (const char *s) | ||
81 | { | ||
82 | struct GNUNET_ABD_Delegate *dele; | ||
83 | size_t enclen = (sizeof (struct GNUNET_CRYPTO_PublicKey)) * 8; | ||
84 | if (enclen % 5 > 0) | ||
85 | enclen += 5 - enclen % 5; | ||
86 | enclen /= 5; /* 260/5 = 52 */ | ||
87 | char subject_pkey[enclen + 1]; | ||
88 | char issuer_pkey[enclen + 1]; | ||
89 | char iss_attr[253 + 1]; | ||
90 | // Needs to be initialized, in case of Type 1 credential (A.a <- B) | ||
91 | char sub_attr[253 + 1] = ""; | ||
92 | char signature[256]; // TODO max payload size | ||
93 | |||
94 | struct GNUNET_CRYPTO_Signature *sig; | ||
95 | struct GNUNET_TIME_Absolute etime_abs; | ||
96 | |||
97 | // If it's A.a <- B.b... | ||
98 | if (6 != sscanf (s, | ||
99 | "%58s.%253s -> %58s.%253s | %s | %" SCNu64, | ||
100 | issuer_pkey, | ||
101 | iss_attr, | ||
102 | subject_pkey, | ||
103 | sub_attr, | ||
104 | signature, | ||
105 | &etime_abs.abs_value_us)) | ||
106 | { | ||
107 | // Try if it's A.a <- B | ||
108 | if (5 != sscanf (s, | ||
109 | "%58s.%253s -> %58s | %s | %" SCNu64, | ||
110 | issuer_pkey, | ||
111 | iss_attr, | ||
112 | subject_pkey, | ||
113 | signature, | ||
114 | &etime_abs.abs_value_us)) | ||
115 | { | ||
116 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
117 | "Unable to parse DEL record string `%s'\n", | ||
118 | s); | ||
119 | return NULL; | ||
120 | } | ||
121 | } | ||
122 | |||
123 | // +1 for \0 | ||
124 | int attr_len; | ||
125 | if (strcmp (sub_attr, "") == 0) | ||
126 | { | ||
127 | attr_len = strlen (iss_attr) + 1; | ||
128 | } | ||
129 | else | ||
130 | { | ||
131 | attr_len = strlen (iss_attr) + strlen (sub_attr) + 2; | ||
132 | } | ||
133 | dele = GNUNET_malloc (sizeof (struct GNUNET_ABD_Delegate) + attr_len); | ||
134 | |||
135 | char tmp_str[attr_len]; | ||
136 | GNUNET_memcpy (tmp_str, iss_attr, strlen (iss_attr)); | ||
137 | if (strcmp (sub_attr, "") != 0) | ||
138 | { | ||
139 | tmp_str[strlen (iss_attr)] = '\0'; | ||
140 | GNUNET_memcpy (tmp_str + strlen (iss_attr) + 1, | ||
141 | sub_attr, | ||
142 | strlen (sub_attr)); | ||
143 | } | ||
144 | tmp_str[attr_len - 1] = '\0'; | ||
145 | |||
146 | if (GNUNET_SYSERR == | ||
147 | GNUNET_CRYPTO_public_key_from_string (subject_pkey, | ||
148 | &dele->subject_key)) | ||
149 | { | ||
150 | GNUNET_free (dele); | ||
151 | return NULL; | ||
152 | } | ||
153 | if (GNUNET_SYSERR == | ||
154 | GNUNET_CRYPTO_public_key_from_string (issuer_pkey, | ||
155 | &dele->issuer_key)) | ||
156 | { | ||
157 | GNUNET_free (dele); | ||
158 | return NULL; | ||
159 | } | ||
160 | GNUNET_assert (sizeof (struct GNUNET_CRYPTO_Signature) == | ||
161 | GNUNET_STRINGS_base64_decode (signature, | ||
162 | strlen (signature), | ||
163 | (void **) &sig)); | ||
164 | dele->signature = *sig; | ||
165 | dele->expiration = etime_abs; | ||
166 | GNUNET_free (sig); | ||
167 | |||
168 | GNUNET_memcpy (&dele[1], tmp_str, attr_len); | ||
169 | |||
170 | dele->issuer_attribute = (char *) &dele[1]; | ||
171 | dele->issuer_attribute_len = strlen (iss_attr); | ||
172 | if (strcmp (sub_attr, "") == 0) | ||
173 | { | ||
174 | dele->subject_attribute = NULL; | ||
175 | dele->subject_attribute_len = 0; | ||
176 | } | ||
177 | else | ||
178 | { | ||
179 | dele->subject_attribute = (char *) &dele[1] + strlen (iss_attr) + 1; | ||
180 | dele->subject_attribute_len = strlen (sub_attr); | ||
181 | } | ||
182 | |||
183 | return dele; | ||
184 | } | ||
185 | |||
186 | |||
187 | /** | ||
188 | * Issue an attribute to a subject | ||
189 | * | ||
190 | * @param issuer the ego that should be used to issue the attribute | ||
191 | * @param subject the subject of the attribute | ||
192 | * @param iss_attr the name of the attribute | ||
193 | * @param expiration the TTL of the credential | ||
194 | * @return handle to the queued request | ||
195 | */ | ||
196 | |||
197 | struct GNUNET_ABD_Delegate * | ||
198 | GNUNET_ABD_delegate_issue ( | ||
199 | const struct GNUNET_CRYPTO_PrivateKey *issuer, | ||
200 | struct GNUNET_CRYPTO_PublicKey *subject, | ||
201 | const char *iss_attr, | ||
202 | const char *sub_attr, | ||
203 | struct GNUNET_TIME_Absolute *expiration) | ||
204 | { | ||
205 | struct DelegateEntry *del; | ||
206 | struct GNUNET_ABD_Delegate *dele; | ||
207 | size_t size; | ||
208 | int attr_len; | ||
209 | |||
210 | if (NULL == sub_attr) | ||
211 | { | ||
212 | // +1 for \0 | ||
213 | attr_len = strlen (iss_attr) + 1; | ||
214 | } | ||
215 | else | ||
216 | { | ||
217 | // +2 for both strings need to be terminated with \0 | ||
218 | attr_len = strlen (iss_attr) + strlen (sub_attr) + 2; | ||
219 | } | ||
220 | size = sizeof (struct DelegateEntry) + attr_len; | ||
221 | |||
222 | char tmp_str[attr_len]; | ||
223 | GNUNET_memcpy (tmp_str, iss_attr, strlen (iss_attr)); | ||
224 | if (NULL != sub_attr) | ||
225 | { | ||
226 | tmp_str[strlen (iss_attr)] = '\0'; | ||
227 | GNUNET_memcpy (tmp_str + strlen (iss_attr) + 1, | ||
228 | sub_attr, | ||
229 | strlen (sub_attr)); | ||
230 | } | ||
231 | tmp_str[attr_len - 1] = '\0'; | ||
232 | |||
233 | del = GNUNET_malloc (size); | ||
234 | del->purpose.size = | ||
235 | htonl (size - sizeof (struct GNUNET_CRYPTO_Signature)); | ||
236 | del->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_DELEGATE); | ||
237 | GNUNET_CRYPTO_key_get_public (issuer, &del->issuer_key); | ||
238 | del->subject_key = *subject; | ||
239 | del->expiration = GNUNET_htonll (expiration->abs_value_us); | ||
240 | del->issuer_attribute_len = htonl (strlen (iss_attr) + 1); | ||
241 | if (NULL == sub_attr) | ||
242 | { | ||
243 | del->subject_attribute_len = htonl (0); | ||
244 | } | ||
245 | else | ||
246 | { | ||
247 | del->subject_attribute_len = htonl (strlen (sub_attr) + 1); | ||
248 | } | ||
249 | |||
250 | GNUNET_memcpy (&del[1], tmp_str, attr_len); | ||
251 | |||
252 | GNUNET_CRYPTO_sign_ (issuer, &del->purpose, &del->signature); | ||
253 | |||
254 | dele = GNUNET_malloc (sizeof (struct GNUNET_ABD_Delegate) + attr_len); | ||
255 | dele->signature = del->signature; | ||
256 | dele->expiration = *expiration; | ||
257 | GNUNET_CRYPTO_key_get_public (issuer, &dele->issuer_key); | ||
258 | |||
259 | dele->subject_key = *subject; | ||
260 | |||
261 | // Copy the combined string at the part in the memory where the struct ends | ||
262 | GNUNET_memcpy (&dele[1], tmp_str, attr_len); | ||
263 | |||
264 | dele->issuer_attribute = (char *) &dele[1]; | ||
265 | dele->issuer_attribute_len = strlen (iss_attr); | ||
266 | if (NULL == sub_attr) | ||
267 | { | ||
268 | dele->subject_attribute = NULL; | ||
269 | dele->subject_attribute_len = 0; | ||
270 | } | ||
271 | else | ||
272 | { | ||
273 | dele->subject_attribute = (char *) &dele[1] + strlen (iss_attr) + 1; | ||
274 | dele->subject_attribute_len = strlen (sub_attr); | ||
275 | } | ||
276 | |||
277 | GNUNET_free (del); | ||
278 | return dele; | ||
279 | } | ||