keyid2sql.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright (C) 2008 Andreas Steffen
  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 in sql format
  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, n;
  30. library_init(NULL, "keyid2sql");
  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. if (private->get_fingerprint(private, KEYID_PUBKEY_SHA1, &chunk))
  46. {
  47. printf("%d, X'", ID_KEY_ID);
  48. for (n = 0; n < chunk.len; n++)
  49. {
  50. printf("%.2x", chunk.ptr[n]);
  51. }
  52. printf("'\n");
  53. }
  54. private->destroy(private);
  55. return 0;
  56. }
  57. public = lib->creds->create(lib->creds, CRED_PUBLIC_KEY, KEY_ANY,
  58. BUILD_BLOB_PEM, chunk_clone(chunk),
  59. BUILD_END);
  60. if (!public)
  61. {
  62. public = lib->creds->create(lib->creds, CRED_PUBLIC_KEY, KEY_RSA,
  63. BUILD_BLOB_PEM, chunk_clone(chunk),
  64. BUILD_END);
  65. }
  66. if (public)
  67. {
  68. if (public->get_fingerprint(public, KEYID_PUBKEY_SHA1, &chunk))
  69. {
  70. printf("%d, X'", ID_KEY_ID);
  71. for (n = 0; n < chunk.len; n++)
  72. {
  73. printf("%.2x", chunk.ptr[n]);
  74. }
  75. printf("'\n");
  76. }
  77. public->destroy(public);
  78. return 0;
  79. }
  80. fprintf(stderr, "unable to parse input key.\n");
  81. return -1;
  82. }