id_to_fp.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* Copyright 2006 Nick Mathewson; see LICENSE for licensing information */
  2. /* $Id$ */
  3. /* id_to_fp.c : Helper for directory authority ops. When somebody sends us
  4. * a private key, this utility converts the private key into a fingerprint
  5. * so you can de-list that fingerprint.
  6. */
  7. #include <openssl/rsa.h>
  8. #include <openssl/bio.h>
  9. #include <openssl/sha.h>
  10. #include <openssl/pem.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #define die(s) do { fprintf(stderr, "%s\n", s); goto err; } while (0)
  15. int
  16. main(int argc, char **argv)
  17. {
  18. BIO *b = NULL;
  19. RSA *key = NULL;
  20. unsigned char *buf = NULL, *bufp;
  21. int len, i;
  22. unsigned char digest[20];
  23. int status = 1;
  24. if (argc < 2) {
  25. fprintf(stderr, "Reading key from stdin...\n");
  26. if (!(b = BIO_new_fp(stdin, BIO_NOCLOSE)))
  27. die("couldn't read from stdin");
  28. } else if (argc == 2) {
  29. if (strcmp(argv[1], "-h") == 0 ||
  30. strcmp(argv[1], "--help") == 0) {
  31. fprintf(stdout, "Usage: %s [keyfile]\n", argv[0]);
  32. status = 0;
  33. goto err;
  34. } else {
  35. if (!(b = BIO_new_file(argv[1], "r")))
  36. die("couldn't open file");
  37. }
  38. } else {
  39. fprintf(stderr, "Usage: %s [keyfile]\n", argv[0]);
  40. goto err;
  41. }
  42. if (!(key = PEM_read_bio_RSAPrivateKey(b, NULL, NULL, NULL)))
  43. die("couldn't parse key");
  44. len = i2d_RSAPublicKey(key, NULL);
  45. if (len < 0)
  46. die("Bizarre key");
  47. bufp = buf = malloc(len+1);
  48. if (!buf)
  49. die("Out of memory");
  50. len = i2d_RSAPublicKey(key, &bufp);
  51. if (len < 0)
  52. die("Bizarre key");
  53. SHA1(buf, len, digest);
  54. for (i=0; i < 20; i += 2) {
  55. printf("%02X%02X ", (int)digest[i], (int)digest[i+1]);
  56. }
  57. printf("\n");
  58. status = 0;
  59. err:
  60. if (buf)
  61. free(buf);
  62. if (key)
  63. RSA_free(key);
  64. if (b)
  65. BIO_free(b);
  66. return status;
  67. }