Convert constant SC_PKCS11_MAX_VIRTUAL_SLOTS to configuration option.

Alon Bar-Lev

svn diff -r 3283:3284 
https://www.opensc-project.org/svn/opensc/branches/alonbl/pkcs11-slots

---

Index: pkcs11-slots/src/pkcs11/pkcs11-global.c
===================================================================
--- pkcs11-slots/src/pkcs11/pkcs11-global.c     (revision 3283)
+++ pkcs11-slots/src/pkcs11/pkcs11-global.c     (revision 3284)
@@ -31,7 +31,7 @@
 
 sc_context_t *context = NULL;
 struct sc_pkcs11_pool session_pool;
-struct sc_pkcs11_slot virtual_slots[SC_PKCS11_MAX_VIRTUAL_SLOTS];
+struct sc_pkcs11_slot *virtual_slots = NULL;
 struct sc_pkcs11_card card_table[SC_PKCS11_MAX_READERS];
 struct sc_pkcs11_config sc_pkcs11_conf;
 
@@ -178,10 +178,8 @@
        }
 
        rv = sc_pkcs11_init_lock((CK_C_INITIALIZE_ARGS_PTR) pInitArgs);
-       if (rv != CKR_OK)   {
-               sc_release_context(context);
-               context = NULL;
-       }
+       if (rv != CKR_OK)
+               goto out;
 
        /* set context options */
        memset(&ctx_opts, 0, sizeof(sc_context_param_t));
@@ -199,8 +197,13 @@
        load_pkcs11_parameters(&sc_pkcs11_conf, context);
 
        first_free_slot = 0;
+       virtual_slots = malloc(sizeof (*virtual_slots) * 
sc_pkcs11_conf.pkcs11_max_virtual_slots);
+       if (virtual_slots == NULL) {
+               rv = CKR_HOST_MEMORY;
+               goto out;
+       }
        pool_initialize(&session_pool, POOL_TYPE_SESSION);
-       for (i=0; i<SC_PKCS11_MAX_VIRTUAL_SLOTS; i++)
+       for (i=0; i<sc_pkcs11_conf.pkcs11_max_virtual_slots; i++)
                slot_initialize(i, &virtual_slots[i]);
        for (i=0; i<SC_PKCS11_MAX_READERS; i++)
                card_initialize(i);
@@ -211,6 +214,16 @@
 out:   
        if (context != NULL)
                sc_debug(context, "C_Initialize: result = %d\n", rv);
+
+       if (rv != CKR_OK) {
+               if (context != NULL) {
+                       sc_release_context(context);
+                       context = NULL;
+               }
+               /* Release and destroy the mutex */
+               sc_pkcs11_free_lock();
+       }
+
        return rv;
 }
 
@@ -232,6 +245,11 @@
        for (i=0; i < (int)sc_ctx_get_reader_count(context); i++)
                card_removed(i);
 
+       if (virtual_slots) {
+               free(virtual_slots);
+               virtual_slots = NULL;
+       }
+
        sc_release_context(context);
        context = NULL;
 
