tor-checkkey.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #define CRYPTO_PRIVATE
  2. #include "orconfig.h"
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include "crypto.h"
  6. #include "torlog.h"
  7. #include "../common/util.h"
  8. #include "compat.h"
  9. #include <openssl/bn.h>
  10. #include <openssl/rsa.h>
  11. int
  12. main(int c, char **v)
  13. {
  14. crypto_pk_env_t *env;
  15. char *str;
  16. RSA *rsa;
  17. int wantdigest=0;
  18. int fname_idx;
  19. char *fname=NULL;
  20. init_logging();
  21. if (c < 2) {
  22. fprintf(stderr, "Hi. I'm tor-checkkey. Tell me a filename that "
  23. "has a PEM-encoded RSA public key (like in a cert) and I'll "
  24. "dump the modulus. Use the --digest option too and I'll "
  25. "dump the digest.\n");
  26. return 1;
  27. }
  28. if (crypto_global_init(0, NULL, NULL)) {
  29. fprintf(stderr, "Couldn't initialize crypto library.\n");
  30. return 1;
  31. }
  32. if (!strcmp(v[1], "--digest")) {
  33. wantdigest = 1;
  34. fname_idx = 2;
  35. if (c<3) {
  36. fprintf(stderr, "too few arguments");
  37. return 1;
  38. }
  39. } else {
  40. wantdigest = 0;
  41. fname_idx = 1;
  42. }
  43. fname = expand_filename(v[fname_idx]);
  44. str = read_file_to_str(fname, 0, NULL);
  45. tor_free(fname);
  46. if (!str) {
  47. fprintf(stderr, "Couldn't read %s\n", v[fname_idx]);
  48. return 1;
  49. }
  50. env = crypto_new_pk_env();
  51. if (crypto_pk_read_public_key_from_string(env, str, strlen(str))<0) {
  52. fprintf(stderr, "Couldn't parse key.\n");
  53. return 1;
  54. }
  55. tor_free(str);
  56. if (wantdigest) {
  57. char digest[HEX_DIGEST_LEN+1];
  58. if (crypto_pk_get_fingerprint(env, digest, 0)<0)
  59. return 1;
  60. printf("%s\n",digest);
  61. } else {
  62. rsa = _crypto_pk_env_get_rsa(env);
  63. str = BN_bn2hex(rsa->n);
  64. printf("%s\n", str);
  65. }
  66. return 0;
  67. }