From 9d5a6ef7858984c182ca42fec7a1367b44a12aa8 Mon Sep 17 00:00:00 2001
From: Dan Horak <dan@danny.cz>
Date: Thu, 15 May 2008 13:42:05 +0200
Subject: [PATCH] add NSS support into ipmidirect plugin


diff --git a/plugins/ipmidirect/Makefile.am b/plugins/ipmidirect/Makefile.am
index ce67400..9d5ec14 100644
--- a/plugins/ipmidirect/Makefile.am
+++ b/plugins/ipmidirect/Makefile.am
@@ -32,6 +32,9 @@
 MAINTAINERCLEANFILES    = Makefile.in *~ core core.*
 
 INCLUDES                = @OPENHPI_INCLUDES@ 
+if HAVE_NSS
+INCLUDES                += @NSS_INCLUDES@
+endif
 
 SUBDIRS                 = t
 DIST_SUBDIRS            = t
@@ -116,7 +119,13 @@ libipmidirect_la_SOURCES= \
 		thread.h \
 		thread.cpp
 
-libipmidirect_la_LIBADD	= @CRYPTO_LIB@ -luuid -lm -lstdc++ $(top_builddir)/utils/libopenhpiutils.la
+libipmidirect_la_LIBADD	= -luuid -lm -lstdc++ $(top_builddir)/utils/libopenhpiutils.la
+if HAVE_OPENSSL
+libipmidirect_la_LIBADD	+= @CRYPTO_LIB@
+endif
+if HAVE_NSS
+libipmidirect_la_LIBADD	+= @NSS_LIBS@
+endif
 libipmidirect_la_LDFLAGS= -module -version-info @HPI_LIB_VERSION@
 
 clean-local:
diff --git a/plugins/ipmidirect/ipmi_auth.cpp b/plugins/ipmidirect/ipmi_auth.cpp
index e2bbd57..67ac2b3 100644
--- a/plugins/ipmidirect/ipmi_auth.cpp
+++ b/plugins/ipmidirect/ipmi_auth.cpp
@@ -32,14 +32,14 @@ IpmiAuthFactory( tIpmiAuthType type )
             return new cIpmiAuthNone;
 
        case eIpmiAuthTypeMd2:
-#ifdef HAVE_OPENSSL_MD2_H
+#if defined(HAVE_OPENSSL_MD2_H) || defined(HAVE_NSS)
             return new cIpmiAuthMd2;
 #else
             break;
 #endif
 
        case eIpmiAuthTypeMd5:
-#ifdef HAVE_OPENSSL_MD5_H
+#if defined(HAVE_OPENSSL_MD5_H) || defined(HAVE_NSS)
             return new cIpmiAuthMd5;
 #else
             break;
@@ -78,6 +78,7 @@ cIpmiAuthNone::Check( cIpmiAuthSg /*d*/[], void * /*code*/ )
 }
 
 
+#if defined(HAVE_OPENSSL)
 #ifdef HAVE_OPENSSL_MD2_H
 #include <openssl/md2.h>
 
@@ -188,6 +189,69 @@ cIpmiAuthMd5::Check( cIpmiAuthSg d[],
   return 0;
 }
 #endif
+#elif defined(HAVE_NSS) /* not HAVE_OPENSSL */
+
+cIpmiAuthMd::cIpmiAuthMd(SECOidTag algo)
+        :cIpmiAuth()
+{
+  context = PK11_CreateDigestContext(algo);
+}
+
+cIpmiAuthMd::~cIpmiAuthMd()
+{
+  PK11_DestroyContext(context, PR_TRUE);
+}
+
+int
+cIpmiAuthMd::Init( const unsigned char *password )
+{
+  memcpy( data, password, 16 );
+
+  return 0;
+}
+
+
+int
+cIpmiAuthMd::Gen( cIpmiAuthSg d[],
+                   void        *output )
+{
+  unsigned int len;
+
+  PK11_DigestBegin(context);
+  PK11_DigestOp(context, data, 16);
+
+  for( int i = 0; d[i].data != 0; i++ )
+       PK11_DigestOp(context, (unsigned char *)(d[i].data), d[i].len);
+
+  PK11_DigestOp(context, data, 16);
+  PK11_DigestFinal(context, (unsigned char *)output, &len, 16);
+ 
+  return 0;
+}
+
+
+int
+cIpmiAuthMd::Check( cIpmiAuthSg d[],
+                     void       *code )
+{
+  unsigned int len;
+  unsigned char md[16];
+
+  PK11_DigestBegin(context);
+  PK11_DigestOp(context, data, 16);
+
+  for( int i = 0; d[i].data != 0; i++ )
+       PK11_DigestOp(context, (unsigned char *)(d[i].data), d[i].len);
+
+  PK11_DigestOp(context, data, 16);
+  PK11_DigestFinal(context, md, &len, 16);
+
+  if ( memcmp( code, md, 16 ) != 0 )
+       return EINVAL;
+
+  return 0;
+}
+#endif
 
 
 int
