revocation_api.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2013, 2016 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file revocation/revocation_api.c
  18. * @brief API to perform and access key revocations
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_revocation_service.h"
  23. #include "gnunet_signatures.h"
  24. #include "gnunet_protocols.h"
  25. #include "revocation.h"
  26. #include <gcrypt.h>
  27. /**
  28. * Handle for the key revocation query.
  29. */
  30. struct GNUNET_REVOCATION_Query
  31. {
  32. /**
  33. * Message queue to the service.
  34. */
  35. struct GNUNET_MQ_Handle *mq;
  36. /**
  37. * Function to call with the result.
  38. */
  39. GNUNET_REVOCATION_Callback func;
  40. /**
  41. * Closure for @e func.
  42. */
  43. void *func_cls;
  44. };
  45. /**
  46. * Generic error handler, called with the appropriate
  47. * error code and the same closure specified at the creation of
  48. * the message queue.
  49. * Not every message queue implementation supports an error handler.
  50. *
  51. * @param cls closure with the `struct GNUNET_NSE_Handle *`
  52. * @param error error code
  53. */
  54. static void
  55. query_mq_error_handler (void *cls,
  56. enum GNUNET_MQ_Error error)
  57. {
  58. struct GNUNET_REVOCATION_Query *q = cls;
  59. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  60. "Revocation query MQ error\n");
  61. q->func (q->func_cls,
  62. GNUNET_SYSERR);
  63. GNUNET_REVOCATION_query_cancel (q);
  64. }
  65. /**
  66. * Handle response to our revocation query.
  67. *
  68. * @param cls our `struct GNUNET_REVOCATION_Query` handle
  69. * @param qrm response we got
  70. */
  71. static void
  72. handle_revocation_query_response (void *cls,
  73. const struct QueryResponseMessage *qrm)
  74. {
  75. struct GNUNET_REVOCATION_Query *q = cls;
  76. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  77. "Revocation query result: %d\n",
  78. (uint32_t) ntohl (qrm->is_valid));
  79. q->func (q->func_cls,
  80. ntohl (qrm->is_valid));
  81. GNUNET_REVOCATION_query_cancel (q);
  82. }
  83. /**
  84. * Check if a key was revoked.
  85. *
  86. * @param cfg the configuration to use
  87. * @param key key to check for revocation
  88. * @param func funtion to call with the result of the check
  89. * @param func_cls closure to pass to @a func
  90. * @return handle to use in #GNUNET_REVOCATION_query_cancel to stop REVOCATION from invoking the callback
  91. */
  92. struct GNUNET_REVOCATION_Query *
  93. GNUNET_REVOCATION_query (const struct GNUNET_CONFIGURATION_Handle *cfg,
  94. const struct GNUNET_CRYPTO_EcdsaPublicKey *key,
  95. GNUNET_REVOCATION_Callback func,
  96. void *func_cls)
  97. {
  98. struct GNUNET_REVOCATION_Query *q
  99. = GNUNET_new (struct GNUNET_REVOCATION_Query);
  100. struct GNUNET_MQ_MessageHandler handlers[] = {
  101. GNUNET_MQ_hd_fixed_size (revocation_query_response,
  102. GNUNET_MESSAGE_TYPE_REVOCATION_QUERY_RESPONSE,
  103. struct QueryResponseMessage,
  104. q),
  105. GNUNET_MQ_handler_end ()
  106. };
  107. struct QueryMessage *qm;
  108. struct GNUNET_MQ_Envelope *env;
  109. q->mq = GNUNET_CLIENT_connect (cfg,
  110. "revocation",
  111. handlers,
  112. &query_mq_error_handler,
  113. q);
  114. if (NULL == q->mq)
  115. {
  116. GNUNET_free (q);
  117. return NULL;
  118. }
  119. q->func = func;
  120. q->func_cls = func_cls;
  121. env = GNUNET_MQ_msg (qm,
  122. GNUNET_MESSAGE_TYPE_REVOCATION_QUERY);
  123. qm->reserved = htonl (0);
  124. qm->key = *key;
  125. GNUNET_MQ_send (q->mq,
  126. env);
  127. return q;
  128. }
  129. /**
  130. * Cancel key revocation check.
  131. *
  132. * @param q query to cancel
  133. */
  134. void
  135. GNUNET_REVOCATION_query_cancel (struct GNUNET_REVOCATION_Query *q)
  136. {
  137. if (NULL != q->mq)
  138. {
  139. GNUNET_MQ_destroy (q->mq);
  140. q->mq = NULL;
  141. }
  142. GNUNET_free (q);
  143. }
  144. /**
  145. * Handle for the key revocation operation.
  146. */
  147. struct GNUNET_REVOCATION_Handle
  148. {
  149. /**
  150. * Message queue to the service.
  151. */
  152. struct GNUNET_MQ_Handle *mq;
  153. /**
  154. * Function to call once we are done.
  155. */
  156. GNUNET_REVOCATION_Callback func;
  157. /**
  158. * Closure for @e func.
  159. */
  160. void *func_cls;
  161. };
  162. /**
  163. * Generic error handler, called with the appropriate
  164. * error code and the same closure specified at the creation of
  165. * the message queue.
  166. * Not every message queue implementation supports an error handler.
  167. *
  168. * @param cls closure with the `struct GNUNET_NSE_Handle *`
  169. * @param error error code
  170. */
  171. static void
  172. revocation_mq_error_handler (void *cls,
  173. enum GNUNET_MQ_Error error)
  174. {
  175. struct GNUNET_REVOCATION_Handle *h = cls;
  176. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  177. "Revocation MQ error\n");
  178. h->func (h->func_cls,
  179. GNUNET_SYSERR);
  180. GNUNET_REVOCATION_revoke_cancel (h);
  181. }
  182. /**
  183. * Handle response to our revocation query.
  184. *
  185. * @param cls our `struct GNUNET_REVOCATION_Handle` handle
  186. * @param rrm response we got
  187. */
  188. static void
  189. handle_revocation_response (void *cls,
  190. const struct RevocationResponseMessage *rrm)
  191. {
  192. struct GNUNET_REVOCATION_Handle *h = cls;
  193. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  194. "Revocation transmission result: %d\n",
  195. (uint32_t) ntohl (rrm->is_valid));
  196. h->func (h->func_cls,
  197. ntohl (rrm->is_valid));
  198. GNUNET_REVOCATION_revoke_cancel (h);
  199. }
  200. /**
  201. * Perform key revocation.
  202. *
  203. * @param cfg the configuration to use
  204. * @param key public key of the key to revoke
  205. * @param sig signature to use on the revocation (should have been
  206. * created using #GNUNET_REVOCATION_sign_revocation).
  207. * @param pow proof of work to use (should have been created by
  208. * iteratively calling #GNUNET_REVOCATION_check_pow)
  209. * @param func funtion to call with the result of the check
  210. * (called with `is_valid` being #GNUNET_NO if
  211. * the revocation worked).
  212. * @param func_cls closure to pass to @a func
  213. * @return handle to use in #GNUNET_REVOCATION_revoke_cancel to stop REVOCATION from invoking the callback
  214. */
  215. struct GNUNET_REVOCATION_Handle *
  216. GNUNET_REVOCATION_revoke (const struct GNUNET_CONFIGURATION_Handle *cfg,
  217. const struct GNUNET_CRYPTO_EcdsaPublicKey *key,
  218. const struct GNUNET_CRYPTO_EcdsaSignature *sig,
  219. uint64_t pow,
  220. GNUNET_REVOCATION_Callback func,
  221. void *func_cls)
  222. {
  223. struct GNUNET_REVOCATION_Handle *h
  224. = GNUNET_new (struct GNUNET_REVOCATION_Handle);
  225. struct GNUNET_MQ_MessageHandler handlers[] = {
  226. GNUNET_MQ_hd_fixed_size (revocation_response,
  227. GNUNET_MESSAGE_TYPE_REVOCATION_REVOKE_RESPONSE,
  228. struct RevocationResponseMessage,
  229. h),
  230. GNUNET_MQ_handler_end ()
  231. };
  232. unsigned long long matching_bits;
  233. struct RevokeMessage *rm;
  234. struct GNUNET_MQ_Envelope *env;
  235. if ( (GNUNET_OK ==
  236. GNUNET_CONFIGURATION_get_value_number (cfg,
  237. "REVOCATION",
  238. "WORKBITS",
  239. &matching_bits)) &&
  240. (GNUNET_YES !=
  241. GNUNET_REVOCATION_check_pow (key,
  242. pow,
  243. (unsigned int) matching_bits)) )
  244. {
  245. GNUNET_break (0);
  246. GNUNET_free (h);
  247. return NULL;
  248. }
  249. h->mq = GNUNET_CLIENT_connect (cfg,
  250. "revocation",
  251. handlers,
  252. &revocation_mq_error_handler,
  253. h);
  254. if (NULL == h->mq)
  255. {
  256. GNUNET_free (h);
  257. return NULL;
  258. }
  259. h->func = func;
  260. h->func_cls = func_cls;
  261. env = GNUNET_MQ_msg (rm,
  262. GNUNET_MESSAGE_TYPE_REVOCATION_REVOKE);
  263. rm->reserved = htonl (0);
  264. rm->proof_of_work = pow;
  265. rm->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_REVOCATION);
  266. rm->purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
  267. sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
  268. rm->public_key = *key;
  269. rm->signature = *sig;
  270. GNUNET_MQ_send (h->mq,
  271. env);
  272. return h;
  273. }
  274. /**
  275. * Cancel key revocation.
  276. *
  277. * @param h operation to cancel
  278. */
  279. void
  280. GNUNET_REVOCATION_revoke_cancel (struct GNUNET_REVOCATION_Handle *h)
  281. {
  282. if (NULL != h->mq)
  283. {
  284. GNUNET_MQ_destroy (h->mq);
  285. h->mq = NULL;
  286. }
  287. GNUNET_free (h);
  288. }
  289. /**
  290. * Calculate the 'proof-of-work' hash (an expensive hash).
  291. *
  292. * @param buf data to hash
  293. * @param buf_len number of bytes in @a buf
  294. * @param result where to write the resulting hash
  295. */
  296. static void
  297. pow_hash (const void *buf,
  298. size_t buf_len,
  299. struct GNUNET_HashCode *result)
  300. {
  301. GNUNET_break (0 ==
  302. gcry_kdf_derive (buf, buf_len,
  303. GCRY_KDF_SCRYPT,
  304. 1 /* subalgo */,
  305. "gnunet-revocation-proof-of-work",
  306. strlen ("gnunet-revocation-proof-of-work"),
  307. 2 /* iterations; keep cost of individual op small */,
  308. sizeof (struct GNUNET_HashCode), result));
  309. }
  310. /**
  311. * Count the leading zeroes in hash.
  312. *
  313. * @param hash to count leading zeros in
  314. * @return the number of leading zero bits.
  315. */
  316. static unsigned int
  317. count_leading_zeroes (const struct GNUNET_HashCode *hash)
  318. {
  319. unsigned int hash_count;
  320. hash_count = 0;
  321. while ((0 == GNUNET_CRYPTO_hash_get_bit (hash, hash_count)))
  322. hash_count++;
  323. return hash_count;
  324. }
  325. /**
  326. * Check if the given proof-of-work value
  327. * would be acceptable for revoking the given key.
  328. *
  329. * @param key key to check for
  330. * @param pow proof of work value
  331. * @param matching_bits how many bits must match (configuration)
  332. * @return #GNUNET_YES if the @a pow is acceptable, #GNUNET_NO if not
  333. */
  334. int
  335. GNUNET_REVOCATION_check_pow (const struct GNUNET_CRYPTO_EcdsaPublicKey *key,
  336. uint64_t pow,
  337. unsigned int matching_bits)
  338. {
  339. char buf[sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) +
  340. sizeof (pow)] GNUNET_ALIGN;
  341. struct GNUNET_HashCode result;
  342. GNUNET_memcpy (buf, &pow, sizeof (pow));
  343. GNUNET_memcpy (&buf[sizeof (pow)], key,
  344. sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
  345. pow_hash (buf, sizeof (buf), &result);
  346. return (count_leading_zeroes (&result) >=
  347. matching_bits) ? GNUNET_YES : GNUNET_NO;
  348. }
  349. /**
  350. * Create a revocation signature.
  351. *
  352. * @param key private key of the key to revoke
  353. * @param sig where to write the revocation signature
  354. */
  355. void
  356. GNUNET_REVOCATION_sign_revocation (const struct GNUNET_CRYPTO_EcdsaPrivateKey *key,
  357. struct GNUNET_CRYPTO_EcdsaSignature *sig)
  358. {
  359. struct RevokeMessage rm;
  360. rm.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_REVOCATION);
  361. rm.purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
  362. sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
  363. GNUNET_CRYPTO_ecdsa_key_get_public (key, &rm.public_key);
  364. GNUNET_assert (GNUNET_OK ==
  365. GNUNET_CRYPTO_ecdsa_sign (key,
  366. &rm.purpose,
  367. sig));
  368. }
  369. /* end of revocation_api.c */