2 * Copyright (c) 2000 - 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
18 * @author Bartłomiej Grzelewski (b.grzelewski@samsung.com)
23 #include <openssl/bio.h>
24 #include <openssl/evp.h>
25 #include <openssl/x509.h>
27 #include <dpl/log/log.h>
29 #include <generic-backend/exception.h>
30 #include <sw-backend/obj.h>
31 #include <sw-backend/internals.h>
33 #define EVP_SUCCESS 1 // DO NOTCHANGE THIS VALUE
34 #define EVP_FAIL 0 // DO NOTCHANGE THIS VALUE
42 AlgoType key2algo(DataType type) {
43 switch(static_cast<int>(type)) {
44 case DataType::Type::KEY_RSA_PRIVATE:
45 case DataType::Type::KEY_RSA_PUBLIC:
46 return AlgoType::RSA_SV;
47 case DataType::Type::KEY_DSA_PRIVATE:
48 case DataType::Type::KEY_DSA_PUBLIC:
49 return AlgoType::DSA_SV;
50 case DataType::Type::KEY_ECDSA_PRIVATE:
51 case DataType::Type::KEY_ECDSA_PUBLIC:
52 return AlgoType::ECDSA_SV;
54 ThrowErr(Exc::Crypto::InputParam, "Invalid key type: ", type);
58 } // namespace anonymous
60 typedef std::unique_ptr<BIO, std::function<void(BIO*)>> BioUniquePtr;
62 RawBuffer SKey::encrypt(const CryptoAlgorithm &alg, const RawBuffer &data)
64 return Internals::symmetricEncrypt(getBinary(), alg, data);
66 RawBuffer SKey::decrypt(const CryptoAlgorithm &alg, const RawBuffer &cipher)
68 return Internals::symmetricDecrypt(getBinary(), alg, cipher);
72 const CryptoAlgorithm &alg,
73 const RawBuffer &message)
75 CryptoAlgorithm algWithType(alg);
76 algWithType.setParam(ParamName::ALGO_TYPE, key2algo(m_type));
77 return Internals::sign(getEvpShPtr().get(), algWithType, message);
80 int AKey::verify(const CryptoAlgorithm &alg, const RawBuffer &message, const RawBuffer &sign) {
81 CryptoAlgorithm algWithType(alg);
82 EVP_PKEY* evp = getEvpShPtr().get();
85 // setup algorithm type basing on evp key type if it doesn't exist
86 if(!algWithType.getParam(ParamName::ALGO_TYPE, type)) {
87 int subType = EVP_PKEY_type(evp->type);
90 type = AlgoType::RSA_SV; break;
92 type = AlgoType::DSA_SV; break;
94 type = AlgoType::ECDSA_SV; break;
96 ThrowErr(Exc::Crypto::InputParam, "Invalid key type: ", subType);
98 algWithType.setParam(ParamName::ALGO_TYPE, type);
100 return Internals::verify(evp, algWithType, message, sign);
103 RawBuffer AKey::encrypt(const CryptoAlgorithm &alg, const RawBuffer &data)
105 return Internals::asymmetricEncrypt(getEvpShPtr(), alg, data);
108 RawBuffer AKey::decrypt(const CryptoAlgorithm &alg, const RawBuffer &data)
110 return Internals::asymmetricDecrypt(getEvpShPtr(), alg, data);
113 EvpShPtr AKey::getEvpShPtr() {
117 EVP_PKEY *pkey = NULL;
118 BioUniquePtr bio(BIO_new(BIO_s_mem()), BIO_free_all);
120 LogDebug("Start to parse key:");
123 (void)BIO_reset(bio.get());
124 BIO_write(bio.get(), m_raw.data(), m_raw.size());
125 pkey = d2i_PrivateKey_bio(bio.get(), NULL);
126 LogDebug("Trying d2i_PrivateKey_bio Status: " << (void*)pkey);
130 (void)BIO_reset(bio.get());
131 BIO_write(bio.get(), m_raw.data(), m_raw.size());
132 pkey = d2i_PUBKEY_bio(bio.get(), NULL);
133 LogDebug("Trying d2i_PUBKEY_bio Status: " << (void*)pkey);
137 ThrowErr(Exc::Crypto::InternalError, "Failed to parse key");
140 m_evp.reset(pkey, EVP_PKEY_free);
144 EvpShPtr Cert::getEvpShPtr() {
148 int size = static_cast<int>(m_raw.size());
149 const unsigned char *ptr = reinterpret_cast<const unsigned char *>(m_raw.data());
151 X509 *x509 = d2i_X509(NULL, &ptr, size);
154 ThrowErr(Exc::Crypto::InternalError, "Failed to parse certificate.");
157 m_evp.reset(X509_get_pubkey(x509), EVP_PKEY_free);
163 } // namespace Crypto