1 #include <gnutls/gnutls.h>
2 #include <gnutls/openpgp.h>
3 #include <gnutls/x509.h>
12 void err(const char* fmt, ...) {
13 static FILE* STDERR = NULL;
17 STDERR = fdopen(STDERR_FILENO, "a");
19 vfprintf(STDERR, fmt, ap);
24 void init_datum(gnutls_datum_t* d) {
28 void free_datum(gnutls_datum_t* d) {
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;
39 c = gnutls_realloc(d->data, x);
44 memcpy(d->data, s, x);
48 /* read the passed-in file descriptor until EOF, store in a single
50 int set_datum_fd(gnutls_datum_t* d, int fd) {
51 unsigned int bufsize = 1024;
55 if (bufsize > d->size) {
57 if (gnutls_realloc(d->data, bufsize) == NULL) {
58 err("out of memory!\n");
66 while (!feof(f) && !ferror(f)) {
68 /* allocate more space by doubling: */
70 if (gnutls_realloc(d->data, bufsize) == NULL) {
71 err("out of memory!\n");
76 len += fread(d->data + len, 1, bufsize - len, f);
79 err("Error reading from fd %d\n", fd);
82 /* touch up buffer size to match reality: */
83 gnutls_realloc(d->data, len);
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) {
92 unsigned char* c = NULL;
96 if (0 != stat(fname, &sbuf)) {
97 err("failed to stat '%s'\n", fname);
101 c = gnutls_realloc(d->data, sbuf.st_size);
103 err("failed to allocate %d bytes for '%s'\n", sbuf.st_size, fname);
108 d->size = sbuf.st_size;
109 file = fopen(fname, "r");
111 err("failed to open '%s' for reading\n", fname);
115 x = fread(d->data, d->size, 1, file);
117 err("tried to read %d bytes, read %d instead from '%s'\n", d->size, x, fname);
126 int main(int argc, char* argv[]) {
127 const char* version = NULL;
129 gnutls_x509_privkey_t x509_privkey;
134 const char *certfile, *keyfile;
135 gnutls_certificate_credentials_t pgp_creds;
137 gnutls_datum_t m, e, d, p, q, u;
138 gnutls_x509_crt_t crt;
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;
145 char output_data[10240];
146 size_t ods = sizeof(output_data);
150 if (ret = gnutls_global_init(), ret) {
151 err("Failed to do gnutls_global_init() (error: %d)\n", ret);
157 version = gnutls_check_version(NULL);
160 printf("gnutls version: %s\n", version);
162 printf("no version found!\n");
166 if (ret = gnutls_x509_privkey_init(&x509_privkey), ret) {
167 err("Failed to initialize X.509 private key (error: %d)\n", ret);
171 if (ret = gnutls_openpgp_privkey_init(&pgp_privkey), ret) {
172 err("Failed to initialized OpenPGP private key (error: %d)\n", ret);
176 /* how do we initialize data? */
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");
185 if (ret = set_datum_file(&data, argv[1]), ret) {
186 err("didn't read file '%s'\n", argv[1]);
190 /* treat the passed file as an X.509 private key, and extract its
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); */
197 /* gnutls_x509_privkey_export_rsa_raw(x509_privkey, &m, &e, &d, &p, &q, &u); */
199 /* try to print the PEM-encoded private key: */
200 /* ret = gnutls_x509_privkey_export (x509_privkey, */
201 /* GNUTLS_X509_FMT_PEM, */
204 /* printf("ret: %u; ods: %u;\n", ret, ods); */
205 /* if (ret == 0) { */
206 /* write(0, output_data, ods); */
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);
217 pgp_algo = gnutls_openpgp_privkey_get_pk_algorithm(pgp_privkey, &pgp_bits);
219 err("failed to get OpenPGP key algorithm (error: %d)\n", pgp_algo);
222 if (pgp_algo != GNUTLS_PK_RSA) {
223 err("OpenPGP Key was not RSA (actual algorithm was: %d)\n", pgp_algo);
227 printf("OpenPGP RSA Key, with %d bits\n", pgp_bits);
230 ret = gnutls_x509_privkey_export (pgp_privkey,
234 printf("ret: %u; ods: %u;\n", ret, ods);
236 write(0, output_data, ods);
240 gnutls_x509_privkey_deinit(x509_privkey);
241 gnutls_openpgp_privkey_deinit(pgp_privkey);
242 gnutls_global_deinit();