[go: up one dir, main page]

File: locking.cpp

package info (click to toggle)
coolkey 1.1.0-6
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,156 kB
  • ctags: 2,124
  • sloc: sh: 8,397; cpp: 6,598; ansic: 3,991; makefile: 181
file content (133 lines) | stat: -rw-r--r-- 3,457 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* ***** BEGIN COPYRIGHT BLOCK *****
 * Copyright (C) 2005 Red Hat, Inc.
 * All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation version
 * 2.1 of the License.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 * ***** END COPYRIGHT BLOCK *****/

#include <assert.h>
#include "mypkcs11.h"
#include <string>
#include "locking.h"
#include "log.h"
#include "PKCS11Exception.h"

class BasicMutex : public Mutex {
  public:
    BasicMutex(CK_C_INITIALIZE_ARGS *initArgs);
    virtual ~BasicMutex();

    virtual void lock();
    virtual void unlock();

  private:
    void *mutex;
    CK_C_INITIALIZE_ARGS *initArgs;
};

class DummyMutex : public Mutex {
  public:
    virtual ~DummyMutex() { }
    virtual void lock() { }
    virtual void unlock() { }
};

BasicMutex::BasicMutex(CK_C_INITIALIZE_ARGS *initArgs_)
    : initArgs(initArgs_)
{
    assert( initArgs->CreateMutex != NULL );
    assert( initArgs->DestroyMutex != NULL );
    assert( initArgs->LockMutex != NULL );
    assert( initArgs->UnlockMutex != NULL );

    CK_RV crv = initArgs->CreateMutex(&mutex);

    if( crv != CKR_OK ) {
        throw PKCS11Exception(crv, "CreateMutex");
    }
}

BasicMutex::~BasicMutex()
{
    CK_RV crv = initArgs->DestroyMutex(mutex);

    if( crv != CKR_OK ) {
        throw PKCS11Exception(crv, "DestroyMutex");
    }
}

void
BasicMutex::lock()
{
    CK_RV crv = initArgs->LockMutex(mutex);

    assert(crv != CKR_MUTEX_BAD);
    if( crv != CKR_OK ) {
        throw PKCS11Exception(crv, "LockMutex");
    }
}

void
BasicMutex::unlock()
{
    CK_RV crv = initArgs->UnlockMutex(mutex);

    assert(crv != CKR_MUTEX_BAD);
    assert(crv != CKR_MUTEX_NOT_LOCKED);
    if( crv != CKR_OK ) {
        throw PKCS11Exception(crv, "UnlockMutex");
    }
}

MutexFactory::MutexFactory(const CK_C_INITIALIZE_ARGS* initArgs_)
    : initArgs(NULL)
{
    if( initArgs_ != NULL ) {
        if( initArgs_->CreateMutex == NULL || initArgs_->DestroyMutex == NULL
            || initArgs_->LockMutex == NULL || initArgs_->UnlockMutex == NULL )
        {
            if( initArgs_->flags & CKF_OS_LOCKING_OK ) {
                // application wants us to lock with OS primitives, which
                // we can't do
                throw PKCS11Exception(CKR_CANT_LOCK,
                    "Library cannot use OS locking primitives");
            } else {
                // Application is single threaded, so we won't do any
                // locking. Leave initArgs == NULL.
            }
        } else {
            // use the provided primitives for locking
            initArgs = new CK_C_INITIALIZE_ARGS(*initArgs_);
        }
    }
}

MutexFactory::~MutexFactory()
{
    if( initArgs != NULL ) {
        delete initArgs;
    }
}

Mutex*
MutexFactory::createMutex() const
{
    if( initArgs == NULL ) {
        return new DummyMutex();
    } else {
        return new BasicMutex(initArgs);
    }
}