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)
133 // special values marking valid values range
135 LAST = SV_RSA_PADDING
138 // algorithm types (ALGO_TYPE param)
139 enum class AlgoType : int {
154 // cryptographic algorithm description
155 class KEY_MANAGER_API CryptoAlgorithm {
157 template <typename T>
158 bool getParam(ParamName name, T& value) const;
160 // returns false if param 'name' is invalid
161 template <typename T>
162 bool setParam(ParamName name, const T& value);
167 virtual bool getBuffer(RawBuffer&) const { return false; }
168 virtual bool getInt(uint64_t&) const { return false; }
169 virtual ~BaseParam() {}
174 typedef std::shared_ptr<BaseParam> BaseParamPtr;
176 class BufferParam : public BaseParam {
178 bool getBuffer(RawBuffer& buffer) const;
179 static BaseParamPtr create(const RawBuffer& buffer);
181 explicit BufferParam(const RawBuffer& value) : m_buffer(value) {}
186 class IntParam : public BaseParam {
188 static BaseParamPtr create(uint64_t value);
189 bool getInt(uint64_t& value) const;
191 explicit IntParam(uint64_t value) : m_int(value) {}
196 std::map<ParamName, BaseParamPtr> m_params;
199 template <typename T>
200 bool CryptoAlgorithm::getParam(ParamName name, T& value) const {
201 auto param = m_params.find(name);
202 if (param == m_params.end())
205 assert(param->second);
208 if (param->second->getInt(valueTmp)) {
209 value = static_cast<T>(valueTmp);
216 bool CryptoAlgorithm::getParam(ParamName name, RawBuffer& value) const;
218 template <typename T>
219 bool CryptoAlgorithm::setParam(ParamName name, const T& value) {
220 if (name < ParamName::FIRST || name > ParamName::LAST)
222 m_params[name] = IntParam::create(static_cast<uint64_t>(value));
227 bool CryptoAlgorithm::setParam(ParamName name, const RawBuffer& value);