1 #include "gnutls-helpers.h"
3 #include <gnutls/openpgp.h>
4 #include <gnutls/x509.h>
11 Author: Daniel Kahn Gillmor <dkg@fifthhorseman.net>
12 Date: Tue, 08 Apr 2008
13 License: GPL v3 or later
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.
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.
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.
28 int main(int argc, char* argv[]) {
31 gnutls_openpgp_crt_t openpgp_crt;
32 gnutls_openpgp_keyid_t keyid;
33 printable_keyid p_keyid;
35 unsigned int usage, bits;
36 gnutls_pk_algorithm_t algo;
38 gnutls_datum_t m, e, p, q, g, y;
39 gnutls_datum_t algolabel;
41 char output_data[10240];
43 size_t uidsz = sizeof(userid);
45 const gnutls_datum_t* all[5];
48 char* const args[] = {"/usr/bin/base64", "--wrap=0", NULL};
63 init_datum(&algolabel);
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");
74 if (ret = gnutls_openpgp_crt_init(&openpgp_crt), ret) {
75 err("Failed to initialize OpenPGP certificate (error: %d)\n", ret);
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: */
83 /* FIXME: we should be auto-detecting the input format, and
84 translating it as needed. */
86 if (getenv("MONKEYSPHERE_RAW")) {
87 err("assuming RAW formatted certificate\n");
88 if (ret = gnutls_openpgp_crt_import(openpgp_crt, &data, GNUTLS_OPENPGP_FMT_RAW), ret) {
89 err("failed to import the OpenPGP certificate in RAW format (error: %d)\n", ret);
93 err("assuming BASE64 formatted certificate\n");
94 if (ret = gnutls_openpgp_crt_import (openpgp_crt, &data, GNUTLS_OPENPGP_FMT_BASE64), ret) {
95 err("failed to import the OpenPGP certificate in BASE64 format (error: %d)\n", ret);
100 if (gnutls_openpgp_crt_get_revoked_status(openpgp_crt)) {
101 err("the primary key was revoked!\n");
105 /* FIXME: We're currently looking at the primary key or maybe the
106 first authentication-capable subkey.
108 Instead, we should be iterating through the primary key and all
109 subkeys: for each one with the authentication usage flag set of a
110 algorithm we can handle, we should output matching UserIDs and
111 the SSH version of the key. */
114 if (ret = gnutls_openpgp_crt_get_key_usage(openpgp_crt, &usage), ret) {
115 err("failed to get the usage flags for the primary key (error: %d)\n", ret);
118 if (usage & GNUTLS_KEY_KEY_AGREEMENT) {
119 err("the primary key can be used for authentication\n");
121 algo = gnutls_openpgp_crt_get_pk_algorithm(openpgp_crt, &bits);
123 err("failed to get the algorithm of the OpenPGP public key (error: %d)\n", algo);
125 } else if (algo == GNUTLS_PK_RSA) {
127 err("OpenPGP RSA certificate, with %d bits\n", bits);
128 ret = gnutls_openpgp_crt_get_pk_rsa_raw(openpgp_crt, &m, &e);
129 if (GNUTLS_E_SUCCESS != ret) {
130 err ("failed to export RSA key parameters (error: %d)\n", ret);
133 } else if (algo == GNUTLS_PK_DSA) {
134 err("OpenPGP DSA Key, with %d bits\n", bits);
135 ret = gnutls_openpgp_crt_get_pk_dsa_raw(openpgp_crt, &p, &q, &g, &y);
136 if (GNUTLS_E_SUCCESS != ret) {
137 err ("failed to export DSA key parameters (error: %d)\n", ret);
141 err("OpenPGP Key was not RSA or DSA -- can't deal! (actual algorithm was: %d)\n", algo);
146 err("primary key is only good for: 0x%08x. Trying subkeys...\n", usage);
148 if (ret = gnutls_openpgp_crt_get_auth_subkey(openpgp_crt, keyid), ret) {
149 err("failed to find a subkey capable of authentication (error: %d)\n", ret);
152 make_keyid_printable(p_keyid, keyid);
153 err("found authentication subkey %.16s\n", p_keyid);
155 ret = gnutls_openpgp_crt_get_subkey_idx(openpgp_crt, keyid);
157 err("could not get the index of subkey %.16s (error: %d)\n", ret);
162 if (gnutls_openpgp_crt_get_subkey_revoked_status(openpgp_crt, keyidx)) {
163 err("The authentication subkey was revoked!\n");
167 if (ret = gnutls_openpgp_crt_get_subkey_usage(openpgp_crt, keyidx, &usage), ret) {
168 err("could not figure out usage of subkey %.16s (error: %d)\n", p_keyid, ret);
171 if ((usage & GNUTLS_KEY_KEY_AGREEMENT) == 0) {
172 err("could not find a subkey with authentication privileges.\n");
176 /* switch, based on the algorithm in question, to extract the MPI
179 algo = gnutls_openpgp_crt_get_subkey_pk_algorithm(openpgp_crt, keyidx, &bits);
181 err("failed to get the algorithm of the authentication subkey (error: %d)\n", algo);
183 } else if (algo == GNUTLS_PK_RSA) {
185 err("OpenPGP RSA subkey, with %d bits\n", bits);
186 ret = gnutls_openpgp_crt_get_subkey_pk_rsa_raw(openpgp_crt, keyidx, &m, &e);
187 if (GNUTLS_E_SUCCESS != ret) {
188 err ("failed to export RSA subkey parameters (error: %d)\n", ret);
191 } else if (algo == GNUTLS_PK_DSA) {
192 err("OpenPGP DSA subkey, with %d bits\n", bits);
193 ret = gnutls_openpgp_crt_get_subkey_pk_dsa_raw(openpgp_crt, keyidx, &p, &q, &g, &y);
194 if (GNUTLS_E_SUCCESS != ret) {
195 err ("failed to export DSA subkey parameters (error: %d)\n", ret);
199 err("OpenPGP subkey was not RSA or DSA -- can't deal! (actual algorithm was: %d)\n", algo);
204 /* make sure userid is NULL-terminated */
205 userid[sizeof(userid) - 1] = 0;
208 /* FIXME: we're just choosing the first UserID from the certificate:
209 instead, we should be selecting every User ID that is adequately
210 signed and matches the spec, and aggregating them with commas for
211 known_hosts output */
213 if (ret = gnutls_openpgp_crt_get_name(openpgp_crt, 0, userid, &uidsz), ret) {
214 err("Failed to fetch the first UserID (error: %d)\n", ret);
218 if (ret = validate_ssh_host_userid(userid), ret) {
219 err("bad userid: not a valid ssh host.\n");
223 /* remove ssh:// from the beginning of userid */
224 memmove(userid, userid + strlen("ssh://"), 1 + strlen(userid) - strlen("ssh://"));
227 /* now we have algo, and the various MPI data are set. Can we
228 export them cleanly? */
230 /* for the moment, we'll just dump the info raw, and pipe it
231 externally through coreutils' /usr/bin/base64 */
233 if (algo == GNUTLS_PK_RSA) {
234 algoname = "ssh-rsa";
240 } else if (algo == GNUTLS_PK_DSA) {
241 algoname = "ssh-dss";
250 err("no idea what this algorithm is: %d\n", algo);
254 if (ret = datum_from_string(&algolabel, algoname), ret) {
255 err("couldn't label string (error: %d)\n", ret);
259 snprintf(output_data, sizeof(output_data), "%s %s ", userid, algoname);
261 write(1, output_data, strlen(output_data));
263 pipefd = create_writing_pipe(&child_pid, args[0], args);
265 err("failed to create a writing pipe (returned %d)\n", pipefd);
269 if (0 != write_data_fd_with_length(pipefd, all, mpicount)) {
270 err("was not able to write out RSA key data\n");
274 if (child_pid != waitpid(child_pid, NULL, 0)) {
275 err("could not wait for child process to return for some reason.\n");
283 gnutls_openpgp_crt_deinit(openpgp_crt);
284 gnutls_global_deinit();