aboutsummaryrefslogtreecommitdiffstats
path: root/SoftHSMv2/src/lib/test/TestsNoPINInitBase.cpp
blob: f5bb0662378ebe6c94d95fbc1ef29d2f96597ea3 (plain)
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

@media only all and (prefers-color-scheme: dark) {
.highlight .hll { background-color: #49483e }
.highlight .c { color: #75715e } /* Comment */
.highlight .err { color: #960050; background-color: #1e0010 } /* Error */
.highlight .k { color: #66d9ef } /* Keyword */
.highlight .l { color: #ae81ff } /* Literal */
.highlight .n { color: #f8f8f2 } /* Name */
.highlight .o { color: #f92672 } /* Operator */
.highlight .p { color: #f8f8f2 } /* Punctuation */
.highlight .ch { color: #75715e } /* Comment.Hashbang */
.highlight .cm { color: #75715e } /* Comment.Multiline */
.highlight .cp { color: #75715e } /* Comment.Preproc */
.highlight .cpf { color: #75715e } /* Comment.PreprocFile */
.highlight .c1 { color: #75715e } /* Comment.Single */
.highlight .cs { color: #75715e } /* Comment.Special */
.highlight .gd { color: #f92672 } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gi { color: #a6e22e } /* Generic.Inserted */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #75715e } /* Generic.Subheading */
.highlight .kc { color: #66d9ef } /* Keyword.Constant */
.highlight .kd { color: #66d9ef } /* Keyword.Declaration */
.highlight .kn { color: #f92672 } /* Keyword.Namespace */
.highlight .kp { color: #66d9ef } /* Keyword.Pseudo */
.highlight .kr { color: #66d9ef } /* Keyword.Reserved */
.highlight .kt { color: #66d9ef } /* Keyword.Type */
.highlight .ld { color: #e6db74 } /* Literal.Date */
.highlight .m { color: #ae81ff } /* Literal.Number */
.highlight .s { color: #e6db74 } /* Literal.String */
.highlight .na { color: #a6e22e } /* Name.Attribute */
.highlight .nb { color: #f8f8f2 } /* Name.Builtin */
.highlight .nc { color: #a6e22e } /* Name.Class */
.highlight .no { color: #66d9ef } /* Name.Constant */
.highlight .nd { color: #a6e22e } /* Name.Decorator */
.highlight .ni { color: #f8f8f2 } /* Name.Entity */
.highlight .ne { color: #a6e22e } /* Name.Exception */
.highlight .nf { color: #a6e22e } /* Name.Function */
.highlight .nl { color: #f8f8f2 } /* Name.Label */
.highlight .nn { color: #f8f8f2 } /* Name.Namespace */
.highlight .nx { color: #a6e22e } /* Name.Other */
.highlight .py { color: #f8f8f2 } /* Name.Property */
.highlight .nt { color: #f92672 } /* Name.Tag */
.highlight .nv { color: #f8f8f2 } /* Name.Variable */
.highlight .ow { color: #f92672 } /* Operator.Word */
.highlight .w { color: #f8f8f2 } /* Text.Whitespace */
.highlight .mb { color: #ae81ff } /* Literal.Number.Bin */
.highlight .mf { color: #ae81ff } /* Literal.Number.Float */
.highlight .mh { color: #ae81ff } /* Literal.Number.Hex */
.highlight .mi { color: #ae81ff } /* Literal.Number.Integer */
.highlight .mo { color: #ae81ff } /* Literal.Number.Oct */
.highlight .sa { color: #e6db74 } /* Literal.String.Affix */
.highlight .sb { color: #e6db74 } /* Literal.String.Backtick */
.highlight .sc { color: #e6db74 } /* Literal.String.Char */
.highlight .dl { color: #e6db74 } /* Literal.String.Delimiter */
.highlight .sd { color: #e6db74 } /* Literal.String.Doc */
.highlight .s2 { color: #e6db74 } /* Literal.String.Double */
.highlight .se { color: #ae81ff } /* Literal.String.Escape */
.highlight .sh { color: #e6db74 } /* Literal.String.Heredoc */
.highlight .si { color: #e6db74 } /* Literal.String.Interpol */
.highlight .sx { color: #e6db74 } /* Literal.String.Other */
.highlight .sr { color: #e6db74 } /* Literal.String.Regex */
.highlight .s1 { color: #e6db74 } /* Literal.String.Single */
.highlight .ss { color: #e6db74 } /* Literal.String.Symbol */
.highlight .bp { color: #f8f8f2 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #a6e22e } /* Name.Function.Magic */
.highlight .vc { color: #f8f8f2 } /* Name.Variable.Class */
.highlight .vg { color: #f8f8f2 } /* Name.Variable.Global */
.highlight .vi { color: #f8f8f2 } /* Name.Variable.Instance */
.highlight .vm { color: #f8f8f2 } /* Name.Variable.Magic */
.highlight .il { color: #ae81ff } /* Literal.Number.Integer.Long */
}
@med
/*
 * Copyright (c) 2010 .SE (The Internet Infrastructure Foundation)
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*****************************************************************************
 TestsNoPINInitBase.cpp

 Base class for test classes. Used when there is no need for user login.
 *****************************************************************************/

#include "TestsNoPINInitBase.h"
#include <cstring>
#include <cppunit/extensions/HelperMacros.h>
#include <vector>
#include <sstream>

#ifdef P11M
#ifdef _WIN32
CK_FUNCTION_LIST_PTR FunctionList::getFunctionListPtr(const char*const libName,  HINSTANCE__* p11Library, const char*getFunctionList) {
#else
#include <dlfcn.h>

static CK_FUNCTION_LIST_PTR getFunctionListPtr(const char*const libName, void *const p11Library, const char*getFunctionList) {
#endif //_WIN32
	CPPUNIT_ASSERT_MESSAGE(libName, p11Library);
#ifdef _WIN32
	const CK_C_GetFunctionList pGFL( (CK_C_GetFunctionList)GetProcAddress(
			p11Library,
			getFunctionList.c_str()
	) );
#else
	const CK_C_GetFunctionList pGFL( (CK_C_GetFunctionList)dlsym(
			p11Library,
			getFunctionList
	) );
#endif //_WIN32
	CPPUNIT_ASSERT_MESSAGE(libName, pGFL);
	CK_FUNCTION_LIST_PTR ptr(NULL_PTR);
	const CK_RV retCode( pGFL(&ptr) );
	if ( !ptr && (retCode)!=CKR_OK) {
		std::ostringstream oss;
		oss << "C_GetFunctionList failed...error no = 0x" << std::hex << retCode << " libName '" << libName << "'.";
		CPPUNIT_ASSERT_MESSAGE(oss.str(), false);
	}
	return ptr;
}
#endif //P11M
void TestsNoPINInitBase::getSlotIDs() {
	bool hasFoundFree(false);
	bool hasFoundInitialized(false);
	CK_ULONG nrOfSlots;
	CPPUNIT_ASSERT( CRYPTOKI_F_PTR( C_GetSlotList(CK_TRUE, NULL_PTR, &nrOfSlots)==CKR_OK ) );
	std::vector<CK_SLOT_ID> slotIDs(nrOfSlots);
	CPPUNIT_ASSERT( CRYPTOKI_F_PTR( C_GetSlotList(CK_TRUE, &slotIDs.front(), &nrOfSlots)==CKR_OK ) );
	for ( std::vector<CK_SLOT_ID>::iterator i=slotIDs.begin(); i!=slotIDs.end(); i++ ) {
		CK_TOKEN_INFO tokenInfo;
		CPPUNIT_ASSERT( CRYPTOKI_F_PTR( C_GetTokenInfo(*i, &tokenInfo)==CKR_OK ) );
		if ( tokenInfo.flags&CKF_TOKEN_INITIALIZED ) {
			if ( !hasFoundInitialized ) {
				hasFoundInitialized = true;
				m_initializedTokenSlotID = *i;
			}
		} else {
			if ( !hasFoundFree ) {
				hasFoundFree = true;
				m_notInitializedTokenSlotID = *i;
			}
		}
	}
	if ( !hasFoundInitialized ) {
		m_initializedTokenSlotID = m_notInitializedTokenSlotID;
	}
}

TestsNoPINInitBase::TestsNoPINInitBase() :
#ifdef P11M
#ifdef _WIN32
		p11Library( LoadLibrary(libName.c_str()) ),
#else
		p11Library( dlopen(P11M, RTLD_LAZY) ),
#endif
		m_ptr(getFunctionListPtr(P11M, p11Library, "C_GetFunctionList")),
#endif
		m_invalidSlotID(((CK_SLOT_ID)1<<31)),
		m_initializedTokenSlotID(m_invalidSlotID),
		m_notInitializedTokenSlotID(m_invalidSlotID),
		m_soPin1((CK_UTF8CHAR_PTR)"12345678"),
		m_soPin1Length(strlen((char*)m_soPin1)),
		m_userPin1((CK_UTF8CHAR_PTR)"1234"),
		m_userPin1Length(strlen((char*)m_userPin1)) {};

void TestsNoPINInitBase::setUp() {
	CK_UTF8CHAR label[32];
	memset(label, ' ', 32);
	memcpy(label, "token1", strlen("token1"));

	// initialize cryptoki
	CPPUNIT_ASSERT_EQUAL( (CK_RV)CKR_OK, CRYPTOKI_F_PTR( C_Initialize(NULL_PTR) ) );
	// update slot IDs to initialized and not initialized token.
	getSlotIDs();
	// (Re)initialize the token
	CPPUNIT_ASSERT_EQUAL( (CK_RV)CKR_OK, CRYPTOKI_F_PTR( C_InitToken(m_initializedTokenSlotID, m_soPin1, m_soPin1Length, label) ) );
	// Reset cryptoki to get new slot IDs.
	CPPUNIT_ASSERT_EQUAL( (CK_RV)CKR_OK, CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) ) );
	CPPUNIT_ASSERT_EQUAL( (CK_RV)CKR_OK, CRYPTOKI_F_PTR( C_Initialize(NULL_PTR) ) );
	// slot IDs must be updated since the ID of the initialized token has changed.
	getSlotIDs();
}

void TestsNoPINInitBase::tearDown() {
	const CK_RV result(CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) ) );
	if ( result==CKR_OK||result==CKR_CRYPTOKI_NOT_INITIALIZED ) {
		return;
	}
	std::ostringstream oss;
	oss << "C_Finalize failed with CK_RV: " << std::hex << result;
	CPPUNIT_ASSERT_MESSAGE(oss.str(), false);
}

#ifdef P11M
TestsNoPINInitBase::~TestsNoPINInitBase() {
	if ( !p11Library ) {
		return;
	}
#ifdef _WIN32
	FreeLibrary(p11Library);
#else
	dlclose(p11Library);
#endif // _WIN32
}

void softHSMLog(const int, const char*, const char*, const int, const char*, ...)
{

}
#else
TestsNoPINInitBase::~TestsNoPINInitBase() {}
#endif // P11M