[csw-devel] SF.net SVN: gar:[11676] csw/mgar/pkg/openssl/trunk/files

chninkel at users.sourceforge.net chninkel at users.sourceforge.net
Sun Nov 21 18:07:29 CET 2010


Revision: 11676
          http://gar.svn.sourceforge.net/gar/?rev=11676&view=rev
Author:   chninkel
Date:     2010-11-21 17:07:28 +0000 (Sun, 21 Nov 2010)

Log Message:
-----------
openssl: added pkcs 11 files (wip)

Added Paths:
-----------
    csw/mgar/pkg/openssl/trunk/files/README.pkcs11
    csw/mgar/pkg/openssl/trunk/files/pkcs11_engine-0.9.8l.patch.2009-11-19
    csw/mgar/pkg/openssl/trunk/files/pkcs11_engine-0.9.8o.patch.2010-09-03

Added: csw/mgar/pkg/openssl/trunk/files/README.pkcs11
===================================================================
--- csw/mgar/pkg/openssl/trunk/files/README.pkcs11	                        (rev 0)
+++ csw/mgar/pkg/openssl/trunk/files/README.pkcs11	2010-11-21 17:07:28 UTC (rev 11676)
@@ -0,0 +1,242 @@
+PKCS#11 engine support for OpenSSL 0.9.8l
+=========================================
+
+[Nov 19, 2009]
+
+Contents:
+
+Overview
+Revisions of the patch for 0.9.8 branch
+FAQs
+Feedback
+
+Overview
+========
+
+This patch containing code available in OpenSolaris adds support for PKCS#11
+engine into OpenSSL and implements PKCS#11 v2.20. It is to be applied against
+OpenSSL 0.9.8l source code distribution as shipped by OpenSSL.Org. Your system
+must provide PKCS#11 backend otherwise the patch is useless. You provide the
+PKCS#11 library name during the build configuration phase, see below.
+
+Patch can be applied like this:
+
+	# NOTE: use gtar if on Solaris
+	tar xfzv openssl-0.9.8l.tar.gz
+	# now download the patch to the current directory
+	# ...
+	cd openssl-0.9.8l
+	# NOTE: must use gpatch if on Solaris (is part of the system)
+	patch -p1 < path-to/pkcs11_engine-0.9.8l.patch.2009-11-19
+
+It is designed to support pure acceleration for RSA, DSA, DH and all the
+symetric ciphers and message digest algorithms that PKCS#11 and OpenSSL share
+except for missing support for patented algorithms MDC2, RC3, RC5 and IDEA.
+
+According to the PKCS#11 providers installed on your machine, it can support
+following mechanisms:
+
+	RSA, DSA, DH, RAND, DES-CBC, DES-EDE3-CBC, DES-ECB, DES-EDE3, RC4,
+	AES-128-CBC, AES-192-CBC, AES-256-CBC, AES-128-ECB, AES-192-ECB,
+	AES-256-ECB, AES-128-CTR, AES-192-CTR, AES-256-CTR, MD5, SHA1, SHA224,
+	SHA256, SHA384, SHA512
+
+Note that for AES counter mode the application must provide their own EVP
+functions since OpenSSL doesn't support counter mode through EVP yet. You may
+see OpenSSH source code (cipher.c) to get the idea how to do that. SunSSH is an
+example of code that uses the PKCS#11 engine and deals with the fork-safety
+problem (see engine.c and packet.c files if interested).
+
+You must provide the location of PKCS#11 library in your system to the
+configure script. You will be instructed to do that when you try to run the
+config script:
+
+	$ ./config 
+	Operating system: i86pc-whatever-solaris2
+	Configuring for solaris-x86-cc
+	You must set --pk11-libname for PKCS#11 library.
+	See README.pkcs11 for more information.
+
+Taking openCryptoki project on Linux AMD64 box as an example, you would run
+configure script like this:
+
+	./config --pk11-libname=/usr/lib64/pkcs11/PKCS11_API.so
+
+To check whether newly built openssl really supports PKCS#11 it's enough to run
+"apps/openssl engine" and look for "(pkcs11) PKCS #11 engine support" in the
+output. If you see no PKCS#11 engine support check that the built openssl binary
+and the PKCS#11 library from --pk11-libname don't conflict on 32/64 bits.
+
+The patch, during various phases of development, was tested on Solaris against
+PKCS#11 engine available from Solaris Cryptographic Framework (Solaris 10 and
+OpenSolaris) and also on Linux using PKCS#11 libraries from openCryptoki project
+(see openCryptoki website http://sourceforge.net/projects/opencryptoki for more
+information). Some Linux distributions even ship those libraries with the
+system. The patch should work on any system that is supported by OpenSSL itself
+and has functional PKCS#11 library.
+
+The patch contains "RSA Security Inc. PKCS #11 Cryptographic Token Interface
+(Cryptoki)" - files cryptoki.h, pkcs11.h, pkcs11f.h and pkcs11t.h which are
+copyrighted by RSA Security Inc., see pkcs11.h for more information.
+
+Other added/modified code in this patch is copyrighted by Sun Microsystems,
+Inc. and is released under the OpenSSL license (see LICENSE file for more
+information).
+
+Revisions of the patch for 0.9.8 branch
+=======================================
+
+2009-11-19
+- adjusted for OpenSSL version 0.9.8l
+
+- bugs and RFEs:
+
+	6479874 OpenSSL should support RSA key by reference/hardware keystores
+	6896677 PKCS#11 engine's hw_pk11_err.h needs to be split
+	6732677 make check to trigger Solaris specific code automatic in the
+		PKCS#11 engine
+
+2009-03-11
+- adjusted for OpenSSL version 0.9.8j 
+
+- README.pkcs11 moved out of the patch, and is shipped together with it in a
+  tarball instead so that it can be read before the patch is applied.
+
+- fixed bugs:
+
+	6804216 pkcs#11 engine should support a key length range for RC4
+	6734038 Apache SSL web server using the pkcs11 engine fails to start if
+		meta slot is disabled
+
+2008-12-02
+- fixed bugs and RFEs (most of the work done by Vladimir Kotal)
+
+	6723504 more granular locking in PKCS#11 engine
+	6667128 CRYPTO_LOCK_PK11_ENGINE assumption does not hold true
+	6710420 PKCS#11 engine source should be lint clean
+	6747327 PKCS#11 engine atfork handlers need to be aware of guys who take
+		it seriously
+	6746712 PKCS#11 engine source code should be cstyle clean
+	6731380 return codes of several functions are not checked in the PKCS#11
+		engine code
+	6746735 PKCS#11 engine should use extended FILE space API
+	6734038 Apache SSL web server using the pkcs11 engine fails to start if
+		meta slot is disabled
+
+2008-08-01
+- fixed bug
+
+	6731839 OpenSSL PKCS#11 engine no longer uses n2cp for symmetric ciphers
+		and digests
+
+- Solaris specific code for slot selection made automatic
+
+2008-07-29
+- update the patch to OpenSSL 0.9.8h version
+- pkcs11t.h updated to the latest version:
+
+	6545665 make CKM_AES_CTR available to non-kernel users
+
+- fixed bugs in the engine code:
+
+	6602801 PK11_SESSION cache has to employ reference counting scheme for
+		asymmetric key operations
+	6605538 pkcs11 functions C_FindObjects[{Init,Final}]() not called
+		atomically
+	6607307 pkcs#11 engine can't read RSA private keys
+	6652362 pk11_RSA_finish() is cutting corners
+	6662112 pk11_destroy_{rsa,dsa,dh}_key_objects() use locking in
+		suboptimal way
+	6666625 pk11_destroy_{rsa,dsa,dh}_key_objects() should be more
+		resilient to destroy failures
+	6667273 OpenSSL engine should not use free() but OPENSSL_free()
+	6670363 PKCS#11 engine fails to reuse existing symmetric keys
+	6678135 memory corruption in pk11_DH_generate_key() in pkcs#11 engine
+	6678503 DSA signature conversion in pk11_dsa_do_verify() ignores size
+		of big numbers leading to failures
+	6706562 pk11_DH_compute_key() returns 0 in case of failure instead of
+		-1
+	6706622 pk11_load_{pub,priv}key create corrupted RSA key references
+	6707129 return values from BN_new() in pk11_DH_generate_key() are not
+		checked
+	6707274 DSA/RSA/DH PKCS#11 engine operations need to be resistant to
+		structure reuse
+	6707782 OpenSSL PKCS#11 engine pretends to be aware of
+		OPENSSL_NO_{RSA,DSA,DH}
+	defines but fails miserably
+	6709966 make check_new_*() to return values to indicate cache hit/miss
+	6705200 pk11_dh struct initialization in PKCS#11 engine is missing
+		generate_params parameter
+	6709513 PKCS#11 engine sets IV length even for ECB modes
+	6728296 buffer length not initialized for C_(En|De)crypt_Final() in the
+		PKCS#11 engine
+	6728871 PKCS#11 engine must reset global_session in pk11_finish()
+
+- new features and enhancements:
+
+	6562155 OpenSSL pkcs#11 engine needs support for SHA224/256/384/512
+	6685012 OpenSSL pkcs#11 engine needs support for new cipher modes
+	6725903 OpenSSL PKCS#11 engine shouldn't use soft token for symmetric
+		ciphers and digests
+
+2007-10-15
+- update for 0.9.8f version
+- update for "6607670 teach pkcs#11 engine how to use keys be reference"
+
+2007-10-02
+- draft for "6607670 teach pkcs#11 engine how to use keys be reference"
+- draft for "6607307 pkcs#11 engine can't read RSA private keys"
+
+2007-09-26
+- 6375348 Using pkcs11 as the SSLCryptoDevice with Apache/OpenSSL causes
+	  significant performance drop
+- 6573196 memory is leaked when OpenSSL is used with PKCS#11 engine
+
+2007-05-25
+- 6558630 race in OpenSSL pkcs11 engine when using symetric block ciphers
+
+2007-05-19
+- initial patch for 0.9.8e using latest OpenSolaris code
+
+FAQs
+====
+
+(1) my build failed on Linux distro with this error:
+
+../libcrypto.a(hw_pk11.o): In function `pk11_library_init':
+hw_pk11.c:(.text+0x20f5): undefined reference to `pthread_atfork'
+
+Answer:
+
+	- don't use "no-threads" when configuring
+	- if you didn't then OpenSSL failed to create a threaded library by
+	  default. You may manually edit Configure and try again. Look for the
+	  architecture that Configure printed, for example:
+
+Configured for linux-elf.
+
+	- then edit Configure, find string "linux-elf" (inluding the quotes),
+	  and add flags to support threads to the 4th column of the 2nd string.
+	  If you build with GCC then adding "-pthread" should be enough. With
+	  "linux-elf" as an example, you would add " -pthread" right after
+	  "-D_REENTRANT", like this:
+
+....-O3 -fomit-frame-pointer -Wall::-D_REENTRANT -pthread::-ldl:.....
+
+(2) I'm using MinGW/MSYS environment and get undeclared reference error for
+pthread_atfork() function when trying to build OpenSSL with the patch.
+
+Answer:
+
+	Sorry, pthread_atfork() is not implemented in the current pthread-win32
+	(as of Nov 2009). You can not use the patch there.
+
+
+Feedback
+========
+
+Please send feedback to security-discuss at opensolaris.org. The patch was
+created by Jan.Pechanec at Sun.COM from code available in OpenSolaris.
+
+Latest version should be always available on http://blogs.sun.com/janp.
+

