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 if (data_size == sizeof (struct GNUNET_CREDENTIAL_AttributeRecordData))
72 GNUNET_asprintf (&attr_str,
75 &cdata[sizeof (attr)]);
77 GNUNET_free (subject_pkey);
80 case GNUNET_GNSRECORD_TYPE_CREDENTIAL:
82 struct GNUNET_CREDENTIAL_CredentialRecordData cred;
83 struct GNUNET_TIME_Absolute etime_abs;
88 const char *expiration;
91 if (data_size < sizeof (struct GNUNET_CREDENTIAL_CredentialRecordData))
92 return NULL; /* malformed */
97 subject_pkey = GNUNET_CRYPTO_ecdsa_public_key_to_string (&cred.subject_key);
98 issuer_pkey = GNUNET_CRYPTO_ecdsa_public_key_to_string (&cred.issuer_key);
99 etime_abs.abs_value_us = GNUNET_ntohll(cred.expiration);
100 expiration = GNUNET_STRINGS_absolute_time_to_string (etime_abs);
101 GNUNET_STRINGS_base64_encode ((char*)&cred.signature,
102 sizeof (struct GNUNET_CRYPTO_EcdsaSignature),
104 GNUNET_asprintf (&cred_str,
105 "%s.%s -> %s | %s | %s",
107 &cdata[sizeof (cred)],
111 GNUNET_free (subject_pkey);
112 GNUNET_free (issuer_pkey);
113 GNUNET_free (signature);
123 * Convert human-readable version of a 'value' of a record to the binary
126 * @param cls closure, unused
127 * @param type type of the record
128 * @param s human-readable string
129 * @param data set to value in binary encoding (will be allocated)
130 * @param data_size set to number of bytes in @a data
131 * @return #GNUNET_OK on success
134 credential_string_to_value (void *cls,
141 return GNUNET_SYSERR;
144 case GNUNET_GNSRECORD_TYPE_ATTRIBUTE:
146 struct GNUNET_CREDENTIAL_AttributeRecordData *attr;
147 char attr_str[253 + 1];
148 char subject_pkey[52 + 1];
156 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
157 _("Unable to parse ATTR record string `%s'\n"),
159 return GNUNET_SYSERR;
163 *data_size = sizeof (struct GNUNET_CREDENTIAL_AttributeRecordData);
164 } else if (2 == matches) {
165 *data_size = sizeof (struct GNUNET_CREDENTIAL_AttributeRecordData) + strlen (attr_str) + 1;
167 *data = attr = GNUNET_malloc (*data_size);
168 GNUNET_CRYPTO_ecdsa_public_key_from_string (subject_pkey,
169 strlen (subject_pkey),
171 if (NULL != attr_str)
172 GNUNET_memcpy (&attr[1],
179 case GNUNET_GNSRECORD_TYPE_CREDENTIAL:
181 struct GNUNET_CREDENTIAL_CredentialRecordData *cred;
183 size_t enclen = (sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)) * 8;
185 enclen += 5 - enclen % 5;
186 enclen /= 5; /* 260/5 = 52 */
187 char subject_pkey[enclen + 1];
188 char issuer_pkey[enclen + 1];
190 char signature[128]; //TODO max payload size
191 char expiration[256];
193 struct GNUNET_CRYPTO_EcdsaSignature *sig;
194 struct GNUNET_TIME_Absolute etime_abs;
197 "%52s.%253s -> %52s | %s | %255[0-9a-zA-Z: ]",
204 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
205 _("Unable to parse CRED record string `%s'\n"),
207 return GNUNET_SYSERR;
209 *data_size = sizeof (struct GNUNET_CREDENTIAL_CredentialRecordData) + strlen (name) + 1;
210 *data = cred = GNUNET_malloc (*data_size);
211 GNUNET_CRYPTO_ecdsa_public_key_from_string (subject_pkey,
212 strlen (subject_pkey),
214 GNUNET_CRYPTO_ecdsa_public_key_from_string (issuer_pkey,
215 strlen (issuer_pkey),
217 GNUNET_STRINGS_fancy_time_to_absolute (expiration,
219 GNUNET_STRINGS_base64_decode (signature,
222 cred->signature = *sig;
223 cred->expiration = GNUNET_htonll (etime_abs.abs_value_us);
224 cred->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_CREDENTIAL);
225 cred->purpose.size = htonl (strlen (name) + 1 + sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
226 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) + sizeof (uint64_t));
228 GNUNET_memcpy (&cred[1],
236 return GNUNET_SYSERR;
242 * Mapping of record type numbers to human-readable
249 { "CRED", GNUNET_GNSRECORD_TYPE_CREDENTIAL },
250 { "ATTR", GNUNET_GNSRECORD_TYPE_ATTRIBUTE },
256 * Convert a type name (i.e. "AAAA") to the corresponding number.
258 * @param cls closure, unused
259 * @param gns_typename name to convert
260 * @return corresponding number, UINT32_MAX on error
263 credential_typename_to_number (void *cls,
264 const char *gns_typename)
269 while ( (name_map[i].name != NULL) &&
270 (0 != strcasecmp (gns_typename, name_map[i].name)) )
272 return name_map[i].number;
277 * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
279 * @param cls closure, unused
280 * @param type number of a type to convert
281 * @return corresponding typestring, NULL on error
284 credential_number_to_typename (void *cls,
290 while ( (name_map[i].name != NULL) &&
291 (type != name_map[i].number) )
293 return name_map[i].name;
298 * Entry point for the plugin.
301 * @return the exported block API
304 libgnunet_plugin_gnsrecord_credential_init (void *cls)
306 struct GNUNET_GNSRECORD_PluginFunctions *api;
308 api = GNUNET_new (struct GNUNET_GNSRECORD_PluginFunctions);
309 api->value_to_string = &credential_value_to_string;
310 api->string_to_value = &credential_string_to_value;
311 api->typename_to_number = &credential_typename_to_number;
312 api->number_to_typename = &credential_number_to_typename;
318 * Exit point from the plugin.
320 * @param cls the return value from #libgnunet_plugin_block_test_init
324 libgnunet_plugin_gnsrecord_credential_done (void *cls)
326 struct GNUNET_GNSRECORD_PluginFunctions *api = cls;
332 /* end of plugin_gnsrecord_credential.c */