Skip to content

Commit

Permalink
Merge pull request #115 from bellgrim/SUPPORT-155
Browse files Browse the repository at this point in the history
SUPPORT-155: auto_ptr is deprecated
  • Loading branch information
jschlyter committed Mar 21, 2015
2 parents fab362b + 07344f8 commit a923913
Show file tree
Hide file tree
Showing 18 changed files with 187 additions and 152 deletions.
1 change: 1 addition & 0 deletions NEWS
Expand Up @@ -12,6 +12,7 @@ Bugfixes:
* SOFTHSM-112: CKM_AES_KEY_WRAP* conflict in pkcs11.h.
* SOFTHSM-114: Fix memory leak in a test script.
* SUPPORT-154: A marked as non-modifiable object cannot be generated.
* SUPPORT-155: auto_ptr is deprecated in C++11, use unique_ptr.
* SUPPORT-157: Derived secrets were truncated after encryption and
could thus not be decrypted.
* Mutex should call MutexFactory wrapper functions.
Expand Down
90 changes: 52 additions & 38 deletions src/lib/SoftHSM.cpp
Expand Up @@ -68,46 +68,46 @@

#include <stdlib.h>

static CK_RV newP11Object(CK_OBJECT_CLASS objClass, CK_KEY_TYPE keyType, CK_CERTIFICATE_TYPE certType, std::auto_ptr< P11Object > &p11object)
static CK_RV newP11Object(CK_OBJECT_CLASS objClass, CK_KEY_TYPE keyType, CK_CERTIFICATE_TYPE certType, P11Object **p11object)
{
switch(objClass) {
case CKO_DATA:
p11object.reset( new P11DataObj);
*p11object = new P11DataObj();
break;
case CKO_CERTIFICATE:
if (certType == CKC_X_509)
p11object.reset( new P11X509CertificateObj );
*p11object = new P11X509CertificateObj();
else if (certType == CKC_OPENPGP)
p11object.reset( new P11OpenPGPPublicKeyObj );
*p11object = new P11OpenPGPPublicKeyObj();
else
return CKR_ATTRIBUTE_VALUE_INVALID;
break;
case CKO_PUBLIC_KEY:
if (keyType == CKK_RSA)
p11object.reset( new P11RSAPublicKeyObj );
*p11object = new P11RSAPublicKeyObj();
else if (keyType == CKK_DSA)
p11object.reset( new P11DSAPublicKeyObj );
*p11object = new P11DSAPublicKeyObj();
else if (keyType == CKK_EC)
p11object.reset( new P11ECPublicKeyObj );
*p11object = new P11ECPublicKeyObj();
else if (keyType == CKK_DH)
p11object.reset( new P11DHPublicKeyObj );
*p11object = new P11DHPublicKeyObj();
else if (keyType == CKK_GOSTR3410)
p11object.reset( new P11GOSTPublicKeyObj );
*p11object = new P11GOSTPublicKeyObj();
else
return CKR_ATTRIBUTE_VALUE_INVALID;
break;
case CKO_PRIVATE_KEY:
// we need to know the type too
if (keyType == CKK_RSA)
p11object.reset( new P11RSAPrivateKeyObj );
*p11object = new P11RSAPrivateKeyObj();
else if (keyType == CKK_DSA)
p11object.reset( new P11DSAPrivateKeyObj );
*p11object = new P11DSAPrivateKeyObj();
else if (keyType == CKK_EC)
p11object.reset( new P11ECPrivateKeyObj );
*p11object = new P11ECPrivateKeyObj();
else if (keyType == CKK_DH)
p11object.reset( new P11DHPrivateKeyObj );
*p11object = new P11DHPrivateKeyObj();
else if (keyType == CKK_GOSTR3410)
p11object.reset( new P11GOSTPrivateKeyObj );
*p11object = new P11GOSTPrivateKeyObj();
else
return CKR_ATTRIBUTE_VALUE_INVALID;
break;
Expand All @@ -120,34 +120,34 @@ static CK_RV newP11Object(CK_OBJECT_CLASS objClass, CK_KEY_TYPE keyType, CK_CERT
(keyType == CKK_SHA384_HMAC) ||
(keyType == CKK_SHA512_HMAC))
{
P11GenericSecretKeyObj* key = new P11GenericSecretKeyObj;
p11object.reset(key);
P11GenericSecretKeyObj* key = new P11GenericSecretKeyObj();
*p11object = key;
key->setKeyType(keyType);
}
else if (keyType == CKK_AES)
{
p11object.reset( new P11AESSecretKeyObj );
*p11object = new P11AESSecretKeyObj();
}
else if ((keyType == CKK_DES) ||
(keyType == CKK_DES2) ||
(keyType == CKK_DES3))
{
P11DESSecretKeyObj* key = new P11DESSecretKeyObj;
p11object.reset(key);
P11DESSecretKeyObj* key = new P11DESSecretKeyObj();
*p11object = key;
key->setKeyType(keyType);
}
else if (keyType == CKK_GOST28147)
{
p11object.reset( new P11GOSTSecretKeyObj );
*p11object = new P11GOSTSecretKeyObj();
}
else
return CKR_ATTRIBUTE_VALUE_INVALID;
break;
case CKO_DOMAIN_PARAMETERS:
if (keyType == CKK_DSA)
p11object.reset( new P11DSADomainObj );
*p11object = new P11DSADomainObj();
else if (keyType == CKK_DH)
p11object.reset( new P11DHDomainObj );
*p11object = new P11DHDomainObj();
else
return CKR_ATTRIBUTE_VALUE_INVALID;
break;
Expand Down Expand Up @@ -238,7 +238,7 @@ static CK_RV extractObjectInformation(CK_ATTRIBUTE_PTR pTemplate,
return CKR_OK;
}

static CK_RV newP11Object(OSObject *object, std::auto_ptr< P11Object > &p11object)
static CK_RV newP11Object(OSObject *object, P11Object **p11object)
{
CK_OBJECT_CLASS objClass = object->getUnsignedLongValue(CKA_CLASS, CKO_VENDOR_DEFINED);
CK_KEY_TYPE keyType = CKK_RSA;
Expand All @@ -250,7 +250,7 @@ static CK_RV newP11Object(OSObject *object, std::auto_ptr< P11Object > &p11objec
CK_RV rv = newP11Object(objClass,keyType,certType,p11object);
if (rv != CKR_OK)
return rv;
if (!p11object->init(object))
if (!(*p11object)->init(object))
return CKR_GENERAL_ERROR; // something went wrong that shouldn't have.
return CKR_OK;
}
Expand All @@ -273,14 +273,18 @@ static void libcleanup()
*****************************************************************************/

// Initialise the one-and-only instance
#ifdef HAVE_CXX11
std::unique_ptr<SoftHSM> SoftHSM::instance(nullptr);
#else
std::auto_ptr<SoftHSM> SoftHSM::instance(NULL);
#endif

// Return the one-and-only instance
SoftHSM* SoftHSM::i()
{
if (!instance.get())
{
instance = std::auto_ptr<SoftHSM>(new SoftHSM());
instance.reset(new SoftHSM());
}

return instance.get();
Expand Down Expand Up @@ -1413,8 +1417,8 @@ CK_RV SoftHSM::C_CopyObject(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject
}

// Get the new P11 object
std::auto_ptr< P11Object > newp11object;
rv = newP11Object(newobject,newp11object);
P11Object* newp11object = NULL;
rv = newP11Object(newobject,&newp11object);
if (rv != CKR_OK)
{
newobject->destroyObject();
Expand All @@ -1423,6 +1427,7 @@ CK_RV SoftHSM::C_CopyObject(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject

// Apply the template
rv = newp11object->saveTemplate(token, isPrivate != CK_FALSE, pTemplate, ulCount, OBJECT_OP_COPY);
delete newp11object;

if (rv != CKR_OK)
{
Expand Down Expand Up @@ -1545,13 +1550,15 @@ CK_RV SoftHSM::C_GetAttributeValue(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE

// Wrap a P11Object around the OSObject so we can access the attributes in the
// context of the object in which it is defined.
std::auto_ptr< P11Object > p11object;
rv = newP11Object(object,p11object);
P11Object* p11object = NULL;
rv = newP11Object(object,&p11object);
if (rv != CKR_OK)
return rv;

// Ask the P11Object to fill the template with attribute values.
return p11object->loadTemplate(token, pTemplate,ulCount);
rv = p11object->loadTemplate(token, pTemplate,ulCount);
delete p11object;
return rv;
}

// Change or set the value of the specified attributes on the specified object
Expand Down Expand Up @@ -1590,13 +1597,15 @@ CK_RV SoftHSM::C_SetAttributeValue(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE

// Wrap a P11Object around the OSObject so we can access the attributes in the
// context of the object in which it is defined.
std::auto_ptr< P11Object > p11object;
rv = newP11Object(object,p11object);
P11Object* p11object = NULL;
rv = newP11Object(object,&p11object);
if (rv != CKR_OK)
return rv;

// Ask the P11Object to save the template with attribute values.
return p11object->saveTemplate(token, isPrivate != CK_FALSE, pTemplate,ulCount,OBJECT_OP_SET);
rv = p11object->saveTemplate(token, isPrivate != CK_FALSE, pTemplate,ulCount,OBJECT_OP_SET);
delete p11object;
return rv;
}

// Initialise object search in the specified session using the specified attribute template as search parameters
Expand Down Expand Up @@ -8810,8 +8819,8 @@ CK_RV SoftHSM::CreateObject(CK_SESSION_HANDLE hSession, CK_ATTRIBUTE_PTR pTempla
return rv;
}

std::auto_ptr< P11Object > p11object;
rv = newP11Object(objClass,keyType,certType,p11object);
P11Object* p11object = NULL;
rv = newP11Object(objClass,keyType,certType,&p11object);
if (rv != CKR_OK)
return rv;

Expand All @@ -8826,14 +8835,19 @@ CK_RV SoftHSM::CreateObject(CK_SESSION_HANDLE hSession, CK_ATTRIBUTE_PTR pTempla
object = sessionObjectStore->createObject(slot->getSlotID(), hSession, isPrivate != CK_FALSE);
}

if (object == NULL) return CKR_GENERAL_ERROR;
if (!p11object->init(object)) return CKR_GENERAL_ERROR;
if (object == NULL || !p11object->init(object))
{
delete p11object;
return CKR_GENERAL_ERROR;
}

rv = p11object->saveTemplate(token, isPrivate != CK_FALSE, pTemplate,ulCount,op);
delete p11object;
if (rv != CKR_OK)
return rv;

if (isOnToken) {
if (isOnToken)
{
*phObject = handleManager->addTokenObject(slot->getSlotID(), isPrivate != CK_FALSE, object);
} else {
*phObject = handleManager->addSessionObject(slot->getSlotID(), hSession, isPrivate != CK_FALSE, object);
Expand Down
4 changes: 4 additions & 0 deletions src/lib/SoftHSM.h
Expand Up @@ -175,7 +175,11 @@ class SoftHSM
SoftHSM();

// The one-and-only instance
#ifdef HAVE_CXX11
static std::unique_ptr<SoftHSM> instance;
#else
static std::auto_ptr<SoftHSM> instance;
#endif

// Is the SoftHSM PKCS #11 library initialised?
bool isInitialised;
Expand Down
6 changes: 5 additions & 1 deletion src/lib/common/Configuration.cpp
Expand Up @@ -36,7 +36,11 @@
#include "log.h"

// Initialise the one-and-only instance
#ifdef HAVE_CXX11
std::unique_ptr<Configuration> Configuration::instance(nullptr);
#else
std::auto_ptr<Configuration> Configuration::instance(NULL);
#endif

// Add all valid configurations
const struct config Configuration::valid_config[] = {
Expand All @@ -51,7 +55,7 @@ Configuration* Configuration::i()
{
if (instance.get() == NULL)
{
instance = std::auto_ptr<Configuration>(new Configuration());
instance.reset(new Configuration());
}

return instance.get();
Expand Down
4 changes: 4 additions & 0 deletions src/lib/common/Configuration.h
Expand Up @@ -98,7 +98,11 @@ class Configuration
private:
Configuration();

#ifdef HAVE_CXX11
static std::unique_ptr<Configuration> instance;
#else
static std::auto_ptr<Configuration> instance;
#endif

std::map<std::string, std::string> stringConfiguration;
std::map<std::string, int> integerConfiguration;
Expand Down
10 changes: 7 additions & 3 deletions src/lib/common/MutexFactory.cpp
Expand Up @@ -60,11 +60,11 @@ bool Mutex::lock()
{
return (isValid && (MutexFactory::i()->LockMutex(handle) == CKR_OK));
}

// Unlock the mutex
void Mutex::unlock()
{
if (isValid)
if (isValid)
{
MutexFactory::i()->UnlockMutex(handle);
}
Expand Down Expand Up @@ -93,7 +93,11 @@ MutexLocker::~MutexLocker()
*****************************************************************************/

// Initialise the one-and-only instance
#ifdef HAVE_CXX11
std::unique_ptr<MutexFactory> MutexFactory::instance(nullptr);
#else
std::auto_ptr<MutexFactory> MutexFactory::instance(NULL);
#endif

// Constructor
MutexFactory::MutexFactory()
Expand All @@ -116,7 +120,7 @@ MutexFactory* MutexFactory::i()
{
if (!instance.get())
{
instance = std::auto_ptr<MutexFactory>(new MutexFactory());
instance.reset(new MutexFactory());
}

return instance.get();
Expand Down
6 changes: 5 additions & 1 deletion src/lib/common/MutexFactory.h
Expand Up @@ -49,7 +49,7 @@ class Mutex

// Lock the mutex
bool lock();

// Unlock the mutex
void unlock();

Expand Down Expand Up @@ -113,7 +113,11 @@ class MutexFactory
CK_RV UnlockMutex(CK_VOID_PTR mutex);

// The one-and-only instance
#ifdef HAVE_CXX11
static std::unique_ptr<MutexFactory> instance;
#else
static std::auto_ptr<MutexFactory> instance;
#endif

// The function pointers
CK_CREATEMUTEX createMutex;
Expand Down
6 changes: 5 additions & 1 deletion src/lib/common/SimpleConfigLoader.cpp
Expand Up @@ -50,14 +50,18 @@
#include "Configuration.h"

// Initialise the one-and-only instance
#ifdef HAVE_CXX11
std::unique_ptr<SimpleConfigLoader> SimpleConfigLoader::instance(nullptr);
#else
std::auto_ptr<SimpleConfigLoader> SimpleConfigLoader::instance(NULL);
#endif

// Return the one-and-only instance
SimpleConfigLoader* SimpleConfigLoader::i()
{
if (instance.get() == NULL)
{
instance = std::auto_ptr<SimpleConfigLoader>(new SimpleConfigLoader());
instance.reset(new SimpleConfigLoader());
}

return instance.get();
Expand Down
4 changes: 4 additions & 0 deletions src/lib/common/SimpleConfigLoader.h
Expand Up @@ -52,7 +52,11 @@ class SimpleConfigLoader : public ConfigLoader
char* trimString(char* text);
bool string2bool(std::string stringValue, bool* boolValue);

#ifdef HAVE_CXX11
static std::unique_ptr<SimpleConfigLoader> instance;
#else
static std::auto_ptr<SimpleConfigLoader> instance;
#endif
};

#endif // !_SOFTHSM_V2_SIMPLECONFIGLOADER_H
Expand Down

0 comments on commit a923913

Please sign in to comment.