summaryrefslogtreecommitdiff
path: root/src/gpg2ssh/gpg2ssh.c
blob: c99f03fe25fbc20ca4fe490791a4b8bac2416cc8 (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. const gnutls_datum_t* all[5];
  35. int pipefd;
  36. pid_t child_pid;
  37. char* const args[] = {"/usr/bin/base64", "--wrap=0", NULL};
  38. const char* algoname;
  39. int mpicount;
  40. int pipestatus;
  41. init_gnutls();
  42. init_datum(&data);
  43. init_datum(&m);
  44. init_datum(&e);
  45. init_datum(&p);
  46. init_datum(&q);
  47. init_datum(&g);
  48. init_datum(&y);
  49. init_datum(&algolabel);
  50. init_keyid(keyid);
  51. /* slurp in the private key from stdin */
  52. if (ret = set_datum_fd(&data, 0), ret) {
  53. err("didn't read file descriptor 0\n");
  54. return 1;
  55. }
  56. if (ret = gnutls_openpgp_crt_init(&openpgp_crt), ret) {
  57. err("Failed to initialize OpenPGP certificate (error: %d)\n", ret);
  58. return 1;
  59. }
  60. /* format could be either: GNUTLS_OPENPGP_FMT_RAW,
  61. GNUTLS_OPENPGP_FMT_BASE64; if MONKEYSPHERE_RAW is set, use RAW,
  62. otherwise, use BASE64: */
  63. /* FIXME: we should be auto-detecting the input format, and
  64. translating it as needed. */
  65. if (getenv("MONKEYSPHERE_RAW")) {
  66. err("assuming RAW formatted certificate\n");
  67. if (ret = gnutls_openpgp_crt_import(openpgp_crt, &data, GNUTLS_OPENPGP_FMT_RAW), ret) {
  68. err("failed to import the OpenPGP certificate in RAW format (error: %d)\n", ret);
  69. return ret;
  70. }
  71. } else {
  72. err("assuming BASE64 formatted certificate\n");
  73. if (ret = gnutls_openpgp_crt_import (openpgp_crt, &data, GNUTLS_OPENPGP_FMT_BASE64), ret) {
  74. err("failed to import the OpenPGP certificate in BASE64 format (error: %d)\n", ret);
  75. return ret;
  76. }
  77. }
  78. if (gnutls_openpgp_crt_get_revoked_status(openpgp_crt)) {
  79. err("the primary key was revoked!\n");
  80. return 1;
  81. }
  82. /* FIXME: We're currently looking at the primary key or maybe the
  83. first authentication-capable subkey.
  84. Instead, we should be iterating through the primary key and all
  85. subkeys: for each one with the authentication usage flag set of a
  86. algorithm we can handle, we should output matching UserIDs and
  87. the SSH version of the key. */
  88. if (ret = gnutls_openpgp_crt_get_key_usage(openpgp_crt, &usage), ret) {
  89. err("failed to get the usage flags for the primary key (error: %d)\n", ret);
  90. return ret;
  91. }
  92. if (usage & GNUTLS_KEY_KEY_AGREEMENT &&
  93. usage & GNUTLS_KEY_KEY_ENCIPHERMENT) {
  94. err("the primary key can be used for authentication and communication encryption!\n");
  95. algo = gnutls_openpgp_crt_get_pk_algorithm(openpgp_crt, &bits);
  96. if (algo < 0) {
  97. err("failed to get the algorithm of the OpenPGP public key (error: %d)\n", algo);
  98. return algo;
  99. } else if (algo == GNUTLS_PK_RSA) {
  100. err("OpenPGP RSA certificate, with %d bits\n", bits);
  101. ret = gnutls_openpgp_crt_get_pk_rsa_raw(openpgp_crt, &m, &e);
  102. if (GNUTLS_E_SUCCESS != ret) {
  103. err ("failed to export RSA key parameters (error: %d)\n", ret);
  104. return 1;
  105. }
  106. } else if (algo == GNUTLS_PK_DSA) {
  107. err("OpenPGP DSA Key, with %d bits\n", bits);
  108. ret = gnutls_openpgp_crt_get_pk_dsa_raw(openpgp_crt, &p, &q, &g, &y);
  109. if (GNUTLS_E_SUCCESS != ret) {
  110. err ("failed to export DSA key parameters (error: %d)\n", ret);
  111. return 1;
  112. }
  113. } else {
  114. err("OpenPGP Key was not RSA or DSA -- can't deal! (actual algorithm was: %d)\n", algo);
  115. return 1;
  116. }
  117. } else {
  118. err("primary key is not good for authentication and communication encryption. Trying subkeys...\n");
  119. if (ret = gnutls_openpgp_crt_get_auth_subkey(openpgp_crt, keyid, 0), ret) {
  120. err("failed to find a subkey capable of authentication and communication encryption (error: %d)\n", ret);
  121. return ret;
  122. }
  123. make_keyid_printable(p_keyid, keyid);
  124. err("found authentication subkey %.16s\n", p_keyid);
  125. ret = gnutls_openpgp_crt_get_subkey_idx(openpgp_crt, keyid);
  126. if (ret < 0) {
  127. err("could not get the index of subkey %.16s (error: %d)\n", ret);
  128. return ret;
  129. }
  130. keyidx = ret;
  131. if (gnutls_openpgp_crt_get_subkey_revoked_status(openpgp_crt, keyidx)) {
  132. err("The authentication subkey was revoked!\n");
  133. return 1;
  134. }
  135. if (ret = gnutls_openpgp_crt_get_subkey_usage(openpgp_crt, keyidx, &usage), ret) {
  136. err("could not figure out usage of subkey %.16s (error: %d)\n", p_keyid, ret);
  137. return ret;
  138. }
  139. if ((usage & GNUTLS_KEY_KEY_AGREEMENT) == 0 &&
  140. usage & GNUTLS_KEY_KEY_ENCIPHERMENT) {
  141. err("could not find a subkey with authentication and communication encryption.\n");
  142. return 1;
  143. }
  144. /* switch, based on the algorithm in question, to extract the MPI
  145. components: */
  146. algo = gnutls_openpgp_crt_get_subkey_pk_algorithm(openpgp_crt, keyidx, &bits);
  147. if (algo < 0) {
  148. err("failed to get the algorithm of the authentication subkey (error: %d)\n", algo);
  149. return algo;
  150. } else if (algo == GNUTLS_PK_RSA) {
  151. err("OpenPGP RSA subkey, with %d bits\n", bits);
  152. ret = gnutls_openpgp_crt_get_subkey_pk_rsa_raw(openpgp_crt, keyidx, &m, &e);
  153. if (GNUTLS_E_SUCCESS != ret) {
  154. err ("failed to export RSA subkey parameters (error: %d)\n", ret);
  155. return 1;
  156. }
  157. } else if (algo == GNUTLS_PK_DSA) {
  158. err("OpenPGP DSA subkey, with %d bits\n", bits);
  159. ret = gnutls_openpgp_crt_get_subkey_pk_dsa_raw(openpgp_crt, keyidx, &p, &q, &g, &y);
  160. if (GNUTLS_E_SUCCESS != ret) {
  161. err ("failed to export DSA subkey parameters (error: %d)\n", ret);
  162. return 1;
  163. }
  164. } else {
  165. err("OpenPGP subkey was not RSA or DSA -- can't deal! (actual algorithm was: %d)\n", algo);
  166. return 1;
  167. }
  168. }
  169. /* make sure userid is NULL-terminated */
  170. userid[sizeof(userid) - 1] = 0;
  171. uidsz--;
  172. /* FIXME: we're just choosing the first UserID from the certificate:
  173. instead, we should be selecting every User ID that is adequately
  174. signed and matches the spec, and aggregating them with commas for
  175. known_hosts output */
  176. if (ret = gnutls_openpgp_crt_get_name(openpgp_crt, 0, userid, &uidsz), ret) {
  177. err("Failed to fetch the first UserID (error: %d)\n", ret);
  178. return ret;
  179. }
  180. if (ret = validate_ssh_host_userid(userid), ret) {
  181. err("bad userid: not a valid ssh host.\n");
  182. return ret;
  183. }
  184. /* remove ssh:// from the beginning of userid */
  185. memmove(userid, userid + strlen("ssh://"), 1 + strlen(userid) - strlen("ssh://"));
  186. /* now we have algo, and the various MPI data are set. Can we
  187. export them cleanly? */
  188. /* for the moment, we'll just dump the info raw, and pipe it
  189. externally through coreutils' /usr/bin/base64 */
  190. if (algo == GNUTLS_PK_RSA) {
  191. algoname = "ssh-rsa";
  192. mpicount = 3;
  193. all[0] = &algolabel;
  194. all[1] = &e;
  195. all[2] = &m;
  196. } else if (algo == GNUTLS_PK_DSA) {
  197. algoname = "ssh-dss";
  198. mpicount = 5;
  199. all[0] = &algolabel;
  200. all[1] = &p;
  201. all[2] = &q;
  202. all[3] = &g;
  203. all[4] = &y;
  204. } else {
  205. err("no idea what this algorithm is: %d\n", algo);
  206. return 1;
  207. }
  208. if (ret = datum_from_string(&algolabel, algoname), ret) {
  209. err("couldn't label string (error: %d)\n", ret);
  210. return ret;
  211. }
  212. snprintf(output_data, sizeof(output_data), "%s %s ", userid, algoname);
  213. pipefd = create_writing_pipe(&child_pid, args[0], args);
  214. if (pipefd < 0) {
  215. err("failed to create a writing pipe (returned %d)\n", pipefd);
  216. return pipefd;
  217. }
  218. write(1, output_data, strlen(output_data));
  219. if (0 != write_data_fd_with_length(pipefd, all, mpicount)) {
  220. err("was not able to write out RSA key data\n");
  221. return 1;
  222. }
  223. close(pipefd);
  224. if (child_pid != waitpid(child_pid, &pipestatus, 0)) {
  225. err("could not wait for child process to return for some reason.\n");
  226. return 1;
  227. }
  228. if (pipestatus != 0) {
  229. err("base64 pipe died with return code %d\n", pipestatus);
  230. return pipestatus;
  231. }
  232. write(1, "\n", 1);
  233. gnutls_openpgp_crt_deinit(openpgp_crt);
  234. gnutls_global_deinit();
  235. return 0;
  236. }