components/coolkey/patches/06-slot.cpp.patch
author Ivo Raisr <ivo.raisr@oracle.com>
Thu, 29 Sep 2016 01:54:21 -0700
changeset 6998 ecb236c14ed7
parent 6401 8e624b116c1d
permissions -rw-r--r--
24654747 coolkey fails to build with studio 12.5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6401
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
     1
Upstream fixes already included in the latest community updates to coolkey v1.1.0
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
     2
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
     3
Adds support and fixes for newer versions of CAC and PIV cards.
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
     4
Addresses issues seen with pcscd restart.
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
     5
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
     6
--- ORIGINAL/./src/coolkey/slot.cpp	2016-06-24 16:07:20.111616788 -0400
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
     7
+++ ././src/coolkey/slot.cpp	2016-06-27 21:05:04.901200633 -0400
6998
ecb236c14ed7 24654747 coolkey fails to build with studio 12.5
Ivo Raisr <ivo.raisr@oracle.com>
parents: 6401
diff changeset
     8
@@ -25,7 +25,6 @@
ecb236c14ed7 24654747 coolkey fails to build with studio 12.5
Ivo Raisr <ivo.raisr@oracle.com>
parents: 6401
diff changeset
     9
 #include "PKCS11Exception.h"
ecb236c14ed7 24654747 coolkey fails to build with studio 12.5
Ivo Raisr <ivo.raisr@oracle.com>
parents: 6401
diff changeset
    10
 #include <winscard.h>
ecb236c14ed7 24654747 coolkey fails to build with studio 12.5
Ivo Raisr <ivo.raisr@oracle.com>
parents: 6401
diff changeset
    11
 #include "slot.h"
ecb236c14ed7 24654747 coolkey fails to build with studio 12.5
Ivo Raisr <ivo.raisr@oracle.com>
parents: 6401
diff changeset
    12
-#include <memory.h>
ecb236c14ed7 24654747 coolkey fails to build with studio 12.5
Ivo Raisr <ivo.raisr@oracle.com>
parents: 6401
diff changeset
    13
 #include "zlib.h"
ecb236c14ed7 24654747 coolkey fails to build with studio 12.5
Ivo Raisr <ivo.raisr@oracle.com>
parents: 6401
diff changeset
    14
 #include "params.h"
ecb236c14ed7 24654747 coolkey fails to build with studio 12.5
Ivo Raisr <ivo.raisr@oracle.com>
parents: 6401
diff changeset
    15
ecb236c14ed7 24654747 coolkey fails to build with studio 12.5
Ivo Raisr <ivo.raisr@oracle.com>
parents: 6401
diff changeset
    16
@@ -33,9 +32,7 @@
ecb236c14ed7 24654747 coolkey fails to build with studio 12.5
Ivo Raisr <ivo.raisr@oracle.com>
parents: 6401
diff changeset
    17
ecb236c14ed7 24654747 coolkey fails to build with studio 12.5
Ivo Raisr <ivo.raisr@oracle.com>
parents: 6401
diff changeset
    18
 #define MIN(x, y) ((x) < (y) ? (x) : (y))
ecb236c14ed7 24654747 coolkey fails to build with studio 12.5
Ivo Raisr <ivo.raisr@oracle.com>
parents: 6401
diff changeset
    19
ecb236c14ed7 24654747 coolkey fails to build with studio 12.5
Ivo Raisr <ivo.raisr@oracle.com>
parents: 6401
diff changeset
    20
-using std::auto_ptr;
ecb236c14ed7 24654747 coolkey fails to build with studio 12.5
Ivo Raisr <ivo.raisr@oracle.com>
parents: 6401
diff changeset
    21
ecb236c14ed7 24654747 coolkey fails to build with studio 12.5
Ivo Raisr <ivo.raisr@oracle.com>
parents: 6401
diff changeset
    22
-
ecb236c14ed7 24654747 coolkey fails to build with studio 12.5
Ivo Raisr <ivo.raisr@oracle.com>
parents: 6401
diff changeset
    23
 #ifdef DEBUG
ecb236c14ed7 24654747 coolkey fails to build with studio 12.5
Ivo Raisr <ivo.raisr@oracle.com>
parents: 6401
diff changeset
    24
 #define PRINTF(args) printf args
ecb236c14ed7 24654747 coolkey fails to build with studio 12.5
Ivo Raisr <ivo.raisr@oracle.com>
parents: 6401
diff changeset
    25
 #else
6401
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    26
@@ -56,6 +56,34 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    27
 {  0x3B, 0x6F, 0x00, 0xFF, 0x52, 0x53, 0x41, 0x53, 0x65, 0x63, 0x75, 0x72,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    28
    0x49, 0x44, 0x28, 0x52, 0x29, 0x31, 0x30 };
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    29
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    30
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    31
+/* ECC curve information
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    32
+ *    Provide information for the limited set of curves supported by our smart card(s).
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    33
+ *    
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    34
+ */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    35
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    36
+typedef struct curveBytes2Name {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    37
+    const CKYByte * bytes;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    38
+    const char *curveName;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    39
+    unsigned int length;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    40
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    41
+} CurveBytes2Name;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    42
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    43
+/* First byte is length of oid byte array. */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    44
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    45
+const CKYByte nistp256[] = { 0x8, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07};
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    46
+const CKYByte nistp384[] = { 0x5, 0x2b, 0x81, 0x04, 0x00, 0x22 };
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    47
+const CKYByte nistp521[] = { 0x05, 0x2b, 0x81, 0x04, 0x00, 0x23 };
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    48
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    49
+const int numECCurves = 3;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    50
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    51
+static CurveBytes2Name curveBytesNamePair[] =
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    52
+{
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    53
+ { nistp256, "nistp256", 256 },
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    54
+ { nistp384, "nistp384", 384 },
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    55
+ { nistp521, "nistp521", 521 }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    56
+};
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    57
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    58
 SlotList::SlotList(Log *log_) : log(log_)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    59
 {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    60
     // initialize things to NULL so we can recover from an exception
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    61
@@ -138,7 +166,11 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    62
 	    throw PKCS11Exception(CKR_HOST_MEMORY);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    63
 	memset(newSlots, 0, numReaders*sizeof(Slot*));
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    64
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    65
-        memcpy(newSlots, slots, sizeof(slots[0]) * numSlots);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    66
+        /* keep coverity happy, even though slot == NULL implies that
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    67
+	 * numSlots == 0 */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    68
+	if (slots) { 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    69
+            memcpy(newSlots, slots, sizeof(slots[0]) * numSlots);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    70
+	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    71
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    72
 	for (unsigned int i=numSlots; i < numReaders; i++) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    73
 	    newSlots[i] = new
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    74
@@ -205,6 +237,29 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    75
     return FALSE;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    76
 }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    77
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    78
+bool
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    79
+SlotList::readerNameExistsInList(const char *readerName,CKYReaderNameList *readerNameList)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    80
+{
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    81
+    if( !readerName || !readerNameList) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    82
+        return FALSE;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    83
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    84
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    85
+    int i = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    86
+    int readerNameCnt = CKYReaderNameList_GetCount(*readerNameList);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    87
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    88
+    const char *curReaderName = NULL;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    89
+    for(i=0; i < readerNameCnt; i++) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    90
+        curReaderName = CKYReaderNameList_GetValue(*readerNameList,i);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    91
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    92
+        if(!strcmp(curReaderName,readerName)) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    93
+            return TRUE;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    94
+        }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    95
+        
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    96
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    97
+    
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    98
+    return FALSE;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    99
+}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   100
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   101
 /*
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   102
  * you need to hold the ReaderList Lock before you can update the ReaderList
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   103
  */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   104
