Update parameter list API
[platform/core/security/key-manager.git] / src / include / ckm / ckm-type.h
1 /*
2  *  Copyright (c) 2000 - 2013 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        ckm-type.h
18  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
19  * @version     1.0
20  * @brief       Sample service implementation.
21  */
22 #pragma once
23
24 #include <stdint.h>
25 #include <cassert>
26
27 #include <string>
28 #include <vector>
29 #include <map>
30 #include <memory>
31
32 #include <ckm/ckm-raw-buffer.h>
33 #include <ckm/ckm-password.h>
34
35 #define KEY_MANAGER_API __attribute__((visibility("default")))
36
37 namespace CKM {
38
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;
44
45 enum class KeyType : int {
46     KEY_NONE = 0,
47     KEY_RSA_PUBLIC,
48     KEY_RSA_PRIVATE,
49     KEY_ECDSA_PUBLIC,
50     KEY_ECDSA_PRIVATE,
51     KEY_DSA_PUBLIC,
52     KEY_DSA_PRIVATE,
53     KEY_AES
54 };
55
56 enum class DataFormat : int {
57     FORM_DER_BASE64 = 0,
58     FORM_DER,
59     FORM_PEM
60 };
61
62 enum class ElipticCurve : int {
63     prime192v1 = 0,
64     prime256v1,
65     secp384r1
66 };
67
68 enum class CertificateFieldId : int {
69     ISSUER = 0,
70     SUBJECT
71 };
72
73 struct Policy {
74     Policy(const Password &pass = Password(), bool extract = true)
75       : password(pass)
76       , extractable(extract)
77     {}
78     virtual ~Policy(){}
79     Password password;  // byte array used to encrypt data inside CKM
80     bool extractable;   // if true key may be extracted from storage
81 };
82
83 enum class HashAlgorithm : int {
84     NONE = 0,
85     SHA1,
86     SHA256,
87     SHA384,
88     SHA512
89 };
90
91 enum class RSAPaddingAlgorithm : int {
92     NONE = 0,
93     PKCS1,
94     X931
95 };
96
97 enum class DBCMAlgType : int {
98     NONE = 0,
99     AES_GCM_256,
100     COUNT
101 };
102
103 typedef int PermissionMask;
104 enum Permission: int {
105     NONE            = 0x00,
106     READ            = 0x01,
107     REMOVE          = 0x02
108     // keep in sync with ckmc_permission_e !
109 };
110
111 const char * ErrorToString(int error);
112
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.
117
118     // encryption & decryption
119     ED_IV = 101,
120     ED_CTR_LEN,
121     ED_AAD,
122     ED_TAG_LEN,
123     ED_LABEL,
124
125     // key generation
126     GEN_KEY_LEN = 201,
127     GEN_EC,             // elliptic curve (ElipticCurve)
128
129     // sign & verify
130     SV_HASH_ALGO = 301, // hash algorithm (HashAlgorithm)
131     SV_RSA_PADDING,     // RSA padding (RSAPaddingAlgorithm)
132
133     // special values marking valid values range
134     FIRST = ALGO_TYPE,
135     LAST = SV_RSA_PADDING
136 };
137
138 // algorithm types (ALGO_TYPE param)
139 enum class AlgoType : int {
140     AES_CTR = 1,
141     AES_CBC,
142     AES_GCM,
143     AES_CFB,
144     RSA_OAEP,
145     RSA_SV,
146     DSA_SV,
147     ECDSA_SV,
148     RSA_GEN,
149     DSA_GEN,
150     ECDSA_GEN,
151 };
152
153 // cryptographic algorithm description
154 class KEY_MANAGER_API CryptoAlgorithm {
155 public:
156     template <typename T>
157     bool getParam(ParamName name, T& value) const;
158
159     // returns false if param 'name' is invalid
160     template <typename T>
161     bool setParam(ParamName name, const T& value);
162
163 protected:
164     class BaseParam {
165     public:
166         virtual bool getBuffer(RawBuffer&) const { return false; }
167         virtual bool getInt(uint64_t&) const { return false; }
168         virtual ~BaseParam() {}
169
170     protected:
171         BaseParam() {}
172     };
173     typedef std::shared_ptr<BaseParam> BaseParamPtr;
174
175     class BufferParam : public BaseParam {
176     public:
177         bool getBuffer(RawBuffer& buffer) const;
178         static BaseParamPtr create(const RawBuffer& buffer);
179     private:
180         explicit BufferParam(const RawBuffer& value) : m_buffer(value) {}
181
182         RawBuffer m_buffer;
183     };
184
185     class IntParam : public BaseParam {
186     public:
187         static BaseParamPtr create(uint64_t value);
188         bool getInt(uint64_t& value) const;
189     private:
190         explicit IntParam(uint64_t value) : m_int(value) {}
191
192         uint64_t m_int;
193     };
194
195     std::map<ParamName, BaseParamPtr> m_params;
196 };
197
198 template <typename T>
199 bool CryptoAlgorithm::getParam(ParamName name, T& value) const {
200     auto param = m_params.find(name);
201     if (param == m_params.end())
202         return false;
203
204     assert(param->second);
205
206     uint64_t valueTmp;
207     if (param->second->getInt(valueTmp)) {
208         value = static_cast<T>(valueTmp);
209         return true;
210     }
211     return false;
212 }
213
214 template <>
215 bool CryptoAlgorithm::getParam(ParamName name, RawBuffer& value) const;
216
217 template <typename T>
218 bool CryptoAlgorithm::setParam(ParamName name, const T& value) {
219     if (name < ParamName::FIRST || name > ParamName::LAST)
220         return false;
221     m_params[name] = IntParam::create(static_cast<uint64_t>(value));
222     return true;
223 }
224
225 template <>
226 bool CryptoAlgorithm::setParam(ParamName name, const RawBuffer& value);
227
228 } // namespace CKM
229