crypto_keys_init.c 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include <stdint.h>
  2. #include <openssl/err.h>
  3. #include <openssl/rand.h>
  4. #include "crypto_entropy.h"
  5. #include "warnp.h"
  6. #include "crypto.h"
  7. #include "crypto_internal.h"
  8. /* Amount of entropy to use for seeding OpenSSL. */
  9. #define RANDBUFLEN 2048
  10. /**
  11. * crypto_keys_init(void):
  12. * Initialize cryptographic keys.
  13. */
  14. int
  15. crypto_keys_init(void)
  16. {
  17. uint8_t randbuf[RANDBUFLEN];
  18. /* Initialize key cache. */
  19. if (crypto_keys_init_keycache())
  20. goto err0;
  21. /* Load OpenSSL error strings. */
  22. ERR_load_crypto_strings();
  23. /* Seed OpenSSL entropy pool. */
  24. if (crypto_entropy_read(randbuf, RANDBUFLEN)) {
  25. warnp("Could not obtain sufficient entropy");
  26. goto err0;
  27. }
  28. RAND_seed(randbuf, RANDBUFLEN);
  29. /* Load server root public key. */
  30. if (crypto_keys_server_import_root()) {
  31. warn0("Could not import server root public key");
  32. goto err0;
  33. }
  34. /* Initialize keys owned by crypto_file. */
  35. if (crypto_file_init_keys()) {
  36. warn0("Could not initialize crypto_file keys");
  37. goto err0;
  38. }
  39. /* Success! */
  40. return (0);
  41. err0:
  42. /* Failure! */
  43. return (-1);
  44. }