Merge commit 'micah/master'
authormike castleman <m@mlcastle.net>
Mon, 14 Apr 2008 20:04:59 +0000 (16:04 -0400)
committermike castleman <m@mlcastle.net>
Mon, 14 Apr 2008 20:04:59 +0000 (16:04 -0400)
.gitignore
Makefile
gnutls-helpers.c [new file with mode: 0644]
gnutls-helpers.h [new file with mode: 0644]
gpg2ssh.c [new file with mode: 0644]
main.c
monkeysphere.conf [new file with mode: 0644]
rhesus/README [new file with mode: 0644]
rhesus/rhesus [new file with mode: 0755]

index d2cf5887b24001a514277a311ac82635dc56c07b..ae9fbd8fc60e6a7ceb6ad9991cf9a03f79ea2ffb 100644 (file)
@@ -1,3 +1,4 @@
 *~
 *.[ao]
 monkeysphere
+gpg2ssh
index f3576b1b453d6df3a22b1af14345c8064dfc2098..4fb4556c493bc7cadf3183566857fabf81f3f121 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,13 @@
-monkeysphere: main.c
-       gcc -g -Wall --pedantic -o monkeysphere main.c `libgnutls-config --libs --cflags` -lgnutls-extra
+monkeysphere: main.c gnutls-helpers.o
+       gcc -g -Wall --pedantic -o monkeysphere main.c `libgnutls-config --libs --cflags` -lgnutls-extra gnutls-helpers.o
+
+gpg2ssh: gpg2ssh.c gnutls-helpers.o
+       gcc -g -Wall --pedantic -o gpg2ssh gpg2ssh.c `libgnutls-config --libs --cflags` -lgnutls-extra gnutls-helpers.o
+
+%.o: %.c
+       gcc -g -Wall --pedantic -o $@ -c $<
 
 clean: 
-       rm monkeysphere
+       rm -f monkeysphere *.o
 
 .PHONY: clean
