Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/crypto/external/bsd/netpgp/dist Add a utility function, netp...
details: https://anonhg.NetBSD.org/src/rev/7e7b7909a36d
branches: trunk
changeset: 757587:7e7b7909a36d
user: agc <agc%NetBSD.org@localhost>
date: Mon Sep 06 18:19:38 2010 +0000
description:
Add a utility function, netpgp_write_sshkey(3), which will take a PGP public
key (RSA only) and format it as an ssh pubkey.
diffstat:
crypto/external/bsd/netpgp/dist/include/netpgp.h | 4 +
crypto/external/bsd/netpgp/dist/src/lib/netpgp.c | 121 ++++++++++++++++++++++-
2 files changed, 124 insertions(+), 1 deletions(-)
diffs (156 lines):
diff -r 8666a7c67a0f -r 7e7b7909a36d crypto/external/bsd/netpgp/dist/include/netpgp.h
--- a/crypto/external/bsd/netpgp/dist/include/netpgp.h Mon Sep 06 18:17:58 2010 +0000
+++ b/crypto/external/bsd/netpgp/dist/include/netpgp.h Mon Sep 06 18:19:38 2010 +0000
@@ -101,6 +101,10 @@
int netpgp_validate_sigs(netpgp_t *);
+/* save pgp key in ssh format */
+int netpgp_write_sshkey(netpgp_t *, char *, const char *, char *, size_t);
+
+
__END_DECLS
#endif /* !NETPGP_H_ */
diff -r 8666a7c67a0f -r 7e7b7909a36d crypto/external/bsd/netpgp/dist/src/lib/netpgp.c
--- a/crypto/external/bsd/netpgp/dist/src/lib/netpgp.c Mon Sep 06 18:17:58 2010 +0000
+++ b/crypto/external/bsd/netpgp/dist/src/lib/netpgp.c Mon Sep 06 18:19:38 2010 +0000
@@ -34,7 +34,7 @@
#if defined(__NetBSD__)
__COPYRIGHT("@(#) Copyright (c) 2009 The NetBSD Foundation, Inc. All rights reserved.");
-__RCSID("$NetBSD: netpgp.c,v 1.73 2010/09/02 07:31:16 agc Exp $");
+__RCSID("$NetBSD: netpgp.c,v 1.74 2010/09/06 18:19:38 agc Exp $");
#endif
#include <sys/types.h>
@@ -621,6 +621,69 @@
p(fp, "\n", NULL);
}
+/* save a pgp pubkey to a temp file */
+static int
+savepubkey(char *res, char *f, size_t size)
+{
+ size_t len;
+ int cc;
+ int wc;
+ int fd;
+
+ (void) snprintf(f, size, "/tmp/pgp2ssh.XXXXXXX");
+ if ((fd = mkstemp(f)) < 0) {
+ (void) fprintf(stderr, "can't create temp file '%s'\n", f);
+ return 0;
+ }
+ len = strlen(res);
+ for (cc = 0 ; (wc = write(fd, &res[cc], len - cc)) > 0 ; cc += wc) {
+ }
+ (void) close(fd);
+ return 1;
+}
+
+/* format a uint32_t */
+static int
+formatu32(uint8_t *buffer, uint32_t value)
+{
+ buffer[0] = (uint8_t)(value >> 24) & 0xff;
+ buffer[1] = (uint8_t)(value >> 16) & 0xff;
+ buffer[2] = (uint8_t)(value >> 8) & 0xff;
+ buffer[3] = (uint8_t)value & 0xff;
+ return sizeof(uint32_t);
+}
+
+/* format a string as (len, string) */
+static int
+formatstring(char *buffer, const uint8_t *s, size_t len)
+{
+ int cc;
+
+ cc = formatu32((uint8_t *)buffer, len);
+ (void) memcpy(&buffer[cc], s, len);
+ return cc + len;
+}
+
+/* format a bignum, checking for "interesting" high bit values */
+static int
+formatbignum(char *buffer, BIGNUM *bn)
+{
+ size_t len;
+ uint8_t *cp;
+ int cc;
+
+ len = (size_t) BN_num_bytes(bn);
+ if ((cp = calloc(1, len + 1)) == NULL) {
+ (void) fprintf(stderr, "calloc failure in formatbignum\n");
+ return 0;
+ }
+ (void) BN_bn2bin(bn, cp + 1);
+ cp[0] = 0x0;
+ cc = (cp[1] & 0x80) ? formatstring(buffer, cp, len + 1) : formatstring(buffer, &cp[1], len);
+ free(cp);
+ return cc;
+}
+
/***************************************************************************/
/* exported functions start here */
/***************************************************************************/
@@ -1665,3 +1728,59 @@
mj_delete(&ids);
return idc;
}
+
+/* find a key in keyring, and write it in ssh format */
+int
+netpgp_write_sshkey(netpgp_t *netpgp, char *s, const char *userid, char *out, size_t size)
+{
+ const __ops_key_t *key;
+ __ops_keyring_t *keyring;
+ __ops_io_t *io;
+ unsigned k;
+ size_t cc;
+ char f[MAXPATHLEN];
+
+ if ((io = calloc(1, sizeof(__ops_io_t))) == NULL) {
+ (void) fprintf(stderr, "netpgp_save_sshpub: bad alloc 1\n");
+ return 0;
+ }
+ io->outs = stdout;
+ io->errs = stderr;
+ io->res = stderr;
+ netpgp->io = io;
+ /* write new to temp file */
+ savepubkey(s, f, sizeof(f));
+ if ((keyring = calloc(1, sizeof(*keyring))) == NULL) {
+ (void) fprintf(stderr, "netpgp_save_sshpub: bad alloc 2\n");
+ return 0;
+ }
+ if (!__ops_keyring_fileread(netpgp->pubring = keyring, 1, f)) {
+ (void) fprintf(stderr, "can't import key\n");
+ return 0;
+ }
+ /* get rsa key */
+ k = 0;
+ key = __ops_getnextkeybyname(netpgp->io, netpgp->pubring, userid, &k);
+ if (key == NULL) {
+ (void) fprintf(stderr, "no key found for '%s'\n", userid);
+ return 0;
+ }
+ if (key->key.pubkey.alg != OPS_PKA_RSA) {
+ /* we're not interested in supporting DSA either :-) */
+ (void) fprintf(stderr, "key not RSA '%s'\n", userid);
+ return 0;
+ }
+ /* XXX - check trust sigs */
+ /* XXX - check expiry */
+ /* XXX - check start */
+ /* XXX - check not weak key */
+ /* get rsa e and n */
+ (void) memset(out, 0x0, size);
+ cc = formatstring((char *)out, (const uint8_t *)"ssh-rsa", 7);
+ cc += formatbignum((char *)&out[cc], key->key.pubkey.key.rsa.e);
+ cc += formatbignum((char *)&out[cc], key->key.pubkey.key.rsa.n);
+ cc += snprintf(&out[cc], size - cc, " %s", key->uids[0]);
+ free(io);
+ free(keyring);
+ return cc;
+}
Home |
Main Index |
Thread Index |
Old Index