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