initial import
[monkeysphere.git] / main.c
1 #include <gnutls/gnutls.h>
2 #include <gnutls/openpgp.h>
3 #include <gnutls/x509.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <unistd.h>
10 #include <stdarg.h>
11
12 void err(const char* fmt, ...) {
13   static FILE* STDERR = NULL;
14   va_list ap;
15
16   if (NULL == STDERR)
17     STDERR = fdopen(STDERR_FILENO, "a");
18   va_start(ap, fmt);
19   vfprintf(STDERR, fmt, ap);
20   va_end(ap);
21 }
22
23
24 void init_datum(gnutls_datum_t* d) {
25   d->data = NULL;
26   d->size = 0;
27 }
28 void free_datum(gnutls_datum_t* d) {
29   gnutls_free(d->data);
30   d->data = NULL;
31   d->size = 0;
32 }
33
34 /* read the passed-in string, store in a single datum */
35 int set_datum_string(gnutls_datum_t* d, const char* s) {
36   unsigned int x = strlen(s)+1;
37   unsigned char* c = NULL;
38
39   c = gnutls_realloc(d->data, x);
40   if (NULL == c)
41     return -1;
42   d->data = c;
43   d->size = x;
44   memcpy(d->data, s, x);
45   return 0;
46 }
47
48 /* read the passed-in file descriptor until EOF, store in a single
49    datum */
50 int set_datum_fd(gnutls_datum_t* d, int fd) {
51   unsigned int bufsize = 1024;
52   unsigned int len = 0;
53
54   FILE* f = NULL;
55   if (bufsize > d->size) {
56     bufsize = 1024;
57     if (gnutls_realloc(d->data, bufsize) == NULL) {
58       err("out of memory!\n");
59       return -1;
60     }
61     d->size = bufsize;
62   } else {
63     bufsize = d->size;
64   }
65   f = fdopen(fd, "r");
66   while (!feof(f) && !ferror(f)) {
67     if (len == bufsize) {
68       /* allocate more space by doubling: */
69       bufsize *= 2;
70       if (gnutls_realloc(d->data, bufsize) == NULL) {
71         err("out of memory!\n"); 
72         return -1;
73       };
74       d->size = bufsize;
75     }
76     len += fread(d->data + len, 1, bufsize - len, f);
77   }
78   if (ferror(f)) {
79     err("Error reading from fd %d\n", fd);
80     return -1;
81   }
82   /* touch up buffer size to match reality: */
83   gnutls_realloc(d->data, len);
84   d->size = len;
85   return 0;
86 }
87
88 /* read the file indicated (by na1me) in the fname parameter.  store
89    its entire contents in a single datum. */
90 int set_datum_file(gnutls_datum_t* d, const char* fname) {
91   struct stat sbuf;
92   unsigned char* c = NULL;
93   FILE* file = NULL;
94   size_t x = 0;
95
96   if (0 != stat(fname, &sbuf)) {
97     err("failed to stat '%s'\n", fname);
98     return -1;
99   }
100   
101   c = gnutls_realloc(d->data, sbuf.st_size);
102   if (NULL == c) {
103     err("failed to allocate %d bytes for '%s'\n", sbuf.st_size, fname);
104     return -1;
105   }
106
107   d->data = c;
108   d->size = sbuf.st_size;
109   file = fopen(fname, "r");
110   if (NULL == file) {
111     err("failed to open '%s' for reading\n",  fname);
112     return -1;
113   }
114
115   x = fread(d->data, d->size, 1, file);
116   if (x != 1) {
117     err("tried to read %d bytes, read %d instead from '%s'\n", d->size, x, fname);
118     fclose(file);
119     return -1;
120   }
121   fclose(file);
122   return 0;
123 }
124
125
126 int main(int argc, char* argv[]) {
127   const char* version = NULL;
128
129   gnutls_x509_privkey_t x509_privkey;
130   gnutls_datum_t data;
131   int ret;
132
133   /*  
134       const char *certfile, *keyfile;
135       gnutls_certificate_credentials_t pgp_creds;
136   */
137   gnutls_datum_t m, e, d, p, q, u;
138   gnutls_x509_crt_t crt;
139
140   gnutls_openpgp_privkey_t pgp_privkey;
141   gnutls_openpgp_crt_fmt_t pgp_format;
142   gnutls_pk_algorithm_t pgp_algo;
143   unsigned int pgp_bits;
144
145   char output_data[10240];
146   size_t ods = sizeof(output_data);
147
148   init_datum(&data);
149
150   if (ret = gnutls_global_init(), ret) {
151     err("Failed to do gnutls_global_init() (error: %d)\n", ret);
152     return 1;
153   }
154
155
156
157   version = gnutls_check_version(NULL);
158
159   if (version) 
160     printf("gnutls version: %s\n", version);
161   else {
162     printf("no version found!\n");
163     return 1;
164   }
165
166   if (ret = gnutls_x509_privkey_init(&x509_privkey), ret) {
167     err("Failed to initialize X.509 private key (error: %d)\n", ret);
168     return 1;
169   }
170
171   if (ret = gnutls_openpgp_privkey_init(&pgp_privkey), ret) {
172     err("Failed to initialized OpenPGP private key (error: %d)\n", ret);
173     return 1;
174   }
175
176   /* how do we initialize data? */
177
178     /* reading from the file descriptor doesn't work right yet:
179       if (ret = set_datum_fd(&data, 0), ret) {
180       err("didn't read file descriptor 0\n");
181       return 1;
182       }
183     */
184
185   if (ret = set_datum_file(&data, argv[1]), ret) {
186     err("didn't read file '%s'\n", argv[1]);
187     return 1;
188   }
189
190   /* treat the passed file as an X.509 private key, and extract its
191      component values: */
192
193 /*   if (ret = gnutls_x509_privkey_import(x509_privkey, &data, GNUTLS_X509_FMT_PEM), ret) { */
194 /*     err("Failed to import the X.509 key (error: %d)\n", ret); */
195 /*     return 1; */
196 /*   } */
197 /*   gnutls_x509_privkey_export_rsa_raw(x509_privkey, &m, &e, &d, &p, &q, &u); */
198
199   /* try to print the PEM-encoded private key: */
200 /*   ret = gnutls_x509_privkey_export (x509_privkey, */
201 /*                                  GNUTLS_X509_FMT_PEM, */
202 /*                                  output_data, */
203 /*                                  &ods); */
204 /*   printf("ret: %u; ods: %u;\n", ret, ods); */
205 /*   if (ret == 0) { */
206 /*     write(0, output_data, ods); */
207 /*   } */
208
209   
210   /* format could be either: GNUTLS_OPENPGP_FMT_RAW,
211      GNUTLS_OPENPGP_FMT_BASE64 */
212   pgp_format = GNUTLS_OPENPGP_FMT_RAW;
213   if (ret = gnutls_openpgp_privkey_import (pgp_privkey, &data, pgp_format, NULL, 0), ret) {
214     err("failed to import the OpenPGP private key (error: %d)\n", ret);
215     return 1;
216   }
217   pgp_algo = gnutls_openpgp_privkey_get_pk_algorithm(pgp_privkey, &pgp_bits);
218   if (pgp_algo < 0) {
219     err("failed to get OpenPGP key algorithm (error: %d)\n", pgp_algo);
220     return 1;
221   }
222   if (pgp_algo != GNUTLS_PK_RSA) {
223     err("OpenPGP Key was not RSA (actual algorithm was: %d)\n", pgp_algo);
224     return 1;
225   }
226   
227   printf("OpenPGP RSA Key, with %d bits\n", pgp_bits);
228
229
230   ret = gnutls_x509_privkey_export (pgp_privkey,
231                                     GNUTLS_X509_FMT_PEM,
232                                     output_data,
233                                     &ods);
234   printf("ret: %u; ods: %u;\n", ret, ods);
235   if (ret == 0) {
236     write(0, output_data, ods);
237   }
238
239
240   gnutls_x509_privkey_deinit(x509_privkey);
241   gnutls_openpgp_privkey_deinit(pgp_privkey);
242   gnutls_global_deinit();
243   return 0;
244 }