components/coolkey/patches/06-slot.cpp.patch
author Stephen Gaul Jr <steve.gaul@oracle.com>
Tue, 12 Jul 2016 17:34:11 -0700
changeset 6401 8e624b116c1d
child 6998 ecb236c14ed7
permissions -rw-r--r--
PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices 22017764 Add Coolkey v1.1.0 to Userland consolidation
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
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
     8
@@ -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
     9
 {  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
    10
    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
    11
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    12
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    13
+/* ECC curve information
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    14
+ *    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
    15
+ *    
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    16
+ */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    17
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    18
+typedef struct curveBytes2Name {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    19
+    const CKYByte * bytes;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    20
+    const char *curveName;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    21
+    unsigned int length;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    22
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    23
+} CurveBytes2Name;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    24
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    25
+/* 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
    26
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    27
+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
    28
+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
    29
+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
    30
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    31
+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
    32
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    33
+static CurveBytes2Name curveBytesNamePair[] =
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
+ { nistp256, "nistp256", 256 },
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    36
+ { nistp384, "nistp384", 384 },
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    37
+ { nistp521, "nistp521", 521 }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    38
+};
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    39
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    40
 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
    41
 {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    42
     // 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
    43
@@ -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
    44
 	    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
    45
 	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
    46
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    47
-        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
    48
+        /* 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
    49
+	 * numSlots == 0 */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    50
+	if (slots) { 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    51
+            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
    52
+	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    53
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    54
 	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
    55
 	    newSlots[i] = new
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    56
@@ -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
    57
     return FALSE;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    58
 }
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
+bool
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    61
+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
    62
+{
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    63
+    if( !readerName || !readerNameList) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    64
+        return FALSE;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    65
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    66
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    67
+    int i = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    68
+    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
    69
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    70
+    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
    71
+    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
    72
+        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
    73
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    74
+        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
    75
+            return TRUE;
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
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    79
+    
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    80
+    return FALSE;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    81
+}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    82
+
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
  * 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
    85
  */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    86
@@ -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
    87
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    88
     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
    89
     if ( status != CKYSUCCESS ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    90
-	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
    91
+	/* 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
    92
+ 	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
    93
+	    (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
    94
+	    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
    95
                 "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
    96
 				CKYCardContext_GetLastError(context));
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
     }
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
-    if (!readerStates) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   101
+    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
   102
 	/* 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
   103
 	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
   104
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   105
-	/* 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
   106
-	 * happy */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   107
-	if (readerStates == NULL &&
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   108
-			 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
   109
-	    readerStates = (SCARD_READERSTATE *)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   110
-				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
   111
-	    if (readerStates) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   112
-		CKYReader_Init(readerStates);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   113
-		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
   114
-		if (status != CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   115
- 		    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
   116
-		    readerStates = NULL;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   117
-		} else {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   118
-		    numReaders = 1;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   119
-		}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   120
-	    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   121
-	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   122
 	CKYReaderNameList_Destroy(readerNames);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   123
 	        
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   124
 	if (readerStates == NULL) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   125
@@ -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
   126
 	return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   127
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   128
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   129
+    if (readerStates == NULL) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   130
+	/* 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
   131
+	 * 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
   132
+	 * 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
   133
+	 * 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
   134
+	 * 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
   135
+	return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   136
+    }
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
     /* 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
   140
      * 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
   141
      * 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
   142
@@ -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
   143
      * don't recognize.
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   144
      */
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
+    /* 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
   147
+     * 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
   148
+     * 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
   149
+     */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   150
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   151
+    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
   152
+    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
   153
+    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
   154
+        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
   155
+ 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   156
+        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
   157
+        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
   158
+            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
   159
+		 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
   160
+        } else {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   161
+	    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
   162
+		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
   163
+		 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
   164
+	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   165
+    } 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   166
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   167
+    if (readerNames == NULL) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   168
+        /* 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
   169
+	 * 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
   170
+	return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   171
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   172
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   173
     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
   174
     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
   175
     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
   176
@@ -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
   177
     : 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
   178
 	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
   179
 	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
   180
-	mCoolkey(false),
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   181
+	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
   182
+	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
   183
+	algs(ALG_NONE), 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   184
 #ifdef USE_SHMEM
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   185
 	shmem(readerName_),
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   186
 #endif
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   187
@@ -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
   188
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   189
     CKYBuffer_InitEmpty(&cardATR);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   190
     CKYBuffer_InitEmpty(&mCUID);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   191
+    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
   192
+	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
   193
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   194
   } catch(PKCS11Exception &) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   195
 	if (conn) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   196
 	    CKYCardConnection_Destroy(conn);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   197
@@ -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
   198
     CKYBuffer_FreeData(&nonce);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   199
     CKYBuffer_FreeData(&cardATR);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   200
     CKYBuffer_FreeData(&mCUID);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   201
+    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
   202
+	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
   203
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   204
 }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   205
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   206
 template <class C>
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   207
@@ -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
   208
     return rv;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   209
 }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   210
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   211
+bool
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   212
+Slot::getPIVLoginType(void)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   213
+{
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   214
+    CKYStatus status;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   215
+    CKYISOStatus apduRC;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   216
+    CKYBuffer buffer;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   217
+    bool local = true;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   218
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   219
+    CKYBuffer_InitEmpty(&buffer);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   220
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   221
+    /* 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
   222
+    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
   223
+    if (status != CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   224
+	/* 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
   225
+	goto done;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   226
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   227
+    /* 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
   228
+     * 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
   229
+     * 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
   230
+    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
   231
+			(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
   232
+	/* 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
   233
+	local = false;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   234
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   235
+done:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   236
+    CKYBuffer_FreeData(&buffer);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   237
+    return true;
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
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   240
 void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   241
 Slot::connectToToken()
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   242
 {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   243
-    CKYStatus status;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   244
+    CKYStatus status = CKYSCARDERR;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   245
     OSTime time = OSTimeNow();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   246
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   247
     mCoolkey = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   248
@@ -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
   249
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   250
     // 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
   251
     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
   252
-        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
   253
-        if( status != CKYSUCCESS ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   254
-            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
   255
+        int i = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   256
+    //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
   257
+	status = CKYSCARDERR;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   258
+        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
   259
+        {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   260
+            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
   261
+            if( status != CKYSUCCESS && 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   262
+                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
   263
+            {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   264
+                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
   265
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   266
+            }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   267
+            else
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   268
+            {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   269
+                break;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   270
+            }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   271
+            OSSleep(100000);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   272
+        }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   273
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   274
+        if( status != CKYSUCCESS)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   275
+        {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   276
             state = UNKNOWN;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   277
             return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   278
         }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   279
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   280
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   281
     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
   282
     if (!slotInfoFound) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   283
 	readSlotInfo();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   284
@@ -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
   285
         state = CARD_PRESENT;
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
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   288
-    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
   289
-		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
   290
-		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
   291
-
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   292
-        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
   293
-        {      
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   294
-            state |=  APPLET_SELECTABLE;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   295
-	    mCoolkey = 1;
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
+    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
   298
+    {      
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   299
+        state |=  APPLET_SELECTABLE;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   300
+	mCoolkey = 1;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   301
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   302
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   303
     /* 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
   304
@@ -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
   305
     // 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
   306
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   307
     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
   308
+    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
   309
+    if (status == CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   310
+	 /* 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
   311
+	 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
   312
+	 isVersion1Key = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   313
+	 needLogin = 1;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   314
+	 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
   315
+         mCoolkey = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   316
+	 mOldCAC = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   317
+	 mCACLocalLogin = getPIVLoginType();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   318
+	return;
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
     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
   321
     if (status != CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   322
         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
   323
-	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
   324
+	status = getCACAid();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   325
 	if (status != CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   326
-            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
   327
+	    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
   328
 	    if (status == CKYSCARDERR) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   329
-		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
   330
-			CKYCardConnection_GetLastError(conn));
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   331
-		disconnect();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   332
+		    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
   333
+				CKYCardConnection_GetLastError(conn));
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   334
+		    disconnect();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   335
 	    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   336
+	    /* CARD is unknown */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   337
 	    return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   338
 	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   339
 	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
   340
@@ -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
   341
          * unfriendly */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   342
 	isVersion1Key = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   343
 	needLogin = 1;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   344
-
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   345
+        mCoolkey = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   346
+	mCACLocalLogin = false;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   347
 	return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   348
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   349
-    mCoolkey = 1;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   350
+    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
   351
     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
   352
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   353
     state |= APPLET_SELECTABLE;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   354
@@ -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
   355
 	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   356
     } else {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   357
 	loggedIn = false;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   358
+	pinCache.invalidate();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   359
 	if (hard) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   360
-	    pinCache.invalidate();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   361
 	    pinCache.clearPin();
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
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   364
@@ -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
   365
     invalidateLogin(false);
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
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   368
+CKYStatus
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   369
+Slot::getCACAid()
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
+    CKYBuffer tBuf;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   372
+    CKYBuffer vBuf;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   373
+    CKYSize tlen, vlen;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   374
+    CKYOffset toffset, voffset;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   375
+    int certSlot = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   376
+    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
   377
+    CKYStatus status;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   378
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   379
+    CKYBuffer_InitEmpty(&tBuf);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   380
+    CKYBuffer_InitEmpty(&vBuf);
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
+    /* 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
   383
+    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
   384
+	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
   385
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   386
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   387
+    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
   388
+    if (status != CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   389
+	/* 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
   390
+	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
   391
+	if (status != CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   392
+	   /* no, just fail */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   393
+	   return status;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   394
+	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   395
+	/* 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
   396
+	mOldCAC = true;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   397
+	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
   398
+	    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
   399
+	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   400
+	maxCacCerts = 3;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   401
+	return CKYSUCCESS;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   402
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   403
+    /* 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
   404
+    mOldCAC = false;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   405
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   406
+    /* read the TLV */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   407
+    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
   408
+    if (status != CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   409
+	goto done;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   410
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   411
+    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
   412
+    if (status != CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   413
+	goto done;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   414
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   415
+    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
   416
+    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
   417
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   418
+    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
   419
+	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
   420
+		voffset += length) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   421
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   422
+	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
   423
+	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
   424
+	toffset += 2;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   425
+	if (length == 0xff) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   426
+	    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
   427
+	    toffset +=2;
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
+	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
   430
+	    continue;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   431
+	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   432
+	/* 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
   433
+	if (length < 10) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   434
+	    continue;
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
+	/* 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
   437
+	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
   438
+	    continue;
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
+	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
   441
+	if (status != CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   442
+	    goto done;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   443
+	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   444
+	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
   445
+								voffset+8, 2);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   446
+	if (status != CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   447
+	    goto done;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   448
+	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   449
+	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
   450
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   451
+	certSlot++;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   452
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   453
+    status = CKYSUCCESS;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   454
+    if (certSlot == 0) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   455
+	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
   456
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   457
+    maxCacCerts = certSlot;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   458
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   459
+done:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   460
+    CKYBuffer_FreeData(&tBuf);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   461
+    CKYBuffer_FreeData(&vBuf);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   462
+    return status;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   463
+}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   464
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   465
 void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   466
 Slot::refreshTokenState()
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   467
 {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   468
     if( cardStateMayHaveChanged() ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   469
-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
   470
+        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
   471
 	invalidateLogin(true);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   472
         closeAllSessions();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   473
 	unloadObjects();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   474
         connectToToken();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   475
 
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
         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
   478
             try {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   479
                 loadObjects();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   480
@@ -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
   481
 //
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   482
 #define COOLKEY "CoolKey"
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   483
 #define POSSESSION " for "
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   484
-    if (!personName || personName == "") {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   485
+    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
   486
 	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
   487
 	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
   488
 	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
   489
@@ -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
   490
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   491
 struct _manList {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   492
      unsigned short type;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   493
-     char *string;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   494
+     const char *string;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   495
 };
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   496
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   497
 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
   498
@@ -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
   499
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   500
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   501
     return CKR_OK;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   502
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   503
 }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   504
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   505
 void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   506
@@ -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
   507
     bool found = FALSE;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   508
     CKYStatus status;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   509
     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
   510
+    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
   511
     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
   512
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   513
+    readerListLock.getLock();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   514
+    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
   515
+	    CKYReader_Init(&pnp);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   516
+	    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
   517
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   518
+    readerListLock.releaseLock();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   519
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   520
 #ifndef notdef
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   521
     do {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   522
 	readerListLock.getLock();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   523
@@ -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
   524
 	    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   525
 	    throw;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   526
 	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   527
-	if (myNumReaders != numReaders) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   528
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   529
+	/* 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
   530
+	 * 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
   531
+	 * 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
   532
+	 */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   533
+	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
   534
+	    unsigned long knownState = 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   535
+				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
   536
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   537
+	    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
   538
+		(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
   539
+		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
   540
+				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
   541
+		readerListLock.releaseLock();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   542
+		*slotp = slotIndexToID(i);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   543
+		found = TRUE;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   544
+		break;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   545
+	    }
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
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   548
+	if (found) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   549
+	    break;
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
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   552
+	if (shuttingDown) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   553
+	    readerListLock.releaseLock();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   554
+	    break;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   555
+	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   556
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   557
+	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
   558
 	    if (myReaderStates) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   559
 		delete [] myReaderStates;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   560
 	    } 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   561
-	    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
   562
+	    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
   563
+            myNumReaders = numReaders + 1;
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
-	memcpy(myReaderStates, readerStates, 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   566
-				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
   567
-	myNumReaders = numReaders;
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
+	memcpy(myReaderStates, readerStates,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   570
+				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
   571
+	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
   572
 	readerListLock.releaseLock();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   573
 	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
   574
-				 myReaderStates, myNumReaders, timeout);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   575
+			 myReaderStates, myNumReaders, timeout);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   576
 	if (status == CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   577
-	    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
   578
-		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
   579
-	        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
   580
+            unsigned long eventState;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   581
+	    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
   582
+		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
   583
 		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
   584
 		    readerListLock.getLock();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   585
-		    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
   586
+		    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
   587
+				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
   588
 		    readerListLock.releaseLock();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   589
 		    *slotp = slotIndexToID(i);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   590
 		    found = TRUE;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   591
 		    break;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   592
 		}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   593
 	    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   594
+            /* 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
   595
+	     * 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
   596
+	    if (!found) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   597
+		eventState = CKYReader_GetEventState(
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   598
+					&myReaderStates[myNumReaders-1]);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   599
+                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
   600
+		    readerListLock.getLock();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   601
+		    CKYReader_SetKnownState(&pnp, 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   602
+				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
   603
+		    readerListLock.releaseLock();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   604
+                    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
   605
+		    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
   606
+		}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   607
+            }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   608
 	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   609
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   610
         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
   611
             break;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   612
         }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   613
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   614
         #ifndef WIN32
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   615
-        if (status != CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   616
-
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   617
-            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
   618
-                                        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
   619
-                (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
   620
-            {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   621
-                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
   622
-            }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   623
-
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
+        /* 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
   627
+	if (status != CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   628
+	    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
   629
+						 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
   630
+	       (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
   631
+		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
   632
+	    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   633
+	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   634
         #endif
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   635
     } while ((status == CKYSUCCESS) ||
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   636
        (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
   637
-        ( 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
   638
+       (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
   639
+       (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
   640
+       (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
   641
 #else
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   642
     do {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   643
 	OSSleep(100);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   644
@@ -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
   645
       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
   646
         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
   647
         break;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   648
+      
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   649
       default:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   650
         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
   651
         break;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   652
@@ -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
   653
 }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   654
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   655
 void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   656
-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
   657
+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
   658
 {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   659
     CKYStatus status;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   660
-    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
   661
+    /* 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
   662
+    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
   663
+	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
   664
+	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
   665
+	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
   666
+	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
   667
+	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
   668
+	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
   669
+    };
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   670
+    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
   671
+	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
   672
+	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
   673
+	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
   674
+	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
   675
+	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
   676
+	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
   677
+    };
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   678
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   679
+    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
   680
+        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
   681
+	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
   682
+	if (status != CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   683
+	    if (doDisconnect) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   684
+	        disconnect();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   685
+	    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   686
+	    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
   687
+	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   688
+	pivContainer = container[instance];
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   689
+	pivKey = keyRef[instance];
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   690
+	return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   691
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   692
+    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
   693
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   694
+    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
   695
+	if (doDisconnect) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   696
+	    disconnect();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   697
+	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   698
+        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
   699
+	return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   700
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   701
+    
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   702
+    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
   703
     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
   704
     if ( status != CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   705
         // 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
   706
-        disconnect();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   707
+	if (doDisconnect) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   708
+	    disconnect();
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
+        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
   711
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   712
+    if (mOldCAC) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   713
+	return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   714
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   715
+    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
   716
+    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
   717
+    if ( status != CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   718
+	if (doDisconnect) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   719
+	    disconnect();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   720
+	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   721
         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
   722
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   723
 }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   724
@@ -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
   725
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   726
 };
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
+class KeyNumMatch {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   729
+  private:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   730
+    CKYByte keyNum;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   731
+    const Slot &slot;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   732
+  public:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   733
+    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
   734
+    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
   735
+        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
   736
+        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
   737
+               && (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
   738
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   739
+};
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
 class ObjectCertCKAIDMatch {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   742
   private:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   743
     CKYByte cka_id;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   744
@@ -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
   745
     return handle;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   746
 }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   747
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   748
+/* 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
   749
+PKCS11Object *
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   750
+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
   751
+{
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   752
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   753
+    if (secretKeyBuffer == NULL ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   754
+        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
   755
+                        "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
   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
+    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
   759
+    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
   760
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   761
+    if (secret == NULL) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   762
+        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
   763
+                        "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
   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
+    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
   767
+    
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   768
+    return secret;
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
 void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   772
 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
   773
     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
   774
@@ -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
   775
     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
   776
     const CKYBuffer *id;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   777
 
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 (isCombined &&
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   780
-	   ((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
   781
-	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
   782
-	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
   783
-	    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
   784
-			"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
   785
-	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   786
-	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
   787
-			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
   788
-	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
   789
+           ((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
   790
+        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
   791
+        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
   792
+            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
   793
+                        "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
   794
+        }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   795
+        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
   796
+                        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
   797
+        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
   798
             // 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
   799
             // 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
   800
             // 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
   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
-			"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
   803
-	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   804
-	keyObj.completeKey(*iter);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   805
+                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
   806
+                                         "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
   807
+        }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   808
+        keyObj.completeKey(*iter);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   809
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   810
+        /*  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
   811
+        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
   812
+            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
   813
+        } else {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   814
+            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
   815
+        }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   816
+       
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   817
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   818
     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
   819
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   820
@@ -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
   821
 void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   822
 Slot::unloadObjects()
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   823
 {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   824
+    algs = ALG_NONE;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   825
     tokenObjects.clear();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   826
     free(personName);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   827
     personName = NULL;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   828
@@ -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
   829
 // 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
   830
 // the token). 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   831
 //
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   832
+//
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
+struct SlotDataPair {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   836
+    unsigned long dataOffset;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   837
+    unsigned long  dataSize;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   838
+};
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   839
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   840
 struct SlotSegmentHeader {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   841
     unsigned short version;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   842
     unsigned short headerSize;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   843
     unsigned char  valid;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   844
-    unsigned char  reserved;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   845
+    unsigned char  firstCacCert;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   846
     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
   847
-    unsigned short reserved2;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   848
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   849
+    unsigned short reserved; 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   850
     unsigned short dataVersion;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   851
     unsigned short dataHeaderOffset;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   852
     unsigned short dataOffset;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   853
     unsigned long  dataHeaderSize;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   854
     unsigned long  dataSize;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   855
-    unsigned long  cert2Offset;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   856
-    unsigned long  cert2Size;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   857
+    unsigned long  nextDataOffset;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   858
+    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
   859
 };
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   860
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   861
+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
   862
+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
   863
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   864
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   865
 #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
   866
 //
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   867
 // 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
   868
@@ -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
   869
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   870
     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
   871
     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
   872
-    delete segName;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   873
+    delete [] segName;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   874
     if (!segment) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   875
 	// 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
   876
 	return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   877
@@ -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
   878
 	return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   879
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   880
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   881
-    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
   882
     if (needInit) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   883
-	segmentHeader->valid = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   884
+	clearValid(0);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   885
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   886
     segmentSize = segment->getSHMemSize();
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
@@ -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
   889
     return segmentHeader->dataVersion;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   890
 }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   891
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   892
+unsigned char
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   893
+SlotMemSegment::getFirstCacCert() const
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   894
+{
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   895
+    if (!segment) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   896
+	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
   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
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   901
+    return segmentHeader->firstCacCert;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   902
+}
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
 void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   905
 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
   906
 {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   907
@@ -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
   908
     segmentHeader->dataVersion = version;
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
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   911
+void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   912
+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
   913
+{
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   914
+    if (!segment) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   915
+	return;
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
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   918
+    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
   919
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   920
+    segmentHeader->firstCacCert = firstCacCert;
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
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   923
 bool
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   924
 SlotMemSegment::isValid() const
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   925
 {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   926
@@ -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
   927
     int size;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   928
     CKYByte *data;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   929
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   930
-    switch (instance) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   931
-    case 0:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   932
-	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
   933
-	size = segmentHeader->dataHeaderSize;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   934
-	break;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   935
-    case 1:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   936
-	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
   937
-	size = segmentHeader->dataSize;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   938
-	break;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   939
-    case 2:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   940
-	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
   941
-	size = segmentHeader->cert2Size;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   942
-	break;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   943
-    default:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   944
+    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
   945
 	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
   946
 	return;
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
+    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
   949
+								.dataOffset];
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   950
+    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
   951
     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
   952
 }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   953
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   954
@@ -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
   955
     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
   956
     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
   957
     CKYByte *shmData;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   958
-    switch (instance) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   959
-    case 0:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   960
-	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
   961
-	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
   962
-	segmentHeader->dataHeaderSize = size;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   963
-	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
   964
-	segmentHeader->dataSize = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   965
-	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
   966
-	segmentHeader->cert2Size = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   967
-	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
   968
-	break;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   969
-    case 1:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   970
-	segmentHeader->dataSize = size;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   971
-	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
   972
-	segmentHeader->cert2Size = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   973
-	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
   974
-	break;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   975
-    case 2:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   976
-	segmentHeader->cert2Size = size;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   977
-	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
   978
-	break;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   979
-    default:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   980
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   981
+    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
   982
 	return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   983
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   984
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   985
+    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
   986
+	segmentHeader->firstCacCert = instance;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   987
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   988
+    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
   989
+    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
   990
+    segmentHeader->nextDataOffset += size;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   991
+    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
   992
+    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
   993
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   994
     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
   995
 }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   996
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   997
@@ -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
   998
 	return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   999
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1000
     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
  1001
-    switch (instance) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1002
-    case 0:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1003
-	segmentHeader->headerSize = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1004
-	segmentHeader->dataHeaderSize = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1005
-	/* fall through */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1006
-    case 1:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1007
-	segmentHeader->dataSize = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1008
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1009
+    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
  1010
+    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
  1011
+    segmentHeader->dataHeaderSize = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1012
+    segmentHeader->dataSize = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1013
+    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
  1014
+	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
  1015
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1016
+    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
  1017
+    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
  1018
     segmentHeader->valid = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1019
+    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
  1020
 }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1021
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1022
 void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1023
@@ -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
  1024
     // 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
  1025
     //
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1026
     CKYStatus status;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1027
-    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
  1028
+    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
  1029
 	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
  1030
     } else {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1031
 	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
  1032
@@ -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
  1033
     return objInfoList;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1034
 }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1035
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1036
-void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1037
-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
  1038
