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/key.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
40 typedef std::unique_ptr<BIO, std::function<void(BIO*)>> BioUniquePtr;
43 const CryptoAlgorithm &alg,
44 const RawBuffer &message)
46 return Internals::sign(getEvpShPtr().get(), alg, message);
49 bool AKey::verify(const CryptoAlgorithm &alg, const RawBuffer &message, const RawBuffer &sign) {
50 return Internals::verify(getEvpShPtr().get(), alg, message, sign);
53 EvpShPtr AKey::getEvpShPtr() {
57 EVP_PKEY *pkey = NULL;
58 BioUniquePtr bio(BIO_new(BIO_s_mem()), BIO_free_all);
60 LogDebug("Start to parse key:");
63 (void)BIO_reset(bio.get());
64 BIO_write(bio.get(), m_key.data(), m_key.size());
65 pkey = d2i_PrivateKey_bio(bio.get(), NULL);
66 LogDebug("Trying d2i_PrivateKey_bio Status: " << (void*)pkey);
70 (void)BIO_reset(bio.get());
71 BIO_write(bio.get(), m_key.data(), m_key.size());
72 pkey = d2i_PUBKEY_bio(bio.get(), NULL);
73 LogDebug("Trying d2i_PUBKEY_bio Status: " << (void*)pkey);
77 LogError("Failed to parse key");
78 ThrowMsg(Exception::InternalError, "Failed to parse key");
81 m_evp.reset(pkey, EVP_PKEY_free);