From 2ad243af50ef1d9a1304c6f96130d481489cc827 Mon Sep 17 00:00:00 2001 From: "Maciej J. Karpiuk" Date: Wed, 17 Sep 2014 13:34:14 +0200 Subject: Class naming scheme re-factoring: move towards better consistency. Change-Id: I8ad4d19991c92f5268ef1f0a411258c848c83c64 --- src/CMakeLists.txt | 4 +- src/manager/CMakeLists.txt | 2 +- src/manager/client/client-manager-impl.cpp | 4 +- src/manager/common/certificate-impl.cpp | 16 +-- src/manager/common/certificate-impl.h | 6 +- src/manager/common/generic-key.cpp | 222 ----------------------------- src/manager/common/generic-key.h | 62 -------- src/manager/common/key-impl.cpp | 222 +++++++++++++++++++++++++++++ src/manager/common/key-impl.h | 62 ++++++++ src/manager/common/pkcs12-impl.cpp | 8 +- src/manager/service/CryptoService.cpp | 26 ++-- src/manager/service/CryptoService.h | 16 +-- src/manager/service/ckm-logic.cpp | 14 +- src/manager/service/ckm-logic.h | 2 +- 14 files changed, 333 insertions(+), 333 deletions(-) delete mode 100644 src/manager/common/generic-key.cpp delete mode 100644 src/manager/common/generic-key.h create mode 100644 src/manager/common/key-impl.cpp create mode 100644 src/manager/common/key-impl.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index bb2d60aa..fdcc3aa4 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -89,7 +89,7 @@ SET(KEY_MANAGER_CLIENT_SOURCES ${KEY_MANAGER_PATH}/common/smack-check.cpp ${KEY_MANAGER_PATH}/common/certificate-impl.cpp ${KEY_MANAGER_PATH}/common/certificate-store.cpp - ${KEY_MANAGER_PATH}/common/generic-key.cpp + ${KEY_MANAGER_PATH}/common/key-impl.cpp ${KEY_MANAGER_PATH}/common/pkcs12-impl.cpp ${KEY_MANAGER_PATH}/dpl/log/src/abstract_log_provider.cpp ${KEY_MANAGER_PATH}/dpl/log/src/dlog_log_provider.cpp @@ -148,7 +148,7 @@ SET(KEY_MANAGER_CONTROL_CLIENT_SOURCES ${KEY_MANAGER_PATH}/common/smack-check.cpp ${KEY_MANAGER_PATH}/common/certificate-impl.cpp ${KEY_MANAGER_PATH}/common/certificate-store.cpp - ${KEY_MANAGER_PATH}/common/generic-key.cpp + ${KEY_MANAGER_PATH}/common/key-impl.cpp ${KEY_MANAGER_PATH}/dpl/log/src/abstract_log_provider.cpp ${KEY_MANAGER_PATH}/dpl/log/src/dlog_log_provider.cpp ${KEY_MANAGER_PATH}/dpl/log/src/log.cpp diff --git a/src/manager/CMakeLists.txt b/src/manager/CMakeLists.txt index f5e23304..5dfbc78f 100644 --- a/src/manager/CMakeLists.txt +++ b/src/manager/CMakeLists.txt @@ -20,7 +20,7 @@ SET(COMMON_SOURCES ${COMMON_PATH}/common/smack-check.cpp ${COMMON_PATH}/common/certificate-impl.cpp ${COMMON_PATH}/common/certificate-store.cpp - ${COMMON_PATH}/common/generic-key.cpp + ${COMMON_PATH}/common/key-impl.cpp ${COMMON_PATH}/dpl/log/src/abstract_log_provider.cpp ${COMMON_PATH}/dpl/log/src/dlog_log_provider.cpp ${COMMON_PATH}/dpl/log/src/log.cpp diff --git a/src/manager/client/client-manager-impl.cpp b/src/manager/client/client-manager-impl.cpp index a838b8a0..6e82722c 100644 --- a/src/manager/client/client-manager-impl.cpp +++ b/src/manager/client/client-manager-impl.cpp @@ -27,7 +27,7 @@ #include #include #include -#include +#include #include namespace { @@ -231,7 +231,7 @@ int ManagerImpl::getKey(const Alias &alias, const Password &password, KeyShPtr & if (retCode != CKM_API_SUCCESS) return retCode; - KeyShPtr keyParsed(new GenericKey(rawData)); + KeyShPtr keyParsed(new KeyImpl(rawData)); if (keyParsed->empty()) { LogDebug("Key empty - failed to parse!"); diff --git a/src/manager/common/certificate-impl.cpp b/src/manager/common/certificate-impl.cpp index f8adbf36..51af4377 100644 --- a/src/manager/common/certificate-impl.cpp +++ b/src/manager/common/certificate-impl.cpp @@ -24,7 +24,7 @@ #include -#include +#include #include #include @@ -129,18 +129,18 @@ bool CertificateImpl::empty() const { return m_x509 == NULL; } -GenericKey::EvpShPtr CertificateImpl::getEvpShPtr() const { - return GenericKey::EvpShPtr(X509_get_pubkey(m_x509), EVP_PKEY_free); +KeyImpl::EvpShPtr CertificateImpl::getEvpShPtr() const { + return KeyImpl::EvpShPtr(X509_get_pubkey(m_x509), EVP_PKEY_free); } -GenericKey CertificateImpl::getGenericKey() const { - GenericKey::EvpShPtr evp(X509_get_pubkey(m_x509), EVP_PKEY_free); +KeyImpl CertificateImpl::getKeyImpl() const { + KeyImpl::EvpShPtr evp(X509_get_pubkey(m_x509), EVP_PKEY_free); if (EVP_PKEY_type(evp->type) == EVP_PKEY_RSA) - return GenericKey(evp, KeyType::KEY_RSA_PUBLIC); + return KeyImpl(evp, KeyType::KEY_RSA_PUBLIC); if (EVP_PKEY_type(evp->type) == EVP_PKEY_EC) - return GenericKey(evp, KeyType::KEY_ECDSA_PUBLIC); + return KeyImpl(evp, KeyType::KEY_ECDSA_PUBLIC); LogError("Unsupported key type in certificate."); - return GenericKey(); + return KeyImpl(); } X509_NAME *getX509Name(X509 *x509, CertificateFieldId type) { diff --git a/src/manager/common/certificate-impl.h b/src/manager/common/certificate-impl.h index 62ebfbb2..679de866 100644 --- a/src/manager/common/certificate-impl.h +++ b/src/manager/common/certificate-impl.h @@ -26,7 +26,7 @@ #include #include -#include +#include namespace CKM { @@ -44,8 +44,8 @@ public: virtual bool empty() const; virtual X509* getX509() const; - GenericKey::EvpShPtr getEvpShPtr() const; - GenericKey getGenericKey() const; + KeyImpl::EvpShPtr getEvpShPtr() const; + KeyImpl getKeyImpl() const; std::string getOneLine(CertificateFieldId type) const; std::string getField(CertificateFieldId type, int fieldNid) const; diff --git a/src/manager/common/generic-key.cpp b/src/manager/common/generic-key.cpp deleted file mode 100644 index 6835b8e4..00000000 --- a/src/manager/common/generic-key.cpp +++ /dev/null @@ -1,222 +0,0 @@ -/* Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License - * - * - * @file generic-key.cpp - * @author Bartlomiej Grzelewski (b.grzelewski@samsung.com) - * @version 1.0 - * @brief Key implementation. - */ -#include - -#include -#include -#include -#include - -#include -#include -#include -#include - -#include - -#include -#include - -namespace CKM { -namespace { - -typedef std::unique_ptr> BioUniquePtr; - -int passcb(char *buff, int size, int rwflag, void *userdata) { - (void) rwflag; - Password *ptr = static_cast(userdata); - if (ptr == NULL) - return 0; - if (ptr->empty()) - return 0; - if (static_cast(ptr->size()) > size) - return 0; - memcpy(buff, ptr->c_str(), ptr->size()); - return ptr->size(); -} - -typedef int(*I2D_CONV)(BIO*, EVP_PKEY*); - -CKM::RawBuffer i2d(I2D_CONV fun, EVP_PKEY* pkey) { - BioUniquePtr bio(BIO_new(BIO_s_mem()), BIO_free_all); - - if (NULL == pkey) { - LogDebug("You are trying to read empty key!"); - return RawBuffer(); - } - - if (NULL == bio.get()) { - LogError("Error in memory allocation! Function: BIO_new."); - return RawBuffer(); - } - - if (1 != fun(bio.get(), pkey)) { - LogError("Error in conversion EVP_PKEY to der"); - return RawBuffer(); - } - - CKM::RawBuffer output(8196); - - int size = BIO_read(bio.get(), output.data(), output.size()); - - if (size <= 0) { - LogError("Error in BIO_read: " << size); - return RawBuffer(); - } - - output.resize(size); - return output; -} - -} // anonymous namespace - -GenericKey::GenericKey() - : m_pkey(NULL, EVP_PKEY_free) - , m_type(KeyType::KEY_NONE) -{} - -GenericKey::GenericKey(const GenericKey &second) { - m_pkey = second.m_pkey; - m_type = second.m_type; -} - -GenericKey::GenericKey(const RawBuffer &buf, const Password &password) - : m_pkey(NULL, EVP_PKEY_free) - , m_type(KeyType::KEY_NONE) -{ - bool isPrivate = false; - EVP_PKEY *pkey = NULL; - BioUniquePtr bio(BIO_new(BIO_s_mem()), BIO_free_all); - - LogDebug("Start to parse key:"); -// printDER(buf); - - if (buf[0] != '-') { - BIO_write(bio.get(), buf.data(), buf.size()); - pkey = d2i_PUBKEY_bio(bio.get(), NULL); - isPrivate = false; - LogDebug("Trying d2i_PUBKEY_bio Status: " << (void*)pkey); - } - - if (!pkey && buf[0] != '-') { - BIO_reset(bio.get()); - BIO_write(bio.get(), buf.data(), buf.size()); - pkey = d2i_PrivateKey_bio(bio.get(), NULL); - isPrivate = true; - LogDebug("Trying d2i_PrivateKey_bio Status: " << (void*)pkey); - } - - if (!pkey && buf[0] == '-') { - BIO_reset(bio.get()); - BIO_write(bio.get(), buf.data(), buf.size()); - pkey = PEM_read_bio_PUBKEY(bio.get(), NULL, passcb, const_cast(&password)); - isPrivate = false; - LogDebug("PEM_read_bio_PUBKEY Status: " << (void*)pkey); - } - - if (!pkey && buf[0] == '-') { - BIO_reset(bio.get()); - BIO_write(bio.get(), buf.data(), buf.size()); - pkey = PEM_read_bio_PrivateKey(bio.get(), NULL, passcb, const_cast(&password)); - isPrivate = true; - LogDebug("PEM_read_bio_PrivateKey Status: " << (void*)pkey); - } - - if (!pkey) { - LogError("Failed to parse key"); - return; - } - - m_pkey.reset(pkey, EVP_PKEY_free); - - int type = EVP_PKEY_type(pkey->type); - - if (type == EVP_PKEY_RSA) { - m_type = isPrivate ? KeyType::KEY_RSA_PRIVATE : KeyType::KEY_RSA_PUBLIC; - } - - if (type == EVP_PKEY_EC) { - m_type = isPrivate ? KeyType::KEY_ECDSA_PRIVATE : KeyType::KEY_ECDSA_PUBLIC; - } - LogDebug("KeyType is: " << (int)m_type << " isPrivate: " << isPrivate); -} - -GenericKey::GenericKey(EvpShPtr pkey, KeyType type) - : m_pkey(pkey) - , m_type(type) -{ - if (type == KeyType::KEY_RSA_PRIVATE || type == KeyType::KEY_RSA_PUBLIC) - if (EVP_PKEY_RSA != EVP_PKEY_type(pkey->type)) { - m_pkey.reset(); - m_type = KeyType::KEY_NONE; - } - if (type == KeyType::KEY_ECDSA_PRIVATE || type == KeyType::KEY_ECDSA_PUBLIC) - if (EVP_PKEY_EC != EVP_PKEY_type(pkey->type)) { - m_pkey.reset(); - m_type = KeyType::KEY_NONE; - } -} - -bool GenericKey::empty() const { - return m_pkey.get() == NULL; -} - -GenericKey::EvpShPtr GenericKey::getEvpShPtr() const { - return m_pkey; -} - -KeyType GenericKey::getType() const { - return m_type; -} - -RawBuffer GenericKey::getDERPRV() const { - return i2d(i2d_PrivateKey_bio, m_pkey.get()); -} - -RawBuffer GenericKey::getDERPUB() const { - return i2d(i2d_PUBKEY_bio, m_pkey.get()); -} - -RawBuffer GenericKey::getDER() const { - if (m_type == KeyType::KEY_ECDSA_PRIVATE || m_type == KeyType::KEY_RSA_PRIVATE) { - return getDERPRV(); - } else if (m_type == KeyType::KEY_RSA_PUBLIC || m_type == KeyType::KEY_ECDSA_PUBLIC) { - return getDERPUB(); - } - return RawBuffer(); -} - -KeyShPtr Key::create(const RawBuffer &raw, const Password &password) { - try { - KeyShPtr output = std::make_shared(raw, password); - if (output->empty()) - output.reset(); - return output; - } catch (const std::bad_alloc &) { - LogDebug("Bad alloc was catch during GenericKey creation"); - } catch (...) { - LogError("Critical error: Unknown exception was caught during GenericKey creation"); - } - return KeyShPtr(); -} - -} // namespace CKM - diff --git a/src/manager/common/generic-key.h b/src/manager/common/generic-key.h deleted file mode 100644 index ac1918cc..00000000 --- a/src/manager/common/generic-key.h +++ /dev/null @@ -1,62 +0,0 @@ -/* Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License - * - * - * @file generic-key.h - * @author Bartlomiej Grzelewski (b.grzelewski@samsung.com) - * @version 1.0 - * @brief Key implementation. - */ -#pragma once - -#include - -#include -#include -#include - -namespace CKM { - -class GenericKey : public Key { -public: - typedef std::shared_ptr EvpShPtr; - - GenericKey(); - GenericKey(const GenericKey &second); - GenericKey(const RawBuffer& buffer, const Password &password = Password()); - GenericKey(EvpShPtr pkey, KeyType type); - - virtual KeyType getType() const; - virtual RawBuffer getDER() const; - virtual RawBuffer getDERPUB() const; - virtual RawBuffer getDERPRV() const; - virtual EvpShPtr getEvpShPtr() const; - virtual ElipticCurve getCurve() const { - // TODO - return ElipticCurve::prime192v1; - } - virtual int getSize() const { - // TODO - return 0; - } - - virtual bool empty() const; - virtual ~GenericKey(){} -protected: - EvpShPtr m_pkey; - KeyType m_type; -}; - -} // namespace CKM - diff --git a/src/manager/common/key-impl.cpp b/src/manager/common/key-impl.cpp new file mode 100644 index 00000000..ceb6ef0f --- /dev/null +++ b/src/manager/common/key-impl.cpp @@ -0,0 +1,222 @@ +/* Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + * + * + * @file key-impl.cpp + * @author Bartlomiej Grzelewski (b.grzelewski@samsung.com) + * @version 1.0 + * @brief Key implementation. + */ +#include + +#include +#include +#include +#include + +#include +#include +#include +#include + +#include + +#include +#include + +namespace CKM { +namespace { + +typedef std::unique_ptr> BioUniquePtr; + +int passcb(char *buff, int size, int rwflag, void *userdata) { + (void) rwflag; + Password *ptr = static_cast(userdata); + if (ptr == NULL) + return 0; + if (ptr->empty()) + return 0; + if (static_cast(ptr->size()) > size) + return 0; + memcpy(buff, ptr->c_str(), ptr->size()); + return ptr->size(); +} + +typedef int(*I2D_CONV)(BIO*, EVP_PKEY*); + +CKM::RawBuffer i2d(I2D_CONV fun, EVP_PKEY* pkey) { + BioUniquePtr bio(BIO_new(BIO_s_mem()), BIO_free_all); + + if (NULL == pkey) { + LogDebug("You are trying to read empty key!"); + return RawBuffer(); + } + + if (NULL == bio.get()) { + LogError("Error in memory allocation! Function: BIO_new."); + return RawBuffer(); + } + + if (1 != fun(bio.get(), pkey)) { + LogError("Error in conversion EVP_PKEY to der"); + return RawBuffer(); + } + + CKM::RawBuffer output(8196); + + int size = BIO_read(bio.get(), output.data(), output.size()); + + if (size <= 0) { + LogError("Error in BIO_read: " << size); + return RawBuffer(); + } + + output.resize(size); + return output; +} + +} // anonymous namespace + +KeyImpl::KeyImpl() + : m_pkey(NULL, EVP_PKEY_free) + , m_type(KeyType::KEY_NONE) +{} + +KeyImpl::KeyImpl(const KeyImpl &second) { + m_pkey = second.m_pkey; + m_type = second.m_type; +} + +KeyImpl::KeyImpl(const RawBuffer &buf, const Password &password) + : m_pkey(NULL, EVP_PKEY_free) + , m_type(KeyType::KEY_NONE) +{ + bool isPrivate = false; + EVP_PKEY *pkey = NULL; + BioUniquePtr bio(BIO_new(BIO_s_mem()), BIO_free_all); + + LogDebug("Start to parse key:"); +// printDER(buf); + + if (buf[0] != '-') { + BIO_write(bio.get(), buf.data(), buf.size()); + pkey = d2i_PUBKEY_bio(bio.get(), NULL); + isPrivate = false; + LogDebug("Trying d2i_PUBKEY_bio Status: " << (void*)pkey); + } + + if (!pkey && buf[0] != '-') { + BIO_reset(bio.get()); + BIO_write(bio.get(), buf.data(), buf.size()); + pkey = d2i_PrivateKey_bio(bio.get(), NULL); + isPrivate = true; + LogDebug("Trying d2i_PrivateKey_bio Status: " << (void*)pkey); + } + + if (!pkey && buf[0] == '-') { + BIO_reset(bio.get()); + BIO_write(bio.get(), buf.data(), buf.size()); + pkey = PEM_read_bio_PUBKEY(bio.get(), NULL, passcb, const_cast(&password)); + isPrivate = false; + LogDebug("PEM_read_bio_PUBKEY Status: " << (void*)pkey); + } + + if (!pkey && buf[0] == '-') { + BIO_reset(bio.get()); + BIO_write(bio.get(), buf.data(), buf.size()); + pkey = PEM_read_bio_PrivateKey(bio.get(), NULL, passcb, const_cast(&password)); + isPrivate = true; + LogDebug("PEM_read_bio_PrivateKey Status: " << (void*)pkey); + } + + if (!pkey) { + LogError("Failed to parse key"); + return; + } + + m_pkey.reset(pkey, EVP_PKEY_free); + + int type = EVP_PKEY_type(pkey->type); + + if (type == EVP_PKEY_RSA) { + m_type = isPrivate ? KeyType::KEY_RSA_PRIVATE : KeyType::KEY_RSA_PUBLIC; + } + + if (type == EVP_PKEY_EC) { + m_type = isPrivate ? KeyType::KEY_ECDSA_PRIVATE : KeyType::KEY_ECDSA_PUBLIC; + } + LogDebug("KeyType is: " << (int)m_type << " isPrivate: " << isPrivate); +} + +KeyImpl::KeyImpl(EvpShPtr pkey, KeyType type) + : m_pkey(pkey) + , m_type(type) +{ + if (type == KeyType::KEY_RSA_PRIVATE || type == KeyType::KEY_RSA_PUBLIC) + if (EVP_PKEY_RSA != EVP_PKEY_type(pkey->type)) { + m_pkey.reset(); + m_type = KeyType::KEY_NONE; + } + if (type == KeyType::KEY_ECDSA_PRIVATE || type == KeyType::KEY_ECDSA_PUBLIC) + if (EVP_PKEY_EC != EVP_PKEY_type(pkey->type)) { + m_pkey.reset(); + m_type = KeyType::KEY_NONE; + } +} + +bool KeyImpl::empty() const { + return m_pkey.get() == NULL; +} + +KeyImpl::EvpShPtr KeyImpl::getEvpShPtr() const { + return m_pkey; +} + +KeyType KeyImpl::getType() const { + return m_type; +} + +RawBuffer KeyImpl::getDERPRV() const { + return i2d(i2d_PrivateKey_bio, m_pkey.get()); +} + +RawBuffer KeyImpl::getDERPUB() const { + return i2d(i2d_PUBKEY_bio, m_pkey.get()); +} + +RawBuffer KeyImpl::getDER() const { + if (m_type == KeyType::KEY_ECDSA_PRIVATE || m_type == KeyType::KEY_RSA_PRIVATE) { + return getDERPRV(); + } else if (m_type == KeyType::KEY_RSA_PUBLIC || m_type == KeyType::KEY_ECDSA_PUBLIC) { + return getDERPUB(); + } + return RawBuffer(); +} + +KeyShPtr Key::create(const RawBuffer &raw, const Password &password) { + try { + KeyShPtr output = std::make_shared(raw, password); + if (output->empty()) + output.reset(); + return output; + } catch (const std::bad_alloc &) { + LogDebug("Bad alloc was catch during KeyImpl creation"); + } catch (...) { + LogError("Critical error: Unknown exception was caught during KeyImpl creation"); + } + return KeyShPtr(); +} + +} // namespace CKM + diff --git a/src/manager/common/key-impl.h b/src/manager/common/key-impl.h new file mode 100644 index 00000000..9d7831a4 --- /dev/null +++ b/src/manager/common/key-impl.h @@ -0,0 +1,62 @@ +/* Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + * + * + * @file key-impl.h + * @author Bartlomiej Grzelewski (b.grzelewski@samsung.com) + * @version 1.0 + * @brief Key implementation. + */ +#pragma once + +#include + +#include +#include +#include + +namespace CKM { + +class KeyImpl : public Key { +public: + typedef std::shared_ptr EvpShPtr; + + KeyImpl(); + KeyImpl(const KeyImpl &second); + KeyImpl(const RawBuffer& buffer, const Password &password = Password()); + KeyImpl(EvpShPtr pkey, KeyType type); + + virtual KeyType getType() const; + virtual RawBuffer getDER() const; + virtual RawBuffer getDERPUB() const; + virtual RawBuffer getDERPRV() const; + virtual EvpShPtr getEvpShPtr() const; + virtual ElipticCurve getCurve() const { + // TODO + return ElipticCurve::prime192v1; + } + virtual int getSize() const { + // TODO + return 0; + } + + virtual bool empty() const; + virtual ~KeyImpl(){} +protected: + EvpShPtr m_pkey; + KeyType m_type; +}; + +} // namespace CKM + diff --git a/src/manager/common/pkcs12-impl.cpp b/src/manager/common/pkcs12-impl.cpp index 255a752a..394b6490 100644 --- a/src/manager/common/pkcs12-impl.cpp +++ b/src/manager/common/pkcs12-impl.cpp @@ -28,7 +28,7 @@ #include #include -#include +#include namespace CKM { namespace { @@ -71,11 +71,11 @@ PKCS12Impl::PKCS12Impl(const RawBuffer &buffer, const Password &password) } if (pkey) { - GenericKey::EvpShPtr ptr(pkey, EVP_PKEY_free); + KeyImpl::EvpShPtr ptr(pkey, EVP_PKEY_free); if (EVP_PKEY_type(pkey->type) == EVP_PKEY_RSA) { - m_pkey = std::make_shared(ptr, KeyType::KEY_RSA_PRIVATE); + m_pkey = std::make_shared(ptr, KeyType::KEY_RSA_PRIVATE); } else if (EVP_PKEY_type(pkey->type) == EVP_PKEY_EC) { - m_pkey = std::make_shared(ptr, KeyType::KEY_ECDSA_PRIVATE); + m_pkey = std::make_shared(ptr, KeyType::KEY_ECDSA_PRIVATE); } else { LogError("Unsupported private key type."); EVP_PKEY_free(pkey); diff --git a/src/manager/service/CryptoService.cpp b/src/manager/service/CryptoService.cpp index 39908a34..b705a93e 100644 --- a/src/manager/service/CryptoService.cpp +++ b/src/manager/service/CryptoService.cpp @@ -20,7 +20,7 @@ #include #include #include -#include +#include #include #include #include @@ -80,8 +80,8 @@ int CryptoService::initialize() { int CryptoService::createKeyPairRSA(const int size, // size in bits [1024, 2048, 4096] - GenericKey &createdPrivateKey, // returned value - GenericKey &createdPublicKey) // returned value + KeyImpl &createdPrivateKey, // returned value + KeyImpl &createdPublicKey) // returned value { EVP_PKEY_CTX *ctx = NULL; EVP_PKEY *pkey = NULL; @@ -141,10 +141,10 @@ int CryptoService::createKeyPairRSA(const int size, // size in bits [1024, 2048, ReThrowMsg(CryptoService::Exception::opensslError,"Error in opensslError function !!"); } - GenericKey::EvpShPtr ptr(pkey, EVP_PKEY_free); // shared ptr will free pkey + KeyImpl::EvpShPtr ptr(pkey, EVP_PKEY_free); // shared ptr will free pkey - createdPrivateKey = GenericKey(ptr, KeyType::KEY_RSA_PRIVATE); - createdPublicKey = GenericKey(ptr, KeyType::KEY_RSA_PUBLIC); + createdPrivateKey = KeyImpl(ptr, KeyType::KEY_RSA_PRIVATE); + createdPublicKey = KeyImpl(ptr, KeyType::KEY_RSA_PUBLIC); if(pparam) { EVP_PKEY_free(pparam); @@ -158,8 +158,8 @@ int CryptoService::createKeyPairRSA(const int size, // size in bits [1024, 2048, } int CryptoService::createKeyPairECDSA(ElipticCurve type, - GenericKey &createdPrivateKey, // returned value - GenericKey &createdPublicKey) // returned value + KeyImpl &createdPrivateKey, // returned value + KeyImpl &createdPublicKey) // returned value { int ecCurve = NOT_DEFINED; EVP_PKEY_CTX *pctx = NULL; @@ -253,10 +253,10 @@ int CryptoService::createKeyPairECDSA(ElipticCurve type, ReThrowMsg(CryptoService::Exception::opensslError,"Error in openssl function !!"); } - GenericKey::EvpShPtr ptr(pkey, EVP_PKEY_free); // shared ptr will free pkey + KeyImpl::EvpShPtr ptr(pkey, EVP_PKEY_free); // shared ptr will free pkey - createdPrivateKey = GenericKey(ptr, KeyType::KEY_ECDSA_PRIVATE); - createdPublicKey = GenericKey(ptr, KeyType::KEY_ECDSA_PUBLIC); + createdPrivateKey = KeyImpl(ptr, KeyType::KEY_ECDSA_PRIVATE); + createdPublicKey = KeyImpl(ptr, KeyType::KEY_ECDSA_PUBLIC); if(pparam) { EVP_PKEY_free(pparam); @@ -273,7 +273,7 @@ int CryptoService::createKeyPairECDSA(ElipticCurve type, return CKM_CRYPTO_CREATEKEY_SUCCESS; } -int CryptoService::createSignature(const GenericKey &privateKey, +int CryptoService::createSignature(const KeyImpl &privateKey, const RawBuffer &message, const HashAlgorithm hashAlgo, const RSAPaddingAlgorithm padAlgo, @@ -402,7 +402,7 @@ int CryptoService::createSignature(const GenericKey &privateKey, return CKM_CREATE_SIGNATURE_SUCCESS; } -int CryptoService::verifySignature(const GenericKey &publicKey, +int CryptoService::verifySignature(const KeyImpl &publicKey, const RawBuffer &message, const RawBuffer &signature, const HashAlgorithm hashAlgo, diff --git a/src/manager/service/CryptoService.h b/src/manager/service/CryptoService.h index 22e92fe5..44eff2fb 100644 --- a/src/manager/service/CryptoService.h +++ b/src/manager/service/CryptoService.h @@ -1,7 +1,7 @@ #pragma once #include -#include +#include #include #include #include @@ -53,20 +53,20 @@ class CryptoService { static int initialize(); static int createKeyPairRSA(const int size, // size in bits [1024, 2048, 4096] - GenericKey &createdPrivateKey, // returned value ==> Key &createdPrivateKey, - GenericKey &createdPublicKey); // returned value ==> Key &createdPublicKey + KeyImpl &createdPrivateKey, // returned value ==> Key &createdPrivateKey, + KeyImpl &createdPublicKey); // returned value ==> Key &createdPublicKey static int createKeyPairECDSA(ElipticCurve type1, - GenericKey &createdPrivateKey, // returned value - GenericKey &createdPublicKey); // returned value + KeyImpl &createdPrivateKey, // returned value + KeyImpl &createdPublicKey); // returned value - int createSignature(const GenericKey &privateKey, + int createSignature(const KeyImpl &privateKey, const RawBuffer &message, const HashAlgorithm hashAlgo, const RSAPaddingAlgorithm padAlgo, RawBuffer &signature); - int verifySignature(const GenericKey &publicKey, + int verifySignature(const KeyImpl &publicKey, const RawBuffer &message, const RawBuffer &signature, const HashAlgorithm hashAlgo, @@ -75,7 +75,7 @@ class CryptoService { int verifyCertificateChain(const CertificateImpl &certificate, const CertificateImplVector &untrustedCertificates, const CertificateImplVector &userTrustedCertificates, - CertificateImplVector &certificateChainVector); + CertificateImplVector &certificateChainVector); private: std::vector verifyCertChain(X509 *cert, diff --git a/src/manager/service/ckm-logic.cpp b/src/manager/service/ckm-logic.cpp index 587d9bb4..f74ea05a 100644 --- a/src/manager/service/ckm-logic.cpp +++ b/src/manager/service/ckm-logic.cpp @@ -27,7 +27,7 @@ #include #include #include -#include +#include namespace { const char * const CERT_SYSTEM_DIR = "/etc/ssl/certs"; @@ -446,7 +446,7 @@ int CKMLogic::createKeyPairRSAHelper( return CKM_API_ERROR_DB_LOCKED; auto &handler = m_userDataMap[cred.uid]; - GenericKey prv, pub; + KeyImpl prv, pub; int retCode; if (CKM_CRYPTO_CREATEKEY_SUCCESS != @@ -534,7 +534,7 @@ int CKMLogic::createKeyPairECDSAHelper( return CKM_API_ERROR_DB_LOCKED; auto &handler = m_userDataMap[cred.uid]; - GenericKey prv, pub; + KeyImpl prv, pub; int retCode; if (CKM_CRYPTO_CREATEKEY_SUCCESS != @@ -721,7 +721,7 @@ RawBuffer CKMLogic::createSignature( break; } - GenericKey keyParsed(row.data, Password()); + KeyImpl keyParsed(row.data, Password()); if (keyParsed.empty()) retCode = CKM_API_ERROR_SERVER_ERROR; else @@ -765,18 +765,18 @@ RawBuffer CKMLogic::verifySignature( do { CryptoService cs; DBRow row; - GenericKey key; + KeyImpl key; retCode = getDataHelper(cred, DBDataType::DB_KEY_FIRST, publicKeyOrCertAlias, password, row); if (retCode == CKM_API_SUCCESS) { - key = GenericKey(row.data); + key = KeyImpl(row.data); } else if (retCode == CKM_API_ERROR_DB_ALIAS_UNKNOWN) { retCode = getDataHelper(cred, DBDataType::CERTIFICATE, publicKeyOrCertAlias, password, row); if (retCode != CKM_API_SUCCESS) break; CertificateImpl cert(row.data, DataFormat::FORM_DER); - key = cert.getGenericKey(); + key = cert.getKeyImpl(); } else { break; } diff --git a/src/manager/service/ckm-logic.h b/src/manager/service/ckm-logic.h index 9ed10c13..1a6971fe 100644 --- a/src/manager/service/ckm-logic.h +++ b/src/manager/service/ckm-logic.h @@ -178,7 +178,7 @@ private: Credentials &cred, const Alias &publicKeyOrCertAlias, const Password &password, // password for public_key (optional) - const GenericKey &genericKey); + const KeyImpl &genericKey); std::map m_userDataMap; CertificateStore m_certStore; -- cgit v1.2.3