-{
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1039
-    CKYISOStatus apduRC;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1040
-    CKYStatus status = CKYSUCCESS;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1041
-    CKYBuffer cert;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1042
-    CKYBuffer rawCert;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1043
-    CKYBuffer shmCert;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1044
-    CKYSize  nextSize;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1045
+typedef enum {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1046
+	BER_UNWRAP,
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1047
+	BER_NEXT
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1048
+} BERop;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1049
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1050
-    OSTime time = OSTimeNow();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1051
+static CKYStatus
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1052
+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
  1053
+{
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1054
+    unsigned char tag;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1055
+    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
  1056
+    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
  1057
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1058
-    CKYBuffer_InitEmpty(&cert);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1059
-    CKYBuffer_InitEmpty(&rawCert);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1060
-    CKYBuffer_InitEmpty(&shmCert);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1061
+    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
  1062
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1063
-    //
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1064
-    // 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
  1065
-    // 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
  1066
-    //
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1067
-    try {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1068
-        selectCACApplet(instance);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1069
-    } catch(PKCS11Exception& e) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1070
-	// 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
  1071
-	// they don't.
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1072
-	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
  1073
-	// 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
  1074
-	// 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
  1075
-	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
  1076
-	    shmem.setValid();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1077
-	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1078
-	return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1079
+    /* 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
  1080
+    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
  1081
+        return CKYLIBFAIL;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1082
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1083
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1084
-    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
  1085
-						 instance, OSTimeNow() - time);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1086
+    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
  1087
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1088
-    if (instance == 0) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1089
-	/* 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
  1090
-	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
  1091
-						&nextSize, &apduRC);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1092
-	if (status != CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1093
-	    handleConnectionError();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1094
-	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1095
-	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
  1096
-						instance, OSTimeNow() - time);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1097
-    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1098
+    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
  1099
+        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
  1100
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1101
-    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
  1102
-    CKYBool needRead = 1;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1103
+        data_length = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1104
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1105
-    /* 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
  1106
-    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
  1107
-	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
  1108
-	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
  1109
-	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
  1110
-	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
  1111
+        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
  1112
+            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
  1113
+				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
  1114
+        }
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
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1117
-	if (instance != 0) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1118
-	    needRead = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1119
-	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1120
+    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
  1121
+        return CKYLIBFAIL;
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
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1124
-	if (shmCertSize >= certSize) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1125
-	    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
  1126
-		/* 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
  1127
-		 * the cache */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1128
-		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
  1129
-		needRead = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1130
-	    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1131
+    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
  1132
+        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
  1133
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1134
+    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
  1135
+		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
  1136
+}
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
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1139
+CKYStatus
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1140
+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
  1141
+{
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1142
+    CKYStatus status;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1143
+    CKYISOStatus apduRC;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1144
+    *nextSize = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1145
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1146
+    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
  1147
+	CKYBuffer pivData;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1148
+	CKYBuffer certInfo;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1149
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1150
+	CKYBuffer_InitEmpty(&pivData);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1151
+	CKYBuffer_InitEmpty(&certInfo);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1152
+	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
  1153
+	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
  1154
+	/* 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
  1155
+	 * propper tag */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1156
+	if (status == CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1157
+	    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
  1158
+	    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
  1159
+	    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
  1160
+	    do {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1161
+		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
  1162
+		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
  1163
+		    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
  1164
+					cert, BER_UNWRAP);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1165
+		}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1166
+		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
  1167
+		    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
  1168
+		    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
  1169
+					&certInfo, BER_UNWRAP);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1170
+		    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
  1171
+			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
  1172
+					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
  1173
+		    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1174
+		}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1175
+		if (status == CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1176
+		    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
  1177
+		    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
  1178
+		    if (status == CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1179
+			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
  1180
+			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
  1181
+		    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1182
+		}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1183
+	    } 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
  1184
