tor-checkkey.c 1.7 KB

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