summaryrefslogtreecommitdiff
path: root/gpg2ssh.c
blob: 87b62a391e2a9d4abb6ce646dad40738fa9d6fcc (plain)
  1. #include "gnutls-helpers.h"
  2. #include <gnutls/openpgp.h>
  3. #include <gnutls/x509.h>
  4. /* for htonl() */
  5. #include <arpa/inet.h>
  6. /*
  7. Author: Daniel Kahn Gillmor <dkg@fifthhorseman.net>
  8. Date: Tue, 08 Apr 2008
  9. License: GPL v3 or later
  10. monkeysphere public key translator: execute this with an GPG
  11. certificate (public key(s) + userid(s)) on stdin. It currently
  12. only works with RSA keys.
  13. It will spit out a version of the first key capable of being used
  14. for authentication on stdout. The output format should be suitable
  15. for appending a known_hosts file.
  16. Requirements: I've only built this so far with GnuTLS v2.3.4 --
  17. version 2.2.0 does not contain the appropriate pieces.
  18. */
  19. int main(int argc, char* argv[]) {
  20. gnutls_datum_t data;
  21. int ret;
  22. gnutls_openpgp_crt_t openpgp_crt;
  23. gnutls_openpgp_keyid_t keyid;
  24. printable_keyid p_keyid;
  25. unsigned int keyidx;
  26. unsigned int usage, bits;
  27. gnutls_pk_algorithm_t algo;
  28. gnutls_datum_t m, e, p, q, g, y;
  29. char output_data[10240];
  30. size_t ods = sizeof(output_data);
  31. init_gnutls();
  32. init_datum(&data);
  33. init_datum(&m);
  34. init_datum(&e);
  35. init_datum(&p);
  36. init_datum(&q);
  37. init_datum(&g);
  38. init_datum(&y);
  39. init_keyid(keyid);
  40. /* slurp in the private key from stdin */
  41. if (ret = set_datum_fd(&data, 0), ret) {
  42. err("didn't read file descriptor 0\n");
  43. return 1;
  44. }
  45. if (ret = gnutls_openpgp_crt_init(&openpgp_crt), ret) {
  46. err("Failed to initialize OpenPGP certificate (error: %d)\n", ret);
  47. return 1;
  48. }
  49. /* format could be either: GNUTLS_OPENPGP_FMT_RAW,
  50. GNUTLS_OPENPGP_FMT_BASE64; if MONKEYSPHERE_RAW is set, use RAW,
  51. otherwise, use BASE64: */
  52. if (getenv("MONKEYSPHERE_RAW")) {
  53. err("assuming RAW formatted certificate\n");
  54. if (ret = gnutls_openpgp_crt_import(openpgp_crt, &data, GNUTLS_OPENPGP_FMT_RAW), ret) {
  55. err("failed to import the OpenPGP certificate in RAW format (error: %d)\n", ret);
  56. return ret;
  57. }
  58. } else {
  59. err("assuming BASE64 formatted certificate\n");
  60. if (ret = gnutls_openpgp_crt_import (openpgp_crt, &data, GNUTLS_OPENPGP_FMT_BASE64), ret) {
  61. err("failed to import the OpenPGP certificate in BASE64 format (error: %d)\n", ret);
  62. return ret;
  63. }
  64. }
  65. if (gnutls_openpgp_crt_get_revoked_status(openpgp_crt)) {
  66. err("the primary key was revoked!\n");
  67. return 1;
  68. }
  69. if (ret = gnutls_openpgp_crt_get_key_usage(openpgp_crt, &usage), ret) {
  70. err("failed to get the usage flags for the primary key (error: %d)\n", ret);
  71. return ret;
  72. }
  73. if (usage & GNUTLS_KEY_KEY_AGREEMENT) {
  74. err("the primary key can be used for authentication\n");
  75. algo = gnutls_openpgp_crt_get_pk_algorithm(openpgp_crt, &bits);
  76. if (algo < 0) {
  77. err("failed to get the algorithm of the OpenPGP public key (error: %d)\n", algo);
  78. return algo;
  79. } else if (algo == GNUTLS_PK_RSA) {
  80. err("OpenPGP RSA certificate, with %d bits\n", bits);
  81. ret = gnutls_openpgp_crt_get_pk_rsa_raw(openpgp_crt, &m, &e);
  82. if (GNUTLS_E_SUCCESS != ret) {
  83. err ("failed to export RSA key parameters (error: %d)\n", ret);
  84. return 1;
  85. }
  86. } else if (algo == GNUTLS_PK_DSA) {
  87. err("OpenPGP DSA Key, with %d bits\n", bits);
  88. ret = gnutls_openpgp_crt_get_pk_dsa_raw(openpgp_crt, &p, &q, &g, &y);
  89. if (GNUTLS_E_SUCCESS != ret) {
  90. err ("failed to export DSA key parameters (error: %d)\n", ret);
  91. return 1;
  92. }
  93. } else {
  94. err("OpenPGP Key was not RSA or DSA -- can't deal! (actual algorithm was: %d)\n", algo);
  95. return 1;
  96. }
  97. } else {
  98. err("primary key is only good for: 0x%08x. Trying subkeys...\n", usage);
  99. if (ret = gnutls_openpgp_crt_get_auth_subkey(openpgp_crt, keyid), ret) {
  100. err("failed to find a subkey capable of authentication (error: %d)\n", ret);
  101. return ret;
  102. }
  103. make_keyid_printable(p_keyid, keyid);
  104. err("found authentication subkey %.16s\n", p_keyid);
  105. ret = gnutls_openpgp_crt_get_subkey_idx(openpgp_crt, keyid);
  106. if (ret < 0) {
  107. err("could not get the index of subkey %.16s (error: %d)\n", ret);
  108. return ret;
  109. }
  110. keyidx = ret;
  111. if (gnutls_openpgp_crt_get_subkey_revoked_status(openpgp_crt, keyidx)) {
  112. err("The authentication subkey was revoked!\n");
  113. return 1;
  114. }
  115. if (ret = gnutls_openpgp_crt_get_subkey_usage(openpgp_crt, keyidx, &usage), ret) {
  116. err("could not figure out usage of subkey %.16s (error: %d)\n", p_keyid, ret);
  117. return ret;
  118. }
  119. if ((usage & GNUTLS_KEY_KEY_AGREEMENT) == 0) {
  120. err("could not find a subkey with authentication privileges.\n");
  121. return 1;
  122. }
  123. /* switch, based on the algorithm in question, to extract the MPI
  124. components: */
  125. algo = gnutls_openpgp_crt_get_subkey_pk_algorithm(openpgp_crt, keyidx, &bits);
  126. if (algo < 0) {
  127. err("failed to get the algorithm of the authentication subkey (error: %d)\n", algo);
  128. return algo;
  129. } else if (algo == GNUTLS_PK_RSA) {
  130. err("OpenPGP RSA subkey, with %d bits\n", bits);
  131. ret = gnutls_openpgp_crt_get_subkey_pk_rsa_raw(openpgp_crt, keyidx, &m, &e);
  132. if (GNUTLS_E_SUCCESS != ret) {
  133. err ("failed to export RSA subkey parameters (error: %d)\n", ret);
  134. return 1;
  135. }
  136. } else if (algo == GNUTLS_PK_DSA) {
  137. err("OpenPGP DSA subkey, with %d bits\n", bits);
  138. ret = gnutls_openpgp_crt_get_subkey_pk_dsa_raw(openpgp_crt, keyidx, &p, &q, &g, &y);
  139. if (GNUTLS_E_SUCCESS != ret) {
  140. err ("failed to export DSA subkey parameters (error: %d)\n", ret);
  141. return 1;
  142. }
  143. } else {
  144. err("OpenPGP subkey was not RSA or DSA -- can't deal! (actual algorithm was: %d)\n", algo);
  145. return 1;
  146. }
  147. }
  148. /* now we have algo, and the various MPI data set. Can we export
  149. them cleanly? */
  150. gnutls_openpgp_crt_deinit(openpgp_crt);
  151. gnutls_global_deinit();
  152. return 0;
  153. }