GNUnet  0.10.x
plugin_gnsrecord_credential.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet
3  Copyright (C) 2013 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 
26 #include "platform.h"
27 
28 #include "gnunet_util_lib.h"
29 
30 #include "credential_misc.h"
33 #include "gnunet_gnsrecord_lib.h"
35 #include "gnunet_signatures.h"
45 static char *
46 credential_value_to_string(void *cls, uint32_t type, const void *data,
47  size_t data_size)
48 {
49  const char *cdata;
50 
51  switch (type)
52  {
55  char *attr_str;
56  char *subject_pkey;
57  char *tmp_str;
58  int i;
59  if (data_size < sizeof(struct GNUNET_CREDENTIAL_DelegationRecord))
60  return NULL; /* malformed */
61  GNUNET_memcpy(&sets, data, sizeof(sets));
62  cdata = data;
63  struct GNUNET_CREDENTIAL_DelegationSet set[ntohl(sets.set_count)];
65  GNUNET_ntohll(sets.data_size), &cdata[sizeof(sets)],
66  ntohl(sets.set_count), set))
67  return NULL;
68 
69  for (i = 0; i < ntohl(sets.set_count); i++)
70  {
71  subject_pkey =
73  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%d len attr\n",
74  set[i].subject_attribute_len);
75  if (0 == set[i].subject_attribute_len)
76  {
77  if (0 == i)
78  {
79  GNUNET_asprintf(&attr_str, "%s", subject_pkey);
80  }
81  else
82  {
83  GNUNET_asprintf(&tmp_str, "%s,%s", attr_str, subject_pkey);
84  GNUNET_free(attr_str);
85  attr_str = tmp_str;
86  }
87  }
88  else
89  {
90  if (0 == i)
91  {
92  GNUNET_asprintf(&attr_str, "%s %s", subject_pkey,
93  set[i].subject_attribute);
94  }
95  else
96  {
97  GNUNET_asprintf(&tmp_str, "%s,%s %s", attr_str, subject_pkey,
98  set[i].subject_attribute);
99  GNUNET_free(attr_str);
100  attr_str = tmp_str;
101  }
102  }
103  GNUNET_free(subject_pkey);
104  }
105  return attr_str;
106  }
107 
109  struct GNUNET_CREDENTIAL_Credential *cred;
110  char *cred_str;
111 
112  cred = GNUNET_CREDENTIAL_credential_deserialize(data, data_size);
114  GNUNET_free(cred);
115  return cred_str;
116  }
117 
119  return GNUNET_strndup(data, data_size);
120  }
121 
122  default:
123  return NULL;
124  }
125 }
126 
127 
139 static int
140 credential_string_to_value(void *cls, uint32_t type, const char *s,
141  void **data, size_t *data_size)
142 {
143  if (NULL == s)
144  return GNUNET_SYSERR;
145  switch (type)
146  {
149  char attr_str[253 + 1];
150  char subject_pkey[52 + 1];
151  char *token;
152  char *tmp_str;
153  int matches = 0;
154  int entries;
155  size_t tmp_data_size;
156  int i;
157 
158  tmp_str = GNUNET_strdup(s);
159  token = strtok(tmp_str, ",");
160  entries = 0;
161  tmp_data_size = 0;
162  *data_size = sizeof(struct GNUNET_CREDENTIAL_DelegationRecord);
163  while (NULL != token)
164  {
165  matches = sscanf(token, "%s %s", subject_pkey, attr_str);
166  if (0 == matches)
167  {
169  _("Unable to parse ATTR record string `%s'\n"), s);
170  GNUNET_free(tmp_str);
171  return GNUNET_SYSERR;
172  }
173  if (1 == matches)
174  {
175  tmp_data_size += sizeof(struct GNUNET_CREDENTIAL_DelegationRecordSet);
176  }
177  else if (2 == matches)
178  {
179  tmp_data_size += sizeof(struct GNUNET_CREDENTIAL_DelegationRecordSet) +
180  strlen(attr_str) + 1;
181  }
182  entries++;
183  token = strtok(NULL, ",");
184  }
185  GNUNET_free(tmp_str);
186  tmp_str = GNUNET_strdup(s);
187  token = strtok(tmp_str, ",");
188  if (NULL == token)
189  {
190  GNUNET_free(tmp_str);
191  GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Malformed string %s\n", s);
192  return GNUNET_SYSERR;
193  }
194  struct GNUNET_CREDENTIAL_DelegationSet set[entries];
195  memset(set, 0, sizeof(struct GNUNET_CREDENTIAL_DelegationSet) * entries);
196  for (i = 0; i < entries; i++)
197  {
198  matches = sscanf(token, "%s %s", subject_pkey, attr_str);
200  subject_pkey, strlen(subject_pkey), &set[i].subject_key);
201  if (2 == matches)
202  {
203  set[i].subject_attribute_len = strlen(attr_str) + 1;
204  set[i].subject_attribute = GNUNET_strdup(attr_str);
205  }
206  token = strtok(NULL, ",");
207  }
208  tmp_data_size = GNUNET_CREDENTIAL_delegation_set_get_size(entries, set);
209 
210  if (-1 == tmp_data_size)
211  {
212  GNUNET_free(tmp_str);
213  return GNUNET_SYSERR;
214  }
215  *data_size += tmp_data_size;
216  *data = sets = GNUNET_malloc(*data_size);
217  GNUNET_CREDENTIAL_delegation_set_serialize(entries, set, tmp_data_size,
218  (char *)&sets[1]);
219  for (i = 0; i < entries; i++)
220  {
221  if (0 != set[i].subject_attribute_len)
222  GNUNET_free((char *)set[i].subject_attribute);
223  }
224  sets->set_count = htonl(entries);
225  sets->data_size = GNUNET_htonll(tmp_data_size);
226 
227  GNUNET_free(tmp_str);
228  return GNUNET_OK;
229  }
230 
232  struct GNUNET_CREDENTIAL_Credential *cred;
234 
235  *data_size = GNUNET_CREDENTIAL_credential_serialize(cred, (char **)data);
236  return GNUNET_OK;
237  }
238 
240  *data_size = strlen(s);
241  *data = GNUNET_strdup(s);
242  return GNUNET_OK;
243  }
244 
245  default:
246  return GNUNET_SYSERR;
247  }
248 }
249 
250 
255 static struct {
256  const char *name;
257  uint32_t number;
258 } name_map[] = { { "CRED", GNUNET_GNSRECORD_TYPE_CREDENTIAL },
260  { "POLICY", GNUNET_GNSRECORD_TYPE_POLICY },
261  { NULL, UINT32_MAX } };
262 
263 
271 static uint32_t
272 credential_typename_to_number(void *cls, const char *gns_typename)
273 {
274  unsigned int i;
275 
276  i = 0;
277  while ((name_map[i].name != NULL) &&
278  (0 != strcasecmp(gns_typename, name_map[i].name)))
279  i++;
280  return name_map[i].number;
281 }
282 
283 
291 static const char *
293 {
294  unsigned int i;
295 
296  i = 0;
297  while ((name_map[i].name != NULL) && (type != name_map[i].number))
298  i++;
299  return name_map[i].name;
300 }
301 
302 
309 void *
311 {
313 
319  return api;
320 }
321 
322 
329 void *
331 {
333 
334  GNUNET_free(api);
335  return NULL;
336 }
337 
338 /* end of plugin_gnsrecord_credential.c */
GNUNET_GNSRECORD_TypenameToNumberFunction typename_to_number
Typename to number.
struct GNUNET_CREDENTIAL_Credential * GNUNET_CREDENTIAL_credential_from_string(const char *s)
GNUNET_GNSRECORD_ValueToStringFunction value_to_string
Conversion to string.
GNUNET_GNSRECORD_NumberToTypenameFunction number_to_typename
Number to typename.
int GNUNET_CREDENTIAL_credential_serialize(struct GNUNET_CREDENTIAL_Credential *cred, char **data)
ssize_t GNUNET_CREDENTIAL_delegation_set_serialize(unsigned int d_count, const struct GNUNET_CREDENTIAL_DelegationSet *dsr, size_t dest_size, char *dest)
Serizalize the given delegation chain entries and credential.
static uint32_t credential_typename_to_number(void *cls, const char *gns_typename)
Convert a type name (i.e.
#define GNUNET_GNSRECORD_TYPE_POLICY
Record type for policies.
static char * subject_key
Subject pubkey string.
GNUNET_GNSRECORD_StringToValueFunction string_to_value
Conversion to binary.
static int credential_string_to_value(void *cls, uint32_t type, const char *s, void **data, size_t *data_size)
Convert human-readable version of a &#39;value&#39; of a record to the binary representation.
void * libgnunet_plugin_gnsrecord_credential_done(void *cls)
Exit point from the plugin.
void * cls
Closure for all of the callbacks.
API to serialize and deserialize delegation chains and credentials.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
static const char * credential_number_to_typename(void *cls, uint32_t type)
Convert a type number (i.e.
int GNUNET_CREDENTIAL_delegation_set_deserialize(size_t len, const char *src, unsigned int d_count, struct GNUNET_CREDENTIAL_DelegationSet *dsr)
Deserialize the given destination.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_new(type)
Allocate a struct or union of the given type.
struct GNUNET_CRYPTO_EcdsaPublicKey subject_pkey
Subject key.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
#define GNUNET_GNSRECORD_TYPE_CREDENTIAL
65546 reserved
int GNUNET_asprintf(char **buf, const char *format,...)
Like asprintf, just portable.
struct ListEntry * entries
List of peers in the list.
size_t GNUNET_CREDENTIAL_delegation_set_get_size(unsigned int ds_count, const struct GNUNET_CREDENTIAL_DelegationSet *dsr)
Calculate how many bytes we will need to serialize the given delegation chain.
static struct @10 name_map[]
Mapping of record type numbers to human-readable record type names.
const char * subject_attribute
The subject attribute.
char * GNUNET_CRYPTO_ecdsa_public_key_to_string(const struct GNUNET_CRYPTO_EcdsaPublicKey *pub)
Convert a public key to a string.
Definition: crypto_ecc.c:334
struct GNUNET_CREDENTIAL_Credential * GNUNET_CREDENTIAL_credential_deserialize(const char *data, size_t data_size)
uint64_t GNUNET_htonll(uint64_t n)
Convert unsigned 64-bit integer to network byte order.
Definition: common_endian.c:35
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
char * GNUNET_CREDENTIAL_credential_to_string(const struct GNUNET_CREDENTIAL_Credential *cred)
#define GNUNET_strndup(a, length)
Wrapper around GNUNET_xstrndup_.
int GNUNET_CRYPTO_ecdsa_public_key_from_string(const char *enc, size_t enclen, struct GNUNET_CRYPTO_EcdsaPublicKey *pub)
Convert a string representing a public key to a public key.
Definition: crypto_ecc.c:468
Each plugin is required to return a pointer to a struct of this type as the return value from its ent...
The attribute delegation record.
#define GNUNET_GNSRECORD_TYPE_ATTRIBUTE
Record type for reverse lookups.
Credential helper functions.
#define GNUNET_log(kind,...)
const char * name
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model
uint32_t number
uint32_t data
The data value.
static size_t data_size
Number of bytes in data.
The attribute delegation record.
#define GNUNET_malloc(size)
Wrapper around malloc.
The attribute delegation record.
static char * credential_value_to_string(void *cls, uint32_t type, const void *data, size_t data_size)
Convert the &#39;value&#39; of a record to a string.
uint64_t GNUNET_ntohll(uint64_t n)
Convert unsigned 64-bit integer to host byte order.
Definition: common_endian.c:48
#define GNUNET_free(ptr)
Wrapper around free.
void * libgnunet_plugin_gnsrecord_credential_init(void *cls)
Entry point for the plugin.