a79b95eb87f94175a9b0e3c7dfb9608b77dbabf4
[platform/core/security/key-manager.git] / src / include / ckmc / ckmc-type.h
1 /*
2  *  Copyright (c) 2000 - 2014 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        ckmc-type.h
18  * @version     1.0
19  * @brief       Definitions of struct for the Key Manager's CAPI and their utility functions.
20  */
21
22 #ifndef __TIZEN_CORE_CKMC_TYPE_H
23 #define __TIZEN_CORE_CKMC_TYPE_H
24
25 #include <stddef.h>
26 #include <ckmc/ckmc-error.h>
27
28 #define KEY_MANAGER_CAPI __attribute__((visibility("default")))
29
30
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
35 /**
36  * @addtogroup CAPI_KEY_MANAGER_TYPES_MODULE
37  * @{
38  */
39
40 /**
41  * @brief Enumeration for key types of key manager.
42  * @since_tizen 2.3
43  */
44 typedef enum __ckmc_key_type {
45     CKMC_KEY_NONE = 0,       /**< key type not specified */
46     CKMC_KEY_RSA_PUBLIC,     /**< RSA public key */
47     CKMC_KEY_RSA_PRIVATE,    /**< RSA private key */
48     CKMC_KEY_ECDSA_PUBLIC,   /**< ECDSA public key */
49     CKMC_KEY_ECDSA_PRIVATE,  /**< ECDSA private key */
50 } ckmc_key_type_e;
51
52 /**
53  * @brief Enumeration for data format.
54  * @since_tizen 2.3
55  */
56 typedef enum __ckmc_data_format {
57     CKMC_FORM_DER_BASE64 = 0,  /**< DER format base64 encoded data */
58     CKMC_FORM_DER,             /**< DER encoded data */
59     CKMC_FORM_PEM              /**< PEM encoded data. It consists of the DER format base64 encoded with additional header and footer lines. */
60 } ckmc_data_format_e;
61
62 /**
63  * @brief Enumeration for elliptic curve.
64  * @since_tizen 2.3
65  */
66 typedef enum __ckmc_ec_type {
67     CKMC_EC_PRIME192V1 = 0, /**< Elliptic curve domain "secp192r1" listed in "SEC 2" recommended elliptic curve domain  */
68     CKMC_EC_PRIME256V1,     /**< "SEC 2" recommended elliptic curve domain - secp256r1 */
69     CKMC_EC_SECP384R1       /**< NIST curve P-384 (covers "secp384r1", the elliptic curve domain listed in See SEC 2 */
70 } ckmc_ec_type_e;
71
72 /**
73  * @brief Enumeration for hash algorithm.
74  * @since_tizen 2.3
75  */
76 typedef enum __ckmc_hash_algo {
77     CKMC_HASH_SHA1 = 0, /**< Hash Algorithm SHA1  */
78     CKMC_HASH_SHA256,   /**< Hash Algorithm SHA256  */
79     CKMC_HASH_SHA384,   /**< Hash Algorithm SHA384  */
80     CKMC_HASH_SHA512    /**< Hash Algorithm SHA512  */
81 } ckmc_hash_algo_e;
82
83 /**
84  * @brief Enumeration for RSA padding algorithm.
85  * @since_tizen 2.3
86  */
87 typedef enum __ckmc_rsa_padding_algo {
88     CKMC_PKCS1_PADDING = 0, /**< PKCS#1 Padding */
89     CKMC_X931_PADDING       /**< X9.31 padding */
90 } ckmc_rsa_padding_algo_e;
91
92 /**
93  * @brief the structure for binary buffer used in key manager CAPI.
94  * @since_tizen 2.3
95  */
96 typedef struct __ckmc_raw_buff {
97         unsigned char* data; /**< Byte array containing binary data */
98         size_t size;         /**< The size of the binary data */
99 } ckmc_raw_buffer_s;
100
101 /**
102  * @brief The structure for a policy for storing key/certificate/binary data.
103  * @since_tizen 2.3
104  */
105 typedef struct __ckmc_policy {
106         char* password;   /**< Byte array used to encrypt data inside CKM. If it is not null, the data(or key, or certificate) is stored encrypted with this password inside key manager */
107         bool extractable; /**< If true key may be extracted from storage */
108 } ckmc_policy_s;
109
110 /**
111  * @brief The structure for key used in key manager CAPI.
112  * @since_tizen 2.3
113  */
114 typedef struct __ckmc_key {
115         unsigned char* raw_key;   /**< Byte array of key. raw_key may be encrypted with password */
116         size_t key_size;          /**< The byte size of raw_key */
117         ckmc_key_type_e key_type; /**< The raw_key's type */
118         char* password;           /**< Byte array used to decrypt data raw_key inside key manager. */
119 } ckmc_key_s;
120
121 /**
122  * @brief The structure for certificate used in key manager CAPI.
123  * @since_tizen 2.3
124  */
125 typedef struct __ckmc_cert {
126         unsigned char* raw_cert;  /**< Byte array of certificate */
127         size_t cert_size;         /**< Byte size of raw_cert */
128         ckmc_data_format_e data_format; /**< Raw_cert's encoding format */
129 } ckmc_cert_s;
130
131 /**
132  * @brief The structure for linked list of alias.
133  * @since_tizen 2.3
134  */
135 typedef struct __ckmc_alias_list {
136         char *alias;  /**< The name of key, certificate or data stored in key manager */
137         struct __ckmc_alias_list *next; /**< The pointer pointing to the next ckmc_alias_list_s */
138 } ckmc_alias_list_s;
139
140 /**
141  * @brief The structure for linked list of ckmc_cert_s
142  * @since_tizen 2.3
143  */
144 typedef struct __ckmc_cert_list {
145         ckmc_cert_s *cert; /**< The pointer of ckmc_cert_s */
146         struct __ckmc_cert_list *next; /**< The pointer pointing to the next ckmc_cert_list_s */
147 } ckmc_cert_list_s;
148
149 /**
150  * @internal
151  * @brief Creates a new @a ckmc_key_s handle and returns it.
152  *
153  * @since_tizen 2.3
154  *
155  * @remarks You must destroy the newly created @a ckmc_key_s by calling ckmc_key_free() if it is no longer needed.
156  *
157  * @param[in] raw_key  The byte array of key \n
158  *                     @a raw_key may be encrypted with password.
159  * @param[in] key_size The byte size of @a raw_key
160  * @param[in] key_type The @a raw_key's type
161  * @param[in] password The byte array used to decrypt @a raw_key inside key manager \n
162  *                     If @a raw_key is not encrypted, @a password can be null.
163  * @param[out] ppkey   The pointer to a newly created @a ckmc_key_s handle
164  *
165  * @return @c 0 on success,
166  *         otherwise a negative error value
167  *
168  * @retval #CKMC_ERROR_INVALID_PARAMETER Input parameter is invalid
169  * @retval #CKMC_ERROR_OUT_OF_MEMORY     Not enough memory
170  *
171  * @see ckmc_key_free()
172  * @see #ckmc_key_s
173  */
174 int ckmc_key_new(unsigned char *raw_key, size_t key_size,
175         ckmc_key_type_e key_type, char *password, ckmc_key_s **ppkey);
176
177 /**
178  * @brief Destroys the @a ckmc_key_s handle and releases all its resources.
179  *
180  * @since_tizen 2.3
181  *
182  * @param[in] key The @a ckmc_key_s handle to destroy
183  *
184  */
185 void ckmc_key_free(ckmc_key_s *key);
186
187 /**
188  * @internal
189  * @brief Creates a new @a ckmc_raw_buffer_s handle and returns it.
190  *
191  * @since_tizen 2.3
192  *
193  * @remarks You must destroy the newly created @a ckmc_raw_buffer_s by calling ckmc_buffer_free() if it is no longer needed.
194  *
195  * @param[in]  data      The byte array of buffer
196  * @param[in]  size      The byte size of buffer
197  * @param[out] ppbuffer  The pointer to a newly created @a ckmc_buffer_s handle
198  *
199  * @return @c 0 on success,
200  *         otherwise a negative error value
201  *
202  * @retval #CKMC_ERROR_INVALID_PARAMETER  Input parameter is invalid
203  * @retval #CKMC_ERROR_OUT_OF_MEMORY      Not enough memory
204  *
205  * @see ckmc_buffer_free()
206  * @see #ckmc_raw_buffer_s
207  */
208 int ckmc_buffer_new(unsigned char *data, size_t size,ckmc_raw_buffer_s **ppbuffer);
209
210 /**
211  * @brief Destroys the @a ckmc_raw_buffer_s handle and releases all its resources.
212  *
213  * @since_tizen 2.3
214  *
215  * @param[in] buffer The @a ckmc_raw_buffer_s handle to destroy
216  *
217  */
218 void ckmc_buffer_free(ckmc_raw_buffer_s *buffer);
219
220 /**
221  * @internal
222  * @brief Creates a new @a ckmc_cert_s handle and returns it.
223  *
224  * @since_tizen 2.3
225  *
226  * @remarks You must destroy the newly created @a ckmc_cert_s by calling ckmc_cert_free() if it is no longer needed.
227  *
228  * @param[in]  raw_cert     The byte array of certificate
229  * @param[in]  cert_size    The byte size of raw_cert
230  * @param[in]  data_format  The encoding format of raw_cert
231  * @param[out] ppcert       The pointer to a newly created @a ckmc_cert_s handle
232  *
233  * @return @c 0 on success,
234  *         otherwise a negative error value
235  *
236  * @retval #CKMC_ERROR_INVALID_PARAMETER  Input parameter is invalid
237  * @retval #CKMC_ERROR_OUT_OF_MEMORY      Not enough memory
238  *
239  * @see ckmc_cert_free()
240  * @see ckmc_load_cert_from_file()
241  * @see ckmc_load_from_pkcs12_file
242  * @see #ckmc_cert_s
243  */
244 int ckmc_cert_new(unsigned char *raw_cert, size_t cert_size,
245         ckmc_data_format_e data_format, ckmc_cert_s **ppcert);
246
247 /**
248  * @brief Destroys the @a ckmc_cert handle and releases all its resources.
249  *
250  * @since_tizen 2.3
251  *
252  * @param[in] cert The @a ckmc_cert_s handle to destroy
253  *
254  * @see ckmc_load_cert_from_file()
255  * @see ckmc_load_from_pkcs12_file
256  */
257 void ckmc_cert_free(ckmc_cert_s *cert);
258
259 /**
260  * @brief Creates a new @a ckmc_cert_s handle from a given file and returns it.
261  *
262  * @since_tizen 2.3
263  *
264  * @remarks You must destroy the newly created @a ckmc_cert_s by calling ckmc_cert_free() if it is no longer needed.
265  *
266  * @param[in]  file_path  The path of certificate file to be loaded \n
267  *                        The only DER or PEM encoded certificate file is supported.
268  * @param[out] cert       The pointer of newly created @a ckmc_cert_s handle
269  *
270  * @return #CKMC_ERROR_NONE on success,
271  *         otherwise a negative error value
272  *
273  * @retval #CKMC_ERROR_NONE                Successful
274  * @retval #CKMC_ERROR_OUT_OF_MEMORY       Not enough memory space
275  * @retval #CKMC_ERROR_INVALID_FORMAT      Invalid certificate file format
276  * @retval #CKMC_ERROR_FILE_ACCESS_DENIED  Provided file does not exist or cannot be accessed
277  *
278  * @see ckmc_cert_free()
279  * @see ckmc_load_from_pkcs12_file()
280  * @see #ckmc_cert_s
281  */
282 int ckmc_load_cert_from_file(const char *file_path, ckmc_cert_s **cert);
283
284 /**
285  * @brief Creates a new @a ckmc_key_s(private key), @a ckmc_cert_s(certificate), and @a ckmc_cert_list_s(CA certificates) handle from a given PKCS#12 file and returns them.
286  *
287  * @since_tizen 2.3
288  *
289  * @remarks You must destroy the newly created @a ckmc_key_s, @a ckmc_cert_s, and @a ckmc_cert_list_s by calling ckmc_key_free(), ckmc_cert_free(), and ckmc_cert_list_all_free() if they are no longer needed.
290  *
291  * @param[in]  file_path    The path of PKCS12 file to be loaded
292  * @param[in]  passphrase   The passphrase used to decrypt the PCKS12 file \n
293  *                          If PKCS12 file is not encrypted, passphrase can be null.
294  * @param[out] private_key  The pointer of newly created @a ckmc_key_s handle for a private key
295  * @param[out] cert         The pointer of newly created @a ckmc_cert_s handle for a certificate \n
296  *                          It is null if the PKCS12 file does not contain a certificate.
297  * @param[out] ca_cert_list The pointer of newly created @a ckmc_cert_list_s handle for CA certificates \n
298  *                          It is null if the PKCS12 file does not contain CA certificates.
299  *
300  * @return #CKMC_ERROR_NONE on success,
301  *         otherwise a negative error value
302  *
303  * @retval #CKMC_ERROR_NONE                Successful
304  * @retval #CKMC_ERROR_OUT_OF_MEMORY       Not enough memory space
305  * @retval #CKMC_ERROR_INVALID_FORMAT      Invalid PKCS12 file format
306  * @retval #CKMC_ERROR_FILE_ACCESS_DENIED  Provided file does not exist or cannot be accessed
307  *
308  * @see ckmc_key_free()
309  * @see ckmc_cert_free()
310  * @see ckmc_cert_list_all_free()
311  * @see #ckmc_key_s
312  * @see #ckmc_cert_s
313  * @see #ckmc_cert_list_s
314  */
315 int ckmc_load_from_pkcs12_file(const char *file_path, const char *passphrase,
316         ckmc_key_s **private_key, ckmc_cert_s **cert,
317         ckmc_cert_list_s **ca_cert_list);
318
319 /**
320  * @internal
321  * @brief Creates a new @a ckmc_alias_list_s handle and returns it.
322  *        The alias pointer in the returned @a ckmc_alias_list_s handle points to the provided characters and next is null.
323  *
324  * @since_tizen 2.3
325  *
326  * @remarks You must destroy the newly created @a ckmc_alias_list_s by calling ckmc_alias_list_free() or ckmc_alias_list_all_free() if it is no longer needed.
327  *
328  * @param[in]  alias        The first item to be set in the newly created @a ckmc_alias_list_s
329  * @param[out] ppalias_list The pointer to a newly created @a ckmc_alias_list_s handle
330  *
331  * @return @c 0 on success,
332  *         otherwise a negative error value
333  *
334  * @retval #CKMC_ERROR_INVALID_PARAMETER Input parameter is invalid
335  * @retval #CKMC_ERROR_OUT_OF_MEMORY     Not enough memory
336  *
337  * @see ckmc_alias_list_all_free()
338  * @see #ckmc_alias_list_s
339  */
340 int ckmc_alias_list_new(char *alias, ckmc_alias_list_s **ppalias_list);
341
342 /**
343  * @internal
344  * @brief Creates a new @a ckmc_alias_list_s handle, adds it to a previous @a ckmc_alias_list_s and returns it.
345  *        The alias pointer in the returned @a ckmc_alias_list_s handle points to the provided characters and next is null.
346  *
347  * @since_tizen 2.3
348  *
349  * @param[in]  previous  The last @a ckmc_alias_list_s handle to which a newly created @a ckmc_alias_list_s is added
350  * @param[in]  alias     The item to be set in the newly created @a ckmc_alias_list_s
351  * @param[out] pplast    The pointer to a newly created and added @a ckmc_alias_list_s handle
352  *
353  * @return @c 0 on success,
354  *         otherwise a negative error value
355  *
356  * @retval #CKMC_ERROR_INVALID_PARAMETER  Input parameter is invalid
357  * @retval #CKMC_ERROR_OUT_OF_MEMORY      Not enough memory
358  *
359  * @see ckmc_alias_list_all_free()
360  * @see #ckmc_alias_list_s
361  */
362 int ckmc_alias_list_add(ckmc_alias_list_s *previous,
363         char *alias, ckmc_alias_list_s **pplast);
364
365 /**
366  * @internal
367  * @brief Destroys the @a ckmc_alias_list_s handle and releases resources of @a ckmc_alias_list_s from the provided first handle cascadingly.
368  *
369  * @since_tizen 2.3
370  *
371  * @remarks It does not destroy an alias itself in @a ckmc_alias_list_s.
372  *
373  * @param[in] first The first @a ckmc_alias_list_s handle to destroy
374  *
375  * @see ckmc_alias_list_all_free()
376  * @see #ckmc_alias_list_s
377  */
378 void ckmc_alias_list_free(ckmc_alias_list_s *first);
379
380 /**
381  * @brief Destroys the @a ckmc_alias_list_s handle and releases all its resources from the provided first handle cascadingly.
382  *
383  * @since_tizen 2.3
384  *
385  * @remarks It also destroys the alias in @a ckmc_alias_list_s.
386  *
387  * @param[in] first The first @a ckmc_alias_list_s handle to destroy
388  *
389  * @see #ckmc_alias_list_s
390  */
391 void ckmc_alias_list_all_free(ckmc_alias_list_s *first);
392
393 /**
394  * @internal
395  * @brief Creates a new @a ckmc_cert_list_s handle and returns it.
396  *        The cert pointer in the returned @a ckmc_cert_list_s handle points to the provided @a ckmc_cert_s and next is null.
397  *
398  * @since_tizen 2.3
399  *
400  * @remarks You must destroy the newly created @a ckmc_cert_list_s by calling ckmc_cert_list_free() or ckmc_cert_list_all_free() if it is no longer needed.
401  *
402  * @param[in]  cert          The first item to be set in the newly created @a ckmc_cert_list_s
403  * @param[out] ppalias_list  The pointer to a newly created @a ckmc_alias_list_s handle
404  *
405  * @return @c 0 on success,
406  *         otherwise a negative error value
407  *
408  * @retval #CKMC_ERROR_INVALID_PARAMETER  Input parameter is invalid
409  * @retval #CKMC_ERROR_OUT_OF_MEMORY      Not enough memory
410  *
411  * @see ckmc_cert_list_all_free()
412  * @see #ckmc_cert_list_s
413  */
414 int ckmc_cert_list_new(ckmc_cert_s *cert, ckmc_cert_list_s **ppalias_list);
415
416 /**
417  * @internal
418  * @brief Creates a new @a ckmc_cert_list_s handle, adds it to a previous @a ckmc_cert_list_s and returns it.
419  *        The cert pointer in the returned @a ckmc_alias_list_s handle points to the provided @a ckmc_cert_s and next is null.
420  *
421  * @since_tizen 2.3
422  *
423  * @param[in]  previous  The last @a ckmc_cert_list_s handle to which a newly created @a ckmc_cert_list_s is added
424  * @param[in]  cert      The item to be set in the newly created @a ckmc_cert_list_s
425  * @param[out] pplast    The pointer to a newly created and added @a ckmc_alias_list_s handle
426  *
427  * @return @c 0 on success,
428  *         otherwise a negative error value
429  *
430  * @retval #CKMC_ERROR_INVALID_PARAMETER  Input parameter is invalid
431  * @retval #CKMC_ERROR_OUT_OF_MEMORY      Not enough memory
432  *
433  * @see ckmc_cert_list_all_free()
434  * @see #ckmc_cert_list_s
435  */
436 int ckmc_cert_list_add(ckmc_cert_list_s *previous,
437         ckmc_cert_s *cert, ckmc_cert_list_s **pplast);
438
439 /**
440  * @internal
441  * @brief Destroys the @a ckmc_cert_list_s handle and releases resources of @a ckmc_cert_list_s from the provided first handle cascadingly.
442  *
443  * @since_tizen 2.3
444  *
445  * @remarks It does not destroy @a ckmc_cert_s itself in @a ckmc_cert_list_s.
446  *
447  * @param[in] first The first @a ckmc_cert_list_s handle to destroy
448  *
449  * @see ckmc_cert_list_all_free()
450  * @see #ckmc_cert_list_s
451  */
452 void ckmc_cert_list_free(ckmc_cert_list_s *first);
453
454 /**
455  * @brief Destroys the @a ckmc_cert_list_s handle and releases all its resources from the provided first handle cascadingly.
456  *
457  * @since_tizen 2.3
458  *
459  * @remarks It also destroys @a ckmc_cert_s in ckmc_cert_list_s.
460  *
461  * @param[in] first The first @a ckmc_cert_list_s handle to destroy
462  *
463  * @see #ckmc_cert_list_s
464  */
465 void ckmc_cert_list_all_free(ckmc_cert_list_s *first);
466
467 /**
468  * @}
469  */
470
471 #ifdef __cplusplus
472 }
473 #endif
474
475 #endif /* __TIZEN_CORE_CKMC_TYPE_H */