vishalmishra434 / rpms / openssh

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