tor-checkkey.c 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #define CRYPTO_PRIVATE
  2. #include "orconfig.h"
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include "crypto.h"
  6. #include "log.h"
  7. #include "util.h"
  8. #include "compat.h"
  9. #include <openssl/bn.h>
  10. #include <openssl/rsa.h>
  11. int main(int c, char **v)
  12. {
  13. crypto_pk_env_t *env;
  14. char *str;
  15. RSA *rsa;
  16. init_logging();
  17. if (c < 2) {
  18. fprintf(stderr, "Hi. I'm tor-checkkey. Tell me a filename that "
  19. "has a PEM-encoded RSA public key (like in a cert) and I'll "
  20. "dump the modulus.\n");
  21. return 1;
  22. }
  23. if (crypto_global_init(0)) {
  24. fprintf(stderr, "Couldn't initialize crypto library.\n");
  25. return 1;
  26. }
  27. str = read_file_to_str(v[1], 0, NULL);
  28. if (!str) {
  29. fprintf(stderr, "Couldn't read %s\n", v[1]);
  30. return 1;
  31. }
  32. env = crypto_new_pk_env();
  33. if (crypto_pk_read_public_key_from_string(env, str, strlen(str))<0) {
  34. fprintf(stderr, "Couldn't parse key.\n");
  35. return 1;
  36. }
  37. tor_free(str);
  38. rsa = _crypto_pk_env_get_rsa(env);
  39. str = BN_bn2hex(rsa->n);
  40. printf("%s\n", str);
  41. return 0;
  42. }