@@ -216,32 +271,19 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   105
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   106
     CKYStatus status = CKYCardContext_ListReaders(context, &readerNames);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   107
     if ( status != CKYSUCCESS ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   108
-	throw PKCS11Exception(CKR_GENERAL_ERROR,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   109
+	/* if the service is stopped, treat it as if we have no readers */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   110
+ 	if ((CKYCardContext_GetLastError(context) != SCARD_E_NO_SERVICE) && 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   111
+	    (CKYCardContext_GetLastError(context) != SCARD_E_SERVICE_STOPPED)) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   112
+	    throw PKCS11Exception(CKR_GENERAL_ERROR,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   113
                 "Failed to list readers: 0x%x\n", 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   114
 				CKYCardContext_GetLastError(context));
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   115
+	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   116
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   117
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   118
-    if (!readerStates) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   119
+    if (readerStates == NULL && readerNames != NULL) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   120
 	/* fresh Reader State list, just create it */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   121
 	readerStates = CKYReader_CreateArray(readerNames, (CKYSize *)&numReaders);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   122
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   123
-	/* if we have no readers, make sure we have at least one to keep things
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   124
-	 * happy */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   125
-	if (readerStates == NULL &&
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   126
-			 CKYReaderNameList_GetCount(readerNames) == 0) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   127
-	    readerStates = (SCARD_READERSTATE *)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   128
-				malloc(sizeof(SCARD_READERSTATE));
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   129
-	    if (readerStates) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   130
-		CKYReader_Init(readerStates);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   131
-		status = CKYReader_SetReaderName(readerStates, "E-Gate 0 0");
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   132
-		if (status != CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   133
- 		    CKYReader_DestroyArray(readerStates, 1);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   134
-		    readerStates = NULL;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   135
-		} else {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   136
-		    numReaders = 1;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   137
-		}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   138
-	    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   139
-	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   140
 	CKYReaderNameList_Destroy(readerNames);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   141
 	        
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   142
 	if (readerStates == NULL) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   143
@@ -251,6 +293,16 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   144
 	return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   145
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   146
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   147
+    if (readerStates == NULL) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   148
+	/* if we didn't have any readers before and we did get new names, 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   149
+	 * that is handled above. If we didn't have any readers before, and
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   150
+	 * we didn't get any names, there is nothing to update. blow out now.
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   151
+	 * This more efficient and makes coverity happy (since coverity doesn't
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   152
+	 * know numReaders and readerStates are linked). */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   153
+	return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   154
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   155
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   156
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   157
     /* it would be tempting at this point just to see if we have more readers
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   158
      * then specified previously. The problem with this is it is possible that
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   159
      * some readers have been deleted, so the only way to tell if we have
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   160
@@ -258,6 +310,33 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   161
      * don't recognize.
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   162
      */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   163
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   164
+    /* Iterate through all the readers to see if we need to make unavailable any
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   165
+     * freshly removed readers. Also, see if any previously removed
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   166
+     * readers have come back from the dead and don't need to be ignored.
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   167
+     */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   168
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   169
+    const char *curReaderName = NULL;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   170
+    unsigned long knownState = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   171
+    for(unsigned int ri = 0 ; ri < numReaders; ri ++)  {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   172
+        knownState = CKYReader_GetKnownState(&readerStates[ri]);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   173
+ 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   174
+        curReaderName =  CKYReader_GetReaderName(&readerStates[ri]); 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   175
+        if(readerNames && readerNameExistsInList(curReaderName,&readerNames)) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   176
+            CKYReader_SetKnownState(&readerStates[ri], 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   177
+		 knownState & ~SCARD_STATE_IGNORE); 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   178
+        } else {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   179
+	    if (!(knownState & SCARD_STATE_UNAVAILABLE))
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   180
+		CKYReader_SetKnownState(&readerStates[ri], 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   181
+		 knownState | SCARD_STATE_UNAVAILABLE | SCARD_STATE_CHANGED);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   182
+	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   183
+    } 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   184
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   185
+    if (readerNames == NULL) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   186
+        /* OK we've marked everything unavailable, we clearly
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   187
+	 * aren't adding any readers, so we can blow out here */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   188
+	return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   189
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   190
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   191
     const char *newReadersData[MAX_READER_DELTA];
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   192
     const char **newReaders = &newReadersData[0];
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   193
     unsigned int newReaderCount = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   194
@@ -330,7 +409,9 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   195
     : log(log_), readerName(NULL), personName(NULL), manufacturer(NULL),
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   196
 	slotInfoFound(false), context(context_), conn(NULL), state(UNKNOWN), 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   197
 	isVersion1Key(false), needLogin(false), fullTokenName(false), 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   198
-	mCoolkey(false),
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   199
+	mCoolkey(false), mOldCAC(false),mCACLocalLogin(false),
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   200
+	pivContainer(-1), pivKey(-1), maxCacCerts(MAX_CERT_SLOTS), 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   201
+	algs(ALG_NONE), 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   202
 #ifdef USE_SHMEM
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   203
 	shmem(readerName_),
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   204
 #endif
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   205
@@ -370,6 +451,9 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   206
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   207
     CKYBuffer_InitEmpty(&cardATR);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   208
     CKYBuffer_InitEmpty(&mCUID);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   209
+    for (int i=0; i < MAX_CERT_SLOTS; i++) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   210
+	CKYBuffer_InitEmpty(&cardAID[i]);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   211
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   212
   } catch(PKCS11Exception &) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   213
 	if (conn) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   214
 	    CKYCardConnection_Destroy(conn);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   215
@@ -437,6 +521,9 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   216
     CKYBuffer_FreeData(&nonce);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   217
     CKYBuffer_FreeData(&cardATR);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   218
     CKYBuffer_FreeData(&mCUID);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   219
+    for (int i=0; i < MAX_CERT_SLOTS; i++) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   220
+	CKYBuffer_FreeData(&cardAID[i]);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   221
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   222
 }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   223
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   224
 template <class C>
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   225
@@ -527,10 +614,39 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   226
     return rv;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   227
 }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   228
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   229
+bool
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   230
+Slot::getPIVLoginType(void)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   231
+{
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   232
+    CKYStatus status;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   233
+    CKYISOStatus apduRC;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   234
+    CKYBuffer buffer;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   235
+    bool local = true;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   236
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   237
+    CKYBuffer_InitEmpty(&buffer);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   238
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   239
+    /* get the discovery object */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   240
+    status = PIVApplet_GetCertificate(conn, &buffer, 0x7e, &apduRC);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   241
+    if (status != CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   242
+	/* Discovery object optional, PIV defaults to local */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   243
+	goto done;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   244
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   245
+    /* techically we probably should parse out the TLVs, but the PIV
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   246
+     * specifies exactly what they should be, so we know exactly which
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   247
+     * byte to look at */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   248
+    if ((CKYBuffer_Size(&buffer) >= 20) && 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   249
+			(CKYBuffer_GetChar(&buffer,17) == 0x60)) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   250
+	/* This tells us we should use global login for this piv card */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   251
+	local = false;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   252
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   253
+done:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   254
+    CKYBuffer_FreeData(&buffer);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   255
+    return true;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   256
+}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   257
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   258
 void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   259
 Slot::connectToToken()
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   260
 {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   261
-    CKYStatus status;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   262
+    CKYStatus status = CKYSCARDERR;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   263
     OSTime time = OSTimeNow();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   264
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   265
     mCoolkey = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   266
@@ -539,13 +655,32 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   267
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   268
     // try to connect to the card
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   269
     if( ! CKYCardConnection_IsConnected(conn) ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   270
-        status = CKYCardConnection_Connect(conn, readerName);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   271
-        if( status != CKYSUCCESS ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   272
-            log->log("Unable to connect to token\n");
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   273
+        int i = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   274
+    //for cranky readers try again a few more times
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   275
+	status = CKYSCARDERR;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   276
+        while( i++ < 5 && status != CKYSUCCESS )
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   277
+        {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   278
+            status = CKYCardConnection_Connect(conn, readerName);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   279
+            if( status != CKYSUCCESS && 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   280
+                CKYCardConnection_GetLastError(conn) == SCARD_E_PROTO_MISMATCH ) 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   281
+            {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   282
+                log->log("Unable to connect to token status %d ConnGetGetLastError %x .\n",status,CKYCardConnection_GetLastError(conn));
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   283
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   284
+            }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   285
+            else
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   286
+            {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   287
+                break;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   288
+            }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   289
+            OSSleep(100000);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   290
+        }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   291
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   292
+        if( status != CKYSUCCESS)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   293
+        {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   294
             state = UNKNOWN;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   295
             return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   296
         }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   297
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   298
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   299
     log->log("time connect: Connect Time %d ms\n", OSTimeNow() - time);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   300
     if (!slotInfoFound) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   301
 	readSlotInfo();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   302
@@ -564,15 +699,10 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   303
         state = CARD_PRESENT;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   304
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   305
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   306
-    if ( CKYBuffer_DataIsEqual(&cardATR, ATR, sizeof (ATR)) || 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   307
-		CKYBuffer_DataIsEqual(&cardATR, ATR1, sizeof(ATR1)) ||
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   308
-		CKYBuffer_DataIsEqual(&cardATR, ATR2, sizeof(ATR2)) ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   309
-
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   310
-        if (Params::hasParam("noAppletOK"))
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   311
-        {      
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   312
-            state |=  APPLET_SELECTABLE;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   313
-	    mCoolkey = 1;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   314
-        }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   315
+    if (Params::hasParam("noAppletOK"))
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   316
+    {      
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   317
+        state |=  APPLET_SELECTABLE;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   318
+	mCoolkey = 1;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   319
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   320
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   321
     /* support CAC card. identify the card based on applets, not the ATRS */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   322
@@ -613,17 +743,30 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   323
     // see if the applet is selectable
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   324
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   325
     log->log("time connnect: Begin transaction %d ms\n", OSTimeNow() - time);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   326
+    status = PIVApplet_Select(conn, NULL);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   327
+    if (status == CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   328
+	 /* CARD is a PIV card */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   329
+	 state |= PIV_CARD | APPLET_SELECTABLE | APPLET_PERSONALIZED;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   330
+	 isVersion1Key = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   331
+	 needLogin = 1;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   332
+	 maxCacCerts = MAX_CERT_SLOTS;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   333
+         mCoolkey = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   334
+	 mOldCAC = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   335
+	 mCACLocalLogin = getPIVLoginType();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   336
+	return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   337
+    } 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   338
     status = CKYApplet_SelectCoolKeyManager(conn, NULL);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   339
     if (status != CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   340
         log->log("CoolKey Select failed 0x%x\n", status);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   341
-	status = CACApplet_SelectPKI(conn, 0, NULL);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   342
+	status = getCACAid();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   343
 	if (status != CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   344
-            log->log("CAC Select failed 0x%x\n", status);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   345
+	    log->log("CAC Select failed 0x%x\n", status);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   346
 	    if (status == CKYSCARDERR) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   347
-		log->log("CAC Card Failure 0x%x\n", 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   348
-			CKYCardConnection_GetLastError(conn));
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   349
-		disconnect();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   350
+		    log->log("Card Failure 0x%x\n",
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   351
+				CKYCardConnection_GetLastError(conn));
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   352
+		    disconnect();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   353
 	    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   354
+	    /* CARD is unknown */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   355
 	    return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   356
 	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   357
 	state |= CAC_CARD | APPLET_SELECTABLE | APPLET_PERSONALIZED;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   358
@@ -633,10 +776,11 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   359
          * unfriendly */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   360
 	isVersion1Key = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   361
 	needLogin = 1;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   362
-
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   363
+        mCoolkey = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   364
+	mCACLocalLogin = false;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   365
 	return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   366
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   367
-    mCoolkey = 1;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   368
+    mCoolkey = 1; /* coolkey applet selected */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   369
     log->log("time connect: Select Applet %d ms\n", OSTimeNow() - time);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   370
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   371
     state |= APPLET_SELECTABLE;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   372
@@ -700,8 +844,8 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   373
 	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   374
     } else {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   375
 	loggedIn = false;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   376
+	pinCache.invalidate();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   377
 	if (hard) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   378
-	    pinCache.invalidate();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   379
 	    pinCache.clearPin();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   380
 	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   381
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   382
@@ -716,17 +860,113 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   383
     invalidateLogin(false);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   384
 }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   385
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   386
+CKYStatus
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   387
+Slot::getCACAid()
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   388
+{
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   389
+    CKYBuffer tBuf;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   390
+    CKYBuffer vBuf;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   391
+    CKYSize tlen, vlen;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   392
+    CKYOffset toffset, voffset;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   393
+    int certSlot = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   394
+    int i,length = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   395
+    CKYStatus status;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   396
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   397
+    CKYBuffer_InitEmpty(&tBuf);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   398
+    CKYBuffer_InitEmpty(&vBuf);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   399
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   400
+    /* clear out the card AID's */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   401
+    for (i=0; i < MAX_CERT_SLOTS; i++) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   402
+	CKYBuffer_Resize(&cardAID[i],0);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   403
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   404
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   405
+    status = CACApplet_SelectCCC(conn,NULL);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   406
+    if (status != CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   407
+	/* are we an old CAC */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   408
+	status = CACApplet_SelectPKI(conn, &cardAID[0], 0, NULL);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   409
+	if (status != CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   410
+	   /* no, just fail */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   411
+	   return status;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   412
+	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   413
+	/* yes, fill in the old applets */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   414
+	mOldCAC = true;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   415
+	for (i=1; i< MAX_CERT_SLOTS; i++) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   416
+	    CACApplet_SelectPKI(conn, &cardAID[i], i, NULL);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   417
+	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   418
+	maxCacCerts = 3;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   419
+	return CKYSUCCESS;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   420
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   421
+    /* definately not an old CAC */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   422
+    mOldCAC = false;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   423
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   424
+    /* read the TLV */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   425
+    status = CACApplet_ReadFile(conn, CAC_TAG_FILE, &tBuf, NULL);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   426
+    if (status != CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   427
+	goto done;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   428
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   429
+    status = CACApplet_ReadFile(conn, CAC_VALUE_FILE, &vBuf, NULL);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   430
+    if (status != CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   431
+	goto done;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   432
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   433
+    tlen = CKYBuffer_Size(&tBuf);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   434
+    vlen = CKYBuffer_Size(&vBuf);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   435
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   436
+    for(toffset = 2, voffset=2; 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   437
+	certSlot < MAX_CERT_SLOTS && toffset < tlen && voffset < vlen ; 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   438
+		voffset += length) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   439
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   440
+	CKYByte tag = CKYBuffer_GetChar(&tBuf, toffset);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   441
+	length = CKYBuffer_GetChar(&tBuf, toffset+1);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   442
+	toffset += 2;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   443
+	if (length == 0xff) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   444
+	    length = CKYBuffer_GetShortLE(&tBuf, toffset);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   445
+	    toffset +=2;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   446
+	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   447
+	if (tag != CAC_TAG_CARDURL) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   448
+	    continue;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   449
+	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   450
+	/* CARDURL tags must be at least 10 bytes long */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   451
+	if (length < 10) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   452
+	    continue;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   453
+	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   454
+	/* check the app type, should be TLV_APP_PKI */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   455
+	if (CKYBuffer_GetChar(&vBuf, voffset+5) != CAC_TLV_APP_PKI) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   456
+	    continue;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   457
+	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   458
+	status = CKYBuffer_AppendBuffer(&cardAID[certSlot], &vBuf, voffset, 5);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   459
+	if (status != CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   460
+	    goto done;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   461
+	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   462
+	status = CKYBuffer_AppendBuffer(&cardAID[certSlot], &vBuf, 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   463
+								voffset+8, 2);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   464
+	if (status != CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   465
+	    goto done;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   466
+	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   467
+	cardEF[certSlot] = CKYBuffer_GetShortLE(&vBuf, voffset+6);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   468
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   469
+	certSlot++;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   470
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   471
+    status = CKYSUCCESS;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   472
+    if (certSlot == 0) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   473
+	status = CKYAPDUFAIL; /* probably neeed a beter error code */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   474
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   475
+    maxCacCerts = certSlot;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   476
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   477
+done:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   478
+    CKYBuffer_FreeData(&tBuf);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   479
+    CKYBuffer_FreeData(&vBuf);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   480
+    return status;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   481
+}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   482
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   483
 void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   484
 Slot::refreshTokenState()
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   485
 {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   486
     if( cardStateMayHaveChanged() ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   487
-log->log("card changed\n");
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   488
+        log->log("card changed\n");
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   489
 	invalidateLogin(true);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   490
         closeAllSessions();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   491
 	unloadObjects();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   492
         connectToToken();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   493
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   494
-
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   495
         if( state & APPLET_PERSONALIZED ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   496
             try {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   497
                 loadObjects();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   498
@@ -924,7 +1164,7 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   499
 //
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   500
 #define COOLKEY "CoolKey"
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   501
 #define POSSESSION " for "
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   502
-    if (!personName || personName == "") {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   503
+    if (!personName || personName[0] == '\0' ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   504
 	const int coolKeySize = sizeof(COOLKEY) ;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   505
 	memcpy(label, COOLKEY, coolKeySize-1);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   506
 	makeSerialString(&label[coolKeySize], maxSize-coolKeySize, cuid);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   507
@@ -964,7 +1204,7 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   508
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   509
 struct _manList {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   510
      unsigned short type;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   511
-     char *string;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   512
+     const char *string;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   513
 };
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   514
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   515
 static const struct _manList  manList[] = {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   516
@@ -1046,6 +1286,7 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   517
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   518
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   519
     return CKR_OK;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   520
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   521
 }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   522
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   523
 void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   524
@@ -1066,7 +1307,16 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   525
     bool found = FALSE;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   526
     CKYStatus status;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   527
     SCARD_READERSTATE *myReaderStates = NULL;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   528
+    static SCARD_READERSTATE pnp = { 0 };
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   529
     unsigned int myNumReaders = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   530
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   531
+    readerListLock.getLock();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   532
+    if (pnp.szReader == 0) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   533
+	    CKYReader_Init(&pnp);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   534
+	    pnp.szReader = "\\\\?PnP?\\Notification";
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   535
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   536
+    readerListLock.releaseLock();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   537
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   538
 #ifndef notdef
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   539
     do {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   540
 	readerListLock.getLock();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   541
@@ -1079,52 +1329,98 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   542
 	    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   543
 	    throw;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   544
 	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   545
-	if (myNumReaders != numReaders) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   546
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   547
+	/* Before round-tripping to the daemon for the duration of the
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   548
+	 * timeout, first see if we lost any readers, and pick a slot
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   549
+	 * from that set to return
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   550
+	 */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   551
+	for (i=0; i < numReaders; i++) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   552
+	    unsigned long knownState = 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   553
+				CKYReader_GetKnownState(&readerStates[i]);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   554
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   555
+	    if ((knownState & SCARD_STATE_UNAVAILABLE) &&
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   556
+		(knownState & SCARD_STATE_CHANGED)) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   557
+		CKYReader_SetKnownState(&readerStates[i], 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   558
+				knownState & ~SCARD_STATE_CHANGED);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   559
+		readerListLock.releaseLock();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   560
+		*slotp = slotIndexToID(i);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   561
+		found = TRUE;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   562
+		break;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   563
+	    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   564
+	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   565
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   566
+	if (found) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   567
+	    break;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   568
+	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   569
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   570
+	if (shuttingDown) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   571
+	    readerListLock.releaseLock();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   572
+	    break;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   573
+	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   574
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   575
+	if (myNumReaders != numReaders + 1) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   576
 	    if (myReaderStates) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   577
 		delete [] myReaderStates;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   578
 	    } 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   579
-	    myReaderStates = new SCARD_READERSTATE [numReaders];
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   580
+	    myReaderStates = new SCARD_READERSTATE [numReaders + 1];
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   581
+            myNumReaders = numReaders + 1;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   582
 	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   583
-	memcpy(myReaderStates, readerStates, 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   584
-				sizeof(SCARD_READERSTATE)*numReaders);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   585
-	myNumReaders = numReaders;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   586
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   587
+	memcpy(myReaderStates, readerStates,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   588
+				sizeof(SCARD_READERSTATE) * numReaders);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   589
+	memcpy(&myReaderStates[numReaders], &pnp, sizeof(pnp));
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   590
 	readerListLock.releaseLock();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   591
 	status = CKYCardContext_WaitForStatusChange(context,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   592
-				 myReaderStates, myNumReaders, timeout);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   593
+			 myReaderStates, myNumReaders, timeout);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   594
 	if (status == CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   595
-	    for (i=0; i < myNumReaders; i++) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   596
-		SCARD_READERSTATE *rsp = &myReaderStates[i];
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   597
-	        unsigned long eventState = CKYReader_GetEventState(rsp);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   598
+            unsigned long eventState;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   599
+	    for (i=0; i < myNumReaders - 1; i++) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   600
+		eventState = CKYReader_GetEventState(&myReaderStates[i]);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   601
 		if (eventState & SCARD_STATE_CHANGED) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   602
 		    readerListLock.getLock();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   603
-		    CKYReader_SetKnownState(&readerStates[i], eventState & ~SCARD_STATE_CHANGED);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   604
+		    CKYReader_SetKnownState(&readerStates[i], 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   605
+				eventState & ~SCARD_STATE_CHANGED);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   606
 		    readerListLock.releaseLock();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   607
 		    *slotp = slotIndexToID(i);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   608
 		    found = TRUE;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   609
 		    break;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   610
 		}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   611
 	    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   612
+            /* No real need to check for an additional card, we already update 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   613
+	     * the list when we iterate. */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   614
+	    if (!found) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   615
+		eventState = CKYReader_GetEventState(
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   616
+					&myReaderStates[myNumReaders-1]);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   617
+                if (eventState & SCARD_STATE_CHANGED) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   618
+		    readerListLock.getLock();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   619
+		    CKYReader_SetKnownState(&pnp, 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   620
+				eventState & ~SCARD_STATE_CHANGED);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   621
+		    readerListLock.releaseLock();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   622
+                    log->log("Reader insertion/removal detected\n");
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   623
+		    continue; /* get the update */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   624
+		}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   625
+            }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   626
 	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   627
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   628
         if (found || (flag == CKF_DONT_BLOCK) || shuttingDown) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   629
             break;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   630
         }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   631
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   632
         #ifndef WIN32
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   633
-        if (status != CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   634
-
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   635
-            if ( (CKYCardContext_GetLastError(context) ==
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   636
-                                        SCARD_E_READER_UNAVAILABLE) ||
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   637
-                (CKYCardContext_GetLastError(context) == SCARD_E_TIMEOUT))
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   638
-            {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   639
-                OSSleep(timeout*PKCS11_CARD_ERROR_LATENCY);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   640
-            }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   641
-
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   642
-
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   643
-        }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   644
+        /* pcsc-lite needs to make progress or something */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   645
+	if (status != CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   646
+	    if ((CKYCardContext_GetLastError(context) ==
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   647
+						 SCARD_E_READER_UNAVAILABLE) ||
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   648
+	       (CKYCardContext_GetLastError(context) == SCARD_E_TIMEOUT)) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   649
+		OSSleep(timeout*PKCS11_CARD_ERROR_LATENCY);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   650
+	    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   651
+	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   652
         #endif
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   653
     } while ((status == CKYSUCCESS) ||
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   654
        (CKYCardContext_GetLastError(context) == SCARD_E_TIMEOUT) ||
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   655
-        ( CKYCardContext_GetLastError(context) == SCARD_E_READER_UNAVAILABLE));
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   656
+       (CKYCardContext_GetLastError(context) == SCARD_E_READER_UNAVAILABLE) ||
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   657
+       (CKYCardContext_GetLastError(context) == SCARD_E_NO_SERVICE) ||
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   658
+       (CKYCardContext_GetLastError(context) == SCARD_E_SERVICE_STOPPED) );
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   659
 #else
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   660
     do {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   661
 	OSSleep(100);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   662
@@ -1161,6 +1457,7 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   663
       case SCARD_W_REMOVED_CARD:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   664
         ckrv = CKR_DEVICE_REMOVED;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   665
         break;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   666
+      
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   667
       default:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   668
         ckrv = CKR_DEVICE_ERROR;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   669
         break;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   670
@@ -1220,14 +1517,68 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   671
 }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   672
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   673
 void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   674
-Slot::selectCACApplet(CKYByte instance)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   675
+Slot::selectCACApplet(CKYByte instance, bool doDisconnect)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   676
 {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   677
     CKYStatus status;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   678
-    status = CACApplet_SelectPKI(conn, instance, NULL);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   679
+    /* PIV containers and keys by instance */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   680
+    static const int container[] = {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   681
+	0x5fc105, 0x5fc10a, 0x5fc10b, 0x5fc101,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   682
+	0x5fc10d, 0x5fc10e, 0x5fc10f, 0x5fc110, 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   683
+	0x5fc111, 0x5fc112, 0x5fc113, 0x5fc114, 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   684
+	0x5fc115, 0x5fc116, 0x5fc117, 0x5fc118, 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   685
+	0x5fc119, 0x5fc11a, 0x5fc11b, 0x5fc11c, 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   686
+	0x5fc11d, 0x5fc11e, 0x5fc11f, 0x5fc120
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   687
+    };
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   688
+    static const int keyRef[] = {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   689
+	0x9a,     0x9c,     0x9d,     0x9e,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   690
+	0x82,     0x83,     0x84,     0x85,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   691
+	0x86,     0x87,     0x88,     0x89,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   692
+	0x8a,     0x8b,     0x8c,     0x8d,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   693
+	0x8e,     0x8f,     0x90,     0x91,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   694
+	0x92,     0x93,     0x94,     0x95
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   695
+    };
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   696
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   697
+    if (state & PIV_CARD) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   698
+        status = PIVApplet_Select(conn, NULL);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   699
+	if (status == CKYSCARDERR) handleConnectionError();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   700
+	if (status != CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   701
+	    if (doDisconnect) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   702
+	        disconnect();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   703
+	    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   704
+	    throw PKCS11Exception(CKR_DEVICE_REMOVED);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   705
+	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   706
+	pivContainer = container[instance];
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   707
+	pivKey = keyRef[instance];
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   708
+	return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   709
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   710
+    CKYBuffer *aid = &cardAID[instance];
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   711
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   712
+    if (CKYBuffer_Size(aid) == 0) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   713
+	if (doDisconnect) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   714
+	    disconnect();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   715
+	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   716
+        throw PKCS11Exception(CKR_DEVICE_REMOVED);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   717
+	return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   718
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   719
+    
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   720
+    status = CKYApplet_SelectFile(conn, aid, NULL);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   721
     if ( status == CKYSCARDERR ) handleConnectionError();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   722
     if ( status != CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   723
         // could not select applet: this just means it's not there
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   724
-        disconnect();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   725
+	if (doDisconnect) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   726
+	    disconnect();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   727
+	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   728
+        throw PKCS11Exception(CKR_DEVICE_REMOVED);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   729
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   730
+    if (mOldCAC) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   731
+	return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   732
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   733
+    status = CACApplet_SelectFile(conn, cardEF[instance], NULL);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   734
+    if ( status == CKYSCARDERR ) handleConnectionError();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   735
+    if ( status != CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   736
+	if (doDisconnect) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   737
+	    disconnect();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   738
+	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   739
         throw PKCS11Exception(CKR_DEVICE_REMOVED);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   740
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   741
 }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   742
@@ -1274,6 +1625,19 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   743
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   744
 };
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   745
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   746
+class KeyNumMatch {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   747
+  private:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   748
+    CKYByte keyNum;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   749
+    const Slot &slot;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   750
+  public:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   751
+    KeyNumMatch(CKYByte keyNum_, const Slot &s) : keyNum(keyNum_), slot(s) { }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   752
+    bool operator() (const PKCS11Object& obj) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   753
+        unsigned long objID = obj.getMuscleObjID();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   754
+        return (slot.getObjectClass(objID) == 'k')
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   755
+               && (slot.getObjectIndex(objID) == keyNum);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   756
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   757
+};
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   758
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   759
 class ObjectCertCKAIDMatch {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   760
   private:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   761
     CKYByte cka_id;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   762
@@ -1307,6 +1671,29 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   763
     return handle;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   764
 }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   765
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   766
+/* Create a short lived Secret Key for ECC key derive. */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   767
+PKCS11Object *
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   768
+Slot::createSecretKeyObject(CK_OBJECT_HANDLE handle, CKYBuffer *secretKeyBuffer, CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulAttributeCount)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   769
+{
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   770
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   771
+    if (secretKeyBuffer == NULL ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   772
+        throw PKCS11Exception(CKR_DEVICE_ERROR,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   773
+                        "Can't create secret key object for ECC.");
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   774
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   775
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   776
+    unsigned long muscleID = 0xfff;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   777
+    PKCS11Object *secret =  new SecretKey(muscleID,  handle, secretKeyBuffer, pTemplate, ulAttributeCount);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   778
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   779
+    if (secret == NULL) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   780
+        throw PKCS11Exception(CKR_DEVICE_ERROR,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   781
+                        "Can't create secret key object for ECC.");
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   782
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   783
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   784
+    tokenObjects.push_back(*secret);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   785
+    
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   786
+    return secret;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   787
+}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   788
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   789
 void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   790
 Slot::addKeyObject(list<PKCS11Object>& objectList, const ListObjectInfo& info,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   791
     CK_OBJECT_HANDLE handle, bool isCombined)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   792
@@ -1316,24 +1703,31 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   793
     CK_OBJECT_CLASS objClass = keyObj.getClass();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   794
     const CKYBuffer *id;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   795
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   796
-
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   797
     if (isCombined &&
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   798
-	   ((objClass == CKO_PUBLIC_KEY) || (objClass == CKO_PRIVATE_KEY))) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   799
-	id = keyObj.getAttribute(CKA_ID);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   800
-	if ((!id) || (CKYBuffer_Size(id) != 1)) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   801
-	    throw PKCS11Exception(CKR_DEVICE_ERROR,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   802
-			"Missing or invalid CKA_ID value");
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   803
-	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   804
-	iter = find_if(objectList.begin(), objectList.end(),
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   805
-			ObjectCertCKAIDMatch(CKYBuffer_GetChar(id,0)));
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   806
-	if ( iter == objectList.end() ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   807
+           ((objClass == CKO_PUBLIC_KEY) || (objClass == CKO_PRIVATE_KEY))) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   808
+        id = keyObj.getAttribute(CKA_ID);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   809
+        if ((!id) || (CKYBuffer_Size(id) != 1)) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   810
+            throw PKCS11Exception(CKR_DEVICE_ERROR,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   811
+                        "Missing or invalid CKA_ID value");
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   812
+        }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   813
+        iter = find_if(objectList.begin(), objectList.end(),
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   814
+                        ObjectCertCKAIDMatch(CKYBuffer_GetChar(id,0)));
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   815
+        if ( iter == objectList.end() ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   816
             // We failed to find a cert with a matching CKA_ID. This
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   817
             // can happen if the cert is not present on the token, or
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   818
             // the der encoded cert stored on the token was corrupted.
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   819
-	    throw PKCS11Exception(CKR_DEVICE_ERROR,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   820
-			"Failed to find cert with matching CKA_ID value");
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   821
-	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   822
-	keyObj.completeKey(*iter);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   823
+                throw PKCS11Exception(CKR_DEVICE_ERROR,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   824
+                                         "Failed to find cert with matching CKA_ID value");
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   825
+        }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   826
+        keyObj.completeKey(*iter);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   827
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   828
+        /*  use key object to determine what algorithms we support */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   829
+        if ( keyObj.getKeyType() == PKCS11Object::ecc) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   830
+            algs = (SlotAlgs) (algs | ALG_ECC);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   831
+        } else {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   832
+            algs = (SlotAlgs) (algs | ALG_RSA);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   833
+        }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   834
+       
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   835
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   836
     objectList.push_back(keyObj);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   837
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   838
@@ -1363,6 +1757,7 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   839
 void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   840
 Slot::unloadObjects()
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   841
 {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   842
+    algs = ALG_NONE;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   843
     tokenObjects.clear();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   844
     free(personName);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   845
     personName = NULL;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   846
@@ -1421,23 +1816,35 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   847
 // Shared memory segments are fixed size (equal to the object memory size of
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   848
 // the token). 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   849
 //
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   850
+//
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   851
+//
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   852
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   853
+struct SlotDataPair {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   854
+    unsigned long dataOffset;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   855
+    unsigned long  dataSize;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   856
+};
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   857
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   858
 struct SlotSegmentHeader {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   859
     unsigned short version;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   860
     unsigned short headerSize;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   861
     unsigned char  valid;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   862
-    unsigned char  reserved;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   863
+    unsigned char  firstCacCert;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   864
     unsigned char  cuid[10];
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   865
-    unsigned short reserved2;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   866
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   867
+    unsigned short reserved; 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   868
     unsigned short dataVersion;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   869
     unsigned short dataHeaderOffset;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   870
     unsigned short dataOffset;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   871
     unsigned long  dataHeaderSize;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   872
     unsigned long  dataSize;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   873
-    unsigned long  cert2Offset;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   874
-    unsigned long  cert2Size;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   875
+    unsigned long  nextDataOffset;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   876
+    SlotDataPair cacCerts[MAX_CERT_SLOTS];
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   877
 };
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   878
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   879
+const unsigned char NOT_A_CAC=0xff; /* place in firstCacCert field */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   880
+const unsigned short CAC_DATA_VERSION=2;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   881
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   882
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   883
 #define MAX_OBJECT_STORE_SIZE 15000
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   884
 //
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   885
 // previous development versions used a segment prefix of
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   886
@@ -1458,7 +1865,7 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   887
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   888
     sprintf(segName,SEGMENT_PREFIX"%s",readerName); 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   889
     segment = SHMem::initSegment(segName, MAX_OBJECT_STORE_SIZE, needInit);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   890
-    delete segName;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   891
+    delete [] segName;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   892
     if (!segment) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   893
 	// just run without shared memory
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   894
 	return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   895
@@ -1472,9 +1879,8 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   896
 	return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   897
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   898
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   899
-    SlotSegmentHeader *segmentHeader = (SlotSegmentHeader *)segmentAddr;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   900
     if (needInit) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   901
-	segmentHeader->valid = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   902
+	clearValid(0);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   903
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   904
     segmentSize = segment->getSHMemSize();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   905
 }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   906
@@ -1548,6 +1954,18 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   907
     return segmentHeader->dataVersion;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   908
 }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   909
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   910
+unsigned char
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   911
+SlotMemSegment::getFirstCacCert() const
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   912
+{
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   913
+    if (!segment) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   914
+	return NOT_A_CAC;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   915
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   916
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   917
+    SlotSegmentHeader *segmentHeader = (SlotSegmentHeader *)segmentAddr;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   918
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   919
+    return segmentHeader->firstCacCert;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   920
+}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   921
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   922
 void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   923
 SlotMemSegment::setVersion(unsigned short version)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   924
 {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   925
@@ -1571,6 +1989,18 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   926
     segmentHeader->dataVersion = version;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   927
 }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   928
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   929
+void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   930
+SlotMemSegment::setFirstCacCert(unsigned char firstCacCert)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   931
+{
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   932
+    if (!segment) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   933
+	return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   934
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   935
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   936
+    SlotSegmentHeader *segmentHeader = (SlotSegmentHeader *)segmentAddr;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   937
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   938
+    segmentHeader->firstCacCert = firstCacCert;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   939
+}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   940
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   941
 bool
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   942
 SlotMemSegment::isValid() const
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   943
 {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   944
@@ -1645,23 +2075,13 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   945
     int size;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   946
     CKYByte *data;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   947
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   948
-    switch (instance) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   949
-    case 0:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   950
-	data  = (CKYByte *) &segmentAddr[segmentHeader->dataHeaderOffset];
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   951
-	size = segmentHeader->dataHeaderSize;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   952
-	break;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   953
-    case 1:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   954
-	data  = (CKYByte *) &segmentAddr[segmentHeader->dataOffset];
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   955
-	size = segmentHeader->dataSize;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   956
-	break;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   957
-    case 2:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   958
-	data  = (CKYByte *) &segmentAddr[segmentHeader->cert2Offset];
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   959
-	size = segmentHeader->cert2Size;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   960
-	break;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   961
-    default:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   962
+    if (instance >= MAX_CERT_SLOTS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   963
 	CKYBuffer_Resize(objData, 0);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   964
 	return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   965
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   966
+    data = (CKYByte *) &segmentAddr[segmentHeader->cacCerts[instance]
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   967
+								.dataOffset];
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   968
+    size = segmentHeader->cacCerts[instance].dataSize;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   969
     CKYBuffer_Replace(objData, 0, data, size);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   970
 }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   971
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   972
@@ -1675,30 +2095,20 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   973
     SlotSegmentHeader *segmentHeader = (SlotSegmentHeader *)segmentAddr;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   974
     int size = CKYBuffer_Size(data);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   975
     CKYByte *shmData;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   976
-    switch (instance) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   977
-    case 0:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   978
-	segmentHeader->headerSize = sizeof *segmentHeader;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   979
-	segmentHeader->dataHeaderOffset = sizeof *segmentHeader;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   980
-	segmentHeader->dataHeaderSize = size;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   981
-	segmentHeader->dataOffset = segmentHeader->dataHeaderOffset + size;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   982
-	segmentHeader->dataSize = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   983
-	segmentHeader->cert2Offset = segmentHeader->dataOffset;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   984
-	segmentHeader->cert2Size = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   985
-	shmData = (CKYByte *) &segmentAddr[segmentHeader->dataHeaderOffset];
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   986
-	break;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   987
-    case 1:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   988
-	segmentHeader->dataSize = size;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   989
-	segmentHeader->cert2Offset = segmentHeader->dataOffset + size;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   990
-	segmentHeader->cert2Size = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   991
-	shmData = (CKYByte *) &segmentAddr[segmentHeader->dataOffset];
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   992
-	break;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   993
-    case 2:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   994
-	segmentHeader->cert2Size = size;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   995
-	shmData = (CKYByte *) &segmentAddr[segmentHeader->cert2Offset];
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   996
-	break;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   997
-    default:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   998
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   999
+    if (instance >= MAX_CERT_SLOTS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1000
 	return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1001
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1002
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1003
+    if (segmentHeader->firstCacCert == NOT_A_CAC) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1004
+	segmentHeader->firstCacCert = instance;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1005
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1006
+    unsigned long dataOffset = segmentHeader->nextDataOffset;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1007
+    segmentHeader->cacCerts[instance].dataOffset = dataOffset;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1008
+    segmentHeader->nextDataOffset += size;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1009
+    segmentHeader->cacCerts[instance].dataSize = size;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1010
+    shmData = (CKYByte *) &segmentAddr[dataOffset];
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1011
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1012
     memcpy(shmData, CKYBuffer_Data(data), size);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1013
 }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1014
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1015
@@ -1710,15 +2120,18 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1016
 	return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1017
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1018
     SlotSegmentHeader *segmentHeader = (SlotSegmentHeader *)segmentAddr;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1019
-    switch (instance) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1020
-    case 0:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1021
-	segmentHeader->headerSize = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1022
-	segmentHeader->dataHeaderSize = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1023
-	/* fall through */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1024
-    case 1:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1025
-	segmentHeader->dataSize = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1026
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1027
+    segmentHeader->headerSize = sizeof *segmentHeader;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1028
+    segmentHeader->dataHeaderOffset = sizeof *segmentHeader;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1029
+    segmentHeader->dataHeaderSize = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1030
+    segmentHeader->dataSize = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1031
+    for (int i=0; i < MAX_CERT_SLOTS; i++) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1032
+	segmentHeader->cacCerts[i].dataSize = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1033
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1034
+    segmentHeader->dataOffset = sizeof *segmentHeader;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1035
+    segmentHeader->nextDataOffset = sizeof *segmentHeader;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1036
     segmentHeader->valid = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1037
+    segmentHeader->firstCacCert = NOT_A_CAC;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1038
 }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1039
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1040
 void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1041
@@ -1756,7 +2169,7 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1042
     // shared memory is protected by our transaction call on the card
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1043
     //
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1044
     CKYStatus status;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1045
-    if (state & CAC_CARD) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1046
+    if (state & GOV_CARD) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1047
 	status = CACApplet_SelectCardManager(conn, NULL);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1048
     } else {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1049
 	status = CKYApplet_SelectCardManager(conn, NULL);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1050
@@ -1989,108 +2402,401 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1051
     return objInfoList;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1052
 }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1053
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1054
-void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1055
-Slot::loadCACCert(CKYByte instance)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1056
-{
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1057
-    CKYISOStatus apduRC;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1058
-    CKYStatus status = CKYSUCCESS;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1059
-    CKYBuffer cert;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1060
-    CKYBuffer rawCert;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1061
-    CKYBuffer shmCert;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1062
-    CKYSize  nextSize;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1063
+typedef enum {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1064
+	BER_UNWRAP,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1065
+	BER_NEXT
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1066
+} BERop;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1067
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1068
-    OSTime time = OSTimeNow();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1069
+static CKYStatus
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1070
+berProcess(CKYBuffer *buf, int matchTag, CKYBuffer *target, BERop type)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1071
+{
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1072
+    unsigned char tag;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1073
+    unsigned int used_length= 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1074
+    unsigned int data_length;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1075
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1076
-    CKYBuffer_InitEmpty(&cert);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1077
-    CKYBuffer_InitEmpty(&rawCert);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1078
-    CKYBuffer_InitEmpty(&shmCert);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1079
+    tag = CKYBuffer_GetChar(buf,used_length++);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1080
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1081
-    //
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1082
-    // not all CAC cards have all the PKI instances
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1083
-    // catch the applet selection errors if they don't
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1084
-    //
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1085
-    try {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1086
-        selectCACApplet(instance);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1087
-    } catch(PKCS11Exception& e) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1088
-	// all CAC's must have instance '0', throw the error it
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1089
-	// they don't.
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1090
-	if (instance == 0) throw e;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1091
-	// If the CAC doesn't have instance '2', and we were updating
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1092
-	// the shared memory, set it to valid now.
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1093
-	if ((instance == 2) && !shmem.isValid()) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1094
-	    shmem.setValid();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1095
-	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1096
-	return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1097
+    /* blow out when we come to the end */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1098
+    if (matchTag && tag != matchTag) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1099
+        return CKYLIBFAIL;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1100
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1101
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1102
-    log->log("CAC Cert %d: select CAC applet:  %d ms\n",
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1103
-						 instance, OSTimeNow() - time);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1104
+    data_length = CKYBuffer_GetChar(buf,used_length++);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1105
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1106
-    if (instance == 0) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1107
-	/* get the first 100 bytes of the cert */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1108
-	status = CACApplet_GetCertificateFirst(conn, &rawCert, 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1109
-						&nextSize, &apduRC);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1110
-	if (status != CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1111
-	    handleConnectionError();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1112
-	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1113
-	log->log("CAC Cert %d: fetch CAC Cert:  %d ms\n", 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1114
-						instance, OSTimeNow() - time);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1115
-    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1116
+    if (data_length & 0x80) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1117
+        int  len_count = data_length & 0x7f;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1118
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1119
-    unsigned short dataVersion = 1;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1120
-    CKYBool needRead = 1;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1121
+        data_length = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1122
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1123
-    /* see if it matches the shared memory */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1124
-    if (shmem.isValid() &&  shmem.getDataVersion() == dataVersion) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1125
-	shmem.readCACCert(&shmCert, instance);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1126
-	CKYSize certSize = CKYBuffer_Size(&rawCert);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1127
-	CKYSize shmCertSize = CKYBuffer_Size(&shmCert);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1128
-	const CKYByte *shmData = CKYBuffer_Data(&shmCert);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1129
+        while (len_count-- > 0) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1130
+            data_length = (data_length << 8) | 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1131
+				CKYBuffer_GetChar(buf,used_length++);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1132
+        }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1133
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1134
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1135
-	if (instance != 0) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1136
-	    needRead = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1137
-	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1138
+    if (data_length > (CKYBuffer_Size(buf)-used_length) ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1139
+        return CKYLIBFAIL;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1140
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1141
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1142
-	if (shmCertSize >= certSize) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1143
-	    if (memcmp(shmData, CKYBuffer_Data(&rawCert), certSize) == 0) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1144
-		/* yes it does, no need to read the rest of the cert, use
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1145
-		 * the cache */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1146
-		CKYBuffer_Replace(&rawCert, 0, shmData, shmCertSize);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1147
-		needRead = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1148
-	    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1149
+    if (type == BER_UNWRAP) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1150
+        return CKYBuffer_AppendBuffer(target, buf, used_length, data_length);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1151
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1152
+    return CKYBuffer_AppendBuffer(target, buf, used_length+data_length,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1153
+		CKYBuffer_Size(buf)-(used_length+data_length));
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1154
+}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1155
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1156
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1157
+CKYStatus
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1158
+Slot::readCACCertificateFirst(CKYBuffer *cert, CKYSize *nextSize) 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1159
+{
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1160
+    CKYStatus status;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1161
+    CKYISOStatus apduRC;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1162
+    *nextSize = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1163
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1164
+    if (state & PIV_CARD) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1165
+	CKYBuffer pivData;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1166
+	CKYBuffer certInfo;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1167
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1168
+	CKYBuffer_InitEmpty(&pivData);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1169
+	CKYBuffer_InitEmpty(&certInfo);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1170
+	CKYBuffer_Resize(cert, 0);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1171
+	status = PIVApplet_GetCertificate(conn, cert, pivContainer, &apduRC);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1172
+	/* actually, on success, we need to parse the certificate and find the
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1173
+	 * propper tag */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1174
+	if (status == CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1175
+	    status = berProcess(cert, 0x53, &pivData, BER_UNWRAP);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1176
+	    CKYBuffer_Resize(cert, 0);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1177
+	    CKYBuffer_AppendChar(cert,0);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1178
+	    do {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1179
+		CKYByte tag = CKYBuffer_GetChar(&pivData,0);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1180
+		if (tag == CAC_TAG_CERTIFICATE) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1181
+		    status = berProcess(&pivData, CAC_TAG_CERTIFICATE, 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1182
+					cert, BER_UNWRAP);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1183
+		}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1184
+		if (tag == CAC_TAG_CERTINFO) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1185
+		    CKYBuffer_Resize(&certInfo, 0);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1186
+		    status = berProcess(&pivData, CAC_TAG_CERTINFO, 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1187
+					&certInfo, BER_UNWRAP);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1188
+		    if (CKYBuffer_Size(&certInfo) == 1) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1189
+			CKYBuffer_SetChar(cert,0,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1190
+					CKYBuffer_GetChar(&certInfo,0));
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1191
+		    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1192
+		}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1193
+		if (status == CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1194
+		    CKYBuffer_Resize(&certInfo, 0);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1195
+		    status = berProcess(&pivData, 0, &certInfo, BER_NEXT);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1196
+		    if (status == CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1197
+			CKYBuffer_Resize(&pivData,0);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1198
+			status = CKYBuffer_AppendCopy(&pivData,&certInfo);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1199
+		    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1200
+		}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1201
+	    } while ((status == CKYSUCCESS) && (CKYBuffer_Size(&pivData) != 0));
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1202
+	    CKYBuffer_FreeData(&pivData);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1203
+	    CKYBuffer_FreeData(&certInfo);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1204
 	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1205
-	if (!needRead && (shmCertSize == 0)) {	
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1206
+	
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1207
+	return status;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1208
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1209
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1210
+    if (mOldCAC) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1211
+	/* get the first 100 bytes of the cert */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1212
+	status = CACApplet_GetCertificateFirst(conn, cert, nextSize, &apduRC);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1213
+	return status;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1214
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1215
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1216
+    CKYBuffer tBuf;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1217
+    CKYBuffer vBuf;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1218
+    CKYSize tlen, vlen;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1219
+    CKYOffset toffset, voffset;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1220
+    int length = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1221
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1222
+    CKYBuffer_InitEmpty(&tBuf);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1223
+    CKYBuffer_InitEmpty(&vBuf);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1224
+    CKYBuffer_Resize(cert, 0);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1225
+    CKYBuffer_AppendChar(cert,0);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1226
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1227
+    /* handle the new CAC card read */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1228
+    /* read the TLV */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1229
+    status = CACApplet_ReadFile(conn, CAC_TAG_FILE, &tBuf, NULL);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1230
+    if (status != CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1231
+	goto done;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1232
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1233
+    status = CACApplet_ReadFile(conn, CAC_VALUE_FILE, &vBuf, NULL);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1234
+    if (status != CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1235
+	goto done;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1236
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1237
+    tlen = CKYBuffer_Size(&tBuf);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1238
+    vlen = CKYBuffer_Size(&vBuf);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1239
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1240
+    /* look for the Cert out of the TLV */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1241
+    for(toffset = 2, voffset=2; toffset < tlen && voffset < vlen ; 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1242
+		voffset += length) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1243
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1244
+	CKYByte tag = CKYBuffer_GetChar(&tBuf, toffset);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1245
+	length = CKYBuffer_GetChar(&tBuf, toffset+1);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1246
+	toffset += 2;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1247
+	if (length == 0xff) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1248
+	    length = CKYBuffer_GetShortLE(&tBuf, toffset);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1249
+	    toffset +=2;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1250
+	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1251
+	if (tag == CAC_TAG_CERTIFICATE) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1252
+	    CKYBuffer_AppendBuffer(cert, &vBuf, voffset, length);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1253
+	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1254
+	if (tag == CAC_TAG_CERTINFO) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1255
+	    CKYBuffer_SetChar(cert,0,CKYBuffer_GetChar(&vBuf,voffset));
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1256
+	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1257
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1258
+    status = CKYSUCCESS;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1259
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1260
+done:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1261
+    CKYBuffer_FreeData(&tBuf);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1262
+    CKYBuffer_FreeData(&vBuf);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1263
+    return status;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1264
+}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1265
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1266
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1267
+const static unsigned long crc_table[] = {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1268
+0x00000000,0x77073096,0xee0e612c,0x990951ba,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1269
+0x076dc419,0x706af48f,0xe963a535,0x9e6495a3,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1270
+0x0edb8832,0x79dcb8a4,0xe0d5e91e,0x97d2d988,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1271
+0x09b64c2b,0x7eb17cbd,0xe7b82d07,0x90bf1d91,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1272
+0x1db71064,0x6ab020f2,0xf3b97148,0x84be41de,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1273
+0x1adad47d,0x6ddde4eb,0xf4d4b551,0x83d385c7,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1274
+0x136c9856,0x646ba8c0,0xfd62f97a,0x8a65c9ec,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1275
+0x14015c4f,0x63066cd9,0xfa0f3d63,0x8d080df5,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1276
+0x3b6e20c8,0x4c69105e,0xd56041e4,0xa2677172,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1277
+0x3c03e4d1,0x4b04d447,0xd20d85fd,0xa50ab56b,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1278
+0x35b5a8fa,0x42b2986c,0xdbbbc9d6,0xacbcf940,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1279
+0x32d86ce3,0x45df5c75,0xdcd60dcf,0xabd13d59,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1280
+0x26d930ac,0x51de003a,0xc8d75180,0xbfd06116,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1281
+0x21b4f4b5,0x56b3c423,0xcfba9599,0xb8bda50f,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1282
+0x2802b89e,0x5f058808,0xc60cd9b2,0xb10be924,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1283
+0x2f6f7c87,0x58684c11,0xc1611dab,0xb6662d3d,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1284
+0x76dc4190,0x01db7106,0x98d220bc,0xefd5102a,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1285
+0x71b18589,0x06b6b51f,0x9fbfe4a5,0xe8b8d433,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1286
+0x7807c9a2,0x0f00f934,0x9609a88e,0xe10e9818,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1287
+0x7f6a0dbb,0x086d3d2d,0x91646c97,0xe6635c01,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1288
+0x6b6b51f4,0x1c6c6162,0x856530d8,0xf262004e,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1289
+0x6c0695ed,0x1b01a57b,0x8208f4c1,0xf50fc457,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1290
+0x65b0d9c6,0x12b7e950,0x8bbeb8ea,0xfcb9887c,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1291
+0x62dd1ddf,0x15da2d49,0x8cd37cf3,0xfbd44c65,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1292
+0x4db26158,0x3ab551ce,0xa3bc0074,0xd4bb30e2,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1293
+0x4adfa541,0x3dd895d7,0xa4d1c46d,0xd3d6f4fb,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1294
+0x4369e96a,0x346ed9fc,0xad678846,0xda60b8d0,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1295
+0x44042d73,0x33031de5,0xaa0a4c5f,0xdd0d7cc9,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1296
+0x5005713c,0x270241aa,0xbe0b1010,0xc90c2086,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1297
+0x5768b525,0x206f85b3,0xb966d409,0xce61e49f,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1298
+0x5edef90e,0x29d9c998,0xb0d09822,0xc7d7a8b4,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1299
+0x59b33d17,0x2eb40d81,0xb7bd5c3b,0xc0ba6cad,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1300
+0xedb88320,0x9abfb3b6,0x03b6e20c,0x74b1d29a,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1301
+0xead54739,0x9dd277af,0x04db2615,0x73dc1683,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1302
+0xe3630b12,0x94643b84,0x0d6d6a3e,0x7a6a5aa8,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1303
+0xe40ecf0b,0x9309ff9d,0x0a00ae27,0x7d079eb1,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1304
+0xf00f9344,0x8708a3d2,0x1e01f268,0x6906c2fe,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1305
+0xf762575d,0x806567cb,0x196c3671,0x6e6b06e7,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1306
+0xfed41b76,0x89d32be0,0x10da7a5a,0x67dd4acc,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1307
+0xf9b9df6f,0x8ebeeff9,0x17b7be43,0x60b08ed5,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1308
+0xd6d6a3e8,0xa1d1937e,0x38d8c2c4,0x4fdff252,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1309
+0xd1bb67f1,0xa6bc5767,0x3fb506dd,0x48b2364b,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1310
+0xd80d2bda,0xaf0a1b4c,0x36034af6,0x41047a60,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1311
+0xdf60efc3,0xa867df55,0x316e8eef,0x4669be79,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1312
+0xcb61b38c,0xbc66831a,0x256fd2a0,0x5268e236,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1313
+0xcc0c7795,0xbb0b4703,0x220216b9,0x5505262f,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1314
+0xc5ba3bbe,0xb2bd0b28,0x2bb45a92,0x5cb36a04,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1315
+0xc2d7ffa7,0xb5d0cf31,0x2cd99e8b,0x5bdeae1d,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1316
+0x9b64c2b0,0xec63f226,0x756aa39c,0x026d930a,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1317
+0x9c0906a9,0xeb0e363f,0x72076785,0x05005713,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1318
+0x95bf4a82,0xe2b87a14,0x7bb12bae,0x0cb61b38,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1319
+0x92d28e9b,0xe5d5be0d,0x7cdcefb7,0x0bdbdf21,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1320
+0x86d3d2d4,0xf1d4e242,0x68ddb3f8,0x1fda836e,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1321
+0x81be16cd,0xf6b9265b,0x6fb077e1,0x18b74777,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1322
+0x88085ae6,0xff0f6a70,0x66063bca,0x11010b5c,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1323
+0x8f659eff,0xf862ae69,0x616bffd3,0x166ccf45,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1324
+0xa00ae278,0xd70dd2ee,0x4e048354,0x3903b3c2,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1325
+0xa7672661,0xd06016f7,0x4969474d,0x3e6e77db,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1326
+0xaed16a4a,0xd9d65adc,0x40df0b66,0x37d83bf0,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1327
+0xa9bcae53,0xdebb9ec5,0x47b2cf7f,0x30b5ffe9,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1328
+0xbdbdf21c,0xcabac28a,0x53b39330,0x24b4a3a6,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1329
+0xbad03605,0xcdd70693,0x54de5729,0x23d967bf,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1330
+0xb3667a2e,0xc4614ab8,0x5d681b02,0x2a6f2b94,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1331
+0xb40bbe37,0xc30c8ea1,0x5a05df1b,0x2d02ef8d
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1332
+};
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1333
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1334
+static unsigned long 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1335
+calc_crc32(const unsigned char *buf, int len)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1336
+{
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1337
+    unsigned long crc = 0xffffffff;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1338
+    int i;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1339
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1340
+    for (i=0; i < len; i++) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1341
+	unsigned char crc_low = crc & 0xff;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1342
+	unsigned long crc_high = crc >> 8;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1343
+	crc = crc_table[crc_low ^ buf[i]] ^ crc_high;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1344
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1345
+    return crc ^ 0xffffffff;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1346
+}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1347
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1348
+/*
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1349
+ * decompress, handles both gzip and zlib trailers
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1350
+ * it also automatically allocates the output buffer and expands it as 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1351
+ * necessary.
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1352
+ */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1353
+static int 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1354
+decompress(CKYBuffer *out, 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1355
+			CKYBuffer *in, CKYOffset offset, CKYSize len)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1356
+{
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1357
+    int zret;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1358
+    CKYStatus status;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1359
+    z_stream stream;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1360
+    int chunk = len *2;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1361
+    int outlen = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1362
+    
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1363
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1364
+    /* allocate inflate state */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1365
+    stream.zalloc = Z_NULL;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1366
+    stream.zfree = Z_NULL;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1367
+    stream.opaque = Z_NULL;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1368
+    stream.avail_in = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1369
+    stream.next_in = Z_NULL;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1370
+    zret = inflateInit(&stream);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1371
+    if (zret != Z_OK)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1372
+        return zret;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1373
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1374
+    status = CKYBuffer_Reserve(out, outlen);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1375
+    if (status != CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1376
+	return Z_MEM_ERROR;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1377
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1378
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1379
+    stream.avail_in = len;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1380
+    stream.next_in =  (Bytef *)(CKYBuffer_Data(in) + offset);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1381
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1382
+    do {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1383
+	CKYBuffer_Resize(out, outlen + chunk);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1384
+ 	stream.avail_out = chunk;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1385
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1386
+	stream.next_out = (Bytef *)CKYBuffer_Data(out)+ outlen;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1387
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1388
+	zret= inflate(&stream, Z_NO_FLUSH);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1389
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1390
+	/* we need the length early so it can be used in error processing */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1391
+	outlen += chunk - stream.avail_out;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1392
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1393
+	/* proccess the error codes */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1394
+	switch (zret) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1395
+	case Z_DATA_ERROR:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1396
+	    /* a DATA error can occur on either corrupted data, or on gzip.
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1397
+	     * data. This is because gzip uses CRC32 and zlib used ADLER32
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1398
+	     * checksums. We need to check to see if this failure is do to
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1399
+	     * a gzip header. */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1400
+	    /* 1) a gzip header includes 4 extra bytes containing the length
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1401
+	     * of the gziped data. This means there must be 4 more bytes
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1402
+	     * in our input buffer that have not been processed */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1403
+	    if (stream.avail_in != 4) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1404
+		break; /* not a gzip header */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1405
+	    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1406
+	    /* The last 4 bytes of a gzip header include the uncompressed length
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1407
+	     * modulo 2^32. Make sure the actual uncompressed length matches
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1408
+	     * the header. */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1409
+	    if ((outlen  & 0xffffffffL)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1410
+				!= CKYBuffer_GetLongLE(in, offset+len-4)) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1411
+		break; /* didn't decode the full length */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1412
+	    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1413
+	    /* At this point it''s pretty likely we have a gzip trailer. Verify
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1414
+	     * the crc32 values to make sure there hasn't been any corruption.
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1415
+	     */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1416
+	    if (calc_crc32(CKYBuffer_Data(out), outlen) != 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1417
+				CKYBuffer_GetLongLE(in,offset+len-8)) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1418
+		break; /* CRC didn't match */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1419
+	    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1420
+ 	    /* This was valid gzip data, and we've successfully uncompressed
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1421
+	     * it. We're now done. */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1422
+	    zret=Z_STREAM_END;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1423
+	    break;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1424
+	case Z_NEED_DICT:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1425
+	    /* if we need the dict, it wasn't in the data, 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1426
+	     * so it's a data error */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1427
+	    zret = Z_DATA_ERROR;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1428
+	    break;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1429
+	case Z_OK:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1430
+	    /* Z_OK means we need more data, expand the buffer and go again.
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1431
+	     * if we don't need more buffer space, then the input must have
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1432
+	     * been truncated, that's a data error */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1433
+	    if (stream.avail_out != 0) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1434
+		zret = Z_DATA_ERROR;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1435
+	    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1436
+	    break;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1437
+ 	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1438
+    } while (zret == Z_OK);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1439
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1440
+    /* cleanup */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1441
+    if (zret == Z_STREAM_END) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1442
+	zret = Z_OK;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1443
+	CKYBuffer_Resize(out, outlen);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1444
+    } else {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1445
+	CKYBuffer_Resize(out, 0);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1446
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1447
+    (void)inflateEnd(&stream);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1448
+    return zret;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1449
+}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1450
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1451
+/*
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1452
+ * only necessary for old CAC cards. New CAC cards have to read the
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1453
+ * whole cert in anyway above....
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1454
+ */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1455
+CKYStatus
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1456
+Slot::readCACCertificateAppend(CKYBuffer *cert, CKYSize nextSize)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1457
+{
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1458
+    CKYISOStatus apduRC;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1459
+    assert(mOldCAC);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1460
+    return CACApplet_GetCertificateAppend(conn, cert, nextSize, &apduRC);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1461
+}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1462
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1463
+void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1464
+Slot::loadCACCert(CKYByte instance)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1465
+{
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1466
+    CKYStatus status = CKYSUCCESS;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1467
+    CKYBuffer cert;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1468
+    CKYBuffer rawCert;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1469
+    CKYBuffer shmCert;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1470
+    CKYSize  nextSize;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1471
+    CKYISOStatus apduRC;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1472
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1473
+    OSTime time = OSTimeNow();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1474
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1475
+    CKYBuffer_InitEmpty(&cert);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1476
+    CKYBuffer_InitEmpty(&rawCert);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1477
+    CKYBuffer_InitEmpty(&shmCert);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1478
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1479
+    //
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1480
+    // not all CAC cards have all the PKI instances
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1481
+    // catch the applet selection errors if they don't
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1482
+    //
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1483
+    try {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1484
+        selectCACApplet(instance, false);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1485
+    } catch(PKCS11Exception& e) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1486
+	return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1487
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1488
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1489
+    log->log("CAC Cert %d: select CAC applet:  %d ms\n",
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1490
+						 instance, OSTimeNow() - time);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1491
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1492
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1493
+    if (shmem.isValid() &&  shmem.getDataVersion() == CAC_DATA_VERSION) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1494
+ 	shmem.readCACCert(&rawCert, instance);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1495
+	if (CKYBuffer_Size(&rawCert) == 0) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1496
 	    /* no cert of this type, just return */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1497
 	    return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1498
 	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1499
-    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1500
-    CKYBuffer_FreeData(&shmCert);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1501
+     } else {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1502
+	status = readCACCertificateFirst(&rawCert, &nextSize);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1503
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1504
-    if (needRead) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1505
-	/* it doesn't, read the new cert and update the cache */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1506
-	if (instance == 0) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1507
-	    shmem.clearValid(0);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1508
-	    shmem.setVersion(SHMEM_VERSION);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1509
-	    shmem.setDataVersion(dataVersion);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1510
-	} else {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1511
-	    status = CACApplet_GetCertificateFirst(conn, &rawCert, 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1512
-						&nextSize, &apduRC);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1513
-	
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1514
-	    if (status != CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1515
-		/* CAC only requires the Certificate in pki '0' */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1516
-		/* if pki '1' or '2' are empty, treat it as a non-fatal error*/
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1517
-		if (instance == 2) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1518
-		    /* we've attempted to read all the certs, shared memory
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1519
-		     * is now valid */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1520
-		    shmem.setValid();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1521
-		}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1522
-		return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1523
-	    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1524
+	if ((status != CKYSUCCESS) || (CKYBuffer_Size(&rawCert) <= 1)) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1525
+	    /* this cert doesn't exists, go to the next one */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1526
+	    return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1527
 	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1528
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1529
 	if (nextSize) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1530
-	    status = CACApplet_GetCertificateAppend(conn, &rawCert, 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1531
-						nextSize, &apduRC);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1532
+	    status = readCACCertificateAppend(&rawCert, nextSize);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1533
 	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1534
 	log->log("CAC Cert %d: Fetch rest :  %d ms\n", 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1535
 						instance, OSTimeNow() - time);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1536
@@ -2098,37 +2804,66 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1537
 	    handleConnectionError();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1538
 	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1539
 	shmem.writeCACCert(&rawCert, instance);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1540
-	if (instance == 2) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1541
-	    shmem.setValid();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1542
-	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1543
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1544
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1545
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1546
     log->log("CAC Cert %d: Cert has been read:  %d ms\n",
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1547
 						instance, OSTimeNow() - time);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1548
-    if (CKYBuffer_GetChar(&rawCert,0) == 1) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1549
-	CKYSize guessFinalSize = CKYBuffer_Size(&rawCert);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1550
-	CKYSize certSize = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1551
+    /* new CACs, and old CACs with the high one bit are compressed, 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1552
+     * uncompress them */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1553
+    if ((CKYBuffer_GetChar(&rawCert,0) & 0x3) == 1) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1554
+	CKYOffset offset = 1;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1555
 	int zret = Z_MEM_ERROR;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1556
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1557
-	do {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1558
-	    guessFinalSize *= 2;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1559
-	    status = CKYBuffer_Resize(&cert, guessFinalSize);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1560
-	    if (status != CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1561
-		    break;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1562
+	/* process the GZIP header if present */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1563
+	/* header_id = 0x1f, 0x8b. CM=8. If we ever support something other
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1564
+	 * than CM=8, we need to change the zlib header below. Currently both
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1565
+	 * gzip and zlib only support CM=8 (DEFLATE) compression */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1566
+	if ((CKYBuffer_GetChar(&rawCert,1) == 0x1f) &&
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1567
+	    (CKYBuffer_GetChar(&rawCert,2) == 0x8b) &&
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1568
+	    (CKYBuffer_GetChar(&rawCert,3) == 8)) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1569
+	    CKYByte flags = CKYBuffer_GetChar(&rawCert,4);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1570
+	    /* this has a gzip header, not raw data. */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1571
+	    offset += 10; /* base size of the gzip header */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1572
+	    if (flags & 4) { /* FEXTRA */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1573
+		CKYSize len = CKYBuffer_GetShortLE(&rawCert,offset);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1574
+		offset += len;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1575
+	    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1576
+	    if (flags & 8) { /* FNAME */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1577
+		while (CKYBuffer_GetChar(&rawCert,offset) != 0) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1578
+		    offset++;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1579
+		}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1580
+		offset++;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1581
+	    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1582
+	    if (flags & 0x10) { /* FComment */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1583
+		while (CKYBuffer_GetChar(&rawCert,offset) != 0) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1584
+		    offset++;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1585
+		}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1586
+		offset++;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1587
+	    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1588
+	    if (flags & 2) { /* FHCRC */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1589
+		offset += 2;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1590
 	    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1591
-	    certSize = guessFinalSize;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1592
-	    zret = uncompress((Bytef *)CKYBuffer_Data(&cert),&certSize,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1593
-			CKYBuffer_Data(&rawCert)+1, CKYBuffer_Size(&rawCert)-1);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1594
-	} while (zret == Z_BUF_ERROR);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1595
+	    offset -= 2;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1596
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1597
+	    /* add zlib header, so libz will be happy */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1598
+	    /* CINFO=7, CM=8, LEVEL=2, DICTFLAG=0, FCHECK= 1c */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1599
+	    /* NOTE: the zlib will fail when procssing the trailer. this is
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1600
+	     * ok because decompress automatically notices the failure and
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1601
+	     * and checks the gzip trailer. */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1602
+	    CKYBuffer_SetChar(&rawCert, offset, 0x78);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1603
+	    CKYBuffer_SetChar(&rawCert, offset+1, 0x9c);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1604
+	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1605
+	/* uncompress. This expands cert as necessary. */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1606
+	zret = decompress(&cert, &rawCert, offset, 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1607
+					CKYBuffer_Size(&rawCert)-offset);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1608
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1609
 	if (zret != Z_OK) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1610
 	    CKYBuffer_FreeData(&rawCert);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1611
 	    CKYBuffer_FreeData(&cert);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1612
 	    throw PKCS11Exception(CKR_DEVICE_ERROR, 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1613
-				"Corrupted compressed CAC Cert");
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1614
+				"Corrupted compressed CAC/PIV Cert");
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1615
 	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1616
-	CKYBuffer_Resize(&cert,certSize);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1617
     } else {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1618
 	CKYBuffer_InitFromBuffer(&cert,&rawCert,1,CKYBuffer_Size(&rawCert)-1);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1619
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1620
@@ -2136,12 +2871,18 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1621
     log->log("CAC Cert %d: Cert has been uncompressed:  %d ms\n",
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1622
 						instance, OSTimeNow() - time);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1623
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1624
-    CACCert certObj(instance, &cert);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1625
-    CACPrivKey privKey(instance, certObj);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1626
-    CACPubKey pubKey(instance, certObj);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1627
+    bool isPIV = (bool)((state & PIV_CARD) == PIV_CARD);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1628
+    CACCert certObj(instance, &cert, isPIV);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1629
+    CACPrivKey privKey(instance, certObj, isPIV);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1630
+    CACPubKey pubKey(instance, certObj, isPIV);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1631
     tokenObjects.push_back(privKey);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1632
     tokenObjects.push_back(pubKey);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1633
     tokenObjects.push_back(certObj);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1634
+    if ( pubKey.getKeyType() == PKCS11Object::ecc) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1635
+        algs = (SlotAlgs) (algs | ALG_ECC);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1636
+    } else {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1637
+        algs = (SlotAlgs) (algs | ALG_RSA);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1638
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1639
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1640
     if (personName == NULL) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1641
 	const char *name = certObj.getName();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1642
@@ -2153,6 +2894,94 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1643
 }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1644
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1645
 void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1646
+Slot::initCACShMem(void)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1647
+{
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1648
+    bool failed = false;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1649
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1650
+    unsigned char firstCert = shmem.getFirstCacCert();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1651
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1652
+    log->log("init CACShMem: \n");
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1653
+    /* check to make sure the shared memory is initialized with a CAC card */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1654
+    if (shmem.isValid() && shmem.getDataVersion() ==  CAC_DATA_VERSION
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1655
+				&& firstCert != NOT_A_CAC) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1656
+	CKYBuffer rawCert;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1657
+	CKYBuffer shmCert;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1658
+	CKYSize  nextSize;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1659
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1660
+        log->log("init CACShMem: valid CAC cache found firstCert = %d\n",
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1661
+						 firstCert);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1662
+	CKYBuffer_InitEmpty(&rawCert);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1663
+	CKYBuffer_InitEmpty(&shmCert);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1664
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1665
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1666
+	/* yes, see if it's this cac card by comparing the first cert 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1667
+	 * in the chain */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1668
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1669
+	/* see if the first cert is in the expected slot */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1670
+	try {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1671
+	    selectCACApplet(firstCert, false);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1672
+ 	} catch(PKCS11Exception& e) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1673
+	    failed = true;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1674
+            log->log("init CACShMem: applet select failed firstCert = %d\n",
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1675
+							firstCert);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1676
+	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1677
+	if (!failed) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1678
+	    CKYStatus status = readCACCertificateFirst(&rawCert, &nextSize);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1679
+	    if ((status != CKYSUCCESS) || CKYBuffer_Size(&rawCert) <= 1) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1680
+		failed = true;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1681
+                log->log("init CACShMem: read Cert failed firstCert = %d\n",
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1682
+			 				firstCert);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1683
+	    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1684
+	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1685
+	if (!failed) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1686
+	    shmem.readCACCert(&shmCert, firstCert);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1687
+	    CKYSize certSize = CKYBuffer_Size(&rawCert);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1688
+	    CKYSize shmCertSize = CKYBuffer_Size(&shmCert);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1689
+	    const CKYByte *shmData = CKYBuffer_Data(&shmCert);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1690
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1691
+	    if (shmCertSize >= certSize) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1692
+		if (memcmp(shmData, CKYBuffer_Data(&rawCert), certSize) == 0) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1693
+		    /* this card is cached, go on and use the cache */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1694
+            	    log->log("init CACShMem: entries match, using cache\n");
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1695
+		    CKYBuffer_FreeData(&rawCert);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1696
+		    CKYBuffer_FreeData(&shmCert);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1697
+		    return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1698
+		}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1699
+            }		
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1700
+            log->log("init CACShMem: no entry match certSize=%d"
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1701
+				" shmCertSize=%d\n",certSize, shmCertSize);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1702
+	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1703
+	CKYBuffer_FreeData(&rawCert);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1704
+	CKYBuffer_FreeData(&shmCert);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1705
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1706
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1707
+    log->log("init CACShMem: starting new cache valid=%d version=%d "
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1708
+		" firstCert=%d\n",shmem.isValid(), shmem.getDataVersion(), 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1709
+				firstCert);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1710
+    /* cache is either invalid or for another card, start initializing it */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1711
+    shmem.clearValid(0);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1712
+    shmem.setVersion(SHMEM_VERSION);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1713
+    shmem.setDataVersion(CAC_DATA_VERSION);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1714
+}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1715
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1716
+void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1717
+Slot::verifyCACShMem(void)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1718
+{
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1719
+    /* if the memory is valid, then nothing to do */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1720
+    if (shmem.isValid()) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1721
+	return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1722
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1723
+    /* if we didn't find any cert fail */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1724
+    if (shmem.getFirstCacCert() == NOT_A_CAC) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1725
+	shmem.clearValid(0);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1726
+	disconnect();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1727
+        throw PKCS11Exception(CKR_DEVICE_REMOVED);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1728
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1729
+    /* we're all set, let others see our results */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1730
+    shmem.setValid(); 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1731
+}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1732
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1733
+void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1734
 Slot::loadObjects()
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1735
 {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1736
     // throw away all token objects!
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1737
@@ -2170,10 +2999,12 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1738
     list<ListObjectInfo> objInfoList;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1739
     std::list<ListObjectInfo>::iterator iter;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1740
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1741
-    if (state & CAC_CARD) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1742
-	loadCACCert(0);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1743
-	loadCACCert(1);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1744
-	loadCACCert(2);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1745
+    if (state & GOV_CARD) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1746
+	initCACShMem();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1747
+	for (int i=0; i < maxCacCerts; i++) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1748
+	   loadCACCert(i);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1749
+	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1750
+	verifyCACShMem();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1751
 	status = trans.end();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1752
 	loadReaderObject();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1753
 	return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1754
@@ -2399,6 +3230,9 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1755
 	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1756
 	return nonceValid;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1757
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1758
+    if (!needLogin) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1759
+	return true;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1760
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1761
     return loggedIn;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1762
 }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1763
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1764
@@ -2415,6 +3249,7 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1765
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1766
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1767
     if (!isVersion1Key) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1768
+	pinCache.invalidate();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1769
 	pinCache.set((const char *)pPin, ulPinLen);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1770
     } else if (nonceValid) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1771
 	throw PKCS11Exception(CKR_USER_ALREADY_LOGGED_IN);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1772
@@ -2424,15 +3259,15 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1773
     CKYStatus status = trans.begin(conn);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1774
     if(status != CKYSUCCESS ) handleConnectionError();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1775
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1776
-    if (state & CAC_CARD) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1777
-	selectCACApplet(0);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1778
+    if (state & GOV_CARD) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1779
+	selectCACApplet(0, true);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1780
     } else {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1781
 	selectApplet();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1782
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1783
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1784
     if (isVersion1Key) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1785
 	attemptLogin((const char *)pPin);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1786
-    } else if (state & CAC_CARD) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1787
+    } else if (state & GOV_CARD) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1788
 	attemptCACLogin();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1789
     } else {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1790
 	oldAttemptLogin();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1791
@@ -2449,16 +3284,19 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1792
     CKYISOStatus result;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1793
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1794
     status = CACApplet_VerifyPIN(conn, 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1795
-		(const char *)CKYBuffer_Data(pinCache.get()), &result);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1796
+		(const char *)CKYBuffer_Data(pinCache.get()), 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1797
+		mCACLocalLogin, &result);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1798
     if( status == CKYSCARDERR ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1799
 	handleConnectionError();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1800
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1801
     switch( result ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1802
       case CKYISO_SUCCESS:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1803
         break;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1804
-      case 6981:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1805
+      case 0x6981:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1806
+	pinCache.clearPin();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1807
         throw PKCS11Exception(CKR_PIN_LOCKED);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1808
       default:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1809
+	pinCache.clearPin();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1810
 	if ((result & 0xff00) == 0x6300) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1811
             throw PKCS11Exception(CKR_PIN_INCORRECT);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1812
 	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1813
@@ -2487,10 +3325,13 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1814
       case CKYISO_SUCCESS:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1815
         break;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1816
       case CKYISO_AUTH_FAILED:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1817
+	pinCache.clearPin();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1818
         throw PKCS11Exception(CKR_PIN_INCORRECT);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1819
       case CKYISO_IDENTITY_BLOCKED:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1820
+	pinCache.clearPin();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1821
         throw PKCS11Exception(CKR_PIN_LOCKED);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1822
       default:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1823
+	pinCache.clearPin();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1824
         throw PKCS11Exception(CKR_DEVICE_ERROR, "Applet returned 0x%04x", 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1825
 								result);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1826
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1827
@@ -2577,7 +3418,7 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1828
         throw PKCS11Exception(CKR_SESSION_HANDLE_INVALID);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1829
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1830
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1831
-    if (state & CAC_CARD) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1832
+    if (state & GOV_CARD) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1833
 	CACLogout();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1834
 	return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1835
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1836
@@ -2704,7 +3545,7 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1837
     ObjectConstIter iter = find_if(tokenObjects.begin(), tokenObjects.end(),
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1838
         ObjectHandleMatch(hObject));
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1839
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1840
-    if( iter == tokenObjects.end() ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1841
+    if ( iter == tokenObjects.end()) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1842
         throw PKCS11Exception(CKR_OBJECT_HANDLE_INVALID);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1843
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1844
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1845
@@ -2788,6 +3629,21 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1846
 }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1847
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1848
 void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1849
+SlotList::derive(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1850
+        CK_OBJECT_HANDLE hBaseKey, CK_ATTRIBUTE_PTR pTemplate, 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1851
+        CK_ULONG ulAttributeCount, CK_OBJECT_HANDLE_PTR phKey)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1852
+{
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1853
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1854
+    CK_SLOT_ID slotID;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1855
+    SessionHandleSuffix suffix;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1856
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1857
+    decomposeSessionHandle(hSession, slotID, suffix);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1858
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1859
+    slots[slotIDToIndex(slotID)]->derive(suffix, pMechanism, hBaseKey, pTemplate, ulAttributeCount, phKey);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1860
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1861
+}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1862
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1863
+void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1864
 Slot::ensureValidSession(SessionHandleSuffix suffix)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1865
 {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1866
     if( ! isValidSession(suffix) ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1867
@@ -2821,6 +3677,23 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1868
     return keyNum & 0xFF;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1869
 }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1870
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1871
+PKCS11Object::KeyType
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1872
+Slot::getKeyTypeFromHandle(CK_OBJECT_HANDLE hKey)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1873
+{
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1874
+    ObjectConstIter iter = find_if(tokenObjects.begin(), tokenObjects.end(),
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1875
+        ObjectHandleMatch(hKey));
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1876
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1877
+    if( iter == tokenObjects.end() ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1878
+         throw PKCS11Exception(CKR_KEY_HANDLE_INVALID);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1879
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1880
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1881
+    if( getObjectClass(iter->getMuscleObjID()) != 'k' ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1882
+        throw PKCS11Exception(CKR_KEY_HANDLE_INVALID);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1883
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1884
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1885
+    return iter->getKeyType();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1886
+}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1887
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1888
 void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1889
 Slot::signInit(SessionHandleSuffix suffix, CK_MECHANISM_PTR pMechanism,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1890
         CK_OBJECT_HANDLE hKey)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1891
@@ -2830,7 +3703,10 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1892
     if( session == sessions.end() ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1893
         throw PKCS11Exception(CKR_SESSION_HANDLE_INVALID);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1894
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1895
-    session->signatureState.initialize(objectHandleToKeyNum(hKey));
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1896
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1897
+    PKCS11Object::KeyType  keyType = getKeyTypeFromHandle(hKey);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1898
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1899
+    session->signatureState.initialize(objectHandleToKeyNum(hKey), keyType);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1900
 }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1901
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1902
 void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1903
@@ -2842,7 +3718,10 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1904
     if( session == sessions.end() ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1905
         throw PKCS11Exception(CKR_SESSION_HANDLE_INVALID);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1906
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1907
-    session->decryptionState.initialize(objectHandleToKeyNum(hKey));
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1908
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1909
+    PKCS11Object::KeyType keyType = getKeyTypeFromHandle(hKey);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1910
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1911
+    session->decryptionState.initialize(objectHandleToKeyNum(hKey), keyType);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1912
 }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1913
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1914
 /**
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1915
@@ -2951,6 +3830,93 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1916
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1917
 }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1918
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1919
+class ECCKeyAgreementParams : public CryptParams {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1920
+  public:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1921
+    ECCKeyAgreementParams(unsigned int keysize) : CryptParams(keysize) { }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1922
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1923
+    CKYByte getDirection() const { return CKY_DIR_NONE;}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1924
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1925
+    CryptOpState& getOpState(Session& session) const {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1926
+        return session.keyAgreementState;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1927
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1928
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1929
+    void padInput(CKYBuffer *paddedInput, const CKYBuffer *unpaddedInput) const {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1930
+        return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1931
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1932
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1933
+    void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1934
+    unpadOutput(CKYBuffer *unpaddedOutput, const CKYBuffer *paddedOutput) const {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1935
+        return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1936
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1937
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1938
+};
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1939
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1940
+class SignatureParams : public CryptParams {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1941
+  public:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1942
+    SignatureParams(unsigned int keysize) : CryptParams(keysize) { }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1943
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1944
+    CKYByte getDirection() const { return CKY_DIR_NONE; }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1945
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1946
+    CryptOpState& getOpState(Session& session) const {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1947
+        return session.signatureState;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1948
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1949
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1950
+    void padInput(CKYBuffer *paddedInput, const CKYBuffer *unpaddedInput) const {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1951
+        return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1952
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1953
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1954
+    void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1955
+    unpadOutput(CKYBuffer *unpaddedOutput, const CKYBuffer *paddedOutput) const {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1956
+        return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1957
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1958
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1959
+};
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1960
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1961
+  
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1962
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1963
+class ECCSignatureParams : public CryptParams {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1964
+  public:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1965
+    ECCSignatureParams(unsigned int keysize) : CryptParams(keysize) { }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1966
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1967
+    CKYByte getDirection() const { return CKY_DIR_NONE; }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1968
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1969
+    CryptOpState& getOpState(Session& session) const {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1970
+        return session.signatureState;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1971
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1972
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1973
+    void padInput(CKYBuffer *paddedInput, const CKYBuffer *unpaddedInput) const {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1974
+        return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1975
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1976
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1977
+    void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1978
+    unpadOutput(CKYBuffer *unpaddedOutput, const CKYBuffer *paddedOutput) const {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1979
+        /* Here we will unpack the DER encoding of the signature */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1980
+  
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1981
+        if ( unpaddedOutput == NULL || paddedOutput == NULL) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1982
+            throw PKCS11Exception(CKR_ARGUMENTS_BAD);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1983
+        }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1984
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1985
+        CKYBuffer rawSignature;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1986
+        CKYBuffer_InitEmpty(&rawSignature); 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1987
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1988
+        DEREncodedSignature sig(paddedOutput);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1989
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1990
+        int rv = sig.getRawSignature(&rawSignature, getKeySize() ); 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1991
+   
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1992
+        if (rv == CKYSUCCESS) { 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1993
+            CKYBuffer_Replace(unpaddedOutput, 0, CKYBuffer_Data(&rawSignature),
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1994
+                             CKYBuffer_Size(&rawSignature));
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1995
+        } else {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1996
+            throw PKCS11Exception(CKR_DEVICE_ERROR);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1997
+        }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1998
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1999
+        CKYBuffer_FreeData(&rawSignature);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2000
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2001
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2002
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2003
+};
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2004
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2005
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2006
 class RSASignatureParams : public CryptParams {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2007
   public:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2008
     RSASignatureParams(unsigned int keysize) : CryptParams(keysize) { }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2009
@@ -3009,8 +3975,38 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2010
         CK_ULONG ulDataLen, CK_BYTE_PTR pSignature,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2011
         CK_ULONG_PTR pulSignatureLen)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2012
 {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2013
-    cryptRSA(suffix, pData, ulDataLen, pSignature, pulSignatureLen,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2014
-        RSASignatureParams(CryptParams::FIXED_KEY_SIZE));
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2015
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2016
+    refreshTokenState();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2017
+    SessionIter session = findSession(suffix);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2018
+    if( session == sessions.end() ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2019
+        throw PKCS11Exception(CKR_SESSION_HANDLE_INVALID);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2020
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2021
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2022
+    if (!isVersion1Key && ! isLoggedIn() ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2023
+        throw PKCS11Exception(CKR_USER_NOT_LOGGED_IN);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2024
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2025
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2026
+    /* Create a default one just to get the sigState */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2027
+    SignatureParams dummyParams(CryptParams::DEFAULT_KEY_SIZE);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2028
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2029
+    CryptOpState sigState = dummyParams.getOpState(*session);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2030
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2031
+    PKCS11Object::KeyType keyType = sigState.keyType;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2032
+   
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2033
+    if ( keyType == PKCS11Object::unknown) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2034
+        throw PKCS11Exception(CKR_DATA_INVALID);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2035
+    } 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2036
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2037
+    if( keyType == Key::ecc ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2038
+        ECCSignatureParams params(CryptParams::ECC_DEFAULT_KEY_SIZE);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2039
+        signECC(suffix, pData, ulDataLen, pSignature, pulSignatureLen,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2040
+            params);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2041
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2042
+    } else if (keyType == Key::rsa) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2043
+        RSASignatureParams params(CryptParams::DEFAULT_KEY_SIZE);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2044
+        cryptRSA(suffix, pData, ulDataLen, pSignature, pulSignatureLen,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2045
+            params);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2046
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2047
 }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2048
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2049
 void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2050
@@ -3018,14 +4014,15 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2051
         CK_ULONG ulDataLen, CK_BYTE_PTR pDecryptedData,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2052
         CK_ULONG_PTR pulDecryptedDataLen)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2053
 {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2054
+    RSADecryptParams params(CryptParams::DEFAULT_KEY_SIZE);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2055
     cryptRSA(suffix, pData, ulDataLen, pDecryptedData, pulDecryptedDataLen,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2056
-        RSADecryptParams(CryptParams::FIXED_KEY_SIZE));
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2057
+        params);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2058
 }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2059
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2060
 void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2061
 Slot::cryptRSA(SessionHandleSuffix suffix, CK_BYTE_PTR pInput,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2062
         CK_ULONG ulInputLen, CK_BYTE_PTR pOutput,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2063
-        CK_ULONG_PTR pulOutputLen, const CryptParams& params)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2064
+        CK_ULONG_PTR pulOutputLen, CryptParams& params)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2065
 {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2066
     refreshTokenState();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2067
     SessionIter session = findSession(suffix);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2068
@@ -3043,6 +4040,11 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2069
     CKYBuffer *result = &opState.result;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2070
     CKYByte keyNum = opState.keyNum;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2071
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2072
+    unsigned int keySize = getRSAKeySize(keyNum);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2073
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2074
+    if (keySize != CryptParams::DEFAULT_KEY_SIZE)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2075
+        params.setKeySize(keySize);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2076
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2077
     if( CKYBuffer_Size(result) == 0 ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2078
         // we haven't already peformed the decryption, so do it now.
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2079
         if( pInput == NULL || ulInputLen == 0) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2080
@@ -3062,7 +4064,8 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2081
   	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2082
 	try {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2083
 	    params.padInput(&inputPad, &input);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2084
-            performRSAOp(&output, &inputPad, keyNum, params.getDirection());
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2085
+            performRSAOp(&output, &inputPad, params.getKeySize(), 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2086
+								keyNum, params.getDirection());
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2087
 	    params.unpadOutput(result, &output);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2088
 	    CKYBuffer_FreeData(&input);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2089
 	    CKYBuffer_FreeData(&inputPad);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2090
@@ -3099,10 +4102,159 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2091
     return &nonce;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2092
 }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2093
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2094
+void Slot::signECC(SessionHandleSuffix suffix, CK_BYTE_PTR pInput,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2095
+        CK_ULONG ulInputLen, CK_BYTE_PTR pOutput,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2096
+        CK_ULONG_PTR pulOutputLen, CryptParams& params)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2097
+{
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2098
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2099
+    if( pulOutputLen == NULL ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2100
+        throw PKCS11Exception(CKR_DATA_INVALID,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2101
+            "output length is NULL");
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2102
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2103
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2104
+    refreshTokenState();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2105
+    SessionIter session = findSession(suffix);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2106
+    if( session == sessions.end() ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2107
+        throw PKCS11Exception(CKR_SESSION_HANDLE_INVALID);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2108
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2109
+    /* version 1 keys may not need login. We catch the error
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2110
+       on the operation. The token will not allow us to sign with
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2111
+       a protected key unless we are logged in.
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2112
+       can be removed when version 0 support is depricated.
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2113
+    */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2114
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2115
+    if (!isVersion1Key && ! isLoggedIn() ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2116
+        throw PKCS11Exception(CKR_USER_NOT_LOGGED_IN);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2117
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2118
+    CryptOpState& opState = params.getOpState(*session);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2119
+    CKYBuffer *result = &opState.result;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2120
+    CKYByte keyNum = opState.keyNum;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2121
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2122
+    unsigned int keySize = getECCKeySize(keyNum);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2123
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2124
+    if(keySize != CryptParams::ECC_DEFAULT_KEY_SIZE)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2125
+        params.setKeySize(keySize);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2126
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2127
+    if( CKYBuffer_Size(result) == 0 ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2128
+	unsigned int maxSize = params.getKeySize()/8;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2129
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2130
+        if( pInput == NULL || ulInputLen == 0) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2131
+            throw PKCS11Exception(CKR_DATA_LEN_RANGE);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2132
+        }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2133
+	if (ulInputLen > maxSize) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2134
+	    //pInput += ulInputLen - maxSize;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2135
+	    ulInputLen = maxSize;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2136
+	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2137
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2138
+        CKYBuffer input;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2139
+        CKYBuffer output;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2140
+        CKYBuffer_InitEmpty(&output);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2141
+        CKYStatus status = CKYBuffer_InitFromData(&input, pInput, ulInputLen);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2142
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2143
+        if (status != CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2144
+            CKYBuffer_FreeData(&output);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2145
+            throw PKCS11Exception(CKR_HOST_MEMORY);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2146
+        }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2147
+        try {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2148
+            performECCSignature(&output, &input, params.getKeySize(), keyNum);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2149
+            params.unpadOutput(result, &output);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2150
+            CKYBuffer_FreeData(&input);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2151
+            CKYBuffer_FreeData(&output);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2152
+        } catch(PKCS11Exception& e) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2153
+            CKYBuffer_FreeData(&input);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2154
+            CKYBuffer_FreeData(&output);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2155
+            throw(e);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2156
+        }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2157
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2158
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2159
+    if( pOutput != NULL ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2160
+        if( *pulOutputLen < CKYBuffer_Size(result) ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2161
+            *pulOutputLen = CKYBuffer_Size(result);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2162
+            throw PKCS11Exception(CKR_BUFFER_TOO_SMALL);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2163
+        }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2164
+        memcpy(pOutput, CKYBuffer_Data(result), CKYBuffer_Size(result));
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2165
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2166
+    *pulOutputLen = CKYBuffer_Size(result);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2167
+}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2168
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2169
+void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2170
+Slot::performECCSignature(CKYBuffer *output, const CKYBuffer *input, 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2171
+						unsigned int keySize, CKYByte keyNum)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2172
+{
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2173
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2174
+    /* establish a transaction */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2175
+    Transaction trans;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2176
+    CKYStatus status = trans.begin(conn);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2177
+    if( status != CKYSUCCESS ) handleConnectionError();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2178
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2179
+    if (state & GOV_CARD) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2180
+	selectCACApplet(keyNum, true);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2181
+    } else {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2182
+	selectApplet();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2183
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2184
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2185
+    CKYISOStatus result;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2186
+    int loginAttempted = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2187
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2188
+retry:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2189
+    if (state & PIV_CARD) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2190
+        status = PIVApplet_SignDecrypt(conn, pivKey, keySize/8, 0, input, output, &result);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2191
+    } else if (state & CAC_CARD) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2192
+        status = CACApplet_SignDecrypt(conn, input, output, &result);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2193
+    } else {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2194
+        status = CKYApplet_ComputeECCSignature(conn, keyNum, input, NULL, output, getNonce(), &result);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2195
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2196
+    /* map the ISO not logged in code to the coolkey one */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2197
+    if ((result == CKYISO_CONDITION_NOT_SATISFIED) ||
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2198
+		(result == CKYISO_SECURITY_NOT_SATISFIED)) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2199
+	result = (CKYStatus) CKYISO_UNAUTHORIZED;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2200
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2201
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2202
+    if (status != CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2203
+        if ( status == CKYSCARDERR ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2204
+            handleConnectionError();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2205
+        }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2206
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2207
+        if (result == CKYISO_DATA_INVALID) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2208
+            throw PKCS11Exception(CKR_DATA_INVALID);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2209
+        }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2210
+        /* version0 keys could be logged out in the middle by someone else,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2211
+           reauthenticate... This code can go away when we depricate.
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2212
+           version0 applets.
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2213
+        */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2214
+        if (!isVersion1Key && !loginAttempted  &&
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2215
+                    (result == CKYISO_UNAUTHORIZED)) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2216
+            /* try to reauthenticate  */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2217
+	    try {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2218
+		if (state & GOV_CARD) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2219
+		    attemptCACLogin();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2220
+		} else {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2221
+		    oldAttemptLogin();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2222
+		}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2223
+            } catch(PKCS11Exception& ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2224
+                /* attemptLogin can throw things like CKR_PIN_INCORRECT
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2225
+                  that don't make sense from a crypto operation. This is
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2226
+                  a result of pin caching. We will reformat any login
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2227
+                  exception to a CKR_DEVICE_ERROR.
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2228
+                */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2229
+                throw PKCS11Exception(CKR_DEVICE_ERROR);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2230
+            }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2231
+            loginAttempted = true;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2232
+            goto retry; /* easier to understand than a while loop in this case. */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2233
+        }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2234
+        throw PKCS11Exception( result == CKYISO_UNAUTHORIZED ?
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2235
+                 CKR_USER_NOT_LOGGED_IN : CKR_DEVICE_ERROR);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2236
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2237
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2238
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2239
+}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2240
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2241
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2242
 void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2243
-Slot::performRSAOp(CKYBuffer *output, const CKYBuffer *input, 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2244
+Slot::performRSAOp(CKYBuffer *output, const CKYBuffer *input, unsigned int keySize,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2245
 					CKYByte keyNum, CKYByte direction)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2246
 {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2247
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2248
     //
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2249
     // establish a transaction
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2250
     //
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2251
@@ -3113,8 +4265,8 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2252
     //
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2253
     // select the applet
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2254
     //
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2255
-    if (state & CAC_CARD) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2256
-	selectCACApplet(keyNum);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2257
+    if (state & GOV_CARD) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2258
+	selectCACApplet(keyNum, true);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2259
     } else {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2260
 	selectApplet();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2261
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2262
@@ -3122,12 +4274,21 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2263
     CKYISOStatus result;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2264
     int loginAttempted = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2265
 retry:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2266
-    if (state & CAC_CARD) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2267
+    if (state & PIV_CARD) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2268
+        status = PIVApplet_SignDecrypt(conn, pivKey, keySize/8, 0, input, output, &result);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2269
+    } else if (state & CAC_CARD) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2270
         status = CACApplet_SignDecrypt(conn, input, output, &result);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2271
     } else {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2272
         status = CKYApplet_ComputeCrypt(conn, keyNum, CKY_RSA_NO_PAD, direction,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2273
 		input, NULL, output, getNonce(), &result);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2274
     } 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2275
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2276
+    /* map the ISO not logged in code to the coolkey one */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2277
+    if ((result == CKYISO_CONDITION_NOT_SATISFIED) ||
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2278
+	 (result == CKYISO_SECURITY_NOT_SATISFIED)) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2279
+	result = CKYISO_UNAUTHORIZED;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2280
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2281
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2282
     if (status != CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2283
 	if ( status == CKYSCARDERR ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2284
 	    handleConnectionError();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2285
@@ -3138,11 +4299,15 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2286
 	// version0 keys could be logged out in the middle by someone else,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2287
 	// reauthenticate... This code can go away when we depricate.
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2288
         // version0 applets.
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2289
-	if (!isVersion1Key && !loginAttempted  && 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2290
+	if (!isVersion1Key && !loginAttempted  && pinCache.isValid() &&
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2291
 					(result == CKYISO_UNAUTHORIZED)) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2292
 	    // try to reauthenticate 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2293
 	    try {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2294
-		oldAttemptLogin();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2295
+		if (state & GOV_CARD) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2296
+		    attemptCACLogin();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2297
+		} else {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2298
+		    oldAttemptLogin();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2299
+		}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2300
 	    } catch(PKCS11Exception& ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2301
 		// attemptLogin can throw things like CKR_PIN_INCORRECT
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2302
 		// that don't make sense from a crypto operation. This is
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2303
@@ -3162,7 +4327,7 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2304
 Slot::seedRandom(SessionHandleSuffix suffix, CK_BYTE_PTR pData,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2305
         CK_ULONG ulDataLen)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2306
 {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2307
-    if (state & CAC_CARD) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2308
+    if (state & GOV_CARD) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2309
 	/* should throw unsupported */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2310
 	throw PKCS11Exception(CKR_DEVICE_ERROR);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2311
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2312
@@ -3214,7 +4379,7 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2313
 Slot::generateRandom(SessionHandleSuffix suffix, const CK_BYTE_PTR pData,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2314
         CK_ULONG ulDataLen)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2315
 {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2316
-    if (state & CAC_CARD) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2317
+    if (state & GOV_CARD) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2318
 	/* should throw unsupported */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2319
 	throw PKCS11Exception(CKR_DEVICE_ERROR);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2320
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2321
@@ -3245,3 +4410,268 @@
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2322
 	throw PKCS11Exception(CKR_DEVICE_ERROR);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2323
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2324
 }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2325
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2326
+#define MAX_NUM_KEYS 8
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2327
+unsigned int
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2328
+Slot::getRSAKeySize(CKYByte keyNum)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2329
+{
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2330
+    unsigned int keySize = CryptParams::DEFAULT_KEY_SIZE;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2331
+    int modSize = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2332
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2333
+    if(keyNum >= MAX_NUM_KEYS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2334
+        return keySize;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2335
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2336
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2337
+    ObjectConstIter iter;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2338
+    iter = find_if(tokenObjects.begin(), tokenObjects.end(),
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2339
+        KeyNumMatch(keyNum,*this));
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2340
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2341
+    if( iter == tokenObjects.end() ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2342
+        return keySize;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2343
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2344
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2345
+    CKYBuffer const *modulus = iter->getAttribute(CKA_MODULUS);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2346
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2347
+    if(modulus) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2348
+        modSize = CKYBuffer_Size(modulus);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2349
+        if(CKYBuffer_GetChar(modulus,0) == 0x0) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2350
+            modSize--;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2351
+        }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2352
+        if(modSize > 0)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2353
+            keySize = modSize * 8;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2354
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2355
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2356
+    return keySize;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2357
+}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2358
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2359
+unsigned int
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2360
+Slot::getECCKeySize(CKYByte keyNum)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2361
+{
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2362
+    return calcECCKeySize(keyNum);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2363
+} 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2364
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2365
+unsigned int
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2366
+Slot::calcECCKeySize(CKYByte keyNum)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2367
+{
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2368
+    unsigned int keySize = CryptParams::ECC_DEFAULT_KEY_SIZE;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2369
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2370
+    if(keyNum >= MAX_NUM_KEYS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2371
+        return keySize;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2372
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2373
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2374
+    ObjectConstIter iter;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2375
+    iter = find_if(tokenObjects.begin(), tokenObjects.end(),
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2376
+        KeyNumMatch(keyNum,*this));
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2377
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2378
+    if( iter == tokenObjects.end() ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2379
+        return keySize;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2380
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2381
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2382
+    CKYBuffer const *eccParams = iter->getAttribute(CKA_EC_PARAMS);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2383
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2384
+    if (eccParams == NULL) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2385
+        return keySize;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2386
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2387
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2388
+    /* Extract the oid from the params */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2389
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2390
+    CKYByte ecParamsLen = CKYBuffer_GetChar(eccParams, 1);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2391
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2392
+    if ( ecParamsLen == 0 ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2393
+        return keySize;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2394
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2395
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2396
+/* Now compare against the limited known list of oid byte info */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2397
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2398
+    unsigned int oidByteLen =  0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2399
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2400
+    CKYByte curByte = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2401
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2402
+    for (int i = 0 ; i < numECCurves ; i++ ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2403
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2404
+        oidByteLen = curveBytesNamePair[i].bytes[0];
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2405
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2406
+        if ( oidByteLen !=  (unsigned int ) ecParamsLen ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2407
+            continue;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2408
+        }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2409
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2410
+        int match = 1;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2411
+        for ( int j = 0 ; j < ecParamsLen ; j++ ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2412
+            curByte = CKYBuffer_GetChar(eccParams, 2 + j );
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2413
+            if ( curveBytesNamePair[i].bytes[ j + 1 ] != curByte ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2414
+                match = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2415
+                break;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2416
+            }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2417
+        }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2418
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2419
+        if ( match == 1 ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2420
+            keySize =  curveBytesNamePair[i].length;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2421
+            return keySize;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2422
+        }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2423
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2424
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2425
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2426
+    return keySize;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2427
+}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2428
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2429
+void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2430
+Slot::derive(SessionHandleSuffix suffix, CK_MECHANISM_PTR pMechanism,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2431
+        CK_OBJECT_HANDLE hBaseKey, CK_ATTRIBUTE_PTR pTemplate, 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2432
+        CK_ULONG ulAttributeCount, CK_OBJECT_HANDLE_PTR phKey)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2433
+{
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2434
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2435
+    log->log("Inside of Slot::Derive! \n");
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2436
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2437
+    ECCKeyAgreementParams params(CryptParams::ECC_DEFAULT_KEY_SIZE);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2438
+    SessionIter session = findSession(suffix);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2439
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2440
+    PKCS11Object::KeyType keyType = getKeyTypeFromHandle(hBaseKey);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2441
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2442
+    session->keyAgreementState.initialize(objectHandleToKeyNum(hBaseKey), keyType);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2443
+    deriveECC(suffix, pMechanism, hBaseKey, pTemplate, ulAttributeCount, phKey, params);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2444
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2445
+}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2446
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2447
+void Slot::deriveECC(SessionHandleSuffix suffix, CK_MECHANISM_PTR pMechanism,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2448
+       CK_OBJECT_HANDLE hBaseKey, CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulAttributeCount, CK_OBJECT_HANDLE_PTR phKey, CryptParams& params)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2449
+{
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2450
+    if (pMechanism == NULL ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2451
+        throw PKCS11Exception(CKR_ARGUMENTS_BAD);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2452
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2453
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2454
+    CK_ECDH1_DERIVE_PARAMS *mechParams      = NULL;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2455
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2456
+    mechParams = (CK_ECDH1_DERIVE_PARAMS*) pMechanism->pParameter;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2457
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2458
+    if (mechParams == NULL || mechParams->kdf != CKD_NULL ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2459
+        throw PKCS11Exception(CKR_ARGUMENTS_BAD);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2460
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2461
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2462
+    refreshTokenState();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2463
+    SessionIter session = findSession(suffix);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2464
+    if( session == sessions.end() ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2465
+        throw PKCS11Exception(CKR_SESSION_HANDLE_INVALID);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2466
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2467
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2468
+     /* version 1 keys may not need login. We catch the error
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2469
+      on the operation. The token will not allow us to sign with
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2470
+      a protected key unless we are logged in.
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2471
+      can be removed when version 0 support is depricated. */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2472
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2473
+    if (!isVersion1Key && ! isLoggedIn() ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2474
+        throw PKCS11Exception(CKR_USER_NOT_LOGGED_IN);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2475
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2476
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2477
+    CryptOpState& opState = params.getOpState(*session);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2478
+    CKYBuffer *result = &opState.result;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2479
+    CKYByte keyNum = opState.keyNum;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2480
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2481
+    unsigned int keySize = getECCKeySize(keyNum);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2482
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2483
+    if(keySize != CryptParams::ECC_DEFAULT_KEY_SIZE)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2484
+        params.setKeySize(keySize);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2485
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2486
+    CK_MECHANISM_TYPE deriveMech = pMechanism->mechanism;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2487
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2488
+    CK_ULONG otherPublicLen = mechParams->ulPublicDataLen;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2489
+    CK_BYTE_PTR    otherPublicData = mechParams->pPublicData;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2490
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2491
+    CKYBuffer secretKeyBuffer;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2492
+    CKYBuffer_InitEmpty(&secretKeyBuffer);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2493
+    CKYBuffer publicDataBuffer;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2494
+    CKYStatus status = CKYBuffer_InitFromData(&publicDataBuffer,otherPublicData, otherPublicLen);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2495
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2496
+    if (status != CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2497
+        CKYBuffer_FreeData(&secretKeyBuffer);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2498
+        throw PKCS11Exception(CKR_HOST_MEMORY);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2499
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2500
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2501
+    PKCS11Object *secret = NULL;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2502
+    *phKey = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2503
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2504
+    if( CKYBuffer_Size(result) == 0 ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2505
+        try {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2506
+            performECCKeyAgreement(deriveMech, &publicDataBuffer, &secretKeyBuffer,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2507
+			 keyNum, params.getKeySize());
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2508
+            CK_OBJECT_HANDLE keyObjectHandle = generateUnusedObjectHandle();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2509
+            secret = createSecretKeyObject(keyObjectHandle, &secretKeyBuffer, pTemplate, ulAttributeCount);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2510
+        } catch(PKCS11Exception& e) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2511
+            CKYBuffer_FreeData(&secretKeyBuffer);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2512
+            CKYBuffer_FreeData(&publicDataBuffer);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2513
+            throw(e);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2514
+        }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2515
+   }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2516
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2517
+   CKYBuffer_FreeData(&secretKeyBuffer);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2518
+   CKYBuffer_FreeData(&publicDataBuffer);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2519
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2520
+   if ( secret ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2521
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2522
+       *phKey = secret->getHandle();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2523
+        delete secret;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2524
+   }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2525
+}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2526
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2527
+void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2528
+Slot::performECCKeyAgreement(CK_MECHANISM_TYPE deriveMech, CKYBuffer *publicDataBuffer, 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2529
+			     CKYBuffer *secretKeyBuffer, CKYByte keyNum, unsigned int keySize)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2530
+{
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2531
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2532
+    Transaction trans;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2533
+    CKYStatus status = trans.begin(conn);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2534
+    if( status != CKYSUCCESS ) handleConnectionError();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2535
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2536
+    if (state & GOV_CARD) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2537
+	selectCACApplet(keyNum, true);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2538
+    } else {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2539
+	selectApplet();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2540
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2541
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2542
+    CKYISOStatus result;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2543
+    int loginAttempted = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2544
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2545
+retry:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2546
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2547
+    if (state & PIV_CARD) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2548
+        status = PIVApplet_SignDecrypt(conn, pivKey, keySize/8, 1, publicDataBuffer, 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2549
+			secretKeyBuffer, &result);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2550
+    } else if (state & CAC_CARD) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2551
+        status = CACApplet_SignDecrypt(conn, publicDataBuffer, secretKeyBuffer, &result);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2552
+    } else {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2553
+    	status = CKYApplet_ComputeECCKeyAgreement(conn, keyNum, 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2554
+			publicDataBuffer , NULL, secretKeyBuffer, getNonce(), &result);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2555
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2556
+    /* map the ISO not logged in code to the coolkey one */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2557
+    if ((result == CKYISO_CONDITION_NOT_SATISFIED) ||
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2558
+		(result == CKYISO_SECURITY_NOT_SATISFIED)) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2559
+	result = (CKYStatus) CKYISO_UNAUTHORIZED;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2560
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2561
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2562
+    if (status != CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2563
+        if ( status == CKYSCARDERR ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2564
+            handleConnectionError();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2565
+        }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2566
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2567
+        if (result == CKYISO_DATA_INVALID) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2568
+            throw PKCS11Exception(CKR_DATA_INVALID);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2569
+        }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2570
+        if (!isVersion1Key && !loginAttempted  &&
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2571
+            (result == CKYISO_UNAUTHORIZED)) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2572
+	    try {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2573
+		if (state & GOV_CARD) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2574
+		    attemptCACLogin();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2575
+		} else {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2576
+		    oldAttemptLogin();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2577
+		}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2578
+	    } catch(PKCS11Exception& ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2579
+              throw PKCS11Exception(CKR_DEVICE_ERROR);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2580
+	    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2581
+	    loginAttempted = true;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2582
+	    goto retry;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2583
+        }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2584
+       
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2585
+        throw PKCS11Exception( result == CKYISO_UNAUTHORIZED ?
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2586
+                               CKR_USER_NOT_LOGGED_IN : CKR_DEVICE_ERROR);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2587
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2588
+    } 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2589
+}