30ba3648f6e8366c8e7ae0ff033577fc003019e0
[platform/framework/native/appfw.git] / src / security / inc / FSecCert_CertService.h
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file        FSecCert_CertService.h
20  * @brief       This header file contains the declarations of CertService APIs.
21  *
22  * This header file contains the declarations of CertService APIs.
23  */
24
25 #ifndef _FSEC_CERT_INTERNAL_CERT_SERVICE_H_
26 #define _FSEC_CERT_INTERNAL_CERT_SERVICE_H_
27
28 #include <FOspConfig.h>
29
30 #include "FSecCert_CertTypes.h"
31
32 namespace Tizen { namespace Security { namespace Cert
33 {
34
35 /**
36  * @class       _CertService
37  * @brief       This class is provide Services API for Certificate Management.
38  * @since 2.1
39  *
40  * The %_CertService class is used for to provide Certificate Management's Services API.
41  *
42  * For more information on the class features, see <a href="../com.osp.cppappprogramming.help/html/dev_guide/security/certificate_namespace.htm">Certificates</a>.
43  *
44  */
45 class _OSP_EXPORT_ _CertService
46 {
47 public:
48         /**
49          * This function initializes the Db tables and removes and installs certificates .
50          *
51          * @since 2.1
52          * @return                      An error code.
53          * @exception           E_SUCCESS                       The method is successful.
54          * @exception           E_OUT_OF_MEMORY         The memory is insufficient.
55          * @exception           E_SYSTEM                        A system error has occurred.
56          *                                                                              - File operation failed.
57          */
58         static result InitializeDb(void);
59
60         /**
61          * This function initializes the Db tables. If tables are not created already, this function creates the Db tables.
62          *
63          * @since 2.1
64          * @return                      An error code.
65          * @exception           E_SUCCESS                       The method is successful.
66          * @exception           E_OUT_OF_MEMORY         The memory is insufficient.
67          * @exception           E_SYSTEM                        A system error has occurred.
68          *                                                                              - File operation failed.
69          */
70         static result Initialize(void);
71
72         /**
73          * This function drops the tables and removes all certificate files from the storage.
74          *
75          * @since 2.1
76          * @return                      An error code.
77          * @exception           E_SUCCESS                       The method is successful.
78          * @exception           E_SYSTEM                        A system error has occurred.
79          *                                                                              - File operation failed.
80          */
81         static result DropTables(void);
82
83         /**
84          *  This function resets (deletes and creates) db tables.
85          *
86          * @since 2.1
87          * @return                      An error code.
88          * @exception           E_SUCCESS                       The method is successful.
89          * @exception           E_SYSTEM                        A system error has occurred.
90          *                                                                              - File operation error.
91          *                                                                              - DB operation failed.
92          */
93         static result ResetTables(void);
94
95         /**
96          *  This function removes all certificates.
97          *
98          * @since 2.1
99          * @return                      An error code.
100          * @exception           E_SUCCESS                       The method is successful.
101          * @exception           E_SYSTEM                        A system error has occurred.
102          *                                                                              - File operation error.
103          *                                                                              - DB operation failed.
104          */
105         static result MasterReset(void);
106
107         /**
108          *      This function reinstall Db.
109          *
110          * @since 2.1
111          * @return                      An error code.
112          * @exception           E_SUCCESS                       The method is successful.
113          * @exception           E_SYSTEM                        A system error has occurred.
114          *                                                                              - File operation error.
115          *                                                                              - DB operation failed.
116          */
117         static result ReInitializeDb(void);
118
119         /**
120           * This function installs the certificates into the Db table identified by input type.
121           *
122           * @since 2.1
123           * @return             If success this function returns number of certificates installed in Db tables,
124           *                 -1 in case of failure, 0 in case of no certificates present in directory.
125           * @param[in]          type                            Type of certificates to install in Db table.
126           * @exception          E_SUCCESS                       The method is successful.
127           * @exception          E_INVALID_ARG           The specified input parameter is invalid.
128           * @exception          E_OUT_OF_MEMORY         The memory is insufficient.
129           * @exception          E_SYSTEM                        A system error has occurred.
130           *                                                                             - File operation failed.
131           *                                                                             - DB operation failed.
132           * @remarks        The specific error code can be accessed using the GetLastResult() method.
133           */
134         static int InsertCert(_CaCertType type);
135
136         /**
137          *      This function installs all certificates identified by a given certificate type.
138          *
139          * @since 2.1
140          * @return                      An error code.
141          * @param[in]           certTrustTypes          Certificate type.
142          * @param[out]          pCertCount                      Number of installed certificates by this function.
143          * @exception           E_SUCCESS                       The method is successful.
144          * @exception           E_INVALID_ARG           The specified input parameter is invalid.
145          * @exception           E_OUT_OF_MEMORY         The memory is insufficient.
146          * @exception           E_SYSTEM                        A system error has occurred.
147          *                                                                              - File operation failed.
148          *                                                                              - DB operation failed.
149          */
150         static result InsertCerts(int certTrustTypes, int* pCertCount);
151
152         /**
153          * This function installs the Default root certificate from pBuffer into Db table identified by type and format of the certificate.
154          *
155          * @since 2.1
156          * @return                      An error code.
157          * @param[in]           type                    Type of the certificate to install in Db table.
158          * @param[in]           format                  Format of input certificate pBuffer (X.509).
159          * @param[in]           pCertBuf                Input certificate pBuffer.
160          * @param[in]           certLen                 Input certificate pBuffer length.
161          * @exception           E_SUCCESS               The method is successful.
162          * @exception           E_INVALID_ARG   The specified input parameter is invalid.
163          * @exception           E_OUT_OF_MEMORY The memory is insufficient.
164          * @exception           E_SYSTEM                A system error has occurred.
165          *                                                                      - File operation failed.
166          *                                                                      - DB operation failed.
167          */
168         static result InsertDefaultCaCertificate(_CaCertType type, _CertFormat format, byte* pCertBuf, int certLen);
169
170         /**
171          * This function installs the root certificate from pBuffer into Db table identified by type and format of the certificate.
172          *
173          * @since 2.1
174          * @return                      An error code.
175          * @param[in]           type                            Type of the certificate to install in Db table.
176          * @param[in]           format                          Format of input certificate pBuffer (X.509).
177          * @param[in]           pCertBuf                        Input certificate pBuffer.
178          * @param[in]           certLen                         Input certificate pBuffer length.
179          * @exception           E_SUCCESS                       The method is successful.
180          * @exception           E_INVALID_ARG           The specified input parameter is invalid.
181          * @exception           E_OUT_OF_MEMORY         The memory is insufficient.
182          * @exception           E_SYSTEM                        A system error has occurred.
183          *                                                                              - File operation failed.
184          *                                                                              - DB operation failed.
185          */
186         static result InsertCaCertificate(_CaCertType type, _CertFormat format, byte* pCertBuf, int certLen);
187
188         /**
189          * This function installs the user root certificate from buffer into Db table identified by format of the certificate.
190          *
191          * @since 2.1
192          * @return                      An error code.
193          * @param[in]           format                          Format of input certificate buffer (X.509).
194          * @param[in]           pCert                           Input certificate buffer.
195          * @param[in]           certLen                         Length of input certificate buffer.
196          * @exception           E_SUCCESS                       The method is successful.
197          * @exception           E_INVALID_ARG           The specified input parameter is invalid.
198          * @exception           E_OUT_OF_MEMORY         The memory is insufficient.
199          */
200         static result InsertUserCaCertificate(_CertFormat format, char* pCert, int certLen);
201
202         /**
203          *       This function installs User Root Certificate given by a certificate file path.
204          *
205          * @since 2.1
206          * @return                      An error code.
207          * @param[in]           pFilePath                       Certificate file path where the certificate file is located.
208          * @exception           E_SUCCESS                       The method is successful.
209          * @exception           E_INVALID_ARG           The specified input parameter is invalid.
210          * @exception           E_OUT_OF_MEMORY     The memory is insufficient.
211          * @exception           E_SYSTEM                        A system error has occurred.
212          *                                                                              - File operation failed.
213          *                                                                              - DB operation failed.
214          */
215         static result InsertUserCaCertificate(byte* pFilePath);
216
217         /**
218          * This function installs the Default user CA certificates from storage identified.
219          *
220          * @since 2.1
221          * @return                      This function returns count of installed Device certificates.
222          * @remarks         The specific error code can be accessed using the GetLastResult() method.
223          */
224         static int InsertUserCaCertificatesToRootDb(void);
225
226         /**
227          * This function inserts user certificate chain into DB and store certificate and private key(encrypted) in file system.
228          * The certificate chain should contain chain of certificate, be in order "DeviceCertificate||CA(n)Certificate||.....". Excluding/including Root CA.
229          * If CA is not present in chain then it should be previously installed. Format of Certificate chain/key should be DER encoded.
230          * Only one private key must be supplied in private key parameter.
231          *
232          * @since 2.1
233          * @return                      An error code.
234          * @param[in]           pCertChainBuffer                User certificate chain buffer.
235          * @param[in]           certChainLength                 Certificate chain buffer length.
236          * @param[in]           pUserPrivateKey                 User private Key buffer.
237          * @param[in]           userPrivateKeyLength    User private key length.
238          * @exception           E_SUCCESS                               The method is successful.
239          * @exception           E_INVALID_ARG                   The specified input parameter is invalid.
240          * @exception           E_OUT_OF_MEMORY         The memory is insufficient.
241          * @exception           E_SYSTEM                                A system error has occurred.
242          *                                                                                      - File operation failed.
243          *                                                                                      - DB operation failed.
244          */
245         static result InsertUserCertChainPrivateKey(char* pCertChainBuffer, int certChainLength, char* pUserPrivateKey, int userPrivateKeyLength);
246
247         /**
248          * This function inserts user certificate chain including private key into DB and store certificate and private key(encrypted) in file system.
249          * The certificate chain should contain chain of certificate, be in order "user key || DeviceCertificate||CA(n)Certificate||.....". Excluding/including Root CA.
250          * If CA is not present in chain then it should be previously installed. Format of Certificate chain/key buffer should be DER encoded.
251          * Only one private key must be supplied in private key parameter.
252          *
253          * @since 2.1
254          * @return                      An error code.
255          * @param[in]           pCertChainPrivateKeyBuffer  User private key and certificate chain buffer.
256          * @param[in]           certChainPrivateKeyLength   Private key  and certificate chain buffer length.
257          * @exception           E_SUCCESS                                       The method is successful.
258          * @exception           E_INVALID_ARG                           The specified input parameter is invalid.
259          * @exception           E_OUT_OF_MEMORY             The memory is insufficient.
260          * @exception           E_SYSTEM                                        A system error has occurred.
261          *                                                                                              - File operation failed.
262          *                                                                                              - DB operation failed.
263          */
264         static result InsertCertificateChainWithPrivateKey(char* pCertChainPrivateKeyBuffer, int certChainPrivateKeyLength);
265
266         /**
267          *   This function verify certificate chain using DB.
268          *   Note : It is a certificate chain which contains User certificate without private key.
269          *   Chain can contain multiple certificate including Device, Intermediate and CA Certificate.
270          *   If Root CA/Intermediate CA is not present in DB then it should be provided with chain, to be able to verify the certificate.
271          *
272          * @since 2.1
273          * @return                      An error code.
274          * @param[in]           pCertCtx            Certificate chain context.
275          * @exception           E_SUCCESS                       The method is successful.
276          * @exception           E_INVALID_ARG           The specified input parameter is invalid.
277          * @exception           E_OUT_OF_MEMORY     The memory is insufficient.
278          * @exception           E_SYSTEM                        A system error has occurred.
279          *                                                                              - File operation failed.
280          *                                                                              - DB operation failed.
281          */
282         static result InsertCertificateChainContext(CertChainCtx pCertCtx);
283
284         /**
285          *      This function installs PKCS#12 contents into certificate DB and store certificate and private key(encrypted) in file system.
286          *  Identified by filename and password provided by user.
287          *
288          * @since 2.1
289          * @return                      An error code.
290          * @param[in]           pPkcs12FilePath                 Filename of PKCS#12 content.
291          * @param[in]           pPkcs12ImportPassword   Password of the PKCS#12 content.
292          * @exception           E_SUCCESS                               The method is successful.
293          * @exception           E_INVALID_ARG                   The specified input parameter is invalid.
294          * @exception           E_OUT_OF_MEMORY         The memory is insufficient.
295          * @exception           E_SYSTEM                                A system error has occurred.
296          *                                                                                      - File operation failed.
297          *                                                                                      - DB operation error.
298          *                                                                                      - OpenSSL operation error.
299          */
300         static result InsertPkcs12Content(char* pPkcs12FilePath, char* pPkcs12ImportPassword);
301
302         /**
303          * This function removes the certificates from storage identified by input certificate type.
304          *
305          * @since 2.1
306          * @return                      An error code.
307          * @param[in]           type                            Type of certificates to remove.
308          * @exception           E_SUCCESS                       The method is successful.
309          * @exception           E_INVALID_ARG           The specified input parameter is invalid.
310          * @exception           E_SYSTEM                        A system error has occurred.
311          *                                                                              - File operation error.
312          *                                                                              - DB operation error.
313          */
314         static result RemoveCert(_CaCertType type);
315
316         /**
317          *      This function removes all certificates identified by a given certificate type.
318          *
319          * @since 2.1
320          * @return                      An error code.
321          * @param[in]           certTrustTypes          Certificate types.
322          * @exception           E_SUCCESS                       The method is successful.
323          * @exception           E_INVALID_ARG           The specified input parameter is invalid.
324          * @exception           E_SYSTEM                        A system error has occurred.
325          *                                                                              - File operation error.
326          *                                                                              - DB operation error.
327          */
328         static result RemoveCerts(int certTrustTypes);
329
330         /**
331          *      This function deletes the root certificate
332          *
333          * @since 2.1
334          * @return                      An error code.
335          * @param[in]           type                Ca Cert Type.
336          * @param[in]           pBuffer             Input Buffer.
337          * @param[in]       bufLen              Buffer length.
338          * @exception           E_SUCCESS                       The method is successful.
339          * @exception           E_INVALID_ARG           The specified input parameter is invalid.
340          * @exception           E_SYSTEM                        A system error has occurred.
341          *                                                                              - File operation error.
342          *                                                                              - DB operation error.
343          */
344         static result RemoveCaCertificate(_CaCertType type, char* pBuffer, int bufLen); // if same certificate is in Db, remove the certificate.
345
346         /**
347          *       This function un-installs User Root Certificate given by a certificate ID.
348          *
349          * @since 2.1
350          * @return                      An error code.
351          * @param[in]           certId                          Certificate Id.
352          * @exception           E_SUCCESS                       The method is successful.
353          * @exception           E_INVALID_ARG           The specified input parameter is invalid.
354          * @exception           E_OUT_OF_MEMORY     The memory is insufficient.
355          * @exception           E_SYSTEM                        A system error has occurred.
356          *                                                                              - File operation error.
357          *                                                                              - DB operation error.
358          */
359         static result RemoveUserCaCertificateByCertId(int certId);
360
361         /**
362          * This function removes the Default user CA certificates from the storage identified.
363          *
364          * @since 2.1
365          * @return                      An error code.
366          * @exception           E_SUCCESS                       The method is successful.
367          * @exception           E_SYSTEM                        A system error has occurred.
368          *                                                                              - File operation error.
369          *                                                                              - DB operation error.
370          */
371         static result RemoveUserCaCertificatesFromRootDb(void);
372
373         /**
374          *      This function deletes user certificate chain on the basis of Certificate ID.
375          *
376          * @since 2.1
377          * @return                      An error code.
378          * @param[in]           certId                          Id of certificate as in DB.
379          * @exception           E_SUCCESS                       The method is successful.
380          * @exception           E_INVALID_ARG           The specified input parameter is invalid.
381          * @exception           E_OUT_OF_MEMORY     The memory is insufficient.
382          * @exception           E_SYSTEM                        A system error has occurred.
383          *                                                                              - File operation error.
384          *                                                                              - DB operation error.
385          */
386         static result RemoveUserCertChainByCertId(int certId);
387
388         /**
389          *  This function opens the context identified by calling application.
390          *
391          * @since 2.1
392          * @return                      An error code.
393          * @param[in]           type                            Calling application type.
394          * @param[out]      pCertCtx                    Pointer to context as out parameter.
395          * @exception           E_SUCCESS                       The method is successful.
396          * @exception           E_INVALID_ARG           The specified input parameter is invalid.
397          * @exception           E_OUT_OF_MEMORY     The memory is insufficient.
398          */
399         static result OpenContext(_CertContextType type, CertChainCtx* pCertCtx);
400
401         /**
402          *  This function adds the input certificate in the opened certificate context.
403          *
404          * @since 2.1
405          * @return                      An error code.
406          * @param[in]           certCtx                         Handle to the certificate context.
407          * @param[in]           pCertBuf                        Certificate pBuffer.
408          * @param[in]           certLen                         Certificate pBuffer length.
409          * @exception           E_SUCCESS                       The method is successful.
410          * @exception           E_INVALID_ARG           The specified input parameter is invalid.
411          * @exception           E_OUT_OF_MEMORY     The memory is insufficient.
412          * @exception           E_SYSTEM                        A system error has occurred.
413          *                                                                              - Certificate Link list operation error.
414          */
415         static result AddCertificate(CertChainCtx certCtx, byte* pCertBuf, int certLen);
416
417         /**
418          *  This function verifies the certificate chain in certificate context with respect to installed root certificates in the device.
419          *
420          * @since 2.1
421          * @return                      An error code.
422          * @param[in]           certCtx                 Handle to certificate chain context.
423          * @param[out]          pDomain                 Root certificate domain type.
424          * @exception           E_SUCCESS               The method is successful.
425          * @exception           E_INVALID_ARG   The specified input parameter is invalid.
426          * @exception           E_SYSTEM                A system error has occurred.
427          *                                                                      - Certificate Link list operation error.
428          */
429         static result VerifyChain(CertChainCtx certCtx, _CertDomainType* pDomain);
430
431         /**
432          *  This function verifies a certificate using given Public key.
433          *
434          * @since 2.1
435          * @return                      An error code.
436          * @param[in]           certHandle                      Handle to certificate.
437          * @param[in]           pPublickey                      Certificate Public Key.
438          * @param[in]           keyLen                          Certificate Public Key length.
439          * @exception           E_SUCCESS                       The method is successful.
440          * @exception           E_INVALID_ARG           The specified input parameter is invalid.
441          * @exception           E_SYSTEM                        A system error has occurred.
442          *                                                                              - Certificate Link list operation error.
443          */
444         static result VerifyCert(CertificateHandle certHandle, byte* pPublickey, int keyLen);
445
446         /**
447          *      This function gets number of certificates in certificate chain represented by context.
448          *
449          * @since 2.1
450          * @return                      An error code.
451          * @param[in]           certCtx                         Handle to certificate context.
452          * @param[out]          pDepth                          Chain depth information.
453          * @exception           E_SUCCESS                       The method is successful.
454          * @exception           E_INVALID_ARG           The specified input parameter is invalid.
455          */
456         static result GetChainDepth(CertChainCtx certCtx, int* pDepth);
457
458         /**
459          *      This function gets nth certificate handle of the chain represented by input context
460          *
461          * @since 2.1
462          * @return                      An error code.
463          * @param[in]           certCtx                         Handle to certificate context.
464          * @param[in]           nth                                     Position of certificate.
465          * @param[out]          phCerticate                     Pointer to handle of certificate.
466          * @exception           E_SUCCESS                       The method is successful.
467          * @exception           E_INVALID_ARG           The specified input parameter is invalid.
468          * @exception           E_SYSTEM                        An unexpected system error has occurred.
469          */
470         static result GetNthCert(CertChainCtx certCtx, int nth, CertificateHandle* phCerticate);
471
472         /**
473          *      This function gets certificate pBuffer using the certificate handle.
474          *
475          * @since 2.1
476          * @return                      An error code.
477          * @param[in]           certHandle                      Handle to certificate.
478          * @param[out]          pBuffer                         Pointer to certificate pBuffer.
479          * @param[out]          certLen                         Output pBuffer length of certificate.
480          * @exception           E_SUCCESS                       The method is successful.
481          * @exception           E_INVALID_ARG           The specified input parameter is invalid.
482          * @exception           E_OUT_OF_MEMORY     The memory is insufficient.
483          * @exception           E_SYSTEM                        An unexpected system error has occurred.
484          */
485         static result GetCertBufferN(CertificateHandle certHandle, char*& pBuffer, int* certLen);
486
487         /**
488          *      This function returns the handle of certificate of input binary or base64 certificate pBuffer.
489          *
490          * @since 2.1
491          * @return                      An error code.
492          * @param[in]           pBuffer                         Buffer of certificate.
493          * @param[in]           bufLen                          Length of input pBuffer.
494          * @param[out]          pCertHandle                     Handle to the certificate out.
495          * @exception           E_SUCCESS                       The method is successful.
496          * @exception           E_INVALID_ARG           The specified input parameter is invalid.
497          * @exception           E_OUT_OF_MEMORY     The memory is insufficient.
498          * @exception           E_SYSTEM                        An unexpected system error has occurred.
499          */
500         static result OpenCertificate(char* pBuffer, int bufLen, CertificateHandle* pCertHandle);
501
502         /**
503          *      This function closes the opened context.
504          *
505          * @since 2.1
506          * @return                      An error code.
507          * @param[in]           certCtx                         Handle to certificate context.
508          * @exception           E_SUCCESS                       The method is successful.
509          * @exception           E_INVALID_ARG           The specified input parameter is invalid.
510          */
511         static result CloseContext(CertChainCtx certCtx);
512
513         /**
514          *      This function returns database Id of Ca certificate for given certificate handle.
515          *
516          * @since 2.1
517          * @return                      An error code.
518          * @param[in]           certHandle                      Handle to the certificate.
519          * @param[in]           certType                        Type of certificate store.
520          * @param[out]          certId                          Reference to integer to get certificate data Id.
521          * @exception           E_SUCCESS                       The method is successful.
522          * @exception           E_INVALID_ARG           The specified input parameter is invalid.
523          * @exception           E_OUT_OF_MEMORY     The memory is insufficient.
524          * @exception           E_SYSTEM                        An unexpected system error has occurred.
525          */
526         static result GetCaCertificateId(CertificateHandle certHandle, _CaCertType certType, int& certId);
527
528         /**
529          *      This function returns database Id of User certificate for given certificate handle.
530          *
531          * @since 2.1
532          * @return                      An error code.
533          * @param[in]           certHandle                      Handle to the certificate.
534          * @param[out]          certId                          Reference to integer to get certificate data Id.
535          * @exception           E_SUCCESS                       The method is successful.
536          * @exception           E_INVALID_ARG           The specified input parameter is invalid.
537          * @exception           E_OUT_OF_MEMORY     The memory is insufficient.
538          * @exception           E_SYSTEM                        An unexpected system error has occurred.
539          */
540         static result GetUserCertificateId(CertificateHandle certHandle, int& certId);
541
542         /**
543          *      This function returns information of certificate requested by _CertFieldType parameter.
544          *
545          * @since 2.1
546          * @return                      An error code.
547          * @param[in]           certHandle                      Handle to the certificate.
548          * @param[in]           field                           Type of combination of information required.
549          * @param[out]          pCertInfo                       Pointer to certificate information structure.
550          * @exception           E_SUCCESS                       The method is successful.
551          * @exception           E_INVALID_ARG           The specified input parameter is invalid.
552          * @exception           E_OUT_OF_MEMORY     The memory is insufficient.
553          * @exception           E_SYSTEM                        An unexpected system error has occurred.
554          */
555         static result GetCertInfo(CertificateHandle certHandle, _CertFieldType field, _CertFieldInfos* pCertInfo);
556
557         /**
558          *      This function closes the handle of certificate .
559          *
560          * @since 2.1
561          * @return                      An error code.
562          * @param[in]           pCertHandle                     Handle to the certificate to close.
563          * @exception           E_SUCCESS                       The method is successful.
564          * @exception           E_INVALID_ARG           The specified input parameter is invalid.
565          * @exception           E_SYSTEM                        An unexpected system error has occurred.
566          * @remarks        This function is not applicable to handle which is received by context APIs.
567          */
568         static result CloseCertificate(CertificateHandle* pCertHandle);
569
570         /**
571          * This function gets the certificate list information by requested format.
572          *
573          * @since 2.1
574          * @return                      An error code.
575          * @param[in]           certFormat          Format of requested certificates.
576          * @param[in,out]       pCertList           Pointer to pointer of Certificate list structure.
577          * @param[out]          count               Number of certificates in the list.
578          * @exception           E_SUCCESS                       The method is successful.
579          * @exception           E_INVALID_ARG           The specified input parameter is invalid.
580          * @exception           E_SYSTEM                        An unexpected system error has occurred.
581          */
582         static result GetCertListByFormatN(_CertFormat certFormat, _CertificateListInfo*& pCertList, int* count);
583
584         /**
585          *        This function gets the root certificate list information by requested certificate ID
586          *
587          * @since 2.1
588          * @return                      An error code.
589          * @param[in]           certId              Format of requested device certificates.
590          * @param[out]          pCertList           Pointer to pointer of Certificate list structure.
591          * @exception           E_SUCCESS                       The method is successful.
592          * @exception           E_INVALID_ARG           The specified input parameter is invalid.
593          * @exception           E_SYSTEM                        An unexpected system error has occurred.
594          */
595         static result GetCaCertListByCertIdN(int certId, _CertificateListInfo*& pCertList);
596
597         /**
598          *       This function frees the certificate list given by a certificate list.
599          *
600          * @since 2.1
601          * @return                      An error code.
602          * @param[in]           pCertList           Pointer to certificate link list.
603          * @exception           E_SUCCESS                       The method is successful.
604          * @exception           E_INVALID_ARG           The specified input parameter is invalid.
605          */
606         static result FreeCertList(_CertificateListInfo* pCertList);
607
608         /**
609          *       This function frees the certificate info given by a struct _CertInfo.
610          *
611          * @since 2.1
612          * @return                      An error code.
613          * @param[in]           pCertInfo           Pointer to certificate info.
614          * @exception           E_SUCCESS                       The method is successful.
615          */
616         static result FreeCertificateInfo(_CertInfo* pCertInfo);
617
618         /**
619          *      This function provides list of installed certificate by type.
620          *
621          * @since 2.1
622          * @return                  If success give handle of root cert, null in case of failure.
623          * @param[in]           type        _CaCertType, Type of certificate.
624          * @param[out]          count           Number of installed certificate.
625          * @remarks         The specific error code can be accessed using the GetLastResult() method.
626          */
627         static CertificateStoreCtx OpenCertificateStoreByType(_CaCertType type, int* pCount);
628
629         /**
630          *      This function counts Root CA.
631          *
632          * @since 2.1
633          * @return                      Count of Root CA, -1 in case of failure.
634          * @param[in]           certificateStoreCtx          Certificate store context, can be get using OpenCertificateStoreByType function.
635          * @remarks         The specific error code can be accessed using the GetLastResult() method.
636          */
637         static int GetCertificateCount(CertificateStoreCtx certificateStoreCtx);
638
639         /**
640          *      This function returns the next root CA pBuffer.
641          *
642          * @since 2.1
643          * @return                      An error code.
644          * @param[in]           certificateStoreCtx          Certificate store context, can be get using OpenCertificateStoreByType function..
645          * @param[out]          pBuffer                         Output Buffer.
646          * @param[in,out]       pBufferLen                      Buffer length.
647          * @exception           E_SUCCESS                       The method is successful.
648          * @exception           E_INVALID_ARG           The specified input parameter is invalid.
649          */
650         static result GetNextCertificate(CertificateStoreCtx certificateStoreCtx, char* pBuffer, int* pBufferLen);
651
652         /**
653          *      This function updates Root CA certificate.
654          *
655          * @since 2.1
656          * @return                      An error code.
657          * @param[in]           type                    CA Cert Type.
658          * @param[in]           pOldCert                Old Certificate Buffer.
659          * @param[in]       oldCertLen              Old Certificate length.
660          * @param[in]       pNewCert                New Certificate Buffer.
661          * @param[in]       newCertLen              New Certificate length.
662          * @exception           E_SUCCESS                               The method is successful.
663          * @exception           E_INVALID_ARG                   The specified input parameter is invalid.
664          * @exception           E_OUT_OF_MEMORY         The memory is insufficient.
665          * @exception           E_SYSTEM                                An unexpected system error has occurred.
666          */
667         static result UpdateCaCertificate(_CaCertType type, char* pOldCert, int oldCertLen, char* pNewCert, int newCertLen); // if same certificate is in Db, replace the certificate using buffer2 and bufferLen2.
668
669         /**
670          *      This function closes root Certificate Handle.
671          *
672          * @since 2.1
673          * @return                      An error code.
674          * @param[in]           certificateStoreCtx          Certificate store context, can be get using OpenCertificateStoreByType function..
675          * @exception           E_SUCCESS                       The method is successful.
676          * @exception           E_INVALID_ARG           The specified input parameter is invalid.
677          */
678         static result CloseCertificateStore(CertificateStoreCtx certificateStoreCtx);
679
680         /**
681          *      This function returns Public key of certificate in DER format.
682          *
683          * @since 2.1
684          * @return                      An error code.
685          * @param[in]           certHandle          Handle to the certificate.
686          * @param[out]      pBuffer                             Buffer to contain public key.
687          * @param[in,out]       pBufLen                         Length of Public Key.
688          * @exception           E_SUCCESS                       The method is successful.
689          * @exception           E_INVALID_ARG           The specified input parameter is invalid.
690          * @exception           E_SYSTEM                        An unexpected system error has occurred.
691          */
692         static result GetCertPublicKey(CertificateHandle certHandle, char* pBuffer, int* pBufLen);
693
694         /**
695          *      This function returns Signature of certificate.
696          *
697          * @since 2.1
698          * @return                      An error code.
699          * @param[in]           certHandle          Handle to the certificate.
700          * @param[out]      pBuffer                             Buffer to contain Signature.
701          * @param[in,out]       bufLen                          Length of Signature.
702          * @exception           E_SUCCESS                       The method is successful.
703          * @exception           E_INVALID_ARG           The specified input parameter is invalid.
704          * @exception           E_SYSTEM                        An unexpected system error has occurred.
705          */
706         static result GetCertSignature(CertificateHandle certHandle, char* pBuffer, int* pBufLen);
707
708         /**
709          *      This function returns Version of X509 certificate.
710          *
711          * @since 2.1
712          * @return                      certificate version number as integer, -1 in case of failure.
713          * @param[in]           certHandle        Handle to the certificate.
714          * @remarks         The specific error code can be accessed using the GetLastResult() method.
715          */
716         static int GetCertVersion(CertificateHandle certHandle);
717
718         /**
719          *      This function checks certificates validity.
720          *
721          * @since 2.1
722          * @return                      An error code.
723          * @param[in]           certHandle          Handle to the certificate.
724          * @param[out]          pValidity                       Validity of certificate; Valid, Expired or Validity Yet to start.
725          * @exception           E_SUCCESS                       The method is successful.
726          * @exception           E_INVALID_ARG           The specified input parameter is invalid.
727          * @exception           E_SYSTEM                        An unexpected system error has occurred.
728          */
729         static result CheckCertValidity(CertificateHandle certHandle, _CertValidityType* pValidity);
730
731         /**
732          *      This function checks certificate type.
733          *
734          * @since 2.1
735          * @return                      An error code.
736          * @param[in]           certHandle          Handle to the certificate.
737          * @param[out]          pCertType                       Type of certificate.
738          * @exception           E_SUCCESS                       The method is successful.
739          * @exception           E_INVALID_ARG           The specified input parameter is invalid.
740          * @exception           E_SYSTEM                        An unexpected system error has occurred.
741          */
742         static result CheckCertType(CertificateHandle certHandle, _CaCertType* pCertType);
743
744         /**
745          *       This function retrieves domain certificate information.
746          *
747          * @since 2.1
748          * @return                      If success this function returns certId installed certificates, -1 in case of failure.
749          * @param[out]          ppDcInfo                        information about domain certificate.
750          * @remarks         The specific error code can be accessed using the GetLastResult() method.
751          */
752         static int GetDomainCertInfoN(_CertFieldInfos*& prDcInfo);
753
754         /**
755          *       This function retrieves certificate information given by a certificate ID.
756          *
757          * @since 2.1
758          * @return                      An error code.
759          * @param[in]           certId                          Certificate ID.
760          * @param[out]          pDcInfo                         Pointer to certificate information structure.
761          * @exception           E_SUCCESS                       The method is successful.
762          * @exception           E_INVALID_ARG           The specified input parameter is invalid.
763          * @exception           E_SYSTEM                        An unexpected system error has occurred.
764          */
765         static result GetCaCertInfoByCertId(int certId, _CertFieldInfos* pDcInfo);
766
767         /**
768          *       This function breaks certificate chain buffer into individual certificate.
769          *   It is assumed here that there is no Private Key in the Chain.
770          *   Chain can contain multiple certificate including Device, Intermediate and CA Certificate.
771          *   This will return a Structure HCertChainCtx containing all the certificates.
772          *
773          * @since 2.1
774          * @return                      An error code.
775          * @param[in]           pCertChainBuffer        Certificate chain buffer.
776          * @param[in]           certChainLength         Certificate chain buffer length.
777          * @param[out]          pCertCtx                        Certificate chain list containing individual certificate.
778          * @exception           E_SUCCESS                       The method is successful.
779          * @exception           E_INVALID_ARG           The specified input parameter is invalid.
780          * @exception           E_OUT_OF_MEMORY     The memory is insufficient.
781          * @exception           E_SYSTEM                        An unexpected system error has occurred.
782          */
783         static result GetParsedCertificateChainN(char* pCertChainBuffer, int certChainLength, CertChainCtx* pCertCtx);
784
785         /**
786          *  This function verifies the certificate chain in certificate context with respect to installed root certificates in the DB.
787          *
788          * @since 2.1
789          * @return                      An error code.
790          * @param[in]           pCertCtx            Handle to certificate chain context.
791          * @exception           E_SUCCESS                       The method is successful.
792          * @exception           E_INVALID_ARG           The specified input parameter is invalid.
793          * @exception           E_OUT_OF_MEMORY     The memory is insufficient.
794          * @exception           E_SYSTEM                        An unexpected system error has occurred.
795          */
796         static result VerifyCertificateChain(CertChainCtx pCertCtx);
797
798         /**
799          *       This function breaks certificate chain buffer into individual certificate.
800          *   It is assumed here that there is no Private Key in the Chain.
801          *   Chain can contain multiple certificate including Device, Intermediate and CA Certificate.
802          *   This will return a Structure ppCertChainList containing all the certificates.
803          *
804          * @since 2.1
805          * @return                      An error code.
806          * @param[in]           pCertChainBuffer                Certificate chain buffer.
807          * @param[in]           certChainLength                 Certificate chain buffer length.
808          * @param[out]          ppCertChainListRef              Certificate chain list containing individual certificate.
809          * @exception           E_SUCCESS                               The method is successful.
810          * @exception           E_INVALID_ARG                   The specified input parameter is invalid.
811          * @exception           E_OUT_OF_MEMORY         The memory is insufficient.
812          * @exception           E_SYSTEM                                An unexpected system error has occurred.
813          */
814         static result MakeCertChainFromBufferN(char* pCertChainBuffer, int certChainLength, _CertRootList*& ppCertChainListRef);
815
816         /**
817          * This function retrieves the user certificate chain on the basis of Subject name of any Intermediate CA and
818          * subject name of user Certificate. Subject name of Device certificate is optional parameter.
819          * Subject name of any intermediate Certificate is compulsory parameter.
820          * This function will retrieve the certificate chain on the basis of Subject name of any intermediate CA taken as
821          * issuer name in function parameters. It can also extract Certificate chain on the basis of Subject name of device
822          * certificate including chain containing the Intermediate Certificate Subject name.
823          * If there are multiple cert chain from the same issuer, it will get all the cert chain and check with subject name
824          * to decide which chain is need to be returned. For input parameter it takes subject name of any intermediate CA as
825          * issuer name, and subject name of user certificate as optional parameter. Format of Subject and Issuer name will
826          * be DER encoded.
827          *
828          * @since 2.1
829          * @return                      An error code.
830          * @param[in]           pIssuerName                 Pointer to Issuer name.
831          * @param[in]           issuerNameLength            Length of Issuer name.
832          * @param[in]           pSubjectName                pointer to Subject name.
833          * @param[in]           subjectNameLength           Length of Subject name.
834          * @param[out]          pUserCertListInfoTypesRef       Pointer to certificate list.
835          * @exception           E_SUCCESS                                       The method is successful.
836          * @exception           E_INVALID_ARG                           The specified input parameter is invalid.
837          * @exception           E_OUT_OF_MEMORY             The memory is insufficient.
838          * @exception           E_SYSTEM                                        An unexpected system error has occurred.
839          */
840         static result GetUserCertChainByIssuerAndSubjectNameN(char* pIssuerName, int issuerNameLength, char* pSubjectName, int subjectNameLength, _CertificateListInfo*& pUserCertListInfoTypesRef);
841
842         /**
843          * This function retrieves the device certificate chain on the basis of Subject name of Device Certificate.
844          * This function retrieves the certificate chain on the basis of Subject name of Device Certificate as function
845          * parameters. If there are multiple cert chain from the same issuer, it will get all the cert chain and check
846          * with subject name to decide which chain is need to be returned.
847          *
848          * @since 2.1
849          * @return                      An error code.
850          * @param[in]           pSubjectName            Pointer to Subject name.
851          * @param[in]           subjectNameLength       Length of Subject name.
852          * @param[out]          pCertChainCtx           Pointer to certificate chain.
853          * @param[out]          pPrivateKeyCtx          Pointer to private key info.
854          * @exception           E_SUCCESS                               The method is successful.
855          * @exception           E_INVALID_ARG                   The specified input parameter is invalid.
856          * @exception           E_OUT_OF_MEMORY         The memory is insufficient.
857          * @exception           E_SYSTEM                                An unexpected system error has occurred.
858          */
859         static result GetUserCertChainBySubjectName(char* pSubjectName, int subjectNameLength, CertChainCtx* pCertChainCtx, PrivateKeyCtx* pPrivateKeyCtx);
860
861         /**
862          *        This function gets the user certificate list information by requested format.
863          *
864          * @since 2.1
865          * @return                      An error code.
866          * @param[in]           certFormat                  Format of requested user certificates.
867          * @param[out]          pUserCertListInfoTypesRef   Pointer to pointer of Certificate list structure.
868          * @param[out]          pCount                      Number of certificates in the list.
869          * @exception           E_SUCCESS                                       The method is successful.
870          * @exception           E_INVALID_ARG                           The specified input parameter is invalid.
871          * @exception           E_OUT_OF_MEMORY             The memory is insufficient.
872          * @exception           E_SYSTEM                                        An unexpected system error has occurred.
873          */
874         static result GetUserCertListInfoTypesByFormatN(_CertFormat certFormat, _CertificateListInfo*& pUserCertListInfoTypesRef, int* pCount);
875
876         /**
877          *        This function gets the user certificate list information by requested certificate ID. It give info of certificate only.
878          *
879          * @since 2.1
880          * @return                      An error code.
881          * @param[in]       certId                          Certificate Id as in Db.
882          * @param[in]       encodingType                    Required encoding type of output buffer (PEM, Base64 or DER).
883          * @param[out]      ppUserCertificateListInfoTypes  Pointer to pointer of Certificate list structure.
884          * @exception           E_SUCCESS                                               The method is successful.
885          * @exception           E_INVALID_ARG                                   The specified input parameter is invalid.
886          * @exception           E_OUT_OF_MEMORY                 The memory is insufficient.
887          * @exception           E_SYSTEM                                                An unexpected system error has occurred.
888          */
889         static result GetUserCertificateByCertIdN(int certId, _CertEncodingType encodingType, _CertInfo*& pUserCertificateInfoRef);
890
891         /**
892          * This function retrieves all user certificate information.
893          *
894          * @since 2.1
895          * @return                      This function returns certId of installed user certificates.
896          * @param[out]          pCertFieldInfosRef        User certificate information.
897          * @remarks         The specific error code can be accessed using the GetLastResult() method.
898          */
899         static int GetUserCertFieldInfoN(_CertFieldInfos*& pCertFieldInfosRef);
900
901         /**
902          *        This function retrieves user certificate information by requested certificate ID.
903          *
904          * @since 2.1
905          * @return                      An error code.
906          * @param[in]       certId              Certificate id of requested user certificates as in DB.
907          * @param[out]      pCertFieldInfos             Pointer to pointer of Certificate infos structure.
908          * @exception           E_SUCCESS                       The method is successful.
909          * @exception           E_INVALID_ARG           The specified input parameter is invalid.
910          * @exception           E_SYSTEM                        An unexpected system error has occurred.
911          */
912         static result GetUserCertFieldInfoByCertId(int certId, _CertFieldInfos* pCertFieldInfos);
913
914         /**
915          *       This function provides certificate subject name given by a certificate handlder. It is complete certificate subject name buffer.
916          *
917          * @since 2.1
918          * @return                      An error code.
919          * @param[in]       certificateHandle   Handle to certificate.
920          * @param[out]      ppSubjectNameRef    Subject name buffer.
921          * @param[out]      pSubjectNameLength  Subject name length.
922          * @exception           E_SUCCESS                       The method is successful.
923          * @exception           E_INVALID_ARG           The specified input parameter is invalid.
924          * @exception           E_SYSTEM                        An unexpected system error has occurred.
925          */
926         static result GetSubjectNameN(CertificateHandle certificateHandle, byte*& ppSubjectNameRef, int* pSubjectNameLength);
927
928         /**
929          *       This function provides certificate issuer name given by a certificate handlder. It is complete certificate issuer name buffer.
930          *
931          * @since 2.1
932          * @return                      An error code.
933          * @param[in]       certificateHandle       Handle to certificate.
934          * @param[out]      pIssuerNameRef          Issuer name buffer.
935          * @param[out]      pIssuerNameLength       Subject name length.
936          * @exception           E_SUCCESS                               The method is successful.
937          * @exception           E_INVALID_ARG                   The specified input parameter is invalid.
938          * @exception           E_SYSTEM                                An unexpected system error has occurred.
939          */
940         static result GetIssuerNameN(CertificateHandle certificateHandle, byte*& pIssuerNameRef, int* pIssuerNameLength);
941
942
943         /**
944          *       This function provides the path of CRT file, which contains all the installed certificate in PEM format.
945          *
946          * @since 2.1
947          * @return                      Path of CRT file containing all certificates in PEM format.
948          */
949         static Tizen::Base::String GetCertificateCrtFilePath(void);
950
951         /**
952          *       This function frees the root certificate list.
953          *
954          * @since 2.1
955          * @return                      An error code.
956          * @param[in]       pRootCertList       Pointer to root certificate link list.
957          * @exception           E_SUCCESS           The method is successful.
958          * @exception           E_INVALID_ARG       The specified input parameter is invalid.
959          */
960         static result FreeRootCertList(_CertRootList* pRootCertList);
961
962         /**
963          *      This function closes the opened private key context.
964          *
965          * @since 2.1
966          * @return                      An error code.
967          * @param[in]           certCtx                         Handle to private key context.
968          * @exception           E_SUCCESS                       The method is successful.
969          * @exception           E_INVALID_ARG           The specified input parameter is invalid.
970          */
971         static result ClosePrivateKeyContext(PrivateKeyCtx privateKeyCtx);
972
973 private:
974         _CertService(void);
975
976         _CertService(const _CertService& rhs);
977
978         ~_CertService(void);
979
980         _CertService& operator =(const _CertService& rhs);
981
982 }; //_CertService
983
984 } } } //Tizen::Security::Cert
985
986 #endif  // _FSEC_CERT_INTERNAL_CERT_SERVICE_H_