@@ -285,7 +303,7 @@
                    CK_SLOT_ID_PTR pSlotList,     /* receives the array of slot 
IDs */
                    CK_ULONG_PTR   pulCount)      /* receives the number of 
slots */
 {
-       CK_SLOT_ID found[SC_PKCS11_MAX_VIRTUAL_SLOTS];
+       CK_SLOT_ID found[sc_pkcs11_conf.pkcs11_max_virtual_slots];
        int i;
        CK_ULONG numMatches;
        sc_pkcs11_slot_t *slot;
@@ -304,7 +322,7 @@
        card_detect_all();
 
        numMatches = 0;
-       for (i=0; i<SC_PKCS11_MAX_VIRTUAL_SLOTS; i++) {
+       for (i=0; i<sc_pkcs11_conf.pkcs11_max_virtual_slots; i++) {
                slot = &virtual_slots[i];
 
                if (!tokenPresent || (slot->slot_info.flags & 
CKF_TOKEN_PRESENT))
Index: pkcs11-slots/src/pkcs11/sc-pkcs11.h
===================================================================
--- pkcs11-slots/src/pkcs11/sc-pkcs11.h (revision 3283)
+++ pkcs11-slots/src/pkcs11/sc-pkcs11.h (revision 3284)
@@ -60,7 +60,7 @@
 extern "C" {
 #endif
 
-#define SC_PKCS11_MAX_VIRTUAL_SLOTS    8
+#define SC_PKCS11_DEF_MAX_VIRTUAL_SLOTS        8
 #define SC_PKCS11_DEF_SLOTS_PER_CARD   4
 #define SC_PKCS11_MAX_READERS           SC_MAX_READERS
 
@@ -90,6 +90,7 @@
 };
 
 struct sc_pkcs11_config {
+       unsigned int pkcs11_max_virtual_slots;
        unsigned int num_slots;
        unsigned char hide_empty_tokens;
        unsigned char lock_login;
@@ -335,7 +336,7 @@
 /* Module variables */
 extern struct sc_context *context;
 extern struct sc_pkcs11_pool session_pool;
-extern struct sc_pkcs11_slot virtual_slots[SC_PKCS11_MAX_VIRTUAL_SLOTS];
+extern struct sc_pkcs11_slot *virtual_slots;
 extern struct sc_pkcs11_card card_table[SC_PKCS11_MAX_READERS];
 extern struct sc_pkcs11_config sc_pkcs11_conf;
 extern unsigned int first_free_slot;
Index: pkcs11-slots/src/pkcs11/slot.c
===================================================================
--- pkcs11-slots/src/pkcs11/slot.c      (revision 3283)
+++ pkcs11-slots/src/pkcs11/slot.c      (revision 3284)
@@ -63,8 +63,8 @@
        else
                avail = sc_pkcs11_conf.num_slots;
 
-       if (first_free_slot + avail > SC_PKCS11_MAX_VIRTUAL_SLOTS)
-               avail = SC_PKCS11_MAX_VIRTUAL_SLOTS - first_free_slot;
+       if (first_free_slot + avail > sc_pkcs11_conf.pkcs11_max_virtual_slots)
+               avail = sc_pkcs11_conf.pkcs11_max_virtual_slots - 
first_free_slot;
        card->first_slot = first_free_slot;
        card->max_slots = avail;
        card->num_slots = 0;
@@ -165,7 +165,7 @@
        if (!report_events) {
                CK_SLOT_ID id;
 
-               for (id = 0; id < SC_PKCS11_MAX_VIRTUAL_SLOTS; id++)
+               for (id = 0; id < sc_pkcs11_conf.pkcs11_max_virtual_slots; id++)
                        virtual_slots[id].events = 0;
        }
 
@@ -184,7 +184,7 @@
 
        sc_debug(context, "%d: smart card removed\n", reader);
 
-       for (i=0; i<SC_PKCS11_MAX_VIRTUAL_SLOTS; i++) {
+       for (i=0; i<sc_pkcs11_conf.pkcs11_max_virtual_slots; i++) {
                if (virtual_slots[i].card &&
                    virtual_slots[i].card->reader == reader)
                                slot_token_removed(i);
@@ -245,7 +245,7 @@
        if (context == NULL)
                return CKR_CRYPTOKI_NOT_INITIALIZED;
 
-       if (id < 0 || id >= SC_PKCS11_MAX_VIRTUAL_SLOTS)
+       if (id < 0 || id >= sc_pkcs11_conf.pkcs11_max_virtual_slots)
                return CKR_SLOT_ID_INVALID;
                
        *slot = &virtual_slots[id];
@@ -324,7 +324,7 @@
        CK_SLOT_ID id;
 
        card_detect_all();
-       for (id = 0; id < SC_PKCS11_MAX_VIRTUAL_SLOTS; id++) {
+       for (id = 0; id < sc_pkcs11_conf.pkcs11_max_virtual_slots; id++) {
                slot = &virtual_slots[id];
                if ((slot->events & SC_EVENT_CARD_INSERTED)
                 && !(slot->slot_info.flags & CKF_TOKEN_PRESENT))
Index: pkcs11-slots/src/pkcs11/misc.c
===================================================================
--- pkcs11-slots/src/pkcs11/misc.c      (revision 3283)
+++ pkcs11-slots/src/pkcs11/misc.c      (revision 3284)
@@ -317,6 +317,7 @@
        int i;
        
        /* Set defaults */
+       conf->pkcs11_max_virtual_slots = SC_PKCS11_DEF_MAX_VIRTUAL_SLOTS;
        conf->num_slots = SC_PKCS11_DEF_SLOTS_PER_CARD;
        conf->hide_empty_tokens = 0;
        conf->lock_login = 0;
@@ -335,6 +336,7 @@
        if (!conf_block)
                return;
 
+       conf->pkcs11_max_virtual_slots = scconf_get_int(conf_block, 
"max_virtual_slots", conf->pkcs11_max_virtual_slots);
        conf->num_slots = scconf_get_int(conf_block, "num_slots", 
conf->num_slots);
        conf->hide_empty_tokens = scconf_get_bool(conf_block, 
"hide_empty_tokens", 0);
        conf->lock_login = scconf_get_bool(conf_block, "lock_login", 0);
Index: pkcs11-slots/etc/opensc.conf.in
===================================================================
--- pkcs11-slots/etc/opensc.conf.in     (revision 3283)
+++ pkcs11-slots/etc/opensc.conf.in     (revision 3284)
@@ -297,13 +297,14 @@
 # Parameters for the OpenSC PKCS11 module
 app opensc-pkcs11 {
        pkcs11 {
+               # Maximum Number of virtual slots.
+               # If there are more slots than defined here,
+               # the remaining slots will be hidden from PKCS#11.
+               max_virtual_slots = 8;
+
                # Maximum number of slots per smart card.
                # If the card has fewer keys than defined here,
                # the remaining number of slots will be empty.
-               #
-               # Note that there is currently a compile time
-               # maximum on the overall number of slots
-               # the pkcs11 module is able to handle.
                num_slots = 4;
 
                # Normally, the pkcs11 module will create
_______________________________________________
opensc-devel mailing list
opensc-devel@lists.opensc-project.org
http://www.opensc-project.org/mailman/listinfo/opensc-devel

Reply via email to