2 This file is part of GNUnet
3 Copyright (C) 2013 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file credential/plugin_gnsrecord_credential.c
23 * @brief gnsrecord plugin to provide the API for CREDENTIAL records
24 * @author Adnan Husain
28 #include "gnunet_util_lib.h"
29 #include "gnunet_gnsrecord_lib.h"
30 #include "gnunet_credential_service.h"
31 #include "gnunet_gnsrecord_plugin.h"
32 #include "gnunet_signatures.h"
36 * Convert the 'value' of a record to a string.
38 * @param cls closure, unused
39 * @param type type of the record
40 * @param data value in binary encoding
41 * @param data_size number of bytes in @a data
42 * @return NULL on error, otherwise human-readable representation of the value
45 credential_value_to_string (void *cls,
55 case GNUNET_GNSRECORD_TYPE_ATTRIBUTE:
57 struct GNUNET_CREDENTIAL_AttributeRecordData attr;
61 if (data_size < sizeof (struct GNUNET_CREDENTIAL_AttributeRecordData))
62 return NULL; /* malformed */
67 subject_pkey = GNUNET_CRYPTO_ecdsa_public_key_to_string (&attr.subject_key);
68 GNUNET_asprintf (&attr_str,
71 &cdata[sizeof (attr)]);
72 GNUNET_free (subject_pkey);
75 case GNUNET_GNSRECORD_TYPE_CREDENTIAL:
77 struct GNUNET_CREDENTIAL_CredentialRecordData cred;
78 struct GNUNET_TIME_Absolute etime_abs;
83 const char *expiration;
86 if (data_size < sizeof (struct GNUNET_CREDENTIAL_CredentialRecordData))
87 return NULL; /* malformed */
92 subject_pkey = GNUNET_CRYPTO_ecdsa_public_key_to_string (&cred.subject_key);
93 issuer_pkey = GNUNET_CRYPTO_ecdsa_public_key_to_string (&cred.issuer_key);
94 etime_abs.abs_value_us = GNUNET_ntohll(cred.expiration);
95 expiration = GNUNET_STRINGS_absolute_time_to_string (etime_abs);
96 GNUNET_STRINGS_base64_encode ((char*)&cred.sig,
97 sizeof (struct GNUNET_CRYPTO_EcdsaSignature),
99 GNUNET_asprintf (&cred_str,
100 "%s.%s -> %s | %s | %s",
102 &cdata[sizeof (cred)],
106 GNUNET_free (subject_pkey);
107 GNUNET_free (issuer_pkey);
108 GNUNET_free (signature);
118 * Convert human-readable version of a 'value' of a record to the binary
121 * @param cls closure, unused
122 * @param type type of the record
123 * @param s human-readable string
124 * @param data set to value in binary encoding (will be allocated)
125 * @param data_size set to number of bytes in @a data
126 * @return #GNUNET_OK on success
129 credential_string_to_value (void *cls,
136 return GNUNET_SYSERR;
139 case GNUNET_GNSRECORD_TYPE_CREDENTIAL:
141 struct GNUNET_CREDENTIAL_CredentialRecordData *cred;
143 size_t enclen = (sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)) * 8;
145 enclen += 5 - enclen % 5;
146 enclen /= 5; /* 260/5 = 52 */
147 char subject_pkey[enclen + 1];
148 char issuer_pkey[enclen + 1];
150 char signature[128]; //TODO max payload size
151 char expiration[256];
153 struct GNUNET_CRYPTO_EcdsaSignature *sig;
154 struct GNUNET_TIME_Absolute etime_abs;
157 "%52s.%253s -> %52s | %s | %255[0-9a-zA-Z: ]",
164 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
165 _("Unable to parse CRED record string `%s'\n"),
167 return GNUNET_SYSERR;
169 *data_size = sizeof (struct GNUNET_CREDENTIAL_CredentialRecordData) + strlen (name) + 1;
170 *data = cred = GNUNET_malloc (*data_size);
171 GNUNET_CRYPTO_ecdsa_public_key_from_string (subject_pkey,
172 strlen (subject_pkey),
174 GNUNET_CRYPTO_ecdsa_public_key_from_string (issuer_pkey,
175 strlen (issuer_pkey),
177 GNUNET_STRINGS_fancy_time_to_absolute (expiration,
179 GNUNET_STRINGS_base64_decode (signature,
183 cred->expiration = GNUNET_htonll (etime_abs.abs_value_us);
184 cred->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_CREDENTIAL);
185 cred->purpose.size = htonl (strlen (name) + 1 + sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
186 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) + sizeof (uint64_t));
188 GNUNET_memcpy (&cred[1],
196 return GNUNET_SYSERR;
202 * Mapping of record type numbers to human-readable
209 { "CRED", GNUNET_GNSRECORD_TYPE_CREDENTIAL },
215 * Convert a type name (i.e. "AAAA") to the corresponding number.
217 * @param cls closure, unused
218 * @param gns_typename name to convert
219 * @return corresponding number, UINT32_MAX on error
222 credential_typename_to_number (void *cls,
223 const char *gns_typename)
228 while ( (name_map[i].name != NULL) &&
229 (0 != strcasecmp (gns_typename, name_map[i].name)) )
231 return name_map[i].number;
236 * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
238 * @param cls closure, unused
239 * @param type number of a type to convert
240 * @return corresponding typestring, NULL on error
243 credential_number_to_typename (void *cls,
249 while ( (name_map[i].name != NULL) &&
250 (type != name_map[i].number) )
252 return name_map[i].name;
257 * Entry point for the plugin.
260 * @return the exported block API
263 libgnunet_plugin_gnsrecord_credential_init (void *cls)
265 struct GNUNET_GNSRECORD_PluginFunctions *api;
267 api = GNUNET_new (struct GNUNET_GNSRECORD_PluginFunctions);
268 api->value_to_string = &credential_value_to_string;
269 api->string_to_value = &credential_string_to_value;
270 api->typename_to_number = &credential_typename_to_number;
271 api->number_to_typename = &credential_number_to_typename;
277 * Exit point from the plugin.
279 * @param cls the return value from #libgnunet_plugin_block_test_init
283 libgnunet_plugin_gnsrecord_credential_done (void *cls)
285 struct GNUNET_GNSRECORD_PluginFunctions *api = cls;
291 /* end of plugin_gnsrecord_credential.c */