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"
35 * Convert the 'value' of a record to a string.
37 * @param cls closure, unused
38 * @param type type of the record
39 * @param data value in binary encoding
40 * @param data_size number of bytes in @a data
41 * @return NULL on error, otherwise human-readable representation of the value
44 credential_value_to_string (void *cls,
54 case GNUNET_GNSRECORD_TYPE_ATTRIBUTE:
56 struct GNUNET_CREDENTIAL_AttributeRecordData attr;
60 if (data_size < sizeof (struct GNUNET_CREDENTIAL_AttributeRecordData))
61 return NULL; /* malformed */
66 subject_pkey = GNUNET_CRYPTO_ecdsa_public_key_to_string (&attr.subject_key);
67 GNUNET_asprintf (&attr_str,
70 &cdata[sizeof (attr)]);
71 GNUNET_free (subject_pkey);
74 case GNUNET_GNSRECORD_TYPE_CREDENTIAL:
76 struct GNUNET_CREDENTIAL_CredentialRecordData cred;
80 if (data_size < sizeof (struct GNUNET_CREDENTIAL_CredentialRecordData))
81 return NULL; /* malformed */
86 subject_pkey = GNUNET_CRYPTO_ecdsa_public_key_to_string (&cred.subject_key);
87 issuer_pkey = GNUNET_CRYPTO_ecdsa_public_key_to_string (&cred.issuer_key);
89 GNUNET_asprintf (&cred_str,
93 &cdata[sizeof (cred)]);
94 GNUNET_free (subject_pkey);
95 GNUNET_free (issuer_pkey);
106 * Convert human-readable version of a 'value' of a record to the binary
109 * @param cls closure, unused
110 * @param type type of the record
111 * @param s human-readable string
112 * @param data set to value in binary encoding (will be allocated)
113 * @param data_size set to number of bytes in @a data
114 * @return #GNUNET_OK on success
117 credential_string_to_value (void *cls,
124 return GNUNET_SYSERR;
127 case GNUNET_GNSRECORD_TYPE_CREDENTIAL:
129 struct GNUNET_CREDENTIAL_CredentialRecordData *cred;
131 size_t enclen = (sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)) * 8;
133 enclen += 5 - enclen % 5;
134 enclen /= 5; /* 260/5 = 52 */
135 char subject_pkey[enclen + 1];
136 char issuer_pkey[enclen + 1];
145 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
146 _("Unable to parse CRED record string `%s'\n"),
148 return GNUNET_SYSERR;
150 *data_size = sizeof (struct GNUNET_CREDENTIAL_CredentialRecordData) + strlen (name) + 1;
151 *data = cred = GNUNET_malloc (*data_size);
152 GNUNET_CRYPTO_ecdsa_public_key_from_string (subject_pkey,
153 strlen (subject_pkey),
155 GNUNET_CRYPTO_ecdsa_public_key_from_string (issuer_pkey,
156 strlen (issuer_pkey),
158 GNUNET_memcpy (&cred[1],
163 *data = GNUNET_strdup (s);
164 *data_size = strlen (s);
168 return GNUNET_SYSERR;
174 * Mapping of record type numbers to human-readable
181 { "CRED", GNUNET_GNSRECORD_TYPE_CREDENTIAL },
187 * Convert a type name (i.e. "AAAA") to the corresponding number.
189 * @param cls closure, unused
190 * @param gns_typename name to convert
191 * @return corresponding number, UINT32_MAX on error
194 credential_typename_to_number (void *cls,
195 const char *gns_typename)
200 while ( (name_map[i].name != NULL) &&
201 (0 != strcasecmp (gns_typename, name_map[i].name)) )
203 return name_map[i].number;
208 * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
210 * @param cls closure, unused
211 * @param type number of a type to convert
212 * @return corresponding typestring, NULL on error
215 credential_number_to_typename (void *cls,
221 while ( (name_map[i].name != NULL) &&
222 (type != name_map[i].number) )
224 return name_map[i].name;
229 * Entry point for the plugin.
232 * @return the exported block API
235 libgnunet_plugin_gnsrecord_credential_init (void *cls)
237 struct GNUNET_GNSRECORD_PluginFunctions *api;
239 api = GNUNET_new (struct GNUNET_GNSRECORD_PluginFunctions);
240 api->value_to_string = &credential_value_to_string;
241 api->string_to_value = &credential_string_to_value;
242 api->typename_to_number = &credential_typename_to_number;
243 api->number_to_typename = &credential_number_to_typename;
249 * Exit point from the plugin.
251 * @param cls the return value from #libgnunet_plugin_block_test_init
255 libgnunet_plugin_gnsrecord_credential_done (void *cls)
257 struct GNUNET_GNSRECORD_PluginFunctions *api = cls;
263 /* end of plugin_gnsrecord_credential.c */