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
128 GEN_EC, // elliptic curve (ElipticCurve)
131 SV_HASH_ALGO = 301, // hash algorithm (HashAlgorithm)
132 SV_RSA_PADDING, // RSA padding (RSAPaddingAlgorithm)
135 // algorithm types (ALGO_TYPE param)
136 enum class AlgoType : int {
147 // cryptographic algorithm description
148 class KEY_MANAGER_API CryptoAlgorithm {
150 template <typename T>
151 bool getParam(ParamName name, T& value) const;
153 // returns false if param 'name' already exists
154 template <typename T>
155 bool addParam(ParamName name, const T& value);
160 virtual bool getBuffer(RawBuffer&) const { return false; }
161 virtual bool getInt(uint64_t&) const { return false; }
162 virtual ~BaseParam() {}
167 typedef std::shared_ptr<BaseParam> BaseParamPtr;
169 class BufferParam : public BaseParam {
171 bool getBuffer(RawBuffer& buffer) const;
172 static BaseParamPtr create(const RawBuffer& buffer);
174 explicit BufferParam(const RawBuffer& value) : m_buffer(value) {}
179 class IntParam : public BaseParam {
181 static BaseParamPtr create(uint64_t value);
182 bool getInt(uint64_t& value) const;
184 explicit IntParam(uint64_t value) : m_int(value) {}
189 std::map<ParamName, BaseParamPtr> m_params;
192 template <typename T>
193 bool CryptoAlgorithm::getParam(ParamName name, T& value) const {
194 auto param = m_params.find(name);
195 if (param == m_params.end())
198 assert(param->second);
201 if (param->second->getInt(valueTmp)) {
202 value = static_cast<T>(valueTmp);
209 bool CryptoAlgorithm::getParam(ParamName name, RawBuffer& value) const;
211 template <typename T>
212 bool CryptoAlgorithm::addParam(ParamName name, const T& value) {
213 return m_params.emplace(name, IntParam::create(static_cast<uint64_t>(value))).second;
217 bool CryptoAlgorithm::addParam(ParamName name, const RawBuffer& value);