2 * Copyright (c) 2000 - 2013 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 Bartlomiej Grzelewski (b.grzelewski@samsung.com)
20 * @brief Sample service implementation.
32 #include <ckm/ckm-raw-buffer.h>
33 #include <ckm/ckm-password.h>
35 #define KEY_MANAGER_API __attribute__((visibility("default")))
39 // used to pass password and raw key data
40 typedef std::vector<RawBuffer> RawBufferVector;
41 typedef std::string Alias;
42 typedef std::string Label;
43 typedef std::vector<Alias> AliasVector;
45 enum class KeyType : int {
56 enum class DataFormat : int {
62 enum class ElipticCurve : int {
68 enum class CertificateFieldId : int {
74 Policy(const Password &pass = Password(), bool extract = true)
76 , extractable(extract)
79 Password password; // byte array used to encrypt data inside CKM
80 bool extractable; // if true key may be extracted from storage
83 enum class HashAlgorithm : int {
91 enum class RSAPaddingAlgorithm : int {
97 enum class DBCMAlgType : int {
103 typedef int PermissionMask;
104 enum Permission: int {
108 // keep in sync with ckmc_permission_e !
111 const char * ErrorToString(int error);
113 // algorithm parameters
114 enum class ParamName : int {
115 ALGO_TYPE = 1, // If there's no such param, the service will try to deduce the algorithm
116 // type from the key.
118 // encryption & decryption
127 GEN_EC, // elliptic curve (ElipticCurve)
130 SV_HASH_ALGO = 301, // hash algorithm (HashAlgorithm)
131 SV_RSA_PADDING, // RSA padding (RSAPaddingAlgorithm)
134 // algorithm types (ALGO_TYPE param)
135 enum class AlgoType : int {
149 // cryptographic algorithm description
150 class KEY_MANAGER_API CryptoAlgorithm {
152 template <typename T>
153 bool getParam(ParamName name, T& value) const;
155 // returns false if param 'name' already exists
156 template <typename T>
157 bool addParam(ParamName name, const T& value);
162 virtual bool getBuffer(RawBuffer&) const { return false; }
163 virtual bool getInt(uint64_t&) const { return false; }
164 virtual ~BaseParam() {}
169 typedef std::shared_ptr<BaseParam> BaseParamPtr;
171 class BufferParam : public BaseParam {
173 bool getBuffer(RawBuffer& buffer) const;
174 static BaseParamPtr create(const RawBuffer& buffer);
176 explicit BufferParam(const RawBuffer& value) : m_buffer(value) {}
181 class IntParam : public BaseParam {
183 static BaseParamPtr create(uint64_t value);
184 bool getInt(uint64_t& value) const;
186 explicit IntParam(uint64_t value) : m_int(value) {}
191 std::map<ParamName, BaseParamPtr> m_params;
194 template <typename T>
195 bool CryptoAlgorithm::getParam(ParamName name, T& value) const {
196 auto param = m_params.find(name);
197 if (param == m_params.end())
200 assert(param->second);
203 if (param->second->getInt(valueTmp)) {
204 value = static_cast<T>(valueTmp);
211 bool CryptoAlgorithm::getParam(ParamName name, RawBuffer& value) const;
213 template <typename T>
214 bool CryptoAlgorithm::addParam(ParamName name, const T& value) {
215 return m_params.emplace(name, IntParam::create(static_cast<uint64_t>(value))).second;
219 bool CryptoAlgorithm::addParam(ParamName name, const RawBuffer& value);