working with subkeys: preparing to write a gnutls-based gpg2ssh capable of feeding...
[monkeysphere.git] / gpg2ssh.c
1 #include "gnutls-helpers.h"
2
3 #include <gnutls/openpgp.h>
4 #include <gnutls/x509.h>
5
6 /* for htonl() */
7 #include <arpa/inet.h>
8
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
40   char output_data[10240];
41   size_t ods = sizeof(output_data);
42
43   init_gnutls();
44   
45   init_datum(&data);
46
47   init_datum(&m);
48   init_datum(&e);
49   init_datum(&p);
50   init_datum(&q);
51   init_datum(&g);
52   init_datum(&y);
53
54   init_keyid(keyid);
55
56   /* slurp in the private key from stdin */
57   if (ret = set_datum_fd(&data, 0), ret) {
58     err("didn't read file descriptor 0\n");
59     return 1;
60   }
61
62
63   if (ret = gnutls_openpgp_crt_init(&openpgp_crt), ret) {
64     err("Failed to initialize OpenPGP certificate (error: %d)\n", ret);
65     return 1;
66   }
67
68   /* format could be either: GNUTLS_OPENPGP_FMT_RAW,
69      GNUTLS_OPENPGP_FMT_BASE64; if MONKEYSPHERE_RAW is set, use RAW,
70      otherwise, use BASE64: */
71
72   if (getenv("MONKEYSPHERE_RAW")) {
73     err("assuming RAW formatted certificate\n");
74     if (ret = gnutls_openpgp_crt_import(openpgp_crt, &data, GNUTLS_OPENPGP_FMT_RAW), ret) {
75       err("failed to import the OpenPGP certificate in RAW format (error: %d)\n", ret);
76       return ret;
77     }
78   } else {
79     err("assuming BASE64 formatted certificate\n");
80     if (ret = gnutls_openpgp_crt_import (openpgp_crt, &data, GNUTLS_OPENPGP_FMT_BASE64), ret) {
81       err("failed to import the OpenPGP certificate in BASE64 format (error: %d)\n", ret);
82       return ret;
83     }
84   }
85
86   if (gnutls_openpgp_crt_get_revoked_status(openpgp_crt)) {
87     err("the primary key was revoked!\n");
88     return 1;
89   }
90
91   if (ret = gnutls_openpgp_crt_get_key_usage(openpgp_crt, &usage), ret) {
92     err("failed to get the usage flags for the primary key (error: %d)\n", ret);
93     return ret;
94   }
95   if (usage & GNUTLS_KEY_KEY_AGREEMENT) {
96     err("the primary key can be used for authentication\n");
97
98     algo = gnutls_openpgp_crt_get_pk_algorithm(openpgp_crt, &bits);
99     if (algo < 0) {
100       err("failed to get the algorithm of the OpenPGP public key (error: %d)\n", algo);
101       return algo;
102     } else if (algo == GNUTLS_PK_RSA) {
103       
104       err("OpenPGP RSA certificate, with %d bits\n", bits);
105       ret = gnutls_openpgp_crt_get_pk_rsa_raw(openpgp_crt, &m, &e);
106       if (GNUTLS_E_SUCCESS != ret) {
107         err ("failed to export RSA key parameters (error: %d)\n", ret);
108         return 1;
109       }
110     } else if (algo == GNUTLS_PK_DSA) {
111       err("OpenPGP DSA Key, with %d bits\n", bits);
112       ret = gnutls_openpgp_crt_get_pk_dsa_raw(openpgp_crt, &p, &q, &g, &y);
113       if (GNUTLS_E_SUCCESS != ret) {
114         err ("failed to export DSA key parameters (error: %d)\n", ret);
115         return 1;
116       }
117     } else {
118       err("OpenPGP Key was not RSA or DSA -- can't deal! (actual algorithm was: %d)\n", algo);
119       return 1;
120     }
121     
122   } else {
123     err("primary key is only good for: 0x%08x.  Trying subkeys...\n", usage);
124     
125     if (ret = gnutls_openpgp_crt_get_auth_subkey(openpgp_crt, keyid), ret) {
126       err("failed to find a subkey capable of authentication (error: %d)\n", ret);
127       return ret;
128     }
129     make_keyid_printable(p_keyid, keyid);
130     err("found authentication subkey %.16s\n", p_keyid);
131
132     ret = gnutls_openpgp_crt_get_subkey_idx(openpgp_crt, keyid);
133     if (ret < 0) {
134       err("could not get the index of subkey %.16s (error: %d)\n", ret);
135       return ret;
136     }
137     keyidx = ret;
138
139     if (gnutls_openpgp_crt_get_subkey_revoked_status(openpgp_crt, keyidx)) {
140       err("The authentication subkey was revoked!\n");
141       return 1;
142     }
143
144     if (ret = gnutls_openpgp_crt_get_subkey_usage(openpgp_crt, keyidx, &usage), ret) {
145       err("could not figure out usage of subkey %.16s (error: %d)\n", p_keyid, ret);
146       return ret;
147     }
148     if ((usage & GNUTLS_KEY_KEY_AGREEMENT) == 0) {
149       err("could not find a subkey with authentication privileges.\n");
150       return 1;
151     }
152
153     /* switch, based on the algorithm in question, to extract the MPI
154        components: */
155
156     algo = gnutls_openpgp_crt_get_subkey_pk_algorithm(openpgp_crt, keyidx, &bits);
157     if (algo < 0) {
158       err("failed to get the algorithm of the authentication subkey (error: %d)\n", algo);
159       return algo;
160     } else if (algo == GNUTLS_PK_RSA) {
161       
162       err("OpenPGP RSA subkey, with %d bits\n", bits);
163       ret = gnutls_openpgp_crt_get_subkey_pk_rsa_raw(openpgp_crt, keyidx, &m, &e);
164       if (GNUTLS_E_SUCCESS != ret) {
165         err ("failed to export RSA subkey parameters (error: %d)\n", ret);
166         return 1;
167       }
168     } else if (algo == GNUTLS_PK_DSA) {
169       err("OpenPGP DSA subkey, with %d bits\n", bits);
170       ret = gnutls_openpgp_crt_get_subkey_pk_dsa_raw(openpgp_crt, keyidx, &p, &q, &g, &y);
171       if (GNUTLS_E_SUCCESS != ret) {
172         err ("failed to export DSA subkey parameters (error: %d)\n", ret);
173         return 1;
174       }
175     } else {
176       err("OpenPGP subkey was not RSA or DSA -- can't deal! (actual algorithm was: %d)\n", algo);
177       return 1;
178     }
179   } 
180
181   /* now we have algo, and the various MPI data set.  Can we export
182      them cleanly? */
183
184   
185
186   gnutls_openpgp_crt_deinit(openpgp_crt);
187   gnutls_global_deinit();
188   return 0;
189 }