diff --git a/gnutls-helpers.c b/gnutls-helpers.c
new file mode 100644 (file)
index 0000000..5a567e2
--- /dev/null
@@ -0,0 +1,347 @@
+/* Author: Daniel Kahn Gillmor <dkg@fifthhorseman.net> */
+/* Date: Fri, 04 Apr 2008 19:31:16 -0400 */
+/* License: GPL v3 or later */
+
+#include "gnutls-helpers.h"
+/* for htonl() */
+#include <arpa/inet.h>
+
+/* for setlocale() */
+#include <locale.h>
+
+/* for isalnum() */
+#include <ctype.h>
+
+int loglevel = 0;
+
+
+void err(const char* fmt, ...) {
+  va_list ap;
+  va_start(ap, fmt);
+  vfprintf(stderr, fmt, ap);
+  va_end(ap);
+  fflush(stderr);
+}
+
+void logfunc(int level, const char* string) {
+  fprintf(stderr, "GnuTLS Logging (%d): %s\n", level, string);
+}
+
+void init_keyid(gnutls_openpgp_keyid_t keyid) {
+  memset(keyid, 'x', sizeof(gnutls_openpgp_keyid_t));
+}
+
+
+
+void make_keyid_printable(printable_keyid out, gnutls_openpgp_keyid_t keyid)
+{
+  static const char hex[16] = "0123456789ABCDEF";
+  unsigned int kix = 0, outix = 0;
+  
+  while (kix < sizeof(gnutls_openpgp_keyid_t)) {
+    out[outix] = hex[(keyid[kix] >> 4) & 0x0f];
+    out[outix + 1] = hex[keyid[kix] & 0x0f];
+    kix++;
+    outix += 2;
+  }
+}
+
+
+int init_gnutls() {
+  const char* version = NULL;
+  const char* debug_string = NULL;
+  int ret;
+
+  if (ret = gnutls_global_init(), ret) {
+    err("Failed to do gnutls_global_init() (error: %d)\n", ret);
+    return 1;
+  }
+
+  version = gnutls_check_version(NULL);
+
+  if (version) 
+    err("gnutls version: %s\n", version);
+  else {
+    err("no version found!\n");
+    return 1;
+  }
+
+  if (debug_string = getenv("MONKEYSPHERE_DEBUG"), debug_string) {
+    loglevel = atoi(debug_string);
+    gnutls_global_set_log_function(logfunc);
+    
+    gnutls_global_set_log_level(loglevel);
+    err("set log level to %d\n", loglevel);
+  }
+  return 0;
+}
+
+void init_datum(gnutls_datum_t* d) {
+  d->data = NULL;
+  d->size = 0;
+}
+void copy_datum(gnutls_datum_t* dest, const gnutls_datum_t* src) {
+  dest->data = gnutls_realloc(dest->data, src->size);
+  dest->size = src->size;
+  memcpy(dest->data, src->data, src->size);
+}
+int compare_data(const gnutls_datum_t* a, const gnutls_datum_t* b) {
+  if (a->size > b->size) {
+    err("a is larger\n");
+    return 1;
+  }
+  if (a->size < b->size) {
+    err("b is larger\n");
+    return -1;
+  }
+  return memcmp(a->data, b->data, a->size);
+}
+void free_datum(gnutls_datum_t* d) {
+  gnutls_free(d->data);
+  d->data = NULL;
+  d->size = 0;
+}
+
+/* read the passed-in string, store in a single datum */
+int set_datum_string(gnutls_datum_t* d, const char* s) {
+  unsigned int x = strlen(s)+1;
+  unsigned char* c = NULL;
+
+  c = gnutls_realloc(d->data, x);
+  if (NULL == c)
+    return -1;
+  d->data = c;
+  d->size = x;
+  memcpy(d->data, s, x);
+  return 0;
+}
+
+/* read the passed-in file descriptor until EOF, store in a single
+   datum */
+int set_datum_fd(gnutls_datum_t* d, int fd) {
+  unsigned int bufsize = 1024;
+  unsigned int len = 0;
+
+  FILE* f = fdopen(fd, "r");
+  if (bufsize > d->size) {
+    bufsize = 1024;
+    d->data = gnutls_realloc(d->data, bufsize);
+    if (d->data == NULL) {
+      err("out of memory!\n");
+      return -1;
+    }
+    d->size = bufsize;
+  } else {
+    bufsize = d->size;
+  }
+  f = fdopen(fd, "r");
+  if (NULL == f) {
+    err("could not fdopen FD %d\n", fd);
+  }
+  clearerr(f);
+  while (!feof(f) && !ferror(f)) { 
+    if (len == bufsize) {
+      /* allocate more space by doubling: */
+      bufsize *= 2;
+      d->data = gnutls_realloc(d->data, bufsize);
+      if (d->data == NULL) {
+       err("out of memory!\n"); 
+       return -1;
+      };
+      d->size = bufsize;
+    }
+    len += fread(d->data + len, 1, bufsize - len, f);
+    /*     err("read %d bytes\n", len); */
+  }
+  if (ferror(f)) {
+    err("Error reading from fd %d (error: %d) (error: %d '%s')\n", fd, ferror(f), errno, strerror(errno));
+    return -1;
+  }
+    
+  /* touch up buffer size to match reality: */
+  d->data = gnutls_realloc(d->data, len);
+  d->size = len;
+  return 0;
+}
+
+/* read the file indicated (by name) in the fname parameter.  store
+   its entire contents in a single datum. */
+int set_datum_file(gnutls_datum_t* d, const char* fname) {
+  struct stat sbuf;
+  unsigned char* c = NULL;
+  FILE* file = NULL;
+  size_t x = 0;
+
+  if (0 != stat(fname, &sbuf)) {
+    err("failed to stat '%s'\n", fname);
+    return -1;
+  }
+  
+  c = gnutls_realloc(d->data, sbuf.st_size);
+  if (NULL == c) {
+    err("failed to allocate %d bytes for '%s'\n", sbuf.st_size, fname);
+    return -1;
+  }
+
+  d->data = c;
+  d->size = sbuf.st_size;
+  file = fopen(fname, "r");
+  if (NULL == file) {
+    err("failed to open '%s' for reading\n",  fname);
+    return -1;
+  }
+
+  x = fread(d->data, d->size, 1, file);
+  if (x != 1) {
+    err("tried to read %d bytes, read %d instead from '%s'\n", d->size, x, fname);
+    fclose(file);
+    return -1;
+  }
+  fclose(file);
+  return 0;
+}
+
+int write_datum_fd(int fd, const gnutls_datum_t* d) {
+  if (d->size != write(fd, d->data, d->size)) {
+    err("failed to write body of datum.\n");
+    return -1;
+  }
+  return 0;
+}
+
+
+int write_datum_fd_with_length(int fd, const gnutls_datum_t* d) {
+  uint32_t len;
+  int looks_negative = (d->data[0] & 0x80);
+  unsigned char zero = 0;
+
+  /* if the first bit is 1, then the datum will appear negative in the
+     MPI encoding style used by OpenSSH.  In that case, we'll increase
+     the length by one, and dump out one more byte */
+
+  if (looks_negative) {
+    len = htonl(d->size + 1);
+  } else {
+    len = htonl(d->size);
+  }
+  if (write(fd, &len, sizeof(len)) != sizeof(len)) {
+    err("failed to write size of datum.\n");
+    return -2;
+  }
+  if (looks_negative) {
+    if (write(fd, &zero, 1) != 1) {
+      err("failed to write padding byte for MPI.\n");
+      return -2;
+    }
+  }
+  return write_datum_fd(fd, d);
+}
+
+int write_data_fd_with_length(int fd, const gnutls_datum_t** d, unsigned int num) {
+  unsigned int i;
+  int ret;
+
+  for (i = 0; i < num; i++)
+    if (ret = write_datum_fd_with_length(fd, d[i]), ret != 0)
+      return ret;
+
+  return 0;
+}
+
+
+int datum_from_string(gnutls_datum_t* d, const char* str) {
+  d->size = strlen(str);
+  d->data = gnutls_realloc(d->data, d->size);
+  if (d->data == 0)
+    return ENOMEM;
+  memcpy(d->data, str, d->size);
+  return 0;
+}
+
+
+int create_writing_pipe(pid_t* pid, const char* path, char* const argv[]) {
+  int p[2];
+  int ret;
+
+  if (pid == NULL) {
+    err("bad pointer passed to create_writing_pipe()\n");
+    return -1;
+  }
+
+  if (ret = pipe(p), ret == -1) {
+    err("failed to create a pipe (error: %d \"%s\")\n", errno, strerror(errno));
+    return -1;
+  }
+
+  *pid = fork();
+  if (*pid == -1) {
+    err("Failed to fork (error: %d \"%s\")\n", errno, strerror(errno));
+    return -1;
+  }
+  if (*pid == 0) { /* this is the child */
+    close(p[1]); /* close unused write end */
+    
+    if (0 != dup2(p[0], 0)) { /* map the reading end into stdin */
+      err("Failed to transfer reading file descriptor to stdin (error: %d \"%s\")\n", errno, strerror(errno));
+      exit(1);
+    }
+    execv(path, argv);
+    err("exec %s failed (error: %d \"%s\")\n", path, errno, strerror(errno));
+    /* close the open file descriptors */
+    close(p[0]);
+    close(0);
+
+    exit(1);
+  } else { /* this is the parent */
+    close(p[0]); /* close unused read end */
+    return p[1];
+  }
+}
+
+int validate_ssh_host_userid(const char* userid) {
+  char* oldlocale = setlocale(LC_ALL, "C");
+  
+  /* choke if userid does not match the expected format
+     ("ssh://fully.qualified.domain.name") */
+  if (strncmp("ssh://", userid, strlen("ssh://")) != 0) {
+    err("The user ID should start with ssh:// for a host key\n");
+    goto fail;
+  }
+  /* so that isalnum will work properly */
+  userid += strlen("ssh://");
+  while (0 != (*userid)) {
+    if (!isalnum(*userid)) {
+      err("label did not start with a letter or a digit! (%s)\n", userid);
+      goto fail;
+    }
+    userid++;
+    while (isalnum(*userid) || ('-' == (*userid)))
+      userid++;
+    if (('.' == (*userid)) || (0 == (*userid))) { /* clean end of label:
+                                                check last char
+                                                isalnum */
+      if (!isalnum(*(userid - 1))) {
+       err("label did not end with a letter or a digit!\n");
+       goto fail;
+      }
+      if ('.' == (*userid)) /* advance to the start of the next label */
+       userid++;
+    } else {
+      err("invalid character in domain name: %c\n", *userid);
+      goto fail;
+    }
+  }
+  /* ensure that the last character is valid: */
+  if (!isalnum(*(userid - 1))) {
+    err("hostname did not end with a letter or a digit!\n");
+    goto fail;
+  }
+  /* FIXME: fqdn's can be unicode now, thanks to RFC 3490 -- how do we
+     make sure that we've got an OK string? */
+
+  return 0;
+
+ fail:
+  setlocale(LC_ALL, oldlocale);
+  return 1;
+}
diff --git a/gnutls-helpers.h b/gnutls-helpers.h
new file mode 100644 (file)
index 0000000..398413f
--- /dev/null
@@ -0,0 +1,66 @@
+/* Author: Daniel Kahn Gillmor <dkg@fifthhorseman.net> */
+/* Date: Fri, 04 Apr 2008 19:31:16 -0400 */
+/* License: GPL v3 or later */
+
+
+#include <gnutls/gnutls.h>
+#include <gnutls/openpgp.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <stdarg.h>
+
+/* Functions to help dealing with GnuTLS for monkeysphere key
+   translation projects: */
+
+/* set everything up, including logging levels.  Return 0 on
+   success */
+int init_gnutls();
+
+/* logging and output functions: */
+
+void err(const char* fmt, ...);
+void logfunc(int level, const char* string);
+
+/* basic datum manipulations: */
+
+void init_datum(gnutls_datum_t* d);
+void copy_datum(gnutls_datum_t* dest, const gnutls_datum_t* src);
+int compare_data(const gnutls_datum_t* a, const gnutls_datum_t* b);
+void free_datum(gnutls_datum_t* d);
+int write_datum_fd(int fd, const gnutls_datum_t* d);
+int write_datum_fd_with_length(int fd, const gnutls_datum_t* d);
+int write_data_fd_with_length(int fd, const gnutls_datum_t** d, unsigned int num);
+
+/* set up a datum from a null-terminated string */
+int datum_from_string(gnutls_datum_t* d, const char* str);
+
+/* keyid manipulations: */
+typedef unsigned char printable_keyid[16];
+
+void init_keyid(gnutls_openpgp_keyid_t keyid);
+void make_keyid_printable(printable_keyid out, gnutls_openpgp_keyid_t keyid);
+
+/* functions to get data into datum objects: */
+
+/* read the passed-in string, store in a single datum */
+int set_datum_string(gnutls_datum_t* d, const char* s);
+
+/* read the passed-in file descriptor until EOF, store in a single
+   datum */
+int set_datum_fd(gnutls_datum_t* d, int fd);
+
+/* read the file indicated (by na1me) in the fname parameter.  store
+   its entire contents in a single datum. */
+int set_datum_file(gnutls_datum_t* d, const char* fname);
+
+/* set up file descriptor pipe for writing (child process pid gets
+   stored in pid, fd is returned)*/
+int create_writing_pipe(pid_t* pid, const char* path, char* const argv[]);
+
+/* return 0 if userid matches the monkeysphere spec for ssh host user IDs */
+int validate_ssh_host_userid(const char* userid);
diff --git a/gpg2ssh.c b/gpg2ssh.c
new file mode 100644 (file)
index 0000000..6155549
--- /dev/null
+++ b/gpg2ssh.c
@@ -0,0 +1,291 @@
+#include "gnutls-helpers.h"
+
+#include <gnutls/openpgp.h>
+#include <gnutls/x509.h>
+
+/* for waitpid() */
+#include <sys/types.h>
+#include <sys/wait.h>
+
+/* 
+   Author: Daniel Kahn Gillmor <dkg@fifthhorseman.net>
+   Date: Tue, 08 Apr 2008
+   License: GPL v3 or later
+
+   monkeysphere public key translator: execute this with an GPG
+   certificate (public key(s) + userid(s)) on stdin.  It currently
+   only works with RSA keys.
+
+   It will spit out a version of the first key capable of being used
+   for authentication on stdout.  The output format should be suitable
+   for appending a known_hosts file.
+
+   Requirements: I've only built this so far with GnuTLS v2.3.4 --
+   version 2.2.0 does not contain the appropriate pieces.
+
+ */
+
+int main(int argc, char* argv[]) {
+  gnutls_datum_t data;
+  int ret;
+  gnutls_openpgp_crt_t openpgp_crt;
+  gnutls_openpgp_keyid_t keyid;
+  printable_keyid p_keyid;
+  unsigned int keyidx;
+  unsigned int usage, bits;
+  gnutls_pk_algorithm_t algo;
+
+  gnutls_datum_t m, e, p, q, g, y;
+  gnutls_datum_t algolabel;
+
+  char output_data[10240];
+  char userid[10240];
+  size_t uidsz = sizeof(userid);
+
+  const gnutls_datum_t* all[5];
+  int pipefd;
+  pid_t child_pid;
+  char* const args[] = {"/usr/bin/base64", "--wrap=0", NULL};
+  const char* algoname;
+  int mpicount;
+  int pipestatus;
+
+  init_gnutls();
+  
+  init_datum(&data);
+
+  init_datum(&m);
+  init_datum(&e);
+  init_datum(&p);
+  init_datum(&q);
+  init_datum(&g);
+  init_datum(&y);
+
+  init_datum(&algolabel);
+
+  init_keyid(keyid);
+
+  /* slurp in the private key from stdin */
+  if (ret = set_datum_fd(&data, 0), ret) {
+    err("didn't read file descriptor 0\n");
+    return 1;
+  }
+
+
+  if (ret = gnutls_openpgp_crt_init(&openpgp_crt), ret) {
+    err("Failed to initialize OpenPGP certificate (error: %d)\n", ret);
+    return 1;
+  }
+
+  /* format could be either: GNUTLS_OPENPGP_FMT_RAW,
+     GNUTLS_OPENPGP_FMT_BASE64; if MONKEYSPHERE_RAW is set, use RAW,
+     otherwise, use BASE64: */
+
+  /* FIXME: we should be auto-detecting the input format, and
+     translating it as needed. */
+
+  if (getenv("MONKEYSPHERE_RAW")) {
+    err("assuming RAW formatted certificate\n");
+    if (ret = gnutls_openpgp_crt_import(openpgp_crt, &data, GNUTLS_OPENPGP_FMT_RAW), ret) {
+      err("failed to import the OpenPGP certificate in RAW format (error: %d)\n", ret);
+      return ret;
+    }
+  } else {
+    err("assuming BASE64 formatted certificate\n");
+    if (ret = gnutls_openpgp_crt_import (openpgp_crt, &data, GNUTLS_OPENPGP_FMT_BASE64), ret) {
+      err("failed to import the OpenPGP certificate in BASE64 format (error: %d)\n", ret);
+      return ret;
+    }
+  }
+
+  if (gnutls_openpgp_crt_get_revoked_status(openpgp_crt)) {
+    err("the primary key was revoked!\n");
+    return 1;
+  }
+
+  /* FIXME: We're currently looking at the primary key or maybe the
+     first authentication-capable subkey.  
+
+     Instead, we should be iterating through the primary key and all
+     subkeys: for each one with the authentication usage flag set of a
+     algorithm we can handle, we should output matching UserIDs and
+     the SSH version of the key. */
+     
+
+  if (ret = gnutls_openpgp_crt_get_key_usage(openpgp_crt, &usage), ret) {
+    err("failed to get the usage flags for the primary key (error: %d)\n", ret);
+    return ret;
+  }
+  if (usage & GNUTLS_KEY_KEY_AGREEMENT) {
+    err("the primary key can be used for authentication\n");
+
+    algo = gnutls_openpgp_crt_get_pk_algorithm(openpgp_crt, &bits);
+    if (algo < 0) {
+      err("failed to get the algorithm of the OpenPGP public key (error: %d)\n", algo);
+      return algo;
+    } else if (algo == GNUTLS_PK_RSA) {
+      
+      err("OpenPGP RSA certificate, with %d bits\n", bits);
+      ret = gnutls_openpgp_crt_get_pk_rsa_raw(openpgp_crt, &m, &e);
+      if (GNUTLS_E_SUCCESS != ret) {
+       err ("failed to export RSA key parameters (error: %d)\n", ret);
+       return 1;
+      }
+    } else if (algo == GNUTLS_PK_DSA) {
+      err("OpenPGP DSA Key, with %d bits\n", bits);
+      ret = gnutls_openpgp_crt_get_pk_dsa_raw(openpgp_crt, &p, &q, &g, &y);
+      if (GNUTLS_E_SUCCESS != ret) {
+       err ("failed to export DSA key parameters (error: %d)\n", ret);
+       return 1;
+      }
+    } else {
+      err("OpenPGP Key was not RSA or DSA -- can't deal! (actual algorithm was: %d)\n", algo);
+      return 1;
+    }
+    
+  } else {
+    err("primary key is only good for: 0x%08x.  Trying subkeys...\n", usage);
+    
+    if (ret = gnutls_openpgp_crt_get_auth_subkey(openpgp_crt, keyid), ret) {
+      err("failed to find a subkey capable of authentication (error: %d)\n", ret);
+      return ret;
+    }
+    make_keyid_printable(p_keyid, keyid);
+    err("found authentication subkey %.16s\n", p_keyid);
+
+    ret = gnutls_openpgp_crt_get_subkey_idx(openpgp_crt, keyid);
+    if (ret < 0) {
+      err("could not get the index of subkey %.16s (error: %d)\n", ret);
+      return ret;
+    }
+    keyidx = ret;
+
+    if (gnutls_openpgp_crt_get_subkey_revoked_status(openpgp_crt, keyidx)) {
+      err("The authentication subkey was revoked!\n");
+      return 1;
+    }
+
+    if (ret = gnutls_openpgp_crt_get_subkey_usage(openpgp_crt, keyidx, &usage), ret) {
+      err("could not figure out usage of subkey %.16s (error: %d)\n", p_keyid, ret);
+      return ret;
+    }
+    if ((usage & GNUTLS_KEY_KEY_AGREEMENT) == 0) {
+      err("could not find a subkey with authentication privileges.\n");
+      return 1;
+    }
+
+    /* switch, based on the algorithm in question, to extract the MPI
+       components: */
+
+    algo = gnutls_openpgp_crt_get_subkey_pk_algorithm(openpgp_crt, keyidx, &bits);
+    if (algo < 0) {
+      err("failed to get the algorithm of the authentication subkey (error: %d)\n", algo);
+      return algo;
+    } else if (algo == GNUTLS_PK_RSA) {
+      
+      err("OpenPGP RSA subkey, with %d bits\n", bits);
+      ret = gnutls_openpgp_crt_get_subkey_pk_rsa_raw(openpgp_crt, keyidx, &m, &e);
+      if (GNUTLS_E_SUCCESS != ret) {
+       err ("failed to export RSA subkey parameters (error: %d)\n", ret);
+       return 1;
+      }
+    } else if (algo == GNUTLS_PK_DSA) {
+      err("OpenPGP DSA subkey, with %d bits\n", bits);
+      ret = gnutls_openpgp_crt_get_subkey_pk_dsa_raw(openpgp_crt, keyidx, &p, &q, &g, &y);
+      if (GNUTLS_E_SUCCESS != ret) {
+       err ("failed to export DSA subkey parameters (error: %d)\n", ret);
+       return 1;
+      }
+    } else {
+      err("OpenPGP subkey was not RSA or DSA -- can't deal! (actual algorithm was: %d)\n", algo);
+      return 1;
+    }
+  } 
+
+  /* make sure userid is NULL-terminated */
+  userid[sizeof(userid) - 1] = 0;
+  uidsz--;
+
+  /* FIXME: we're just choosing the first UserID from the certificate:
+     instead, we should be selecting every User ID that is adequately
+     signed and matches the spec, and aggregating them with commas for
+     known_hosts output */
+
+  if (ret = gnutls_openpgp_crt_get_name(openpgp_crt, 0, userid, &uidsz), ret) {
+    err("Failed to fetch the first UserID (error: %d)\n", ret);
+    return ret;
+  }
+
+  if (ret = validate_ssh_host_userid(userid), ret) {
+    err("bad userid: not a valid ssh host.\n");
+    return ret;
+  }
+
+  /* remove ssh:// from the beginning of userid */
+  memmove(userid, userid + strlen("ssh://"), 1 + strlen(userid) - strlen("ssh://"));
+  
+
+  /* now we have algo, and the various MPI data are set.  Can we
+     export them cleanly? */
+
+  /* for the moment, we'll just dump the info raw, and pipe it
+     externally through coreutils' /usr/bin/base64 */
+
+  if (algo == GNUTLS_PK_RSA) {
+    algoname = "ssh-rsa";
+    mpicount = 3;
+
+    all[0] = &algolabel;
+    all[1] = &e;
+    all[2] = &m;
+  } else if (algo == GNUTLS_PK_DSA) {
+    algoname = "ssh-dss";
+    mpicount = 5;
+
+    all[0] = &algolabel;
+    all[1] = &p;
+    all[2] = &q;
+    all[3] = &g;
+    all[4] = &y;
+  } else {
+    err("no idea what this algorithm is: %d\n", algo);
+    return 1;
+  }
+
+  if (ret = datum_from_string(&algolabel, algoname), ret) {
+    err("couldn't label string (error: %d)\n", ret);
+    return ret;
+  }
+
+  snprintf(output_data, sizeof(output_data), "%s %s ", userid, algoname);
+
+  pipefd = create_writing_pipe(&child_pid, args[0], args);
+  if (pipefd < 0) {
+    err("failed to create a writing pipe (returned %d)\n", pipefd);
+    return pipefd;
+  }
+    
+  write(1, output_data, strlen(output_data));
+
+  if (0 != write_data_fd_with_length(pipefd, all, mpicount)) {
+    err("was not able to write out RSA key data\n");
+    return 1;
+  }
+  close(pipefd);
+  if (child_pid != waitpid(child_pid, &pipestatus, 0)) {
+    err("could not wait for child process to return for some reason.\n");
+    return 1;
+  }
+  if (pipestatus != 0) {
+    err("base64 pipe died with return code %d\n", pipestatus);
+    return pipestatus;
+  }
+
+  write(1, "\n", 1);
+
+
+
+  gnutls_openpgp_crt_deinit(openpgp_crt);
+  gnutls_global_deinit();
+  return 0;
+}
diff --git a/main.c b/main.c
index 782e33766eaa9470a1f778b8885b6c61be29b3a6..d6bac685399c090749c50eeb33e80b4ad735b6ce 100644 (file)
--- a/main.c
+++ b/main.c
-#include <gnutls/gnutls.h>
+#include "gnutls-helpers.h"
+
 #include <gnutls/openpgp.h>
 #include <gnutls/x509.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
