2 * Copyright (c) 2015-2020 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)
21 #include <openssl/bio.h>
22 #include <openssl/evp.h>
23 #include <openssl/x509.h>
25 #include <dpl/log/log.h>
28 #include <generic-backend/exception.h>
29 #include <sw-backend/obj.h>
30 #include <sw-backend/store.h>
31 #include <sw-backend/internals.h>
39 AlgoType key2algo(DataType type)
42 case DataType::KEY_RSA_PRIVATE:
43 case DataType::KEY_RSA_PUBLIC:
44 return AlgoType::RSA_SV;
46 case DataType::KEY_DSA_PRIVATE:
47 case DataType::KEY_DSA_PUBLIC:
48 return AlgoType::DSA_SV;
50 case DataType::KEY_ECDSA_PRIVATE:
51 case DataType::KEY_ECDSA_PUBLIC:
52 return AlgoType::ECDSA_SV;
55 ThrowErr(Exc::Crypto::InputParam, "Invalid key type: ", type);
59 } // namespace anonymous
61 Token BData::derive(const CryptoAlgorithm &, const Password &, const RawBuffer &)
66 RawBuffer SKey::encrypt(const CryptoAlgorithm &alg, const RawBuffer &data)
68 return Internals::symmetricEncrypt(getBinary(), alg, data);
70 RawBuffer SKey::decrypt(const CryptoAlgorithm &alg, const RawBuffer &cipher)
72 return Internals::symmetricDecrypt(getBinary(), alg, cipher);
76 const CryptoAlgorithm &alg,
77 const RawBuffer &message)
79 CryptoAlgorithm algWithType(alg);
80 algWithType.setParam(ParamName::ALGO_TYPE, key2algo(m_type));
81 return Internals::sign(getEvpShPtr().get(), algWithType, message);
84 int AKey::verify(const CryptoAlgorithm &alg, const RawBuffer &message,
85 const RawBuffer &sign)
87 CryptoAlgorithm algWithType(alg);
88 EVP_PKEY *evp = getEvpShPtr().get();
91 // setup algorithm type basing on evp key type if it doesn't exist
92 if (!algWithType.getParam(ParamName::ALGO_TYPE, type)) {
93 int subType = EVP_PKEY_type(EVP_PKEY_id(evp));
97 type = AlgoType::RSA_SV;
101 type = AlgoType::DSA_SV;
105 type = AlgoType::ECDSA_SV;
109 ThrowErr(Exc::Crypto::InputParam, "Invalid key type: ", subType);
112 algWithType.setParam(ParamName::ALGO_TYPE, type);
115 return Internals::verify(evp, algWithType, message, sign);
118 RawBuffer AKey::encrypt(const CryptoAlgorithm &alg, const RawBuffer &data)
120 return Internals::asymmetricEncrypt(getEvpShPtr(), alg, data);
123 RawBuffer AKey::decrypt(const CryptoAlgorithm &alg, const RawBuffer &data)
125 return Internals::asymmetricDecrypt(getEvpShPtr(), alg, data);
128 Token AKey::derive(const CryptoAlgorithm &alg, const Password &pass, const RawBuffer & /* digest */)
130 auto data = Internals::deriveECDH(getEvpShPtr(), alg);
132 return Token(backendId(), data.type, Store::pack(data.buffer, pass));
135 EvpShPtr AKey::getEvpShPtr()
140 EVP_PKEY *pkey = NULL;
141 auto bio = uptr<BIO_free_all>(BIO_new(BIO_s_mem()));
143 LogDebug("Start to parse key:");
146 (void)BIO_reset(bio.get());
147 BIO_write(bio.get(), m_raw.data(), m_raw.size());
148 pkey = d2i_PrivateKey_bio(bio.get(), NULL);
149 LogDebug("Trying d2i_PrivateKey_bio Status: " << (void *)pkey);
153 (void)BIO_reset(bio.get());
154 BIO_write(bio.get(), m_raw.data(), m_raw.size());
155 pkey = d2i_PUBKEY_bio(bio.get(), NULL);
156 LogDebug("Trying d2i_PUBKEY_bio Status: " << (void *)pkey);
160 ThrowErr(Exc::Crypto::InternalError, "Failed to parse key");
162 m_evp.reset(pkey, EVP_PKEY_free);
166 EvpShPtr Cert::getEvpShPtr()
171 int size = static_cast<int>(m_raw.size());
172 const unsigned char *ptr = reinterpret_cast<const unsigned char *>
175 X509 *x509 = d2i_X509(NULL, &ptr, size);
178 ThrowErr(Exc::Crypto::InternalError, "Failed to parse certificate.");
180 m_evp.reset(X509_get_pubkey(x509), EVP_PKEY_free);
186 } // namespace Crypto