summaryrefslogtreecommitdiff
path: root/main.c
blob: 638666baabe416ec4c0dcdcbe528db373763bb96 (plain)
  1. #include "gnutls-helpers.h"
  2. #include <gnutls/openpgp.h>
  3. #include <gnutls/x509.h>
  4. /*
  5. Author: Daniel Kahn Gillmor <dkg@fifthhorseman.net>
  6. Date: Tue, 01 Apr 2008
  7. License: GPL v3 or later
  8. monkeysphere private key translator: execute this with an GPG
  9. secret key on stdin (at the moment, only passphraseless RSA keys
  10. work).
  11. It will spit out a PEM-encoded version of the key on stdout, which
  12. can be fed into ssh-add like this:
  13. gpg --export-secret-keys $KEYID | monkeysphere | ssh-add -c /dev/stdin
  14. Requirements: I've only built this so far with GnuTLS v2.3.4 --
  15. version 2.2.0 does not contain the appropriate pieces.
  16. Notes: gpgkey2ssh doesn't seem to provide the same public
  17. keys. Mighty weird!
  18. 0 wt215@squeak:~/monkeysphere$ gpg --export-secret-keys 1DCDF89F | ~dkg/src/monkeysphere/monkeysphere | ssh-add -c /dev/stdin
  19. gnutls version: 2.3.4
  20. OpenPGP RSA Key, with 1024 bits
  21. Identity added: /dev/stdin (/dev/stdin)
  22. The user has to confirm each use of the key
  23. 0 wt215@squeak:~/monkeysphere$ ssh-add -L
  24. ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQC9gWQqfrnhQKDQnND/3eOexpddE64J+1zp9fcyCje7H5LKclb6DBV2HS6WgW32PJhIzvP+fYZM3dzXea3fpv14y1SicXiRBDgF9SnsNA1qWn2RyzkLcKy7PmM0PDYtU1oiLTcQj/xkWcqW2sLKHT/WW+vZP5XP7RMGN/yWNMfE2Q== /dev/stdin
  25. 0 wt215@squeak:~/monkeysphere$ gpgkey2ssh 1DCDF89F
  26. ssh-rsa AAAAB3NzaC1yc2EAAACBAL2BZCp+ueFAoNCc0P/d457Gl10Trgn7XOn19zIKN7sfkspyVvoMFXYdLpaBbfY8mEjO8/59hkzd3Nd5rd+m/XjLVKJxeJEEOAX1Kew0DWpafZHLOQtwrLs+YzQ8Ni1TWiItNxCP/GRZypbawsodP9Zb69k/lc/tEwY3/JY0x8TZAAAAAwEAAQ== COMMENT
  27. 0 wt215@squeak:~/monkeysphere$
  28. */
  29. int main(int argc, char* argv[]) {
  30. gnutls_x509_privkey_t x509_privkey;
  31. gnutls_datum_t data, test, clean;
  32. int ret;
  33. /*
  34. const char *certfile, *keyfile;
  35. gnutls_certificate_credentials_t pgp_creds;
  36. */
  37. gnutls_datum_t m, e, d, p, q, u, g, y, x;
  38. /* gnutls_x509_crt_t crt; */
  39. gnutls_openpgp_privkey_t pgp_privkey;
  40. gnutls_pk_algorithm_t pgp_algo;
  41. unsigned int pgp_bits;
  42. char output_data[10240];
  43. size_t ods = sizeof(output_data);
  44. init_gnutls();
  45. init_datum(&data);
  46. init_datum(&test);
  47. init_datum(&clean);
  48. init_datum(&m);
  49. init_datum(&e);
  50. init_datum(&d);
  51. init_datum(&p);
  52. init_datum(&q);
  53. init_datum(&u);
  54. init_datum(&g);
  55. init_datum(&y);
  56. init_datum(&x);
  57. if (ret = gnutls_x509_privkey_init(&x509_privkey), ret) {
  58. err("Failed to initialize X.509 private key (error: %d)\n", ret);
  59. return 1;
  60. }
  61. if (ret = gnutls_openpgp_privkey_init(&pgp_privkey), ret) {
  62. err("Failed to initialized OpenPGP private key (error: %d)\n", ret);
  63. return 1;
  64. }
  65. /* slurp in the private key from stdin */
  66. if (ret = set_datum_fd(&data, 0), ret) {
  67. err("didn't read file descriptor 0\n");
  68. return 1;
  69. }
  70. /* Or, instead, read in key from a file name:
  71. if (ret = set_datum_file(&data, argv[1]), ret) {
  72. err("didn't read file '%s'\n", argv[1]);
  73. return 1;
  74. }
  75. */
  76. /* treat the passed file as an X.509 private key, and extract its
  77. component values: */
  78. /* if (ret = gnutls_x509_privkey_import(x509_privkey, &data, GNUTLS_X509_FMT_PEM), ret) { */
  79. /* err("Failed to import the X.509 key (error: %d)\n", ret); */
  80. /* return 1; */
  81. /* } */
  82. /* gnutls_x509_privkey_export_rsa_raw(x509_privkey, &m, &e, &d, &p, &q, &u); */
  83. /* try to print the PEM-encoded private key: */
  84. /* ret = gnutls_x509_privkey_export (x509_privkey, */
  85. /* GNUTLS_X509_FMT_PEM, */
  86. /* output_data, */
  87. /* &ods); */
  88. /* printf("ret: %u; ods: %u;\n", ret, ods); */
  89. /* if (ret == 0) { */
  90. /* write(0, output_data, ods); */
  91. /* } */
  92. /* format could be either: GNUTLS_OPENPGP_FMT_RAW,
  93. GNUTLS_OPENPGP_FMT_BASE64; if MONKEYSPHERE_RAW is set, use RAW,
  94. otherwise, use BASE64: */
  95. if (getenv("MONKEYSPHERE_RAW")) {
  96. err("assuming RAW formatted private keys\n");
  97. if (ret = gnutls_openpgp_privkey_import(pgp_privkey, &data, GNUTLS_OPENPGP_FMT_RAW, NULL, 0), ret)
  98. err("failed to import the OpenPGP private key in RAW format (error: %d)\n", ret);
  99. } else {
  100. err("assuming BASE64 formatted private keys\n");
  101. if (ret = gnutls_openpgp_privkey_import (pgp_privkey, &data, GNUTLS_OPENPGP_FMT_BASE64, NULL, 0), ret)
  102. err("failed to import the OpenPGP private key in BASE64 format (error: %d)\n", ret);
  103. }
  104. pgp_algo = gnutls_openpgp_privkey_get_pk_algorithm(pgp_privkey, &pgp_bits);
  105. if (pgp_algo < 0) {
  106. err("failed to get OpenPGP key algorithm (error: %d)\n", pgp_algo);
  107. return 1;
  108. }
  109. if (pgp_algo == GNUTLS_PK_RSA) {
  110. err("OpenPGP RSA Key, with %d bits\n", pgp_bits);
  111. ret = gnutls_openpgp_privkey_export_rsa_raw(pgp_privkey, &m, &e, &d, &p, &q, &u);
  112. if (GNUTLS_E_SUCCESS != ret) {
  113. err ("failed to export RSA key parameters (error: %d)\n", ret);
  114. return 1;
  115. }
  116. ret = gnutls_x509_privkey_import_rsa_raw (x509_privkey, &m, &e, &d, &p, &q, &u);
  117. if (GNUTLS_E_SUCCESS != ret) {
  118. err ("failed to import RSA key parameters (error: %d)\n", ret);
  119. return 1;
  120. }
  121. } else if (pgp_algo == GNUTLS_PK_DSA) {
  122. err("OpenPGP DSA Key, with %d bits\n", pgp_bits);
  123. ret = gnutls_openpgp_privkey_export_dsa_raw(pgp_privkey, &p, &q, &g, &y, &x);
  124. if (GNUTLS_E_SUCCESS != ret) {
  125. err ("failed to export DSA key parameters (error: %d)\n", ret);
  126. return 1;
  127. }
  128. ret = gnutls_x509_privkey_import_dsa_raw (x509_privkey, &p, &q, &g, &y, &x);
  129. if (GNUTLS_E_SUCCESS != ret) {
  130. err ("failed to import DSA key parameters (error: %d)\n", ret);
  131. return 1;
  132. }
  133. } else {
  134. err("OpenPGP Key was not RSA or DSA -- can't deal! (actual algorithm was: %d)\n", pgp_algo);
  135. return 1;
  136. }
  137. ret = gnutls_x509_privkey_fix(x509_privkey);
  138. if (ret != 0) {
  139. err("failed to fix up the private key in X.509 format (error: %d)\n", ret);
  140. return 1;
  141. }
  142. ret = gnutls_x509_privkey_export (x509_privkey,
  143. GNUTLS_X509_FMT_PEM,
  144. output_data,
  145. &ods);
  146. printf("ret: %u; ods: %u;\n", ret, ods);
  147. if (ret == 0) {
  148. write(1, output_data, ods);
  149. }
  150. gnutls_x509_privkey_deinit(x509_privkey);
  151. gnutls_openpgp_privkey_deinit(pgp_privkey);
  152. gnutls_global_deinit();
  153. return 0;
  154. }