GNUnet 0.22.0
delegate_misc.c
Go to the documentation of this file.
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
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#include "delegate_misc.h"
36
37char *
39 const struct GNUNET_ABD_Delegate *cred)
40{
41 char *cred_str;
42 char *subject_pkey;
43 char *issuer_pkey;
44 char *signature;
45
48 GNUNET_STRINGS_base64_encode ((char *) &cred->signature,
49 sizeof (struct GNUNET_CRYPTO_Signature),
50 &signature);
51 if (0 == cred->subject_attribute_len)
52 {
53 GNUNET_asprintf (&cred_str,
54 "%s.%s -> %s | %s | %" SCNu64,
56 cred->issuer_attribute,
58 signature,
59 cred->expiration.abs_value_us);
60 }
61 else
62 {
63 GNUNET_asprintf (&cred_str,
64 "%s.%s -> %s.%s | %s | %" SCNu64,
66 cred->issuer_attribute,
68 cred->subject_attribute,
69 signature,
70 cred->expiration.abs_value_us);
71 }
74 GNUNET_free (signature);
75
76 return cred_str;
77}
78
79
82{
83 struct GNUNET_ABD_Delegate *dele;
84 size_t enclen = (sizeof (struct GNUNET_CRYPTO_PublicKey)) * 8;
85 if (enclen % 5 > 0)
86 enclen += 5 - enclen % 5;
87 enclen /= 5; /* 260/5 = 52 */
88 char subject_pkey[enclen + 1];
89 char issuer_pkey[enclen + 1];
90 char iss_attr[253 + 1];
91 // Needs to be initialized, in case of Type 1 credential (A.a <- B)
92 char sub_attr[253 + 1] = "";
93 char signature[256]; // TODO max payload size
94
95 struct GNUNET_CRYPTO_Signature *sig;
96 struct GNUNET_TIME_Absolute etime_abs;
97
98 // If it's A.a <- B.b...
99 if (6 != sscanf (s,
100 "%58s.%253s -> %58s.%253s | %s | %" SCNu64,
102 iss_attr,
104 sub_attr,
105 signature,
106 &etime_abs.abs_value_us))
107 {
108 // Try if it's A.a <- B
109 if (5 != sscanf (s,
110 "%58s.%253s -> %58s | %s | %" SCNu64,
112 iss_attr,
114 signature,
115 &etime_abs.abs_value_us))
116 {
118 "Unable to parse DEL record string `%s'\n",
119 s);
120 return NULL;
121 }
122 }
123
124 // +1 for \0
125 int attr_len;
126 if (strcmp (sub_attr, "") == 0)
127 {
128 attr_len = strlen (iss_attr) + 1;
129 }
130 else
131 {
132 attr_len = strlen (iss_attr) + strlen (sub_attr) + 2;
133 }
134 dele = GNUNET_malloc (sizeof (struct GNUNET_ABD_Delegate) + attr_len);
135
136 char tmp_str[attr_len];
137 GNUNET_memcpy (tmp_str, iss_attr, strlen (iss_attr));
138 if (strcmp (sub_attr, "") != 0)
139 {
140 tmp_str[strlen (iss_attr)] = '\0';
141 GNUNET_memcpy (tmp_str + strlen (iss_attr) + 1,
142 sub_attr,
143 strlen (sub_attr));
144 }
145 tmp_str[attr_len - 1] = '\0';
146
147 if (GNUNET_SYSERR ==
149 &dele->subject_key))
150 {
151 GNUNET_free (dele);
152 return NULL;
153 }
154 if (GNUNET_SYSERR ==
156 &dele->issuer_key))
157 {
158 GNUNET_free (dele);
159 return NULL;
160 }
161 GNUNET_assert (sizeof (struct GNUNET_CRYPTO_Signature) ==
163 strlen (signature),
164 (void **) &sig));
165 dele->signature = *sig;
166 dele->expiration = etime_abs;
167 GNUNET_free (sig);
168
169 GNUNET_memcpy (&dele[1], tmp_str, attr_len);
170
171 dele->issuer_attribute = (char *) &dele[1];
172 dele->issuer_attribute_len = strlen (iss_attr);
173 if (strcmp (sub_attr, "") == 0)
174 {
175 dele->subject_attribute = NULL;
176 dele->subject_attribute_len = 0;
177 }
178 else
179 {
180 dele->subject_attribute = (char *) &dele[1] + strlen (iss_attr) + 1;
181 dele->subject_attribute_len = strlen (sub_attr);
182 }
183
184 return dele;
185}
186
187
198struct GNUNET_ABD_Delegate *
200 const struct GNUNET_CRYPTO_PrivateKey *issuer,
202 const char *iss_attr,
203 const char *sub_attr,
205{
206 struct DelegateEntry *del;
207 struct GNUNET_ABD_Delegate *dele;
208 size_t size;
209 int attr_len;
210
211 if (NULL == sub_attr)
212 {
213 // +1 for \0
214 attr_len = strlen (iss_attr) + 1;
215 }
216 else
217 {
218 // +2 for both strings need to be terminated with \0
219 attr_len = strlen (iss_attr) + strlen (sub_attr) + 2;
220 }
221 size = sizeof (struct DelegateEntry) + attr_len;
222
223 char tmp_str[attr_len];
224 GNUNET_memcpy (tmp_str, iss_attr, strlen (iss_attr));
225 if (NULL != sub_attr)
226 {
227 tmp_str[strlen (iss_attr)] = '\0';
228 GNUNET_memcpy (tmp_str + strlen (iss_attr) + 1,
229 sub_attr,
230 strlen (sub_attr));
231 }
232 tmp_str[attr_len - 1] = '\0';
233
235 del->purpose.size =
236 htonl (size - sizeof (struct GNUNET_CRYPTO_Signature));
237 del->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_DELEGATE);
238 GNUNET_CRYPTO_key_get_public (issuer, &del->issuer_key);
239 del->subject_key = *subject;
240 del->expiration = GNUNET_htonll (expiration->abs_value_us);
241 del->issuer_attribute_len = htonl (strlen (iss_attr) + 1);
242 if (NULL == sub_attr)
243 {
244 del->subject_attribute_len = htonl (0);
245 }
246 else
247 {
248 del->subject_attribute_len = htonl (strlen (sub_attr) + 1);
249 }
250
251 GNUNET_memcpy (&del[1], tmp_str, attr_len);
252
253 GNUNET_CRYPTO_sign_ (issuer, &del->purpose, &del->signature);
254
255 dele = GNUNET_malloc (sizeof (struct GNUNET_ABD_Delegate) + attr_len);
256 dele->signature = del->signature;
257 dele->expiration = *expiration;
259
260 dele->subject_key = *subject;
261
262 // Copy the combined string at the part in the memory where the struct ends
263 GNUNET_memcpy (&dele[1], tmp_str, attr_len);
264
265 dele->issuer_attribute = (char *) &dele[1];
266 dele->issuer_attribute_len = strlen (iss_attr);
267 if (NULL == sub_attr)
268 {
269 dele->subject_attribute = NULL;
270 dele->subject_attribute_len = 0;
271 }
272 else
273 {
274 dele->subject_attribute = (char *) &dele[1] + strlen (iss_attr) + 1;
275 dele->subject_attribute_len = strlen (sub_attr);
276 }
277
279 return dele;
280}
IPC messages between ABD API and ABD service.
char * GNUNET_ABD_delegate_to_string(const struct GNUNET_ABD_Delegate *cred)
Definition: delegate_misc.c:38
struct GNUNET_ABD_Delegate * GNUNET_ABD_delegate_from_string(const char *s)
Definition: delegate_misc.c:81
Delegate helper functions.
struct GNUNET_CRYPTO_PublicKey issuer_pkey
Issuer key.
Definition: gnunet-abd.c:107
static char * subject
Subject pubkey string.
Definition: gnunet-abd.c:87
struct GNUNET_CRYPTO_PublicKey subject_pkey
Subject key.
Definition: gnunet-abd.c:102
static gnutls_certificate_credentials_t cred
The credential.
static struct GNUNET_TIME_Relative expiration
User supplied expiration value.
static int del
Desired action is to remove a record.
API to the Credential service.
struct GNUNET_ABD_Delegate * GNUNET_ABD_delegate_issue(const struct GNUNET_CRYPTO_PrivateKey *issuer, struct GNUNET_CRYPTO_PublicKey *subject, const char *iss_attr, const char *sub_attr, struct GNUNET_TIME_Absolute *expiration)
Issue an attribute to a subject.
#define GNUNET_log(kind,...)
char * GNUNET_CRYPTO_public_key_to_string(const struct GNUNET_CRYPTO_PublicKey *key)
Creates a (Base32) string representation of the public key.
Definition: crypto_pkey.c:379
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_key_get_public(const struct GNUNET_CRYPTO_PrivateKey *privkey, struct GNUNET_CRYPTO_PublicKey *key)
Retrieves the public key representation of a private key.
Definition: crypto_pkey.c:430
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_sign_(const struct GNUNET_CRYPTO_PrivateKey *priv, const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose, struct GNUNET_CRYPTO_Signature *sig)
Sign a given block.
Definition: crypto_pkey.c:293
uint64_t GNUNET_htonll(uint64_t n)
Convert unsigned 64-bit integer to network byte order.
Definition: common_endian.c:37
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_public_key_from_string(const char *str, struct GNUNET_CRYPTO_PublicKey *key)
Parses a (Base32) string representation of the public key.
Definition: crypto_pkey.c:399
@ GNUNET_SYSERR
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
@ GNUNET_ERROR_TYPE_ERROR
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
size_t GNUNET_STRINGS_base64_decode(const char *data, size_t len, void **output)
Decode from Base64.
Definition: strings.c:1724
size_t GNUNET_STRINGS_base64_encode(const void *in, size_t len, char **output)
Encode into Base64.
Definition: strings.c:1622
static unsigned int size
Size of the "table".
Definition: peer.c:68
#define GNUNET_SIGNATURE_PURPOSE_DELEGATE
Signature for a GNUnet credential (Reclaim)
const char * subject_attribute
The subject attribute.
const char * issuer_attribute
The issuer attribute.
uint32_t issuer_attribute_len
Length of the issuer attribute.
uint32_t subject_attribute_len
Length of the subject attribute.
struct GNUNET_CRYPTO_Signature signature
Signature of this credential.
struct GNUNET_CRYPTO_PublicKey issuer_key
The issuer of the credential.
struct GNUNET_TIME_Absolute expiration
Expiration of this credential.
struct GNUNET_CRYPTO_PublicKey subject_key
Public key of the subject this credential was issued to.
A private key for an identity as per LSD0001.
An identity key as per LSD0001.
An identity signature as per LSD0001.
Time for absolute times used by GNUnet, in microseconds.
uint64_t abs_value_us
The actual value.