key2keyid.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright (C) 2008-2009 Martin Willi
  3. * HSR Hochschule fuer Technik Rapperswil
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. * for more details.
  14. */
  15. #include <stdio.h>
  16. #include <library.h>
  17. #include <utils/debug.h>
  18. #include <credentials/keys/private_key.h>
  19. #include <credentials/keys/public_key.h>
  20. /**
  21. * print the keyids of a private or public key
  22. */
  23. int main(int argc, char *argv[])
  24. {
  25. public_key_t *public;
  26. private_key_t *private;
  27. chunk_t chunk;
  28. char buf[8096];
  29. int read;
  30. library_init(NULL, "key2keyid");
  31. lib->plugins->load(lib->plugins, PLUGINS);
  32. atexit(library_deinit);
  33. read = fread(buf, 1, sizeof(buf), stdin);
  34. if (read <= 0)
  35. {
  36. fprintf(stderr, "reading key failed.\n");
  37. return -1;
  38. }
  39. chunk = chunk_create(buf, read);
  40. private = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, KEY_RSA,
  41. BUILD_BLOB_PEM, chunk_clone(chunk),
  42. BUILD_END);
  43. if (private)
  44. {
  45. printf("parsed %d bits %N private key.\n",
  46. private->get_keysize(private),
  47. key_type_names, private->get_type(private));
  48. if (private->get_fingerprint(private, KEYID_PUBKEY_INFO_SHA1, &chunk))
  49. {
  50. printf("subjectPublicKeyInfo keyid: %#B\n", &chunk);
  51. }
  52. if (private->get_fingerprint(private, KEYID_PUBKEY_SHA1, &chunk))
  53. {
  54. printf("subjectPublicKey keyid: %#B\n", &chunk);
  55. }
  56. if (private->get_fingerprint(private, KEYID_PGPV3, &chunk))
  57. {
  58. printf("PGP version 3 keyid: %#B\n", &chunk);
  59. }
  60. private->destroy(private);
  61. return 0;
  62. }
  63. public = lib->creds->create(lib->creds, CRED_PUBLIC_KEY, KEY_ANY,
  64. BUILD_BLOB_PEM, chunk_clone(chunk),
  65. BUILD_END);
  66. if (!public)
  67. {
  68. public = lib->creds->create(lib->creds, CRED_PUBLIC_KEY, KEY_RSA,
  69. BUILD_BLOB_PEM, chunk_clone(chunk),
  70. BUILD_END);
  71. }
  72. if (public)
  73. {
  74. printf("parsed %d bits %N public key.\n",
  75. public->get_keysize(public),
  76. key_type_names, public->get_type(public));
  77. if (public->get_fingerprint(public, KEYID_PUBKEY_INFO_SHA1, &chunk))
  78. {
  79. printf("subjectPublicKeyInfo keyid: %#B\n", &chunk);
  80. }
  81. if (public->get_fingerprint(public, KEYID_PUBKEY_SHA1, &chunk))
  82. {
  83. printf("subjectPublicKey keyid: %#B\n", &chunk);
  84. }
  85. if (public->get_fingerprint(public, KEYID_PGPV3, &chunk))
  86. {
  87. printf("PGP version 3 keyid: %#B\n", &chunk);
  88. }
  89. public->destroy(public);
  90. return 0;
  91. }
  92. fprintf(stderr, "unable to parse input key.\n");
  93. return -1;
  94. }