2 This file is part of GNUnet
3 Copyright (C) 2013, 2016 GNUnet e.V.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @file revocation/revocation_api.c
22 * @brief API to perform and access key revocations
23 * @author Christian Grothoff
26 #include "gnunet_revocation_service.h"
27 #include "gnunet_signatures.h"
28 #include "gnunet_protocols.h"
29 #include "revocation.h"
34 * Handle for the key revocation query.
36 struct GNUNET_REVOCATION_Query
39 * Message queue to the service.
41 struct GNUNET_MQ_Handle *mq;
44 * Function to call with the result.
46 GNUNET_REVOCATION_Callback func;
49 * Closure for @e func.
56 * Generic error handler, called with the appropriate
57 * error code and the same closure specified at the creation of
59 * Not every message queue implementation supports an error handler.
61 * @param cls closure with the `struct GNUNET_NSE_Handle *`
62 * @param error error code
65 query_mq_error_handler (void *cls,
66 enum GNUNET_MQ_Error error)
68 struct GNUNET_REVOCATION_Query *q = cls;
70 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
71 "Revocation query MQ error\n");
74 GNUNET_REVOCATION_query_cancel (q);
79 * Handle response to our revocation query.
81 * @param cls our `struct GNUNET_REVOCATION_Query` handle
82 * @param qrm response we got
85 handle_revocation_query_response (void *cls,
86 const struct QueryResponseMessage *qrm)
88 struct GNUNET_REVOCATION_Query *q = cls;
90 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
91 "Revocation query result: %d\n",
92 (uint32_t) ntohl (qrm->is_valid));
94 ntohl (qrm->is_valid));
95 GNUNET_REVOCATION_query_cancel (q);
100 * Check if a key was revoked.
102 * @param cfg the configuration to use
103 * @param key key to check for revocation
104 * @param func funtion to call with the result of the check
105 * @param func_cls closure to pass to @a func
106 * @return handle to use in #GNUNET_REVOCATION_query_cancel to stop REVOCATION from invoking the callback
108 struct GNUNET_REVOCATION_Query *
109 GNUNET_REVOCATION_query (const struct GNUNET_CONFIGURATION_Handle *cfg,
110 const struct GNUNET_CRYPTO_EcdsaPublicKey *key,
111 GNUNET_REVOCATION_Callback func,
114 struct GNUNET_REVOCATION_Query *q
115 = GNUNET_new (struct GNUNET_REVOCATION_Query);
116 struct GNUNET_MQ_MessageHandler handlers[] = {
117 GNUNET_MQ_hd_fixed_size (revocation_query_response,
118 GNUNET_MESSAGE_TYPE_REVOCATION_QUERY_RESPONSE,
119 struct QueryResponseMessage,
121 GNUNET_MQ_handler_end ()
123 struct QueryMessage *qm;
124 struct GNUNET_MQ_Envelope *env;
126 q->mq = GNUNET_CLIENT_connect (cfg,
129 &query_mq_error_handler,
137 q->func_cls = func_cls;
138 env = GNUNET_MQ_msg (qm,
139 GNUNET_MESSAGE_TYPE_REVOCATION_QUERY);
140 qm->reserved = htonl (0);
142 GNUNET_MQ_send (q->mq,
149 * Cancel key revocation check.
151 * @param q query to cancel
154 GNUNET_REVOCATION_query_cancel (struct GNUNET_REVOCATION_Query *q)
158 GNUNET_MQ_destroy (q->mq);
166 * Handle for the key revocation operation.
168 struct GNUNET_REVOCATION_Handle
171 * Message queue to the service.
173 struct GNUNET_MQ_Handle *mq;
176 * Function to call once we are done.
178 GNUNET_REVOCATION_Callback func;
181 * Closure for @e func.
188 * Generic error handler, called with the appropriate
189 * error code and the same closure specified at the creation of
191 * Not every message queue implementation supports an error handler.
193 * @param cls closure with the `struct GNUNET_NSE_Handle *`
194 * @param error error code
197 revocation_mq_error_handler (void *cls,
198 enum GNUNET_MQ_Error error)
200 struct GNUNET_REVOCATION_Handle *h = cls;
202 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
203 "Revocation MQ error\n");
204 h->func (h->func_cls,
206 GNUNET_REVOCATION_revoke_cancel (h);
211 * Handle response to our revocation query.
213 * @param cls our `struct GNUNET_REVOCATION_Handle` handle
214 * @param rrm response we got
217 handle_revocation_response (void *cls,
218 const struct RevocationResponseMessage *rrm)
220 struct GNUNET_REVOCATION_Handle *h = cls;
222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
223 "Revocation transmission result: %d\n",
224 (uint32_t) ntohl (rrm->is_valid));
225 h->func (h->func_cls,
226 ntohl (rrm->is_valid));
227 GNUNET_REVOCATION_revoke_cancel (h);
232 * Perform key revocation.
234 * @param cfg the configuration to use
235 * @param key public key of the key to revoke
236 * @param sig signature to use on the revocation (should have been
237 * created using #GNUNET_REVOCATION_sign_revocation).
238 * @param pow proof of work to use (should have been created by
239 * iteratively calling #GNUNET_REVOCATION_check_pow)
240 * @param func funtion to call with the result of the check
241 * (called with `is_valid` being #GNUNET_NO if
242 * the revocation worked).
243 * @param func_cls closure to pass to @a func
244 * @return handle to use in #GNUNET_REVOCATION_revoke_cancel to stop REVOCATION from invoking the callback
246 struct GNUNET_REVOCATION_Handle *
247 GNUNET_REVOCATION_revoke (const struct GNUNET_CONFIGURATION_Handle *cfg,
248 const struct GNUNET_CRYPTO_EcdsaPublicKey *key,
249 const struct GNUNET_CRYPTO_EcdsaSignature *sig,
251 GNUNET_REVOCATION_Callback func,
254 struct GNUNET_REVOCATION_Handle *h
255 = GNUNET_new (struct GNUNET_REVOCATION_Handle);
256 struct GNUNET_MQ_MessageHandler handlers[] = {
257 GNUNET_MQ_hd_fixed_size (revocation_response,
258 GNUNET_MESSAGE_TYPE_REVOCATION_REVOKE_RESPONSE,
259 struct RevocationResponseMessage,
261 GNUNET_MQ_handler_end ()
263 unsigned long long matching_bits;
264 struct RevokeMessage *rm;
265 struct GNUNET_MQ_Envelope *env;
268 GNUNET_CONFIGURATION_get_value_number (cfg,
273 GNUNET_REVOCATION_check_pow (key,
275 (unsigned int) matching_bits)))
282 h->mq = GNUNET_CLIENT_connect (cfg,
285 &revocation_mq_error_handler,
293 h->func_cls = func_cls;
294 env = GNUNET_MQ_msg (rm,
295 GNUNET_MESSAGE_TYPE_REVOCATION_REVOKE);
296 rm->reserved = htonl (0);
297 rm->proof_of_work = pow;
298 rm->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_REVOCATION);
299 rm->purpose.size = htonl (sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose)
300 + sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey));
301 rm->public_key = *key;
302 rm->signature = *sig;
303 GNUNET_MQ_send (h->mq,
310 * Cancel key revocation.
312 * @param h operation to cancel
315 GNUNET_REVOCATION_revoke_cancel (struct GNUNET_REVOCATION_Handle *h)
319 GNUNET_MQ_destroy (h->mq);
327 * Count the leading zeroes in hash.
329 * @param hash to count leading zeros in
330 * @return the number of leading zero bits.
333 count_leading_zeroes (const struct GNUNET_HashCode *hash)
335 unsigned int hash_count;
338 while ((0 == GNUNET_CRYPTO_hash_get_bit (hash, hash_count)))
345 * Check if the given proof-of-work value
346 * would be acceptable for revoking the given key.
348 * @param key key to check for
349 * @param pow proof of work value
350 * @param matching_bits how many bits must match (configuration)
351 * @return #GNUNET_YES if the @a pow is acceptable, #GNUNET_NO if not
354 GNUNET_REVOCATION_check_pow (const struct GNUNET_CRYPTO_EcdsaPublicKey *key,
356 unsigned int matching_bits)
358 char buf[sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey)
359 + sizeof(pow)] GNUNET_ALIGN;
360 struct GNUNET_HashCode result;
362 GNUNET_memcpy (buf, &pow, sizeof(pow));
363 GNUNET_memcpy (&buf[sizeof(pow)], key,
364 sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey));
365 GNUNET_CRYPTO_pow_hash ("gnunet-revocation-proof-of-work",
369 return (count_leading_zeroes (&result) >=
370 matching_bits) ? GNUNET_YES : GNUNET_NO;
375 * Create a revocation signature.
377 * @param key private key of the key to revoke
378 * @param sig where to write the revocation signature
381 GNUNET_REVOCATION_sign_revocation (const struct
382 GNUNET_CRYPTO_EcdsaPrivateKey *key,
383 struct GNUNET_CRYPTO_EcdsaSignature *sig)
385 struct RevokeMessage rm;
387 rm.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_REVOCATION);
388 rm.purpose.size = htonl (sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose)
389 + sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey));
390 GNUNET_CRYPTO_ecdsa_key_get_public (key, &rm.public_key);
391 GNUNET_assert (GNUNET_OK ==
392 GNUNET_CRYPTO_ecdsa_sign_ (key,
398 /* end of revocation_api.c */