1Index: openssl/Configure 2diff -u openssl/Configure:1.8.6.1 openssl/Configure:1.8 3--- openssl/Configure:1.8.6.1 Sun Jan 15 15:45:33 2012 4+++ openssl/Configure Mon Jun 13 14:25:15 2011 5@@ -12,7 +12,7 @@ 6 7 # see INSTALL for instructions. 8 9-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"; 10+my $usage="Usage: Configure --pk11-libname=PK11_LIB_LOCATION --pk11-flavor=FLAVOR [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"; 11 12 # Options: 13 # 14@@ -25,6 +25,12 @@ 15 # default). This needn't be set in advance, you can 16 # just as well use "make INSTALL_PREFIX=/whatever install". 17 # 18+# --pk11-libname PKCS#11 library name. 19+# (No default) 20+# 21+# --pk11-flavor either crypto-accelerator or sign-only 22+# (No default) 23+# 24 # --with-krb5-dir Declare where Kerberos 5 lives. The libraries are expected 25 # to live in the subdirectory lib/ and the header files in 26 # include/. A value is required. 27@@ -335,7 +341,7 @@ 28 "linux-ppc", "gcc:-DB_ENDIAN -DTERMIO -O3 -Wall::-D_REENTRANT::-ldl:BN_LLONG RC4_CHAR RC4_CHUNK DES_RISC1 DES_UNROLL::linux_ppc32.o::::::::::dlfcn:linux-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", 29 #### IA-32 targets... 30 "linux-ia32-icc", "icc:-DL_ENDIAN -DTERMIO -O2 -no_cpprt::-D_REENTRANT::-ldl:BN_LLONG ${x86_gcc_des} ${x86_gcc_opts}:${x86_elf_asm}:dlfcn:linux-shared:-KPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", 31-"linux-elf", "gcc:-DL_ENDIAN -DTERMIO -O3 -fomit-frame-pointer -Wall::-D_REENTRANT::-ldl:BN_LLONG ${x86_gcc_des} ${x86_gcc_opts}:${x86_elf_asm}:dlfcn:linux-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", 32+"linux-elf", "gcc:-DL_ENDIAN -DTERMIO -O3 -fomit-frame-pointer -Wall::-D_REENTRANT -pthread::-ldl:BN_LLONG ${x86_gcc_des} ${x86_gcc_opts}:${x86_elf_asm}:dlfcn:linux-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", 33 "linux-aout", "gcc:-DL_ENDIAN -DTERMIO -O3 -fomit-frame-pointer -march=i486 -Wall::(unknown):::BN_LLONG ${x86_gcc_des} ${x86_gcc_opts}:${x86_out_asm}", 34 #### 35 "linux-generic64","gcc:-DTERMIO -O3 -Wall::-D_REENTRANT::-ldl:SIXTY_FOUR_BIT_LONG RC4_CHAR RC4_CHUNK DES_INT DES_UNROLL BF_PTR:${no_asm}:dlfcn:linux-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", 36@@ -343,7 +349,7 @@ 37 "linux-ia64", "gcc:-DL_ENDIAN -DTERMIO -O3 -Wall::-D_REENTRANT::-ldl:SIXTY_FOUR_BIT_LONG RC4_CHUNK:${ia64_asm}:dlfcn:linux-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", 38 "linux-ia64-ecc","ecc:-DL_ENDIAN -DTERMIO -O2 -Wall -no_cpprt::-D_REENTRANT::-ldl:SIXTY_FOUR_BIT_LONG RC4_CHUNK:${ia64_asm}:dlfcn:linux-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", 39 "linux-ia64-icc","icc:-DL_ENDIAN -DTERMIO -O2 -Wall -no_cpprt::-D_REENTRANT::-ldl:SIXTY_FOUR_BIT_LONG RC4_CHUNK:${ia64_asm}:dlfcn:linux-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", 40-"linux-x86_64", "gcc:-m64 -DL_ENDIAN -DTERMIO -O3 -Wall -DMD32_REG_T=int::-D_REENTRANT::-ldl:SIXTY_FOUR_BIT_LONG RC4_CHUNK DES_INT DES_UNROLL:${x86_64_asm}:dlfcn:linux-shared:-fPIC:-m64:.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", 41+"linux-x86_64", "gcc:-m64 -DL_ENDIAN -DTERMIO -O3 -Wall -DMD32_REG_T=int::-D_REENTRANT -pthread::-ldl:SIXTY_FOUR_BIT_LONG RC4_CHUNK DES_INT DES_UNROLL:${x86_64_asm}:dlfcn:linux-shared:-fPIC:-m64:.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", 42 #### SPARC Linux setups 43 # Ray Miller <ray.miller@computing-services.oxford.ac.uk> has patiently 44 # assisted with debugging of following two configs. 45@@ -590,6 +596,10 @@ 46 my $idx_ranlib = $idx++; 47 my $idx_arflags = $idx++; 48 49+# PKCS#11 engine patch 50+my $pk11_libname=""; 51+my $pk11_flavor=""; 52+ 53 my $prefix=""; 54 my $libdir=""; 55 my $openssldir=""; 56@@ -828,6 +838,14 @@ 57 { 58 $flags.=$_." "; 59 } 60+ elsif (/^--pk11-libname=(.*)$/) 61+ { 62+ $pk11_libname=$1; 63+ } 64+ elsif (/^--pk11-flavor=(.*)$/) 65+ { 66+ $pk11_flavor=$1; 67+ } 68 elsif (/^--prefix=(.*)$/) 69 { 70 $prefix=$1; 71@@ -963,6 +981,22 @@ 72 exit 0; 73 } 74 75+if (! $pk11_libname) 76+ { 77+ print STDERR "You must set --pk11-libname for PKCS#11 library.\n"; 78+ print STDERR "See README.pkcs11 for more information.\n"; 79+ exit 1; 80+ } 81+ 82+if (! $pk11_flavor 83+ || !($pk11_flavor eq "crypto-accelerator" || $pk11_flavor eq "sign-only")) 84+ { 85+ print STDERR "You must set --pk11-flavor.\n"; 86+ print STDERR "Choices are crypto-accelerator and sign-only.\n"; 87+ print STDERR "See README.pkcs11 for more information.\n"; 88+ exit 1; 89+ } 90+ 91 if ($target =~ m/^CygWin32(-.*)$/) { 92 $target = "Cygwin".$1; 93 } 94@@ -1078,6 +1112,25 @@ 95 print "\n"; 96 } 97 98+if ($pk11_flavor eq "crypto-accelerator") 99+ { 100+ $openssl_other_defines .= "#define OPENSSL_NO_HW_PKCS11SO\n"; 101+ $default_depflags .= " -DOPENSSL_NO_HW_PKCS11SO"; 102+ $depflags .= " -DOPENSSL_NO_HW_PKCS11SO"; 103+ $options .= " no-hw-pkcs11so"; 104+ print " no-hw-pkcs11so [pk11-flavor]"; 105+ print " OPENSSL_NO_HW_PKCS11SO\n"; 106+ } 107+else 108+ { 109+ $openssl_other_defines .= "#define OPENSSL_NO_HW_PKCS11CA\n"; 110+ $default_depflags .= " -DOPENSSL_NO_HW_PKCS11CA"; 111+ $depflags .= " -DOPENSSL_NO_HW_PKCS11CA"; 112+ $options .= " no-hw-pkcs11ca"; 113+ print " no-hw-pkcs11ca [pk11-flavor]"; 114+ print " OPENSSL_NO_HW_PKCS11CA\n"; 115+} 116+ 117 my $IsMK1MF=scalar grep /^$target$/,@MK1MF_Builds; 118 119 $IsMK1MF=1 if ($target eq "mingw" && $^O ne "cygwin" && !is_msys()); 120@@ -1129,6 +1182,8 @@ 121 if ($flags ne "") { $cflags="$flags$cflags"; } 122 else { $no_user_cflags=1; } 123 124+$cflags="-DPK11_LIB_LOCATION=\"$pk11_libname\" $cflags"; 125+ 126 # Kerberos settings. The flavor must be provided from outside, either through 127 # the script "config" or manually. 128 if (!$no_krb5) 129@@ -1492,6 +1547,7 @@ 130 s/^VERSION=.*/VERSION=$version/; 131 s/^MAJOR=.*/MAJOR=$major/; 132 s/^MINOR=.*/MINOR=$minor/; 133+ s/^PK11_LIB_LOCATION=.*/PK11_LIB_LOCATION=$pk11_libname/; 134 s/^SHLIB_VERSION_NUMBER=.*/SHLIB_VERSION_NUMBER=$shlib_version_number/; 135 s/^SHLIB_VERSION_HISTORY=.*/SHLIB_VERSION_HISTORY=$shlib_version_history/; 136 s/^SHLIB_MAJOR=.*/SHLIB_MAJOR=$shlib_major/; 137Index: openssl/Makefile.org 138diff -u openssl/Makefile.org:1.4.6.1 openssl/Makefile.org:1.4 139--- openssl/Makefile.org:1.4.6.1 Sun Jan 15 15:45:33 2012 140+++ openssl/Makefile.org Mon Jun 13 14:25:15 2011 141@@ -26,6 +26,9 @@ 142 INSTALL_PREFIX= 143 INSTALLTOP=/usr/local/ssl 144 145+# You must set this through --pk11-libname configure option. 146+PK11_LIB_LOCATION= 147+ 148 # Do not edit this manually. Use Configure --openssldir=DIR do change this! 149 OPENSSLDIR=/usr/local/ssl 150 151Index: openssl/README.pkcs11 152diff -u /dev/null openssl/README.pkcs11:1.6.4.1 153--- /dev/null Mon Jan 16 18:53:41 2012 154+++ openssl/README.pkcs11 Mon Jun 13 18:27:39 2011 155@@ -0,0 +1,261 @@ 156+ISC modified 157+============ 158+ 159+The previous key naming scheme was kept for backward compatibility. 160+ 161+The PKCS#11 engine exists in two flavors, crypto-accelerator and 162+sign-only. The first one is from the Solaris patch and uses the 163+PKCS#11 device for all crypto operations it supports. The second 164+is a stripped down version which provides only the useful 165+function (i.e., signature with a RSA private key in the device 166+protected key store and key loading). 167+ 168+As a hint PKCS#11 boards should use the crypto-accelerator flavor, 169+external PKCS#11 devices the sign-only. SCA 6000 is an example 170+of the first, AEP Keyper of the second. 171+ 172+Note it is mandatory to set a pk11-flavor (and only one) in 173+config/Configure. 174+ 175+PKCS#11 engine support for OpenSSL 0.9.8l 176+========================================= 177+ 178+[Nov 19, 2009] 179+ 180+Contents: 181+ 182+Overview 183+Revisions of the patch for 0.9.8 branch 184+FAQs 185+Feedback 186+ 187+Overview 188+======== 189+ 190+This patch containing code available in OpenSolaris adds support for PKCS#11 191+engine into OpenSSL and implements PKCS#11 v2.20. It is to be applied against 192+OpenSSL 0.9.8l source code distribution as shipped by OpenSSL.Org. Your system 193+must provide PKCS#11 backend otherwise the patch is useless. You provide the 194+PKCS#11 library name during the build configuration phase, see below. 195+ 196+Patch can be applied like this: 197+ 198+ # NOTE: use gtar if on Solaris 199+ tar xfzv openssl-0.9.8l.tar.gz 200+ # now download the patch to the current directory 201+ # ... 202+ cd openssl-0.9.8l 203+ # NOTE: must use gpatch if on Solaris (is part of the system) 204+ patch -p1 < path-to/pkcs11_engine-0.9.8l.patch.2009-11-19 205+ 206+It is designed to support pure acceleration for RSA, DSA, DH and all the 207+symetric ciphers and message digest algorithms that PKCS#11 and OpenSSL share 208+except for missing support for patented algorithms MDC2, RC3, RC5 and IDEA. 209+ 210+According to the PKCS#11 providers installed on your machine, it can support 211+following mechanisms: 212+ 213+ RSA, DSA, DH, RAND, DES-CBC, DES-EDE3-CBC, DES-ECB, DES-EDE3, RC4, 214+ AES-128-CBC, AES-192-CBC, AES-256-CBC, AES-128-ECB, AES-192-ECB, 215+ AES-256-ECB, AES-128-CTR, AES-192-CTR, AES-256-CTR, MD5, SHA1, SHA224, 216+ SHA256, SHA384, SHA512 217+ 218+Note that for AES counter mode the application must provide their own EVP 219+functions since OpenSSL doesn't support counter mode through EVP yet. You may 220+see OpenSSH source code (cipher.c) to get the idea how to do that. SunSSH is an 221+example of code that uses the PKCS#11 engine and deals with the fork-safety 222+problem (see engine.c and packet.c files if interested). 223+ 224+You must provide the location of PKCS#11 library in your system to the 225+configure script. You will be instructed to do that when you try to run the 226+config script: 227+ 228+ $ ./config 229+ Operating system: i86pc-whatever-solaris2 230+ Configuring for solaris-x86-cc 231+ You must set --pk11-libname for PKCS#11 library. 232+ See README.pkcs11 for more information. 233+ 234+Taking openCryptoki project on Linux AMD64 box as an example, you would run 235+configure script like this: 236+ 237+ ./config --pk11-libname=/usr/lib64/pkcs11/PKCS11_API.so 238+ 239+To check whether newly built openssl really supports PKCS#11 it's enough to run 240+"apps/openssl engine" and look for "(pkcs11) PKCS #11 engine support" in the 241+output. If you see no PKCS#11 engine support check that the built openssl binary 242+and the PKCS#11 library from --pk11-libname don't conflict on 32/64 bits. 243+ 244+The patch, during various phases of development, was tested on Solaris against 245+PKCS#11 engine available from Solaris Cryptographic Framework (Solaris 10 and 246+OpenSolaris) and also on Linux using PKCS#11 libraries from openCryptoki project 247+(see openCryptoki website http://sourceforge.net/projects/opencryptoki for more 248+information). Some Linux distributions even ship those libraries with the 249+system. The patch should work on any system that is supported by OpenSSL itself 250+and has functional PKCS#11 library. 251+ 252+The patch contains "RSA Security Inc. PKCS #11 Cryptographic Token Interface 253+(Cryptoki)" - files cryptoki.h, pkcs11.h, pkcs11f.h and pkcs11t.h which are 254+copyrighted by RSA Security Inc., see pkcs11.h for more information. 255+ 256+Other added/modified code in this patch is copyrighted by Sun Microsystems, 257+Inc. and is released under the OpenSSL license (see LICENSE file for more 258+information). 259+ 260+Revisions of the patch for 0.9.8 branch 261+======================================= 262+ 263+2009-11-19 264+- adjusted for OpenSSL version 0.9.8l 265+ 266+- bugs and RFEs: 267+ 268+ 6479874 OpenSSL should support RSA key by reference/hardware keystores 269+ 6896677 PKCS#11 engine's hw_pk11_err.h needs to be split 270+ 6732677 make check to trigger Solaris specific code automatic in the 271+ PKCS#11 engine 272+ 273+2009-03-11 274+- adjusted for OpenSSL version 0.9.8j 275+ 276+- README.pkcs11 moved out of the patch, and is shipped together with it in a 277+ tarball instead so that it can be read before the patch is applied. 278+ 279+- fixed bugs: 280+ 281+ 6804216 pkcs#11 engine should support a key length range for RC4 282+ 6734038 Apache SSL web server using the pkcs11 engine fails to start if 283+ meta slot is disabled 284+ 285+2008-12-02 286+- fixed bugs and RFEs (most of the work done by Vladimir Kotal) 287+ 288+ 6723504 more granular locking in PKCS#11 engine 289+ 6667128 CRYPTO_LOCK_PK11_ENGINE assumption does not hold true 290+ 6710420 PKCS#11 engine source should be lint clean 291+ 6747327 PKCS#11 engine atfork handlers need to be aware of guys who take 292+ it seriously 293+ 6746712 PKCS#11 engine source code should be cstyle clean 294+ 6731380 return codes of several functions are not checked in the PKCS#11 295+ engine code 296+ 6746735 PKCS#11 engine should use extended FILE space API 297+ 6734038 Apache SSL web server using the pkcs11 engine fails to start if 298+ meta slot is disabled 299+ 300+2008-08-01 301+- fixed bug 302+ 303+ 6731839 OpenSSL PKCS#11 engine no longer uses n2cp for symmetric ciphers 304+ and digests 305+ 306+- Solaris specific code for slot selection made automatic 307+ 308+2008-07-29 309+- update the patch to OpenSSL 0.9.8h version 310+- pkcs11t.h updated to the latest version: 311+ 312+ 6545665 make CKM_AES_CTR available to non-kernel users 313+ 314+- fixed bugs in the engine code: 315+ 316+ 6602801 PK11_SESSION cache has to employ reference counting scheme for 317+ asymmetric key operations 318+ 6605538 pkcs11 functions C_FindObjects[{Init,Final}]() not called 319+ atomically 320+ 6607307 pkcs#11 engine can't read RSA private keys 321+ 6652362 pk11_RSA_finish() is cutting corners 322+ 6662112 pk11_destroy_{rsa,dsa,dh}_key_objects() use locking in 323+ suboptimal way 324+ 6666625 pk11_destroy_{rsa,dsa,dh}_key_objects() should be more 325+ resilient to destroy failures 326+ 6667273 OpenSSL engine should not use free() but OPENSSL_free() 327+ 6670363 PKCS#11 engine fails to reuse existing symmetric keys 328+ 6678135 memory corruption in pk11_DH_generate_key() in pkcs#11 engine 329+ 6678503 DSA signature conversion in pk11_dsa_do_verify() ignores size 330+ of big numbers leading to failures 331+ 6706562 pk11_DH_compute_key() returns 0 in case of failure instead of 332+ -1 333+ 6706622 pk11_load_{pub,priv}key create corrupted RSA key references 334+ 6707129 return values from BN_new() in pk11_DH_generate_key() are not 335+ checked 336+ 6707274 DSA/RSA/DH PKCS#11 engine operations need to be resistant to 337+ structure reuse 338+ 6707782 OpenSSL PKCS#11 engine pretends to be aware of 339+ OPENSSL_NO_{RSA,DSA,DH} 340+ defines but fails miserably 341+ 6709966 make check_new_*() to return values to indicate cache hit/miss 342+ 6705200 pk11_dh struct initialization in PKCS#11 engine is missing 343+ generate_params parameter 344+ 6709513 PKCS#11 engine sets IV length even for ECB modes 345+ 6728296 buffer length not initialized for C_(En|De)crypt_Final() in the 346+ PKCS#11 engine 347+ 6728871 PKCS#11 engine must reset global_session in pk11_finish() 348+ 349+- new features and enhancements: 350+ 351+ 6562155 OpenSSL pkcs#11 engine needs support for SHA224/256/384/512 352+ 6685012 OpenSSL pkcs#11 engine needs support for new cipher modes 353+ 6725903 OpenSSL PKCS#11 engine shouldn't use soft token for symmetric 354+ ciphers and digests 355+ 356+2007-10-15 357+- update for 0.9.8f version 358+- update for "6607670 teach pkcs#11 engine how to use keys be reference" 359+ 360+2007-10-02 361+- draft for "6607670 teach pkcs#11 engine how to use keys be reference" 362+- draft for "6607307 pkcs#11 engine can't read RSA private keys" 363+ 364+2007-09-26 365+- 6375348 Using pkcs11 as the SSLCryptoDevice with Apache/OpenSSL causes 366+ significant performance drop 367+- 6573196 memory is leaked when OpenSSL is used with PKCS#11 engine 368+ 369+2007-05-25 370+- 6558630 race in OpenSSL pkcs11 engine when using symetric block ciphers 371+ 372+2007-05-19 373+- initial patch for 0.9.8e using latest OpenSolaris code 374+ 375+FAQs 376+==== 377+ 378+(1) my build failed on Linux distro with this error: 379+ 380+../libcrypto.a(hw_pk11.o): In function `pk11_library_init': 381+hw_pk11.c:(.text+0x20f5): undefined reference to `pthread_atfork' 382+ 383+Answer: 384+ 385+ - don't use "no-threads" when configuring 386+ - if you didn't then OpenSSL failed to create a threaded library by 387+ default. You may manually edit Configure and try again. Look for the 388+ architecture that Configure printed, for example: 389+ 390+Configured for linux-elf. 391+ 392+ - then edit Configure, find string "linux-elf" (inluding the quotes), 393+ and add flags to support threads to the 4th column of the 2nd string. 394+ If you build with GCC then adding "-pthread" should be enough. With 395+ "linux-elf" as an example, you would add " -pthread" right after 396+ "-D_REENTRANT", like this: 397+ 398+....-O3 -fomit-frame-pointer -Wall::-D_REENTRANT -pthread::-ldl:..... 399+ 400+(2) I'm using MinGW/MSYS environment and get undeclared reference error for 401+pthread_atfork() function when trying to build OpenSSL with the patch. 402+ 403+Answer: 404+ 405+ Sorry, pthread_atfork() is not implemented in the current pthread-win32 406+ (as of Nov 2009). You can not use the patch there. 407+ 408+ 409+Feedback 410+======== 411+ 412+Please send feedback to security-discuss@opensolaris.org. The patch was 413+created by Jan.Pechanec@Sun.COM from code available in OpenSolaris. 414+ 415+Latest version should be always available on http://blogs.sun.com/janp. 416+ 417Index: openssl/crypto/opensslconf.h 418diff -u openssl/crypto/opensslconf.h:1.5.10.1 openssl/crypto/opensslconf.h:1.5 419--- openssl/crypto/opensslconf.h:1.5.10.1 Sun Jan 15 15:45:34 2012 420+++ openssl/crypto/opensslconf.h Fri Sep 4 10:43:21 2009 421@@ -38,6 +38,9 @@ 422 423 #endif /* OPENSSL_DOING_MAKEDEPEND */ 424 425+#ifndef OPENSSL_THREADS 426+# define OPENSSL_THREADS 427+#endif 428 #ifndef OPENSSL_NO_DYNAMIC_ENGINE 429 # define OPENSSL_NO_DYNAMIC_ENGINE 430 #endif 431@@ -79,6 +82,8 @@ 432 # endif 433 #endif 434 435+#define OPENSSL_CPUID_OBJ 436+ 437 /* crypto/opensslconf.h.in */ 438 439 #ifdef OPENSSL_DOING_MAKEDEPEND 440@@ -140,7 +145,7 @@ 441 * This enables code handling data aligned at natural CPU word 442 * boundary. See crypto/rc4/rc4_enc.c for further details. 443 */ 444-#undef RC4_CHUNK 445+#define RC4_CHUNK unsigned long 446 #endif 447 #endif 448 449@@ -148,7 +153,7 @@ 450 /* If this is set to 'unsigned int' on a DEC Alpha, this gives about a 451 * %20 speed up (longs are 8 bytes, int's are 4). */ 452 #ifndef DES_LONG 453-#define DES_LONG unsigned long 454+#define DES_LONG unsigned int 455 #endif 456 #endif 457 458@@ -162,9 +167,9 @@ 459 /* The prime number generation stuff may not work when 460 * EIGHT_BIT but I don't care since I've only used this mode 461 * for debuging the bignum libraries */ 462-#undef SIXTY_FOUR_BIT_LONG 463+#define SIXTY_FOUR_BIT_LONG 464 #undef SIXTY_FOUR_BIT 465-#define THIRTY_TWO_BIT 466+#undef THIRTY_TWO_BIT 467 #undef SIXTEEN_BIT 468 #undef EIGHT_BIT 469 #endif 470@@ -178,7 +183,7 @@ 471 472 #if defined(HEADER_BF_LOCL_H) && !defined(CONFIG_HEADER_BF_LOCL_H) 473 #define CONFIG_HEADER_BF_LOCL_H 474-#undef BF_PTR 475+#define BF_PTR2 476 #endif /* HEADER_BF_LOCL_H */ 477 478 #if defined(HEADER_DES_LOCL_H) && !defined(CONFIG_HEADER_DES_LOCL_H) 479@@ -208,7 +213,7 @@ 480 /* Unroll the inner loop, this sometimes helps, sometimes hinders. 481 * Very mucy CPU dependant */ 482 #ifndef DES_UNROLL 483-#undef DES_UNROLL 484+#define DES_UNROLL 485 #endif 486 487 /* These default values were supplied by 488Index: openssl/crypto/bio/bss_file.c 489diff -u openssl/crypto/bio/bss_file.c:1.5.6.1 openssl/crypto/bio/bss_file.c:1.5 490--- openssl/crypto/bio/bss_file.c:1.5.6.1 Sun Jan 15 15:45:35 2012 491+++ openssl/crypto/bio/bss_file.c Mon Jun 13 14:25:17 2011 492@@ -125,7 +125,7 @@ 493 { 494 SYSerr(SYS_F_FOPEN,get_last_sys_error()); 495 ERR_add_error_data(5,"fopen('",filename,"','",mode,"')"); 496- if (errno == ENOENT) 497+ if ((errno == ENOENT) || ((*mode == 'r') && (errno == EACCES))) 498 BIOerr(BIO_F_BIO_NEW_FILE,BIO_R_NO_SUCH_FILE); 499 else 500 BIOerr(BIO_F_BIO_NEW_FILE,ERR_R_SYS_LIB); 501Index: openssl/crypto/engine/Makefile 502diff -u openssl/crypto/engine/Makefile:1.6.6.1 openssl/crypto/engine/Makefile:1.6 503--- openssl/crypto/engine/Makefile:1.6.6.1 Sun Jan 15 15:45:35 2012 504+++ openssl/crypto/engine/Makefile Mon Jun 13 14:25:19 2011 505@@ -21,12 +21,14 @@ 506 eng_table.c eng_pkey.c eng_fat.c eng_all.c \ 507 tb_rsa.c tb_dsa.c tb_ecdsa.c tb_dh.c tb_ecdh.c tb_rand.c tb_store.c \ 508 tb_cipher.c tb_digest.c \ 509- eng_openssl.c eng_cnf.c eng_dyn.c eng_cryptodev.c eng_padlock.c 510+ eng_openssl.c eng_cnf.c eng_dyn.c eng_cryptodev.c eng_padlock.c \ 511+ hw_pk11.c hw_pk11_pub.c hw_pk11so.c hw_pk11so_pub.c 512 LIBOBJ= eng_err.o eng_lib.o eng_list.o eng_init.o eng_ctrl.o \ 513 eng_table.o eng_pkey.o eng_fat.o eng_all.o \ 514 tb_rsa.o tb_dsa.o tb_ecdsa.o tb_dh.o tb_ecdh.o tb_rand.o tb_store.o \ 515 tb_cipher.o tb_digest.o \ 516- eng_openssl.o eng_cnf.o eng_dyn.o eng_cryptodev.o eng_padlock.o 517+ eng_openssl.o eng_cnf.o eng_dyn.o eng_cryptodev.o eng_padlock.o \ 518+ hw_pk11.o hw_pk11_pub.o hw_pk11so.o hw_pk11so_pub.o 519 520 SRC= $(LIBSRC) 521 522@@ -288,6 +290,102 @@ 523 eng_table.o: ../../include/openssl/symhacks.h ../../include/openssl/x509.h 524 eng_table.o: ../../include/openssl/x509_vfy.h ../cryptlib.h eng_int.h 525 eng_table.o: eng_table.c 526+hw_pk11.o: ../../include/openssl/e_os2.h ../../include/openssl/opensslconf.h 527+hw_pk11.o: ../../include/openssl/engine.h ../../include/openssl/ossl_typ.h 528+hw_pk11.o: ../../include/openssl/bn.h ../../include/openssl/rsa.h 529+hw_pk11.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h 530+hw_pk11.o: ../../include/openssl/crypto.h ../../include/openssl/stack.h 531+hw_pk11.o: ../../include/openssl/safestack.h ../../include/openssl/opensslv.h 532+hw_pk11.o: ../../include/openssl/symhacks.h ../../include/openssl/dsa.h 533+hw_pk11.o: ../../include/openssl/dh.h ../../include/openssl/rand.h 534+hw_pk11.o: ../../include/openssl/ui.h ../../include/openssl/err.h 535+hw_pk11.o: ../../include/openssl/lhash.h ../../include/openssl/dso.h 536+hw_pk11.o: ../../include/openssl/pem.h ../../include/openssl/evp.h 537+hw_pk11.o: ../../include/openssl/md2.h ../../include/openssl/md4.h 538+hw_pk11.o: ../../include/openssl/md5.h ../../include/openssl/sha.h 539+hw_pk11.o: ../../include/openssl/ripemd.h ../../include/openssl/des.h 540+hw_pk11.o: ../../include/openssl/des_old.h ../../include/openssl/ui_compat.h 541+hw_pk11.o: ../../include/openssl/rc4.h ../../include/openssl/rc2.h 542+hw_pk11.o: ../../crypto/rc5/rc5.h ../../include/openssl/blowfish.h 543+hw_pk11.o: ../../include/openssl/cast.h ../../include/openssl/idea.h 544+hw_pk11.o: ../../crypto/mdc2/mdc2.h ../../include/openssl/aes.h 545+hw_pk11.o: ../../include/openssl/objects.h ../../include/openssl/obj_mac.h 546+hw_pk11.o: ../../include/openssl/x509.h ../../include/openssl/buffer.h 547+hw_pk11.o: ../../include/openssl/x509_vfy.h ../../include/openssl/pkcs7.h 548+hw_pk11.o: ../../include/openssl/pem2.h ../cryptlib.h 549+hw_pk11.o: ../../e_os.h hw_pk11_err.c hw_pk11_err.h hw_pk11.c 550+hw_pk11_pub.o: ../../include/openssl/e_os2.h ../../include/openssl/opensslconf.h 551+hw_pk11_pub.o: ../../include/openssl/engine.h ../../include/openssl/ossl_typ.h 552+hw_pk11_pub.o: ../../include/openssl/bn.h ../../include/openssl/rsa.h 553+hw_pk11_pub.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h 554+hw_pk11_pub.o: ../../include/openssl/crypto.h ../../include/openssl/stack.h 555+hw_pk11_pub.o: ../../include/openssl/safestack.h ../../include/openssl/opensslv.h 556+hw_pk11_pub.o: ../../include/openssl/symhacks.h ../../include/openssl/dsa.h 557+hw_pk11_pub.o: ../../include/openssl/dh.h ../../include/openssl/rand.h 558+hw_pk11_pub.o: ../../include/openssl/ui.h ../../include/openssl/err.h 559+hw_pk11_pub.o: ../../include/openssl/lhash.h ../../include/openssl/dso.h 560+hw_pk11_pub.o: ../../include/openssl/pem.h ../../include/openssl/evp.h 561+hw_pk11_pub.o: ../../include/openssl/md2.h ../../include/openssl/md4.h 562+hw_pk11_pub.o: ../../include/openssl/md5.h ../../include/openssl/sha.h 563+hw_pk11_pub.o: ../../include/openssl/ripemd.h ../../include/openssl/des.h 564+hw_pk11_pub.o: ../../include/openssl/des_old.h ../../include/openssl/ui_compat.h 565+hw_pk11_pub.o: ../../include/openssl/rc4.h ../../include/openssl/rc2.h 566+hw_pk11_pub.o: ../../crypto/rc5/rc5.h ../../include/openssl/blowfish.h 567+hw_pk11_pub.o: ../../include/openssl/cast.h ../../include/openssl/idea.h 568+hw_pk11_pub.o: ../../crypto/mdc2/mdc2.h ../../include/openssl/aes.h 569+hw_pk11_pub.o: ../../include/openssl/objects.h ../../include/openssl/obj_mac.h 570+hw_pk11_pub.o: ../../include/openssl/x509.h ../../include/openssl/buffer.h 571+hw_pk11_pub.o: ../../include/openssl/x509_vfy.h ../../include/openssl/pkcs7.h 572+hw_pk11_pub.o: ../../include/openssl/pem2.h ../cryptlib.h 573+hw_pk11_pub.o: ../../e_os.h hw_pk11_err.c hw_pk11_err.h hw_pk11_pub.c 574+hw_pk11so.o: ../../include/openssl/e_os2.h ../../include/openssl/opensslconf.h 575+hw_pk11so.o: ../../include/openssl/engine.h ../../include/openssl/ossl_typ.h 576+hw_pk11so.o: ../../include/openssl/bn.h ../../include/openssl/rsa.h 577+hw_pk11so.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h 578+hw_pk11so.o: ../../include/openssl/crypto.h ../../include/openssl/stack.h 579+hw_pk11so.o: ../../include/openssl/safestack.h ../../include/openssl/opensslv.h 580+hw_pk11so.o: ../../include/openssl/symhacks.h ../../include/openssl/dsa.h 581+hw_pk11so.o: ../../include/openssl/dh.h ../../include/openssl/rand.h 582+hw_pk11so.o: ../../include/openssl/ui.h ../../include/openssl/err.h 583+hw_pk11so.o: ../../include/openssl/lhash.h ../../include/openssl/dso.h 584+hw_pk11so.o: ../../include/openssl/pem.h ../../include/openssl/evp.h 585+hw_pk11so.o: ../../include/openssl/md2.h ../../include/openssl/md4.h 586+hw_pk11so.o: ../../include/openssl/md5.h ../../include/openssl/sha.h 587+hw_pk11so.o: ../../include/openssl/ripemd.h ../../include/openssl/des.h 588+hw_pk11so.o: ../../include/openssl/des_old.h ../../include/openssl/ui_compat.h 589+hw_pk11so.o: ../../include/openssl/rc4.h ../../include/openssl/rc2.h 590+hw_pk11so.o: ../../crypto/rc5/rc5.h ../../include/openssl/blowfish.h 591+hw_pk11so.o: ../../include/openssl/cast.h ../../include/openssl/idea.h 592+hw_pk11so.o: ../../crypto/mdc2/mdc2.h ../../include/openssl/aes.h 593+hw_pk11so.o: ../../include/openssl/objects.h ../../include/openssl/obj_mac.h 594+hw_pk11so.o: ../../include/openssl/x509.h ../../include/openssl/buffer.h 595+hw_pk11so.o: ../../include/openssl/x509_vfy.h ../../include/openssl/pkcs7.h 596+hw_pk11so.o: ../../include/openssl/pem2.h ../cryptlib.h 597+hw_pk11so.o: ../../e_os.h hw_pk11_err.c hw_pk11_err.h hw_pk11so.c 598+hw_pk11so_pub.o: ../../include/openssl/e_os2.h ../../include/openssl/opensslconf.h 599+hw_pk11so_pub.o: ../../include/openssl/engine.h ../../include/openssl/ossl_typ.h 600+hw_pk11so_pub.o: ../../include/openssl/bn.h ../../include/openssl/rsa.h 601+hw_pk11so_pub.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h 602+hw_pk11so_pub.o: ../../include/openssl/crypto.h ../../include/openssl/stack.h 603+hw_pk11so_pub.o: ../../include/openssl/safestack.h ../../include/openssl/opensslv.h 604+hw_pk11so_pub.o: ../../include/openssl/symhacks.h ../../include/openssl/dsa.h 605+hw_pk11so_pub.o: ../../include/openssl/dh.h ../../include/openssl/rand.h 606+hw_pk11so_pub.o: ../../include/openssl/ui.h ../../include/openssl/err.h 607+hw_pk11so_pub.o: ../../include/openssl/lhash.h ../../include/openssl/dso.h 608+hw_pk11so_pub.o: ../../include/openssl/pem.h ../../include/openssl/evp.h 609+hw_pk11so_pub.o: ../../include/openssl/md2.h ../../include/openssl/md4.h 610+hw_pk11so_pub.o: ../../include/openssl/md5.h ../../include/openssl/sha.h 611+hw_pk11so_pub.o: ../../include/openssl/ripemd.h ../../include/openssl/des.h 612+hw_pk11so_pub.o: ../../include/openssl/des_old.h ../../include/openssl/ui_compat.h 613+hw_pk11so_pub.o: ../../include/openssl/rc4.h ../../include/openssl/rc2.h 614+hw_pk11so_pub.o: ../../crypto/rc5/rc5.h ../../include/openssl/blowfish.h 615+hw_pk11so_pub.o: ../../include/openssl/cast.h ../../include/openssl/idea.h 616+hw_pk11so_pub.o: ../../crypto/mdc2/mdc2.h ../../include/openssl/aes.h 617+hw_pk11so_pub.o: ../../include/openssl/objects.h ../../include/openssl/obj_mac.h 618+hw_pk11so_pub.o: ../../include/openssl/x509.h ../../include/openssl/buffer.h 619+hw_pk11so_pub.o: ../../include/openssl/x509_vfy.h ../../include/openssl/pkcs7.h 620+hw_pk11so_pub.o: ../../include/openssl/pem2.h ../cryptlib.h 621+hw_pk11so_pub.o: ../../e_os.h hw_pk11_err.c hw_pk11_err.h hw_pk11so_pub.c 622 tb_cipher.o: ../../e_os.h ../../include/openssl/asn1.h 623 tb_cipher.o: ../../include/openssl/bio.h ../../include/openssl/buffer.h 624 tb_cipher.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h 625Index: openssl/crypto/engine/cryptoki.h 626diff -u /dev/null openssl/crypto/engine/cryptoki.h:1.4 627--- /dev/null Mon Jan 16 18:53:42 2012 628+++ openssl/crypto/engine/cryptoki.h Thu Dec 18 00:14:12 2008 629@@ -0,0 +1,103 @@ 630+/* 631+ * CDDL HEADER START 632+ * 633+ * The contents of this file are subject to the terms of the 634+ * Common Development and Distribution License, Version 1.0 only 635+ * (the "License"). You may not use this file except in compliance 636+ * with the License. 637+ * 638+ * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 639+ * or http://www.opensolaris.org/os/licensing. 640+ * See the License for the specific language governing permissions 641+ * and limitations under the License. 642+ * 643+ * When distributing Covered Code, include this CDDL HEADER in each 644+ * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 645+ * If applicable, add the following below this CDDL HEADER, with the 646+ * fields enclosed by brackets "[]" replaced with your own identifying 647+ * information: Portions Copyright [yyyy] [name of copyright owner] 648+ * 649+ * CDDL HEADER END 650+ */ 651+/* 652+ * Copyright 2003 Sun Microsystems, Inc. All rights reserved. 653+ * Use is subject to license terms. 654+ */ 655+ 656+#ifndef _CRYPTOKI_H 657+#define _CRYPTOKI_H 658+ 659+/* ident "@(#)cryptoki.h 1.2 05/06/08 SMI" */ 660+ 661+#ifdef __cplusplus 662+extern "C" { 663+#endif 664+ 665+#ifndef CK_PTR 666+#define CK_PTR * 667+#endif 668+ 669+#ifndef CK_DEFINE_FUNCTION 670+#define CK_DEFINE_FUNCTION(returnType, name) returnType name 671+#endif 672+ 673+#ifndef CK_DECLARE_FUNCTION 674+#define CK_DECLARE_FUNCTION(returnType, name) returnType name 675+#endif 676+ 677+#ifndef CK_DECLARE_FUNCTION_POINTER 678+#define CK_DECLARE_FUNCTION_POINTER(returnType, name) returnType (* name) 679+#endif 680+ 681+#ifndef CK_CALLBACK_FUNCTION 682+#define CK_CALLBACK_FUNCTION(returnType, name) returnType (* name) 683+#endif 684+ 685+#ifndef NULL_PTR 686+#include <unistd.h> /* For NULL */ 687+#define NULL_PTR NULL 688+#endif 689+ 690+/* 691+ * pkcs11t.h defines TRUE and FALSE in a way that upsets lint 692+ */ 693+#ifndef CK_DISABLE_TRUE_FALSE 694+#define CK_DISABLE_TRUE_FALSE 695+#ifndef TRUE 696+#define TRUE 1 697+#endif /* TRUE */ 698+#ifndef FALSE 699+#define FALSE 0 700+#endif /* FALSE */ 701+#endif /* CK_DISABLE_TRUE_FALSE */ 702+ 703+#undef CK_PKCS11_FUNCTION_INFO 704+ 705+#include "pkcs11.h" 706+ 707+/* Solaris specific functions */ 708+ 709+#include <stdlib.h> 710+ 711+/* 712+ * SUNW_C_GetMechSession will initialize the framework and do all 713+ * the necessary PKCS#11 calls to create a session capable of 714+ * providing operations on the requested mechanism 715+ */ 716+CK_RV SUNW_C_GetMechSession(CK_MECHANISM_TYPE mech, 717+ CK_SESSION_HANDLE_PTR hSession); 718+ 719+/* 720+ * SUNW_C_KeyToObject will create a secret key object for the given 721+ * mechanism from the rawkey data. 722+ */ 723+CK_RV SUNW_C_KeyToObject(CK_SESSION_HANDLE hSession, 724+ CK_MECHANISM_TYPE mech, const void *rawkey, size_t rawkey_len, 725+ CK_OBJECT_HANDLE_PTR obj); 726+ 727+ 728+#ifdef __cplusplus 729+} 730+#endif 731+ 732+#endif /* _CRYPTOKI_H */ 733Index: openssl/crypto/engine/eng_all.c 734diff -u openssl/crypto/engine/eng_all.c:1.4.6.1 openssl/crypto/engine/eng_all.c:1.4 735--- openssl/crypto/engine/eng_all.c:1.4.6.1 Sun Jan 15 15:45:36 2012 736+++ openssl/crypto/engine/eng_all.c Mon Jun 13 14:25:19 2011 737@@ -110,6 +110,14 @@ 738 #if defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_NO_CAPIENG) 739 ENGINE_load_capi(); 740 #endif 741+#ifndef OPENSSL_NO_HW_PKCS11 742+#ifndef OPENSSL_NO_HW_PKCS11CA 743+ ENGINE_load_pk11ca(); 744+#endif 745+#ifndef OPENSSL_NO_HW_PKCS11SO 746+ ENGINE_load_pk11so(); 747+#endif 748+#endif 749 #endif 750 } 751 752Index: openssl/crypto/engine/engine.h 753diff -u openssl/crypto/engine/engine.h:1.4.6.1 openssl/crypto/engine/engine.h:1.4 754--- openssl/crypto/engine/engine.h:1.4.6.1 Sun Jan 15 15:45:36 2012 755+++ openssl/crypto/engine/engine.h Mon Jun 13 14:25:19 2011 756@@ -344,6 +344,12 @@ 757 void ENGINE_load_capi(void); 758 #endif 759 #endif 760+#ifndef OPENSSL_NO_HW_PKCS11CA 761+void ENGINE_load_pk11ca(void); 762+#endif 763+#ifndef OPENSSL_NO_HW_PKCS11SO 764+void ENGINE_load_pk11so(void); 765+#endif 766 767 /* Get and set global flags (ENGINE_TABLE_FLAG_***) for the implementation 768 * "registry" handling. */ 769Index: openssl/crypto/engine/hw_pk11.c 770diff -u /dev/null openssl/crypto/engine/hw_pk11.c:1.26.4.2 771--- /dev/null Mon Jan 16 18:53:42 2012 772+++ openssl/crypto/engine/hw_pk11.c Thu Jun 16 12:31:35 2011 773@@ -0,0 +1,4057 @@ 774+/* 775+ * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 776+ * Use is subject to license terms. 777+ */ 778+ 779+/* crypto/engine/hw_pk11.c */ 780+/* 781+ * This product includes software developed by the OpenSSL Project for 782+ * use in the OpenSSL Toolkit (http://www.openssl.org/). 783+ * 784+ * This project also referenced hw_pkcs11-0.9.7b.patch written by 785+ * Afchine Madjlessi. 786+ */ 787+/* 788+ * ==================================================================== 789+ * Copyright (c) 2000-2001 The OpenSSL Project. All rights reserved. 790+ * 791+ * Redistribution and use in source and binary forms, with or without 792+ * modification, are permitted provided that the following conditions 793+ * are met: 794+ * 795+ * 1. Redistributions of source code must retain the above copyright 796+ * notice, this list of conditions and the following disclaimer. 797+ * 798+ * 2. Redistributions in binary form must reproduce the above copyright 799+ * notice, this list of conditions and the following disclaimer in 800+ * the documentation and/or other materials provided with the 801+ * distribution. 802+ * 803+ * 3. All advertising materials mentioning features or use of this 804+ * software must display the following acknowledgment: 805+ * "This product includes software developed by the OpenSSL Project 806+ * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 807+ * 808+ * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 809+ * endorse or promote products derived from this software without 810+ * prior written permission. For written permission, please contact 811+ * licensing@OpenSSL.org. 812+ * 813+ * 5. Products derived from this software may not be called "OpenSSL" 814+ * nor may "OpenSSL" appear in their names without prior written 815+ * permission of the OpenSSL Project. 816+ * 817+ * 6. Redistributions of any form whatsoever must retain the following 818+ * acknowledgment: 819+ * "This product includes software developed by the OpenSSL Project 820+ * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 821+ * 822+ * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 823+ * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 824+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 825+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 826+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 827+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 828+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 829+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 830+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 831+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 832+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 833+ * OF THE POSSIBILITY OF SUCH DAMAGE. 834+ * ==================================================================== 835+ * 836+ * This product includes cryptographic software written by Eric Young 837+ * (eay@cryptsoft.com). This product includes software written by Tim 838+ * Hudson (tjh@cryptsoft.com). 839+ * 840+ */ 841+ 842+#include <stdio.h> 843+#include <stdlib.h> 844+#include <string.h> 845+#include <sys/types.h> 846+ 847+#include <openssl/e_os2.h> 848+#include <openssl/crypto.h> 849+#include <cryptlib.h> 850+#include <openssl/engine.h> 851+#include <openssl/dso.h> 852+#include <openssl/err.h> 853+#include <openssl/bn.h> 854+#include <openssl/md5.h> 855+#include <openssl/pem.h> 856+#ifndef OPENSSL_NO_RSA 857+#include <openssl/rsa.h> 858+#endif 859+#ifndef OPENSSL_NO_DSA 860+#include <openssl/dsa.h> 861+#endif 862+#ifndef OPENSSL_NO_DH 863+#include <openssl/dh.h> 864+#endif 865+#include <openssl/rand.h> 866+#include <openssl/objects.h> 867+#include <openssl/x509.h> 868+#include <openssl/aes.h> 869+ 870+#ifdef OPENSSL_SYS_WIN32 871+typedef int pid_t; 872+#define getpid() GetCurrentProcessId() 873+#define NOPTHREADS 874+#ifndef NULL_PTR 875+#define NULL_PTR NULL 876+#endif 877+#define CK_DEFINE_FUNCTION(returnType, name) \ 878+ returnType __declspec(dllexport) name 879+#define CK_DECLARE_FUNCTION(returnType, name) \ 880+ returnType __declspec(dllimport) name 881+#define CK_DECLARE_FUNCTION_POINTER(returnType, name) \ 882+ returnType __declspec(dllimport) (* name) 883+#else 884+#include <signal.h> 885+#include <unistd.h> 886+#include <dlfcn.h> 887+#endif 888+ 889+#ifndef NOPTHREADS 890+#include <pthread.h> 891+#endif 892+ 893+#ifndef OPENSSL_NO_HW 894+#ifndef OPENSSL_NO_HW_PK11 895+#ifndef OPENSSL_NO_HW_PK11CA 896+ 897+/* label for debug messages printed on stderr */ 898+#define PK11_DBG "PKCS#11 ENGINE DEBUG" 899+/* prints a lot of debug messages on stderr about slot selection process */ 900+/* #undef DEBUG_SLOT_SELECTION */ 901+/* 902+ * Solaris specific code. See comment at check_hw_mechanisms() for more 903+ * information. 904+ */ 905+#if defined(__SVR4) && defined(__sun) 906+#undef SOLARIS_HW_SLOT_SELECTION 907+#endif 908+ 909+/* 910+ * AES counter mode is not supported in the OpenSSL EVP API yet and neither 911+ * there are official OIDs for mechanisms based on this mode. With our changes, 912+ * an application can define its own EVP calls for AES counter mode and then 913+ * it can make use of hardware acceleration through this engine. However, it's 914+ * better if we keep AES CTR support code under ifdef's. 915+ */ 916+#define SOLARIS_AES_CTR 917+ 918+#ifdef OPENSSL_SYS_WIN32 919+#pragma pack(push, cryptoki, 1) 920+#include "cryptoki.h" 921+#include "pkcs11.h" 922+#pragma pack(pop, cryptoki) 923+#else 924+#include "cryptoki.h" 925+#include "pkcs11.h" 926+#endif 927+#include "hw_pk11ca.h" 928+#include "hw_pk11_err.c" 929+ 930+#ifdef SOLARIS_AES_CTR 931+/* 932+ * NIDs for AES counter mode that will be defined during the engine 933+ * initialization. 934+ */ 935+static int NID_aes_128_ctr = NID_undef; 936+static int NID_aes_192_ctr = NID_undef; 937+static int NID_aes_256_ctr = NID_undef; 938+#endif /* SOLARIS_AES_CTR */ 939+ 940+/* 941+ * We use this lock to prevent multiple C_Login()s, guard getpassphrase(), 942+ * uri_struct manipulation, and static token info. All of that is used by the 943+ * RSA keys by reference feature. 944+ */ 945+#ifndef NOPTHREADS 946+pthread_mutex_t *token_lock; 947+#endif 948+ 949+#ifdef SOLARIS_HW_SLOT_SELECTION 950+/* 951+ * Tables for symmetric ciphers and digest mechs found in the pkcs11_kernel 952+ * library. See comment at check_hw_mechanisms() for more information. 953+ */ 954+static int *hw_cnids; 955+static int *hw_dnids; 956+#endif /* SOLARIS_HW_SLOT_SELECTION */ 957+ 958+/* PKCS#11 session caches and their locks for all operation types */ 959+static PK11_CACHE session_cache[OP_MAX]; 960+ 961+/* 962+ * We cache the flags so that we do not have to run C_GetTokenInfo() again when 963+ * logging into the token. 964+ */ 965+CK_FLAGS pubkey_token_flags; 966+ 967+/* 968+ * As stated in v2.20, 11.7 Object Management Function, in section for 969+ * C_FindObjectsInit(), at most one search operation may be active at a given 970+ * time in a given session. Therefore, C_Find{,Init,Final}Objects() should be 971+ * grouped together to form one atomic search operation. This is already 972+ * ensured by the property of unique PKCS#11 session handle used for each 973+ * PK11_SESSION object. 974+ * 975+ * This is however not the biggest concern - maintaining consistency of the 976+ * underlying object store is more important. The same section of the spec also 977+ * says that one thread can be in the middle of a search operation while another 978+ * thread destroys the object matching the search template which would result in 979+ * invalid handle returned from the search operation. 980+ * 981+ * Hence, the following locks are used for both protection of the object stores. 982+ * They are also used for active list protection. 983+ */ 984+#ifndef NOPTHREADS 985+pthread_mutex_t *find_lock[OP_MAX] = { NULL }; 986+#endif 987+ 988+/* 989+ * lists of asymmetric key handles which are active (referenced by at least one 990+ * PK11_SESSION structure, either held by a thread or present in free_session 991+ * list) for given algorithm type 992+ */ 993+PK11_active *active_list[OP_MAX] = { NULL }; 994+ 995+/* 996+ * Create all secret key objects in a global session so that they are available 997+ * to use for other sessions. These other sessions may be opened or closed 998+ * without losing the secret key objects. 999+ */ 1000+static CK_SESSION_HANDLE global_session = CK_INVALID_HANDLE; 1001+ 1002+/* ENGINE level stuff */ 1003+static int pk11_init(ENGINE *e); 1004+static int pk11_library_init(ENGINE *e); 1005+static int pk11_finish(ENGINE *e); 1006+static int pk11_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void)); 1007+static int pk11_destroy(ENGINE *e); 1008+ 1009+/* RAND stuff */ 1010+static void pk11_rand_seed(const void *buf, int num); 1011+static void pk11_rand_add(const void *buf, int num, double add_entropy); 1012+static void pk11_rand_cleanup(void); 1013+static int pk11_rand_bytes(unsigned char *buf, int num); 1014+static int pk11_rand_status(void); 1015+ 1016+/* These functions are also used in other files */ 1017+PK11_SESSION *pk11_get_session(PK11_OPTYPE optype); 1018+void pk11_return_session(PK11_SESSION *sp, PK11_OPTYPE optype); 1019+ 1020+/* active list manipulation functions used in this file */ 1021+extern int pk11_active_delete(CK_OBJECT_HANDLE h, PK11_OPTYPE type); 1022+extern void pk11_free_active_list(PK11_OPTYPE type); 1023+ 1024+#ifndef OPENSSL_NO_RSA 1025+int pk11_destroy_rsa_key_objects(PK11_SESSION *session); 1026+int pk11_destroy_rsa_object_pub(PK11_SESSION *sp, CK_BBOOL uselock); 1027+int pk11_destroy_rsa_object_priv(PK11_SESSION *sp, CK_BBOOL uselock); 1028+#endif 1029+#ifndef OPENSSL_NO_DSA 1030+int pk11_destroy_dsa_key_objects(PK11_SESSION *session); 1031+int pk11_destroy_dsa_object_pub(PK11_SESSION *sp, CK_BBOOL uselock); 1032+int pk11_destroy_dsa_object_priv(PK11_SESSION *sp, CK_BBOOL uselock); 1033+#endif 1034+#ifndef OPENSSL_NO_DH 1035+int pk11_destroy_dh_key_objects(PK11_SESSION *session); 1036+int pk11_destroy_dh_object(PK11_SESSION *session, CK_BBOOL uselock); 1037+#endif 1038+ 1039+/* Local helper functions */ 1040+static int pk11_free_all_sessions(void); 1041+static int pk11_free_session_list(PK11_OPTYPE optype); 1042+static int pk11_setup_session(PK11_SESSION *sp, PK11_OPTYPE optype); 1043+static int pk11_destroy_cipher_key_objects(PK11_SESSION *session); 1044+static int pk11_destroy_object(CK_SESSION_HANDLE session, CK_OBJECT_HANDLE oh, 1045+ CK_BBOOL persistent); 1046+static const char *get_PK11_LIBNAME(void); 1047+static void free_PK11_LIBNAME(void); 1048+static long set_PK11_LIBNAME(const char *name); 1049+ 1050+/* Symmetric cipher and digest support functions */ 1051+static int cipher_nid_to_pk11(int nid); 1052+#ifdef SOLARIS_AES_CTR 1053+static int pk11_add_NID(char *sn, char *ln); 1054+static int pk11_add_aes_ctr_NIDs(void); 1055+#endif /* SOLARIS_AES_CTR */ 1056+static int pk11_usable_ciphers(const int **nids); 1057+static int pk11_usable_digests(const int **nids); 1058+static int pk11_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key, 1059+ const unsigned char *iv, int enc); 1060+static int pk11_cipher_final(PK11_SESSION *sp); 1061+#if OPENSSL_VERSION_NUMBER < 0x10000000L 1062+static int pk11_cipher_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, 1063+ const unsigned char *in, unsigned int inl); 1064+#else 1065+static int pk11_cipher_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, 1066+ const unsigned char *in, size_t inl); 1067+#endif 1068+static int pk11_cipher_cleanup(EVP_CIPHER_CTX *ctx); 1069+static int pk11_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher, 1070+ const int **nids, int nid); 1071+static int pk11_engine_digests(ENGINE *e, const EVP_MD **digest, 1072+ const int **nids, int nid); 1073+static CK_OBJECT_HANDLE pk11_get_cipher_key(EVP_CIPHER_CTX *ctx, 1074+ const unsigned char *key, CK_KEY_TYPE key_type, PK11_SESSION *sp); 1075+static int check_new_cipher_key(PK11_SESSION *sp, const unsigned char *key, 1076+ int key_len); 1077+static int md_nid_to_pk11(int nid); 1078+static int pk11_digest_init(EVP_MD_CTX *ctx); 1079+static int pk11_digest_update(EVP_MD_CTX *ctx, const void *data, 1080+ size_t count); 1081+static int pk11_digest_final(EVP_MD_CTX *ctx, unsigned char *md); 1082+static int pk11_digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from); 1083+static int pk11_digest_cleanup(EVP_MD_CTX *ctx); 1084+ 1085+static int pk11_choose_slots(int *any_slot_found); 1086+static void pk11_find_symmetric_ciphers(CK_FUNCTION_LIST_PTR pflist, 1087+ CK_SLOT_ID current_slot, int *current_slot_n_cipher, 1088+ int *local_cipher_nids); 1089+static void pk11_find_digests(CK_FUNCTION_LIST_PTR pflist, 1090+ CK_SLOT_ID current_slot, int *current_slot_n_digest, 1091+ int *local_digest_nids); 1092+static void pk11_get_symmetric_cipher(CK_FUNCTION_LIST_PTR, int slot_id, 1093+ CK_MECHANISM_TYPE mech, int *current_slot_n_cipher, int *local_cipher_nids, 1094+ int id); 1095+static void pk11_get_digest(CK_FUNCTION_LIST_PTR pflist, int slot_id, 1096+ CK_MECHANISM_TYPE mech, int *current_slot_n_digest, int *local_digest_nids, 1097+ int id); 1098+ 1099+static int pk11_init_all_locks(void); 1100+static void pk11_free_all_locks(void); 1101+ 1102+#ifdef SOLARIS_HW_SLOT_SELECTION 1103+static int check_hw_mechanisms(void); 1104+static int nid_in_table(int nid, int *nid_table); 1105+#endif /* SOLARIS_HW_SLOT_SELECTION */ 1106+ 1107+/* Index for the supported ciphers */ 1108+enum pk11_cipher_id { 1109+ PK11_DES_CBC, 1110+ PK11_DES3_CBC, 1111+ PK11_DES_ECB, 1112+ PK11_DES3_ECB, 1113+ PK11_RC4, 1114+ PK11_AES_128_CBC, 1115+ PK11_AES_192_CBC, 1116+ PK11_AES_256_CBC, 1117+ PK11_AES_128_ECB, 1118+ PK11_AES_192_ECB, 1119+ PK11_AES_256_ECB, 1120+ PK11_BLOWFISH_CBC, 1121+#ifdef SOLARIS_AES_CTR 1122+ PK11_AES_128_CTR, 1123+ PK11_AES_192_CTR, 1124+ PK11_AES_256_CTR, 1125+#endif /* SOLARIS_AES_CTR */ 1126+ PK11_CIPHER_MAX 1127+}; 1128+ 1129+/* Index for the supported digests */ 1130+enum pk11_digest_id { 1131+ PK11_MD5, 1132+ PK11_SHA1, 1133+ PK11_SHA224, 1134+ PK11_SHA256, 1135+ PK11_SHA384, 1136+ PK11_SHA512, 1137+ PK11_DIGEST_MAX 1138+}; 1139+ 1140+#define TRY_OBJ_DESTROY(sp, obj_hdl, retval, uselock, alg_type, priv) \ 1141+ { \ 1142+ if (uselock) \ 1143+ LOCK_OBJSTORE(alg_type); \ 1144+ if (pk11_active_delete(obj_hdl, alg_type) == 1) \ 1145+ { \ 1146+ retval = pk11_destroy_object(sp->session, obj_hdl, \ 1147+ priv ? sp->priv_persistent : sp->pub_persistent); \ 1148+ } \ 1149+ if (uselock) \ 1150+ UNLOCK_OBJSTORE(alg_type); \ 1151+ } 1152+ 1153+static int cipher_nids[PK11_CIPHER_MAX]; 1154+static int digest_nids[PK11_DIGEST_MAX]; 1155+static int cipher_count = 0; 1156+static int digest_count = 0; 1157+static CK_BBOOL pk11_have_rsa = CK_FALSE; 1158+static CK_BBOOL pk11_have_recover = CK_FALSE; 1159+static CK_BBOOL pk11_have_dsa = CK_FALSE; 1160+static CK_BBOOL pk11_have_dh = CK_FALSE; 1161+static CK_BBOOL pk11_have_random = CK_FALSE; 1162+ 1163+typedef struct PK11_CIPHER_st 1164+ { 1165+ enum pk11_cipher_id id; 1166+ int nid; 1167+ int iv_len; 1168+ int min_key_len; 1169+ int max_key_len; 1170+ CK_KEY_TYPE key_type; 1171+ CK_MECHANISM_TYPE mech_type; 1172+ } PK11_CIPHER; 1173+ 1174+static PK11_CIPHER ciphers[] = 1175+ { 1176+ { PK11_DES_CBC, NID_des_cbc, 8, 8, 8, 1177+ CKK_DES, CKM_DES_CBC, }, 1178+ { PK11_DES3_CBC, NID_des_ede3_cbc, 8, 24, 24, 1179+ CKK_DES3, CKM_DES3_CBC, }, 1180+ { PK11_DES_ECB, NID_des_ecb, 0, 8, 8, 1181+ CKK_DES, CKM_DES_ECB, }, 1182+ { PK11_DES3_ECB, NID_des_ede3_ecb, 0, 24, 24, 1183+ CKK_DES3, CKM_DES3_ECB, }, 1184+ { PK11_RC4, NID_rc4, 0, 16, 256, 1185+ CKK_RC4, CKM_RC4, }, 1186+ { PK11_AES_128_CBC, NID_aes_128_cbc, 16, 16, 16, 1187+ CKK_AES, CKM_AES_CBC, }, 1188+ { PK11_AES_192_CBC, NID_aes_192_cbc, 16, 24, 24, 1189+ CKK_AES, CKM_AES_CBC, }, 1190+ { PK11_AES_256_CBC, NID_aes_256_cbc, 16, 32, 32, 1191+ CKK_AES, CKM_AES_CBC, }, 1192+ { PK11_AES_128_ECB, NID_aes_128_ecb, 0, 16, 16, 1193+ CKK_AES, CKM_AES_ECB, }, 1194+ { PK11_AES_192_ECB, NID_aes_192_ecb, 0, 24, 24, 1195+ CKK_AES, CKM_AES_ECB, }, 1196+ { PK11_AES_256_ECB, NID_aes_256_ecb, 0, 32, 32, 1197+ CKK_AES, CKM_AES_ECB, }, 1198+ { PK11_BLOWFISH_CBC, NID_bf_cbc, 8, 16, 16, 1199+ CKK_BLOWFISH, CKM_BLOWFISH_CBC, }, 1200+#ifdef SOLARIS_AES_CTR 1201+ /* we don't know the correct NIDs until the engine is initialized */ 1202+ { PK11_AES_128_CTR, NID_undef, 16, 16, 16, 1203+ CKK_AES, CKM_AES_CTR, }, 1204+ { PK11_AES_192_CTR, NID_undef, 16, 24, 24, 1205+ CKK_AES, CKM_AES_CTR, }, 1206+ { PK11_AES_256_CTR, NID_undef, 16, 32, 32, 1207+ CKK_AES, CKM_AES_CTR, }, 1208+#endif /* SOLARIS_AES_CTR */ 1209+ }; 1210+ 1211+typedef struct PK11_DIGEST_st 1212+ { 1213+ enum pk11_digest_id id; 1214+ int nid; 1215+ CK_MECHANISM_TYPE mech_type; 1216+ } PK11_DIGEST; 1217+ 1218+static PK11_DIGEST digests[] = 1219+ { 1220+ {PK11_MD5, NID_md5, CKM_MD5, }, 1221+ {PK11_SHA1, NID_sha1, CKM_SHA_1, }, 1222+ {PK11_SHA224, NID_sha224, CKM_SHA224, }, 1223+ {PK11_SHA256, NID_sha256, CKM_SHA256, }, 1224+ {PK11_SHA384, NID_sha384, CKM_SHA384, }, 1225+ {PK11_SHA512, NID_sha512, CKM_SHA512, }, 1226+ {0, NID_undef, 0xFFFF, }, 1227+ }; 1228+ 1229+/* 1230+ * Structure to be used for the cipher_data/md_data in 1231+ * EVP_CIPHER_CTX/EVP_MD_CTX structures in order to use the same pk11 1232+ * session in multiple cipher_update calls 1233+ */ 1234+typedef struct PK11_CIPHER_STATE_st 1235+ { 1236+ PK11_SESSION *sp; 1237+ } PK11_CIPHER_STATE; 1238+ 1239+ 1240+/* 1241+ * libcrypto EVP stuff - this is how we get wired to EVP so the engine gets 1242+ * called when libcrypto requests a cipher NID. 1243+ * 1244+ * Note how the PK11_CIPHER_STATE is used here. 1245+ */ 1246+ 1247+/* DES CBC EVP */ 1248+static const EVP_CIPHER pk11_des_cbc = 1249+ { 1250+ NID_des_cbc, 1251+ 8, 8, 8, 1252+ EVP_CIPH_CBC_MODE, 1253+ pk11_cipher_init, 1254+ pk11_cipher_do_cipher, 1255+ pk11_cipher_cleanup, 1256+ sizeof (PK11_CIPHER_STATE), 1257+ EVP_CIPHER_set_asn1_iv, 1258+ EVP_CIPHER_get_asn1_iv, 1259+ NULL 1260+ }; 1261+ 1262+/* 3DES CBC EVP */ 1263+static const EVP_CIPHER pk11_3des_cbc = 1264+ { 1265+ NID_des_ede3_cbc, 1266+ 8, 24, 8, 1267+ EVP_CIPH_CBC_MODE, 1268+ pk11_cipher_init, 1269+ pk11_cipher_do_cipher, 1270+ pk11_cipher_cleanup, 1271+ sizeof (PK11_CIPHER_STATE), 1272+ EVP_CIPHER_set_asn1_iv, 1273+ EVP_CIPHER_get_asn1_iv, 1274+ NULL 1275+ }; 1276+ 1277+/* 1278+ * ECB modes don't use an Initial Vector so that's why set_asn1_parameters and 1279+ * get_asn1_parameters fields are set to NULL. 1280+ */ 1281+static const EVP_CIPHER pk11_des_ecb = 1282+ { 1283+ NID_des_ecb, 1284+ 8, 8, 8, 1285+ EVP_CIPH_ECB_MODE, 1286+ pk11_cipher_init, 1287+ pk11_cipher_do_cipher, 1288+ pk11_cipher_cleanup, 1289+ sizeof (PK11_CIPHER_STATE), 1290+ NULL, 1291+ NULL, 1292+ NULL 1293+ }; 1294+ 1295+static const EVP_CIPHER pk11_3des_ecb = 1296+ { 1297+ NID_des_ede3_ecb, 1298+ 8, 24, 8, 1299+ EVP_CIPH_ECB_MODE, 1300+ pk11_cipher_init, 1301+ pk11_cipher_do_cipher, 1302+ pk11_cipher_cleanup, 1303+ sizeof (PK11_CIPHER_STATE), 1304+ NULL, 1305+ NULL, 1306+ NULL 1307+ }; 1308+ 1309+ 1310+static const EVP_CIPHER pk11_aes_128_cbc = 1311+ { 1312+ NID_aes_128_cbc, 1313+ 16, 16, 16, 1314+ EVP_CIPH_CBC_MODE, 1315+ pk11_cipher_init, 1316+ pk11_cipher_do_cipher, 1317+ pk11_cipher_cleanup, 1318+ sizeof (PK11_CIPHER_STATE), 1319+ EVP_CIPHER_set_asn1_iv, 1320+ EVP_CIPHER_get_asn1_iv, 1321+ NULL 1322+ }; 1323+ 1324+static const EVP_CIPHER pk11_aes_192_cbc = 1325+ { 1326+ NID_aes_192_cbc, 1327+ 16, 24, 16, 1328+ EVP_CIPH_CBC_MODE, 1329+ pk11_cipher_init, 1330+ pk11_cipher_do_cipher, 1331+ pk11_cipher_cleanup, 1332+ sizeof (PK11_CIPHER_STATE), 1333+ EVP_CIPHER_set_asn1_iv, 1334+ EVP_CIPHER_get_asn1_iv, 1335+ NULL 1336+ }; 1337+ 1338+static const EVP_CIPHER pk11_aes_256_cbc = 1339+ { 1340+ NID_aes_256_cbc, 1341+ 16, 32, 16, 1342+ EVP_CIPH_CBC_MODE, 1343+ pk11_cipher_init, 1344+ pk11_cipher_do_cipher, 1345+ pk11_cipher_cleanup, 1346+ sizeof (PK11_CIPHER_STATE), 1347+ EVP_CIPHER_set_asn1_iv, 1348+ EVP_CIPHER_get_asn1_iv, 1349+ NULL 1350+ }; 1351+ 1352+/* 1353+ * ECB modes don't use IV so that's why set_asn1_parameters and 1354+ * get_asn1_parameters are set to NULL. 1355+ */ 1356+static const EVP_CIPHER pk11_aes_128_ecb = 1357+ { 1358+ NID_aes_128_ecb, 1359+ 16, 16, 0, 1360+ EVP_CIPH_ECB_MODE, 1361+ pk11_cipher_init, 1362+ pk11_cipher_do_cipher, 1363+ pk11_cipher_cleanup, 1364+ sizeof (PK11_CIPHER_STATE), 1365+ NULL, 1366+ NULL, 1367+ NULL 1368+ }; 1369+ 1370+static const EVP_CIPHER pk11_aes_192_ecb = 1371+ { 1372+ NID_aes_192_ecb, 1373+ 16, 24, 0, 1374+ EVP_CIPH_ECB_MODE, 1375+ pk11_cipher_init, 1376+ pk11_cipher_do_cipher, 1377+ pk11_cipher_cleanup, 1378+ sizeof (PK11_CIPHER_STATE), 1379+ NULL, 1380+ NULL, 1381+ NULL 1382+ }; 1383+ 1384+static const EVP_CIPHER pk11_aes_256_ecb = 1385+ { 1386+ NID_aes_256_ecb, 1387+ 16, 32, 0, 1388+ EVP_CIPH_ECB_MODE, 1389+ pk11_cipher_init, 1390+ pk11_cipher_do_cipher, 1391+ pk11_cipher_cleanup, 1392+ sizeof (PK11_CIPHER_STATE), 1393+ NULL, 1394+ NULL, 1395+ NULL 1396+ }; 1397+ 1398+#ifdef SOLARIS_AES_CTR 1399+/* 1400+ * NID_undef's will be changed to the AES counter mode NIDs as soon they are 1401+ * created in pk11_library_init(). Note that the need to change these structures 1402+ * is the reason why we don't define them with the const keyword. 1403+ */ 1404+static EVP_CIPHER pk11_aes_128_ctr = 1405+ { 1406+ NID_undef, 1407+ 16, 16, 16, 1408+ EVP_CIPH_CBC_MODE, 1409+ pk11_cipher_init, 1410+ pk11_cipher_do_cipher, 1411+ pk11_cipher_cleanup, 1412+ sizeof (PK11_CIPHER_STATE), 1413+ EVP_CIPHER_set_asn1_iv, 1414+ EVP_CIPHER_get_asn1_iv, 1415+ NULL 1416+ }; 1417+ 1418+static EVP_CIPHER pk11_aes_192_ctr = 1419+ { 1420+ NID_undef, 1421+ 16, 24, 16, 1422+ EVP_CIPH_CBC_MODE, 1423+ pk11_cipher_init, 1424+ pk11_cipher_do_cipher, 1425+ pk11_cipher_cleanup, 1426+ sizeof (PK11_CIPHER_STATE), 1427+ EVP_CIPHER_set_asn1_iv, 1428+ EVP_CIPHER_get_asn1_iv, 1429+ NULL 1430+ }; 1431+ 1432+static EVP_CIPHER pk11_aes_256_ctr = 1433+ { 1434+ NID_undef, 1435+ 16, 32, 16, 1436+ EVP_CIPH_CBC_MODE, 1437+ pk11_cipher_init, 1438+ pk11_cipher_do_cipher, 1439+ pk11_cipher_cleanup, 1440+ sizeof (PK11_CIPHER_STATE), 1441+ EVP_CIPHER_set_asn1_iv, 1442+ EVP_CIPHER_get_asn1_iv, 1443+ NULL 1444+ }; 1445+#endif /* SOLARIS_AES_CTR */ 1446+ 1447+static const EVP_CIPHER pk11_bf_cbc = 1448+ { 1449+ NID_bf_cbc, 1450+ 8, 16, 8, 1451+ EVP_CIPH_VARIABLE_LENGTH, 1452+ pk11_cipher_init, 1453+ pk11_cipher_do_cipher, 1454+ pk11_cipher_cleanup, 1455+ sizeof (PK11_CIPHER_STATE), 1456+ EVP_CIPHER_set_asn1_iv, 1457+ EVP_CIPHER_get_asn1_iv, 1458+ NULL 1459+ }; 1460+ 1461+static const EVP_CIPHER pk11_rc4 = 1462+ { 1463+ NID_rc4, 1464+ 1, 16, 0, 1465+ EVP_CIPH_VARIABLE_LENGTH, 1466+ pk11_cipher_init, 1467+ pk11_cipher_do_cipher, 1468+ pk11_cipher_cleanup, 1469+ sizeof (PK11_CIPHER_STATE), 1470+ NULL, 1471+ NULL, 1472+ NULL 1473+ }; 1474+ 1475+static const EVP_MD pk11_md5 = 1476+ { 1477+ NID_md5, 1478+ NID_md5WithRSAEncryption, 1479+ MD5_DIGEST_LENGTH, 1480+ 0, 1481+ pk11_digest_init, 1482+ pk11_digest_update, 1483+ pk11_digest_final, 1484+ pk11_digest_copy, 1485+ pk11_digest_cleanup, 1486+ EVP_PKEY_RSA_method, 1487+ MD5_CBLOCK, 1488+ sizeof (PK11_CIPHER_STATE), 1489+ }; 1490+ 1491+static const EVP_MD pk11_sha1 = 1492+ { 1493+ NID_sha1, 1494+ NID_sha1WithRSAEncryption, 1495+ SHA_DIGEST_LENGTH, 1496+ 0, 1497+ pk11_digest_init, 1498+ pk11_digest_update, 1499+ pk11_digest_final, 1500+ pk11_digest_copy, 1501+ pk11_digest_cleanup, 1502+ EVP_PKEY_RSA_method, 1503+ SHA_CBLOCK, 1504+ sizeof (PK11_CIPHER_STATE), 1505+ }; 1506+ 1507+static const EVP_MD pk11_sha224 = 1508+ { 1509+ NID_sha224, 1510+ NID_sha224WithRSAEncryption, 1511+ SHA224_DIGEST_LENGTH, 1512+ 0, 1513+ pk11_digest_init, 1514+ pk11_digest_update, 1515+ pk11_digest_final, 1516+ pk11_digest_copy, 1517+ pk11_digest_cleanup, 1518+ EVP_PKEY_RSA_method, 1519+ /* SHA-224 uses the same cblock size as SHA-256 */ 1520+ SHA256_CBLOCK, 1521+ sizeof (PK11_CIPHER_STATE), 1522+ }; 1523+ 1524+static const EVP_MD pk11_sha256 = 1525+ { 1526+ NID_sha256, 1527+ NID_sha256WithRSAEncryption, 1528+ SHA256_DIGEST_LENGTH, 1529+ 0, 1530+ pk11_digest_init, 1531+ pk11_digest_update, 1532+ pk11_digest_final, 1533+ pk11_digest_copy, 1534+ pk11_digest_cleanup, 1535+ EVP_PKEY_RSA_method, 1536+ SHA256_CBLOCK, 1537+ sizeof (PK11_CIPHER_STATE), 1538+ }; 1539+ 1540+static const EVP_MD pk11_sha384 = 1541+ { 1542+ NID_sha384, 1543+ NID_sha384WithRSAEncryption, 1544+ SHA384_DIGEST_LENGTH, 1545+ 0, 1546+ pk11_digest_init, 1547+ pk11_digest_update, 1548+ pk11_digest_final, 1549+ pk11_digest_copy, 1550+ pk11_digest_cleanup, 1551+ EVP_PKEY_RSA_method, 1552+ /* SHA-384 uses the same cblock size as SHA-512 */ 1553+ SHA512_CBLOCK, 1554+ sizeof (PK11_CIPHER_STATE), 1555+ }; 1556+ 1557+static const EVP_MD pk11_sha512 = 1558+ { 1559+ NID_sha512, 1560+ NID_sha512WithRSAEncryption, 1561+ SHA512_DIGEST_LENGTH, 1562+ 0, 1563+ pk11_digest_init, 1564+ pk11_digest_update, 1565+ pk11_digest_final, 1566+ pk11_digest_copy, 1567+ pk11_digest_cleanup, 1568+ EVP_PKEY_RSA_method, 1569+ SHA512_CBLOCK, 1570+ sizeof (PK11_CIPHER_STATE), 1571+ }; 1572+ 1573+/* 1574+ * Initialization function. Sets up various PKCS#11 library components. 1575+ * The definitions for control commands specific to this engine 1576+ */ 1577+#define PK11_CMD_SO_PATH ENGINE_CMD_BASE 1578+#define PK11_CMD_PIN (ENGINE_CMD_BASE+1) 1579+#define PK11_CMD_SLOT (ENGINE_CMD_BASE+2) 1580+static const ENGINE_CMD_DEFN pk11_cmd_defns[] = 1581+ { 1582+ { 1583+ PK11_CMD_SO_PATH, 1584+ "SO_PATH", 1585+ "Specifies the path to the 'pkcs#11' shared library", 1586+ ENGINE_CMD_FLAG_STRING 1587+ }, 1588+ { 1589+ PK11_CMD_PIN, 1590+ "PIN", 1591+ "Specifies the pin code", 1592+ ENGINE_CMD_FLAG_STRING 1593+ }, 1594+ { 1595+ PK11_CMD_SLOT, 1596+ "SLOT", 1597+ "Specifies the slot (default is auto select)", 1598+ ENGINE_CMD_FLAG_NUMERIC, 1599+ }, 1600+ {0, NULL, NULL, 0} 1601+ }; 1602+ 1603+ 1604+static RAND_METHOD pk11_random = 1605+ { 1606+ pk11_rand_seed, 1607+ pk11_rand_bytes, 1608+ pk11_rand_cleanup, 1609+ pk11_rand_add, 1610+ pk11_rand_bytes, 1611+ pk11_rand_status 1612+ }; 1613+ 1614+ 1615+/* Constants used when creating the ENGINE */ 1616+#ifdef OPENSSL_NO_HW_PK11SO 1617+#error "can't load both crypto-accelerator and sign-only PKCS#11 engines" 1618+#endif 1619+static const char *engine_pk11_id = "pkcs11"; 1620+static const char *engine_pk11_name = 1621+ "PKCS #11 engine support (crypto accelerator)"; 1622+ 1623+CK_FUNCTION_LIST_PTR pFuncList = NULL; 1624+static const char PK11_GET_FUNCTION_LIST[] = "C_GetFunctionList"; 1625+ 1626+/* 1627+ * This is a static string constant for the DSO file name and the function 1628+ * symbol names to bind to. We set it in the Configure script based on whether 1629+ * this is 32 or 64 bit build. 1630+ */ 1631+static const char def_PK11_LIBNAME[] = PK11_LIB_LOCATION; 1632+ 1633+static CK_BBOOL true = TRUE; 1634+static CK_BBOOL false = FALSE; 1635+/* Needed in hw_pk11_pub.c as well so that's why it is not static. */ 1636+CK_SLOT_ID pubkey_SLOTID = 0; 1637+static CK_SLOT_ID rand_SLOTID = 0; 1638+static CK_SLOT_ID SLOTID = 0; 1639+char *pk11_pin = NULL; 1640+static CK_BBOOL pk11_library_initialized = FALSE; 1641+static CK_BBOOL pk11_atfork_initialized = FALSE; 1642+static int pk11_pid = 0; 1643+ 1644+static DSO *pk11_dso = NULL; 1645+ 1646+/* allocate and initialize all locks used by the engine itself */ 1647+static int pk11_init_all_locks(void) 1648+ { 1649+#ifndef NOPTHREADS 1650+ int type; 1651+ 1652+ if ((token_lock = OPENSSL_malloc(sizeof (pthread_mutex_t))) == NULL) 1653+ goto malloc_err; 1654+ (void) pthread_mutex_init(token_lock, NULL); 1655+ 1656+#ifndef OPENSSL_NO_RSA 1657+ find_lock[OP_RSA] = OPENSSL_malloc(sizeof (pthread_mutex_t)); 1658+ if (find_lock[OP_RSA] == NULL) 1659+ goto malloc_err; 1660+ (void) pthread_mutex_init(find_lock[OP_RSA], NULL); 1661+#endif /* OPENSSL_NO_RSA */ 1662+ 1663+#ifndef OPENSSL_NO_DSA 1664+ find_lock[OP_DSA] = OPENSSL_malloc(sizeof (pthread_mutex_t)); 1665+ if (find_lock[OP_DSA] == NULL) 1666+ goto malloc_err; 1667+ (void) pthread_mutex_init(find_lock[OP_DSA], NULL); 1668+#endif /* OPENSSL_NO_DSA */ 1669+ 1670+#ifndef OPENSSL_NO_DH 1671+ find_lock[OP_DH] = OPENSSL_malloc(sizeof (pthread_mutex_t)); 1672+ if (find_lock[OP_DH] == NULL) 1673+ goto malloc_err; 1674+ (void) pthread_mutex_init(find_lock[OP_DH], NULL); 1675+#endif /* OPENSSL_NO_DH */ 1676+ 1677+ for (type = 0; type < OP_MAX; type++) 1678+ { 1679+ session_cache[type].lock = 1680+ OPENSSL_malloc(sizeof (pthread_mutex_t)); 1681+ if (session_cache[type].lock == NULL) 1682+ goto malloc_err; 1683+ (void) pthread_mutex_init(session_cache[type].lock, NULL); 1684+ } 1685+ 1686+ return (1); 1687+ 1688+malloc_err: 1689+ pk11_free_all_locks(); 1690+ PK11err(PK11_F_INIT_ALL_LOCKS, PK11_R_MALLOC_FAILURE); 1691+ return (0); 1692+#else 1693+ return (1); 1694+#endif 1695+ } 1696+ 1697+static void pk11_free_all_locks(void) 1698+ { 1699+#ifndef NOPTHREADS 1700+ int type; 1701+ 1702+#ifndef OPENSSL_NO_RSA 1703+ if (find_lock[OP_RSA] != NULL) 1704+ { 1705+ (void) pthread_mutex_destroy(find_lock[OP_RSA]); 1706+ OPENSSL_free(find_lock[OP_RSA]); 1707+ find_lock[OP_RSA] = NULL; 1708+ } 1709+#endif /* OPENSSL_NO_RSA */ 1710+#ifndef OPENSSL_NO_DSA 1711+ if (find_lock[OP_DSA] != NULL) 1712+ { 1713+ (void) pthread_mutex_destroy(find_lock[OP_DSA]); 1714+ OPENSSL_free(find_lock[OP_DSA]); 1715+ find_lock[OP_DSA] = NULL; 1716+ } 1717+#endif /* OPENSSL_NO_DSA */ 1718+#ifndef OPENSSL_NO_DH 1719+ if (find_lock[OP_DH] != NULL) 1720+ { 1721+ (void) pthread_mutex_destroy(find_lock[OP_DH]); 1722+ OPENSSL_free(find_lock[OP_DH]); 1723+ find_lock[OP_DH] = NULL; 1724+ } 1725+#endif /* OPENSSL_NO_DH */ 1726+ 1727+ for (type = 0; type < OP_MAX; type++) 1728+ { 1729+ if (session_cache[type].lock != NULL) 1730+ { 1731+ (void) pthread_mutex_destroy(session_cache[type].lock); 1732+ OPENSSL_free(session_cache[type].lock); 1733+ session_cache[type].lock = NULL; 1734+ } 1735+ } 1736+#endif 1737+ } 1738+ 1739+/* 1740+ * This internal function is used by ENGINE_pk11() and "dynamic" ENGINE support. 1741+ */ 1742+static int bind_pk11(ENGINE *e) 1743+ { 1744+#ifndef OPENSSL_NO_RSA 1745+ const RSA_METHOD *rsa = NULL; 1746+ RSA_METHOD *pk11_rsa = PK11_RSA(); 1747+#endif /* OPENSSL_NO_RSA */ 1748+ if (!pk11_library_initialized) 1749+ if (!pk11_library_init(e)) 1750+ return (0); 1751+ 1752+ if (!ENGINE_set_id(e, engine_pk11_id) || 1753+ !ENGINE_set_name(e, engine_pk11_name) || 1754+ !ENGINE_set_ciphers(e, pk11_engine_ciphers) || 1755+ !ENGINE_set_digests(e, pk11_engine_digests)) 1756+ return (0); 1757+#ifndef OPENSSL_NO_RSA 1758+ if (pk11_have_rsa == CK_TRUE) 1759+ { 1760+ if (!ENGINE_set_RSA(e, PK11_RSA()) || 1761+ !ENGINE_set_load_privkey_function(e, pk11_load_privkey) || 1762+ !ENGINE_set_load_pubkey_function(e, pk11_load_pubkey)) 1763+ return (0); 1764+#ifdef DEBUG_SLOT_SELECTION 1765+ fprintf(stderr, "%s: registered RSA\n", PK11_DBG); 1766+#endif /* DEBUG_SLOT_SELECTION */ 1767+ } 1768+#endif /* OPENSSL_NO_RSA */ 1769+#ifndef OPENSSL_NO_DSA 1770+ if (pk11_have_dsa == CK_TRUE) 1771+ { 1772+ if (!ENGINE_set_DSA(e, PK11_DSA())) 1773+ return (0); 1774+#ifdef DEBUG_SLOT_SELECTION 1775+ fprintf(stderr, "%s: registered DSA\n", PK11_DBG); 1776+#endif /* DEBUG_SLOT_SELECTION */ 1777+ } 1778+#endif /* OPENSSL_NO_DSA */ 1779+#ifndef OPENSSL_NO_DH 1780+ if (pk11_have_dh == CK_TRUE) 1781+ { 1782+ if (!ENGINE_set_DH(e, PK11_DH())) 1783+ return (0); 1784+#ifdef DEBUG_SLOT_SELECTION 1785+ fprintf(stderr, "%s: registered DH\n", PK11_DBG); 1786+#endif /* DEBUG_SLOT_SELECTION */ 1787+ } 1788+#endif /* OPENSSL_NO_DH */ 1789+ if (pk11_have_random) 1790+ { 1791+ if (!ENGINE_set_RAND(e, &pk11_random)) 1792+ return (0); 1793+#ifdef DEBUG_SLOT_SELECTION 1794+ fprintf(stderr, "%s: registered random\n", PK11_DBG); 1795+#endif /* DEBUG_SLOT_SELECTION */ 1796+ } 1797+ if (!ENGINE_set_init_function(e, pk11_init) || 1798+ !ENGINE_set_destroy_function(e, pk11_destroy) || 1799+ !ENGINE_set_finish_function(e, pk11_finish) || 1800+ !ENGINE_set_ctrl_function(e, pk11_ctrl) || 1801+ !ENGINE_set_cmd_defns(e, pk11_cmd_defns)) 1802+ return (0); 1803+ 1804+/* 1805+ * Apache calls OpenSSL function RSA_blinding_on() once during startup 1806+ * which in turn calls bn_mod_exp. Since we do not implement bn_mod_exp 1807+ * here, we wire it back to the OpenSSL software implementation. 1808+ * Since it is used only once, performance is not a concern. 1809+ */ 1810+#ifndef OPENSSL_NO_RSA 1811+ rsa = RSA_PKCS1_SSLeay(); 1812+ pk11_rsa->rsa_mod_exp = rsa->rsa_mod_exp; 1813+ pk11_rsa->bn_mod_exp = rsa->bn_mod_exp; 1814+ if (pk11_have_recover != CK_TRUE) 1815+ pk11_rsa->rsa_pub_dec = rsa->rsa_pub_dec; 1816+#endif /* OPENSSL_NO_RSA */ 1817+ 1818+ /* Ensure the pk11 error handling is set up */ 1819+ ERR_load_pk11_strings(); 1820+ 1821+ return (1); 1822+ } 1823+ 1824+/* Dynamic engine support is disabled at a higher level for Solaris */ 1825+#ifdef ENGINE_DYNAMIC_SUPPORT 1826+#error "dynamic engine not supported" 1827+static int bind_helper(ENGINE *e, const char *id) 1828+ { 1829+ if (id && (strcmp(id, engine_pk11_id) != 0)) 1830+ return (0); 1831+ 1832+ if (!bind_pk11(e)) 1833+ return (0); 1834+ 1835+ return (1); 1836+ } 1837+ 1838+IMPLEMENT_DYNAMIC_CHECK_FN() 1839+IMPLEMENT_DYNAMIC_BIND_FN(bind_helper) 1840+ 1841+#else 1842+static ENGINE *engine_pk11(void) 1843+ { 1844+ ENGINE *ret = ENGINE_new(); 1845+ 1846+ if (!ret) 1847+ return (NULL); 1848+ 1849+ if (!bind_pk11(ret)) 1850+ { 1851+ ENGINE_free(ret); 1852+ return (NULL); 1853+ } 1854+ 1855+ return (ret); 1856+ } 1857+ 1858+void 1859+ENGINE_load_pk11(void) 1860+ { 1861+ ENGINE *e_pk11 = NULL; 1862+ 1863+ /* 1864+ * Do not use dynamic PKCS#11 library on Solaris due to 1865+ * security reasons. We will link it in statically. 1866+ */ 1867+ /* Attempt to load PKCS#11 library */ 1868+ if (!pk11_dso) 1869+ pk11_dso = DSO_load(NULL, get_PK11_LIBNAME(), NULL, 0); 1870+ 1871+ if (pk11_dso == NULL) 1872+ { 1873+ PK11err(PK11_F_LOAD, PK11_R_DSO_FAILURE); 1874+ return; 1875+ } 1876+ 1877+ e_pk11 = engine_pk11(); 1878+ if (!e_pk11) 1879+ { 1880+ DSO_free(pk11_dso); 1881+ pk11_dso = NULL; 1882+ return; 1883+ } 1884+ 1885+ /* 1886+ * At this point, the pk11 shared library is either dynamically 1887+ * loaded or statically linked in. So, initialize the pk11 1888+ * library before calling ENGINE_set_default since the latter 1889+ * needs cipher and digest algorithm information 1890+ */ 1891+ if (!pk11_library_init(e_pk11)) 1892+ { 1893+ DSO_free(pk11_dso); 1894+ pk11_dso = NULL; 1895+ ENGINE_free(e_pk11); 1896+ return; 1897+ } 1898+ 1899+ ENGINE_add(e_pk11); 1900+ 1901+ ENGINE_free(e_pk11); 1902+ ERR_clear_error(); 1903+ } 1904+#endif /* ENGINE_DYNAMIC_SUPPORT */ 1905+ 1906+/* 1907+ * These are the static string constants for the DSO file name and 1908+ * the function symbol names to bind to. 1909+ */ 1910+static const char *PK11_LIBNAME = NULL; 1911+ 1912+static const char *get_PK11_LIBNAME(void) 1913+ { 1914+ if (PK11_LIBNAME) 1915+ return (PK11_LIBNAME); 1916+ 1917+ return (def_PK11_LIBNAME); 1918+ } 1919+ 1920+static void free_PK11_LIBNAME(void) 1921+ { 1922+ if (PK11_LIBNAME) 1923+ OPENSSL_free((void*)PK11_LIBNAME); 1924+ 1925+ PK11_LIBNAME = NULL; 1926+ } 1927+ 1928+static long set_PK11_LIBNAME(const char *name) 1929+ { 1930+ free_PK11_LIBNAME(); 1931+ 1932+ return ((PK11_LIBNAME = BUF_strdup(name)) != NULL ? 1 : 0); 1933+ } 1934+ 1935+/* acquire all engine specific mutexes before fork */ 1936+static void pk11_fork_prepare(void) 1937+ { 1938+#ifndef NOPTHREADS 1939+ int i; 1940+ 1941+ if (!pk11_library_initialized) 1942+ return; 1943+ 1944+ LOCK_OBJSTORE(OP_RSA); 1945+ LOCK_OBJSTORE(OP_DSA); 1946+ LOCK_OBJSTORE(OP_DH); 1947+ (void) pthread_mutex_lock(token_lock); 1948+ for (i = 0; i < OP_MAX; i++) 1949+ { 1950+ (void) pthread_mutex_lock(session_cache[i].lock); 1951+ } 1952+#endif 1953+ } 1954+ 1955+/* release all engine specific mutexes */ 1956+static void pk11_fork_parent(void) 1957+ { 1958+#ifndef NOPTHREADS 1959+ int i; 1960+ 1961+ if (!pk11_library_initialized) 1962+ return; 1963+ 1964+ for (i = OP_MAX - 1; i >= 0; i--) 1965+ { 1966+ (void) pthread_mutex_unlock(session_cache[i].lock); 1967+ } 1968+ UNLOCK_OBJSTORE(OP_DH); 1969+ UNLOCK_OBJSTORE(OP_DSA); 1970+ UNLOCK_OBJSTORE(OP_RSA); 1971+ (void) pthread_mutex_unlock(token_lock); 1972+#endif 1973+ } 1974+ 1975+/* 1976+ * same situation as in parent - we need to unlock all locks to make them 1977+ * accessible to all threads. 1978+ */ 1979+static void pk11_fork_child(void) 1980+ { 1981+#ifndef NOPTHREADS 1982+ int i; 1983+ 1984+ if (!pk11_library_initialized) 1985+ return; 1986+ 1987+ for (i = OP_MAX - 1; i >= 0; i--) 1988+ { 1989+ (void) pthread_mutex_unlock(session_cache[i].lock); 1990+ } 1991+ UNLOCK_OBJSTORE(OP_DH); 1992+ UNLOCK_OBJSTORE(OP_DSA); 1993+ UNLOCK_OBJSTORE(OP_RSA); 1994+ (void) pthread_mutex_unlock(token_lock); 1995+#endif 1996+ } 1997+ 1998+/* Initialization function for the pk11 engine */ 1999+static int pk11_init(ENGINE *e) 2000+{ 2001+ return (pk11_library_init(e)); 2002+} 2003+ 2004+static CK_C_INITIALIZE_ARGS pk11_init_args = 2005+ { 2006+ NULL_PTR, /* CreateMutex */ 2007+ NULL_PTR, /* DestroyMutex */ 2008+ NULL_PTR, /* LockMutex */ 2009+ NULL_PTR, /* UnlockMutex */ 2010+ CKF_OS_LOCKING_OK, /* flags */ 2011+ NULL_PTR, /* pReserved */ 2012+ }; 2013+ 2014+/* 2015+ * Initialization function. Sets up various PKCS#11 library components. 2016+ * It selects a slot based on predefined critiera. In the process, it also 2017+ * count how many ciphers and digests to support. Since the cipher and 2018+ * digest information is needed when setting default engine, this function 2019+ * needs to be called before calling ENGINE_set_default. 2020+ */ 2021+/* ARGSUSED */ 2022+static int pk11_library_init(ENGINE *e) 2023+ { 2024+ CK_C_GetFunctionList p; 2025+ CK_RV rv = CKR_OK; 2026+ CK_INFO info; 2027+ CK_ULONG ul_state_len; 2028+ int any_slot_found; 2029+ int i; 2030+#ifndef OPENSSL_SYS_WIN32 2031+ struct sigaction sigint_act, sigterm_act, sighup_act; 2032+#endif 2033+ 2034+ /* 2035+ * pk11_library_initialized is set to 0 in pk11_finish() which 2036+ * is called from ENGINE_finish(). However, if there is still 2037+ * at least one existing functional reference to the engine 2038+ * (see engine(3) for more information), pk11_finish() is 2039+ * skipped. For example, this can happen if an application 2040+ * forgets to clear one cipher context. In case of a fork() 2041+ * when the application is finishing the engine so that it can 2042+ * be reinitialized in the child, forgotten functional 2043+ * reference causes pk11_library_initialized to stay 1. In 2044+ * that case we need the PID check so that we properly 2045+ * initialize the engine again. 2046+ */ 2047+ if (pk11_library_initialized) 2048+ { 2049+ if (pk11_pid == getpid()) 2050+ { 2051+ return (1); 2052+ } 2053+ else 2054+ { 2055+ global_session = CK_INVALID_HANDLE; 2056+ /* 2057+ * free the locks first to prevent memory leak in case 2058+ * the application calls fork() without finishing the 2059+ * engine first. 2060+ */ 2061+ pk11_free_all_locks(); 2062+ } 2063+ } 2064+ 2065+ if (pk11_dso == NULL) 2066+ { 2067+ PK11err(PK11_F_LIBRARY_INIT, PK11_R_DSO_FAILURE); 2068+ goto err; 2069+ } 2070+ 2071+#ifdef SOLARIS_AES_CTR 2072+ /* 2073+ * We must do this before we start working with slots since we need all 2074+ * NIDs there. 2075+ */ 2076+ if (pk11_add_aes_ctr_NIDs() == 0) 2077+ goto err; 2078+#endif /* SOLARIS_AES_CTR */ 2079+ 2080+#ifdef SOLARIS_HW_SLOT_SELECTION 2081+ if (check_hw_mechanisms() == 0) 2082+ goto err; 2083+#endif /* SOLARIS_HW_SLOT_SELECTION */ 2084+ 2085+ /* get the C_GetFunctionList function from the loaded library */ 2086+ p = (CK_C_GetFunctionList)DSO_bind_func(pk11_dso, 2087+ PK11_GET_FUNCTION_LIST); 2088+ if (!p) 2089+ { 2090+ PK11err(PK11_F_LIBRARY_INIT, PK11_R_DSO_FAILURE); 2091+ goto err; 2092+ } 2093+ 2094+ /* get the full function list from the loaded library */ 2095+ rv = p(&pFuncList); 2096+ if (rv != CKR_OK) 2097+ { 2098+ PK11err_add_data(PK11_F_LIBRARY_INIT, PK11_R_DSO_FAILURE, rv); 2099+ goto err; 2100+ } 2101+ 2102+#ifndef OPENSSL_SYS_WIN32 2103+ /* Not all PKCS#11 library are signal safe! */ 2104+ 2105+ (void) memset(&sigint_act, 0, sizeof(sigint_act)); 2106+ (void) memset(&sigterm_act, 0, sizeof(sigterm_act)); 2107+ (void) memset(&sighup_act, 0, sizeof(sighup_act)); 2108+ (void) sigaction(SIGINT, NULL, &sigint_act); 2109+ (void) sigaction(SIGTERM, NULL, &sigterm_act); 2110+ (void) sigaction(SIGHUP, NULL, &sighup_act); 2111+#endif 2112+ rv = pFuncList->C_Initialize((CK_VOID_PTR)&pk11_init_args); 2113+#ifndef OPENSSL_SYS_WIN32 2114+ (void) sigaction(SIGINT, &sigint_act, NULL); 2115+ (void) sigaction(SIGTERM, &sigterm_act, NULL); 2116+ (void) sigaction(SIGHUP, &sighup_act, NULL); 2117+#endif 2118+ if ((rv != CKR_OK) && (rv != CKR_CRYPTOKI_ALREADY_INITIALIZED)) 2119+ { 2120+ PK11err_add_data(PK11_F_LIBRARY_INIT, PK11_R_INITIALIZE, rv); 2121+ goto err; 2122+ } 2123+ 2124+ rv = pFuncList->C_GetInfo(&info); 2125+ if (rv != CKR_OK) 2126+ { 2127+ PK11err_add_data(PK11_F_LIBRARY_INIT, PK11_R_GETINFO, rv); 2128+ goto err; 2129+ } 2130+ 2131+ if (pk11_choose_slots(&any_slot_found) == 0) 2132+ goto err; 2133+ 2134+ /* 2135+ * The library we use, set in def_PK11_LIBNAME, may not offer any 2136+ * slot(s). In that case, we must not proceed but we must not return an 2137+ * error. The reason is that applications that try to set up the PKCS#11 2138+ * engine don't exit on error during the engine initialization just 2139+ * because no slot was present. 2140+ */ 2141+ if (any_slot_found == 0) 2142+ return (1); 2143+ 2144+ if (global_session == CK_INVALID_HANDLE) 2145+ { 2146+ /* Open the global_session for the new process */ 2147+ rv = pFuncList->C_OpenSession(SLOTID, CKF_SERIAL_SESSION, 2148+ NULL_PTR, NULL_PTR, &global_session); 2149+ if (rv != CKR_OK) 2150+ { 2151+ PK11err_add_data(PK11_F_LIBRARY_INIT, 2152+ PK11_R_OPENSESSION, rv); 2153+ goto err; 2154+ } 2155+ } 2156+ 2157+ /* 2158+ * Disable digest if C_GetOperationState is not supported since 2159+ * this function is required by OpenSSL digest copy function 2160+ */ 2161+ /* Keyper fails to return CKR_FUNCTION_NOT_SUPPORTED */ 2162+ if (pFuncList->C_GetOperationState(global_session, NULL, &ul_state_len) 2163+ != CKR_OK) { 2164+#ifdef DEBUG_SLOT_SELECTION 2165+ fprintf(stderr, "%s: C_GetOperationState() not supported, " 2166+ "setting digest_count to 0\n", PK11_DBG); 2167+#endif /* DEBUG_SLOT_SELECTION */ 2168+ digest_count = 0; 2169+ } 2170+ 2171+ pk11_library_initialized = TRUE; 2172+ pk11_pid = getpid(); 2173+ /* 2174+ * if initialization of the locks fails pk11_init_all_locks() 2175+ * will do the cleanup. 2176+ */ 2177+ if (!pk11_init_all_locks()) 2178+ goto err; 2179+ for (i = 0; i < OP_MAX; i++) 2180+ session_cache[i].head = NULL; 2181+ /* 2182+ * initialize active lists. We only use active lists 2183+ * for asymmetric ciphers. 2184+ */ 2185+ for (i = 0; i < OP_MAX; i++) 2186+ active_list[i] = NULL; 2187+ 2188+#ifndef NOPTHREADS 2189+ if (!pk11_atfork_initialized) 2190+ { 2191+ if (pthread_atfork(pk11_fork_prepare, pk11_fork_parent, 2192+ pk11_fork_child) != 0) 2193+ { 2194+ PK11err(PK11_F_LIBRARY_INIT, PK11_R_ATFORK_FAILED); 2195+ goto err; 2196+ } 2197+ pk11_atfork_initialized = TRUE; 2198+ } 2199+#endif 2200+ 2201+ return (1); 2202+ 2203+err: 2204+ return (0); 2205+ } 2206+ 2207+/* Destructor (complements the "ENGINE_pk11()" constructor) */ 2208+/* ARGSUSED */ 2209+static int pk11_destroy(ENGINE *e) 2210+ { 2211+ free_PK11_LIBNAME(); 2212+ ERR_unload_pk11_strings(); 2213+ if (pk11_pin) { 2214+ memset(pk11_pin, 0, strlen(pk11_pin)); 2215+ OPENSSL_free((void*)pk11_pin); 2216+ } 2217+ pk11_pin = NULL; 2218+ return (1); 2219+ } 2220+ 2221+/* 2222+ * Termination function to clean up the session, the token, and the pk11 2223+ * library. 2224+ */ 2225+/* ARGSUSED */ 2226+static int pk11_finish(ENGINE *e) 2227+ { 2228+ int i; 2229+ 2230+ if (pk11_pin) { 2231+ memset(pk11_pin, 0, strlen(pk11_pin)); 2232+ OPENSSL_free((void*)pk11_pin); 2233+ } 2234+ pk11_pin = NULL; 2235+ 2236+ if (pk11_dso == NULL) 2237+ { 2238+ PK11err(PK11_F_FINISH, PK11_R_NOT_LOADED); 2239+ goto err; 2240+ } 2241+ 2242+ OPENSSL_assert(pFuncList != NULL); 2243+ 2244+ if (pk11_free_all_sessions() == 0) 2245+ goto err; 2246+ 2247+ /* free all active lists */ 2248+ for (i = 0; i < OP_MAX; i++) 2249+ pk11_free_active_list(i); 2250+ 2251+ pFuncList->C_CloseSession(global_session); 2252+ global_session = CK_INVALID_HANDLE; 2253+ 2254+ /* 2255+ * Since we are part of a library (libcrypto.so), calling this function 2256+ * may have side-effects. 2257+ */ 2258+#if 0 2259+ pFuncList->C_Finalize(NULL); 2260+#endif 2261+ 2262+ if (!DSO_free(pk11_dso)) 2263+ { 2264+ PK11err(PK11_F_FINISH, PK11_R_DSO_FAILURE); 2265+ goto err; 2266+ } 2267+ pk11_dso = NULL; 2268+ pFuncList = NULL; 2269+ pk11_library_initialized = FALSE; 2270+ pk11_pid = 0; 2271+ /* 2272+ * There is no way how to unregister atfork handlers (other than 2273+ * unloading the library) so we just free the locks. For this reason 2274+ * the atfork handlers check if the engine is initialized and bail out 2275+ * immediately if not. This is necessary in case a process finishes 2276+ * the engine before calling fork(). 2277+ */ 2278+ pk11_free_all_locks(); 2279+ 2280+ return (1); 2281+ 2282+err: 2283+ return (0); 2284+ } 2285+ 2286+/* Standard engine interface function to set the dynamic library path */ 2287+/* ARGSUSED */ 2288+static int pk11_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void)) 2289+ { 2290+ int initialized = ((pk11_dso == NULL) ? 0 : 1); 2291+ 2292+ switch (cmd) 2293+ { 2294+ case PK11_CMD_SO_PATH: 2295+ if (p == NULL) 2296+ { 2297+ PK11err(PK11_F_CTRL, ERR_R_PASSED_NULL_PARAMETER); 2298+ return (0); 2299+ } 2300+ 2301+ if (initialized) 2302+ { 2303+ PK11err(PK11_F_CTRL, PK11_R_ALREADY_LOADED); 2304+ return (0); 2305+ } 2306+ 2307+ return (set_PK11_LIBNAME((const char *)p)); 2308+ case PK11_CMD_PIN: 2309+ if (pk11_pin) { 2310+ memset(pk11_pin, 0, strlen(pk11_pin)); 2311+ OPENSSL_free((void*)pk11_pin); 2312+ } 2313+ pk11_pin = NULL; 2314+ 2315+ if (p == NULL) 2316+ { 2317+ PK11err(PK11_F_CTRL, ERR_R_PASSED_NULL_PARAMETER); 2318+ return (0); 2319+ } 2320+ 2321+ pk11_pin = BUF_strdup(p); 2322+ if (pk11_pin == NULL) 2323+ { 2324+ PK11err(PK11_F_GET_SESSION, PK11_R_MALLOC_FAILURE); 2325+ return (0); 2326+ } 2327+ return (1); 2328+ case PK11_CMD_SLOT: 2329+ SLOTID = (CK_SLOT_ID)i; 2330+#ifdef DEBUG_SLOT_SELECTION 2331+ fprintf(stderr, "%s: slot set\n", PK11_DBG); 2332+#endif 2333+ return (1); 2334+ default: 2335+ break; 2336+ } 2337+ 2338+ PK11err(PK11_F_CTRL, PK11_R_CTRL_COMMAND_NOT_IMPLEMENTED); 2339+ 2340+ return (0); 2341+ } 2342+ 2343+ 2344+/* Required function by the engine random interface. It does nothing here */ 2345+static void pk11_rand_cleanup(void) 2346+ { 2347+ return; 2348+ } 2349+ 2350+/* ARGSUSED */ 2351+static void pk11_rand_add(const void *buf, int num, double add) 2352+ { 2353+ PK11_SESSION *sp; 2354+ 2355+ if ((sp = pk11_get_session(OP_RAND)) == NULL) 2356+ return; 2357+ 2358+ /* 2359+ * Ignore any errors (e.g. CKR_RANDOM_SEED_NOT_SUPPORTED) since 2360+ * the calling functions do not care anyway 2361+ */ 2362+ pFuncList->C_SeedRandom(sp->session, (unsigned char *) buf, num); 2363+ pk11_return_session(sp, OP_RAND); 2364+ 2365+ return; 2366+ } 2367+ 2368+static void pk11_rand_seed(const void *buf, int num) 2369+ { 2370+ pk11_rand_add(buf, num, 0); 2371+ } 2372+ 2373+static int pk11_rand_bytes(unsigned char *buf, int num) 2374+ { 2375+ CK_RV rv; 2376+ PK11_SESSION *sp; 2377+ 2378+ if ((sp = pk11_get_session(OP_RAND)) == NULL) 2379+ return (0); 2380+ 2381+ rv = pFuncList->C_GenerateRandom(sp->session, buf, num); 2382+ if (rv != CKR_OK) 2383+ { 2384+ PK11err_add_data(PK11_F_RAND_BYTES, PK11_R_GENERATERANDOM, rv); 2385+ pk11_return_session(sp, OP_RAND); 2386+ return (0); 2387+ } 2388+ 2389+ pk11_return_session(sp, OP_RAND); 2390+ return (1); 2391+ } 2392+ 2393+/* Required function by the engine random interface. It does nothing here */ 2394+static int pk11_rand_status(void) 2395+ { 2396+ return (1); 2397+ } 2398+ 2399+/* Free all BIGNUM structures from PK11_SESSION. */ 2400+static void pk11_free_nums(PK11_SESSION *sp, PK11_OPTYPE optype) 2401+ { 2402+ switch (optype) 2403+ { 2404+#ifndef OPENSSL_NO_RSA 2405+ case OP_RSA: 2406+ if (sp->opdata_rsa_n_num != NULL) 2407+ { 2408+ BN_free(sp->opdata_rsa_n_num); 2409+ sp->opdata_rsa_n_num = NULL; 2410+ } 2411+ if (sp->opdata_rsa_e_num != NULL) 2412+ { 2413+ BN_free(sp->opdata_rsa_e_num); 2414+ sp->opdata_rsa_e_num = NULL; 2415+ } 2416+ if (sp->opdata_rsa_pn_num != NULL) 2417+ { 2418+ BN_free(sp->opdata_rsa_pn_num); 2419+ sp->opdata_rsa_pn_num = NULL; 2420+ } 2421+ if (sp->opdata_rsa_pe_num != NULL) 2422+ { 2423+ BN_free(sp->opdata_rsa_pe_num); 2424+ sp->opdata_rsa_pe_num = NULL; 2425+ } 2426+ if (sp->opdata_rsa_d_num != NULL) 2427+ { 2428+ BN_free(sp->opdata_rsa_d_num); 2429+ sp->opdata_rsa_d_num = NULL; 2430+ } 2431+ break; 2432+#endif 2433+#ifndef OPENSSL_NO_DSA 2434+ case OP_DSA: 2435+ if (sp->opdata_dsa_pub_num != NULL) 2436+ { 2437+ BN_free(sp->opdata_dsa_pub_num); 2438+ sp->opdata_dsa_pub_num = NULL; 2439+ } 2440+ if (sp->opdata_dsa_priv_num != NULL) 2441+ { 2442+ BN_free(sp->opdata_dsa_priv_num); 2443+ sp->opdata_dsa_priv_num = NULL; 2444+ } 2445+ break; 2446+#endif 2447+#ifndef OPENSSL_NO_DH 2448+ case OP_DH: 2449+ if (sp->opdata_dh_priv_num != NULL) 2450+ { 2451+ BN_free(sp->opdata_dh_priv_num); 2452+ sp->opdata_dh_priv_num = NULL; 2453+ } 2454+ break; 2455+#endif 2456+ default: 2457+ break; 2458+ } 2459+ } 2460+ 2461+/* 2462+ * Get new PK11_SESSION structure ready for use. Every process must have 2463+ * its own freelist of PK11_SESSION structures so handle fork() here 2464+ * by destroying the old and creating new freelist. 2465+ * The returned PK11_SESSION structure is disconnected from the freelist. 2466+ */ 2467+PK11_SESSION * 2468+pk11_get_session(PK11_OPTYPE optype) 2469+ { 2470+ PK11_SESSION *sp = NULL, *sp1, *freelist; 2471+#ifndef NOPTHREADS 2472+ pthread_mutex_t *freelist_lock = NULL; 2473+#endif 2474+ static pid_t pid = 0; 2475+ pid_t new_pid; 2476+ CK_RV rv; 2477+ 2478+ switch (optype) 2479+ { 2480+ case OP_RSA: 2481+ case OP_DSA: 2482+ case OP_DH: 2483+ case OP_RAND: 2484+ case OP_DIGEST: 2485+ case OP_CIPHER: 2486+#ifndef NOPTHREADS 2487+ freelist_lock = session_cache[optype].lock; 2488+#endif 2489+ break; 2490+ default: 2491+ PK11err(PK11_F_GET_SESSION, 2492+ PK11_R_INVALID_OPERATION_TYPE); 2493+ return (NULL); 2494+ } 2495+#ifndef NOPTHREADS 2496+ (void) pthread_mutex_lock(freelist_lock); 2497+#else 2498+ CRYPTO_w_lock(CRYPTO_LOCK_PK11_ENGINE); 2499+#endif 2500+ 2501+ /* 2502+ * Will use it to find out if we forked. We cannot use the PID field in 2503+ * the session structure because we could get a newly allocated session 2504+ * here, with no PID information. 2505+ */ 2506+ if (pid == 0) 2507+ pid = getpid(); 2508+ 2509+ freelist = session_cache[optype].head; 2510+ sp = freelist; 2511+ 2512+ /* 2513+ * If the free list is empty, allocate new unitialized (filled 2514+ * with zeroes) PK11_SESSION structure otherwise return first 2515+ * structure from the freelist. 2516+ */ 2517+ if (sp == NULL) 2518+ { 2519+ if ((sp = OPENSSL_malloc(sizeof (PK11_SESSION))) == NULL) 2520+ { 2521+ PK11err(PK11_F_GET_SESSION, 2522+ PK11_R_MALLOC_FAILURE); 2523+ goto err; 2524+ } 2525+ (void) memset(sp, 0, sizeof (PK11_SESSION)); 2526+ 2527+ /* 2528+ * It is a new session so it will look like a cache miss to the 2529+ * code below. So, we must not try to to destroy its members so 2530+ * mark them as unused. 2531+ */ 2532+ sp->opdata_rsa_priv_key = CK_INVALID_HANDLE; 2533+ sp->opdata_rsa_pub_key = CK_INVALID_HANDLE; 2534+ } 2535+ else 2536+ { 2537+ freelist = sp->next; 2538+ } 2539+ 2540+ /* 2541+ * Check whether we have forked. In that case, we must get rid of all 2542+ * inherited sessions and start allocating new ones. 2543+ */ 2544+ if (pid != (new_pid = getpid())) 2545+ { 2546+ pid = new_pid; 2547+ 2548+ /* 2549+ * We are a new process and thus need to free any inherited 2550+ * PK11_SESSION objects aside from the first session (sp) which 2551+ * is the only PK11_SESSION structure we will reuse (for the 2552+ * head of the list). 2553+ */ 2554+ while ((sp1 = freelist) != NULL) 2555+ { 2556+ freelist = sp1->next; 2557+ /* 2558+ * NOTE: we do not want to call pk11_free_all_sessions() 2559+ * here because it would close underlying PKCS#11 2560+ * sessions and destroy all objects. 2561+ */ 2562+ pk11_free_nums(sp1, optype); 2563+ OPENSSL_free(sp1); 2564+ } 2565+ 2566+ /* we have to free the active list as well. */ 2567+ pk11_free_active_list(optype); 2568+ 2569+ /* Initialize the process */ 2570+ rv = pFuncList->C_Initialize((CK_VOID_PTR)&pk11_init_args); 2571+ if ((rv != CKR_OK) && (rv != CKR_CRYPTOKI_ALREADY_INITIALIZED)) 2572+ { 2573+ PK11err_add_data(PK11_F_GET_SESSION, PK11_R_INITIALIZE, 2574+ rv); 2575+ OPENSSL_free(sp); 2576+ sp = NULL; 2577+ goto err; 2578+ } 2579+ 2580+ /* 2581+ * Choose slot here since the slot table is different on this 2582+ * process. If we are here then we must have found at least one 2583+ * usable slot before so we don't need to check any_slot_found. 2584+ * See pk11_library_init()'s usage of this function for more 2585+ * information. 2586+ */ 2587+#ifdef SOLARIS_HW_SLOT_SELECTION 2588+ if (check_hw_mechanisms() == 0) 2589+ goto err; 2590+#endif /* SOLARIS_HW_SLOT_SELECTION */ 2591+ if (pk11_choose_slots(NULL) == 0) 2592+ goto err; 2593+ 2594+ /* Open the global_session for the new process */ 2595+ rv = pFuncList->C_OpenSession(SLOTID, CKF_SERIAL_SESSION, 2596+ NULL_PTR, NULL_PTR, &global_session); 2597+ if (rv != CKR_OK) 2598+ { 2599+ PK11err_add_data(PK11_F_GET_SESSION, PK11_R_OPENSESSION, 2600+ rv); 2601+ OPENSSL_free(sp); 2602+ sp = NULL; 2603+ goto err; 2604+ } 2605+ 2606+ /* 2607+ * It is an inherited session from our parent so it needs 2608+ * re-initialization. 2609+ */ 2610+ if (pk11_setup_session(sp, optype) == 0) 2611+ { 2612+ OPENSSL_free(sp); 2613+ sp = NULL; 2614+ goto err; 2615+ } 2616+ if (pk11_token_relogin(sp->session) == 0) 2617+ { 2618+ /* 2619+ * We will keep the session in the cache list and let 2620+ * the caller cope with the situation. 2621+ */ 2622+ freelist = sp; 2623+ sp = NULL; 2624+ goto err; 2625+ } 2626+ } 2627+ 2628+ if (sp->pid == 0) 2629+ { 2630+ /* It is a new session and needs initialization. */ 2631+ if (pk11_setup_session(sp, optype) == 0) 2632+ { 2633+ OPENSSL_free(sp); 2634+ sp = NULL; 2635+ } 2636+ } 2637+ 2638+ /* set new head for the list of PK11_SESSION objects */ 2639+ session_cache[optype].head = freelist; 2640+ 2641+err: 2642+ if (sp != NULL) 2643+ sp->next = NULL; 2644+ 2645+#ifndef NOPTHREADS 2646+ (void) pthread_mutex_unlock(freelist_lock); 2647+#else 2648+ CRYPTO_w_unlock(CRYPTO_LOCK_PK11_ENGINE); 2649+#endif 2650+ 2651+ return (sp); 2652+ } 2653+ 2654+ 2655+void 2656+pk11_return_session(PK11_SESSION *sp, PK11_OPTYPE optype) 2657+ { 2658+#ifndef NOPTHREADS 2659+ pthread_mutex_t *freelist_lock; 2660+#endif 2661+ PK11_SESSION *freelist; 2662+ 2663+ /* 2664+ * If this is a session from the parent it will be taken care of and 2665+ * freed in pk11_get_session() as part of the post-fork clean up the 2666+ * next time we will ask for a new session. 2667+ */ 2668+ if (sp == NULL || sp->pid != getpid()) 2669+ return; 2670+ 2671+ switch (optype) 2672+ { 2673+ case OP_RSA: 2674+ case OP_DSA: 2675+ case OP_DH: 2676+ case OP_RAND: 2677+ case OP_DIGEST: 2678+ case OP_CIPHER: 2679+#ifndef NOPTHREADS 2680+ freelist_lock = session_cache[optype].lock; 2681+#endif 2682+ break; 2683+ default: 2684+ PK11err(PK11_F_RETURN_SESSION, 2685+ PK11_R_INVALID_OPERATION_TYPE); 2686+ return; 2687+ } 2688+ 2689+#ifndef NOPTHREADS 2690+ (void) pthread_mutex_lock(freelist_lock); 2691+#else 2692+ CRYPTO_w_lock(CRYPTO_LOCK_PK11_ENGINE); 2693+#endif 2694+ freelist = session_cache[optype].head; 2695+ sp->next = freelist; 2696+ session_cache[optype].head = sp; 2697+#ifndef NOPTHREADS 2698+ (void) pthread_mutex_unlock(freelist_lock); 2699+#else 2700+ CRYPTO_w_unlock(CRYPTO_LOCK_PK11_ENGINE); 2701+#endif 2702+ } 2703+ 2704+ 2705+/* Destroy all objects. This function is called when the engine is finished */ 2706+static int pk11_free_all_sessions() 2707+ { 2708+ int ret = 1; 2709+ int type; 2710+ 2711+#ifndef OPENSSL_NO_RSA 2712+ (void) pk11_destroy_rsa_key_objects(NULL); 2713+#endif /* OPENSSL_NO_RSA */ 2714+#ifndef OPENSSL_NO_DSA 2715+ (void) pk11_destroy_dsa_key_objects(NULL); 2716+#endif /* OPENSSL_NO_DSA */ 2717+#ifndef OPENSSL_NO_DH 2718+ (void) pk11_destroy_dh_key_objects(NULL); 2719+#endif /* OPENSSL_NO_DH */ 2720+ (void) pk11_destroy_cipher_key_objects(NULL); 2721+ 2722+ /* 2723+ * We try to release as much as we can but any error means that we will 2724+ * return 0 on exit. 2725+ */ 2726+ for (type = 0; type < OP_MAX; type++) 2727+ { 2728+ if (pk11_free_session_list(type) == 0) 2729+ ret = 0; 2730+ } 2731+ 2732+ return (ret); 2733+ } 2734+ 2735+/* 2736+ * Destroy session structures from the linked list specified. Free as many 2737+ * sessions as possible but any failure in C_CloseSession() means that we 2738+ * return an error on return. 2739+ */ 2740+static int pk11_free_session_list(PK11_OPTYPE optype) 2741+ { 2742+ CK_RV rv; 2743+ PK11_SESSION *sp = NULL; 2744+ PK11_SESSION *freelist = NULL; 2745+ pid_t mypid = getpid(); 2746+#ifndef NOPTHREADS 2747+ pthread_mutex_t *freelist_lock; 2748+#endif 2749+ int ret = 1; 2750+ 2751+ switch (optype) 2752+ { 2753+ case OP_RSA: 2754+ case OP_DSA: 2755+ case OP_DH: 2756+ case OP_RAND: 2757+ case OP_DIGEST: 2758+ case OP_CIPHER: 2759+#ifndef NOPTHREADS 2760+ freelist_lock = session_cache[optype].lock; 2761+#endif 2762+ break; 2763+ default: 2764+ PK11err(PK11_F_FREE_ALL_SESSIONS, 2765+ PK11_R_INVALID_OPERATION_TYPE); 2766+ return (0); 2767+ } 2768+ 2769+#ifndef NOPTHREADS 2770+ (void) pthread_mutex_lock(freelist_lock); 2771+#else 2772+ CRYPTO_w_lock(CRYPTO_LOCK_PK11_ENGINE); 2773+#endif 2774+ freelist = session_cache[optype].head; 2775+ while ((sp = freelist) != NULL) 2776+ { 2777+ if (sp->session != CK_INVALID_HANDLE && sp->pid == mypid) 2778+ { 2779+ rv = pFuncList->C_CloseSession(sp->session); 2780+ if (rv != CKR_OK) 2781+ { 2782+ PK11err_add_data(PK11_F_FREE_ALL_SESSIONS, 2783+ PK11_R_CLOSESESSION, rv); 2784+ ret = 0; 2785+ } 2786+ } 2787+ freelist = sp->next; 2788+ pk11_free_nums(sp, optype); 2789+ OPENSSL_free(sp); 2790+ } 2791+ 2792+#ifndef NOPTHREADS 2793+ (void) pthread_mutex_unlock(freelist_lock); 2794+#else 2795+ CRYPTO_w_unlock(CRYPTO_LOCK_PK11_ENGINE); 2796+#endif 2797+ return (ret); 2798+ } 2799+ 2800+ 2801+static int 2802+pk11_setup_session(PK11_SESSION *sp, PK11_OPTYPE optype) 2803+ { 2804+ CK_RV rv; 2805+ CK_SLOT_ID myslot; 2806+ 2807+ switch (optype) 2808+ { 2809+ case OP_RSA: 2810+ case OP_DSA: 2811+ case OP_DH: 2812+ myslot = pubkey_SLOTID; 2813+ break; 2814+ case OP_RAND: 2815+ myslot = rand_SLOTID; 2816+ break; 2817+ case OP_DIGEST: 2818+ case OP_CIPHER: 2819+ myslot = SLOTID; 2820+ break; 2821+ default: 2822+ PK11err(PK11_F_SETUP_SESSION, 2823+ PK11_R_INVALID_OPERATION_TYPE); 2824+ return (0); 2825+ } 2826+ 2827+ sp->session = CK_INVALID_HANDLE; 2828+#ifdef DEBUG_SLOT_SELECTION 2829+ fprintf(stderr, "%s: myslot=%d optype=%d\n", PK11_DBG, myslot, optype); 2830+#endif /* DEBUG_SLOT_SELECTION */ 2831+ rv = pFuncList->C_OpenSession(myslot, CKF_SERIAL_SESSION, 2832+ NULL_PTR, NULL_PTR, &sp->session); 2833+ if (rv == CKR_CRYPTOKI_NOT_INITIALIZED) 2834+ { 2835+ /* 2836+ * We are probably a child process so force the 2837+ * reinitialize of the session 2838+ */ 2839+ pk11_library_initialized = FALSE; 2840+ if (!pk11_library_init(NULL)) 2841+ return (0); 2842+ rv = pFuncList->C_OpenSession(myslot, CKF_SERIAL_SESSION, 2843+ NULL_PTR, NULL_PTR, &sp->session); 2844+ } 2845+ if (rv != CKR_OK) 2846+ { 2847+ PK11err_add_data(PK11_F_SETUP_SESSION, PK11_R_OPENSESSION, rv); 2848+ return (0); 2849+ } 2850+ 2851+ sp->pid = getpid(); 2852+ 2853+ switch (optype) 2854+ { 2855+#ifndef OPENSSL_NO_RSA 2856+ case OP_RSA: 2857+ sp->opdata_rsa_pub_key = CK_INVALID_HANDLE; 2858+ sp->opdata_rsa_priv_key = CK_INVALID_HANDLE; 2859+ sp->opdata_rsa_pub = NULL; 2860+ sp->opdata_rsa_n_num = NULL; 2861+ sp->opdata_rsa_e_num = NULL; 2862+ sp->opdata_rsa_priv = NULL; 2863+ sp->opdata_rsa_pn_num = NULL; 2864+ sp->opdata_rsa_pe_num = NULL; 2865+ sp->opdata_rsa_d_num = NULL; 2866+ break; 2867+#endif /* OPENSSL_NO_RSA */ 2868+#ifndef OPENSSL_NO_DSA 2869+ case OP_DSA: 2870+ sp->opdata_dsa_pub_key = CK_INVALID_HANDLE; 2871+ sp->opdata_dsa_priv_key = CK_INVALID_HANDLE; 2872+ sp->opdata_dsa_pub = NULL; 2873+ sp->opdata_dsa_pub_num = NULL; 2874+ sp->opdata_dsa_priv = NULL; 2875+ sp->opdata_dsa_priv_num = NULL; 2876+ break; 2877+#endif /* OPENSSL_NO_DSA */ 2878+#ifndef OPENSSL_NO_DH 2879+ case OP_DH: 2880+ sp->opdata_dh_key = CK_INVALID_HANDLE; 2881+ sp->opdata_dh = NULL; 2882+ sp->opdata_dh_priv_num = NULL; 2883+ break; 2884+#endif /* OPENSSL_NO_DH */ 2885+ case OP_CIPHER: 2886+ sp->opdata_cipher_key = CK_INVALID_HANDLE; 2887+ sp->opdata_encrypt = -1; 2888+ break; 2889+ default: 2890+ break; 2891+ } 2892+ 2893+ /* 2894+ * We always initialize the session as containing a non-persistent 2895+ * object. The key load functions set it to persistent if that is so. 2896+ */ 2897+ sp->pub_persistent = CK_FALSE; 2898+ sp->priv_persistent = CK_FALSE; 2899+ return (1); 2900+ } 2901+ 2902+#ifndef OPENSSL_NO_RSA 2903+/* Destroy RSA public key from single session. */ 2904+int 2905+pk11_destroy_rsa_object_pub(PK11_SESSION *sp, CK_BBOOL uselock) 2906+ { 2907+ int ret = 0; 2908+ 2909+ if (sp->opdata_rsa_pub_key != CK_INVALID_HANDLE) 2910+ { 2911+ TRY_OBJ_DESTROY(sp, sp->opdata_rsa_pub_key, 2912+ ret, uselock, OP_RSA, CK_FALSE); 2913+ sp->opdata_rsa_pub_key = CK_INVALID_HANDLE; 2914+ sp->opdata_rsa_pub = NULL; 2915+ if (sp->opdata_rsa_n_num != NULL) 2916+ { 2917+ BN_free(sp->opdata_rsa_n_num); 2918+ sp->opdata_rsa_n_num = NULL; 2919+ } 2920+ if (sp->opdata_rsa_e_num != NULL) 2921+ { 2922+ BN_free(sp->opdata_rsa_e_num); 2923+ sp->opdata_rsa_e_num = NULL; 2924+ } 2925+ } 2926+ 2927+ return (ret); 2928+ } 2929+ 2930+/* Destroy RSA private key from single session. */ 2931+int 2932+pk11_destroy_rsa_object_priv(PK11_SESSION *sp, CK_BBOOL uselock) 2933+ { 2934+ int ret = 0; 2935+ 2936+ if (sp->opdata_rsa_priv_key != CK_INVALID_HANDLE) 2937+ { 2938+ TRY_OBJ_DESTROY(sp, sp->opdata_rsa_priv_key, 2939+ ret, uselock, OP_RSA, CK_TRUE); 2940+ sp->opdata_rsa_priv_key = CK_INVALID_HANDLE; 2941+ sp->opdata_rsa_priv = NULL; 2942+ if (sp->opdata_rsa_d_num != NULL) 2943+ { 2944+ BN_free(sp->opdata_rsa_d_num); 2945+ sp->opdata_rsa_d_num = NULL; 2946+ } 2947+ 2948+ /* 2949+ * For the RSA key by reference code, public components 'n'/'e' 2950+ * are the key components we use to check for the cache hit. We 2951+ * must free those as well. 2952+ */ 2953+ if (sp->opdata_rsa_pn_num != NULL) 2954+ { 2955+ BN_free(sp->opdata_rsa_pn_num); 2956+ sp->opdata_rsa_pn_num = NULL; 2957+ } 2958+ if (sp->opdata_rsa_pe_num != NULL) 2959+ { 2960+ BN_free(sp->opdata_rsa_pe_num); 2961+ sp->opdata_rsa_pe_num = NULL; 2962+ } 2963+ } 2964+ 2965+ return (ret); 2966+ } 2967+ 2968+/* 2969+ * Destroy RSA key object wrapper. If session is NULL, try to destroy all 2970+ * objects in the free list. 2971+ */ 2972+int 2973+pk11_destroy_rsa_key_objects(PK11_SESSION *session) 2974+ { 2975+ int ret = 1; 2976+ PK11_SESSION *sp = NULL; 2977+ PK11_SESSION *local_free_session; 2978+ CK_BBOOL uselock = TRUE; 2979+ 2980+ if (session != NULL) 2981+ local_free_session = session; 2982+ else 2983+ { 2984+#ifndef NOPTHREADS 2985+ (void) pthread_mutex_lock(session_cache[OP_RSA].lock); 2986+#else 2987+ CRYPTO_w_lock(CRYPTO_LOCK_PK11_ENGINE); 2988+#endif 2989+ local_free_session = session_cache[OP_RSA].head; 2990+ uselock = FALSE; 2991+ } 2992+ 2993+ /* 2994+ * go through the list of sessions and delete key objects 2995+ */ 2996+ while ((sp = local_free_session) != NULL) 2997+ { 2998+ local_free_session = sp->next; 2999+ 3000+ /* 3001+ * Do not terminate list traversal if one of the 3002+ * destroy operations fails. 3003+ */ 3004+ if (pk11_destroy_rsa_object_pub(sp, uselock) == 0) 3005+ { 3006+ ret = 0; 3007+ continue; 3008+ } 3009+ if (pk11_destroy_rsa_object_priv(sp, uselock) == 0) 3010+ { 3011+ ret = 0; 3012+ continue; 3013+ } 3014+ } 3015+ 3016+#ifndef NOPTHREADS 3017+ if (session == NULL) 3018+ (void) pthread_mutex_unlock(session_cache[OP_RSA].lock); 3019+#else 3020+ if (session == NULL) 3021+ CRYPTO_w_unlock(CRYPTO_LOCK_PK11_ENGINE); 3022+#endif 3023+ 3024+ return (ret); 3025+ } 3026+#endif /* OPENSSL_NO_RSA */ 3027+ 3028+#ifndef OPENSSL_NO_DSA 3029+/* Destroy DSA public key from single session. */ 3030+int 3031+pk11_destroy_dsa_object_pub(PK11_SESSION *sp, CK_BBOOL uselock) 3032+ { 3033+ int ret = 0; 3034+ 3035+ if (sp->opdata_dsa_pub_key != CK_INVALID_HANDLE) 3036+ { 3037+ TRY_OBJ_DESTROY(sp, sp->opdata_dsa_pub_key, 3038+ ret, uselock, OP_DSA, CK_FALSE); 3039+ sp->opdata_dsa_pub_key = CK_INVALID_HANDLE; 3040+ sp->opdata_dsa_pub = NULL; 3041+ if (sp->opdata_dsa_pub_num != NULL) 3042+ { 3043+ BN_free(sp->opdata_dsa_pub_num); 3044+ sp->opdata_dsa_pub_num = NULL; 3045+ } 3046+ } 3047+ 3048+ return (ret); 3049+ } 3050+ 3051+/* Destroy DSA private key from single session. */ 3052+int 3053+pk11_destroy_dsa_object_priv(PK11_SESSION *sp, CK_BBOOL uselock) 3054+ { 3055+ int ret = 0; 3056+ 3057+ if (sp->opdata_dsa_priv_key != CK_INVALID_HANDLE) 3058+ { 3059+ TRY_OBJ_DESTROY(sp, sp->opdata_dsa_priv_key, 3060+ ret, uselock, OP_DSA, CK_TRUE); 3061+ sp->opdata_dsa_priv_key = CK_INVALID_HANDLE; 3062+ sp->opdata_dsa_priv = NULL; 3063+ if (sp->opdata_dsa_priv_num != NULL) 3064+ { 3065+ BN_free(sp->opdata_dsa_priv_num); 3066+ sp->opdata_dsa_priv_num = NULL; 3067+ } 3068+ } 3069+ 3070+ return (ret); 3071+ } 3072+ 3073+/* 3074+ * Destroy DSA key object wrapper. If session is NULL, try to destroy all 3075+ * objects in the free list. 3076+ */ 3077+int 3078+pk11_destroy_dsa_key_objects(PK11_SESSION *session) 3079+ { 3080+ int ret = 1; 3081+ PK11_SESSION *sp = NULL; 3082+ PK11_SESSION *local_free_session; 3083+ CK_BBOOL uselock = TRUE; 3084+ 3085+ if (session != NULL) 3086+ local_free_session = session; 3087+ else 3088+ { 3089+#ifndef NOPTHREADS 3090+ (void) pthread_mutex_lock(session_cache[OP_DSA].lock); 3091+#else 3092+ CRYPTO_w_lock(CRYPTO_LOCK_PK11_ENGINE); 3093+#endif 3094+ local_free_session = session_cache[OP_DSA].head; 3095+ uselock = FALSE; 3096+ } 3097+ 3098+ /* 3099+ * go through the list of sessions and delete key objects 3100+ */ 3101+ while ((sp = local_free_session) != NULL) 3102+ { 3103+ local_free_session = sp->next; 3104+ 3105+ /* 3106+ * Do not terminate list traversal if one of the 3107+ * destroy operations fails. 3108+ */ 3109+ if (pk11_destroy_dsa_object_pub(sp, uselock) == 0) 3110+ { 3111+ ret = 0; 3112+ continue; 3113+ } 3114+ if (pk11_destroy_dsa_object_priv(sp, uselock) == 0) 3115+ { 3116+ ret = 0; 3117+ continue; 3118+ } 3119+ } 3120+ 3121+#ifndef NOPTHREADS 3122+ if (session == NULL) 3123+ (void) pthread_mutex_unlock(session_cache[OP_DSA].lock); 3124+#else 3125+ if (session == NULL) 3126+ CRYPTO_w_unlock(CRYPTO_LOCK_PK11_ENGINE); 3127+#endif 3128+ 3129+ return (ret); 3130+ } 3131+#endif /* OPENSSL_NO_DSA */ 3132+ 3133+#ifndef OPENSSL_NO_DH 3134+/* Destroy DH key from single session. */ 3135+int 3136+pk11_destroy_dh_object(PK11_SESSION *sp, CK_BBOOL uselock) 3137+ { 3138+ int ret = 0; 3139+ 3140+ if (sp->opdata_dh_key != CK_INVALID_HANDLE) 3141+ { 3142+ TRY_OBJ_DESTROY(sp, sp->opdata_dh_key, 3143+ ret, uselock, OP_DH, CK_TRUE); 3144+ sp->opdata_dh_key = CK_INVALID_HANDLE; 3145+ sp->opdata_dh = NULL; 3146+ if (sp->opdata_dh_priv_num != NULL) 3147+ { 3148+ BN_free(sp->opdata_dh_priv_num); 3149+ sp->opdata_dh_priv_num = NULL; 3150+ } 3151+ } 3152+ 3153+ return (ret); 3154+ } 3155+ 3156+/* 3157+ * Destroy DH key object wrapper. 3158+ * 3159+ * arg0: pointer to PKCS#11 engine session structure 3160+ * if session is NULL, try to destroy all objects in the free list 3161+ */ 3162+int 3163+pk11_destroy_dh_key_objects(PK11_SESSION *session) 3164+ { 3165+ int ret = 1; 3166+ PK11_SESSION *sp = NULL; 3167+ PK11_SESSION *local_free_session; 3168+ CK_BBOOL uselock = TRUE; 3169+ 3170+ if (session != NULL) 3171+ local_free_session = session; 3172+ else 3173+ { 3174+#ifndef NOPTHREADS 3175+ (void) pthread_mutex_lock(session_cache[OP_DH].lock); 3176+#else 3177+ CRYPTO_w_lock(CRYPTO_LOCK_PK11_ENGINE); 3178+#endif 3179+ local_free_session = session_cache[OP_DH].head; 3180+ uselock = FALSE; 3181+ } 3182+ 3183+ while ((sp = local_free_session) != NULL) 3184+ { 3185+ local_free_session = sp->next; 3186+ 3187+ /* 3188+ * Do not terminate list traversal if one of the 3189+ * destroy operations fails. 3190+ */ 3191+ if (pk11_destroy_dh_object(sp, uselock) == 0) 3192+ { 3193+ ret = 0; 3194+ continue; 3195+ } 3196+ } 3197+ 3198+#ifndef NOPTHREADS 3199+ if (session == NULL) 3200+ (void) pthread_mutex_unlock(session_cache[OP_DH].lock); 3201+#else 3202+ if (session == NULL) 3203+ CRYPTO_w_unlock(CRYPTO_LOCK_PK11_ENGINE); 3204+#endif 3205+ 3206+ return (ret); 3207+ } 3208+#endif /* OPENSSL_NO_DH */ 3209+ 3210+static int 3211+pk11_destroy_object(CK_SESSION_HANDLE session, CK_OBJECT_HANDLE oh, 3212+ CK_BBOOL persistent) 3213+ { 3214+ CK_RV rv; 3215+ 3216+ /* 3217+ * We never try to destroy persistent objects which are the objects 3218+ * stored in the keystore. Also, we always use read-only sessions so 3219+ * C_DestroyObject() would be returning CKR_SESSION_READ_ONLY here. 3220+ */ 3221+ if (persistent == CK_TRUE) 3222+ return (1); 3223+ 3224+ rv = pFuncList->C_DestroyObject(session, oh); 3225+ if (rv != CKR_OK) 3226+ { 3227+ PK11err_add_data(PK11_F_DESTROY_OBJECT, PK11_R_DESTROYOBJECT, 3228+ rv); 3229+ return (0); 3230+ } 3231+ 3232+ return (1); 3233+ } 3234+ 3235+ 3236+/* Symmetric ciphers and digests support functions */ 3237+ 3238+static int 3239+cipher_nid_to_pk11(int nid) 3240+ { 3241+ int i; 3242+ 3243+ for (i = 0; i < PK11_CIPHER_MAX; i++) 3244+ if (ciphers[i].nid == nid) 3245+ return (ciphers[i].id); 3246+ return (-1); 3247+ } 3248+ 3249+static int 3250+pk11_usable_ciphers(const int **nids) 3251+ { 3252+ if (cipher_count > 0) 3253+ *nids = cipher_nids; 3254+ else 3255+ *nids = NULL; 3256+ return (cipher_count); 3257+ } 3258+ 3259+static int 3260+pk11_usable_digests(const int **nids) 3261+ { 3262+ if (digest_count > 0) 3263+ *nids = digest_nids; 3264+ else 3265+ *nids = NULL; 3266+ return (digest_count); 3267+ } 3268+ 3269+/* 3270+ * Init context for encryption or decryption using a symmetric key. 3271+ */ 3272+static int pk11_init_symmetric(EVP_CIPHER_CTX *ctx, PK11_CIPHER *pcipher, 3273+ PK11_SESSION *sp, CK_MECHANISM_PTR pmech) 3274+ { 3275+ CK_RV rv; 3276+#ifdef SOLARIS_AES_CTR 3277+ CK_AES_CTR_PARAMS ctr_params; 3278+#endif /* SOLARIS_AES_CTR */ 3279+ 3280+ /* 3281+ * We expect pmech->mechanism to be already set and 3282+ * pParameter/ulParameterLen initialized to NULL/0 before 3283+ * pk11_init_symetric() is called. 3284+ */ 3285+ OPENSSL_assert(pmech->mechanism != 0); 3286+ OPENSSL_assert(pmech->pParameter == NULL); 3287+ OPENSSL_assert(pmech->ulParameterLen == 0); 3288+ 3289+#ifdef SOLARIS_AES_CTR 3290+ if (ctx->cipher->nid == NID_aes_128_ctr || 3291+ ctx->cipher->nid == NID_aes_192_ctr || 3292+ ctx->cipher->nid == NID_aes_256_ctr) 3293+ { 3294+ pmech->pParameter = (void *)(&ctr_params); 3295+ pmech->ulParameterLen = sizeof (ctr_params); 3296+ /* 3297+ * For now, we are limited to the fixed length of the counter, 3298+ * it covers the whole counter block. That's what RFC 4344 3299+ * needs. For more information on internal structure of the 3300+ * counter block, see RFC 3686. If needed in the future, we can 3301+ * add code so that the counter length can be set via 3302+ * ENGINE_ctrl() function. 3303+ */ 3304+ ctr_params.ulCounterBits = AES_BLOCK_SIZE * 8; 3305+ OPENSSL_assert(pcipher->iv_len == AES_BLOCK_SIZE); 3306+ (void) memcpy(ctr_params.cb, ctx->iv, AES_BLOCK_SIZE); 3307+ } 3308+ else 3309+#endif /* SOLARIS_AES_CTR */ 3310+ { 3311+ if (pcipher->iv_len > 0) 3312+ { 3313+ pmech->pParameter = (void *)ctx->iv; 3314+ pmech->ulParameterLen = pcipher->iv_len; 3315+ } 3316+ } 3317+ 3318+ /* if we get here, the encryption needs to be reinitialized */ 3319+ if (ctx->encrypt) 3320+ rv = pFuncList->C_EncryptInit(sp->session, pmech, 3321+ sp->opdata_cipher_key); 3322+ else 3323+ rv = pFuncList->C_DecryptInit(sp->session, pmech, 3324+ sp->opdata_cipher_key); 3325+ 3326+ if (rv != CKR_OK) 3327+ { 3328+ PK11err_add_data(PK11_F_CIPHER_INIT, ctx->encrypt ? 3329+ PK11_R_ENCRYPTINIT : PK11_R_DECRYPTINIT, rv); 3330+ pk11_return_session(sp, OP_CIPHER); 3331+ return (0); 3332+ } 3333+ 3334+ return (1); 3335+ } 3336+ 3337+/* ARGSUSED */ 3338+static int 3339+pk11_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key, 3340+ const unsigned char *iv, int enc) 3341+ { 3342+ CK_MECHANISM mech; 3343+ int index; 3344+ PK11_CIPHER_STATE *state = (PK11_CIPHER_STATE *) ctx->cipher_data; 3345+ PK11_SESSION *sp; 3346+ PK11_CIPHER *p_ciph_table_row; 3347+ 3348+ state->sp = NULL; 3349+ 3350+ index = cipher_nid_to_pk11(ctx->cipher->nid); 3351+ if (index < 0 || index >= PK11_CIPHER_MAX) 3352+ return (0); 3353+ 3354+ p_ciph_table_row = &ciphers[index]; 3355+ /* 3356+ * iv_len in the ctx->cipher structure is the maximum IV length for the 3357+ * current cipher and it must be less or equal to the IV length in our 3358+ * ciphers table. The key length must be in the allowed interval. From 3359+ * all cipher modes that the PKCS#11 engine supports only RC4 allows a 3360+ * key length to be in some range, all other NIDs have a precise key 3361+ * length. Every application can define its own EVP functions so this 3362+ * code serves as a sanity check. 3363+ * 3364+ * Note that the reason why the IV length in ctx->cipher might be 3365+ * greater than the actual length is that OpenSSL uses BLOCK_CIPHER_defs 3366+ * macro to define functions that return EVP structures for all DES 3367+ * modes. So, even ECB modes get 8 byte IV. 3368+ */ 3369+ if (ctx->cipher->iv_len < p_ciph_table_row->iv_len || 3370+ ctx->key_len < p_ciph_table_row->min_key_len || 3371+ ctx->key_len > p_ciph_table_row->max_key_len) { 3372+ PK11err(PK11_F_CIPHER_INIT, PK11_R_KEY_OR_IV_LEN_PROBLEM); 3373+ return (0); 3374+ } 3375+ 3376+ if ((sp = pk11_get_session(OP_CIPHER)) == NULL) 3377+ return (0); 3378+ 3379+ /* if applicable, the mechanism parameter is used for IV */ 3380+ mech.mechanism = p_ciph_table_row->mech_type; 3381+ mech.pParameter = NULL; 3382+ mech.ulParameterLen = 0; 3383+ 3384+ /* The key object is destroyed here if it is not the current key. */ 3385+ (void) check_new_cipher_key(sp, key, ctx->key_len); 3386+ 3387+ /* 3388+ * If the key is the same and the encryption is also the same, then 3389+ * just reuse it. However, we must not forget to reinitialize the 3390+ * context that was finalized in pk11_cipher_cleanup(). 3391+ */ 3392+ if (sp->opdata_cipher_key != CK_INVALID_HANDLE && 3393+ sp->opdata_encrypt == ctx->encrypt) 3394+ { 3395+ state->sp = sp; 3396+ if (pk11_init_symmetric(ctx, p_ciph_table_row, sp, &mech) == 0) 3397+ return (0); 3398+ 3399+ return (1); 3400+ } 3401+ 3402+ /* 3403+ * Check if the key has been invalidated. If so, a new key object 3404+ * needs to be created. 3405+ */ 3406+ if (sp->opdata_cipher_key == CK_INVALID_HANDLE) 3407+ { 3408+ sp->opdata_cipher_key = pk11_get_cipher_key( 3409+ ctx, key, p_ciph_table_row->key_type, sp); 3410+ } 3411+ 3412+ if (sp->opdata_encrypt != ctx->encrypt && sp->opdata_encrypt != -1) 3413+ { 3414+ /* 3415+ * The previous encryption/decryption is different. Need to 3416+ * terminate the previous * active encryption/decryption here. 3417+ */ 3418+ if (!pk11_cipher_final(sp)) 3419+ { 3420+ pk11_return_session(sp, OP_CIPHER); 3421+ return (0); 3422+ } 3423+ } 3424+ 3425+ if (sp->opdata_cipher_key == CK_INVALID_HANDLE) 3426+ { 3427+ pk11_return_session(sp, OP_CIPHER); 3428+ return (0); 3429+ } 3430+ 3431+ /* now initialize the context with a new key */ 3432+ if (pk11_init_symmetric(ctx, p_ciph_table_row, sp, &mech) == 0) 3433+ return (0); 3434+ 3435+ sp->opdata_encrypt = ctx->encrypt; 3436+ state->sp = sp; 3437+ 3438+ return (1); 3439+ } 3440+ 3441+/* 3442+ * When reusing the same key in an encryption/decryption session for a 3443+ * decryption/encryption session, we need to close the active session 3444+ * and recreate a new one. Note that the key is in the global session so 3445+ * that it needs not be recreated. 3446+ * 3447+ * It is more appropriate to use C_En/DecryptFinish here. At the time of this 3448+ * development, these two functions in the PKCS#11 libraries used return 3449+ * unexpected errors when passing in 0 length output. It may be a good 3450+ * idea to try them again if performance is a problem here and fix 3451+ * C_En/DecryptFinial if there are bugs there causing the problem. 3452+ */ 3453+static int 3454+pk11_cipher_final(PK11_SESSION *sp) 3455+ { 3456+ CK_RV rv; 3457+ 3458+ rv = pFuncList->C_CloseSession(sp->session); 3459+ if (rv != CKR_OK) 3460+ { 3461+ PK11err_add_data(PK11_F_CIPHER_FINAL, PK11_R_CLOSESESSION, rv); 3462+ return (0); 3463+ } 3464+ 3465+ rv = pFuncList->C_OpenSession(SLOTID, CKF_SERIAL_SESSION, 3466+ NULL_PTR, NULL_PTR, &sp->session); 3467+ if (rv != CKR_OK) 3468+ { 3469+ PK11err_add_data(PK11_F_CIPHER_FINAL, PK11_R_OPENSESSION, rv); 3470+ return (0); 3471+ } 3472+ 3473+ return (1); 3474+ } 3475+ 3476+/* 3477+ * An engine interface function. The calling function allocates sufficient 3478+ * memory for the output buffer "out" to hold the results. 3479+ */ 3480+#if OPENSSL_VERSION_NUMBER < 0x10000000L 3481+static int 3482+pk11_cipher_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, 3483+ const unsigned char *in, unsigned int inl) 3484+#else 3485+static int 3486+pk11_cipher_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, 3487+ const unsigned char *in, size_t inl) 3488+#endif 3489+ { 3490+ PK11_CIPHER_STATE *state = (PK11_CIPHER_STATE *) ctx->cipher_data; 3491+ PK11_SESSION *sp; 3492+ CK_RV rv; 3493+ unsigned long outl = inl; 3494+ 3495+ if (state == NULL || state->sp == NULL) 3496+ return (0); 3497+ 3498+ sp = (PK11_SESSION *) state->sp; 3499+ 3500+ if (!inl) 3501+ return (1); 3502+ 3503+ /* RC4 is the only stream cipher we support */ 3504+ if (ctx->cipher->nid != NID_rc4 && (inl % ctx->cipher->block_size) != 0) 3505+ return (0); 3506+ 3507+ if (ctx->encrypt) 3508+ { 3509+ rv = pFuncList->C_EncryptUpdate(sp->session, 3510+ (unsigned char *)in, inl, out, &outl); 3511+ 3512+ if (rv != CKR_OK) 3513+ { 3514+ PK11err_add_data(PK11_F_CIPHER_DO_CIPHER, 3515+ PK11_R_ENCRYPTUPDATE, rv); 3516+ return (0); 3517+ } 3518+ } 3519+ else 3520+ { 3521+ rv = pFuncList->C_DecryptUpdate(sp->session, 3522+ (unsigned char *)in, inl, out, &outl); 3523+ 3524+ if (rv != CKR_OK) 3525+ { 3526+ PK11err_add_data(PK11_F_CIPHER_DO_CIPHER, 3527+ PK11_R_DECRYPTUPDATE, rv); 3528+ return (0); 3529+ } 3530+ } 3531+ 3532+ /* 3533+ * For DES_CBC, DES3_CBC, AES_CBC, and RC4, the output size is always 3534+ * the same size of input. 3535+ * The application has guaranteed to call the block ciphers with 3536+ * correctly aligned buffers. 3537+ */ 3538+ if (inl != outl) 3539+ return (0); 3540+ 3541+ return (1); 3542+ } 3543+ 3544+/* 3545+ * Return the session to the pool. Calling C_EncryptFinal() and C_DecryptFinal() 3546+ * here is the right thing because in EVP_DecryptFinal_ex(), engine's 3547+ * do_cipher() is not even called, and in EVP_EncryptFinal_ex() it is called but 3548+ * the engine can't find out that it's the finalizing call. We wouldn't 3549+ * necessarily have to finalize the context here since reinitializing it with 3550+ * C_(Encrypt|Decrypt)Init() should be fine but for the sake of correctness, 3551+ * let's do it. Some implementations might leak memory if the previously used 3552+ * context is initialized without finalizing it first. 3553+ */ 3554+static int 3555+pk11_cipher_cleanup(EVP_CIPHER_CTX *ctx) 3556+ { 3557+ CK_RV rv; 3558+ CK_ULONG len = EVP_MAX_BLOCK_LENGTH; 3559+ CK_BYTE buf[EVP_MAX_BLOCK_LENGTH]; 3560+ PK11_CIPHER_STATE *state = ctx->cipher_data; 3561+ 3562+ if (state != NULL && state->sp != NULL) 3563+ { 3564+ /* 3565+ * We are not interested in the data here, we just need to get 3566+ * rid of the context. 3567+ */ 3568+ if (ctx->encrypt) 3569+ rv = pFuncList->C_EncryptFinal( 3570+ state->sp->session, buf, &len); 3571+ else 3572+ rv = pFuncList->C_DecryptFinal( 3573+ state->sp->session, buf, &len); 3574+ 3575+ if (rv != CKR_OK) 3576+ { 3577+ PK11err_add_data(PK11_F_CIPHER_CLEANUP, ctx->encrypt ? 3578+ PK11_R_ENCRYPTFINAL : PK11_R_DECRYPTFINAL, rv); 3579+ pk11_return_session(state->sp, OP_CIPHER); 3580+ return (0); 3581+ } 3582+ 3583+ pk11_return_session(state->sp, OP_CIPHER); 3584+ state->sp = NULL; 3585+ } 3586+ 3587+ return (1); 3588+ } 3589+ 3590+/* 3591+ * Registered by the ENGINE when used to find out how to deal with 3592+ * a particular NID in the ENGINE. This says what we'll do at the 3593+ * top level - note, that list is restricted by what we answer with 3594+ */ 3595+/* ARGSUSED */ 3596+static int 3597+pk11_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher, 3598+ const int **nids, int nid) 3599+ { 3600+ if (!cipher) 3601+ return (pk11_usable_ciphers(nids)); 3602+ 3603+ switch (nid) 3604+ { 3605+ case NID_des_ede3_cbc: 3606+ *cipher = &pk11_3des_cbc; 3607+ break; 3608+ case NID_des_cbc: 3609+ *cipher = &pk11_des_cbc; 3610+ break; 3611+ case NID_des_ede3_ecb: 3612+ *cipher = &pk11_3des_ecb; 3613+ break; 3614+ case NID_des_ecb: 3615+ *cipher = &pk11_des_ecb; 3616+ break; 3617+ case NID_aes_128_cbc: 3618+ *cipher = &pk11_aes_128_cbc; 3619+ break; 3620+ case NID_aes_192_cbc: 3621+ *cipher = &pk11_aes_192_cbc; 3622+ break; 3623+ case NID_aes_256_cbc: 3624+ *cipher = &pk11_aes_256_cbc; 3625+ break; 3626+ case NID_aes_128_ecb: 3627+ *cipher = &pk11_aes_128_ecb; 3628+ break; 3629+ case NID_aes_192_ecb: 3630+ *cipher = &pk11_aes_192_ecb; 3631+ break; 3632+ case NID_aes_256_ecb: 3633+ *cipher = &pk11_aes_256_ecb; 3634+ break; 3635+ case NID_bf_cbc: 3636+ *cipher = &pk11_bf_cbc; 3637+ break; 3638+ case NID_rc4: 3639+ *cipher = &pk11_rc4; 3640+ break; 3641+ default: 3642+#ifdef SOLARIS_AES_CTR 3643+ /* 3644+ * These can't be in separated cases because the NIDs 3645+ * here are not constants. 3646+ */ 3647+ if (nid == NID_aes_128_ctr) 3648+ *cipher = &pk11_aes_128_ctr; 3649+ else if (nid == NID_aes_192_ctr) 3650+ *cipher = &pk11_aes_192_ctr; 3651+ else if (nid == NID_aes_256_ctr) 3652+ *cipher = &pk11_aes_256_ctr; 3653+ else 3654+#endif /* SOLARIS_AES_CTR */ 3655+ *cipher = NULL; 3656+ break; 3657+ } 3658+ return (*cipher != NULL); 3659+ } 3660+ 3661+/* ARGSUSED */ 3662+static int 3663+pk11_engine_digests(ENGINE *e, const EVP_MD **digest, 3664+ const int **nids, int nid) 3665+ { 3666+ if (!digest) 3667+ return (pk11_usable_digests(nids)); 3668+ 3669+ switch (nid) 3670+ { 3671+ case NID_md5: 3672+ *digest = &pk11_md5; 3673+ break; 3674+ case NID_sha1: 3675+ *digest = &pk11_sha1; 3676+ break; 3677+ case NID_sha224: 3678+ *digest = &pk11_sha224; 3679+ break; 3680+ case NID_sha256: 3681+ *digest = &pk11_sha256; 3682+ break; 3683+ case NID_sha384: 3684+ *digest = &pk11_sha384; 3685+ break; 3686+ case NID_sha512: 3687+ *digest = &pk11_sha512; 3688+ break; 3689+ default: 3690+ *digest = NULL; 3691+ break; 3692+ } 3693+ return (*digest != NULL); 3694+ } 3695+ 3696+ 3697+/* Create a secret key object in a PKCS#11 session */ 3698+static CK_OBJECT_HANDLE pk11_get_cipher_key(EVP_CIPHER_CTX *ctx, 3699+ const unsigned char *key, CK_KEY_TYPE key_type, PK11_SESSION *sp) 3700+ { 3701+ CK_RV rv; 3702+ CK_OBJECT_HANDLE h_key = CK_INVALID_HANDLE; 3703+ CK_OBJECT_CLASS obj_key = CKO_SECRET_KEY; 3704+ CK_ULONG ul_key_attr_count = 6; 3705+ 3706+ CK_ATTRIBUTE a_key_template[] = 3707+ { 3708+ {CKA_CLASS, (void*) NULL, sizeof (CK_OBJECT_CLASS)}, 3709+ {CKA_KEY_TYPE, (void*) NULL, sizeof (CK_KEY_TYPE)}, 3710+ {CKA_TOKEN, &false, sizeof (false)}, 3711+ {CKA_ENCRYPT, &true, sizeof (true)}, 3712+ {CKA_DECRYPT, &true, sizeof (true)}, 3713+ {CKA_VALUE, (void*) NULL, 0}, 3714+ }; 3715+ 3716+ /* 3717+ * Create secret key object in global_session. All other sessions 3718+ * can use the key handles. Here is why: 3719+ * OpenSSL will call EncryptInit and EncryptUpdate using a secret key. 3720+ * It may then call DecryptInit and DecryptUpdate using the same key. 3721+ * To use the same key object, we need to call EncryptFinal with 3722+ * a 0 length message. Currently, this does not work for 3DES 3723+ * mechanism. To get around this problem, we close the session and 3724+ * then create a new session to use the same key object. When a session 3725+ * is closed, all the object handles will be invalid. Thus, create key 3726+ * objects in a global session, an individual session may be closed to 3727+ * terminate the active operation. 3728+ */ 3729+ CK_SESSION_HANDLE session = global_session; 3730+ a_key_template[0].pValue = &obj_key; 3731+ a_key_template[1].pValue = &key_type; 3732+ a_key_template[5].pValue = (void *) key; 3733+ a_key_template[5].ulValueLen = (unsigned long) ctx->key_len; 3734+ 3735+ rv = pFuncList->C_CreateObject(session, 3736+ a_key_template, ul_key_attr_count, &h_key); 3737+ if (rv != CKR_OK) 3738+ { 3739+ PK11err_add_data(PK11_F_GET_CIPHER_KEY, PK11_R_CREATEOBJECT, 3740+ rv); 3741+ goto err; 3742+ } 3743+ 3744+ /* 3745+ * Save the key information used in this session. 3746+ * The max can be saved is PK11_KEY_LEN_MAX. 3747+ */ 3748+ sp->opdata_key_len = ctx->key_len > PK11_KEY_LEN_MAX ? 3749+ PK11_KEY_LEN_MAX : ctx->key_len; 3750+ (void) memcpy(sp->opdata_key, key, sp->opdata_key_len); 3751+err: 3752+ 3753+ return (h_key); 3754+ } 3755+ 3756+static int 3757+md_nid_to_pk11(int nid) 3758+ { 3759+ int i; 3760+ 3761+ for (i = 0; i < PK11_DIGEST_MAX; i++) 3762+ if (digests[i].nid == nid) 3763+ return (digests[i].id); 3764+ return (-1); 3765+ } 3766+ 3767+static int 3768+pk11_digest_init(EVP_MD_CTX *ctx) 3769+ { 3770+ CK_RV rv; 3771+ CK_MECHANISM mech; 3772+ int index; 3773+ PK11_SESSION *sp; 3774+ PK11_DIGEST *pdp; 3775+ PK11_CIPHER_STATE *state = (PK11_CIPHER_STATE *) ctx->md_data; 3776+ 3777+ state->sp = NULL; 3778+ 3779+ index = md_nid_to_pk11(ctx->digest->type); 3780+ if (index < 0 || index >= PK11_DIGEST_MAX) 3781+ return (0); 3782+ 3783+ pdp = &digests[index]; 3784+ if ((sp = pk11_get_session(OP_DIGEST)) == NULL) 3785+ return (0); 3786+ 3787+ /* at present, no parameter is needed for supported digests */ 3788+ mech.mechanism = pdp->mech_type; 3789+ mech.pParameter = NULL; 3790+ mech.ulParameterLen = 0; 3791+ 3792+ rv = pFuncList->C_DigestInit(sp->session, &mech); 3793+ 3794+ if (rv != CKR_OK) 3795+ { 3796+ PK11err_add_data(PK11_F_DIGEST_INIT, PK11_R_DIGESTINIT, rv); 3797+ pk11_return_session(sp, OP_DIGEST); 3798+ return (0); 3799+ } 3800+ 3801+ state->sp = sp; 3802+ 3803+ return (1); 3804+ } 3805+ 3806+static int 3807+pk11_digest_update(EVP_MD_CTX *ctx, const void *data, size_t count) 3808+ { 3809+ CK_RV rv; 3810+ PK11_CIPHER_STATE *state = (PK11_CIPHER_STATE *) ctx->md_data; 3811+ 3812+ /* 0 length message will cause a failure in C_DigestFinal */ 3813+ if (count == 0) 3814+ return (1); 3815+ 3816+ if (state == NULL || state->sp == NULL) 3817+ return (0); 3818+ 3819+ rv = pFuncList->C_DigestUpdate(state->sp->session, (CK_BYTE *) data, 3820+ count); 3821+ 3822+ if (rv != CKR_OK) 3823+ { 3824+ PK11err_add_data(PK11_F_DIGEST_UPDATE, PK11_R_DIGESTUPDATE, rv); 3825+ pk11_return_session(state->sp, OP_DIGEST); 3826+ state->sp = NULL; 3827+ return (0); 3828+ } 3829+ 3830+ return (1); 3831+ } 3832+ 3833+static int 3834+pk11_digest_final(EVP_MD_CTX *ctx, unsigned char *md) 3835+ { 3836+ CK_RV rv; 3837+ unsigned long len; 3838+ PK11_CIPHER_STATE *state = (PK11_CIPHER_STATE *) ctx->md_data; 3839+ len = ctx->digest->md_size; 3840+ 3841+ if (state == NULL || state->sp == NULL) 3842+ return (0); 3843+ 3844+ rv = pFuncList->C_DigestFinal(state->sp->session, md, &len); 3845+ 3846+ if (rv != CKR_OK) 3847+ { 3848+ PK11err_add_data(PK11_F_DIGEST_FINAL, PK11_R_DIGESTFINAL, rv); 3849+ pk11_return_session(state->sp, OP_DIGEST); 3850+ state->sp = NULL; 3851+ return (0); 3852+ } 3853+ 3854+ if (ctx->digest->md_size != len) 3855+ return (0); 3856+ 3857+ /* 3858+ * Final is called and digest is returned, so return the session 3859+ * to the pool 3860+ */ 3861+ pk11_return_session(state->sp, OP_DIGEST); 3862+ state->sp = NULL; 3863+ 3864+ return (1); 3865+ } 3866+ 3867+static int 3868+pk11_digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from) 3869+ { 3870+ CK_RV rv; 3871+ int ret = 0; 3872+ PK11_CIPHER_STATE *state, *state_to; 3873+ CK_BYTE_PTR pstate = NULL; 3874+ CK_ULONG ul_state_len; 3875+ 3876+ /* The copy-from state */ 3877+ state = (PK11_CIPHER_STATE *) from->md_data; 3878+ if (state == NULL || state->sp == NULL) 3879+ goto err; 3880+ 3881+ /* Initialize the copy-to state */ 3882+ if (!pk11_digest_init(to)) 3883+ goto err; 3884+ state_to = (PK11_CIPHER_STATE *) to->md_data; 3885+ 3886+ /* Get the size of the operation state of the copy-from session */ 3887+ rv = pFuncList->C_GetOperationState(state->sp->session, NULL, 3888+ &ul_state_len); 3889+ 3890+ if (rv != CKR_OK) 3891+ { 3892+ PK11err_add_data(PK11_F_DIGEST_COPY, PK11_R_GET_OPERATION_STATE, 3893+ rv); 3894+ goto err; 3895+ } 3896+ if (ul_state_len == 0) 3897+ { 3898+ goto err; 3899+ } 3900+ 3901+ pstate = OPENSSL_malloc(ul_state_len); 3902+ if (pstate == NULL) 3903+ { 3904+ PK11err(PK11_F_DIGEST_COPY, PK11_R_MALLOC_FAILURE); 3905+ goto err; 3906+ } 3907+ 3908+ /* Get the operation state of the copy-from session */ 3909+ rv = pFuncList->C_GetOperationState(state->sp->session, pstate, 3910+ &ul_state_len); 3911+ 3912+ if (rv != CKR_OK) 3913+ { 3914+ PK11err_add_data(PK11_F_DIGEST_COPY, PK11_R_GET_OPERATION_STATE, 3915+ rv); 3916+ goto err; 3917+ } 3918+ 3919+ /* Set the operation state of the copy-to session */ 3920+ rv = pFuncList->C_SetOperationState(state_to->sp->session, pstate, 3921+ ul_state_len, 0, 0); 3922+ 3923+ if (rv != CKR_OK) 3924+ { 3925+ PK11err_add_data(PK11_F_DIGEST_COPY, 3926+ PK11_R_SET_OPERATION_STATE, rv); 3927+ goto err; 3928+ } 3929+ 3930+ ret = 1; 3931+err: 3932+ if (pstate != NULL) 3933+ OPENSSL_free(pstate); 3934+ 3935+ return (ret); 3936+ } 3937+ 3938+/* Return any pending session state to the pool */ 3939+static int 3940+pk11_digest_cleanup(EVP_MD_CTX *ctx) 3941+ { 3942+ PK11_CIPHER_STATE *state = ctx->md_data; 3943+ unsigned char buf[EVP_MAX_MD_SIZE]; 3944+ 3945+ if (state != NULL && state->sp != NULL) 3946+ { 3947+ /* 3948+ * If state->sp is not NULL then pk11_digest_final() has not 3949+ * been called yet. We must call it now to free any memory 3950+ * that might have been allocated in the token when 3951+ * pk11_digest_init() was called. pk11_digest_final() 3952+ * will return the session to the cache. 3953+ */ 3954+ if (!pk11_digest_final(ctx, buf)) 3955+ return (0); 3956+ } 3957+ 3958+ return (1); 3959+ } 3960+ 3961+/* 3962+ * Check if the new key is the same as the key object in the session. If the key 3963+ * is the same, no need to create a new key object. Otherwise, the old key 3964+ * object needs to be destroyed and a new one will be created. Return 1 for 3965+ * cache hit, 0 for cache miss. Note that we must check the key length first 3966+ * otherwise we could end up reusing a different, longer key with the same 3967+ * prefix. 3968+ */ 3969+static int check_new_cipher_key(PK11_SESSION *sp, const unsigned char *key, 3970+ int key_len) 3971+ { 3972+ if (sp->opdata_key_len != key_len || 3973+ memcmp(sp->opdata_key, key, key_len) != 0) 3974+ { 3975+ (void) pk11_destroy_cipher_key_objects(sp); 3976+ return (0); 3977+ } 3978+ return (1); 3979+ } 3980+ 3981+/* Destroy one or more secret key objects. */ 3982+static int pk11_destroy_cipher_key_objects(PK11_SESSION *session) 3983+ { 3984+ int ret = 0; 3985+ PK11_SESSION *sp = NULL; 3986+ PK11_SESSION *local_free_session; 3987+ 3988+ if (session != NULL) 3989+ local_free_session = session; 3990+ else 3991+ { 3992+#ifndef NOPTHREADS 3993+ (void) pthread_mutex_lock(session_cache[OP_CIPHER].lock); 3994+#else 3995+ CRYPTO_w_lock(CRYPTO_LOCK_PK11_ENGINE); 3996+#endif 3997+ local_free_session = session_cache[OP_CIPHER].head; 3998+ } 3999+ 4000+ while ((sp = local_free_session) != NULL) 4001+ { 4002+ local_free_session = sp->next; 4003+ 4004+ if (sp->opdata_cipher_key != CK_INVALID_HANDLE) 4005+ { 4006+ /* 4007+ * The secret key object is created in the 4008+ * global_session. See pk11_get_cipher_key(). 4009+ */ 4010+ if (pk11_destroy_object(global_session, 4011+ sp->opdata_cipher_key, CK_FALSE) == 0) 4012+ goto err; 4013+ sp->opdata_cipher_key = CK_INVALID_HANDLE; 4014+ } 4015+ } 4016+ ret = 1; 4017+err: 4018+ 4019+#ifndef NOPTHREADS 4020+ if (session == NULL) 4021+ (void) pthread_mutex_unlock(session_cache[OP_CIPHER].lock); 4022+#else 4023+ if (session == NULL) 4024+ CRYPTO_w_unlock(CRYPTO_LOCK_PK11_ENGINE); 4025+#endif 4026+ 4027+ return (ret); 4028+ } 4029+ 4030+ 4031+/* 4032+ * Public key mechanisms optionally supported 4033+ * 4034+ * CKM_RSA_X_509 4035+ * CKM_RSA_PKCS 4036+ * CKM_DSA 4037+ * 4038+ * The first slot that supports at least one of those mechanisms is chosen as a 4039+ * public key slot. 4040+ * 4041+ * Symmetric ciphers optionally supported 4042+ * 4043+ * CKM_DES3_CBC 4044+ * CKM_DES_CBC 4045+ * CKM_AES_CBC 4046+ * CKM_DES3_ECB 4047+ * CKM_DES_ECB 4048+ * CKM_AES_ECB 4049+ * CKM_AES_CTR 4050+ * CKM_RC4 4051+ * CKM_BLOWFISH_CBC 4052+ * 4053+ * Digests optionally supported 4054+ * 4055+ * CKM_MD5 4056+ * CKM_SHA_1 4057+ * CKM_SHA224 4058+ * CKM_SHA256 4059+ * CKM_SHA384 4060+ * CKM_SHA512 4061+ * 4062+ * The output of this function is a set of global variables indicating which 4063+ * mechanisms from RSA, DSA, DH and RAND are present, and also two arrays of 4064+ * mechanisms, one for symmetric ciphers and one for digests. Also, 3 global 4065+ * variables carry information about which slot was chosen for (a) public key 4066+ * mechanisms, (b) random operations, and (c) symmetric ciphers and digests. 4067+ */ 4068+static int 4069+pk11_choose_slots(int *any_slot_found) 4070+ { 4071+ CK_SLOT_ID_PTR pSlotList = NULL_PTR; 4072+ CK_ULONG ulSlotCount = 0; 4073+ CK_MECHANISM_INFO mech_info; 4074+ CK_TOKEN_INFO token_info; 4075+ unsigned int i; 4076+ CK_RV rv; 4077+ CK_SLOT_ID best_slot_sofar = 0; 4078+ CK_BBOOL found_candidate_slot = CK_FALSE; 4079+ int slot_n_cipher = 0; 4080+ int slot_n_digest = 0; 4081+ CK_SLOT_ID current_slot = 0; 4082+ int current_slot_n_cipher = 0; 4083+ int current_slot_n_digest = 0; 4084+ 4085+ int local_cipher_nids[PK11_CIPHER_MAX]; 4086+ int local_digest_nids[PK11_DIGEST_MAX]; 4087+ 4088+ /* let's initialize the output parameter */ 4089+ if (any_slot_found != NULL) 4090+ *any_slot_found = 0; 4091+ 4092+ /* Get slot list for memory allocation */ 4093+ rv = pFuncList->C_GetSlotList(CK_FALSE, NULL_PTR, &ulSlotCount); 4094+ 4095+ if (rv != CKR_OK) 4096+ { 4097+ PK11err_add_data(PK11_F_CHOOSE_SLOT, PK11_R_GETSLOTLIST, rv); 4098+ return (0); 4099+ } 4100+ 4101+ /* it's not an error if we didn't find any providers */ 4102+ if (ulSlotCount == 0) 4103+ { 4104+#ifdef DEBUG_SLOT_SELECTION 4105+ fprintf(stderr, "%s: no crypto providers found\n", PK11_DBG); 4106+#endif /* DEBUG_SLOT_SELECTION */ 4107+ return (1); 4108+ } 4109+ 4110+ pSlotList = OPENSSL_malloc(ulSlotCount * sizeof (CK_SLOT_ID)); 4111+ 4112+ if (pSlotList == NULL) 4113+ { 4114+ PK11err(PK11_F_CHOOSE_SLOT, PK11_R_MALLOC_FAILURE); 4115+ return (0); 4116+ } 4117+ 4118+ /* Get the slot list for processing */ 4119+ rv = pFuncList->C_GetSlotList(CK_FALSE, pSlotList, &ulSlotCount); 4120+ if (rv != CKR_OK) 4121+ { 4122+ PK11err_add_data(PK11_F_CHOOSE_SLOT, PK11_R_GETSLOTLIST, rv); 4123+ OPENSSL_free(pSlotList); 4124+ return (0); 4125+ } 4126+ 4127+#ifdef DEBUG_SLOT_SELECTION 4128+ fprintf(stderr, "%s: provider: %s\n", PK11_DBG, def_PK11_LIBNAME); 4129+ fprintf(stderr, "%s: number of slots: %d\n", PK11_DBG, ulSlotCount); 4130+ 4131+ fprintf(stderr, "%s: == checking rand slots ==\n", PK11_DBG); 4132+#endif /* DEBUG_SLOT_SELECTION */ 4133+ for (i = 0; i < ulSlotCount; i++) 4134+ { 4135+ current_slot = pSlotList[i]; 4136+ 4137+#ifdef DEBUG_SLOT_SELECTION 4138+ fprintf(stderr, "%s: checking slot: %d\n", PK11_DBG, i); 4139+#endif /* DEBUG_SLOT_SELECTION */ 4140+ /* Check if slot has random support. */ 4141+ rv = pFuncList->C_GetTokenInfo(current_slot, &token_info); 4142+ if (rv != CKR_OK) 4143+ continue; 4144+ 4145+#ifdef DEBUG_SLOT_SELECTION 4146+ fprintf(stderr, "%s: token label: %.32s\n", PK11_DBG, token_info.label); 4147+#endif /* DEBUG_SLOT_SELECTION */ 4148+ 4149+ if (token_info.flags & CKF_RNG) 4150+ { 4151+#ifdef DEBUG_SLOT_SELECTION 4152+ fprintf(stderr, "%s: this token has CKF_RNG flag\n", PK11_DBG); 4153+#endif /* DEBUG_SLOT_SELECTION */ 4154+ pk11_have_random = CK_TRUE; 4155+ rand_SLOTID = current_slot; 4156+ break; 4157+ } 4158+ } 4159+ 4160+#ifdef DEBUG_SLOT_SELECTION 4161+ fprintf(stderr, "%s: == checking pubkey slots ==\n", PK11_DBG); 4162+#endif /* DEBUG_SLOT_SELECTION */ 4163+ 4164+ pubkey_SLOTID = pSlotList[0]; 4165+ for (i = 0; i < ulSlotCount; i++) 4166+ { 4167+ CK_BBOOL slot_has_rsa = CK_FALSE; 4168+ CK_BBOOL slot_has_recover = CK_FALSE; 4169+ CK_BBOOL slot_has_dsa = CK_FALSE; 4170+ CK_BBOOL slot_has_dh = CK_FALSE; 4171+ current_slot = pSlotList[i]; 4172+ 4173+#ifdef DEBUG_SLOT_SELECTION 4174+ fprintf(stderr, "%s: checking slot: %d\n", PK11_DBG, i); 4175+#endif /* DEBUG_SLOT_SELECTION */ 4176+ rv = pFuncList->C_GetTokenInfo(current_slot, &token_info); 4177+ if (rv != CKR_OK) 4178+ continue; 4179+ 4180+#ifdef DEBUG_SLOT_SELECTION 4181+ fprintf(stderr, "%s: token label: %.32s\n", PK11_DBG, token_info.label); 4182+#endif /* DEBUG_SLOT_SELECTION */ 4183+ 4184+#ifndef OPENSSL_NO_RSA 4185+ /* 4186+ * Check if this slot is capable of signing and 4187+ * verifying with CKM_RSA_PKCS. 4188+ */ 4189+ rv = pFuncList->C_GetMechanismInfo(current_slot, CKM_RSA_PKCS, 4190+ &mech_info); 4191+ 4192+ if (rv == CKR_OK && ((mech_info.flags & CKF_SIGN) && 4193+ (mech_info.flags & CKF_VERIFY))) 4194+ { 4195+ /* 4196+ * Check if this slot is capable of encryption, 4197+ * decryption, sign, and verify with CKM_RSA_X_509. 4198+ */ 4199+ rv = pFuncList->C_GetMechanismInfo(current_slot, 4200+ CKM_RSA_X_509, &mech_info); 4201+ 4202+ if (rv == CKR_OK && ((mech_info.flags & CKF_SIGN) && 4203+ (mech_info.flags & CKF_VERIFY) && 4204+ (mech_info.flags & CKF_ENCRYPT) && 4205+ (mech_info.flags & CKF_DECRYPT))) 4206+ { 4207+ slot_has_rsa = CK_TRUE; 4208+ if (mech_info.flags & CKF_VERIFY_RECOVER) 4209+ { 4210+ slot_has_recover = CK_TRUE; 4211+ } 4212+ } 4213+ } 4214+#endif /* OPENSSL_NO_RSA */ 4215+ 4216+#ifndef OPENSSL_NO_DSA 4217+ /* 4218+ * Check if this slot is capable of signing and 4219+ * verifying with CKM_DSA. 4220+ */ 4221+ rv = pFuncList->C_GetMechanismInfo(current_slot, CKM_DSA, 4222+ &mech_info); 4223+ if (rv == CKR_OK && ((mech_info.flags & CKF_SIGN) && 4224+ (mech_info.flags & CKF_VERIFY))) 4225+ { 4226+ slot_has_dsa = CK_TRUE; 4227+ } 4228+ 4229+#endif /* OPENSSL_NO_DSA */ 4230+ 4231+#ifndef OPENSSL_NO_DH 4232+ /* 4233+ * Check if this slot is capable of DH key generataion and 4234+ * derivation. 4235+ */ 4236+ rv = pFuncList->C_GetMechanismInfo(current_slot, 4237+ CKM_DH_PKCS_KEY_PAIR_GEN, &mech_info); 4238+ 4239+ if (rv == CKR_OK && (mech_info.flags & CKF_GENERATE_KEY_PAIR)) 4240+ { 4241+ rv = pFuncList->C_GetMechanismInfo(current_slot, 4242+ CKM_DH_PKCS_DERIVE, &mech_info); 4243+ if (rv == CKR_OK && (mech_info.flags & CKF_DERIVE)) 4244+ { 4245+ slot_has_dh = CK_TRUE; 4246+ } 4247+ } 4248+#endif /* OPENSSL_NO_DH */ 4249+ 4250+ if (!found_candidate_slot && 4251+ (slot_has_rsa || slot_has_dsa || slot_has_dh)) 4252+ { 4253+#ifdef DEBUG_SLOT_SELECTION 4254+ fprintf(stderr, 4255+ "%s: potential slot: %d\n", PK11_DBG, current_slot); 4256+#endif /* DEBUG_SLOT_SELECTION */ 4257+ best_slot_sofar = current_slot; 4258+ pk11_have_rsa = slot_has_rsa; 4259+ pk11_have_recover = slot_has_recover; 4260+ pk11_have_dsa = slot_has_dsa; 4261+ pk11_have_dh = slot_has_dh; 4262+ found_candidate_slot = CK_TRUE; 4263+ /* 4264+ * Cache the flags for later use. We might 4265+ * need those if RSA keys by reference feature 4266+ * is used. 4267+ */ 4268+ pubkey_token_flags = token_info.flags; 4269+#ifdef DEBUG_SLOT_SELECTION 4270+ fprintf(stderr, 4271+ "%s: setting found_candidate_slot to CK_TRUE\n", 4272+ PK11_DBG); 4273+ fprintf(stderr, 4274+ "%s: best so far slot: %d\n", PK11_DBG, 4275+ best_slot_sofar); 4276+ fprintf(stderr, "%s: pubkey flags changed to " 4277+ "%lu.\n", PK11_DBG, pubkey_token_flags); 4278+ } 4279+ else 4280+ { 4281+ fprintf(stderr, 4282+ "%s: no rsa/dsa/dh\n", PK11_DBG); 4283+ } 4284+#else 4285+ } /* if */ 4286+#endif /* DEBUG_SLOT_SELECTION */ 4287+ } /* for */ 4288+ 4289+ if (found_candidate_slot == CK_TRUE) 4290+ { 4291+ pubkey_SLOTID = best_slot_sofar; 4292+ } 4293+ 4294+ found_candidate_slot = CK_FALSE; 4295+ best_slot_sofar = 0; 4296+ 4297+#ifdef DEBUG_SLOT_SELECTION 4298+ fprintf(stderr, "%s: == checking cipher/digest ==\n", PK11_DBG); 4299+#endif /* DEBUG_SLOT_SELECTION */ 4300+ 4301+ SLOTID = pSlotList[0]; 4302+ for (i = 0; i < ulSlotCount; i++) 4303+ { 4304+#ifdef DEBUG_SLOT_SELECTION 4305+ fprintf(stderr, "%s: checking slot: %d\n", PK11_DBG, i); 4306+#endif /* DEBUG_SLOT_SELECTION */ 4307+ 4308+ current_slot = pSlotList[i]; 4309+ current_slot_n_cipher = 0; 4310+ current_slot_n_digest = 0; 4311+ (void) memset(local_cipher_nids, 0, sizeof (local_cipher_nids)); 4312+ (void) memset(local_digest_nids, 0, sizeof (local_digest_nids)); 4313+ 4314+ pk11_find_symmetric_ciphers(pFuncList, current_slot, 4315+ ¤t_slot_n_cipher, local_cipher_nids); 4316+ 4317+ pk11_find_digests(pFuncList, current_slot, 4318+ ¤t_slot_n_digest, local_digest_nids); 4319+ 4320+#ifdef DEBUG_SLOT_SELECTION 4321+ fprintf(stderr, "%s: current_slot_n_cipher %d\n", PK11_DBG, 4322+ current_slot_n_cipher); 4323+ fprintf(stderr, "%s: current_slot_n_digest %d\n", PK11_DBG, 4324+ current_slot_n_digest); 4325+ fprintf(stderr, "%s: best so far cipher/digest slot: %d\n", 4326+ PK11_DBG, best_slot_sofar); 4327+#endif /* DEBUG_SLOT_SELECTION */ 4328+ 4329+ /* 4330+ * If the current slot supports more ciphers/digests than 4331+ * the previous best one we change the current best to this one, 4332+ * otherwise leave it where it is. 4333+ */ 4334+ if ((current_slot_n_cipher + current_slot_n_digest) > 4335+ (slot_n_cipher + slot_n_digest)) 4336+ { 4337+#ifdef DEBUG_SLOT_SELECTION 4338+ fprintf(stderr, 4339+ "%s: changing best so far slot to %d\n", 4340+ PK11_DBG, current_slot); 4341+#endif /* DEBUG_SLOT_SELECTION */ 4342+ best_slot_sofar = SLOTID = current_slot; 4343+ cipher_count = slot_n_cipher = current_slot_n_cipher; 4344+ digest_count = slot_n_digest = current_slot_n_digest; 4345+ (void) memcpy(cipher_nids, local_cipher_nids, 4346+ sizeof (local_cipher_nids)); 4347+ (void) memcpy(digest_nids, local_digest_nids, 4348+ sizeof (local_digest_nids)); 4349+ } 4350+ } 4351+ 4352+#ifdef DEBUG_SLOT_SELECTION 4353+ fprintf(stderr, 4354+ "%s: chosen pubkey slot: %d\n", PK11_DBG, pubkey_SLOTID); 4355+ fprintf(stderr, 4356+ "%s: chosen rand slot: %d\n", PK11_DBG, rand_SLOTID); 4357+ fprintf(stderr, 4358+ "%s: chosen cipher/digest slot: %d\n", PK11_DBG, SLOTID); 4359+ fprintf(stderr, 4360+ "%s: pk11_have_rsa %d\n", PK11_DBG, pk11_have_rsa); 4361+ fprintf(stderr, 4362+ "%s: pk11_have_recover %d\n", PK11_DBG, pk11_have_recover); 4363+ fprintf(stderr, 4364+ "%s: pk11_have_dsa %d\n", PK11_DBG, pk11_have_dsa); 4365+ fprintf(stderr, 4366+ "%s: pk11_have_dh %d\n", PK11_DBG, pk11_have_dh); 4367+ fprintf(stderr, 4368+ "%s: pk11_have_random %d\n", PK11_DBG, pk11_have_random); 4369+ fprintf(stderr, 4370+ "%s: cipher_count %d\n", PK11_DBG, cipher_count); 4371+ fprintf(stderr, 4372+ "%s: digest_count %d\n", PK11_DBG, digest_count); 4373+#endif /* DEBUG_SLOT_SELECTION */ 4374+ 4375+ if (pSlotList != NULL) 4376+ OPENSSL_free(pSlotList); 4377+ 4378+#ifdef SOLARIS_HW_SLOT_SELECTION 4379+ OPENSSL_free(hw_cnids); 4380+ OPENSSL_free(hw_dnids); 4381+#endif /* SOLARIS_HW_SLOT_SELECTION */ 4382+ 4383+ if (any_slot_found != NULL) 4384+ *any_slot_found = 1; 4385+ return (1); 4386+ } 4387+ 4388+static void pk11_get_symmetric_cipher(CK_FUNCTION_LIST_PTR pflist, 4389+ int slot_id, CK_MECHANISM_TYPE mech, int *current_slot_n_cipher, 4390+ int *local_cipher_nids, int id) 4391+ { 4392+ CK_MECHANISM_INFO mech_info; 4393+ CK_RV rv; 4394+ 4395+#ifdef DEBUG_SLOT_SELECTION 4396+ fprintf(stderr, "%s: checking mech: %x", PK11_DBG, mech); 4397+#endif /* DEBUG_SLOT_SELECTION */ 4398+ rv = pflist->C_GetMechanismInfo(slot_id, mech, &mech_info); 4399+ 4400+ if (rv != CKR_OK) 4401+ { 4402+#ifdef DEBUG_SLOT_SELECTION 4403+ fprintf(stderr, " not found\n"); 4404+#endif /* DEBUG_SLOT_SELECTION */ 4405+ return; 4406+ } 4407+ 4408+ if ((mech_info.flags & CKF_ENCRYPT) && 4409+ (mech_info.flags & CKF_DECRYPT)) 4410+ { 4411+#ifdef SOLARIS_HW_SLOT_SELECTION 4412+ if (nid_in_table(ciphers[id].nid, hw_cnids)) 4413+#endif /* SOLARIS_HW_SLOT_SELECTION */ 4414+ { 4415+#ifdef DEBUG_SLOT_SELECTION 4416+ fprintf(stderr, " usable\n"); 4417+#endif /* DEBUG_SLOT_SELECTION */ 4418+ local_cipher_nids[(*current_slot_n_cipher)++] = 4419+ ciphers[id].nid; 4420+ } 4421+#ifdef SOLARIS_HW_SLOT_SELECTION 4422+#ifdef DEBUG_SLOT_SELECTION 4423+ else 4424+ { 4425+ fprintf(stderr, " rejected, software implementation only\n"); 4426+ } 4427+#endif /* DEBUG_SLOT_SELECTION */ 4428+#endif /* SOLARIS_HW_SLOT_SELECTION */ 4429+ } 4430+#ifdef DEBUG_SLOT_SELECTION 4431+ else 4432+ { 4433+ fprintf(stderr, " unusable\n"); 4434+ } 4435+#endif /* DEBUG_SLOT_SELECTION */ 4436+ 4437+ return; 4438+ } 4439+ 4440+static void pk11_get_digest(CK_FUNCTION_LIST_PTR pflist, int slot_id, 4441+ CK_MECHANISM_TYPE mech, int *current_slot_n_digest, int *local_digest_nids, 4442+ int id) 4443+ { 4444+ CK_MECHANISM_INFO mech_info; 4445+ CK_RV rv; 4446+ 4447+#ifdef DEBUG_SLOT_SELECTION 4448+ fprintf(stderr, "%s: checking mech: %x", PK11_DBG, mech); 4449+#endif /* DEBUG_SLOT_SELECTION */ 4450+ rv = pflist->C_GetMechanismInfo(slot_id, mech, &mech_info); 4451+ 4452+ if (rv != CKR_OK) 4453+ { 4454+#ifdef DEBUG_SLOT_SELECTION 4455+ fprintf(stderr, " not found\n"); 4456+#endif /* DEBUG_SLOT_SELECTION */ 4457+ return; 4458+ } 4459+ 4460+ if (mech_info.flags & CKF_DIGEST) 4461+ { 4462+#ifdef SOLARIS_HW_SLOT_SELECTION 4463+ if (nid_in_table(digests[id].nid, hw_dnids)) 4464+#endif /* SOLARIS_HW_SLOT_SELECTION */ 4465+ { 4466+#ifdef DEBUG_SLOT_SELECTION 4467+ fprintf(stderr, " usable\n"); 4468+#endif /* DEBUG_SLOT_SELECTION */ 4469+ local_digest_nids[(*current_slot_n_digest)++] = 4470+ digests[id].nid; 4471+ } 4472+#ifdef SOLARIS_HW_SLOT_SELECTION 4473+#ifdef DEBUG_SLOT_SELECTION 4474+ else 4475+ { 4476+ fprintf(stderr, " rejected, software implementation only\n"); 4477+ } 4478+#endif /* DEBUG_SLOT_SELECTION */ 4479+#endif /* SOLARIS_HW_SLOT_SELECTION */ 4480+ } 4481+#ifdef DEBUG_SLOT_SELECTION 4482+ else 4483+ { 4484+ fprintf(stderr, " unusable\n"); 4485+ } 4486+#endif /* DEBUG_SLOT_SELECTION */ 4487+ 4488+ return; 4489+ } 4490+ 4491+#ifdef SOLARIS_AES_CTR 4492+/* create a new NID when we have no OID for that mechanism */ 4493+static int pk11_add_NID(char *sn, char *ln) 4494+ { 4495+ ASN1_OBJECT *o; 4496+ int nid; 4497+ 4498+ if ((o = ASN1_OBJECT_create(OBJ_new_nid(1), (unsigned char *)"", 4499+ 1, sn, ln)) == NULL) 4500+ { 4501+ return (0); 4502+ } 4503+ 4504+ /* will return NID_undef on error */ 4505+ nid = OBJ_add_object(o); 4506+ ASN1_OBJECT_free(o); 4507+ 4508+ return (nid); 4509+ } 4510+ 4511+/* 4512+ * Create new NIDs for AES counter mode. OpenSSL doesn't support them now so we 4513+ * have to help ourselves here. 4514+ */ 4515+static int pk11_add_aes_ctr_NIDs(void) 4516+ { 4517+ /* are we already set? */ 4518+ if (NID_aes_256_ctr != NID_undef) 4519+ return (1); 4520+ 4521+ /* 4522+ * There are no official names for AES counter modes yet so we just 4523+ * follow the format of those that exist. 4524+ */ 4525+ if ((NID_aes_128_ctr = pk11_add_NID("AES-128-CTR", "aes-128-ctr")) == 4526+ NID_undef) 4527+ goto err; 4528+ ciphers[PK11_AES_128_CTR].nid = pk11_aes_128_ctr.nid = NID_aes_128_ctr; 4529+ if ((NID_aes_192_ctr = pk11_add_NID("AES-192-CTR", "aes-192-ctr")) == 4530+ NID_undef) 4531+ goto err; 4532+ ciphers[PK11_AES_192_CTR].nid = pk11_aes_192_ctr.nid = NID_aes_192_ctr; 4533+ if ((NID_aes_256_ctr = pk11_add_NID("AES-256-CTR", "aes-256-ctr")) == 4534+ NID_undef) 4535+ goto err; 4536+ ciphers[PK11_AES_256_CTR].nid = pk11_aes_256_ctr.nid = NID_aes_256_ctr; 4537+ return (1); 4538+ 4539+err: 4540+ PK11err(PK11_F_ADD_AES_CTR_NIDS, PK11_R_ADD_NID_FAILED); 4541+ return (0); 4542+ } 4543+#endif /* SOLARIS_AES_CTR */ 4544+ 4545+/* Find what symmetric ciphers this slot supports. */ 4546+static void pk11_find_symmetric_ciphers(CK_FUNCTION_LIST_PTR pflist, 4547+ CK_SLOT_ID current_slot, int *current_slot_n_cipher, int *local_cipher_nids) 4548+ { 4549+ int i; 4550+ 4551+ for (i = 0; i < PK11_CIPHER_MAX; ++i) 4552+ { 4553+ pk11_get_symmetric_cipher(pflist, current_slot, 4554+ ciphers[i].mech_type, current_slot_n_cipher, 4555+ local_cipher_nids, ciphers[i].id); 4556+ } 4557+ } 4558+ 4559+/* Find what digest algorithms this slot supports. */ 4560+static void pk11_find_digests(CK_FUNCTION_LIST_PTR pflist, 4561+ CK_SLOT_ID current_slot, int *current_slot_n_digest, int *local_digest_nids) 4562+ { 4563+ int i; 4564+ 4565+ for (i = 0; i < PK11_DIGEST_MAX; ++i) 4566+ { 4567+ pk11_get_digest(pflist, current_slot, digests[i].mech_type, 4568+ current_slot_n_digest, local_digest_nids, digests[i].id); 4569+ } 4570+ } 4571+ 4572+#ifdef SOLARIS_HW_SLOT_SELECTION 4573+/* 4574+ * It would be great if we could use pkcs11_kernel directly since this library 4575+ * offers hardware slots only. That's the easiest way to achieve the situation 4576+ * where we use the hardware accelerators when present and OpenSSL native code 4577+ * otherwise. That presumes the fact that OpenSSL native code is faster than the 4578+ * code in the soft token. It's a logical assumption - Crypto Framework has some 4579+ * inherent overhead so going there for the software implementation of a 4580+ * mechanism should be logically slower in contrast to the OpenSSL native code, 4581+ * presuming that both implementations are of similar speed. For example, the 4582+ * soft token for AES is roughly three times slower than OpenSSL for 64 byte 4583+ * blocks and still 20% slower for 8KB blocks. So, if we want to ship products 4584+ * that use the PKCS#11 engine by default, we must somehow avoid that regression 4585+ * on machines without hardware acceleration. That's why switching to the 4586+ * pkcs11_kernel library seems like a very good idea. 4587+ * 4588+ * The problem is that OpenSSL built with SunStudio is roughly 2x slower for 4589+ * asymmetric operations (RSA/DSA/DH) than the soft token built with the same 4590+ * compiler. That means that if we switched to pkcs11_kernel from the libpkcs11 4591+ * library, we would have had a performance regression on machines without 4592+ * hardware acceleration for asymmetric operations for all applications that use 4593+ * the PKCS#11 engine. There is one such application - Apache web server since 4594+ * it's shipped configured to use the PKCS#11 engine by default. Having said 4595+ * that, we can't switch to the pkcs11_kernel library now and have to come with 4596+ * a solution that, on non-accelerated machines, uses the OpenSSL native code 4597+ * for all symmetric ciphers and digests while it uses the soft token for 4598+ * asymmetric operations. 4599+ * 4600+ * This is the idea: dlopen() pkcs11_kernel directly and find out what 4601+ * mechanisms are there. We don't care about duplications (more slots can 4602+ * support the same mechanism), we just want to know what mechanisms can be 4603+ * possibly supported in hardware on that particular machine. As said before, 4604+ * pkcs11_kernel will show you hardware providers only. 4605+ * 4606+ * Then, we rely on the fact that since we use libpkcs11 library we will find 4607+ * the metaslot. When we go through the metaslot's mechanisms for symmetric 4608+ * ciphers and digests, we check that any found mechanism is in the table 4609+ * created using the pkcs11_kernel library. So, as a result we have two arrays 4610+ * of mechanisms that were advertised as supported in hardware which was the 4611+ * goal of that whole excercise. Thus, we can use libpkcs11 but avoid soft token 4612+ * code for symmetric ciphers and digests. See pk11_choose_slots() for more 4613+ * information. 4614+ * 4615+ * This is Solaris specific code, if SOLARIS_HW_SLOT_SELECTION is not defined 4616+ * the code won't be used. 4617+ */ 4618+#if defined(__sparcv9) || defined(__x86_64) || defined(__amd64) 4619+static const char pkcs11_kernel[] = "/usr/lib/security/64/pkcs11_kernel.so.1"; 4620+#else 4621+static const char pkcs11_kernel[] = "/usr/lib/security/pkcs11_kernel.so.1"; 4622+#endif 4623+ 4624+/* 4625+ * Check hardware capabilities of the machines. The output are two lists, 4626+ * hw_cnids and hw_dnids, that contain hardware mechanisms found in all hardware 4627+ * providers together. They are not sorted and may contain duplicate mechanisms. 4628+ */ 4629+static int check_hw_mechanisms(void) 4630+ { 4631+ int i; 4632+ CK_RV rv; 4633+ void *handle; 4634+ CK_C_GetFunctionList p; 4635+ CK_TOKEN_INFO token_info; 4636+ CK_ULONG ulSlotCount = 0; 4637+ int n_cipher = 0, n_digest = 0; 4638+ CK_FUNCTION_LIST_PTR pflist = NULL; 4639+ CK_SLOT_ID_PTR pSlotList = NULL_PTR; 4640+ int *tmp_hw_cnids = NULL, *tmp_hw_dnids = NULL; 4641+ int hw_ctable_size, hw_dtable_size; 4642+ 4643+#ifdef DEBUG_SLOT_SELECTION 4644+ fprintf(stderr, "%s: SOLARIS_HW_SLOT_SELECTION code running\n", 4645+ PK11_DBG); 4646+#endif 4647+ if ((handle = dlopen(pkcs11_kernel, RTLD_LAZY)) == NULL) 4648+ { 4649+ PK11err(PK11_F_CHECK_HW_MECHANISMS, PK11_R_DSO_FAILURE); 4650+ goto err; 4651+ } 4652+ 4653+ if ((p = (CK_C_GetFunctionList)dlsym(handle, 4654+ PK11_GET_FUNCTION_LIST)) == NULL) 4655+ { 4656+ PK11err(PK11_F_CHECK_HW_MECHANISMS, PK11_R_DSO_FAILURE); 4657+ goto err; 4658+ } 4659+ 4660+ /* get the full function list from the loaded library */ 4661+ if (p(&pflist) != CKR_OK) 4662+ { 4663+ PK11err(PK11_F_CHECK_HW_MECHANISMS, PK11_R_DSO_FAILURE); 4664+ goto err; 4665+ } 4666+ 4667+ rv = pflist->C_Initialize((CK_VOID_PTR)&pk11_init_args); 4668+ if ((rv != CKR_OK) && (rv != CKR_CRYPTOKI_ALREADY_INITIALIZED)) 4669+ { 4670+ PK11err_add_data(PK11_F_CHECK_HW_MECHANISMS, 4671+ PK11_R_INITIALIZE, rv); 4672+ goto err; 4673+ } 4674+ 4675+ if (pflist->C_GetSlotList(0, NULL_PTR, &ulSlotCount) != CKR_OK) 4676+ { 4677+ PK11err(PK11_F_CHECK_HW_MECHANISMS, PK11_R_GETSLOTLIST); 4678+ goto err; 4679+ } 4680+ 4681+ /* no slots, set the hw mechanism tables as empty */ 4682+ if (ulSlotCount == 0) 4683+ { 4684+#ifdef DEBUG_SLOT_SELECTION 4685+ fprintf(stderr, "%s: no hardware mechanisms found\n", PK11_DBG); 4686+#endif 4687+ hw_cnids = OPENSSL_malloc(sizeof (int)); 4688+ hw_dnids = OPENSSL_malloc(sizeof (int)); 4689+ if (hw_cnids == NULL || hw_dnids == NULL) 4690+ { 4691+ PK11err(PK11_F_CHECK_HW_MECHANISMS, 4692+ PK11_R_MALLOC_FAILURE); 4693+ return (0); 4694+ } 4695+ /* this means empty tables */ 4696+ hw_cnids[0] = NID_undef; 4697+ hw_dnids[0] = NID_undef; 4698+ return (1); 4699+ } 4700+ 4701+ pSlotList = OPENSSL_malloc(ulSlotCount * sizeof (CK_SLOT_ID)); 4702+ if (pSlotList == NULL) 4703+ { 4704+ PK11err(PK11_F_CHECK_HW_MECHANISMS, PK11_R_MALLOC_FAILURE); 4705+ goto err; 4706+ } 4707+ 4708+ /* Get the slot list for processing */ 4709+ if (pflist->C_GetSlotList(0, pSlotList, &ulSlotCount) != CKR_OK) 4710+ { 4711+ PK11err(PK11_F_CHECK_HW_MECHANISMS, PK11_R_GETSLOTLIST); 4712+ goto err; 4713+ } 4714+ 4715+ /* 4716+ * We don't care about duplicit mechanisms in multiple slots and also 4717+ * reserve one slot for the terminal NID_undef which we use to stop the 4718+ * search. 4719+ */ 4720+ hw_ctable_size = ulSlotCount * PK11_CIPHER_MAX + 1; 4721+ hw_dtable_size = ulSlotCount * PK11_DIGEST_MAX + 1; 4722+ tmp_hw_cnids = OPENSSL_malloc(hw_ctable_size * sizeof (int)); 4723+ tmp_hw_dnids = OPENSSL_malloc(hw_dtable_size * sizeof (int)); 4724+ if (tmp_hw_cnids == NULL || tmp_hw_dnids == NULL) 4725+ { 4726+ PK11err(PK11_F_CHECK_HW_MECHANISMS, PK11_R_MALLOC_FAILURE); 4727+ goto err; 4728+ } 4729+ 4730+ /* 4731+ * Do not use memset since we should not rely on the fact that NID_undef 4732+ * is zero now. 4733+ */ 4734+ for (i = 0; i < hw_ctable_size; ++i) 4735+ tmp_hw_cnids[i] = NID_undef; 4736+ for (i = 0; i < hw_dtable_size; ++i) 4737+ tmp_hw_dnids[i] = NID_undef; 4738+ 4739+#ifdef DEBUG_SLOT_SELECTION 4740+ fprintf(stderr, "%s: provider: %s\n", PK11_DBG, pkcs11_kernel); 4741+ fprintf(stderr, "%s: found %d hardware slots\n", PK11_DBG, ulSlotCount); 4742+ fprintf(stderr, "%s: now looking for mechs supported in hw\n", 4743+ PK11_DBG); 4744+#endif /* DEBUG_SLOT_SELECTION */ 4745+ 4746+ for (i = 0; i < ulSlotCount; i++) 4747+ { 4748+ if (pflist->C_GetTokenInfo(pSlotList[i], &token_info) != CKR_OK) 4749+ continue; 4750+ 4751+#ifdef DEBUG_SLOT_SELECTION 4752+ fprintf(stderr, "%s: token label: %.32s\n", PK11_DBG, token_info.label); 4753+#endif /* DEBUG_SLOT_SELECTION */ 4754+ 4755+ /* 4756+ * We are filling the hw mech tables here. Global tables are 4757+ * still NULL so all mechanisms are put into tmp tables. 4758+ */ 4759+ pk11_find_symmetric_ciphers(pflist, pSlotList[i], 4760+ &n_cipher, tmp_hw_cnids); 4761+ pk11_find_digests(pflist, pSlotList[i], 4762+ &n_digest, tmp_hw_dnids); 4763+ } 4764+ 4765+ /* 4766+ * Since we are part of a library (libcrypto.so), calling this function 4767+ * may have side-effects. Also, C_Finalize() is triggered by 4768+ * dlclose(3C). 4769+ */ 4770+#if 0 4771+ pflist->C_Finalize(NULL); 4772+#endif 4773+ OPENSSL_free(pSlotList); 4774+ (void) dlclose(handle); 4775+ hw_cnids = tmp_hw_cnids; 4776+ hw_dnids = tmp_hw_dnids; 4777+ 4778+#ifdef DEBUG_SLOT_SELECTION 4779+ fprintf(stderr, "%s: hw mechs check complete\n", PK11_DBG); 4780+#endif /* DEBUG_SLOT_SELECTION */ 4781+ return (1); 4782+ 4783+err: 4784+ if (pSlotList != NULL) 4785+ OPENSSL_free(pSlotList); 4786+ if (tmp_hw_cnids != NULL) 4787+ OPENSSL_free(tmp_hw_cnids); 4788+ if (tmp_hw_dnids != NULL) 4789+ OPENSSL_free(tmp_hw_dnids); 4790+ 4791+ return (0); 4792+ } 4793+ 4794+/* 4795+ * Check presence of a NID in the table of NIDs. The table may be NULL (i.e., 4796+ * non-existent). 4797+ */ 4798+static int nid_in_table(int nid, int *nid_table) 4799+ { 4800+ int i = 0; 4801+ 4802+ /* 4803+ * a special case. NULL means that we are initializing a new 4804+ * table. 4805+ */ 4806+ if (nid_table == NULL) 4807+ return (1); 4808+ 4809+ /* 4810+ * the table is never full, there is always at least one 4811+ * NID_undef. 4812+ */ 4813+ while (nid_table[i] != NID_undef) 4814+ { 4815+ if (nid_table[i++] == nid) 4816+ { 4817+#ifdef DEBUG_SLOT_SELECTION 4818+ fprintf(stderr, " (NID %d in hw table, idx %d)", nid, i); 4819+#endif /* DEBUG_SLOT_SELECTION */ 4820+ return (1); 4821+ } 4822+ } 4823+ 4824+ return (0); 4825+ } 4826+#endif /* SOLARIS_HW_SLOT_SELECTION */ 4827+ 4828+#endif /* OPENSSL_NO_HW_PK11CA */ 4829+#endif /* OPENSSL_NO_HW_PK11 */ 4830+#endif /* OPENSSL_NO_HW */ 4831Index: openssl/crypto/engine/hw_pk11_err.c 4832diff -u /dev/null openssl/crypto/engine/hw_pk11_err.c:1.4.10.1 4833--- /dev/null Mon Jan 16 18:53:42 2012 4834+++ openssl/crypto/engine/hw_pk11_err.c Tue Jun 14 21:52:40 2011 4835@@ -0,0 +1,288 @@ 4836+/* 4837+ * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 4838+ * Use is subject to license terms. 4839+ */ 4840+ 4841+/* crypto/engine/hw_pk11_err.c */ 4842+/* 4843+ * This product includes software developed by the OpenSSL Project for 4844+ * use in the OpenSSL Toolkit (http://www.openssl.org/). 4845+ * 4846+ * This project also referenced hw_pkcs11-0.9.7b.patch written by 4847+ * Afchine Madjlessi. 4848+ */ 4849+/* 4850+ * ==================================================================== 4851+ * Copyright (c) 2000-2001 The OpenSSL Project. All rights reserved. 4852+ * 4853+ * Redistribution and use in source and binary forms, with or without 4854+ * modification, are permitted provided that the following conditions 4855+ * are met: 4856+ * 4857+ * 1. Redistributions of source code must retain the above copyright 4858+ * notice, this list of conditions and the following disclaimer. 4859+ * 4860+ * 2. Redistributions in binary form must reproduce the above copyright 4861+ * notice, this list of conditions and the following disclaimer in 4862+ * the documentation and/or other materials provided with the 4863+ * distribution. 4864+ * 4865+ * 3. All advertising materials mentioning features or use of this 4866+ * software must display the following acknowledgment: 4867+ * "This product includes software developed by the OpenSSL Project 4868+ * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 4869+ * 4870+ * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 4871+ * endorse or promote products derived from this software without 4872+ * prior written permission. For written permission, please contact 4873+ * licensing@OpenSSL.org. 4874+ * 4875+ * 5. Products derived from this software may not be called "OpenSSL" 4876+ * nor may "OpenSSL" appear in their names without prior written 4877+ * permission of the OpenSSL Project. 4878+ * 4879+ * 6. Redistributions of any form whatsoever must retain the following 4880+ * acknowledgment: 4881+ * "This product includes software developed by the OpenSSL Project 4882+ * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 4883+ * 4884+ * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 4885+ * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4886+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 4887+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 4888+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 4889+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 4890+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 4891+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4892+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 4893+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 4894+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 4895+ * OF THE POSSIBILITY OF SUCH DAMAGE. 4896+ * ==================================================================== 4897+ * 4898+ * This product includes cryptographic software written by Eric Young 4899+ * (eay@cryptsoft.com). This product includes software written by Tim 4900+ * Hudson (tjh@cryptsoft.com). 4901+ * 4902+ */ 4903+ 4904+#include <stdio.h> 4905+#include <openssl/err.h> 4906+#include "hw_pk11_err.h" 4907+ 4908+/* BEGIN ERROR CODES */ 4909+#ifndef OPENSSL_NO_ERR 4910+static ERR_STRING_DATA pk11_str_functs[]= 4911+{ 4912+{ ERR_PACK(0, PK11_F_INIT, 0), "PK11_INIT"}, 4913+{ ERR_PACK(0, PK11_F_FINISH, 0), "PK11_FINISH"}, 4914+{ ERR_PACK(0, PK11_F_DESTROY, 0), "PK11_DESTROY"}, 4915+{ ERR_PACK(0, PK11_F_CTRL, 0), "PK11_CTRL"}, 4916+{ ERR_PACK(0, PK11_F_RSA_INIT, 0), "PK11_RSA_INIT"}, 4917+{ ERR_PACK(0, PK11_F_RSA_FINISH, 0), "PK11_RSA_FINISH"}, 4918+{ ERR_PACK(0, PK11_F_GET_PUB_RSA_KEY, 0), "PK11_GET_PUB_RSA_KEY"}, 4919+{ ERR_PACK(0, PK11_F_GET_PRIV_RSA_KEY, 0), "PK11_GET_PRIV_RSA_KEY"}, 4920+{ ERR_PACK(0, PK11_F_RSA_GEN_KEY, 0), "PK11_RSA_GEN_KEY"}, 4921+{ ERR_PACK(0, PK11_F_RSA_PUB_ENC, 0), "PK11_RSA_PUB_ENC"}, 4922+{ ERR_PACK(0, PK11_F_RSA_PRIV_ENC, 0), "PK11_RSA_PRIV_ENC"}, 4923+{ ERR_PACK(0, PK11_F_RSA_PUB_DEC, 0), "PK11_RSA_PUB_DEC"}, 4924+{ ERR_PACK(0, PK11_F_RSA_PRIV_DEC, 0), "PK11_RSA_PRIV_DEC"}, 4925+{ ERR_PACK(0, PK11_F_RSA_SIGN, 0), "PK11_RSA_SIGN"}, 4926+{ ERR_PACK(0, PK11_F_RSA_VERIFY, 0), "PK11_RSA_VERIFY"}, 4927+{ ERR_PACK(0, PK11_F_RAND_ADD, 0), "PK11_RAND_ADD"}, 4928+{ ERR_PACK(0, PK11_F_RAND_BYTES, 0), "PK11_RAND_BYTES"}, 4929+{ ERR_PACK(0, PK11_F_GET_SESSION, 0), "PK11_GET_SESSION"}, 4930+{ ERR_PACK(0, PK11_F_FREE_SESSION, 0), "PK11_FREE_SESSION"}, 4931+{ ERR_PACK(0, PK11_F_LOAD_PUBKEY, 0), "PK11_LOAD_PUBKEY"}, 4932+{ ERR_PACK(0, PK11_F_LOAD_PRIVKEY, 0), "PK11_LOAD_PRIV_KEY"}, 4933+{ ERR_PACK(0, PK11_F_RSA_PUB_ENC_LOW, 0), "PK11_RSA_PUB_ENC_LOW"}, 4934+{ ERR_PACK(0, PK11_F_RSA_PRIV_ENC_LOW, 0), "PK11_RSA_PRIV_ENC_LOW"}, 4935+{ ERR_PACK(0, PK11_F_RSA_PUB_DEC_LOW, 0), "PK11_RSA_PUB_DEC_LOW"}, 4936+{ ERR_PACK(0, PK11_F_RSA_PRIV_DEC_LOW, 0), "PK11_RSA_PRIV_DEC_LOW"}, 4937+{ ERR_PACK(0, PK11_F_DSA_SIGN, 0), "PK11_DSA_SIGN"}, 4938+{ ERR_PACK(0, PK11_F_DSA_VERIFY, 0), "PK11_DSA_VERIFY"}, 4939+{ ERR_PACK(0, PK11_F_DSA_INIT, 0), "PK11_DSA_INIT"}, 4940+{ ERR_PACK(0, PK11_F_DSA_FINISH, 0), "PK11_DSA_FINISH"}, 4941+{ ERR_PACK(0, PK11_F_GET_PUB_DSA_KEY, 0), "PK11_GET_PUB_DSA_KEY"}, 4942+{ ERR_PACK(0, PK11_F_GET_PRIV_DSA_KEY, 0), "PK11_GET_PRIV_DSA_KEY"}, 4943+{ ERR_PACK(0, PK11_F_DH_INIT, 0), "PK11_DH_INIT"}, 4944+{ ERR_PACK(0, PK11_F_DH_FINISH, 0), "PK11_DH_FINISH"}, 4945+{ ERR_PACK(0, PK11_F_MOD_EXP_DH, 0), "PK11_MOD_EXP_DH"}, 4946+{ ERR_PACK(0, PK11_F_GET_DH_KEY, 0), "PK11_GET_DH_KEY"}, 4947+{ ERR_PACK(0, PK11_F_FREE_ALL_SESSIONS, 0), "PK11_FREE_ALL_SESSIONS"}, 4948+{ ERR_PACK(0, PK11_F_SETUP_SESSION, 0), "PK11_SETUP_SESSION"}, 4949+{ ERR_PACK(0, PK11_F_DESTROY_OBJECT, 0), "PK11_DESTROY_OBJECT"}, 4950+{ ERR_PACK(0, PK11_F_CIPHER_INIT, 0), "PK11_CIPHER_INIT"}, 4951+{ ERR_PACK(0, PK11_F_CIPHER_DO_CIPHER, 0), "PK11_CIPHER_DO_CIPHER"}, 4952+{ ERR_PACK(0, PK11_F_GET_CIPHER_KEY, 0), "PK11_GET_CIPHER_KEY"}, 4953+{ ERR_PACK(0, PK11_F_DIGEST_INIT, 0), "PK11_DIGEST_INIT"}, 4954+{ ERR_PACK(0, PK11_F_DIGEST_UPDATE, 0), "PK11_DIGEST_UPDATE"}, 4955+{ ERR_PACK(0, PK11_F_DIGEST_FINAL, 0), "PK11_DIGEST_FINAL"}, 4956+{ ERR_PACK(0, PK11_F_CHOOSE_SLOT, 0), "PK11_CHOOSE_SLOT"}, 4957+{ ERR_PACK(0, PK11_F_CIPHER_FINAL, 0), "PK11_CIPHER_FINAL"}, 4958+{ ERR_PACK(0, PK11_F_LIBRARY_INIT, 0), "PK11_LIBRARY_INIT"}, 4959+{ ERR_PACK(0, PK11_F_LOAD, 0), "ENGINE_LOAD_PK11"}, 4960+{ ERR_PACK(0, PK11_F_DH_GEN_KEY, 0), "PK11_DH_GEN_KEY"}, 4961+{ ERR_PACK(0, PK11_F_DH_COMP_KEY, 0), "PK11_DH_COMP_KEY"}, 4962+{ ERR_PACK(0, PK11_F_DIGEST_COPY, 0), "PK11_DIGEST_COPY"}, 4963+{ ERR_PACK(0, PK11_F_CIPHER_CLEANUP, 0), "PK11_CIPHER_CLEANUP"}, 4964+{ ERR_PACK(0, PK11_F_ACTIVE_ADD, 0), "PK11_ACTIVE_ADD"}, 4965+{ ERR_PACK(0, PK11_F_ACTIVE_DELETE, 0), "PK11_ACTIVE_DELETE"}, 4966+{ ERR_PACK(0, PK11_F_CHECK_HW_MECHANISMS, 0), "PK11_CHECK_HW_MECHANISMS"}, 4967+{ ERR_PACK(0, PK11_F_INIT_SYMMETRIC, 0), "PK11_INIT_SYMMETRIC"}, 4968+{ ERR_PACK(0, PK11_F_ADD_AES_CTR_NIDS, 0), "PK11_ADD_AES_CTR_NIDS"}, 4969+{ ERR_PACK(0, PK11_F_INIT_ALL_LOCKS, 0), "PK11_INIT_ALL_LOCKS"}, 4970+{ ERR_PACK(0, PK11_F_RETURN_SESSION, 0), "PK11_RETURN_SESSION"}, 4971+{ ERR_PACK(0, PK11_F_GET_PIN, 0), "PK11_GET_PIN"}, 4972+{ ERR_PACK(0, PK11_F_FIND_ONE_OBJECT, 0), "PK11_FIND_ONE_OBJECT"}, 4973+{ ERR_PACK(0, PK11_F_CHECK_TOKEN_ATTRS, 0), "PK11_CHECK_TOKEN_ATTRS"}, 4974+{ ERR_PACK(0, PK11_F_CACHE_PIN, 0), "PK11_CACHE_PIN"}, 4975+{ ERR_PACK(0, PK11_F_MLOCK_PIN_IN_MEMORY, 0), "PK11_MLOCK_PIN_IN_MEMORY"}, 4976+{ ERR_PACK(0, PK11_F_TOKEN_LOGIN, 0), "PK11_TOKEN_LOGIN"}, 4977+{ ERR_PACK(0, PK11_F_TOKEN_RELOGIN, 0), "PK11_TOKEN_RELOGIN"}, 4978+{ ERR_PACK(0, PK11_F_RUN_ASKPASS, 0), "PK11_F_RUN_ASKPASS"}, 4979+{ 0, NULL} 4980+}; 4981+ 4982+static ERR_STRING_DATA pk11_str_reasons[]= 4983+{ 4984+{ PK11_R_ALREADY_LOADED, "PKCS#11 DSO already loaded"}, 4985+{ PK11_R_DSO_FAILURE, "unable to load PKCS#11 DSO"}, 4986+{ PK11_R_NOT_LOADED, "PKCS#11 DSO not loaded"}, 4987+{ PK11_R_PASSED_NULL_PARAMETER, "null parameter passed"}, 4988+{ PK11_R_COMMAND_NOT_IMPLEMENTED, "command not implemented"}, 4989+{ PK11_R_INITIALIZE, "C_Initialize failed"}, 4990+{ PK11_R_FINALIZE, "C_Finalize failed"}, 4991+{ PK11_R_GETINFO, "C_GetInfo faile"}, 4992+{ PK11_R_GETSLOTLIST, "C_GetSlotList failed"}, 4993+{ PK11_R_NO_MODULUS_OR_NO_EXPONENT, "no modulus or no exponent"}, 4994+{ PK11_R_ATTRIBUT_SENSITIVE_OR_INVALID, "attr sensitive or invalid"}, 4995+{ PK11_R_GETATTRIBUTVALUE, "C_GetAttributeValue failed"}, 4996+{ PK11_R_NO_MODULUS, "no modulus"}, 4997+{ PK11_R_NO_EXPONENT, "no exponent"}, 4998+{ PK11_R_FINDOBJECTSINIT, "C_FindObjectsInit failed"}, 4999+{ PK11_R_FINDOBJECTS, "C_FindObjects failed"}, 5000+{ PK11_R_FINDOBJECTSFINAL, "C_FindObjectsFinal failed"}, 5001+{ PK11_R_CREATEOBJECT, "C_CreateObject failed"}, 5002+{ PK11_R_DESTROYOBJECT, "C_DestroyObject failed"}, 5003+{ PK11_R_OPENSESSION, "C_OpenSession failed"}, 5004+{ PK11_R_CLOSESESSION, "C_CloseSession failed"}, 5005+{ PK11_R_ENCRYPTINIT, "C_EncryptInit failed"}, 5006+{ PK11_R_ENCRYPT, "C_Encrypt failed"}, 5007+{ PK11_R_SIGNINIT, "C_SignInit failed"}, 5008+{ PK11_R_SIGN, "C_Sign failed"}, 5009+{ PK11_R_DECRYPTINIT, "C_DecryptInit failed"}, 5010+{ PK11_R_DECRYPT, "C_Decrypt failed"}, 5011+{ PK11_R_VERIFYINIT, "C_VerifyRecover failed"}, 5012+{ PK11_R_VERIFY, "C_Verify failed"}, 5013+{ PK11_R_VERIFYRECOVERINIT, "C_VerifyRecoverInit failed"}, 5014+{ PK11_R_VERIFYRECOVER, "C_VerifyRecover failed"}, 5015+{ PK11_R_GEN_KEY, "C_GenerateKeyPair failed"}, 5016+{ PK11_R_SEEDRANDOM, "C_SeedRandom failed"}, 5017+{ PK11_R_GENERATERANDOM, "C_GenerateRandom failed"}, 5018+{ PK11_R_INVALID_MESSAGE_LENGTH, "invalid message length"}, 5019+{ PK11_R_UNKNOWN_ALGORITHM_TYPE, "unknown algorithm type"}, 5020+{ PK11_R_UNKNOWN_ASN1_OBJECT_ID, "unknown asn1 onject id"}, 5021+{ PK11_R_UNKNOWN_PADDING_TYPE, "unknown padding type"}, 5022+{ PK11_R_PADDING_CHECK_FAILED, "padding check failed"}, 5023+{ PK11_R_DIGEST_TOO_BIG, "digest too big"}, 5024+{ PK11_R_MALLOC_FAILURE, "malloc failure"}, 5025+{ PK11_R_CTRL_COMMAND_NOT_IMPLEMENTED, "ctl command not implemented"}, 5026+{ PK11_R_DATA_GREATER_THAN_MOD_LEN, "data is bigger than mod"}, 5027+{ PK11_R_DATA_TOO_LARGE_FOR_MODULUS, "data is too larger for mod"}, 5028+{ PK11_R_MISSING_KEY_COMPONENT, "a dsa component is missing"}, 5029+{ PK11_R_INVALID_SIGNATURE_LENGTH, "invalid signature length"}, 5030+{ PK11_R_INVALID_DSA_SIGNATURE_R, "missing r in dsa verify"}, 5031+{ PK11_R_INVALID_DSA_SIGNATURE_S, "missing s in dsa verify"}, 5032+{ PK11_R_INCONSISTENT_KEY, "inconsistent key type"}, 5033+{ PK11_R_ENCRYPTUPDATE, "C_EncryptUpdate failed"}, 5034+{ PK11_R_DECRYPTUPDATE, "C_DecryptUpdate failed"}, 5035+{ PK11_R_DIGESTINIT, "C_DigestInit failed"}, 5036+{ PK11_R_DIGESTUPDATE, "C_DigestUpdate failed"}, 5037+{ PK11_R_DIGESTFINAL, "C_DigestFinal failed"}, 5038+{ PK11_R_ENCRYPTFINAL, "C_EncryptFinal failed"}, 5039+{ PK11_R_DECRYPTFINAL, "C_DecryptFinal failed"}, 5040+{ PK11_R_NO_PRNG_SUPPORT, "Slot does not support PRNG"}, 5041+{ PK11_R_GETTOKENINFO, "C_GetTokenInfo failed"}, 5042+{ PK11_R_DERIVEKEY, "C_DeriveKey failed"}, 5043+{ PK11_R_GET_OPERATION_STATE, "C_GetOperationState failed"}, 5044+{ PK11_R_SET_OPERATION_STATE, "C_SetOperationState failed"}, 5045+{ PK11_R_INVALID_HANDLE, "invalid PKCS#11 object handle"}, 5046+{ PK11_R_KEY_OR_IV_LEN_PROBLEM, "IV or key length incorrect"}, 5047+{ PK11_R_INVALID_OPERATION_TYPE, "invalid operation type"}, 5048+{ PK11_R_ADD_NID_FAILED, "failed to add NID" }, 5049+{ PK11_R_ATFORK_FAILED, "atfork() failed" }, 5050+{ PK11_R_TOKEN_LOGIN_FAILED, "C_Login() failed on token" }, 5051+{ PK11_R_MORE_THAN_ONE_OBJECT_FOUND, "more than one object found" }, 5052+{ PK11_R_INVALID_PKCS11_URI, "pkcs11 URI provided is invalid" }, 5053+{ PK11_R_COULD_NOT_READ_PIN, "could not read PIN from terminal" }, 5054+{ PK11_R_PIN_NOT_READ_FROM_COMMAND, "PIN not read from external command" }, 5055+{ PK11_R_COULD_NOT_OPEN_COMMAND, "could not popen() dialog command" }, 5056+{ PK11_R_PIPE_FAILED, "pipe() failed" }, 5057+{ PK11_R_BAD_PASSPHRASE_SPEC, "bad passphrasedialog specification" }, 5058+{ PK11_R_TOKEN_NOT_INITIALIZED, "token not initialized" }, 5059+{ PK11_R_TOKEN_PIN_NOT_SET, "token PIN required but not set" }, 5060+{ PK11_R_TOKEN_PIN_NOT_PROVIDED, "token PIN required but not provided" }, 5061+{ PK11_R_MISSING_OBJECT_LABEL, "missing mandatory 'object' keyword" }, 5062+{ PK11_R_TOKEN_ATTRS_DO_NOT_MATCH, "token attrs provided do not match" }, 5063+{ PK11_R_PRIV_KEY_NOT_FOUND, "private key not found in keystore" }, 5064+{ PK11_R_NO_OBJECT_FOUND, "specified object not found" }, 5065+{ PK11_R_PIN_CACHING_POLICY_INVALID, "PIN set but caching policy invalid" }, 5066+{ PK11_R_SYSCONF_FAILED, "sysconf() failed" }, 5067+{ PK11_R_MMAP_FAILED, "mmap() failed" }, 5068+{ PK11_R_PRIV_PROC_LOCK_MEMORY_MISSING, "PROC_LOCK_MEMORY privilege missing" }, 5069+{ PK11_R_MLOCK_FAILED, "mlock() failed" }, 5070+{ PK11_R_FORK_FAILED, "fork() failed" }, 5071+{ 0, NULL} 5072+}; 5073+#endif /* OPENSSL_NO_ERR */ 5074+ 5075+static int pk11_lib_error_code = 0; 5076+static int pk11_error_init = 1; 5077+ 5078+static void 5079+ERR_load_pk11_strings(void) 5080+ { 5081+ if (pk11_lib_error_code == 0) 5082+ pk11_lib_error_code = ERR_get_next_error_library(); 5083+ 5084+ if (pk11_error_init) 5085+ { 5086+ pk11_error_init = 0; 5087+#ifndef OPENSSL_NO_ERR 5088+ ERR_load_strings(pk11_lib_error_code, pk11_str_functs); 5089+ ERR_load_strings(pk11_lib_error_code, pk11_str_reasons); 5090+#endif 5091+ } 5092+} 5093+ 5094+static void 5095+ERR_unload_pk11_strings(void) 5096+ { 5097+ if (pk11_error_init == 0) 5098+ { 5099+#ifndef OPENSSL_NO_ERR 5100+ ERR_unload_strings(pk11_lib_error_code, pk11_str_functs); 5101+ ERR_unload_strings(pk11_lib_error_code, pk11_str_reasons); 5102+#endif 5103+ pk11_error_init = 1; 5104+ } 5105+} 5106+ 5107+void 5108+ERR_pk11_error(int function, int reason, char *file, int line) 5109+{ 5110+ if (pk11_lib_error_code == 0) 5111+ pk11_lib_error_code = ERR_get_next_error_library(); 5112+ ERR_PUT_error(pk11_lib_error_code, function, reason, file, line); 5113+} 5114+ 5115+void 5116+PK11err_add_data(int function, int reason, CK_RV rv) 5117+{ 5118+ char tmp_buf[20]; 5119+ 5120+ PK11err(function, reason); 5121+ (void) BIO_snprintf(tmp_buf, sizeof (tmp_buf), "%lx", rv); 5122+ ERR_add_error_data(2, "PK11 CK_RV=0X", tmp_buf); 5123+} 5124Index: openssl/crypto/engine/hw_pk11_err.h 5125diff -u /dev/null openssl/crypto/engine/hw_pk11_err.h:1.9.10.1 5126--- /dev/null Mon Jan 16 18:53:42 2012 5127+++ openssl/crypto/engine/hw_pk11_err.h Tue Jun 14 21:52:40 2011 5128@@ -0,0 +1,440 @@ 5129+/* 5130+ * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 5131+ * Use is subject to license terms. 5132+ */ 5133+ 5134+/* 5135+ * This product includes software developed by the OpenSSL Project for 5136+ * use in the OpenSSL Toolkit (http://www.openssl.org/). 5137+ * 5138+ * This project also referenced hw_pkcs11-0.9.7b.patch written by 5139+ * Afchine Madjlessi. 5140+ */ 5141+/* 5142+ * ==================================================================== 5143+ * Copyright (c) 2000-2001 The OpenSSL Project. All rights reserved. 5144+ * 5145+ * Redistribution and use in source and binary forms, with or without 5146+ * modification, are permitted provided that the following conditions 5147+ * are met: 5148+ * 5149+ * 1. Redistributions of source code must retain the above copyright 5150+ * notice, this list of conditions and the following disclaimer. 5151+ * 5152+ * 2. Redistributions in binary form must reproduce the above copyright 5153+ * notice, this list of conditions and the following disclaimer in 5154+ * the documentation and/or other materials provided with the 5155+ * distribution. 5156+ * 5157+ * 3. All advertising materials mentioning features or use of this 5158+ * software must display the following acknowledgment: 5159+ * "This product includes software developed by the OpenSSL Project 5160+ * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 5161+ * 5162+ * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 5163+ * endorse or promote products derived from this software without 5164+ * prior written permission. For written permission, please contact 5165+ * licensing@OpenSSL.org. 5166+ * 5167+ * 5. Products derived from this software may not be called "OpenSSL" 5168+ * nor may "OpenSSL" appear in their names without prior written 5169+ * permission of the OpenSSL Project. 5170+ * 5171+ * 6. Redistributions of any form whatsoever must retain the following 5172+ * acknowledgment: 5173+ * "This product includes software developed by the OpenSSL Project 5174+ * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 5175+ * 5176+ * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 5177+ * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 5178+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 5179+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 5180+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 5181+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 5182+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 5183+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 5184+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 5185+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 5186+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 5187+ * OF THE POSSIBILITY OF SUCH DAMAGE. 5188+ * ==================================================================== 5189+ * 5190+ * This product includes cryptographic software written by Eric Young 5191+ * (eay@cryptsoft.com). This product includes software written by Tim 5192+ * Hudson (tjh@cryptsoft.com). 5193+ * 5194+ */ 5195+ 5196+#ifndef HW_PK11_ERR_H 5197+#define HW_PK11_ERR_H 5198+ 5199+void ERR_pk11_error(int function, int reason, char *file, int line); 5200+void PK11err_add_data(int function, int reason, CK_RV rv); 5201+#define PK11err(f, r) ERR_pk11_error((f), (r), __FILE__, __LINE__) 5202+ 5203+/* Error codes for the PK11 functions. */ 5204+ 5205+/* Function codes. */ 5206+ 5207+#define PK11_F_INIT 100 5208+#define PK11_F_FINISH 101 5209+#define PK11_F_DESTROY 102 5210+#define PK11_F_CTRL 103 5211+#define PK11_F_RSA_INIT 104 5212+#define PK11_F_RSA_FINISH 105 5213+#define PK11_F_GET_PUB_RSA_KEY 106 5214+#define PK11_F_GET_PRIV_RSA_KEY 107 5215+#define PK11_F_RSA_GEN_KEY 108 5216+#define PK11_F_RSA_PUB_ENC 109 5217+#define PK11_F_RSA_PRIV_ENC 110 5218+#define PK11_F_RSA_PUB_DEC 111 5219+#define PK11_F_RSA_PRIV_DEC 112 5220+#define PK11_F_RSA_SIGN 113 5221+#define PK11_F_RSA_VERIFY 114 5222+#define PK11_F_RAND_ADD 115 5223+#define PK11_F_RAND_BYTES 116 5224+#define PK11_F_GET_SESSION 117 5225+#define PK11_F_FREE_SESSION 118 5226+#define PK11_F_LOAD_PUBKEY 119 5227+#define PK11_F_LOAD_PRIVKEY 120 5228+#define PK11_F_RSA_PUB_ENC_LOW 121 5229+#define PK11_F_RSA_PRIV_ENC_LOW 122 5230+#define PK11_F_RSA_PUB_DEC_LOW 123 5231+#define PK11_F_RSA_PRIV_DEC_LOW 124 5232+#define PK11_F_DSA_SIGN 125 5233+#define PK11_F_DSA_VERIFY 126 5234+#define PK11_F_DSA_INIT 127 5235+#define PK11_F_DSA_FINISH 128 5236+#define PK11_F_GET_PUB_DSA_KEY 129 5237+#define PK11_F_GET_PRIV_DSA_KEY 130 5238+#define PK11_F_DH_INIT 131 5239+#define PK11_F_DH_FINISH 132 5240+#define PK11_F_MOD_EXP_DH 133 5241+#define PK11_F_GET_DH_KEY 134 5242+#define PK11_F_FREE_ALL_SESSIONS 135 5243+#define PK11_F_SETUP_SESSION 136 5244+#define PK11_F_DESTROY_OBJECT 137 5245+#define PK11_F_CIPHER_INIT 138 5246+#define PK11_F_CIPHER_DO_CIPHER 139 5247+#define PK11_F_GET_CIPHER_KEY 140 5248+#define PK11_F_DIGEST_INIT 141 5249+#define PK11_F_DIGEST_UPDATE 142 5250+#define PK11_F_DIGEST_FINAL 143 5251+#define PK11_F_CHOOSE_SLOT 144 5252+#define PK11_F_CIPHER_FINAL 145 5253+#define PK11_F_LIBRARY_INIT 146 5254+#define PK11_F_LOAD 147 5255+#define PK11_F_DH_GEN_KEY 148 5256+#define PK11_F_DH_COMP_KEY 149 5257+#define PK11_F_DIGEST_COPY 150 5258+#define PK11_F_CIPHER_CLEANUP 151 5259+#define PK11_F_ACTIVE_ADD 152 5260+#define PK11_F_ACTIVE_DELETE 153 5261+#define PK11_F_CHECK_HW_MECHANISMS 154 5262+#define PK11_F_INIT_SYMMETRIC 155 5263+#define PK11_F_ADD_AES_CTR_NIDS 156 5264+#define PK11_F_INIT_ALL_LOCKS 157 5265+#define PK11_F_RETURN_SESSION 158 5266+#define PK11_F_GET_PIN 159 5267+#define PK11_F_FIND_ONE_OBJECT 160 5268+#define PK11_F_CHECK_TOKEN_ATTRS 161 5269+#define PK11_F_CACHE_PIN 162 5270+#define PK11_F_MLOCK_PIN_IN_MEMORY 163 5271+#define PK11_F_TOKEN_LOGIN 164 5272+#define PK11_F_TOKEN_RELOGIN 165 5273+#define PK11_F_RUN_ASKPASS 166 5274+ 5275+/* Reason codes. */ 5276+#define PK11_R_ALREADY_LOADED 100 5277+#define PK11_R_DSO_FAILURE 101 5278+#define PK11_R_NOT_LOADED 102 5279+#define PK11_R_PASSED_NULL_PARAMETER 103 5280+#define PK11_R_COMMAND_NOT_IMPLEMENTED 104 5281+#define PK11_R_INITIALIZE 105 5282+#define PK11_R_FINALIZE 106 5283+#define PK11_R_GETINFO 107 5284+#define PK11_R_GETSLOTLIST 108 5285+#define PK11_R_NO_MODULUS_OR_NO_EXPONENT 109 5286+#define PK11_R_ATTRIBUT_SENSITIVE_OR_INVALID 110 5287+#define PK11_R_GETATTRIBUTVALUE 111 5288+#define PK11_R_NO_MODULUS 112 5289+#define PK11_R_NO_EXPONENT 113 5290+#define PK11_R_FINDOBJECTSINIT 114 5291+#define PK11_R_FINDOBJECTS 115 5292+#define PK11_R_FINDOBJECTSFINAL 116 5293+#define PK11_R_CREATEOBJECT 118 5294+#define PK11_R_DESTROYOBJECT 119 5295+#define PK11_R_OPENSESSION 120 5296+#define PK11_R_CLOSESESSION 121 5297+#define PK11_R_ENCRYPTINIT 122 5298+#define PK11_R_ENCRYPT 123 5299+#define PK11_R_SIGNINIT 124 5300+#define PK11_R_SIGN 125 5301+#define PK11_R_DECRYPTINIT 126 5302+#define PK11_R_DECRYPT 127 5303+#define PK11_R_VERIFYINIT 128 5304+#define PK11_R_VERIFY 129 5305+#define PK11_R_VERIFYRECOVERINIT 130 5306+#define PK11_R_VERIFYRECOVER 131 5307+#define PK11_R_GEN_KEY 132 5308+#define PK11_R_SEEDRANDOM 133 5309+#define PK11_R_GENERATERANDOM 134 5310+#define PK11_R_INVALID_MESSAGE_LENGTH 135 5311+#define PK11_R_UNKNOWN_ALGORITHM_TYPE 136 5312+#define PK11_R_UNKNOWN_ASN1_OBJECT_ID 137 5313+#define PK11_R_UNKNOWN_PADDING_TYPE 138 5314+#define PK11_R_PADDING_CHECK_FAILED 139 5315+#define PK11_R_DIGEST_TOO_BIG 140 5316+#define PK11_R_MALLOC_FAILURE 141 5317+#define PK11_R_CTRL_COMMAND_NOT_IMPLEMENTED 142 5318+#define PK11_R_DATA_GREATER_THAN_MOD_LEN 143 5319+#define PK11_R_DATA_TOO_LARGE_FOR_MODULUS 144 5320+#define PK11_R_MISSING_KEY_COMPONENT 145 5321+#define PK11_R_INVALID_SIGNATURE_LENGTH 146 5322+#define PK11_R_INVALID_DSA_SIGNATURE_R 147 5323+#define PK11_R_INVALID_DSA_SIGNATURE_S 148 5324+#define PK11_R_INCONSISTENT_KEY 149 5325+#define PK11_R_ENCRYPTUPDATE 150 5326+#define PK11_R_DECRYPTUPDATE 151 5327+#define PK11_R_DIGESTINIT 152 5328+#define PK11_R_DIGESTUPDATE 153 5329+#define PK11_R_DIGESTFINAL 154 5330+#define PK11_R_ENCRYPTFINAL 155 5331+#define PK11_R_DECRYPTFINAL 156 5332+#define PK11_R_NO_PRNG_SUPPORT 157 5333+#define PK11_R_GETTOKENINFO 158 5334+#define PK11_R_DERIVEKEY 159 5335+#define PK11_R_GET_OPERATION_STATE 160 5336+#define PK11_R_SET_OPERATION_STATE 161 5337+#define PK11_R_INVALID_HANDLE 162 5338+#define PK11_R_KEY_OR_IV_LEN_PROBLEM 163 5339+#define PK11_R_INVALID_OPERATION_TYPE 164 5340+#define PK11_R_ADD_NID_FAILED 165 5341+#define PK11_R_ATFORK_FAILED 166 5342+ 5343+#define PK11_R_TOKEN_LOGIN_FAILED 167 5344+#define PK11_R_MORE_THAN_ONE_OBJECT_FOUND 168 5345+#define PK11_R_INVALID_PKCS11_URI 169 5346+#define PK11_R_COULD_NOT_READ_PIN 170 5347+#define PK11_R_COULD_NOT_OPEN_COMMAND 171 5348+#define PK11_R_PIPE_FAILED 172 5349+#define PK11_R_PIN_NOT_READ_FROM_COMMAND 173 5350+#define PK11_R_BAD_PASSPHRASE_SPEC 174 5351+#define PK11_R_TOKEN_NOT_INITIALIZED 175 5352+#define PK11_R_TOKEN_PIN_NOT_SET 176 5353+#define PK11_R_TOKEN_PIN_NOT_PROVIDED 177 5354+#define PK11_R_MISSING_OBJECT_LABEL 178 5355+#define PK11_R_TOKEN_ATTRS_DO_NOT_MATCH 179 5356+#define PK11_R_PRIV_KEY_NOT_FOUND 180 5357+#define PK11_R_NO_OBJECT_FOUND 181 5358+#define PK11_R_PIN_CACHING_POLICY_INVALID 182 5359+#define PK11_R_SYSCONF_FAILED 183 5360+#define PK11_R_MMAP_FAILED 183 5361+#define PK11_R_PRIV_PROC_LOCK_MEMORY_MISSING 184 5362+#define PK11_R_MLOCK_FAILED 185 5363+#define PK11_R_FORK_FAILED 186 5364+ 5365+/* max byte length of a symetric key we support */ 5366+#define PK11_KEY_LEN_MAX 32 5367+ 5368+#ifdef NOPTHREADS 5369+/* 5370+ * CRYPTO_LOCK_PK11_ENGINE lock is primarily used for the protection of the 5371+ * free_session list and active_list but generally serves as a global 5372+ * per-process lock for the whole engine. 5373+ * 5374+ * We reuse CRYPTO_LOCK_EC lock (which is defined in OpenSSL for EC method) as 5375+ * the global engine lock. This is not optimal w.r.t. performance but 5376+ * it's safe. 5377+ */ 5378+#define CRYPTO_LOCK_PK11_ENGINE CRYPTO_LOCK_EC 5379+#endif 5380+ 5381+/* 5382+ * This structure encapsulates all reusable information for a PKCS#11 5383+ * session. A list of these objects is created on behalf of the 5384+ * calling application using an on-demand method. Each operation 5385+ * type (see PK11_OPTYPE below) has its own per-process list. 5386+ * Each of the lists is basically a cache for faster PKCS#11 object 5387+ * access to avoid expensive C_Find{,Init,Final}Object() calls. 5388+ * 5389+ * When a new request comes in, an object will be taken from the list 5390+ * (if there is one) or a new one is created to handle the request 5391+ * (if the list is empty). See pk11_get_session() on how it is done. 5392+ */ 5393+typedef struct PK11_st_SESSION 5394+ { 5395+ struct PK11_st_SESSION *next; 5396+ CK_SESSION_HANDLE session; /* PK11 session handle */ 5397+ pid_t pid; /* Current process ID */ 5398+ CK_BBOOL pub_persistent; /* is pub key in keystore? */ 5399+ CK_BBOOL priv_persistent;/* is priv key in keystore? */ 5400+ union 5401+ { 5402+#ifndef OPENSSL_NO_RSA 5403+ struct 5404+ { 5405+ CK_OBJECT_HANDLE rsa_pub_key; /* pub handle */ 5406+ CK_OBJECT_HANDLE rsa_priv_key; /* priv handle */ 5407+ RSA *rsa_pub; /* pub key addr */ 5408+ BIGNUM *rsa_n_num; /* pub modulus */ 5409+ BIGNUM *rsa_e_num; /* pub exponent */ 5410+ RSA *rsa_priv; /* priv key addr */ 5411+ BIGNUM *rsa_pn_num; /* pub modulus */ 5412+ BIGNUM *rsa_pe_num; /* pub exponent */ 5413+ BIGNUM *rsa_d_num; /* priv exponent */ 5414+ } u_RSA; 5415+#endif /* OPENSSL_NO_RSA */ 5416+#ifndef OPENSSL_NO_DSA 5417+ struct 5418+ { 5419+ CK_OBJECT_HANDLE dsa_pub_key; /* pub handle */ 5420+ CK_OBJECT_HANDLE dsa_priv_key; /* priv handle */ 5421+ DSA *dsa_pub; /* pub key addr */ 5422+ BIGNUM *dsa_pub_num; /* pub key */ 5423+ DSA *dsa_priv; /* priv key addr */ 5424+ BIGNUM *dsa_priv_num; /* priv key */ 5425+ } u_DSA; 5426+#endif /* OPENSSL_NO_DSA */ 5427+#ifndef OPENSSL_NO_DH 5428+ struct 5429+ { 5430+ CK_OBJECT_HANDLE dh_key; /* key handle */ 5431+ DH *dh; /* dh key addr */ 5432+ BIGNUM *dh_priv_num; /* priv dh key */ 5433+ } u_DH; 5434+#endif /* OPENSSL_NO_DH */ 5435+ struct 5436+ { 5437+ CK_OBJECT_HANDLE cipher_key; /* key handle */ 5438+ unsigned char key[PK11_KEY_LEN_MAX]; 5439+ int key_len; /* priv key len */ 5440+ int encrypt; /* 1/0 enc/decr */ 5441+ } u_cipher; 5442+ } opdata_u; 5443+ } PK11_SESSION; 5444+ 5445+#define opdata_rsa_pub_key opdata_u.u_RSA.rsa_pub_key 5446+#define opdata_rsa_priv_key opdata_u.u_RSA.rsa_priv_key 5447+#define opdata_rsa_pub opdata_u.u_RSA.rsa_pub 5448+#define opdata_rsa_priv opdata_u.u_RSA.rsa_priv 5449+#define opdata_rsa_n_num opdata_u.u_RSA.rsa_n_num 5450+#define opdata_rsa_e_num opdata_u.u_RSA.rsa_e_num 5451+#define opdata_rsa_pn_num opdata_u.u_RSA.rsa_pn_num 5452+#define opdata_rsa_pe_num opdata_u.u_RSA.rsa_pe_num 5453+#define opdata_rsa_d_num opdata_u.u_RSA.rsa_d_num 5454+#define opdata_dsa_pub_key opdata_u.u_DSA.dsa_pub_key 5455+#define opdata_dsa_priv_key opdata_u.u_DSA.dsa_priv_key 5456+#define opdata_dsa_pub opdata_u.u_DSA.dsa_pub 5457+#define opdata_dsa_pub_num opdata_u.u_DSA.dsa_pub_num 5458+#define opdata_dsa_priv opdata_u.u_DSA.dsa_priv 5459+#define opdata_dsa_priv_num opdata_u.u_DSA.dsa_priv_num 5460+#define opdata_dh_key opdata_u.u_DH.dh_key 5461+#define opdata_dh opdata_u.u_DH.dh 5462+#define opdata_dh_priv_num opdata_u.u_DH.dh_priv_num 5463+#define opdata_cipher_key opdata_u.u_cipher.cipher_key 5464+#define opdata_key opdata_u.u_cipher.key 5465+#define opdata_key_len opdata_u.u_cipher.key_len 5466+#define opdata_encrypt opdata_u.u_cipher.encrypt 5467+ 5468+/* 5469+ * We have 3 different groups of operation types: 5470+ * 1) asymmetric operations 5471+ * 2) random operations 5472+ * 3) symmetric and digest operations 5473+ * 5474+ * This division into groups stems from the fact that it's common that hardware 5475+ * providers may support operations from one group only. For example, hardware 5476+ * providers on UltraSPARC T2, n2rng(7d), ncp(7d), and n2cp(7d), each support 5477+ * only a single group of operations. 5478+ * 5479+ * For every group a different slot can be chosen. That means that we must have 5480+ * at least 3 different lists of cached PKCS#11 sessions since sessions from 5481+ * different groups may be initialized in different slots. 5482+ * 5483+ * To provide locking granularity in multithreaded environment, the groups are 5484+ * further splitted into types with each type having a separate session cache. 5485+ */ 5486+typedef enum PK11_OPTYPE_ENUM 5487+ { 5488+ OP_RAND, 5489+ OP_RSA, 5490+ OP_DSA, 5491+ OP_DH, 5492+ OP_CIPHER, 5493+ OP_DIGEST, 5494+ OP_MAX 5495+ } PK11_OPTYPE; 5496+ 5497+/* 5498+ * This structure contains the heads of the lists forming the object caches 5499+ * and locks associated with the lists. 5500+ */ 5501+typedef struct PK11_st_CACHE 5502+ { 5503+ PK11_SESSION *head; 5504+#ifndef NOPTHREADS 5505+ pthread_mutex_t *lock; 5506+#endif 5507+ } PK11_CACHE; 5508+ 5509+/* structure for tracking handles of asymmetric key objects */ 5510+typedef struct PK11_active_st 5511+ { 5512+ CK_OBJECT_HANDLE h; 5513+ unsigned int refcnt; 5514+ struct PK11_active_st *prev; 5515+ struct PK11_active_st *next; 5516+ } PK11_active; 5517+ 5518+#ifndef NOPTHREADS 5519+extern pthread_mutex_t *find_lock[]; 5520+#endif 5521+extern PK11_active *active_list[]; 5522+/* 5523+ * These variables are specific for the RSA keys by reference code. See 5524+ * hw_pk11_pub.c for explanation. 5525+ */ 5526+extern CK_FLAGS pubkey_token_flags; 5527+ 5528+#ifndef NOPTHREADS 5529+#define LOCK_OBJSTORE(alg_type) \ 5530+ (void) pthread_mutex_lock(find_lock[alg_type]) 5531+#define UNLOCK_OBJSTORE(alg_type) \ 5532+ (void) pthread_mutex_unlock(find_lock[alg_type]) 5533+#else 5534+#define LOCK_OBJSTORE(alg_type) \ 5535+ CRYPTO_w_lock(CRYPTO_LOCK_PK11_ENGINE) 5536+#define UNLOCK_OBJSTORE(alg_type) \ 5537+ CRYPTO_w_unlock(CRYPTO_LOCK_PK11_ENGINE) 5538+#endif 5539+ 5540+extern PK11_SESSION *pk11_get_session(PK11_OPTYPE optype); 5541+extern void pk11_return_session(PK11_SESSION *sp, PK11_OPTYPE optype); 5542+extern int pk11_token_relogin(CK_SESSION_HANDLE session); 5543+ 5544+#ifndef OPENSSL_NO_RSA 5545+extern int pk11_destroy_rsa_key_objects(PK11_SESSION *session); 5546+extern int pk11_destroy_rsa_object_pub(PK11_SESSION *sp, CK_BBOOL uselock); 5547+extern int pk11_destroy_rsa_object_priv(PK11_SESSION *sp, CK_BBOOL uselock); 5548+extern EVP_PKEY *pk11_load_privkey(ENGINE *e, const char *pubkey_file, 5549+ UI_METHOD *ui_method, void *callback_data); 5550+extern EVP_PKEY *pk11_load_pubkey(ENGINE *e, const char *pubkey_file, 5551+ UI_METHOD *ui_method, void *callback_data); 5552+extern RSA_METHOD *PK11_RSA(void); 5553+#endif /* OPENSSL_NO_RSA */ 5554+#ifndef OPENSSL_NO_DSA 5555+extern int pk11_destroy_dsa_key_objects(PK11_SESSION *session); 5556+extern int pk11_destroy_dsa_object_pub(PK11_SESSION *sp, CK_BBOOL uselock); 5557+extern int pk11_destroy_dsa_object_priv(PK11_SESSION *sp, CK_BBOOL uselock); 5558+extern DSA_METHOD *PK11_DSA(void); 5559+#endif /* OPENSSL_NO_DSA */ 5560+#ifndef OPENSSL_NO_DH 5561+extern int pk11_destroy_dh_key_objects(PK11_SESSION *session); 5562+extern int pk11_destroy_dh_object(PK11_SESSION *sp, CK_BBOOL uselock); 5563+extern DH_METHOD *PK11_DH(void); 5564+#endif /* OPENSSL_NO_DH */ 5565+ 5566+extern CK_FUNCTION_LIST_PTR pFuncList; 5567+ 5568+#endif /* HW_PK11_ERR_H */ 5569Index: openssl/crypto/engine/hw_pk11_pub.c 5570diff -u /dev/null openssl/crypto/engine/hw_pk11_pub.c:1.32.4.3 5571--- /dev/null Mon Jan 16 18:53:42 2012 5572+++ openssl/crypto/engine/hw_pk11_pub.c Fri Jun 17 07:56:20 2011 5573@@ -0,0 +1,3530 @@ 5574+/* 5575+ * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 5576+ * Use is subject to license terms. 5577+ */ 5578+ 5579+/* crypto/engine/hw_pk11_pub.c */ 5580+/* 5581+ * This product includes software developed by the OpenSSL Project for 5582+ * use in the OpenSSL Toolkit (http://www.openssl.org/). 5583+ * 5584+ * This project also referenced hw_pkcs11-0.9.7b.patch written by 5585+ * Afchine Madjlessi. 5586+ */ 5587+/* 5588+ * ==================================================================== 5589+ * Copyright (c) 2000-2001 The OpenSSL Project. All rights reserved. 5590+ * 5591+ * Redistribution and use in source and binary forms, with or without 5592+ * modification, are permitted provided that the following conditions 5593+ * are met: 5594+ * 5595+ * 1. Redistributions of source code must retain the above copyright 5596+ * notice, this list of conditions and the following disclaimer. 5597+ * 5598+ * 2. Redistributions in binary form must reproduce the above copyright 5599+ * notice, this list of conditions and the following disclaimer in 5600+ * the documentation and/or other materials provided with the 5601+ * distribution. 5602+ * 5603+ * 3. All advertising materials mentioning features or use of this 5604+ * software must display the following acknowledgment: 5605+ * "This product includes software developed by the OpenSSL Project 5606+ * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 5607+ * 5608+ * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 5609+ * endorse or promote products derived from this software without 5610+ * prior written permission. For written permission, please contact 5611+ * licensing@OpenSSL.org. 5612+ * 5613+ * 5. Products derived from this software may not be called "OpenSSL" 5614+ * nor may "OpenSSL" appear in their names without prior written 5615+ * permission of the OpenSSL Project. 5616+ * 5617+ * 6. Redistributions of any form whatsoever must retain the following 5618+ * acknowledgment: 5619+ * "This product includes software developed by the OpenSSL Project 5620+ * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 5621+ * 5622+ * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 5623+ * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 5624+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 5625+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 5626+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 5627+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 5628+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 5629+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 5630+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 5631+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 5632+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 5633+ * OF THE POSSIBILITY OF SUCH DAMAGE. 5634+ * ==================================================================== 5635+ * 5636+ * This product includes cryptographic software written by Eric Young 5637+ * (eay@cryptsoft.com). This product includes software written by Tim 5638+ * Hudson (tjh@cryptsoft.com). 5639+ * 5640+ */ 5641+ 5642+#include <stdio.h> 5643+#include <stdlib.h> 5644+#include <string.h> 5645+#include <sys/types.h> 5646+ 5647+#include <openssl/e_os2.h> 5648+#include <openssl/crypto.h> 5649+#include <cryptlib.h> 5650+#include <openssl/engine.h> 5651+#include <openssl/dso.h> 5652+#include <openssl/err.h> 5653+#include <openssl/bn.h> 5654+#include <openssl/pem.h> 5655+#ifndef OPENSSL_NO_RSA 5656+#include <openssl/rsa.h> 5657+#endif /* OPENSSL_NO_RSA */ 5658+#ifndef OPENSSL_NO_DSA 5659+#include <openssl/dsa.h> 5660+#endif /* OPENSSL_NO_DSA */ 5661+#ifndef OPENSSL_NO_DH 5662+#include <openssl/dh.h> 5663+#endif /* OPENSSL_NO_DH */ 5664+#include <openssl/rand.h> 5665+#include <openssl/objects.h> 5666+#include <openssl/x509.h> 5667+ 5668+#ifdef OPENSSL_SYS_WIN32 5669+#define NOPTHREADS 5670+typedef int pid_t; 5671+#define HAVE_GETPASSPHRASE 5672+static char *getpassphrase(const char *prompt); 5673+#ifndef NULL_PTR 5674+#define NULL_PTR NULL 5675+#endif 5676+#define CK_DEFINE_FUNCTION(returnType, name) \ 5677+ returnType __declspec(dllexport) name 5678+#define CK_DECLARE_FUNCTION(returnType, name) \ 5679+ returnType __declspec(dllimport) name 5680+#define CK_DECLARE_FUNCTION_POINTER(returnType, name) \ 5681+ returnType __declspec(dllimport) (* name) 5682+#else 5683+#include <unistd.h> 5684+#endif 5685+ 5686+#ifndef NOPTHREADS 5687+#include <pthread.h> 5688+#endif 5689+ 5690+#ifndef OPENSSL_NO_HW 5691+#ifndef OPENSSL_NO_HW_PK11 5692+#ifndef OPENSSL_NO_HW_PK11CA 5693+ 5694+#ifdef OPENSSL_SYS_WIN32 5695+#pragma pack(push, cryptoki, 1) 5696+#include "cryptoki.h" 5697+#include "pkcs11.h" 5698+#pragma pack(pop, cryptoki) 5699+#else 5700+#include "cryptoki.h" 5701+#include "pkcs11.h" 5702+#endif 5703+#include "hw_pk11ca.h" 5704+#include "hw_pk11_err.h" 5705+ 5706+static CK_BBOOL pk11_login_done = CK_FALSE; 5707+extern CK_SLOT_ID pubkey_SLOTID; 5708+#ifndef NOPTHREADS 5709+extern pthread_mutex_t *token_lock; 5710+#endif 5711+ 5712+#if !(defined(HAVE_GETPASSPHRASE) || (defined (__SVR4) && defined (__sun))) 5713+#define getpassphrase(x) getpass(x) 5714+#endif 5715+ 5716+#ifndef OPENSSL_NO_RSA 5717+/* RSA stuff */ 5718+static int pk11_RSA_public_encrypt(int flen, const unsigned char *from, 5719+ unsigned char *to, RSA *rsa, int padding); 5720+static int pk11_RSA_private_encrypt(int flen, const unsigned char *from, 5721+ unsigned char *to, RSA *rsa, int padding); 5722+static int pk11_RSA_public_decrypt(int flen, const unsigned char *from, 5723+ unsigned char *to, RSA *rsa, int padding); 5724+static int pk11_RSA_private_decrypt(int flen, const unsigned char *from, 5725+ unsigned char *to, RSA *rsa, int padding); 5726+static int pk11_RSA_init(RSA *rsa); 5727+static int pk11_RSA_finish(RSA *rsa); 5728+static int pk11_RSA_sign(int type, const unsigned char *m, unsigned int m_len, 5729+ unsigned char *sigret, unsigned int *siglen, const RSA *rsa); 5730+#if OPENSSL_VERSION_NUMBER < 0x10000000L 5731+static int pk11_RSA_verify(int dtype, const unsigned char *m, 5732+ unsigned int m_len, unsigned char *sigbuf, unsigned int siglen, 5733+ const RSA *rsa); 5734+#else 5735+static int pk11_RSA_verify(int dtype, const unsigned char *m, 5736+ unsigned int m_len, const unsigned char *sigbuf, unsigned int siglen, 5737+ const RSA *rsa); 5738+#endif 5739+EVP_PKEY *pk11_load_privkey(ENGINE*, const char *privkey_file, 5740+ UI_METHOD *ui_method, void *callback_data); 5741+EVP_PKEY *pk11_load_pubkey(ENGINE*, const char *pubkey_file, 5742+ UI_METHOD *ui_method, void *callback_data); 5743+ 5744+static int pk11_RSA_public_encrypt_low(int flen, const unsigned char *from, 5745+ unsigned char *to, RSA *rsa); 5746+static int pk11_RSA_private_encrypt_low(int flen, const unsigned char *from, 5747+ unsigned char *to, RSA *rsa); 5748+static int pk11_RSA_public_decrypt_low(int flen, const unsigned char *from, 5749+ unsigned char *to, RSA *rsa); 5750+static int pk11_RSA_private_decrypt_low(int flen, const unsigned char *from, 5751+ unsigned char *to, RSA *rsa); 5752+ 5753+static CK_OBJECT_HANDLE pk11_get_public_rsa_key(RSA* rsa, RSA** key_ptr, 5754+ BIGNUM **rsa_n_num, BIGNUM **rsa_e_num, CK_SESSION_HANDLE session); 5755+static CK_OBJECT_HANDLE pk11_get_private_rsa_key(RSA* rsa, RSA** key_ptr, 5756+ BIGNUM **rsa_d_num, BIGNUM **rsa_n_num, BIGNUM **rsa_e_num, 5757+ CK_SESSION_HANDLE session); 5758+ 5759+static int check_new_rsa_key_pub(PK11_SESSION *sp, const RSA *rsa); 5760+static int check_new_rsa_key_priv(PK11_SESSION *sp, const RSA *rsa); 5761+#endif 5762+ 5763+/* DSA stuff */ 5764+#ifndef OPENSSL_NO_DSA 5765+static int pk11_DSA_init(DSA *dsa); 5766+static int pk11_DSA_finish(DSA *dsa); 5767+static DSA_SIG *pk11_dsa_do_sign(const unsigned char *dgst, int dlen, 5768+ DSA *dsa); 5769+static int pk11_dsa_do_verify(const unsigned char *dgst, int dgst_len, 5770+ DSA_SIG *sig, DSA *dsa); 5771+ 5772+static CK_OBJECT_HANDLE pk11_get_public_dsa_key(DSA* dsa, DSA **key_ptr, 5773+ BIGNUM **dsa_pub_num, CK_SESSION_HANDLE session); 5774+static CK_OBJECT_HANDLE pk11_get_private_dsa_key(DSA* dsa, DSA **key_ptr, 5775+ BIGNUM **dsa_priv_num, CK_SESSION_HANDLE session); 5776+ 5777+static int check_new_dsa_key_pub(PK11_SESSION *sp, DSA *dsa); 5778+static int check_new_dsa_key_priv(PK11_SESSION *sp, DSA *dsa); 5779+#endif 5780+ 5781+/* DH stuff */ 5782+#ifndef OPENSSL_NO_DH 5783+static int pk11_DH_init(DH *dh); 5784+static int pk11_DH_finish(DH *dh); 5785+static int pk11_DH_generate_key(DH *dh); 5786+static int pk11_DH_compute_key(unsigned char *key, 5787+ const BIGNUM *pub_key, DH *dh); 5788+ 5789+static CK_OBJECT_HANDLE pk11_get_dh_key(DH* dh, DH **key_ptr, 5790+ BIGNUM **priv_key, CK_SESSION_HANDLE session); 5791+ 5792+static int check_new_dh_key(PK11_SESSION *sp, DH *dh); 5793+#endif 5794+ 5795+static int find_one_object(PK11_OPTYPE op, CK_SESSION_HANDLE s, 5796+ CK_ATTRIBUTE_PTR ptempl, CK_ULONG nattr, CK_OBJECT_HANDLE_PTR pkey); 5797+static int init_template_value(BIGNUM *bn, CK_VOID_PTR *pValue, 5798+ CK_ULONG *ulValueLen); 5799+static void attr_to_BN(CK_ATTRIBUTE_PTR attr, CK_BYTE attr_data[], BIGNUM **bn); 5800+ 5801+static int pk11_token_login(CK_SESSION_HANDLE session, CK_BBOOL *login_done, 5802+ CK_BBOOL is_private); 5803+ 5804+/* Read mode string to be used for fopen() */ 5805+#if SOLARIS_OPENSSL 5806+static char *read_mode_flags = "rF"; 5807+#else 5808+static char *read_mode_flags = "r"; 5809+#endif 5810+ 5811+/* 5812+ * increment/create reference for an asymmetric key handle via active list 5813+ * manipulation. If active list operation fails, unlock (if locked), set error 5814+ * variable and jump to the specified label. 5815+ */ 5816+#define KEY_HANDLE_REFHOLD(key_handle, alg_type, unlock, var, label) \ 5817+ { \ 5818+ if (pk11_active_add(key_handle, alg_type) < 0) \ 5819+ { \ 5820+ var = TRUE; \ 5821+ if (unlock) \ 5822+ UNLOCK_OBJSTORE(alg_type); \ 5823+ goto label; \ 5824+ } \ 5825+ } 5826+ 5827+/* 5828+ * Find active list entry according to object handle and return pointer to the 5829+ * entry otherwise return NULL. 5830+ * 5831+ * This function presumes it is called with lock protecting the active list 5832+ * held. 5833+ */ 5834+static PK11_active *pk11_active_find(CK_OBJECT_HANDLE h, PK11_OPTYPE type) 5835+ { 5836+ PK11_active *entry; 5837+ 5838+ for (entry = active_list[type]; entry != NULL; entry = entry->next) 5839+ if (entry->h == h) 5840+ return (entry); 5841+ 5842+ return (NULL); 5843+ } 5844+ 5845+/* 5846+ * Search for an entry in the active list using PKCS#11 object handle as a 5847+ * search key and return refcnt of the found/created entry or -1 in case of 5848+ * failure. 5849+ * 5850+ * This function presumes it is called with lock protecting the active list 5851+ * held. 5852+ */ 5853+int 5854+pk11_active_add(CK_OBJECT_HANDLE h, PK11_OPTYPE type) 5855+ { 5856+ PK11_active *entry = NULL; 5857+ 5858+ if (h == CK_INVALID_HANDLE) 5859+ { 5860+ PK11err(PK11_F_ACTIVE_ADD, PK11_R_INVALID_HANDLE); 5861+ return (-1); 5862+ } 5863+ 5864+ /* search for entry in the active list */ 5865+ if ((entry = pk11_active_find(h, type)) != NULL) 5866+ entry->refcnt++; 5867+ else 5868+ { 5869+ /* not found, create new entry and add it to the list */ 5870+ entry = OPENSSL_malloc(sizeof (PK11_active)); 5871+ if (entry == NULL) 5872+ { 5873+ PK11err(PK11_F_ACTIVE_ADD, PK11_R_MALLOC_FAILURE); 5874+ return (-1); 5875+ } 5876+ entry->h = h; 5877+ entry->refcnt = 1; 5878+ entry->prev = NULL; 5879+ entry->next = NULL; 5880+ /* connect the newly created entry to the list */ 5881+ if (active_list[type] == NULL) 5882+ active_list[type] = entry; 5883+ else /* make the entry first in the list */ 5884+ { 5885+ entry->next = active_list[type]; 5886+ active_list[type]->prev = entry; 5887+ active_list[type] = entry; 5888+ } 5889+ } 5890+ 5891+ return (entry->refcnt); 5892+ } 5893+ 5894+/* 5895+ * Remove active list entry from the list and free it. 5896+ * 5897+ * This function presumes it is called with lock protecting the active list 5898+ * held. 5899+ */ 5900+void 5901+pk11_active_remove(PK11_active *entry, PK11_OPTYPE type) 5902+ { 5903+ PK11_active *prev_entry; 5904+ 5905+ /* remove the entry from the list and free it */ 5906+ if ((prev_entry = entry->prev) != NULL) 5907+ { 5908+ prev_entry->next = entry->next; 5909+ if (entry->next != NULL) 5910+ entry->next->prev = prev_entry; 5911+ } 5912+ else 5913+ { 5914+ active_list[type] = entry->next; 5915+ /* we were the first but not the only one */ 5916+ if (entry->next != NULL) 5917+ entry->next->prev = NULL; 5918+ } 5919+ 5920+ /* sanitization */ 5921+ entry->h = CK_INVALID_HANDLE; 5922+ entry->prev = NULL; 5923+ entry->next = NULL; 5924+ OPENSSL_free(entry); 5925+ } 5926+ 5927+/* Free all entries from the active list. */ 5928+void 5929+pk11_free_active_list(PK11_OPTYPE type) 5930+ { 5931+ PK11_active *entry; 5932+ 5933+ /* only for asymmetric types since only they have C_Find* locks. */ 5934+ switch (type) 5935+ { 5936+ case OP_RSA: 5937+ case OP_DSA: 5938+ case OP_DH: 5939+ break; 5940+ default: 5941+ return; 5942+ } 5943+ 5944+ /* see find_lock array definition for more info on object locking */ 5945+ LOCK_OBJSTORE(type); 5946+ while ((entry = active_list[type]) != NULL) 5947+ pk11_active_remove(entry, type); 5948+ UNLOCK_OBJSTORE(type); 5949+ } 5950+ 5951+/* 5952+ * Search for active list entry associated with given PKCS#11 object handle, 5953+ * decrement its refcnt and if it drops to 0, disconnect the entry and free it. 5954+ * 5955+ * Return 1 if the PKCS#11 object associated with the entry has no references, 5956+ * return 0 if there is at least one reference, -1 on error. 5957+ * 5958+ * This function presumes it is called with lock protecting the active list 5959+ * held. 5960+ */ 5961+int 5962+pk11_active_delete(CK_OBJECT_HANDLE h, PK11_OPTYPE type) 5963+ { 5964+ PK11_active *entry = NULL; 5965+ 5966+ if ((entry = pk11_active_find(h, type)) == NULL) 5967+ { 5968+ PK11err(PK11_F_ACTIVE_DELETE, PK11_R_INVALID_HANDLE); 5969+ return (-1); 5970+ } 5971+ 5972+ OPENSSL_assert(entry->refcnt > 0); 5973+ entry->refcnt--; 5974+ if (entry->refcnt == 0) 5975+ { 5976+ pk11_active_remove(entry, type); 5977+ return (1); 5978+ } 5979+ 5980+ return (0); 5981+ } 5982+ 5983+#ifndef OPENSSL_NO_RSA 5984+/* Our internal RSA_METHOD that we provide pointers to */ 5985+static RSA_METHOD pk11_rsa = 5986+ { 5987+ "PKCS#11 RSA method", 5988+ pk11_RSA_public_encrypt, /* rsa_pub_encrypt */ 5989+ pk11_RSA_public_decrypt, /* rsa_pub_decrypt */ 5990+ pk11_RSA_private_encrypt, /* rsa_priv_encrypt */ 5991+ pk11_RSA_private_decrypt, /* rsa_priv_decrypt */ 5992+ NULL, /* rsa_mod_exp */ 5993+ NULL, /* bn_mod_exp */ 5994+ pk11_RSA_init, /* init */ 5995+ pk11_RSA_finish, /* finish */ 5996+ RSA_FLAG_SIGN_VER, /* flags */ 5997+ NULL, /* app_data */ 5998+ pk11_RSA_sign, /* rsa_sign */ 5999+ pk11_RSA_verify /* rsa_verify */ 6000+ }; 6001+ 6002+RSA_METHOD * 6003+PK11_RSA(void) 6004+ { 6005+ return (&pk11_rsa); 6006+ } 6007+#endif 6008+ 6009+#ifndef OPENSSL_NO_DSA 6010+/* Our internal DSA_METHOD that we provide pointers to */ 6011+static DSA_METHOD pk11_dsa = 6012+ { 6013+ "PKCS#11 DSA method", 6014+ pk11_dsa_do_sign, /* dsa_do_sign */ 6015+ NULL, /* dsa_sign_setup */ 6016+ pk11_dsa_do_verify, /* dsa_do_verify */ 6017+ NULL, /* dsa_mod_exp */ 6018+ NULL, /* bn_mod_exp */ 6019+ pk11_DSA_init, /* init */ 6020+ pk11_DSA_finish, /* finish */ 6021+ 0, /* flags */ 6022+ NULL /* app_data */ 6023+ }; 6024+ 6025+DSA_METHOD * 6026+PK11_DSA(void) 6027+ { 6028+ return (&pk11_dsa); 6029+ } 6030+#endif 6031+ 6032+#ifndef OPENSSL_NO_DH 6033+/* 6034+ * PKCS #11 V2.20, section 11.2 specifies that the number of bytes needed for 6035+ * output buffer may somewhat exceed the precise number of bytes needed, but 6036+ * should not exceed it by a large amount. That may be caused, for example, by 6037+ * rounding it up to multiple of X in the underlying bignum library. 8 should be 6038+ * enough. 6039+ */ 6040+#define DH_BUF_RESERVE 8 6041+ 6042+/* Our internal DH_METHOD that we provide pointers to */ 6043+static DH_METHOD pk11_dh = 6044+ { 6045+ "PKCS#11 DH method", 6046+ pk11_DH_generate_key, /* generate_key */ 6047+ pk11_DH_compute_key, /* compute_key */ 6048+ NULL, /* bn_mod_exp */ 6049+ pk11_DH_init, /* init */ 6050+ pk11_DH_finish, /* finish */ 6051+ 0, /* flags */ 6052+ NULL, /* app_data */ 6053+ NULL /* generate_params */ 6054+ }; 6055+ 6056+DH_METHOD * 6057+PK11_DH(void) 6058+ { 6059+ return (&pk11_dh); 6060+ } 6061+#endif 6062+ 6063+/* Size of an SSL signature: MD5+SHA1 */ 6064+#define SSL_SIG_LENGTH 36 6065+ 6066+/* Lengths of DSA data and signature */ 6067+#define DSA_DATA_LEN 20 6068+#define DSA_SIGNATURE_LEN 40 6069+ 6070+static CK_BBOOL true = TRUE; 6071+static CK_BBOOL false = FALSE; 6072+ 6073+#ifndef OPENSSL_NO_RSA 6074+/* 6075+ * Similiar to OpenSSL to take advantage of the paddings. The goal is to 6076+ * support all paddings in this engine although PK11 library does not 6077+ * support all the paddings used in OpenSSL. 6078+ * The input errors should have been checked in the padding functions. 6079+ */ 6080+static int pk11_RSA_public_encrypt(int flen, const unsigned char *from, 6081+ unsigned char *to, RSA *rsa, int padding) 6082+ { 6083+ int i, num = 0, r = -1; 6084+ unsigned char *buf = NULL; 6085+ 6086+ num = BN_num_bytes(rsa->n); 6087+ if ((buf = (unsigned char *)OPENSSL_malloc(num)) == NULL) 6088+ { 6089+ RSAerr(PK11_F_RSA_PUB_ENC, PK11_R_MALLOC_FAILURE); 6090+ goto err; 6091+ } 6092+ 6093+ switch (padding) 6094+ { 6095+ case RSA_PKCS1_PADDING: 6096+ i = RSA_padding_add_PKCS1_type_2(buf, num, from, flen); 6097+ break; 6098+#ifndef OPENSSL_NO_SHA 6099+ case RSA_PKCS1_OAEP_PADDING: 6100+ i = RSA_padding_add_PKCS1_OAEP(buf, num, from, flen, NULL, 0); 6101+ break; 6102+#endif 6103+ case RSA_SSLV23_PADDING: 6104+ i = RSA_padding_add_SSLv23(buf, num, from, flen); 6105+ break; 6106+ case RSA_NO_PADDING: 6107+ i = RSA_padding_add_none(buf, num, from, flen); 6108+ break; 6109+ default: 6110+ RSAerr(PK11_F_RSA_PUB_ENC, PK11_R_UNKNOWN_PADDING_TYPE); 6111+ goto err; 6112+ } 6113+ if (i <= 0) goto err; 6114+ 6115+ /* PK11 functions are called here */ 6116+ r = pk11_RSA_public_encrypt_low(num, buf, to, rsa); 6117+err: 6118+ if (buf != NULL) 6119+ { 6120+ OPENSSL_cleanse(buf, num); 6121+ OPENSSL_free(buf); 6122+ } 6123+ return (r); 6124+ } 6125+ 6126+ 6127+/* 6128+ * Similar to Openssl to take advantage of the paddings. The input errors 6129+ * should be catched in the padding functions 6130+ */ 6131+static int pk11_RSA_private_encrypt(int flen, const unsigned char *from, 6132+ unsigned char *to, RSA *rsa, int padding) 6133+ { 6134+ int i, num = 0, r = -1; 6135+ unsigned char *buf = NULL; 6136+ 6137+ num = BN_num_bytes(rsa->n); 6138+ if ((buf = (unsigned char *)OPENSSL_malloc(num)) == NULL) 6139+ { 6140+ RSAerr(PK11_F_RSA_PRIV_ENC, PK11_R_MALLOC_FAILURE); 6141+ goto err; 6142+ } 6143+ 6144+ switch (padding) 6145+ { 6146+ case RSA_PKCS1_PADDING: 6147+ i = RSA_padding_add_PKCS1_type_1(buf, num, from, flen); 6148+ break; 6149+ case RSA_NO_PADDING: 6150+ i = RSA_padding_add_none(buf, num, from, flen); 6151+ break; 6152+ case RSA_SSLV23_PADDING: 6153+ default: 6154+ RSAerr(PK11_F_RSA_PRIV_ENC, PK11_R_UNKNOWN_PADDING_TYPE); 6155+ goto err; 6156+ } 6157+ if (i <= 0) goto err; 6158+ 6159+ /* PK11 functions are called here */ 6160+ r = pk11_RSA_private_encrypt_low(num, buf, to, rsa); 6161+err: 6162+ if (buf != NULL) 6163+ { 6164+ OPENSSL_cleanse(buf, num); 6165+ OPENSSL_free(buf); 6166+ } 6167+ return (r); 6168+ } 6169+ 6170+/* Similar to OpenSSL code. Input errors are also checked here */ 6171+static int pk11_RSA_private_decrypt(int flen, const unsigned char *from, 6172+ unsigned char *to, RSA *rsa, int padding) 6173+ { 6174+ BIGNUM f; 6175+ int j, num = 0, r = -1; 6176+ unsigned char *p; 6177+ unsigned char *buf = NULL; 6178+ 6179+ BN_init(&f); 6180+ 6181+ num = BN_num_bytes(rsa->n); 6182+ 6183+ if ((buf = (unsigned char *)OPENSSL_malloc(num)) == NULL) 6184+ { 6185+ RSAerr(PK11_F_RSA_PRIV_DEC, PK11_R_MALLOC_FAILURE); 6186+ goto err; 6187+ } 6188+ 6189+ /* 6190+ * This check was for equality but PGP does evil things 6191+ * and chops off the top '0' bytes 6192+ */ 6193+ if (flen > num) 6194+ { 6195+ RSAerr(PK11_F_RSA_PRIV_DEC, 6196+ PK11_R_DATA_GREATER_THAN_MOD_LEN); 6197+ goto err; 6198+ } 6199+ 6200+ /* make data into a big number */ 6201+ if (BN_bin2bn(from, (int)flen, &f) == NULL) 6202+ goto err; 6203+ 6204+ if (BN_ucmp(&f, rsa->n) >= 0) 6205+ { 6206+ RSAerr(PK11_F_RSA_PRIV_DEC, 6207+ PK11_R_DATA_TOO_LARGE_FOR_MODULUS); 6208+ goto err; 6209+ } 6210+ 6211+ /* PK11 functions are called here */ 6212+ r = pk11_RSA_private_decrypt_low(flen, from, buf, rsa); 6213+ 6214+ /* 6215+ * PK11 CKM_RSA_X_509 mechanism pads 0's at the beginning. 6216+ * Needs to skip these 0's paddings here. 6217+ */ 6218+ for (j = 0; j < r; j++) 6219+ if (buf[j] != 0) 6220+ break; 6221+ 6222+ p = buf + j; 6223+ j = r - j; /* j is only used with no-padding mode */ 6224+ 6225+ switch (padding) 6226+ { 6227+ case RSA_PKCS1_PADDING: 6228+ r = RSA_padding_check_PKCS1_type_2(to, num, p, j, num); 6229+ break; 6230+#ifndef OPENSSL_NO_SHA 6231+ case RSA_PKCS1_OAEP_PADDING: 6232+ r = RSA_padding_check_PKCS1_OAEP(to, num, p, j, num, NULL, 0); 6233+ break; 6234+#endif 6235+ case RSA_SSLV23_PADDING: 6236+ r = RSA_padding_check_SSLv23(to, num, p, j, num); 6237+ break; 6238+ case RSA_NO_PADDING: 6239+ r = RSA_padding_check_none(to, num, p, j, num); 6240+ break; 6241+ default: 6242+ RSAerr(PK11_F_RSA_PRIV_DEC, PK11_R_UNKNOWN_PADDING_TYPE); 6243+ goto err; 6244+ } 6245+ if (r < 0) 6246+ RSAerr(PK11_F_RSA_PRIV_DEC, PK11_R_PADDING_CHECK_FAILED); 6247+ 6248+err: 6249+ BN_clear_free(&f); 6250+ if (buf != NULL) 6251+ { 6252+ OPENSSL_cleanse(buf, num); 6253+ OPENSSL_free(buf); 6254+ } 6255+ return (r); 6256+ } 6257+ 6258+/* Similar to OpenSSL code. Input errors are also checked here */ 6259+static int pk11_RSA_public_decrypt(int flen, const unsigned char *from, 6260+ unsigned char *to, RSA *rsa, int padding) 6261+ { 6262+ BIGNUM f; 6263+ int i, num = 0, r = -1; 6264+ unsigned char *p; 6265+ unsigned char *buf = NULL; 6266+ 6267+ BN_init(&f); 6268+ num = BN_num_bytes(rsa->n); 6269+ buf = (unsigned char *)OPENSSL_malloc(num); 6270+ if (buf == NULL) 6271+ { 6272+ RSAerr(PK11_F_RSA_PUB_DEC, PK11_R_MALLOC_FAILURE); 6273+ goto err; 6274+ } 6275+ 6276+ /* 6277+ * This check was for equality but PGP does evil things 6278+ * and chops off the top '0' bytes 6279+ */ 6280+ if (flen > num) 6281+ { 6282+ RSAerr(PK11_F_RSA_PUB_DEC, PK11_R_DATA_GREATER_THAN_MOD_LEN); 6283+ goto err; 6284+ } 6285+ 6286+ if (BN_bin2bn(from, flen, &f) == NULL) 6287+ goto err; 6288+ 6289+ if (BN_ucmp(&f, rsa->n) >= 0) 6290+ { 6291+ RSAerr(PK11_F_RSA_PUB_DEC, 6292+ PK11_R_DATA_TOO_LARGE_FOR_MODULUS); 6293+ goto err; 6294+ } 6295+ 6296+ /* PK11 functions are called here */ 6297+ r = pk11_RSA_public_decrypt_low(flen, from, buf, rsa); 6298+ 6299+ /* 6300+ * PK11 CKM_RSA_X_509 mechanism pads 0's at the beginning. 6301+ * Needs to skip these 0's here 6302+ */ 6303+ for (i = 0; i < r; i++) 6304+ if (buf[i] != 0) 6305+ break; 6306+ 6307+ p = buf + i; 6308+ i = r - i; /* i is only used with no-padding mode */ 6309+ 6310+ switch (padding) 6311+ { 6312+ case RSA_PKCS1_PADDING: 6313+ r = RSA_padding_check_PKCS1_type_1(to, num, p, i, num); 6314+ break; 6315+ case RSA_NO_PADDING: 6316+ r = RSA_padding_check_none(to, num, p, i, num); 6317+ break; 6318+ default: 6319+ RSAerr(PK11_F_RSA_PUB_DEC, PK11_R_UNKNOWN_PADDING_TYPE); 6320+ goto err; 6321+ } 6322+ if (r < 0) 6323+ RSAerr(PK11_F_RSA_PUB_DEC, PK11_R_PADDING_CHECK_FAILED); 6324+ 6325+err: 6326+ BN_clear_free(&f); 6327+ if (buf != NULL) 6328+ { 6329+ OPENSSL_cleanse(buf, num); 6330+ OPENSSL_free(buf); 6331+ } 6332+ return (r); 6333+ } 6334+ 6335+/* 6336+ * This function implements RSA public encryption using C_EncryptInit and 6337+ * C_Encrypt pk11 interfaces. Note that the CKM_RSA_X_509 is used here. 6338+ * The calling function allocated sufficient memory in "to" to store results. 6339+ */ 6340+static int pk11_RSA_public_encrypt_low(int flen, 6341+ const unsigned char *from, unsigned char *to, RSA *rsa) 6342+ { 6343+ CK_ULONG bytes_encrypted = flen; 6344+ int retval = -1; 6345+ CK_RV rv; 6346+ CK_MECHANISM mech_rsa = {CKM_RSA_X_509, NULL, 0}; 6347+ CK_MECHANISM *p_mech = &mech_rsa; 6348+ CK_OBJECT_HANDLE h_pub_key = CK_INVALID_HANDLE; 6349+ PK11_SESSION *sp; 6350+ 6351+ if ((sp = pk11_get_session(OP_RSA)) == NULL) 6352+ return (-1); 6353+ 6354+ (void) check_new_rsa_key_pub(sp, rsa); 6355+ 6356+ h_pub_key = sp->opdata_rsa_pub_key; 6357+ if (h_pub_key == CK_INVALID_HANDLE) 6358+ h_pub_key = sp->opdata_rsa_pub_key = 6359+ pk11_get_public_rsa_key(rsa, &sp->opdata_rsa_pub, 6360+ &sp->opdata_rsa_n_num, &sp->opdata_rsa_e_num, 6361+ sp->session); 6362+ 6363+ if (h_pub_key != CK_INVALID_HANDLE) 6364+ { 6365+ rv = pFuncList->C_EncryptInit(sp->session, p_mech, 6366+ h_pub_key); 6367+ 6368+ if (rv != CKR_OK) 6369+ { 6370+ PK11err_add_data(PK11_F_RSA_PUB_ENC_LOW, 6371+ PK11_R_ENCRYPTINIT, rv); 6372+ pk11_return_session(sp, OP_RSA); 6373+ return (-1); 6374+ } 6375+ 6376+ rv = pFuncList->C_Encrypt(sp->session, 6377+ (unsigned char *)from, flen, to, &bytes_encrypted); 6378+ 6379+ if (rv != CKR_OK) 6380+ { 6381+ PK11err_add_data(PK11_F_RSA_PUB_ENC_LOW, 6382+ PK11_R_ENCRYPT, rv); 6383+ pk11_return_session(sp, OP_RSA); 6384+ return (-1); 6385+ } 6386+ retval = bytes_encrypted; 6387+ } 6388+ 6389+ pk11_return_session(sp, OP_RSA); 6390+ return (retval); 6391+ } 6392+ 6393+ 6394+/* 6395+ * This function implements RSA private encryption using C_SignInit and 6396+ * C_Sign pk11 APIs. Note that CKM_RSA_X_509 is used here. 6397+ * The calling function allocated sufficient memory in "to" to store results. 6398+ */ 6399+static int pk11_RSA_private_encrypt_low(int flen, 6400+ const unsigned char *from, unsigned char *to, RSA *rsa) 6401+ { 6402+ CK_ULONG ul_sig_len = flen; 6403+ int retval = -1; 6404+ CK_RV rv; 6405+ CK_MECHANISM mech_rsa = {CKM_RSA_X_509, NULL, 0}; 6406+ CK_MECHANISM *p_mech = &mech_rsa; 6407+ CK_OBJECT_HANDLE h_priv_key = CK_INVALID_HANDLE; 6408+ PK11_SESSION *sp; 6409+ 6410+ if ((sp = pk11_get_session(OP_RSA)) == NULL) 6411+ return (-1); 6412+ 6413+ (void) check_new_rsa_key_priv(sp, rsa); 6414+ 6415+ h_priv_key = sp->opdata_rsa_priv_key; 6416+ if (h_priv_key == CK_INVALID_HANDLE) 6417+ { 6418+ h_priv_key = sp->opdata_rsa_priv_key = 6419+ pk11_get_private_rsa_key(rsa, &sp->opdata_rsa_priv, 6420+ &sp->opdata_rsa_d_num, &sp->opdata_rsa_pn_num, 6421+ &sp->opdata_rsa_pe_num, sp->session); 6422+ } 6423+ 6424+ if (h_priv_key != CK_INVALID_HANDLE) 6425+ { 6426+ rv = pFuncList->C_SignInit(sp->session, p_mech, 6427+ h_priv_key); 6428+ 6429+ if (rv != CKR_OK) 6430+ { 6431+ PK11err_add_data(PK11_F_RSA_PRIV_ENC_LOW, 6432+ PK11_R_SIGNINIT, rv); 6433+ pk11_return_session(sp, OP_RSA); 6434+ return (-1); 6435+ } 6436+ 6437+ rv = pFuncList->C_Sign(sp->session, 6438+ (unsigned char *)from, flen, to, &ul_sig_len); 6439+ 6440+ if (rv != CKR_OK) 6441+ { 6442+ PK11err_add_data(PK11_F_RSA_PRIV_ENC_LOW, PK11_R_SIGN, 6443+ rv); 6444+ pk11_return_session(sp, OP_RSA); 6445+ return (-1); 6446+ } 6447+ 6448+ retval = ul_sig_len; 6449+ } 6450+ 6451+ pk11_return_session(sp, OP_RSA); 6452+ return (retval); 6453+ } 6454+ 6455+ 6456+/* 6457+ * This function implements RSA private decryption using C_DecryptInit and 6458+ * C_Decrypt pk11 APIs. Note that CKM_RSA_X_509 mechanism is used here. 6459+ * The calling function allocated sufficient memory in "to" to store results. 6460+ */ 6461+static int pk11_RSA_private_decrypt_low(int flen, 6462+ const unsigned char *from, unsigned char *to, RSA *rsa) 6463+ { 6464+ CK_ULONG bytes_decrypted = flen; 6465+ int retval = -1; 6466+ CK_RV rv; 6467+ CK_MECHANISM mech_rsa = {CKM_RSA_X_509, NULL, 0}; 6468+ CK_MECHANISM *p_mech = &mech_rsa; 6469+ CK_OBJECT_HANDLE h_priv_key; 6470+ PK11_SESSION *sp; 6471+ 6472+ if ((sp = pk11_get_session(OP_RSA)) == NULL) 6473+ return (-1); 6474+ 6475+ (void) check_new_rsa_key_priv(sp, rsa); 6476+ 6477+ h_priv_key = sp->opdata_rsa_priv_key; 6478+ if (h_priv_key == CK_INVALID_HANDLE) 6479+ h_priv_key = sp->opdata_rsa_priv_key = 6480+ pk11_get_private_rsa_key(rsa, &sp->opdata_rsa_priv, 6481+ &sp->opdata_rsa_d_num, &sp->opdata_rsa_pn_num, 6482+ &sp->opdata_rsa_pe_num, sp->session); 6483+ 6484+ if (h_priv_key != CK_INVALID_HANDLE) 6485+ { 6486+ rv = pFuncList->C_DecryptInit(sp->session, p_mech, 6487+ h_priv_key); 6488+ 6489+ if (rv != CKR_OK) 6490+ { 6491+ PK11err_add_data(PK11_F_RSA_PRIV_DEC_LOW, 6492+ PK11_R_DECRYPTINIT, rv); 6493+ pk11_return_session(sp, OP_RSA); 6494+ return (-1); 6495+ } 6496+ 6497+ rv = pFuncList->C_Decrypt(sp->session, 6498+ (unsigned char *)from, flen, to, &bytes_decrypted); 6499+ 6500+ if (rv != CKR_OK) 6501+ { 6502+ PK11err_add_data(PK11_F_RSA_PRIV_DEC_LOW, 6503+ PK11_R_DECRYPT, rv); 6504+ pk11_return_session(sp, OP_RSA); 6505+ return (-1); 6506+ } 6507+ retval = bytes_decrypted; 6508+ } 6509+ 6510+ pk11_return_session(sp, OP_RSA); 6511+ return (retval); 6512+ } 6513+ 6514+ 6515+/* 6516+ * This function implements RSA public decryption using C_VerifyRecoverInit 6517+ * and C_VerifyRecover pk11 APIs. Note that CKM_RSA_X_509 is used here. 6518+ * The calling function allocated sufficient memory in "to" to store results. 6519+ */ 6520+static int pk11_RSA_public_decrypt_low(int flen, 6521+ const unsigned char *from, unsigned char *to, RSA *rsa) 6522+ { 6523+ CK_ULONG bytes_decrypted = flen; 6524+ int retval = -1; 6525+ CK_RV rv; 6526+ CK_MECHANISM mech_rsa = {CKM_RSA_X_509, NULL, 0}; 6527+ CK_MECHANISM *p_mech = &mech_rsa; 6528+ CK_OBJECT_HANDLE h_pub_key = CK_INVALID_HANDLE; 6529+ PK11_SESSION *sp; 6530+ 6531+ if ((sp = pk11_get_session(OP_RSA)) == NULL) 6532+ return (-1); 6533+ 6534+ (void) check_new_rsa_key_pub(sp, rsa); 6535+ 6536+ h_pub_key = sp->opdata_rsa_pub_key; 6537+ if (h_pub_key == CK_INVALID_HANDLE) 6538+ h_pub_key = sp->opdata_rsa_pub_key = 6539+ pk11_get_public_rsa_key(rsa, &sp->opdata_rsa_pub, 6540+ &sp->opdata_rsa_n_num, &sp->opdata_rsa_e_num, 6541+ sp->session); 6542+ 6543+ if (h_pub_key != CK_INVALID_HANDLE) 6544+ { 6545+ rv = pFuncList->C_VerifyRecoverInit(sp->session, 6546+ p_mech, h_pub_key); 6547+ 6548+ if (rv != CKR_OK) 6549+ { 6550+ PK11err_add_data(PK11_F_RSA_PUB_DEC_LOW, 6551+ PK11_R_VERIFYRECOVERINIT, rv); 6552+ pk11_return_session(sp, OP_RSA); 6553+ return (-1); 6554+ } 6555+ 6556+ rv = pFuncList->C_VerifyRecover(sp->session, 6557+ (unsigned char *)from, flen, to, &bytes_decrypted); 6558+ 6559+ if (rv != CKR_OK) 6560+ { 6561+ PK11err_add_data(PK11_F_RSA_PUB_DEC_LOW, 6562+ PK11_R_VERIFYRECOVER, rv); 6563+ pk11_return_session(sp, OP_RSA); 6564+ return (-1); 6565+ } 6566+ retval = bytes_decrypted; 6567+ } 6568+ 6569+ pk11_return_session(sp, OP_RSA); 6570+ return (retval); 6571+ } 6572+ 6573+static int pk11_RSA_init(RSA *rsa) 6574+ { 6575+ /* 6576+ * This flag in the RSA_METHOD enables the new rsa_sign, 6577+ * rsa_verify functions. See rsa.h for details. 6578+ */ 6579+ rsa->flags |= RSA_FLAG_SIGN_VER; 6580+ 6581+ return (1); 6582+ } 6583+ 6584+static int pk11_RSA_finish(RSA *rsa) 6585+ { 6586+ /* 6587+ * Since we are overloading OpenSSL's native RSA_eay_finish() we need 6588+ * to do the same as in the original function, i.e. to free bignum 6589+ * structures. 6590+ */ 6591+ if (rsa->_method_mod_n != NULL) 6592+ BN_MONT_CTX_free(rsa->_method_mod_n); 6593+ if (rsa->_method_mod_p != NULL) 6594+ BN_MONT_CTX_free(rsa->_method_mod_p); 6595+ if (rsa->_method_mod_q != NULL) 6596+ BN_MONT_CTX_free(rsa->_method_mod_q); 6597+ 6598+ return (1); 6599+ } 6600+ 6601+/* 6602+ * Standard engine interface function. Majority codes here are from 6603+ * rsa/rsa_sign.c. We replaced the decrypt function call by C_Sign of PKCS#11. 6604+ * See more details in rsa/rsa_sign.c 6605+ */ 6606+static int pk11_RSA_sign(int type, const unsigned char *m, unsigned int m_len, 6607+ unsigned char *sigret, unsigned int *siglen, const RSA *rsa) 6608+ { 6609+ X509_SIG sig; 6610+ ASN1_TYPE parameter; 6611+ int i, j = 0; 6612+ unsigned char *p, *s = NULL; 6613+ X509_ALGOR algor; 6614+ ASN1_OCTET_STRING digest; 6615+ CK_RV rv; 6616+ CK_MECHANISM mech_rsa = {CKM_RSA_PKCS, NULL, 0}; 6617+ CK_MECHANISM *p_mech = &mech_rsa; 6618+ CK_OBJECT_HANDLE h_priv_key; 6619+ PK11_SESSION *sp = NULL; 6620+ int ret = 0; 6621+ unsigned long ulsiglen; 6622+ 6623+ /* Encode the digest */ 6624+ /* Special case: SSL signature, just check the length */ 6625+ if (type == NID_md5_sha1) 6626+ { 6627+ if (m_len != SSL_SIG_LENGTH) 6628+ { 6629+ PK11err(PK11_F_RSA_SIGN, 6630+ PK11_R_INVALID_MESSAGE_LENGTH); 6631+ goto err; 6632+ } 6633+ i = SSL_SIG_LENGTH; 6634+ s = (unsigned char *)m; 6635+ } 6636+ else 6637+ { 6638+ sig.algor = &algor; 6639+ sig.algor->algorithm = OBJ_nid2obj(type); 6640+ if (sig.algor->algorithm == NULL) 6641+ { 6642+ PK11err(PK11_F_RSA_SIGN, 6643+ PK11_R_UNKNOWN_ALGORITHM_TYPE); 6644+ goto err; 6645+ } 6646+ if (sig.algor->algorithm->length == 0) 6647+ { 6648+ PK11err(PK11_F_RSA_SIGN, 6649+ PK11_R_UNKNOWN_ASN1_OBJECT_ID); 6650+ goto err; 6651+ } 6652+ parameter.type = V_ASN1_NULL; 6653+ parameter.value.ptr = NULL; 6654+ sig.algor->parameter = ¶meter; 6655+ 6656+ sig.digest = &digest; 6657+ sig.digest->data = (unsigned char *)m; 6658+ sig.digest->length = m_len; 6659+ 6660+ i = i2d_X509_SIG(&sig, NULL); 6661+ } 6662+ 6663+ j = RSA_size(rsa); 6664+ if ((i - RSA_PKCS1_PADDING) > j) 6665+ { 6666+ PK11err(PK11_F_RSA_SIGN, PK11_R_DIGEST_TOO_BIG); 6667+ goto err; 6668+ } 6669+ 6670+ if (type != NID_md5_sha1) 6671+ { 6672+ s = (unsigned char *)OPENSSL_malloc((unsigned int)(j + 1)); 6673+ if (s == NULL) 6674+ { 6675+ PK11err(PK11_F_RSA_SIGN, PK11_R_MALLOC_FAILURE); 6676+ goto err; 6677+ } 6678+ p = s; 6679+ (void) i2d_X509_SIG(&sig, &p); 6680+ } 6681+ 6682+ if ((sp = pk11_get_session(OP_RSA)) == NULL) 6683+ goto err; 6684+ 6685+ (void) check_new_rsa_key_priv(sp, rsa); 6686+ 6687+ h_priv_key = sp->opdata_rsa_priv_key; 6688+ if (h_priv_key == CK_INVALID_HANDLE) 6689+ h_priv_key = sp->opdata_rsa_priv_key = 6690+ pk11_get_private_rsa_key((RSA *)rsa, 6691+ &sp->opdata_rsa_priv, &sp->opdata_rsa_d_num, 6692+ &sp->opdata_rsa_pn_num, &sp->opdata_rsa_pe_num, 6693+ sp->session); 6694+ 6695+ if (h_priv_key != CK_INVALID_HANDLE) 6696+ { 6697+ rv = pFuncList->C_SignInit(sp->session, p_mech, h_priv_key); 6698+ 6699+ if (rv != CKR_OK) 6700+ { 6701+ PK11err_add_data(PK11_F_RSA_SIGN, PK11_R_SIGNINIT, rv); 6702+ goto err; 6703+ } 6704+ 6705+ ulsiglen = j; 6706+ rv = pFuncList->C_Sign(sp->session, s, i, sigret, 6707+ (CK_ULONG_PTR) &ulsiglen); 6708+ *siglen = ulsiglen; 6709+ 6710+ if (rv != CKR_OK) 6711+ { 6712+ PK11err_add_data(PK11_F_RSA_SIGN, PK11_R_SIGN, rv); 6713+ goto err; 6714+ } 6715+ ret = 1; 6716+ } 6717+ 6718+err: 6719+ if ((type != NID_md5_sha1) && (s != NULL)) 6720+ { 6721+ (void) memset(s, 0, (unsigned int)(j + 1)); 6722+ OPENSSL_free(s); 6723+ } 6724+ 6725+ pk11_return_session(sp, OP_RSA); 6726+ return (ret); 6727+ } 6728+ 6729+#if OPENSSL_VERSION_NUMBER < 0x10000000L 6730+static int pk11_RSA_verify(int type, const unsigned char *m, 6731+ unsigned int m_len, unsigned char *sigbuf, unsigned int siglen, 6732+ const RSA *rsa) 6733+#else 6734+static int pk11_RSA_verify(int type, const unsigned char *m, 6735+ unsigned int m_len, const unsigned char *sigbuf, unsigned int siglen, 6736+ const RSA *rsa) 6737+#endif 6738+ { 6739+ X509_SIG sig; 6740+ ASN1_TYPE parameter; 6741+ int i, j = 0; 6742+ unsigned char *p, *s = NULL; 6743+ X509_ALGOR algor; 6744+ ASN1_OCTET_STRING digest; 6745+ CK_RV rv; 6746+ CK_MECHANISM mech_rsa = {CKM_RSA_PKCS, NULL, 0}; 6747+ CK_MECHANISM *p_mech = &mech_rsa; 6748+ CK_OBJECT_HANDLE h_pub_key; 6749+ PK11_SESSION *sp = NULL; 6750+ int ret = 0; 6751+ 6752+ /* Encode the digest */ 6753+ /* Special case: SSL signature, just check the length */ 6754+ if (type == NID_md5_sha1) 6755+ { 6756+ if (m_len != SSL_SIG_LENGTH) 6757+ { 6758+ PK11err(PK11_F_RSA_VERIFY, 6759+ PK11_R_INVALID_MESSAGE_LENGTH); 6760+ goto err; 6761+ } 6762+ i = SSL_SIG_LENGTH; 6763+ s = (unsigned char *)m; 6764+ } 6765+ else 6766+ { 6767+ sig.algor = &algor; 6768+ sig.algor->algorithm = OBJ_nid2obj(type); 6769+ if (sig.algor->algorithm == NULL) 6770+ { 6771+ PK11err(PK11_F_RSA_VERIFY, 6772+ PK11_R_UNKNOWN_ALGORITHM_TYPE); 6773+ goto err; 6774+ } 6775+ if (sig.algor->algorithm->length == 0) 6776+ { 6777+ PK11err(PK11_F_RSA_VERIFY, 6778+ PK11_R_UNKNOWN_ASN1_OBJECT_ID); 6779+ goto err; 6780+ } 6781+ parameter.type = V_ASN1_NULL; 6782+ parameter.value.ptr = NULL; 6783+ sig.algor->parameter = ¶meter; 6784+ sig.digest = &digest; 6785+ sig.digest->data = (unsigned char *)m; 6786+ sig.digest->length = m_len; 6787+ i = i2d_X509_SIG(&sig, NULL); 6788+ } 6789+ 6790+ j = RSA_size(rsa); 6791+ if ((i - RSA_PKCS1_PADDING) > j) 6792+ { 6793+ PK11err(PK11_F_RSA_VERIFY, PK11_R_DIGEST_TOO_BIG); 6794+ goto err; 6795+ } 6796+ 6797+ if (type != NID_md5_sha1) 6798+ { 6799+ s = (unsigned char *)OPENSSL_malloc((unsigned int)(j + 1)); 6800+ if (s == NULL) 6801+ { 6802+ PK11err(PK11_F_RSA_VERIFY, PK11_R_MALLOC_FAILURE); 6803+ goto err; 6804+ } 6805+ p = s; 6806+ (void) i2d_X509_SIG(&sig, &p); 6807+ } 6808+ 6809+ if ((sp = pk11_get_session(OP_RSA)) == NULL) 6810+ goto err; 6811+ 6812+ (void) check_new_rsa_key_pub(sp, rsa); 6813+ 6814+ h_pub_key = sp->opdata_rsa_pub_key; 6815+ if (h_pub_key == CK_INVALID_HANDLE) 6816+ h_pub_key = sp->opdata_rsa_pub_key = 6817+ pk11_get_public_rsa_key((RSA *)rsa, &sp->opdata_rsa_pub, 6818+ &sp->opdata_rsa_n_num, &sp->opdata_rsa_e_num, 6819+ sp->session); 6820+ 6821+ if (h_pub_key != CK_INVALID_HANDLE) 6822+ { 6823+ rv = pFuncList->C_VerifyInit(sp->session, p_mech, 6824+ h_pub_key); 6825+ 6826+ if (rv != CKR_OK) 6827+ { 6828+ PK11err_add_data(PK11_F_RSA_VERIFY, PK11_R_VERIFYINIT, 6829+ rv); 6830+ goto err; 6831+ } 6832+ rv = pFuncList->C_Verify(sp->session, s, i, 6833+ (CK_BYTE_PTR)sigbuf, (CK_ULONG)siglen); 6834+ 6835+ if (rv != CKR_OK) 6836+ { 6837+ PK11err_add_data(PK11_F_RSA_VERIFY, PK11_R_VERIFY, rv); 6838+ goto err; 6839+ } 6840+ ret = 1; 6841+ } 6842+ 6843+err: 6844+ if ((type != NID_md5_sha1) && (s != NULL)) 6845+ { 6846+ (void) memset(s, 0, (unsigned int)(j + 1)); 6847+ OPENSSL_free(s); 6848+ } 6849+ 6850+ pk11_return_session(sp, OP_RSA); 6851+ return (ret); 6852+ } 6853+ 6854+static int hndidx_rsa = -1; 6855+ 6856+#define MAXATTR 1024 6857+ 6858+/* 6859+ * Load RSA private key from a file or get its PKCS#11 handle if stored in the 6860+ * PKCS#11 token. 6861+ */ 6862+/* ARGSUSED */ 6863+EVP_PKEY *pk11_load_privkey(ENGINE *e, const char *privkey_file, 6864+ UI_METHOD *ui_method, void *callback_data) 6865+ { 6866+ EVP_PKEY *pkey = NULL; 6867+ FILE *privkey; 6868+ CK_OBJECT_HANDLE h_priv_key = CK_INVALID_HANDLE; 6869+ RSA *rsa = NULL; 6870+ PK11_SESSION *sp; 6871+ /* Anything else below is needed for the key by reference extension. */ 6872+ CK_RV rv; 6873+ CK_BBOOL is_token = TRUE; 6874+ CK_BBOOL rollback = FALSE; 6875+ CK_BYTE attr_data[2][MAXATTR]; 6876+ CK_OBJECT_CLASS key_class = CKO_PRIVATE_KEY; 6877+ CK_OBJECT_HANDLE ks_key = CK_INVALID_HANDLE; /* key in keystore */ 6878+ 6879+ /* we look for private keys only */ 6880+ CK_ATTRIBUTE search_templ[] = 6881+ { 6882+ {CKA_TOKEN, &is_token, sizeof(is_token)}, 6883+ {CKA_CLASS, &key_class, sizeof(key_class)}, 6884+ {CKA_LABEL, NULL, 0} 6885+ }; 6886+ 6887+ /* 6888+ * These public attributes are needed to initialize the OpenSSL RSA 6889+ * structure with something we can use to look up the key. Note that we 6890+ * never ask for private components. 6891+ */ 6892+ CK_ATTRIBUTE get_templ[] = 6893+ { 6894+ {CKA_MODULUS, (void *)attr_data[0], MAXATTR}, /* n */ 6895+ {CKA_PUBLIC_EXPONENT, (void *)attr_data[1], MAXATTR}, /* e */ 6896+ }; 6897+ 6898+ if ((sp = pk11_get_session(OP_RSA)) == NULL) 6899+ return (NULL); 6900+ 6901+ /* 6902+ * Use simple scheme "pkcs11:<KEY_LABEL>" for now. 6903+ */ 6904+ if (strstr(privkey_file, "pkcs11:") == privkey_file) 6905+ { 6906+ search_templ[2].pValue = strstr(privkey_file, ":") + 1; 6907+ search_templ[2].ulValueLen = strlen(search_templ[2].pValue); 6908+ 6909+ if (pk11_token_login(sp->session, &pk11_login_done, 6910+ CK_TRUE) == 0) 6911+ goto err; 6912+ 6913+ /* 6914+ * Now let's try to find the key in the token. It is a failure 6915+ * if we can't find it. 6916+ */ 6917+ if (find_one_object(OP_RSA, sp->session, search_templ, 3, 6918+ &ks_key) == 0) 6919+ goto err; 6920+ 6921+ if (hndidx_rsa == -1) 6922+ hndidx_rsa = RSA_get_ex_new_index(0, 6923+ "pkcs11 RSA HSM key handle", 6924+ NULL, NULL, NULL); 6925+ 6926+ /* 6927+ * We might have a cache hit which we could confirm 6928+ * according to the 'n'/'e' params, RSA public pointer 6929+ * as NULL, and non-NULL RSA private pointer. However, 6930+ * it is easier just to recreate everything. We expect 6931+ * the keys to be loaded once and used many times. We 6932+ * do not check the return value because even in case 6933+ * of failure the sp structure will have both key 6934+ * pointer and object handle cleaned and 6935+ * pk11_destroy_object() reports the failure to the 6936+ * OpenSSL error message buffer. 6937+ */ 6938+ (void) pk11_destroy_rsa_object_priv(sp, TRUE); 6939+ 6940+ sp->opdata_rsa_priv_key = ks_key; 6941+ /* This object shall not be deleted on a cache miss. */ 6942+ sp->priv_persistent = CK_TRUE; 6943+ 6944+ /* 6945+ * Cache the RSA private structure pointer. We do not 6946+ * use it now for key-by-ref keys but let's do it for 6947+ * consistency reasons. 6948+ */ 6949+ if ((rsa = sp->opdata_rsa_priv = RSA_new_method(e)) == NULL) 6950+ goto err; 6951+ 6952+ /* 6953+ * Now we have to initialize an OpenSSL RSA structure, 6954+ * everything else is 0 or NULL. 6955+ */ 6956+ rsa->flags = RSA_FLAG_SIGN_VER | RSA_FLAG_EXT_PKEY; 6957+ RSA_set_ex_data(rsa, hndidx_rsa, (void *) ks_key); 6958+ 6959+ if ((rv = pFuncList->C_GetAttributeValue(sp->session, ks_key, 6960+ get_templ, 2)) != CKR_OK) 6961+ { 6962+ PK11err_add_data(PK11_F_LOAD_PRIVKEY, 6963+ PK11_R_GETATTRIBUTVALUE, rv); 6964+ goto err; 6965+ } 6966+ 6967+ /* 6968+ * We do not use pk11_get_private_rsa_key() here so we 6969+ * must take care of handle management ourselves. 6970+ */ 6971+ KEY_HANDLE_REFHOLD(ks_key, OP_RSA, FALSE, rollback, err); 6972+ 6973+ /* 6974+ * Those are the sensitive components we do not want to export 6975+ * from the token at all: rsa->(d|p|q|dmp1|dmq1|iqmp). 6976+ */ 6977+ attr_to_BN(&get_templ[0], attr_data[0], &rsa->n); 6978+ attr_to_BN(&get_templ[1], attr_data[1], &rsa->e); 6979+ /* 6980+ * Must have 'n'/'e' components in the session structure as 6981+ * well. They serve as a public look-up key for the private key 6982+ * in the keystore. 6983+ */ 6984+ attr_to_BN(&get_templ[0], attr_data[0], 6985+ &sp->opdata_rsa_pn_num); 6986+ attr_to_BN(&get_templ[1], attr_data[1], 6987+ &sp->opdata_rsa_pe_num); 6988+ 6989+ if ((pkey = EVP_PKEY_new()) == NULL) 6990+ goto err; 6991+ 6992+ if (EVP_PKEY_assign_RSA(pkey, rsa) == 0) 6993+ goto err; 6994+ } 6995+ else if ((privkey = fopen(privkey_file, read_mode_flags)) != NULL) 6996+ { 6997+ pkey = PEM_read_PrivateKey(privkey, NULL, NULL, NULL); 6998+ (void) fclose(privkey); 6999+ if (pkey != NULL) 7000+ { 7001+ rsa = EVP_PKEY_get1_RSA(pkey); 7002+ if (rsa != NULL) 7003+ { 7004+ /* 7005+ * This will always destroy the RSA 7006+ * object since we have a new RSA 7007+ * structure here. 7008+ */ 7009+ (void) check_new_rsa_key_priv(sp, rsa); 7010+ sp->priv_persistent = CK_FALSE; 7011+ 7012+ h_priv_key = sp->opdata_rsa_priv_key = 7013+ pk11_get_private_rsa_key(rsa, 7014+ &sp->opdata_rsa_priv, 7015+ &sp->opdata_rsa_d_num, 7016+ &sp->opdata_rsa_pn_num, 7017+ &sp->opdata_rsa_pe_num, sp->session); 7018+ if (h_priv_key == CK_INVALID_HANDLE) 7019+ goto err; 7020+ } 7021+ else 7022+ goto err; 7023+ } 7024+ } 7025+ 7026+ pk11_return_session(sp, OP_RSA); 7027+ return (pkey); 7028+err: 7029+ pk11_return_session(sp, OP_RSA); 7030+ if (rsa != NULL) 7031+ RSA_free(rsa); 7032+ if (pkey != NULL) 7033+ { 7034+ EVP_PKEY_free(pkey); 7035+ pkey = NULL; 7036+ } 7037+ rollback = rollback; 7038+ return (pkey); 7039+ } 7040+ 7041+/* 7042+ * Load RSA public key from a file or get its PKCS#11 handle if stored in the 7043+ * PKCS#11 token. 7044+ */ 7045+/* ARGSUSED */ 7046+EVP_PKEY *pk11_load_pubkey(ENGINE *e, const char *pubkey_file, 7047+ UI_METHOD *ui_method, void *callback_data) 7048+ { 7049+ EVP_PKEY *pkey = NULL; 7050+ FILE *pubkey; 7051+ CK_OBJECT_HANDLE h_pub_key = CK_INVALID_HANDLE; 7052+ RSA *rsa = NULL; 7053+ PK11_SESSION *sp; 7054+ /* Anything else below is needed for the key by reference extension. */ 7055+ CK_RV rv; 7056+ CK_BBOOL is_token = TRUE; 7057+ CK_BYTE attr_data[2][MAXATTR]; 7058+ CK_OBJECT_CLASS key_class = CKO_PUBLIC_KEY; 7059+ CK_OBJECT_HANDLE ks_key = CK_INVALID_HANDLE; /* key in keystore */ 7060+ 7061+ /* we look for public keys only */ 7062+ CK_ATTRIBUTE search_templ[] = 7063+ { 7064+ {CKA_TOKEN, &is_token, sizeof(is_token)}, 7065+ {CKA_CLASS, &key_class, sizeof(key_class)}, 7066+ {CKA_LABEL, NULL, 0} 7067+ }; 7068+ 7069+ /* 7070+ * These public attributes are needed to initialize OpenSSL RSA 7071+ * structure with something we can use to look up the key. 7072+ */ 7073+ CK_ATTRIBUTE get_templ[] = 7074+ { 7075+ {CKA_MODULUS, (void *)attr_data[0], MAXATTR}, /* n */ 7076+ {CKA_PUBLIC_EXPONENT, (void *)attr_data[1], MAXATTR}, /* e */ 7077+ }; 7078+ 7079+ if ((sp = pk11_get_session(OP_RSA)) == NULL) 7080+ return (NULL); 7081+ 7082+ /* 7083+ * Use simple scheme "pkcs11:<KEY_LABEL>" for now. 7084+ */ 7085+ if (strstr(pubkey_file, "pkcs11:") == pubkey_file) 7086+ { 7087+ search_templ[2].pValue = strstr(pubkey_file, ":") + 1; 7088+ search_templ[2].ulValueLen = strlen(search_templ[2].pValue); 7089+ 7090+ if (pk11_token_login(sp->session, &pk11_login_done, 7091+ CK_FALSE) == 0) 7092+ goto err; 7093+ 7094+ /* 7095+ * Now let's try to find the key in the token. It is a failure 7096+ * if we can't find it. 7097+ */ 7098+ if (find_one_object(OP_RSA, sp->session, search_templ, 3, 7099+ &ks_key) == 0) 7100+ goto err; 7101+ 7102+ /* 7103+ * We load a new public key so we will create a new RSA 7104+ * structure. No cache hit is possible. 7105+ */ 7106+ (void) pk11_destroy_rsa_object_pub(sp, TRUE); 7107+ 7108+ sp->opdata_rsa_pub_key = ks_key; 7109+ /* This object shall not be deleted on a cache miss. */ 7110+ sp->pub_persistent = CK_TRUE; 7111+ 7112+ /* 7113+ * Cache the RSA public structure pointer. 7114+ */ 7115+ if ((rsa = sp->opdata_rsa_pub = RSA_new_method(e)) == NULL) 7116+ goto err; 7117+ 7118+ /* 7119+ * Now we have to initialize an OpenSSL RSA structure, 7120+ * everything else is 0 or NULL. 7121+ */ 7122+ rsa->flags = RSA_FLAG_SIGN_VER; 7123+ 7124+ if ((rv = pFuncList->C_GetAttributeValue(sp->session, ks_key, 7125+ get_templ, 2)) != CKR_OK) 7126+ { 7127+ PK11err_add_data(PK11_F_LOAD_PUBKEY, 7128+ PK11_R_GETATTRIBUTVALUE, rv); 7129+ goto err; 7130+ } 7131+ 7132+ attr_to_BN(&get_templ[0], attr_data[0], &rsa->n); 7133+ attr_to_BN(&get_templ[1], attr_data[1], &rsa->e); 7134+ 7135+ if ((pkey = EVP_PKEY_new()) == NULL) 7136+ goto err; 7137+ 7138+ if (EVP_PKEY_assign_RSA(pkey, rsa) == 0) 7139+ goto err; 7140+ 7141+ /* 7142+ * Create a session object from it so that when calling 7143+ * pk11_get_public_rsa_key() the next time, we can find it. The 7144+ * reason why we do that is that we cannot tell from the RSA 7145+ * structure (OpenSSL RSA structure does not have any room for 7146+ * additional data used by the engine, for example) if it bears 7147+ * a public key stored in the keystore or not so it's better if 7148+ * we always have a session key. Note that this is different 7149+ * from what we do for the private keystore objects but in that 7150+ * case, we can tell from the RSA structure that the keystore 7151+ * object is in play - the 'd' component is NULL in that case. 7152+ */ 7153+ h_pub_key = sp->opdata_rsa_pub_key = 7154+ pk11_get_public_rsa_key(rsa, 7155+ &sp->opdata_rsa_pub, &sp->opdata_rsa_n_num, 7156+ &sp->opdata_rsa_e_num, sp->session); 7157+ if (h_pub_key == CK_INVALID_HANDLE) 7158+ goto err; 7159+ } 7160+ else if ((pubkey = fopen(pubkey_file, read_mode_flags)) != NULL) 7161+ { 7162+ pkey = PEM_read_PUBKEY(pubkey, NULL, NULL, NULL); 7163+ (void) fclose(pubkey); 7164+ if (pkey != NULL) 7165+ { 7166+ rsa = EVP_PKEY_get1_RSA(pkey); 7167+ if (rsa != NULL) 7168+ { 7169+ /* 7170+ * This will always destroy the RSA 7171+ * object since we have a new RSA 7172+ * structure here. 7173+ */ 7174+ (void) check_new_rsa_key_pub(sp, rsa); 7175+ sp->pub_persistent = CK_FALSE; 7176+ 7177+ h_pub_key = sp->opdata_rsa_pub_key = 7178+ pk11_get_public_rsa_key(rsa, 7179+ &sp->opdata_rsa_pub, &sp->opdata_rsa_n_num, 7180+ &sp->opdata_rsa_e_num, sp->session); 7181+ if (h_pub_key == CK_INVALID_HANDLE) 7182+ goto err; 7183+ } 7184+ else 7185+ goto err; 7186+ } 7187+ } 7188+ 7189+ pk11_return_session(sp, OP_RSA); 7190+ return (pkey); 7191+err: 7192+ pk11_return_session(sp, OP_RSA); 7193+ if (rsa != NULL) 7194+ RSA_free(rsa); 7195+ if (pkey != NULL) 7196+ { 7197+ EVP_PKEY_free(pkey); 7198+ pkey = NULL; 7199+ } 7200+ return (pkey); 7201+ } 7202+ 7203+/* 7204+ * Create a public key object in a session from a given rsa structure. 7205+ * The *rsa_n_num and *rsa_e_num pointers are non-NULL for RSA public keys. 7206+ */ 7207+static CK_OBJECT_HANDLE pk11_get_public_rsa_key(RSA *rsa, 7208+ RSA **key_ptr, BIGNUM **rsa_n_num, BIGNUM **rsa_e_num, 7209+ CK_SESSION_HANDLE session) 7210+ { 7211+ CK_RV rv; 7212+ CK_OBJECT_HANDLE h_key = CK_INVALID_HANDLE; 7213+ CK_ULONG found; 7214+ CK_OBJECT_CLASS o_key = CKO_PUBLIC_KEY; 7215+ CK_KEY_TYPE k_type = CKK_RSA; 7216+ CK_ULONG ul_key_attr_count = 8; 7217+ CK_BBOOL rollback = FALSE; 7218+ 7219+ CK_ATTRIBUTE a_key_template[] = 7220+ { 7221+ {CKA_CLASS, (void *) NULL, sizeof (CK_OBJECT_CLASS)}, 7222+ {CKA_KEY_TYPE, (void *) NULL, sizeof (CK_KEY_TYPE)}, 7223+ {CKA_TOKEN, &false, sizeof (true)}, 7224+ {CKA_ENCRYPT, &true, sizeof (true)}, 7225+ {CKA_VERIFY, &true, sizeof (true)}, 7226+ {CKA_VERIFY_RECOVER, &true, sizeof (true)}, 7227+ {CKA_MODULUS, (void *)NULL, 0}, 7228+ {CKA_PUBLIC_EXPONENT, (void *)NULL, 0} 7229+ }; 7230+ 7231+ int i; 7232+ 7233+ a_key_template[0].pValue = &o_key; 7234+ a_key_template[1].pValue = &k_type; 7235+ 7236+ a_key_template[6].ulValueLen = BN_num_bytes(rsa->n); 7237+ a_key_template[6].pValue = (CK_VOID_PTR)OPENSSL_malloc( 7238+ (size_t)a_key_template[6].ulValueLen); 7239+ if (a_key_template[6].pValue == NULL) 7240+ { 7241+ PK11err(PK11_F_GET_PUB_RSA_KEY, PK11_R_MALLOC_FAILURE); 7242+ goto malloc_err; 7243+ } 7244+ 7245+ BN_bn2bin(rsa->n, a_key_template[6].pValue); 7246+ 7247+ a_key_template[7].ulValueLen = BN_num_bytes(rsa->e); 7248+ a_key_template[7].pValue = (CK_VOID_PTR)OPENSSL_malloc( 7249+ (size_t)a_key_template[7].ulValueLen); 7250+ if (a_key_template[7].pValue == NULL) 7251+ { 7252+ PK11err(PK11_F_GET_PUB_RSA_KEY, PK11_R_MALLOC_FAILURE); 7253+ goto malloc_err; 7254+ } 7255+ 7256+ BN_bn2bin(rsa->e, a_key_template[7].pValue); 7257+ 7258+ /* see find_lock array definition for more info on object locking */ 7259+ LOCK_OBJSTORE(OP_RSA); 7260+ 7261+ rv = pFuncList->C_FindObjectsInit(session, a_key_template, 7262+ ul_key_attr_count); 7263+ 7264+ if (rv != CKR_OK) 7265+ { 7266+ PK11err_add_data(PK11_F_GET_PUB_RSA_KEY, 7267+ PK11_R_FINDOBJECTSINIT, rv); 7268+ goto err; 7269+ } 7270+ 7271+ rv = pFuncList->C_FindObjects(session, &h_key, 1, &found); 7272+ 7273+ if (rv != CKR_OK) 7274+ { 7275+ PK11err_add_data(PK11_F_GET_PUB_RSA_KEY, 7276+ PK11_R_FINDOBJECTS, rv); 7277+ goto err; 7278+ } 7279+ 7280+ rv = pFuncList->C_FindObjectsFinal(session); 7281+ 7282+ if (rv != CKR_OK) 7283+ { 7284+ PK11err_add_data(PK11_F_GET_PUB_RSA_KEY, 7285+ PK11_R_FINDOBJECTSFINAL, rv); 7286+ goto err; 7287+ } 7288+ 7289+ if (found == 0) 7290+ { 7291+ rv = pFuncList->C_CreateObject(session, 7292+ a_key_template, ul_key_attr_count, &h_key); 7293+ if (rv != CKR_OK) 7294+ { 7295+ PK11err_add_data(PK11_F_GET_PUB_RSA_KEY, 7296+ PK11_R_CREATEOBJECT, rv); 7297+ goto err; 7298+ } 7299+ } 7300+ 7301+ if (rsa_n_num != NULL) 7302+ if ((*rsa_n_num = BN_dup(rsa->n)) == NULL) 7303+ { 7304+ PK11err(PK11_F_GET_PUB_RSA_KEY, PK11_R_MALLOC_FAILURE); 7305+ rollback = TRUE; 7306+ goto err; 7307+ } 7308+ if (rsa_e_num != NULL) 7309+ if ((*rsa_e_num = BN_dup(rsa->e)) == NULL) 7310+ { 7311+ PK11err(PK11_F_GET_PUB_RSA_KEY, PK11_R_MALLOC_FAILURE); 7312+ BN_free(*rsa_n_num); 7313+ *rsa_n_num = NULL; 7314+ rollback = TRUE; 7315+ goto err; 7316+ } 7317+ 7318+ /* LINTED: E_CONSTANT_CONDITION */ 7319+ KEY_HANDLE_REFHOLD(h_key, OP_RSA, FALSE, rollback, err); 7320+ if (key_ptr != NULL) 7321+ *key_ptr = rsa; 7322+ 7323+err: 7324+ if (rollback) 7325+ { 7326+ /* 7327+ * We do not care about the return value from C_DestroyObject() 7328+ * since we are doing rollback. 7329+ */ 7330+ if (found == 0) 7331+ (void) pFuncList->C_DestroyObject(session, h_key); 7332+ h_key = CK_INVALID_HANDLE; 7333+ } 7334+ 7335+ UNLOCK_OBJSTORE(OP_RSA); 7336+ 7337+malloc_err: 7338+ for (i = 6; i <= 7; i++) 7339+ { 7340+ if (a_key_template[i].pValue != NULL) 7341+ { 7342+ OPENSSL_free(a_key_template[i].pValue); 7343+ a_key_template[i].pValue = NULL; 7344+ } 7345+ } 7346+ 7347+ return (h_key); 7348+ } 7349+ 7350+/* 7351+ * Create a private key object in the session from a given rsa structure. 7352+ * The *rsa_d_num pointer is non-NULL for RSA private keys. 7353+ */ 7354+static CK_OBJECT_HANDLE 7355+pk11_get_private_rsa_key(RSA *rsa, RSA **key_ptr, BIGNUM **rsa_d_num, 7356+ BIGNUM **rsa_n_num, BIGNUM **rsa_e_num, CK_SESSION_HANDLE session) 7357+ { 7358+ CK_RV rv; 7359+ CK_OBJECT_HANDLE h_key = CK_INVALID_HANDLE; 7360+ int i; 7361+ CK_ULONG found; 7362+ CK_OBJECT_CLASS o_key = CKO_PRIVATE_KEY; 7363+ CK_KEY_TYPE k_type = CKK_RSA; 7364+ CK_ULONG ul_key_attr_count = 14; 7365+ CK_BBOOL rollback = FALSE; 7366+ 7367+ /* Both CKA_TOKEN and CKA_SENSITIVE have to be FALSE for session keys */ 7368+ CK_ATTRIBUTE a_key_template[] = 7369+ { 7370+ {CKA_CLASS, (void *) NULL, sizeof (CK_OBJECT_CLASS)}, 7371+ {CKA_KEY_TYPE, (void *) NULL, sizeof (CK_KEY_TYPE)}, 7372+ {CKA_TOKEN, &false, sizeof (true)}, 7373+ {CKA_SENSITIVE, &false, sizeof (true)}, 7374+ {CKA_DECRYPT, &true, sizeof (true)}, 7375+ {CKA_SIGN, &true, sizeof (true)}, 7376+ {CKA_MODULUS, (void *)NULL, 0}, 7377+ {CKA_PUBLIC_EXPONENT, (void *)NULL, 0}, 7378+ {CKA_PRIVATE_EXPONENT, (void *)NULL, 0}, 7379+ {CKA_PRIME_1, (void *)NULL, 0}, 7380+ {CKA_PRIME_2, (void *)NULL, 0}, 7381+ {CKA_EXPONENT_1, (void *)NULL, 0}, 7382+ {CKA_EXPONENT_2, (void *)NULL, 0}, 7383+ {CKA_COEFFICIENT, (void *)NULL, 0}, 7384+ }; 7385+ 7386+ if ((rsa->flags & RSA_FLAG_EXT_PKEY) != 0) { 7387+ h_key = (CK_OBJECT_HANDLE)RSA_get_ex_data(rsa, hndidx_rsa); 7388+ LOCK_OBJSTORE(OP_RSA); 7389+ goto set; 7390+ } 7391+ 7392+ a_key_template[0].pValue = &o_key; 7393+ a_key_template[1].pValue = &k_type; 7394+ 7395+ /* Put the private key components into the template */ 7396+ if (init_template_value(rsa->n, &a_key_template[6].pValue, 7397+ &a_key_template[6].ulValueLen) == 0 || 7398+ init_template_value(rsa->e, &a_key_template[7].pValue, 7399+ &a_key_template[7].ulValueLen) == 0 || 7400+ init_template_value(rsa->d, &a_key_template[8].pValue, 7401+ &a_key_template[8].ulValueLen) == 0 || 7402+ init_template_value(rsa->p, &a_key_template[9].pValue, 7403+ &a_key_template[9].ulValueLen) == 0 || 7404+ init_template_value(rsa->q, &a_key_template[10].pValue, 7405+ &a_key_template[10].ulValueLen) == 0 || 7406+ init_template_value(rsa->dmp1, &a_key_template[11].pValue, 7407+ &a_key_template[11].ulValueLen) == 0 || 7408+ init_template_value(rsa->dmq1, &a_key_template[12].pValue, 7409+ &a_key_template[12].ulValueLen) == 0 || 7410+ init_template_value(rsa->iqmp, &a_key_template[13].pValue, 7411+ &a_key_template[13].ulValueLen) == 0) 7412+ { 7413+ PK11err(PK11_F_GET_PRIV_RSA_KEY, PK11_R_MALLOC_FAILURE); 7414+ goto malloc_err; 7415+ } 7416+ 7417+ /* see find_lock array definition for more info on object locking */ 7418+ LOCK_OBJSTORE(OP_RSA); 7419+ 7420+ /* 7421+ * We are getting the private key but the private 'd' 7422+ * component is NULL. That means this is key by reference RSA 7423+ * key. In that case, we can use only public components for 7424+ * searching for the private key handle. 7425+ */ 7426+ if (rsa->d == NULL) 7427+ { 7428+ ul_key_attr_count = 8; 7429+ /* 7430+ * We will perform the search in the token, not in the existing 7431+ * session keys. 7432+ */ 7433+ a_key_template[2].pValue = &true; 7434+ } 7435+ 7436+ rv = pFuncList->C_FindObjectsInit(session, a_key_template, 7437+ ul_key_attr_count); 7438+ 7439+ if (rv != CKR_OK) 7440+ { 7441+ PK11err_add_data(PK11_F_GET_PRIV_RSA_KEY, 7442+ PK11_R_FINDOBJECTSINIT, rv); 7443+ goto err; 7444+ } 7445+ 7446+ rv = pFuncList->C_FindObjects(session, &h_key, 1, &found); 7447+ 7448+ if (rv != CKR_OK) 7449+ { 7450+ PK11err_add_data(PK11_F_GET_PRIV_RSA_KEY, 7451+ PK11_R_FINDOBJECTS, rv); 7452+ goto err; 7453+ } 7454+ 7455+ rv = pFuncList->C_FindObjectsFinal(session); 7456+ 7457+ if (rv != CKR_OK) 7458+ { 7459+ PK11err_add_data(PK11_F_GET_PRIV_RSA_KEY, 7460+ PK11_R_FINDOBJECTSFINAL, rv); 7461+ goto err; 7462+ } 7463+ 7464+ if (found == 0) 7465+ { 7466+ /* 7467+ * We have an RSA structure with 'n'/'e' components 7468+ * only so we tried to find the private key in the 7469+ * keystore. If it was really a token key we have a 7470+ * problem. Note that for other key types we just 7471+ * create a new session key using the private 7472+ * components from the RSA structure. 7473+ */ 7474+ if (rsa->d == NULL) 7475+ { 7476+ PK11err(PK11_F_GET_PRIV_RSA_KEY, 7477+ PK11_R_PRIV_KEY_NOT_FOUND); 7478+ goto err; 7479+ } 7480+ 7481+ rv = pFuncList->C_CreateObject(session, 7482+ a_key_template, ul_key_attr_count, &h_key); 7483+ if (rv != CKR_OK) 7484+ { 7485+ PK11err_add_data(PK11_F_GET_PRIV_RSA_KEY, 7486+ PK11_R_CREATEOBJECT, rv); 7487+ goto err; 7488+ } 7489+ } 7490+ 7491+set: 7492+ if (rsa_d_num != NULL) 7493+ { 7494+ /* 7495+ * When RSA keys by reference code is used, we never 7496+ * extract private components from the keystore. In 7497+ * that case 'd' was set to NULL and we expect the 7498+ * application to properly cope with that. It is 7499+ * documented in openssl(5). In general, if keys by 7500+ * reference are used we expect it to be used 7501+ * exclusively using the high level API and then there 7502+ * is no problem. If the application expects the 7503+ * private components to be read from the keystore 7504+ * then that is not a supported way of usage. 7505+ */ 7506+ if (rsa->d != NULL && (*rsa_d_num = BN_dup(rsa->d)) == NULL) 7507+ { 7508+ PK11err(PK11_F_GET_PRIV_RSA_KEY, PK11_R_MALLOC_FAILURE); 7509+ rollback = TRUE; 7510+ goto err; 7511+ } 7512+ else 7513+ *rsa_d_num = NULL; 7514+ } 7515+ 7516+ /* 7517+ * For the key by reference code, we need public components as well 7518+ * since 'd' component is always NULL. For that reason, we always cache 7519+ * 'n'/'e' components as well. 7520+ */ 7521+ *rsa_n_num = BN_dup(rsa->n); 7522+ *rsa_e_num = BN_dup(rsa->e); 7523+ 7524+ /* LINTED: E_CONSTANT_CONDITION */ 7525+ KEY_HANDLE_REFHOLD(h_key, OP_RSA, FALSE, rollback, err); 7526+ if (key_ptr != NULL) 7527+ *key_ptr = rsa; 7528+ 7529+err: 7530+ if (rollback) 7531+ { 7532+ /* 7533+ * We do not care about the return value from C_DestroyObject() 7534+ * since we are doing rollback. 7535+ */ 7536+ if (found == 0 && 7537+ (rsa->flags & RSA_FLAG_EXT_PKEY) == 0) 7538+ (void) pFuncList->C_DestroyObject(session, h_key); 7539+ h_key = CK_INVALID_HANDLE; 7540+ } 7541+ 7542+ UNLOCK_OBJSTORE(OP_RSA); 7543+ 7544+malloc_err: 7545+ /* 7546+ * 6 to 13 entries in the key template are key components. 7547+ * They need to be freed upon exit or error. 7548+ */ 7549+ for (i = 6; i <= 13; i++) 7550+ { 7551+ if (a_key_template[i].pValue != NULL) 7552+ { 7553+ (void) memset(a_key_template[i].pValue, 0, 7554+ a_key_template[i].ulValueLen); 7555+ OPENSSL_free(a_key_template[i].pValue); 7556+ a_key_template[i].pValue = NULL; 7557+ } 7558+ } 7559+ 7560+ return (h_key); 7561+ } 7562+ 7563+/* 7564+ * Check for cache miss and clean the object pointer and handle 7565+ * in such case. Return 1 for cache hit, 0 for cache miss. 7566+ */ 7567+static int check_new_rsa_key_pub(PK11_SESSION *sp, const RSA *rsa) 7568+ { 7569+ /* 7570+ * Provide protection against RSA structure reuse by making the 7571+ * check for cache hit stronger. Only public components of RSA 7572+ * key matter here so it is sufficient to compare them with values 7573+ * cached in PK11_SESSION structure. 7574+ * 7575+ * We must check the handle as well since with key by reference, public 7576+ * components 'n'/'e' are cached in private keys as well. That means we 7577+ * could have a cache hit in a private key when looking for a public 7578+ * key. That would not work, you cannot have one PKCS#11 object for 7579+ * both data signing and verifying. 7580+ */ 7581+ if ((sp->opdata_rsa_pub != rsa) || 7582+ (BN_cmp(sp->opdata_rsa_n_num, rsa->n) != 0) || 7583+ (BN_cmp(sp->opdata_rsa_e_num, rsa->e) != 0) || 7584+ (sp->opdata_rsa_priv_key != CK_INVALID_HANDLE)) 7585+ { 7586+ /* 7587+ * We do not check the return value because even in case of 7588+ * failure the sp structure will have both key pointer 7589+ * and object handle cleaned and pk11_destroy_object() 7590+ * reports the failure to the OpenSSL error message buffer. 7591+ */ 7592+ (void) pk11_destroy_rsa_object_pub(sp, TRUE); 7593+ return (0); 7594+ } 7595+ return (1); 7596+ } 7597+ 7598+/* 7599+ * Check for cache miss and clean the object pointer and handle 7600+ * in such case. Return 1 for cache hit, 0 for cache miss. 7601+ */ 7602+static int check_new_rsa_key_priv(PK11_SESSION *sp, const RSA *rsa) 7603+ { 7604+ /* 7605+ * Provide protection against RSA structure reuse by making 7606+ * the check for cache hit stronger. Comparing public exponent 7607+ * of RSA key with value cached in PK11_SESSION structure 7608+ * should be sufficient. Note that we want to compare the 7609+ * public component since with the keys by reference 7610+ * mechanism, private components are not in the RSA 7611+ * structure. Also, see check_new_rsa_key_pub() about why we 7612+ * compare the handle as well. 7613+ */ 7614+ if ((sp->opdata_rsa_priv != rsa) || 7615+ (BN_cmp(sp->opdata_rsa_pn_num, rsa->n) != 0) || 7616+ (BN_cmp(sp->opdata_rsa_pe_num, rsa->e) != 0) || 7617+ (sp->opdata_rsa_pn_num == NULL) || 7618+ (sp->opdata_rsa_pe_num == NULL) || 7619+ (sp->opdata_rsa_pub_key != CK_INVALID_HANDLE)) 7620+ { 7621+ /* 7622+ * We do not check the return value because even in case of 7623+ * failure the sp structure will have both key pointer 7624+ * and object handle cleaned and pk11_destroy_object() 7625+ * reports the failure to the OpenSSL error message buffer. 7626+ */ 7627+ (void) pk11_destroy_rsa_object_priv(sp, TRUE); 7628+ return (0); 7629+ } 7630+ return (1); 7631+ } 7632+#endif 7633+ 7634+#ifndef OPENSSL_NO_DSA 7635+/* The DSA function implementation */ 7636+/* ARGSUSED */ 7637+static int pk11_DSA_init(DSA *dsa) 7638+ { 7639+ return (1); 7640+ } 7641+ 7642+/* ARGSUSED */ 7643+static int pk11_DSA_finish(DSA *dsa) 7644+ { 7645+ return (1); 7646+ } 7647+ 7648+ 7649+static DSA_SIG * 7650+pk11_dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa) 7651+ { 7652+ BIGNUM *r = NULL, *s = NULL; 7653+ int i; 7654+ DSA_SIG *dsa_sig = NULL; 7655+ 7656+ CK_RV rv; 7657+ CK_MECHANISM Mechanism_dsa = {CKM_DSA, NULL, 0}; 7658+ CK_MECHANISM *p_mech = &Mechanism_dsa; 7659+ CK_OBJECT_HANDLE h_priv_key; 7660+ 7661+ /* 7662+ * The signature is the concatenation of r and s, 7663+ * each is 20 bytes long 7664+ */ 7665+ unsigned char sigret[DSA_SIGNATURE_LEN]; 7666+ unsigned long siglen = DSA_SIGNATURE_LEN; 7667+ unsigned int siglen2 = DSA_SIGNATURE_LEN / 2; 7668+ 7669+ PK11_SESSION *sp = NULL; 7670+ 7671+ if ((dsa->p == NULL) || (dsa->q == NULL) || (dsa->g == NULL)) 7672+ { 7673+ PK11err(PK11_F_DSA_SIGN, PK11_R_MISSING_KEY_COMPONENT); 7674+ goto ret; 7675+ } 7676+ 7677+ i = BN_num_bytes(dsa->q); /* should be 20 */ 7678+ if (dlen > i) 7679+ { 7680+ PK11err(PK11_F_DSA_SIGN, PK11_R_INVALID_SIGNATURE_LENGTH); 7681+ goto ret; 7682+ } 7683+ 7684+ if ((sp = pk11_get_session(OP_DSA)) == NULL) 7685+ goto ret; 7686+ 7687+ (void) check_new_dsa_key_priv(sp, dsa); 7688+ 7689+ h_priv_key = sp->opdata_dsa_priv_key; 7690+ if (h_priv_key == CK_INVALID_HANDLE) 7691+ h_priv_key = sp->opdata_dsa_priv_key = 7692+ pk11_get_private_dsa_key((DSA *)dsa, 7693+ &sp->opdata_dsa_priv, 7694+ &sp->opdata_dsa_priv_num, sp->session); 7695+ 7696+ if (h_priv_key != CK_INVALID_HANDLE) 7697+ { 7698+ rv = pFuncList->C_SignInit(sp->session, p_mech, h_priv_key); 7699+ 7700+ if (rv != CKR_OK) 7701+ { 7702+ PK11err_add_data(PK11_F_DSA_SIGN, PK11_R_SIGNINIT, rv); 7703+ goto ret; 7704+ } 7705+ 7706+ (void) memset(sigret, 0, siglen); 7707+ rv = pFuncList->C_Sign(sp->session, 7708+ (unsigned char*) dgst, dlen, sigret, 7709+ (CK_ULONG_PTR) &siglen); 7710+ 7711+ if (rv != CKR_OK) 7712+ { 7713+ PK11err_add_data(PK11_F_DSA_SIGN, PK11_R_SIGN, rv); 7714+ goto ret; 7715+ } 7716+ } 7717+ 7718+ 7719+ if ((s = BN_new()) == NULL) 7720+ { 7721+ PK11err(PK11_F_DSA_SIGN, PK11_R_MALLOC_FAILURE); 7722+ goto ret; 7723+ } 7724+ 7725+ if ((r = BN_new()) == NULL) 7726+ { 7727+ PK11err(PK11_F_DSA_SIGN, PK11_R_MALLOC_FAILURE); 7728+ goto ret; 7729+ } 7730+ 7731+ if ((dsa_sig = DSA_SIG_new()) == NULL) 7732+ { 7733+ PK11err(PK11_F_DSA_SIGN, PK11_R_MALLOC_FAILURE); 7734+ goto ret; 7735+ } 7736+ 7737+ if (BN_bin2bn(sigret, siglen2, r) == NULL || 7738+ BN_bin2bn(&sigret[siglen2], siglen2, s) == NULL) 7739+ { 7740+ PK11err(PK11_F_DSA_SIGN, PK11_R_MALLOC_FAILURE); 7741+ goto ret; 7742+ } 7743+ 7744+ dsa_sig->r = r; 7745+ dsa_sig->s = s; 7746+ 7747+ret: 7748+ if (dsa_sig == NULL) 7749+ { 7750+ if (r != NULL) 7751+ BN_free(r); 7752+ if (s != NULL) 7753+ BN_free(s); 7754+ } 7755+ 7756+ pk11_return_session(sp, OP_DSA); 7757+ return (dsa_sig); 7758+ } 7759+ 7760+static int 7761+pk11_dsa_do_verify(const unsigned char *dgst, int dlen, DSA_SIG *sig, 7762+ DSA *dsa) 7763+ { 7764+ int i; 7765+ CK_RV rv; 7766+ int retval = 0; 7767+ CK_MECHANISM Mechanism_dsa = {CKM_DSA, NULL, 0}; 7768+ CK_MECHANISM *p_mech = &Mechanism_dsa; 7769+ CK_OBJECT_HANDLE h_pub_key; 7770+ 7771+ unsigned char sigbuf[DSA_SIGNATURE_LEN]; 7772+ unsigned long siglen = DSA_SIGNATURE_LEN; 7773+ unsigned long siglen2 = DSA_SIGNATURE_LEN/2; 7774+ 7775+ PK11_SESSION *sp = NULL; 7776+ 7777+ if (BN_is_zero(sig->r) || sig->r->neg || BN_ucmp(sig->r, dsa->q) >= 0) 7778+ { 7779+ PK11err(PK11_F_DSA_VERIFY, 7780+ PK11_R_INVALID_DSA_SIGNATURE_R); 7781+ goto ret; 7782+ } 7783+ 7784+ if (BN_is_zero(sig->s) || sig->s->neg || BN_ucmp(sig->s, dsa->q) >= 0) 7785+ { 7786+ PK11err(PK11_F_DSA_VERIFY, 7787+ PK11_R_INVALID_DSA_SIGNATURE_S); 7788+ goto ret; 7789+ } 7790+ 7791+ i = BN_num_bytes(dsa->q); /* should be 20 */ 7792+ 7793+ if (dlen > i) 7794+ { 7795+ PK11err(PK11_F_DSA_VERIFY, 7796+ PK11_R_INVALID_SIGNATURE_LENGTH); 7797+ goto ret; 7798+ } 7799+ 7800+ if ((sp = pk11_get_session(OP_DSA)) == NULL) 7801+ goto ret; 7802+ 7803+ (void) check_new_dsa_key_pub(sp, dsa); 7804+ 7805+ h_pub_key = sp->opdata_dsa_pub_key; 7806+ if (h_pub_key == CK_INVALID_HANDLE) 7807+ h_pub_key = sp->opdata_dsa_pub_key = 7808+ pk11_get_public_dsa_key((DSA *)dsa, &sp->opdata_dsa_pub, 7809+ &sp->opdata_dsa_pub_num, sp->session); 7810+ 7811+ if (h_pub_key != CK_INVALID_HANDLE) 7812+ { 7813+ rv = pFuncList->C_VerifyInit(sp->session, p_mech, 7814+ h_pub_key); 7815+ 7816+ if (rv != CKR_OK) 7817+ { 7818+ PK11err_add_data(PK11_F_DSA_VERIFY, PK11_R_VERIFYINIT, 7819+ rv); 7820+ goto ret; 7821+ } 7822+ 7823+ /* 7824+ * The representation of each of the two big numbers could 7825+ * be shorter than DSA_SIGNATURE_LEN/2 bytes so we need 7826+ * to act accordingly and shift if necessary. 7827+ */ 7828+ (void) memset(sigbuf, 0, siglen); 7829+ BN_bn2bin(sig->r, sigbuf + siglen2 - BN_num_bytes(sig->r)); 7830+ BN_bn2bin(sig->s, &sigbuf[siglen2] + siglen2 - 7831+ BN_num_bytes(sig->s)); 7832+ 7833+ rv = pFuncList->C_Verify(sp->session, 7834+ (unsigned char *) dgst, dlen, sigbuf, (CK_ULONG)siglen); 7835+ 7836+ if (rv != CKR_OK) 7837+ { 7838+ PK11err_add_data(PK11_F_DSA_VERIFY, PK11_R_VERIFY, rv); 7839+ goto ret; 7840+ } 7841+ } 7842+ 7843+ retval = 1; 7844+ret: 7845+ 7846+ pk11_return_session(sp, OP_DSA); 7847+ return (retval); 7848+ } 7849+ 7850+ 7851+/* 7852+ * Create a public key object in a session from a given dsa structure. 7853+ * The *dsa_pub_num pointer is non-NULL for DSA public keys. 7854+ */ 7855+static CK_OBJECT_HANDLE pk11_get_public_dsa_key(DSA* dsa, 7856+ DSA **key_ptr, BIGNUM **dsa_pub_num, CK_SESSION_HANDLE session) 7857+ { 7858+ CK_RV rv; 7859+ CK_OBJECT_CLASS o_key = CKO_PUBLIC_KEY; 7860+ CK_OBJECT_HANDLE h_key = CK_INVALID_HANDLE; 7861+ CK_ULONG found; 7862+ CK_KEY_TYPE k_type = CKK_DSA; 7863+ CK_ULONG ul_key_attr_count = 8; 7864+ CK_BBOOL rollback = FALSE; 7865+ int i; 7866+ 7867+ CK_ATTRIBUTE a_key_template[] = 7868+ { 7869+ {CKA_CLASS, (void *) NULL, sizeof (CK_OBJECT_CLASS)}, 7870+ {CKA_KEY_TYPE, (void *) NULL, sizeof (CK_KEY_TYPE)}, 7871+ {CKA_TOKEN, &false, sizeof (true)}, 7872+ {CKA_VERIFY, &true, sizeof (true)}, 7873+ {CKA_PRIME, (void *)NULL, 0}, /* p */ 7874+ {CKA_SUBPRIME, (void *)NULL, 0}, /* q */ 7875+ {CKA_BASE, (void *)NULL, 0}, /* g */ 7876+ {CKA_VALUE, (void *)NULL, 0} /* pub_key - y */ 7877+ }; 7878+ 7879+ a_key_template[0].pValue = &o_key; 7880+ a_key_template[1].pValue = &k_type; 7881+ 7882+ if (init_template_value(dsa->p, &a_key_template[4].pValue, 7883+ &a_key_template[4].ulValueLen) == 0 || 7884+ init_template_value(dsa->q, &a_key_template[5].pValue, 7885+ &a_key_template[5].ulValueLen) == 0 || 7886+ init_template_value(dsa->g, &a_key_template[6].pValue, 7887+ &a_key_template[6].ulValueLen) == 0 || 7888+ init_template_value(dsa->pub_key, &a_key_template[7].pValue, 7889+ &a_key_template[7].ulValueLen) == 0) 7890+ { 7891+ PK11err(PK11_F_GET_PUB_DSA_KEY, PK11_R_MALLOC_FAILURE); 7892+ goto malloc_err; 7893+ } 7894+ 7895+ /* see find_lock array definition for more info on object locking */ 7896+ LOCK_OBJSTORE(OP_DSA); 7897+ rv = pFuncList->C_FindObjectsInit(session, a_key_template, 7898+ ul_key_attr_count); 7899+ 7900+ if (rv != CKR_OK) 7901+ { 7902+ PK11err_add_data(PK11_F_GET_PUB_DSA_KEY, 7903+ PK11_R_FINDOBJECTSINIT, rv); 7904+ goto err; 7905+ } 7906+ 7907+ rv = pFuncList->C_FindObjects(session, &h_key, 1, &found); 7908+ 7909+ if (rv != CKR_OK) 7910+ { 7911+ PK11err_add_data(PK11_F_GET_PUB_DSA_KEY, 7912+ PK11_R_FINDOBJECTS, rv); 7913+ goto err; 7914+ } 7915+ 7916+ rv = pFuncList->C_FindObjectsFinal(session); 7917+ 7918+ if (rv != CKR_OK) 7919+ { 7920+ PK11err_add_data(PK11_F_GET_PUB_DSA_KEY, 7921+ PK11_R_FINDOBJECTSFINAL, rv); 7922+ goto err; 7923+ } 7924+ 7925+ if (found == 0) 7926+ { 7927+ rv = pFuncList->C_CreateObject(session, 7928+ a_key_template, ul_key_attr_count, &h_key); 7929+ if (rv != CKR_OK) 7930+ { 7931+ PK11err_add_data(PK11_F_GET_PUB_DSA_KEY, 7932+ PK11_R_CREATEOBJECT, rv); 7933+ goto err; 7934+ } 7935+ } 7936+ 7937+ if (dsa_pub_num != NULL) 7938+ if ((*dsa_pub_num = BN_dup(dsa->pub_key)) == NULL) 7939+ { 7940+ PK11err(PK11_F_GET_PUB_DSA_KEY, PK11_R_MALLOC_FAILURE); 7941+ rollback = TRUE; 7942+ goto err; 7943+ } 7944+ 7945+ /* LINTED: E_CONSTANT_CONDITION */ 7946+ KEY_HANDLE_REFHOLD(h_key, OP_DSA, FALSE, rollback, err); 7947+ if (key_ptr != NULL) 7948+ *key_ptr = dsa; 7949+ 7950+err: 7951+ if (rollback) 7952+ { 7953+ /* 7954+ * We do not care about the return value from C_DestroyObject() 7955+ * since we are doing rollback. 7956+ */ 7957+ if (found == 0) 7958+ (void) pFuncList->C_DestroyObject(session, h_key); 7959+ h_key = CK_INVALID_HANDLE; 7960+ } 7961+ 7962+ UNLOCK_OBJSTORE(OP_DSA); 7963+ 7964+malloc_err: 7965+ for (i = 4; i <= 7; i++) 7966+ { 7967+ if (a_key_template[i].pValue != NULL) 7968+ { 7969+ OPENSSL_free(a_key_template[i].pValue); 7970+ a_key_template[i].pValue = NULL; 7971+ } 7972+ } 7973+ 7974+ return (h_key); 7975+ } 7976+ 7977+/* 7978+ * Create a private key object in the session from a given dsa structure 7979+ * The *dsa_priv_num pointer is non-NULL for DSA private keys. 7980+ */ 7981+static CK_OBJECT_HANDLE pk11_get_private_dsa_key(DSA* dsa, 7982+ DSA **key_ptr, BIGNUM **dsa_priv_num, CK_SESSION_HANDLE session) 7983+ { 7984+ CK_RV rv; 7985+ CK_OBJECT_HANDLE h_key = CK_INVALID_HANDLE; 7986+ CK_OBJECT_CLASS o_key = CKO_PRIVATE_KEY; 7987+ int i; 7988+ CK_ULONG found; 7989+ CK_KEY_TYPE k_type = CKK_DSA; 7990+ CK_ULONG ul_key_attr_count = 9; 7991+ CK_BBOOL rollback = FALSE; 7992+ 7993+ /* Both CKA_TOKEN and CKA_SENSITIVE have to be FALSE for session keys */ 7994+ CK_ATTRIBUTE a_key_template[] = 7995+ { 7996+ {CKA_CLASS, (void *) NULL, sizeof (CK_OBJECT_CLASS)}, 7997+ {CKA_KEY_TYPE, (void *) NULL, sizeof (CK_KEY_TYPE)}, 7998+ {CKA_TOKEN, &false, sizeof (true)}, 7999+ {CKA_SENSITIVE, &false, sizeof (true)}, 8000+ {CKA_SIGN, &true, sizeof (true)}, 8001+ {CKA_PRIME, (void *)NULL, 0}, /* p */ 8002+ {CKA_SUBPRIME, (void *)NULL, 0}, /* q */ 8003+ {CKA_BASE, (void *)NULL, 0}, /* g */ 8004+ {CKA_VALUE, (void *)NULL, 0} /* priv_key - x */ 8005+ }; 8006+ 8007+ a_key_template[0].pValue = &o_key; 8008+ a_key_template[1].pValue = &k_type; 8009+ 8010+ /* Put the private key components into the template */ 8011+ if (init_template_value(dsa->p, &a_key_template[5].pValue, 8012+ &a_key_template[5].ulValueLen) == 0 || 8013+ init_template_value(dsa->q, &a_key_template[6].pValue, 8014+ &a_key_template[6].ulValueLen) == 0 || 8015+ init_template_value(dsa->g, &a_key_template[7].pValue, 8016+ &a_key_template[7].ulValueLen) == 0 || 8017+ init_template_value(dsa->priv_key, &a_key_template[8].pValue, 8018+ &a_key_template[8].ulValueLen) == 0) 8019+ { 8020+ PK11err(PK11_F_GET_PRIV_DSA_KEY, PK11_R_MALLOC_FAILURE); 8021+ goto malloc_err; 8022+ } 8023+ 8024+ /* see find_lock array definition for more info on object locking */ 8025+ LOCK_OBJSTORE(OP_DSA); 8026+ rv = pFuncList->C_FindObjectsInit(session, a_key_template, 8027+ ul_key_attr_count); 8028+ 8029+ if (rv != CKR_OK) 8030+ { 8031+ PK11err_add_data(PK11_F_GET_PRIV_DSA_KEY, 8032+ PK11_R_FINDOBJECTSINIT, rv); 8033+ goto err; 8034+ } 8035+ 8036+ rv = pFuncList->C_FindObjects(session, &h_key, 1, &found); 8037+ 8038+ if (rv != CKR_OK) 8039+ { 8040+ PK11err_add_data(PK11_F_GET_PRIV_DSA_KEY, 8041+ PK11_R_FINDOBJECTS, rv); 8042+ goto err; 8043+ } 8044+ 8045+ rv = pFuncList->C_FindObjectsFinal(session); 8046+ 8047+ if (rv != CKR_OK) 8048+ { 8049+ PK11err_add_data(PK11_F_GET_PRIV_DSA_KEY, 8050+ PK11_R_FINDOBJECTSFINAL, rv); 8051+ goto err; 8052+ } 8053+ 8054+ if (found == 0) 8055+ { 8056+ rv = pFuncList->C_CreateObject(session, 8057+ a_key_template, ul_key_attr_count, &h_key); 8058+ if (rv != CKR_OK) 8059+ { 8060+ PK11err_add_data(PK11_F_GET_PRIV_DSA_KEY, 8061+ PK11_R_CREATEOBJECT, rv); 8062+ goto err; 8063+ } 8064+ } 8065+ 8066+ if (dsa_priv_num != NULL) 8067+ if ((*dsa_priv_num = BN_dup(dsa->priv_key)) == NULL) 8068+ { 8069+ PK11err(PK11_F_GET_PRIV_DSA_KEY, PK11_R_MALLOC_FAILURE); 8070+ rollback = TRUE; 8071+ goto err; 8072+ } 8073+ 8074+ /* LINTED: E_CONSTANT_CONDITION */ 8075+ KEY_HANDLE_REFHOLD(h_key, OP_DSA, FALSE, rollback, err); 8076+ if (key_ptr != NULL) 8077+ *key_ptr = dsa; 8078+ 8079+err: 8080+ if (rollback) 8081+ { 8082+ /* 8083+ * We do not care about the return value from C_DestroyObject() 8084+ * since we are doing rollback. 8085+ */ 8086+ if (found == 0) 8087+ (void) pFuncList->C_DestroyObject(session, h_key); 8088+ h_key = CK_INVALID_HANDLE; 8089+ } 8090+ 8091+ UNLOCK_OBJSTORE(OP_DSA); 8092+ 8093+malloc_err: 8094+ /* 8095+ * 5 to 8 entries in the key template are key components. 8096+ * They need to be freed apon exit or error. 8097+ */ 8098+ for (i = 5; i <= 8; i++) 8099+ { 8100+ if (a_key_template[i].pValue != NULL) 8101+ { 8102+ (void) memset(a_key_template[i].pValue, 0, 8103+ a_key_template[i].ulValueLen); 8104+ OPENSSL_free(a_key_template[i].pValue); 8105+ a_key_template[i].pValue = NULL; 8106+ } 8107+ } 8108+ 8109+ return (h_key); 8110+ } 8111+ 8112+/* 8113+ * Check for cache miss and clean the object pointer and handle 8114+ * in such case. Return 1 for cache hit, 0 for cache miss. 8115+ */ 8116+static int check_new_dsa_key_pub(PK11_SESSION *sp, DSA *dsa) 8117+ { 8118+ /* 8119+ * Provide protection against DSA structure reuse by making the 8120+ * check for cache hit stronger. Only public key component of DSA 8121+ * key matters here so it is sufficient to compare it with value 8122+ * cached in PK11_SESSION structure. 8123+ */ 8124+ if ((sp->opdata_dsa_pub != dsa) || 8125+ (BN_cmp(sp->opdata_dsa_pub_num, dsa->pub_key) != 0)) 8126+ { 8127+ /* 8128+ * We do not check the return value because even in case of 8129+ * failure the sp structure will have both key pointer 8130+ * and object handle cleaned and pk11_destroy_object() 8131+ * reports the failure to the OpenSSL error message buffer. 8132+ */ 8133+ (void) pk11_destroy_dsa_object_pub(sp, TRUE); 8134+ return (0); 8135+ } 8136+ return (1); 8137+ } 8138+ 8139+/* 8140+ * Check for cache miss and clean the object pointer and handle 8141+ * in such case. Return 1 for cache hit, 0 for cache miss. 8142+ */ 8143+static int check_new_dsa_key_priv(PK11_SESSION *sp, DSA *dsa) 8144+ { 8145+ /* 8146+ * Provide protection against DSA structure reuse by making the 8147+ * check for cache hit stronger. Only private key component of DSA 8148+ * key matters here so it is sufficient to compare it with value 8149+ * cached in PK11_SESSION structure. 8150+ */ 8151+ if ((sp->opdata_dsa_priv != dsa) || 8152+ (BN_cmp(sp->opdata_dsa_priv_num, dsa->priv_key) != 0)) 8153+ { 8154+ /* 8155+ * We do not check the return value because even in case of 8156+ * failure the sp structure will have both key pointer 8157+ * and object handle cleaned and pk11_destroy_object() 8158+ * reports the failure to the OpenSSL error message buffer. 8159+ */ 8160+ (void) pk11_destroy_dsa_object_priv(sp, TRUE); 8161+ return (0); 8162+ } 8163+ return (1); 8164+ } 8165+#endif 8166+ 8167+ 8168+#ifndef OPENSSL_NO_DH 8169+/* The DH function implementation */ 8170+/* ARGSUSED */ 8171+static int pk11_DH_init(DH *dh) 8172+ { 8173+ return (1); 8174+ } 8175+ 8176+/* ARGSUSED */ 8177+static int pk11_DH_finish(DH *dh) 8178+ { 8179+ return (1); 8180+ } 8181+ 8182+/* 8183+ * Generate DH key-pair. 8184+ * 8185+ * Warning: Unlike OpenSSL's DH_generate_key(3) we ignore dh->priv_key 8186+ * and override it even if it is set. OpenSSL does not touch dh->priv_key 8187+ * if set and just computes dh->pub_key. It looks like PKCS#11 standard 8188+ * is not capable of providing this functionality. This could be a problem 8189+ * for applications relying on OpenSSL's semantics. 8190+ */ 8191+static int pk11_DH_generate_key(DH *dh) 8192+ { 8193+ CK_ULONG i; 8194+ CK_RV rv, rv1; 8195+ int reuse_mem_len = 0, ret = 0; 8196+ PK11_SESSION *sp = NULL; 8197+ CK_BYTE_PTR reuse_mem; 8198+ 8199+ CK_MECHANISM mechanism = {CKM_DH_PKCS_KEY_PAIR_GEN, NULL_PTR, 0}; 8200+ CK_OBJECT_HANDLE h_pub_key = CK_INVALID_HANDLE; 8201+ CK_OBJECT_HANDLE h_priv_key = CK_INVALID_HANDLE; 8202+ 8203+ CK_ULONG ul_pub_key_attr_count = 3; 8204+ CK_ATTRIBUTE pub_key_template[] = 8205+ { 8206+ {CKA_PRIVATE, &false, sizeof (false)}, 8207+ {CKA_PRIME, (void *)NULL, 0}, 8208+ {CKA_BASE, (void *)NULL, 0} 8209+ }; 8210+ 8211+ CK_ULONG ul_priv_key_attr_count = 3; 8212+ CK_ATTRIBUTE priv_key_template[] = 8213+ { 8214+ {CKA_PRIVATE, &false, sizeof (false)}, 8215+ {CKA_SENSITIVE, &false, sizeof (false)}, 8216+ {CKA_DERIVE, &true, sizeof (true)} 8217+ }; 8218+ 8219+ CK_ULONG pub_key_attr_result_count = 1; 8220+ CK_ATTRIBUTE pub_key_result[] = 8221+ { 8222+ {CKA_VALUE, (void *)NULL, 0} 8223+ }; 8224+ 8225+ CK_ULONG priv_key_attr_result_count = 1; 8226+ CK_ATTRIBUTE priv_key_result[] = 8227+ { 8228+ {CKA_VALUE, (void *)NULL, 0} 8229+ }; 8230+ 8231+ pub_key_template[1].ulValueLen = BN_num_bytes(dh->p); 8232+ if (pub_key_template[1].ulValueLen > 0) 8233+ { 8234+ /* 8235+ * We must not increase ulValueLen by DH_BUF_RESERVE since that 8236+ * could cause the same rounding problem. See definition of 8237+ * DH_BUF_RESERVE above. 8238+ */ 8239+ pub_key_template[1].pValue = 8240+ OPENSSL_malloc(pub_key_template[1].ulValueLen + 8241+ DH_BUF_RESERVE); 8242+ if (pub_key_template[1].pValue == NULL) 8243+ { 8244+ PK11err(PK11_F_DH_GEN_KEY, PK11_R_MALLOC_FAILURE); 8245+ goto err; 8246+ } 8247+ 8248+ i = BN_bn2bin(dh->p, pub_key_template[1].pValue); 8249+ } 8250+ else 8251+ goto err; 8252+ 8253+ pub_key_template[2].ulValueLen = BN_num_bytes(dh->g); 8254+ if (pub_key_template[2].ulValueLen > 0) 8255+ { 8256+ pub_key_template[2].pValue = 8257+ OPENSSL_malloc(pub_key_template[2].ulValueLen + 8258+ DH_BUF_RESERVE); 8259+ if (pub_key_template[2].pValue == NULL) 8260+ { 8261+ PK11err(PK11_F_DH_GEN_KEY, PK11_R_MALLOC_FAILURE); 8262+ goto err; 8263+ } 8264+ 8265+ i = BN_bn2bin(dh->g, pub_key_template[2].pValue); 8266+ } 8267+ else 8268+ goto err; 8269+ 8270+ /* 8271+ * Note: we are only using PK11_SESSION structure for getting 8272+ * a session handle. The objects created in this function are 8273+ * destroyed before return and thus not cached. 8274+ */ 8275+ if ((sp = pk11_get_session(OP_DH)) == NULL) 8276+ goto err; 8277+ 8278+ rv = pFuncList->C_GenerateKeyPair(sp->session, 8279+ &mechanism, 8280+ pub_key_template, 8281+ ul_pub_key_attr_count, 8282+ priv_key_template, 8283+ ul_priv_key_attr_count, 8284+ &h_pub_key, 8285+ &h_priv_key); 8286+ if (rv != CKR_OK) 8287+ { 8288+ PK11err_add_data(PK11_F_DH_GEN_KEY, PK11_R_GEN_KEY, rv); 8289+ goto err; 8290+ } 8291+ 8292+ /* 8293+ * Reuse the larger memory allocated. We know the larger memory 8294+ * should be sufficient for reuse. 8295+ */ 8296+ if (pub_key_template[1].ulValueLen > pub_key_template[2].ulValueLen) 8297+ { 8298+ reuse_mem = pub_key_template[1].pValue; 8299+ reuse_mem_len = pub_key_template[1].ulValueLen + DH_BUF_RESERVE; 8300+ } 8301+ else 8302+ { 8303+ reuse_mem = pub_key_template[2].pValue; 8304+ reuse_mem_len = pub_key_template[2].ulValueLen + DH_BUF_RESERVE; 8305+ } 8306+ 8307+ rv = pFuncList->C_GetAttributeValue(sp->session, h_pub_key, 8308+ pub_key_result, pub_key_attr_result_count); 8309+ rv1 = pFuncList->C_GetAttributeValue(sp->session, h_priv_key, 8310+ priv_key_result, priv_key_attr_result_count); 8311+ 8312+ if (rv != CKR_OK || rv1 != CKR_OK) 8313+ { 8314+ rv = (rv != CKR_OK) ? rv : rv1; 8315+ PK11err_add_data(PK11_F_DH_GEN_KEY, 8316+ PK11_R_GETATTRIBUTVALUE, rv); 8317+ goto err; 8318+ } 8319+ 8320+ if (((CK_LONG) pub_key_result[0].ulValueLen) <= 0 || 8321+ ((CK_LONG) priv_key_result[0].ulValueLen) <= 0) 8322+ { 8323+ PK11err(PK11_F_DH_GEN_KEY, PK11_R_GETATTRIBUTVALUE); 8324+ goto err; 8325+ } 8326+ 8327+ /* Reuse the memory allocated */ 8328+ pub_key_result[0].pValue = reuse_mem; 8329+ pub_key_result[0].ulValueLen = reuse_mem_len; 8330+ 8331+ rv = pFuncList->C_GetAttributeValue(sp->session, h_pub_key, 8332+ pub_key_result, pub_key_attr_result_count); 8333+ 8334+ if (rv != CKR_OK) 8335+ { 8336+ PK11err_add_data(PK11_F_DH_GEN_KEY, 8337+ PK11_R_GETATTRIBUTVALUE, rv); 8338+ goto err; 8339+ } 8340+ 8341+ if (pub_key_result[0].type == CKA_VALUE) 8342+ { 8343+ if (dh->pub_key == NULL) 8344+ if ((dh->pub_key = BN_new()) == NULL) 8345+ { 8346+ PK11err(PK11_F_DH_GEN_KEY, 8347+ PK11_R_MALLOC_FAILURE); 8348+ goto err; 8349+ } 8350+ dh->pub_key = BN_bin2bn(pub_key_result[0].pValue, 8351+ pub_key_result[0].ulValueLen, dh->pub_key); 8352+ if (dh->pub_key == NULL) 8353+ { 8354+ PK11err(PK11_F_DH_GEN_KEY, PK11_R_MALLOC_FAILURE); 8355+ goto err; 8356+ } 8357+ } 8358+ 8359+ /* Reuse the memory allocated */ 8360+ priv_key_result[0].pValue = reuse_mem; 8361+ priv_key_result[0].ulValueLen = reuse_mem_len; 8362+ 8363+ rv = pFuncList->C_GetAttributeValue(sp->session, h_priv_key, 8364+ priv_key_result, priv_key_attr_result_count); 8365+ 8366+ if (rv != CKR_OK) 8367+ { 8368+ PK11err_add_data(PK11_F_DH_GEN_KEY, 8369+ PK11_R_GETATTRIBUTVALUE, rv); 8370+ goto err; 8371+ } 8372+ 8373+ if (priv_key_result[0].type == CKA_VALUE) 8374+ { 8375+ if (dh->priv_key == NULL) 8376+ if ((dh->priv_key = BN_new()) == NULL) 8377+ { 8378+ PK11err(PK11_F_DH_GEN_KEY, 8379+ PK11_R_MALLOC_FAILURE); 8380+ goto err; 8381+ } 8382+ dh->priv_key = BN_bin2bn(priv_key_result[0].pValue, 8383+ priv_key_result[0].ulValueLen, dh->priv_key); 8384+ if (dh->priv_key == NULL) 8385+ { 8386+ PK11err(PK11_F_DH_GEN_KEY, PK11_R_MALLOC_FAILURE); 8387+ goto err; 8388+ } 8389+ } 8390+ 8391+ ret = 1; 8392+ 8393+err: 8394+ 8395+ if (h_pub_key != CK_INVALID_HANDLE) 8396+ { 8397+ rv = pFuncList->C_DestroyObject(sp->session, h_pub_key); 8398+ if (rv != CKR_OK) 8399+ { 8400+ PK11err_add_data(PK11_F_DH_GEN_KEY, 8401+ PK11_R_DESTROYOBJECT, rv); 8402+ } 8403+ } 8404+ 8405+ if (h_priv_key != CK_INVALID_HANDLE) 8406+ { 8407+ rv = pFuncList->C_DestroyObject(sp->session, h_priv_key); 8408+ if (rv != CKR_OK) 8409+ { 8410+ PK11err_add_data(PK11_F_DH_GEN_KEY, 8411+ PK11_R_DESTROYOBJECT, rv); 8412+ } 8413+ } 8414+ 8415+ for (i = 1; i <= 2; i++) 8416+ { 8417+ if (pub_key_template[i].pValue != NULL) 8418+ { 8419+ OPENSSL_free(pub_key_template[i].pValue); 8420+ pub_key_template[i].pValue = NULL; 8421+ } 8422+ } 8423+ 8424+ pk11_return_session(sp, OP_DH); 8425+ return (ret); 8426+ } 8427+ 8428+static int pk11_DH_compute_key(unsigned char *key, const BIGNUM *pub_key, 8429+ DH *dh) 8430+ { 8431+ unsigned int i; 8432+ CK_MECHANISM mechanism = {CKM_DH_PKCS_DERIVE, NULL_PTR, 0}; 8433+ CK_OBJECT_CLASS key_class = CKO_SECRET_KEY; 8434+ CK_KEY_TYPE key_type = CKK_GENERIC_SECRET; 8435+ CK_OBJECT_HANDLE h_derived_key = CK_INVALID_HANDLE; 8436+ CK_OBJECT_HANDLE h_key = CK_INVALID_HANDLE; 8437+ 8438+ CK_ULONG ul_priv_key_attr_count = 2; 8439+ CK_ATTRIBUTE priv_key_template[] = 8440+ { 8441+ {CKA_CLASS, (void*) NULL, sizeof (key_class)}, 8442+ {CKA_KEY_TYPE, (void*) NULL, sizeof (key_type)}, 8443+ }; 8444+ 8445+ CK_ULONG priv_key_attr_result_count = 1; 8446+ CK_ATTRIBUTE priv_key_result[] = 8447+ { 8448+ {CKA_VALUE, (void *)NULL, 0} 8449+ }; 8450+ 8451+ CK_RV rv; 8452+ int ret = -1; 8453+ PK11_SESSION *sp = NULL; 8454+ 8455+ if (dh->priv_key == NULL) 8456+ goto err; 8457+ 8458+ priv_key_template[0].pValue = &key_class; 8459+ priv_key_template[1].pValue = &key_type; 8460+ 8461+ if ((sp = pk11_get_session(OP_DH)) == NULL) 8462+ goto err; 8463+ 8464+ mechanism.ulParameterLen = BN_num_bytes(pub_key); 8465+ mechanism.pParameter = OPENSSL_malloc(mechanism.ulParameterLen); 8466+ if (mechanism.pParameter == NULL) 8467+ { 8468+ PK11err(PK11_F_DH_COMP_KEY, PK11_R_MALLOC_FAILURE); 8469+ goto err; 8470+ } 8471+ BN_bn2bin(pub_key, mechanism.pParameter); 8472+ 8473+ (void) check_new_dh_key(sp, dh); 8474+ 8475+ h_key = sp->opdata_dh_key; 8476+ if (h_key == CK_INVALID_HANDLE) 8477+ h_key = sp->opdata_dh_key = 8478+ pk11_get_dh_key((DH*) dh, &sp->opdata_dh, 8479+ &sp->opdata_dh_priv_num, sp->session); 8480+ 8481+ if (h_key == CK_INVALID_HANDLE) 8482+ { 8483+ PK11err(PK11_F_DH_COMP_KEY, PK11_R_CREATEOBJECT); 8484+ goto err; 8485+ } 8486+ 8487+ rv = pFuncList->C_DeriveKey(sp->session, 8488+ &mechanism, 8489+ h_key, 8490+ priv_key_template, 8491+ ul_priv_key_attr_count, 8492+ &h_derived_key); 8493+ if (rv != CKR_OK) 8494+ { 8495+ PK11err_add_data(PK11_F_DH_COMP_KEY, PK11_R_DERIVEKEY, rv); 8496+ goto err; 8497+ } 8498+ 8499+ rv = pFuncList->C_GetAttributeValue(sp->session, h_derived_key, 8500+ priv_key_result, priv_key_attr_result_count); 8501+ 8502+ if (rv != CKR_OK) 8503+ { 8504+ PK11err_add_data(PK11_F_DH_COMP_KEY, PK11_R_GETATTRIBUTVALUE, 8505+ rv); 8506+ goto err; 8507+ } 8508+ 8509+ if (((CK_LONG) priv_key_result[0].ulValueLen) <= 0) 8510+ { 8511+ PK11err(PK11_F_DH_COMP_KEY, PK11_R_GETATTRIBUTVALUE); 8512+ goto err; 8513+ } 8514+ priv_key_result[0].pValue = 8515+ OPENSSL_malloc(priv_key_result[0].ulValueLen); 8516+ if (!priv_key_result[0].pValue) 8517+ { 8518+ PK11err(PK11_F_DH_COMP_KEY, PK11_R_MALLOC_FAILURE); 8519+ goto err; 8520+ } 8521+ 8522+ rv = pFuncList->C_GetAttributeValue(sp->session, h_derived_key, 8523+ priv_key_result, priv_key_attr_result_count); 8524+ 8525+ if (rv != CKR_OK) 8526+ { 8527+ PK11err_add_data(PK11_F_DH_COMP_KEY, PK11_R_GETATTRIBUTVALUE, 8528+ rv); 8529+ goto err; 8530+ } 8531+ 8532+ /* 8533+ * OpenSSL allocates the output buffer 'key' which is the same 8534+ * length of the public key. It is long enough for the derived key 8535+ */ 8536+ if (priv_key_result[0].type == CKA_VALUE) 8537+ { 8538+ /* 8539+ * CKM_DH_PKCS_DERIVE mechanism is not supposed to strip 8540+ * leading zeros from a computed shared secret. However, 8541+ * OpenSSL always did it so we must do the same here. The 8542+ * vagueness of the spec regarding leading zero bytes was 8543+ * finally cleared with TLS 1.1 (RFC 4346) saying that leading 8544+ * zeros are stripped before the computed data is used as the 8545+ * pre-master secret. 8546+ */ 8547+ for (i = 0; i < priv_key_result[0].ulValueLen; ++i) 8548+ { 8549+ if (((char *)priv_key_result[0].pValue)[i] != 0) 8550+ break; 8551+ } 8552+ 8553+ (void) memcpy(key, ((char *)priv_key_result[0].pValue) + i, 8554+ priv_key_result[0].ulValueLen - i); 8555+ ret = priv_key_result[0].ulValueLen - i; 8556+ } 8557+ 8558+err: 8559+ 8560+ if (h_derived_key != CK_INVALID_HANDLE) 8561+ { 8562+ rv = pFuncList->C_DestroyObject(sp->session, h_derived_key); 8563+ if (rv != CKR_OK) 8564+ { 8565+ PK11err_add_data(PK11_F_DH_COMP_KEY, 8566+ PK11_R_DESTROYOBJECT, rv); 8567+ } 8568+ } 8569+ if (priv_key_result[0].pValue) 8570+ { 8571+ OPENSSL_free(priv_key_result[0].pValue); 8572+ priv_key_result[0].pValue = NULL; 8573+ } 8574+ 8575+ if (mechanism.pParameter) 8576+ { 8577+ OPENSSL_free(mechanism.pParameter); 8578+ mechanism.pParameter = NULL; 8579+ } 8580+ 8581+ pk11_return_session(sp, OP_DH); 8582+ return (ret); 8583+ } 8584+ 8585+ 8586+static CK_OBJECT_HANDLE pk11_get_dh_key(DH* dh, 8587+ DH **key_ptr, BIGNUM **dh_priv_num, CK_SESSION_HANDLE session) 8588+ { 8589+ CK_RV rv; 8590+ CK_OBJECT_HANDLE h_key = CK_INVALID_HANDLE; 8591+ CK_OBJECT_CLASS class = CKO_PRIVATE_KEY; 8592+ CK_KEY_TYPE key_type = CKK_DH; 8593+ CK_ULONG found; 8594+ CK_BBOOL rollback = FALSE; 8595+ int i; 8596+ 8597+ CK_ULONG ul_key_attr_count = 7; 8598+ CK_ATTRIBUTE key_template[] = 8599+ { 8600+ {CKA_CLASS, (void*) NULL, sizeof (class)}, 8601+ {CKA_KEY_TYPE, (void*) NULL, sizeof (key_type)}, 8602+ {CKA_DERIVE, &true, sizeof (true)}, 8603+ {CKA_PRIVATE, &false, sizeof (false)}, 8604+ {CKA_PRIME, (void *) NULL, 0}, 8605+ {CKA_BASE, (void *) NULL, 0}, 8606+ {CKA_VALUE, (void *) NULL, 0}, 8607+ }; 8608+ 8609+ key_template[0].pValue = &class; 8610+ key_template[1].pValue = &key_type; 8611+ 8612+ key_template[4].ulValueLen = BN_num_bytes(dh->p); 8613+ key_template[4].pValue = (CK_VOID_PTR)OPENSSL_malloc( 8614+ (size_t)key_template[4].ulValueLen); 8615+ if (key_template[4].pValue == NULL) 8616+ { 8617+ PK11err(PK11_F_GET_DH_KEY, PK11_R_MALLOC_FAILURE); 8618+ goto malloc_err; 8619+ } 8620+ 8621+ BN_bn2bin(dh->p, key_template[4].pValue); 8622+ 8623+ key_template[5].ulValueLen = BN_num_bytes(dh->g); 8624+ key_template[5].pValue = (CK_VOID_PTR)OPENSSL_malloc( 8625+ (size_t)key_template[5].ulValueLen); 8626+ if (key_template[5].pValue == NULL) 8627+ { 8628+ PK11err(PK11_F_GET_DH_KEY, PK11_R_MALLOC_FAILURE); 8629+ goto malloc_err; 8630+ } 8631+ 8632+ BN_bn2bin(dh->g, key_template[5].pValue); 8633+ 8634+ key_template[6].ulValueLen = BN_num_bytes(dh->priv_key); 8635+ key_template[6].pValue = (CK_VOID_PTR)OPENSSL_malloc( 8636+ (size_t)key_template[6].ulValueLen); 8637+ if (key_template[6].pValue == NULL) 8638+ { 8639+ PK11err(PK11_F_GET_DH_KEY, PK11_R_MALLOC_FAILURE); 8640+ goto malloc_err; 8641+ } 8642+ 8643+ BN_bn2bin(dh->priv_key, key_template[6].pValue); 8644+ 8645+ /* see find_lock array definition for more info on object locking */ 8646+ LOCK_OBJSTORE(OP_DH); 8647+ rv = pFuncList->C_FindObjectsInit(session, key_template, 8648+ ul_key_attr_count); 8649+ 8650+ if (rv != CKR_OK) 8651+ { 8652+ PK11err_add_data(PK11_F_GET_DH_KEY, PK11_R_FINDOBJECTSINIT, rv); 8653+ goto err; 8654+ } 8655+ 8656+ rv = pFuncList->C_FindObjects(session, &h_key, 1, &found); 8657+ 8658+ if (rv != CKR_OK) 8659+ { 8660+ PK11err_add_data(PK11_F_GET_DH_KEY, PK11_R_FINDOBJECTS, rv); 8661+ goto err; 8662+ } 8663+ 8664+ rv = pFuncList->C_FindObjectsFinal(session); 8665+ 8666+ if (rv != CKR_OK) 8667+ { 8668+ PK11err_add_data(PK11_F_GET_DH_KEY, PK11_R_FINDOBJECTSFINAL, 8669+ rv); 8670+ goto err; 8671+ } 8672+ 8673+ if (found == 0) 8674+ { 8675+ rv = pFuncList->C_CreateObject(session, 8676+ key_template, ul_key_attr_count, &h_key); 8677+ if (rv != CKR_OK) 8678+ { 8679+ PK11err_add_data(PK11_F_GET_DH_KEY, PK11_R_CREATEOBJECT, 8680+ rv); 8681+ goto err; 8682+ } 8683+ } 8684+ 8685+ if (dh_priv_num != NULL) 8686+ if ((*dh_priv_num = BN_dup(dh->priv_key)) == NULL) 8687+ { 8688+ PK11err(PK11_F_GET_DH_KEY, PK11_R_MALLOC_FAILURE); 8689+ rollback = TRUE; 8690+ goto err; 8691+ } 8692+ 8693+ /* LINTED: E_CONSTANT_CONDITION */ 8694+ KEY_HANDLE_REFHOLD(h_key, OP_DH, FALSE, rollback, err); 8695+ if (key_ptr != NULL) 8696+ *key_ptr = dh; 8697+ 8698+err: 8699+ if (rollback) 8700+ { 8701+ /* 8702+ * We do not care about the return value from C_DestroyObject() 8703+ * since we are doing rollback. 8704+ */ 8705+ if (found == 0) 8706+ (void) pFuncList->C_DestroyObject(session, h_key); 8707+ h_key = CK_INVALID_HANDLE; 8708+ } 8709+ 8710+ UNLOCK_OBJSTORE(OP_DH); 8711+ 8712+malloc_err: 8713+ for (i = 4; i <= 6; i++) 8714+ { 8715+ if (key_template[i].pValue != NULL) 8716+ { 8717+ OPENSSL_free(key_template[i].pValue); 8718+ key_template[i].pValue = NULL; 8719+ } 8720+ } 8721+ 8722+ return (h_key); 8723+ } 8724+ 8725+/* 8726+ * Check for cache miss and clean the object pointer and handle 8727+ * in such case. Return 1 for cache hit, 0 for cache miss. 8728+ * 8729+ * Note: we rely on pk11_destroy_dh_key_objects() to set sp->opdata_dh 8730+ * to CK_INVALID_HANDLE even when it fails to destroy the object. 8731+ */ 8732+static int check_new_dh_key(PK11_SESSION *sp, DH *dh) 8733+ { 8734+ /* 8735+ * Provide protection against DH structure reuse by making the 8736+ * check for cache hit stronger. Private key component of DH key 8737+ * is unique so it is sufficient to compare it with value cached 8738+ * in PK11_SESSION structure. 8739+ */ 8740+ if ((sp->opdata_dh != dh) || 8741+ (BN_cmp(sp->opdata_dh_priv_num, dh->priv_key) != 0)) 8742+ { 8743+ /* 8744+ * We do not check the return value because even in case of 8745+ * failure the sp structure will have both key pointer 8746+ * and object handle cleaned and pk11_destroy_object() 8747+ * reports the failure to the OpenSSL error message buffer. 8748+ */ 8749+ (void) pk11_destroy_dh_object(sp, TRUE); 8750+ return (0); 8751+ } 8752+ return (1); 8753+ } 8754+#endif 8755+ 8756+/* 8757+ * Local function to simplify key template population 8758+ * Return 0 -- error, 1 -- no error 8759+ */ 8760+static int 8761+init_template_value(BIGNUM *bn, CK_VOID_PTR *p_value, 8762+ CK_ULONG *ul_value_len) 8763+ { 8764+ CK_ULONG len = 0; 8765+ 8766+ /* 8767+ * This function can be used on non-initialized BIGNUMs. It is 8768+ * easier to check that here than individually in the callers. 8769+ */ 8770+ if (bn != NULL) 8771+ len = BN_num_bytes(bn); 8772+ 8773+ if (bn == NULL || len == 0) 8774+ return (1); 8775+ 8776+ *ul_value_len = len; 8777+ *p_value = (CK_VOID_PTR)OPENSSL_malloc((size_t)*ul_value_len); 8778+ if (*p_value == NULL) 8779+ return (0); 8780+ 8781+ BN_bn2bin(bn, *p_value); 8782+ 8783+ return (1); 8784+ } 8785+ 8786+static void 8787+attr_to_BN(CK_ATTRIBUTE_PTR attr, CK_BYTE attr_data[], BIGNUM **bn) 8788+ { 8789+ if (attr->ulValueLen > 0) 8790+ *bn = BN_bin2bn(attr_data, attr->ulValueLen, NULL); 8791+ } 8792+ 8793+/* 8794+ * Find one object in the token. It is an error if we can not find the 8795+ * object or if we find more objects based on the template we got. 8796+ * 8797+ * Returns: 8798+ * 1 OK 8799+ * 0 no object or more than 1 object found 8800+ */ 8801+static int 8802+find_one_object(PK11_OPTYPE op, CK_SESSION_HANDLE s, 8803+ CK_ATTRIBUTE_PTR ptempl, CK_ULONG nattr, CK_OBJECT_HANDLE_PTR pkey) 8804+ { 8805+ CK_RV rv; 8806+ CK_ULONG objcnt; 8807+ 8808+ LOCK_OBJSTORE(op); 8809+ if ((rv = pFuncList->C_FindObjectsInit(s, ptempl, nattr)) != CKR_OK) 8810+ { 8811+ PK11err_add_data(PK11_F_FIND_ONE_OBJECT, 8812+ PK11_R_FINDOBJECTSINIT, rv); 8813+ goto err; 8814+ } 8815+ 8816+ rv = pFuncList->C_FindObjects(s, pkey, 1, &objcnt); 8817+ if (rv != CKR_OK) 8818+ { 8819+ PK11err_add_data(PK11_F_FIND_ONE_OBJECT, PK11_R_FINDOBJECTS, 8820+ rv); 8821+ goto err; 8822+ } 8823+ 8824+ if (objcnt > 1) 8825+ { 8826+ PK11err(PK11_F_FIND_ONE_OBJECT, 8827+ PK11_R_MORE_THAN_ONE_OBJECT_FOUND); 8828+ goto err; 8829+ } 8830+ else if (objcnt == 0) 8831+ { 8832+ PK11err(PK11_F_FIND_ONE_OBJECT, PK11_R_NO_OBJECT_FOUND); 8833+ goto err; 8834+ } 8835+ 8836+ (void) pFuncList->C_FindObjectsFinal(s); 8837+ UNLOCK_OBJSTORE(op); 8838+ return (1); 8839+err: 8840+ UNLOCK_OBJSTORE(op); 8841+ return (0); 8842+ } 8843+ 8844+/* from uri stuff */ 8845+ 8846+extern char *pk11_pin; 8847+ 8848+static int pk11_get_pin(void); 8849+ 8850+static int 8851+pk11_get_pin(void) 8852+{ 8853+ char *pin; 8854+ 8855+ /* The getpassphrase() function is not MT safe. */ 8856+#ifndef NOPTHREADS 8857+ (void) pthread_mutex_lock(token_lock); 8858+#else 8859+ CRYPTO_w_lock(CRYPTO_LOCK_PK11_ENGINE); 8860+#endif 8861+ pin = getpassphrase("Enter PIN: "); 8862+ if (pin == NULL) 8863+ { 8864+ PK11err(PK11_F_GET_PIN, PK11_R_COULD_NOT_READ_PIN); 8865+#ifndef NOPTHREADS 8866+ (void) pthread_mutex_unlock(token_lock); 8867+#else 8868+ CRYPTO_w_unlock(CRYPTO_LOCK_PK11_ENGINE); 8869+#endif 8870+ goto err; 8871+ } 8872+ pk11_pin = BUF_strdup(pin); 8873+ if (pk11_pin == NULL) 8874+ { 8875+ PK11err(PK11_F_LOAD_PRIVKEY, PK11_R_MALLOC_FAILURE); 8876+#ifndef NOPTHREADS 8877+ (void) pthread_mutex_unlock(token_lock); 8878+#else 8879+ CRYPTO_w_unlock(CRYPTO_LOCK_PK11_ENGINE); 8880+#endif 8881+ goto err; 8882+ } 8883+ memset(pin, 0, strlen(pin)); 8884+#ifndef NOPTHREADS 8885+ (void) pthread_mutex_unlock(token_lock); 8886+#else 8887+ CRYPTO_w_unlock(CRYPTO_LOCK_PK11_ENGINE); 8888+#endif 8889+ return (1); 8890+err: 8891+ return (0); 8892+ } 8893+ 8894+/* 8895+ * Log in to the keystore if we are supposed to do that at all. Take care of 8896+ * reading and caching the PIN etc. Log in only once even when called from 8897+ * multiple threads. 8898+ * 8899+ * Returns: 8900+ * 1 on success 8901+ * 0 on failure 8902+ */ 8903+static int 8904+pk11_token_login(CK_SESSION_HANDLE session, CK_BBOOL *login_done, 8905+ CK_BBOOL is_private) 8906+ { 8907+ CK_RV rv; 8908+ 8909+#if 0 8910+ /* doesn't work on the AEP Keyper??? */ 8911+ if ((pubkey_token_flags & CKF_TOKEN_INITIALIZED) == 0) 8912+ { 8913+ PK11err(PK11_F_TOKEN_LOGIN, 8914+ PK11_R_TOKEN_NOT_INITIALIZED); 8915+ goto err; 8916+ } 8917+#endif 8918+ 8919+ /* 8920+ * If login is required or needed but the PIN has not been 8921+ * even initialized we can bail out right now. Note that we 8922+ * are supposed to always log in if we are going to access 8923+ * private keys. However, we may need to log in even for 8924+ * accessing public keys in case that the CKF_LOGIN_REQUIRED 8925+ * flag is set. 8926+ */ 8927+ if (((pubkey_token_flags & CKF_LOGIN_REQUIRED) || 8928+ (is_private == CK_TRUE)) && 8929+ (~pubkey_token_flags & CKF_USER_PIN_INITIALIZED)) 8930+ { 8931+ PK11err(PK11_F_TOKEN_LOGIN, PK11_R_TOKEN_PIN_NOT_SET); 8932+ goto err; 8933+ } 8934+ 8935+ /* 8936+ * Note on locking: it is possible that more than one thread 8937+ * gets into pk11_get_pin() so we must deal with that. We 8938+ * cannot avoid it since we cannot guard fork() in there with 8939+ * a lock because we could end up in a dead lock in the 8940+ * child. Why? Remember we are in a multithreaded environment 8941+ * so we must lock all mutexes in the prefork function to 8942+ * avoid a situation in which a thread that did not call 8943+ * fork() held a lock, making future unlocking impossible. We 8944+ * lock right before C_Login(). 8945+ */ 8946+ if ((pubkey_token_flags & CKF_LOGIN_REQUIRED) || 8947+ (is_private == CK_TRUE)) 8948+ { 8949+ if (*login_done == CK_FALSE) 8950+ { 8951+ if ((pk11_pin == NULL) && (pk11_get_pin() == 0)) 8952+ { 8953+ PK11err(PK11_F_TOKEN_LOGIN, 8954+ PK11_R_TOKEN_PIN_NOT_PROVIDED); 8955+ goto err; 8956+ } 8957+ } 8958+ 8959+ /* 8960+ * Note that what we are logging into is the keystore from 8961+ * pubkey_SLOTID because we work with OP_RSA session type here. 8962+ * That also means that we can work with only one keystore in 8963+ * the engine. 8964+ * 8965+ * We must make sure we do not try to login more than once. 8966+ * Also, see the comment above on locking strategy. 8967+ */ 8968+ 8969+#ifndef NOPTHREADS 8970+ (void) pthread_mutex_lock(token_lock); 8971+#else 8972+ (void) pthread_mutex_lock(freelist_lock); 8973+#endif 8974+ if (*login_done == CK_FALSE) 8975+ { 8976+ if ((rv = pFuncList->C_Login(session, 8977+ CKU_USER, (CK_UTF8CHAR*)pk11_pin, 8978+ strlen(pk11_pin))) != CKR_OK) 8979+ { 8980+ PK11err_add_data(PK11_F_TOKEN_LOGIN, 8981+ PK11_R_TOKEN_LOGIN_FAILED, rv); 8982+ goto err_locked; 8983+ } 8984+ 8985+ *login_done = CK_TRUE; 8986+ 8987+ } 8988+#ifndef NOPTHREADS 8989+ (void) pthread_mutex_unlock(token_lock); 8990+#else 8991+ CRYPTO_w_unlock(CRYPTO_LOCK_PK11_ENGINE); 8992+#endif 8993+ } 8994+ else 8995+ { 8996+ /* 8997+ * If token does not require login we take it as the 8998+ * login was done. 8999+ */ 9000+ *login_done = CK_TRUE; 9001+ } 9002+ 9003+ return (1); 9004+ 9005+err_locked: 9006+ if (pk11_pin) { 9007+ memset(pk11_pin, 0, strlen(pk11_pin)); 9008+ OPENSSL_free((void*)pk11_pin); 9009+ } 9010+ pk11_pin = NULL; 9011+#ifndef NOPTHREADS 9012+ (void) pthread_mutex_unlock(token_lock); 9013+#else 9014+ CRYPTO_w_unlock(CRYPTO_LOCK_PK11_ENGINE); 9015+#endif 9016+err: 9017+ return (0); 9018+ } 9019+ 9020+/* 9021+ * Log in to the keystore in the child if we were logged in in the 9022+ * parent. There are similarities in the code with pk11_token_login() 9023+ * but still it is quite different so we need a separate function for 9024+ * this. 9025+ * 9026+ * Note that this function is called under the locked session mutex when fork is 9027+ * detected. That means that C_Login() will be called from the child just once. 9028+ * 9029+ * Returns: 9030+ * 1 on success 9031+ * 0 on failure 9032+ */ 9033+int 9034+pk11_token_relogin(CK_SESSION_HANDLE session) 9035+ { 9036+ CK_RV rv; 9037+ 9038+ if ((pk11_pin == NULL) && (pk11_get_pin() == 0)) 9039+ goto err; 9040+ 9041+#ifndef NOPTHREADS 9042+ (void) pthread_mutex_lock(token_lock); 9043+#else 9044+ (void) pthread_mutex_lock(freelist_lock); 9045+#endif 9046+ if ((rv = pFuncList->C_Login(session, CKU_USER, 9047+ (CK_UTF8CHAR_PTR)pk11_pin, strlen(pk11_pin))) != CKR_OK) 9048+ { 9049+ PK11err_add_data(PK11_F_TOKEN_RELOGIN, 9050+ PK11_R_TOKEN_LOGIN_FAILED, rv); 9051+#ifndef NOPTHREADS 9052+ (void) pthread_mutex_unlock(token_lock); 9053+#else 9054+ CRYPTO_w_unlock(CRYPTO_LOCK_PK11_ENGINE); 9055+#endif 9056+ goto err; 9057+ } 9058+#ifndef NOPTHREADS 9059+ (void) pthread_mutex_unlock(token_lock); 9060+#else 9061+ CRYPTO_w_unlock(CRYPTO_LOCK_PK11_ENGINE); 9062+#endif 9063+ 9064+ return (1); 9065+err: 9066+ return (0); 9067+ } 9068+ 9069+#ifdef OPENSSL_SYS_WIN32 9070+char *getpassphrase(const char *prompt) 9071+ { 9072+ static char buf[128]; 9073+ HANDLE h; 9074+ DWORD cc, mode; 9075+ int cnt; 9076+ 9077+ h = GetStdHandle(STD_INPUT_HANDLE); 9078+ fputs(prompt, stderr); 9079+ fflush(stderr); 9080+ fflush(stdout); 9081+ FlushConsoleInputBuffer(h); 9082+ GetConsoleMode(h, &mode); 9083+ SetConsoleMode(h, ENABLE_PROCESSED_INPUT); 9084+ 9085+ for (cnt = 0; cnt < sizeof(buf) - 1; cnt++) 9086+ { 9087+ ReadFile(h, buf + cnt, 1, &cc, NULL); 9088+ if (buf[cnt] == '\r') 9089+ break; 9090+ fputc('*', stdout); 9091+ fflush(stderr); 9092+ fflush(stdout); 9093+ } 9094+ 9095+ SetConsoleMode(h, mode); 9096+ buf[cnt] = '\0'; 9097+ fputs("\n", stderr); 9098+ return buf; 9099+ } 9100+#endif /* OPENSSL_SYS_WIN32 */ 9101+#endif /* OPENSSL_NO_HW_PK11CA */ 9102+#endif /* OPENSSL_NO_HW_PK11 */ 9103+#endif /* OPENSSL_NO_HW */ 9104Index: openssl/crypto/engine/hw_pk11ca.h 9105diff -u /dev/null openssl/crypto/engine/hw_pk11ca.h:1.2.4.2 9106--- /dev/null Mon Jan 16 18:53:42 2012 9107+++ openssl/crypto/engine/hw_pk11ca.h Wed Jun 15 21:12:32 2011 9108@@ -0,0 +1,32 @@ 9109+/* Redefine all pk11/PK11 external symbols to pk11ca/PK11CA */ 9110+ 9111+#define token_lock pk11ca_token_lock 9112+#define find_lock pk11ca_find_lock 9113+#define active_list pk11ca_active_list 9114+#define pubkey_token_flags pk11ca_pubkey_token_flags 9115+#define pubkey_SLOTID pk11ca_pubkey_SLOTID 9116+#define ERR_pk11_error ERR_pk11ca_error 9117+#define PK11err_add_data PK11CAerr_add_data 9118+#define pk11_get_session pk11ca_get_session 9119+#define pk11_return_session pk11ca_return_session 9120+#define pk11_active_add pk11ca_active_add 9121+#define pk11_active_delete pk11ca_active_delete 9122+#define pk11_active_remove pk11ca_active_remove 9123+#define pk11_free_active_list pk11ca_free_active_list 9124+#define pk11_destroy_rsa_key_objects pk11ca_destroy_rsa_key_objects 9125+#define pk11_destroy_rsa_object_pub pk11ca_destroy_rsa_object_pub 9126+#define pk11_destroy_rsa_object_priv pk11ca_destroy_rsa_object_priv 9127+#define pk11_load_privkey pk11ca_load_privkey 9128+#define pk11_load_pubkey pk11ca_load_pubkey 9129+#define PK11_RSA PK11CA_RSA 9130+#define pk11_destroy_dsa_key_objects pk11ca_destroy_dsa_key_objects 9131+#define pk11_destroy_dsa_object_pub pk11ca_destroy_dsa_object_pub 9132+#define pk11_destroy_dsa_object_priv pk11ca_destroy_dsa_object_priv 9133+#define PK11_DSA PK11CA_DSA 9134+#define pk11_destroy_dh_key_objects pk11ca_destroy_dh_key_objects 9135+#define pk11_destroy_dh_object pk11ca_destroy_dh_object 9136+#define PK11_DH PK11CA_DH 9137+#define pk11_token_relogin pk11ca_token_relogin 9138+#define pFuncList pk11ca_pFuncList 9139+#define pk11_pin pk11ca_pin 9140+#define ENGINE_load_pk11 ENGINE_load_pk11ca 9141Index: openssl/crypto/engine/hw_pk11so.c 9142diff -u /dev/null openssl/crypto/engine/hw_pk11so.c:1.3.4.2 9143--- /dev/null Mon Jan 16 18:53:42 2012 9144+++ openssl/crypto/engine/hw_pk11so.c Thu Jun 16 12:31:35 2011 9145@@ -0,0 +1,1745 @@ 9146+/* 9147+ * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 9148+ * Use is subject to license terms. 9149+ */ 9150+ 9151+/* crypto/engine/hw_pk11.c */ 9152+/* 9153+ * This product includes software developed by the OpenSSL Project for 9154+ * use in the OpenSSL Toolkit (http://www.openssl.org/). 9155+ * 9156+ * This project also referenced hw_pkcs11-0.9.7b.patch written by 9157+ * Afchine Madjlessi. 9158+ */ 9159+/* 9160+ * ==================================================================== 9161+ * Copyright (c) 2000-2001 The OpenSSL Project. All rights reserved. 9162+ * 9163+ * Redistribution and use in source and binary forms, with or without 9164+ * modification, are permitted provided that the following conditions 9165+ * are met: 9166+ * 9167+ * 1. Redistributions of source code must retain the above copyright 9168+ * notice, this list of conditions and the following disclaimer. 9169+ * 9170+ * 2. Redistributions in binary form must reproduce the above copyright 9171+ * notice, this list of conditions and the following disclaimer in 9172+ * the documentation and/or other materials provided with the 9173+ * distribution. 9174+ * 9175+ * 3. All advertising materials mentioning features or use of this 9176+ * software must display the following acknowledgment: 9177+ * "This product includes software developed by the OpenSSL Project 9178+ * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 9179+ * 9180+ * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 9181+ * endorse or promote products derived from this software without 9182+ * prior written permission. For written permission, please contact 9183+ * licensing@OpenSSL.org. 9184+ * 9185+ * 5. Products derived from this software may not be called "OpenSSL" 9186+ * nor may "OpenSSL" appear in their names without prior written 9187+ * permission of the OpenSSL Project. 9188+ * 9189+ * 6. Redistributions of any form whatsoever must retain the following 9190+ * acknowledgment: 9191+ * "This product includes software developed by the OpenSSL Project 9192+ * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 9193+ * 9194+ * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 9195+ * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 9196+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 9197+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 9198+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 9199+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 9200+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 9201+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 9202+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 9203+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 9204+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 9205+ * OF THE POSSIBILITY OF SUCH DAMAGE. 9206+ * ==================================================================== 9207+ * 9208+ * This product includes cryptographic software written by Eric Young 9209+ * (eay@cryptsoft.com). This product includes software written by Tim 9210+ * Hudson (tjh@cryptsoft.com). 9211+ * 9212+ */ 9213+ 9214+/* Modified to keep only RNG and RSA Sign */ 9215+ 9216+#ifdef OPENSSL_NO_RSA 9217+#error RSA is disabled 9218+#endif 9219+ 9220+#include <stdio.h> 9221+#include <stdlib.h> 9222+#include <string.h> 9223+#include <sys/types.h> 9224+ 9225+#include <openssl/e_os2.h> 9226+#include <openssl/crypto.h> 9227+#include <cryptlib.h> 9228+#include <openssl/engine.h> 9229+#include <openssl/dso.h> 9230+#include <openssl/err.h> 9231+#include <openssl/bn.h> 9232+#include <openssl/md5.h> 9233+#include <openssl/pem.h> 9234+#include <openssl/rsa.h> 9235+#include <openssl/rand.h> 9236+#include <openssl/objects.h> 9237+#include <openssl/x509.h> 9238+ 9239+#ifdef OPENSSL_SYS_WIN32 9240+typedef int pid_t; 9241+#define getpid() GetCurrentProcessId() 9242+#define NOPTHREADS 9243+#ifndef NULL_PTR 9244+#define NULL_PTR NULL 9245+#endif 9246+#define CK_DEFINE_FUNCTION(returnType, name) \ 9247+ returnType __declspec(dllexport) name 9248+#define CK_DECLARE_FUNCTION(returnType, name) \ 9249+ returnType __declspec(dllimport) name 9250+#define CK_DECLARE_FUNCTION_POINTER(returnType, name) \ 9251+ returnType __declspec(dllimport) (* name) 9252+#else 9253+#include <signal.h> 9254+#include <unistd.h> 9255+#include <dlfcn.h> 9256+#endif 9257+ 9258+#ifndef NOPTHREADS 9259+#include <pthread.h> 9260+#endif 9261+ 9262+#ifndef OPENSSL_NO_HW 9263+#ifndef OPENSSL_NO_HW_PK11 9264+#ifndef OPENSSL_NO_HW_PK11SO 9265+ 9266+/* label for debug messages printed on stderr */ 9267+#define PK11_DBG "PKCS#11 ENGINE DEBUG" 9268+/* prints a lot of debug messages on stderr about slot selection process */ 9269+/*#undef DEBUG_SLOT_SELECTION */ 9270+ 9271+#ifndef OPENSSL_NO_DSA 9272+#define OPENSSL_NO_DSA 9273+#endif 9274+#ifndef OPENSSL_NO_DH 9275+#define OPENSSL_NO_DH 9276+#endif 9277+ 9278+#ifdef OPENSSL_SYS_WIN32 9279+#pragma pack(push, cryptoki, 1) 9280+#include "cryptoki.h" 9281+#include "pkcs11.h" 9282+#pragma pack(pop, cryptoki) 9283+#else 9284+#include "cryptoki.h" 9285+#include "pkcs11.h" 9286+#endif 9287+#include "hw_pk11so.h" 9288+#include "hw_pk11_err.c" 9289+ 9290+/* 9291+ * We use this lock to prevent multiple C_Login()s, guard getpassphrase(), 9292+ * uri_struct manipulation, and static token info. All of that is used by the 9293+ * RSA keys by reference feature. 9294+ */ 9295+#ifndef NOPTHREADS 9296+pthread_mutex_t *token_lock; 9297+#endif 9298+ 9299+/* PKCS#11 session caches and their locks for all operation types */ 9300+static PK11_CACHE session_cache[OP_MAX]; 9301+ 9302+/* 9303+ * We cache the flags so that we do not have to run C_GetTokenInfo() again when 9304+ * logging into the token. 9305+ */ 9306+CK_FLAGS pubkey_token_flags; 9307+ 9308+/* 9309+ * As stated in v2.20, 11.7 Object Management Function, in section for 9310+ * C_FindObjectsInit(), at most one search operation may be active at a given 9311+ * time in a given session. Therefore, C_Find{,Init,Final}Objects() should be 9312+ * grouped together to form one atomic search operation. This is already 9313+ * ensured by the property of unique PKCS#11 session handle used for each 9314+ * PK11_SESSION object. 9315+ * 9316+ * This is however not the biggest concern - maintaining consistency of the 9317+ * underlying object store is more important. The same section of the spec also 9318+ * says that one thread can be in the middle of a search operation while another 9319+ * thread destroys the object matching the search template which would result in 9320+ * invalid handle returned from the search operation. 9321+ * 9322+ * Hence, the following locks are used for both protection of the object stores. 9323+ * They are also used for active list protection. 9324+ */ 9325+#ifndef NOPTHREADS 9326+pthread_mutex_t *find_lock[OP_MAX] = { NULL }; 9327+#endif 9328+ 9329+/* 9330+ * lists of asymmetric key handles which are active (referenced by at least one 9331+ * PK11_SESSION structure, either held by a thread or present in free_session 9332+ * list) for given algorithm type 9333+ */ 9334+PK11_active *active_list[OP_MAX] = { NULL }; 9335+ 9336+/* 9337+ * Create all secret key objects in a global session so that they are available 9338+ * to use for other sessions. These other sessions may be opened or closed 9339+ * without losing the secret key objects. 9340+ */ 9341+static CK_SESSION_HANDLE global_session = CK_INVALID_HANDLE; 9342+ 9343+/* ENGINE level stuff */ 9344+static int pk11_init(ENGINE *e); 9345+static int pk11_library_init(ENGINE *e); 9346+static int pk11_finish(ENGINE *e); 9347+static int pk11_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void)); 9348+static int pk11_destroy(ENGINE *e); 9349+ 9350+/* RAND stuff */ 9351+static void pk11_rand_seed(const void *buf, int num); 9352+static void pk11_rand_add(const void *buf, int num, double add_entropy); 9353+static void pk11_rand_cleanup(void); 9354+static int pk11_rand_bytes(unsigned char *buf, int num); 9355+static int pk11_rand_status(void); 9356+ 9357+/* These functions are also used in other files */ 9358+PK11_SESSION *pk11_get_session(PK11_OPTYPE optype); 9359+void pk11_return_session(PK11_SESSION *sp, PK11_OPTYPE optype); 9360+ 9361+/* active list manipulation functions used in this file */ 9362+extern int pk11_active_delete(CK_OBJECT_HANDLE h, PK11_OPTYPE type); 9363+extern void pk11_free_active_list(PK11_OPTYPE type); 9364+ 9365+int pk11_destroy_rsa_key_objects(PK11_SESSION *session); 9366+int pk11_destroy_rsa_object_pub(PK11_SESSION *sp, CK_BBOOL uselock); 9367+int pk11_destroy_rsa_object_priv(PK11_SESSION *sp, CK_BBOOL uselock); 9368+ 9369+/* Local helper functions */ 9370+static int pk11_free_all_sessions(void); 9371+static int pk11_free_session_list(PK11_OPTYPE optype); 9372+static int pk11_setup_session(PK11_SESSION *sp, PK11_OPTYPE optype); 9373+static int pk11_destroy_object(CK_SESSION_HANDLE session, CK_OBJECT_HANDLE oh, 9374+ CK_BBOOL persistent); 9375+static const char *get_PK11_LIBNAME(void); 9376+static void free_PK11_LIBNAME(void); 9377+static long set_PK11_LIBNAME(const char *name); 9378+ 9379+static int pk11_choose_slots(int *any_slot_found); 9380+ 9381+static int pk11_init_all_locks(void); 9382+static void pk11_free_all_locks(void); 9383+ 9384+#define TRY_OBJ_DESTROY(sp, obj_hdl, retval, uselock, alg_type, priv) \ 9385+ { \ 9386+ if (uselock) \ 9387+ LOCK_OBJSTORE(alg_type); \ 9388+ if (pk11_active_delete(obj_hdl, alg_type) == 1) \ 9389+ { \ 9390+ retval = pk11_destroy_object(sp->session, obj_hdl, \ 9391+ priv ? sp->priv_persistent : sp->pub_persistent); \ 9392+ } \ 9393+ if (uselock) \ 9394+ UNLOCK_OBJSTORE(alg_type); \ 9395+ } 9396+ 9397+static CK_BBOOL pk11_have_rsa = CK_FALSE; 9398+static CK_BBOOL pk11_have_random = CK_FALSE; 9399+ 9400+/* 9401+ * Initialization function. Sets up various PKCS#11 library components. 9402+ * The definitions for control commands specific to this engine 9403+ */ 9404+#define PK11_CMD_SO_PATH ENGINE_CMD_BASE 9405+#define PK11_CMD_PIN (ENGINE_CMD_BASE+1) 9406+#define PK11_CMD_SLOT (ENGINE_CMD_BASE+2) 9407+static const ENGINE_CMD_DEFN pk11_cmd_defns[] = 9408+ { 9409+ { 9410+ PK11_CMD_SO_PATH, 9411+ "SO_PATH", 9412+ "Specifies the path to the 'pkcs#11' shared library", 9413+ ENGINE_CMD_FLAG_STRING 9414+ }, 9415+ { 9416+ PK11_CMD_PIN, 9417+ "PIN", 9418+ "Specifies the pin code", 9419+ ENGINE_CMD_FLAG_STRING 9420+ }, 9421+ { 9422+ PK11_CMD_SLOT, 9423+ "SLOT", 9424+ "Specifies the slot (default is auto select)", 9425+ ENGINE_CMD_FLAG_NUMERIC, 9426+ }, 9427+ {0, NULL, NULL, 0} 9428+ }; 9429+ 9430+ 9431+static RAND_METHOD pk11_random = 9432+ { 9433+ pk11_rand_seed, 9434+ pk11_rand_bytes, 9435+ pk11_rand_cleanup, 9436+ pk11_rand_add, 9437+ pk11_rand_bytes, 9438+ pk11_rand_status 9439+ }; 9440+ 9441+ 9442+/* Constants used when creating the ENGINE */ 9443+#ifdef OPENSSL_NO_HW_PK11CA 9444+#error "can't load both crypto-accelerator and sign-only PKCS#11 engines" 9445+#endif 9446+static const char *engine_pk11_id = "pkcs11"; 9447+static const char *engine_pk11_name = "PKCS #11 engine support (sign only)"; 9448+ 9449+CK_FUNCTION_LIST_PTR pFuncList = NULL; 9450+static const char PK11_GET_FUNCTION_LIST[] = "C_GetFunctionList"; 9451+ 9452+/* 9453+ * This is a static string constant for the DSO file name and the function 9454+ * symbol names to bind to. We set it in the Configure script based on whether 9455+ * this is 32 or 64 bit build. 9456+ */ 9457+static const char def_PK11_LIBNAME[] = PK11_LIB_LOCATION; 9458+ 9459+/* Needed in hw_pk11_pub.c as well so that's why it is not static. */ 9460+CK_SLOT_ID pubkey_SLOTID = 0; 9461+static CK_SLOT_ID rand_SLOTID = 0; 9462+static CK_SLOT_ID SLOTID = 0; 9463+char *pk11_pin = NULL; 9464+static CK_BBOOL pk11_library_initialized = FALSE; 9465+static CK_BBOOL pk11_atfork_initialized = FALSE; 9466+static int pk11_pid = 0; 9467+ 9468+static DSO *pk11_dso = NULL; 9469+ 9470+/* allocate and initialize all locks used by the engine itself */ 9471+static int pk11_init_all_locks(void) 9472+ { 9473+#ifndef NOPTHREADS 9474+ int type; 9475+ 9476+ if ((token_lock = OPENSSL_malloc(sizeof (pthread_mutex_t))) == NULL) 9477+ goto malloc_err; 9478+ (void) pthread_mutex_init(token_lock, NULL); 9479+ 9480+ find_lock[OP_RSA] = OPENSSL_malloc(sizeof (pthread_mutex_t)); 9481+ if (find_lock[OP_RSA] == NULL) 9482+ goto malloc_err; 9483+ (void) pthread_mutex_init(find_lock[OP_RSA], NULL); 9484+ 9485+ for (type = 0; type < OP_MAX; type++) 9486+ { 9487+ session_cache[type].lock = 9488+ OPENSSL_malloc(sizeof (pthread_mutex_t)); 9489+ if (session_cache[type].lock == NULL) 9490+ goto malloc_err; 9491+ (void) pthread_mutex_init(session_cache[type].lock, NULL); 9492+ } 9493+ 9494+ return (1); 9495+ 9496+malloc_err: 9497+ pk11_free_all_locks(); 9498+ PK11err(PK11_F_INIT_ALL_LOCKS, PK11_R_MALLOC_FAILURE); 9499+ return (0); 9500+#else 9501+ return (1); 9502+#endif 9503+ } 9504+ 9505+static void pk11_free_all_locks(void) 9506+ { 9507+#ifndef NOPTHREADS 9508+ int type; 9509+ 9510+ if (find_lock[OP_RSA] != NULL) 9511+ { 9512+ (void) pthread_mutex_destroy(find_lock[OP_RSA]); 9513+ OPENSSL_free(find_lock[OP_RSA]); 9514+ find_lock[OP_RSA] = NULL; 9515+ } 9516+ 9517+ for (type = 0; type < OP_MAX; type++) 9518+ { 9519+ if (session_cache[type].lock != NULL) 9520+ { 9521+ (void) pthread_mutex_destroy(session_cache[type].lock); 9522+ OPENSSL_free(session_cache[type].lock); 9523+ session_cache[type].lock = NULL; 9524+ } 9525+ } 9526+#endif 9527+ } 9528+ 9529+/* 9530+ * This internal function is used by ENGINE_pk11() and "dynamic" ENGINE support. 9531+ */ 9532+static int bind_pk11(ENGINE *e) 9533+ { 9534+ if (!pk11_library_initialized) 9535+ if (!pk11_library_init(e)) 9536+ return (0); 9537+ 9538+ if (!ENGINE_set_id(e, engine_pk11_id) || 9539+ !ENGINE_set_name(e, engine_pk11_name)) 9540+ return (0); 9541+ 9542+ if (pk11_have_rsa == CK_TRUE) 9543+ { 9544+ if (!ENGINE_set_RSA(e, PK11_RSA()) || 9545+ !ENGINE_set_load_privkey_function(e, pk11_load_privkey) || 9546+ !ENGINE_set_load_pubkey_function(e, pk11_load_pubkey)) 9547+ return (0); 9548+#ifdef DEBUG_SLOT_SELECTION 9549+ fprintf(stderr, "%s: registered RSA\n", PK11_DBG); 9550+#endif /* DEBUG_SLOT_SELECTION */ 9551+ } 9552+ 9553+ if (pk11_have_random) 9554+ { 9555+ if (!ENGINE_set_RAND(e, &pk11_random)) 9556+ return (0); 9557+#ifdef DEBUG_SLOT_SELECTION 9558+ fprintf(stderr, "%s: registered random\n", PK11_DBG); 9559+#endif /* DEBUG_SLOT_SELECTION */ 9560+ } 9561+ if (!ENGINE_set_init_function(e, pk11_init) || 9562+ !ENGINE_set_destroy_function(e, pk11_destroy) || 9563+ !ENGINE_set_finish_function(e, pk11_finish) || 9564+ !ENGINE_set_ctrl_function(e, pk11_ctrl) || 9565+ !ENGINE_set_cmd_defns(e, pk11_cmd_defns)) 9566+ return (0); 9567+ 9568+ /* Ensure the pk11 error handling is set up */ 9569+ ERR_load_pk11_strings(); 9570+ 9571+ return (1); 9572+ } 9573+ 9574+/* Dynamic engine support is disabled at a higher level for Solaris */ 9575+#ifdef ENGINE_DYNAMIC_SUPPORT 9576+#error "dynamic engine not supported" 9577+static int bind_helper(ENGINE *e, const char *id) 9578+ { 9579+ if (id && (strcmp(id, engine_pk11_id) != 0)) 9580+ return (0); 9581+ 9582+ if (!bind_pk11(e)) 9583+ return (0); 9584+ 9585+ return (1); 9586+ } 9587+ 9588+IMPLEMENT_DYNAMIC_CHECK_FN() 9589+IMPLEMENT_DYNAMIC_BIND_FN(bind_helper) 9590+ 9591+#else 9592+static ENGINE *engine_pk11(void) 9593+ { 9594+ ENGINE *ret = ENGINE_new(); 9595+ 9596+ if (!ret) 9597+ return (NULL); 9598+ 9599+ if (!bind_pk11(ret)) 9600+ { 9601+ ENGINE_free(ret); 9602+ return (NULL); 9603+ } 9604+ 9605+ return (ret); 9606+ } 9607+ 9608+void 9609+ENGINE_load_pk11(void) 9610+ { 9611+ ENGINE *e_pk11 = NULL; 9612+ 9613+ /* 9614+ * Do not use dynamic PKCS#11 library on Solaris due to 9615+ * security reasons. We will link it in statically. 9616+ */ 9617+ /* Attempt to load PKCS#11 library */ 9618+ if (!pk11_dso) 9619+ pk11_dso = DSO_load(NULL, get_PK11_LIBNAME(), NULL, 0); 9620+ 9621+ if (pk11_dso == NULL) 9622+ { 9623+ PK11err(PK11_F_LOAD, PK11_R_DSO_FAILURE); 9624+ return; 9625+ } 9626+ 9627+ e_pk11 = engine_pk11(); 9628+ if (!e_pk11) 9629+ { 9630+ DSO_free(pk11_dso); 9631+ pk11_dso = NULL; 9632+ return; 9633+ } 9634+ 9635+ /* 9636+ * At this point, the pk11 shared library is either dynamically 9637+ * loaded or statically linked in. So, initialize the pk11 9638+ * library before calling ENGINE_set_default since the latter 9639+ * needs cipher and digest algorithm information 9640+ */ 9641+ if (!pk11_library_init(e_pk11)) 9642+ { 9643+ DSO_free(pk11_dso); 9644+ pk11_dso = NULL; 9645+ ENGINE_free(e_pk11); 9646+ return; 9647+ } 9648+ 9649+ ENGINE_add(e_pk11); 9650+ 9651+ ENGINE_free(e_pk11); 9652+ ERR_clear_error(); 9653+ } 9654+#endif /* ENGINE_DYNAMIC_SUPPORT */ 9655+ 9656+/* 9657+ * These are the static string constants for the DSO file name and 9658+ * the function symbol names to bind to. 9659+ */ 9660+static const char *PK11_LIBNAME = NULL; 9661+ 9662+static const char *get_PK11_LIBNAME(void) 9663+ { 9664+ if (PK11_LIBNAME) 9665+ return (PK11_LIBNAME); 9666+ 9667+ return (def_PK11_LIBNAME); 9668+ } 9669+ 9670+static void free_PK11_LIBNAME(void) 9671+ { 9672+ if (PK11_LIBNAME) 9673+ OPENSSL_free((void*)PK11_LIBNAME); 9674+ 9675+ PK11_LIBNAME = NULL; 9676+ } 9677+ 9678+static long set_PK11_LIBNAME(const char *name) 9679+ { 9680+ free_PK11_LIBNAME(); 9681+ 9682+ return ((PK11_LIBNAME = BUF_strdup(name)) != NULL ? 1 : 0); 9683+ } 9684+ 9685+/* acquire all engine specific mutexes before fork */ 9686+static void pk11_fork_prepare(void) 9687+ { 9688+#ifndef NOPTHREADS 9689+ int i; 9690+ 9691+ if (!pk11_library_initialized) 9692+ return; 9693+ 9694+ LOCK_OBJSTORE(OP_RSA); 9695+ (void) pthread_mutex_lock(token_lock); 9696+ for (i = 0; i < OP_MAX; i++) 9697+ { 9698+ (void) pthread_mutex_lock(session_cache[i].lock); 9699+ } 9700+#endif 9701+ } 9702+ 9703+/* release all engine specific mutexes */ 9704+static void pk11_fork_parent(void) 9705+ { 9706+#ifndef NOPTHREADS 9707+ int i; 9708+ 9709+ if (!pk11_library_initialized) 9710+ return; 9711+ 9712+ for (i = OP_MAX - 1; i >= 0; i--) 9713+ { 9714+ (void) pthread_mutex_unlock(session_cache[i].lock); 9715+ } 9716+ UNLOCK_OBJSTORE(OP_RSA); 9717+ (void) pthread_mutex_unlock(token_lock); 9718+#endif 9719+ } 9720+ 9721+/* 9722+ * same situation as in parent - we need to unlock all locks to make them 9723+ * accessible to all threads. 9724+ */ 9725+static void pk11_fork_child(void) 9726+ { 9727+#ifndef NOPTHREADS 9728+ int i; 9729+ 9730+ if (!pk11_library_initialized) 9731+ return; 9732+ 9733+ for (i = OP_MAX - 1; i >= 0; i--) 9734+ { 9735+ (void) pthread_mutex_unlock(session_cache[i].lock); 9736+ } 9737+ UNLOCK_OBJSTORE(OP_RSA); 9738+ (void) pthread_mutex_unlock(token_lock); 9739+#endif 9740+ } 9741+ 9742+/* Initialization function for the pk11 engine */ 9743+static int pk11_init(ENGINE *e) 9744+{ 9745+ return (pk11_library_init(e)); 9746+} 9747+ 9748+static CK_C_INITIALIZE_ARGS pk11_init_args = 9749+ { 9750+ NULL_PTR, /* CreateMutex */ 9751+ NULL_PTR, /* DestroyMutex */ 9752+ NULL_PTR, /* LockMutex */ 9753+ NULL_PTR, /* UnlockMutex */ 9754+ CKF_OS_LOCKING_OK, /* flags */ 9755+ NULL_PTR, /* pReserved */ 9756+ }; 9757+ 9758+/* 9759+ * Initialization function. Sets up various PKCS#11 library components. 9760+ * It selects a slot based on predefined critiera. In the process, it also 9761+ * count how many ciphers and digests to support. Since the cipher and 9762+ * digest information is needed when setting default engine, this function 9763+ * needs to be called before calling ENGINE_set_default. 9764+ */ 9765+/* ARGSUSED */ 9766+static int pk11_library_init(ENGINE *e) 9767+ { 9768+ CK_C_GetFunctionList p; 9769+ CK_RV rv = CKR_OK; 9770+ CK_INFO info; 9771+ int any_slot_found; 9772+ int i; 9773+#ifndef OPENSSL_SYS_WIN32 9774+ struct sigaction sigint_act, sigterm_act, sighup_act; 9775+#endif 9776+ 9777+ /* 9778+ * pk11_library_initialized is set to 0 in pk11_finish() which 9779+ * is called from ENGINE_finish(). However, if there is still 9780+ * at least one existing functional reference to the engine 9781+ * (see engine(3) for more information), pk11_finish() is 9782+ * skipped. For example, this can happen if an application 9783+ * forgets to clear one cipher context. In case of a fork() 9784+ * when the application is finishing the engine so that it can 9785+ * be reinitialized in the child, forgotten functional 9786+ * reference causes pk11_library_initialized to stay 1. In 9787+ * that case we need the PID check so that we properly 9788+ * initialize the engine again. 9789+ */ 9790+ if (pk11_library_initialized) 9791+ { 9792+ if (pk11_pid == getpid()) 9793+ { 9794+ return (1); 9795+ } 9796+ else 9797+ { 9798+ global_session = CK_INVALID_HANDLE; 9799+ /* 9800+ * free the locks first to prevent memory leak in case 9801+ * the application calls fork() without finishing the 9802+ * engine first. 9803+ */ 9804+ pk11_free_all_locks(); 9805+ } 9806+ } 9807+ 9808+ if (pk11_dso == NULL) 9809+ { 9810+ PK11err(PK11_F_LIBRARY_INIT, PK11_R_DSO_FAILURE); 9811+ goto err; 9812+ } 9813+ 9814+ /* get the C_GetFunctionList function from the loaded library */ 9815+ p = (CK_C_GetFunctionList)DSO_bind_func(pk11_dso, 9816+ PK11_GET_FUNCTION_LIST); 9817+ if (!p) 9818+ { 9819+ PK11err(PK11_F_LIBRARY_INIT, PK11_R_DSO_FAILURE); 9820+ goto err; 9821+ } 9822+ 9823+ /* get the full function list from the loaded library */ 9824+ rv = p(&pFuncList); 9825+ if (rv != CKR_OK) 9826+ { 9827+ PK11err_add_data(PK11_F_LIBRARY_INIT, PK11_R_DSO_FAILURE, rv); 9828+ goto err; 9829+ } 9830+ 9831+#ifndef OPENSSL_SYS_WIN32 9832+ /* Not all PKCS#11 library are signal safe! */ 9833+ 9834+ (void) memset(&sigint_act, 0, sizeof(sigint_act)); 9835+ (void) memset(&sigterm_act, 0, sizeof(sigterm_act)); 9836+ (void) memset(&sighup_act, 0, sizeof(sighup_act)); 9837+ (void) sigaction(SIGINT, NULL, &sigint_act); 9838+ (void) sigaction(SIGTERM, NULL, &sigterm_act); 9839+ (void) sigaction(SIGHUP, NULL, &sighup_act); 9840+#endif 9841+ rv = pFuncList->C_Initialize((CK_VOID_PTR)&pk11_init_args); 9842+#ifndef OPENSSL_SYS_WIN32 9843+ (void) sigaction(SIGINT, &sigint_act, NULL); 9844+ (void) sigaction(SIGTERM, &sigterm_act, NULL); 9845+ (void) sigaction(SIGHUP, &sighup_act, NULL); 9846+#endif 9847+ if ((rv != CKR_OK) && (rv != CKR_CRYPTOKI_ALREADY_INITIALIZED)) 9848+ { 9849+ PK11err_add_data(PK11_F_LIBRARY_INIT, PK11_R_INITIALIZE, rv); 9850+ goto err; 9851+ } 9852+ 9853+ rv = pFuncList->C_GetInfo(&info); 9854+ if (rv != CKR_OK) 9855+ { 9856+ PK11err_add_data(PK11_F_LIBRARY_INIT, PK11_R_GETINFO, rv); 9857+ goto err; 9858+ } 9859+ 9860+ if (pk11_choose_slots(&any_slot_found) == 0) 9861+ goto err; 9862+ 9863+ /* 9864+ * The library we use, set in def_PK11_LIBNAME, may not offer any 9865+ * slot(s). In that case, we must not proceed but we must not return an 9866+ * error. The reason is that applications that try to set up the PKCS#11 9867+ * engine don't exit on error during the engine initialization just 9868+ * because no slot was present. 9869+ */ 9870+ if (any_slot_found == 0) 9871+ return (1); 9872+ 9873+ if (global_session == CK_INVALID_HANDLE) 9874+ { 9875+ /* Open the global_session for the new process */ 9876+ rv = pFuncList->C_OpenSession(SLOTID, CKF_SERIAL_SESSION, 9877+ NULL_PTR, NULL_PTR, &global_session); 9878+ if (rv != CKR_OK) 9879+ { 9880+ PK11err_add_data(PK11_F_LIBRARY_INIT, 9881+ PK11_R_OPENSESSION, rv); 9882+ goto err; 9883+ } 9884+ } 9885+ 9886+ pk11_library_initialized = TRUE; 9887+ pk11_pid = getpid(); 9888+ /* 9889+ * if initialization of the locks fails pk11_init_all_locks() 9890+ * will do the cleanup. 9891+ */ 9892+ if (!pk11_init_all_locks()) 9893+ goto err; 9894+ for (i = 0; i < OP_MAX; i++) 9895+ session_cache[i].head = NULL; 9896+ /* 9897+ * initialize active lists. We only use active lists 9898+ * for asymmetric ciphers. 9899+ */ 9900+ for (i = 0; i < OP_MAX; i++) 9901+ active_list[i] = NULL; 9902+ 9903+#ifndef NOPTHREADS 9904+ if (!pk11_atfork_initialized) 9905+ { 9906+ if (pthread_atfork(pk11_fork_prepare, pk11_fork_parent, 9907+ pk11_fork_child) != 0) 9908+ { 9909+ PK11err(PK11_F_LIBRARY_INIT, PK11_R_ATFORK_FAILED); 9910+ goto err; 9911+ } 9912+ pk11_atfork_initialized = TRUE; 9913+ } 9914+#endif 9915+ 9916+ return (1); 9917+ 9918+err: 9919+ return (0); 9920+ } 9921+ 9922+/* Destructor (complements the "ENGINE_pk11()" constructor) */ 9923+/* ARGSUSED */ 9924+static int pk11_destroy(ENGINE *e) 9925+ { 9926+ free_PK11_LIBNAME(); 9927+ ERR_unload_pk11_strings(); 9928+ if (pk11_pin) { 9929+ memset(pk11_pin, 0, strlen(pk11_pin)); 9930+ OPENSSL_free((void*)pk11_pin); 9931+ } 9932+ pk11_pin = NULL; 9933+ return (1); 9934+ } 9935+ 9936+/* 9937+ * Termination function to clean up the session, the token, and the pk11 9938+ * library. 9939+ */ 9940+/* ARGSUSED */ 9941+static int pk11_finish(ENGINE *e) 9942+ { 9943+ int i; 9944+ 9945+ if (pk11_pin) { 9946+ memset(pk11_pin, 0, strlen(pk11_pin)); 9947+ OPENSSL_free((void*)pk11_pin); 9948+ } 9949+ pk11_pin = NULL; 9950+ 9951+ if (pk11_dso == NULL) 9952+ { 9953+ PK11err(PK11_F_FINISH, PK11_R_NOT_LOADED); 9954+ goto err; 9955+ } 9956+ 9957+ OPENSSL_assert(pFuncList != NULL); 9958+ 9959+ if (pk11_free_all_sessions() == 0) 9960+ goto err; 9961+ 9962+ /* free all active lists */ 9963+ for (i = 0; i < OP_MAX; i++) 9964+ pk11_free_active_list(i); 9965+ 9966+ pFuncList->C_CloseSession(global_session); 9967+ global_session = CK_INVALID_HANDLE; 9968+ 9969+ /* 9970+ * Since we are part of a library (libcrypto.so), calling this function 9971+ * may have side-effects. 9972+ */ 9973+#if 0 9974+ pFuncList->C_Finalize(NULL); 9975+#endif 9976+ 9977+ if (!DSO_free(pk11_dso)) 9978+ { 9979+ PK11err(PK11_F_FINISH, PK11_R_DSO_FAILURE); 9980+ goto err; 9981+ } 9982+ pk11_dso = NULL; 9983+ pFuncList = NULL; 9984+ pk11_library_initialized = FALSE; 9985+ pk11_pid = 0; 9986+ /* 9987+ * There is no way how to unregister atfork handlers (other than 9988+ * unloading the library) so we just free the locks. For this reason 9989+ * the atfork handlers check if the engine is initialized and bail out 9990+ * immediately if not. This is necessary in case a process finishes 9991+ * the engine before calling fork(). 9992+ */ 9993+ pk11_free_all_locks(); 9994+ 9995+ return (1); 9996+ 9997+err: 9998+ return (0); 9999+ } 10000+ 10001+/* Standard engine interface function to set the dynamic library path */ 10002+/* ARGSUSED */ 10003+static int pk11_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void)) 10004+ { 10005+ int initialized = ((pk11_dso == NULL) ? 0 : 1); 10006+ 10007+ switch (cmd) 10008+ { 10009+ case PK11_CMD_SO_PATH: 10010+ if (p == NULL) 10011+ { 10012+ PK11err(PK11_F_CTRL, ERR_R_PASSED_NULL_PARAMETER); 10013+ return (0); 10014+ } 10015+ 10016+ if (initialized) 10017+ { 10018+ PK11err(PK11_F_CTRL, PK11_R_ALREADY_LOADED); 10019+ return (0); 10020+ } 10021+ 10022+ return (set_PK11_LIBNAME((const char *)p)); 10023+ case PK11_CMD_PIN: 10024+ if (pk11_pin) { 10025+ memset(pk11_pin, 0, strlen(pk11_pin)); 10026+ OPENSSL_free((void*)pk11_pin); 10027+ } 10028+ pk11_pin = NULL; 10029+ 10030+ if (p == NULL) 10031+ { 10032+ PK11err(PK11_F_CTRL, ERR_R_PASSED_NULL_PARAMETER); 10033+ return (0); 10034+ } 10035+ 10036+ pk11_pin = BUF_strdup(p); 10037+ if (pk11_pin == NULL) 10038+ { 10039+ PK11err(PK11_F_GET_SESSION, PK11_R_MALLOC_FAILURE); 10040+ return (0); 10041+ } 10042+ return (1); 10043+ case PK11_CMD_SLOT: 10044+ SLOTID = (CK_SLOT_ID)i; 10045+#ifdef DEBUG_SLOT_SELECTION 10046+ fprintf(stderr, "%s: slot set\n", PK11_DBG); 10047+#endif 10048+ return (1); 10049+ default: 10050+ break; 10051+ } 10052+ 10053+ PK11err(PK11_F_CTRL, PK11_R_CTRL_COMMAND_NOT_IMPLEMENTED); 10054+ 10055+ return (0); 10056+ } 10057+ 10058+ 10059+/* Required function by the engine random interface. It does nothing here */ 10060+static void pk11_rand_cleanup(void) 10061+ { 10062+ return; 10063+ } 10064+ 10065+/* ARGSUSED */ 10066+static void pk11_rand_add(const void *buf, int num, double add) 10067+ { 10068+ PK11_SESSION *sp; 10069+ 10070+ if ((sp = pk11_get_session(OP_RAND)) == NULL) 10071+ return; 10072+ 10073+ /* 10074+ * Ignore any errors (e.g. CKR_RANDOM_SEED_NOT_SUPPORTED) since 10075+ * the calling functions do not care anyway 10076+ */ 10077+ pFuncList->C_SeedRandom(sp->session, (unsigned char *) buf, num); 10078+ pk11_return_session(sp, OP_RAND); 10079+ 10080+ return; 10081+ } 10082+ 10083+static void pk11_rand_seed(const void *buf, int num) 10084+ { 10085+ pk11_rand_add(buf, num, 0); 10086+ } 10087+ 10088+static int pk11_rand_bytes(unsigned char *buf, int num) 10089+ { 10090+ CK_RV rv; 10091+ PK11_SESSION *sp; 10092+ 10093+ if ((sp = pk11_get_session(OP_RAND)) == NULL) 10094+ return (0); 10095+ 10096+ rv = pFuncList->C_GenerateRandom(sp->session, buf, num); 10097+ if (rv != CKR_OK) 10098+ { 10099+ PK11err_add_data(PK11_F_RAND_BYTES, PK11_R_GENERATERANDOM, rv); 10100+ pk11_return_session(sp, OP_RAND); 10101+ return (0); 10102+ } 10103+ 10104+ pk11_return_session(sp, OP_RAND); 10105+ return (1); 10106+ } 10107+ 10108+/* Required function by the engine random interface. It does nothing here */ 10109+static int pk11_rand_status(void) 10110+ { 10111+ return (1); 10112+ } 10113+ 10114+/* Free all BIGNUM structures from PK11_SESSION. */ 10115+static void pk11_free_nums(PK11_SESSION *sp, PK11_OPTYPE optype) 10116+ { 10117+ switch (optype) 10118+ { 10119+ case OP_RSA: 10120+ if (sp->opdata_rsa_n_num != NULL) 10121+ { 10122+ BN_free(sp->opdata_rsa_n_num); 10123+ sp->opdata_rsa_n_num = NULL; 10124+ } 10125+ if (sp->opdata_rsa_e_num != NULL) 10126+ { 10127+ BN_free(sp->opdata_rsa_e_num); 10128+ sp->opdata_rsa_e_num = NULL; 10129+ } 10130+ if (sp->opdata_rsa_pn_num != NULL) 10131+ { 10132+ BN_free(sp->opdata_rsa_pn_num); 10133+ sp->opdata_rsa_pn_num = NULL; 10134+ } 10135+ if (sp->opdata_rsa_pe_num != NULL) 10136+ { 10137+ BN_free(sp->opdata_rsa_pe_num); 10138+ sp->opdata_rsa_pe_num = NULL; 10139+ } 10140+ if (sp->opdata_rsa_d_num != NULL) 10141+ { 10142+ BN_free(sp->opdata_rsa_d_num); 10143+ sp->opdata_rsa_d_num = NULL; 10144+ } 10145+ break; 10146+ default: 10147+ break; 10148+ } 10149+ } 10150+ 10151+/* 10152+ * Get new PK11_SESSION structure ready for use. Every process must have 10153+ * its own freelist of PK11_SESSION structures so handle fork() here 10154+ * by destroying the old and creating new freelist. 10155+ * The returned PK11_SESSION structure is disconnected from the freelist. 10156+ */ 10157+PK11_SESSION * 10158+pk11_get_session(PK11_OPTYPE optype) 10159+ { 10160+ PK11_SESSION *sp = NULL, *sp1, *freelist; 10161+#ifndef NOPTHREADS 10162+ pthread_mutex_t *freelist_lock = NULL; 10163+#endif 10164+ static pid_t pid = 0; 10165+ pid_t new_pid; 10166+ CK_RV rv; 10167+ 10168+ switch (optype) 10169+ { 10170+ case OP_RSA: 10171+ case OP_DSA: 10172+ case OP_DH: 10173+ case OP_RAND: 10174+ case OP_DIGEST: 10175+ case OP_CIPHER: 10176+#ifndef NOPTHREADS 10177+ freelist_lock = session_cache[optype].lock; 10178+#endif 10179+ break; 10180+ default: 10181+ PK11err(PK11_F_GET_SESSION, 10182+ PK11_R_INVALID_OPERATION_TYPE); 10183+ return (NULL); 10184+ } 10185+#ifndef NOPTHREADS 10186+ (void) pthread_mutex_lock(freelist_lock); 10187+#else 10188+ CRYPTO_w_lock(CRYPTO_LOCK_PK11_ENGINE); 10189+#endif 10190+ 10191+ /* 10192+ * Will use it to find out if we forked. We cannot use the PID field in 10193+ * the session structure because we could get a newly allocated session 10194+ * here, with no PID information. 10195+ */ 10196+ if (pid == 0) 10197+ pid = getpid(); 10198+ 10199+ freelist = session_cache[optype].head; 10200+ sp = freelist; 10201+ 10202+ /* 10203+ * If the free list is empty, allocate new unitialized (filled 10204+ * with zeroes) PK11_SESSION structure otherwise return first 10205+ * structure from the freelist. 10206+ */ 10207+ if (sp == NULL) 10208+ { 10209+ if ((sp = OPENSSL_malloc(sizeof (PK11_SESSION))) == NULL) 10210+ { 10211+ PK11err(PK11_F_GET_SESSION, 10212+ PK11_R_MALLOC_FAILURE); 10213+ goto err; 10214+ } 10215+ (void) memset(sp, 0, sizeof (PK11_SESSION)); 10216+ 10217+ /* 10218+ * It is a new session so it will look like a cache miss to the 10219+ * code below. So, we must not try to to destroy its members so 10220+ * mark them as unused. 10221+ */ 10222+ sp->opdata_rsa_priv_key = CK_INVALID_HANDLE; 10223+ sp->opdata_rsa_pub_key = CK_INVALID_HANDLE; 10224+ } 10225+ else 10226+ { 10227+ freelist = sp->next; 10228+ } 10229+ 10230+ /* 10231+ * Check whether we have forked. In that case, we must get rid of all 10232+ * inherited sessions and start allocating new ones. 10233+ */ 10234+ if (pid != (new_pid = getpid())) 10235+ { 10236+ pid = new_pid; 10237+ 10238+ /* 10239+ * We are a new process and thus need to free any inherited 10240+ * PK11_SESSION objects aside from the first session (sp) which 10241+ * is the only PK11_SESSION structure we will reuse (for the 10242+ * head of the list). 10243+ */ 10244+ while ((sp1 = freelist) != NULL) 10245+ { 10246+ freelist = sp1->next; 10247+ /* 10248+ * NOTE: we do not want to call pk11_free_all_sessions() 10249+ * here because it would close underlying PKCS#11 10250+ * sessions and destroy all objects. 10251+ */ 10252+ pk11_free_nums(sp1, optype); 10253+ OPENSSL_free(sp1); 10254+ } 10255+ 10256+ /* we have to free the active list as well. */ 10257+ pk11_free_active_list(optype); 10258+ 10259+ /* Initialize the process */ 10260+ rv = pFuncList->C_Initialize((CK_VOID_PTR)&pk11_init_args); 10261+ if ((rv != CKR_OK) && (rv != CKR_CRYPTOKI_ALREADY_INITIALIZED)) 10262+ { 10263+ PK11err_add_data(PK11_F_GET_SESSION, PK11_R_INITIALIZE, 10264+ rv); 10265+ OPENSSL_free(sp); 10266+ sp = NULL; 10267+ goto err; 10268+ } 10269+ 10270+ /* 10271+ * Choose slot here since the slot table is different on this 10272+ * process. If we are here then we must have found at least one 10273+ * usable slot before so we don't need to check any_slot_found. 10274+ * See pk11_library_init()'s usage of this function for more 10275+ * information. 10276+ */ 10277+ if (pk11_choose_slots(NULL) == 0) 10278+ goto err; 10279+ 10280+ /* Open the global_session for the new process */ 10281+ rv = pFuncList->C_OpenSession(SLOTID, CKF_SERIAL_SESSION, 10282+ NULL_PTR, NULL_PTR, &global_session); 10283+ if (rv != CKR_OK) 10284+ { 10285+ PK11err_add_data(PK11_F_GET_SESSION, PK11_R_OPENSESSION, 10286+ rv); 10287+ OPENSSL_free(sp); 10288+ sp = NULL; 10289+ goto err; 10290+ } 10291+ 10292+ /* 10293+ * It is an inherited session from our parent so it needs 10294+ * re-initialization. 10295+ */ 10296+ if (pk11_setup_session(sp, optype) == 0) 10297+ { 10298+ OPENSSL_free(sp); 10299+ sp = NULL; 10300+ goto err; 10301+ } 10302+ if (pk11_token_relogin(sp->session) == 0) 10303+ { 10304+ /* 10305+ * We will keep the session in the cache list and let 10306+ * the caller cope with the situation. 10307+ */ 10308+ freelist = sp; 10309+ sp = NULL; 10310+ goto err; 10311+ } 10312+ } 10313+ 10314+ if (sp->pid == 0) 10315+ { 10316+ /* It is a new session and needs initialization. */ 10317+ if (pk11_setup_session(sp, optype) == 0) 10318+ { 10319+ OPENSSL_free(sp); 10320+ sp = NULL; 10321+ } 10322+ } 10323+ 10324+ /* set new head for the list of PK11_SESSION objects */ 10325+ session_cache[optype].head = freelist; 10326+ 10327+err: 10328+ if (sp != NULL) 10329+ sp->next = NULL; 10330+ 10331+#ifndef NOPTHREADS 10332+ (void) pthread_mutex_unlock(freelist_lock); 10333+#else 10334+ CRYPTO_w_unlock(CRYPTO_LOCK_PK11_ENGINE); 10335+#endif 10336+ 10337+ return (sp); 10338+ } 10339+ 10340+ 10341+void 10342+pk11_return_session(PK11_SESSION *sp, PK11_OPTYPE optype) 10343+ { 10344+#ifndef NOPTHREADS 10345+ pthread_mutex_t *freelist_lock; 10346+#endif 10347+ PK11_SESSION *freelist; 10348+ 10349+ /* 10350+ * If this is a session from the parent it will be taken care of and 10351+ * freed in pk11_get_session() as part of the post-fork clean up the 10352+ * next time we will ask for a new session. 10353+ */ 10354+ if (sp == NULL || sp->pid != getpid()) 10355+ return; 10356+ 10357+ switch (optype) 10358+ { 10359+ case OP_RSA: 10360+ case OP_DSA: 10361+ case OP_DH: 10362+ case OP_RAND: 10363+ case OP_DIGEST: 10364+ case OP_CIPHER: 10365+#ifndef NOPTHREADS 10366+ freelist_lock = session_cache[optype].lock; 10367+#endif 10368+ break; 10369+ default: 10370+ PK11err(PK11_F_RETURN_SESSION, 10371+ PK11_R_INVALID_OPERATION_TYPE); 10372+ return; 10373+ } 10374+ 10375+#ifndef NOPTHREADS 10376+ (void) pthread_mutex_lock(freelist_lock); 10377+#else 10378+ CRYPTO_w_lock(CRYPTO_LOCK_PK11_ENGINE); 10379+#endif 10380+ freelist = session_cache[optype].head; 10381+ sp->next = freelist; 10382+ session_cache[optype].head = sp; 10383+#ifndef NOPTHREADS 10384+ (void) pthread_mutex_unlock(freelist_lock); 10385+#else 10386+ CRYPTO_w_unlock(CRYPTO_LOCK_PK11_ENGINE); 10387+#endif 10388+ } 10389+ 10390+ 10391+/* Destroy all objects. This function is called when the engine is finished */ 10392+static int pk11_free_all_sessions() 10393+ { 10394+ int ret = 1; 10395+ int type; 10396+ 10397+ (void) pk11_destroy_rsa_key_objects(NULL); 10398+ 10399+ /* 10400+ * We try to release as much as we can but any error means that we will 10401+ * return 0 on exit. 10402+ */ 10403+ for (type = 0; type < OP_MAX; type++) 10404+ { 10405+ if (pk11_free_session_list(type) == 0) 10406+ ret = 0; 10407+ } 10408+ 10409+ return (ret); 10410+ } 10411+ 10412+/* 10413+ * Destroy session structures from the linked list specified. Free as many 10414+ * sessions as possible but any failure in C_CloseSession() means that we 10415+ * return an error on return. 10416+ */ 10417+static int pk11_free_session_list(PK11_OPTYPE optype) 10418+ { 10419+ CK_RV rv; 10420+ PK11_SESSION *sp = NULL; 10421+ PK11_SESSION *freelist = NULL; 10422+ pid_t mypid = getpid(); 10423+#ifndef NOPTHREADS 10424+ pthread_mutex_t *freelist_lock; 10425+#endif 10426+ int ret = 1; 10427+ 10428+ switch (optype) 10429+ { 10430+ case OP_RSA: 10431+ case OP_DSA: 10432+ case OP_DH: 10433+ case OP_RAND: 10434+ case OP_DIGEST: 10435+ case OP_CIPHER: 10436+#ifndef NOPTHREADS 10437+ freelist_lock = session_cache[optype].lock; 10438+#endif 10439+ break; 10440+ default: 10441+ PK11err(PK11_F_FREE_ALL_SESSIONS, 10442+ PK11_R_INVALID_OPERATION_TYPE); 10443+ return (0); 10444+ } 10445+ 10446+#ifndef NOPTHREADS 10447+ (void) pthread_mutex_lock(freelist_lock); 10448+#else 10449+ CRYPTO_w_lock(CRYPTO_LOCK_PK11_ENGINE); 10450+#endif 10451+ freelist = session_cache[optype].head; 10452+ while ((sp = freelist) != NULL) 10453+ { 10454+ if (sp->session != CK_INVALID_HANDLE && sp->pid == mypid) 10455+ { 10456+ rv = pFuncList->C_CloseSession(sp->session); 10457+ if (rv != CKR_OK) 10458+ { 10459+ PK11err_add_data(PK11_F_FREE_ALL_SESSIONS, 10460+ PK11_R_CLOSESESSION, rv); 10461+ ret = 0; 10462+ } 10463+ } 10464+ freelist = sp->next; 10465+ pk11_free_nums(sp, optype); 10466+ OPENSSL_free(sp); 10467+ } 10468+ 10469+#ifndef NOPTHREADS 10470+ (void) pthread_mutex_unlock(freelist_lock); 10471+#else 10472+ CRYPTO_w_unlock(CRYPTO_LOCK_PK11_ENGINE); 10473+#endif 10474+ return (ret); 10475+ } 10476+ 10477+ 10478+static int 10479+pk11_setup_session(PK11_SESSION *sp, PK11_OPTYPE optype) 10480+ { 10481+ CK_RV rv; 10482+ CK_SLOT_ID myslot; 10483+ 10484+ switch (optype) 10485+ { 10486+ case OP_RSA: 10487+ myslot = pubkey_SLOTID; 10488+ break; 10489+ case OP_RAND: 10490+ myslot = rand_SLOTID; 10491+ break; 10492+ default: 10493+ PK11err(PK11_F_SETUP_SESSION, 10494+ PK11_R_INVALID_OPERATION_TYPE); 10495+ return (0); 10496+ } 10497+ 10498+ sp->session = CK_INVALID_HANDLE; 10499+#ifdef DEBUG_SLOT_SELECTION 10500+ fprintf(stderr, "%s: myslot=%d optype=%d\n", PK11_DBG, myslot, optype); 10501+#endif /* DEBUG_SLOT_SELECTION */ 10502+ rv = pFuncList->C_OpenSession(myslot, CKF_SERIAL_SESSION, 10503+ NULL_PTR, NULL_PTR, &sp->session); 10504+ if (rv == CKR_CRYPTOKI_NOT_INITIALIZED) 10505+ { 10506+ /* 10507+ * We are probably a child process so force the 10508+ * reinitialize of the session 10509+ */ 10510+ pk11_library_initialized = FALSE; 10511+ if (!pk11_library_init(NULL)) 10512+ return (0); 10513+ rv = pFuncList->C_OpenSession(myslot, CKF_SERIAL_SESSION, 10514+ NULL_PTR, NULL_PTR, &sp->session); 10515+ } 10516+ if (rv != CKR_OK) 10517+ { 10518+ PK11err_add_data(PK11_F_SETUP_SESSION, PK11_R_OPENSESSION, rv); 10519+ return (0); 10520+ } 10521+ 10522+ sp->pid = getpid(); 10523+ 10524+ if (optype == OP_RSA) 10525+ { 10526+ sp->opdata_rsa_pub_key = CK_INVALID_HANDLE; 10527+ sp->opdata_rsa_priv_key = CK_INVALID_HANDLE; 10528+ sp->opdata_rsa_pub = NULL; 10529+ sp->opdata_rsa_n_num = NULL; 10530+ sp->opdata_rsa_e_num = NULL; 10531+ sp->opdata_rsa_priv = NULL; 10532+ sp->opdata_rsa_pn_num = NULL; 10533+ sp->opdata_rsa_pe_num = NULL; 10534+ sp->opdata_rsa_d_num = NULL; 10535+ } 10536+ 10537+ /* 10538+ * We always initialize the session as containing a non-persistent 10539+ * object. The key load functions set it to persistent if that is so. 10540+ */ 10541+ sp->pub_persistent = CK_FALSE; 10542+ sp->priv_persistent = CK_FALSE; 10543+ return (1); 10544+ } 10545+ 10546+/* Destroy RSA public key from single session. */ 10547+int 10548+pk11_destroy_rsa_object_pub(PK11_SESSION *sp, CK_BBOOL uselock) 10549+ { 10550+ int ret = 0; 10551+ 10552+ if (sp->opdata_rsa_pub_key != CK_INVALID_HANDLE) 10553+ { 10554+ TRY_OBJ_DESTROY(sp, sp->opdata_rsa_pub_key, 10555+ ret, uselock, OP_RSA, CK_FALSE); 10556+ sp->opdata_rsa_pub_key = CK_INVALID_HANDLE; 10557+ sp->opdata_rsa_pub = NULL; 10558+ if (sp->opdata_rsa_n_num != NULL) 10559+ { 10560+ BN_free(sp->opdata_rsa_n_num); 10561+ sp->opdata_rsa_n_num = NULL; 10562+ } 10563+ if (sp->opdata_rsa_e_num != NULL) 10564+ { 10565+ BN_free(sp->opdata_rsa_e_num); 10566+ sp->opdata_rsa_e_num = NULL; 10567+ } 10568+ } 10569+ 10570+ return (ret); 10571+ } 10572+ 10573+/* Destroy RSA private key from single session. */ 10574+int 10575+pk11_destroy_rsa_object_priv(PK11_SESSION *sp, CK_BBOOL uselock) 10576+ { 10577+ int ret = 0; 10578+ 10579+ if (sp->opdata_rsa_priv_key != CK_INVALID_HANDLE) 10580+ { 10581+ TRY_OBJ_DESTROY(sp, sp->opdata_rsa_priv_key, 10582+ ret, uselock, OP_RSA, CK_TRUE); 10583+ sp->opdata_rsa_priv_key = CK_INVALID_HANDLE; 10584+ sp->opdata_rsa_priv = NULL; 10585+ if (sp->opdata_rsa_d_num != NULL) 10586+ { 10587+ BN_free(sp->opdata_rsa_d_num); 10588+ sp->opdata_rsa_d_num = NULL; 10589+ } 10590+ 10591+ /* 10592+ * For the RSA key by reference code, public components 'n'/'e' 10593+ * are the key components we use to check for the cache hit. We 10594+ * must free those as well. 10595+ */ 10596+ if (sp->opdata_rsa_pn_num != NULL) 10597+ { 10598+ BN_free(sp->opdata_rsa_pn_num); 10599+ sp->opdata_rsa_pn_num = NULL; 10600+ } 10601+ if (sp->opdata_rsa_pe_num != NULL) 10602+ { 10603+ BN_free(sp->opdata_rsa_pe_num); 10604+ sp->opdata_rsa_pe_num = NULL; 10605+ } 10606+ } 10607+ 10608+ return (ret); 10609+ } 10610+ 10611+/* 10612+ * Destroy RSA key object wrapper. If session is NULL, try to destroy all 10613+ * objects in the free list. 10614+ */ 10615+int 10616+pk11_destroy_rsa_key_objects(PK11_SESSION *session) 10617+ { 10618+ int ret = 1; 10619+ PK11_SESSION *sp = NULL; 10620+ PK11_SESSION *local_free_session; 10621+ CK_BBOOL uselock = TRUE; 10622+ 10623+ if (session != NULL) 10624+ local_free_session = session; 10625+ else 10626+ { 10627+#ifndef NOPTHREADS 10628+ (void) pthread_mutex_lock(session_cache[OP_RSA].lock); 10629+#else 10630+ CRYPTO_w_lock(CRYPTO_LOCK_PK11_ENGINE); 10631+#endif 10632+ local_free_session = session_cache[OP_RSA].head; 10633+ uselock = FALSE; 10634+ } 10635+ 10636+ /* 10637+ * go through the list of sessions and delete key objects 10638+ */ 10639+ while ((sp = local_free_session) != NULL) 10640+ { 10641+ local_free_session = sp->next; 10642+ 10643+ /* 10644+ * Do not terminate list traversal if one of the 10645+ * destroy operations fails. 10646+ */ 10647+ if (pk11_destroy_rsa_object_pub(sp, uselock) == 0) 10648+ { 10649+ ret = 0; 10650+ continue; 10651+ } 10652+ if (pk11_destroy_rsa_object_priv(sp, uselock) == 0) 10653+ { 10654+ ret = 0; 10655+ continue; 10656+ } 10657+ } 10658+ 10659+#ifndef NOPTHREADS 10660+ if (session == NULL) 10661+ (void) pthread_mutex_unlock(session_cache[OP_RSA].lock); 10662+#else 10663+ if (session == NULL) 10664+ CRYPTO_w_unlock(CRYPTO_LOCK_PK11_ENGINE); 10665+#endif 10666+ 10667+ return (ret); 10668+ } 10669+ 10670+static int 10671+pk11_destroy_object(CK_SESSION_HANDLE session, CK_OBJECT_HANDLE oh, 10672+ CK_BBOOL persistent) 10673+ { 10674+ CK_RV rv; 10675+ 10676+ /* 10677+ * We never try to destroy persistent objects which are the objects 10678+ * stored in the keystore. Also, we always use read-only sessions so 10679+ * C_DestroyObject() would be returning CKR_SESSION_READ_ONLY here. 10680+ */ 10681+ if (persistent == CK_TRUE) 10682+ return (1); 10683+ 10684+ rv = pFuncList->C_DestroyObject(session, oh); 10685+ if (rv != CKR_OK) 10686+ { 10687+ PK11err_add_data(PK11_F_DESTROY_OBJECT, PK11_R_DESTROYOBJECT, 10688+ rv); 10689+ return (0); 10690+ } 10691+ 10692+ return (1); 10693+ } 10694+ 10695+ 10696+/* 10697+ * Public key mechanisms optionally supported 10698+ * 10699+ * CKM_RSA_PKCS 10700+ * 10701+ * The first slot that supports at least one of those mechanisms is chosen as a 10702+ * public key slot. 10703+ * 10704+ * The output of this function is a set of global variables indicating which 10705+ * mechanisms from RSA, DSA, DH and RAND are present, and also two arrays of 10706+ * mechanisms, one for symmetric ciphers and one for digests. Also, 3 global 10707+ * variables carry information about which slot was chosen for (a) public key 10708+ * mechanisms, (b) random operations, and (c) symmetric ciphers and digests. 10709+ */ 10710+static int 10711+pk11_choose_slots(int *any_slot_found) 10712+ { 10713+ CK_SLOT_ID_PTR pSlotList = NULL_PTR; 10714+ CK_ULONG ulSlotCount = 0; 10715+ CK_MECHANISM_INFO mech_info; 10716+ CK_TOKEN_INFO token_info; 10717+ unsigned int i; 10718+ CK_RV rv; 10719+ CK_SLOT_ID best_slot_sofar = 0; 10720+ CK_BBOOL found_candidate_slot = CK_FALSE; 10721+ CK_SLOT_ID current_slot = 0; 10722+ 10723+ /* let's initialize the output parameter */ 10724+ if (any_slot_found != NULL) 10725+ *any_slot_found = 0; 10726+ 10727+ /* Get slot list for memory allocation */ 10728+ rv = pFuncList->C_GetSlotList(CK_FALSE, NULL_PTR, &ulSlotCount); 10729+ 10730+ if (rv != CKR_OK) 10731+ { 10732+ PK11err_add_data(PK11_F_CHOOSE_SLOT, PK11_R_GETSLOTLIST, rv); 10733+ return (0); 10734+ } 10735+ 10736+ /* it's not an error if we didn't find any providers */ 10737+ if (ulSlotCount == 0) 10738+ { 10739+#ifdef DEBUG_SLOT_SELECTION 10740+ fprintf(stderr, "%s: no crypto providers found\n", PK11_DBG); 10741+#endif /* DEBUG_SLOT_SELECTION */ 10742+ return (1); 10743+ } 10744+ 10745+ pSlotList = OPENSSL_malloc(ulSlotCount * sizeof (CK_SLOT_ID)); 10746+ 10747+ if (pSlotList == NULL) 10748+ { 10749+ PK11err(PK11_F_CHOOSE_SLOT, PK11_R_MALLOC_FAILURE); 10750+ return (0); 10751+ } 10752+ 10753+ /* Get the slot list for processing */ 10754+ rv = pFuncList->C_GetSlotList(CK_FALSE, pSlotList, &ulSlotCount); 10755+ if (rv != CKR_OK) 10756+ { 10757+ PK11err_add_data(PK11_F_CHOOSE_SLOT, PK11_R_GETSLOTLIST, rv); 10758+ OPENSSL_free(pSlotList); 10759+ return (0); 10760+ } 10761+ 10762+#ifdef DEBUG_SLOT_SELECTION 10763+ fprintf(stderr, "%s: provider: %s\n", PK11_DBG, def_PK11_LIBNAME); 10764+ fprintf(stderr, "%s: number of slots: %d\n", PK11_DBG, ulSlotCount); 10765+ 10766+ fprintf(stderr, "%s: == checking rand slots ==\n", PK11_DBG); 10767+#endif /* DEBUG_SLOT_SELECTION */ 10768+ for (i = 0; i < ulSlotCount; i++) 10769+ { 10770+ current_slot = pSlotList[i]; 10771+ 10772+#ifdef DEBUG_SLOT_SELECTION 10773+ fprintf(stderr, "%s: checking slot: %d\n", PK11_DBG, i); 10774+#endif /* DEBUG_SLOT_SELECTION */ 10775+ /* Check if slot has random support. */ 10776+ rv = pFuncList->C_GetTokenInfo(current_slot, &token_info); 10777+ if (rv != CKR_OK) 10778+ continue; 10779+ 10780+#ifdef DEBUG_SLOT_SELECTION 10781+ fprintf(stderr, "%s: token label: %.32s\n", PK11_DBG, token_info.label); 10782+#endif /* DEBUG_SLOT_SELECTION */ 10783+ 10784+ if (token_info.flags & CKF_RNG) 10785+ { 10786+#ifdef DEBUG_SLOT_SELECTION 10787+ fprintf(stderr, "%s: this token has CKF_RNG flag\n", PK11_DBG); 10788+#endif /* DEBUG_SLOT_SELECTION */ 10789+ pk11_have_random = CK_TRUE; 10790+ rand_SLOTID = current_slot; 10791+ break; 10792+ } 10793+ } 10794+ 10795+#ifdef DEBUG_SLOT_SELECTION 10796+ fprintf(stderr, "%s: == checking pubkey slots ==\n", PK11_DBG); 10797+#endif /* DEBUG_SLOT_SELECTION */ 10798+ 10799+ pubkey_SLOTID = pSlotList[0]; 10800+ for (i = 0; i < ulSlotCount; i++) 10801+ { 10802+ CK_BBOOL slot_has_rsa = CK_FALSE; 10803+ current_slot = pSlotList[i]; 10804+ 10805+#ifdef DEBUG_SLOT_SELECTION 10806+ fprintf(stderr, "%s: checking slot: %d\n", PK11_DBG, i); 10807+#endif /* DEBUG_SLOT_SELECTION */ 10808+ rv = pFuncList->C_GetTokenInfo(current_slot, &token_info); 10809+ if (rv != CKR_OK) 10810+ continue; 10811+ 10812+#ifdef DEBUG_SLOT_SELECTION 10813+ fprintf(stderr, "%s: token label: %.32s\n", PK11_DBG, token_info.label); 10814+#endif /* DEBUG_SLOT_SELECTION */ 10815+ 10816+ /* 10817+ * Check if this slot is capable of signing with CKM_RSA_PKCS. 10818+ */ 10819+ rv = pFuncList->C_GetMechanismInfo(current_slot, CKM_RSA_PKCS, 10820+ &mech_info); 10821+ 10822+ if (rv == CKR_OK && ((mech_info.flags & CKF_SIGN))) 10823+ { 10824+ slot_has_rsa = CK_TRUE; 10825+ } 10826+ 10827+ if (!found_candidate_slot && slot_has_rsa) 10828+ { 10829+#ifdef DEBUG_SLOT_SELECTION 10830+ fprintf(stderr, 10831+ "%s: potential slot: %d\n", PK11_DBG, current_slot); 10832+#endif /* DEBUG_SLOT_SELECTION */ 10833+ best_slot_sofar = current_slot; 10834+ pk11_have_rsa = slot_has_rsa; 10835+ found_candidate_slot = CK_TRUE; 10836+ /* 10837+ * Cache the flags for later use. We might 10838+ * need those if RSA keys by reference feature 10839+ * is used. 10840+ */ 10841+ pubkey_token_flags = token_info.flags; 10842+#ifdef DEBUG_SLOT_SELECTION 10843+ fprintf(stderr, 10844+ "%s: setting found_candidate_slot to CK_TRUE\n", 10845+ PK11_DBG); 10846+ fprintf(stderr, 10847+ "%s: best so far slot: %d\n", PK11_DBG, 10848+ best_slot_sofar); 10849+ fprintf(stderr, "%s: pubkey flags changed to " 10850+ "%lu.\n", PK11_DBG, pubkey_token_flags); 10851+ } 10852+ else 10853+ { 10854+ fprintf(stderr, 10855+ "%s: no rsa\n", PK11_DBG); 10856+ } 10857+#else 10858+ } /* if */ 10859+#endif /* DEBUG_SLOT_SELECTION */ 10860+ } /* for */ 10861+ 10862+ if (found_candidate_slot == CK_TRUE) 10863+ { 10864+ pubkey_SLOTID = best_slot_sofar; 10865+ } 10866+ 10867+ /*SLOTID = pSlotList[0];*/ 10868+ 10869+#ifdef DEBUG_SLOT_SELECTION 10870+ fprintf(stderr, 10871+ "%s: chosen pubkey slot: %d\n", PK11_DBG, pubkey_SLOTID); 10872+ fprintf(stderr, 10873+ "%s: chosen rand slot: %d\n", PK11_DBG, rand_SLOTID); 10874+ fprintf(stderr, 10875+ "%s: pk11_have_rsa %d\n", PK11_DBG, pk11_have_rsa); 10876+ fprintf(stderr, 10877+ "%s: pk11_have_random %d\n", PK11_DBG, pk11_have_random); 10878+#endif /* DEBUG_SLOT_SELECTION */ 10879+ 10880+ if (pSlotList != NULL) 10881+ OPENSSL_free(pSlotList); 10882+ 10883+ if (any_slot_found != NULL) 10884+ *any_slot_found = 1; 10885+ return (1); 10886+ } 10887+ 10888+#endif /* OPENSSL_NO_HW_PK11SO */ 10889+#endif /* OPENSSL_NO_HW_PK11 */ 10890+#endif /* OPENSSL_NO_HW */ 10891Index: openssl/crypto/engine/hw_pk11so.h 10892diff -u /dev/null openssl/crypto/engine/hw_pk11so.h:1.2.4.2 10893--- /dev/null Mon Jan 16 18:53:42 2012 10894+++ openssl/crypto/engine/hw_pk11so.h Wed Jun 15 21:12:32 2011 10895@@ -0,0 +1,32 @@ 10896+/* Redefine all pk11/PK11 external symbols to pk11so/PK11SO */ 10897+ 10898+#define token_lock pk11so_token_lock 10899+#define find_lock pk11so_find_lock 10900+#define active_list pk11so_active_list 10901+#define pubkey_token_flags pk11so_pubkey_token_flags 10902+#define pubkey_SLOTID pk11so_pubkey_SLOTID 10903+#define ERR_pk11_error ERR_pk11so_error 10904+#define PK11err_add_data PK11SOerr_add_data 10905+#define pk11_get_session pk11so_get_session 10906+#define pk11_return_session pk11so_return_session 10907+#define pk11_active_add pk11so_active_add 10908+#define pk11_active_delete pk11so_active_delete 10909+#define pk11_active_remove pk11so_active_remove 10910+#define pk11_free_active_list pk11so_free_active_list 10911+#define pk11_destroy_rsa_key_objects pk11so_destroy_rsa_key_objects 10912+#define pk11_destroy_rsa_object_pub pk11so_destroy_rsa_object_pub 10913+#define pk11_destroy_rsa_object_priv pk11so_destroy_rsa_object_priv 10914+#define pk11_load_privkey pk11so_load_privkey 10915+#define pk11_load_pubkey pk11so_load_pubkey 10916+#define PK11_RSA PK11SO_RSA 10917+#define pk11_destroy_dsa_key_objects pk11so_destroy_dsa_key_objects 10918+#define pk11_destroy_dsa_object_pub pk11so_destroy_dsa_object_pub 10919+#define pk11_destroy_dsa_object_priv pk11so_destroy_dsa_object_priv 10920+#define PK11_DSA PK11SO_DSA 10921+#define pk11_destroy_dh_key_objects pk11so_destroy_dh_key_objects 10922+#define pk11_destroy_dh_object pk11so_destroy_dh_object 10923+#define PK11_DH PK11SO_DH 10924+#define pk11_token_relogin pk11so_token_relogin 10925+#define pFuncList pk11so_pFuncList 10926+#define pk11_pin pk11so_pin 10927+#define ENGINE_load_pk11 ENGINE_load_pk11so 10928Index: openssl/crypto/engine/hw_pk11so_pub.c 10929diff -u /dev/null openssl/crypto/engine/hw_pk11so_pub.c:1.2.4.3 10930--- /dev/null Mon Jan 16 18:53:42 2012 10931+++ openssl/crypto/engine/hw_pk11so_pub.c Fri Jun 17 07:56:21 2011 10932@@ -0,0 +1,1622 @@ 10933+/* 10934+ * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 10935+ * Use is subject to license terms. 10936+ */ 10937+ 10938+/* crypto/engine/hw_pk11_pub.c */ 10939+/* 10940+ * This product includes software developed by the OpenSSL Project for 10941+ * use in the OpenSSL Toolkit (http://www.openssl.org/). 10942+ * 10943+ * This project also referenced hw_pkcs11-0.9.7b.patch written by 10944+ * Afchine Madjlessi. 10945+ */ 10946+/* 10947+ * ==================================================================== 10948+ * Copyright (c) 2000-2001 The OpenSSL Project. All rights reserved. 10949+ * 10950+ * Redistribution and use in source and binary forms, with or without 10951+ * modification, are permitted provided that the following conditions 10952+ * are met: 10953+ * 10954+ * 1. Redistributions of source code must retain the above copyright 10955+ * notice, this list of conditions and the following disclaimer. 10956+ * 10957+ * 2. Redistributions in binary form must reproduce the above copyright 10958+ * notice, this list of conditions and the following disclaimer in 10959+ * the documentation and/or other materials provided with the 10960+ * distribution. 10961+ * 10962+ * 3. All advertising materials mentioning features or use of this 10963+ * software must display the following acknowledgment: 10964+ * "This product includes software developed by the OpenSSL Project 10965+ * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 10966+ * 10967+ * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 10968+ * endorse or promote products derived from this software without 10969+ * prior written permission. For written permission, please contact 10970+ * licensing@OpenSSL.org. 10971+ * 10972+ * 5. Products derived from this software may not be called "OpenSSL" 10973+ * nor may "OpenSSL" appear in their names without prior written 10974+ * permission of the OpenSSL Project. 10975+ * 10976+ * 6. Redistributions of any form whatsoever must retain the following 10977+ * acknowledgment: 10978+ * "This product includes software developed by the OpenSSL Project 10979+ * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 10980+ * 10981+ * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 10982+ * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 10983+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 10984+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 10985+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 10986+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 10987+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 10988+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 10989+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 10990+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 10991+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 10992+ * OF THE POSSIBILITY OF SUCH DAMAGE. 10993+ * ==================================================================== 10994+ * 10995+ * This product includes cryptographic software written by Eric Young 10996+ * (eay@cryptsoft.com). This product includes software written by Tim 10997+ * Hudson (tjh@cryptsoft.com). 10998+ * 10999+ */ 11000+ 11001+/* Modified to keep only RNG and RSA Sign */ 11002+ 11003+#ifdef OPENSSL_NO_RSA 11004+#error RSA is disabled 11005+#endif 11006+ 11007+#include <stdio.h> 11008+#include <stdlib.h> 11009+#include <string.h> 11010+#include <sys/types.h> 11011+ 11012+#include <openssl/e_os2.h> 11013+#include <openssl/crypto.h> 11014+#include <cryptlib.h> 11015+#include <openssl/engine.h> 11016+#include <openssl/dso.h> 11017+#include <openssl/err.h> 11018+#include <openssl/bn.h> 11019+#include <openssl/pem.h> 11020+#include <openssl/rsa.h> 11021+#include <openssl/rand.h> 11022+#include <openssl/objects.h> 11023+#include <openssl/x509.h> 11024+ 11025+#ifdef OPENSSL_SYS_WIN32 11026+#define NOPTHREADS 11027+typedef int pid_t; 11028+#define HAVE_GETPASSPHRASE 11029+static char *getpassphrase(const char *prompt); 11030+#ifndef NULL_PTR 11031+#define NULL_PTR NULL 11032+#endif 11033+#define CK_DEFINE_FUNCTION(returnType, name) \ 11034+ returnType __declspec(dllexport) name 11035+#define CK_DECLARE_FUNCTION(returnType, name) \ 11036+ returnType __declspec(dllimport) name 11037+#define CK_DECLARE_FUNCTION_POINTER(returnType, name) \ 11038+ returnType __declspec(dllimport) (* name) 11039+#else 11040+#include <unistd.h> 11041+#endif 11042+ 11043+#ifndef NOPTHREADS 11044+#include <pthread.h> 11045+#endif 11046+ 11047+#ifndef OPENSSL_NO_HW 11048+#ifndef OPENSSL_NO_HW_PK11 11049+#ifndef OPENSSL_NO_HW_PK11SO 11050+ 11051+#ifdef OPENSSL_SYS_WIN32 11052+#pragma pack(push, cryptoki, 1) 11053+#include "cryptoki.h" 11054+#include "pkcs11.h" 11055+#pragma pack(pop, cryptoki) 11056+#else 11057+#include "cryptoki.h" 11058+#include "pkcs11.h" 11059+#endif 11060+#include "hw_pk11so.h" 11061+#include "hw_pk11_err.h" 11062+ 11063+static CK_BBOOL pk11_login_done = CK_FALSE; 11064+extern CK_SLOT_ID pubkey_SLOTID; 11065+#ifndef NOPTHREADS 11066+extern pthread_mutex_t *token_lock; 11067+#endif 11068+ 11069+#if !(defined(HAVE_GETPASSPHRASE) || (defined (__SVR4) && defined (__sun))) 11070+#define getpassphrase(x) getpass(x) 11071+#endif 11072+ 11073+/* RSA stuff */ 11074+static int pk11_RSA_sign(int type, const unsigned char *m, unsigned int m_len, 11075+ unsigned char *sigret, unsigned int *siglen, const RSA *rsa); 11076+EVP_PKEY *pk11_load_privkey(ENGINE*, const char *privkey_file, 11077+ UI_METHOD *ui_method, void *callback_data); 11078+EVP_PKEY *pk11_load_pubkey(ENGINE*, const char *pubkey_file, 11079+ UI_METHOD *ui_method, void *callback_data); 11080+ 11081+static CK_OBJECT_HANDLE pk11_get_public_rsa_key(RSA* rsa, RSA** key_ptr, 11082+ BIGNUM **rsa_n_num, BIGNUM **rsa_e_num, CK_SESSION_HANDLE session); 11083+static CK_OBJECT_HANDLE pk11_get_private_rsa_key(RSA* rsa, RSA** key_ptr, 11084+ BIGNUM **rsa_d_num, BIGNUM **rsa_n_num, BIGNUM **rsa_e_num, 11085+ CK_SESSION_HANDLE session); 11086+ 11087+static int check_new_rsa_key_pub(PK11_SESSION *sp, const RSA *rsa); 11088+static int check_new_rsa_key_priv(PK11_SESSION *sp, const RSA *rsa); 11089+ 11090+static int find_one_object(PK11_OPTYPE op, CK_SESSION_HANDLE s, 11091+ CK_ATTRIBUTE_PTR ptempl, CK_ULONG nattr, CK_OBJECT_HANDLE_PTR pkey); 11092+static int init_template_value(BIGNUM *bn, CK_VOID_PTR *pValue, 11093+ CK_ULONG *ulValueLen); 11094+static void attr_to_BN(CK_ATTRIBUTE_PTR attr, CK_BYTE attr_data[], BIGNUM **bn); 11095+ 11096+static int pk11_token_login(CK_SESSION_HANDLE session, CK_BBOOL *login_done, 11097+ CK_BBOOL is_private); 11098+ 11099+/* Read mode string to be used for fopen() */ 11100+#if SOLARIS_OPENSSL 11101+static char *read_mode_flags = "rF"; 11102+#else 11103+static char *read_mode_flags = "r"; 11104+#endif 11105+ 11106+/* 11107+ * increment/create reference for an asymmetric key handle via active list 11108+ * manipulation. If active list operation fails, unlock (if locked), set error 11109+ * variable and jump to the specified label. 11110+ */ 11111+#define KEY_HANDLE_REFHOLD(key_handle, alg_type, unlock, var, label) \ 11112+ { \ 11113+ if (pk11_active_add(key_handle, alg_type) < 0) \ 11114+ { \ 11115+ var = TRUE; \ 11116+ if (unlock) \ 11117+ UNLOCK_OBJSTORE(alg_type); \ 11118+ goto label; \ 11119+ } \ 11120+ } 11121+ 11122+/* 11123+ * Find active list entry according to object handle and return pointer to the 11124+ * entry otherwise return NULL. 11125+ * 11126+ * This function presumes it is called with lock protecting the active list 11127+ * held. 11128+ */ 11129+static PK11_active *pk11_active_find(CK_OBJECT_HANDLE h, PK11_OPTYPE type) 11130+ { 11131+ PK11_active *entry; 11132+ 11133+ for (entry = active_list[type]; entry != NULL; entry = entry->next) 11134+ if (entry->h == h) 11135+ return (entry); 11136+ 11137+ return (NULL); 11138+ } 11139+ 11140+/* 11141+ * Search for an entry in the active list using PKCS#11 object handle as a 11142+ * search key and return refcnt of the found/created entry or -1 in case of 11143+ * failure. 11144+ * 11145+ * This function presumes it is called with lock protecting the active list 11146+ * held. 11147+ */ 11148+int 11149+pk11_active_add(CK_OBJECT_HANDLE h, PK11_OPTYPE type) 11150+ { 11151+ PK11_active *entry = NULL; 11152+ 11153+ if (h == CK_INVALID_HANDLE) 11154+ { 11155+ PK11err(PK11_F_ACTIVE_ADD, PK11_R_INVALID_HANDLE); 11156+ return (-1); 11157+ } 11158+ 11159+ /* search for entry in the active list */ 11160+ if ((entry = pk11_active_find(h, type)) != NULL) 11161+ entry->refcnt++; 11162+ else 11163+ { 11164+ /* not found, create new entry and add it to the list */ 11165+ entry = OPENSSL_malloc(sizeof (PK11_active)); 11166+ if (entry == NULL) 11167+ { 11168+ PK11err(PK11_F_ACTIVE_ADD, PK11_R_MALLOC_FAILURE); 11169+ return (-1); 11170+ } 11171+ entry->h = h; 11172+ entry->refcnt = 1; 11173+ entry->prev = NULL; 11174+ entry->next = NULL; 11175+ /* connect the newly created entry to the list */ 11176+ if (active_list[type] == NULL) 11177+ active_list[type] = entry; 11178+ else /* make the entry first in the list */ 11179+ { 11180+ entry->next = active_list[type]; 11181+ active_list[type]->prev = entry; 11182+ active_list[type] = entry; 11183+ } 11184+ } 11185+ 11186+ return (entry->refcnt); 11187+ } 11188+ 11189+/* 11190+ * Remove active list entry from the list and free it. 11191+ * 11192+ * This function presumes it is called with lock protecting the active list 11193+ * held. 11194+ */ 11195+void 11196+pk11_active_remove(PK11_active *entry, PK11_OPTYPE type) 11197+ { 11198+ PK11_active *prev_entry; 11199+ 11200+ /* remove the entry from the list and free it */ 11201+ if ((prev_entry = entry->prev) != NULL) 11202+ { 11203+ prev_entry->next = entry->next; 11204+ if (entry->next != NULL) 11205+ entry->next->prev = prev_entry; 11206+ } 11207+ else 11208+ { 11209+ active_list[type] = entry->next; 11210+ /* we were the first but not the only one */ 11211+ if (entry->next != NULL) 11212+ entry->next->prev = NULL; 11213+ } 11214+ 11215+ /* sanitization */ 11216+ entry->h = CK_INVALID_HANDLE; 11217+ entry->prev = NULL; 11218+ entry->next = NULL; 11219+ OPENSSL_free(entry); 11220+ } 11221+ 11222+/* Free all entries from the active list. */ 11223+void 11224+pk11_free_active_list(PK11_OPTYPE type) 11225+ { 11226+ PK11_active *entry; 11227+ 11228+ /* only for asymmetric types since only they have C_Find* locks. */ 11229+ switch (type) 11230+ { 11231+ case OP_RSA: 11232+ break; 11233+ default: 11234+ return; 11235+ } 11236+ 11237+ /* see find_lock array definition for more info on object locking */ 11238+ LOCK_OBJSTORE(type); 11239+ while ((entry = active_list[type]) != NULL) 11240+ pk11_active_remove(entry, type); 11241+ UNLOCK_OBJSTORE(type); 11242+ } 11243+ 11244+/* 11245+ * Search for active list entry associated with given PKCS#11 object handle, 11246+ * decrement its refcnt and if it drops to 0, disconnect the entry and free it. 11247+ * 11248+ * Return 1 if the PKCS#11 object associated with the entry has no references, 11249+ * return 0 if there is at least one reference, -1 on error. 11250+ * 11251+ * This function presumes it is called with lock protecting the active list 11252+ * held. 11253+ */ 11254+int 11255+pk11_active_delete(CK_OBJECT_HANDLE h, PK11_OPTYPE type) 11256+ { 11257+ PK11_active *entry = NULL; 11258+ 11259+ if ((entry = pk11_active_find(h, type)) == NULL) 11260+ { 11261+ PK11err(PK11_F_ACTIVE_DELETE, PK11_R_INVALID_HANDLE); 11262+ return (-1); 11263+ } 11264+ 11265+ OPENSSL_assert(entry->refcnt > 0); 11266+ entry->refcnt--; 11267+ if (entry->refcnt == 0) 11268+ { 11269+ pk11_active_remove(entry, type); 11270+ return (1); 11271+ } 11272+ 11273+ return (0); 11274+ } 11275+ 11276+/* Our internal RSA_METHOD that we provide pointers to */ 11277+static RSA_METHOD pk11_rsa; 11278+ 11279+RSA_METHOD * 11280+PK11_RSA(void) 11281+ { 11282+ const RSA_METHOD *rsa; 11283+ 11284+ if (pk11_rsa.name == NULL) 11285+ { 11286+ rsa = RSA_PKCS1_SSLeay(); 11287+ memcpy(&pk11_rsa, rsa, sizeof(*rsa)); 11288+ pk11_rsa.name = "PKCS#11 RSA method"; 11289+ pk11_rsa.rsa_sign = pk11_RSA_sign; 11290+ } 11291+ return (&pk11_rsa); 11292+ } 11293+ 11294+/* Size of an SSL signature: MD5+SHA1 */ 11295+#define SSL_SIG_LENGTH 36 11296+ 11297+static CK_BBOOL true = TRUE; 11298+static CK_BBOOL false = FALSE; 11299+ 11300+/* 11301+ * Standard engine interface function. Majority codes here are from 11302+ * rsa/rsa_sign.c. We replaced the decrypt function call by C_Sign of PKCS#11. 11303+ * See more details in rsa/rsa_sign.c 11304+ */ 11305+static int pk11_RSA_sign(int type, const unsigned char *m, unsigned int m_len, 11306+ unsigned char *sigret, unsigned int *siglen, const RSA *rsa) 11307+ { 11308+ X509_SIG sig; 11309+ ASN1_TYPE parameter; 11310+ int i, j = 0; 11311+ unsigned char *p, *s = NULL; 11312+ X509_ALGOR algor; 11313+ ASN1_OCTET_STRING digest; 11314+ CK_RV rv; 11315+ CK_MECHANISM mech_rsa = {CKM_RSA_PKCS, NULL, 0}; 11316+ CK_MECHANISM *p_mech = &mech_rsa; 11317+ CK_OBJECT_HANDLE h_priv_key; 11318+ PK11_SESSION *sp = NULL; 11319+ int ret = 0; 11320+ unsigned long ulsiglen; 11321+ 11322+ /* Encode the digest */ 11323+ /* Special case: SSL signature, just check the length */ 11324+ if (type == NID_md5_sha1) 11325+ { 11326+ if (m_len != SSL_SIG_LENGTH) 11327+ { 11328+ PK11err(PK11_F_RSA_SIGN, 11329+ PK11_R_INVALID_MESSAGE_LENGTH); 11330+ goto err; 11331+ } 11332+ i = SSL_SIG_LENGTH; 11333+ s = (unsigned char *)m; 11334+ } 11335+ else 11336+ { 11337+ sig.algor = &algor; 11338+ sig.algor->algorithm = OBJ_nid2obj(type); 11339+ if (sig.algor->algorithm == NULL) 11340+ { 11341+ PK11err(PK11_F_RSA_SIGN, 11342+ PK11_R_UNKNOWN_ALGORITHM_TYPE); 11343+ goto err; 11344+ } 11345+ if (sig.algor->algorithm->length == 0) 11346+ { 11347+ PK11err(PK11_F_RSA_SIGN, 11348+ PK11_R_UNKNOWN_ASN1_OBJECT_ID); 11349+ goto err; 11350+ } 11351+ parameter.type = V_ASN1_NULL; 11352+ parameter.value.ptr = NULL; 11353+ sig.algor->parameter = ¶meter; 11354+ 11355+ sig.digest = &digest; 11356+ sig.digest->data = (unsigned char *)m; 11357+ sig.digest->length = m_len; 11358+ 11359+ i = i2d_X509_SIG(&sig, NULL); 11360+ } 11361+ 11362+ j = RSA_size(rsa); 11363+ if ((i - RSA_PKCS1_PADDING) > j) 11364+ { 11365+ PK11err(PK11_F_RSA_SIGN, PK11_R_DIGEST_TOO_BIG); 11366+ goto err; 11367+ } 11368+ 11369+ if (type != NID_md5_sha1) 11370+ { 11371+ s = (unsigned char *)OPENSSL_malloc((unsigned int)(j + 1)); 11372+ if (s == NULL) 11373+ { 11374+ PK11err(PK11_F_RSA_SIGN, PK11_R_MALLOC_FAILURE); 11375+ goto err; 11376+ } 11377+ p = s; 11378+ (void) i2d_X509_SIG(&sig, &p); 11379+ } 11380+ 11381+ if ((sp = pk11_get_session(OP_RSA)) == NULL) 11382+ goto err; 11383+ 11384+ (void) check_new_rsa_key_priv(sp, rsa); 11385+ 11386+ h_priv_key = sp->opdata_rsa_priv_key; 11387+ if (h_priv_key == CK_INVALID_HANDLE) 11388+ h_priv_key = sp->opdata_rsa_priv_key = 11389+ pk11_get_private_rsa_key((RSA *)rsa, 11390+ &sp->opdata_rsa_priv, &sp->opdata_rsa_d_num, 11391+ &sp->opdata_rsa_pn_num, &sp->opdata_rsa_pe_num, 11392+ sp->session); 11393+ 11394+ if (h_priv_key != CK_INVALID_HANDLE) 11395+ { 11396+ rv = pFuncList->C_SignInit(sp->session, p_mech, h_priv_key); 11397+ 11398+ if (rv != CKR_OK) 11399+ { 11400+ PK11err_add_data(PK11_F_RSA_SIGN, PK11_R_SIGNINIT, rv); 11401+ goto err; 11402+ } 11403+ 11404+ ulsiglen = j; 11405+ rv = pFuncList->C_Sign(sp->session, s, i, sigret, 11406+ (CK_ULONG_PTR) &ulsiglen); 11407+ *siglen = ulsiglen; 11408+ 11409+ if (rv != CKR_OK) 11410+ { 11411+ PK11err_add_data(PK11_F_RSA_SIGN, PK11_R_SIGN, rv); 11412+ goto err; 11413+ } 11414+ ret = 1; 11415+ } 11416+ 11417+err: 11418+ if ((type != NID_md5_sha1) && (s != NULL)) 11419+ { 11420+ (void) memset(s, 0, (unsigned int)(j + 1)); 11421+ OPENSSL_free(s); 11422+ } 11423+ 11424+ pk11_return_session(sp, OP_RSA); 11425+ return (ret); 11426+ } 11427+ 11428+static int hndidx_rsa = -1; 11429+ 11430+#define MAXATTR 1024 11431+ 11432+/* 11433+ * Load RSA private key from a file or get its PKCS#11 handle if stored in the 11434+ * PKCS#11 token. 11435+ */ 11436+/* ARGSUSED */ 11437+EVP_PKEY *pk11_load_privkey(ENGINE *e, const char *privkey_file, 11438+ UI_METHOD *ui_method, void *callback_data) 11439+ { 11440+ EVP_PKEY *pkey = NULL; 11441+ FILE *privkey; 11442+ CK_OBJECT_HANDLE h_priv_key = CK_INVALID_HANDLE; 11443+ RSA *rsa = NULL; 11444+ PK11_SESSION *sp; 11445+ /* Anything else below is needed for the key by reference extension. */ 11446+ CK_RV rv; 11447+ CK_BBOOL is_token = TRUE; 11448+ CK_BBOOL rollback = FALSE; 11449+ CK_BYTE attr_data[2][MAXATTR]; 11450+ CK_OBJECT_CLASS key_class = CKO_PRIVATE_KEY; 11451+ CK_OBJECT_HANDLE ks_key = CK_INVALID_HANDLE; /* key in keystore */ 11452+ 11453+ /* we look for private keys only */ 11454+ CK_ATTRIBUTE search_templ[] = 11455+ { 11456+ {CKA_TOKEN, &is_token, sizeof(is_token)}, 11457+ {CKA_CLASS, &key_class, sizeof(key_class)}, 11458+ {CKA_LABEL, NULL, 0} 11459+ }; 11460+ 11461+ /* 11462+ * These public attributes are needed to initialize the OpenSSL RSA 11463+ * structure with something we can use to look up the key. Note that we 11464+ * never ask for private components. 11465+ */ 11466+ CK_ATTRIBUTE get_templ[] = 11467+ { 11468+ {CKA_MODULUS, (void *)attr_data[0], MAXATTR}, /* n */ 11469+ {CKA_PUBLIC_EXPONENT, (void *)attr_data[1], MAXATTR}, /* e */ 11470+ }; 11471+ 11472+ if ((sp = pk11_get_session(OP_RSA)) == NULL) 11473+ return (NULL); 11474+ 11475+ /* 11476+ * Use simple scheme "pkcs11:<KEY_LABEL>" for now. 11477+ */ 11478+ if (strstr(privkey_file, "pkcs11:") == privkey_file) 11479+ { 11480+ search_templ[2].pValue = strstr(privkey_file, ":") + 1; 11481+ search_templ[2].ulValueLen = strlen(search_templ[2].pValue); 11482+ 11483+ if (pk11_token_login(sp->session, &pk11_login_done, 11484+ CK_TRUE) == 0) 11485+ goto err; 11486+ 11487+ /* 11488+ * Now let's try to find the key in the token. It is a failure 11489+ * if we can't find it. 11490+ */ 11491+ if (find_one_object(OP_RSA, sp->session, search_templ, 3, 11492+ &ks_key) == 0) 11493+ goto err; 11494+ 11495+ if (hndidx_rsa == -1) 11496+ hndidx_rsa = RSA_get_ex_new_index(0, 11497+ "pkcs11 RSA HSM key handle", 11498+ NULL, NULL, NULL); 11499+ 11500+ /* 11501+ * We might have a cache hit which we could confirm 11502+ * according to the 'n'/'e' params, RSA public pointer 11503+ * as NULL, and non-NULL RSA private pointer. However, 11504+ * it is easier just to recreate everything. We expect 11505+ * the keys to be loaded once and used many times. We 11506+ * do not check the return value because even in case 11507+ * of failure the sp structure will have both key 11508+ * pointer and object handle cleaned and 11509+ * pk11_destroy_object() reports the failure to the 11510+ * OpenSSL error message buffer. 11511+ */ 11512+ (void) pk11_destroy_rsa_object_priv(sp, TRUE); 11513+ 11514+ sp->opdata_rsa_priv_key = ks_key; 11515+ /* This object shall not be deleted on a cache miss. */ 11516+ sp->priv_persistent = CK_TRUE; 11517+ 11518+ /* 11519+ * Cache the RSA private structure pointer. We do not 11520+ * use it now for key-by-ref keys but let's do it for 11521+ * consistency reasons. 11522+ */ 11523+ if ((rsa = sp->opdata_rsa_priv = RSA_new_method(e)) == NULL) 11524+ goto err; 11525+ 11526+ /* 11527+ * Now we have to initialize an OpenSSL RSA structure, 11528+ * everything else is 0 or NULL. 11529+ */ 11530+ rsa->flags = RSA_FLAG_SIGN_VER | RSA_FLAG_EXT_PKEY; 11531+ RSA_set_ex_data(rsa, hndidx_rsa, (void *) ks_key); 11532+ 11533+ if ((rv = pFuncList->C_GetAttributeValue(sp->session, ks_key, 11534+ get_templ, 2)) != CKR_OK) 11535+ { 11536+ PK11err_add_data(PK11_F_LOAD_PRIVKEY, 11537+ PK11_R_GETATTRIBUTVALUE, rv); 11538+ goto err; 11539+ } 11540+ 11541+ /* 11542+ * We do not use pk11_get_private_rsa_key() here so we 11543+ * must take care of handle management ourselves. 11544+ */ 11545+ KEY_HANDLE_REFHOLD(ks_key, OP_RSA, FALSE, rollback, err); 11546+ 11547+ /* 11548+ * Those are the sensitive components we do not want to export 11549+ * from the token at all: rsa->(d|p|q|dmp1|dmq1|iqmp). 11550+ */ 11551+ attr_to_BN(&get_templ[0], attr_data[0], &rsa->n); 11552+ attr_to_BN(&get_templ[1], attr_data[1], &rsa->e); 11553+ /* 11554+ * Must have 'n'/'e' components in the session structure as 11555+ * well. They serve as a public look-up key for the private key 11556+ * in the keystore. 11557+ */ 11558+ attr_to_BN(&get_templ[0], attr_data[0], 11559+ &sp->opdata_rsa_pn_num); 11560+ attr_to_BN(&get_templ[1], attr_data[1], 11561+ &sp->opdata_rsa_pe_num); 11562+ 11563+ if ((pkey = EVP_PKEY_new()) == NULL) 11564+ goto err; 11565+ 11566+ if (EVP_PKEY_assign_RSA(pkey, rsa) == 0) 11567+ goto err; 11568+ } 11569+ else if ((privkey = fopen(privkey_file, read_mode_flags)) != NULL) 11570+ { 11571+ pkey = PEM_read_PrivateKey(privkey, NULL, NULL, NULL); 11572+ (void) fclose(privkey); 11573+ if (pkey != NULL) 11574+ { 11575+ rsa = EVP_PKEY_get1_RSA(pkey); 11576+ if (rsa != NULL) 11577+ { 11578+ /* 11579+ * This will always destroy the RSA 11580+ * object since we have a new RSA 11581+ * structure here. 11582+ */ 11583+ (void) check_new_rsa_key_priv(sp, rsa); 11584+ sp->priv_persistent = CK_FALSE; 11585+ 11586+ h_priv_key = sp->opdata_rsa_priv_key = 11587+ pk11_get_private_rsa_key(rsa, 11588+ &sp->opdata_rsa_priv, 11589+ &sp->opdata_rsa_d_num, 11590+ &sp->opdata_rsa_pn_num, 11591+ &sp->opdata_rsa_pe_num, sp->session); 11592+ if (h_priv_key == CK_INVALID_HANDLE) 11593+ goto err; 11594+ } 11595+ else 11596+ goto err; 11597+ } 11598+ } 11599+ 11600+ pk11_return_session(sp, OP_RSA); 11601+ return (pkey); 11602+err: 11603+ pk11_return_session(sp, OP_RSA); 11604+ if (rsa != NULL) 11605+ RSA_free(rsa); 11606+ if (pkey != NULL) 11607+ { 11608+ EVP_PKEY_free(pkey); 11609+ pkey = NULL; 11610+ } 11611+ rollback = rollback; 11612+ return (pkey); 11613+ } 11614+ 11615+/* 11616+ * Load RSA public key from a file or get its PKCS#11 handle if stored in the 11617+ * PKCS#11 token. 11618+ */ 11619+/* ARGSUSED */ 11620+EVP_PKEY *pk11_load_pubkey(ENGINE *e, const char *pubkey_file, 11621+ UI_METHOD *ui_method, void *callback_data) 11622+ { 11623+ EVP_PKEY *pkey = NULL; 11624+ FILE *pubkey; 11625+ CK_OBJECT_HANDLE h_pub_key = CK_INVALID_HANDLE; 11626+ RSA *rsa = NULL; 11627+ PK11_SESSION *sp; 11628+ /* Anything else below is needed for the key by reference extension. */ 11629+ CK_RV rv; 11630+ CK_BBOOL is_token = TRUE; 11631+ CK_BYTE attr_data[2][MAXATTR]; 11632+ CK_OBJECT_CLASS key_class = CKO_PUBLIC_KEY; 11633+ CK_OBJECT_HANDLE ks_key = CK_INVALID_HANDLE; /* key in keystore */ 11634+ 11635+ /* we look for public keys only */ 11636+ CK_ATTRIBUTE search_templ[] = 11637+ { 11638+ {CKA_TOKEN, &is_token, sizeof(is_token)}, 11639+ {CKA_CLASS, &key_class, sizeof(key_class)}, 11640+ {CKA_LABEL, NULL, 0} 11641+ }; 11642+ 11643+ /* 11644+ * These public attributes are needed to initialize OpenSSL RSA 11645+ * structure with something we can use to look up the key. 11646+ */ 11647+ CK_ATTRIBUTE get_templ[] = 11648+ { 11649+ {CKA_MODULUS, (void *)attr_data[0], MAXATTR}, /* n */ 11650+ {CKA_PUBLIC_EXPONENT, (void *)attr_data[1], MAXATTR}, /* e */ 11651+ }; 11652+ 11653+ if ((sp = pk11_get_session(OP_RSA)) == NULL) 11654+ return (NULL); 11655+ 11656+ /* 11657+ * Use simple scheme "pkcs11:<KEY_LABEL>" for now. 11658+ */ 11659+ if (strstr(pubkey_file, "pkcs11:") == pubkey_file) 11660+ { 11661+ search_templ[2].pValue = strstr(pubkey_file, ":") + 1; 11662+ search_templ[2].ulValueLen = strlen(search_templ[2].pValue); 11663+ 11664+ if (pk11_token_login(sp->session, &pk11_login_done, 11665+ CK_FALSE) == 0) 11666+ goto err; 11667+ 11668+ /* 11669+ * Now let's try to find the key in the token. It is a failure 11670+ * if we can't find it. 11671+ */ 11672+ if (find_one_object(OP_RSA, sp->session, search_templ, 3, 11673+ &ks_key) == 0) 11674+ goto err; 11675+ 11676+ /* 11677+ * We load a new public key so we will create a new RSA 11678+ * structure. No cache hit is possible. 11679+ */ 11680+ (void) pk11_destroy_rsa_object_pub(sp, TRUE); 11681+ 11682+ sp->opdata_rsa_pub_key = ks_key; 11683+ /* This object shall not be deleted on a cache miss. */ 11684+ sp->pub_persistent = CK_TRUE; 11685+ 11686+ /* 11687+ * Cache the RSA public structure pointer. 11688+ */ 11689+ if ((rsa = sp->opdata_rsa_pub = RSA_new_method(e)) == NULL) 11690+ goto err; 11691+ 11692+ /* 11693+ * Now we have to initialize an OpenSSL RSA structure, 11694+ * everything else is 0 or NULL. 11695+ */ 11696+ rsa->flags = RSA_FLAG_SIGN_VER; 11697+ 11698+ if ((rv = pFuncList->C_GetAttributeValue(sp->session, ks_key, 11699+ get_templ, 2)) != CKR_OK) 11700+ { 11701+ PK11err_add_data(PK11_F_LOAD_PUBKEY, 11702+ PK11_R_GETATTRIBUTVALUE, rv); 11703+ goto err; 11704+ } 11705+ 11706+ attr_to_BN(&get_templ[0], attr_data[0], &rsa->n); 11707+ attr_to_BN(&get_templ[1], attr_data[1], &rsa->e); 11708+ 11709+ if ((pkey = EVP_PKEY_new()) == NULL) 11710+ goto err; 11711+ 11712+ if (EVP_PKEY_assign_RSA(pkey, rsa) == 0) 11713+ goto err; 11714+ 11715+ /* 11716+ * Create a session object from it so that when calling 11717+ * pk11_get_public_rsa_key() the next time, we can find it. The 11718+ * reason why we do that is that we cannot tell from the RSA 11719+ * structure (OpenSSL RSA structure does not have any room for 11720+ * additional data used by the engine, for example) if it bears 11721+ * a public key stored in the keystore or not so it's better if 11722+ * we always have a session key. Note that this is different 11723+ * from what we do for the private keystore objects but in that 11724+ * case, we can tell from the RSA structure that the keystore 11725+ * object is in play - the 'd' component is NULL in that case. 11726+ */ 11727+ h_pub_key = sp->opdata_rsa_pub_key = 11728+ pk11_get_public_rsa_key(rsa, 11729+ &sp->opdata_rsa_pub, &sp->opdata_rsa_n_num, 11730+ &sp->opdata_rsa_e_num, sp->session); 11731+ if (h_pub_key == CK_INVALID_HANDLE) 11732+ goto err; 11733+ } 11734+ else if ((pubkey = fopen(pubkey_file, read_mode_flags)) != NULL) 11735+ { 11736+ pkey = PEM_read_PUBKEY(pubkey, NULL, NULL, NULL); 11737+ (void) fclose(pubkey); 11738+ if (pkey != NULL) 11739+ { 11740+ rsa = EVP_PKEY_get1_RSA(pkey); 11741+ if (rsa != NULL) 11742+ { 11743+ /* 11744+ * This will always destroy the RSA 11745+ * object since we have a new RSA 11746+ * structure here. 11747+ */ 11748+ (void) check_new_rsa_key_pub(sp, rsa); 11749+ sp->pub_persistent = CK_FALSE; 11750+ 11751+ h_pub_key = sp->opdata_rsa_pub_key = 11752+ pk11_get_public_rsa_key(rsa, 11753+ &sp->opdata_rsa_pub, &sp->opdata_rsa_n_num, 11754+ &sp->opdata_rsa_e_num, sp->session); 11755+ if (h_pub_key == CK_INVALID_HANDLE) 11756+ goto err; 11757+ } 11758+ else 11759+ goto err; 11760+ } 11761+ } 11762+ 11763+ pk11_return_session(sp, OP_RSA); 11764+ return (pkey); 11765+err: 11766+ pk11_return_session(sp, OP_RSA); 11767+ if (rsa != NULL) 11768+ RSA_free(rsa); 11769+ if (pkey != NULL) 11770+ { 11771+ EVP_PKEY_free(pkey); 11772+ pkey = NULL; 11773+ } 11774+ return (pkey); 11775+ } 11776+ 11777+/* 11778+ * Create a public key object in a session from a given rsa structure. 11779+ * The *rsa_n_num and *rsa_e_num pointers are non-NULL for RSA public keys. 11780+ */ 11781+static CK_OBJECT_HANDLE pk11_get_public_rsa_key(RSA *rsa, 11782+ RSA **key_ptr, BIGNUM **rsa_n_num, BIGNUM **rsa_e_num, 11783+ CK_SESSION_HANDLE session) 11784+ { 11785+ CK_RV rv; 11786+ CK_OBJECT_HANDLE h_key = CK_INVALID_HANDLE; 11787+ CK_ULONG found; 11788+ CK_OBJECT_CLASS o_key = CKO_PUBLIC_KEY; 11789+ CK_KEY_TYPE k_type = CKK_RSA; 11790+ CK_ULONG ul_key_attr_count = 8; 11791+ CK_BBOOL rollback = FALSE; 11792+ 11793+ CK_ATTRIBUTE a_key_template[] = 11794+ { 11795+ {CKA_CLASS, (void *) NULL, sizeof (CK_OBJECT_CLASS)}, 11796+ {CKA_KEY_TYPE, (void *) NULL, sizeof (CK_KEY_TYPE)}, 11797+ {CKA_TOKEN, &false, sizeof (true)}, 11798+ {CKA_ENCRYPT, &true, sizeof (true)}, 11799+ {CKA_VERIFY, &true, sizeof (true)}, 11800+ {CKA_VERIFY_RECOVER, &true, sizeof (true)}, 11801+ {CKA_MODULUS, (void *)NULL, 0}, 11802+ {CKA_PUBLIC_EXPONENT, (void *)NULL, 0} 11803+ }; 11804+ 11805+ int i; 11806+ 11807+ a_key_template[0].pValue = &o_key; 11808+ a_key_template[1].pValue = &k_type; 11809+ 11810+ a_key_template[6].ulValueLen = BN_num_bytes(rsa->n); 11811+ a_key_template[6].pValue = (CK_VOID_PTR)OPENSSL_malloc( 11812+ (size_t)a_key_template[6].ulValueLen); 11813+ if (a_key_template[6].pValue == NULL) 11814+ { 11815+ PK11err(PK11_F_GET_PUB_RSA_KEY, PK11_R_MALLOC_FAILURE); 11816+ goto malloc_err; 11817+ } 11818+ 11819+ BN_bn2bin(rsa->n, a_key_template[6].pValue); 11820+ 11821+ a_key_template[7].ulValueLen = BN_num_bytes(rsa->e); 11822+ a_key_template[7].pValue = (CK_VOID_PTR)OPENSSL_malloc( 11823+ (size_t)a_key_template[7].ulValueLen); 11824+ if (a_key_template[7].pValue == NULL) 11825+ { 11826+ PK11err(PK11_F_GET_PUB_RSA_KEY, PK11_R_MALLOC_FAILURE); 11827+ goto malloc_err; 11828+ } 11829+ 11830+ BN_bn2bin(rsa->e, a_key_template[7].pValue); 11831+ 11832+ /* see find_lock array definition for more info on object locking */ 11833+ LOCK_OBJSTORE(OP_RSA); 11834+ 11835+ rv = pFuncList->C_FindObjectsInit(session, a_key_template, 11836+ ul_key_attr_count); 11837+ 11838+ if (rv != CKR_OK) 11839+ { 11840+ PK11err_add_data(PK11_F_GET_PUB_RSA_KEY, 11841+ PK11_R_FINDOBJECTSINIT, rv); 11842+ goto err; 11843+ } 11844+ 11845+ rv = pFuncList->C_FindObjects(session, &h_key, 1, &found); 11846+ 11847+ if (rv != CKR_OK) 11848+ { 11849+ PK11err_add_data(PK11_F_GET_PUB_RSA_KEY, 11850+ PK11_R_FINDOBJECTS, rv); 11851+ goto err; 11852+ } 11853+ 11854+ rv = pFuncList->C_FindObjectsFinal(session); 11855+ 11856+ if (rv != CKR_OK) 11857+ { 11858+ PK11err_add_data(PK11_F_GET_PUB_RSA_KEY, 11859+ PK11_R_FINDOBJECTSFINAL, rv); 11860+ goto err; 11861+ } 11862+ 11863+ if (found == 0) 11864+ { 11865+ rv = pFuncList->C_CreateObject(session, 11866+ a_key_template, ul_key_attr_count, &h_key); 11867+ if (rv != CKR_OK) 11868+ { 11869+ PK11err_add_data(PK11_F_GET_PUB_RSA_KEY, 11870+ PK11_R_CREATEOBJECT, rv); 11871+ goto err; 11872+ } 11873+ } 11874+ 11875+ if (rsa_n_num != NULL) 11876+ if ((*rsa_n_num = BN_dup(rsa->n)) == NULL) 11877+ { 11878+ PK11err(PK11_F_GET_PUB_RSA_KEY, PK11_R_MALLOC_FAILURE); 11879+ rollback = TRUE; 11880+ goto err; 11881+ } 11882+ if (rsa_e_num != NULL) 11883+ if ((*rsa_e_num = BN_dup(rsa->e)) == NULL) 11884+ { 11885+ PK11err(PK11_F_GET_PUB_RSA_KEY, PK11_R_MALLOC_FAILURE); 11886+ BN_free(*rsa_n_num); 11887+ *rsa_n_num = NULL; 11888+ rollback = TRUE; 11889+ goto err; 11890+ } 11891+ 11892+ /* LINTED: E_CONSTANT_CONDITION */ 11893+ KEY_HANDLE_REFHOLD(h_key, OP_RSA, FALSE, rollback, err); 11894+ if (key_ptr != NULL) 11895+ *key_ptr = rsa; 11896+ 11897+err: 11898+ if (rollback) 11899+ { 11900+ /* 11901+ * We do not care about the return value from C_DestroyObject() 11902+ * since we are doing rollback. 11903+ */ 11904+ if (found == 0) 11905+ (void) pFuncList->C_DestroyObject(session, h_key); 11906+ h_key = CK_INVALID_HANDLE; 11907+ } 11908+ 11909+ UNLOCK_OBJSTORE(OP_RSA); 11910+ 11911+malloc_err: 11912+ for (i = 6; i <= 7; i++) 11913+ { 11914+ if (a_key_template[i].pValue != NULL) 11915+ { 11916+ OPENSSL_free(a_key_template[i].pValue); 11917+ a_key_template[i].pValue = NULL; 11918+ } 11919+ } 11920+ 11921+ return (h_key); 11922+ } 11923+ 11924+/* 11925+ * Create a private key object in the session from a given rsa structure. 11926+ * The *rsa_d_num pointer is non-NULL for RSA private keys. 11927+ */ 11928+static CK_OBJECT_HANDLE 11929+pk11_get_private_rsa_key(RSA *rsa, RSA **key_ptr, BIGNUM **rsa_d_num, 11930+ BIGNUM **rsa_n_num, BIGNUM **rsa_e_num, CK_SESSION_HANDLE session) 11931+ { 11932+ CK_RV rv; 11933+ CK_OBJECT_HANDLE h_key = CK_INVALID_HANDLE; 11934+ int i; 11935+ CK_ULONG found; 11936+ CK_OBJECT_CLASS o_key = CKO_PRIVATE_KEY; 11937+ CK_KEY_TYPE k_type = CKK_RSA; 11938+ CK_ULONG ul_key_attr_count = 14; 11939+ CK_BBOOL rollback = FALSE; 11940+ 11941+ /* Both CKA_TOKEN and CKA_SENSITIVE have to be FALSE for session keys */ 11942+ CK_ATTRIBUTE a_key_template[] = 11943+ { 11944+ {CKA_CLASS, (void *) NULL, sizeof (CK_OBJECT_CLASS)}, 11945+ {CKA_KEY_TYPE, (void *) NULL, sizeof (CK_KEY_TYPE)}, 11946+ {CKA_TOKEN, &false, sizeof (true)}, 11947+ {CKA_SENSITIVE, &false, sizeof (true)}, 11948+ {CKA_DECRYPT, &true, sizeof (true)}, 11949+ {CKA_SIGN, &true, sizeof (true)}, 11950+ {CKA_MODULUS, (void *)NULL, 0}, 11951+ {CKA_PUBLIC_EXPONENT, (void *)NULL, 0}, 11952+ {CKA_PRIVATE_EXPONENT, (void *)NULL, 0}, 11953+ {CKA_PRIME_1, (void *)NULL, 0}, 11954+ {CKA_PRIME_2, (void *)NULL, 0}, 11955+ {CKA_EXPONENT_1, (void *)NULL, 0}, 11956+ {CKA_EXPONENT_2, (void *)NULL, 0}, 11957+ {CKA_COEFFICIENT, (void *)NULL, 0}, 11958+ }; 11959+ 11960+ if ((rsa->flags & RSA_FLAG_EXT_PKEY) != 0) { 11961+ h_key = (CK_OBJECT_HANDLE)RSA_get_ex_data(rsa, hndidx_rsa); 11962+ LOCK_OBJSTORE(OP_RSA); 11963+ goto set; 11964+ } 11965+ 11966+ a_key_template[0].pValue = &o_key; 11967+ a_key_template[1].pValue = &k_type; 11968+ 11969+ /* Put the private key components into the template */ 11970+ if (init_template_value(rsa->n, &a_key_template[6].pValue, 11971+ &a_key_template[6].ulValueLen) == 0 || 11972+ init_template_value(rsa->e, &a_key_template[7].pValue, 11973+ &a_key_template[7].ulValueLen) == 0 || 11974+ init_template_value(rsa->d, &a_key_template[8].pValue, 11975+ &a_key_template[8].ulValueLen) == 0 || 11976+ init_template_value(rsa->p, &a_key_template[9].pValue, 11977+ &a_key_template[9].ulValueLen) == 0 || 11978+ init_template_value(rsa->q, &a_key_template[10].pValue, 11979+ &a_key_template[10].ulValueLen) == 0 || 11980+ init_template_value(rsa->dmp1, &a_key_template[11].pValue, 11981+ &a_key_template[11].ulValueLen) == 0 || 11982+ init_template_value(rsa->dmq1, &a_key_template[12].pValue, 11983+ &a_key_template[12].ulValueLen) == 0 || 11984+ init_template_value(rsa->iqmp, &a_key_template[13].pValue, 11985+ &a_key_template[13].ulValueLen) == 0) 11986+ { 11987+ PK11err(PK11_F_GET_PRIV_RSA_KEY, PK11_R_MALLOC_FAILURE); 11988+ goto malloc_err; 11989+ } 11990+ 11991+ /* see find_lock array definition for more info on object locking */ 11992+ LOCK_OBJSTORE(OP_RSA); 11993+ 11994+ /* 11995+ * We are getting the private key but the private 'd' 11996+ * component is NULL. That means this is key by reference RSA 11997+ * key. In that case, we can use only public components for 11998+ * searching for the private key handle. 11999+ */ 12000+ if (rsa->d == NULL) 12001+ { 12002+ ul_key_attr_count = 8; 12003+ /* 12004+ * We will perform the search in the token, not in the existing 12005+ * session keys. 12006+ */ 12007+ a_key_template[2].pValue = &true; 12008+ } 12009+ 12010+ rv = pFuncList->C_FindObjectsInit(session, a_key_template, 12011+ ul_key_attr_count); 12012+ 12013+ if (rv != CKR_OK) 12014+ { 12015+ PK11err_add_data(PK11_F_GET_PRIV_RSA_KEY, 12016+ PK11_R_FINDOBJECTSINIT, rv); 12017+ goto err; 12018+ } 12019+ 12020+ rv = pFuncList->C_FindObjects(session, &h_key, 1, &found); 12021+ 12022+ if (rv != CKR_OK) 12023+ { 12024+ PK11err_add_data(PK11_F_GET_PRIV_RSA_KEY, 12025+ PK11_R_FINDOBJECTS, rv); 12026+ goto err; 12027+ } 12028+ 12029+ rv = pFuncList->C_FindObjectsFinal(session); 12030+ 12031+ if (rv != CKR_OK) 12032+ { 12033+ PK11err_add_data(PK11_F_GET_PRIV_RSA_KEY, 12034+ PK11_R_FINDOBJECTSFINAL, rv); 12035+ goto err; 12036+ } 12037+ 12038+ if (found == 0) 12039+ { 12040+ /* 12041+ * We have an RSA structure with 'n'/'e' components 12042+ * only so we tried to find the private key in the 12043+ * keystore. If it was really a token key we have a 12044+ * problem. Note that for other key types we just 12045+ * create a new session key using the private 12046+ * components from the RSA structure. 12047+ */ 12048+ if (rsa->d == NULL) 12049+ { 12050+ PK11err(PK11_F_GET_PRIV_RSA_KEY, 12051+ PK11_R_PRIV_KEY_NOT_FOUND); 12052+ goto err; 12053+ } 12054+ 12055+ rv = pFuncList->C_CreateObject(session, 12056+ a_key_template, ul_key_attr_count, &h_key); 12057+ if (rv != CKR_OK) 12058+ { 12059+ PK11err_add_data(PK11_F_GET_PRIV_RSA_KEY, 12060+ PK11_R_CREATEOBJECT, rv); 12061+ goto err; 12062+ } 12063+ } 12064+ 12065+set: 12066+ if (rsa_d_num != NULL) 12067+ { 12068+ /* 12069+ * When RSA keys by reference code is used, we never 12070+ * extract private components from the keystore. In 12071+ * that case 'd' was set to NULL and we expect the 12072+ * application to properly cope with that. It is 12073+ * documented in openssl(5). In general, if keys by 12074+ * reference are used we expect it to be used 12075+ * exclusively using the high level API and then there 12076+ * is no problem. If the application expects the 12077+ * private components to be read from the keystore 12078+ * then that is not a supported way of usage. 12079+ */ 12080+ if (rsa->d != NULL && (*rsa_d_num = BN_dup(rsa->d)) == NULL) 12081+ { 12082+ PK11err(PK11_F_GET_PRIV_RSA_KEY, PK11_R_MALLOC_FAILURE); 12083+ rollback = TRUE; 12084+ goto err; 12085+ } 12086+ else 12087+ *rsa_d_num = NULL; 12088+ } 12089+ 12090+ /* 12091+ * For the key by reference code, we need public components as well 12092+ * since 'd' component is always NULL. For that reason, we always cache 12093+ * 'n'/'e' components as well. 12094+ */ 12095+ *rsa_n_num = BN_dup(rsa->n); 12096+ *rsa_e_num = BN_dup(rsa->e); 12097+ 12098+ /* LINTED: E_CONSTANT_CONDITION */ 12099+ KEY_HANDLE_REFHOLD(h_key, OP_RSA, FALSE, rollback, err); 12100+ if (key_ptr != NULL) 12101+ *key_ptr = rsa; 12102+ 12103+err: 12104+ if (rollback) 12105+ { 12106+ /* 12107+ * We do not care about the return value from C_DestroyObject() 12108+ * since we are doing rollback. 12109+ */ 12110+ if (found == 0 && 12111+ (rsa->flags & RSA_FLAG_EXT_PKEY) == 0) 12112+ (void) pFuncList->C_DestroyObject(session, h_key); 12113+ h_key = CK_INVALID_HANDLE; 12114+ } 12115+ 12116+ UNLOCK_OBJSTORE(OP_RSA); 12117+ 12118+malloc_err: 12119+ /* 12120+ * 6 to 13 entries in the key template are key components. 12121+ * They need to be freed upon exit or error. 12122+ */ 12123+ for (i = 6; i <= 13; i++) 12124+ { 12125+ if (a_key_template[i].pValue != NULL) 12126+ { 12127+ (void) memset(a_key_template[i].pValue, 0, 12128+ a_key_template[i].ulValueLen); 12129+ OPENSSL_free(a_key_template[i].pValue); 12130+ a_key_template[i].pValue = NULL; 12131+ } 12132+ } 12133+ 12134+ return (h_key); 12135+ } 12136+ 12137+/* 12138+ * Check for cache miss and clean the object pointer and handle 12139+ * in such case. Return 1 for cache hit, 0 for cache miss. 12140+ */ 12141+static int check_new_rsa_key_pub(PK11_SESSION *sp, const RSA *rsa) 12142+ { 12143+ /* 12144+ * Provide protection against RSA structure reuse by making the 12145+ * check for cache hit stronger. Only public components of RSA 12146+ * key matter here so it is sufficient to compare them with values 12147+ * cached in PK11_SESSION structure. 12148+ * 12149+ * We must check the handle as well since with key by reference, public 12150+ * components 'n'/'e' are cached in private keys as well. That means we 12151+ * could have a cache hit in a private key when looking for a public 12152+ * key. That would not work, you cannot have one PKCS#11 object for 12153+ * both data signing and verifying. 12154+ */ 12155+ if ((sp->opdata_rsa_pub != rsa) || 12156+ (BN_cmp(sp->opdata_rsa_n_num, rsa->n) != 0) || 12157+ (BN_cmp(sp->opdata_rsa_e_num, rsa->e) != 0) || 12158+ (sp->opdata_rsa_priv_key != CK_INVALID_HANDLE)) 12159+ { 12160+ /* 12161+ * We do not check the return value because even in case of 12162+ * failure the sp structure will have both key pointer 12163+ * and object handle cleaned and pk11_destroy_object() 12164+ * reports the failure to the OpenSSL error message buffer. 12165+ */ 12166+ (void) pk11_destroy_rsa_object_pub(sp, TRUE); 12167+ return (0); 12168+ } 12169+ return (1); 12170+ } 12171+ 12172+/* 12173+ * Check for cache miss and clean the object pointer and handle 12174+ * in such case. Return 1 for cache hit, 0 for cache miss. 12175+ */ 12176+static int check_new_rsa_key_priv(PK11_SESSION *sp, const RSA *rsa) 12177+ { 12178+ /* 12179+ * Provide protection against RSA structure reuse by making 12180+ * the check for cache hit stronger. Comparing public exponent 12181+ * of RSA key with value cached in PK11_SESSION structure 12182+ * should be sufficient. Note that we want to compare the 12183+ * public component since with the keys by reference 12184+ * mechanism, private components are not in the RSA 12185+ * structure. Also, see check_new_rsa_key_pub() about why we 12186+ * compare the handle as well. 12187+ */ 12188+ if ((sp->opdata_rsa_priv != rsa) || 12189+ (BN_cmp(sp->opdata_rsa_pn_num, rsa->n) != 0) || 12190+ (BN_cmp(sp->opdata_rsa_pe_num, rsa->e) != 0) || 12191+ (sp->opdata_rsa_pn_num == NULL) || 12192+ (sp->opdata_rsa_pe_num == NULL) || 12193+ (sp->opdata_rsa_pub_key != CK_INVALID_HANDLE)) 12194+ { 12195+ /* 12196+ * We do not check the return value because even in case of 12197+ * failure the sp structure will have both key pointer 12198+ * and object handle cleaned and pk11_destroy_object() 12199+ * reports the failure to the OpenSSL error message buffer. 12200+ */ 12201+ (void) pk11_destroy_rsa_object_priv(sp, TRUE); 12202+ return (0); 12203+ } 12204+ return (1); 12205+ } 12206+ 12207+/* 12208+ * Local function to simplify key template population 12209+ * Return 0 -- error, 1 -- no error 12210+ */ 12211+static int 12212+init_template_value(BIGNUM *bn, CK_VOID_PTR *p_value, 12213+ CK_ULONG *ul_value_len) 12214+ { 12215+ CK_ULONG len = 0; 12216+ 12217+ /* 12218+ * This function can be used on non-initialized BIGNUMs. It is 12219+ * easier to check that here than individually in the callers. 12220+ */ 12221+ if (bn != NULL) 12222+ len = BN_num_bytes(bn); 12223+ 12224+ if (bn == NULL || len == 0) 12225+ return (1); 12226+ 12227+ *ul_value_len = len; 12228+ *p_value = (CK_VOID_PTR)OPENSSL_malloc((size_t)*ul_value_len); 12229+ if (*p_value == NULL) 12230+ return (0); 12231+ 12232+ BN_bn2bin(bn, *p_value); 12233+ 12234+ return (1); 12235+ } 12236+ 12237+static void 12238+attr_to_BN(CK_ATTRIBUTE_PTR attr, CK_BYTE attr_data[], BIGNUM **bn) 12239+ { 12240+ if (attr->ulValueLen > 0) 12241+ *bn = BN_bin2bn(attr_data, attr->ulValueLen, NULL); 12242+ } 12243+ 12244+/* 12245+ * Find one object in the token. It is an error if we can not find the 12246+ * object or if we find more objects based on the template we got. 12247+ * 12248+ * Returns: 12249+ * 1 OK 12250+ * 0 no object or more than 1 object found 12251+ */ 12252+static int 12253+find_one_object(PK11_OPTYPE op, CK_SESSION_HANDLE s, 12254+ CK_ATTRIBUTE_PTR ptempl, CK_ULONG nattr, CK_OBJECT_HANDLE_PTR pkey) 12255+ { 12256+ CK_RV rv; 12257+ CK_ULONG objcnt; 12258+ 12259+ LOCK_OBJSTORE(op); 12260+ if ((rv = pFuncList->C_FindObjectsInit(s, ptempl, nattr)) != CKR_OK) 12261+ { 12262+ PK11err_add_data(PK11_F_FIND_ONE_OBJECT, 12263+ PK11_R_FINDOBJECTSINIT, rv); 12264+ goto err; 12265+ } 12266+ 12267+ rv = pFuncList->C_FindObjects(s, pkey, 1, &objcnt); 12268+ if (rv != CKR_OK) 12269+ { 12270+ PK11err_add_data(PK11_F_FIND_ONE_OBJECT, PK11_R_FINDOBJECTS, 12271+ rv); 12272+ goto err; 12273+ } 12274+ 12275+ if (objcnt > 1) 12276+ { 12277+ PK11err(PK11_F_FIND_ONE_OBJECT, 12278+ PK11_R_MORE_THAN_ONE_OBJECT_FOUND); 12279+ goto err; 12280+ } 12281+ else if (objcnt == 0) 12282+ { 12283+ PK11err(PK11_F_FIND_ONE_OBJECT, PK11_R_NO_OBJECT_FOUND); 12284+ goto err; 12285+ } 12286+ 12287+ (void) pFuncList->C_FindObjectsFinal(s); 12288+ UNLOCK_OBJSTORE(op); 12289+ return (1); 12290+err: 12291+ UNLOCK_OBJSTORE(op); 12292+ return (0); 12293+ } 12294+ 12295+/* from uri stuff */ 12296+ 12297+extern char *pk11_pin; 12298+ 12299+static int pk11_get_pin(void); 12300+ 12301+static int 12302+pk11_get_pin(void) 12303+{ 12304+ char *pin; 12305+ 12306+ /* The getpassphrase() function is not MT safe. */ 12307+#ifndef NOPTHREADS 12308+ (void) pthread_mutex_lock(token_lock); 12309+#else 12310+ CRYPTO_w_lock(CRYPTO_LOCK_PK11_ENGINE); 12311+#endif 12312+ pin = getpassphrase("Enter PIN: "); 12313+ if (pin == NULL) 12314+ { 12315+ PK11err(PK11_F_GET_PIN, PK11_R_COULD_NOT_READ_PIN); 12316+#ifndef NOPTHREADS 12317+ (void) pthread_mutex_unlock(token_lock); 12318+#else 12319+ CRYPTO_w_unlock(CRYPTO_LOCK_PK11_ENGINE); 12320+#endif 12321+ goto err; 12322+ } 12323+ pk11_pin = BUF_strdup(pin); 12324+ if (pk11_pin == NULL) 12325+ { 12326+ PK11err(PK11_F_LOAD_PRIVKEY, PK11_R_MALLOC_FAILURE); 12327+#ifndef NOPTHREADS 12328+ (void) pthread_mutex_unlock(token_lock); 12329+#else 12330+ CRYPTO_w_unlock(CRYPTO_LOCK_PK11_ENGINE); 12331+#endif 12332+ goto err; 12333+ } 12334+ memset(pin, 0, strlen(pin)); 12335+#ifndef NOPTHREADS 12336+ (void) pthread_mutex_unlock(token_lock); 12337+#else 12338+ CRYPTO_w_unlock(CRYPTO_LOCK_PK11_ENGINE); 12339+#endif 12340+ return (1); 12341+err: 12342+ return (0); 12343+ } 12344+ 12345+/* 12346+ * Log in to the keystore if we are supposed to do that at all. Take care of 12347+ * reading and caching the PIN etc. Log in only once even when called from 12348+ * multiple threads. 12349+ * 12350+ * Returns: 12351+ * 1 on success 12352+ * 0 on failure 12353+ */ 12354+static int 12355+pk11_token_login(CK_SESSION_HANDLE session, CK_BBOOL *login_done, 12356+ CK_BBOOL is_private) 12357+ { 12358+ CK_RV rv; 12359+ 12360+#if 0 12361+ /* doesn't work on the AEP Keyper??? */ 12362+ if ((pubkey_token_flags & CKF_TOKEN_INITIALIZED) == 0) 12363+ { 12364+ PK11err(PK11_F_TOKEN_LOGIN, 12365+ PK11_R_TOKEN_NOT_INITIALIZED); 12366+ goto err; 12367+ } 12368+#endif 12369+ 12370+ /* 12371+ * If login is required or needed but the PIN has not been 12372+ * even initialized we can bail out right now. Note that we 12373+ * are supposed to always log in if we are going to access 12374+ * private keys. However, we may need to log in even for 12375+ * accessing public keys in case that the CKF_LOGIN_REQUIRED 12376+ * flag is set. 12377+ */ 12378+ if (((pubkey_token_flags & CKF_LOGIN_REQUIRED) || 12379+ (is_private == CK_TRUE)) && 12380+ (~pubkey_token_flags & CKF_USER_PIN_INITIALIZED)) 12381+ { 12382+ PK11err(PK11_F_TOKEN_LOGIN, PK11_R_TOKEN_PIN_NOT_SET); 12383+ goto err; 12384+ } 12385+ 12386+ /* 12387+ * Note on locking: it is possible that more than one thread 12388+ * gets into pk11_get_pin() so we must deal with that. We 12389+ * cannot avoid it since we cannot guard fork() in there with 12390+ * a lock because we could end up in a dead lock in the 12391+ * child. Why? Remember we are in a multithreaded environment 12392+ * so we must lock all mutexes in the prefork function to 12393+ * avoid a situation in which a thread that did not call 12394+ * fork() held a lock, making future unlocking impossible. We 12395+ * lock right before C_Login(). 12396+ */ 12397+ if ((pubkey_token_flags & CKF_LOGIN_REQUIRED) || 12398+ (is_private == CK_TRUE)) 12399+ { 12400+ if (*login_done == CK_FALSE) 12401+ { 12402+ if ((pk11_pin == NULL) && (pk11_get_pin() == 0)) 12403+ { 12404+ PK11err(PK11_F_TOKEN_LOGIN, 12405+ PK11_R_TOKEN_PIN_NOT_PROVIDED); 12406+ goto err; 12407+ } 12408+ } 12409+ 12410+ /* 12411+ * Note that what we are logging into is the keystore from 12412+ * pubkey_SLOTID because we work with OP_RSA session type here. 12413+ * That also means that we can work with only one keystore in 12414+ * the engine. 12415+ * 12416+ * We must make sure we do not try to login more than once. 12417+ * Also, see the comment above on locking strategy. 12418+ */ 12419+ 12420+#ifndef NOPTHREADS 12421+ (void) pthread_mutex_lock(token_lock); 12422+#else 12423+ (void) pthread_mutex_lock(freelist_lock); 12424+#endif 12425+ if (*login_done == CK_FALSE) 12426+ { 12427+ if ((rv = pFuncList->C_Login(session, 12428+ CKU_USER, (CK_UTF8CHAR*)pk11_pin, 12429+ strlen(pk11_pin))) != CKR_OK) 12430+ { 12431+ PK11err_add_data(PK11_F_TOKEN_LOGIN, 12432+ PK11_R_TOKEN_LOGIN_FAILED, rv); 12433+ goto err_locked; 12434+ } 12435+ 12436+ *login_done = CK_TRUE; 12437+ 12438+ } 12439+#ifndef NOPTHREADS 12440+ (void) pthread_mutex_unlock(token_lock); 12441+#else 12442+ CRYPTO_w_unlock(CRYPTO_LOCK_PK11_ENGINE); 12443+#endif 12444+ } 12445+ else 12446+ { 12447+ /* 12448+ * If token does not require login we take it as the 12449+ * login was done. 12450+ */ 12451+ *login_done = CK_TRUE; 12452+ } 12453+ 12454+ return (1); 12455+ 12456+err_locked: 12457+ if (pk11_pin) { 12458+ memset(pk11_pin, 0, strlen(pk11_pin)); 12459+ OPENSSL_free((void*)pk11_pin); 12460+ } 12461+ pk11_pin = NULL; 12462+#ifndef NOPTHREADS 12463+ (void) pthread_mutex_unlock(token_lock); 12464+#else 12465+ CRYPTO_w_unlock(CRYPTO_LOCK_PK11_ENGINE); 12466+#endif 12467+err: 12468+ return (0); 12469+ } 12470+ 12471+/* 12472+ * Log in to the keystore in the child if we were logged in in the 12473+ * parent. There are similarities in the code with pk11_token_login() 12474+ * but still it is quite different so we need a separate function for 12475+ * this. 12476+ * 12477+ * Note that this function is called under the locked session mutex when fork is 12478+ * detected. That means that C_Login() will be called from the child just once. 12479+ * 12480+ * Returns: 12481+ * 1 on success 12482+ * 0 on failure 12483+ */ 12484+int 12485+pk11_token_relogin(CK_SESSION_HANDLE session) 12486+ { 12487+ CK_RV rv; 12488+ 12489+ if ((pk11_pin == NULL) && (pk11_get_pin() == 0)) 12490+ goto err; 12491+ 12492+#ifndef NOPTHREADS 12493+ (void) pthread_mutex_lock(token_lock); 12494+#else 12495+ (void) pthread_mutex_lock(freelist_lock); 12496+#endif 12497+ if ((rv = pFuncList->C_Login(session, CKU_USER, 12498+ (CK_UTF8CHAR_PTR)pk11_pin, strlen(pk11_pin))) != CKR_OK) 12499+ { 12500+ PK11err_add_data(PK11_F_TOKEN_RELOGIN, 12501+ PK11_R_TOKEN_LOGIN_FAILED, rv); 12502+#ifndef NOPTHREADS 12503+ (void) pthread_mutex_unlock(token_lock); 12504+#else 12505+ CRYPTO_w_unlock(CRYPTO_LOCK_PK11_ENGINE); 12506+#endif 12507+ goto err; 12508+ } 12509+#ifndef NOPTHREADS 12510+ (void) pthread_mutex_unlock(token_lock); 12511+#else 12512+ CRYPTO_w_unlock(CRYPTO_LOCK_PK11_ENGINE); 12513+#endif 12514+ 12515+ return (1); 12516+err: 12517+ return (0); 12518+ } 12519+ 12520+#ifdef OPENSSL_SYS_WIN32 12521+char *getpassphrase(const char *prompt) 12522+ { 12523+ static char buf[128]; 12524+ HANDLE h; 12525+ DWORD cc, mode; 12526+ int cnt; 12527+ 12528+ h = GetStdHandle(STD_INPUT_HANDLE); 12529+ fputs(prompt, stderr); 12530+ fflush(stderr); 12531+ fflush(stdout); 12532+ FlushConsoleInputBuffer(h); 12533+ GetConsoleMode(h, &mode); 12534+ SetConsoleMode(h, ENABLE_PROCESSED_INPUT); 12535+ 12536+ for (cnt = 0; cnt < sizeof(buf) - 1; cnt++) 12537+ { 12538+ ReadFile(h, buf + cnt, 1, &cc, NULL); 12539+ if (buf[cnt] == '\r') 12540+ break; 12541+ fputc('*', stdout); 12542+ fflush(stderr); 12543+ fflush(stdout); 12544+ } 12545+ 12546+ SetConsoleMode(h, mode); 12547+ buf[cnt] = '\0'; 12548+ fputs("\n", stderr); 12549+ return buf; 12550+ } 12551+#endif /* OPENSSL_SYS_WIN32 */ 12552+#endif /* OPENSSL_NO_HW_PK11SO */ 12553+#endif /* OPENSSL_NO_HW_PK11 */ 12554+#endif /* OPENSSL_NO_HW */ 12555Index: openssl/crypto/engine/pkcs11.h 12556diff -u /dev/null openssl/crypto/engine/pkcs11.h:1.1.1.1 12557--- /dev/null Mon Jan 16 18:53:42 2012 12558+++ openssl/crypto/engine/pkcs11.h Wed Oct 24 23:27:09 2007 12559@@ -0,0 +1,299 @@ 12560+/* pkcs11.h include file for PKCS #11. */ 12561+/* $Revision: 1.1.2.3 $ */ 12562+ 12563+/* License to copy and use this software is granted provided that it is 12564+ * identified as "RSA Security Inc. PKCS #11 Cryptographic Token Interface 12565+ * (Cryptoki)" in all material mentioning or referencing this software. 12566+ 12567+ * License is also granted to make and use derivative works provided that 12568+ * such works are identified as "derived from the RSA Security Inc. PKCS #11 12569+ * Cryptographic Token Interface (Cryptoki)" in all material mentioning or 12570+ * referencing the derived work. 12571+ 12572+ * RSA Security Inc. makes no representations concerning either the 12573+ * merchantability of this software or the suitability of this software for 12574+ * any particular purpose. It is provided "as is" without express or implied 12575+ * warranty of any kind. 12576+ */ 12577+ 12578+#ifndef _PKCS11_H_ 12579+#define _PKCS11_H_ 1 12580+ 12581+#ifdef __cplusplus 12582+extern "C" { 12583+#endif 12584+ 12585+/* Before including this file (pkcs11.h) (or pkcs11t.h by 12586+ * itself), 6 platform-specific macros must be defined. These 12587+ * macros are described below, and typical definitions for them 12588+ * are also given. Be advised that these definitions can depend 12589+ * on both the platform and the compiler used (and possibly also 12590+ * on whether a Cryptoki library is linked statically or 12591+ * dynamically). 12592+ * 12593+ * In addition to defining these 6 macros, the packing convention 12594+ * for Cryptoki structures should be set. The Cryptoki 12595+ * convention on packing is that structures should be 1-byte 12596+ * aligned. 12597+ * 12598+ * If you're using Microsoft Developer Studio 5.0 to produce 12599+ * Win32 stuff, this might be done by using the following 12600+ * preprocessor directive before including pkcs11.h or pkcs11t.h: 12601+ * 12602+ * #pragma pack(push, cryptoki, 1) 12603+ * 12604+ * and using the following preprocessor directive after including 12605+ * pkcs11.h or pkcs11t.h: 12606+ * 12607+ * #pragma pack(pop, cryptoki) 12608+ * 12609+ * If you're using an earlier version of Microsoft Developer 12610+ * Studio to produce Win16 stuff, this might be done by using 12611+ * the following preprocessor directive before including 12612+ * pkcs11.h or pkcs11t.h: 12613+ * 12614+ * #pragma pack(1) 12615+ * 12616+ * In a UNIX environment, you're on your own for this. You might 12617+ * not need to do (or be able to do!) anything. 12618+ * 12619+ * 12620+ * Now for the macros: 12621+ * 12622+ * 12623+ * 1. CK_PTR: The indirection string for making a pointer to an 12624+ * object. It can be used like this: 12625+ * 12626+ * typedef CK_BYTE CK_PTR CK_BYTE_PTR; 12627+ * 12628+ * If you're using Microsoft Developer Studio 5.0 to produce 12629+ * Win32 stuff, it might be defined by: 12630+ * 12631+ * #define CK_PTR * 12632+ * 12633+ * If you're using an earlier version of Microsoft Developer 12634+ * Studio to produce Win16 stuff, it might be defined by: 12635+ * 12636+ * #define CK_PTR far * 12637+ * 12638+ * In a typical UNIX environment, it might be defined by: 12639+ * 12640+ * #define CK_PTR * 12641+ * 12642+ * 12643+ * 2. CK_DEFINE_FUNCTION(returnType, name): A macro which makes 12644+ * an exportable Cryptoki library function definition out of a 12645+ * return type and a function name. It should be used in the 12646+ * following fashion to define the exposed Cryptoki functions in 12647+ * a Cryptoki library: 12648+ * 12649+ * CK_DEFINE_FUNCTION(CK_RV, C_Initialize)( 12650+ * CK_VOID_PTR pReserved 12651+ * ) 12652+ * { 12653+ * ... 12654+ * } 12655+ * 12656+ * If you're using Microsoft Developer Studio 5.0 to define a 12657+ * function in a Win32 Cryptoki .dll, it might be defined by: 12658+ * 12659+ * #define CK_DEFINE_FUNCTION(returnType, name) \ 12660+ * returnType __declspec(dllexport) name 12661+ * 12662+ * If you're using an earlier version of Microsoft Developer 12663+ * Studio to define a function in a Win16 Cryptoki .dll, it 12664+ * might be defined by: 12665+ * 12666+ * #define CK_DEFINE_FUNCTION(returnType, name) \ 12667+ * returnType __export _far _pascal name 12668+ * 12669+ * In a UNIX environment, it might be defined by: 12670+ * 12671+ * #define CK_DEFINE_FUNCTION(returnType, name) \ 12672+ * returnType name 12673+ * 12674+ * 12675+ * 3. CK_DECLARE_FUNCTION(returnType, name): A macro which makes 12676+ * an importable Cryptoki library function declaration out of a 12677+ * return type and a function name. It should be used in the 12678+ * following fashion: 12679+ * 12680+ * extern CK_DECLARE_FUNCTION(CK_RV, C_Initialize)( 12681+ * CK_VOID_PTR pReserved 12682+ * ); 12683+ * 12684+ * If you're using Microsoft Developer Studio 5.0 to declare a 12685+ * function in a Win32 Cryptoki .dll, it might be defined by: 12686+ * 12687+ * #define CK_DECLARE_FUNCTION(returnType, name) \ 12688+ * returnType __declspec(dllimport) name 12689+ * 12690+ * If you're using an earlier version of Microsoft Developer 12691+ * Studio to declare a function in a Win16 Cryptoki .dll, it 12692+ * might be defined by: 12693+ * 12694+ * #define CK_DECLARE_FUNCTION(returnType, name) \ 12695+ * returnType __export _far _pascal name 12696+ * 12697+ * In a UNIX environment, it might be defined by: 12698+ * 12699+ * #define CK_DECLARE_FUNCTION(returnType, name) \ 12700+ * returnType name 12701+ * 12702+ * 12703+ * 4. CK_DECLARE_FUNCTION_POINTER(returnType, name): A macro 12704+ * which makes a Cryptoki API function pointer declaration or 12705+ * function pointer type declaration out of a return type and a 12706+ * function name. It should be used in the following fashion: 12707+ * 12708+ * // Define funcPtr to be a pointer to a Cryptoki API function 12709+ * // taking arguments args and returning CK_RV. 12710+ * CK_DECLARE_FUNCTION_POINTER(CK_RV, funcPtr)(args); 12711+ * 12712+ * or 12713+ * 12714+ * // Define funcPtrType to be the type of a pointer to a 12715+ * // Cryptoki API function taking arguments args and returning 12716+ * // CK_RV, and then define funcPtr to be a variable of type 12717+ * // funcPtrType. 12718+ * typedef CK_DECLARE_FUNCTION_POINTER(CK_RV, funcPtrType)(args); 12719+ * funcPtrType funcPtr; 12720+ * 12721+ * If you're using Microsoft Developer Studio 5.0 to access 12722+ * functions in a Win32 Cryptoki .dll, in might be defined by: 12723+ * 12724+ * #define CK_DECLARE_FUNCTION_POINTER(returnType, name) \ 12725+ * returnType __declspec(dllimport) (* name) 12726+ * 12727+ * If you're using an earlier version of Microsoft Developer 12728+ * Studio to access functions in a Win16 Cryptoki .dll, it might 12729+ * be defined by: 12730+ * 12731+ * #define CK_DECLARE_FUNCTION_POINTER(returnType, name) \ 12732+ * returnType __export _far _pascal (* name) 12733+ * 12734+ * In a UNIX environment, it might be defined by: 12735+ * 12736+ * #define CK_DECLARE_FUNCTION_POINTER(returnType, name) \ 12737+ * returnType (* name) 12738+ * 12739+ * 12740+ * 5. CK_CALLBACK_FUNCTION(returnType, name): A macro which makes 12741+ * a function pointer type for an application callback out of 12742+ * a return type for the callback and a name for the callback. 12743+ * It should be used in the following fashion: 12744+ * 12745+ * CK_CALLBACK_FUNCTION(CK_RV, myCallback)(args); 12746+ * 12747+ * to declare a function pointer, myCallback, to a callback 12748+ * which takes arguments args and returns a CK_RV. It can also 12749+ * be used like this: 12750+ * 12751+ * typedef CK_CALLBACK_FUNCTION(CK_RV, myCallbackType)(args); 12752+ * myCallbackType myCallback; 12753+ * 12754+ * If you're using Microsoft Developer Studio 5.0 to do Win32 12755+ * Cryptoki development, it might be defined by: 12756+ * 12757+ * #define CK_CALLBACK_FUNCTION(returnType, name) \ 12758+ * returnType (* name) 12759+ * 12760+ * If you're using an earlier version of Microsoft Developer 12761+ * Studio to do Win16 development, it might be defined by: 12762+ * 12763+ * #define CK_CALLBACK_FUNCTION(returnType, name) \ 12764+ * returnType _far _pascal (* name) 12765+ * 12766+ * In a UNIX environment, it might be defined by: 12767+ * 12768+ * #define CK_CALLBACK_FUNCTION(returnType, name) \ 12769+ * returnType (* name) 12770+ * 12771+ * 12772+ * 6. NULL_PTR: This macro is the value of a NULL pointer. 12773+ * 12774+ * In any ANSI/ISO C environment (and in many others as well), 12775+ * this should best be defined by 12776+ * 12777+ * #ifndef NULL_PTR 12778+ * #define NULL_PTR 0 12779+ * #endif 12780+ */ 12781+ 12782+ 12783+/* All the various Cryptoki types and #define'd values are in the 12784+ * file pkcs11t.h. */ 12785+#include "pkcs11t.h" 12786+ 12787+#define __PASTE(x,y) x##y 12788+ 12789+ 12790+/* ============================================================== 12791+ * Define the "extern" form of all the entry points. 12792+ * ============================================================== 12793+ */ 12794+ 12795+#define CK_NEED_ARG_LIST 1 12796+#define CK_PKCS11_FUNCTION_INFO(name) \ 12797+ extern CK_DECLARE_FUNCTION(CK_RV, name) 12798+ 12799+/* pkcs11f.h has all the information about the Cryptoki 12800+ * function prototypes. */ 12801+#include "pkcs11f.h" 12802+ 12803+#undef CK_NEED_ARG_LIST 12804+#undef CK_PKCS11_FUNCTION_INFO 12805+ 12806+ 12807+/* ============================================================== 12808+ * Define the typedef form of all the entry points. That is, for 12809+ * each Cryptoki function C_XXX, define a type CK_C_XXX which is 12810+ * a pointer to that kind of function. 12811+ * ============================================================== 12812+ */ 12813+ 12814+#define CK_NEED_ARG_LIST 1 12815+#define CK_PKCS11_FUNCTION_INFO(name) \ 12816+ typedef CK_DECLARE_FUNCTION_POINTER(CK_RV, __PASTE(CK_,name)) 12817+ 12818+/* pkcs11f.h has all the information about the Cryptoki 12819+ * function prototypes. */ 12820+#include "pkcs11f.h" 12821+ 12822+#undef CK_NEED_ARG_LIST 12823+#undef CK_PKCS11_FUNCTION_INFO 12824+ 12825+ 12826+/* ============================================================== 12827+ * Define structed vector of entry points. A CK_FUNCTION_LIST 12828+ * contains a CK_VERSION indicating a library's Cryptoki version 12829+ * and then a whole slew of function pointers to the routines in 12830+ * the library. This type was declared, but not defined, in 12831+ * pkcs11t.h. 12832+ * ============================================================== 12833+ */ 12834+ 12835+#define CK_PKCS11_FUNCTION_INFO(name) \ 12836+ __PASTE(CK_,name) name; 12837+ 12838+struct CK_FUNCTION_LIST { 12839+ 12840+ CK_VERSION version; /* Cryptoki version */ 12841+ 12842+/* Pile all the function pointers into the CK_FUNCTION_LIST. */ 12843+/* pkcs11f.h has all the information about the Cryptoki 12844+ * function prototypes. */ 12845+#include "pkcs11f.h" 12846+ 12847+}; 12848+ 12849+#undef CK_PKCS11_FUNCTION_INFO 12850+ 12851+ 12852+#undef __PASTE 12853+ 12854+#ifdef __cplusplus 12855+} 12856+#endif 12857+ 12858+#endif 12859Index: openssl/crypto/engine/pkcs11f.h 12860diff -u /dev/null openssl/crypto/engine/pkcs11f.h:1.1.1.1 12861--- /dev/null Mon Jan 16 18:53:42 2012 12862+++ openssl/crypto/engine/pkcs11f.h Wed Oct 24 23:27:09 2007 12863@@ -0,0 +1,912 @@ 12864+/* pkcs11f.h include file for PKCS #11. */ 12865+/* $Revision: 1.1.2.3 $ */ 12866+ 12867+/* License to copy and use this software is granted provided that it is 12868+ * identified as "RSA Security Inc. PKCS #11 Cryptographic Token Interface 12869+ * (Cryptoki)" in all material mentioning or referencing this software. 12870+ 12871+ * License is also granted to make and use derivative works provided that 12872+ * such works are identified as "derived from the RSA Security Inc. PKCS #11 12873+ * Cryptographic Token Interface (Cryptoki)" in all material mentioning or 12874+ * referencing the derived work. 12875+ 12876+ * RSA Security Inc. makes no representations concerning either the 12877+ * merchantability of this software or the suitability of this software for 12878+ * any particular purpose. It is provided "as is" without express or implied 12879+ * warranty of any kind. 12880+ */ 12881+ 12882+/* This header file contains pretty much everything about all the */ 12883+/* Cryptoki function prototypes. Because this information is */ 12884+/* used for more than just declaring function prototypes, the */ 12885+/* order of the functions appearing herein is important, and */ 12886+/* should not be altered. */ 12887+ 12888+/* General-purpose */ 12889+ 12890+/* C_Initialize initializes the Cryptoki library. */ 12891+CK_PKCS11_FUNCTION_INFO(C_Initialize) 12892+#ifdef CK_NEED_ARG_LIST 12893+( 12894+ CK_VOID_PTR pInitArgs /* if this is not NULL_PTR, it gets 12895+ * cast to CK_C_INITIALIZE_ARGS_PTR 12896+ * and dereferenced */ 12897+); 12898+#endif 12899+ 12900+ 12901+/* C_Finalize indicates that an application is done with the 12902+ * Cryptoki library. */ 12903+CK_PKCS11_FUNCTION_INFO(C_Finalize) 12904+#ifdef CK_NEED_ARG_LIST 12905+( 12906+ CK_VOID_PTR pReserved /* reserved. Should be NULL_PTR */ 12907+); 12908+#endif 12909+ 12910+ 12911+/* C_GetInfo returns general information about Cryptoki. */ 12912+CK_PKCS11_FUNCTION_INFO(C_GetInfo) 12913+#ifdef CK_NEED_ARG_LIST 12914+( 12915+ CK_INFO_PTR pInfo /* location that receives information */ 12916+); 12917+#endif 12918+ 12919+ 12920+/* C_GetFunctionList returns the function list. */ 12921+CK_PKCS11_FUNCTION_INFO(C_GetFunctionList) 12922+#ifdef CK_NEED_ARG_LIST 12923+( 12924+ CK_FUNCTION_LIST_PTR_PTR ppFunctionList /* receives pointer to 12925+ * function list */ 12926+); 12927+#endif 12928+ 12929+ 12930+ 12931+/* Slot and token management */ 12932+ 12933+/* C_GetSlotList obtains a list of slots in the system. */ 12934+CK_PKCS11_FUNCTION_INFO(C_GetSlotList) 12935+#ifdef CK_NEED_ARG_LIST 12936+( 12937+ CK_BBOOL tokenPresent, /* only slots with tokens? */ 12938+ CK_SLOT_ID_PTR pSlotList, /* receives array of slot IDs */ 12939+ CK_ULONG_PTR pulCount /* receives number of slots */ 12940+); 12941+#endif 12942+ 12943+ 12944+/* C_GetSlotInfo obtains information about a particular slot in 12945+ * the system. */ 12946+CK_PKCS11_FUNCTION_INFO(C_GetSlotInfo) 12947+#ifdef CK_NEED_ARG_LIST 12948+( 12949+ CK_SLOT_ID slotID, /* the ID of the slot */ 12950+ CK_SLOT_INFO_PTR pInfo /* receives the slot information */ 12951+); 12952+#endif 12953+ 12954+ 12955+/* C_GetTokenInfo obtains information about a particular token 12956+ * in the system. */ 12957+CK_PKCS11_FUNCTION_INFO(C_GetTokenInfo) 12958+#ifdef CK_NEED_ARG_LIST 12959+( 12960+ CK_SLOT_ID slotID, /* ID of the token's slot */ 12961+ CK_TOKEN_INFO_PTR pInfo /* receives the token information */ 12962+); 12963+#endif 12964+ 12965+ 12966+/* C_GetMechanismList obtains a list of mechanism types 12967+ * supported by a token. */ 12968+CK_PKCS11_FUNCTION_INFO(C_GetMechanismList) 12969+#ifdef CK_NEED_ARG_LIST 12970+( 12971+ CK_SLOT_ID slotID, /* ID of token's slot */ 12972+ CK_MECHANISM_TYPE_PTR pMechanismList, /* gets mech. array */ 12973+ CK_ULONG_PTR pulCount /* gets # of mechs. */ 12974+); 12975+#endif 12976+ 12977+ 12978+/* C_GetMechanismInfo obtains information about a particular 12979+ * mechanism possibly supported by a token. */ 12980+CK_PKCS11_FUNCTION_INFO(C_GetMechanismInfo) 12981+#ifdef CK_NEED_ARG_LIST 12982+( 12983+ CK_SLOT_ID slotID, /* ID of the token's slot */ 12984+ CK_MECHANISM_TYPE type, /* type of mechanism */ 12985+ CK_MECHANISM_INFO_PTR pInfo /* receives mechanism info */ 12986+); 12987+#endif 12988+ 12989+ 12990+/* C_InitToken initializes a token. */ 12991+CK_PKCS11_FUNCTION_INFO(C_InitToken) 12992+#ifdef CK_NEED_ARG_LIST 12993+/* pLabel changed from CK_CHAR_PTR to CK_UTF8CHAR_PTR for v2.10 */ 12994+( 12995+ CK_SLOT_ID slotID, /* ID of the token's slot */ 12996+ CK_UTF8CHAR_PTR pPin, /* the SO's initial PIN */ 12997+ CK_ULONG ulPinLen, /* length in bytes of the PIN */ 12998+ CK_UTF8CHAR_PTR pLabel /* 32-byte token label (blank padded) */ 12999+); 13000+#endif 13001+ 13002+ 13003+/* C_InitPIN initializes the normal user's PIN. */ 13004+CK_PKCS11_FUNCTION_INFO(C_InitPIN) 13005+#ifdef CK_NEED_ARG_LIST 13006+( 13007+ CK_SESSION_HANDLE hSession, /* the session's handle */ 13008+ CK_UTF8CHAR_PTR pPin, /* the normal user's PIN */ 13009+ CK_ULONG ulPinLen /* length in bytes of the PIN */ 13010+); 13011+#endif 13012+ 13013+ 13014+/* C_SetPIN modifies the PIN of the user who is logged in. */ 13015+CK_PKCS11_FUNCTION_INFO(C_SetPIN) 13016+#ifdef CK_NEED_ARG_LIST 13017+( 13018+ CK_SESSION_HANDLE hSession, /* the session's handle */ 13019+ CK_UTF8CHAR_PTR pOldPin, /* the old PIN */ 13020+ CK_ULONG ulOldLen, /* length of the old PIN */ 13021+ CK_UTF8CHAR_PTR pNewPin, /* the new PIN */ 13022+ CK_ULONG ulNewLen /* length of the new PIN */ 13023+); 13024+#endif 13025+ 13026+ 13027+ 13028+/* Session management */ 13029+ 13030+/* C_OpenSession opens a session between an application and a 13031+ * token. */ 13032+CK_PKCS11_FUNCTION_INFO(C_OpenSession) 13033+#ifdef CK_NEED_ARG_LIST 13034+( 13035+ CK_SLOT_ID slotID, /* the slot's ID */ 13036+ CK_FLAGS flags, /* from CK_SESSION_INFO */ 13037+ CK_VOID_PTR pApplication, /* passed to callback */ 13038+ CK_NOTIFY Notify, /* callback function */ 13039+ CK_SESSION_HANDLE_PTR phSession /* gets session handle */ 13040+); 13041+#endif 13042+ 13043+ 13044+/* C_CloseSession closes a session between an application and a 13045+ * token. */ 13046+CK_PKCS11_FUNCTION_INFO(C_CloseSession) 13047+#ifdef CK_NEED_ARG_LIST 13048+( 13049+ CK_SESSION_HANDLE hSession /* the session's handle */ 13050+); 13051+#endif 13052+ 13053+ 13054+/* C_CloseAllSessions closes all sessions with a token. */ 13055+CK_PKCS11_FUNCTION_INFO(C_CloseAllSessions) 13056+#ifdef CK_NEED_ARG_LIST 13057+( 13058+ CK_SLOT_ID slotID /* the token's slot */ 13059+); 13060+#endif 13061+ 13062+ 13063+/* C_GetSessionInfo obtains information about the session. */ 13064+CK_PKCS11_FUNCTION_INFO(C_GetSessionInfo) 13065+#ifdef CK_NEED_ARG_LIST 13066+( 13067+ CK_SESSION_HANDLE hSession, /* the session's handle */ 13068+ CK_SESSION_INFO_PTR pInfo /* receives session info */ 13069+); 13070+#endif 13071+ 13072+ 13073+/* C_GetOperationState obtains the state of the cryptographic operation 13074+ * in a session. */ 13075+CK_PKCS11_FUNCTION_INFO(C_GetOperationState) 13076+#ifdef CK_NEED_ARG_LIST 13077+( 13078+ CK_SESSION_HANDLE hSession, /* session's handle */ 13079+ CK_BYTE_PTR pOperationState, /* gets state */ 13080+ CK_ULONG_PTR pulOperationStateLen /* gets state length */ 13081+); 13082+#endif 13083+ 13084+ 13085+/* C_SetOperationState restores the state of the cryptographic 13086+ * operation in a session. */ 13087+CK_PKCS11_FUNCTION_INFO(C_SetOperationState) 13088+#ifdef CK_NEED_ARG_LIST 13089+( 13090+ CK_SESSION_HANDLE hSession, /* session's handle */ 13091+ CK_BYTE_PTR pOperationState, /* holds state */ 13092+ CK_ULONG ulOperationStateLen, /* holds state length */ 13093+ CK_OBJECT_HANDLE hEncryptionKey, /* en/decryption key */ 13094+ CK_OBJECT_HANDLE hAuthenticationKey /* sign/verify key */ 13095+); 13096+#endif 13097+ 13098+ 13099+/* C_Login logs a user into a token. */ 13100+CK_PKCS11_FUNCTION_INFO(C_Login) 13101+#ifdef CK_NEED_ARG_LIST 13102+( 13103+ CK_SESSION_HANDLE hSession, /* the session's handle */ 13104+ CK_USER_TYPE userType, /* the user type */ 13105+ CK_UTF8CHAR_PTR pPin, /* the user's PIN */ 13106+ CK_ULONG ulPinLen /* the length of the PIN */ 13107+); 13108+#endif 13109+ 13110+ 13111+/* C_Logout logs a user out from a token. */ 13112+CK_PKCS11_FUNCTION_INFO(C_Logout) 13113+#ifdef CK_NEED_ARG_LIST 13114+( 13115+ CK_SESSION_HANDLE hSession /* the session's handle */ 13116+); 13117+#endif 13118+ 13119+ 13120+ 13121+/* Object management */ 13122+ 13123+/* C_CreateObject creates a new object. */ 13124+CK_PKCS11_FUNCTION_INFO(C_CreateObject) 13125+#ifdef CK_NEED_ARG_LIST 13126+( 13127+ CK_SESSION_HANDLE hSession, /* the session's handle */ 13128+ CK_ATTRIBUTE_PTR pTemplate, /* the object's template */ 13129+ CK_ULONG ulCount, /* attributes in template */ 13130+ CK_OBJECT_HANDLE_PTR phObject /* gets new object's handle. */ 13131+); 13132+#endif 13133+ 13134+ 13135+/* C_CopyObject copies an object, creating a new object for the 13136+ * copy. */ 13137+CK_PKCS11_FUNCTION_INFO(C_CopyObject) 13138+#ifdef CK_NEED_ARG_LIST 13139+( 13140+ CK_SESSION_HANDLE hSession, /* the session's handle */ 13141+ CK_OBJECT_HANDLE hObject, /* the object's handle */ 13142+ CK_ATTRIBUTE_PTR pTemplate, /* template for new object */ 13143+ CK_ULONG ulCount, /* attributes in template */ 13144+ CK_OBJECT_HANDLE_PTR phNewObject /* receives handle of copy */ 13145+); 13146+#endif 13147+ 13148+ 13149+/* C_DestroyObject destroys an object. */ 13150+CK_PKCS11_FUNCTION_INFO(C_DestroyObject) 13151+#ifdef CK_NEED_ARG_LIST 13152+( 13153+ CK_SESSION_HANDLE hSession, /* the session's handle */ 13154+ CK_OBJECT_HANDLE hObject /* the object's handle */ 13155+); 13156+#endif 13157+ 13158+ 13159+/* C_GetObjectSize gets the size of an object in bytes. */ 13160+CK_PKCS11_FUNCTION_INFO(C_GetObjectSize) 13161+#ifdef CK_NEED_ARG_LIST 13162+( 13163+ CK_SESSION_HANDLE hSession, /* the session's handle */ 13164+ CK_OBJECT_HANDLE hObject, /* the object's handle */ 13165+ CK_ULONG_PTR pulSize /* receives size of object */ 13166+); 13167+#endif 13168+ 13169+ 13170+/* C_GetAttributeValue obtains the value of one or more object 13171+ * attributes. */ 13172+CK_PKCS11_FUNCTION_INFO(C_GetAttributeValue) 13173+#ifdef CK_NEED_ARG_LIST 13174+( 13175+ CK_SESSION_HANDLE hSession, /* the session's handle */ 13176+ CK_OBJECT_HANDLE hObject, /* the object's handle */ 13177+ CK_ATTRIBUTE_PTR pTemplate, /* specifies attrs; gets vals */ 13178+ CK_ULONG ulCount /* attributes in template */ 13179+); 13180+#endif 13181+ 13182+ 13183+/* C_SetAttributeValue modifies the value of one or more object 13184+ * attributes */ 13185+CK_PKCS11_FUNCTION_INFO(C_SetAttributeValue) 13186+#ifdef CK_NEED_ARG_LIST 13187+( 13188+ CK_SESSION_HANDLE hSession, /* the session's handle */ 13189+ CK_OBJECT_HANDLE hObject, /* the object's handle */ 13190+ CK_ATTRIBUTE_PTR pTemplate, /* specifies attrs and values */ 13191+ CK_ULONG ulCount /* attributes in template */ 13192+); 13193+#endif 13194+ 13195+ 13196+/* C_FindObjectsInit initializes a search for token and session 13197+ * objects that match a template. */ 13198+CK_PKCS11_FUNCTION_INFO(C_FindObjectsInit) 13199+#ifdef CK_NEED_ARG_LIST 13200+( 13201+ CK_SESSION_HANDLE hSession, /* the session's handle */ 13202+ CK_ATTRIBUTE_PTR pTemplate, /* attribute values to match */ 13203+ CK_ULONG ulCount /* attrs in search template */ 13204+); 13205+#endif 13206+ 13207+ 13208+/* C_FindObjects continues a search for token and session 13209+ * objects that match a template, obtaining additional object 13210+ * handles. */ 13211+CK_PKCS11_FUNCTION_INFO(C_FindObjects) 13212+#ifdef CK_NEED_ARG_LIST 13213+( 13214+ CK_SESSION_HANDLE hSession, /* session's handle */ 13215+ CK_OBJECT_HANDLE_PTR phObject, /* gets obj. handles */ 13216+ CK_ULONG ulMaxObjectCount, /* max handles to get */ 13217+ CK_ULONG_PTR pulObjectCount /* actual # returned */ 13218+); 13219+#endif 13220+ 13221+ 13222+/* C_FindObjectsFinal finishes a search for token and session 13223+ * objects. */ 13224+CK_PKCS11_FUNCTION_INFO(C_FindObjectsFinal) 13225+#ifdef CK_NEED_ARG_LIST 13226+( 13227+ CK_SESSION_HANDLE hSession /* the session's handle */ 13228+); 13229+#endif 13230+ 13231+ 13232+ 13233+/* Encryption and decryption */ 13234+ 13235+/* C_EncryptInit initializes an encryption operation. */ 13236+CK_PKCS11_FUNCTION_INFO(C_EncryptInit) 13237+#ifdef CK_NEED_ARG_LIST 13238+( 13239+ CK_SESSION_HANDLE hSession, /* the session's handle */ 13240+ CK_MECHANISM_PTR pMechanism, /* the encryption mechanism */ 13241+ CK_OBJECT_HANDLE hKey /* handle of encryption key */ 13242+); 13243+#endif 13244+ 13245+ 13246+/* C_Encrypt encrypts single-part data. */ 13247+CK_PKCS11_FUNCTION_INFO(C_Encrypt) 13248+#ifdef CK_NEED_ARG_LIST 13249+( 13250+ CK_SESSION_HANDLE hSession, /* session's handle */ 13251+ CK_BYTE_PTR pData, /* the plaintext data */ 13252+ CK_ULONG ulDataLen, /* bytes of plaintext */ 13253+ CK_BYTE_PTR pEncryptedData, /* gets ciphertext */ 13254+ CK_ULONG_PTR pulEncryptedDataLen /* gets c-text size */ 13255+); 13256+#endif 13257+ 13258+ 13259+/* C_EncryptUpdate continues a multiple-part encryption 13260+ * operation. */ 13261+CK_PKCS11_FUNCTION_INFO(C_EncryptUpdate) 13262+#ifdef CK_NEED_ARG_LIST 13263+( 13264+ CK_SESSION_HANDLE hSession, /* session's handle */ 13265+ CK_BYTE_PTR pPart, /* the plaintext data */ 13266+ CK_ULONG ulPartLen, /* plaintext data len */ 13267+ CK_BYTE_PTR pEncryptedPart, /* gets ciphertext */ 13268+ CK_ULONG_PTR pulEncryptedPartLen /* gets c-text size */ 13269+); 13270+#endif 13271+ 13272+ 13273+/* C_EncryptFinal finishes a multiple-part encryption 13274+ * operation. */ 13275+CK_PKCS11_FUNCTION_INFO(C_EncryptFinal) 13276+#ifdef CK_NEED_ARG_LIST 13277+( 13278+ CK_SESSION_HANDLE hSession, /* session handle */ 13279+ CK_BYTE_PTR pLastEncryptedPart, /* last c-text */ 13280+ CK_ULONG_PTR pulLastEncryptedPartLen /* gets last size */ 13281+); 13282+#endif 13283+ 13284+ 13285+/* C_DecryptInit initializes a decryption operation. */ 13286+CK_PKCS11_FUNCTION_INFO(C_DecryptInit) 13287+#ifdef CK_NEED_ARG_LIST 13288+( 13289+ CK_SESSION_HANDLE hSession, /* the session's handle */ 13290+ CK_MECHANISM_PTR pMechanism, /* the decryption mechanism */ 13291+ CK_OBJECT_HANDLE hKey /* handle of decryption key */ 13292+); 13293+#endif 13294+ 13295+ 13296+/* C_Decrypt decrypts encrypted data in a single part. */ 13297+CK_PKCS11_FUNCTION_INFO(C_Decrypt) 13298+#ifdef CK_NEED_ARG_LIST 13299+( 13300+ CK_SESSION_HANDLE hSession, /* session's handle */ 13301+ CK_BYTE_PTR pEncryptedData, /* ciphertext */ 13302+ CK_ULONG ulEncryptedDataLen, /* ciphertext length */ 13303+ CK_BYTE_PTR pData, /* gets plaintext */ 13304+ CK_ULONG_PTR pulDataLen /* gets p-text size */ 13305+); 13306+#endif 13307+ 13308+ 13309+/* C_DecryptUpdate continues a multiple-part decryption 13310+ * operation. */ 13311+CK_PKCS11_FUNCTION_INFO(C_DecryptUpdate) 13312+#ifdef CK_NEED_ARG_LIST 13313+( 13314+ CK_SESSION_HANDLE hSession, /* session's handle */ 13315+ CK_BYTE_PTR pEncryptedPart, /* encrypted data */ 13316+ CK_ULONG ulEncryptedPartLen, /* input length */ 13317+ CK_BYTE_PTR pPart, /* gets plaintext */ 13318+ CK_ULONG_PTR pulPartLen /* p-text size */ 13319+); 13320+#endif 13321+ 13322+ 13323+/* C_DecryptFinal finishes a multiple-part decryption 13324+ * operation. */ 13325+CK_PKCS11_FUNCTION_INFO(C_DecryptFinal) 13326+#ifdef CK_NEED_ARG_LIST 13327+( 13328+ CK_SESSION_HANDLE hSession, /* the session's handle */ 13329+ CK_BYTE_PTR pLastPart, /* gets plaintext */ 13330+ CK_ULONG_PTR pulLastPartLen /* p-text size */ 13331+); 13332+#endif 13333+ 13334+ 13335+ 13336+/* Message digesting */ 13337+ 13338+/* C_DigestInit initializes a message-digesting operation. */ 13339+CK_PKCS11_FUNCTION_INFO(C_DigestInit) 13340+#ifdef CK_NEED_ARG_LIST 13341+( 13342+ CK_SESSION_HANDLE hSession, /* the session's handle */ 13343+ CK_MECHANISM_PTR pMechanism /* the digesting mechanism */ 13344+); 13345+#endif 13346+ 13347+ 13348+/* C_Digest digests data in a single part. */ 13349+CK_PKCS11_FUNCTION_INFO(C_Digest) 13350+#ifdef CK_NEED_ARG_LIST 13351+( 13352+ CK_SESSION_HANDLE hSession, /* the session's handle */ 13353+ CK_BYTE_PTR pData, /* data to be digested */ 13354+ CK_ULONG ulDataLen, /* bytes of data to digest */ 13355+ CK_BYTE_PTR pDigest, /* gets the message digest */ 13356+ CK_ULONG_PTR pulDigestLen /* gets digest length */ 13357+); 13358+#endif 13359+ 13360+ 13361+/* C_DigestUpdate continues a multiple-part message-digesting 13362+ * operation. */ 13363+CK_PKCS11_FUNCTION_INFO(C_DigestUpdate) 13364+#ifdef CK_NEED_ARG_LIST 13365+( 13366+ CK_SESSION_HANDLE hSession, /* the session's handle */ 13367+ CK_BYTE_PTR pPart, /* data to be digested */ 13368+ CK_ULONG ulPartLen /* bytes of data to be digested */ 13369+); 13370+#endif 13371+ 13372+ 13373+/* C_DigestKey continues a multi-part message-digesting 13374+ * operation, by digesting the value of a secret key as part of 13375+ * the data already digested. */ 13376+CK_PKCS11_FUNCTION_INFO(C_DigestKey) 13377+#ifdef CK_NEED_ARG_LIST 13378+( 13379+ CK_SESSION_HANDLE hSession, /* the session's handle */ 13380+ CK_OBJECT_HANDLE hKey /* secret key to digest */ 13381+); 13382+#endif 13383+ 13384+ 13385+/* C_DigestFinal finishes a multiple-part message-digesting 13386+ * operation. */ 13387+CK_PKCS11_FUNCTION_INFO(C_DigestFinal) 13388+#ifdef CK_NEED_ARG_LIST 13389+( 13390+ CK_SESSION_HANDLE hSession, /* the session's handle */ 13391+ CK_BYTE_PTR pDigest, /* gets the message digest */ 13392+ CK_ULONG_PTR pulDigestLen /* gets byte count of digest */ 13393+); 13394+#endif 13395+ 13396+ 13397+ 13398+/* Signing and MACing */ 13399+ 13400+/* C_SignInit initializes a signature (private key encryption) 13401+ * operation, where the signature is (will be) an appendix to 13402+ * the data, and plaintext cannot be recovered from the 13403+ *signature. */ 13404+CK_PKCS11_FUNCTION_INFO(C_SignInit) 13405+#ifdef CK_NEED_ARG_LIST 13406+( 13407+ CK_SESSION_HANDLE hSession, /* the session's handle */ 13408+ CK_MECHANISM_PTR pMechanism, /* the signature mechanism */ 13409+ CK_OBJECT_HANDLE hKey /* handle of signature key */ 13410+); 13411+#endif 13412+ 13413+ 13414+/* C_Sign signs (encrypts with private key) data in a single 13415+ * part, where the signature is (will be) an appendix to the 13416+ * data, and plaintext cannot be recovered from the signature. */ 13417+CK_PKCS11_FUNCTION_INFO(C_Sign) 13418+#ifdef CK_NEED_ARG_LIST 13419+( 13420+ CK_SESSION_HANDLE hSession, /* the session's handle */ 13421+ CK_BYTE_PTR pData, /* the data to sign */ 13422+ CK_ULONG ulDataLen, /* count of bytes to sign */ 13423+ CK_BYTE_PTR pSignature, /* gets the signature */ 13424+ CK_ULONG_PTR pulSignatureLen /* gets signature length */ 13425+); 13426+#endif 13427+ 13428+ 13429+/* C_SignUpdate continues a multiple-part signature operation, 13430+ * where the signature is (will be) an appendix to the data, 13431+ * and plaintext cannot be recovered from the signature. */ 13432+CK_PKCS11_FUNCTION_INFO(C_SignUpdate) 13433+#ifdef CK_NEED_ARG_LIST 13434+( 13435+ CK_SESSION_HANDLE hSession, /* the session's handle */ 13436+ CK_BYTE_PTR pPart, /* the data to sign */ 13437+ CK_ULONG ulPartLen /* count of bytes to sign */ 13438+); 13439+#endif 13440+ 13441+ 13442+/* C_SignFinal finishes a multiple-part signature operation, 13443+ * returning the signature. */ 13444+CK_PKCS11_FUNCTION_INFO(C_SignFinal) 13445+#ifdef CK_NEED_ARG_LIST 13446+( 13447+ CK_SESSION_HANDLE hSession, /* the session's handle */ 13448+ CK_BYTE_PTR pSignature, /* gets the signature */ 13449+ CK_ULONG_PTR pulSignatureLen /* gets signature length */ 13450+); 13451+#endif 13452+ 13453+ 13454+/* C_SignRecoverInit initializes a signature operation, where 13455+ * the data can be recovered from the signature. */ 13456+CK_PKCS11_FUNCTION_INFO(C_SignRecoverInit) 13457+#ifdef CK_NEED_ARG_LIST 13458+( 13459+ CK_SESSION_HANDLE hSession, /* the session's handle */ 13460+ CK_MECHANISM_PTR pMechanism, /* the signature mechanism */ 13461+ CK_OBJECT_HANDLE hKey /* handle of the signature key */ 13462+); 13463+#endif 13464+ 13465+ 13466+/* C_SignRecover signs data in a single operation, where the 13467+ * data can be recovered from the signature. */ 13468+CK_PKCS11_FUNCTION_INFO(C_SignRecover) 13469+#ifdef CK_NEED_ARG_LIST 13470+( 13471+ CK_SESSION_HANDLE hSession, /* the session's handle */ 13472+ CK_BYTE_PTR pData, /* the data to sign */ 13473+ CK_ULONG ulDataLen, /* count of bytes to sign */ 13474+ CK_BYTE_PTR pSignature, /* gets the signature */ 13475+ CK_ULONG_PTR pulSignatureLen /* gets signature length */ 13476+); 13477+#endif 13478+ 13479+ 13480+ 13481+/* Verifying signatures and MACs */ 13482+ 13483+/* C_VerifyInit initializes a verification operation, where the 13484+ * signature is an appendix to the data, and plaintext cannot 13485+ * cannot be recovered from the signature (e.g. DSA). */ 13486+CK_PKCS11_FUNCTION_INFO(C_VerifyInit) 13487+#ifdef CK_NEED_ARG_LIST 13488+( 13489+ CK_SESSION_HANDLE hSession, /* the session's handle */ 13490+ CK_MECHANISM_PTR pMechanism, /* the verification mechanism */ 13491+ CK_OBJECT_HANDLE hKey /* verification key */ 13492+); 13493+#endif 13494+ 13495+ 13496+/* C_Verify verifies a signature in a single-part operation, 13497+ * where the signature is an appendix to the data, and plaintext 13498+ * cannot be recovered from the signature. */ 13499+CK_PKCS11_FUNCTION_INFO(C_Verify) 13500+#ifdef CK_NEED_ARG_LIST 13501+( 13502+ CK_SESSION_HANDLE hSession, /* the session's handle */ 13503+ CK_BYTE_PTR pData, /* signed data */ 13504+ CK_ULONG ulDataLen, /* length of signed data */ 13505+ CK_BYTE_PTR pSignature, /* signature */ 13506+ CK_ULONG ulSignatureLen /* signature length*/ 13507+); 13508+#endif 13509+ 13510+ 13511+/* C_VerifyUpdate continues a multiple-part verification 13512+ * operation, where the signature is an appendix to the data, 13513+ * and plaintext cannot be recovered from the signature. */ 13514+CK_PKCS11_FUNCTION_INFO(C_VerifyUpdate) 13515+#ifdef CK_NEED_ARG_LIST 13516+( 13517+ CK_SESSION_HANDLE hSession, /* the session's handle */ 13518+ CK_BYTE_PTR pPart, /* signed data */ 13519+ CK_ULONG ulPartLen /* length of signed data */ 13520+); 13521+#endif 13522+ 13523+ 13524+/* C_VerifyFinal finishes a multiple-part verification 13525+ * operation, checking the signature. */ 13526+CK_PKCS11_FUNCTION_INFO(C_VerifyFinal) 13527+#ifdef CK_NEED_ARG_LIST 13528+( 13529+ CK_SESSION_HANDLE hSession, /* the session's handle */ 13530+ CK_BYTE_PTR pSignature, /* signature to verify */ 13531+ CK_ULONG ulSignatureLen /* signature length */ 13532+); 13533+#endif 13534+ 13535+ 13536+/* C_VerifyRecoverInit initializes a signature verification 13537+ * operation, where the data is recovered from the signature. */ 13538+CK_PKCS11_FUNCTION_INFO(C_VerifyRecoverInit) 13539+#ifdef CK_NEED_ARG_LIST 13540+( 13541+ CK_SESSION_HANDLE hSession, /* the session's handle */ 13542+ CK_MECHANISM_PTR pMechanism, /* the verification mechanism */ 13543+ CK_OBJECT_HANDLE hKey /* verification key */ 13544+); 13545+#endif 13546+ 13547+ 13548+/* C_VerifyRecover verifies a signature in a single-part 13549+ * operation, where the data is recovered from the signature. */ 13550+CK_PKCS11_FUNCTION_INFO(C_VerifyRecover) 13551+#ifdef CK_NEED_ARG_LIST 13552+( 13553+ CK_SESSION_HANDLE hSession, /* the session's handle */ 13554+ CK_BYTE_PTR pSignature, /* signature to verify */ 13555+ CK_ULONG ulSignatureLen, /* signature length */ 13556+ CK_BYTE_PTR pData, /* gets signed data */ 13557+ CK_ULONG_PTR pulDataLen /* gets signed data len */ 13558+); 13559+#endif 13560+ 13561+ 13562+ 13563+/* Dual-function cryptographic operations */ 13564+ 13565+/* C_DigestEncryptUpdate continues a multiple-part digesting 13566+ * and encryption operation. */ 13567+CK_PKCS11_FUNCTION_INFO(C_DigestEncryptUpdate) 13568+#ifdef CK_NEED_ARG_LIST 13569+( 13570+ CK_SESSION_HANDLE hSession, /* session's handle */ 13571+ CK_BYTE_PTR pPart, /* the plaintext data */ 13572+ CK_ULONG ulPartLen, /* plaintext length */ 13573+ CK_BYTE_PTR pEncryptedPart, /* gets ciphertext */ 13574+ CK_ULONG_PTR pulEncryptedPartLen /* gets c-text length */ 13575+); 13576+#endif 13577+ 13578+ 13579+/* C_DecryptDigestUpdate continues a multiple-part decryption and 13580+ * digesting operation. */ 13581+CK_PKCS11_FUNCTION_INFO(C_DecryptDigestUpdate) 13582+#ifdef CK_NEED_ARG_LIST 13583+( 13584+ CK_SESSION_HANDLE hSession, /* session's handle */ 13585+ CK_BYTE_PTR pEncryptedPart, /* ciphertext */ 13586+ CK_ULONG ulEncryptedPartLen, /* ciphertext length */ 13587+ CK_BYTE_PTR pPart, /* gets plaintext */ 13588+ CK_ULONG_PTR pulPartLen /* gets plaintext len */ 13589+); 13590+#endif 13591+ 13592+ 13593+/* C_SignEncryptUpdate continues a multiple-part signing and 13594+ * encryption operation. */ 13595+CK_PKCS11_FUNCTION_INFO(C_SignEncryptUpdate) 13596+#ifdef CK_NEED_ARG_LIST 13597+( 13598+ CK_SESSION_HANDLE hSession, /* session's handle */ 13599+ CK_BYTE_PTR pPart, /* the plaintext data */ 13600+ CK_ULONG ulPartLen, /* plaintext length */ 13601+ CK_BYTE_PTR pEncryptedPart, /* gets ciphertext */ 13602+ CK_ULONG_PTR pulEncryptedPartLen /* gets c-text length */ 13603+); 13604+#endif 13605+ 13606+ 13607+/* C_DecryptVerifyUpdate continues a multiple-part decryption and 13608+ * verify operation. */ 13609+CK_PKCS11_FUNCTION_INFO(C_DecryptVerifyUpdate) 13610+#ifdef CK_NEED_ARG_LIST 13611+( 13612+ CK_SESSION_HANDLE hSession, /* session's handle */ 13613+ CK_BYTE_PTR pEncryptedPart, /* ciphertext */ 13614+ CK_ULONG ulEncryptedPartLen, /* ciphertext length */ 13615+ CK_BYTE_PTR pPart, /* gets plaintext */ 13616+ CK_ULONG_PTR pulPartLen /* gets p-text length */ 13617+); 13618+#endif 13619+ 13620+ 13621+ 13622+/* Key management */ 13623+ 13624+/* C_GenerateKey generates a secret key, creating a new key 13625+ * object. */ 13626+CK_PKCS11_FUNCTION_INFO(C_GenerateKey) 13627+#ifdef CK_NEED_ARG_LIST 13628+( 13629+ CK_SESSION_HANDLE hSession, /* the session's handle */ 13630+ CK_MECHANISM_PTR pMechanism, /* key generation mech. */ 13631+ CK_ATTRIBUTE_PTR pTemplate, /* template for new key */ 13632+ CK_ULONG ulCount, /* # of attrs in template */ 13633+ CK_OBJECT_HANDLE_PTR phKey /* gets handle of new key */ 13634+); 13635+#endif 13636+ 13637+ 13638+/* C_GenerateKeyPair generates a public-key/private-key pair, 13639+ * creating new key objects. */ 13640+CK_PKCS11_FUNCTION_INFO(C_GenerateKeyPair) 13641+#ifdef CK_NEED_ARG_LIST 13642+( 13643+ CK_SESSION_HANDLE hSession, /* session 13644+ * handle */ 13645+ CK_MECHANISM_PTR pMechanism, /* key-gen 13646+ * mech. */ 13647+ CK_ATTRIBUTE_PTR pPublicKeyTemplate, /* template 13648+ * for pub. 13649+ * key */ 13650+ CK_ULONG ulPublicKeyAttributeCount, /* # pub. 13651+ * attrs. */ 13652+ CK_ATTRIBUTE_PTR pPrivateKeyTemplate, /* template 13653+ * for priv. 13654+ * key */ 13655+ CK_ULONG ulPrivateKeyAttributeCount, /* # priv. 13656+ * attrs. */ 13657+ CK_OBJECT_HANDLE_PTR phPublicKey, /* gets pub. 13658+ * key 13659+ * handle */ 13660+ CK_OBJECT_HANDLE_PTR phPrivateKey /* gets 13661+ * priv. key 13662+ * handle */ 13663+); 13664+#endif 13665+ 13666+ 13667+/* C_WrapKey wraps (i.e., encrypts) a key. */ 13668+CK_PKCS11_FUNCTION_INFO(C_WrapKey) 13669+#ifdef CK_NEED_ARG_LIST 13670+( 13671+ CK_SESSION_HANDLE hSession, /* the session's handle */ 13672+ CK_MECHANISM_PTR pMechanism, /* the wrapping mechanism */ 13673+ CK_OBJECT_HANDLE hWrappingKey, /* wrapping key */ 13674+ CK_OBJECT_HANDLE hKey, /* key to be wrapped */ 13675+ CK_BYTE_PTR pWrappedKey, /* gets wrapped key */ 13676+ CK_ULONG_PTR pulWrappedKeyLen /* gets wrapped key size */ 13677+); 13678+#endif 13679+ 13680+ 13681+/* C_UnwrapKey unwraps (decrypts) a wrapped key, creating a new 13682+ * key object. */ 13683+CK_PKCS11_FUNCTION_INFO(C_UnwrapKey) 13684+#ifdef CK_NEED_ARG_LIST 13685+( 13686+ CK_SESSION_HANDLE hSession, /* session's handle */ 13687+ CK_MECHANISM_PTR pMechanism, /* unwrapping mech. */ 13688+ CK_OBJECT_HANDLE hUnwrappingKey, /* unwrapping key */ 13689+ CK_BYTE_PTR pWrappedKey, /* the wrapped key */ 13690+ CK_ULONG ulWrappedKeyLen, /* wrapped key len */ 13691+ CK_ATTRIBUTE_PTR pTemplate, /* new key template */ 13692+ CK_ULONG ulAttributeCount, /* template length */ 13693+ CK_OBJECT_HANDLE_PTR phKey /* gets new handle */ 13694+); 13695+#endif 13696+ 13697+ 13698+/* C_DeriveKey derives a key from a base key, creating a new key 13699+ * object. */ 13700+CK_PKCS11_FUNCTION_INFO(C_DeriveKey) 13701+#ifdef CK_NEED_ARG_LIST 13702+( 13703+ CK_SESSION_HANDLE hSession, /* session's handle */ 13704+ CK_MECHANISM_PTR pMechanism, /* key deriv. mech. */ 13705+ CK_OBJECT_HANDLE hBaseKey, /* base key */ 13706+ CK_ATTRIBUTE_PTR pTemplate, /* new key template */ 13707+ CK_ULONG ulAttributeCount, /* template length */ 13708+ CK_OBJECT_HANDLE_PTR phKey /* gets new handle */ 13709+); 13710+#endif 13711+ 13712+ 13713+ 13714+/* Random number generation */ 13715+ 13716+/* C_SeedRandom mixes additional seed material into the token's 13717+ * random number generator. */ 13718+CK_PKCS11_FUNCTION_INFO(C_SeedRandom) 13719+#ifdef CK_NEED_ARG_LIST 13720+( 13721+ CK_SESSION_HANDLE hSession, /* the session's handle */ 13722+ CK_BYTE_PTR pSeed, /* the seed material */ 13723+ CK_ULONG ulSeedLen /* length of seed material */ 13724+); 13725+#endif 13726+ 13727+ 13728+/* C_GenerateRandom generates random data. */ 13729+CK_PKCS11_FUNCTION_INFO(C_GenerateRandom) 13730+#ifdef CK_NEED_ARG_LIST 13731+( 13732+ CK_SESSION_HANDLE hSession, /* the session's handle */ 13733+ CK_BYTE_PTR RandomData, /* receives the random data */ 13734+ CK_ULONG ulRandomLen /* # of bytes to generate */ 13735+); 13736+#endif 13737+ 13738+ 13739+ 13740+/* Parallel function management */ 13741+ 13742+/* C_GetFunctionStatus is a legacy function; it obtains an 13743+ * updated status of a function running in parallel with an 13744+ * application. */ 13745+CK_PKCS11_FUNCTION_INFO(C_GetFunctionStatus) 13746+#ifdef CK_NEED_ARG_LIST 13747+( 13748+ CK_SESSION_HANDLE hSession /* the session's handle */ 13749+); 13750+#endif 13751+ 13752+ 13753+/* C_CancelFunction is a legacy function; it cancels a function 13754+ * running in parallel. */ 13755+CK_PKCS11_FUNCTION_INFO(C_CancelFunction) 13756+#ifdef CK_NEED_ARG_LIST 13757+( 13758+ CK_SESSION_HANDLE hSession /* the session's handle */ 13759+); 13760+#endif 13761+ 13762+ 13763+ 13764+/* Functions added in for Cryptoki Version 2.01 or later */ 13765+ 13766+/* C_WaitForSlotEvent waits for a slot event (token insertion, 13767+ * removal, etc.) to occur. */ 13768+CK_PKCS11_FUNCTION_INFO(C_WaitForSlotEvent) 13769+#ifdef CK_NEED_ARG_LIST 13770+( 13771+ CK_FLAGS flags, /* blocking/nonblocking flag */ 13772+ CK_SLOT_ID_PTR pSlot, /* location that receives the slot ID */ 13773+ CK_VOID_PTR pRserved /* reserved. Should be NULL_PTR */ 13774+); 13775+#endif 13776Index: openssl/crypto/engine/pkcs11t.h 13777diff -u /dev/null openssl/crypto/engine/pkcs11t.h:1.2 13778--- /dev/null Mon Jan 16 18:53:42 2012 13779+++ openssl/crypto/engine/pkcs11t.h Sat Aug 30 11:58:07 2008 13780@@ -0,0 +1,1885 @@ 13781+/* pkcs11t.h include file for PKCS #11. */ 13782+/* $Revision: 1.1.2.3 $ */ 13783+ 13784+/* License to copy and use this software is granted provided that it is 13785+ * identified as "RSA Security Inc. PKCS #11 Cryptographic Token Interface 13786+ * (Cryptoki)" in all material mentioning or referencing this software. 13787+ 13788+ * License is also granted to make and use derivative works provided that 13789+ * such works are identified as "derived from the RSA Security Inc. PKCS #11 13790+ * Cryptographic Token Interface (Cryptoki)" in all material mentioning or 13791+ * referencing the derived work. 13792+ 13793+ * RSA Security Inc. makes no representations concerning either the 13794+ * merchantability of this software or the suitability of this software for 13795+ * any particular purpose. It is provided "as is" without express or implied 13796+ * warranty of any kind. 13797+ */ 13798+ 13799+/* See top of pkcs11.h for information about the macros that 13800+ * must be defined and the structure-packing conventions that 13801+ * must be set before including this file. */ 13802+ 13803+#ifndef _PKCS11T_H_ 13804+#define _PKCS11T_H_ 1 13805+ 13806+#define CRYPTOKI_VERSION_MAJOR 2 13807+#define CRYPTOKI_VERSION_MINOR 20 13808+#define CRYPTOKI_VERSION_AMENDMENT 3 13809+ 13810+#define CK_TRUE 1 13811+#define CK_FALSE 0 13812+ 13813+#ifndef CK_DISABLE_TRUE_FALSE 13814+#ifndef FALSE 13815+#define FALSE CK_FALSE 13816+#endif 13817+ 13818+#ifndef TRUE 13819+#define TRUE CK_TRUE 13820+#endif 13821+#endif 13822+ 13823+/* an unsigned 8-bit value */ 13824+typedef unsigned char CK_BYTE; 13825+ 13826+/* an unsigned 8-bit character */ 13827+typedef CK_BYTE CK_CHAR; 13828+ 13829+/* an 8-bit UTF-8 character */ 13830+typedef CK_BYTE CK_UTF8CHAR; 13831+ 13832+/* a BYTE-sized Boolean flag */ 13833+typedef CK_BYTE CK_BBOOL; 13834+ 13835+/* an unsigned value, at least 32 bits long */ 13836+typedef unsigned long int CK_ULONG; 13837+ 13838+/* a signed value, the same size as a CK_ULONG */ 13839+/* CK_LONG is new for v2.0 */ 13840+typedef long int CK_LONG; 13841+ 13842+/* at least 32 bits; each bit is a Boolean flag */ 13843+typedef CK_ULONG CK_FLAGS; 13844+ 13845+ 13846+/* some special values for certain CK_ULONG variables */ 13847+#define CK_UNAVAILABLE_INFORMATION (~0UL) 13848+#define CK_EFFECTIVELY_INFINITE 0 13849+ 13850+ 13851+typedef CK_BYTE CK_PTR CK_BYTE_PTR; 13852+typedef CK_CHAR CK_PTR CK_CHAR_PTR; 13853+typedef CK_UTF8CHAR CK_PTR CK_UTF8CHAR_PTR; 13854+typedef CK_ULONG CK_PTR CK_ULONG_PTR; 13855+typedef void CK_PTR CK_VOID_PTR; 13856+ 13857+/* Pointer to a CK_VOID_PTR-- i.e., pointer to pointer to void */ 13858+typedef CK_VOID_PTR CK_PTR CK_VOID_PTR_PTR; 13859+ 13860+ 13861+/* The following value is always invalid if used as a session */ 13862+/* handle or object handle */ 13863+#define CK_INVALID_HANDLE 0 13864+ 13865+ 13866+typedef struct CK_VERSION { 13867+ CK_BYTE major; /* integer portion of version number */ 13868+ CK_BYTE minor; /* 1/100ths portion of version number */ 13869+} CK_VERSION; 13870+ 13871+typedef CK_VERSION CK_PTR CK_VERSION_PTR; 13872+ 13873+ 13874+typedef struct CK_INFO { 13875+ /* manufacturerID and libraryDecription have been changed from 13876+ * CK_CHAR to CK_UTF8CHAR for v2.10 */ 13877+ CK_VERSION cryptokiVersion; /* Cryptoki interface ver */ 13878+ CK_UTF8CHAR manufacturerID[32]; /* blank padded */ 13879+ CK_FLAGS flags; /* must be zero */ 13880+ 13881+ /* libraryDescription and libraryVersion are new for v2.0 */ 13882+ CK_UTF8CHAR libraryDescription[32]; /* blank padded */ 13883+ CK_VERSION libraryVersion; /* version of library */ 13884+} CK_INFO; 13885+ 13886+typedef CK_INFO CK_PTR CK_INFO_PTR; 13887+ 13888+ 13889+/* CK_NOTIFICATION enumerates the types of notifications that 13890+ * Cryptoki provides to an application */ 13891+/* CK_NOTIFICATION has been changed from an enum to a CK_ULONG 13892+ * for v2.0 */ 13893+typedef CK_ULONG CK_NOTIFICATION; 13894+#define CKN_SURRENDER 0 13895+ 13896+/* The following notification is new for PKCS #11 v2.20 amendment 3 */ 13897+#define CKN_OTP_CHANGED 1 13898+ 13899+ 13900+typedef CK_ULONG CK_SLOT_ID; 13901+ 13902+typedef CK_SLOT_ID CK_PTR CK_SLOT_ID_PTR; 13903+ 13904+ 13905+/* CK_SLOT_INFO provides information about a slot */ 13906+typedef struct CK_SLOT_INFO { 13907+ /* slotDescription and manufacturerID have been changed from 13908+ * CK_CHAR to CK_UTF8CHAR for v2.10 */ 13909+ CK_UTF8CHAR slotDescription[64]; /* blank padded */ 13910+ CK_UTF8CHAR manufacturerID[32]; /* blank padded */ 13911+ CK_FLAGS flags; 13912+ 13913+ /* hardwareVersion and firmwareVersion are new for v2.0 */ 13914+ CK_VERSION hardwareVersion; /* version of hardware */ 13915+ CK_VERSION firmwareVersion; /* version of firmware */ 13916+} CK_SLOT_INFO; 13917+ 13918+/* flags: bit flags that provide capabilities of the slot 13919+ * Bit Flag Mask Meaning 13920+ */ 13921+#define CKF_TOKEN_PRESENT 0x00000001 /* a token is there */ 13922+#define CKF_REMOVABLE_DEVICE 0x00000002 /* removable devices*/ 13923+#define CKF_HW_SLOT 0x00000004 /* hardware slot */ 13924+ 13925+typedef CK_SLOT_INFO CK_PTR CK_SLOT_INFO_PTR; 13926+ 13927+ 13928+/* CK_TOKEN_INFO provides information about a token */ 13929+typedef struct CK_TOKEN_INFO { 13930+ /* label, manufacturerID, and model have been changed from 13931+ * CK_CHAR to CK_UTF8CHAR for v2.10 */ 13932+ CK_UTF8CHAR label[32]; /* blank padded */ 13933+ CK_UTF8CHAR manufacturerID[32]; /* blank padded */ 13934+ CK_UTF8CHAR model[16]; /* blank padded */ 13935+ CK_CHAR serialNumber[16]; /* blank padded */ 13936+ CK_FLAGS flags; /* see below */ 13937+ 13938+ /* ulMaxSessionCount, ulSessionCount, ulMaxRwSessionCount, 13939+ * ulRwSessionCount, ulMaxPinLen, and ulMinPinLen have all been 13940+ * changed from CK_USHORT to CK_ULONG for v2.0 */ 13941+ CK_ULONG ulMaxSessionCount; /* max open sessions */ 13942+ CK_ULONG ulSessionCount; /* sess. now open */ 13943+ CK_ULONG ulMaxRwSessionCount; /* max R/W sessions */ 13944+ CK_ULONG ulRwSessionCount; /* R/W sess. now open */ 13945+ CK_ULONG ulMaxPinLen; /* in bytes */ 13946+ CK_ULONG ulMinPinLen; /* in bytes */ 13947+ CK_ULONG ulTotalPublicMemory; /* in bytes */ 13948+ CK_ULONG ulFreePublicMemory; /* in bytes */ 13949+ CK_ULONG ulTotalPrivateMemory; /* in bytes */ 13950+ CK_ULONG ulFreePrivateMemory; /* in bytes */ 13951+ 13952+ /* hardwareVersion, firmwareVersion, and time are new for 13953+ * v2.0 */ 13954+ CK_VERSION hardwareVersion; /* version of hardware */ 13955+ CK_VERSION firmwareVersion; /* version of firmware */ 13956+ CK_CHAR utcTime[16]; /* time */ 13957+} CK_TOKEN_INFO; 13958+ 13959+/* The flags parameter is defined as follows: 13960+ * Bit Flag Mask Meaning 13961+ */ 13962+#define CKF_RNG 0x00000001 /* has random # 13963+ * generator */ 13964+#define CKF_WRITE_PROTECTED 0x00000002 /* token is 13965+ * write- 13966+ * protected */ 13967+#define CKF_LOGIN_REQUIRED 0x00000004 /* user must 13968+ * login */ 13969+#define CKF_USER_PIN_INITIALIZED 0x00000008 /* normal user's 13970+ * PIN is set */ 13971+ 13972+/* CKF_RESTORE_KEY_NOT_NEEDED is new for v2.0. If it is set, 13973+ * that means that *every* time the state of cryptographic 13974+ * operations of a session is successfully saved, all keys 13975+ * needed to continue those operations are stored in the state */ 13976+#define CKF_RESTORE_KEY_NOT_NEEDED 0x00000020 13977+ 13978+/* CKF_CLOCK_ON_TOKEN is new for v2.0. If it is set, that means 13979+ * that the token has some sort of clock. The time on that 13980+ * clock is returned in the token info structure */ 13981+#define CKF_CLOCK_ON_TOKEN 0x00000040 13982+ 13983+/* CKF_PROTECTED_AUTHENTICATION_PATH is new for v2.0. If it is 13984+ * set, that means that there is some way for the user to login 13985+ * without sending a PIN through the Cryptoki library itself */ 13986+#define CKF_PROTECTED_AUTHENTICATION_PATH 0x00000100 13987+ 13988+/* CKF_DUAL_CRYPTO_OPERATIONS is new for v2.0. If it is true, 13989+ * that means that a single session with the token can perform 13990+ * dual simultaneous cryptographic operations (digest and 13991+ * encrypt; decrypt and digest; sign and encrypt; and decrypt 13992+ * and sign) */ 13993+#define CKF_DUAL_CRYPTO_OPERATIONS 0x00000200 13994+ 13995+/* CKF_TOKEN_INITIALIZED if new for v2.10. If it is true, the 13996+ * token has been initialized using C_InitializeToken or an 13997+ * equivalent mechanism outside the scope of PKCS #11. 13998+ * Calling C_InitializeToken when this flag is set will cause 13999+ * the token to be reinitialized. */ 14000+#define CKF_TOKEN_INITIALIZED 0x00000400 14001+ 14002+/* CKF_SECONDARY_AUTHENTICATION if new for v2.10. If it is 14003+ * true, the token supports secondary authentication for 14004+ * private key objects. This flag is deprecated in v2.11 and 14005+ onwards. */ 14006+#define CKF_SECONDARY_AUTHENTICATION 0x00000800 14007+ 14008+/* CKF_USER_PIN_COUNT_LOW if new for v2.10. If it is true, an 14009+ * incorrect user login PIN has been entered at least once 14010+ * since the last successful authentication. */ 14011+#define CKF_USER_PIN_COUNT_LOW 0x00010000 14012+ 14013+/* CKF_USER_PIN_FINAL_TRY if new for v2.10. If it is true, 14014+ * supplying an incorrect user PIN will it to become locked. */ 14015+#define CKF_USER_PIN_FINAL_TRY 0x00020000 14016+ 14017+/* CKF_USER_PIN_LOCKED if new for v2.10. If it is true, the 14018+ * user PIN has been locked. User login to the token is not 14019+ * possible. */ 14020+#define CKF_USER_PIN_LOCKED 0x00040000 14021+ 14022+/* CKF_USER_PIN_TO_BE_CHANGED if new for v2.10. If it is true, 14023+ * the user PIN value is the default value set by token 14024+ * initialization or manufacturing, or the PIN has been 14025+ * expired by the card. */ 14026+#define CKF_USER_PIN_TO_BE_CHANGED 0x00080000 14027+ 14028+/* CKF_SO_PIN_COUNT_LOW if new for v2.10. If it is true, an 14029+ * incorrect SO login PIN has been entered at least once since 14030+ * the last successful authentication. */ 14031+#define CKF_SO_PIN_COUNT_LOW 0x00100000 14032+ 14033+/* CKF_SO_PIN_FINAL_TRY if new for v2.10. If it is true, 14034+ * supplying an incorrect SO PIN will it to become locked. */ 14035+#define CKF_SO_PIN_FINAL_TRY 0x00200000 14036+ 14037+/* CKF_SO_PIN_LOCKED if new for v2.10. If it is true, the SO 14038+ * PIN has been locked. SO login to the token is not possible. 14039+ */ 14040+#define CKF_SO_PIN_LOCKED 0x00400000 14041+ 14042+/* CKF_SO_PIN_TO_BE_CHANGED if new for v2.10. If it is true, 14043+ * the SO PIN value is the default value set by token 14044+ * initialization or manufacturing, or the PIN has been 14045+ * expired by the card. */ 14046+#define CKF_SO_PIN_TO_BE_CHANGED 0x00800000 14047+ 14048+typedef CK_TOKEN_INFO CK_PTR CK_TOKEN_INFO_PTR; 14049+ 14050+ 14051+/* CK_SESSION_HANDLE is a Cryptoki-assigned value that 14052+ * identifies a session */ 14053+typedef CK_ULONG CK_SESSION_HANDLE; 14054+ 14055+typedef CK_SESSION_HANDLE CK_PTR CK_SESSION_HANDLE_PTR; 14056+ 14057+ 14058+/* CK_USER_TYPE enumerates the types of Cryptoki users */ 14059+/* CK_USER_TYPE has been changed from an enum to a CK_ULONG for 14060+ * v2.0 */ 14061+typedef CK_ULONG CK_USER_TYPE; 14062+/* Security Officer */ 14063+#define CKU_SO 0 14064+/* Normal user */ 14065+#define CKU_USER 1 14066+/* Context specific (added in v2.20) */ 14067+#define CKU_CONTEXT_SPECIFIC 2 14068+ 14069+/* CK_STATE enumerates the session states */ 14070+/* CK_STATE has been changed from an enum to a CK_ULONG for 14071+ * v2.0 */ 14072+typedef CK_ULONG CK_STATE; 14073+#define CKS_RO_PUBLIC_SESSION 0 14074+#define CKS_RO_USER_FUNCTIONS 1 14075+#define CKS_RW_PUBLIC_SESSION 2 14076+#define CKS_RW_USER_FUNCTIONS 3 14077+#define CKS_RW_SO_FUNCTIONS 4 14078+ 14079+ 14080+/* CK_SESSION_INFO provides information about a session */ 14081+typedef struct CK_SESSION_INFO { 14082+ CK_SLOT_ID slotID; 14083+ CK_STATE state; 14084+ CK_FLAGS flags; /* see below */ 14085+ 14086+ /* ulDeviceError was changed from CK_USHORT to CK_ULONG for 14087+ * v2.0 */ 14088+ CK_ULONG ulDeviceError; /* device-dependent error code */ 14089+} CK_SESSION_INFO; 14090+ 14091+/* The flags are defined in the following table: 14092+ * Bit Flag Mask Meaning 14093+ */ 14094+#define CKF_RW_SESSION 0x00000002 /* session is r/w */ 14095+#define CKF_SERIAL_SESSION 0x00000004 /* no parallel */ 14096+ 14097+typedef CK_SESSION_INFO CK_PTR CK_SESSION_INFO_PTR; 14098+ 14099+ 14100+/* CK_OBJECT_HANDLE is a token-specific identifier for an 14101+ * object */ 14102+typedef CK_ULONG CK_OBJECT_HANDLE; 14103+ 14104+typedef CK_OBJECT_HANDLE CK_PTR CK_OBJECT_HANDLE_PTR; 14105+ 14106+ 14107+/* CK_OBJECT_CLASS is a value that identifies the classes (or 14108+ * types) of objects that Cryptoki recognizes. It is defined 14109+ * as follows: */ 14110+/* CK_OBJECT_CLASS was changed from CK_USHORT to CK_ULONG for 14111+ * v2.0 */ 14112+typedef CK_ULONG CK_OBJECT_CLASS; 14113+ 14114+/* The following classes of objects are defined: */ 14115+/* CKO_HW_FEATURE is new for v2.10 */ 14116+/* CKO_DOMAIN_PARAMETERS is new for v2.11 */ 14117+/* CKO_MECHANISM is new for v2.20 */ 14118+#define CKO_DATA 0x00000000 14119+#define CKO_CERTIFICATE 0x00000001 14120+#define CKO_PUBLIC_KEY 0x00000002 14121+#define CKO_PRIVATE_KEY 0x00000003 14122+#define CKO_SECRET_KEY 0x00000004 14123+#define CKO_HW_FEATURE 0x00000005 14124+#define CKO_DOMAIN_PARAMETERS 0x00000006 14125+#define CKO_MECHANISM 0x00000007 14126+ 14127+/* CKO_OTP_KEY is new for PKCS #11 v2.20 amendment 1 */ 14128+#define CKO_OTP_KEY 0x00000008 14129+ 14130+#define CKO_VENDOR_DEFINED 0x80000000 14131+ 14132+typedef CK_OBJECT_CLASS CK_PTR CK_OBJECT_CLASS_PTR; 14133+ 14134+/* CK_HW_FEATURE_TYPE is new for v2.10. CK_HW_FEATURE_TYPE is a 14135+ * value that identifies the hardware feature type of an object 14136+ * with CK_OBJECT_CLASS equal to CKO_HW_FEATURE. */ 14137+typedef CK_ULONG CK_HW_FEATURE_TYPE; 14138+ 14139+/* The following hardware feature types are defined */ 14140+/* CKH_USER_INTERFACE is new for v2.20 */ 14141+#define CKH_MONOTONIC_COUNTER 0x00000001 14142+#define CKH_CLOCK 0x00000002 14143+#define CKH_USER_INTERFACE 0x00000003 14144+#define CKH_VENDOR_DEFINED 0x80000000 14145+ 14146+/* CK_KEY_TYPE is a value that identifies a key type */ 14147+/* CK_KEY_TYPE was changed from CK_USHORT to CK_ULONG for v2.0 */ 14148+typedef CK_ULONG CK_KEY_TYPE; 14149+ 14150+/* the following key types are defined: */ 14151+#define CKK_RSA 0x00000000 14152+#define CKK_DSA 0x00000001 14153+#define CKK_DH 0x00000002 14154+ 14155+/* CKK_ECDSA and CKK_KEA are new for v2.0 */ 14156+/* CKK_ECDSA is deprecated in v2.11, CKK_EC is preferred. */ 14157+#define CKK_ECDSA 0x00000003 14158+#define CKK_EC 0x00000003 14159+#define CKK_X9_42_DH 0x00000004 14160+#define CKK_KEA 0x00000005 14161+ 14162+#define CKK_GENERIC_SECRET 0x00000010 14163+#define CKK_RC2 0x00000011 14164+#define CKK_RC4 0x00000012 14165+#define CKK_DES 0x00000013 14166+#define CKK_DES2 0x00000014 14167+#define CKK_DES3 0x00000015 14168+ 14169+/* all these key types are new for v2.0 */ 14170+#define CKK_CAST 0x00000016 14171+#define CKK_CAST3 0x00000017 14172+/* CKK_CAST5 is deprecated in v2.11, CKK_CAST128 is preferred. */ 14173+#define CKK_CAST5 0x00000018 14174+#define CKK_CAST128 0x00000018 14175+#define CKK_RC5 0x00000019 14176+#define CKK_IDEA 0x0000001A 14177+#define CKK_SKIPJACK 0x0000001B 14178+#define CKK_BATON 0x0000001C 14179+#define CKK_JUNIPER 0x0000001D 14180+#define CKK_CDMF 0x0000001E 14181+#define CKK_AES 0x0000001F 14182+ 14183+/* BlowFish and TwoFish are new for v2.20 */ 14184+#define CKK_BLOWFISH 0x00000020 14185+#define CKK_TWOFISH 0x00000021 14186+ 14187+/* SecurID, HOTP, and ACTI are new for PKCS #11 v2.20 amendment 1 */ 14188+#define CKK_SECURID 0x00000022 14189+#define CKK_HOTP 0x00000023 14190+#define CKK_ACTI 0x00000024 14191+ 14192+/* Camellia is new for PKCS #11 v2.20 amendment 3 */ 14193+#define CKK_CAMELLIA 0x00000025 14194+/* ARIA is new for PKCS #11 v2.20 amendment 3 */ 14195+#define CKK_ARIA 0x00000026 14196+ 14197+ 14198+#define CKK_VENDOR_DEFINED 0x80000000 14199+ 14200+ 14201+/* CK_CERTIFICATE_TYPE is a value that identifies a certificate 14202+ * type */ 14203+/* CK_CERTIFICATE_TYPE was changed from CK_USHORT to CK_ULONG 14204+ * for v2.0 */ 14205+typedef CK_ULONG CK_CERTIFICATE_TYPE; 14206+ 14207+/* The following certificate types are defined: */ 14208+/* CKC_X_509_ATTR_CERT is new for v2.10 */ 14209+/* CKC_WTLS is new for v2.20 */ 14210+#define CKC_X_509 0x00000000 14211+#define CKC_X_509_ATTR_CERT 0x00000001 14212+#define CKC_WTLS 0x00000002 14213+#define CKC_VENDOR_DEFINED 0x80000000 14214+ 14215+ 14216+/* CK_ATTRIBUTE_TYPE is a value that identifies an attribute 14217+ * type */ 14218+/* CK_ATTRIBUTE_TYPE was changed from CK_USHORT to CK_ULONG for 14219+ * v2.0 */ 14220+typedef CK_ULONG CK_ATTRIBUTE_TYPE; 14221+ 14222+/* The CKF_ARRAY_ATTRIBUTE flag identifies an attribute which 14223+ consists of an array of values. */ 14224+#define CKF_ARRAY_ATTRIBUTE 0x40000000 14225+ 14226+/* The following OTP-related defines are new for PKCS #11 v2.20 amendment 1 14227+ and relates to the CKA_OTP_FORMAT attribute */ 14228+#define CK_OTP_FORMAT_DECIMAL 0 14229+#define CK_OTP_FORMAT_HEXADECIMAL 1 14230+#define CK_OTP_FORMAT_ALPHANUMERIC 2 14231+#define CK_OTP_FORMAT_BINARY 3 14232+ 14233+/* The following OTP-related defines are new for PKCS #11 v2.20 amendment 1 14234+ and relates to the CKA_OTP_..._REQUIREMENT attributes */ 14235+#define CK_OTP_PARAM_IGNORED 0 14236+#define CK_OTP_PARAM_OPTIONAL 1 14237+#define CK_OTP_PARAM_MANDATORY 2 14238+ 14239+/* The following attribute types are defined: */ 14240+#define CKA_CLASS 0x00000000 14241+#define CKA_TOKEN 0x00000001 14242+#define CKA_PRIVATE 0x00000002 14243+#define CKA_LABEL 0x00000003 14244+#define CKA_APPLICATION 0x00000010 14245+#define CKA_VALUE 0x00000011 14246+ 14247+/* CKA_OBJECT_ID is new for v2.10 */ 14248+#define CKA_OBJECT_ID 0x00000012 14249+ 14250+#define CKA_CERTIFICATE_TYPE 0x00000080 14251+#define CKA_ISSUER 0x00000081 14252+#define CKA_SERIAL_NUMBER 0x00000082 14253+ 14254+/* CKA_AC_ISSUER, CKA_OWNER, and CKA_ATTR_TYPES are new 14255+ * for v2.10 */ 14256+#define CKA_AC_ISSUER 0x00000083 14257+#define CKA_OWNER 0x00000084 14258+#define CKA_ATTR_TYPES 0x00000085 14259+ 14260+/* CKA_TRUSTED is new for v2.11 */ 14261+#define CKA_TRUSTED 0x00000086 14262+ 14263+/* CKA_CERTIFICATE_CATEGORY ... 14264+ * CKA_CHECK_VALUE are new for v2.20 */ 14265+#define CKA_CERTIFICATE_CATEGORY 0x00000087 14266+#define CKA_JAVA_MIDP_SECURITY_DOMAIN 0x00000088 14267+#define CKA_URL 0x00000089 14268+#define CKA_HASH_OF_SUBJECT_PUBLIC_KEY 0x0000008A 14269+#define CKA_HASH_OF_ISSUER_PUBLIC_KEY 0x0000008B 14270+#define CKA_CHECK_VALUE 0x00000090 14271+ 14272+#define CKA_KEY_TYPE 0x00000100 14273+#define CKA_SUBJECT 0x00000101 14274+#define CKA_ID 0x00000102 14275+#define CKA_SENSITIVE 0x00000103 14276+#define CKA_ENCRYPT 0x00000104 14277+#define CKA_DECRYPT 0x00000105 14278+#define CKA_WRAP 0x00000106 14279+#define CKA_UNWRAP 0x00000107 14280+#define CKA_SIGN 0x00000108 14281+#define CKA_SIGN_RECOVER 0x00000109 14282+#define CKA_VERIFY 0x0000010A 14283+#define CKA_VERIFY_RECOVER 0x0000010B 14284+#define CKA_DERIVE 0x0000010C 14285+#define CKA_START_DATE 0x00000110 14286+#define CKA_END_DATE 0x00000111 14287+#define CKA_MODULUS 0x00000120 14288+#define CKA_MODULUS_BITS 0x00000121 14289+#define CKA_PUBLIC_EXPONENT 0x00000122 14290+#define CKA_PRIVATE_EXPONENT 0x00000123 14291+#define CKA_PRIME_1 0x00000124 14292+#define CKA_PRIME_2 0x00000125 14293+#define CKA_EXPONENT_1 0x00000126 14294+#define CKA_EXPONENT_2 0x00000127 14295+#define CKA_COEFFICIENT 0x00000128 14296+#define CKA_PRIME 0x00000130 14297+#define CKA_SUBPRIME 0x00000131 14298+#define CKA_BASE 0x00000132 14299+ 14300+/* CKA_PRIME_BITS and CKA_SUB_PRIME_BITS are new for v2.11 */ 14301+#define CKA_PRIME_BITS 0x00000133 14302+#define CKA_SUBPRIME_BITS 0x00000134 14303+#define CKA_SUB_PRIME_BITS CKA_SUBPRIME_BITS 14304+/* (To retain backwards-compatibility) */ 14305+ 14306+#define CKA_VALUE_BITS 0x00000160 14307+#define CKA_VALUE_LEN 0x00000161 14308+ 14309+/* CKA_EXTRACTABLE, CKA_LOCAL, CKA_NEVER_EXTRACTABLE, 14310+ * CKA_ALWAYS_SENSITIVE, CKA_MODIFIABLE, CKA_ECDSA_PARAMS, 14311+ * and CKA_EC_POINT are new for v2.0 */ 14312+#define CKA_EXTRACTABLE 0x00000162 14313+#define CKA_LOCAL 0x00000163 14314+#define CKA_NEVER_EXTRACTABLE 0x00000164 14315+#define CKA_ALWAYS_SENSITIVE 0x00000165 14316+ 14317+/* CKA_KEY_GEN_MECHANISM is new for v2.11 */ 14318+#define CKA_KEY_GEN_MECHANISM 0x00000166 14319+ 14320+#define CKA_MODIFIABLE 0x00000170 14321+ 14322+/* CKA_ECDSA_PARAMS is deprecated in v2.11, 14323+ * CKA_EC_PARAMS is preferred. */ 14324+#define CKA_ECDSA_PARAMS 0x00000180 14325+#define CKA_EC_PARAMS 0x00000180 14326+ 14327+#define CKA_EC_POINT 0x00000181 14328+ 14329+/* CKA_SECONDARY_AUTH, CKA_AUTH_PIN_FLAGS, 14330+ * are new for v2.10. Deprecated in v2.11 and onwards. */ 14331+#define CKA_SECONDARY_AUTH 0x00000200 14332+#define CKA_AUTH_PIN_FLAGS 0x00000201 14333+ 14334+/* CKA_ALWAYS_AUTHENTICATE ... 14335+ * CKA_UNWRAP_TEMPLATE are new for v2.20 */ 14336+#define CKA_ALWAYS_AUTHENTICATE 0x00000202 14337+ 14338+#define CKA_WRAP_WITH_TRUSTED 0x00000210 14339+#define CKA_WRAP_TEMPLATE (CKF_ARRAY_ATTRIBUTE|0x00000211) 14340+#define CKA_UNWRAP_TEMPLATE (CKF_ARRAY_ATTRIBUTE|0x00000212) 14341+ 14342+/* CKA_OTP... atttributes are new for PKCS #11 v2.20 amendment 3. */ 14343+#define CKA_OTP_FORMAT 0x00000220 14344+#define CKA_OTP_LENGTH 0x00000221 14345+#define CKA_OTP_TIME_INTERVAL 0x00000222 14346+#define CKA_OTP_USER_FRIENDLY_MODE 0x00000223 14347+#define CKA_OTP_CHALLENGE_REQUIREMENT 0x00000224 14348+#define CKA_OTP_TIME_REQUIREMENT 0x00000225 14349+#define CKA_OTP_COUNTER_REQUIREMENT 0x00000226 14350+#define CKA_OTP_PIN_REQUIREMENT 0x00000227 14351+#define CKA_OTP_COUNTER 0x0000022E 14352+#define CKA_OTP_TIME 0x0000022F 14353+#define CKA_OTP_USER_IDENTIFIER 0x0000022A 14354+#define CKA_OTP_SERVICE_IDENTIFIER 0x0000022B 14355+#define CKA_OTP_SERVICE_LOGO 0x0000022C 14356+#define CKA_OTP_SERVICE_LOGO_TYPE 0x0000022D 14357+ 14358+ 14359+/* CKA_HW_FEATURE_TYPE, CKA_RESET_ON_INIT, and CKA_HAS_RESET 14360+ * are new for v2.10 */ 14361+#define CKA_HW_FEATURE_TYPE 0x00000300 14362+#define CKA_RESET_ON_INIT 0x00000301 14363+#define CKA_HAS_RESET 0x00000302 14364+ 14365+/* The following attributes are new for v2.20 */ 14366+#define CKA_PIXEL_X 0x00000400 14367+#define CKA_PIXEL_Y 0x00000401 14368+#define CKA_RESOLUTION 0x00000402 14369+#define CKA_CHAR_ROWS 0x00000403 14370+#define CKA_CHAR_COLUMNS 0x00000404 14371+#define CKA_COLOR 0x00000405 14372+#define CKA_BITS_PER_PIXEL 0x00000406 14373+#define CKA_CHAR_SETS 0x00000480 14374+#define CKA_ENCODING_METHODS 0x00000481 14375+#define CKA_MIME_TYPES 0x00000482 14376+#define CKA_MECHANISM_TYPE 0x00000500 14377+#define CKA_REQUIRED_CMS_ATTRIBUTES 0x00000501 14378+#define CKA_DEFAULT_CMS_ATTRIBUTES 0x00000502 14379+#define CKA_SUPPORTED_CMS_ATTRIBUTES 0x00000503 14380+#define CKA_ALLOWED_MECHANISMS (CKF_ARRAY_ATTRIBUTE|0x00000600) 14381+ 14382+#define CKA_VENDOR_DEFINED 0x80000000 14383+ 14384+/* CK_ATTRIBUTE is a structure that includes the type, length 14385+ * and value of an attribute */ 14386+typedef struct CK_ATTRIBUTE { 14387+ CK_ATTRIBUTE_TYPE type; 14388+ CK_VOID_PTR pValue; 14389+ 14390+ /* ulValueLen went from CK_USHORT to CK_ULONG for v2.0 */ 14391+ CK_ULONG ulValueLen; /* in bytes */ 14392+} CK_ATTRIBUTE; 14393+ 14394+typedef CK_ATTRIBUTE CK_PTR CK_ATTRIBUTE_PTR; 14395+ 14396+ 14397+/* CK_DATE is a structure that defines a date */ 14398+typedef struct CK_DATE{ 14399+ CK_CHAR year[4]; /* the year ("1900" - "9999") */ 14400+ CK_CHAR month[2]; /* the month ("01" - "12") */ 14401+ CK_CHAR day[2]; /* the day ("01" - "31") */ 14402+} CK_DATE; 14403+ 14404+ 14405+/* CK_MECHANISM_TYPE is a value that identifies a mechanism 14406+ * type */ 14407+/* CK_MECHANISM_TYPE was changed from CK_USHORT to CK_ULONG for 14408+ * v2.0 */ 14409+typedef CK_ULONG CK_MECHANISM_TYPE; 14410+ 14411+/* the following mechanism types are defined: */ 14412+#define CKM_RSA_PKCS_KEY_PAIR_GEN 0x00000000 14413+#define CKM_RSA_PKCS 0x00000001 14414+#define CKM_RSA_9796 0x00000002 14415+#define CKM_RSA_X_509 0x00000003 14416+ 14417+/* CKM_MD2_RSA_PKCS, CKM_MD5_RSA_PKCS, and CKM_SHA1_RSA_PKCS 14418+ * are new for v2.0. They are mechanisms which hash and sign */ 14419+#define CKM_MD2_RSA_PKCS 0x00000004 14420+#define CKM_MD5_RSA_PKCS 0x00000005 14421+#define CKM_SHA1_RSA_PKCS 0x00000006 14422+ 14423+/* CKM_RIPEMD128_RSA_PKCS, CKM_RIPEMD160_RSA_PKCS, and 14424+ * CKM_RSA_PKCS_OAEP are new for v2.10 */ 14425+#define CKM_RIPEMD128_RSA_PKCS 0x00000007 14426+#define CKM_RIPEMD160_RSA_PKCS 0x00000008 14427+#define CKM_RSA_PKCS_OAEP 0x00000009 14428+ 14429+/* CKM_RSA_X9_31_KEY_PAIR_GEN, CKM_RSA_X9_31, CKM_SHA1_RSA_X9_31, 14430+ * CKM_RSA_PKCS_PSS, and CKM_SHA1_RSA_PKCS_PSS are new for v2.11 */ 14431+#define CKM_RSA_X9_31_KEY_PAIR_GEN 0x0000000A 14432+#define CKM_RSA_X9_31 0x0000000B 14433+#define CKM_SHA1_RSA_X9_31 0x0000000C 14434+#define CKM_RSA_PKCS_PSS 0x0000000D 14435+#define CKM_SHA1_RSA_PKCS_PSS 0x0000000E 14436+ 14437+#define CKM_DSA_KEY_PAIR_GEN 0x00000010 14438+#define CKM_DSA 0x00000011 14439+#define CKM_DSA_SHA1 0x00000012 14440+#define CKM_DH_PKCS_KEY_PAIR_GEN 0x00000020 14441+#define CKM_DH_PKCS_DERIVE 0x00000021 14442+ 14443+/* CKM_X9_42_DH_KEY_PAIR_GEN, CKM_X9_42_DH_DERIVE, 14444+ * CKM_X9_42_DH_HYBRID_DERIVE, and CKM_X9_42_MQV_DERIVE are new for 14445+ * v2.11 */ 14446+#define CKM_X9_42_DH_KEY_PAIR_GEN 0x00000030 14447+#define CKM_X9_42_DH_DERIVE 0x00000031 14448+#define CKM_X9_42_DH_HYBRID_DERIVE 0x00000032 14449+#define CKM_X9_42_MQV_DERIVE 0x00000033 14450+ 14451+/* CKM_SHA256/384/512 are new for v2.20 */ 14452+#define CKM_SHA256_RSA_PKCS 0x00000040 14453+#define CKM_SHA384_RSA_PKCS 0x00000041 14454+#define CKM_SHA512_RSA_PKCS 0x00000042 14455+#define CKM_SHA256_RSA_PKCS_PSS 0x00000043 14456+#define CKM_SHA384_RSA_PKCS_PSS 0x00000044 14457+#define CKM_SHA512_RSA_PKCS_PSS 0x00000045 14458+ 14459+/* SHA-224 RSA mechanisms are new for PKCS #11 v2.20 amendment 3 */ 14460+#define CKM_SHA224_RSA_PKCS 0x00000046 14461+#define CKM_SHA224_RSA_PKCS_PSS 0x00000047 14462+ 14463+#define CKM_RC2_KEY_GEN 0x00000100 14464+#define CKM_RC2_ECB 0x00000101 14465+#define CKM_RC2_CBC 0x00000102 14466+#define CKM_RC2_MAC 0x00000103 14467+ 14468+/* CKM_RC2_MAC_GENERAL and CKM_RC2_CBC_PAD are new for v2.0 */ 14469+#define CKM_RC2_MAC_GENERAL 0x00000104 14470+#define CKM_RC2_CBC_PAD 0x00000105 14471+ 14472+#define CKM_RC4_KEY_GEN 0x00000110 14473+#define CKM_RC4 0x00000111 14474+#define CKM_DES_KEY_GEN 0x00000120 14475+#define CKM_DES_ECB 0x00000121 14476+#define CKM_DES_CBC 0x00000122 14477+#define CKM_DES_MAC 0x00000123 14478+ 14479+/* CKM_DES_MAC_GENERAL and CKM_DES_CBC_PAD are new for v2.0 */ 14480+#define CKM_DES_MAC_GENERAL 0x00000124 14481+#define CKM_DES_CBC_PAD 0x00000125 14482+ 14483+#define CKM_DES2_KEY_GEN 0x00000130 14484+#define CKM_DES3_KEY_GEN 0x00000131 14485+#define CKM_DES3_ECB 0x00000132 14486+#define CKM_DES3_CBC 0x00000133 14487+#define CKM_DES3_MAC 0x00000134 14488+ 14489+/* CKM_DES3_MAC_GENERAL, CKM_DES3_CBC_PAD, CKM_CDMF_KEY_GEN, 14490+ * CKM_CDMF_ECB, CKM_CDMF_CBC, CKM_CDMF_MAC, 14491+ * CKM_CDMF_MAC_GENERAL, and CKM_CDMF_CBC_PAD are new for v2.0 */ 14492+#define CKM_DES3_MAC_GENERAL 0x00000135 14493+#define CKM_DES3_CBC_PAD 0x00000136 14494+#define CKM_CDMF_KEY_GEN 0x00000140 14495+#define CKM_CDMF_ECB 0x00000141 14496+#define CKM_CDMF_CBC 0x00000142 14497+#define CKM_CDMF_MAC 0x00000143 14498+#define CKM_CDMF_MAC_GENERAL 0x00000144 14499+#define CKM_CDMF_CBC_PAD 0x00000145 14500+ 14501+/* the following four DES mechanisms are new for v2.20 */ 14502+#define CKM_DES_OFB64 0x00000150 14503+#define CKM_DES_OFB8 0x00000151 14504+#define CKM_DES_CFB64 0x00000152 14505+#define CKM_DES_CFB8 0x00000153 14506+ 14507+#define CKM_MD2 0x00000200 14508+ 14509+/* CKM_MD2_HMAC and CKM_MD2_HMAC_GENERAL are new for v2.0 */ 14510+#define CKM_MD2_HMAC 0x00000201 14511+#define CKM_MD2_HMAC_GENERAL 0x00000202 14512+ 14513+#define CKM_MD5 0x00000210 14514+ 14515+/* CKM_MD5_HMAC and CKM_MD5_HMAC_GENERAL are new for v2.0 */ 14516+#define CKM_MD5_HMAC 0x00000211 14517+#define CKM_MD5_HMAC_GENERAL 0x00000212 14518+ 14519+#define CKM_SHA_1 0x00000220 14520+ 14521+/* CKM_SHA_1_HMAC and CKM_SHA_1_HMAC_GENERAL are new for v2.0 */ 14522+#define CKM_SHA_1_HMAC 0x00000221 14523+#define CKM_SHA_1_HMAC_GENERAL 0x00000222 14524+ 14525+/* CKM_RIPEMD128, CKM_RIPEMD128_HMAC, 14526+ * CKM_RIPEMD128_HMAC_GENERAL, CKM_RIPEMD160, CKM_RIPEMD160_HMAC, 14527+ * and CKM_RIPEMD160_HMAC_GENERAL are new for v2.10 */ 14528+#define CKM_RIPEMD128 0x00000230 14529+#define CKM_RIPEMD128_HMAC 0x00000231 14530+#define CKM_RIPEMD128_HMAC_GENERAL 0x00000232 14531+#define CKM_RIPEMD160 0x00000240 14532+#define CKM_RIPEMD160_HMAC 0x00000241 14533+#define CKM_RIPEMD160_HMAC_GENERAL 0x00000242 14534+ 14535+/* CKM_SHA256/384/512 are new for v2.20 */ 14536+#define CKM_SHA256 0x00000250 14537+#define CKM_SHA256_HMAC 0x00000251 14538+#define CKM_SHA256_HMAC_GENERAL 0x00000252 14539+ 14540+/* SHA-224 is new for PKCS #11 v2.20 amendment 3 */ 14541+#define CKM_SHA224 0x00000255 14542+#define CKM_SHA224_HMAC 0x00000256 14543+#define CKM_SHA224_HMAC_GENERAL 0x00000257 14544+ 14545+#define CKM_SHA384 0x00000260 14546+#define CKM_SHA384_HMAC 0x00000261 14547+#define CKM_SHA384_HMAC_GENERAL 0x00000262 14548+#define CKM_SHA512 0x00000270 14549+#define CKM_SHA512_HMAC 0x00000271 14550+#define CKM_SHA512_HMAC_GENERAL 0x00000272 14551+ 14552+/* SecurID is new for PKCS #11 v2.20 amendment 1 */ 14553+#define CKM_SECURID_KEY_GEN 0x00000280 14554+#define CKM_SECURID 0x00000282 14555+ 14556+/* HOTP is new for PKCS #11 v2.20 amendment 1 */ 14557+#define CKM_HOTP_KEY_GEN 0x00000290 14558+#define CKM_HOTP 0x00000291 14559+ 14560+/* ACTI is new for PKCS #11 v2.20 amendment 1 */ 14561+#define CKM_ACTI 0x000002A0 14562+#define CKM_ACTI_KEY_GEN 0x000002A1 14563+ 14564+/* All of the following mechanisms are new for v2.0 */ 14565+/* Note that CAST128 and CAST5 are the same algorithm */ 14566+#define CKM_CAST_KEY_GEN 0x00000300 14567+#define CKM_CAST_ECB 0x00000301 14568+#define CKM_CAST_CBC 0x00000302 14569+#define CKM_CAST_MAC 0x00000303 14570+#define CKM_CAST_MAC_GENERAL 0x00000304 14571+#define CKM_CAST_CBC_PAD 0x00000305 14572+#define CKM_CAST3_KEY_GEN 0x00000310 14573+#define CKM_CAST3_ECB 0x00000311 14574+#define CKM_CAST3_CBC 0x00000312 14575+#define CKM_CAST3_MAC 0x00000313 14576+#define CKM_CAST3_MAC_GENERAL 0x00000314 14577+#define CKM_CAST3_CBC_PAD 0x00000315 14578+#define CKM_CAST5_KEY_GEN 0x00000320 14579+#define CKM_CAST128_KEY_GEN 0x00000320 14580+#define CKM_CAST5_ECB 0x00000321 14581+#define CKM_CAST128_ECB 0x00000321 14582+#define CKM_CAST5_CBC 0x00000322 14583+#define CKM_CAST128_CBC 0x00000322 14584+#define CKM_CAST5_MAC 0x00000323 14585+#define CKM_CAST128_MAC 0x00000323 14586+#define CKM_CAST5_MAC_GENERAL 0x00000324 14587+#define CKM_CAST128_MAC_GENERAL 0x00000324 14588+#define CKM_CAST5_CBC_PAD 0x00000325 14589+#define CKM_CAST128_CBC_PAD 0x00000325 14590+#define CKM_RC5_KEY_GEN 0x00000330 14591+#define CKM_RC5_ECB 0x00000331 14592+#define CKM_RC5_CBC 0x00000332 14593+#define CKM_RC5_MAC 0x00000333 14594+#define CKM_RC5_MAC_GENERAL 0x00000334 14595+#define CKM_RC5_CBC_PAD 0x00000335 14596+#define CKM_IDEA_KEY_GEN 0x00000340 14597+#define CKM_IDEA_ECB 0x00000341 14598+#define CKM_IDEA_CBC 0x00000342 14599+#define CKM_IDEA_MAC 0x00000343 14600+#define CKM_IDEA_MAC_GENERAL 0x00000344 14601+#define CKM_IDEA_CBC_PAD 0x00000345 14602+#define CKM_GENERIC_SECRET_KEY_GEN 0x00000350 14603+#define CKM_CONCATENATE_BASE_AND_KEY 0x00000360 14604+#define CKM_CONCATENATE_BASE_AND_DATA 0x00000362 14605+#define CKM_CONCATENATE_DATA_AND_BASE 0x00000363 14606+#define CKM_XOR_BASE_AND_DATA 0x00000364 14607+#define CKM_EXTRACT_KEY_FROM_KEY 0x00000365 14608+#define CKM_SSL3_PRE_MASTER_KEY_GEN 0x00000370 14609+#define CKM_SSL3_MASTER_KEY_DERIVE 0x00000371 14610+#define CKM_SSL3_KEY_AND_MAC_DERIVE 0x00000372 14611+ 14612+/* CKM_SSL3_MASTER_KEY_DERIVE_DH, CKM_TLS_PRE_MASTER_KEY_GEN, 14613+ * CKM_TLS_MASTER_KEY_DERIVE, CKM_TLS_KEY_AND_MAC_DERIVE, and 14614+ * CKM_TLS_MASTER_KEY_DERIVE_DH are new for v2.11 */ 14615+#define CKM_SSL3_MASTER_KEY_DERIVE_DH 0x00000373 14616+#define CKM_TLS_PRE_MASTER_KEY_GEN 0x00000374 14617+#define CKM_TLS_MASTER_KEY_DERIVE 0x00000375 14618+#define CKM_TLS_KEY_AND_MAC_DERIVE 0x00000376 14619+#define CKM_TLS_MASTER_KEY_DERIVE_DH 0x00000377 14620+ 14621+/* CKM_TLS_PRF is new for v2.20 */ 14622+#define CKM_TLS_PRF 0x00000378 14623+ 14624+#define CKM_SSL3_MD5_MAC 0x00000380 14625+#define CKM_SSL3_SHA1_MAC 0x00000381 14626+#define CKM_MD5_KEY_DERIVATION 0x00000390 14627+#define CKM_MD2_KEY_DERIVATION 0x00000391 14628+#define CKM_SHA1_KEY_DERIVATION 0x00000392 14629+ 14630+/* CKM_SHA256/384/512 are new for v2.20 */ 14631+#define CKM_SHA256_KEY_DERIVATION 0x00000393 14632+#define CKM_SHA384_KEY_DERIVATION 0x00000394 14633+#define CKM_SHA512_KEY_DERIVATION 0x00000395 14634+ 14635+/* SHA-224 key derivation is new for PKCS #11 v2.20 amendment 3 */ 14636+#define CKM_SHA224_KEY_DERIVATION 0x00000396 14637+ 14638+#define CKM_PBE_MD2_DES_CBC 0x000003A0 14639+#define CKM_PBE_MD5_DES_CBC 0x000003A1 14640+#define CKM_PBE_MD5_CAST_CBC 0x000003A2 14641+#define CKM_PBE_MD5_CAST3_CBC 0x000003A3 14642+#define CKM_PBE_MD5_CAST5_CBC 0x000003A4 14643+#define CKM_PBE_MD5_CAST128_CBC 0x000003A4 14644+#define CKM_PBE_SHA1_CAST5_CBC 0x000003A5 14645+#define CKM_PBE_SHA1_CAST128_CBC 0x000003A5 14646+#define CKM_PBE_SHA1_RC4_128 0x000003A6 14647+#define CKM_PBE_SHA1_RC4_40 0x000003A7 14648+#define CKM_PBE_SHA1_DES3_EDE_CBC 0x000003A8 14649+#define CKM_PBE_SHA1_DES2_EDE_CBC 0x000003A9 14650+#define CKM_PBE_SHA1_RC2_128_CBC 0x000003AA 14651+#define CKM_PBE_SHA1_RC2_40_CBC 0x000003AB 14652+ 14653+/* CKM_PKCS5_PBKD2 is new for v2.10 */ 14654+#define CKM_PKCS5_PBKD2 0x000003B0 14655+ 14656+#define CKM_PBA_SHA1_WITH_SHA1_HMAC 0x000003C0 14657+ 14658+/* WTLS mechanisms are new for v2.20 */ 14659+#define CKM_WTLS_PRE_MASTER_KEY_GEN 0x000003D0 14660+#define CKM_WTLS_MASTER_KEY_DERIVE 0x000003D1 14661+#define CKM_WTLS_MASTER_KEY_DERIVE_DH_ECC 0x000003D2 14662+#define CKM_WTLS_PRF 0x000003D3 14663+#define CKM_WTLS_SERVER_KEY_AND_MAC_DERIVE 0x000003D4 14664+#define CKM_WTLS_CLIENT_KEY_AND_MAC_DERIVE 0x000003D5 14665+ 14666+#define CKM_KEY_WRAP_LYNKS 0x00000400 14667+#define CKM_KEY_WRAP_SET_OAEP 0x00000401 14668+ 14669+/* CKM_CMS_SIG is new for v2.20 */ 14670+#define CKM_CMS_SIG 0x00000500 14671+ 14672+/* CKM_KIP mechanisms are new for PKCS #11 v2.20 amendment 2 */ 14673+#define CKM_KIP_DERIVE 0x00000510 14674+#define CKM_KIP_WRAP 0x00000511 14675+#define CKM_KIP_MAC 0x00000512 14676+ 14677+/* Camellia is new for PKCS #11 v2.20 amendment 3 */ 14678+#define CKM_CAMELLIA_KEY_GEN 0x00000550 14679+#define CKM_CAMELLIA_ECB 0x00000551 14680+#define CKM_CAMELLIA_CBC 0x00000552 14681+#define CKM_CAMELLIA_MAC 0x00000553 14682+#define CKM_CAMELLIA_MAC_GENERAL 0x00000554 14683+#define CKM_CAMELLIA_CBC_PAD 0x00000555 14684+#define CKM_CAMELLIA_ECB_ENCRYPT_DATA 0x00000556 14685+#define CKM_CAMELLIA_CBC_ENCRYPT_DATA 0x00000557 14686+#define CKM_CAMELLIA_CTR 0x00000558 14687+ 14688+/* ARIA is new for PKCS #11 v2.20 amendment 3 */ 14689+#define CKM_ARIA_KEY_GEN 0x00000560 14690+#define CKM_ARIA_ECB 0x00000561 14691+#define CKM_ARIA_CBC 0x00000562 14692+#define CKM_ARIA_MAC 0x00000563 14693+#define CKM_ARIA_MAC_GENERAL 0x00000564 14694+#define CKM_ARIA_CBC_PAD 0x00000565 14695+#define CKM_ARIA_ECB_ENCRYPT_DATA 0x00000566 14696+#define CKM_ARIA_CBC_ENCRYPT_DATA 0x00000567 14697+ 14698+/* Fortezza mechanisms */ 14699+#define CKM_SKIPJACK_KEY_GEN 0x00001000 14700+#define CKM_SKIPJACK_ECB64 0x00001001 14701+#define CKM_SKIPJACK_CBC64 0x00001002 14702+#define CKM_SKIPJACK_OFB64 0x00001003 14703+#define CKM_SKIPJACK_CFB64 0x00001004 14704+#define CKM_SKIPJACK_CFB32 0x00001005 14705+#define CKM_SKIPJACK_CFB16 0x00001006 14706+#define CKM_SKIPJACK_CFB8 0x00001007 14707+#define CKM_SKIPJACK_WRAP 0x00001008 14708+#define CKM_SKIPJACK_PRIVATE_WRAP 0x00001009 14709+#define CKM_SKIPJACK_RELAYX 0x0000100a 14710+#define CKM_KEA_KEY_PAIR_GEN 0x00001010 14711+#define CKM_KEA_KEY_DERIVE 0x00001011 14712+#define CKM_FORTEZZA_TIMESTAMP 0x00001020 14713+#define CKM_BATON_KEY_GEN 0x00001030 14714+#define CKM_BATON_ECB128 0x00001031 14715+#define CKM_BATON_ECB96 0x00001032 14716+#define CKM_BATON_CBC128 0x00001033 14717+#define CKM_BATON_COUNTER 0x00001034 14718+#define CKM_BATON_SHUFFLE 0x00001035 14719+#define CKM_BATON_WRAP 0x00001036 14720+ 14721+/* CKM_ECDSA_KEY_PAIR_GEN is deprecated in v2.11, 14722+ * CKM_EC_KEY_PAIR_GEN is preferred */ 14723+#define CKM_ECDSA_KEY_PAIR_GEN 0x00001040 14724+#define CKM_EC_KEY_PAIR_GEN 0x00001040 14725+ 14726+#define CKM_ECDSA 0x00001041 14727+#define CKM_ECDSA_SHA1 0x00001042 14728+ 14729+/* CKM_ECDH1_DERIVE, CKM_ECDH1_COFACTOR_DERIVE, and CKM_ECMQV_DERIVE 14730+ * are new for v2.11 */ 14731+#define CKM_ECDH1_DERIVE 0x00001050 14732+#define CKM_ECDH1_COFACTOR_DERIVE 0x00001051 14733+#define CKM_ECMQV_DERIVE 0x00001052 14734+ 14735+#define CKM_JUNIPER_KEY_GEN 0x00001060 14736+#define CKM_JUNIPER_ECB128 0x00001061 14737+#define CKM_JUNIPER_CBC128 0x00001062 14738+#define CKM_JUNIPER_COUNTER 0x00001063 14739+#define CKM_JUNIPER_SHUFFLE 0x00001064 14740+#define CKM_JUNIPER_WRAP 0x00001065 14741+#define CKM_FASTHASH 0x00001070 14742+ 14743+/* CKM_AES_KEY_GEN, CKM_AES_ECB, CKM_AES_CBC, CKM_AES_MAC, 14744+ * CKM_AES_MAC_GENERAL, CKM_AES_CBC_PAD, CKM_DSA_PARAMETER_GEN, 14745+ * CKM_DH_PKCS_PARAMETER_GEN, and CKM_X9_42_DH_PARAMETER_GEN are 14746+ * new for v2.11 */ 14747+#define CKM_AES_KEY_GEN 0x00001080 14748+#define CKM_AES_ECB 0x00001081 14749+#define CKM_AES_CBC 0x00001082 14750+#define CKM_AES_MAC 0x00001083 14751+#define CKM_AES_MAC_GENERAL 0x00001084 14752+#define CKM_AES_CBC_PAD 0x00001085 14753+ 14754+/* AES counter mode is new for PKCS #11 v2.20 amendment 3 */ 14755+#define CKM_AES_CTR 0x00001086 14756+ 14757+/* BlowFish and TwoFish are new for v2.20 */ 14758+#define CKM_BLOWFISH_KEY_GEN 0x00001090 14759+#define CKM_BLOWFISH_CBC 0x00001091 14760+#define CKM_TWOFISH_KEY_GEN 0x00001092 14761+#define CKM_TWOFISH_CBC 0x00001093 14762+ 14763+ 14764+/* CKM_xxx_ENCRYPT_DATA mechanisms are new for v2.20 */ 14765+#define CKM_DES_ECB_ENCRYPT_DATA 0x00001100 14766+#define CKM_DES_CBC_ENCRYPT_DATA 0x00001101 14767+#define CKM_DES3_ECB_ENCRYPT_DATA 0x00001102 14768+#define CKM_DES3_CBC_ENCRYPT_DATA 0x00001103 14769+#define CKM_AES_ECB_ENCRYPT_DATA 0x00001104 14770+#define CKM_AES_CBC_ENCRYPT_DATA 0x00001105 14771+ 14772+#define CKM_DSA_PARAMETER_GEN 0x00002000 14773+#define CKM_DH_PKCS_PARAMETER_GEN 0x00002001 14774+#define CKM_X9_42_DH_PARAMETER_GEN 0x00002002 14775+ 14776+#define CKM_VENDOR_DEFINED 0x80000000 14777+ 14778+typedef CK_MECHANISM_TYPE CK_PTR CK_MECHANISM_TYPE_PTR; 14779+ 14780+ 14781+/* CK_MECHANISM is a structure that specifies a particular 14782+ * mechanism */ 14783+typedef struct CK_MECHANISM { 14784+ CK_MECHANISM_TYPE mechanism; 14785+ CK_VOID_PTR pParameter; 14786+ 14787+ /* ulParameterLen was changed from CK_USHORT to CK_ULONG for 14788+ * v2.0 */ 14789+ CK_ULONG ulParameterLen; /* in bytes */ 14790+} CK_MECHANISM; 14791+ 14792+typedef CK_MECHANISM CK_PTR CK_MECHANISM_PTR; 14793+ 14794+ 14795+/* CK_MECHANISM_INFO provides information about a particular 14796+ * mechanism */ 14797+typedef struct CK_MECHANISM_INFO { 14798+ CK_ULONG ulMinKeySize; 14799+ CK_ULONG ulMaxKeySize; 14800+ CK_FLAGS flags; 14801+} CK_MECHANISM_INFO; 14802+ 14803+/* The flags are defined as follows: 14804+ * Bit Flag Mask Meaning */ 14805+#define CKF_HW 0x00000001 /* performed by HW */ 14806+ 14807+/* The flags CKF_ENCRYPT, CKF_DECRYPT, CKF_DIGEST, CKF_SIGN, 14808+ * CKG_SIGN_RECOVER, CKF_VERIFY, CKF_VERIFY_RECOVER, 14809+ * CKF_GENERATE, CKF_GENERATE_KEY_PAIR, CKF_WRAP, CKF_UNWRAP, 14810+ * and CKF_DERIVE are new for v2.0. They specify whether or not 14811+ * a mechanism can be used for a particular task */ 14812+#define CKF_ENCRYPT 0x00000100 14813+#define CKF_DECRYPT 0x00000200 14814+#define CKF_DIGEST 0x00000400 14815+#define CKF_SIGN 0x00000800 14816+#define CKF_SIGN_RECOVER 0x00001000 14817+#define CKF_VERIFY 0x00002000 14818+#define CKF_VERIFY_RECOVER 0x00004000 14819+#define CKF_GENERATE 0x00008000 14820+#define CKF_GENERATE_KEY_PAIR 0x00010000 14821+#define CKF_WRAP 0x00020000 14822+#define CKF_UNWRAP 0x00040000 14823+#define CKF_DERIVE 0x00080000 14824+ 14825+/* CKF_EC_F_P, CKF_EC_F_2M, CKF_EC_ECPARAMETERS, CKF_EC_NAMEDCURVE, 14826+ * CKF_EC_UNCOMPRESS, and CKF_EC_COMPRESS are new for v2.11. They 14827+ * describe a token's EC capabilities not available in mechanism 14828+ * information. */ 14829+#define CKF_EC_F_P 0x00100000 14830+#define CKF_EC_F_2M 0x00200000 14831+#define CKF_EC_ECPARAMETERS 0x00400000 14832+#define CKF_EC_NAMEDCURVE 0x00800000 14833+#define CKF_EC_UNCOMPRESS 0x01000000 14834+#define CKF_EC_COMPRESS 0x02000000 14835+ 14836+#define CKF_EXTENSION 0x80000000 /* FALSE for this version */ 14837+ 14838+typedef CK_MECHANISM_INFO CK_PTR CK_MECHANISM_INFO_PTR; 14839+ 14840+ 14841+/* CK_RV is a value that identifies the return value of a 14842+ * Cryptoki function */ 14843+/* CK_RV was changed from CK_USHORT to CK_ULONG for v2.0 */ 14844+typedef CK_ULONG CK_RV; 14845+ 14846+#define CKR_OK 0x00000000 14847+#define CKR_CANCEL 0x00000001 14848+#define CKR_HOST_MEMORY 0x00000002 14849+#define CKR_SLOT_ID_INVALID 0x00000003 14850+ 14851+/* CKR_FLAGS_INVALID was removed for v2.0 */ 14852+ 14853+/* CKR_GENERAL_ERROR and CKR_FUNCTION_FAILED are new for v2.0 */ 14854+#define CKR_GENERAL_ERROR 0x00000005 14855+#define CKR_FUNCTION_FAILED 0x00000006 14856+ 14857+/* CKR_ARGUMENTS_BAD, CKR_NO_EVENT, CKR_NEED_TO_CREATE_THREADS, 14858+ * and CKR_CANT_LOCK are new for v2.01 */ 14859+#define CKR_ARGUMENTS_BAD 0x00000007 14860+#define CKR_NO_EVENT 0x00000008 14861+#define CKR_NEED_TO_CREATE_THREADS 0x00000009 14862+#define CKR_CANT_LOCK 0x0000000A 14863+ 14864+#define CKR_ATTRIBUTE_READ_ONLY 0x00000010 14865+#define CKR_ATTRIBUTE_SENSITIVE 0x00000011 14866+#define CKR_ATTRIBUTE_TYPE_INVALID 0x00000012 14867+#define CKR_ATTRIBUTE_VALUE_INVALID 0x00000013 14868+#define CKR_DATA_INVALID 0x00000020 14869+#define CKR_DATA_LEN_RANGE 0x00000021 14870+#define CKR_DEVICE_ERROR 0x00000030 14871+#define CKR_DEVICE_MEMORY 0x00000031 14872+#define CKR_DEVICE_REMOVED 0x00000032 14873+#define CKR_ENCRYPTED_DATA_INVALID 0x00000040 14874+#define CKR_ENCRYPTED_DATA_LEN_RANGE 0x00000041 14875+#define CKR_FUNCTION_CANCELED 0x00000050 14876+#define CKR_FUNCTION_NOT_PARALLEL 0x00000051 14877+ 14878+/* CKR_FUNCTION_NOT_SUPPORTED is new for v2.0 */ 14879+#define CKR_FUNCTION_NOT_SUPPORTED 0x00000054 14880+ 14881+#define CKR_KEY_HANDLE_INVALID 0x00000060 14882+ 14883+/* CKR_KEY_SENSITIVE was removed for v2.0 */ 14884+ 14885+#define CKR_KEY_SIZE_RANGE 0x00000062 14886+#define CKR_KEY_TYPE_INCONSISTENT 0x00000063 14887+ 14888+/* CKR_KEY_NOT_NEEDED, CKR_KEY_CHANGED, CKR_KEY_NEEDED, 14889+ * CKR_KEY_INDIGESTIBLE, CKR_KEY_FUNCTION_NOT_PERMITTED, 14890+ * CKR_KEY_NOT_WRAPPABLE, and CKR_KEY_UNEXTRACTABLE are new for 14891+ * v2.0 */ 14892+#define CKR_KEY_NOT_NEEDED 0x00000064 14893+#define CKR_KEY_CHANGED 0x00000065 14894+#define CKR_KEY_NEEDED 0x00000066 14895+#define CKR_KEY_INDIGESTIBLE 0x00000067 14896+#define CKR_KEY_FUNCTION_NOT_PERMITTED 0x00000068 14897+#define CKR_KEY_NOT_WRAPPABLE 0x00000069 14898+#define CKR_KEY_UNEXTRACTABLE 0x0000006A 14899+ 14900+#define CKR_MECHANISM_INVALID 0x00000070 14901+#define CKR_MECHANISM_PARAM_INVALID 0x00000071 14902+ 14903+/* CKR_OBJECT_CLASS_INCONSISTENT and CKR_OBJECT_CLASS_INVALID 14904+ * were removed for v2.0 */ 14905+#define CKR_OBJECT_HANDLE_INVALID 0x00000082 14906+#define CKR_OPERATION_ACTIVE 0x00000090 14907+#define CKR_OPERATION_NOT_INITIALIZED 0x00000091 14908+#define CKR_PIN_INCORRECT 0x000000A0 14909+#define CKR_PIN_INVALID 0x000000A1 14910+#define CKR_PIN_LEN_RANGE 0x000000A2 14911+ 14912+/* CKR_PIN_EXPIRED and CKR_PIN_LOCKED are new for v2.0 */ 14913+#define CKR_PIN_EXPIRED 0x000000A3 14914+#define CKR_PIN_LOCKED 0x000000A4 14915+ 14916+#define CKR_SESSION_CLOSED 0x000000B0 14917+#define CKR_SESSION_COUNT 0x000000B1 14918+#define CKR_SESSION_HANDLE_INVALID 0x000000B3 14919+#define CKR_SESSION_PARALLEL_NOT_SUPPORTED 0x000000B4 14920+#define CKR_SESSION_READ_ONLY 0x000000B5 14921+#define CKR_SESSION_EXISTS 0x000000B6 14922+ 14923+/* CKR_SESSION_READ_ONLY_EXISTS and 14924+ * CKR_SESSION_READ_WRITE_SO_EXISTS are new for v2.0 */ 14925+#define CKR_SESSION_READ_ONLY_EXISTS 0x000000B7 14926+#define CKR_SESSION_READ_WRITE_SO_EXISTS 0x000000B8 14927+ 14928+#define CKR_SIGNATURE_INVALID 0x000000C0 14929+#define CKR_SIGNATURE_LEN_RANGE 0x000000C1 14930+#define CKR_TEMPLATE_INCOMPLETE 0x000000D0 14931+#define CKR_TEMPLATE_INCONSISTENT 0x000000D1 14932+#define CKR_TOKEN_NOT_PRESENT 0x000000E0 14933+#define CKR_TOKEN_NOT_RECOGNIZED 0x000000E1 14934+#define CKR_TOKEN_WRITE_PROTECTED 0x000000E2 14935+#define CKR_UNWRAPPING_KEY_HANDLE_INVALID 0x000000F0 14936+#define CKR_UNWRAPPING_KEY_SIZE_RANGE 0x000000F1 14937+#define CKR_UNWRAPPING_KEY_TYPE_INCONSISTENT 0x000000F2 14938+#define CKR_USER_ALREADY_LOGGED_IN 0x00000100 14939+#define CKR_USER_NOT_LOGGED_IN 0x00000101 14940+#define CKR_USER_PIN_NOT_INITIALIZED 0x00000102 14941+#define CKR_USER_TYPE_INVALID 0x00000103 14942+ 14943+/* CKR_USER_ANOTHER_ALREADY_LOGGED_IN and CKR_USER_TOO_MANY_TYPES 14944+ * are new to v2.01 */ 14945+#define CKR_USER_ANOTHER_ALREADY_LOGGED_IN 0x00000104 14946+#define CKR_USER_TOO_MANY_TYPES 0x00000105 14947+ 14948+#define CKR_WRAPPED_KEY_INVALID 0x00000110 14949+#define CKR_WRAPPED_KEY_LEN_RANGE 0x00000112 14950+#define CKR_WRAPPING_KEY_HANDLE_INVALID 0x00000113 14951+#define CKR_WRAPPING_KEY_SIZE_RANGE 0x00000114 14952+#define CKR_WRAPPING_KEY_TYPE_INCONSISTENT 0x00000115 14953+#define CKR_RANDOM_SEED_NOT_SUPPORTED 0x00000120 14954+ 14955+/* These are new to v2.0 */ 14956+#define CKR_RANDOM_NO_RNG 0x00000121 14957+ 14958+/* These are new to v2.11 */ 14959+#define CKR_DOMAIN_PARAMS_INVALID 0x00000130 14960+ 14961+/* These are new to v2.0 */ 14962+#define CKR_BUFFER_TOO_SMALL 0x00000150 14963+#define CKR_SAVED_STATE_INVALID 0x00000160 14964+#define CKR_INFORMATION_SENSITIVE 0x00000170 14965+#define CKR_STATE_UNSAVEABLE 0x00000180 14966+ 14967+/* These are new to v2.01 */ 14968+#define CKR_CRYPTOKI_NOT_INITIALIZED 0x00000190 14969+#define CKR_CRYPTOKI_ALREADY_INITIALIZED 0x00000191 14970+#define CKR_MUTEX_BAD 0x000001A0 14971+#define CKR_MUTEX_NOT_LOCKED 0x000001A1 14972+ 14973+/* The following return values are new for PKCS #11 v2.20 amendment 3 */ 14974+#define CKR_NEW_PIN_MODE 0x000001B0 14975+#define CKR_NEXT_OTP 0x000001B1 14976+ 14977+/* This is new to v2.20 */ 14978+#define CKR_FUNCTION_REJECTED 0x00000200 14979+ 14980+#define CKR_VENDOR_DEFINED 0x80000000 14981+ 14982+ 14983+/* CK_NOTIFY is an application callback that processes events */ 14984+typedef CK_CALLBACK_FUNCTION(CK_RV, CK_NOTIFY)( 14985+ CK_SESSION_HANDLE hSession, /* the session's handle */ 14986+ CK_NOTIFICATION event, 14987+ CK_VOID_PTR pApplication /* passed to C_OpenSession */ 14988+); 14989+ 14990+ 14991+/* CK_FUNCTION_LIST is a structure holding a Cryptoki spec 14992+ * version and pointers of appropriate types to all the 14993+ * Cryptoki functions */ 14994+/* CK_FUNCTION_LIST is new for v2.0 */ 14995+typedef struct CK_FUNCTION_LIST CK_FUNCTION_LIST; 14996+ 14997+typedef CK_FUNCTION_LIST CK_PTR CK_FUNCTION_LIST_PTR; 14998+ 14999+typedef CK_FUNCTION_LIST_PTR CK_PTR CK_FUNCTION_LIST_PTR_PTR; 15000+ 15001+ 15002+/* CK_CREATEMUTEX is an application callback for creating a 15003+ * mutex object */ 15004+typedef CK_CALLBACK_FUNCTION(CK_RV, CK_CREATEMUTEX)( 15005+ CK_VOID_PTR_PTR ppMutex /* location to receive ptr to mutex */ 15006+); 15007+ 15008+ 15009+/* CK_DESTROYMUTEX is an application callback for destroying a 15010+ * mutex object */ 15011+typedef CK_CALLBACK_FUNCTION(CK_RV, CK_DESTROYMUTEX)( 15012+ CK_VOID_PTR pMutex /* pointer to mutex */ 15013+); 15014+ 15015+ 15016+/* CK_LOCKMUTEX is an application callback for locking a mutex */ 15017+typedef CK_CALLBACK_FUNCTION(CK_RV, CK_LOCKMUTEX)( 15018+ CK_VOID_PTR pMutex /* pointer to mutex */ 15019+); 15020+ 15021+ 15022+/* CK_UNLOCKMUTEX is an application callback for unlocking a 15023+ * mutex */ 15024+typedef CK_CALLBACK_FUNCTION(CK_RV, CK_UNLOCKMUTEX)( 15025+ CK_VOID_PTR pMutex /* pointer to mutex */ 15026+); 15027+ 15028+ 15029+/* CK_C_INITIALIZE_ARGS provides the optional arguments to 15030+ * C_Initialize */ 15031+typedef struct CK_C_INITIALIZE_ARGS { 15032+ CK_CREATEMUTEX CreateMutex; 15033+ CK_DESTROYMUTEX DestroyMutex; 15034+ CK_LOCKMUTEX LockMutex; 15035+ CK_UNLOCKMUTEX UnlockMutex; 15036+ CK_FLAGS flags; 15037+ CK_VOID_PTR pReserved; 15038+} CK_C_INITIALIZE_ARGS; 15039+ 15040+/* flags: bit flags that provide capabilities of the slot 15041+ * Bit Flag Mask Meaning 15042+ */ 15043+#define CKF_LIBRARY_CANT_CREATE_OS_THREADS 0x00000001 15044+#define CKF_OS_LOCKING_OK 0x00000002 15045+ 15046+typedef CK_C_INITIALIZE_ARGS CK_PTR CK_C_INITIALIZE_ARGS_PTR; 15047+ 15048+ 15049+/* additional flags for parameters to functions */ 15050+ 15051+/* CKF_DONT_BLOCK is for the function C_WaitForSlotEvent */ 15052+#define CKF_DONT_BLOCK 1 15053+ 15054+/* CK_RSA_PKCS_OAEP_MGF_TYPE is new for v2.10. 15055+ * CK_RSA_PKCS_OAEP_MGF_TYPE is used to indicate the Message 15056+ * Generation Function (MGF) applied to a message block when 15057+ * formatting a message block for the PKCS #1 OAEP encryption 15058+ * scheme. */ 15059+typedef CK_ULONG CK_RSA_PKCS_MGF_TYPE; 15060+ 15061+typedef CK_RSA_PKCS_MGF_TYPE CK_PTR CK_RSA_PKCS_MGF_TYPE_PTR; 15062+ 15063+/* The following MGFs are defined */ 15064+/* CKG_MGF1_SHA256, CKG_MGF1_SHA384, and CKG_MGF1_SHA512 15065+ * are new for v2.20 */ 15066+#define CKG_MGF1_SHA1 0x00000001 15067+#define CKG_MGF1_SHA256 0x00000002 15068+#define CKG_MGF1_SHA384 0x00000003 15069+#define CKG_MGF1_SHA512 0x00000004 15070+/* SHA-224 is new for PKCS #11 v2.20 amendment 3 */ 15071+#define CKG_MGF1_SHA224 0x00000005 15072+ 15073+/* CK_RSA_PKCS_OAEP_SOURCE_TYPE is new for v2.10. 15074+ * CK_RSA_PKCS_OAEP_SOURCE_TYPE is used to indicate the source 15075+ * of the encoding parameter when formatting a message block 15076+ * for the PKCS #1 OAEP encryption scheme. */ 15077+typedef CK_ULONG CK_RSA_PKCS_OAEP_SOURCE_TYPE; 15078+ 15079+typedef CK_RSA_PKCS_OAEP_SOURCE_TYPE CK_PTR CK_RSA_PKCS_OAEP_SOURCE_TYPE_PTR; 15080+ 15081+/* The following encoding parameter sources are defined */ 15082+#define CKZ_DATA_SPECIFIED 0x00000001 15083+ 15084+/* CK_RSA_PKCS_OAEP_PARAMS is new for v2.10. 15085+ * CK_RSA_PKCS_OAEP_PARAMS provides the parameters to the 15086+ * CKM_RSA_PKCS_OAEP mechanism. */ 15087+typedef struct CK_RSA_PKCS_OAEP_PARAMS { 15088+ CK_MECHANISM_TYPE hashAlg; 15089+ CK_RSA_PKCS_MGF_TYPE mgf; 15090+ CK_RSA_PKCS_OAEP_SOURCE_TYPE source; 15091+ CK_VOID_PTR pSourceData; 15092+ CK_ULONG ulSourceDataLen; 15093+} CK_RSA_PKCS_OAEP_PARAMS; 15094+ 15095+typedef CK_RSA_PKCS_OAEP_PARAMS CK_PTR CK_RSA_PKCS_OAEP_PARAMS_PTR; 15096+ 15097+/* CK_RSA_PKCS_PSS_PARAMS is new for v2.11. 15098+ * CK_RSA_PKCS_PSS_PARAMS provides the parameters to the 15099+ * CKM_RSA_PKCS_PSS mechanism(s). */ 15100+typedef struct CK_RSA_PKCS_PSS_PARAMS { 15101+ CK_MECHANISM_TYPE hashAlg; 15102+ CK_RSA_PKCS_MGF_TYPE mgf; 15103+ CK_ULONG sLen; 15104+} CK_RSA_PKCS_PSS_PARAMS; 15105+ 15106+typedef CK_RSA_PKCS_PSS_PARAMS CK_PTR CK_RSA_PKCS_PSS_PARAMS_PTR; 15107+ 15108+/* CK_EC_KDF_TYPE is new for v2.11. */ 15109+typedef CK_ULONG CK_EC_KDF_TYPE; 15110+ 15111+/* The following EC Key Derivation Functions are defined */ 15112+#define CKD_NULL 0x00000001 15113+#define CKD_SHA1_KDF 0x00000002 15114+ 15115+/* CK_ECDH1_DERIVE_PARAMS is new for v2.11. 15116+ * CK_ECDH1_DERIVE_PARAMS provides the parameters to the 15117+ * CKM_ECDH1_DERIVE and CKM_ECDH1_COFACTOR_DERIVE mechanisms, 15118+ * where each party contributes one key pair. 15119+ */ 15120+typedef struct CK_ECDH1_DERIVE_PARAMS { 15121+ CK_EC_KDF_TYPE kdf; 15122+ CK_ULONG ulSharedDataLen; 15123+ CK_BYTE_PTR pSharedData; 15124+ CK_ULONG ulPublicDataLen; 15125+ CK_BYTE_PTR pPublicData; 15126+} CK_ECDH1_DERIVE_PARAMS; 15127+ 15128+typedef CK_ECDH1_DERIVE_PARAMS CK_PTR CK_ECDH1_DERIVE_PARAMS_PTR; 15129+ 15130+ 15131+/* CK_ECDH2_DERIVE_PARAMS is new for v2.11. 15132+ * CK_ECDH2_DERIVE_PARAMS provides the parameters to the 15133+ * CKM_ECMQV_DERIVE mechanism, where each party contributes two key pairs. */ 15134+typedef struct CK_ECDH2_DERIVE_PARAMS { 15135+ CK_EC_KDF_TYPE kdf; 15136+ CK_ULONG ulSharedDataLen; 15137+ CK_BYTE_PTR pSharedData; 15138+ CK_ULONG ulPublicDataLen; 15139+ CK_BYTE_PTR pPublicData; 15140+ CK_ULONG ulPrivateDataLen; 15141+ CK_OBJECT_HANDLE hPrivateData; 15142+ CK_ULONG ulPublicDataLen2; 15143+ CK_BYTE_PTR pPublicData2; 15144+} CK_ECDH2_DERIVE_PARAMS; 15145+ 15146+typedef CK_ECDH2_DERIVE_PARAMS CK_PTR CK_ECDH2_DERIVE_PARAMS_PTR; 15147+ 15148+typedef struct CK_ECMQV_DERIVE_PARAMS { 15149+ CK_EC_KDF_TYPE kdf; 15150+ CK_ULONG ulSharedDataLen; 15151+ CK_BYTE_PTR pSharedData; 15152+ CK_ULONG ulPublicDataLen; 15153+ CK_BYTE_PTR pPublicData; 15154+ CK_ULONG ulPrivateDataLen; 15155+ CK_OBJECT_HANDLE hPrivateData; 15156+ CK_ULONG ulPublicDataLen2; 15157+ CK_BYTE_PTR pPublicData2; 15158+ CK_OBJECT_HANDLE publicKey; 15159+} CK_ECMQV_DERIVE_PARAMS; 15160+ 15161+typedef CK_ECMQV_DERIVE_PARAMS CK_PTR CK_ECMQV_DERIVE_PARAMS_PTR; 15162+ 15163+/* Typedefs and defines for the CKM_X9_42_DH_KEY_PAIR_GEN and the 15164+ * CKM_X9_42_DH_PARAMETER_GEN mechanisms (new for PKCS #11 v2.11) */ 15165+typedef CK_ULONG CK_X9_42_DH_KDF_TYPE; 15166+typedef CK_X9_42_DH_KDF_TYPE CK_PTR CK_X9_42_DH_KDF_TYPE_PTR; 15167+ 15168+/* The following X9.42 DH key derivation functions are defined 15169+ (besides CKD_NULL already defined : */ 15170+#define CKD_SHA1_KDF_ASN1 0x00000003 15171+#define CKD_SHA1_KDF_CONCATENATE 0x00000004 15172+ 15173+/* CK_X9_42_DH1_DERIVE_PARAMS is new for v2.11. 15174+ * CK_X9_42_DH1_DERIVE_PARAMS provides the parameters to the 15175+ * CKM_X9_42_DH_DERIVE key derivation mechanism, where each party 15176+ * contributes one key pair */ 15177+typedef struct CK_X9_42_DH1_DERIVE_PARAMS { 15178+ CK_X9_42_DH_KDF_TYPE kdf; 15179+ CK_ULONG ulOtherInfoLen; 15180+ CK_BYTE_PTR pOtherInfo; 15181+ CK_ULONG ulPublicDataLen; 15182+ CK_BYTE_PTR pPublicData; 15183+} CK_X9_42_DH1_DERIVE_PARAMS; 15184+ 15185+typedef struct CK_X9_42_DH1_DERIVE_PARAMS CK_PTR CK_X9_42_DH1_DERIVE_PARAMS_PTR; 15186+ 15187+/* CK_X9_42_DH2_DERIVE_PARAMS is new for v2.11. 15188+ * CK_X9_42_DH2_DERIVE_PARAMS provides the parameters to the 15189+ * CKM_X9_42_DH_HYBRID_DERIVE and CKM_X9_42_MQV_DERIVE key derivation 15190+ * mechanisms, where each party contributes two key pairs */ 15191+typedef struct CK_X9_42_DH2_DERIVE_PARAMS { 15192+ CK_X9_42_DH_KDF_TYPE kdf; 15193+ CK_ULONG ulOtherInfoLen; 15194+ CK_BYTE_PTR pOtherInfo; 15195+ CK_ULONG ulPublicDataLen; 15196+ CK_BYTE_PTR pPublicData; 15197+ CK_ULONG ulPrivateDataLen; 15198+ CK_OBJECT_HANDLE hPrivateData; 15199+ CK_ULONG ulPublicDataLen2; 15200+ CK_BYTE_PTR pPublicData2; 15201+} CK_X9_42_DH2_DERIVE_PARAMS; 15202+ 15203+typedef CK_X9_42_DH2_DERIVE_PARAMS CK_PTR CK_X9_42_DH2_DERIVE_PARAMS_PTR; 15204+ 15205+typedef struct CK_X9_42_MQV_DERIVE_PARAMS { 15206+ CK_X9_42_DH_KDF_TYPE kdf; 15207+ CK_ULONG ulOtherInfoLen; 15208+ CK_BYTE_PTR pOtherInfo; 15209+ CK_ULONG ulPublicDataLen; 15210+ CK_BYTE_PTR pPublicData; 15211+ CK_ULONG ulPrivateDataLen; 15212+ CK_OBJECT_HANDLE hPrivateData; 15213+ CK_ULONG ulPublicDataLen2; 15214+ CK_BYTE_PTR pPublicData2; 15215+ CK_OBJECT_HANDLE publicKey; 15216+} CK_X9_42_MQV_DERIVE_PARAMS; 15217+ 15218+typedef CK_X9_42_MQV_DERIVE_PARAMS CK_PTR CK_X9_42_MQV_DERIVE_PARAMS_PTR; 15219+ 15220+/* CK_KEA_DERIVE_PARAMS provides the parameters to the 15221+ * CKM_KEA_DERIVE mechanism */ 15222+/* CK_KEA_DERIVE_PARAMS is new for v2.0 */ 15223+typedef struct CK_KEA_DERIVE_PARAMS { 15224+ CK_BBOOL isSender; 15225+ CK_ULONG ulRandomLen; 15226+ CK_BYTE_PTR pRandomA; 15227+ CK_BYTE_PTR pRandomB; 15228+ CK_ULONG ulPublicDataLen; 15229+ CK_BYTE_PTR pPublicData; 15230+} CK_KEA_DERIVE_PARAMS; 15231+ 15232+typedef CK_KEA_DERIVE_PARAMS CK_PTR CK_KEA_DERIVE_PARAMS_PTR; 15233+ 15234+ 15235+/* CK_RC2_PARAMS provides the parameters to the CKM_RC2_ECB and 15236+ * CKM_RC2_MAC mechanisms. An instance of CK_RC2_PARAMS just 15237+ * holds the effective keysize */ 15238+typedef CK_ULONG CK_RC2_PARAMS; 15239+ 15240+typedef CK_RC2_PARAMS CK_PTR CK_RC2_PARAMS_PTR; 15241+ 15242+ 15243+/* CK_RC2_CBC_PARAMS provides the parameters to the CKM_RC2_CBC 15244+ * mechanism */ 15245+typedef struct CK_RC2_CBC_PARAMS { 15246+ /* ulEffectiveBits was changed from CK_USHORT to CK_ULONG for 15247+ * v2.0 */ 15248+ CK_ULONG ulEffectiveBits; /* effective bits (1-1024) */ 15249+ 15250+ CK_BYTE iv[8]; /* IV for CBC mode */ 15251+} CK_RC2_CBC_PARAMS; 15252+ 15253+typedef CK_RC2_CBC_PARAMS CK_PTR CK_RC2_CBC_PARAMS_PTR; 15254+ 15255+ 15256+/* CK_RC2_MAC_GENERAL_PARAMS provides the parameters for the 15257+ * CKM_RC2_MAC_GENERAL mechanism */ 15258+/* CK_RC2_MAC_GENERAL_PARAMS is new for v2.0 */ 15259+typedef struct CK_RC2_MAC_GENERAL_PARAMS { 15260+ CK_ULONG ulEffectiveBits; /* effective bits (1-1024) */ 15261+ CK_ULONG ulMacLength; /* Length of MAC in bytes */ 15262+} CK_RC2_MAC_GENERAL_PARAMS; 15263+ 15264+typedef CK_RC2_MAC_GENERAL_PARAMS CK_PTR \ 15265+ CK_RC2_MAC_GENERAL_PARAMS_PTR; 15266+ 15267+ 15268+/* CK_RC5_PARAMS provides the parameters to the CKM_RC5_ECB and 15269+ * CKM_RC5_MAC mechanisms */ 15270+/* CK_RC5_PARAMS is new for v2.0 */ 15271+typedef struct CK_RC5_PARAMS { 15272+ CK_ULONG ulWordsize; /* wordsize in bits */ 15273+ CK_ULONG ulRounds; /* number of rounds */ 15274+} CK_RC5_PARAMS; 15275+ 15276+typedef CK_RC5_PARAMS CK_PTR CK_RC5_PARAMS_PTR; 15277+ 15278+ 15279+/* CK_RC5_CBC_PARAMS provides the parameters to the CKM_RC5_CBC 15280+ * mechanism */ 15281+/* CK_RC5_CBC_PARAMS is new for v2.0 */ 15282+typedef struct CK_RC5_CBC_PARAMS { 15283+ CK_ULONG ulWordsize; /* wordsize in bits */ 15284+ CK_ULONG ulRounds; /* number of rounds */ 15285+ CK_BYTE_PTR pIv; /* pointer to IV */ 15286+ CK_ULONG ulIvLen; /* length of IV in bytes */ 15287+} CK_RC5_CBC_PARAMS; 15288+ 15289+typedef CK_RC5_CBC_PARAMS CK_PTR CK_RC5_CBC_PARAMS_PTR; 15290+ 15291+ 15292+/* CK_RC5_MAC_GENERAL_PARAMS provides the parameters for the 15293+ * CKM_RC5_MAC_GENERAL mechanism */ 15294+/* CK_RC5_MAC_GENERAL_PARAMS is new for v2.0 */ 15295+typedef struct CK_RC5_MAC_GENERAL_PARAMS { 15296+ CK_ULONG ulWordsize; /* wordsize in bits */ 15297+ CK_ULONG ulRounds; /* number of rounds */ 15298+ CK_ULONG ulMacLength; /* Length of MAC in bytes */ 15299+} CK_RC5_MAC_GENERAL_PARAMS; 15300+ 15301+typedef CK_RC5_MAC_GENERAL_PARAMS CK_PTR \ 15302+ CK_RC5_MAC_GENERAL_PARAMS_PTR; 15303+ 15304+ 15305+/* CK_MAC_GENERAL_PARAMS provides the parameters to most block 15306+ * ciphers' MAC_GENERAL mechanisms. Its value is the length of 15307+ * the MAC */ 15308+/* CK_MAC_GENERAL_PARAMS is new for v2.0 */ 15309+typedef CK_ULONG CK_MAC_GENERAL_PARAMS; 15310+ 15311+typedef CK_MAC_GENERAL_PARAMS CK_PTR CK_MAC_GENERAL_PARAMS_PTR; 15312+ 15313+/* CK_DES/AES_ECB/CBC_ENCRYPT_DATA_PARAMS are new for v2.20 */ 15314+typedef struct CK_DES_CBC_ENCRYPT_DATA_PARAMS { 15315+ CK_BYTE iv[8]; 15316+ CK_BYTE_PTR pData; 15317+ CK_ULONG length; 15318+} CK_DES_CBC_ENCRYPT_DATA_PARAMS; 15319+ 15320+typedef CK_DES_CBC_ENCRYPT_DATA_PARAMS CK_PTR CK_DES_CBC_ENCRYPT_DATA_PARAMS_PTR; 15321+ 15322+typedef struct CK_AES_CBC_ENCRYPT_DATA_PARAMS { 15323+ CK_BYTE iv[16]; 15324+ CK_BYTE_PTR pData; 15325+ CK_ULONG length; 15326+} CK_AES_CBC_ENCRYPT_DATA_PARAMS; 15327+ 15328+typedef CK_AES_CBC_ENCRYPT_DATA_PARAMS CK_PTR CK_AES_CBC_ENCRYPT_DATA_PARAMS_PTR; 15329+ 15330+/* CK_SKIPJACK_PRIVATE_WRAP_PARAMS provides the parameters to the 15331+ * CKM_SKIPJACK_PRIVATE_WRAP mechanism */ 15332+/* CK_SKIPJACK_PRIVATE_WRAP_PARAMS is new for v2.0 */ 15333+typedef struct CK_SKIPJACK_PRIVATE_WRAP_PARAMS { 15334+ CK_ULONG ulPasswordLen; 15335+ CK_BYTE_PTR pPassword; 15336+ CK_ULONG ulPublicDataLen; 15337+ CK_BYTE_PTR pPublicData; 15338+ CK_ULONG ulPAndGLen; 15339+ CK_ULONG ulQLen; 15340+ CK_ULONG ulRandomLen; 15341+ CK_BYTE_PTR pRandomA; 15342+ CK_BYTE_PTR pPrimeP; 15343+ CK_BYTE_PTR pBaseG; 15344+ CK_BYTE_PTR pSubprimeQ; 15345+} CK_SKIPJACK_PRIVATE_WRAP_PARAMS; 15346+ 15347+typedef CK_SKIPJACK_PRIVATE_WRAP_PARAMS CK_PTR \ 15348+ CK_SKIPJACK_PRIVATE_WRAP_PTR; 15349+ 15350+ 15351+/* CK_SKIPJACK_RELAYX_PARAMS provides the parameters to the 15352+ * CKM_SKIPJACK_RELAYX mechanism */ 15353+/* CK_SKIPJACK_RELAYX_PARAMS is new for v2.0 */ 15354+typedef struct CK_SKIPJACK_RELAYX_PARAMS { 15355+ CK_ULONG ulOldWrappedXLen; 15356+ CK_BYTE_PTR pOldWrappedX; 15357+ CK_ULONG ulOldPasswordLen; 15358+ CK_BYTE_PTR pOldPassword; 15359+ CK_ULONG ulOldPublicDataLen; 15360+ CK_BYTE_PTR pOldPublicData; 15361+ CK_ULONG ulOldRandomLen; 15362+ CK_BYTE_PTR pOldRandomA; 15363+ CK_ULONG ulNewPasswordLen; 15364+ CK_BYTE_PTR pNewPassword; 15365+ CK_ULONG ulNewPublicDataLen; 15366+ CK_BYTE_PTR pNewPublicData; 15367+ CK_ULONG ulNewRandomLen; 15368+ CK_BYTE_PTR pNewRandomA; 15369+} CK_SKIPJACK_RELAYX_PARAMS; 15370+ 15371+typedef CK_SKIPJACK_RELAYX_PARAMS CK_PTR \ 15372+ CK_SKIPJACK_RELAYX_PARAMS_PTR; 15373+ 15374+ 15375+typedef struct CK_PBE_PARAMS { 15376+ CK_BYTE_PTR pInitVector; 15377+ CK_UTF8CHAR_PTR pPassword; 15378+ CK_ULONG ulPasswordLen; 15379+ CK_BYTE_PTR pSalt; 15380+ CK_ULONG ulSaltLen; 15381+ CK_ULONG ulIteration; 15382+} CK_PBE_PARAMS; 15383+ 15384+typedef CK_PBE_PARAMS CK_PTR CK_PBE_PARAMS_PTR; 15385+ 15386+ 15387+/* CK_KEY_WRAP_SET_OAEP_PARAMS provides the parameters to the 15388+ * CKM_KEY_WRAP_SET_OAEP mechanism */ 15389+/* CK_KEY_WRAP_SET_OAEP_PARAMS is new for v2.0 */ 15390+typedef struct CK_KEY_WRAP_SET_OAEP_PARAMS { 15391+ CK_BYTE bBC; /* block contents byte */ 15392+ CK_BYTE_PTR pX; /* extra data */ 15393+ CK_ULONG ulXLen; /* length of extra data in bytes */ 15394+} CK_KEY_WRAP_SET_OAEP_PARAMS; 15395+ 15396+typedef CK_KEY_WRAP_SET_OAEP_PARAMS CK_PTR \ 15397+ CK_KEY_WRAP_SET_OAEP_PARAMS_PTR; 15398+ 15399+ 15400+typedef struct CK_SSL3_RANDOM_DATA { 15401+ CK_BYTE_PTR pClientRandom; 15402+ CK_ULONG ulClientRandomLen; 15403+ CK_BYTE_PTR pServerRandom; 15404+ CK_ULONG ulServerRandomLen; 15405+} CK_SSL3_RANDOM_DATA; 15406+ 15407+ 15408+typedef struct CK_SSL3_MASTER_KEY_DERIVE_PARAMS { 15409+ CK_SSL3_RANDOM_DATA RandomInfo; 15410+ CK_VERSION_PTR pVersion; 15411+} CK_SSL3_MASTER_KEY_DERIVE_PARAMS; 15412+ 15413+typedef struct CK_SSL3_MASTER_KEY_DERIVE_PARAMS CK_PTR \ 15414+ CK_SSL3_MASTER_KEY_DERIVE_PARAMS_PTR; 15415+ 15416+ 15417+typedef struct CK_SSL3_KEY_MAT_OUT { 15418+ CK_OBJECT_HANDLE hClientMacSecret; 15419+ CK_OBJECT_HANDLE hServerMacSecret; 15420+ CK_OBJECT_HANDLE hClientKey; 15421+ CK_OBJECT_HANDLE hServerKey; 15422+ CK_BYTE_PTR pIVClient; 15423+ CK_BYTE_PTR pIVServer; 15424+} CK_SSL3_KEY_MAT_OUT; 15425+ 15426+typedef CK_SSL3_KEY_MAT_OUT CK_PTR CK_SSL3_KEY_MAT_OUT_PTR; 15427+ 15428+ 15429+typedef struct CK_SSL3_KEY_MAT_PARAMS { 15430+ CK_ULONG ulMacSizeInBits; 15431+ CK_ULONG ulKeySizeInBits; 15432+ CK_ULONG ulIVSizeInBits; 15433+ CK_BBOOL bIsExport; 15434+ CK_SSL3_RANDOM_DATA RandomInfo; 15435+ CK_SSL3_KEY_MAT_OUT_PTR pReturnedKeyMaterial; 15436+} CK_SSL3_KEY_MAT_PARAMS; 15437+ 15438+typedef CK_SSL3_KEY_MAT_PARAMS CK_PTR CK_SSL3_KEY_MAT_PARAMS_PTR; 15439+ 15440+/* CK_TLS_PRF_PARAMS is new for version 2.20 */ 15441+typedef struct CK_TLS_PRF_PARAMS { 15442+ CK_BYTE_PTR pSeed; 15443+ CK_ULONG ulSeedLen; 15444+ CK_BYTE_PTR pLabel; 15445+ CK_ULONG ulLabelLen; 15446+ CK_BYTE_PTR pOutput; 15447+ CK_ULONG_PTR pulOutputLen; 15448+} CK_TLS_PRF_PARAMS; 15449+ 15450+typedef CK_TLS_PRF_PARAMS CK_PTR CK_TLS_PRF_PARAMS_PTR; 15451+ 15452+/* WTLS is new for version 2.20 */ 15453+typedef struct CK_WTLS_RANDOM_DATA { 15454+ CK_BYTE_PTR pClientRandom; 15455+ CK_ULONG ulClientRandomLen; 15456+ CK_BYTE_PTR pServerRandom; 15457+ CK_ULONG ulServerRandomLen; 15458+} CK_WTLS_RANDOM_DATA; 15459+ 15460+typedef CK_WTLS_RANDOM_DATA CK_PTR CK_WTLS_RANDOM_DATA_PTR; 15461+ 15462+typedef struct CK_WTLS_MASTER_KEY_DERIVE_PARAMS { 15463+ CK_MECHANISM_TYPE DigestMechanism; 15464+ CK_WTLS_RANDOM_DATA RandomInfo; 15465+ CK_BYTE_PTR pVersion; 15466+} CK_WTLS_MASTER_KEY_DERIVE_PARAMS; 15467+ 15468+typedef CK_WTLS_MASTER_KEY_DERIVE_PARAMS CK_PTR \ 15469+ CK_WTLS_MASTER_KEY_DERIVE_PARAMS_PTR; 15470+ 15471+typedef struct CK_WTLS_PRF_PARAMS { 15472+ CK_MECHANISM_TYPE DigestMechanism; 15473+ CK_BYTE_PTR pSeed; 15474+ CK_ULONG ulSeedLen; 15475+ CK_BYTE_PTR pLabel; 15476+ CK_ULONG ulLabelLen; 15477+ CK_BYTE_PTR pOutput; 15478+ CK_ULONG_PTR pulOutputLen; 15479+} CK_WTLS_PRF_PARAMS; 15480+ 15481+typedef CK_WTLS_PRF_PARAMS CK_PTR CK_WTLS_PRF_PARAMS_PTR; 15482+ 15483+typedef struct CK_WTLS_KEY_MAT_OUT { 15484+ CK_OBJECT_HANDLE hMacSecret; 15485+ CK_OBJECT_HANDLE hKey; 15486+ CK_BYTE_PTR pIV; 15487+} CK_WTLS_KEY_MAT_OUT; 15488+ 15489+typedef CK_WTLS_KEY_MAT_OUT CK_PTR CK_WTLS_KEY_MAT_OUT_PTR; 15490+ 15491+typedef struct CK_WTLS_KEY_MAT_PARAMS { 15492+ CK_MECHANISM_TYPE DigestMechanism; 15493+ CK_ULONG ulMacSizeInBits; 15494+ CK_ULONG ulKeySizeInBits; 15495+ CK_ULONG ulIVSizeInBits; 15496+ CK_ULONG ulSequenceNumber; 15497+ CK_BBOOL bIsExport; 15498+ CK_WTLS_RANDOM_DATA RandomInfo; 15499+ CK_WTLS_KEY_MAT_OUT_PTR pReturnedKeyMaterial; 15500+} CK_WTLS_KEY_MAT_PARAMS; 15501+ 15502+typedef CK_WTLS_KEY_MAT_PARAMS CK_PTR CK_WTLS_KEY_MAT_PARAMS_PTR; 15503+ 15504+/* CMS is new for version 2.20 */ 15505+typedef struct CK_CMS_SIG_PARAMS { 15506+ CK_OBJECT_HANDLE certificateHandle; 15507+ CK_MECHANISM_PTR pSigningMechanism; 15508+ CK_MECHANISM_PTR pDigestMechanism; 15509+ CK_UTF8CHAR_PTR pContentType; 15510+ CK_BYTE_PTR pRequestedAttributes; 15511+ CK_ULONG ulRequestedAttributesLen; 15512+ CK_BYTE_PTR pRequiredAttributes; 15513+ CK_ULONG ulRequiredAttributesLen; 15514+} CK_CMS_SIG_PARAMS; 15515+ 15516+typedef CK_CMS_SIG_PARAMS CK_PTR CK_CMS_SIG_PARAMS_PTR; 15517+ 15518+typedef struct CK_KEY_DERIVATION_STRING_DATA { 15519+ CK_BYTE_PTR pData; 15520+ CK_ULONG ulLen; 15521+} CK_KEY_DERIVATION_STRING_DATA; 15522+ 15523+typedef CK_KEY_DERIVATION_STRING_DATA CK_PTR \ 15524+ CK_KEY_DERIVATION_STRING_DATA_PTR; 15525+ 15526+ 15527+/* The CK_EXTRACT_PARAMS is used for the 15528+ * CKM_EXTRACT_KEY_FROM_KEY mechanism. It specifies which bit 15529+ * of the base key should be used as the first bit of the 15530+ * derived key */ 15531+/* CK_EXTRACT_PARAMS is new for v2.0 */ 15532+typedef CK_ULONG CK_EXTRACT_PARAMS; 15533+ 15534+typedef CK_EXTRACT_PARAMS CK_PTR CK_EXTRACT_PARAMS_PTR; 15535+ 15536+/* CK_PKCS5_PBKD2_PSEUDO_RANDOM_FUNCTION_TYPE is new for v2.10. 15537+ * CK_PKCS5_PBKD2_PSEUDO_RANDOM_FUNCTION_TYPE is used to 15538+ * indicate the Pseudo-Random Function (PRF) used to generate 15539+ * key bits using PKCS #5 PBKDF2. */ 15540+typedef CK_ULONG CK_PKCS5_PBKD2_PSEUDO_RANDOM_FUNCTION_TYPE; 15541+ 15542+typedef CK_PKCS5_PBKD2_PSEUDO_RANDOM_FUNCTION_TYPE CK_PTR CK_PKCS5_PBKD2_PSEUDO_RANDOM_FUNCTION_TYPE_PTR; 15543+ 15544+/* The following PRFs are defined in PKCS #5 v2.0. */ 15545+#define CKP_PKCS5_PBKD2_HMAC_SHA1 0x00000001 15546+ 15547+ 15548+/* CK_PKCS5_PBKDF2_SALT_SOURCE_TYPE is new for v2.10. 15549+ * CK_PKCS5_PBKDF2_SALT_SOURCE_TYPE is used to indicate the 15550+ * source of the salt value when deriving a key using PKCS #5 15551+ * PBKDF2. */ 15552+typedef CK_ULONG CK_PKCS5_PBKDF2_SALT_SOURCE_TYPE; 15553+ 15554+typedef CK_PKCS5_PBKDF2_SALT_SOURCE_TYPE CK_PTR CK_PKCS5_PBKDF2_SALT_SOURCE_TYPE_PTR; 15555+ 15556+/* The following salt value sources are defined in PKCS #5 v2.0. */ 15557+#define CKZ_SALT_SPECIFIED 0x00000001 15558+ 15559+/* CK_PKCS5_PBKD2_PARAMS is new for v2.10. 15560+ * CK_PKCS5_PBKD2_PARAMS is a structure that provides the 15561+ * parameters to the CKM_PKCS5_PBKD2 mechanism. */ 15562+typedef struct CK_PKCS5_PBKD2_PARAMS { 15563+ CK_PKCS5_PBKDF2_SALT_SOURCE_TYPE saltSource; 15564+ CK_VOID_PTR pSaltSourceData; 15565+ CK_ULONG ulSaltSourceDataLen; 15566+ CK_ULONG iterations; 15567+ CK_PKCS5_PBKD2_PSEUDO_RANDOM_FUNCTION_TYPE prf; 15568+ CK_VOID_PTR pPrfData; 15569+ CK_ULONG ulPrfDataLen; 15570+ CK_UTF8CHAR_PTR pPassword; 15571+ CK_ULONG_PTR ulPasswordLen; 15572+} CK_PKCS5_PBKD2_PARAMS; 15573+ 15574+typedef CK_PKCS5_PBKD2_PARAMS CK_PTR CK_PKCS5_PBKD2_PARAMS_PTR; 15575+ 15576+/* All CK_OTP structs are new for PKCS #11 v2.20 amendment 3 */ 15577+ 15578+typedef CK_ULONG CK_OTP_PARAM_TYPE; 15579+typedef CK_OTP_PARAM_TYPE CK_PARAM_TYPE; /* B/w compatibility */ 15580+ 15581+typedef struct CK_OTP_PARAM { 15582+ CK_OTP_PARAM_TYPE type; 15583+ CK_VOID_PTR pValue; 15584+ CK_ULONG ulValueLen; 15585+} CK_OTP_PARAM; 15586+ 15587+typedef CK_OTP_PARAM CK_PTR CK_OTP_PARAM_PTR; 15588+ 15589+typedef struct CK_OTP_PARAMS { 15590+ CK_OTP_PARAM_PTR pParams; 15591+ CK_ULONG ulCount; 15592+} CK_OTP_PARAMS; 15593+ 15594+typedef CK_OTP_PARAMS CK_PTR CK_OTP_PARAMS_PTR; 15595+ 15596+typedef struct CK_OTP_SIGNATURE_INFO { 15597+ CK_OTP_PARAM_PTR pParams; 15598+ CK_ULONG ulCount; 15599+} CK_OTP_SIGNATURE_INFO; 15600+ 15601+typedef CK_OTP_SIGNATURE_INFO CK_PTR CK_OTP_SIGNATURE_INFO_PTR; 15602+ 15603+/* The following OTP-related defines are new for PKCS #11 v2.20 amendment 1 */ 15604+#define CK_OTP_VALUE 0 15605+#define CK_OTP_PIN 1 15606+#define CK_OTP_CHALLENGE 2 15607+#define CK_OTP_TIME 3 15608+#define CK_OTP_COUNTER 4 15609+#define CK_OTP_FLAGS 5 15610+#define CK_OTP_OUTPUT_LENGTH 6 15611+#define CK_OTP_OUTPUT_FORMAT 7 15612+ 15613+/* The following OTP-related defines are new for PKCS #11 v2.20 amendment 1 */ 15614+#define CKF_NEXT_OTP 0x00000001 15615+#define CKF_EXCLUDE_TIME 0x00000002 15616+#define CKF_EXCLUDE_COUNTER 0x00000004 15617+#define CKF_EXCLUDE_CHALLENGE 0x00000008 15618+#define CKF_EXCLUDE_PIN 0x00000010 15619+#define CKF_USER_FRIENDLY_OTP 0x00000020 15620+ 15621+/* CK_KIP_PARAMS is new for PKCS #11 v2.20 amendment 2 */ 15622+typedef struct CK_KIP_PARAMS { 15623+ CK_MECHANISM_PTR pMechanism; 15624+ CK_OBJECT_HANDLE hKey; 15625+ CK_BYTE_PTR pSeed; 15626+ CK_ULONG ulSeedLen; 15627+} CK_KIP_PARAMS; 15628+ 15629+typedef CK_KIP_PARAMS CK_PTR CK_KIP_PARAMS_PTR; 15630+ 15631+/* CK_AES_CTR_PARAMS is new for PKCS #11 v2.20 amendment 3 */ 15632+typedef struct CK_AES_CTR_PARAMS { 15633+ CK_ULONG ulCounterBits; 15634+ CK_BYTE cb[16]; 15635+} CK_AES_CTR_PARAMS; 15636+ 15637+typedef CK_AES_CTR_PARAMS CK_PTR CK_AES_CTR_PARAMS_PTR; 15638+ 15639+/* CK_CAMELLIA_CTR_PARAMS is new for PKCS #11 v2.20 amendment 3 */ 15640+typedef struct CK_CAMELLIA_CTR_PARAMS { 15641+ CK_ULONG ulCounterBits; 15642+ CK_BYTE cb[16]; 15643+} CK_CAMELLIA_CTR_PARAMS; 15644+ 15645+typedef CK_CAMELLIA_CTR_PARAMS CK_PTR CK_CAMELLIA_CTR_PARAMS_PTR; 15646+ 15647+/* CK_CAMELLIA_CBC_ENCRYPT_DATA_PARAMS is new for PKCS #11 v2.20 amendment 3 */ 15648+typedef struct CK_CAMELLIA_CBC_ENCRYPT_DATA_PARAMS { 15649+ CK_BYTE iv[16]; 15650+ CK_BYTE_PTR pData; 15651+ CK_ULONG length; 15652+} CK_CAMELLIA_CBC_ENCRYPT_DATA_PARAMS; 15653+ 15654+typedef CK_CAMELLIA_CBC_ENCRYPT_DATA_PARAMS CK_PTR CK_CAMELLIA_CBC_ENCRYPT_DATA_PARAMS_PTR; 15655+ 15656+/* CK_ARIA_CBC_ENCRYPT_DATA_PARAMS is new for PKCS #11 v2.20 amendment 3 */ 15657+typedef struct CK_ARIA_CBC_ENCRYPT_DATA_PARAMS { 15658+ CK_BYTE iv[16]; 15659+ CK_BYTE_PTR pData; 15660+ CK_ULONG length; 15661+} CK_ARIA_CBC_ENCRYPT_DATA_PARAMS; 15662+ 15663+typedef CK_ARIA_CBC_ENCRYPT_DATA_PARAMS CK_PTR CK_ARIA_CBC_ENCRYPT_DATA_PARAMS_PTR; 15664+ 15665+#endif 15666Index: openssl/util/libeay.num 15667diff -u openssl/util/libeay.num:1.7.6.1 openssl/util/libeay.num:1.7 15668--- openssl/util/libeay.num:1.7.6.1 Sun Jan 15 15:45:40 2012 15669+++ openssl/util/libeay.num Mon Jun 13 14:25:25 2011 15670@@ -3728,3 +3728,5 @@ 15671 pqueue_size 4114 EXIST::FUNCTION: 15672 OPENSSL_uni2asc 4115 EXIST:NETWARE:FUNCTION: 15673 OPENSSL_asc2uni 4116 EXIST:NETWARE:FUNCTION: 15674+ENGINE_load_pk11ca 4117 EXIST::FUNCTION:HW_PKCS11CA,ENGINE 15675+ENGINE_load_pk11so 4117 EXIST::FUNCTION:HW_PKCS11SO,ENGINE 15676Index: openssl/util/mk1mf.pl 15677diff -u openssl/util/mk1mf.pl:1.8.6.1 openssl/util/mk1mf.pl:1.8 15678--- openssl/util/mk1mf.pl:1.8.6.1 Sun Jan 15 15:45:40 2012 15679+++ openssl/util/mk1mf.pl Mon Jun 13 14:25:25 2011 15680@@ -87,6 +87,8 @@ 15681 no-ecdh - No ECDH 15682 no-engine - No engine 15683 no-hw - No hw 15684+ no-hw-pkcs11ca - No hw PKCS#11 CA flavor 15685+ no-hw-pkcs11so - No hw PKCS#11 SO flavor 15686 nasm - Use NASM for x86 asm 15687 nw-nasm - Use NASM x86 asm for NetWare 15688 nw-mwasm - Use Metrowerks x86 asm for NetWare 15689@@ -242,6 +244,8 @@ 15690 $cflags.=" -DOPENSSL_NO_ECDH" if $no_ecdh; 15691 $cflags.=" -DOPENSSL_NO_ENGINE" if $no_engine; 15692 $cflags.=" -DOPENSSL_NO_HW" if $no_hw; 15693+$cflags.=" -DOPENSSL_NO_HW_PKCS11CA" if $no_hw_pkcs11ca; 15694+$cflags.=" -DOPENSSL_NO_HW_PKCS11SO" if $no_hw_pkcs11so; 15695 $cflags.=" -DOPENSSL_FIPS" if $fips; 15696 $cflags.= " -DZLIB" if $zlib_opt; 15697 $cflags.= " -DZLIB_SHARED" if $zlib_opt == 2; 15698@@ -316,6 +320,9 @@ 15699 $dir=$val; 15700 } 15701 15702+ if ($key eq "PK11_LIB_LOCATION") 15703+ { $cflags .= " -D$key=\\\"$val\\\"" if $val ne "";} 15704+ 15705 if ($key eq "KRB5_INCLUDES") 15706 { $cflags .= " $val";} 15707 15708@@ -1301,6 +1308,8 @@ 15709 "no-ecdh" => \$no_ecdh, 15710 "no-engine" => \$no_engine, 15711 "no-hw" => \$no_hw, 15712+ "no-hw-pkcs11ca" => \$no_hw_pkcs11ca, 15713+ "no-hw-pkcs11so" => \$no_hw_pkcs11so, 15714 "just-ssl" => 15715 [\$no_rc2, \$no_idea, \$no_des, \$no_bf, \$no_cast, 15716 \$no_md2, \$no_sha, \$no_mdc2, \$no_dsa, \$no_dh, 15717Index: openssl/util/mkdef.pl 15718diff -u openssl/util/mkdef.pl:1.6.6.1 openssl/util/mkdef.pl:1.6 15719--- openssl/util/mkdef.pl:1.6.6.1 Sun Jan 15 15:45:40 2012 15720+++ openssl/util/mkdef.pl Mon Jun 13 14:25:25 2011 15721@@ -93,7 +93,7 @@ 15722 # External "algorithms" 15723 "FP_API", "STDIO", "SOCK", "KRB5", "DGRAM", 15724 # Engines 15725- "STATIC_ENGINE", "ENGINE", "HW", "GMP", 15726+ "STATIC_ENGINE", "ENGINE", "HW", "GMP", "HW_PKCS11CA", "HW_PKCS11SO", 15727 # RFC3779 support 15728 "RFC3779", 15729 # TLS extension support 15730@@ -122,6 +122,7 @@ 15731 my $no_md2; my $no_md4; my $no_md5; my $no_sha; my $no_ripemd; my $no_mdc2; 15732 my $no_rsa; my $no_dsa; my $no_dh; my $no_hmac=0; my $no_aes; my $no_krb5; 15733 my $no_ec; my $no_ecdsa; my $no_ecdh; my $no_engine; my $no_hw; my $no_camellia; 15734+my $no_pkcs11ca; my $no_pkcs11so; 15735 my $no_seed; 15736 my $no_fp_api; my $no_static_engine; my $no_gmp; my $no_deprecated; 15737 my $no_rfc3779; my $no_tlsext; my $no_cms; my $no_capieng; my $no_jpake; 15738@@ -214,6 +215,8 @@ 15739 elsif (/^no-cms$/) { $no_cms=1; } 15740 elsif (/^no-capieng$/) { $no_capieng=1; } 15741 elsif (/^no-jpake$/) { $no_jpake=1; } 15742+ elsif (/^no-hw-pkcs11ca$/) { $no_pkcs11ca=1; } 15743+ elsif (/^no-hw-pkcs11so$/) { $no_pkcs11so=1; } 15744 } 15745 15746 15747@@ -1155,6 +1158,8 @@ 15748 if ($keyword eq "KRB5" && $no_krb5) { return 0; } 15749 if ($keyword eq "ENGINE" && $no_engine) { return 0; } 15750 if ($keyword eq "HW" && $no_hw) { return 0; } 15751+ if ($keyword eq "HW_PKCS11CA" && $no_pkcs11ca) { return 0; } 15752+ if ($keyword eq "HW_PKCS11SO" && $no_pkcs11so) { return 0; } 15753 if ($keyword eq "FP_API" && $no_fp_api) { return 0; } 15754 if ($keyword eq "STATIC_ENGINE" && $no_static_engine) { return 0; } 15755 if ($keyword eq "GMP" && $no_gmp) { return 0; } 15756Index: openssl/util/pl/VC-32.pl 15757diff -u openssl/util/pl/VC-32.pl:1.6.6.1 openssl/util/pl/VC-32.pl:1.6 15758--- openssl/util/pl/VC-32.pl:1.6.6.1 Sun Jan 15 15:45:41 2012 15759+++ openssl/util/pl/VC-32.pl Mon Jun 13 14:25:26 2011 15760@@ -52,7 +52,7 @@ 15761 my $f = $shlib || $fips ?' /MD':' /MT'; 15762 $lib_cflag='/Zl' if (!$shlib); # remove /DEFAULTLIBs from static lib 15763 $opt_cflags=$f.' /Ox'; 15764- $dbg_cflags=$f.'d /Od -DDEBUG -D_DEBUG'; 15765+ $dbg_cflags=$f.'d /Od /Zi -DDEBUG -D_DEBUG'; 15766 $lflags="/nologo /subsystem:console /opt:ref"; 15767 } 15768 elsif ($FLAVOR =~ /CE/) 15769