94df06bf4deb01f4b90873cc8bb2c06bde5783f7
[platform/core/security/cert-svc.git] / vcore / src / cert-svc / ccert.h
1 /**
2  * Copyright (c) 2015 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        ccert.h
18  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
19  * @version     1.0
20  * @brief       This is part of C api for ValidationCore.
21  */
22 #ifndef _CERTSVC_CCERT_H_
23 #define _CERTSVC_CCERT_H_
24
25 #include <time.h>
26
27 #include <cert-svc/cinstance.h>
28 #include <cert-svc/cstring.h>
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33
34 typedef struct CertSvcCertificate_t {
35     size_t privateHandler;
36     CertSvcInstance privateInstance;
37 } CertSvcCertificate;
38
39 typedef struct CertSvcCertificateList_t {
40     size_t privateHandler;
41     CertSvcInstance privateInstance;
42 } CertSvcCertificateList;
43
44 #define MAX_STORE_ENUMS 5
45 typedef enum certImportType_t {
46     NONE_STORE   =  0,
47     VPN_STORE    =  1 << 0,
48     WIFI_STORE   =  1 << 1,
49     EMAIL_STORE  =  1 << 2,
50     SYSTEM_STORE =  1 << 3,
51     ALL_STORE    =  VPN_STORE | WIFI_STORE | EMAIL_STORE | SYSTEM_STORE
52 } CertStoreType;
53
54 typedef enum certStatus_t {
55     DISABLED     =  0,
56     ENABLED      =  1,
57 } CertStatus;
58
59 typedef struct CertSvcStoreCertList_t{
60     char *gname;            // keyfile group name
61     char *title;            // common Name / Alias provided by the user
62     CertStatus status;
63     CertStoreType storeType;
64     struct CertSvcStoreCertList_t *next;
65 }CertSvcStoreCertList;
66
67 typedef enum certType_t {
68     PEM_CRT          = 1 << 0,
69     P12_END_USER     = 1 << 1,
70     P12_INTERMEDIATE = 1 << 2,
71     P12_TRUSTED      = 1 << 3,
72     P12_PKEY         = 1 << 4,
73     INVALID_DATA     = 1 << 5,
74 } CertType;
75
76 typedef enum CertSvcCertificateForm_t {
77 /*    CERTSVC_FORM_PEM, */
78     CERTSVC_FORM_DER,
79     CERTSVC_FORM_DER_BASE64
80 } CertSvcCertificateForm;
81
82 typedef enum CertSvcCertificateField_t {
83     CERTSVC_SUBJECT,
84     CERTSVC_SUBJECT_COMMON_NAME,
85     CERTSVC_SUBJECT_COUNTRY_NAME,
86     CERTSVC_SUBJECT_STATE_NAME,
87     CERTSVC_SUBJECT_ORGANIZATION_NAME,
88     CERTSVC_SUBJECT_ORGANIZATION_UNIT_NAME,
89     CERTSVC_SUBJECT_EMAIL_ADDRESS,
90     CERTSVC_ISSUER,
91     CERTSVC_ISSUER_COMMON_NAME,
92     CERTSVC_ISSUER_COUNTRY_NAME,
93     CERTSVC_ISSUER_STATE_NAME,
94     CERTSVC_ISSUER_ORGANIZATION_NAME,
95     CERTSVC_ISSUER_ORGANIZATION_UNIT_NAME,
96     CERTSVC_VERSION,
97     CERTSVC_SERIAL_NUMBER,
98     CERTSVC_KEY_USAGE,
99     CERTSVC_KEY,
100     CERTSVC_SIGNATURE_ALGORITHM
101 } CertSvcCertificateField;
102
103 typedef enum CertSvcVisibility_t {
104         CERTSVC_VISIBILITY_DEVELOPER            = 1,
105         CERTSVC_VISIBILITY_TEST                 = 1 << 1,
106         CERTSVC_VISIBILITY_PUBLIC               = 1 << 6,
107         CERTSVC_VISIBILITY_PARTNER              = 1 << 7,
108         CERTSVC_VISIBILITY_PARTNER_OPERATOR     = 1 << 8,
109         CERTSVC_VISIBILITY_PARTNER_MANUFACTURER = 1 << 9,
110         CERTSVC_VISIBILITY_PLATFORM             = 1 << 10
111 } CertSvcVisibility;
112
113 /**
114  * This function will return certificate for the unique name identifier passed (gname).
115  *
116  * @param[in] instance CertSvcInstance object.
117  * @param[in] storeType Refers to the store (WIFI_STORE, VPN_STORE, EMAIL_STORE, SSL_STORE).
118  * @oaran[in] gname Refers to the unique name identifier associated for the certificate.
119  * @param[out] certificate Certificate for the gname passed.
120  * @return CERTSVC_SUCCESS, CERTSVC_BAD_ALLOC, CERTSVC_FAIL, CERTSVC_WRONG_ARGUMENT
121  */
122 int certsvc_get_certificate(CertSvcInstance instance,
123                             CertStoreType storeType,
124                             const char *gname,
125                             CertSvcCertificate *certificate);
126
127 /**
128  * Read certificate from file. Certificate must be in PEM/CER/DER format.
129  *
130  * @param[in] instance CertSvcInstance object.
131  * @param[in] location Path to file with certificate file.
132  * @param[out] certificate Certificate id assigned to loaded certificate.
133  * @return CERTSVC_SUCCESS, CERTSVC_BAD_ALLOC, CERTSVC_FAIL, CERTSVC_WRONG_ARGUMENT
134  */
135 int certsvc_certificate_new_from_file(CertSvcInstance instance,
136                                       const char *location,
137                                       CertSvcCertificate *certificate);
138
139 /**
140  * Read certificate stored in memory.
141  *
142  * @param[in] instance CertSvcInstance object.
143  * @param[in] memory Pointer to memory with certificate data.
144  * @param[in] len Size of certificate.
145  * @param[in] form Certificate format.
146  * @param[out] certificate Certificate id assigned to loaded certificate.
147  * @return CERTSVC_SUCCESS, CERTSVC_BAD_ALLOC, CERTSVC_FAIL
148  */
149 int certsvc_certificate_new_from_memory(CertSvcInstance instance,
150                                         const unsigned char *memory,
151                                         size_t len,
152                                         CertSvcCertificateForm form,
153                                         CertSvcCertificate *certificate);
154
155 /**
156  * Free structures connected with certificate.
157  *
158  * @param[in] certificate Certificate id.
159  */
160 void certsvc_certificate_free(CertSvcCertificate certificate);
161
162 /**
163  * Save certificate to file. It saves certificate in DER format.
164  *
165  * @param[in] certificate Certificate id.
166  * @param[in] location Path to file.
167  * @return CERTSVC_SUCCESS, CERTSVC_FAIL, CERTSVC_WRONG_ARGUMENT
168  */
169 int certsvc_certificate_save_file(CertSvcCertificate certificate, const char *location);
170
171 /**
172  * Search certificate with specific data. Result is stored in CertSvcInstance.
173  * This function will erase all preverious results stored in CertSvcInstance but
174  * it will not erase any CertSvcCertificate.
175  *
176  * You can search by fields: CERTSVC_SUBJECT, CERTSVC_ISSUER, CERTSVC_SUBJECT_COMMON_NAME
177  *
178  * @param[in] instance CertSvcInstance object.
179  * @param[in] field Certificate filed name.
180  * @param[in] value Value to search for.
181  * @param[out] handler Handler to search result.
182  * @return CERTSVC_SUCCESS, CERTSVC_BAD_ALLOC, CERTSVC_FAIL, CERTSVC_WRONG_ARGUMENT
183  */
184 int certsvc_certificate_search(CertSvcInstance instance,
185                                CertSvcCertificateField field,
186                                const char *value,
187                                CertSvcCertificateList *handler);
188
189 /**
190  * This function will return certificate id founded by certsvc_certificate_search.
191  * You can call this function multiple times to get all results.
192  *
193  * @param[in] hadler Hander to search results.
194  * @param[in] position
195  * @param[out] certificate Certficate id.
196  * @return CERTSVC_SUCCESS, CERTSVC_WRONG_ARGUMENT
197  */
198 int certsvc_certificate_list_get_one(CertSvcCertificateList handler,
199                                      size_t position,
200                                      CertSvcCertificate *certificate);
201
202 /**
203  * Return number of elements on the list.
204  *
205  * @param[in] handler Handler to certifiacte list.
206  * @param[out] length Size of list.
207  * @return CERTSVC_SUCCESS, CERTSVC_WRONG_ARGUMENT
208  */
209 int certsvc_certificate_list_get_length(CertSvcCertificateList handler,
210                                         size_t *size);
211
212 /**
213  * This function will free list. It will not free certificates on the list.
214  * You may free each certificate with certsvc_certificate_free.
215  *
216  * @param[in] handler Handler to search result.
217  */
218 void certsvc_certificate_list_free(CertSvcCertificateList handler);
219
220 /**
221  * Compare parent certificate subject with child issuer field.
222  *
223  * @param[in] child
224  * @param[in] parent
225  * @param[out] status CERTSVC_TRUE if "signer" was used to sign "child" certificate in other cases it will return CERTSVC_FALSE.
226  * @return CERTSVC_SUCCESS, CERTSVC_WRONG_ARGUMENT
227  */
228 int certsvc_certificate_is_signed_by(CertSvcCertificate child,
229                                      CertSvcCertificate parent,
230                                      int *status);
231
232 /**
233  * Extract specific data from certificate. Data in buffer could be free
234  * by certsvc_free_string function or by
235  * certsvc_instance_free or vcore_instance_reset.
236  *
237  * @param[in] certificate Certificate id.
238  * @param[in] field Type of data to extract.
239  * @param[out] buffer Extracted data.
240  * return CERTSVC_SUCCESS, CERTSVC_BAD_ALLOC, CERTSVC_FAIL
241  */
242 int certsvc_certificate_get_string_field(CertSvcCertificate certificate,
243                                          CertSvcCertificateField field,
244                                          CertSvcString *buffer);
245
246 /**
247  * Extract NOT AFTER data from certificate.
248  *
249  * @param[in] certificate Certificate id.
250  * @param[out] result date
251  * @return CERTSVC_SUCCESS, CERTSVC_FAIL, CERTSVC_WRONG_ARGUMENT
252  */
253 int certsvc_certificate_get_not_after(CertSvcCertificate certificate, time_t *result);
254
255 /**
256  * Extract NOT AFTER data from certificate.
257  *
258  * @param[in] certificate Certificate id.
259  * @param[out] result date
260  * @return CERTSVC_SUCCESS, CERTSVC_FAIL, CERTSVC_WRONG_ARGUMENT
261  */
262 int certsvc_certificate_get_not_before(CertSvcCertificate certificate, time_t *result);
263
264 /**
265  * Check certificate. This fuction compares SUBJECT and ISSUER fields.
266  * TODO: This fuction should also check ROOTCA field in certificate.
267  *
268  * @param[in] certificate Certificate id.
269  * @param[out] status CERTSVC_TRUE or CERTSVC_FALSE
270  * @return CERTSVC_SUCCESS, CERTSVC_WRONG_ARGUMENT
271  */
272 int certsvc_certificate_is_root_ca(CertSvcCertificate certificate, int *status);
273
274 /**
275  * Sort certificates chain. This fuction modifies certificate_array.
276  *
277  * If function success:
278  *  * certificate array will contain end entity certificate as first element
279  *  * last element on the certificate_array will contain Root CA certificate or
280  *    CA certificate (if Root CA is not present).
281  *
282  * @param[in/out] certificate_array
283  * @param[in] size Size of certificate_array
284  * @return CERTSVC_SUCCESS, CERTSVC_FAIL, CERTSVC_WRONG_ARGUMENT, CERTSVC_BAD_ALLOC
285  */
286 int certsvc_certificate_chain_sort(CertSvcCertificate *unsortedChain, size_t size);
287
288 /**
289  * Base64 string will be connected with same instance as message.
290  * You can free base64 string with certsvc_string_free (or certsvc_instance_reset).
291  *
292  * @param[in] message Buffer with input data.
293  * @param[out] base64 Buffer with output data.
294  * @return CERTSVC_SUCCESS, CERTSVC_FAIL, CERTSVC_WRONG_ARGUMENT
295  */
296 int certsvc_base64_encode(CertSvcString message, CertSvcString *base64);
297
298 /**
299  * Message string will be connected with same certsvc instance as base64.
300  * You can free base64 string with certsvc_string_free (or certsvc_instance_reset).
301  *
302  * @param[in] base64 Buffer with input data.
303  * @param[out] message Buffer with output data.
304  * @return CERTSVC_SUCCESS, CERTSVC_FAIL, CERTSVC_WRONG_ARGUMENT
305  */
306 int certsvc_base64_decode(CertSvcString base64, CertSvcString *message);
307
308 /**
309  * Use certificate to verify message.
310  *
311  * @param[in] certificate
312  * @param[in] message
313  * @param[in] algorithm May be set to NULL.
314  * @param[out] status Will be set only if function return CERTSVC_SUCCESS.
315  *                    It could be set to: CERTSVC_SUCCESS, CERTSVC_INVALID_SIGNATURE
316  * @return CERTSVC_SUCCESS, CERTSVC_FAIL, CERTSVC_WRONG_ARGUMENT, CERTSVC_INVALID_ALGORITHM
317  */
318 int certsvc_message_verify(
319     CertSvcCertificate certificate,
320     CertSvcString message,
321     CertSvcString signature,
322     const char *algorithm,
323     int *status);
324
325 /**
326  * This function will create full chain and verify in.
327  *
328  * First argument of function will be treatet as endentity certificate.
329  *
330  * This function will success if root CA certificate is stored in
331  * trusted array.
332  *
333  * @param[in] certificate Certificate to verify.
334  * @param[in] trusted Array with trusted certificates.
335  * @param[in] trustedSize Number of trusted certificates in array.
336  * @param[in] untrusted Array with untrusted certificates.
337  * @param[in] untrustedSize Number of untrusted certificate in array.
338  * @param[out] status Will be set only if function return CERTSVC_SUCCESS.
339  *                    It could be set to: CERTSVC_SUCCESS, CERTSVC_FAIL
340  * @return CERTSVC_SUCCESS, CERTSVC_FAIL, CERTSVC_WRONG_ARGUMENT
341  */
342 int certsvc_certificate_verify(
343     CertSvcCertificate certificate,
344     const CertSvcCertificate *trusted,
345     size_t trustedSize,
346     const CertSvcCertificate *untrusted,
347     size_t untrustedSize,
348     int *status);
349
350 /**
351  * This function will create full chain and verify in.
352  * And this function checks the CA Flag strictly.
353  *
354  * First argument of function will be treatet as endentity certificate.
355  *
356  * This function will success if root CA certificate is stored in
357  * trusted array.
358  *
359  * @param[in] certificate Certificate to verify.
360  * @param[in] trusted Array with trusted certificates.
361  * @param[in] trustedSize Number of trusted certificates in array.
362  * @param[in] untrusted Array with untrusted certificates.
363  * @param[in] untrustedSize Number of untrusted certificate in array.
364  * @param[out] status Will be set only if function return CERTSVC_SUCCESS.
365  *                    It could be set to: CERTSVC_SUCCESS, CERTSVC_FAIL
366  * @return CERTSVC_SUCCESS, CERTSVC_FAIL, CERTSVC_WRONG_ARGUMENT
367  */
368 int certsvc_certificate_verify_with_caflag(
369             CertSvcCertificate certificate,
370             const CertSvcCertificate *trusted,
371             size_t trustedSize,
372             const CertSvcCertificate *untrusted,
373             size_t untrustedSize,
374             int *status);
375
376 /**
377  * This function returns visibility of input certificate.
378  *
379  * @param[in] The root certificate to check visibility.
380  * @param[out] Visibility level
381  * @return CERTSVC_SUCCESS, CERTSVC_FAIL, CERTSVC_IO_ERROR
382  *
383  */
384 int certsvc_certificate_get_visibility(CertSvcCertificate certificate, CertSvcVisibility *visibility);
385
386
387 #ifdef __cplusplus
388 }
389 #endif
390
391 #endif
392