+	    CKYBuffer_FreeData(&pivData);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1185
+	    CKYBuffer_FreeData(&certInfo);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1186
 	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1187
-	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
  1188
+	
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1189
+	return status;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1190
+    }
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
+    if (mOldCAC) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1193
+	/* 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
  1194
+	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
  1195
+	return status;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1196
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1197
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1198
+    CKYBuffer tBuf;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1199
+    CKYBuffer vBuf;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1200
+    CKYSize tlen, vlen;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1201
+    CKYOffset toffset, voffset;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1202
+    int length = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1203
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1204
+    CKYBuffer_InitEmpty(&tBuf);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1205
+    CKYBuffer_InitEmpty(&vBuf);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1206
+    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
  1207
+    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
  1208
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1209
+    /* 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
  1210
+    /* read the TLV */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1211
+    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
  1212
+    if (status != CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1213
+	goto done;
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
+    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
  1216
+    if (status != CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1217
+	goto done;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1218
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1219
+    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
  1220
+    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
  1221
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1222
+    /* 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
  1223
+    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
  1224
+		voffset += length) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1225
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1226
+	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
  1227
+	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
  1228
+	toffset += 2;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1229
+	if (length == 0xff) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1230
+	    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
  1231
+	    toffset +=2;
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
+	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
  1234
+	    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
  1235
+	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1236
+	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
  1237
+	    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
  1238
+	}
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
+    status = CKYSUCCESS;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1241
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1242
+done:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1243
+    CKYBuffer_FreeData(&tBuf);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1244
+    CKYBuffer_FreeData(&vBuf);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1245
+    return status;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1246
+}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1247
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1248
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1249
+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
  1250
+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
  1251
+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
  1252
+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
  1253
+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
  1254
+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
  1255
+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
  1256
+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
  1257
+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
  1258
+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
  1259
+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
  1260
+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
  1261
+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
  1262
+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
  1263
+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
  1264
+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
  1265
+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
  1266
+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
  1267
+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
  1268
+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
  1269
+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
  1270
+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
  1271
+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
  1272
+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
  1273
+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
  1274
+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
  1275
+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
  1276
+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
  1277
+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
  1278
+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
  1279
+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
  1280
+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
  1281
+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
  1282
+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
  1283
+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
  1284
+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
  1285
+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
  1286
+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
  1287
+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
  1288
+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
  1289
+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
  1290
+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
  1291
+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
  1292
+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
  1293
+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
  1294
+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
  1295
+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
  1296
+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
  1297
+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
  1298
+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
  1299
+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
  1300
+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
  1301
+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
  1302
+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
  1303
+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
  1304
+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
  1305
+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
  1306
+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
  1307
+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
  1308
+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
  1309
+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
  1310
+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
  1311
+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
  1312
+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
  1313
+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
  1314
+};
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1315
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1316
+static unsigned long 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1317
+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
  1318
+{
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1319
+    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
  1320
+    int i;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1321
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1322
+    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
  1323
+	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
  1324
+	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
  1325
+	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
  1326
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1327
+    return crc ^ 0xffffffff;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1328
+}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1329
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1330
+/*
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1331
+ * 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
  1332
+ * 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
  1333
+ * necessary.
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1334
+ */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1335
+static int 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1336
+decompress(CKYBuffer *out, 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1337
+			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
  1338
+{
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1339
+    int zret;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1340
+    CKYStatus status;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1341
+    z_stream stream;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1342
+    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
  1343
+    int outlen = 0;
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
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1346
+    /* allocate inflate state */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1347
+    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
  1348
+    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
  1349
+    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
  1350
+    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
  1351
+    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
  1352
+    zret = inflateInit(&stream);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1353
+    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
  1354
+        return zret;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1355
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1356
+    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
  1357
+    if (status != CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1358
+	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
  1359
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1360
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1361
+    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
  1362
+    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
  1363
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1364
+    do {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1365
+	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
  1366
+ 	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
  1367
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1368
+	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
  1369
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1370
+	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
  1371
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1372
+	/* 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
  1373
+	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
  1374
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1375
+	/* 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
  1376
+	switch (zret) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1377
+	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
  1378
+	    /* 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
  1379
+	     * 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
  1380
+	     * 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
  1381
+	     * a gzip header. */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1382
+	    /* 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
  1383
+	     * 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
  1384
+	     * 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
  1385
+	    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
  1386
+		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
  1387
+	    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1388
+	    /* 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
  1389
+	     * 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
  1390
+	     * the header. */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1391
+	    if ((outlen  & 0xffffffffL)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1392
+				!= 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
  1393
+		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
  1394
+	    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1395
+	    /* 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
  1396
+	     * 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
  1397
+	     */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1398
+	    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
  1399
+				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
  1400
+		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
  1401
+	    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1402
+ 	    /* 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
  1403
+	     * 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
  1404
+	    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
  1405
+	    break;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1406
+	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
  1407
+	    /* 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
  1408
+	     * 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
  1409
+	    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
  1410
+	    break;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1411
+	case Z_OK:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1412
+	    /* 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
  1413
+	     * 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
  1414
+	     * 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
  1415
+	    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
  1416
+		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
  1417
+	    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1418
+	    break;
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
+    } 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
  1421
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1422
+    /* cleanup */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1423
+    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
  1424
+	zret = Z_OK;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1425
+	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
  1426
+    } else {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1427
+	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
  1428
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1429
+    (void)inflateEnd(&stream);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1430
+    return zret;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1431
+}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1432
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1433
+/*
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1434
+ * 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
  1435
+ * 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
  1436
+ */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1437
+CKYStatus
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1438
+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
  1439
+{
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1440
+    CKYISOStatus apduRC;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1441
+    assert(mOldCAC);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1442
+    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
  1443
+}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1444
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1445
+void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1446
+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
  1447
+{
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1448
+    CKYStatus status = CKYSUCCESS;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1449
+    CKYBuffer cert;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1450
+    CKYBuffer rawCert;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1451
+    CKYBuffer shmCert;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1452
+    CKYSize  nextSize;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1453
+    CKYISOStatus apduRC;
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
+    OSTime time = OSTimeNow();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1456
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1457
+    CKYBuffer_InitEmpty(&cert);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1458
+    CKYBuffer_InitEmpty(&rawCert);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1459
+    CKYBuffer_InitEmpty(&shmCert);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1460
+
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
+    // 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
  1463
+    // 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
  1464
+    //
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1465
+    try {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1466
+        selectCACApplet(instance, false);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1467
+    } catch(PKCS11Exception& e) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1468
+	return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1469
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1470
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1471
+    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
  1472
+						 instance, OSTimeNow() - time);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1473
+
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
+    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
  1476
+ 	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
  1477
+	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
  1478
 	    /* 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
  1479
 	    return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1480
 	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1481
-    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1482
-    CKYBuffer_FreeData(&shmCert);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1483
+     } else {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1484
+	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
  1485
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1486
-    if (needRead) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1487
-	/* 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
  1488
-	if (instance == 0) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1489
-	    shmem.clearValid(0);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1490
-	    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
  1491
-	    shmem.setDataVersion(dataVersion);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1492
-	} else {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1493
-	    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
  1494
-						&nextSize, &apduRC);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1495
-	
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1496
-	    if (status != CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1497
-		/* 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
  1498
-		/* 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
  1499
-		if (instance == 2) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1500
-		    /* 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
  1501
-		     * is now valid */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1502
-		    shmem.setValid();
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
-		return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1505
-	    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1506
+	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
  1507
+	    /* 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
  1508
+	    return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1509
 	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1510
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1511
 	if (nextSize) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1512
-	    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
  1513
-						nextSize, &apduRC);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1514
+	    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
  1515
 	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1516
 	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
  1517
 						instance, OSTimeNow() - time);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1518
@@ -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
  1519
 	    handleConnectionError();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1520
 	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1521
 	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
  1522
-	if (instance == 2) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1523
-	    shmem.setValid();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1524
-	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1525
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1526
 
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
     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
  1529
 						instance, OSTimeNow() - time);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1530
-    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
  1531
-	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
  1532
-	CKYSize certSize = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1533
+    /* 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
  1534
+     * uncompress them */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1535
+    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
  1536
+	CKYOffset offset = 1;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1537
 	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
  1538
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1539
-	do {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1540
-	    guessFinalSize *= 2;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1541
-	    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
  1542
-	    if (status != CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1543
-		    break;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1544
+	/* 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
  1545
+	/* 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
  1546
+	 * 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
  1547
+	 * 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
  1548
+	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
  1549
+	    (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
  1550
+	    (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
  1551
+	    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
  1552
+	    /* 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
  1553
+	    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
  1554
+	    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
  1555
+		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
  1556
+		offset += len;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1557
+	    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1558
+	    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
  1559
+		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
  1560
+		    offset++;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1561
+		}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1562
+		offset++;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1563
+	    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1564
+	    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
  1565
+		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
  1566
+		    offset++;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1567
+		}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1568
+		offset++;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1569
+	    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1570
+	    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
  1571
+		offset += 2;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1572
 	    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1573
-	    certSize = guessFinalSize;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1574
-	    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
  1575
-			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
  1576
-	} 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
  1577
+	    offset -= 2;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1578
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1579
+	    /* 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
  1580
+	    /* 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
  1581
+	    /* 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
  1582
+	     * 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
  1583
+	     * 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
  1584
+	    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
  1585
+	    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
  1586
+	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1587
+	/* 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
  1588
+	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
  1589
+					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
  1590
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1591
 	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
  1592
 	    CKYBuffer_FreeData(&rawCert);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1593
 	    CKYBuffer_FreeData(&cert);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1594
 	    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
  1595
-				"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
  1596
+				"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
  1597
 	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1598
-	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
  1599
     } else {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1600
 	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
  1601
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1602
@@ -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
  1603
     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
  1604
 						instance, OSTimeNow() - time);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1605
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1606
-    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
  1607
-    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
  1608
-    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
  1609
+    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
  1610
+    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
  1611
+    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
  1612
+    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
  1613
     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
  1614
     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
  1615
     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
  1616
+    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
  1617
+        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
  1618
+    } else {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1619
+        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
  1620
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1621
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1622
     if (personName == NULL) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1623
 	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
  1624
@@ -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
  1625
 }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1626
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1627
 void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1628
+Slot::initCACShMem(void)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1629
+{
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1630
+    bool failed = false;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1631
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1632
+    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
  1633
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1634
+    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
  1635
+    /* 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
  1636
+    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
  1637
+				&& 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
  1638
+	CKYBuffer rawCert;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1639
+	CKYBuffer shmCert;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1640
+	CKYSize  nextSize;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1641
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1642
+        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
  1643
+						 firstCert);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1644
+	CKYBuffer_InitEmpty(&rawCert);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1645
+	CKYBuffer_InitEmpty(&shmCert);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1646
+
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
+	/* 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
  1649
+	 * in the chain */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1650
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1651
+	/* 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
  1652
+	try {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1653
+	    selectCACApplet(firstCert, false);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1654
+ 	} catch(PKCS11Exception& e) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1655
+	    failed = true;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1656
+            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
  1657
+							firstCert);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1658
+	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1659
+	if (!failed) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1660
+	    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
  1661
+	    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
  1662
+		failed = true;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1663
+                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
  1664
+			 				firstCert);
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
+	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1667
+	if (!failed) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1668
+	    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
  1669
+	    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
  1670
+	    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
  1671
+	    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
  1672
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1673
+	    if (shmCertSize >= certSize) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1674
+		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
  1675
+		    /* 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
  1676
+            	    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
  1677
+		    CKYBuffer_FreeData(&rawCert);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1678
+		    CKYBuffer_FreeData(&shmCert);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1679
+		    return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1680
+		}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1681
+            }		
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1682
+            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
  1683
+				" 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
  1684
+	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1685
+	CKYBuffer_FreeData(&rawCert);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1686
+	CKYBuffer_FreeData(&shmCert);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1687
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1688
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1689
+    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
  1690
+		" 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
  1691
+				firstCert);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1692
+    /* 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
  1693
+    shmem.clearValid(0);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1694
+    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
  1695
+    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
  1696
+}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1697
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1698
+void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1699
+Slot::verifyCACShMem(void)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1700
+{
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1701
+    /* 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
  1702
+    if (shmem.isValid()) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1703
+	return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1704
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1705
+    /* 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
  1706
+    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
  1707
+	shmem.clearValid(0);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1708
+	disconnect();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1709
+        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
  1710
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1711
+    /* 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
  1712
+    shmem.setValid(); 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1713
+}
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
+void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1716
 Slot::loadObjects()
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1717
 {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1718
     // 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
  1719
@@ -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
  1720
     list<ListObjectInfo> objInfoList;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1721
     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
  1722
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1723
-    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
  1724
-	loadCACCert(0);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1725
-	loadCACCert(1);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1726
-	loadCACCert(2);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1727
+    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
  1728
+	initCACShMem();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1729
+	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
  1730
+	   loadCACCert(i);
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
+	verifyCACShMem();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1733
 	status = trans.end();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1734
 	loadReaderObject();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1735
 	return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1736
@@ -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
  1737
 	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1738
 	return nonceValid;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1739
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1740
+    if (!needLogin) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1741
+	return true;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1742
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1743
     return loggedIn;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1744
 }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1745
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1746
@@ -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
  1747
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1748
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1749
     if (!isVersion1Key) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1750
+	pinCache.invalidate();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1751
 	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
  1752
     } else if (nonceValid) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1753
 	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
  1754
@@ -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
  1755
     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
  1756
     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
  1757
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1758
-    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
  1759
-	selectCACApplet(0);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1760
+    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
  1761
+	selectCACApplet(0, true);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1762
     } else {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1763
 	selectApplet();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1764
     }
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
     if (isVersion1Key) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1767
 	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
  1768
-    } 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
  1769
+    } 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
  1770
 	attemptCACLogin();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1771
     } else {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1772
 	oldAttemptLogin();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1773
@@ -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
  1774
     CKYISOStatus result;
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
     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
  1777
-		(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
  1778
+		(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
  1779
+		mCACLocalLogin, &result);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1780
     if( status == CKYSCARDERR ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1781
 	handleConnectionError();
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
     switch( result ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1784
       case CKYISO_SUCCESS:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1785
         break;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1786
-      case 6981:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1787
+      case 0x6981:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1788
+	pinCache.clearPin();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1789
         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
  1790
       default:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1791
+	pinCache.clearPin();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1792
 	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
  1793
             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
  1794
 	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1795
@@ -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
  1796
       case CKYISO_SUCCESS:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1797
         break;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1798
       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
  1799
+	pinCache.clearPin();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1800
         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
  1801
       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
  1802
+	pinCache.clearPin();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1803
         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
  1804
       default:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1805
+	pinCache.clearPin();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1806
         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
  1807
 								result);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1808
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1809
@@ -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
  1810
         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
  1811
     }
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
-    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
  1814
+    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
  1815
 	CACLogout();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1816
 	return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1817
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1818
@@ -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
  1819
     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
  1820
         ObjectHandleMatch(hObject));
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1821
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1822
-    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
  1823
+    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
  1824
         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
  1825
     }
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
@@ -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
  1828
 }
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
 void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1831
+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
  1832
+        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
  1833
+        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
  1834
+{
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
+    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
  1837
+    SessionHandleSuffix suffix;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1838
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1839
+    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
  1840
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1841
+    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
  1842
+
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
+void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1846
 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
  1847
 {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1848
     if( ! isValidSession(suffix) ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1849
@@ -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
  1850
     return keyNum & 0xFF;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1851
 }
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
+PKCS11Object::KeyType
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1854
+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
  1855
+{
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1856
+    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
  1857
+        ObjectHandleMatch(hKey));
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
+    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
  1860
+         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
  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
+    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
  1864
+        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
  1865
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1866
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1867
+    return iter->getKeyType();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1868
+}
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
 void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1871
 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
  1872
         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
@@ -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
  1874
     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
  1875
         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
  1876
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1877
-    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
  1878
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1879
+    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
  1880
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1881
+    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
  1882
 }
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
 void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1885
@@ -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
  1886
     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
  1887
         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
  1888
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1889
-    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
  1890
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1891
+    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
  1892
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1893
+    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
  1894
 }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1895
 
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
@@ -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
  1898
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1899
 }
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
+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
  1902
+  public:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1903
+    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
  1904
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1905
+    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
  1906
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1907
+    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
  1908
+        return session.keyAgreementState;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1909
+    }
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
+    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
  1912
+        return;
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
+    void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1916
+    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
  1917
+        return;
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
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1920
+};
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1921
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1922
+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
  1923
+  public:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1924
+    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
  1925
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1926
+    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
  1927
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1928
+    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
  1929
+        return session.signatureState;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1930
+    }
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
+    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
  1933
+        return;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1934
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1935
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1936
+    void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1937
+    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
  1938
+        return;
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
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1941
+};
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1942
+
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
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1945
+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
  1946
+  public:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1947
+    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
  1948
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1949
+    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
  1950
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1951
+    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
  1952
+        return session.signatureState;
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
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1955
+    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
  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
+    void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1960
+    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
  1961
+        /* 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
  1962
+  
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1963
+        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
  1964
+            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
  1965
+        }
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
+        CKYBuffer rawSignature;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1968
+        CKYBuffer_InitEmpty(&rawSignature); 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1969
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1970
+        DEREncodedSignature sig(paddedOutput);
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
+        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
  1973
+   
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1974
+        if (rv == CKYSUCCESS) { 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1975
+            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
  1976
+                             CKYBuffer_Size(&rawSignature));
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1977
+        } else {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1978
+            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
  1979
+        }
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
+        CKYBuffer_FreeData(&rawSignature);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1982
+
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
+};
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1986
+
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
 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
  1989
   public:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1990
     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
  1991
@@ -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
  1992
         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
  1993
         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
  1994
 {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1995
-    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
  1996
-        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
  1997
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1998
+    refreshTokenState();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  1999
+    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
  2000
+    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
  2001
+        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
  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
+    if (!isVersion1Key && ! isLoggedIn() ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2005
+        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
  2006
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2007
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2008
+    /* 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
  2009
+    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
  2010
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2011
+    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
  2012
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2013
+    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
  2014
+   
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2015
+    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
  2016
+        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
  2017
+    } 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2018
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2019
+    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
  2020
+        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
  2021
+        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
  2022
+            params);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2023
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2024
+    } 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
  2025
+        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
  2026
+        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
  2027
+            params);
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
 }
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
 void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2032
@@ -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
  2033
         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
  2034
         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
  2035
 {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2036
+    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
  2037
     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
  2038
-        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
  2039
+        params);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2040
 }
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
 void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2043
 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
  2044
         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
  2045
-        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
  2046
+        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
  2047
 {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2048
     refreshTokenState();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2049
     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
  2050
@@ -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
  2051
     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
  2052
     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
  2053
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2054
+    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
  2055
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2056
+    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
  2057
+        params.setKeySize(keySize);
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
     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
  2060
         // 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
  2061
         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
  2062
@@ -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
  2063
   	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2064
 	try {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2065
 	    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
  2066
-            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
  2067
+            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
  2068
+								keyNum, params.getDirection());
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2069
 	    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
  2070
 	    CKYBuffer_FreeData(&input);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2071
 	    CKYBuffer_FreeData(&inputPad);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2072
@@ -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
  2073
     return &nonce;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2074
 }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2075
 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2076
+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
  2077
+        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
  2078
+        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
  2079
+{
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2080
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2081
+    if( pulOutputLen == NULL ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2082
+        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
  2083
+            "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
  2084
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2085
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2086
+    refreshTokenState();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2087
+    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
  2088
+    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
  2089
+        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
  2090
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2091
+    /* 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
  2092
+       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
  2093
+       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
  2094
+       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
  2095
+    */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2096
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2097
+    if (!isVersion1Key && ! isLoggedIn() ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2098
+        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
  2099
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2100
+    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
  2101
+    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
  2102
+    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
  2103
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2104
+    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
  2105
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2106
+    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
  2107
+        params.setKeySize(keySize);
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
+    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
  2110
+	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
  2111
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2112
+        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
  2113
+            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
  2114
+        }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2115
+	if (ulInputLen > maxSize) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2116
+	    //pInput += ulInputLen - maxSize;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2117
+	    ulInputLen = maxSize;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2118
+	}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2119
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2120
+        CKYBuffer input;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2121
+        CKYBuffer output;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2122
+        CKYBuffer_InitEmpty(&output);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2123
+        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
  2124
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2125
+        if (status != CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2126
+            CKYBuffer_FreeData(&output);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2127
+            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
  2128
+        }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2129
+        try {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2130
+            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
  2131
+            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
  2132
+            CKYBuffer_FreeData(&input);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2133
+            CKYBuffer_FreeData(&output);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2134
+        } catch(PKCS11Exception& e) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2135
+            CKYBuffer_FreeData(&input);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2136
+            CKYBuffer_FreeData(&output);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2137
+            throw(e);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2138
+        }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2139
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2140
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2141
+    if( pOutput != NULL ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2142
+        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
  2143
+            *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
  2144
+            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
  2145
+        }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2146
+        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
  2147
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2148
+    *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
  2149
+}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2150
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2151
+void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2152
+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
  2153
+						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
  2154
+{
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2155
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2156
+    /* establish a transaction */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2157
+    Transaction trans;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2158
+    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
  2159
+    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
  2160
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2161
+    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
  2162
+	selectCACApplet(keyNum, true);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2163
+    } else {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2164
+	selectApplet();
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
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2167
+    CKYISOStatus result;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2168
+    int loginAttempted = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2169
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2170
+retry:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2171
+    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
  2172
+        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
  2173
+    } 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
  2174
+        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
  2175
+    } else {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2176
+        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
  2177
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2178
+    /* 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
  2179
+    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
  2180
+		(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
  2181
+	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
  2182
+    }
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
+    if (status != CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2185
+        if ( status == CKYSCARDERR ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2186
+            handleConnectionError();
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
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2189
+        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
  2190
+            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
  2191
+        }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2192
+        /* 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
  2193
+           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
  2194
+           version0 applets.
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
+        if (!isVersion1Key && !loginAttempted  &&
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2197
+                    (result == CKYISO_UNAUTHORIZED)) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2198
+            /* try to reauthenticate  */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2199
+	    try {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2200
+		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
  2201
+		    attemptCACLogin();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2202
+		} else {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2203
+		    oldAttemptLogin();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2204
+		}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2205
+            } catch(PKCS11Exception& ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2206
+                /* 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
  2207
+                  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
  2208
+                  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
  2209
+                  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
  2210
+                */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2211
+                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
  2212
+            }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2213
+            loginAttempted = true;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2214
+            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
  2215
+        }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2216
+        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
  2217
+                 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
  2218
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2219
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2220
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2221
+}
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
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2224
 void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2225
-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
  2226
+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
  2227
 					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
  2228
 {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2229
+
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
     // establish a transaction
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2232
     //
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2233
@@ -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
  2234
     //
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2235
     // select the applet
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
-    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
  2238
-	selectCACApplet(keyNum);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2239
+    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
  2240
+	selectCACApplet(keyNum, true);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2241
     } else {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2242
 	selectApplet();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2243
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2244
@@ -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
  2245
     CKYISOStatus result;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2246
     int loginAttempted = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2247
 retry:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2248
-    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
  2249
+    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
  2250
+        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
  2251
+    } 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
  2252
         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
  2253
     } else {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2254
         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
  2255
 		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
  2256
     } 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2257
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2258
+    /* 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
  2259
+    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
  2260
+	 (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
  2261
+	result = CKYISO_UNAUTHORIZED;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2262
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2263
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2264
     if (status != CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2265
 	if ( status == CKYSCARDERR ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2266
 	    handleConnectionError();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2267
@@ -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
  2268
 	// 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
  2269
 	// 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
  2270
         // version0 applets.
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2271
-	if (!isVersion1Key && !loginAttempted  && 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2272
+	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
  2273
 					(result == CKYISO_UNAUTHORIZED)) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2274
 	    // try to reauthenticate 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2275
 	    try {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2276
-		oldAttemptLogin();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2277
+		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
  2278
+		    attemptCACLogin();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2279
+		} else {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2280
+		    oldAttemptLogin();
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
 	    } catch(PKCS11Exception& ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2283
 		// 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
  2284
 		// 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
  2285
@@ -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
  2286
 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
  2287
         CK_ULONG ulDataLen)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2288
 {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2289
-    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
  2290
+    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
  2291
 	/* should throw unsupported */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2292
 	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
  2293
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2294
@@ -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
  2295
 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
  2296
         CK_ULONG ulDataLen)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2297
 {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2298
-    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
  2299
+    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
  2300
 	/* should throw unsupported */
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2301
 	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
  2302
     }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2303
