2 * Copyright (c) 2000 - 2014 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
19 * @brief Definitions of struct for the Key Manager's CAPI and their utility functions.
22 #ifndef __TIZEN_CORE_CKMC_TYPE_H
23 #define __TIZEN_CORE_CKMC_TYPE_H
26 #include <ckmc/ckmc-error.h>
28 #define KEY_MANAGER_CAPI __attribute__((visibility("default")))
36 * @addtogroup CAPI_KEY_MANAGER_TYPES_MODULE
41 * alias can be provided as an alias alone, or together with label - in this
42 * case, separator " " (space bar) is used to separate label and alias.
43 * @see key-manager_doc.h
45 extern char const * const ckmc_label_name_separator;
48 * @brief Enumeration for key types of key manager.
51 typedef enum __ckmc_key_type {
52 CKMC_KEY_NONE = 0, /**< key type not specified */
53 CKMC_KEY_RSA_PUBLIC, /**< RSA public key */
54 CKMC_KEY_RSA_PRIVATE, /**< RSA private key */
55 CKMC_KEY_ECDSA_PUBLIC, /**< ECDSA public key */
56 CKMC_KEY_ECDSA_PRIVATE, /**< ECDSA private key */
57 CKMC_KEY_DSA_PUBLIC, /**< DSA public key */
58 CKMC_KEY_DSA_PRIVATE, /**< DSA private key */
59 CKMC_KEY_AES, /**< AES key */
63 * @brief Enumeration for data format.
66 typedef enum __ckmc_data_format {
67 CKMC_FORM_DER_BASE64 = 0, /**< DER format base64 encoded data */
68 CKMC_FORM_DER, /**< DER encoded data */
69 CKMC_FORM_PEM /**< PEM encoded data. It consists of the DER format base64 encoded with additional header and footer lines. */
73 * @brief Enumeration for elliptic curve.
76 typedef enum __ckmc_ec_type {
77 CKMC_EC_PRIME192V1 = 0, /**< Elliptic curve domain "secp192r1" listed in "SEC 2" recommended elliptic curve domain */
78 CKMC_EC_PRIME256V1, /**< "SEC 2" recommended elliptic curve domain - secp256r1 */
79 CKMC_EC_SECP384R1 /**< NIST curve P-384 (covers "secp384r1", the elliptic curve domain listed in See SEC 2 */
83 * @brief Enumeration for hash algorithm.
86 typedef enum __ckmc_hash_algo {
87 CKMC_HASH_NONE = 0, /**< No Hash Algorithm */
88 CKMC_HASH_SHA1, /**< Hash Algorithm SHA1 */
89 CKMC_HASH_SHA256, /**< Hash Algorithm SHA256 */
90 CKMC_HASH_SHA384, /**< Hash Algorithm SHA384 */
91 CKMC_HASH_SHA512 /**< Hash Algorithm SHA512 */
95 * @brief Enumeration for RSA padding algorithm.
98 typedef enum __ckmc_rsa_padding_algo {
99 CKMC_NONE_PADDING = 0, /**< No Padding */
100 CKMC_PKCS1_PADDING, /**< PKCS#1 Padding */
101 CKMC_X931_PADDING /**< X9.31 padding */
102 } ckmc_rsa_padding_algo_e;
105 * @deprecated, use ckmc_permission_e instead
106 * @brief Enumeration for database access rights.
109 typedef enum __ckmc_access_right{
110 CKMC_AR_READ = 0, /**< access right for read*/
111 CKMC_AR_READ_REMOVE /**< access right for read and remove*/
112 } ckmc_access_right_e;
115 * @brief Enumeration for permissions to access/modify alias.
118 typedef enum __ckmc_permission{
119 CKMC_PERMISSION_NONE = 0x00, /**< clear permissions */
120 CKMC_PERMISSION_READ = 0x01, /**< read allowed */
121 CKMC_PERMISSION_REMOVE = 0x02 /**< remove allowed */
125 * @brief the structure for binary buffer used in key manager CAPI.
128 typedef struct __ckmc_raw_buff {
129 unsigned char* data; /**< Byte array containing binary data */
130 size_t size; /**< The size of the binary data */
134 * @brief The structure for a policy for storing key/certificate/binary data.
137 typedef struct __ckmc_policy {
138 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 */
139 bool extractable; /**< If true key may be extracted from storage */
143 * @brief The structure for key used in key manager CAPI.
146 typedef struct __ckmc_key {
147 unsigned char* raw_key; /**< Byte array of key. raw_key may be encrypted with password */
148 size_t key_size; /**< The byte size of raw_key */
149 ckmc_key_type_e key_type; /**< The raw_key's type */
150 char* password; /**< Byte array used to decrypt data raw_key inside key manager. */
154 * @brief The structure for certificate used in key manager CAPI.
157 typedef struct __ckmc_cert {
158 unsigned char* raw_cert; /**< Byte array of certificate */
159 size_t cert_size; /**< Byte size of raw_cert */
160 ckmc_data_format_e data_format; /**< Raw_cert's encoding format */
164 * @brief The structure for linked list of alias.
167 typedef struct __ckmc_alias_list {
168 char *alias; /**< The name of key, certificate or data stored in key manager */
169 struct __ckmc_alias_list *next; /**< The pointer pointing to the next ckmc_alias_list_s */
173 * @brief The structure for linked list of ckmc_cert_s
176 typedef struct __ckmc_cert_list {
177 ckmc_cert_s *cert; /**< The pointer of ckmc_cert_s */
178 struct __ckmc_cert_list *next; /**< The pointer pointing to the next ckmc_cert_list_s */
182 * @brief The structure for PKCS12 used in key manager CAPI.
185 typedef struct __ckmc_pkcs12 {
186 ckmc_key_s *priv_key; /**< private key, may be null */
187 ckmc_cert_s *cert; /**< certificate, may be null */
188 ckmc_cert_list_s *ca_chain; /**< chain certificates list, may be null */
194 * @brief Creates a new @a ckmc_key_s handle and returns it.
198 * @remarks You must destroy the newly created @a ckmc_key_s by calling ckmc_key_free() if it is no longer needed.
200 * @param[in] raw_key The byte array of key \n
201 * @a raw_key may be encrypted with password.
202 * @param[in] key_size The byte size of @a raw_key
203 * @param[in] key_type The @a raw_key's type
204 * @param[in] password The byte array used to decrypt @a raw_key inside key manager \n
205 * If @a raw_key is not encrypted, @a password can be null.
206 * @param[out] ppkey The pointer to a newly created @a ckmc_key_s handle
208 * @return @c 0 on success,
209 * otherwise a negative error value
211 * @retval #CKMC_ERROR_INVALID_PARAMETER Input parameter is invalid
212 * @retval #CKMC_ERROR_OUT_OF_MEMORY Not enough memory
214 * @see ckmc_key_free()
217 int ckmc_key_new(unsigned char *raw_key, size_t key_size,
218 ckmc_key_type_e key_type, char *password, ckmc_key_s **ppkey);
221 * @brief Destroys the @a ckmc_key_s handle and releases all its resources.
225 * @param[in] key The @a ckmc_key_s handle to destroy
228 void ckmc_key_free(ckmc_key_s *key);
232 * @brief Creates a new @a ckmc_raw_buffer_s handle and returns it.
236 * @remarks You must destroy the newly created @a ckmc_raw_buffer_s by calling ckmc_buffer_free() if it is no longer needed.
238 * @param[in] data The byte array of buffer
239 * @param[in] size The byte size of buffer
240 * @param[out] ppbuffer The pointer to a newly created @a ckmc_buffer_s handle
242 * @return @c 0 on success,
243 * otherwise a negative error value
245 * @retval #CKMC_ERROR_INVALID_PARAMETER Input parameter is invalid
246 * @retval #CKMC_ERROR_OUT_OF_MEMORY Not enough memory
248 * @see ckmc_buffer_free()
249 * @see #ckmc_raw_buffer_s
251 int ckmc_buffer_new(unsigned char *data, size_t size,ckmc_raw_buffer_s **ppbuffer);
254 * @brief Destroys the @a ckmc_raw_buffer_s handle and releases all its resources.
258 * @param[in] buffer The @a ckmc_raw_buffer_s handle to destroy
261 void ckmc_buffer_free(ckmc_raw_buffer_s *buffer);
265 * @brief Creates a new @a ckmc_cert_s handle and returns it.
269 * @remarks You must destroy the newly created @a ckmc_cert_s by calling ckmc_cert_free() if it is no longer needed.
271 * @param[in] raw_cert The byte array of certificate
272 * @param[in] cert_size The byte size of raw_cert
273 * @param[in] data_format The encoding format of raw_cert
274 * @param[out] ppcert The pointer to a newly created @a ckmc_cert_s handle
276 * @return @c 0 on success,
277 * otherwise a negative error value
279 * @retval #CKMC_ERROR_INVALID_PARAMETER Input parameter is invalid
280 * @retval #CKMC_ERROR_OUT_OF_MEMORY Not enough memory
282 * @see ckmc_cert_free()
283 * @see ckmc_load_cert_from_file()
286 int ckmc_cert_new(unsigned char *raw_cert, size_t cert_size,
287 ckmc_data_format_e data_format, ckmc_cert_s **ppcert);
290 * @brief Destroys the @a ckmc_cert handle and releases all its resources.
294 * @param[in] cert The @a ckmc_cert_s handle to destroy
296 * @see ckmc_load_cert_from_file()
297 * @see ckmc_load_from_pkcs12_file
299 void ckmc_cert_free(ckmc_cert_s *cert);
302 * @brief Creates a new @a ckmc_cert_s handle from a given file and returns it.
306 * @remarks You must destroy the newly created @a ckmc_cert_s by calling ckmc_cert_free() if it is no longer needed.
308 * @param[in] file_path The path of certificate file to be loaded \n
309 * The only DER or PEM encoded certificate file is supported.
310 * @param[out] cert The pointer of newly created @a ckmc_cert_s handle
312 * @return #CKMC_ERROR_NONE on success,
313 * otherwise a negative error value
315 * @retval #CKMC_ERROR_NONE Successful
316 * @retval #CKMC_ERROR_OUT_OF_MEMORY Not enough memory space
317 * @retval #CKMC_ERROR_INVALID_FORMAT Invalid certificate file format
318 * @retval #CKMC_ERROR_FILE_ACCESS_DENIED Provided file does not exist or cannot be accessed
320 * @see ckmc_cert_free()
323 int ckmc_load_cert_from_file(const char *file_path, ckmc_cert_s **cert);
326 * @brief Creates a new @a ckmc_pkcs12_s handle and returns it.
330 * @remarks You must destroy the newly created @a ckmc_pkcs12_s by calling ckmc_pkcs12_free() if it is no longer needed.
331 * @remarks On success, private_key, cert && ca_cert_list ownership is transferred into newly returned ckmc_pkcs12_s.
333 * @param[in] private_key @a ckmc_key_s handle to the private key (optional)
334 * @param[in] cert @a ckmc_cert_s handle to the certificate (optional)
335 * @param[in] ca_cert_list @a ckmc_cert_list_s list of chain certificate handles (optional)
336 * @param[out] pkcs12_bundle The pointer to a newly created @a ckmc_pkcs12_s handle
338 * @return @c 0 on success,
339 * otherwise a negative error value
341 * @retval #CKMC_ERROR_INVALID_PARAMETER Input parameter is invalid or private_key, cert and ca_cert_list all are null.
342 * @retval #CKMC_ERROR_OUT_OF_MEMORY Not enough memory
344 * @see ckmc_pkcs12_free()
345 * @see ckmc_load_from_pkcs12_file()
346 * @see ckmc_load_from_pkcs12_file2()
349 * @see #ckmc_cert_list_s
350 * @see #ckmc_pkcs12_s
352 int ckmc_pkcs12_new(ckmc_key_s *private_key, ckmc_cert_s *cert,
353 ckmc_cert_list_s *ca_cert_list, ckmc_pkcs12_s **pkcs12_bundle);
356 * @deprecated, use @a ckmc_load_from_pkcs12_file2() instead
357 * @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.
361 * @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.
363 * @param[in] file_path The path of PKCS12 file to be loaded
364 * @param[in] passphrase The passphrase used to decrypt the PCKS12 file \n
365 * If PKCS12 file is not encrypted, passphrase can be null.
366 * @param[out] private_key The pointer of newly created @a ckmc_key_s handle for a private key
367 * @param[out] cert The pointer of newly created @a ckmc_cert_s handle for a certificate \n
368 * It is null if the PKCS12 file does not contain a certificate.
369 * @param[out] ca_cert_list The pointer of newly created @a ckmc_cert_list_s handle for CA certificates \n
370 * It is null if the PKCS12 file does not contain CA certificates.
372 * @return #CKMC_ERROR_NONE on success,
373 * otherwise a negative error value
375 * @retval #CKMC_ERROR_NONE Successful
376 * @retval #CKMC_ERROR_OUT_OF_MEMORY Not enough memory space
377 * @retval #CKMC_ERROR_INVALID_FORMAT Invalid PKCS12 file format
378 * @retval #CKMC_ERROR_FILE_ACCESS_DENIED Provided file does not exist or cannot be accessed
380 * @see ckmc_pkcs12_new()
381 * @see ckmc_load_from_pkcs12_file2()
382 * @see ckmc_key_free()
383 * @see ckmc_cert_free()
384 * @see ckmc_cert_list_all_free()
387 * @see #ckmc_cert_list_s
389 int ckmc_load_from_pkcs12_file(const char *file_path, const char *passphrase,
390 ckmc_key_s **private_key, ckmc_cert_s **cert,
391 ckmc_cert_list_s **ca_cert_list);
394 * @brief Creates a new @a ckmc_pkcs12_s handle from a given PKCS#12 file and returns it.
398 * @remarks You must destroy the newly created @a ckmc_pkcs12_s by calling ckmc_pkcs12_free() if they are no longer needed.
400 * @param[in] file_path The path of PKCS12 file to be loaded
401 * @param[in] passphrase The passphrase used to decrypt the PCKS12 file \n
402 * If PKCS12 file is not encrypted, passphrase can be null.
403 * @param[out] ca_cert_list The pointer of newly created @a ckmc_cert_list_s handle for CA certificates \n
404 * It is null if the PKCS12 file does not contain CA certificates.
406 * @return #CKMC_ERROR_NONE on success,
407 * otherwise a negative error value
409 * @retval #CKMC_ERROR_NONE Successful
410 * @retval #CKMC_ERROR_OUT_OF_MEMORY Not enough memory space
411 * @retval #CKMC_ERROR_INVALID_FORMAT Invalid PKCS12 file format
412 * @retval #CKMC_ERROR_FILE_ACCESS_DENIED Provided file does not exist or cannot be accessed
414 * @see ckmc_pkcs12_free()
415 * @see #ckmc_pkcs12_s
417 int ckmc_load_from_pkcs12_file2(const char *file_path, const char *passphrase, ckmc_pkcs12_s **pkcs12_bundle);
420 * @brief Destroys the @a ckmc_pkcs12_s handle and releases all its resources.
424 * @param[in] pkcs12 The @a ckmc_pkcs12_s handle to destroy
426 * @see ckmc_pkcs12_new()
427 * @see ckmc_load_from_pkcs12_file2()
429 void ckmc_pkcs12_free(ckmc_pkcs12_s *pkcs12);
433 * @brief Creates a new @a ckmc_alias_list_s handle and returns it.
434 * The alias pointer in the returned @a ckmc_alias_list_s handle points to the provided characters and next is null.
438 * @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.
440 * @param[in] alias The first item to be set in the newly created @a ckmc_alias_list_s
441 * @param[out] ppalias_list The pointer to a newly created @a ckmc_alias_list_s handle
443 * @return @c 0 on success,
444 * otherwise a negative error value
446 * @retval #CKMC_ERROR_INVALID_PARAMETER Input parameter is invalid
447 * @retval #CKMC_ERROR_OUT_OF_MEMORY Not enough memory
449 * @see ckmc_alias_list_all_free()
450 * @see #ckmc_alias_list_s
452 int ckmc_alias_list_new(char *alias, ckmc_alias_list_s **ppalias_list);
456 * @brief Creates a new @a ckmc_alias_list_s handle, adds it to a previous @a ckmc_alias_list_s and returns it.
457 * The alias pointer in the returned @a ckmc_alias_list_s handle points to the provided characters and next is null.
461 * @param[in] previous The last @a ckmc_alias_list_s handle to which a newly created @a ckmc_alias_list_s is added
462 * @param[in] alias The item to be set in the newly created @a ckmc_alias_list_s
463 * @param[out] pplast The pointer to a newly created and added @a ckmc_alias_list_s handle
465 * @return @c 0 on success,
466 * otherwise a negative error value
468 * @retval #CKMC_ERROR_INVALID_PARAMETER Input parameter is invalid
469 * @retval #CKMC_ERROR_OUT_OF_MEMORY Not enough memory
471 * @see ckmc_alias_list_all_free()
472 * @see #ckmc_alias_list_s
474 int ckmc_alias_list_add(ckmc_alias_list_s *previous,
475 char *alias, ckmc_alias_list_s **pplast);
479 * @brief Destroys the @a ckmc_alias_list_s handle and releases resources of @a ckmc_alias_list_s from the provided first handle cascadingly.
483 * @remarks It does not destroy an alias itself in @a ckmc_alias_list_s.
485 * @param[in] first The first @a ckmc_alias_list_s handle to destroy
487 * @see ckmc_alias_list_all_free()
488 * @see #ckmc_alias_list_s
490 void ckmc_alias_list_free(ckmc_alias_list_s *first);
493 * @brief Destroys the @a ckmc_alias_list_s handle and releases all its resources from the provided first handle cascadingly.
497 * @remarks It also destroys the alias in @a ckmc_alias_list_s.
499 * @param[in] first The first @a ckmc_alias_list_s handle to destroy
501 * @see #ckmc_alias_list_s
503 void ckmc_alias_list_all_free(ckmc_alias_list_s *first);
507 * @brief Creates a new @a ckmc_cert_list_s handle and returns it.
508 * The cert pointer in the returned @a ckmc_cert_list_s handle points to the provided @a ckmc_cert_s and next is null.
512 * @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.
514 * @param[in] cert The first item to be set in the newly created @a ckmc_cert_list_s
515 * @param[out] ppalias_list The pointer to a newly created @a ckmc_alias_list_s handle
517 * @return @c 0 on success,
518 * otherwise a negative error value
520 * @retval #CKMC_ERROR_INVALID_PARAMETER Input parameter is invalid
521 * @retval #CKMC_ERROR_OUT_OF_MEMORY Not enough memory
523 * @see ckmc_cert_list_all_free()
524 * @see #ckmc_cert_list_s
526 int ckmc_cert_list_new(ckmc_cert_s *cert, ckmc_cert_list_s **ppalias_list);
530 * @brief Creates a new @a ckmc_cert_list_s handle, adds it to a previous @a ckmc_cert_list_s and returns it.
531 * The cert pointer in the returned @a ckmc_alias_list_s handle points to the provided @a ckmc_cert_s and next is null.
535 * @param[in] previous The last @a ckmc_cert_list_s handle to which a newly created @a ckmc_cert_list_s is added
536 * @param[in] cert The item to be set in the newly created @a ckmc_cert_list_s
537 * @param[out] pplast The pointer to a newly created and added @a ckmc_alias_list_s handle
539 * @return @c 0 on success,
540 * otherwise a negative error value
542 * @retval #CKMC_ERROR_INVALID_PARAMETER Input parameter is invalid
543 * @retval #CKMC_ERROR_OUT_OF_MEMORY Not enough memory
545 * @see ckmc_cert_list_all_free()
546 * @see #ckmc_cert_list_s
548 int ckmc_cert_list_add(ckmc_cert_list_s *previous,
549 ckmc_cert_s *cert, ckmc_cert_list_s **pplast);
553 * @brief Destroys the @a ckmc_cert_list_s handle and releases resources of @a ckmc_cert_list_s from the provided first handle cascadingly.
557 * @remarks It does not destroy @a ckmc_cert_s itself in @a ckmc_cert_list_s.
559 * @param[in] first The first @a ckmc_cert_list_s handle to destroy
561 * @see ckmc_cert_list_all_free()
562 * @see #ckmc_cert_list_s
564 void ckmc_cert_list_free(ckmc_cert_list_s *first);
567 * @brief Destroys the @a ckmc_cert_list_s handle and releases all its resources from the provided first handle cascadingly.
571 * @remarks It also destroys @a ckmc_cert_s in ckmc_cert_list_s.
573 * @param[in] first The first @a ckmc_cert_list_s handle to destroy
575 * @see #ckmc_cert_list_s
577 void ckmc_cert_list_all_free(ckmc_cert_list_s *first);
587 #endif /* __TIZEN_CORE_CKMC_TYPE_H */