-#include <stdarg.h>
-
-void err(const char* fmt, ...) {
-  static FILE* STDERR = NULL;
-  va_list ap;
-
-  if (NULL == STDERR)
-    STDERR = fdopen(STDERR_FILENO, "a");
-  va_start(ap, fmt);
-  vfprintf(STDERR, fmt, ap);
-  va_end(ap);
-}
 
+/* 
+   Author: Daniel Kahn Gillmor <dkg@fifthhorseman.net>
+   Date: Tue, 01 Apr 2008
+   License: GPL v3 or later
 
-void init_datum(gnutls_datum_t* d) {
-  d->data = NULL;
-  d->size = 0;
-}
-void free_datum(gnutls_datum_t* d) {
-  gnutls_free(d->data);
-  d->data = NULL;
-  d->size = 0;
-}
+   monkeysphere private key translator: execute this with an GPG
+   secret key on stdin (at the moment, only passphraseless RSA keys
+   work).
 
-/* read the passed-in string, store in a single datum */
-int set_datum_string(gnutls_datum_t* d, const char* s) {
-  unsigned int x = strlen(s)+1;
-  unsigned char* c = NULL;
-
-  c = gnutls_realloc(d->data, x);
-  if (NULL == c)
-    return -1;
-  d->data = c;
-  d->size = x;
-  memcpy(d->data, s, x);
-  return 0;
-}
+   It will spit out a PEM-encoded version of the key on stdout, which
+   can be fed into ssh-add like this:
 
