tor-checkkey.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 "../common/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. int wantdigest=0;
  17. int fname_idx;
  18. init_logging();
  19. if (c < 2) {
  20. fprintf(stderr, "Hi. I'm tor-checkkey. Tell me a filename that "
  21. "has a PEM-encoded RSA public key (like in a cert) and I'll "
  22. "dump the modulus. Use the --digest option too and I'll "
  23. "dump the digest.\n");
  24. return 1;
  25. }
  26. if (crypto_global_init(0, NULL, NULL)) {
  27. fprintf(stderr, "Couldn't initialize crypto library.\n");
  28. return 1;
  29. }
  30. if (!strcmp(v[1], "--digest")) {
  31. wantdigest = 1;
  32. fname_idx = 2;
  33. if (c<3) {
  34. fprintf(stderr, "too few arguments");
  35. return 1;
  36. }
  37. } else {
  38. wantdigest = 0;
  39. fname_idx = 1;
  40. }
  41. str = read_file_to_str(v[fname_idx], 0, NULL);
  42. if (!str) {
  43. fprintf(stderr, "Couldn't read %s\n", v[fname_idx]);
  44. return 1;
  45. }
  46. env = crypto_new_pk_env();
  47. if (crypto_pk_read_public_key_from_string(env, str, strlen(str))<0) {
  48. fprintf(stderr, "Couldn't parse key.\n");
  49. return 1;
  50. }
  51. tor_free(str);
  52. if (wantdigest) {
  53. char digest[HEX_DIGEST_LEN+1];
  54. if (crypto_pk_get_fingerprint(env, digest, 0)<0)
  55. return 1;
  56. printf("%s\n",digest);
  57. } else {
  58. rsa = _crypto_pk_env_get_rsa(env);
  59. str = BN_bn2hex(rsa->n);
  60. printf("%s\n", str);
  61. }
  62. return 0;
  63. }