Added: csw/mgar/pkg/openssl/trunk/files/pkcs11_engine-0.9.8l.patch.2009-11-19
===================================================================
--- csw/mgar/pkg/openssl/trunk/files/pkcs11_engine-0.9.8l.patch.2009-11-19	                        (rev 0)
+++ csw/mgar/pkg/openssl/trunk/files/pkcs11_engine-0.9.8l.patch.2009-11-19	2010-11-21 17:07:28 UTC (rev 11676)
@@ -0,0 +1,12674 @@
+diff -cr openssl-0.9.8l/Configure openssl-0.9.8l-with_pkcs11_engine/Configure
+*** openssl-0.9.8l/Configure	Thu Nov  5 13:07:06 2009
+--- openssl-0.9.8l-with_pkcs11_engine/Configure	Thu Nov 19 17:16:33 2009
+***************
+*** 12,18 ****
+  
+  # see INSTALL for instructions.
+  
+! my $usage="Usage: Configure [no-<cipher> ...] [enable-<cipher> ...] [experimental-<cipher> ...] [-Dxxx] [-lxxx] [-Lxxx] [-fxxx] [-Kxxx] [no-hw-xxx|no-hw] [[no-]threads] [[no-]shared] [[no-]zlib|zlib-dynamic] [enable-montasm] [no-asm] [no-dso] [no-krb5] [386] [--prefix=DIR] [--openssldir=OPENSSLDIR] [--with-xxx[=vvv]] [--test-sanity] os/compiler[:flags]\n";
+  
+  # Options:
+  #
+--- 12,18 ----
+  
+  # see INSTALL for instructions.
+  
+! my $usage="Usage: Configure --pk11-libname=PK11_LIB_LOCATION [no-<cipher> ...] [enable-<cipher> ...] [experimental-<cipher> ...] [-Dxxx] [-lxxx] [-Lxxx] [-fxxx] [-Kxxx] [no-hw-xxx|no-hw] [[no-]threads] [[no-]shared] [[no-]zlib|zlib-dynamic] [enable-montasm] [no-asm] [no-dso] [no-krb5] [386] [--prefix=DIR] [--openssldir=OPENSSLDIR] [--with-xxx[=vvv]] [--test-sanity] os/compiler[:flags]\n";
+  
+  # Options:
+  #
+***************
+*** 21,26 ****
+--- 21,29 ----
+  # --prefix      prefix for the OpenSSL include, lib and bin directories
+  #               (Default: the OPENSSLDIR directory)
+  #
++ # --pk11-libname  PKCS#11 library name.
++ #               (Default: none)
++ #
+  # --install_prefix  Additional prefix for package builders (empty by
+  #               default).  This needn't be set in advance, you can
+  #               just as well use "make INSTALL_PREFIX=/whatever install".
+***************
+*** 580,585 ****
+--- 583,591 ----
+  my $idx_ranlib = $idx++;
+  my $idx_arflags = $idx++;
+  
++ # PKCS#11 engine patch
++ my $pk11_libname="";
++ 
+  my $prefix="";
+  my $openssldir="";
+  my $exe_ext="";
+***************
+*** 812,817 ****
+--- 818,827 ----
+  				{
+  				$flags.=$_." ";
+  				}
++                         elsif (/^--pk11-libname=(.*)$/)
++                                 {
++                                 $pk11_libname=$1;
++                                 }
+  			elsif (/^--prefix=(.*)$/)
+  				{
+  				$prefix=$1;
+***************
+*** 943,948 ****
+--- 953,965 ----
+  	exit 0;
+  }
+  
++ if (! $pk11_libname)
++         {
++         print STDERR "You must set --pk11-libname for PKCS#11 library.\n";
++         print STDERR "See README.pkcs11 for more information.\n";
++         exit 1;
++         }
++ 
+  if ($target =~ m/^CygWin32(-.*)$/) {
+  	$target = "Cygwin".$1;
+  }
+***************
+*** 1103,1108 ****
+--- 1120,1127 ----
+  if ($flags ne "")	{ $cflags="$flags$cflags"; }
+  else			{ $no_user_cflags=1;       }
+  
++ $cflags="-DPK11_LIB_LOCATION=\"$pk11_libname\" $cflags";
++ 
+  # Kerberos settings.  The flavor must be provided from outside, either through
+  # the script "config" or manually.
+  if (!$no_krb5)
+***************
+*** 1456,1461 ****
+--- 1475,1481 ----
+  	s/^VERSION=.*/VERSION=$version/;
+  	s/^MAJOR=.*/MAJOR=$major/;
+  	s/^MINOR=.*/MINOR=$minor/;
++ 	s/^PK11_LIB_LOCATION=.*/PK11_LIB_LOCATION=$pk11_libname/;
+  	s/^SHLIB_VERSION_NUMBER=.*/SHLIB_VERSION_NUMBER=$shlib_version_number/;
+  	s/^SHLIB_VERSION_HISTORY=.*/SHLIB_VERSION_HISTORY=$shlib_version_history/;
+  	s/^SHLIB_MAJOR=.*/SHLIB_MAJOR=$shlib_major/;
+diff -cr openssl-0.9.8l/Makefile openssl-0.9.8l-with_pkcs11_engine/Makefile
+*** openssl-0.9.8l/Makefile	Thu Nov  5 17:15:53 2009
+--- openssl-0.9.8l-with_pkcs11_engine/Makefile	Thu Nov 19 17:28:00 2009
+***************
+*** 11,21 ****
+  SHLIB_VERSION_HISTORY=
+  SHLIB_MAJOR=0
+  SHLIB_MINOR=9.8
+! SHLIB_EXT=
+! PLATFORM=dist
+! OPTIONS= no-camellia no-capieng no-cms no-gmp no-jpake no-krb5 no-mdc2 no-montasm no-rc5 no-rfc3779 no-seed no-shared no-zlib no-zlib-dynamic
+! CONFIGURE_ARGS=dist
+! SHLIB_TARGET=
+  
+  # HERE indicates where this Makefile lives.  This can be used to indicate
+  # where sub-Makefiles are expected to be.  Currently has very limited usage,
+--- 11,21 ----
+  SHLIB_VERSION_HISTORY=
+  SHLIB_MAJOR=0
+  SHLIB_MINOR=9.8
+! SHLIB_EXT=.so.$(SHLIB_MAJOR).$(SHLIB_MINOR)
+! PLATFORM=solaris64-x86_64-gcc
+! OPTIONS=--pk11-libname=/usr/lib/64/libpkcs11.so.1 no-camellia no-capieng no-cms no-gmp no-jpake no-krb5 no-mdc2 no-montasm no-rc5 no-rfc3779 no-seed no-shared no-zlib no-zlib-dynamic
+! CONFIGURE_ARGS=solaris64-x86_64-gcc --pk11-libname=/usr/lib/64/libpkcs11.so.1
+! SHLIB_TARGET=solaris-shared
+  
+  # HERE indicates where this Makefile lives.  This can be used to indicate
+  # where sub-Makefiles are expected to be.  Currently has very limited usage,
+***************
+*** 28,33 ****
+--- 28,36 ----
+  INSTALL_PREFIX=
+  INSTALLTOP=/usr/local/ssl
+  
++ # You must set this through --pk11-libname configure option.
++ PK11_LIB_LOCATION=/usr/lib/64/libpkcs11.so.1
++ 
+  # Do not edit this manually. Use Configure --openssldir=DIR do change this!
+  OPENSSLDIR=/usr/local/ssl
+  
+***************
+*** 59,78 ****
+  # equal 4.
+  # PKCS1_CHECK - pkcs1 tests.
+  
+! CC= cc
+! CFLAG= -O
+  DEPFLAG= -DOPENSSL_NO_CAMELLIA -DOPENSSL_NO_CAPIENG -DOPENSSL_NO_CMS -DOPENSSL_NO_GMP -DOPENSSL_NO_JPAKE -DOPENSSL_NO_MDC2 -DOPENSSL_NO_RC5 -DOPENSSL_NO_RFC3779 -DOPENSSL_NO_SEED
+  PEX_LIBS= 
+! EX_LIBS= 
+  EXE_EXT= 
+  ARFLAGS= 
+  AR=ar $(ARFLAGS) r
+  ARD=ar $(ARFLAGS) d
+  RANLIB= /usr/bin/ranlib
+! PERL= /usr/bin/perl5
+  TAR= tar
+  TARFLAGS= --no-recursion
+! MAKEDEPPROG=makedepend
+  
+  # We let the C compiler driver to take care of .s files. This is done in
+  # order to be excused from maintaining a separate set of architecture
+--- 62,81 ----
+  # equal 4.
+  # PKCS1_CHECK - pkcs1 tests.
+  
+! CC= gcc
+! CFLAG= -DOPENSSL_THREADS -D_REENTRANT -DDSO_DLFCN -DHAVE_DLFCN_H -DPK11_LIB_LOCATION=\"/usr/lib/64/libpkcs11.so.1\" -m64 -O3 -Wall -DL_ENDIAN -DMD32_REG_T=int -DOPENSSL_BN_ASM_MONT -DSHA1_ASM -DSHA256_ASM -DSHA512_ASM -DMD5_ASM -DAES_ASM
+  DEPFLAG= -DOPENSSL_NO_CAMELLIA -DOPENSSL_NO_CAPIENG -DOPENSSL_NO_CMS -DOPENSSL_NO_GMP -DOPENSSL_NO_JPAKE -DOPENSSL_NO_MDC2 -DOPENSSL_NO_RC5 -DOPENSSL_NO_RFC3779 -DOPENSSL_NO_SEED
+  PEX_LIBS= 
+! EX_LIBS= -lsocket -lnsl -ldl
+  EXE_EXT= 
+  ARFLAGS= 
+  AR=ar $(ARFLAGS) r
+  ARD=ar $(ARFLAGS) d
+  RANLIB= /usr/bin/ranlib
+! PERL= /usr/bin/perl
+  TAR= tar
+  TARFLAGS= --no-recursion
+! MAKEDEPPROG= gcc
+  
+  # We let the C compiler driver to take care of .s files. This is done in
+  # order to be excused from maintaining a separate set of architecture
+***************
+*** 87,102 ****
+  PROCESSOR= 
+  
+  # CPUID module collects small commonly used assembler snippets
+! CPUID_OBJ= 
+! BN_ASM= bn_asm.o
+  DES_ENC= des_enc.o fcrypt_b.o
+! AES_ASM_OBJ= aes_core.o aes_cbc.o
+  BF_ENC= bf_enc.o
+  CAST_ENC= c_enc.o
+! RC4_ENC= rc4_enc.o rc4_skey.o
+  RC5_ENC= rc5_enc.o
+! MD5_ASM_OBJ= 
+! SHA1_ASM_OBJ= 
+  RMD160_ASM_OBJ= 
+  
+  # KRB5 stuff
+--- 90,105 ----
+  PROCESSOR= 
+  
+  # CPUID module collects small commonly used assembler snippets
+! CPUID_OBJ= x86_64cpuid.o
+! BN_ASM= x86_64-gcc.o x86_64-mont.o
+  DES_ENC= des_enc.o fcrypt_b.o
+! AES_ASM_OBJ= aes-x86_64.o
+  BF_ENC= bf_enc.o
+  CAST_ENC= c_enc.o
+! RC4_ENC= rc4-x86_64.o
+  RC5_ENC= rc5_enc.o
+! MD5_ASM_OBJ= md5-x86_64.o
+! SHA1_ASM_OBJ= sha1-x86_64.o sha256-x86_64.o sha512-x86_64.o
+  RMD160_ASM_OBJ= 
+  
+  # KRB5 stuff
+***************
+*** 169,176 ****
+  SHARED_SSL=libssl$(SHLIB_EXT)
+  SHARED_FIPS=
+  SHARED_LIBS=
+! SHARED_LIBS_LINK_EXTS=
+! SHARED_LDFLAGS=
+  
+  GENERAL=        Makefile
+  BASENAME=       openssl
+--- 172,179 ----
+  SHARED_SSL=libssl$(SHLIB_EXT)
+  SHARED_FIPS=
+  SHARED_LIBS=
+! SHARED_LIBS_LINK_EXTS=.so.$(SHLIB_MAJOR) .so
+! SHARED_LDFLAGS=-m64 -shared -static-libgcc
+  
+  GENERAL=        Makefile
+  BASENAME=       openssl
+diff -cr openssl-0.9.8l/Makefile.bak openssl-0.9.8l-with_pkcs11_engine/Makefile.bak
+*** openssl-0.9.8l/Makefile.bak	Thu Nov  5 16:23:10 2009
+--- openssl-0.9.8l-with_pkcs11_engine/Makefile.bak	Thu Nov 19 17:16:33 2009
+***************
+*** 12,20 ****
+  SHLIB_MAJOR=0
+  SHLIB_MINOR=9.8
+  SHLIB_EXT=
+! PLATFORM=debug-ben-debug
+! OPTIONS= no-camellia no-capieng no-cms no-gmp no-jpake no-krb5 no-mdc2 no-montasm no-rc5 no-rfc3779 no-seed no-shared no-zlib no-zlib-dynamic
+! CONFIGURE_ARGS=debug-ben-debug
+  SHLIB_TARGET=
+  
+  # HERE indicates where this Makefile lives.  This can be used to indicate
+--- 12,20 ----
+  SHLIB_MAJOR=0
+  SHLIB_MINOR=9.8
+  SHLIB_EXT=
+! PLATFORM=dist
+! OPTIONS=--pk11-libname=/usr/lib/64/libpkcs11.so.1 no-camellia no-capieng no-cms no-gmp no-jpake no-krb5 no-mdc2 no-montasm no-rc5 no-rfc3779 no-seed no-shared no-zlib no-zlib-dynamic
+! CONFIGURE_ARGS=dist
+  SHLIB_TARGET=
+  
+  # HERE indicates where this Makefile lives.  This can be used to indicate
+***************
+*** 28,33 ****
+--- 28,36 ----
+  INSTALL_PREFIX=
+  INSTALLTOP=/usr/local/ssl
+  
++ # You must set this through --pk11-libname configure option.
++ PK11_LIB_LOCATION=/usr/lib/64/libpkcs11.so.1
++ 
+  # Do not edit this manually. Use Configure --openssldir=DIR do change this!
+  OPENSSLDIR=/usr/local/ssl
+  
+***************
+*** 59,66 ****
+  # equal 4.
+  # PKCS1_CHECK - pkcs1 tests.
+  
+! CC= gcc
+! CFLAG= -Wall -pedantic -DPEDANTIC -Wno-long-long -Wsign-compare -Wmissing-prototypes -Wshadow -Wformat -Werror -DCRYPTO_MDEBUG_ALL -DCRYPTO_MDEBUG_ABORT -DREF_CHECK -DOPENSSL_NO_DEPRECATED -DBN_DEBUG -DREF_CHECK -DCONF_DEBUG -DBN_CTX_DEBUG  -DDEBUG_SAFESTACK -g3 -O2 -pipe
+  DEPFLAG= -DOPENSSL_NO_CAMELLIA -DOPENSSL_NO_CAPIENG -DOPENSSL_NO_CMS -DOPENSSL_NO_GMP -DOPENSSL_NO_JPAKE -DOPENSSL_NO_MDC2 -DOPENSSL_NO_RC5 -DOPENSSL_NO_RFC3779 -DOPENSSL_NO_SEED
+  PEX_LIBS= 
+  EX_LIBS= 
+--- 62,69 ----
+  # equal 4.
+  # PKCS1_CHECK - pkcs1 tests.
+  
+! CC= cc
+! CFLAG= -O
+  DEPFLAG= -DOPENSSL_NO_CAMELLIA -DOPENSSL_NO_CAPIENG -DOPENSSL_NO_CMS -DOPENSSL_NO_GMP -DOPENSSL_NO_JPAKE -DOPENSSL_NO_MDC2 -DOPENSSL_NO_RC5 -DOPENSSL_NO_RFC3779 -DOPENSSL_NO_SEED
+  PEX_LIBS= 
+  EX_LIBS= 
+***************
+*** 87,102 ****
+  PROCESSOR= 
+  
+  # CPUID module collects small commonly used assembler snippets
+! CPUID_OBJ= 
+! BN_ASM= bn_asm.o
+  DES_ENC= des_enc.o fcrypt_b.o
+! AES_ASM_OBJ= aes_core.o aes_cbc.o
+  BF_ENC= bf_enc.o
+  CAST_ENC= c_enc.o
+! RC4_ENC= rc4_enc.o rc4_skey.o
+  RC5_ENC= rc5_enc.o
+! MD5_ASM_OBJ= 
+! SHA1_ASM_OBJ= 
+  RMD160_ASM_OBJ= 
+  
+  # KRB5 stuff
+--- 90,105 ----
+  PROCESSOR= 
+  
+  # CPUID module collects small commonly used assembler snippets
+! CPUID_OBJ= x86_64cpuid.o
+! BN_ASM= x86_64-gcc.o x86_64-mont.o
+  DES_ENC= des_enc.o fcrypt_b.o
+! AES_ASM_OBJ= aes-x86_64.o
+  BF_ENC= bf_enc.o
+  CAST_ENC= c_enc.o
+! RC4_ENC= rc4-x86_64.o
+  RC5_ENC= rc5_enc.o
+! MD5_ASM_OBJ= md5-x86_64.o
+! SHA1_ASM_OBJ= sha1-x86_64.o sha256-x86_64.o sha512-x86_64.o
+  RMD160_ASM_OBJ= 
+  
+  # KRB5 stuff
+***************
+*** 169,176 ****
+  SHARED_SSL=libssl$(SHLIB_EXT)
+  SHARED_FIPS=
+  SHARED_LIBS=
+! SHARED_LIBS_LINK_EXTS=
+! SHARED_LDFLAGS=
+  
+  GENERAL=        Makefile
+  BASENAME=       openssl
+--- 172,179 ----
+  SHARED_SSL=libssl$(SHLIB_EXT)
+  SHARED_FIPS=
+  SHARED_LIBS=
+! SHARED_LIBS_LINK_EXTS=.so.$(SHLIB_MAJOR) .so
+! SHARED_LDFLAGS=-m64 -shared -static-libgcc
+  
+  GENERAL=        Makefile
+  BASENAME=       openssl
+diff -cr openssl-0.9.8l/Makefile.org openssl-0.9.8l-with_pkcs11_engine/Makefile.org
+*** openssl-0.9.8l/Makefile.org	Tue Mar  3 23:40:29 2009
+--- openssl-0.9.8l-with_pkcs11_engine/Makefile.org	Thu Nov 19 17:16:33 2009
+***************
+*** 26,31 ****
+--- 26,34 ----
+  INSTALL_PREFIX=
+  INSTALLTOP=/usr/local/ssl
+  
++ # You must set this through --pk11-libname configure option.
++ PK11_LIB_LOCATION=
++ 
+  # Do not edit this manually. Use Configure --openssldir=DIR do change this!
+  OPENSSLDIR=/usr/local/ssl
+  
+Only in openssl-0.9.8l-with_pkcs11_engine: Makefile.orig
+diff -cr openssl-0.9.8l/apps/CA.pl openssl-0.9.8l-with_pkcs11_engine/apps/CA.pl
+*** openssl-0.9.8l/apps/CA.pl	Thu Nov  5 17:16:02 2009
+--- openssl-0.9.8l-with_pkcs11_engine/apps/CA.pl	Thu Nov 19 17:28:03 2009
+***************
+*** 1,4 ****
+! #!/usr/bin/perl5
+  #
+  # CA - wrapper around ca to make it easier to use ... basically ca requires
+  #      some setup stuff to be done before you can use it and this makes
+--- 1,4 ----
+! #!/usr/bin/perl
+  #
+  # CA - wrapper around ca to make it easier to use ... basically ca requires
+  #      some setup stuff to be done before you can use it and this makes
+diff -cr openssl-0.9.8l/crypto/opensslconf.h openssl-0.9.8l-with_pkcs11_engine/crypto/opensslconf.h
+*** openssl-0.9.8l/crypto/opensslconf.h	Thu Nov  5 17:15:53 2009
+--- openssl-0.9.8l-with_pkcs11_engine/crypto/opensslconf.h	Thu Nov 19 17:28:00 2009
+***************
+*** 38,43 ****
+--- 38,46 ----
+  
+  #endif /* OPENSSL_DOING_MAKEDEPEND */
+  
++ #ifndef OPENSSL_THREADS
++ # define OPENSSL_THREADS
++ #endif
+  #ifndef OPENSSL_NO_DYNAMIC_ENGINE
+  # define OPENSSL_NO_DYNAMIC_ENGINE
+  #endif
+***************
+*** 79,84 ****
+--- 82,89 ----
+  # endif
+  #endif
+  
++ #define OPENSSL_CPUID_OBJ
++ 
+  /* crypto/opensslconf.h.in */
+  
+  #ifdef OPENSSL_DOING_MAKEDEPEND
+***************
+*** 140,146 ****
+   * This enables code handling data aligned at natural CPU word
+   * boundary. See crypto/rc4/rc4_enc.c for further details.
+   */
+! #undef RC4_CHUNK
+  #endif
+  #endif
+  
+--- 145,151 ----
+   * This enables code handling data aligned at natural CPU word
+   * boundary. See crypto/rc4/rc4_enc.c for further details.
+   */
+! #define RC4_CHUNK unsigned long
+  #endif
+  #endif
+  
+***************
+*** 148,154 ****
+  /* If this is set to 'unsigned int' on a DEC Alpha, this gives about a
+   * %20 speed up (longs are 8 bytes, int's are 4). */
+  #ifndef DES_LONG
+! #define DES_LONG unsigned long
+  #endif
+  #endif
+  
+--- 153,159 ----
+  /* If this is set to 'unsigned int' on a DEC Alpha, this gives about a
+   * %20 speed up (longs are 8 bytes, int's are 4). */
+  #ifndef DES_LONG
+! #define DES_LONG unsigned int
+  #endif
+  #endif
+  
+***************
+*** 162,170 ****
+  /* The prime number generation stuff may not work when
+   * EIGHT_BIT but I don't care since I've only used this mode
+   * for debuging the bignum libraries */
+! #undef SIXTY_FOUR_BIT_LONG
+  #undef SIXTY_FOUR_BIT
+! #define THIRTY_TWO_BIT
+  #undef SIXTEEN_BIT
+  #undef EIGHT_BIT
+  #endif
+--- 167,175 ----
+  /* The prime number generation stuff may not work when
+   * EIGHT_BIT but I don't care since I've only used this mode
+   * for debuging the bignum libraries */
+! #define SIXTY_FOUR_BIT_LONG
+  #undef SIXTY_FOUR_BIT
+! #undef THIRTY_TWO_BIT
+  #undef SIXTEEN_BIT
+  #undef EIGHT_BIT
+  #endif
+***************
+*** 178,184 ****
+  
+  #if defined(HEADER_BF_LOCL_H) && !defined(CONFIG_HEADER_BF_LOCL_H)
+  #define CONFIG_HEADER_BF_LOCL_H
+! #undef BF_PTR
+  #endif /* HEADER_BF_LOCL_H */
+  
+  #if defined(HEADER_DES_LOCL_H) && !defined(CONFIG_HEADER_DES_LOCL_H)
+--- 183,189 ----
+  
+  #if defined(HEADER_BF_LOCL_H) && !defined(CONFIG_HEADER_BF_LOCL_H)
+  #define CONFIG_HEADER_BF_LOCL_H
+! #define BF_PTR2
+  #endif /* HEADER_BF_LOCL_H */
+  
+  #if defined(HEADER_DES_LOCL_H) && !defined(CONFIG_HEADER_DES_LOCL_H)
+***************
+*** 208,214 ****
+  /* Unroll the inner loop, this sometimes helps, sometimes hinders.
+   * Very mucy CPU dependant */
+  #ifndef DES_UNROLL
+! #undef DES_UNROLL
+  #endif
+  
+  /* These default values were supplied by
+--- 213,219 ----
+  /* Unroll the inner loop, this sometimes helps, sometimes hinders.
+   * Very mucy CPU dependant */
+  #ifndef DES_UNROLL
+! #define DES_UNROLL
+  #endif
+  
+  /* These default values were supplied by
+diff -cr openssl-0.9.8l/crypto/engine/Makefile openssl-0.9.8l-with_pkcs11_engine/crypto/engine/Makefile
+*** openssl-0.9.8l/crypto/engine/Makefile	Thu Nov  5 14:53:23 2009
+--- openssl-0.9.8l-with_pkcs11_engine/crypto/engine/Makefile	Thu Nov 19 17:31:35 2009
+***************
+*** 21,32 ****
+  	eng_table.c eng_pkey.c eng_fat.c eng_all.c \
+  	tb_rsa.c tb_dsa.c tb_ecdsa.c tb_dh.c tb_ecdh.c tb_rand.c tb_store.c \
+  	tb_cipher.c tb_digest.c \
+! 	eng_openssl.c eng_cnf.c eng_dyn.c eng_cryptodev.c eng_padlock.c
+  LIBOBJ= eng_err.o eng_lib.o eng_list.o eng_init.o eng_ctrl.o \
+  	eng_table.o eng_pkey.o eng_fat.o eng_all.o \
+  	tb_rsa.o tb_dsa.o tb_ecdsa.o tb_dh.o tb_ecdh.o tb_rand.o tb_store.o \
+  	tb_cipher.o tb_digest.o \
+! 	eng_openssl.o eng_cnf.o eng_dyn.o eng_cryptodev.o eng_padlock.o
+  
+  SRC= $(LIBSRC)
+  
+--- 21,34 ----
+  	eng_table.c eng_pkey.c eng_fat.c eng_all.c \
+  	tb_rsa.c tb_dsa.c tb_ecdsa.c tb_dh.c tb_ecdh.c tb_rand.c tb_store.c \
+  	tb_cipher.c tb_digest.c \
+! 	eng_openssl.c eng_cnf.c eng_dyn.c eng_cryptodev.c eng_padlock.c \
+! 	hw_pk11.c hw_pk11_pub.c hw_pk11_uri.c
+  LIBOBJ= eng_err.o eng_lib.o eng_list.o eng_init.o eng_ctrl.o \
+  	eng_table.o eng_pkey.o eng_fat.o eng_all.o \
+  	tb_rsa.o tb_dsa.o tb_ecdsa.o tb_dh.o tb_ecdh.o tb_rand.o tb_store.o \
+  	tb_cipher.o tb_digest.o \
+! 	eng_openssl.o eng_cnf.o eng_dyn.o eng_cryptodev.o eng_padlock.o \
+! 	hw_pk11.o hw_pk11_pub.o hw_pk11_uri.o
+  
+  SRC= $(LIBSRC)
+  
+***************
+*** 286,291 ****
+--- 288,348 ----
+  eng_table.o: ../../include/openssl/symhacks.h ../../include/openssl/x509.h
+  eng_table.o: ../../include/openssl/x509_vfy.h ../cryptlib.h eng_int.h
+  eng_table.o: eng_table.c
++ hw_pk11.o: ../../include/openssl/e_os2.h ../../include/openssl/opensslconf.h
++ hw_pk11.o: ../../include/openssl/engine.h ../../include/openssl/ossl_typ.h
++ hw_pk11.o: ../../include/openssl/bn.h ../../include/openssl/rsa.h
++ hw_pk11.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h
++ hw_pk11.o: ../../include/openssl/crypto.h ../../include/openssl/stack.h
++ hw_pk11.o: ../../include/openssl/safestack.h ../../include/openssl/opensslv.h
++ hw_pk11.o: ../../include/openssl/symhacks.h ../../include/openssl/dsa.h
++ hw_pk11.o: ../../include/openssl/dh.h ../../include/openssl/rand.h
++ hw_pk11.o: ../../include/openssl/ui.h ../../include/openssl/err.h
++ hw_pk11.o: ../../include/openssl/lhash.h ../../include/openssl/dso.h
++ hw_pk11.o: ../../include/openssl/pem.h ../../include/openssl/evp.h
++ hw_pk11.o: ../../include/openssl/md2.h ../../include/openssl/md4.h
++ hw_pk11.o: ../../include/openssl/md5.h ../../include/openssl/sha.h
++ hw_pk11.o: ../../include/openssl/ripemd.h ../../include/openssl/des.h
++ hw_pk11.o: ../../include/openssl/des_old.h ../../include/openssl/ui_compat.h
++ hw_pk11.o: ../../include/openssl/rc4.h ../../include/openssl/rc2.h
++ hw_pk11.o: ../../crypto/rc5/rc5.h ../../include/openssl/blowfish.h
++ hw_pk11.o: ../../include/openssl/cast.h ../../include/openssl/idea.h
++ hw_pk11.o: ../../crypto/mdc2/mdc2.h ../../include/openssl/aes.h
++ hw_pk11.o: ../../include/openssl/objects.h ../../include/openssl/obj_mac.h
++ hw_pk11.o: ../../include/openssl/x509.h ../../include/openssl/buffer.h
++ hw_pk11.o: ../../include/openssl/x509_vfy.h ../../include/openssl/pkcs7.h
++ hw_pk11.o: ../../include/openssl/pem2.h ../cryptlib.h
++ hw_pk11.o: ../../e_os.h hw_pk11_err.c hw_pk11_err.h hw_pk11.c
++ hw_pk11_pub.o: ../../include/openssl/e_os2.h ../../include/openssl/opensslconf.h
++ hw_pk11_pub.o: ../../include/openssl/engine.h ../../include/openssl/ossl_typ.h
++ hw_pk11_pub.o: ../../include/openssl/bn.h ../../include/openssl/rsa.h
++ hw_pk11_pub.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h
++ hw_pk11_pub.o: ../../include/openssl/crypto.h ../../include/openssl/stack.h
++ hw_pk11_pub.o: ../../include/openssl/safestack.h ../../include/openssl/opensslv.h
++ hw_pk11_pub.o: ../../include/openssl/symhacks.h ../../include/openssl/dsa.h
++ hw_pk11_pub.o: ../../include/openssl/dh.h ../../include/openssl/rand.h
++ hw_pk11_pub.o: ../../include/openssl/ui.h ../../include/openssl/err.h
++ hw_pk11_pub.o: ../../include/openssl/lhash.h ../../include/openssl/dso.h
++ hw_pk11_pub.o: ../../include/openssl/pem.h ../../include/openssl/evp.h
++ hw_pk11_pub.o: ../../include/openssl/md2.h ../../include/openssl/md4.h
++ hw_pk11_pub.o: ../../include/openssl/md5.h ../../include/openssl/sha.h
++ hw_pk11_pub.o: ../../include/openssl/ripemd.h ../../include/openssl/des.h
++ hw_pk11_pub.o: ../../include/openssl/des_old.h ../../include/openssl/ui_compat.h
++ hw_pk11_pub.o: ../../include/openssl/rc4.h ../../include/openssl/rc2.h
++ hw_pk11_pub.o: ../../crypto/rc5/rc5.h ../../include/openssl/blowfish.h
++ hw_pk11_pub.o: ../../include/openssl/cast.h ../../include/openssl/idea.h
++ hw_pk11_pub.o: ../../crypto/mdc2/mdc2.h ../../include/openssl/aes.h
++ hw_pk11_pub.o: ../../include/openssl/objects.h ../../include/openssl/obj_mac.h
++ hw_pk11_pub.o: ../../include/openssl/x509.h ../../include/openssl/buffer.h
++ hw_pk11_pub.o: ../../include/openssl/x509_vfy.h ../../include/openssl/pkcs7.h
++ hw_pk11_pub.o: ../../include/openssl/pem2.h ../cryptlib.h
++ hw_pk11_pub.o: ../../e_os.h hw_pk11_err.c hw_pk11_err.h hw_pk11_pub.c
++ hw_pk11_uri.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h
++ hw_pk11_uri.o: ../../include/openssl/opensslconf.h
++ hw_pk11_uri.o: ../../include/openssl/opensslv.h
++ hw_pk11_uri.o: ../../include/openssl/ossl_typ.h
++ hw_pk11_uri.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h
++ hw_pk11_uri.o: ../../include/openssl/symhacks.h hw_pk11.h hw_pk11_err.h
++ hw_pk11_uri.o: hw_pk11_uri.c hw_pk11_uri.h pkcs11f.h pkcs11t.h
+  tb_cipher.o: ../../e_os.h ../../include/openssl/asn1.h
+  tb_cipher.o: ../../include/openssl/bio.h ../../include/openssl/buffer.h
+  tb_cipher.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h
+diff -cr openssl-0.9.8l/crypto/engine/cryptoki.h openssl-0.9.8l-with_pkcs11_engine/crypto/engine/cryptoki.h
+*** openssl-0.9.8l/crypto/engine/cryptoki.h	Thu Nov 19 17:33:41 2009
+--- openssl-0.9.8l-with_pkcs11_engine/crypto/engine/cryptoki.h	Thu Nov 19 17:16:33 2009
+***************
+*** 0 ****
+--- 1,103 ----
++ /*
++  * CDDL HEADER START
++  *
++  * The contents of this file are subject to the terms of the
++  * Common Development and Distribution License, Version 1.0 only
++  * (the "License").  You may not use this file except in compliance
++  * with the License.
++  *
++  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
++  * or http://www.opensolaris.org/os/licensing.
++  * See the License for the specific language governing permissions
++  * and limitations under the License.
++  *
++  * When distributing Covered Code, include this CDDL HEADER in each
++  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
++  * If applicable, add the following below this CDDL HEADER, with the
++  * fields enclosed by brackets "[]" replaced with your own identifying
++  * information: Portions Copyright [yyyy] [name of copyright owner]
++  *
++  * CDDL HEADER END
++  */
++ /*
++  * Copyright 2003 Sun Microsystems, Inc.   All rights reserved.
++  * Use is subject to license terms.
++  */
++ 
++ #ifndef	_CRYPTOKI_H
++ #define	_CRYPTOKI_H
++ 
++ #pragma ident	"@(#)cryptoki.h	1.2	05/06/08 SMI"
++ 
++ #ifdef	__cplusplus
++ extern "C" {
++ #endif
++ 
++ #ifndef	CK_PTR
++ #define	CK_PTR *
++ #endif
++ 
++ #ifndef CK_DEFINE_FUNCTION
++ #define	CK_DEFINE_FUNCTION(returnType, name) returnType name
++ #endif
++ 
++ #ifndef CK_DECLARE_FUNCTION
++ #define	CK_DECLARE_FUNCTION(returnType, name) returnType name
++ #endif
++ 
++ #ifndef CK_DECLARE_FUNCTION_POINTER
++ #define	CK_DECLARE_FUNCTION_POINTER(returnType, name) returnType (* name)
++ #endif
++ 
++ #ifndef CK_CALLBACK_FUNCTION
++ #define	CK_CALLBACK_FUNCTION(returnType, name) returnType (* name)
++ #endif
++ 
++ #ifndef NULL_PTR
++ #include <unistd.h>	/* For NULL */
++ #define	NULL_PTR NULL
++ #endif
++ 
++ /*
++  * pkcs11t.h defines TRUE and FALSE in a way that upsets lint
++  */
++ #ifndef	CK_DISABLE_TRUE_FALSE
++ #define	CK_DISABLE_TRUE_FALSE
++ #ifndef	TRUE
++ #define	TRUE	1
++ #endif /* TRUE */
++ #ifndef	FALSE
++ #define	FALSE	0
++ #endif /* FALSE */
++ #endif /* CK_DISABLE_TRUE_FALSE */
++ 
++ #undef CK_PKCS11_FUNCTION_INFO
++ 
++ #include "pkcs11.h"
++ 
++ /* Solaris specific functions */
++ 
++ #include <stdlib.h>
++ 
++ /*
++  * SUNW_C_GetMechSession will initialize the framework and do all
++  * the necessary PKCS#11 calls to create a session capable of
++  * providing operations on the requested mechanism
++  */
++ CK_RV SUNW_C_GetMechSession(CK_MECHANISM_TYPE mech,
++     CK_SESSION_HANDLE_PTR hSession);
++ 
++ /*
++  * SUNW_C_KeyToObject will create a secret key object for the given
++  * mechanism from the rawkey data.
++  */
++ CK_RV SUNW_C_KeyToObject(CK_SESSION_HANDLE hSession,
++     CK_MECHANISM_TYPE mech, const void *rawkey, size_t rawkey_len,
++     CK_OBJECT_HANDLE_PTR obj);
++ 
++ 
++ #ifdef	__cplusplus
++ }
++ #endif
++ 
++ #endif	/* _CRYPTOKI_H */
+diff -cr openssl-0.9.8l/crypto/engine/eng_all.c openssl-0.9.8l-with_pkcs11_engine/crypto/engine/eng_all.c
+*** openssl-0.9.8l/crypto/engine/eng_all.c	Wed Jun  4 20:01:39 2008
+--- openssl-0.9.8l-with_pkcs11_engine/crypto/engine/eng_all.c	Thu Nov 19 17:16:33 2009
+***************
+*** 110,116 ****
+--- 110,119 ----
+  #if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG)
+  	ENGINE_load_capi();
+  #endif
++ #ifndef OPENSSL_NO_HW_PKCS11
++ 	ENGINE_load_pk11();
+  #endif
++ #endif
+  	}
+  
+  #if defined(__OpenBSD__) || defined(__FreeBSD__)
+diff -cr openssl-0.9.8l/crypto/engine/engine.h openssl-0.9.8l-with_pkcs11_engine/crypto/engine/engine.h
+*** openssl-0.9.8l/crypto/engine/engine.h	Wed Jun  4 20:01:40 2008
+--- openssl-0.9.8l-with_pkcs11_engine/crypto/engine/engine.h	Thu Nov 19 17:16:33 2009
+***************
+*** 337,342 ****
+--- 337,343 ----
+  void ENGINE_load_ubsec(void);
+  #endif
+  void ENGINE_load_cryptodev(void);
++ void ENGINE_load_pk11(void);
+  void ENGINE_load_padlock(void);
+  void ENGINE_load_builtin_engines(void);
+  #ifndef OPENSSL_NO_CAPIENG
+diff -cr openssl-0.9.8l/crypto/engine/hw_pk11.c openssl-0.9.8l-with_pkcs11_engine/crypto/engine/hw_pk11.c
+*** openssl-0.9.8l/crypto/engine/hw_pk11.c	Thu Nov 19 17:33:41 2009
+--- openssl-0.9.8l-with_pkcs11_engine/crypto/engine/hw_pk11.c	Thu Nov 19 18:00:37 2009
+***************
+*** 0 ****
+--- 1,3813 ----
++ /*
++  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
++  * Use is subject to license terms.
++  */
++ 
++ /* crypto/engine/hw_pk11.c */
++ /*
++  * This product includes software developed by the OpenSSL Project for
++  * use in the OpenSSL Toolkit (http://www.openssl.org/).
++  *
++  * This project also referenced hw_pkcs11-0.9.7b.patch written by
++  * Afchine Madjlessi.
++  */
++ /*
++  * ====================================================================
++  * Copyright (c) 2000-2001 The OpenSSL Project.  All rights reserved.
++  *
++  * Redistribution and use in source and binary forms, with or without
++  * modification, are permitted provided that the following conditions
++  * are met:
++  *
++  * 1. Redistributions of source code must retain the above copyright
++  *    notice, this list of conditions and the following disclaimer.
++  *
++  * 2. Redistributions in binary form must reproduce the above copyright
++  *    notice, this list of conditions and the following disclaimer in
++  *    the documentation and/or other materials provided with the
++  *    distribution.
++  *
++  * 3. All advertising materials mentioning features or use of this
++  *    software must display the following acknowledgment:
++  *    "This product includes software developed by the OpenSSL Project
++  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
++  *
++  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
++  *    endorse or promote products derived from this software without
++  *    prior written permission. For written permission, please contact
++  *    licensing at OpenSSL.org.
++  *
++  * 5. Products derived from this software may not be called "OpenSSL"
++  *    nor may "OpenSSL" appear in their names without prior written
++  *    permission of the OpenSSL Project.
++  *
++  * 6. Redistributions of any form whatsoever must retain the following
++  *    acknowledgment:
++  *    "This product includes software developed by the OpenSSL Project
++  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
++  *
++  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
++  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
++  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
++  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
++  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
++  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
++  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
++  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
++  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
++  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
++  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
++  * OF THE POSSIBILITY OF SUCH DAMAGE.
++  * ====================================================================
++  *
++  * This product includes cryptographic software written by Eric Young
++  * (eay at cryptsoft.com).  This product includes software written by Tim
++  * Hudson (tjh at cryptsoft.com).
++  *
++  */
++ 
++ #include <stdio.h>
++ #include <stdlib.h>
++ #include <string.h>
++ #include <sys/types.h>
++ #include <unistd.h>
++ #include <strings.h>
++ 
++ #include <openssl/e_os2.h>
++ #include <openssl/crypto.h>
++ #include <openssl/engine.h>
++ #include <openssl/dso.h>
++ #include <openssl/err.h>
++ #include <openssl/bn.h>
++ #include <openssl/md5.h>
++ #include <openssl/pem.h>
++ #ifndef OPENSSL_NO_RSA
++ #include <openssl/rsa.h>
++ #endif
++ #ifndef OPENSSL_NO_DSA
++ #include <openssl/dsa.h>
++ #endif
++ #ifndef OPENSSL_NO_DH
++ #include <openssl/dh.h>
++ #endif
++ #include <openssl/rand.h>
++ #include <openssl/objects.h>
++ #include <openssl/x509.h>
++ #include <openssl/aes.h>
++ #include <cryptlib.h>
++ #include <dlfcn.h>
++ #include <pthread.h>
++ 
++ #ifndef OPENSSL_NO_HW
++ #ifndef OPENSSL_NO_HW_PK11
++ 
++ /* label for debug messages printed on stderr */
++ #define	PK11_DBG	"PKCS#11 ENGINE DEBUG"
++ /* prints a lot of debug messages on stderr about slot selection process */
++ #undef	DEBUG_SLOT_SELECTION
++ /*
++  * Solaris specific code. See comment at check_hw_mechanisms() for more
++  * information.
++  */
++ #if defined(__SVR4) && defined(__sun)
++ #define	SOLARIS_HW_SLOT_SELECTION
++ #endif
++ 
++ /*
++  * AES counter mode is not supported in the OpenSSL EVP API yet and neither
++  * there are official OIDs for mechanisms based on this mode. With our changes,
++  * an application can define its own EVP calls for AES counter mode and then
++  * it can make use of hardware acceleration through this engine. However, it's
++  * better if we keep AES CTR support code under ifdef's.
++  */
++ #define	SOLARIS_AES_CTR
++ 
++ #include "cryptoki.h"
++ #include "pkcs11.h"
++ #include "hw_pk11.h"
++ #include "hw_pk11_uri.h"
++ #include "hw_pk11_err.c"
++ 
++ #ifdef	SOLARIS_AES_CTR
++ /*
++  * NIDs for AES counter mode that will be defined during the engine
++  * initialization.
++  */
++ int NID_aes_128_ctr = NID_undef;
++ int NID_aes_192_ctr = NID_undef;
++ int NID_aes_256_ctr = NID_undef;
++ #endif	/* SOLARIS_AES_CTR */
++ 
++ /*
++  * We use this lock to prevent multiple C_Login()s, guard getpassphrase(),
++  * uri_struct manipulation, and static token info. All of that is used by the
++  * RSA keys by reference feature.
++  */
++ pthread_mutex_t *uri_lock;
++ 
++ #ifdef	SOLARIS_HW_SLOT_SELECTION
++ /*
++  * Tables for symmetric ciphers and digest mechs found in the pkcs11_kernel
++  * library. See comment at check_hw_mechanisms() for more information.
++  */
++ int *hw_cnids;
++ int *hw_dnids;
++ #endif	/* SOLARIS_HW_SLOT_SELECTION */
++ 
++ /* PKCS#11 session caches and their locks for all operation types */
++ static PK11_CACHE session_cache[OP_MAX];
++ 
++ /*
++  * We cache the flags so that we do not have to run C_GetTokenInfo() again when
++  * logging into the token.
++  */
++ CK_FLAGS pubkey_token_flags;
++ 
++ /*
++  * As stated in v2.20, 11.7 Object Management Function, in section for
++  * C_FindObjectsInit(), at most one search operation may be active at a given
++  * time in a given session. Therefore, C_Find{,Init,Final}Objects() should be
++  * grouped together to form one atomic search operation. This is already
++  * ensured by the property of unique PKCS#11 session handle used for each
++  * PK11_SESSION object.
++  *
++  * This is however not the biggest concern - maintaining consistency of the
++  * underlying object store is more important. The same section of the spec also
++  * says that one thread can be in the middle of a search operation while another
++  * thread destroys the object matching the search template which would result in
++  * invalid handle returned from the search operation.
++  *
++  * Hence, the following locks are used for both protection of the object stores.
++  * They are also used for active list protection.
++  */
++ pthread_mutex_t *find_lock[OP_MAX] = { NULL };
++ 
++ /*
++  * lists of asymmetric key handles which are active (referenced by at least one
++  * PK11_SESSION structure, either held by a thread or present in free_session
++  * list) for given algorithm type
++  */
++ PK11_active *active_list[OP_MAX] = { NULL };
++ 
++ /*
++  * Create all secret key objects in a global session so that they are available
++  * to use for other sessions. These other sessions may be opened or closed
++  * without losing the secret key objects.
++  */
++ static CK_SESSION_HANDLE	global_session = CK_INVALID_HANDLE;
++ 
++ /* ENGINE level stuff */
++ static int pk11_init(ENGINE *e);
++ static int pk11_library_init(ENGINE *e);
++ static int pk11_finish(ENGINE *e);
++ static int pk11_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)());
++ static int pk11_destroy(ENGINE *e);
++ 
++ /* RAND stuff */
++ static void pk11_rand_seed(const void *buf, int num);
++ static void pk11_rand_add(const void *buf, int num, double add_entropy);
++ static void pk11_rand_cleanup(void);
++ static int pk11_rand_bytes(unsigned char *buf, int num);
++ static int pk11_rand_status(void);
++ 
++ /* These functions are also used in other files */
++ PK11_SESSION *pk11_get_session(PK11_OPTYPE optype);
++ void pk11_return_session(PK11_SESSION *sp, PK11_OPTYPE optype);
++ 
++ /* active list manipulation functions used in this file */
++ extern int pk11_active_delete(CK_OBJECT_HANDLE h, PK11_OPTYPE type);
++ extern void pk11_free_active_list(PK11_OPTYPE type);
++ 
++ #ifndef OPENSSL_NO_RSA
++ int pk11_destroy_rsa_key_objects(PK11_SESSION *session);
++ int pk11_destroy_rsa_object_pub(PK11_SESSION *sp, CK_BBOOL uselock);
++ int pk11_destroy_rsa_object_priv(PK11_SESSION *sp, CK_BBOOL uselock);
++ #endif
++ #ifndef OPENSSL_NO_DSA
++ int pk11_destroy_dsa_key_objects(PK11_SESSION *session);
++ int pk11_destroy_dsa_object_pub(PK11_SESSION *sp, CK_BBOOL uselock);
++ int pk11_destroy_dsa_object_priv(PK11_SESSION *sp, CK_BBOOL uselock);
++ #endif
++ #ifndef OPENSSL_NO_DH
++ int pk11_destroy_dh_key_objects(PK11_SESSION *session);
++ int pk11_destroy_dh_object(PK11_SESSION *session, CK_BBOOL uselock);
++ #endif
++ 
++ /* Local helper functions */
++ static int pk11_free_all_sessions(void);
++ static int pk11_free_session_list(PK11_OPTYPE optype);
++ static int pk11_setup_session(PK11_SESSION *sp, PK11_OPTYPE optype);
++ static int pk11_destroy_cipher_key_objects(PK11_SESSION *session);
++ static int pk11_destroy_object(CK_SESSION_HANDLE handle, CK_OBJECT_HANDLE oh,
++     CK_BBOOL persistent);
++ static const char *get_PK11_LIBNAME(void);
++ static void free_PK11_LIBNAME(void);
++ static long set_PK11_LIBNAME(const char *name);
++ 
++ /* Symmetric cipher and digest support functions */
++ static int cipher_nid_to_pk11(int nid);
++ #ifdef	SOLARIS_AES_CTR
++ static int pk11_add_NID(char *sn, char *ln);
++ static int pk11_add_aes_ctr_NIDs(void);
++ #endif	/* SOLARIS_AES_CTR */
++ static int pk11_usable_ciphers(const int **nids);
++ static int pk11_usable_digests(const int **nids);
++ static int pk11_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
++ 	const unsigned char *iv, int enc);
++ static int pk11_cipher_final(PK11_SESSION *sp);
++ static int pk11_cipher_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
++ 	const unsigned char *in, unsigned int inl);
++ static int pk11_cipher_cleanup(EVP_CIPHER_CTX *ctx);
++ static int pk11_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
++ 	const int **nids, int nid);
++ static int pk11_engine_digests(ENGINE *e, const EVP_MD **digest,
++ 	const int **nids, int nid);
++ static CK_OBJECT_HANDLE pk11_get_cipher_key(EVP_CIPHER_CTX *ctx,
++ 	const unsigned char *key, CK_KEY_TYPE key_type, PK11_SESSION *sp);
++ static int check_new_cipher_key(PK11_SESSION *sp, const unsigned char *key,
++ 	int key_len);
++ static int md_nid_to_pk11(int nid);
++ static int pk11_digest_init(EVP_MD_CTX *ctx);
++ static int pk11_digest_update(EVP_MD_CTX *ctx, const void *data,
++ 	size_t count);
++ static int pk11_digest_final(EVP_MD_CTX *ctx, unsigned char *md);
++ static int pk11_digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from);
++ static int pk11_digest_cleanup(EVP_MD_CTX *ctx);
++ 
++ static int pk11_choose_slots(int *any_slot_found);
++ static void pk11_find_symmetric_ciphers(CK_FUNCTION_LIST_PTR pflist,
++     CK_SLOT_ID current_slot, int *current_slot_n_cipher,
++     int *local_cipher_nids);
++ static void pk11_find_digests(CK_FUNCTION_LIST_PTR pflist,
++     CK_SLOT_ID current_slot, int *current_slot_n_digest,
++     int *local_digest_nids);
++ static void pk11_get_symmetric_cipher(CK_FUNCTION_LIST_PTR, int slot_id,
++     CK_MECHANISM_TYPE mech, int *current_slot_n_cipher, int *local_cipher_nids,
++     int id);
++ static void pk11_get_digest(CK_FUNCTION_LIST_PTR pflist, int slot_id,
++     CK_MECHANISM_TYPE mech, int *current_slot_n_digest, int *local_digest_nids,
++     int id);
++ 
++ static int pk11_init_all_locks(void);
++ static void pk11_free_all_locks(void);
++ 
++ #ifdef	SOLARIS_HW_SLOT_SELECTION
++ static int check_hw_mechanisms(void);
++ static int nid_in_table(int nid, int *nid_table);
++ #endif	/* SOLARIS_HW_SLOT_SELECTION */
++ 
++ /* Index for the supported ciphers */
++ enum pk11_cipher_id {
++ 	PK11_DES_CBC,
++ 	PK11_DES3_CBC,
++ 	PK11_DES_ECB,
++ 	PK11_DES3_ECB,
++ 	PK11_RC4,
++ 	PK11_AES_128_CBC,
++ 	PK11_AES_192_CBC,
++ 	PK11_AES_256_CBC,
++ 	PK11_AES_128_ECB,
++ 	PK11_AES_192_ECB,
++ 	PK11_AES_256_ECB,
++ 	PK11_BLOWFISH_CBC,
++ #ifdef	SOLARIS_AES_CTR
++ 	PK11_AES_128_CTR,
++ 	PK11_AES_192_CTR,
++ 	PK11_AES_256_CTR,
++ #endif	/* SOLARIS_AES_CTR */
++ 	PK11_CIPHER_MAX
++ };
++ 
++ /* Index for the supported digests */
++ enum pk11_digest_id {
++ 	PK11_MD5,
++ 	PK11_SHA1,
++ 	PK11_SHA224,
++ 	PK11_SHA256,
++ 	PK11_SHA384,
++ 	PK11_SHA512,
++ 	PK11_DIGEST_MAX
++ };
++ 
++ #define	TRY_OBJ_DESTROY(sp, obj_hdl, retval, uselock, alg_type)	\
++ 	{								\
++ 	if (uselock)							\
++ 		LOCK_OBJSTORE(alg_type);				\
++ 	if (pk11_active_delete(obj_hdl, alg_type) == 1)			\
++ 		{							\
++ 		retval = pk11_destroy_object(sp->session, obj_hdl,	\
++ 		    sp->persistent);					\
++ 		}							\
++ 	if (uselock)							\
++ 		UNLOCK_OBJSTORE(alg_type);				\
++ 	}
++ 
++ static int cipher_nids[PK11_CIPHER_MAX];
++ static int digest_nids[PK11_DIGEST_MAX];
++ static int cipher_count		= 0;
++ static int digest_count		= 0;
++ static CK_BBOOL pk11_have_rsa	= CK_FALSE;
++ static CK_BBOOL pk11_have_dsa	= CK_FALSE;
++ static CK_BBOOL pk11_have_dh	= CK_FALSE;
++ static CK_BBOOL pk11_have_random = CK_FALSE;
++ 
++ typedef struct PK11_CIPHER_st
++ 	{
++ 	enum pk11_cipher_id	id;
++ 	int			nid;
++ 	int			iv_len;
++ 	int			min_key_len;
++ 	int			max_key_len;
++ 	CK_KEY_TYPE		key_type;
++ 	CK_MECHANISM_TYPE	mech_type;
++ 	} PK11_CIPHER;
++ 
++ static PK11_CIPHER ciphers[] =
++ 	{
++ 	{ PK11_DES_CBC,		NID_des_cbc,		8,	 8,   8,
++ 		CKK_DES,	CKM_DES_CBC, },
++ 	{ PK11_DES3_CBC,	NID_des_ede3_cbc,	8,	24,  24,
++ 		CKK_DES3,	CKM_DES3_CBC, },
++ 	{ PK11_DES_ECB,		NID_des_ecb,		0,	 8,   8,
++ 		CKK_DES,	CKM_DES_ECB, },
++ 	{ PK11_DES3_ECB,	NID_des_ede3_ecb,	0,	24,  24,
++ 		CKK_DES3,	CKM_DES3_ECB, },
++ 	{ PK11_RC4,		NID_rc4,		0,	16, 256,
++ 		CKK_RC4,	CKM_RC4, },
++ 	{ PK11_AES_128_CBC,	NID_aes_128_cbc,	16,	16,  16,
++ 		CKK_AES,	CKM_AES_CBC, },
++ 	{ PK11_AES_192_CBC,	NID_aes_192_cbc,	16,	24,  24,
++ 		CKK_AES,	CKM_AES_CBC, },
++ 	{ PK11_AES_256_CBC,	NID_aes_256_cbc,	16,	32,  32,
++ 		CKK_AES,	CKM_AES_CBC, },
++ 	{ PK11_AES_128_ECB,	NID_aes_128_ecb,	0,	16,  16,
++ 		CKK_AES,	CKM_AES_ECB, },
++ 	{ PK11_AES_192_ECB,	NID_aes_192_ecb,	0,	24,  24,
++ 		CKK_AES,	CKM_AES_ECB, },
++ 	{ PK11_AES_256_ECB,	NID_aes_256_ecb,	0,	32,  32,
++ 		CKK_AES,	CKM_AES_ECB, },
++ 	{ PK11_BLOWFISH_CBC,	NID_bf_cbc,		8,	16,  16,
++ 		CKK_BLOWFISH,	CKM_BLOWFISH_CBC, },
++ #ifdef	SOLARIS_AES_CTR
++ 	/* we don't know the correct NIDs until the engine is initialized */
++ 	{ PK11_AES_128_CTR,	NID_undef,		16,	16,  16,
++ 		CKK_AES,	CKM_AES_CTR, },
++ 	{ PK11_AES_192_CTR,	NID_undef,		16,	24,  24,
++ 		CKK_AES,	CKM_AES_CTR, },
++ 	{ PK11_AES_256_CTR,	NID_undef,		16,	32,  32,
++ 		CKK_AES,	CKM_AES_CTR, },
++ #endif	/* SOLARIS_AES_CTR */
++ 	};
++ 
++ typedef struct PK11_DIGEST_st
++ 	{
++ 	enum pk11_digest_id	id;
++ 	int			nid;
++ 	CK_MECHANISM_TYPE	mech_type;
++ 	} PK11_DIGEST;
++ 
++ static PK11_DIGEST digests[] =
++ 	{
++ 	{PK11_MD5,	NID_md5,	CKM_MD5, },
++ 	{PK11_SHA1,	NID_sha1,	CKM_SHA_1, },
++ 	{PK11_SHA224,	NID_sha224,	CKM_SHA224, },
++ 	{PK11_SHA256,	NID_sha256,	CKM_SHA256, },
++ 	{PK11_SHA384,	NID_sha384,	CKM_SHA384, },
++ 	{PK11_SHA512,	NID_sha512,	CKM_SHA512, },
++ 	{0,		NID_undef,	0xFFFF, },
++ 	};
++ 
++ /*
++  * Structure to be used for the cipher_data/md_data in
++  * EVP_CIPHER_CTX/EVP_MD_CTX structures in order to use the same pk11
++  * session in multiple cipher_update calls
++  */
++ typedef struct PK11_CIPHER_STATE_st
++ 	{
++ 	PK11_SESSION	*sp;
++ 	} PK11_CIPHER_STATE;
++ 
++ 
++ /*
++  * libcrypto EVP stuff - this is how we get wired to EVP so the engine gets
++  * called when libcrypto requests a cipher NID.
++  *
++  * Note how the PK11_CIPHER_STATE is used here.
++  */
++ 
++ /* DES CBC EVP */
++ static const EVP_CIPHER pk11_des_cbc =
++ 	{
++ 	NID_des_cbc,
++ 	8, 8, 8,
++ 	EVP_CIPH_CBC_MODE,
++ 	pk11_cipher_init,
++ 	pk11_cipher_do_cipher,
++ 	pk11_cipher_cleanup,
++ 	sizeof (PK11_CIPHER_STATE),
++ 	EVP_CIPHER_set_asn1_iv,
++ 	EVP_CIPHER_get_asn1_iv,
++ 	NULL
++ 	};
++ 
++ /* 3DES CBC EVP */
++ static const EVP_CIPHER pk11_3des_cbc =
++ 	{
++ 	NID_des_ede3_cbc,
++ 	8, 24, 8,
++ 	EVP_CIPH_CBC_MODE,
++ 	pk11_cipher_init,
++ 	pk11_cipher_do_cipher,
++ 	pk11_cipher_cleanup,
++ 	sizeof (PK11_CIPHER_STATE),
++ 	EVP_CIPHER_set_asn1_iv,
++ 	EVP_CIPHER_get_asn1_iv,
++ 	NULL
++ 	};
++ 
++ /*
++  * ECB modes don't use an Initial Vector so that's why set_asn1_parameters and
++  * get_asn1_parameters fields are set to NULL.
++  */
++ static const EVP_CIPHER pk11_des_ecb =
++ 	{
++ 	NID_des_ecb,
++ 	8, 8, 8,
++ 	EVP_CIPH_ECB_MODE,
++ 	pk11_cipher_init,
++ 	pk11_cipher_do_cipher,
++ 	pk11_cipher_cleanup,
++ 	sizeof (PK11_CIPHER_STATE),
++ 	NULL,
++ 	NULL,
++ 	NULL
++ 	};
++ 
++ static const EVP_CIPHER pk11_3des_ecb =
++ 	{
++ 	NID_des_ede3_ecb,
++ 	8, 24, 8,
++ 	EVP_CIPH_ECB_MODE,
++ 	pk11_cipher_init,
++ 	pk11_cipher_do_cipher,
++ 	pk11_cipher_cleanup,
++ 	sizeof (PK11_CIPHER_STATE),
++ 	NULL,
++ 	NULL,
++ 	NULL
++ 	};
++ 
++ 
++ static const EVP_CIPHER pk11_aes_128_cbc =
++ 	{
++ 	NID_aes_128_cbc,
++ 	16, 16, 16,
++ 	EVP_CIPH_CBC_MODE,
++ 	pk11_cipher_init,
++ 	pk11_cipher_do_cipher,
++ 	pk11_cipher_cleanup,
++ 	sizeof (PK11_CIPHER_STATE),
++ 	EVP_CIPHER_set_asn1_iv,
++ 	EVP_CIPHER_get_asn1_iv,
++ 	NULL
++ 	};
++ 
++ static const EVP_CIPHER pk11_aes_192_cbc =
++ 	{
++ 	NID_aes_192_cbc,
++ 	16, 24, 16,
++ 	EVP_CIPH_CBC_MODE,
++ 	pk11_cipher_init,
++ 	pk11_cipher_do_cipher,
++ 	pk11_cipher_cleanup,
++ 	sizeof (PK11_CIPHER_STATE),
++ 	EVP_CIPHER_set_asn1_iv,
++ 	EVP_CIPHER_get_asn1_iv,
++ 	NULL
++ 	};
++ 
++ static const EVP_CIPHER pk11_aes_256_cbc =
++ 	{
++ 	NID_aes_256_cbc,
++ 	16, 32, 16,
++ 	EVP_CIPH_CBC_MODE,
++ 	pk11_cipher_init,
++ 	pk11_cipher_do_cipher,
++ 	pk11_cipher_cleanup,
++ 	sizeof (PK11_CIPHER_STATE),
++ 	EVP_CIPHER_set_asn1_iv,
++ 	EVP_CIPHER_get_asn1_iv,
++ 	NULL
++ 	};
++ 
++ /*
++  * ECB modes don't use IV so that's why set_asn1_parameters and
++  * get_asn1_parameters are set to NULL.
++  */
++ static const EVP_CIPHER pk11_aes_128_ecb =
++ 	{
++ 	NID_aes_128_ecb,
++ 	16, 16, 0,
++ 	EVP_CIPH_ECB_MODE,
++ 	pk11_cipher_init,
++ 	pk11_cipher_do_cipher,
++ 	pk11_cipher_cleanup,
++ 	sizeof (PK11_CIPHER_STATE),
++ 	NULL,
++ 	NULL,
++ 	NULL
++ 	};
++ 
++ static const EVP_CIPHER pk11_aes_192_ecb =
++ 	{
++ 	NID_aes_192_ecb,
++ 	16, 24, 0,
++ 	EVP_CIPH_ECB_MODE,
++ 	pk11_cipher_init,
++ 	pk11_cipher_do_cipher,
++ 	pk11_cipher_cleanup,
++ 	sizeof (PK11_CIPHER_STATE),
++ 	NULL,
++ 	NULL,
++ 	NULL
++ 	};
++ 
++ static const EVP_CIPHER pk11_aes_256_ecb =
++ 	{
++ 	NID_aes_256_ecb,
++ 	16, 32, 0,
++ 	EVP_CIPH_ECB_MODE,
++ 	pk11_cipher_init,
++ 	pk11_cipher_do_cipher,
++ 	pk11_cipher_cleanup,
++ 	sizeof (PK11_CIPHER_STATE),
++ 	NULL,
++ 	NULL,
++ 	NULL
++ 	};
++ 
++ #ifdef	SOLARIS_AES_CTR
++ /*
++  * NID_undef's will be changed to the AES counter mode NIDs as soon they are
++  * created in pk11_library_init(). Note that the need to change these structures
++  * is the reason why we don't define them with the const keyword.
++  */
++ static EVP_CIPHER pk11_aes_128_ctr =
++ 	{
++ 	NID_undef,
++ 	16, 16, 16,
++ 	EVP_CIPH_CBC_MODE,
++ 	pk11_cipher_init,
++ 	pk11_cipher_do_cipher,
++ 	pk11_cipher_cleanup,
++ 	sizeof (PK11_CIPHER_STATE),
++ 	EVP_CIPHER_set_asn1_iv,
++ 	EVP_CIPHER_get_asn1_iv,
++ 	NULL
++ 	};
++ 
++ static EVP_CIPHER pk11_aes_192_ctr =
++ 	{
++ 	NID_undef,
++ 	16, 24, 16,
++ 	EVP_CIPH_CBC_MODE,
++ 	pk11_cipher_init,
++ 	pk11_cipher_do_cipher,
++ 	pk11_cipher_cleanup,
++ 	sizeof (PK11_CIPHER_STATE),
++ 	EVP_CIPHER_set_asn1_iv,
++ 	EVP_CIPHER_get_asn1_iv,
++ 	NULL
++ 	};
++ 
++ static EVP_CIPHER pk11_aes_256_ctr =
++ 	{
++ 	NID_undef,
++ 	16, 32, 16,
++ 	EVP_CIPH_CBC_MODE,
++ 	pk11_cipher_init,
++ 	pk11_cipher_do_cipher,
++ 	pk11_cipher_cleanup,
++ 	sizeof (PK11_CIPHER_STATE),
++ 	EVP_CIPHER_set_asn1_iv,
++ 	EVP_CIPHER_get_asn1_iv,
++ 	NULL
++ 	};
++ #endif	/* SOLARIS_AES_CTR */
++ 
++ static const EVP_CIPHER pk11_bf_cbc =
++ 	{
++ 	NID_bf_cbc,
++ 	8, 16, 8,
++ 	EVP_CIPH_VARIABLE_LENGTH,
++ 	pk11_cipher_init,
++ 	pk11_cipher_do_cipher,
++ 	pk11_cipher_cleanup,
++ 	sizeof (PK11_CIPHER_STATE),
++ 	EVP_CIPHER_set_asn1_iv,
++ 	EVP_CIPHER_get_asn1_iv,
++ 	NULL
++ 	};
++ 
++ static const EVP_CIPHER pk11_rc4 =
++ 	{
++ 	NID_rc4,
++ 	1, 16, 0,
++ 	EVP_CIPH_VARIABLE_LENGTH,
++ 	pk11_cipher_init,
++ 	pk11_cipher_do_cipher,
++ 	pk11_cipher_cleanup,
++ 	sizeof (PK11_CIPHER_STATE),
++ 	NULL,
++ 	NULL,
++ 	NULL
++ 	};
++ 
++ static const EVP_MD pk11_md5 =
++ 	{
++ 	NID_md5,
++ 	NID_md5WithRSAEncryption,
++ 	MD5_DIGEST_LENGTH,
++ 	0,
++ 	pk11_digest_init,
++ 	pk11_digest_update,
++ 	pk11_digest_final,
++ 	pk11_digest_copy,
++ 	pk11_digest_cleanup,
++ 	EVP_PKEY_RSA_method,
++ 	MD5_CBLOCK,
++ 	sizeof (PK11_CIPHER_STATE),
++ 	};
++ 
++ static const EVP_MD pk11_sha1 =
++ 	{
++ 	NID_sha1,
++ 	NID_sha1WithRSAEncryption,
++ 	SHA_DIGEST_LENGTH,
++ 	0,
++ 	pk11_digest_init,
++ 	pk11_digest_update,
++ 	pk11_digest_final,
++ 	pk11_digest_copy,
++ 	pk11_digest_cleanup,
++ 	EVP_PKEY_RSA_method,
++ 	SHA_CBLOCK,
++ 	sizeof (PK11_CIPHER_STATE),
++ 	};
++ 
++ static const EVP_MD pk11_sha224 =
++ 	{
++ 	NID_sha224,
++ 	NID_sha224WithRSAEncryption,
++ 	SHA224_DIGEST_LENGTH,
++ 	0,
++ 	pk11_digest_init,
++ 	pk11_digest_update,
++ 	pk11_digest_final,
++ 	pk11_digest_copy,
++ 	pk11_digest_cleanup,
++ 	EVP_PKEY_RSA_method,
++ 	/* SHA-224 uses the same cblock size as SHA-256 */
++ 	SHA256_CBLOCK,
++ 	sizeof (PK11_CIPHER_STATE),
++ 	};
++ 
++ static const EVP_MD pk11_sha256 =
++ 	{
++ 	NID_sha256,
++ 	NID_sha256WithRSAEncryption,
++ 	SHA256_DIGEST_LENGTH,
++ 	0,
++ 	pk11_digest_init,
++ 	pk11_digest_update,
++ 	pk11_digest_final,
++ 	pk11_digest_copy,
++ 	pk11_digest_cleanup,
++ 	EVP_PKEY_RSA_method,
++ 	SHA256_CBLOCK,
++ 	sizeof (PK11_CIPHER_STATE),
++ 	};
++ 
++ static const EVP_MD pk11_sha384 =
++ 	{
++ 	NID_sha384,
++ 	NID_sha384WithRSAEncryption,
++ 	SHA384_DIGEST_LENGTH,
++ 	0,
++ 	pk11_digest_init,
++ 	pk11_digest_update,
++ 	pk11_digest_final,
++ 	pk11_digest_copy,
++ 	pk11_digest_cleanup,
++ 	EVP_PKEY_RSA_method,
++ 	/* SHA-384 uses the same cblock size as SHA-512 */
++ 	SHA512_CBLOCK,
++ 	sizeof (PK11_CIPHER_STATE),
++ 	};
++ 
++ static const EVP_MD pk11_sha512 =
++ 	{
++ 	NID_sha512,
++ 	NID_sha512WithRSAEncryption,
++ 	SHA512_DIGEST_LENGTH,
++ 	0,
++ 	pk11_digest_init,
++ 	pk11_digest_update,
++ 	pk11_digest_final,
++ 	pk11_digest_copy,
++ 	pk11_digest_cleanup,
++ 	EVP_PKEY_RSA_method,
++ 	SHA512_CBLOCK,
++ 	sizeof (PK11_CIPHER_STATE),
++ 	};
++ 
++ /*
++  * Initialization function. Sets up various PKCS#11 library components.
++  * The definitions for control commands specific to this engine
++  */
++ #define	PK11_CMD_SO_PATH		ENGINE_CMD_BASE
++ static const ENGINE_CMD_DEFN pk11_cmd_defns[] =
++ 	{
++ 		{
++ 		PK11_CMD_SO_PATH,
++ 		"SO_PATH",
++ 		"Specifies the path to the 'pkcs#11' shared library",
++ 		ENGINE_CMD_FLAG_STRING
++ 		},
++ 		{0, NULL, NULL, 0}
++ 	};
++ 
++ 
++ static RAND_METHOD pk11_random =
++ 	{
++ 	pk11_rand_seed,
++ 	pk11_rand_bytes,
++ 	pk11_rand_cleanup,
++ 	pk11_rand_add,
++ 	pk11_rand_bytes,
++ 	pk11_rand_status
++ 	};
++ 
++ 
++ /* Constants used when creating the ENGINE */
++ static const char *engine_pk11_id = "pkcs11";
++ static const char *engine_pk11_name = "PKCS #11 engine support";
++ 
++ CK_FUNCTION_LIST_PTR pFuncList = NULL;
++ static const char PK11_GET_FUNCTION_LIST[] = "C_GetFunctionList";
++ 
++ /*
++  * This is a static string constant for the DSO file name and the function
++  * symbol names to bind to. We set it in the Configure script based on whether
++  * this is 32 or 64 bit build.
++  */
++ static const char def_PK11_LIBNAME[] = PK11_LIB_LOCATION;
++ 
++ static CK_BBOOL true = TRUE;
++ static CK_BBOOL false = FALSE;
++ /* Needed in hw_pk11_pub.c as well so that's why it is not static. */
++ CK_SLOT_ID pubkey_SLOTID = 0;
++ static CK_SLOT_ID rand_SLOTID = 0;
++ static CK_SLOT_ID SLOTID = 0;
++ static CK_BBOOL pk11_library_initialized = FALSE;
++ static CK_BBOOL pk11_atfork_initialized = FALSE;
++ static int pk11_pid = 0;
++ 
++ static DSO *pk11_dso = NULL;
++ 
++ /* allocate and initialize all locks used by the engine itself */
++ static int pk11_init_all_locks(void)
++ 	{
++ 	int type;
++ 
++ #ifndef OPENSSL_NO_RSA
++ 	find_lock[OP_RSA] = OPENSSL_malloc(sizeof (pthread_mutex_t));
++ 	if (find_lock[OP_RSA] == NULL)
++ 		goto malloc_err;
++ 	(void) pthread_mutex_init(find_lock[OP_RSA], NULL);
++ #endif /* OPENSSL_NO_RSA */
++ 
++ 	if ((uri_lock = OPENSSL_malloc(sizeof (pthread_mutex_t))) == NULL)
++ 		goto malloc_err;
++ 	(void) pthread_mutex_init(uri_lock, NULL);
++ 
++ #ifndef OPENSSL_NO_DSA
++ 	find_lock[OP_DSA] = OPENSSL_malloc(sizeof (pthread_mutex_t));
++ 	if (find_lock[OP_DSA] == NULL)
++ 		goto malloc_err;
++ 	(void) pthread_mutex_init(find_lock[OP_DSA], NULL);
++ #endif /* OPENSSL_NO_DSA */
++ 
++ #ifndef OPENSSL_NO_DH
++ 	find_lock[OP_DH] = OPENSSL_malloc(sizeof (pthread_mutex_t));
++ 	if (find_lock[OP_DH] == NULL)
++ 		goto malloc_err;
++ 	(void) pthread_mutex_init(find_lock[OP_DH], NULL);
++ #endif /* OPENSSL_NO_DH */
++ 
++ 	for (type = 0; type < OP_MAX; type++)
++ 		{
++ 		session_cache[type].lock =
++ 		    OPENSSL_malloc(sizeof (pthread_mutex_t));
++ 		if (session_cache[type].lock == NULL)
++ 			goto malloc_err;
++ 		(void) pthread_mutex_init(session_cache[type].lock, NULL);
++ 		}
++ 
++ 	return (1);
++ 
++ malloc_err:
++ 	pk11_free_all_locks();
++ 	PK11err(PK11_F_INIT_ALL_LOCKS, PK11_R_MALLOC_FAILURE);
++ 	return (0);
++ 	}
++ 
++ static void pk11_free_all_locks(void)
++ 	{
++ 	int type;
++ 
++ #ifndef OPENSSL_NO_RSA
++ 	if (find_lock[OP_RSA] != NULL)
++ 		{
++ 		(void) pthread_mutex_destroy(find_lock[OP_RSA]);
++ 		OPENSSL_free(find_lock[OP_RSA]);
++ 		find_lock[OP_RSA] = NULL;
++ 		}
++ #endif /* OPENSSL_NO_RSA */
++ #ifndef OPENSSL_NO_DSA
++ 	if (find_lock[OP_DSA] != NULL)
++ 		{
++ 		(void) pthread_mutex_destroy(find_lock[OP_DSA]);
++ 		OPENSSL_free(find_lock[OP_DSA]);
++ 		find_lock[OP_DSA] = NULL;
++ 		}
++ #endif /* OPENSSL_NO_DSA */
++ #ifndef OPENSSL_NO_DH
++ 	if (find_lock[OP_DH] != NULL)
++ 		{
++ 		(void) pthread_mutex_destroy(find_lock[OP_DH]);
++ 		OPENSSL_free(find_lock[OP_DH]);
++ 		find_lock[OP_DH] = NULL;
++ 		}
++ #endif /* OPENSSL_NO_DH */
++ 
++ 	for (type = 0; type < OP_MAX; type++)
++ 		{
++ 		if (session_cache[type].lock != NULL)
++ 			{
++ 			(void) pthread_mutex_destroy(session_cache[type].lock);
++ 			OPENSSL_free(session_cache[type].lock);
++ 			session_cache[type].lock = NULL;
++ 			}
++ 		}
++ 	}
++ 
++ /*
++  * This internal function is used by ENGINE_pk11() and "dynamic" ENGINE support.
++  */
++ static int bind_pk11(ENGINE *e)
++ 	{
++ #ifndef OPENSSL_NO_RSA
++ 	const RSA_METHOD *rsa = NULL;
++ 	RSA_METHOD *pk11_rsa = PK11_RSA();
++ #endif	/* OPENSSL_NO_RSA */
++ 	if (!pk11_library_initialized)
++ 		if (!pk11_library_init(e))
++ 			return (0);
++ 
++ 	if (!ENGINE_set_id(e, engine_pk11_id) ||
++ 	    !ENGINE_set_name(e, engine_pk11_name) ||
++ 	    !ENGINE_set_ciphers(e, pk11_engine_ciphers) ||
++ 	    !ENGINE_set_digests(e, pk11_engine_digests))
++ 		return (0);
++ #ifndef OPENSSL_NO_RSA
++ 	if (pk11_have_rsa == CK_TRUE)
++ 		{
++ 		if (!ENGINE_set_RSA(e, PK11_RSA()) ||
++ 		    !ENGINE_set_load_privkey_function(e, pk11_load_privkey) ||
++ 		    !ENGINE_set_load_pubkey_function(e, pk11_load_pubkey))
++ 			return (0);
++ #ifdef	DEBUG_SLOT_SELECTION
++ 		fprintf(stderr, "%s: registered RSA\n", PK11_DBG);
++ #endif	/* DEBUG_SLOT_SELECTION */
++ 		}
++ #endif	/* OPENSSL_NO_RSA */
++ #ifndef OPENSSL_NO_DSA
++ 	if (pk11_have_dsa == CK_TRUE)
++ 		{
++ 		if (!ENGINE_set_DSA(e, PK11_DSA()))
++ 			return (0);
++ #ifdef	DEBUG_SLOT_SELECTION
++ 		fprintf(stderr, "%s: registered DSA\n", PK11_DBG);
++ #endif	/* DEBUG_SLOT_SELECTION */
++ 		}
++ #endif	/* OPENSSL_NO_DSA */
++ #ifndef OPENSSL_NO_DH
++ 	if (pk11_have_dh == CK_TRUE)
++ 		{
++ 		if (!ENGINE_set_DH(e, PK11_DH()))
++ 			return (0);
++ #ifdef	DEBUG_SLOT_SELECTION
++ 		fprintf(stderr, "%s: registered DH\n", PK11_DBG);
++ #endif	/* DEBUG_SLOT_SELECTION */
++ 		}
++ #endif	/* OPENSSL_NO_DH */
++ 	if (pk11_have_random)
++ 		{
++ 		if (!ENGINE_set_RAND(e, &pk11_random))
++ 			return (0);
++ #ifdef	DEBUG_SLOT_SELECTION
++ 		fprintf(stderr, "%s: registered random\n", PK11_DBG);
++ #endif	/* DEBUG_SLOT_SELECTION */
++ 		}
++ 	if (!ENGINE_set_init_function(e, pk11_init) ||
++ 	    !ENGINE_set_destroy_function(e, pk11_destroy) ||
++ 	    !ENGINE_set_finish_function(e, pk11_finish) ||
++ 	    !ENGINE_set_ctrl_function(e, pk11_ctrl) ||
++ 	    !ENGINE_set_cmd_defns(e, pk11_cmd_defns))
++ 		return (0);
++ 
++ /*
++  * Apache calls OpenSSL function RSA_blinding_on() once during startup
++  * which in turn calls bn_mod_exp. Since we do not implement bn_mod_exp
++  * here, we wire it back to the OpenSSL software implementation.
++  * Since it is used only once, performance is not a concern.
++  */
++ #ifndef OPENSSL_NO_RSA
++ 	rsa = RSA_PKCS1_SSLeay();
++ 	pk11_rsa->rsa_mod_exp = rsa->rsa_mod_exp;
++ 	pk11_rsa->bn_mod_exp = rsa->bn_mod_exp;
++ #endif	/* OPENSSL_NO_RSA */
++ 
++ 	/* Ensure the pk11 error handling is set up */
++ 	ERR_load_pk11_strings();
++ 
++ 	return (1);
++ 	}
++ 
++ /* Dynamic engine support is disabled at a higher level for Solaris */
++ #ifdef	ENGINE_DYNAMIC_SUPPORT
++ static int bind_helper(ENGINE *e, const char *id)
++ 	{
++ 	if (id && (strcmp(id, engine_pk11_id) != 0))
++ 		return (0);
++ 
++ 	if (!bind_pk11(e))
++ 		return (0);
++ 
++ 	return (1);
++ 	}
++ 
++ IMPLEMENT_DYNAMIC_CHECK_FN()
++ IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
++ 
++ #else
++ static ENGINE *engine_pk11(void)
++ 	{
++ 	ENGINE *ret = ENGINE_new();
++ 
++ 	if (!ret)
++ 		return (NULL);
++ 
++ 	if (!bind_pk11(ret))
++ 		{
++ 		ENGINE_free(ret);
++ 		return (NULL);
++ 		}
++ 
++ 	return (ret);
++ 	}
++ 
++ void
++ ENGINE_load_pk11(void)
++ 	{
++ 	ENGINE *e_pk11 = NULL;
++ 
++ 	/*
++ 	 * Do not use dynamic PKCS#11 library on Solaris due to
++ 	 * security reasons. We will link it in statically.
++ 	 */
++ 	/* Attempt to load PKCS#11 library */
++ 	if (!pk11_dso)
++ 		pk11_dso = DSO_load(NULL, get_PK11_LIBNAME(), NULL, 0);
++ 
++ 	if (pk11_dso == NULL)
++ 		{
++ 		PK11err(PK11_F_LOAD, PK11_R_DSO_FAILURE);
++ 		return;
++ 		}
++ 
++ 	e_pk11 = engine_pk11();
++ 	if (!e_pk11)
++ 		{
++ 		DSO_free(pk11_dso);
++ 		pk11_dso = NULL;
++ 		return;
++ 		}
++ 
++ 	/*
++ 	 * At this point, the pk11 shared library is either dynamically
++ 	 * loaded or statically linked in. So, initialize the pk11
++ 	 * library before calling ENGINE_set_default since the latter
++ 	 * needs cipher and digest algorithm information
++ 	 */
++ 	if (!pk11_library_init(e_pk11))
++ 		{
++ 		DSO_free(pk11_dso);
++ 		pk11_dso = NULL;
++ 		ENGINE_free(e_pk11);
++ 		return;
++ 		}
++ 
++ 	ENGINE_add(e_pk11);
++ 
++ 	ENGINE_free(e_pk11);
++ 	ERR_clear_error();
++ 	}
++ #endif	/* ENGINE_DYNAMIC_SUPPORT */
++ 
++ /*
++  * These are the static string constants for the DSO file name and
++  * the function symbol names to bind to.
++  */
++ static const char *PK11_LIBNAME = NULL;
++ 
++ static const char *get_PK11_LIBNAME(void)
++ 	{
++ 	if (PK11_LIBNAME)
++ 		return (PK11_LIBNAME);
++ 
++ 	return (def_PK11_LIBNAME);
++ 	}
++ 
++ static void free_PK11_LIBNAME(void)
++ 	{
++ 	if (PK11_LIBNAME)
++ 		OPENSSL_free((void*)PK11_LIBNAME);
++ 
++ 	PK11_LIBNAME = NULL;
++ 	}
++ 
++ static long set_PK11_LIBNAME(const char *name)
++ 	{
++ 	free_PK11_LIBNAME();
++ 
++ 	return ((PK11_LIBNAME = BUF_strdup(name)) != NULL ? 1 : 0);
++ 	}
++ 
++ /* acquire all engine specific mutexes before fork */
++ static void pk11_fork_prepare(void)
++ 	{
++ 	int i;
++ 
++ 	if (!pk11_library_initialized)
++ 		return;
++ 
++ 	LOCK_OBJSTORE(OP_RSA);
++ 	LOCK_OBJSTORE(OP_DSA);
++ 	LOCK_OBJSTORE(OP_DH);
++ 	(void) pthread_mutex_lock(uri_lock);
++ 	for (i = 0; i < OP_MAX; i++)
++ 		{
++ 		(void) pthread_mutex_lock(session_cache[i].lock);
++ 		}
++ 	}
++ 
++ /* release all engine specific mutexes */
++ static void pk11_fork_parent(void)
++ 	{
++ 	int i;
++ 
++ 	if (!pk11_library_initialized)
++ 		return;
++ 
++ 	for (i = OP_MAX - 1; i >= 0; i--)
++ 		{
++ 		(void) pthread_mutex_unlock(session_cache[i].lock);
++ 		}
++ 	UNLOCK_OBJSTORE(OP_DH);
++ 	UNLOCK_OBJSTORE(OP_DSA);
++ 	UNLOCK_OBJSTORE(OP_RSA);
++ 	(void) pthread_mutex_unlock(uri_lock);
++ 	}
++ 
++ /*
++  * same situation as in parent - we need to unlock all locks to make them
++  * accessible to all threads.
++  */
++ static void pk11_fork_child(void)
++ 	{
++ 	int i;
++ 
++ 	if (!pk11_library_initialized)
++ 		return;
++ 
++ 	for (i = OP_MAX - 1; i >= 0; i--)
++ 		{
++ 		(void) pthread_mutex_unlock(session_cache[i].lock);
++ 		}
++ 	UNLOCK_OBJSTORE(OP_DH);
++ 	UNLOCK_OBJSTORE(OP_DSA);
++ 	UNLOCK_OBJSTORE(OP_RSA);
++ 	(void) pthread_mutex_unlock(uri_lock);
++ 	}
++ 
++ /* Initialization function for the pk11 engine */
++ static int pk11_init(ENGINE *e)
++ {
++ 	return (pk11_library_init(e));
++ }
++ 
++ /*
++  * Initialization function. Sets up various PKCS#11 library components.
++  * It selects a slot based on predefined critiera. In the process, it also
++  * count how many ciphers and digests to support. Since the cipher and
++  * digest information is needed when setting default engine, this function
++  * needs to be called before calling ENGINE_set_default.
++  */
++ /* ARGSUSED */
++ static int pk11_library_init(ENGINE *e)
++ 	{
++ 	CK_C_GetFunctionList p;
++ 	CK_RV rv = CKR_OK;
++ 	CK_INFO info;
++ 	CK_ULONG ul_state_len;
++ 	int any_slot_found;
++ 	int i;
++ 
++ 	/*
++ 	 * pk11_library_initialized is set to 0 in pk11_finish() which is called
++ 	 * from ENGINE_finish(). However, if there is still at least one
++ 	 * existing functional reference to the engine (see engine(3) for more
++ 	 * information), pk11_finish() is skipped. For example, this can happen
++ 	 * if an application forgets to clear one cipher context. In case of a
++ 	 * fork() when the application is finishing the engine so that it can be
++ 	 * reinitialized in the child, forgotten functional reference causes
++ 	 * pk11_library_initialized to stay 1. In that case we need the PID
++ 	 * check so that we properly initialize the engine again.
++ 	 */
++ 	if (pk11_library_initialized)
++ 		{
++ 		if (pk11_pid == getpid())
++ 			{
++ 			return (1);
++ 			}
++ 		else
++ 			{
++ 			global_session = CK_INVALID_HANDLE;
++ 			/*
++ 			 * free the locks first to prevent memory leak in case
++ 			 * the application calls fork() without finishing the
++ 			 * engine first.
++ 			 */
++ 			pk11_free_all_locks();
++ 			}
++ 		}
++ 
++ 	if (pk11_dso == NULL)
++ 		{
++ 		PK11err(PK11_F_LIBRARY_INIT, PK11_R_DSO_FAILURE);
++ 		goto err;
++ 		}
++ 
++ #ifdef	SOLARIS_AES_CTR
++ 	/*
++ 	 * We must do this before we start working with slots since we need all
++ 	 * NIDs there.
++ 	 */
++ 	if (pk11_add_aes_ctr_NIDs() == 0)
++ 		goto err;
++ #endif	/* SOLARIS_AES_CTR */
++ 
++ #ifdef	SOLARIS_HW_SLOT_SELECTION
++ 	if (check_hw_mechanisms() == 0)
++ 		goto err;
++ #endif	/* SOLARIS_HW_SLOT_SELECTION */
++ 
++ 	/* get the C_GetFunctionList function from the loaded library */
++ 	p = (CK_C_GetFunctionList)DSO_bind_func(pk11_dso,
++ 		PK11_GET_FUNCTION_LIST);
++ 	if (!p)
++ 		{
++ 		PK11err(PK11_F_LIBRARY_INIT, PK11_R_DSO_FAILURE);
++ 		goto err;
++ 		}
++ 
++ 	/* get the full function list from the loaded library */
++ 	rv = p(&pFuncList);
++ 	if (rv != CKR_OK)
++ 		{
++ 		PK11err_add_data(PK11_F_LIBRARY_INIT, PK11_R_DSO_FAILURE, rv);
++ 		goto err;
++ 		}
++ 
++ 	rv = pFuncList->C_Initialize(NULL_PTR);
++ 	if ((rv != CKR_OK) && (rv != CKR_CRYPTOKI_ALREADY_INITIALIZED))
++ 		{
++ 		PK11err_add_data(PK11_F_LIBRARY_INIT, PK11_R_INITIALIZE, rv);
++ 		goto err;
++ 		}
++ 
++ 	rv = pFuncList->C_GetInfo(&info);
++ 	if (rv != CKR_OK)
++ 		{
++ 		PK11err_add_data(PK11_F_LIBRARY_INIT, PK11_R_GETINFO, rv);
++ 		goto err;
++ 		}
++ 
++ 	if (pk11_choose_slots(&any_slot_found) == 0)
++ 		goto err;
++ 
++ 	/*
++ 	 * The library we use, set in def_PK11_LIBNAME, may not offer any
++ 	 * slot(s). In that case, we must not proceed but we must not return an
++ 	 * error. The reason is that applications that try to set up the PKCS#11
++ 	 * engine don't exit on error during the engine initialization just
++ 	 * because no slot was present.
++ 	 */
++ 	if (any_slot_found == 0)
++ 		return (1);
++ 
++ 	if (global_session == CK_INVALID_HANDLE)
++ 		{
++ 		/* Open the global_session for the new process */
++ 		rv = pFuncList->C_OpenSession(SLOTID, CKF_SERIAL_SESSION,
++ 			NULL_PTR, NULL_PTR, &global_session);
++ 		if (rv != CKR_OK)
++ 			{
++ 			PK11err_add_data(PK11_F_LIBRARY_INIT,
++ 			    PK11_R_OPENSESSION, rv);
++ 			goto err;
++ 			}
++ 		}
++ 
++ 	/*
++ 	 * Disable digest if C_GetOperationState is not supported since
++ 	 * this function is required by OpenSSL digest copy function
++ 	 */
++ 	if (pFuncList->C_GetOperationState(global_session, NULL, &ul_state_len)
++ 			== CKR_FUNCTION_NOT_SUPPORTED) {
++ #ifdef	DEBUG_SLOT_SELECTION
++ 		fprintf(stderr, "%s: C_GetOperationState() not supported, "
++ 		    "setting digest_count to 0\n", PK11_DBG);
++ #endif	/* DEBUG_SLOT_SELECTION */
++ 		digest_count = 0;
++ 	}
++ 
++ 	pk11_library_initialized = TRUE;
++ 	pk11_pid = getpid();
++ 	/*
++ 	 * if initialization of the locks fails pk11_init_all_locks()
++ 	 * will do the cleanup.
++ 	 */
++ 	if (!pk11_init_all_locks())
++ 		goto err;
++ 	for (i = 0; i < OP_MAX; i++)
++ 		session_cache[i].head = NULL;
++ 	/*
++ 	 * initialize active lists. We only use active lists
++ 	 * for asymmetric ciphers.
++ 	 */
++ 	for (i = 0; i < OP_MAX; i++)
++ 		active_list[i] = NULL;
++ 
++ 	if (!pk11_atfork_initialized)
++ 		{
++ 		if (pthread_atfork(pk11_fork_prepare, pk11_fork_parent,
++ 		    pk11_fork_child) != 0)
++ 			{
++ 			PK11err(PK11_F_LIBRARY_INIT, PK11_R_ATFORK_FAILED);
++ 			goto err;
++ 			}
++ 		pk11_atfork_initialized = TRUE;
++ 		}
++ 
++ 	return (1);
++ 
++ err:
++ 	return (0);
++ 	}
++ 
++ /* Destructor (complements the "ENGINE_pk11()" constructor) */
++ /* ARGSUSED */
++ static int pk11_destroy(ENGINE *e)
++ 	{
++ 	free_PK11_LIBNAME();
++ 	ERR_unload_pk11_strings();
++ 	return (1);
++ 	}
++ 
++ /*
++  * Termination function to clean up the session, the token, and the pk11
++  * library.
++  */
++ /* ARGSUSED */
++ static int pk11_finish(ENGINE *e)
++ 	{
++ 	int i;
++ 
++ 	if (pk11_dso == NULL)
++ 		{
++ 		PK11err(PK11_F_FINISH, PK11_R_NOT_LOADED);
++ 		goto err;
++ 		}
++ 
++ 	OPENSSL_assert(pFuncList != NULL);
++ 
++ 	if (pk11_free_all_sessions() == 0)
++ 		goto err;
++ 
++ 	/* free all active lists */
++ 	for (i = 0; i < OP_MAX; i++)
++ 		pk11_free_active_list(i);
++ 
++ 	pFuncList->C_CloseSession(global_session);
++ 	global_session = CK_INVALID_HANDLE;
++ 
++ 	/*
++ 	 * Since we are part of a library (libcrypto.so), calling this function
++ 	 * may have side-effects.
++ 	 */
++ #if 0
++ 	pFuncList->C_Finalize(NULL);
++ #endif
++ 
++ 	if (!DSO_free(pk11_dso))
++ 		{
++ 		PK11err(PK11_F_FINISH, PK11_R_DSO_FAILURE);
++ 		goto err;
++ 		}
++ 	pk11_dso = NULL;
++ 	pFuncList = NULL;
++ 	pk11_library_initialized = FALSE;
++ 	pk11_pid = 0;
++ 	/*
++ 	 * There is no way how to unregister atfork handlers (other than
++ 	 * unloading the library) so we just free the locks. For this reason
++ 	 * the atfork handlers check if the engine is initialized and bail out
++ 	 * immediately if not. This is necessary in case a process finishes
++ 	 * the engine before calling fork().
++ 	 */
++ 	pk11_free_all_locks();
++ 
++ 	return (1);
++ 
++ err:
++ 	return (0);
++ 	}
++ 
++ /* Standard engine interface function to set the dynamic library path */
++ /* ARGSUSED */
++ static int pk11_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
++ 	{
++ 	int initialized = ((pk11_dso == NULL) ? 0 : 1);
++ 
++ 	switch (cmd)
++ 		{
++ 	case PK11_CMD_SO_PATH:
++ 		if (p == NULL)
++ 			{
++ 			PK11err(PK11_F_CTRL, ERR_R_PASSED_NULL_PARAMETER);
++ 			return (0);
++ 			}
++ 
++ 		if (initialized)
++ 			{
++ 			PK11err(PK11_F_CTRL, PK11_R_ALREADY_LOADED);
++ 			return (0);
++ 			}
++ 
++ 		return (set_PK11_LIBNAME((const char *)p));
++ 	default:
++ 		break;
++ 		}
++ 
++ 	PK11err(PK11_F_CTRL, PK11_R_CTRL_COMMAND_NOT_IMPLEMENTED);
++ 
++ 	return (0);
++ 	}
++ 
++ 
++ /* Required function by the engine random interface. It does nothing here */
++ static void pk11_rand_cleanup(void)
++ 	{
++ 	return;
++ 	}
++ 
++ /* ARGSUSED */
++ static void pk11_rand_add(const void *buf, int num, double add)
++ 	{
++ 	PK11_SESSION *sp;
++ 
++ 	if ((sp = pk11_get_session(OP_RAND)) == NULL)
++ 		return;
++ 
++ 	/*
++ 	 * Ignore any errors (e.g. CKR_RANDOM_SEED_NOT_SUPPORTED) since
++ 	 * the calling functions do not care anyway
++ 	 */
++ 	pFuncList->C_SeedRandom(sp->session, (unsigned char *) buf, num);
++ 	pk11_return_session(sp, OP_RAND);
++ 
++ 	return;
++ 	}
++ 
++ static void pk11_rand_seed(const void *buf, int num)
++ 	{
++ 	pk11_rand_add(buf, num, 0);
++ 	}
++ 
++ static int pk11_rand_bytes(unsigned char *buf, int num)
++ 	{
++ 	CK_RV rv;
++ 	PK11_SESSION *sp;
++ 
++ 	if ((sp = pk11_get_session(OP_RAND)) == NULL)
++ 		return (0);
++ 
++ 	rv = pFuncList->C_GenerateRandom(sp->session, buf, num);
++ 	if (rv != CKR_OK)
++ 		{
++ 		PK11err_add_data(PK11_F_RAND_BYTES, PK11_R_GENERATERANDOM, rv);
++ 		pk11_return_session(sp, OP_RAND);
++ 		return (0);
++ 		}
++ 
++ 	pk11_return_session(sp, OP_RAND);
++ 	return (1);
++ 	}
++ 
++ /* Required function by the engine random interface. It does nothing here */
++ static int pk11_rand_status(void)
++ 	{
++ 	return (1);
++ 	}
++ 
++ /* Free all BIGNUM structures from PK11_SESSION. */
++ static void pk11_free_nums(PK11_SESSION *sp, PK11_OPTYPE optype)
++ 	{
++ 	switch (optype)
++ 		{
++ #ifndef	OPENSSL_NO_RSA
++ 		case OP_RSA:
++ 			if (sp->opdata_rsa_n_num != NULL)
++ 				{
++ 				BN_free(sp->opdata_rsa_n_num);
++ 				sp->opdata_rsa_n_num = NULL;
++ 				}
++ 			if (sp->opdata_rsa_e_num != NULL)
++ 				{
++ 				BN_free(sp->opdata_rsa_e_num);
++ 				sp->opdata_rsa_e_num = NULL;
++ 				}
++ 			if (sp->opdata_rsa_d_num != NULL)
++ 				{
++ 				BN_free(sp->opdata_rsa_d_num);
++ 				sp->opdata_rsa_d_num = NULL;
++ 				}
++ 			break;
++ #endif
++ #ifndef	OPENSSL_NO_DSA
++ 		case OP_DSA:
++ 			if (sp->opdata_dsa_pub_num != NULL)
++ 				{
++ 				BN_free(sp->opdata_dsa_pub_num);
++ 				sp->opdata_dsa_pub_num = NULL;
++ 				}
++ 			if (sp->opdata_dsa_priv_num != NULL)
++ 				{
++ 				BN_free(sp->opdata_dsa_priv_num);
++ 				sp->opdata_dsa_priv_num = NULL;
++ 				}
++ 			break;
++ #endif
++ #ifndef	OPENSSL_NO_DH
++ 		case OP_DH:
++ 			if (sp->opdata_dh_priv_num != NULL)
++ 				{
++ 				BN_free(sp->opdata_dh_priv_num);
++ 				sp->opdata_dh_priv_num = NULL;
++ 				}
++ 			break;
++ #endif
++ 		default:
++ 			break;
++ 		}
++ 	}
++ 
++ /*
++  * Get new PK11_SESSION structure ready for use. Every process must have
++  * its own freelist of PK11_SESSION structures so handle fork() here
++  * by destroying the old and creating new freelist.
++  * The returned PK11_SESSION structure is disconnected from the freelist.
++  */
++ PK11_SESSION *
++ pk11_get_session(PK11_OPTYPE optype)
++ 	{
++ 	PK11_SESSION *sp = NULL, *sp1, *freelist;
++ 	pthread_mutex_t *freelist_lock;
++ 	static pid_t pid = 0;
++ 	pid_t new_pid;
++ 	CK_RV rv;
++ 
++ 	switch (optype)
++ 		{
++ 		case OP_RSA:
++ 		case OP_DSA:
++ 		case OP_DH:
++ 		case OP_RAND:
++ 		case OP_DIGEST:
++ 		case OP_CIPHER:
++ 			freelist_lock = session_cache[optype].lock;
++ 			break;
++ 		default:
++ 			PK11err(PK11_F_GET_SESSION,
++ 				PK11_R_INVALID_OPERATION_TYPE);
++ 			return (NULL);
++ 		}
++ 	(void) pthread_mutex_lock(freelist_lock);
++ 
++ 	/*
++ 	 * Will use it to find out if we forked. We cannot use the PID field in
++ 	 * the session structure because we could get a newly allocated session
++ 	 * here, with no PID information.
++ 	 */
++ 	if (pid == 0)
++ 		pid = getpid();
++ 
++ 	freelist = session_cache[optype].head;
++ 	sp = freelist;
++ 
++ 	/*
++ 	 * If the free list is empty, allocate new unitialized (filled
++ 	 * with zeroes) PK11_SESSION structure otherwise return first
++ 	 * structure from the freelist.
++ 	 */
++ 	if (sp == NULL)
++ 		{
++ 		if ((sp = OPENSSL_malloc(sizeof (PK11_SESSION))) == NULL)
++ 			{
++ 			PK11err(PK11_F_GET_SESSION,
++ 				PK11_R_MALLOC_FAILURE);
++ 			goto err;
++ 			}
++ 		(void) memset(sp, 0, sizeof (PK11_SESSION));
++ 
++ 		/*
++ 		 * It is a new session so it will look like a cache miss to the
++ 		 * code below. So, we must not try to to destroy its members so
++ 		 * mark them as unused.
++ 		 */
++ 		sp->opdata_rsa_priv_key = CK_INVALID_HANDLE;
++ 		sp->opdata_rsa_pub_key = CK_INVALID_HANDLE;
++ 		}
++ 	else
++ 		freelist = sp->next;
++ 
++ 	/*
++ 	 * Check whether we have forked. In that case, we must get rid of all
++ 	 * inherited sessions and start allocating new ones.
++ 	 */
++ 	if (pid != (new_pid = getpid()))
++ 		{
++ 		pid = new_pid;
++ 
++ 		/*
++ 		 * We are a new process and thus need to free any inherited
++ 		 * PK11_SESSION objects aside from the first session (sp) which
++ 		 * is the only PK11_SESSION structure we will reuse (for the
++ 		 * head of the list).
++ 		 */
++ 		while ((sp1 = freelist) != NULL)
++ 			{
++ 			freelist = sp1->next;
++ 			/*
++ 			 * NOTE: we do not want to call pk11_free_all_sessions()
++ 			 * here because it would close underlying PKCS#11
++ 			 * sessions and destroy all objects.
++ 			 */
++ 			pk11_free_nums(sp1, optype);
++ 			OPENSSL_free(sp1);
++ 			}
++ 
++ 		/* we have to free the active list as well. */
++ 		pk11_free_active_list(optype);
++ 
++ 		/* Initialize the process */
++ 		rv = pFuncList->C_Initialize(NULL_PTR);
++ 		if ((rv != CKR_OK) && (rv != CKR_CRYPTOKI_ALREADY_INITIALIZED))
++ 			{
++ 			PK11err_add_data(PK11_F_GET_SESSION, PK11_R_INITIALIZE,
++ 			    rv);
++ 			OPENSSL_free(sp);
++ 			sp = NULL;
++ 			goto err;
++ 			}
++ 
++ 		/*
++ 		 * Choose slot here since the slot table is different on this
++ 		 * process. If we are here then we must have found at least one
++ 		 * usable slot before so we don't need to check any_slot_found.
++ 		 * See pk11_library_init()'s usage of this function for more
++ 		 * information.
++ 		 */
++ #ifdef	SOLARIS_HW_SLOT_SELECTION
++ 		if (check_hw_mechanisms() == 0)
++ 			goto err;
++ #endif	/* SOLARIS_HW_SLOT_SELECTION */
++ 		if (pk11_choose_slots(NULL) == 0)
++ 			goto err;
++ 
++ 		/* Open the global_session for the new process */
++ 		rv = pFuncList->C_OpenSession(SLOTID, CKF_SERIAL_SESSION,
++ 			NULL_PTR, NULL_PTR, &global_session);
++ 		if (rv != CKR_OK)
++ 			{
++ 			PK11err_add_data(PK11_F_GET_SESSION, PK11_R_OPENSESSION,
++ 			    rv);
++ 			OPENSSL_free(sp);
++ 			sp = NULL;
++ 			goto err;
++ 			}
++ 
++ 		/*
++ 		 * It is an inherited session from our parent so it needs
++ 		 * re-initialization.
++ 		 */
++ 		if (pk11_setup_session(sp, optype) == 0)
++ 			{
++ 			OPENSSL_free(sp);
++ 			sp = NULL;
++ 			goto err;
++ 			}
++ 		if (pk11_token_relogin(sp->session) == 0) 
++ 			{
++ 			/*
++ 			 * We will keep the session in the cache list and let
++ 			 * the caller cope with the situation.
++ 			 */
++ 			freelist = sp;
++ 			sp = NULL;
++ 			goto err;
++ 			}
++ 		}
++ 
++ 	if (sp->pid == 0)
++ 		{
++ 		/* It is a new session and needs initialization. */
++ 		if (pk11_setup_session(sp, optype) == 0)
++ 			{
++ 			OPENSSL_free(sp);
++ 			sp = NULL;
++ 			}
++ 		}
++ 
++ 	/* set new head for the list of PK11_SESSION objects */
++ 	session_cache[optype].head = freelist;
++ 
++ err:
++ 	if (sp != NULL)
++ 		sp->next = NULL;
++ 
++ 	(void) pthread_mutex_unlock(freelist_lock);
++ 
++ 	return (sp);
++ 	}
++ 
++ 
++ void
++ pk11_return_session(PK11_SESSION *sp, PK11_OPTYPE optype)
++ 	{
++ 	pthread_mutex_t *freelist_lock;
++ 	PK11_SESSION *freelist;
++ 
++ 	/*
++ 	 * If this is a session from the parent it will be taken care of and
++ 	 * freed in pk11_get_session() as part of the post-fork clean up the
++ 	 * next time we will ask for a new session.
++ 	 */
++ 	if (sp == NULL || sp->pid != getpid())
++ 		return;
++ 
++ 	switch (optype)
++ 		{
++ 		case OP_RSA:
++ 		case OP_DSA:
++ 		case OP_DH:
++ 		case OP_RAND:
++ 		case OP_DIGEST:
++ 		case OP_CIPHER:
++ 			freelist_lock = session_cache[optype].lock;
++ 			break;
++ 		default:
++ 			PK11err(PK11_F_RETURN_SESSION,
++ 				PK11_R_INVALID_OPERATION_TYPE);
++ 			return;
++ 		}
++ 
++ 	(void) pthread_mutex_lock(freelist_lock);
++ 	freelist = session_cache[optype].head;
++ 	sp->next = freelist;
++ 	session_cache[optype].head = sp;
++ 	(void) pthread_mutex_unlock(freelist_lock);
++ 	}
++ 
++ 
++ /* Destroy all objects. This function is called when the engine is finished */
++ static int pk11_free_all_sessions()
++ 	{
++ 	int ret = 1;
++ 	int type;
++ 
++ #ifndef OPENSSL_NO_RSA
++ 	(void) pk11_destroy_rsa_key_objects(NULL);
++ #endif	/* OPENSSL_NO_RSA */
++ #ifndef OPENSSL_NO_DSA
++ 	(void) pk11_destroy_dsa_key_objects(NULL);
++ #endif	/* OPENSSL_NO_DSA */
++ #ifndef OPENSSL_NO_DH
++ 	(void) pk11_destroy_dh_key_objects(NULL);
++ #endif	/* OPENSSL_NO_DH */
++ 	(void) pk11_destroy_cipher_key_objects(NULL);
++ 
++ 	/*
++ 	 * We try to release as much as we can but any error means that we will
++ 	 * return 0 on exit.
++ 	 */
++ 	for (type = 0; type < OP_MAX; type++)
++ 		{
++ 		if (pk11_free_session_list(type) == 0)
++ 			ret = 0;
++ 		}
++ 
++ 	return (ret);
++ 	}
++ 
++ /*
++  * Destroy session structures from the linked list specified. Free as many
++  * sessions as possible but any failure in C_CloseSession() means that we
++  * return an error on return.
++  */
++ static int pk11_free_session_list(PK11_OPTYPE optype)
++ 	{
++ 	CK_RV rv;
++ 	PK11_SESSION *sp = NULL;
++ 	PK11_SESSION *freelist = NULL;
++ 	pid_t mypid = getpid();
++ 	pthread_mutex_t *freelist_lock;
++ 	int ret = 1;
++ 
++ 	switch (optype)
++ 		{
++ 		case OP_RSA:
++ 		case OP_DSA:
++ 		case OP_DH:
++ 		case OP_RAND:
++ 		case OP_DIGEST:
++ 		case OP_CIPHER:
++ 			freelist_lock = session_cache[optype].lock;
++ 			break;
++ 		default:
++ 			PK11err(PK11_F_FREE_ALL_SESSIONS,
++ 				PK11_R_INVALID_OPERATION_TYPE);
++ 			return (0);
++ 		}
++ 
++ 	(void) pthread_mutex_lock(freelist_lock);
++ 	freelist = session_cache[optype].head;
++ 	while ((sp = freelist) != NULL)
++ 		{
++ 		if (sp->session != CK_INVALID_HANDLE && sp->pid == mypid)
++ 			{
++ 			rv = pFuncList->C_CloseSession(sp->session);
++ 			if (rv != CKR_OK)
++ 				{
++ 				PK11err_add_data(PK11_F_FREE_ALL_SESSIONS,
++ 					PK11_R_CLOSESESSION, rv);
++ 				ret = 0;
++ 				}
++ 			}
++ 		freelist = sp->next;
++ 		pk11_free_nums(sp, optype);
++ 		OPENSSL_free(sp);
++ 		}
++ 
++ 	(void) pthread_mutex_unlock(freelist_lock);
++ 	return (ret);
++ 	}
++ 
++ 
++ static int
++ pk11_setup_session(PK11_SESSION *sp, PK11_OPTYPE optype)
++ 	{
++ 	CK_RV rv;
++ 	CK_SLOT_ID myslot;
++ 
++ 	switch (optype)
++ 		{
++ 		case OP_RSA:
++ 		case OP_DSA:
++ 		case OP_DH:
++ 			myslot = pubkey_SLOTID;
++ 			break;
++ 		case OP_RAND:
++ 			myslot = rand_SLOTID;
++ 			break;
++ 		case OP_DIGEST:
++ 		case OP_CIPHER:
++ 			myslot = SLOTID;
++ 			break;
++ 		default:
++ 			PK11err(PK11_F_SETUP_SESSION,
++ 			    PK11_R_INVALID_OPERATION_TYPE);
++ 			return (0);
++ 		}
++ 
++ 	sp->session = CK_INVALID_HANDLE;
++ #ifdef	DEBUG_SLOT_SELECTION
++ 	fprintf(stderr, "%s: myslot=%d optype=%d\n", PK11_DBG, myslot, optype);
++ #endif	/* DEBUG_SLOT_SELECTION */
++ 	rv = pFuncList->C_OpenSession(myslot, CKF_SERIAL_SESSION,
++ 		NULL_PTR, NULL_PTR, &sp->session);
++ 	if (rv == CKR_CRYPTOKI_NOT_INITIALIZED)
++ 		{
++ 		/*
++ 		 * We are probably a child process so force the
++ 		 * reinitialize of the session
++ 		 */
++ 		pk11_library_initialized = FALSE;
++ 		if (!pk11_library_init(NULL))
++ 			return (0);
++ 		rv = pFuncList->C_OpenSession(myslot, CKF_SERIAL_SESSION,
++ 			NULL_PTR, NULL_PTR, &sp->session);
++ 		}
++ 	if (rv != CKR_OK)
++ 		{
++ 		PK11err_add_data(PK11_F_SETUP_SESSION, PK11_R_OPENSESSION, rv);
++ 		return (0);
++ 		}
++ 
++ 	sp->pid = getpid();
++ 
++ 	switch (optype)
++ 		{
++ #ifndef OPENSSL_NO_RSA
++ 		case OP_RSA:
++ 			sp->opdata_rsa_pub_key = CK_INVALID_HANDLE;
++ 			sp->opdata_rsa_priv_key = CK_INVALID_HANDLE;
++ 			sp->opdata_rsa_pub = NULL;
++ 			sp->opdata_rsa_n_num = NULL;
++ 			sp->opdata_rsa_e_num = NULL;
++ 			sp->opdata_rsa_priv = NULL;
++ 			sp->opdata_rsa_d_num = NULL;
++ 			break;
++ #endif	/* OPENSSL_NO_RSA */
++ #ifndef OPENSSL_NO_DSA
++ 		case OP_DSA:
++ 			sp->opdata_dsa_pub_key = CK_INVALID_HANDLE;
++ 			sp->opdata_dsa_priv_key = CK_INVALID_HANDLE;
++ 			sp->opdata_dsa_pub = NULL;
++ 			sp->opdata_dsa_pub_num = NULL;
++ 			sp->opdata_dsa_priv = NULL;
++ 			sp->opdata_dsa_priv_num = NULL;
++ 			break;
++ #endif	/* OPENSSL_NO_DSA */
++ #ifndef OPENSSL_NO_DH
++ 		case OP_DH:
++ 			sp->opdata_dh_key = CK_INVALID_HANDLE;
++ 			sp->opdata_dh = NULL;
++ 			sp->opdata_dh_priv_num = NULL;
++ 			break;
++ #endif	/* OPENSSL_NO_DH */
++ 		case OP_CIPHER:
++ 			sp->opdata_cipher_key = CK_INVALID_HANDLE;
++ 			sp->opdata_encrypt = -1;
++ 			break;
++ 		}
++ 
++ 	/*
++ 	 * We always initialize the session as containing a non-persistent
++ 	 * object. The key load functions set it to persistent if that is so.
++ 	 */
++ 	sp->persistent = CK_FALSE;
++ 	return (1);
++ 	}
++ 
++ #ifndef OPENSSL_NO_RSA
++ /* Destroy RSA public key from single session. */
++ int
++ pk11_destroy_rsa_object_pub(PK11_SESSION *sp, CK_BBOOL uselock)
++ 	{
++ 	int ret = 0;
++ 
++ 	if (sp->opdata_rsa_pub_key != CK_INVALID_HANDLE)
++ 		{
++ 		TRY_OBJ_DESTROY(sp, sp->opdata_rsa_pub_key,
++ 		    ret, uselock, OP_RSA);
++ 		sp->opdata_rsa_pub_key = CK_INVALID_HANDLE;
++ 		sp->opdata_rsa_pub = NULL;
++ 		if (sp->opdata_rsa_n_num != NULL)
++ 			{
++ 			BN_free(sp->opdata_rsa_n_num);
++ 			sp->opdata_rsa_n_num = NULL;
++ 			}
++ 		if (sp->opdata_rsa_e_num != NULL)
++ 			{
++ 			BN_free(sp->opdata_rsa_e_num);
++ 			sp->opdata_rsa_e_num = NULL;
++ 			}
++ 		}
++ 
++ 	return (ret);
++ 	}
++ 
++ /* Destroy RSA private key from single session. */
++ int
++ pk11_destroy_rsa_object_priv(PK11_SESSION *sp, CK_BBOOL uselock)
++ 	{
++ 	int ret = 0;
++ 
++ 	if (sp->opdata_rsa_priv_key != CK_INVALID_HANDLE)
++ 		{
++ 		TRY_OBJ_DESTROY(sp, sp->opdata_rsa_priv_key,
++ 		    ret, uselock, OP_RSA);
++ 		sp->opdata_rsa_priv_key = CK_INVALID_HANDLE;
++ 		sp->opdata_rsa_priv = NULL;
++ 		if (sp->opdata_rsa_d_num != NULL)
++ 			{
++ 			BN_free(sp->opdata_rsa_d_num);
++ 			sp->opdata_rsa_d_num = NULL;
++ 			}
++ 
++ 		/*
++ 		 * For the RSA key by reference code, public components 'n'/'e'
++ 		 * are the key components we use to check for the cache hit. We
++ 		 * must free those as well.
++ 		 */
++ 		if (sp->opdata_rsa_n_num != NULL)
++ 			{
++ 			BN_free(sp->opdata_rsa_n_num);
++ 			sp->opdata_rsa_n_num = NULL;
++ 			}
++ 		if (sp->opdata_rsa_e_num != NULL)
++ 			{
++ 			BN_free(sp->opdata_rsa_e_num);
++ 			sp->opdata_rsa_e_num = NULL;
++ 			}
++ 		}
++ 
++ 	return (ret);
++ 	}
++ 
++ /*
++  * Destroy RSA key object wrapper. If session is NULL, try to destroy all
++  * objects in the free list.
++  */
++ int
++ pk11_destroy_rsa_key_objects(PK11_SESSION *session)
++ 	{
++ 	int ret = 1;
++ 	PK11_SESSION *sp = NULL;
++ 	PK11_SESSION *local_free_session;
++ 	CK_BBOOL uselock = TRUE;
++ 
++ 	if (session != NULL)
++ 		local_free_session = session;
++ 	else
++ 		{
++ 		(void) pthread_mutex_lock(session_cache[OP_RSA].lock);
++ 		local_free_session = session_cache[OP_RSA].head;
++ 		uselock = FALSE;
++ 		}
++ 
++ 	/*
++ 	 * go through the list of sessions and delete key objects
++ 	 */
++ 	while ((sp = local_free_session) != NULL)
++ 		{
++ 		local_free_session = sp->next;
++ 
++ 		/*
++ 		 * Do not terminate list traversal if one of the
++ 		 * destroy operations fails.
++ 		 */
++ 		if (pk11_destroy_rsa_object_pub(sp, uselock) == 0)
++ 			{
++ 			ret = 0;
++ 			continue;
++ 			}
++ 		if (pk11_destroy_rsa_object_priv(sp, uselock) == 0)
++ 			{
++ 			ret = 0;
++ 			continue;
++ 			}
++ 		}
++ 
++ 	if (session == NULL)
++ 		(void) pthread_mutex_unlock(session_cache[OP_RSA].lock);
++ 
++ 	return (ret);
++ 	}
++ #endif	/* OPENSSL_NO_RSA */
++ 
++ #ifndef OPENSSL_NO_DSA
++ /* Destroy DSA public key from single session. */
++ int
++ pk11_destroy_dsa_object_pub(PK11_SESSION *sp, CK_BBOOL uselock)
++ 	{
++ 	int ret = 0;
++ 
++ 	if (sp->opdata_dsa_pub_key != CK_INVALID_HANDLE)
++ 		{
++ 		TRY_OBJ_DESTROY(sp, sp->opdata_dsa_pub_key,
++ 		    ret, uselock, OP_DSA);
++ 		sp->opdata_dsa_pub_key = CK_INVALID_HANDLE;
++ 		sp->opdata_dsa_pub = NULL;
++ 		if (sp->opdata_dsa_pub_num != NULL)
++ 			{
++ 			BN_free(sp->opdata_dsa_pub_num);
++ 			sp->opdata_dsa_pub_num = NULL;
++ 			}
++ 		}
++ 
++ 	return (ret);
++ 	}
++ 
++ /* Destroy DSA private key from single session. */
++ int
++ pk11_destroy_dsa_object_priv(PK11_SESSION *sp, CK_BBOOL uselock)
++ 	{
++ 	int ret = 0;
++ 
++ 	if (sp->opdata_dsa_priv_key != CK_INVALID_HANDLE)
++ 		{
++ 		TRY_OBJ_DESTROY(sp, sp->opdata_dsa_priv_key,
++ 		    ret, uselock, OP_DSA);
++ 		sp->opdata_dsa_priv_key = CK_INVALID_HANDLE;
++ 		sp->opdata_dsa_priv = NULL;
++ 		if (sp->opdata_dsa_priv_num != NULL)
++ 			{
++ 			BN_free(sp->opdata_dsa_priv_num);
++ 			sp->opdata_dsa_priv_num = NULL;
++ 			}
++ 		}
++ 
++ 	return (ret);
++ 	}
++ 
++ /*
++  * Destroy DSA key object wrapper. If session is NULL, try to destroy all
++  * objects in the free list.
++  */
++ int
++ pk11_destroy_dsa_key_objects(PK11_SESSION *session)
++ 	{
++ 	int ret = 1;
++ 	PK11_SESSION *sp = NULL;
++ 	PK11_SESSION *local_free_session;
++ 	CK_BBOOL uselock = TRUE;
++ 
++ 	if (session != NULL)
++ 		local_free_session = session;
++ 	else
++ 		{
++ 		(void) pthread_mutex_lock(session_cache[OP_DSA].lock);
++ 		local_free_session = session_cache[OP_DSA].head;
++ 		uselock = FALSE;
++ 		}
++ 
++ 	/*
++ 	 * go through the list of sessions and delete key objects
++ 	 */
++ 	while ((sp = local_free_session) != NULL)
++ 		{
++ 		local_free_session = sp->next;
++ 
++ 		/*
++ 		 * Do not terminate list traversal if one of the
++ 		 * destroy operations fails.
++ 		 */
++ 		if (pk11_destroy_dsa_object_pub(sp, uselock) == 0)
++ 			{
++ 			ret = 0;
++ 			continue;
++ 			}
++ 		if (pk11_destroy_dsa_object_priv(sp, uselock) == 0)
++ 			{
++ 			ret = 0;
++ 			continue;
++ 			}
++ 		}
++ 
++ 	if (session == NULL)
++ 		(void) pthread_mutex_unlock(session_cache[OP_DSA].lock);
++ 
++ 	return (ret);
++ 	}
++ #endif	/* OPENSSL_NO_DSA */
++ 
++ #ifndef OPENSSL_NO_DH
++ /* Destroy DH key from single session. */
++ int
++ pk11_destroy_dh_object(PK11_SESSION *sp, CK_BBOOL uselock)
++ 	{
++ 	int ret = 0;
++ 
++ 	if (sp->opdata_dh_key != CK_INVALID_HANDLE)
++ 		{
++ 		TRY_OBJ_DESTROY(sp, sp->opdata_dh_key,
++ 		    ret, uselock, OP_DH);
++ 		sp->opdata_dh_key = CK_INVALID_HANDLE;
++ 		sp->opdata_dh = NULL;
++ 		if (sp->opdata_dh_priv_num != NULL)
++ 			{
++ 			BN_free(sp->opdata_dh_priv_num);
++ 			sp->opdata_dh_priv_num = NULL;
++ 			}
++ 		}
++ 
++ 	return (ret);
++ 	}
++ 
++ /*
++  * Destroy DH key object wrapper.
++  *
++  * arg0: pointer to PKCS#11 engine session structure
++  *       if session is NULL, try to destroy all objects in the free list
++  */
++ int
++ pk11_destroy_dh_key_objects(PK11_SESSION *session)
++ 	{
++ 	int ret = 1;
++ 	PK11_SESSION *sp = NULL;
++ 	PK11_SESSION *local_free_session;
++ 	CK_BBOOL uselock = TRUE;
++ 
++ 	if (session != NULL)
++ 		local_free_session = session;
++ 	else
++ 		{
++ 		(void) pthread_mutex_lock(session_cache[OP_DH].lock);
++ 		local_free_session = session_cache[OP_DH].head;
++ 		uselock = FALSE;
++ 		}
++ 
++ 	while ((sp = local_free_session) != NULL)
++ 		{
++ 		local_free_session = sp->next;
++ 
++ 		/*
++ 		 * Do not terminate list traversal if one of the
++ 		 * destroy operations fails.
++ 		 */
++ 		if (pk11_destroy_dh_object(sp, uselock) == 0)
++ 			{
++ 			ret = 0;
++ 			continue;
++ 			}
++ 		}
++ err:
++ 	if (session == NULL)
++ 		(void) pthread_mutex_unlock(session_cache[OP_DH].lock);
++ 
++ 	return (ret);
++ 	}
++ #endif	/* OPENSSL_NO_DH */
++ 
++ static int
++ pk11_destroy_object(CK_SESSION_HANDLE session, CK_OBJECT_HANDLE oh,
++     CK_BBOOL persistent)
++ 	{
++ 	CK_RV rv;
++ 
++ 	/*
++ 	 * We never try to destroy persistent objects which are the objects
++ 	 * stored in the keystore. Also, we always use read-only sessions so
++ 	 * C_DestroyObject() would be returning CKR_SESSION_READ_ONLY here.
++ 	 */
++ 	if (persistent == CK_TRUE)
++ 		return (1);
++ 
++ 	rv = pFuncList->C_DestroyObject(session, oh);
++ 	if (rv != CKR_OK)
++ 		{
++ 		PK11err_add_data(PK11_F_DESTROY_OBJECT, PK11_R_DESTROYOBJECT,
++ 		    rv);
++ 		return (0);
++ 		}
++ 
++ 	return (1);
++ 	}
++ 
++ 
++ /* Symmetric ciphers and digests support functions */
++ 
++ static int
++ cipher_nid_to_pk11(int nid)
++ 	{
++ 	int i;
++ 
++ 	for (i = 0; i < PK11_CIPHER_MAX; i++)
++ 		if (ciphers[i].nid == nid)
++ 			return (ciphers[i].id);
++ 	return (-1);
++ 	}
++ 
++ static int
++ pk11_usable_ciphers(const int **nids)
++ 	{
++ 	if (cipher_count > 0)
++ 		*nids = cipher_nids;
++ 	else
++ 		*nids = NULL;
++ 	return (cipher_count);
++ 	}
++ 
++ static int
++ pk11_usable_digests(const int **nids)
++ 	{
++ 	if (digest_count > 0)
++ 		*nids = digest_nids;
++ 	else
++ 		*nids = NULL;
++ 	return (digest_count);
++ 	}
++ 
++ /*
++  * Init context for encryption or decryption using a symmetric key.
++  */
++ static int pk11_init_symmetric(EVP_CIPHER_CTX *ctx, PK11_CIPHER *pcipher,
++ 	PK11_SESSION *sp, CK_MECHANISM_PTR pmech)
++ 	{
++ 	CK_RV rv;
++ #ifdef	SOLARIS_AES_CTR
++ 	CK_AES_CTR_PARAMS ctr_params;
++ #endif	/* SOLARIS_AES_CTR */
++ 
++ 	/*
++ 	 * We expect pmech->mechanism to be already set and
++ 	 * pParameter/ulParameterLen initialized to NULL/0 before
++ 	 * pk11_init_symetric() is called.
++ 	 */
++ 	OPENSSL_assert(pmech->mechanism != NULL);
++ 	OPENSSL_assert(pmech->pParameter == NULL);
++ 	OPENSSL_assert(pmech->ulParameterLen == 0);
++ 
++ #ifdef	SOLARIS_AES_CTR
++ 	if (ctx->cipher->nid == NID_aes_128_ctr ||
++ 	    ctx->cipher->nid == NID_aes_192_ctr ||
++ 	    ctx->cipher->nid == NID_aes_256_ctr)
++ 		{
++ 		pmech->pParameter = (void *)(&ctr_params);
++ 		pmech->ulParameterLen = sizeof (ctr_params);
++ 		/*
++ 		 * For now, we are limited to the fixed length of the counter,
++ 		 * it covers the whole counter block. That's what RFC 4344
++ 		 * needs. For more information on internal structure of the
++ 		 * counter block, see RFC 3686. If needed in the future, we can
++ 		 * add code so that the counter length can be set via
++ 		 * ENGINE_ctrl() function.
++ 		 */
++ 		ctr_params.ulCounterBits = AES_BLOCK_SIZE * 8;
++ 		OPENSSL_assert(pcipher->iv_len == AES_BLOCK_SIZE);
++ 		(void) memcpy(ctr_params.cb, ctx->iv, AES_BLOCK_SIZE);
++ 		}
++ 	else
++ #endif	/* SOLARIS_AES_CTR */
++ 		{
++ 		if (pcipher->iv_len > 0)
++ 			{
++ 			pmech->pParameter = (void *)ctx->iv;
++ 			pmech->ulParameterLen = pcipher->iv_len;
++ 			}
++ 		}
++ 
++ 	/* if we get here, the encryption needs to be reinitialized */
++ 	if (ctx->encrypt)
++ 		rv = pFuncList->C_EncryptInit(sp->session, pmech,
++ 			sp->opdata_cipher_key);
++ 	else
++ 		rv = pFuncList->C_DecryptInit(sp->session, pmech,
++ 			sp->opdata_cipher_key);
++ 
++ 	if (rv != CKR_OK)
++ 		{
++ 		PK11err_add_data(PK11_F_CIPHER_INIT, ctx->encrypt ?
++ 		    PK11_R_ENCRYPTINIT : PK11_R_DECRYPTINIT, rv);
++ 		pk11_return_session(sp, OP_CIPHER);
++ 		return (0);
++ 		}
++ 
++ 	return (1);
++ 	}
++ 
++ /* ARGSUSED */
++ static int
++ pk11_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
++     const unsigned char *iv, int enc)
++ 	{
++ 	CK_MECHANISM mech;
++ 	int index;
++ 	PK11_CIPHER_STATE *state = (PK11_CIPHER_STATE *) ctx->cipher_data;
++ 	PK11_SESSION *sp;
++ 	PK11_CIPHER *p_ciph_table_row;
++ 
++ 	state->sp = NULL;
++ 
++ 	index = cipher_nid_to_pk11(ctx->cipher->nid);
++ 	if (index < 0 || index >= PK11_CIPHER_MAX)
++ 		return (0);
++ 
++ 	p_ciph_table_row = &ciphers[index];
++ 	/*
++ 	 * iv_len in the ctx->cipher structure is the maximum IV length for the
++ 	 * current cipher and it must be less or equal to the IV length in our
++ 	 * ciphers table. The key length must be in the allowed interval. From
++ 	 * all cipher modes that the PKCS#11 engine supports only RC4 allows a
++ 	 * key length to be in some range, all other NIDs have a precise key
++ 	 * length. Every application can define its own EVP functions so this
++ 	 * code serves as a sanity check.
++ 	 *
++ 	 * Note that the reason why the IV length in ctx->cipher might be
++ 	 * greater than the actual length is that OpenSSL uses BLOCK_CIPHER_defs
++ 	 * macro to define functions that return EVP structures for all DES
++ 	 * modes. So, even ECB modes get 8 byte IV.
++ 	 */
++ 	if (ctx->cipher->iv_len < p_ciph_table_row->iv_len ||
++ 	    ctx->key_len < p_ciph_table_row->min_key_len ||
++ 	    ctx->key_len > p_ciph_table_row->max_key_len) {
++ 		PK11err(PK11_F_CIPHER_INIT, PK11_R_KEY_OR_IV_LEN_PROBLEM);
++ 		return (0);
++ 	}
++ 
++ 	if ((sp = pk11_get_session(OP_CIPHER)) == NULL)
++ 		return (0);
++ 
++ 	/* if applicable, the mechanism parameter is used for IV */
++ 	mech.mechanism = p_ciph_table_row->mech_type;
++ 	mech.pParameter = NULL;
++ 	mech.ulParameterLen = 0;
++ 
++ 	/* The key object is destroyed here if it is not the current key. */
++ 	(void) check_new_cipher_key(sp, key, ctx->key_len);
++ 
++ 	/*
++ 	 * If the key is the same and the encryption is also the same, then
++ 	 * just reuse it. However, we must not forget to reinitialize the
++ 	 * context that was finalized in pk11_cipher_cleanup().
++ 	 */
++ 	if (sp->opdata_cipher_key != CK_INVALID_HANDLE &&
++ 	    sp->opdata_encrypt == ctx->encrypt)
++ 		{
++ 		state->sp = sp;
++ 		if (pk11_init_symmetric(ctx, p_ciph_table_row, sp, &mech) == 0)
++ 			return (0);
++ 
++ 		return (1);
++ 		}
++ 
++ 	/*
++ 	 * Check if the key has been invalidated. If so, a new key object
++ 	 * needs to be created.
++ 	 */
++ 	if (sp->opdata_cipher_key == CK_INVALID_HANDLE)
++ 		{
++ 		sp->opdata_cipher_key = pk11_get_cipher_key(
++ 			ctx, key, p_ciph_table_row->key_type, sp);
++ 		}
++ 
++ 	if (sp->opdata_encrypt != ctx->encrypt && sp->opdata_encrypt != -1)
++ 		{
++ 		/*
++ 		 * The previous encryption/decryption is different. Need to
++ 		 * terminate the previous * active encryption/decryption here.
++ 		 */
++ 		if (!pk11_cipher_final(sp))
++ 			{
++ 			pk11_return_session(sp, OP_CIPHER);
++ 			return (0);
++ 			}
++ 		}
++ 
++ 	if (sp->opdata_cipher_key == CK_INVALID_HANDLE)
++ 		{
++ 		pk11_return_session(sp, OP_CIPHER);
++ 		return (0);
++ 		}
++ 
++ 	/* now initialize the context with a new key */

@@ Diff output truncated at 100000 characters. @@

This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.


More information about the devel mailing list