-/* read the passed-in file descriptor until EOF, store in a single
-   datum */
-int set_datum_fd(gnutls_datum_t* d, int fd) {
-  unsigned int bufsize = 1024;
-  unsigned int len = 0;
-
-  FILE* f = NULL;
-  if (bufsize > d->size) {
-    bufsize = 1024;
-    if (gnutls_realloc(d->data, bufsize) == NULL) {
-      err("out of memory!\n");
-      return -1;
-    }
-    d->size = bufsize;
-  } else {
-    bufsize = d->size;
+    gpg --export-secret-keys $KEYID | monkeysphere | ssh-add -c /dev/stdin
+
+   Requirements: I've only built this so far with GnuTLS v2.3.4 --
+   version 2.2.0 does not contain the appropriate pieces.
+
+   Notes: gpgkey2ssh doesn't seem to provide the same public
+   keys. Mighty weird!
+
+0 wt215@squeak:~/monkeysphere$ gpg --export-secret-keys 1DCDF89F | ~dkg/src/monkeysphere/monkeysphere  | ssh-add -c /dev/stdin
+gnutls version: 2.3.4
+OpenPGP RSA Key, with 1024 bits
+Identity added: /dev/stdin (/dev/stdin)
+The user has to confirm each use of the key
+0 wt215@squeak:~/monkeysphere$ ssh-add -L
+ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQC9gWQqfrnhQKDQnND/3eOexpddE64J+1zp9fcyCje7H5LKclb6DBV2HS6WgW32PJhIzvP+fYZM3dzXea3fpv14y1SicXiRBDgF9SnsNA1qWn2RyzkLcKy7PmM0PDYtU1oiLTcQj/xkWcqW2sLKHT/WW+vZP5XP7RMGN/yWNMfE2Q== /dev/stdin
+0 wt215@squeak:~/monkeysphere$ gpgkey2ssh 1DCDF89F
+ssh-rsa AAAAB3NzaC1yc2EAAACBAL2BZCp+ueFAoNCc0P/d457Gl10Trgn7XOn19zIKN7sfkspyVvoMFXYdLpaBbfY8mEjO8/59hkzd3Nd5rd+m/XjLVKJxeJEEOAX1Kew0DWpafZHLOQtwrLs+YzQ8Ni1TWiItNxCP/GRZypbawsodP9Zb69k/lc/tEwY3/JY0x8TZAAAAAwEAAQ== COMMENT
+0 wt215@squeak:~/monkeysphere$ 
+
+ */
+
+
+int convert_pgp_to_x509(gnutls_x509_privkey_t* output, gnutls_datum_t* input) {
+  gnutls_openpgp_privkey_t pgp_privkey;
+  gnutls_datum_t m, e, d, p, q, u, g, y, x;
+  gnutls_pk_algorithm_t pgp_algo;
+  unsigned int pgp_bits;
+  int ret;
+
+  init_datum(&m);
+  init_datum(&e);
+  init_datum(&d);
+  init_datum(&p);
+  init_datum(&q);
+  init_datum(&u);
+  init_datum(&g);
+  init_datum(&y);
+  init_datum(&x);
+
+  if (ret = gnutls_openpgp_privkey_init(&pgp_privkey), ret) {
+    err("Failed to initialized OpenPGP private key (error: %d)\n", ret);
+    return 1;
   }
-  f = fdopen(fd, "r");
-  while (!feof(f) && !ferror(f)) {
-    if (len == bufsize) {
-      /* allocate more space by doubling: */
-      bufsize *= 2;
-      if (gnutls_realloc(d->data, bufsize) == NULL) {
-       err("out of memory!\n"); 
-       return -1;
-      };
-      d->size = bufsize;
-    }
-    len += fread(d->data + len, 1, bufsize - len, f);
+
+
+  /* format could be either: GNUTLS_OPENPGP_FMT_RAW,
+     GNUTLS_OPENPGP_FMT_BASE64; if MONKEYSPHERE_RAW is set, use RAW,
+     otherwise, use BASE64: */
+
+  if (getenv("MONKEYSPHERE_RAW")) {
+    err("assuming RAW formatted private keys\n");
+    if (ret = gnutls_openpgp_privkey_import(pgp_privkey, input, GNUTLS_OPENPGP_FMT_RAW, NULL, 0), ret)
+      err("failed to import the OpenPGP private key in RAW format (error: %d)\n", ret);
+  } else {
+    err("assuming BASE64 formatted private keys\n");
+    if (ret = gnutls_openpgp_privkey_import (pgp_privkey, input, GNUTLS_OPENPGP_FMT_BASE64, NULL, 0), ret)
+      err("failed to import the OpenPGP private key in BASE64 format (error: %d)\n", ret);
   }
-  if (ferror(f)) {
-    err("Error reading from fd %d\n", fd);
-    return -1;
+
+  pgp_algo = gnutls_openpgp_privkey_get_pk_algorithm(pgp_privkey, &pgp_bits);
+  if (pgp_algo < 0) {
+    err("failed to get OpenPGP key algorithm (error: %d)\n", pgp_algo);
+    return 1;
   }
-  /* touch up buffer size to match reality: */
-  gnutls_realloc(d->data, len);
-  d->size = len;
-  return 0;
-}
+  if (pgp_algo == GNUTLS_PK_RSA) {
+    err("OpenPGP RSA Key, with %d bits\n", pgp_bits);
+    ret = gnutls_openpgp_privkey_export_rsa_raw(pgp_privkey, &m, &e, &d, &p, &q, &u);
+    if (GNUTLS_E_SUCCESS != ret) {
+      err ("failed to export RSA key parameters (error: %d)\n", ret);
+      return 1;
+    }
+
+    ret = gnutls_x509_privkey_import_rsa_raw (*output, &m, &e, &d, &p, &q, &u); 
+    if (GNUTLS_E_SUCCESS != ret) {
+      err ("failed to import RSA key parameters (error: %d)\n", ret);
+      return 1;
+    }
+  } else if (pgp_algo == GNUTLS_PK_DSA) {
+    err("OpenPGP DSA Key, with %d bits\n", pgp_bits);
+    ret = gnutls_openpgp_privkey_export_dsa_raw(pgp_privkey, &p, &q, &g, &y, &x);
+    if (GNUTLS_E_SUCCESS != ret) {
+      err ("failed to export DSA key parameters (error: %d)\n", ret);
+      return 1;
+    }
 
-/* read the file indicated (by na1me) in the fname parameter.  store
-   its entire contents in a single datum. */
-int set_datum_file(gnutls_datum_t* d, const char* fname) {
-  struct stat sbuf;
-  unsigned char* c = NULL;
-  FILE* file = NULL;
-  size_t x = 0;
-
-  if (0 != stat(fname, &sbuf)) {
-    err("failed to stat '%s'\n", fname);
-    return -1;
+    ret = gnutls_x509_privkey_import_dsa_raw (*output, &p, &q, &g, &y, &x); 
+    if (GNUTLS_E_SUCCESS != ret) {
+      err ("failed to import DSA key parameters (error: %d)\n", ret);
+      return 1;
+    }
+  } else {
+    err("OpenPGP Key was not RSA or DSA -- can't deal! (actual algorithm was: %d)\n", pgp_algo);
+    return 1;
   }
   
-  c = gnutls_realloc(d->data, sbuf.st_size);
-  if (NULL == c) {
-    err("failed to allocate %d bytes for '%s'\n", sbuf.st_size, fname);
-    return -1;
+  ret = gnutls_x509_privkey_fix(*output);
+  if (ret != 0) {
+    err("failed to fix up the private key in X.509 format (error: %d)\n", ret);
+    return 1; 
   }
 
-  d->data = c;
-  d->size = sbuf.st_size;
-  file = fopen(fname, "r");
-  if (NULL == file) {
-    err("failed to open '%s' for reading\n",  fname);
-    return -1;
-  }
-
-  x = fread(d->data, d->size, 1, file);
-  if (x != 1) {
-    err("tried to read %d bytes, read %d instead from '%s'\n", d->size, x, fname);
-    fclose(file);
-    return -1;
-  }
-  fclose(file);
+  gnutls_openpgp_privkey_deinit(pgp_privkey);
   return 0;
 }
 
-
-int main(int argc, char* argv[]) {
-  const char* version = NULL;
-
+int convert_x509_to_pgp(gnutls_openpgp_privkey_t* output, gnutls_datum_t* input) {
   gnutls_x509_privkey_t x509_privkey;
-  gnutls_datum_t data;
+  gnutls_datum_t m, e, d, p, q, u, g, y, x;
+  gnutls_pk_algorithm_t x509_algo;
   int ret;
 
-  /*  
-      const char *certfile, *keyfile;
-      gnutls_certificate_credentials_t pgp_creds;
-  */
-  gnutls_datum_t m, e, d, p, q, u;
-  gnutls_x509_crt_t crt;
+  init_datum(&m);
+  init_datum(&e);
+  init_datum(&d);
+  init_datum(&p);
+  init_datum(&q);
+  init_datum(&u);
+  init_datum(&g);
+  init_datum(&y);
+  init_datum(&x);
 
-  gnutls_openpgp_privkey_t pgp_privkey;
-  gnutls_openpgp_crt_fmt_t pgp_format;
-  gnutls_pk_algorithm_t pgp_algo;
-  unsigned int pgp_bits;
-
-  char output_data[10240];
-  size_t ods = sizeof(output_data);
-
-  init_datum(&data);
-
-  if (ret = gnutls_global_init(), ret) {
-    err("Failed to do gnutls_global_init() (error: %d)\n", ret);
+  if (ret = gnutls_x509_privkey_init(&x509_privkey), ret) {
+    err("Failed to initialized X.509 private key (error: %d)\n", ret);
     return 1;
   }
 
 
+  /* format could be either:     GNUTLS_X509_FMT_DER,
+    GNUTLS_X509_FMT_PEM; if MONKEYSPHERE_DER is set, use DER,
+     otherwise, use PEM: */
 
-  version = gnutls_check_version(NULL);
+  if (getenv("MONKEYSPHERE_DER")) {
+    err("assuming DER formatted private keys\n");
+    if (ret = gnutls_x509_privkey_import(x509_privkey, input, GNUTLS_X509_FMT_DER), ret)
+      err("failed to import the X.509 private key in DER format (error: %d)\n", ret);
+  } else {
+    err("assuming PEM formatted private keys\n");
+    if (ret = gnutls_x509_privkey_import (x509_privkey, input, GNUTLS_X509_FMT_PEM), ret)
+      err("failed to import the X.509 private key in PEM format (error: %d)\n", ret);
+  }
 
-  if (version) 
-    printf("gnutls version: %s\n", version);
-  else {
-    printf("no version found!\n");
+  x509_algo = gnutls_x509_privkey_get_pk_algorithm(x509_privkey);
+  if (x509_algo < 0) {
+    err("failed to get X.509 key algorithm (error: %d)\n", x509_algo);
     return 1;
   }
+  if (x509_algo == GNUTLS_PK_RSA) {
+    err("X.509 RSA Key\n");
+    ret = gnutls_x509_privkey_export_rsa_raw(x509_privkey, &m, &e, &d, &p, &q, &u);
+    if (GNUTLS_E_SUCCESS != ret) {
+      err ("failed to export RSA key parameters (error: %d)\n", ret);
+      return 1;
+    }
 
-  if (ret = gnutls_x509_privkey_init(&x509_privkey), ret) {
-    err("Failed to initialize X.509 private key (error: %d)\n", ret);
+    /*     ret = gnutls_openpgp_privkey_import_rsa_raw (*output, &m, &e, &d, &p, &q, &u);  */
+    ret = GNUTLS_E_UNIMPLEMENTED_FEATURE;
+    if (GNUTLS_E_SUCCESS != ret) {
+      err ("failed to import RSA key parameters (error: %d)\n", ret);
+      return 1;
+    }
+  } else if (x509_algo == GNUTLS_PK_DSA) {
+    err("X.509 DSA Key\n");
+    ret = gnutls_x509_privkey_export_dsa_raw(x509_privkey, &p, &q, &g, &y, &x);
+    if (GNUTLS_E_SUCCESS != ret) {
+      err ("failed to export DSA key parameters (error: %d)\n", ret);
+      return 1;
+    }
+
+    /*    ret = gnutls_openpgp_privkey_import_dsa_raw (*output, &p, &q, &g, &y, &x);  */
+    ret = GNUTLS_E_UNIMPLEMENTED_FEATURE;
+    if (GNUTLS_E_SUCCESS != ret) {
+      err ("failed to import DSA key parameters (error: %d)\n", ret);
+      return 1;
+    }
+  } else {
+    err("OpenPGP Key was not RSA or DSA -- can't deal! (actual algorithm was: %d)\n", x509_algo);
     return 1;
   }
+  
+  gnutls_x509_privkey_deinit(x509_privkey);
+  return 0;
+}
 
-  if (ret = gnutls_openpgp_privkey_init(&pgp_privkey), ret) {
-    err("Failed to initialized OpenPGP private key (error: %d)\n", ret);
+
+int main(int argc, char* argv[]) {
+  gnutls_datum_t data;
+  int ret;
+  gnutls_x509_privkey_t x509_privkey;
+
+  char output_data[10240];
+  size_t ods = sizeof(output_data);
+
+  init_gnutls();
+  
+  init_datum(&data);
+
+  /* slurp in the private key from stdin */
+  if (ret = set_datum_fd(&data, 0), ret) {
+    err("didn't read file descriptor 0\n");
     return 1;
   }
 
-  /* how do we initialize data? */
 
-    /* reading from the file descriptor doesn't work right yet:
-      if (ret = set_datum_fd(&data, 0), ret) {
-      err("didn't read file descriptor 0\n");
-      return 1;
-      }
-    */
 
+  /* Or, instead, read in key from a file name: 
   if (ret = set_datum_file(&data, argv[1]), ret) {
     err("didn't read file '%s'\n", argv[1]);
     return 1;
   }
+*/
 
   /* treat the passed file as an X.509 private key, and extract its
      component values: */
@@ -206,39 +245,27 @@ int main(int argc, char* argv[]) {
 /*     write(0, output_data, ods); */
 /*   } */
 
-  
-  /* format could be either: GNUTLS_OPENPGP_FMT_RAW,
-     GNUTLS_OPENPGP_FMT_BASE64 */
-  pgp_format = GNUTLS_OPENPGP_FMT_RAW;
-  if (ret = gnutls_openpgp_privkey_import (pgp_privkey, &data, pgp_format, NULL, 0), ret) {
-    err("failed to import the OpenPGP private key (error: %d)\n", ret);
-    return 1;
-  }
-  pgp_algo = gnutls_openpgp_privkey_get_pk_algorithm(pgp_privkey, &pgp_bits);
-  if (pgp_algo < 0) {
-    err("failed to get OpenPGP key algorithm (error: %d)\n", pgp_algo);
-    return 1;
-  }
-  if (pgp_algo != GNUTLS_PK_RSA) {
-    err("OpenPGP Key was not RSA (actual algorithm was: %d)\n", pgp_algo);
+
+  if (ret = gnutls_x509_privkey_init(&x509_privkey), ret) {
+    err("Failed to initialize X.509 private key (error: %d)\n", ret);
     return 1;
   }
-  
-  printf("OpenPGP RSA Key, with %d bits\n", pgp_bits);
 
+  if (ret = convert_pgp_to_x509(&x509_privkey, &data), ret) {
+    return ret;
+  }
 
-  ret = gnutls_x509_privkey_export (pgp_privkey,
+  ret = gnutls_x509_privkey_export (x509_privkey,
                                    GNUTLS_X509_FMT_PEM,
                                    output_data,
                                    &ods);
   printf("ret: %u; ods: %u;\n", ret, ods);
   if (ret == 0) {
-    write(0, output_data, ods);
+    write(1, output_data, ods);
   }
 
 
   gnutls_x509_privkey_deinit(x509_privkey);
-  gnutls_openpgp_privkey_deinit(pgp_privkey);
   gnutls_global_deinit();
   return 0;
 }
diff --git a/monkeysphere.conf b/monkeysphere.conf
new file mode 100644 (file)
index 0000000..1e3abf9
--- /dev/null
@@ -0,0 +1,7 @@
+# monkeysphere configuration file
+# this is currently meant to be sourced by bash.
+CONF_DIR=/etc/monkeysphere
+AUTH_USER_IDS_DIR="$CONF_DIR"/auth_user_ids
+KEYRING="$CONF_DIR"/keyring.gpg
+KEYSERVER=subkeys.pgp.net
+GNUPGHOME="$CONF_DIR"/gnupg
diff --git a/rhesus/README b/rhesus/README
new file mode 100644 (file)
index 0000000..226361c
--- /dev/null
@@ -0,0 +1,7 @@
+rhesus is the monkeysphere authorized_keys generator.
+
+It's goal is to take a user's auth_user_ids file, which contains gpg
+user ids (and possibly authorized_keys options), use gpg to fetch the
+keys of the specified users, do a monkeysphere policy check on each
+id, and generate authorized_keys lines for verified id.
+
diff --git a/rhesus/rhesus b/rhesus/rhesus
new file mode 100755 (executable)
index 0000000..fe98b39
--- /dev/null
@@ -0,0 +1,139 @@
+#!/bin/sh
+
+# rhesus: monkeysphere authorized_keys update script
+#
+# Written by
+# Jameson Rollins <jrollins@fifthhorseman.net>
+#
+# Copyright 2008, released under the GPL, version 3 or later
+
+##################################################
+# load conf file
+#. /etc/monkeysphere/monkeysphere.conf
+. ~/ms/monkeysphere.conf
+
+#AUTH_KEYS_DIR_BASE=/var/lib/monkeysphere/authorized_keys/
+AUTH_KEYS_DIR_BASE=~/ms/authorized_keys
+
+export GNUPGHOME
+##################################################
+
+CMD=$(basename $0)
+
+usage() {
+cat <<EOF
+usage: $CMD USERNAME
+EOF
+}
+
+failure() {
+    echo "$1" >&2
+    exit ${2:-'1'}
+}
+
+meat() {
+    grep -v -e "^[[:space:]]*#" -e '^$' "$1"
+}
+
+cutline() {
+    head --line="$1" | tail -1
+}
+
+### MAIN
+
+if [ -z "$1" ] ; then
+    usage
+    exit 1
+fi
+
+# user name of user to update
+USERNAME="$1"
+if ! id "$USERNAME" > /dev/null ; then
+    failure "User '$USERNAME' does not exist."
+fi
+
+AUTH_USER_IDS="$AUTH_USER_IDS_DIR"/"$USERNAME"
+if [ ! -e "$AUTH_USER_IDS" ] ; then
+    failure "No auth_user_ids file for user '$USERNAME'."
+fi
+
+AUTH_KEYS_DIR="$AUTH_KEYS_DIR_BASE"/"$USERNAME"/keys
+AUTH_KEYS_FILE="$AUTH_KEYS_DIR_BASE"/authorized_keys
+
+# make sure the gnupg home exists with proper permissions
+mkdir -p "$GNUPGHOME"
+chmod 0700 "$GNUPGHOME"
+
+# find number of user ids in auth_user_ids file
+NLINES=$(meat "$AUTH_USER_IDS" | wc -l)
+
+# clean out keys file and remake keys directory
+rm -rf "$AUTH_KEYS_DIR"
+mkdir -p "$AUTH_KEYS_DIR"
+
+# loop through all user ids, and generate ssh keys
+for (( N=1; N<=$NLINES; N=N+1 )) ; do
+    # get user id
+    USERID=$(meat "$AUTH_USER_IDS" | cutline "$N" )
+    USERID_HASH=$(echo "$USERID" | sha1sum | awk '{ print $1 }')
+
+    KEYFILE="$AUTH_KEYS_DIR"/"$USERID_HASH"
+
+    # search for key on keyserver
+    echo -n "ms: finding key for '$USERID'..."
+    RETURN=$(echo 1 | gpg --quiet --batch --command-fd 0 --with-colons --keyserver "$KEYSERVER" --search ="$USERID" 2> /dev/null)
+
+    # if the key was found...
+    if [ "$RETURN" ] ; then
+       echo " found."
+
+       # checking key attributes
+       # see /usr/share/doc/gnupg/DETAILS.gz:
+
+       PUB_INFO=$(gpg --fixed-list-mode --with-colons --list-keys --with-fingerprint ="$USERID" | grep '^pub:')
+
+       echo -n "ms: "
+
+#      # if not an authorization key exit
+#      if echo "$PUB_INFO" | cut -d: -f12 | grep -v -q '[aA]' ; then
+#         echo "not an authorization key --> SKIPPING"
+#         continue
+#      fi
+
+       # if key is not fully trusted exit
+        # (this includes not revoked or expired)
+       # determine trust
+       TRUST=$(echo "$PUB_INFO" | cut -d: -f2)
+       case "$TRUST" in
+           'i')
+               echo -n "invalid" ;;
+           'r')
+               echo -n "revoked" ;;
+           'e')
+               echo -n "expired" ;;
+           '-'|'q'|'n'|'m')
+               echo -n "unacceptable trust" ;;
+           'f'|'u')
+               echo -n "fully trusted"
+                # convert pgp key to ssh key, and write to cache file
+               echo " -> generating ssh key..."
+               gpgkey2ssh "$KEYID" | sed -e "s/COMMENT/$USERID/" > "$KEYFILE"
+               continue
+           ;;
+           *)
+               echo -n "unknown trust" ;;
+       esac
+       echo " -> SKIPPING"
+    fi
+done
+
+if [ $(ls "$AUTH_KEYS_DIR") ]  ; then
+    echo "ms: writing ms authorized_keys file..."
+    cat "$AUTH_KEYS_DIR"/* > "$AUTH_KEYS_FILE"
+else
+    echo "ms: no gpg keys to add to authorized_keys file."
+fi
+if [ -s ~"$USERNAME"/.ssh/authorized_keys ] ; then
+    echo "ms: adding user authorized_keys..."
+    cat ~"$USERNAME"/.ssh/authorized_keys >> "$AUTH_KEYS_FILE"
+fi