Version 0.1.18
[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 namespace {
41
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;
53     default:
54         ThrowErr(Exc::Crypto::InputParam, "Invalid key type: ", type);
55     }
56 }
57
58 } // namespace anonymous
59
60 typedef std::unique_ptr<BIO, std::function<void(BIO*)>> BioUniquePtr;
61
62 RawBuffer SKey::getBinary() const {
63     return m_key;
64 }
65
66 RawBuffer SKey::encrypt(const CryptoAlgorithm &alg, const RawBuffer &data)
67 {
68     return Internals::symmetricEncrypt(getBinary(), alg, data);
69 }
70 RawBuffer SKey::decrypt(const CryptoAlgorithm &alg, const RawBuffer &cipher)
71 {
72     return Internals::symmetricDecrypt(getBinary(), alg, cipher);
73 }
74
75 RawBuffer AKey::sign(
76     const CryptoAlgorithm &alg,
77     const RawBuffer &message)
78 {
79     CryptoAlgorithm algWithType(alg);
80     algWithType.setParam(ParamName::ALGO_TYPE, key2algo(m_type));
81     return Internals::sign(getEvpShPtr().get(), algWithType, message);
82 }
83
84 RawBuffer AKey::getBinary() const {
85     return m_key;
86 }
87
88 int AKey::verify(const CryptoAlgorithm &alg, const RawBuffer &message, const RawBuffer &sign) {
89     CryptoAlgorithm algWithType(alg);
90     EVP_PKEY* evp = getEvpShPtr().get();
91     AlgoType type;
92
93     // setup algorithm type basing on evp key type if it doesn't exist
94     if(!algWithType.getParam(ParamName::ALGO_TYPE, type)) {
95         int subType = EVP_PKEY_type(evp->type);
96         switch(subType) {
97         case EVP_PKEY_RSA:
98             type = AlgoType::RSA_SV; break;
99         case EVP_PKEY_DSA:
100             type = AlgoType::DSA_SV; break;
101         case EVP_PKEY_EC:
102             type = AlgoType::ECDSA_SV; break;
103         default:
104             ThrowErr(Exc::Crypto::InputParam, "Invalid key type: ", subType);
105         }
106         algWithType.setParam(ParamName::ALGO_TYPE, type);
107     }
108     return Internals::verify(evp, algWithType, message, sign);
109 }
110
111 RawBuffer AKey::encrypt(const CryptoAlgorithm &alg, const RawBuffer &data)
112 {
113     return Internals::asymmetricEncrypt(getEvpShPtr(), alg, data);
114 }
115
116 RawBuffer AKey::decrypt(const CryptoAlgorithm &alg, const RawBuffer &data)
117 {
118     return Internals::asymmetricDecrypt(getEvpShPtr(), alg, data);
119 }
120
121 EvpShPtr AKey::getEvpShPtr() {
122     if (m_evp)
123         return m_evp;
124
125     EVP_PKEY *pkey = NULL;
126     BioUniquePtr bio(BIO_new(BIO_s_mem()), BIO_free_all);
127
128     LogDebug("Start to parse key:");
129
130     if (!pkey) {
131         (void)BIO_reset(bio.get());
132         BIO_write(bio.get(), m_key.data(), m_key.size());
133         pkey = d2i_PrivateKey_bio(bio.get(), NULL);
134         LogDebug("Trying d2i_PrivateKey_bio Status: " << (void*)pkey);
135     }
136
137     if (!pkey) {
138         (void)BIO_reset(bio.get());
139         BIO_write(bio.get(), m_key.data(), m_key.size());
140         pkey = d2i_PUBKEY_bio(bio.get(), NULL);
141         LogDebug("Trying d2i_PUBKEY_bio Status: " << (void*)pkey);
142     }
143
144     if (!pkey) {
145         ThrowErr(Exc::Crypto::InternalError, "Failed to parse key");
146     }
147
148     m_evp.reset(pkey, EVP_PKEY_free);
149     return m_evp;
150 }
151
152 EvpShPtr Cert::getEvpShPtr() {
153     if (m_evp)
154         return m_evp;
155
156     int size = static_cast<int>(m_key.size());
157     const unsigned char *ptr = reinterpret_cast<const unsigned char *>(m_key.data());
158
159     X509 *x509 = d2i_X509(NULL, &ptr, size);
160
161     if (!x509) {
162         ThrowErr(Exc::Crypto::InternalError, "Failed to parse certificate.");
163     }
164
165     m_evp.reset(X509_get_pubkey(x509), EVP_PKEY_free);
166     X509_free(x509);
167     return m_evp;
168 }
169
170 } // namespace SW
171 } // namespace Crypto
172 } // namespace CKM
173