@@ -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
  2304
 	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
  2305
     }
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
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2308
+#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
  2309
+unsigned int
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2310
+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
  2311
+{
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2312
+    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
  2313
+    int modSize = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2314
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2315
+    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
  2316
+        return keySize;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2317
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2318
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2319
+    ObjectConstIter iter;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2320
+    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
  2321
+        KeyNumMatch(keyNum,*this));
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2322
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2323
+    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
  2324
+        return keySize;
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
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2327
+    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
  2328
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2329
+    if(modulus) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2330
+        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
  2331
+        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
  2332
+            modSize--;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2333
+        }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2334
+        if(modSize > 0)
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2335
+            keySize = modSize * 8;
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
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2338
+    return keySize;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2339
+}
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
+unsigned int
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2342
+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
  2343
+{
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2344
+    return calcECCKeySize(keyNum);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2345
+} 
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
+unsigned int
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2348
+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
  2349
+{
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2350
+    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
  2351
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2352
+    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
  2353
+        return keySize;
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
+    ObjectConstIter iter;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2357
+    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
  2358
+        KeyNumMatch(keyNum,*this));
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2359
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2360
+    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
  2361
+        return keySize;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2362
+    }
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
+    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
  2365
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2366
+    if (eccParams == NULL) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2367
+        return keySize;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2368
+    }
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
+    /* 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
  2371
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2372
+    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
  2373
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2374
+    if ( ecParamsLen == 0 ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2375
+        return keySize;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2376
+    }
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
+/* 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
  2379
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2380
+    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
  2381
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2382
+    CKYByte curByte = 0;
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
+    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
  2385
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2386
+        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
  2387
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2388
+        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
  2389
+            continue;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2390
+        }
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
+        int match = 1;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2393
+        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
  2394
+            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
  2395
+            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
  2396
+                match = 0;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2397
+                break;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2398
+            }
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
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2401
+        if ( match == 1 ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2402
+            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
  2403
+            return keySize;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2404
+        }
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
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2407
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2408
+    return keySize;
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
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2411
+void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2412
+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
  2413
+        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
  2414
+        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
  2415
+{
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
+    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
  2418
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2419
+    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
  2420
+    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
  2421
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2422
+    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
  2423
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2424
+    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
  2425
+    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
  2426
+
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 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
  2430
+       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
  2431
+{
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2432
+    if (pMechanism == NULL ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2433
+        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
  2434
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2435
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2436
+    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
  2437
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2438
+    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
  2439
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2440
+    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
  2441
+        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
  2442
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2443
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2444
+    refreshTokenState();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2445
+    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
  2446
+    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
  2447
+        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
  2448
+    }
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
+     /* 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
  2451
+      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
  2452
+      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
  2453
+      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
  2454
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2455
+    if (!isVersion1Key && ! isLoggedIn() ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2456
+        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
  2457
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2458
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2459
+    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
  2460
+    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
  2461
+    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
  2462
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2463
+    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
  2464
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2465
+    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
  2466
+        params.setKeySize(keySize);
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
+    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
  2469
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2470
+    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
  2471
+    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
  2472
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2473
+    CKYBuffer secretKeyBuffer;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2474
+    CKYBuffer_InitEmpty(&secretKeyBuffer);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2475
+    CKYBuffer publicDataBuffer;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2476
+    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
  2477
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2478
+    if (status != CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2479
+        CKYBuffer_FreeData(&secretKeyBuffer);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2480
+        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
  2481
+    }
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
+    PKCS11Object *secret = NULL;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2484
+    *phKey = 0;
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
+    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
  2487
+        try {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2488
+            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
  2489
+			 keyNum, params.getKeySize());
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2490
+            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
  2491
+            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
  2492
+        } catch(PKCS11Exception& e) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2493
+            CKYBuffer_FreeData(&secretKeyBuffer);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2494
+            CKYBuffer_FreeData(&publicDataBuffer);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2495
+            throw(e);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2496
+        }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2497
+   }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2498
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2499
+   CKYBuffer_FreeData(&secretKeyBuffer);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2500
+   CKYBuffer_FreeData(&publicDataBuffer);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2501
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2502
+   if ( secret ) {
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
+       *phKey = secret->getHandle();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2505
+        delete secret;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2506
+   }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2507
+}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2508
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2509
+void
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2510
+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
  2511
+			     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
  2512
+{
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2513
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2514
+    Transaction trans;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2515
+    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
  2516
+    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
  2517
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2518
+    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
  2519
+	selectCACApplet(keyNum, true);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2520
+    } else {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2521
+	selectApplet();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2522
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2523
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2524
+    CKYISOStatus result;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2525
+    int loginAttempted = 0;
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
+retry:
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2528
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2529
+    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
  2530
+        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
  2531
+			secretKeyBuffer, &result);
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2532
+    } 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
  2533
+        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
  2534
+    } else {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2535
+    	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
  2536
+			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
  2537
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2538
+    /* 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
  2539
+    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
  2540
+		(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
  2541
+	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
  2542
+    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2543
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2544
+    if (status != CKYSUCCESS) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2545
+        if ( status == CKYSCARDERR ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2546
+            handleConnectionError();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2547
+        }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2548
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2549
+        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
  2550
+            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
  2551
+        }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2552
+        if (!isVersion1Key && !loginAttempted  &&
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2553
+            (result == CKYISO_UNAUTHORIZED)) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2554
+	    try {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2555
+		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
  2556
+		    attemptCACLogin();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2557
+		} else {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2558
+		    oldAttemptLogin();
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2559
+		}
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2560
+	    } catch(PKCS11Exception& ) {
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2561
+              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
  2562
+	    }
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2563
+	    loginAttempted = true;
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2564
+	    goto retry;
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
+        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
  2568
+                               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
  2569
+
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2570
+    } 
8e624b116c1d PSARC/2016/219 Coolkey PKCS#11 provider for smartcard devices
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
  2571
+}