1e81a3b0f7926beede23cfa0c7dde18b97d6d42d
[platform/core/security/key-manager.git] / src / manager / crypto / sw-backend / key.cpp
1 /*
2  *  Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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
15  */
16 /*
17  * @file       key.cpp
18  * @author     BartÅ‚omiej Grzelewski (b.grzelewski@samsung.com)
19  * @version    1.0
20  */
21 #include <memory>
22
23 #include <openssl/bio.h>
24 #include <openssl/evp.h>
25 #include <openssl/x509.h>
26
27 #include <dpl/log/log.h>
28
29 #include <generic-backend/exception.h>
30 #include <sw-backend/key.h>
31 #include <sw-backend/internals.h>
32
33 #define EVP_SUCCESS 1   // DO NOTCHANGE THIS VALUE
34 #define EVP_FAIL    0   // DO NOTCHANGE THIS VALUE
35
36 namespace CKM {
37 namespace Crypto {
38 namespace SW {
39
40 typedef std::unique_ptr<BIO, std::function<void(BIO*)>> BioUniquePtr;
41
42 RawBuffer SKey::getBinary() const {
43     return m_key;
44 }
45
46 RawBuffer SKey::encrypt(const CryptoAlgorithm &alg, const RawBuffer &data)
47 {
48     return Internals::symmetricEncrypt(getBinary(), alg, data);
49 }
50 RawBuffer SKey::decrypt(const CryptoAlgorithm &alg, const RawBuffer &cipher)
51 {
52     return Internals::symmetricDecrypt(getBinary(), alg, cipher);
53 }
54
55 RawBuffer AKey::sign(
56     const CryptoAlgorithm &alg,
57     const RawBuffer &message)
58 {
59     return Internals::sign(getEvpShPtr().get(), alg, message);
60 }
61
62 RawBuffer AKey::getBinary() const {
63     return m_key;
64 }
65
66 int AKey::verify(const CryptoAlgorithm &alg, const RawBuffer &message, const RawBuffer &sign) {
67     return Internals::verify(getEvpShPtr().get(), alg, message, sign);
68 }
69
70 EvpShPtr AKey::getEvpShPtr() {
71     if (m_evp)
72         return m_evp;
73
74     EVP_PKEY *pkey = NULL;
75     BioUniquePtr bio(BIO_new(BIO_s_mem()), BIO_free_all);
76
77     LogDebug("Start to parse key:");
78
79     if (!pkey) {
80         (void)BIO_reset(bio.get());
81         BIO_write(bio.get(), m_key.data(), m_key.size());
82         pkey = d2i_PrivateKey_bio(bio.get(), NULL);
83         LogDebug("Trying d2i_PrivateKey_bio Status: " << (void*)pkey);
84     }
85
86     if (!pkey) {
87         (void)BIO_reset(bio.get());
88         BIO_write(bio.get(), m_key.data(), m_key.size());
89         pkey = d2i_PUBKEY_bio(bio.get(), NULL);
90         LogDebug("Trying d2i_PUBKEY_bio Status: " << (void*)pkey);
91     }
92
93     if (!pkey) {
94         ThrowErr(Exc::Crypto::InternalError, "Failed to parse key");
95     }
96
97     m_evp.reset(pkey, EVP_PKEY_free);
98     return m_evp;
99 }
100
101 EvpShPtr Cert::getEvpShPtr() {
102     if (m_evp)
103         return m_evp;
104
105     int size = static_cast<int>(m_key.size());
106     const unsigned char *ptr = reinterpret_cast<const unsigned char *>(m_key.data());
107
108     X509 *x509 = d2i_X509(NULL, &ptr, size);
109
110     if (!x509) {
111         ThrowErr(Exc::Crypto::InternalError, "Failed to parse certificate.");
112     }
113
114     m_evp.reset(X509_get_pubkey(x509), EVP_PKEY_free);
115     X509_free(x509);
116     return m_evp;
117 }
118
119 } // namespace SW
120 } // namespace Crypto
121 } // namespace CKM
122