tor-checkkey.c 990 B

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