60defec
diff -up openssh-5.3p1/authfd.c.nss-keys openssh-5.3p1/authfd.c
60defec
--- openssh-5.3p1/authfd.c.nss-keys	2006-09-01 07:38:36.000000000 +0200
a595f1f
+++ openssh-5.3p1/authfd.c	2009-11-27 13:43:00.000000000 +0100
60defec
@@ -626,6 +626,45 @@ ssh_update_card(AuthenticationConnection
60defec
 	return decode_reply(type);
adad2a8
 }
a3ba41c
 
60defec
+int
60defec
+ssh_update_nss_key(AuthenticationConnection *auth, int add,
60defec
+    const char *tokenname, const char *keyname,
60defec
+    const char *pass, u_int life, u_int confirm)
adad2a8
+{
60defec
+	Buffer msg;
60defec
+	int type, constrained = (life || confirm);
adad2a8
+
60defec
+	if (add) {
60defec
+		type = constrained ?
60defec
+		    SSH_AGENTC_ADD_NSS_KEY_CONSTRAINED :
60defec
+		    SSH_AGENTC_ADD_NSS_KEY;
60defec
+	} else
60defec
+		type = SSH_AGENTC_REMOVE_NSS_KEY;
adad2a8
+
60defec
+	buffer_init(&msg;;
60defec
+	buffer_put_char(&msg, type);
60defec
+	buffer_put_cstring(&msg, tokenname);
60defec
+	buffer_put_cstring(&msg, keyname);
60defec
+	buffer_put_cstring(&msg, pass);
adad2a8
+
60defec
+	if (constrained) {
60defec
+		if (life != 0) {
60defec
+			buffer_put_char(&msg, SSH_AGENT_CONSTRAIN_LIFETIME);
60defec
+			buffer_put_int(&msg, life);
adad2a8
+		}
60defec
+		if (confirm != 0)
60defec
+			buffer_put_char(&msg, SSH_AGENT_CONSTRAIN_CONFIRM);
adad2a8
+	}
adad2a8
+
60defec
+	if (ssh_request_reply(auth, &msg, &msg) == 0) {
60defec
+		buffer_free(&msg;;
60defec
+		return 0;
adad2a8
+	}
60defec
+	type = buffer_get_char(&msg;;
60defec
+	buffer_free(&msg;;
60defec
+	return decode_reply(type);
adad2a8
+}
adad2a8
+
60defec
 /*
60defec
  * Removes all identities from the agent.  This call is not meant to be used
60defec
  * by normal applications.
60defec
diff -up openssh-5.3p1/authfd.h.nss-keys openssh-5.3p1/authfd.h
60defec
--- openssh-5.3p1/authfd.h.nss-keys	2006-08-05 04:39:39.000000000 +0200
a595f1f
+++ openssh-5.3p1/authfd.h	2009-11-27 13:43:01.000000000 +0100
adad2a8
@@ -49,6 +49,12 @@
adad2a8
 #define SSH2_AGENTC_ADD_ID_CONSTRAINED		25
adad2a8
 #define SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED 26
a3ba41c
 
adad2a8
+/* nss */
adad2a8
+#define SSH_AGENTC_ADD_NSS_KEY			30
adad2a8
+#define SSH_AGENTC_REMOVE_NSS_KEY		31
adad2a8
+#define SSH_AGENTC_ADD_NSS_KEY_CONSTRAINED	32
adad2a8
+
adad2a8
+
adad2a8
 #define	SSH_AGENT_CONSTRAIN_LIFETIME		1
adad2a8
 #define	SSH_AGENT_CONSTRAIN_CONFIRM		2
a3ba41c
 
adad2a8
@@ -83,6 +89,8 @@ int	 ssh_remove_all_identities(Authentic
adad2a8
 int	 ssh_lock_agent(AuthenticationConnection *, int, const char *);
adad2a8
 int	 ssh_update_card(AuthenticationConnection *, int, const char *,
adad2a8
     const char *, u_int, u_int);
adad2a8
+int	 ssh_update_nss_key(AuthenticationConnection *, int, const char *,
adad2a8
+    const char *, const char *, u_int, u_int);
a3ba41c
 
adad2a8
 int
adad2a8
 ssh_decrypt_challenge(AuthenticationConnection *, Key *, BIGNUM *, u_char[16],
60defec
diff -up openssh-5.3p1/configure.ac.nss-keys openssh-5.3p1/configure.ac
a595f1f
--- openssh-5.3p1/configure.ac.nss-keys	2009-11-27 13:42:57.000000000 +0100
a595f1f
+++ openssh-5.3p1/configure.ac	2009-11-27 13:48:44.000000000 +0100
a595f1f
@@ -3526,6 +3526,21 @@ AC_ARG_WITH(kerberos5,
adad2a8
 	]
adad2a8
 )
a3ba41c
 
adad2a8
+# Check whether user wants NSS support
adad2a8
+LIBNSS_MSG="no"
adad2a8
+AC_ARG_WITH(nss,
adad2a8
+	[  --with-nss   Enable NSS support],
adad2a8
+	[ if test "x$withval" != "xno" ; then
adad2a8
+		AC_DEFINE(HAVE_LIBNSS,1,[Define if you want NSS support.])
adad2a8
+		LIBNSS_MSG="yes"
adad2a8
+		CPPFLAGS="$CPPFLAGS -I/usr/include/nss3 -I/usr/include/nspr4"
adad2a8
+		AC_CHECK_HEADERS(pk11pub.h)
adad2a8
+		LIBS="$LIBS -lnss3"
a595f1f
+		AC_CHECK_DECLS([SEC_ERROR_LOCKED_PASSWORD], [], [], [#include <secerr.h>])
adad2a8
+	fi
adad2a8
+	])
adad2a8
+AC_SUBST(LIBNSS)
adad2a8
+
adad2a8
 # Looking for programs, paths and files
a3ba41c
 
adad2a8
 PRIVSEP_PATH=/var/empty
a595f1f
@@ -4253,6 +4269,7 @@ echo "              TCP Wrappers support
adad2a8
 echo "              MD5 password support: $MD5_MSG"
adad2a8
 echo "                   libedit support: $LIBEDIT_MSG"
adad2a8
 echo "  Solaris process contract support: $SPC_MSG"
adad2a8
+echo "                       NSS support: $LIBNSS_MSG"
adad2a8
 echo "       IP address in \$DISPLAY hack: $DISPLAY_HACK_MSG"
adad2a8
 echo "           Translate v4 in v6 hack: $IPV4_IN6_HACK_MSG"
adad2a8
 echo "                  BSD Auth support: $BSD_AUTH_MSG"
60defec
diff -up openssh-5.3p1/key.c.nss-keys openssh-5.3p1/key.c
60defec
--- openssh-5.3p1/key.c.nss-keys	2008-11-03 09:24:17.000000000 +0100
a595f1f
+++ openssh-5.3p1/key.c	2009-11-27 13:43:01.000000000 +0100
60defec
@@ -96,6 +96,54 @@ key_new(int type)
60defec
 	return k;
adad2a8
 }
a3ba41c
 
60defec
+#ifdef HAVE_LIBNSS
60defec
+Key *
60defec
+key_new_nss(int type)
adad2a8
+{
60defec
+	Key *k = key_new(type);
adad2a8
+
60defec
+	k->nss = xcalloc(1, sizeof(*k->nss));
60defec
+	k->flags = KEY_FLAG_EXT | KEY_FLAG_NSS;
adad2a8
+
60defec
+	return k;
60defec
+}
adad2a8
+
60defec
+Key *
60defec
+key_new_nss_copy(int type, const Key *c)
60defec
+{
60defec
+	Key *k = key_new_nss(type);
adad2a8
+
60defec
+	switch (k->type) {
60defec
+		case KEY_RSA:
60defec
+			if ((BN_copy(k->rsa->n, c->rsa->n) == NULL) ||
60defec
+				(BN_copy(k->rsa->e, c->rsa->e) == NULL))
60defec
+				fatal("key_new_nss_copy: BN_copy failed");
60defec
+			break;
60defec
+		case KEY_DSA:
60defec
+			if ((BN_copy(k->dsa->p, c->rsa->p) == NULL) ||
60defec
+				(BN_copy(k->dsa->q, c->dsa->q) == NULL) ||
60defec
+				(BN_copy(k->dsa->g, c->dsa->g) == NULL) ||
60defec
+				(BN_copy(k->dsa->pub_key, c->dsa->pub_key) == NULL))
60defec
+				fatal("key_new_nss_copy: BN_copy failed");
60defec
+			break;
adad2a8
+	}
60defec
+		
60defec
+	k->nss->privk = SECKEY_CopyPrivateKey(c->nss->privk);
60defec
+	if (k->nss->privk == NULL)
60defec
+		fatal("key_new_nss_copy: SECKEY_CopyPrivateKey failed");
60defec
+
60defec
+	k->nss->pubk = SECKEY_CopyPublicKey(c->nss->pubk);
60defec
+	if (k->nss->pubk == NULL)
60defec
+		fatal("key_new_nss_copy: SECKEY_CopyPublicKey failed");
60defec
+	
60defec
+	if (c->nss->privk->wincx)
60defec
+		k->nss->privk->wincx = xstrdup(c->nss->privk->wincx);
60defec
+
60defec
+	return k;
adad2a8
+}
60defec
+#endif
adad2a8
+
60defec
+
60defec
 Key *
60defec
 key_new_private(int type)
60defec
 {
60defec
@@ -151,6 +199,19 @@ key_free(Key *k)
60defec
 		fatal("key_free: bad key type %d", k->type);
60defec
 		break;
60defec
 	}
60defec
+#ifdef HAVE_LIBNSS
60defec
+	if (k->flags & KEY_FLAG_NSS) {
60defec
+		if (k->nss->privk != NULL && k->nss->privk->wincx != NULL) {
60defec
+			memset(k->nss->privk->wincx, 0,
60defec
+				strlen(k->nss->privk->wincx));
60defec
+			xfree(k->nss->privk->wincx);
60defec
+			k->nss->privk->wincx = NULL;
60defec
+		}
60defec
+		SECKEY_DestroyPrivateKey(k->nss->privk);
60defec
+		SECKEY_DestroyPublicKey(k->nss->pubk);
60defec
+		xfree(k->nss);
60defec
+	}
60defec
+#endif
60defec
 	xfree(k);
60defec
 }
a3ba41c
 
60defec
diff -up openssh-5.3p1/key.h.nss-keys openssh-5.3p1/key.h
60defec
--- openssh-5.3p1/key.h.nss-keys	2008-06-12 20:40:35.000000000 +0200
a595f1f
+++ openssh-5.3p1/key.h	2009-11-27 13:43:01.000000000 +0100
60defec
@@ -29,11 +29,17 @@
60defec
 #include <openssl/rsa.h>
60defec
 #include <openssl/dsa.h>
60defec
 
60defec
+#ifdef HAVE_LIBNSS
60defec
+#include <nss.h>
60defec
+#include <keyhi.h>
60defec
+#endif
60defec
+
60defec
 typedef struct Key Key;
60defec
 enum types {
60defec
 	KEY_RSA1,
60defec
 	KEY_RSA,
60defec
 	KEY_DSA,
60defec
+	KEY_NSS,
60defec
 	KEY_UNSPEC
60defec
 };
60defec
 enum fp_type {
60defec
@@ -48,16 +54,30 @@ enum fp_rep {
60defec
 
60defec
 /* key is stored in external hardware */
60defec
 #define KEY_FLAG_EXT		0x0001
60defec
+#define KEY_FLAG_NSS		0x0002
60defec
+
60defec
+#ifdef HAVE_LIBNSS
60defec
+typedef struct NSSKey NSSKey;
60defec
+struct NSSKey {
60defec
+	SECKEYPrivateKey *privk;
60defec
+	SECKEYPublicKey *pubk;
60defec
+};
60defec
+#endif
60defec
 
60defec
 struct Key {
60defec
 	int	 type;
60defec
 	int	 flags;
60defec
 	RSA	*rsa;
60defec
 	DSA	*dsa;
60defec
+#ifdef HAVE_LIBNSS
60defec
+	NSSKey  *nss;
60defec
+#endif
60defec
 };
60defec
 
60defec
 Key		*key_new(int);
60defec
 Key		*key_new_private(int);
60defec
+Key 		*key_new_nss(int);
60defec
+Key		*key_new_nss_copy(int, const Key *);
60defec
 void		 key_free(Key *);
60defec
 Key		*key_demote(const Key *);
60defec
 int		 key_equal(const Key *, const Key *);
60defec
diff -up openssh-5.3p1/Makefile.in.nss-keys openssh-5.3p1/Makefile.in
60defec
--- openssh-5.3p1/Makefile.in.nss-keys	2009-08-28 02:47:38.000000000 +0200
a595f1f
+++ openssh-5.3p1/Makefile.in	2009-11-27 13:43:01.000000000 +0100
60defec
@@ -71,7 +71,7 @@ LIBSSH_OBJS=acss.o authfd.o authfile.o b
60defec
 	atomicio.o key.o dispatch.o kex.o mac.o uidswap.o uuencode.o misc.o \
60defec
 	monitor_fdpass.o rijndael.o ssh-dss.o ssh-rsa.o dh.o kexdh.o \
60defec
 	kexgex.o kexdhc.o kexgexc.o scard.o msg.o progressmeter.o dns.o \
60defec
-	entropy.o scard-opensc.o gss-genr.o umac.o jpake.o schnorr.o
60defec
+	entropy.o scard-opensc.o gss-genr.o umac.o jpake.o schnorr.o nsskeys.o
60defec
 
60defec
 SSHOBJS= ssh.o readconf.o clientloop.o sshtty.o \
1c90146
 	sshconnect.o sshconnect1.o sshconnect2.o mux.o \
60defec
diff -up /dev/null openssh-5.3p1/nsskeys.c
a595f1f
--- /dev/null	2009-11-27 11:08:21.619709673 +0100
a595f1f
+++ openssh-5.3p1/nsskeys.c	2009-11-27 13:45:42.000000000 +0100
a595f1f
@@ -0,0 +1,443 @@
c3274cc
+/*
c3274cc
+ * Copyright (c) 2001 Markus Friedl.  All rights reserved.
c3274cc
+ * Copyright (c) 2007 Red Hat, Inc. All rights reserved.
a595f1f
+ * Copyright (c) 2009 Pierre Ossman for Cendio AB
c3274cc
+ *
c3274cc
+ * Redistribution and use in source and binary forms, with or without
c3274cc
+ * modification, are permitted provided that the following conditions
c3274cc
+ * are met:
c3274cc
+ * 1. Redistributions of source code must retain the above copyright
c3274cc
+ *    notice, this list of conditions and the following disclaimer.
c3274cc
+ * 2. Redistributions in binary form must reproduce the above copyright
c3274cc
+ *    notice, this list of conditions and the following disclaimer in the
c3274cc
+ *    documentation and/or other materials provided with the distribution.
c3274cc
+ *
c3274cc
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
c3274cc
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
c3274cc
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
c3274cc
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
c3274cc
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
c3274cc
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
c3274cc
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
c3274cc
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
c3274cc
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
c3274cc
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
c3274cc
+ */
c3274cc
+
c3274cc
+#include "includes.h"
c3274cc
+#ifdef HAVE_LIBNSS
c3274cc
+
c3274cc
+#include <sys/types.h>
c3274cc
+
c3274cc
+#include <stdarg.h>
c3274cc
+#include <string.h>
c3274cc
+#include <unistd.h>
c3274cc
+
c3274cc
+#include <openssl/evp.h>
c3274cc
+
c3274cc
+#include <nss.h>
c3274cc
+#include <keyhi.h>
c3274cc
+#include <pk11pub.h>
c3274cc
+#include <cert.h>
a595f1f
+#include <secmod.h>
a595f1f
+#include <secerr.h>
c3274cc
+
c3274cc
+#include "xmalloc.h"
c3274cc
+#include "key.h"
c3274cc
+#include "log.h"
c3274cc
+#include "misc.h"
c3274cc
+#include "nsskeys.h"
c3274cc
+#include "pathnames.h"
c3274cc
+
c3274cc
+static char *
c3274cc
+password_cb(PK11SlotInfo *slot, PRBool retry, void *arg)
c3274cc
+{
c3274cc
+	char *password = arg;
c3274cc
+	if (retry || password == NULL)
c3274cc
+		return NULL;
c3274cc
+	
c3274cc
+	return PL_strdup(password);
c3274cc
+}
c3274cc
+
c3274cc
+int
c3274cc
+nss_init(PK11PasswordFunc pwfn)
c3274cc
+{
c3274cc
+	char *dbpath;
c3274cc
+	char buf[MAXPATHLEN];
c3274cc
+
c3274cc
+	if (NSS_IsInitialized())
c3274cc
+		return 0;
c3274cc
+
c3274cc
+	if ((dbpath=getenv("NSS_DB_PATH")) == NULL) {
c3274cc
+		struct passwd *pw;
c3274cc
+		if ((pw = getpwuid(getuid())) == NULL ||
c3274cc
+			pw->pw_dir == NULL) {
c3274cc
+			return -1;
c3274cc
+		}
c3274cc
+		snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir,
c3274cc
+			    _PATH_SSH_USER_DIR);
c3274cc
+		dbpath = buf;
c3274cc
+	}
c3274cc
+
a595f1f
+	if (NSS_Init(dbpath) != SECSuccess) {
a595f1f
+		debug("Failed to initialize NSS library. Attempting without DB...");
a595f1f
+		if (NSS_NoDB_Init(NULL) != SECSuccess)
a595f1f
+			return -1;
a595f1f
+	}
c3274cc
+
c3274cc
+	if (pwfn == NULL) {
c3274cc
+		pwfn = password_cb;
c3274cc
+	}
c3274cc
+
c3274cc
+	PK11_SetPasswordFunc(pwfn);
c3274cc
+	
c3274cc
+	return 0;
c3274cc
+}
c3274cc
+
a595f1f
+int
a595f1f
+nss_load_module(const char *modpath)
a595f1f
+{
a595f1f
+	char spec[MAXPATHLEN + 40];
a595f1f
+	SECMODModule *module;
a595f1f
+
a595f1f
+	debug("loading PKCS#11 module '%s'", modpath);
a595f1f
+
a595f1f
+	snprintf(spec, sizeof(spec), "library=\"%s\" name=\"Foobar\"", modpath);
a595f1f
+	module = SECMOD_LoadUserModule(spec, NULL, PR_FALSE);
a595f1f
+	if (!module || !module->loaded) {
a595f1f
+		if (module)
a595f1f
+			SECMOD_DestroyModule(module);
a595f1f
+		return -1;
a595f1f
+	}
a595f1f
+
a595f1f
+	return 0;
a595f1f
+}
a595f1f
+
c3274cc
+static Key *
c3274cc
+make_key_from_privkey(SECKEYPrivateKey *privk, char *password)
c3274cc
+{
c3274cc
+	Key *k;
c3274cc
+	switch (SECKEY_GetPrivateKeyType(privk)) {
c3274cc
+		case rsaKey:
c3274cc
+			k = key_new_nss(KEY_RSA);
c3274cc
+			break;
c3274cc
+		case dsaKey:
c3274cc
+			k = key_new_nss(KEY_DSA);
c3274cc
+			break;
c3274cc
+		default:
c3274cc
+			return NULL;
c3274cc
+	}
c3274cc
+	k->nss->pubk = SECKEY_ConvertToPublicKey(privk);
c3274cc
+	if (k->nss->pubk != NULL) {
c3274cc
+		k->nss->privk = SECKEY_CopyPrivateKey(privk);
c3274cc
+	}
c3274cc
+	if (k->nss->privk != NULL) {
c3274cc
+		if (password != NULL) {
c3274cc
+			k->nss->privk->wincx = xstrdup(password);
c3274cc
+		}
c3274cc
+		return k;
c3274cc
+	}
c3274cc
+	key_free(k);
c3274cc
+	return NULL;
c3274cc
+}
c3274cc
+
c3274cc
+static Key **
c3274cc
+add_key_to_list(Key *k, Key **keys, size_t *i, size_t *allocated)
c3274cc
+{
c3274cc
+	if (*allocated < *i + 2) {
c3274cc
+		*allocated += 16;
c3274cc
+		keys = xrealloc(keys, *allocated, sizeof(k));
c3274cc
+	}
c3274cc
+	keys[*i] = k;
c3274cc
+	(*i)++;
c3274cc
+	keys[*i] = NULL;
c3274cc
+	return keys;
c3274cc
+}
c3274cc
+
c3274cc
+static int
c3274cc
+nss_convert_pubkey(Key *k)
c3274cc
+{
c3274cc
+	u_char *n;
c3274cc
+	unsigned int len;
c3274cc
+	char *p;
c3274cc
+
c3274cc
+	switch (k->type) {
c3274cc
+		case KEY_RSA:
c3274cc
+			n = k->nss->pubk->u.rsa.modulus.data;
c3274cc
+			len = k->nss->pubk->u.rsa.modulus.len;
c3274cc
+
c3274cc
+			if (BN_bin2bn(n, len, k->rsa->n) == NULL) {
c3274cc
+				fatal("nss_convert_pubkey: BN_bin2bn failed");
c3274cc
+			}
c3274cc
+
c3274cc
+			n = k->nss->pubk->u.rsa.publicExponent.data;
c3274cc
+			len = k->nss->pubk->u.rsa.publicExponent.len;
c3274cc
+
c3274cc
+			if (BN_bin2bn(n, len, k->rsa->e) == NULL) {
c3274cc
+				fatal("nss_convert_pubkey: BN_bin2bn failed");
c3274cc
+			}
c3274cc
+			break;
c3274cc
+		case KEY_DSA:
c3274cc
+			n = k->nss->pubk->u.dsa.params.prime.data;
c3274cc
+			len = k->nss->pubk->u.dsa.params.prime.len;
c3274cc
+
c3274cc
+			if (BN_bin2bn(n, len, k->dsa->p) == NULL) {
c3274cc
+				fatal("nss_convert_pubkey: BN_bin2bn failed");
c3274cc
+			}
c3274cc
+
c3274cc
+			n = k->nss->pubk->u.dsa.params.subPrime.data;
c3274cc
+			len = k->nss->pubk->u.dsa.params.subPrime.len;
c3274cc
+
c3274cc
+			if (BN_bin2bn(n, len, k->dsa->q) == NULL) {
c3274cc
+				fatal("nss_convert_pubkey: BN_bin2bn failed");
c3274cc
+			}
c3274cc
+
c3274cc
+			n = k->nss->pubk->u.dsa.params.base.data;
c3274cc
+			len = k->nss->pubk->u.dsa.params.base.len;
c3274cc
+
c3274cc
+			if (BN_bin2bn(n, len, k->dsa->g) == NULL) {
c3274cc
+				fatal("nss_convert_pubkey: BN_bin2bn failed");
c3274cc
+			}
c3274cc
+
c3274cc
+			n = k->nss->pubk->u.dsa.publicValue.data;
c3274cc
+			len = k->nss->pubk->u.dsa.publicValue.len;
c3274cc
+
c3274cc
+			if (BN_bin2bn(n, len, k->dsa->pub_key) == NULL) {
c3274cc
+				fatal("nss_convert_pubkey: BN_bin2bn failed");
c3274cc
+			}
c3274cc
+			break;
c3274cc
+	}
c3274cc
+
adad2a8
+	p = key_fingerprint(k, SSH_FP_MD5, SSH_FP_HEX);
adad2a8
+	debug("fingerprint %u %s", key_size(k), p);
c3274cc
+	xfree(p);
c3274cc
+
c3274cc
+	return 0;
c3274cc
+}
c3274cc
+
a595f1f
+static int
a595f1f
+nss_authenticate(PK11SlotInfo *slot, char *password, int pwprompts, char **output)
a595f1f
+{
a595f1f
+	int i, quit;
a595f1f
+
a595f1f
+	*output = NULL;
a595f1f
+
a595f1f
+	if (!PK11_NeedLogin(slot))
a595f1f
+		return 0;
a595f1f
+
a595f1f
+	for (i = 0; i < pwprompts; i++) {
a595f1f
+		SECStatus rv;
a595f1f
+		CK_TOKEN_INFO info;
a595f1f
+
a595f1f
+		rv = PK11_GetTokenInfo(slot, &info;;
a595f1f
+		if (rv != SECSuccess) {
a595f1f
+			error("Failed to get information for token %s",
a595f1f
+				PK11_GetTokenName(slot));
a595f1f
+			return -1;
a595f1f
+		}
a595f1f
+
a595f1f
+		if (info.flags & CKF_USER_PIN_LOCKED) {
a595f1f
+			error("Passphrase for token %s is locked",
a595f1f
+				PK11_GetTokenName(slot));
a595f1f
+			return -1;
a595f1f
+		}
a595f1f
+
a595f1f
+		if (info.flags & CKF_USER_PIN_FINAL_TRY)
a595f1f
+			debug2("Final passphrase attempt for token %s",
a595f1f
+				PK11_GetTokenName(slot));
a595f1f
+		else if (info.flags & CKF_USER_PIN_COUNT_LOW)
a595f1f
+			debug2("Previous failed passphrase attempt for token %s",
a595f1f
+				PK11_GetTokenName(slot));
a595f1f
+
a595f1f
+		if (password != NULL)
a595f1f
+			*output = xstrdup(password);
a595f1f
+		else {
a595f1f
+			char *prompt;
a595f1f
+			if (asprintf(&prompt, "Enter passphrase for token %s: ",
a595f1f
+				PK11_GetTokenName(slot)) < 0)
a595f1f
+				fatal("password_cb: asprintf failed");
a595f1f
+			*output = read_passphrase(prompt, RP_ALLOW_STDIN);
a595f1f
+		}
a595f1f
+
a595f1f
+		if (strcmp(*output, "") == 0) {
a595f1f
+			debug2("no passphrase given, ignoring slot");
a595f1f
+			quit = 1;
a595f1f
+			goto cleanup;
a595f1f
+		}
a595f1f
+
a595f1f
+		quit = 0;
a595f1f
+
a595f1f
+		rv = PK11_Authenticate(slot, PR_TRUE, *output);
a595f1f
+		if (rv == SECSuccess)
a595f1f
+			return 0;
a595f1f
+
a595f1f
+		switch (PORT_GetError()) {
a595f1f
+		case SEC_ERROR_BAD_PASSWORD:
a595f1f
+			debug2("Incorrect passphrase, try again...");
a595f1f
+			break;
a595f1f
+		case SEC_ERROR_INVALID_ARGS:
a595f1f
+		case SEC_ERROR_BAD_DATA:
a595f1f
+			debug2("Invalid passphrase, try again...");
a595f1f
+			break;
a595f1f
+#if HAVE_SEC_ERROR_LOCKED_PASSWORD
a595f1f
+		case SEC_ERROR_LOCKED_PASSWORD:
a595f1f
+			error("Unable to authenticate, token passphrase is locked");
a595f1f
+			quit = 1;
a595f1f
+			break;
a595f1f
+#endif
a595f1f
+		default:
a595f1f
+			error("Failure while authenticating against token");
a595f1f
+			quit = 1;
a595f1f
+		}
a595f1f
+
a595f1f
+cleanup:
a595f1f
+		memset(*output, 0, strlen(*output));
a595f1f
+		xfree(*output);
a595f1f
+		*output = NULL;
a595f1f
+
a595f1f
+		/* No point in retrying the same password */
a595f1f
+		if (password != NULL)
a595f1f
+			break;
a595f1f
+
a595f1f
+		if (quit)
a595f1f
+			break;
a595f1f
+	}
a595f1f
+
a595f1f
+	return -1;
a595f1f
+}
a595f1f
+
c3274cc
+static Key **
c3274cc
+nss_find_privkeys(const char *tokenname, const char *keyname,
a595f1f
+    char *password, int pwprompts)
c3274cc
+{
c3274cc
+	Key *k = NULL;
c3274cc
+	Key **keys = NULL;
c3274cc
+	PK11SlotList *slots;
c3274cc
+	PK11SlotListElement *sle;
c3274cc
+	size_t allocated = 0;
c3274cc
+	size_t i = 0;
c3274cc
+
c3274cc
+	if ((slots=PK11_FindSlotsByNames(NULL, NULL, tokenname, PR_TRUE)) == NULL) {
c3274cc
+		if (tokenname == NULL) {
c3274cc
+			debug("No NSS token found");
c3274cc
+		} else {
c3274cc
+			debug("NSS token not found: %s", tokenname);
c3274cc
+		}
c3274cc
+		return NULL;
c3274cc
+	}
c3274cc
+	
c3274cc
+	for (sle = slots->head; sle; sle = sle->next) {
c3274cc
+		SECKEYPrivateKeyList *list;
c3274cc
+		SECKEYPrivateKeyListNode *node;
a595f1f
+		char *tmppass;
a595f1f
+
a595f1f
+		if (nss_authenticate(sle->slot, password, pwprompts, &tmppass) == -1)
a595f1f
+			break;
c3274cc
+
c3274cc
+		debug("Looking for: %s:%s", tokenname, keyname);
c3274cc
+		list = PK11_ListPrivKeysInSlot(sle->slot, (char *)keyname,
c3274cc
+			tmppass);
c3274cc
+		if (list == NULL && keyname != NULL) {
c3274cc
+			char *fooname;
c3274cc
+			/* NSS bug workaround */
c3274cc
+			if (asprintf(&fooname, "%s~", keyname) < 0) {
c3274cc
+				error("nss_find_privkey: asprintf failed");
c3274cc
+				PK11_FreeSlotList(slots);
c3274cc
+				return NULL;
c3274cc
+			}
c3274cc
+			list = PK11_ListPrivKeysInSlot(sle->slot, fooname,
c3274cc
+			tmppass);
c3274cc
+			free(fooname);
c3274cc
+		}
c3274cc
+		if (list == NULL && keyname != NULL) {
c3274cc
+			CERTCertificate *cert;
c3274cc
+			SECKEYPrivateKey *privk;
c3274cc
+			cert = CERT_FindCertByNickname(CERT_GetDefaultCertDB(),
c3274cc
+				(char *)keyname);
c3274cc
+			if (cert == NULL)
c3274cc
+				goto cleanup;
c3274cc
+			privk = PK11_FindPrivateKeyFromCert(sle->slot, cert, tmppass);
c3274cc
+			CERT_DestroyCertificate(cert);
c3274cc
+			if (privk == NULL)
c3274cc
+				goto cleanup;
c3274cc
+			if ((k=make_key_from_privkey(privk, tmppass)) != NULL) {
c3274cc
+				nss_convert_pubkey(k);
c3274cc
+				keys = add_key_to_list(k, keys, &i, &allocated);
c3274cc
+			}
c3274cc
+			SECKEY_DestroyPrivateKey(privk);
c3274cc
+		} else {
c3274cc
+			if (list == NULL)
c3274cc
+				goto cleanup;
c3274cc
+			for (node=PRIVKEY_LIST_HEAD(list); !PRIVKEY_LIST_END(node, list);
c3274cc
+				node=PRIVKEY_LIST_NEXT(node))
c3274cc
+				if ((k=make_key_from_privkey(node->key, tmppass)) != NULL) {
c3274cc
+					nss_convert_pubkey(k);
c3274cc
+					keys = add_key_to_list(k, keys, &i, &allocated);
c3274cc
+				}
c3274cc
+			SECKEY_DestroyPrivateKeyList(list);
c3274cc
+		}
c3274cc
+cleanup:
a595f1f
+		if (tmppass != NULL) {
c3274cc
+			memset(tmppass, 0, strlen(tmppass));
c3274cc
+			xfree(tmppass);
c3274cc
+		}
c3274cc
+	}
c3274cc
+	PK11_FreeSlotList(slots);
c3274cc
+
c3274cc
+	return keys;
c3274cc
+}
c3274cc
+
c3274cc
+Key **
c3274cc
+nss_get_keys(const char *tokenname, const char *keyname,
a595f1f
+    char *password, int pwprompts, int num_modules, const char **modules)
c3274cc
+{
a595f1f
+	int i;
c3274cc
+	Key **keys;
c3274cc
+
c3274cc
+	if (nss_init(NULL) == -1) {
c3274cc
+		error("Failed to initialize NSS library");
c3274cc
+		return NULL;
c3274cc
+	}
c3274cc
+
a595f1f
+	for (i = 0;i < num_modules;i++) {
a595f1f
+		if (nss_load_module(modules[i]) == -1) {
a595f1f
+			error("Failed to load PKCS#11 module '%s'", modules[i]);
a595f1f
+			return NULL;
a595f1f
+		}
a595f1f
+	}
a595f1f
+
a595f1f
+	keys = nss_find_privkeys(tokenname, keyname, password, pwprompts);
c3274cc
+	if (keys == NULL && keyname != NULL) {
c3274cc
+		error("Cannot find key in nss, token removed");
c3274cc
+		return NULL;
c3274cc
+	}
c3274cc
+#if 0
c3274cc
+	keys = xcalloc(3, sizeof(Key *));
c3274cc
+
c3274cc
+	if (k->type == KEY_RSA) {
c3274cc
+		n = key_new_nss_copy(KEY_RSA1, k);
c3274cc
+
c3274cc
+		keys[0] = n;
c3274cc
+		keys[1] = k;
c3274cc
+		keys[2] = NULL;
c3274cc
+	} else {
c3274cc
+		keys[0] = k;
c3274cc
+		keys[1] = NULL;
c3274cc
+	}
c3274cc
+#endif
c3274cc
+	return keys;
c3274cc
+}
c3274cc
+
c3274cc
+char *
c3274cc
+nss_get_key_label(Key *key)
c3274cc
+{
c3274cc
+	char *label, *nickname;
c3274cc
+	
c3274cc
+	nickname = PK11_GetPrivateKeyNickname(key->nss->privk);
c3274cc
+	label = xstrdup(nickname);
c3274cc
+	PORT_Free(nickname);
c3274cc
+
c3274cc
+	return label;
c3274cc
+}
c3274cc
+
c3274cc
+#endif /* HAVE_LIBNSS */
60defec
diff -up /dev/null openssh-5.3p1/nsskeys.h
a595f1f
--- /dev/null	2009-11-27 11:08:21.619709673 +0100
a595f1f
+++ openssh-5.3p1/nsskeys.h	2009-11-27 13:43:01.000000000 +0100
c3274cc
@@ -0,0 +1,39 @@
c3274cc
+/*
c3274cc
+ * Copyright (c) 2001 Markus Friedl.  All rights reserved.
c3274cc
+ * Copyright (c) 2007 Red Hat, Inc.  All rights reserved.
c3274cc
+ *
c3274cc
+ * Redistribution and use in source and binary forms, with or without
c3274cc
+ * modification, are permitted provided that the following conditions
c3274cc
+ * are met:
c3274cc
+ * 1. Redistributions of source code must retain the above copyright
c3274cc
+ *    notice, this list of conditions and the following disclaimer.
c3274cc
+ * 2. Redistributions in binary form must reproduce the above copyright
c3274cc
+ *    notice, this list of conditions and the following disclaimer in the
c3274cc
+ *    documentation and/or other materials provided with the distribution.
c3274cc
+ *
c3274cc
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
c3274cc
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
c3274cc
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
c3274cc
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
c3274cc
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
c3274cc
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
c3274cc
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
c3274cc
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
c3274cc
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
c3274cc
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
c3274cc
+ */
c3274cc
+
c3274cc
+#ifndef NSSKEYS_H
c3274cc
+#define NSSKEYS_H
c3274cc
+#ifdef HAVE_LIBNSS
c3274cc
+#include <pk11func.h>
c3274cc
+#include <prtypes.h>
c3274cc
+
c3274cc
+int	nss_init(PK11PasswordFunc);
a595f1f
+Key	**nss_get_keys(const char *, const char *, char *, int , int , const char **);
c3274cc
+char	*nss_get_key_label(Key *);
60defec
+/*void	 sc_close(void);*/
60defec
+/*int	 sc_put_key(Key *, const char *);*/
60defec
+
adad2a8
+#endif
60defec
+#endif
60defec
diff -up openssh-5.3p1/readconf.c.nss-keys openssh-5.3p1/readconf.c
60defec
--- openssh-5.3p1/readconf.c.nss-keys	2009-07-05 23:12:27.000000000 +0200
a595f1f
+++ openssh-5.3p1/readconf.c	2009-11-27 13:43:01.000000000 +0100
60defec
@@ -124,6 +124,7 @@ typedef enum {
60defec
 	oKbdInteractiveAuthentication, oKbdInteractiveDevices, oHostKeyAlias,
60defec
 	oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
60defec
 	oHostKeyAlgorithms, oBindAddress, oSmartcardDevice,
a595f1f
+	oUseNSS, oNSSToken, oNSSModule,
60defec
 	oClearAllForwardings, oNoHostAuthenticationForLocalhost,
60defec
 	oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
60defec
 	oAddressFamily, oGssAuthentication, oGssDelegateCreds,
a595f1f
@@ -210,6 +211,15 @@ static struct {
60defec
 #else
60defec
 	{ "smartcarddevice", oUnsupported },
60defec
 #endif
adad2a8
+#ifdef HAVE_LIBNSS
60defec
+	{ "usenss", oUseNSS },
60defec
+	{ "nsstoken", oNSSToken },
a595f1f
+	{ "nssmodule", oNSSModule },
60defec
+#else
60defec
+	{ "usenss", oUnsupported },
60defec
+	{ "nsstoken", oNSSToken },
a595f1f
+	{ "nssmodule", oUnsupported },
adad2a8
+#endif
60defec
 	{ "clearallforwardings", oClearAllForwardings },
60defec
 	{ "enablesshkeysign", oEnableSSHKeysign },
60defec
 	{ "verifyhostkeydns", oVerifyHostKeyDNS },
a595f1f
@@ -613,6 +623,28 @@ parse_string:
60defec
 		charptr = &options->smartcard_device;
60defec
 		goto parse_string;
adad2a8
 
60defec
+	case oUseNSS:
60defec
+		intptr = &options->use_nss;
60defec
+		goto parse_flag;
60defec
+
60defec
+	case oNSSToken:
60defec
+		charptr = &options->nss_token;
60defec
+		goto parse_command;
60defec
+
a595f1f
+	case oNSSModule:
a595f1f
+		arg = strdelim(&s);
a595f1f
+		if (!arg || *arg == '\0')
a595f1f
+			fatal("%.200s line %d: Missing argument.", filename, linenum);
a595f1f
+		if (*activep) {
a595f1f
+			intptr = &options->num_nss_modules;
a595f1f
+			if (*intptr >= SSH_MAX_NSS_MODULES)
a595f1f
+				fatal("%.200s line %d: Too many PKCS#11 modules specified (max %d).",
a595f1f
+				    filename, linenum, SSH_MAX_NSS_MODULES);
a595f1f
+			charptr = &options->nss_modules[*intptr];
a595f1f
+			*charptr = xstrdup(arg);
a595f1f
+			*intptr = *intptr + 1;
a595f1f
+		}
a595f1f
+		break;
60defec
 	case oProxyCommand:
60defec
 		charptr = &options->proxy_command;
60defec
 parse_command:
a595f1f
@@ -1052,6 +1084,9 @@ initialize_options(Options * options)
60defec
 	options->preferred_authentications = NULL;
60defec
 	options->bind_address = NULL;
60defec
 	options->smartcard_device = NULL;
60defec
+	options->use_nss = -1;
60defec
+	options->nss_token = NULL;
a595f1f
+	options->num_nss_modules = 0;
60defec
 	options->enable_ssh_keysign = - 1;
60defec
 	options->no_host_authentication_for_localhost = - 1;
60defec
 	options->identities_only = - 1;
a595f1f
@@ -1183,6 +1218,8 @@ fill_default_options(Options * options)
60defec
 		options->no_host_authentication_for_localhost = 0;
60defec
 	if (options->identities_only == -1)
60defec
 		options->identities_only = 0;
60defec
+	if (options->use_nss == -1)
60defec
+		options->use_nss = 0;
60defec
 	if (options->enable_ssh_keysign == -1)
60defec
 		options->enable_ssh_keysign = 0;
60defec
 	if (options->rekey_limit == -1)
60defec
diff -up openssh-5.3p1/readconf.h.nss-keys openssh-5.3p1/readconf.h
60defec
--- openssh-5.3p1/readconf.h.nss-keys	2009-07-05 23:12:27.000000000 +0200
a595f1f
+++ openssh-5.3p1/readconf.h	2009-11-27 13:43:01.000000000 +0100
a595f1f
@@ -85,6 +85,10 @@ typedef struct {
60defec
 	char   *preferred_authentications;
60defec
 	char   *bind_address;	/* local socket address for connection to sshd */
60defec
 	char   *smartcard_device; /* Smartcard reader device */
60defec
+	int     use_nss;        /* Use NSS library for keys */
60defec
+	char   *nss_token;      /* Look for NSS keys on token */
a595f1f
+	int     num_nss_modules; /* Number of PCKS#11 modules. */
a595f1f
+	char   *nss_modules[SSH_MAX_NSS_MODULES];
60defec
 	int	verify_host_key_dns;	/* Verify host key using DNS */
60defec
 
60defec
 	int     num_identity_files;	/* Number of files for RSA/DSA identities. */
60defec
diff -up openssh-5.3p1/ssh-add.c.nss-keys openssh-5.3p1/ssh-add.c
60defec
--- openssh-5.3p1/ssh-add.c.nss-keys	2008-02-28 09:13:52.000000000 +0100
a595f1f
+++ openssh-5.3p1/ssh-add.c	2009-11-27 13:43:01.000000000 +0100
adad2a8
@@ -44,6 +44,14 @@
c3274cc
 #include <openssl/evp.h>
ec52761
 #include "openbsd-compat/openssl-compat.h"
c3274cc
 
c3274cc
+#ifdef HAVE_LIBNSS
c3274cc
+#include <nss.h>
c3274cc
+#include <secmod.h>
c3274cc
+#include <pk11pub.h>
c3274cc
+#include <keyhi.h>
c3274cc
+#include <cert.h>
c3274cc
+#endif
c3274cc
+
c3274cc
 #include <fcntl.h>
c3274cc
 #include <pwd.h>
c3274cc
 #include <stdarg.h>
adad2a8
@@ -57,6 +65,7 @@
c3274cc
 #include "rsa.h"
c3274cc
 #include "log.h"
c3274cc
 #include "key.h"
c3274cc
+#include "nsskeys.h"
c3274cc
 #include "buffer.h"
c3274cc
 #include "authfd.h"
c3274cc
 #include "authfile.h"
60defec
@@ -307,6 +316,128 @@ do_file(AuthenticationConnection *ac, in
c3274cc
 	return 0;
c3274cc
 }
c3274cc
 
c3274cc
+#ifdef HAVE_LIBNSS
c3274cc
+static char *
c3274cc
+password_cb(PK11SlotInfo *slot, PRBool retry, void *arg)
c3274cc
+{
c3274cc
+	char **passcache = arg;
c3274cc
+	char *password, *p2 = NULL;
c3274cc
+	char *prompt;
c3274cc
+	
c3274cc
+	if (retry)
c3274cc
+		return NULL;
c3274cc
+	
c3274cc
+	if (asprintf(&prompt, "Enter passphrase for token %s: ",
c3274cc
+		PK11_GetTokenName(slot)) < 0)
c3274cc
+		fatal("password_cb: asprintf failed");
c3274cc
+
c3274cc
+	password = read_passphrase(prompt, RP_ALLOW_STDIN);
c3274cc
+	
c3274cc
+	if (password != NULL && (p2=PL_strdup(password)) == NULL) {
c3274cc
+		memset(password, 0, strlen(password));
c3274cc
+		fatal("password_cb: PL_strdup failed");
c3274cc
+	}
c3274cc
+
c3274cc
+	if (passcache != NULL) {
c3274cc
+		if (*passcache != NULL) {
c3274cc
+			memset(*passcache, 0, strlen(*passcache));
c3274cc
+			xfree(*passcache);
c3274cc
+		}
c3274cc
+		*passcache = password;
c3274cc
+	} else {
c3274cc
+		memset(password, 0, strlen(password));
c3274cc
+		xfree(password);
c3274cc
+	}
c3274cc
+	
c3274cc
+	return p2;
c3274cc
+}
c3274cc
+
c3274cc
+static int
c3274cc
+add_slot_keys(AuthenticationConnection *ac, PK11SlotInfo *slot, int add)
c3274cc
+{
c3274cc
+	SECKEYPrivateKeyList *list;
c3274cc
+	SECKEYPrivateKeyListNode *node;
c3274cc
+	char *passcache = NULL;
c3274cc
+	char *tokenname;
60defec
+	char **xkeyname = NULL;
c3274cc
+	
c3274cc
+	int count = 0;
60defec
+	int i;
c3274cc
+	
c3274cc
+	if (PK11_NeedLogin(slot))
c3274cc
+		PK11_Authenticate(slot, PR_TRUE, &passcache);
c3274cc
+		
c3274cc
+	if ((list=PK11_ListPrivKeysInSlot(slot, NULL, NULL)) == NULL) {
c3274cc
+		return 0;
c3274cc
+	}
c3274cc
+	
c3274cc
+	tokenname = PK11_GetTokenName(slot);
c3274cc
+	
c3274cc
+	for (node=PRIVKEY_LIST_HEAD(list); !PRIVKEY_LIST_END(node, list);
c3274cc
+		node=PRIVKEY_LIST_NEXT(node)) {
c3274cc
+		char *keyname;
c3274cc
+		SECKEYPublicKey *pub;
c3274cc
+		
c3274cc
+		keyname = PK11_GetPrivateKeyNickname(node->key);
c3274cc
+		if (keyname == NULL || *keyname == '\0') {
c3274cc
+			/* no nickname to refer to */
c3274cc
+			CERTCertificate *cert;
c3274cc
+			char *kn;
c3274cc
+			cert = PK11_GetCertFromPrivateKey(node->key);
c3274cc
+			if (cert == NULL)
c3274cc
+				continue;
c3274cc
+			kn = strchr(cert->nickname, ':');
c3274cc
+			if (kn == NULL)
c3274cc
+				kn = cert->nickname;
c3274cc
+			else
c3274cc
+				kn++;
c3274cc
+			keyname = PORT_Strdup(kn);
c3274cc
+			CERT_DestroyCertificate(cert);
c3274cc
+			if (keyname == NULL)
c3274cc
+				continue;
c3274cc
+		}
c3274cc
+		pub = SECKEY_ConvertToPublicKey(node->key);
c3274cc
+		if (pub == NULL) {
c3274cc
+			fprintf(stderr, "No public key for: %s:%s\n",
c3274cc
+				tokenname, keyname);
c3274cc
+			continue; /* not possible to obtain public key */
c3274cc
+		}
c3274cc
+		SECKEY_DestroyPublicKey(pub);
60defec
+	
60defec
+		if ((count % 10) == 0)	
60defec
+			xkeyname = xrealloc (xkeyname, count + 10, sizeof (char *));
c3274cc
+		
60defec
+		xkeyname[count++] = keyname;
60defec
+	}
60defec
+
60defec
+	PK11_Logout(slot);
60defec
+
60defec
+	for (i = 0; i < count; i++) {
60defec
+		if (ssh_update_nss_key(ac, add, tokenname, xkeyname[i],
c3274cc
+			passcache?passcache:"",	lifetime, confirm)) {
c3274cc
+			fprintf(stderr, "Key %s: %s:%s\n",
60defec
+				add?"added":"removed", tokenname, xkeyname[i]);
c3274cc
+		} else {
c3274cc
+			fprintf(stderr, "Could not %s key: %s:%s\n",
60defec
+				add?"add":"remove", tokenname, xkeyname[i]);
c3274cc
+		}
60defec
+		PORT_Free(xkeyname[i]);
c3274cc
+	}
c3274cc
+
60defec
+	if (xkeyname != NULL)
60defec
+		free (xkeyname);
60defec
+
c3274cc
+	if (passcache != NULL) {
c3274cc
+		memset(passcache, 0, strlen(passcache));
c3274cc
+		xfree(passcache);
c3274cc
+	}
c3274cc
+	
c3274cc
+	SECKEY_DestroyPrivateKeyList(list);
c3274cc
+	
c3274cc
+	return count;
c3274cc
+}
c3274cc
+#endif
c3274cc
+
c3274cc
 static void
c3274cc
 usage(void)
c3274cc
 {
60defec
@@ -334,6 +465,10 @@ main(int argc, char **argv)
c3274cc
 	AuthenticationConnection *ac = NULL;
c3274cc
 	char *sc_reader_id = NULL;
c3274cc
 	int i, ch, deleting = 0, ret = 0;
c3274cc
+#ifdef HAVE_LIBNSS
c3274cc
+	char *token_id = NULL;
c3274cc
+	int use_nss = 0;
c3274cc
+#endif
c3274cc
 
c3274cc
 	/* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
c3274cc
 	sanitise_stdfd();
60defec
@@ -351,7 +486,7 @@ main(int argc, char **argv)
c3274cc
 		    "Could not open a connection to your authentication agent.\n");
c3274cc
 		exit(2);
c3274cc
 	}
c3274cc
-	while ((ch = getopt(argc, argv, "lLcdDxXe:s:t:")) != -1) {
c3274cc
+	while ((ch = getopt(argc, argv, "lLcdDnxXe:s:t:T:")) != -1) {
c3274cc
 		switch (ch) {
c3274cc
 		case 'l':
c3274cc
 		case 'L':
60defec
@@ -373,6 +508,11 @@ main(int argc, char **argv)
c3274cc
 			if (delete_all(ac) == -1)
c3274cc
 				ret = 1;
c3274cc
 			goto done;
c3274cc
+#ifdef HAVE_LIBNSS
c3274cc
+		case 'n':
c3274cc
+			use_nss = 1;
c3274cc
+			break;
c3274cc
+#endif
c3274cc
 		case 's':
c3274cc
 			sc_reader_id = optarg;
c3274cc
 			break;
60defec
@@ -387,6 +527,11 @@ main(int argc, char **argv)
c3274cc
 				goto done;
c3274cc
 			}
c3274cc
 			break;
c3274cc
+#ifdef HAVE_LIBNSS
c3274cc
+		case 'T':
c9833c9
+			token_id = optarg;
c9833c9
+			break;
c3274cc
+#endif
c9833c9
 		default:
c9833c9
 			usage();
c9833c9
 			ret = 1;
60defec
@@ -400,6 +545,40 @@ main(int argc, char **argv)
c9833c9
 			ret = 1;
c9833c9
 		goto done;
c3274cc
 	}
c3274cc
+#ifdef HAVE_LIBNSS
adad2a8
+	if (use_nss) {
adad2a8
+		PK11SlotList *slots;
adad2a8
+		PK11SlotListElement *sle;
adad2a8
+		int count = 0;
adad2a8
+		if (nss_init(password_cb) == -1) {
adad2a8
+			fprintf(stderr, "Failed to initialize NSS library\n");
adad2a8
+			ret = 1;
adad2a8
+			goto done;
adad2a8
+		}
adad2a8
+		
adad2a8
+		if ((slots=PK11_GetAllTokens(CKM_INVALID_MECHANISM, PR_FALSE, PR_FALSE,
adad2a8
+			NULL)) == NULL) {
adad2a8
+			fprintf(stderr, "No tokens found\n");
adad2a8
+			ret = 1;
adad2a8
+			goto nss_done;
c9833c9
+		}
a3ba41c
+
adad2a8
+		for (sle = slots->head; sle; sle = sle->next) {
adad2a8
+			int rv;
adad2a8
+			if ((rv=add_slot_keys(ac, sle->slot, !deleting)) == -1) {
adad2a8
+				ret = 1;
adad2a8
+			}
adad2a8
+			count += rv;
adad2a8
+		}
adad2a8
+		if (count == 0) {
adad2a8
+			ret = 1;
adad2a8
+		}
60defec
+nss_done:		
60defec
+		NSS_Shutdown();
60defec
+		clear_pass();
60defec
+		goto done;
60defec
+	}
60defec
+#endif
60defec
 	if (argc == 0) {
60defec
 		char buf[MAXPATHLEN];
60defec
 		struct passwd *pw;
60defec
diff -up openssh-5.3p1/ssh-agent.c.nss-keys openssh-5.3p1/ssh-agent.c
60defec
--- openssh-5.3p1/ssh-agent.c.nss-keys	2009-06-21 09:50:15.000000000 +0200
a595f1f
+++ openssh-5.3p1/ssh-agent.c	2009-11-27 13:43:01.000000000 +0100
60defec
@@ -80,6 +80,10 @@
60defec
 #include "scard.h"
60defec
 #endif
60defec
 
60defec
+#ifdef HAVE_LIBNSS
60defec
+#include "nsskeys.h"
60defec
+#endif
60defec
+
60defec
 #if defined(HAVE_SYS_PRCTL_H)
60defec
 #include <sys/prctl.h>	/* For prctl() and PR_SET_DUMPABLE */
60defec
 #endif
60defec
@@ -714,6 +718,114 @@ send:
60defec
 }
60defec
 #endif /* SMARTCARD */
60defec
 
60defec
+#ifdef HAVE_LIBNSS
60defec
+static void
60defec
+process_add_nss_key (SocketEntry *e)
60defec
+{
60defec
+	char *tokenname = NULL, *keyname = NULL, *password = NULL;
60defec
+	int i, version, success = 0, death = 0, confirm = 0;
60defec
+	Key **keys, *k;
60defec
+	Identity *id;
60defec
+	Idtab *tab;
60defec
+
60defec
+	tokenname = buffer_get_string(&e->request, NULL);
60defec
+	keyname = buffer_get_string(&e->request, NULL);
60defec
+	password = buffer_get_string(&e->request, NULL);
60defec
+
60defec
+	while (buffer_len(&e->request)) {
60defec
+		switch (buffer_get_char(&e->request)) {
60defec
+		case SSH_AGENT_CONSTRAIN_LIFETIME:
60defec
+			death = time(NULL) + buffer_get_int(&e->request);
60defec
+			break;
60defec
+		case SSH_AGENT_CONSTRAIN_CONFIRM:
60defec
+			confirm = 1;
60defec
+			break;
60defec
+		default:
60defec
+			break;
60defec
+		}
60defec
+	}
60defec
+	if (lifetime && !death)
60defec
+		death = time(NULL) + lifetime;
60defec
+
a595f1f
+	keys = nss_get_keys(tokenname, keyname, password, 1, 0, NULL);
60defec
+	/* password is owned by keys[0] now */
60defec
+	xfree(tokenname);
60defec
+	xfree(keyname);
60defec
+
60defec
+	if (keys == NULL) {
60defec
+		memset(password, 0, strlen(password));
60defec
+		xfree(password);
60defec
+		error("nss_get_keys failed");
60defec
+		goto send;
60defec
+	}
60defec
+	for (i = 0; keys[i] != NULL; i++) {
60defec
+		k = keys[i];
60defec
+		version = k->type == KEY_RSA1 ? 1 : 2;
60defec
+		tab = idtab_lookup(version);
60defec
+		if (lookup_identity(k, version) == NULL) {
60defec
+			id = xmalloc(sizeof(Identity));
60defec
+			id->key = k;
60defec
+			id->comment = nss_get_key_label(k);
60defec
+			id->death = death;
60defec
+			id->confirm = confirm;
60defec
+			TAILQ_INSERT_TAIL(&tab->idlist, id, next);
60defec
+			tab->nentries++;
60defec
+			success = 1;
60defec
+		} else {
60defec
+			key_free(k);
60defec
+		}
60defec
+		keys[i] = NULL;
60defec
+	}
60defec
+	xfree(keys);
60defec
+send:
60defec
+	buffer_put_int(&e->output, 1);
60defec
+	buffer_put_char(&e->output,
60defec
+	    success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
60defec
+}
60defec
+
60defec
+static void
60defec
+process_remove_nss_key(SocketEntry *e)
60defec
+{
60defec
+	char *tokenname = NULL, *keyname = NULL, *password = NULL;
60defec
+	int i, version, success = 0;
60defec
+	Key **keys, *k = NULL;
60defec
+	Identity *id;
60defec
+	Idtab *tab;
60defec
+
60defec
+	tokenname = buffer_get_string(&e->request, NULL);
60defec
+	keyname = buffer_get_string(&e->request, NULL);
60defec
+	password = buffer_get_string(&e->request, NULL);
60defec
+
a595f1f
+	keys = nss_get_keys(tokenname, keyname, password, 1, 0, NULL);
60defec
+	xfree(tokenname);
60defec
+	xfree(keyname);
60defec
+	xfree(password);
60defec
+
60defec
+	if (keys == NULL || keys[0] == NULL) {
60defec
+		error("nss_get_keys failed");
60defec
+		goto send;
60defec
+	}
60defec
+	for (i = 0; keys[i] != NULL; i++) {
60defec
+		k = keys[i];
60defec
+		version = k->type == KEY_RSA1 ? 1 : 2;
60defec
+		if ((id = lookup_identity(k, version)) != NULL) {
60defec
+			tab = idtab_lookup(version);
60defec
+			TAILQ_REMOVE(&tab->idlist, id, next);
60defec
+			tab->nentries--;
60defec
+			free_identity(id);
60defec
+			success = 1;
60defec
+		}
60defec
+		key_free(k);
60defec
+		keys[i] = NULL;
60defec
+	}
60defec
+	xfree(keys);
60defec
+send:
60defec
+	buffer_put_int(&e->output, 1);
60defec
+	buffer_put_char(&e->output,
60defec
+	    success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
60defec
+}
60defec
+#endif /* HAVE_LIBNSS */
60defec
+
60defec
 /* dispatch incoming messages */
60defec
 
60defec
 static void
60defec
@@ -806,6 +918,15 @@ process_message(SocketEntry *e)
60defec
 		process_remove_smartcard_key(e);
60defec
 		break;
60defec
 #endif /* SMARTCARD */
60defec
+#ifdef HAVE_LIBNSS
60defec
+	case SSH_AGENTC_ADD_NSS_KEY:
60defec
+	case SSH_AGENTC_ADD_NSS_KEY_CONSTRAINED:
60defec
+		process_add_nss_key(e);
60defec
+		break;
60defec
+	case SSH_AGENTC_REMOVE_NSS_KEY:
60defec
+		process_remove_nss_key(e);
60defec
+		break;
60defec
+#endif /* SMARTCARD */
60defec
 	default:
60defec
 		/* Unknown message.  Respond with failure. */
60defec
 		error("Unknown message %d", type);
60defec
diff -up openssh-5.3p1/ssh.c.nss-keys openssh-5.3p1/ssh.c
60defec
--- openssh-5.3p1/ssh.c.nss-keys	2009-07-05 23:16:56.000000000 +0200
a595f1f
+++ openssh-5.3p1/ssh.c	2009-11-27 13:43:01.000000000 +0100
60defec
@@ -105,6 +105,9 @@
60defec
 #ifdef SMARTCARD
60defec
 #include "scard.h"
60defec
 #endif
60defec
+#ifdef HAVE_LIBNSS
60defec
+#include "nsskeys.h"
60defec
+#endif
60defec
 
60defec
 extern char *__progname;
60defec
 
60defec
@@ -1234,9 +1237,11 @@ load_public_identity_files(void)
60defec
 	int i = 0;
60defec
 	Key *public;
60defec
 	struct passwd *pw;
60defec
-#ifdef SMARTCARD
60defec
+#if defined(SMARTCARD) || defined(HAVE_LIBNSS)
60defec
 	Key **keys;
60defec
+#endif
60defec
 
60defec
+#ifdef SMARTCARD
60defec
 	if (options.smartcard_device != NULL &&
60defec
 	    options.num_identity_files < SSH_MAX_IDENTITY_FILES &&
60defec
 	    (keys = sc_get_keys(options.smartcard_device, NULL)) != NULL) {
a595f1f
@@ -1259,6 +1264,29 @@ load_public_identity_files(void)
60defec
 		xfree(keys);
60defec
 	}
60defec
 #endif /* SMARTCARD */
60defec
+#ifdef HAVE_LIBNSS
60defec
+	if (options.use_nss &&
60defec
+	    options.num_identity_files < SSH_MAX_IDENTITY_FILES &&
a595f1f
+	    (keys = nss_get_keys(options.nss_token, NULL, NULL, 
a595f1f
+	    	options.number_of_password_prompts, options.num_nss_modules,
a595f1f
+	    	options.nss_modules)) != NULL) {
60defec
+		int count;
60defec
+		for (count = 0; keys[count] != NULL; count++) {
60defec
+			memmove(&options.identity_files[1], &options.identity_files[0],
60defec
+			    sizeof(char *) * (SSH_MAX_IDENTITY_FILES - 1));
60defec
+			memmove(&options.identity_keys[1], &options.identity_keys[0],
60defec
+			    sizeof(Key *) * (SSH_MAX_IDENTITY_FILES - 1));
60defec
+			options.num_identity_files++;
60defec
+			options.identity_keys[0] = keys[count];
60defec
+			options.identity_files[0] = nss_get_key_label(keys[count]);
60defec
+		}
60defec
+		if (options.num_identity_files > SSH_MAX_IDENTITY_FILES)
60defec
+			options.num_identity_files = SSH_MAX_IDENTITY_FILES;
60defec
+		i += count;
60defec
+		xfree(keys);
adad2a8
+	}
60defec
+#endif /* HAVE_LIBNSS */
60defec
+
60defec
 	if ((pw = getpwuid(original_real_uid)) == NULL)
60defec
 		fatal("load_public_identity_files: getpwuid failed");
60defec
 	pwname = xstrdup(pw->pw_name);
60defec
diff -up openssh-5.3p1/ssh-dss.c.nss-keys openssh-5.3p1/ssh-dss.c
60defec
--- openssh-5.3p1/ssh-dss.c.nss-keys	2006-11-07 13:14:42.000000000 +0100
a595f1f
+++ openssh-5.3p1/ssh-dss.c	2009-11-27 13:43:01.000000000 +0100
60defec
@@ -39,6 +39,10 @@
60defec
 #include "log.h"
60defec
 #include "key.h"
c3274cc
 
c3274cc
+#ifdef HAVE_LIBNSS
c3274cc
+#include <cryptohi.h>
c3274cc
+#endif
c3274cc
+
60defec
 #define INTBLOB_LEN	20
60defec
 #define SIGBLOB_LEN	(2*INTBLOB_LEN)
c3274cc
 
60defec
@@ -57,6 +61,34 @@ ssh_dss_sign(const Key *key, u_char **si
60defec
 		error("ssh_dss_sign: no DSA key");
c3274cc
 		return -1;
c3274cc
 	}
c3274cc
+#ifdef HAVE_LIBNSS
c3274cc
+	if (key->flags & KEY_FLAG_NSS) {
c3274cc
+		SECItem sigitem;
60defec
+		SECItem *rawsig;
c3274cc
+
c3274cc
+		memset(&sigitem, 0, sizeof(sigitem));
c3274cc
+		if (SEC_SignData(&sigitem, (u_char *)data, datalen, key->nss->privk,
60defec
+			SEC_OID_ANSIX9_DSA_SIGNATURE_WITH_SHA1_DIGEST) != SECSuccess) {
60defec
+			error("ssh_dss_sign: sign failed");
c3274cc
+			return -1;
c3274cc
+		}
60defec
+		
60defec
+		if ((rawsig=DSAU_DecodeDerSig(&sigitem)) == NULL) {
60defec
+			error("ssh_dss_sign: der decode failed");
c3274cc
+			SECITEM_ZfreeItem(&sigitem, PR_FALSE);
c3274cc
+			return -1;
c3274cc
+		}
adad2a8
+		SECITEM_ZfreeItem(&sigitem, PR_FALSE);
60defec
+		if (rawsig->len != SIGBLOB_LEN) {
60defec
+			error("ssh_dss_sign: unsupported signature length %d",
60defec
+				rawsig->len);
60defec
+			SECITEM_ZfreeItem(rawsig, PR_TRUE);
60defec
+			return -1;
60defec
+		}
60defec
+		memcpy(sigblob, rawsig->data, SIGBLOB_LEN);
60defec
+		SECITEM_ZfreeItem(rawsig, PR_TRUE);
c3274cc
+	} else {
c3274cc
+#endif
60defec
 	EVP_DigestInit(&md, evp_md);
c3274cc
 	EVP_DigestUpdate(&md, data, datalen);
c3274cc
 	EVP_DigestFinal(&md, digest, &dlen);
60defec
@@ -80,7 +112,9 @@ ssh_dss_sign(const Key *key, u_char **si
60defec
 	BN_bn2bin(sig->r, sigblob+ SIGBLOB_LEN - INTBLOB_LEN - rlen);
60defec
 	BN_bn2bin(sig->s, sigblob+ SIGBLOB_LEN - slen);
60defec
 	DSA_SIG_free(sig);
c3274cc
-
c3274cc
+#ifdef HAVE_LIBNSS
c3274cc
+	}
c3274cc
+#endif
60defec
 	if (datafellows & SSH_BUG_SIGBLOB) {
60defec
 		if (lenp != NULL)
60defec
 			*lenp = SIGBLOB_LEN;
a595f1f
diff -up openssh-5.3p1/ssh.h.nss-keys openssh-5.3p1/ssh.h
a595f1f
--- openssh-5.3p1/ssh.h.nss-keys	2006-08-05 04:39:41.000000000 +0200
a595f1f
+++ openssh-5.3p1/ssh.h	2009-11-27 13:43:01.000000000 +0100
a595f1f
@@ -28,6 +28,12 @@
a595f1f
 #define SSH_MAX_IDENTITY_FILES		100
a595f1f
 
a595f1f
 /*
a595f1f
+ * Maximum number of PKCS#11 modules that can be specified in configuration
a595f1f
+ * files or on the command line.
a595f1f
+ */
a595f1f
+#define SSH_MAX_NSS_MODULES			10
a595f1f
+
a595f1f
+/*
a595f1f
  * Maximum length of lines in authorized_keys file.
a595f1f
  * Current value permits 16kbit RSA and RSA1 keys and 8kbit DSA keys, with
a595f1f
  * some room for options and comments.
60defec
diff -up openssh-5.3p1/ssh-keygen.c.nss-keys openssh-5.3p1/ssh-keygen.c
60defec
--- openssh-5.3p1/ssh-keygen.c.nss-keys	2009-06-22 08:11:07.000000000 +0200
a595f1f
+++ openssh-5.3p1/ssh-keygen.c	2009-11-27 13:43:01.000000000 +0100
adad2a8
@@ -53,6 +53,11 @@
c3274cc
 #include "scard.h"
c3274cc
 #endif
c3274cc
 
c3274cc
+#ifdef HAVE_LIBNSS
c3274cc
+#include <nss.h>
c3274cc
+#include "nsskeys.h"
c3274cc
+#endif
c3274cc
+
c3274cc
 /* Number of bits in the RSA/DSA key.  This value can be set on the command line. */
c3274cc
 #define DEFAULT_BITS		2048
c3274cc
 #define DEFAULT_BITS_DSA	1024
adad2a8
@@ -501,6 +506,26 @@ do_download(struct passwd *pw, const cha
c3274cc
 }
c3274cc
 #endif /* SMARTCARD */
c3274cc
 
c3274cc
+#ifdef HAVE_LIBNSS
c3274cc
+static void
c3274cc
+do_nss_download(struct passwd *pw, const char *tokenname, const char *keyname)
c3274cc
+{
c3274cc
+	Key **keys = NULL;
c3274cc
+	int i;
c3274cc
+	
a595f1f
+	keys = nss_get_keys(tokenname, keyname, NULL, 1, 0, NULL);
c3274cc
+	if (keys == NULL)
c3274cc
+		fatal("cannot find public key in NSS");
c3274cc
+	for (i = 0; keys[i]; i++) {
c3274cc
+		key_write(keys[i], stdout);
c3274cc
+		key_free(keys[i]);
c3274cc
+		fprintf(stdout, "\n");
c3274cc
+	}
c3274cc
+	xfree(keys);
c3274cc
+	exit(0);
c3274cc
+}
c3274cc
+#endif /* HAVE_LIBNSS */
c3274cc
+
c3274cc
 static void
c3274cc
 do_fingerprint(struct passwd *pw)
c3274cc
 {
adad2a8
@@ -1083,7 +1108,8 @@ main(int argc, char **argv)
c3274cc
 	Key *private, *public;
c3274cc
 	struct passwd *pw;
c3274cc
 	struct stat st;
c3274cc
-	int opt, type, fd, download = 0;
c3274cc
+	int opt, type, fd, download = 1;
c3274cc
+	int use_nss = 0;
c3274cc
 	u_int32_t memory = 0, generator_wanted = 0, trials = 100;
c3274cc
 	int do_gen_candidates = 0, do_screen_candidates = 0;
ec52761
 	BIGNUM *start = NULL;
adad2a8
@@ -1116,7 +1142,7 @@ main(int argc, char **argv)
c3274cc
 	}
c3274cc
 
c9833c9
 	while ((opt = getopt(argc, argv,
c3274cc
-	    "degiqpclBHvxXyF:b:f:t:U:D:P:N:C:r:g:R:T:G:M:S:a:W:")) != -1) {
c3274cc
+	    "degiqpclnBHvxXyF:b:f:t:U:D:P:N:C:r:g:R:T:G:M:S:a:W:")) != -1) {
c3274cc
 		switch (opt) {
c3274cc
 		case 'b':
c3274cc
 			bits = (u_int32_t)strtonum(optarg, 768, 32768, &errstr);
adad2a8
@@ -1156,6 +1182,10 @@ main(int argc, char **argv)
c3274cc
 		case 'g':
c3274cc
 			print_generic = 1;
c3274cc
 			break;
c3274cc
+		case 'n':
c3274cc
+			use_nss = 1;
c3274cc
+			download = 1;
c3274cc
+			break;
c3274cc
 		case 'P':
c3274cc
 			identity_passphrase = optarg;
c3274cc
 			break;
adad2a8
@@ -1187,10 +1217,10 @@ main(int argc, char **argv)
c3274cc
 		case 't':
c3274cc
 			key_type_name = optarg;
c3274cc
 			break;
c3274cc
-		case 'D':
c3274cc
-			download = 1;
c3274cc
-			/*FALLTHROUGH*/
c3274cc
 		case 'U':
c3274cc
+			download = 0;
c3274cc
+			/*FALLTHROUGH*/
c3274cc
+		case 'D':
c3274cc
 			reader_id = optarg;
c3274cc
 			break;
c3274cc
 		case 'v':
adad2a8
@@ -1299,6 +1329,17 @@ main(int argc, char **argv)
c3274cc
 			exit(0);
c3274cc
 		}
c3274cc
 	}
c3274cc
+
c3274cc
+	if (use_nss) {
c3274cc
+#ifdef HAVE_LIBNSS
c3274cc
+		if (download)
c3274cc
+			do_nss_download(pw, reader_id, identity_file);
c3274cc
+		else
c3274cc
+			fatal("no support for NSS key upload.");
c3274cc
+#else
c3274cc
+		fatal("no support for NSS keys.");
c3274cc
+#endif
c3274cc
+	}
c3274cc
 	if (reader_id != NULL) {
c3274cc
 #ifdef SMARTCARD
c3274cc
 		if (download)
60defec
diff -up openssh-5.3p1/ssh-rsa.c.nss-keys openssh-5.3p1/ssh-rsa.c
60defec
--- openssh-5.3p1/ssh-rsa.c.nss-keys	2006-09-01 07:38:37.000000000 +0200
a595f1f
+++ openssh-5.3p1/ssh-rsa.c	2009-11-27 13:43:01.000000000 +0100
60defec
@@ -32,6 +32,10 @@
60defec
 #include "compat.h"
60defec
 #include "ssh.h"
60defec
 
c3274cc
+#ifdef HAVE_LIBNSS
60defec
+#include <cryptohi.h>
c3274cc
+#endif
60defec
+
60defec
 static int openssh_RSA_verify(int, u_char *, u_int, u_char *, u_int, RSA *);
c3274cc
 
60defec
 /* RSASSA-PKCS1-v1_5 (PKCS #1 v2.0 signature) with SHA1 */
60defec
@@ -50,6 +54,38 @@ ssh_rsa_sign(const Key *key, u_char **si
60defec
 		error("ssh_rsa_sign: no RSA key");
60defec
 		return -1;
60defec
 	}
a3ba41c
+
60defec
+	slen = RSA_size(key->rsa);
60defec
+	sig = xmalloc(slen);
a3ba41c
+
60defec
+#ifdef HAVE_LIBNSS
60defec
+	if (key->flags & KEY_FLAG_NSS) {
60defec
+		SECItem sigitem;
60defec
+		SECOidTag alg;
60defec
+
60defec
+		memset(&sigitem, 0, sizeof(sigitem));
60defec
+		alg = (datafellows & SSH_BUG_RSASIGMD5) ?
60defec
+			SEC_OID_PKCS1_MD5_WITH_RSA_ENCRYPTION :
60defec
+			SEC_OID_PKCS1_SHA1_WITH_RSA_ENCRYPTION;
60defec
+
60defec
+		if (SEC_SignData(&sigitem, (u_char *)data, datalen, key->nss->privk,
60defec
+			alg) != SECSuccess) {
60defec
+			error("ssh_rsa_sign: sign failed");
60defec
+			return -1;
60defec
+		}
60defec
+		if (sigitem.len > slen) {
60defec
+			error("ssh_rsa_sign: slen %u slen2 %u", slen, sigitem.len);
60defec
+			xfree(sig);
60defec
+			SECITEM_ZfreeItem(&sigitem, PR_FALSE);
60defec
+			return -1;
60defec
+		}
60defec
+		if (sigitem.len < slen) {
60defec
+			memset(sig, 0, slen - sigitem.len);
60defec
+		}
60defec
+		memcpy(sig+slen-sigitem.len, sigitem.data, sigitem.len);
60defec
+		SECITEM_ZfreeItem(&sigitem, PR_FALSE);
60defec
+	} else {
60defec
+#endif
60defec
 	nid = (datafellows & SSH_BUG_RSASIGMD5) ? NID_md5 : NID_sha1;
60defec
 	if ((evp_md = EVP_get_digestbynid(nid)) == NULL) {
60defec
 		error("ssh_rsa_sign: EVP_get_digestbynid %d failed", nid);
60defec
@@ -59,9 +95,6 @@ ssh_rsa_sign(const Key *key, u_char **si
60defec
 	EVP_DigestUpdate(&md, data, datalen);
60defec
 	EVP_DigestFinal(&md, digest, &dlen);
60defec
 
60defec
-	slen = RSA_size(key->rsa);
60defec
-	sig = xmalloc(slen);
60defec
-
60defec
 	ok = RSA_sign(nid, digest, dlen, sig, &len, key->rsa);
60defec
 	memset(digest, 'd', sizeof(digest));
60defec
 
60defec
@@ -83,6 +116,9 @@ ssh_rsa_sign(const Key *key, u_char **si
60defec
 		xfree(sig);
60defec
 		return -1;
60defec
 	}
60defec
+#ifdef HAVE_LIBNSS
60defec
+	}
60defec
+#endif
60defec
 	/* encode signature */
60defec
 	buffer_init(&b);
60defec
 	buffer_put_cstring(&b, "ssh-rsa");