summaryrefslogtreecommitdiff
path: root/gpg2ssh.c
blob: f5bd55f54287f45efcb5785c99e40d4bb15b8d5e (plain)
  1. #include "gnutls-helpers.h"
  2. #include <gnutls/openpgp.h>
  3. #include <gnutls/x509.h>
  4. /* for waitpid() */
  5. #include <sys/types.h>
  6. #include <sys/wait.h>
  7. /*
  8. Author: Daniel Kahn Gillmor <dkg@fifthhorseman.net>
  9. Date: Tue, 08 Apr 2008
  10. License: GPL v3 or later
  11. monkeysphere public key translator: execute this with an GPG
  12. certificate (public key(s) + userid(s)) on stdin. It currently
  13. only works with RSA keys.
  14. It will spit out a version of the first key capable of being used
  15. for authentication on stdout. The output format should be suitable
  16. for appending a known_hosts file.
  17. Requirements: I've only built this so far with GnuTLS v2.3.4 --
  18. version 2.2.0 does not contain the appropriate pieces.
  19. */
  20. int main(int argc, char* argv[]) {
  21. gnutls_datum_t data;
  22. int ret;
  23. gnutls_openpgp_crt_t openpgp_crt;
  24. gnutls_openpgp_keyid_t keyid;
  25. printable_keyid p_keyid;
  26. unsigned int keyidx;
  27. unsigned int usage, bits;
  28. gnutls_pk_algorithm_t algo;
  29. gnutls_datum_t m, e, p, q, g, y;
  30. gnutls_datum_t algolabel;
  31. char output_data[10240];
  32. char userid[10240];
  33. size_t uidsz = sizeof(userid);
  34. init_gnutls();
  35. init_datum(&data);
  36. init_datum(&m);
  37. init_datum(&e);
  38. init_datum(&p);
  39. init_datum(&q);
  40. init_datum(&g);
  41. init_datum(&y);
  42. init_datum(&algolabel);
  43. init_keyid(keyid);
  44. /* slurp in the private key from stdin */
  45. if (ret = set_datum_fd(&data, 0), ret) {
  46. err("didn't read file descriptor 0\n");
  47. return 1;
  48. }
  49. if (ret = gnutls_openpgp_crt_init(&openpgp_crt), ret) {
  50. err("Failed to initialize OpenPGP certificate (error: %d)\n", ret);
  51. return 1;
  52. }
  53. /* format could be either: GNUTLS_OPENPGP_FMT_RAW,
  54. GNUTLS_OPENPGP_FMT_BASE64; if MONKEYSPHERE_RAW is set, use RAW,
  55. otherwise, use BASE64: */
  56. if (getenv("MONKEYSPHERE_RAW")) {
  57. err("assuming RAW formatted certificate\n");
  58. if (ret = gnutls_openpgp_crt_import(openpgp_crt, &data, GNUTLS_OPENPGP_FMT_RAW), ret) {
  59. err("failed to import the OpenPGP certificate in RAW format (error: %d)\n", ret);
  60. return ret;
  61. }
  62. } else {
  63. err("assuming BASE64 formatted certificate\n");
  64. if (ret = gnutls_openpgp_crt_import (openpgp_crt, &data, GNUTLS_OPENPGP_FMT_BASE64), ret) {
  65. err("failed to import the OpenPGP certificate in BASE64 format (error: %d)\n", ret);
  66. return ret;
  67. }
  68. }
  69. if (gnutls_openpgp_crt_get_revoked_status(openpgp_crt)) {
  70. err("the primary key was revoked!\n");
  71. return 1;
  72. }
  73. if (ret = gnutls_openpgp_crt_get_key_usage(openpgp_crt, &usage), ret) {
  74. err("failed to get the usage flags for the primary key (error: %d)\n", ret);
  75. return ret;
  76. }
  77. if (usage & GNUTLS_KEY_KEY_AGREEMENT) {
  78. err("the primary key can be used for authentication\n");
  79. algo = gnutls_openpgp_crt_get_pk_algorithm(openpgp_crt, &bits);
  80. if (algo < 0) {
  81. err("failed to get the algorithm of the OpenPGP public key (error: %d)\n", algo);
  82. return algo;
  83. } else if (algo == GNUTLS_PK_RSA) {
  84. err("OpenPGP RSA certificate, with %d bits\n", bits);
  85. ret = gnutls_openpgp_crt_get_pk_rsa_raw(openpgp_crt, &m, &e);
  86. if (GNUTLS_E_SUCCESS != ret) {
  87. err ("failed to export RSA key parameters (error: %d)\n", ret);
  88. return 1;
  89. }
  90. } else if (algo == GNUTLS_PK_DSA) {
  91. err("OpenPGP DSA Key, with %d bits\n", bits);
  92. ret = gnutls_openpgp_crt_get_pk_dsa_raw(openpgp_crt, &p, &q, &g, &y);
  93. if (GNUTLS_E_SUCCESS != ret) {
  94. err ("failed to export DSA key parameters (error: %d)\n", ret);
  95. return 1;
  96. }
  97. } else {
  98. err("OpenPGP Key was not RSA or DSA -- can't deal! (actual algorithm was: %d)\n", algo);
  99. return 1;
  100. }
  101. } else {
  102. err("primary key is only good for: 0x%08x. Trying subkeys...\n", usage);
  103. if (ret = gnutls_openpgp_crt_get_auth_subkey(openpgp_crt, keyid), ret) {
  104. err("failed to find a subkey capable of authentication (error: %d)\n", ret);
  105. return ret;
  106. }
  107. make_keyid_printable(p_keyid, keyid);
  108. err("found authentication subkey %.16s\n", p_keyid);
  109. ret = gnutls_openpgp_crt_get_subkey_idx(openpgp_crt, keyid);
  110. if (ret < 0) {
  111. err("could not get the index of subkey %.16s (error: %d)\n", ret);
  112. return ret;
  113. }
  114. keyidx = ret;
  115. if (gnutls_openpgp_crt_get_subkey_revoked_status(openpgp_crt, keyidx)) {
  116. err("The authentication subkey was revoked!\n");
  117. return 1;
  118. }
  119. if (ret = gnutls_openpgp_crt_get_subkey_usage(openpgp_crt, keyidx, &usage), ret) {
  120. err("could not figure out usage of subkey %.16s (error: %d)\n", p_keyid, ret);
  121. return ret;
  122. }
  123. if ((usage & GNUTLS_KEY_KEY_AGREEMENT) == 0) {
  124. err("could not find a subkey with authentication privileges.\n");
  125. return 1;
  126. }
  127. /* switch, based on the algorithm in question, to extract the MPI
  128. components: */
  129. algo = gnutls_openpgp_crt_get_subkey_pk_algorithm(openpgp_crt, keyidx, &bits);
  130. if (algo < 0) {
  131. err("failed to get the algorithm of the authentication subkey (error: %d)\n", algo);
  132. return algo;
  133. } else if (algo == GNUTLS_PK_RSA) {
  134. err("OpenPGP RSA subkey, with %d bits\n", bits);
  135. ret = gnutls_openpgp_crt_get_subkey_pk_rsa_raw(openpgp_crt, keyidx, &m, &e);
  136. if (GNUTLS_E_SUCCESS != ret) {
  137. err ("failed to export RSA subkey parameters (error: %d)\n", ret);
  138. return 1;
  139. }
  140. } else if (algo == GNUTLS_PK_DSA) {
  141. err("OpenPGP DSA subkey, with %d bits\n", bits);
  142. ret = gnutls_openpgp_crt_get_subkey_pk_dsa_raw(openpgp_crt, keyidx, &p, &q, &g, &y);
  143. if (GNUTLS_E_SUCCESS != ret) {
  144. err ("failed to export DSA subkey parameters (error: %d)\n", ret);
  145. return 1;
  146. }
  147. } else {
  148. err("OpenPGP subkey was not RSA or DSA -- can't deal! (actual algorithm was: %d)\n", algo);
  149. return 1;
  150. }
  151. }
  152. /* make sure userid is NULL-terminated */
  153. userid[sizeof(userid) - 1] = 0;
  154. uidsz--;
  155. /* FIXME: we're just choosing the first UserID from the certificate:
  156. instead, we should be choosing the one that's adequately signed,
  157. and matches the monkeysphere specification. */
  158. if (ret = gnutls_openpgp_crt_get_name(openpgp_crt, 0, userid, &uidsz), ret) {
  159. err("Failed to fetch the first UserID (error: %d)\n", ret);
  160. return ret;
  161. }
  162. if (ret = validate_ssh_host_userid(userid), ret) {
  163. err("bad userid: not a valid ssh host.\n");
  164. return ret;
  165. }
  166. /* remove ssh:// from the beginning of userid */
  167. memmove(userid, userid + strlen("ssh://"), 1 + strlen(userid) - strlen("ssh://"));
  168. /* now we have algo, and the various MPI data are set. Can we
  169. export them cleanly? */
  170. /* for the moment, we'll just dump the info raw, and pipe it
  171. externally through coreutils' /usr/bin/base64 */
  172. if (algo == GNUTLS_PK_RSA) {
  173. const gnutls_datum_t* all[3];
  174. int pipefd;
  175. pid_t child_pid;
  176. char* const args[] = {"/usr/bin/base64", "--wrap=0", NULL};
  177. const char* algoname = "ssh-rsa";
  178. snprintf(output_data, sizeof(output_data), "%s %s ", userid, algoname);
  179. write(1, output_data, strlen(output_data));
  180. pipefd = create_writing_pipe(&child_pid, args[0], args);
  181. if (pipefd < 0) {
  182. err("failed to create a writing pipe (returned %d)\n", pipefd);
  183. return pipefd;
  184. }
  185. if (ret = datum_from_string(&algolabel, algoname), ret) {
  186. err("couldn't label string (error: %d)\n", ret);
  187. return ret;
  188. }
  189. all[0] = &algolabel;
  190. all[1] = &e;
  191. all[2] = &m;
  192. if (0 != write_data_fd_with_length(pipefd, all, 3)) {
  193. err("was not able to write out RSA key data\n");
  194. return 1;
  195. }
  196. close(pipefd);
  197. if (child_pid != waitpid(child_pid, NULL, 0)) {
  198. err("could not wait for child process to return for some reason.\n");
  199. return 1;
  200. }
  201. write(1, "\n", 1);
  202. } else if (algo == GNUTLS_PK_DSA) {
  203. err("Don't know how to export DSA ssh pubkeys yet.\n");
  204. return 1;
  205. } else {
  206. err("no idea what this algorithm is: %d\n", algo);
  207. return 1;
  208. }
  209. gnutls_openpgp_crt_deinit(openpgp_crt);
  210. gnutls_global_deinit();
  211. return 0;
  212. }