heimdal: import heimdal's trunk svn rev 23697 + lorikeet-heimdal patches
[sfrench/samba-autobuild/.git] / source4 / heimdal / lib / hx509 / ks_p11.c
index 0d7c312c72413dfa2f462b699e0dc5bdba6145ec..19db6004ce401ec077cc568f5406a3bd8850fc98 100644 (file)
@@ -32,7 +32,7 @@
  */
 
 #include "hx_locl.h"
-RCSID("$Id: ks_p11.c 22071 2007-11-14 20:04:50Z lha $");
+RCSID("$Id$");
 #ifdef HAVE_DLFCN_H
 #include <dlfcn.h>
 #endif
@@ -65,7 +65,7 @@ struct p11_module {
     void *dl_handle;
     CK_FUNCTION_LIST_PTR funcs;
     CK_ULONG num_slots;
-    unsigned int refcount;
+    unsigned int ref;
     struct p11_slot *slot;
 };
 
@@ -309,7 +309,8 @@ p11_init_slot(hx509_context context,
     CK_SESSION_HANDLE session;
     CK_SLOT_INFO slot_info;
     CK_TOKEN_INFO token_info;
-    int ret, i;
+    size_t i;
+    int ret;
 
     slot->certs = NULL;
     slot->id = id;
@@ -502,7 +503,7 @@ iterate_entries(hx509_context context,
 {
     CK_OBJECT_HANDLE object;
     CK_ULONG object_count;
-    int ret, i;
+    int ret, ret2, i;
 
     ret = P11FUNC(p, FindObjectsInit, (session, search_data, num_search_data));
     if (ret != CKR_OK) {
@@ -556,13 +557,12 @@ iterate_entries(hx509_context context,
        query[i].pValue = NULL;
     }
 
-    ret = P11FUNC(p, FindObjectsFinal, (session));
-    if (ret != CKR_OK) {
-       return -2;
+    ret2 = P11FUNC(p, FindObjectsFinal, (session));
+    if (ret2 != CKR_OK) {
+       return ret2;
     }
 
-
-    return 0;
+    return ret;
 }
                
 static BIGNUM *
@@ -640,9 +640,11 @@ collect_private_key(hx509_context context,
     p11rsa->slot = slot;
     p11rsa->private_key = object;
     
-    p->refcount++;
-    if (p->refcount == 0)
-       _hx509_abort("pkcs11 refcount to high");
+    if (p->ref == 0)
+       _hx509_abort("pkcs11 ref == 0 on alloc");
+    p->ref++;
+    if (p->ref == UINT_MAX)
+       _hx509_abort("pkcs11 ref == UINT_MAX on alloc");
 
     RSA_set_method(rsa, &p11_rsa_pkcs1_method);
     ret = RSA_set_app_data(rsa, p11rsa);
@@ -695,9 +697,11 @@ collect_cert(hx509_context context,
     if (ret)
        return ret;
 
-    p->refcount++;
-    if (p->refcount == 0)
-       _hx509_abort("pkcs11 refcount to high");
+    if (p->ref == 0)
+       _hx509_abort("pkcs11 ref == 0 on alloc");
+    p->ref++;
+    if (p->ref == UINT_MAX)
+       _hx509_abort("pkcs11 ref to high");
 
     _hx509_cert_set_release(cert, p11_cert_release, p);
 
@@ -808,7 +812,7 @@ p11_init(hx509_context context,
        return ENOMEM;
     }
 
-    p->refcount = 1;
+    p->ref = 1;
 
     str = strchr(list, ',');
     if (str)
@@ -934,9 +938,9 @@ p11_release_module(struct p11_module *p)
 {
     int i;
 
-    if (p->refcount == 0)
-       _hx509_abort("pkcs11 refcount to low");
-    if (--p->refcount > 0)
+    if (p->ref == 0)
+       _hx509_abort("pkcs11 ref to low");
+    if (--p->ref > 0)
        return;
 
     for (i = 0; i < p->num_slots; i++) {