tor-checkkey.c 1.7 KB

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