diff --git a/plugins/ipmidirect/ipmi_auth.h b/plugins/ipmidirect/ipmi_auth.h
index fd66433..1f326f2 100644
--- a/plugins/ipmidirect/ipmi_auth.h
+++ b/plugins/ipmidirect/ipmi_auth.h
@@ -111,6 +111,7 @@ public:
 };
 
 
+#if defined(HAVE_OPENSSL)
 #ifdef HAVE_OPENSSL_MD2_H
 class cIpmiAuthMd2 : public cIpmiAuth
 {
@@ -135,6 +136,36 @@ public:
   virtual int Check( cIpmiAuthSg data[], void *code );
 };
 #endif
+#elif defined(HAVE_NSS) /* ! HAVE_OPENSSL */
+#include <nss.h>
+#include <pk11func.h>
+
+
+class cIpmiAuthMd : public cIpmiAuth
+{
+  unsigned char data[16];
+  PK11Context *context;
+
+public:
+  cIpmiAuthMd(SECOidTag algo);
+  virtual ~cIpmiAuthMd();
+  virtual int Init( const unsigned char *password );
+  virtual int Gen( cIpmiAuthSg   data[], void *output );
+  virtual int Check( cIpmiAuthSg data[], void *code );
+};
+
+class cIpmiAuthMd2 : public cIpmiAuthMd
+{
+public:
+  cIpmiAuthMd2() : cIpmiAuthMd(SEC_OID_MD2) {};
+};
+
+class cIpmiAuthMd5 : public cIpmiAuthMd
+{
+public:
+  cIpmiAuthMd5() : cIpmiAuthMd(SEC_OID_MD5) {};
+};
+#endif
 
 
 class cIpmiAuthStraight : public cIpmiAuth
diff --git a/plugins/ipmidirect/t/Makefile.am b/plugins/ipmidirect/t/Makefile.am
index 79e751d..f6caf86 100644
--- a/plugins/ipmidirect/t/Makefile.am
+++ b/plugins/ipmidirect/t/Makefile.am
@@ -40,7 +40,9 @@ MAINTAINERCLEANFILES 	= Makefile.in *~
 CLEANFILES		= @CLEANFILES@ $(MOSTLYCLEANFILES)
 
 INCLUDES		= @OPENHPI_INCLUDES@ -I$(top_srcdir)/plugins/ipmidirect
-
+if HAVE_NSS
+INCLUDES		+= @NSS_INCLUDES@
+endif
 
 $(CON_REMOTE_SOURCES):
 	if test ! -f $@ -a ! -L $@; then \
@@ -64,11 +66,15 @@ TESTS = \
 
 con_000_SOURCES = con_000.cpp
 nodist_con_000_SOURCES = $(CON_REMOTE_SOURCES)
+if HAVE_OPENSSL
 con_000_LDADD   = @CRYPTO_LIB@
+endif
 
 con_001_SOURCES = con_001.c
 nodist_con_001_SOURCES = $(CON_REMOTE_SOURCES)
+if HAVE_OPENSSL
 con_001_LDADD   = @CRYPTO_LIB@
+endif
 
 thread_000_SOURCES = thread_000.cpp test.h
 nodist_thread_000_SOURCES = $(THREAD_REMOTE_SOURCES)
-- 
1.5.4.1

