2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
17 * @file scheme-test.cpp
18 * @author Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
21 #include <scheme-test.h>
23 #include <sys/smack.h>
24 #include <sys/types.h>
26 #include <sys/sendfile.h>
34 #include <boost/test/unit_test.hpp>
36 #include <smack-access.h>
38 #include <db-crypto.h>
39 #include <file-system.h>
40 #include <key-provider.h>
42 #include <crypto-init.h>
48 const uid_t UID = 7654;
49 const gid_t GID = 7654;
50 const char* const DBPASS = "db-pass";
51 const char* const LABEL = "my-label";
52 const Label DB_LABEL = "/" + string(LABEL);
53 const int ENC_SCHEME_OFFSET = 24;
54 const string TEST_DATA_STR = "test-data";
55 RawBuffer TEST_DATA(TEST_DATA_STR.begin(), TEST_DATA_STR.end());
56 const Password TEST_PASS = "custom user password";
57 const size_t IV_LEN = 16;
58 const size_t CHAIN_LEN = 3;
59 const uid_t CKM_UID = 0;
60 const gid_t CKM_GID = 0;
72 // [password][exportable]
73 Policy policy[2][2] = {
74 {{ Password(), false }, { Password(), true }},
75 {{ TEST_PASS, false }, { TEST_PASS, true }},
89 { Group::SINGLE_ITEM, {
90 Item("data-alias1", DataType::BINARY_DATA, policy[NO_PASS][EXP])
92 { Group::SINGLE_ITEM, {
93 Item("data-alias2", DataType::BINARY_DATA, policy[PASS][EXP])
97 { Group::KEY_PAIR_RSA, {
98 Item("key-rsa-alias-prv1", DataType::KEY_RSA_PRIVATE, policy[NO_PASS][NO_EXP]),
99 Item("key-rsa-alias-pub1", DataType::KEY_RSA_PUBLIC, policy[NO_PASS][NO_EXP])
101 { Group::KEY_PAIR_RSA, {
102 Item("key-rsa-alias-prv2", DataType::KEY_RSA_PRIVATE, policy[NO_PASS][EXP]),
103 Item("key-rsa-alias-pub2", DataType::KEY_RSA_PUBLIC, policy[NO_PASS][EXP]),
105 { Group::KEY_PAIR_RSA, {
106 Item("key-rsa-alias-prv3", DataType::KEY_RSA_PRIVATE, policy[PASS][NO_EXP]),
107 Item("key-rsa-alias-pub3", DataType::KEY_RSA_PUBLIC, policy[PASS][NO_EXP]),
109 { Group::KEY_PAIR_RSA, {
110 Item("key-rsa-alias-prv4", DataType::KEY_RSA_PRIVATE, policy[PASS][EXP]),
111 Item("key-rsa-alias-pub4", DataType::KEY_RSA_PUBLIC, policy[PASS][EXP]),
113 // different policies
114 { Group::KEY_PAIR_RSA, {
115 Item("key-rsa-alias-prv5", DataType::KEY_RSA_PRIVATE, policy[PASS][NO_EXP]),
116 Item("key-rsa-alias-pub5", DataType::KEY_RSA_PUBLIC, policy[NO_PASS][EXP]),
120 { Group::SINGLE_ITEM, {
121 Item("key-aes-alias1", DataType::KEY_AES, policy[NO_PASS][NO_EXP]),
123 { Group::SINGLE_ITEM, {
124 Item("key-aes-alias2", DataType::KEY_AES, policy[NO_PASS][EXP]),
126 { Group::SINGLE_ITEM, {
127 Item("key-aes-alias3", DataType::KEY_AES, policy[PASS][NO_EXP]),
129 { Group::SINGLE_ITEM, {
130 Item("key-aes-alias4", DataType::KEY_AES, policy[PASS][EXP]),
134 { Group::CERT_CHAIN, {
135 Item("cert-root-alias1", DataType::CERTIFICATE, policy[NO_PASS][NO_EXP]),
136 Item("cert-im-ca-alias1", DataType::CERTIFICATE, policy[NO_PASS][NO_EXP]),
137 Item("cert-leaf-alias1", DataType::CERTIFICATE, policy[NO_PASS][NO_EXP]),
139 { Group::CERT_CHAIN, {
140 Item("cert-root-alias2", DataType::CERTIFICATE, policy[NO_PASS][EXP]),
141 Item("cert-im-ca-alias2", DataType::CERTIFICATE, policy[NO_PASS][EXP]),
142 Item("cert-leaf-alias2", DataType::CERTIFICATE, policy[NO_PASS][EXP]),
144 { Group::CERT_CHAIN, {
145 Item("cert-root-alias3", DataType::CERTIFICATE, policy[PASS][NO_EXP]),
146 Item("cert-im-ca-alias3", DataType::CERTIFICATE, policy[PASS][NO_EXP]),
147 Item("cert-leaf-alias3", DataType::CERTIFICATE, policy[PASS][NO_EXP]),
149 { Group::CERT_CHAIN, {
150 Item("cert-root-alias4", DataType::CERTIFICATE, policy[PASS][EXP]),
151 Item("cert-im-ca-alias4", DataType::CERTIFICATE, policy[PASS][EXP]),
152 Item("cert-leaf-alias4", DataType::CERTIFICATE, policy[PASS][EXP]),
156 { Group::SINGLE_ITEM, {
157 Item("pkcs-alias1", DataType::CHAIN_CERT_0, policy[NO_PASS][NO_EXP]),
159 { Group::SINGLE_ITEM, {
160 Item("pkcs-alias2", DataType::CHAIN_CERT_0, policy[NO_PASS][EXP]),
162 { Group::SINGLE_ITEM, {
163 Item("pkcs-alias3", DataType::CHAIN_CERT_0, policy[PASS][NO_EXP]),
165 { Group::SINGLE_ITEM, {
166 Item("pkcs-alias4", DataType::CHAIN_CERT_0, policy[PASS][EXP]),
170 const size_t CHAIN_SIZE = 3;
172 // TEST_ROOT_CA, expires 2035
173 std::string TEST_ROOT_CA =
174 "-----BEGIN CERTIFICATE-----\n"
175 "MIIDnzCCAoegAwIBAgIJAMH/ADkC5YSTMA0GCSqGSIb3DQEBBQUAMGYxCzAJBgNV\n"
176 "BAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMQ0wCwYDVQQKDARBQ01FMRAwDgYD\n"
177 "VQQLDAdUZXN0aW5nMSEwHwYDVQQDDBhUZXN0IHJvb3QgY2EgY2VydGlmaWNhdGUw\n"
178 "HhcNMTQxMjMwMTcyMTUyWhcNMjQxMjI3MTcyMTUyWjBmMQswCQYDVQQGEwJBVTET\n"
179 "MBEGA1UECAwKU29tZS1TdGF0ZTENMAsGA1UECgwEQUNNRTEQMA4GA1UECwwHV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
214 "Htv4Kxpm3P+C+xP9AEteCJfO+7p8MWgtWEJOknJyt55zeKS2JwZIq57KcbqD8U7v\n"
215 "vAUx1ymtUhlFPFd7J1mJ3pou+3aFYmGShYhGHpbrmUwjp7HVP588jrW1NoZVHdMc\n"
216 "4OgJWFrViXeu9+maIcekjMB/+9Y0dUgQuK5ZuT5H/Jwet7Th/o9uufTUZjBzRvrB\n"
217 "pbXgQpqgME2av4Q/6LuldPCTHLtWXgFUU2R+yCGmuGilvhFJnKoQryAbYnIQNWE8\n"
218 "SLoHQ9s1i7Zyb7HU6UAaqMOz15LBkyAqtNyJcO2p7Q/p5YK0xfD4xisI5qXucqVm\n"
220 "-----END CERTIFICATE-----\n";
222 // TEST_LEAF, signed by TEST_IM_CA, expires 2035
223 std::string TEST_LEAF =
224 "-----BEGIN CERTIFICATE-----\n"
225 "MIIDOzCCAiMCAQEwDQYJKoZIhvcNAQEFBQAwZDELMAkGA1UEBhMCQVUxEzARBgNV\n"
226 "BAgMClNvbWUtU3RhdGUxDTALBgNVBAoMBEFDTUUxEDAOBgNVBAsMB1Rlc3Rpbmcx\n"
227 "HzAdBgNVBAMMFlRlc3QgSU0gQ0EgY2VydGlmaWNhdGUwHhcNMTUwMTE2MTY0ODE0\n"
228 "WhcNMzUwMTExMTY0ODE0WjBjMQswCQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1T\n"
229 "dGF0ZTENMAsGA1UECgwEQUNNRTEQMA4GA1UECwwHVGVzdGluZzEeMBwGA1UEAwwV\n"
230 "VGVzdCBsZWFmIGNlcnRpZmljYXRlMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIB\n"
231 "CgKCAQEAzTdDIa2tDmRxFnIgiG+mBz8GoSVODs0ImNQGbqj+pLhBOFRH8fsah4Jl\n"
232 "z5YF9KwhMVLknnHGFLE/Nb7Ac35kEzhMQMpTRxohW83oxw3eZ8zN/FBoKqg4qHRq\n"
233 "QR8kS10YXTgrBR0ex/Vp+OUKEw6h7yL2r4Tpvrn9/qHwsxtLxqWbDIVf1O9b1Lfc\n"
234 "bllYMdmV5E62yN5tcwrDP8gvHjFnVeLzrG8wTpc9FR90/0Jkfp5jAJcArOBLrT0E\n"
235 "4VRqs+4HuwT8jAwFAmNnc7IYX5qSjtSWkmmHe73K/lzB+OiI0JEc/3eWUTWqwTSk\n"
236 "4tNCiQGBKJ39LXPTBBJdzmxVH7CUDQIDAQABMA0GCSqGSIb3DQEBBQUAA4IBAQAp\n"
237 "UdDOGu3hNiG+Vn10aQ6B1ZmOj3t+45gUV3sC+y8hB8EK1g4P5Ke9bVDts0T5eOnj\n"
238 "CSc+6VoND5O4adI0IFFRFljHNVnvjeosHfUZNnowsmA2ptQBtC1g5ZKRvKXlkC5/\n"
239 "i5BGgRqPFA7y9WB9Y05MrJHf3E+Oz/RBsLeeNiNN+rF5X1vYExvGHpo0M0zS0ze9\n"
240 "HtC0aOy8ocsTrQkf3ceHTAXx2i8ftoSSD4klojtWFpWMrNQa52F7wB9nU6FfKRuF\n"
241 "Zj/T1JkYXKkEwZU6nAR2jdZp3EP9xj3o15V/tyFcXHx6l8NTxn4cJb+Xe4VquQJz\n"
242 "6ON7PVe0ABN/AlwVQiFE\n"
243 "-----END CERTIFICATE-----\n";
248 void operator()(int* fd) {
254 typedef std::unique_ptr<int, FdCloser> FdPtr;
256 void restoreFile(const string& filename) {
257 string sourcePath = "/usr/share/ckm-db-test/" + filename;
258 string targetPath = "/opt/data/ckm/" + filename;
262 int sourceFd = TEMP_FAILURE_RETRY(open(sourcePath.c_str(), O_RDONLY));
264 BOOST_REQUIRE_MESSAGE(sourceFd > 0, "Opening " << sourcePath << " failed: " << strerror(err));
266 FdPtr sourceFdPtr(&sourceFd);
268 int targetFd = TEMP_FAILURE_RETRY(creat(targetPath.c_str(), 0644));
270 BOOST_REQUIRE_MESSAGE(targetFd > 0, "Creating " << targetPath << " failed: " << strerror(err));
272 ret = fchown(targetFd, CKM_UID, CKM_GID);
274 BOOST_REQUIRE_MESSAGE(ret != -1, "fchown() failed: " << strerror(err));
276 FdPtr targetFdPtr(&targetFd);
278 struct stat sourceStat;
279 ret = fstat(sourceFd, &sourceStat);
281 BOOST_REQUIRE_MESSAGE(ret != -1, "fstat() failed: " << strerror(err));
283 ret = sendfile(targetFd, sourceFd, 0, sourceStat.st_size);
285 BOOST_REQUIRE_MESSAGE(ret != -1, "sendfile() failed: " << strerror(err));
287 ret = fsync(targetFd);
289 BOOST_REQUIRE_MESSAGE(ret != -1, "fsync() failed: " << strerror(err));
296 void generateRandom(size_t random_bytes, unsigned char *output)
298 if(random_bytes<=0 || !output)
299 throw runtime_error("Invalid param");
301 std::ifstream is("/dev/urandom", std::ifstream::binary);
303 throw runtime_error("Failed to read /dev/urandom");
304 is.read(reinterpret_cast<char*>(output), random_bytes);
305 if(static_cast<std::streamsize>(random_bytes) != is.gcount())
306 throw runtime_error("Not enough bytes read from /dev/urandom");
309 RawBuffer createRandomBuffer(size_t random_bytes)
311 RawBuffer buffer(random_bytes);
312 generateRandom(buffer.size(), buffer.data());
315 } // namespace anonymous
318 SchemeTest::SchemeTest() : m_userChanged(false), m_directAccessEnabled(false) {
319 m_control = Control::create();
320 m_mgr = Manager::create();
324 sa.add("System", LABEL, "rwx");
325 sa.add(LABEL, "System", "rwx");
326 sa.add(LABEL, "System::Run", "x");
330 SchemeTest::~SchemeTest() {
336 void SchemeTest::RemoveUserData() {
337 if(CKM_API_SUCCESS != m_control->lockUserKey(UID))
338 throw runtime_error("lockUserKey failed");
340 if(CKM_API_SUCCESS != m_control->removeUserData(UID))
341 throw runtime_error("removeUserData failed");
344 void SchemeTest::SwitchToUser() {
348 if(CKM_API_SUCCESS != m_control->unlockUserKey(UID, DBPASS))
349 throw runtime_error("unlockUserKey failed");
353 if (smack_new_label_from_self(&label) <= 0)
354 throw runtime_error("smack_new_label_from_self failed");
356 m_origLabel = string(label);
359 if(0 > smack_set_label_for_self(LABEL))
360 throw runtime_error("smack_set_label_for_self failed");
363 throw runtime_error("setegid failed");
366 throw runtime_error("seteuid failed");
368 m_userChanged = true;
371 void SchemeTest::SwitchToRoot() {
376 throw runtime_error("seteuid failed");
378 throw runtime_error("setegid failed");
380 if(0 > smack_set_label_for_self(m_origLabel.c_str()))
381 throw runtime_error("smack_set_label_for_self failed");
383 if(m_control->lockUserKey(UID) != CKM_API_SUCCESS)
384 throw runtime_error("lockUserKey failed");
387 void SchemeTest::FillDb() {
389 ifstream is("/usr/share/ckm-db-test/encryption-scheme.p12");
391 throw runtime_error("Failed to read pkcs");
392 istreambuf_iterator<char> begin(is), end;
393 RawBuffer pkcsBuffer(begin, end);
394 auto pkcs = PKCS12::create(pkcsBuffer, Password());
396 throw runtime_error("Empty pkcs");
401 RawBuffer rootCaBuffer(TEST_ROOT_CA.begin(), TEST_ROOT_CA.end());
402 CertificateShPtr rootCa = CKM::Certificate::create(rootCaBuffer, CKM::DataFormat::FORM_PEM);
403 RawBuffer imCaBuffer(TEST_IM_CA.begin(), TEST_IM_CA.end());
404 CertificateShPtr imCa = CKM::Certificate::create(imCaBuffer, CKM::DataFormat::FORM_PEM);
405 RawBuffer leafBuffer(TEST_LEAF.begin(), TEST_LEAF.end());
406 CertificateShPtr leaf = CKM::Certificate::create(leafBuffer, CKM::DataFormat::FORM_PEM);
408 for(const auto& g:GROUPS) {
410 case Group::KEY_PAIR_RSA:
411 if(g.items.size() != 2)
412 throw runtime_error("Wrong number of keys");
413 if( g.items[0].type != DataType::KEY_RSA_PRIVATE ||
414 g.items[1].type != DataType::KEY_RSA_PUBLIC)
415 throw runtime_error("Invalid item type");
417 if(CKM_API_SUCCESS != m_mgr->createKeyPairRSA(1024,
422 throw runtime_error("createKeyPair failed");
425 case Group::CERT_CHAIN:
426 if(g.items.size() != CHAIN_SIZE)
427 throw runtime_error("Wrong number of certificates");
428 if( g.items[0].type != DataType::CERTIFICATE ||
429 g.items[1].type != DataType::CERTIFICATE ||
430 g.items[2].type != DataType::CERTIFICATE)
431 throw runtime_error("Invalid item type");
433 if(CKM_API_SUCCESS != m_mgr->saveCertificate(g.items[0].alias, rootCa, g.items[0].policy))
434 throw runtime_error("saveCertificate failed");
435 if(CKM_API_SUCCESS != m_mgr->saveCertificate(g.items[1].alias, imCa, g.items[1].policy))
436 throw runtime_error("saveCertificate failed");
437 if(CKM_API_SUCCESS != m_mgr->saveCertificate(g.items[2].alias, leaf, g.items[2].policy))
438 throw runtime_error("saveCertificate failed");
442 for(const auto& i:g.items) {
444 case DataType::BINARY_DATA:
445 if(CKM_API_SUCCESS != m_mgr->saveData(i.alias, TEST_DATA, i.policy))
446 throw runtime_error("saveData failed");
449 case DataType::KEY_AES:
450 if(CKM_API_SUCCESS != m_mgr->createKeyAES(256, i.alias, i.policy))
451 throw runtime_error("createKeyAES failed");
454 case DataType::CHAIN_CERT_0: // PKCS
455 if(CKM_API_SUCCESS != m_mgr->savePKCS12(i.alias, pkcs, i.policy, i.policy))
456 throw runtime_error("savePkcs12 failed");
460 throw runtime_error("unsupported data type");
468 void SchemeTest::ReadAll(bool useWrongPass) {
471 for(const auto& g:GROUPS) {
472 for(const auto& i:g.items) {
474 Password pass = i.policy.password;
483 case DataType::BINARY_DATA:
485 RawBuffer receivedData;
486 ret = m_mgr->getData(i.alias, pass, receivedData);
487 BOOST_REQUIRE_MESSAGE(useWrongPass || receivedData == TEST_DATA,
488 "Received data is different for " << i.alias);
492 case DataType::KEY_AES:
493 case DataType::KEY_RSA_PRIVATE:
494 case DataType::KEY_RSA_PUBLIC:
496 KeyShPtr receivedKey;
497 ret = m_mgr->getKey(i.alias, pass, receivedKey);
501 case DataType::CERTIFICATE:
503 CertificateShPtr receivedCert;
504 ret = m_mgr->getCertificate(i.alias, pass, receivedCert);
508 case DataType::CHAIN_CERT_0: // pkcs
511 ret = m_mgr->getPKCS12(i.alias, pass, pass, pkcs);
516 BOOST_FAIL("Unsupported data type " << i.type);
519 if(i.policy.extractable) {
521 BOOST_REQUIRE_MESSAGE(ret == CKM_API_ERROR_AUTHENTICATION_FAILED,
522 "Reading item " << i.alias << " should fail with " <<
523 CKM_API_ERROR_AUTHENTICATION_FAILED << " got: " << ret);
525 BOOST_REQUIRE_MESSAGE(ret == CKM_API_SUCCESS, "Reading item " << i.alias <<
526 " failed with " << ret);
529 BOOST_REQUIRE_MESSAGE(ret == CKM_API_ERROR_NOT_EXPORTABLE, "Item " << i.alias <<
530 " should not be exportable");
535 void SchemeTest::SignVerify() {
538 for(const auto& g:GROUPS) {
539 if(g.type == Group::KEY_PAIR_RSA) {
540 BOOST_REQUIRE_MESSAGE(g.items.size() == 2, "Wrong number of keys");
541 BOOST_REQUIRE_MESSAGE(g.items[0].type == DataType::KEY_RSA_PRIVATE &&
542 g.items[1].type == DataType::KEY_RSA_PUBLIC, "Wrong key");
544 SignVerifyItem(g.items[0], g.items[1]);
546 for(const auto& i:g.items) {
548 case DataType::CHAIN_CERT_0:
549 SignVerifyItem(i, i);
560 void SchemeTest::EncryptDecrypt() {
563 for(const auto& g:GROUPS) {
564 if(g.type == Group::KEY_PAIR_RSA) {
565 BOOST_REQUIRE_MESSAGE(g.items.size() == 2, "Wrong number of keys");
566 BOOST_REQUIRE_MESSAGE(g.items[0].type == DataType::KEY_RSA_PRIVATE &&
567 g.items[1].type == DataType::KEY_RSA_PUBLIC, "Wrong key");
569 EncryptDecryptItem(g.items[0], g.items[1]);
571 for(const auto& i:g.items) {
573 case DataType::KEY_AES:
574 EncryptDecryptItem(i);
577 case DataType::CHAIN_CERT_0:
578 EncryptDecryptItem(i, i);
589 void SchemeTest::CreateChain() {
592 for(const auto& g:GROUPS) {
593 if(g.type == Group::CERT_CHAIN) {
594 BOOST_REQUIRE_MESSAGE(g.items.size() == CHAIN_SIZE, "Not enough certificates");
595 for(const auto& c:g.items)
596 BOOST_REQUIRE_MESSAGE(c.type == DataType::CERTIFICATE, "Wrong item type");
597 Items trusted(CHAIN_SIZE-1);
598 std::copy(g.items.begin(), g.items.begin() + CHAIN_SIZE-1, trusted.begin());
600 // last one is ee (leaf)
601 CreateChainItem(g.items.back(), trusted);
603 for(const auto& i:g.items) {
604 if(i.type == DataType::CHAIN_CERT_0) // PKCS
605 CreateChainItem(i, { i });
611 void SchemeTest::RemoveAll() {
614 for(const auto& g:GROUPS) {
615 for(const auto& i:g.items) {
616 int ret = m_mgr->removeAlias(i.alias);
617 BOOST_REQUIRE_MESSAGE(ret == CKM_API_SUCCESS,
618 "removeAlias() failed with " << ret << " for " << i.alias);
622 size_t SchemeTest::CountObjects() {
623 EnableDirectDbAccess();
626 for(const auto& g:GROUPS) {
627 for(const auto& i:g.items) {
629 // it is assumed that aliases are different
630 m_db->getRows(i.alias, DB_LABEL, DataType::DB_FIRST, DataType::DB_LAST, rows);
637 void SchemeTest::RestoreDb() {
638 restoreFile("key-7654");
639 restoreFile("db-key-7654");
640 restoreFile("db-7654");
642 m_directAccessEnabled = false;
645 void SchemeTest::CheckSchemeVersion(const ItemFilter& filter, int version) {
646 EnableDirectDbAccess();
648 for(const auto& g:GROUPS) {
649 for(const auto& i:g.items) {
650 if(!filter.Matches(i))
654 m_db->getRows(i.alias, DB_LABEL, filter.typeFrom, filter.typeTo, rows);
655 BOOST_REQUIRE_MESSAGE(rows.size() > 0, "No rows found for " << i.alias);
656 for(const auto& r : rows) {
657 BOOST_REQUIRE_MESSAGE(
658 (r.encryptionScheme >> ENC_SCHEME_OFFSET) == version,
659 "Wrong encryption scheme for " << i.alias << ". Expected " << version <<
660 " got: " << (r.encryptionScheme >> ENC_SCHEME_OFFSET));
666 void SchemeTest::EnableDirectDbAccess() {
669 if(m_directAccessEnabled)
672 // direct access to db
674 auto wrappedDKEK = fs.getDKEK();
675 auto keyProvider = KeyProvider(wrappedDKEK, DBPASS);
677 auto wrappedDatabaseDEK = fs.getDBDEK();
678 RawBuffer key = keyProvider.getPureDEK(wrappedDatabaseDEK);
680 m_db.reset(new DB::Crypto(fs.getDBPath(), key));
681 m_directAccessEnabled = true;
684 void SchemeTest::SignVerifyItem(const Item& itemPrv, const Item& itemPub) {
686 KeyShPtr receivedKey;
688 // create/verify signature
689 ret = m_mgr->createSignature(itemPrv.alias,
690 itemPrv.policy.password,
692 HashAlgorithm::SHA512,
693 RSAPaddingAlgorithm::X931,
695 BOOST_REQUIRE_MESSAGE(ret == CKM_API_SUCCESS, "createSignature() failed with " << ret <<
696 " for " << itemPrv.alias);
697 ret = m_mgr->verifySignature(itemPub.alias,
698 itemPub.policy.password,
701 HashAlgorithm::SHA512,
702 RSAPaddingAlgorithm::X931);
703 BOOST_REQUIRE_MESSAGE(ret == CKM_API_SUCCESS, "verifySignature() failed with " << ret <<
704 " for " << itemPub.alias);
708 void SchemeTest::EncryptDecryptItem(const Item& item) {
709 CryptoAlgorithm algo;
710 RawBuffer iv = createRandomBuffer(IV_LEN);
711 RawBuffer encrypted, decrypted;
714 algo.setParam(ParamName::ALGO_TYPE, AlgoType::AES_GCM);
715 algo.setParam(ParamName::ED_IV, iv);
717 ret = m_mgr->encrypt(algo, item.alias, item.policy.password, TEST_DATA, encrypted);
718 BOOST_REQUIRE_MESSAGE(ret == CKM_API_SUCCESS, "encrypt() failed iwth " << ret << " for " <<
721 ret = m_mgr->decrypt(algo, item.alias, item.policy.password, encrypted, decrypted);
722 BOOST_REQUIRE_MESSAGE(ret == CKM_API_SUCCESS, "decrypt() failed iwth " << ret << " for " <<
725 BOOST_REQUIRE_MESSAGE(decrypted == TEST_DATA, "Decrypted data not equal to original");
728 void SchemeTest::EncryptDecryptItem(const Item& itemPrv, const Item& itemPub) {
729 CryptoAlgorithm algo;
730 RawBuffer encrypted, decrypted;
733 algo.setParam(ParamName::ALGO_TYPE, AlgoType::RSA_OAEP);
735 ret = m_mgr->encrypt(algo, itemPub.alias, itemPub.policy.password, TEST_DATA, encrypted);
736 BOOST_REQUIRE_MESSAGE(ret == CKM_API_SUCCESS, "encrypt() failed iwth " << ret << " for " <<
739 ret = m_mgr->decrypt(algo, itemPrv.alias, itemPrv.policy.password, encrypted, decrypted);
740 BOOST_REQUIRE_MESSAGE(ret == CKM_API_SUCCESS, "decrypt() failed iwth " << ret << " for " <<
743 BOOST_REQUIRE_MESSAGE(decrypted == TEST_DATA, "Decrypted data not equal to original");
746 void SchemeTest::CreateChainItem(const Item& leaf, const Items& certs) {
747 CertificateShPtrVector chain;
750 if(!leaf.policy.extractable || !leaf.policy.password.empty())
753 for(const auto& i : certs) {
754 if(!i.policy.extractable || !i.policy.password.empty())
756 trusted.push_back(i.alias);
759 CertificateShPtr leafCrt;
760 int ret = m_mgr->getCertificate(leaf.alias, leaf.policy.password, leafCrt);
761 BOOST_REQUIRE_MESSAGE(ret == CKM_API_SUCCESS,
762 "getCertificate failed with " << ret << " for " <<
765 ret = m_mgr->getCertificateChain(leafCrt, AliasVector(), trusted, false, chain);
766 BOOST_REQUIRE_MESSAGE(ret == CKM_API_SUCCESS,
767 "getCertificateChain() failed with " << ret);
768 BOOST_REQUIRE_MESSAGE(chain.size() == CHAIN_LEN, "Wrong chain length: " << chain.size());