03dc266eb3eb22f916367c4fe251b3746fe0e2b0
[platform/core/test/security-tests.git] / src / ckm / unprivileged / encryption-decryption-env.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       encryption-decryption-env.cpp
18  * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
19  * @version    1.0
20  */
21
22 #include <encryption-decryption-env.h>
23
24 using namespace CKM;
25
26 EncryptionError SyncApi::encrypt(ckmc_param_list_h params,
27                                  const char *key_alias,
28                                  const char *password,
29                                  const ckmc_raw_buffer_s& decrypted,
30                                  ckmc_raw_buffer_s **ppencrypted)
31 {
32     return ckmcError2Result(ckmc_encrypt_data(params, key_alias, password, decrypted, ppencrypted));
33 }
34
35 EncryptionError SyncApi::decrypt(ckmc_param_list_h params,
36                                  const char *key_alias,
37                                  const char *password,
38                                  const ckmc_raw_buffer_s& encrypted,
39                                  ckmc_raw_buffer_s **ppdecrypted)
40 {
41     return ckmcError2Result(ckmc_decrypt_data(params, key_alias, password, encrypted, ppdecrypted));
42 }
43
44 EncryptionError SyncApi::ckmcError2Result(int error) {
45     switch (error) {
46     case CKMC_ERROR_NONE:                   return EncryptionError::SUCCESS;
47     case CKMC_ERROR_INVALID_PARAMETER:      return EncryptionError::INVALID_PARAM;
48     case CKMC_ERROR_SERVER_ERROR:           return EncryptionError::SERVER_ERROR;
49     case CKMC_ERROR_DB_ALIAS_UNKNOWN:       return EncryptionError::ALIAS_UNKNOWN;
50     case CKMC_ERROR_AUTHENTICATION_FAILED:  return EncryptionError::AUTH_FAILED;
51     default:                                return EncryptionError::OTHER;
52     }
53 }
54
55
56
57 void AsyncApi::Observer::ReceivedError(int error) {
58     Finished(error);
59 }
60 void AsyncApi::Observer::ReceivedEncrypted(RawBuffer && buffer) {
61     m_buffer = std::move(buffer);
62     Finished();
63 }
64
65 void AsyncApi::Observer::ReceivedDecrypted(RawBuffer && buffer) {
66     m_buffer = std::move(buffer);
67     Finished();
68 }
69
70 void AsyncApi::Observer::WaitForResponse() {
71     std::unique_lock<std::mutex> lock(m_mutex);
72     m_cv.wait(lock, [this] {return m_finished;});
73 }
74 void AsyncApi::Observer::Finished(int error)
75 {
76     m_error = error;
77     m_finished = true;
78     m_cv.notify_one();
79 }
80
81 EncryptionError AsyncApi::crypt(cryptoFn operation,
82                                 ckmc_param_list_h params,
83                                 const char *key_alias,
84                                 const char *password,
85                                 const ckmc_raw_buffer_s& in,
86                                 ckmc_raw_buffer_s **ppout)
87 {
88     // C++ API doesn't have to check that
89     if(!params || !key_alias || !ppout)
90         return EncryptionError::INVALID_PARAM;
91
92     CKM::ManagerAsync mgr;
93     std::shared_ptr<Observer> obs = std::make_shared<Observer>();
94
95     // params
96     const CryptoAlgorithm* ca = reinterpret_cast<const CryptoAlgorithm*>(params);
97
98     // password
99     Password pass;
100     if (password)
101         pass = password;
102
103     // buffer
104     RawBuffer inBuffer(in.data, in.data + in.size);
105
106     // crypto operation
107     (mgr.*operation)(obs, *ca, key_alias, pass, inBuffer);
108     obs->WaitForResponse();
109     if(obs->m_error != CKM_API_SUCCESS)
110         return ckmError2Result(obs->m_error);
111
112     int ret = ckmc_buffer_new(obs->m_buffer.data(), obs->m_buffer.size(), ppout);
113     if (ret != CKMC_ERROR_NONE)
114         return EncryptionError::OTHER;
115
116     return EncryptionError::SUCCESS;
117 }
118
119 EncryptionError AsyncApi::encrypt(ckmc_param_list_h params,
120                                   const char *key_alias,
121                                   const char *password,
122                                   const ckmc_raw_buffer_s& plain,
123                                   ckmc_raw_buffer_s **ppencrypted)
124 {
125     return crypt(&CKM::ManagerAsync::encrypt, params, key_alias, password, plain, ppencrypted);
126 }
127
128 EncryptionError AsyncApi::decrypt(ckmc_param_list_h params,
129                                   const char *key_alias,
130                                   const char *password,
131                                   const ckmc_raw_buffer_s& encrypted,
132                                   ckmc_raw_buffer_s **ppdecrypted)
133 {
134     return crypt(&CKM::ManagerAsync::decrypt, params, key_alias, password, encrypted, ppdecrypted);
135 }
136
137 EncryptionError AsyncApi::ckmError2Result(int error)
138 {
139     switch (error) {
140     case CKM_API_SUCCESS:                      return EncryptionError::SUCCESS;
141     case CKM_API_ERROR_INPUT_PARAM:            return EncryptionError::INVALID_PARAM;
142     case CKM_API_ERROR_SERVER_ERROR:           return EncryptionError::SERVER_ERROR;
143     case CKM_API_ERROR_DB_ALIAS_UNKNOWN:       return EncryptionError::ALIAS_UNKNOWN;
144     case CKM_API_ERROR_AUTHENTICATION_FAILED:  return EncryptionError::AUTH_FAILED;
145     default:                                   return EncryptionError::OTHER;
146     }
147 }