3a79cfbba89349d64529cf3014169bb90dda3c9d
[platform/framework/native/appfw.git] / src / security / cert / FSecCert_CertDbStore.cpp
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_CertDbStore.cpp
20  * @brief               This file contains implementation of X509 Certificate Database.
21 */
22
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <error.h>
27 #include <new>
28 #include <sys/stat.h>
29 #include <assert.h>
30 #include <dirent.h>
31 #include <FIoDatabase.h>
32 #include <FIoDbEnumerator.h>
33 #include <FIoDbStatement.h>
34 #include <unique_ptr.h>
35 #include <FIoFile.h>
36 #include <FBaseByteBuffer.h>
37 #include <FBaseString.h>
38 #include <FBaseResult.h>
39 #include <FBaseSysLog.h>
40 #include <FBaseUtilStringUtil.h>
41 #include "FSecCert_CertDbStore.h"
42 #include "FSecCert_Base64.h"
43
44 using namespace Tizen::Io;
45 using namespace Tizen::Base;
46
47 namespace Tizen { namespace Security { namespace Cert
48 {
49
50 _CertDbStore::_CertDbStore(void)
51 {
52         __rootCaCertTableCreated = Database::Exists(_CERT_ROOT_CA_CERT_TABLE);
53         __userCertTableCreated = Database::Exists(_CERT_USER_CERT_TABLE);
54 }
55
56 _CertDbStore::~_CertDbStore(void)
57 {
58         //Empty body
59 }
60
61 result
62 _CertDbStore::IsRootCaCertTableCreated(void)
63 {
64         if(!Database::Exists(_CERT_ROOT_CA_CERT_TABLE))
65         {
66                 return E_SYSTEM;
67         }
68
69         return E_SUCCESS;
70 }
71
72 result
73 _CertDbStore::IsUserCertTableCreated(void)
74 {
75         if(!Database::Exists(_CERT_USER_CERT_TABLE))
76         {
77                 return E_SYSTEM;
78         }
79
80         return E_SUCCESS;
81
82 }
83
84 result
85 _CertDbStore::CreateCertificateTables(void)
86 {
87         result r = E_SUCCESS;
88         String sql;
89         String sql2;
90         std::unique_ptr< Database > pRootCaDatabase(new (std::nothrow) Database());
91         SysTryReturnResult(NID_SEC_CERT, pRootCaDatabase != null, E_OUT_OF_MEMORY, "Failed to allocate memory.");
92
93         r = pRootCaDatabase->Construct(_CERT_ROOT_CA_CERT_TABLE, true);
94         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to construct database instance.", GetErrorMessage(r));
95
96         sql.Append(L"CREATE TABLE IF NOT EXISTS rootcert (\
97                                   certId            INTEGER PRIMARY KEY AUTOINCREMENT,\
98                                   certType          SMALLINT DEFAULT 0,\
99                                   certFormat        SMALLINT DEFAULT 0,\
100                                   fileName                      VARCHAR,\
101                                   subjectNameLen        SMALLINT DEFAULT 20,\
102                                   subjectName           VARCHAR,\
103                                   issuerNameLen         SMALLINT DEFAULT 20,\
104                                   issuerName            VARCHAR,\
105                                   parentCa                      SMALLINT,\
106                                   installed                     VARCHAR,\
107                                   serialNo                      VARCHAR,\
108                               serialNoLen               SMALLINT DEFAULT 20)");
109
110         r = pRootCaDatabase->ExecuteSql(sql, true);
111         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to execute sql.", GetErrorMessage(r));
112
113         std::unique_ptr< Database > pUserCertDatabase(new (std::nothrow) Database());
114         SysTryReturnResult(NID_SEC_CERT, pUserCertDatabase != null, E_OUT_OF_MEMORY, "Failed to allocate memory.");
115
116         r = pUserCertDatabase->Construct(_CERT_USER_CERT_TABLE, true);
117         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to create certificate table. ", GetErrorMessage(r));
118
119         // Create a database table
120         sql2.Append(L"CREATE TABLE IF NOT EXISTS usercert (\
121                   certId            INTEGER PRIMARY KEY AUTOINCREMENT,\
122                   certPubKeyHash                        VARCHAR DEFAULT null,\
123                   certFormat     SMALLINT DEFAULT 0,\
124                   fileName      VARCHAR,\
125                   subjectNameLen        SMALLINT DEFAULT 20,\
126                   subjectName           VARCHAR,\
127                   issuerNameLen         SMALLINT DEFAULT 20,\
128                   issuerName            VARCHAR,\
129                   prvKeyPath            VARCHAR DEFAULT null,\
130                   prvKeyLen                     SMALLINT DEFAULT 0,\
131                   parentCa                      SMALLINT DEFAULT 0,\
132                   installed                     VARCHAR,\
133                   serialNo                      VARCHAR,\
134                   serialNoLen           SMALLINT DEFAULT 20)");
135
136         r = pUserCertDatabase->ExecuteSql(sql2, true);
137         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to execute sql.", GetErrorMessage(r));
138
139         return r;
140 }
141
142 result
143 _CertDbStore::DeleteCaCertFiles(void)
144 {
145         result r = E_SUCCESS;
146         String strVal;
147
148         std::unique_ptr< Database > pDatabase(new (std::nothrow) Database());
149         SysTryReturnResult(NID_SEC_CERT, pDatabase != null, E_OUT_OF_MEMORY, "Failed to allocate memory.");
150
151         r = pDatabase->Construct(_CERT_ROOT_CA_CERT_TABLE, DB_OPEN_READ_WRITE, 0);
152         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to create certificate table.", GetErrorMessage(r));
153
154         r = pDatabase->BeginTransaction();
155         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to begin transaction.", GetErrorMessage(r));
156
157         std::unique_ptr< DbEnumerator > pEnum(pDatabase->QueryN(L"SELECT * from rootcert"));
158         SysTryReturnResult(NID_SEC_CERT, pEnum != null, E_DATA_NOT_FOUND, "Failed to get data.");
159
160         while (pEnum->MoveNext() == E_SUCCESS)
161         {
162                 r = pEnum->GetStringAt(3, strVal);
163                 SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to get string.", GetErrorMessage(r));
164                 Tizen::Io::File::Remove(strVal);
165         }
166
167         return r;
168 }
169
170 result
171 _CertDbStore::DeleteUserCertFiles(void)
172 {
173         result r = E_SUCCESS;
174         Database database;
175         String strVal;
176
177         r = database.Construct(_CERT_USER_CERT_TABLE, DB_OPEN_READ_WRITE, 0);
178         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to open user cert database.", GetErrorMessage(r));
179
180         std::unique_ptr< DbEnumerator > pEnum(database.QueryN(L"SELECT * from usercert"));
181         SysTryReturnResult(NID_SEC_CERT, pEnum != null, E_DATA_NOT_FOUND, "Failed to find any user certificate in database.");
182
183         while (pEnum->MoveNext() == E_SUCCESS)
184         {
185                 r = pEnum->GetStringAt(3, strVal);
186                 SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to get string.", GetErrorMessage(r));
187                 Tizen::Io::File::Remove(strVal);
188         }
189
190         return r;
191 }
192
193 result
194 _CertDbStore::DropCertificateTables(void)
195 {
196         result r = E_SUCCESS;
197         Database database;
198
199         r = Database::Delete(_CERT_ROOT_CA_CERT_TABLE);
200         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to delete CA table.", GetErrorMessage(r));
201
202         r = Database::Delete(_CERT_USER_CERT_TABLE);
203         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to delete user table.", GetErrorMessage(r));
204
205         //Remove root certificates from the file system
206         DeleteCaCertFiles();
207
208         return r;
209 }
210
211 result
212 _CertDbStore::RemoveCertificateById(int certId)
213 {
214         return E_FAILURE;
215 }
216
217 result
218 _CertDbStore::RemoveAllCertificates(void)
219 {
220         return E_FAILURE;
221 }
222
223 result
224 _CertDbStore::GetNumberOfCertificates(int& count)
225 {
226         return E_FAILURE;
227 }
228
229
230 _CaCertDbStore::_CaCertDbStore(void)
231 {
232 }
233
234 _CaCertDbStore::~_CaCertDbStore(void)
235 {
236 }
237
238 result
239 _CaCertDbStore::InsertCaCertificate(CaCertRecord* pCertRecord)
240 {
241         result r = E_SUCCESS;
242         byte subjectNameBase64[_MAX_ISSUER_SUBJECT_NAME_SIZE] = {0, };
243         byte issuerNameBase64[_MAX_ISSUER_SUBJECT_NAME_SIZE] = {0, };
244         byte base64SerialNum[_MAX_SERIAL_NUMBER_SIZE] = {0, };
245         int subjectNameBase64Len = 0;
246         int base64IssuerNameLen = 0;
247         int base64SerialNumLen = 0;
248         String statement;
249
250         SysTryReturnResult(NID_SEC_CERT, pCertRecord != null, E_INVALID_ARG, "Invalid input parameter.");
251
252         memcpy(subjectNameBase64, pCertRecord->subjectName, pCertRecord->subjectNameLen);
253         memcpy(issuerNameBase64, pCertRecord->issuerName, pCertRecord->issuerNameLen);
254         memcpy(base64SerialNum, pCertRecord->serialNo, _MAX_SERIAL_NUMBER_SIZE);
255         memset(pCertRecord->subjectName, 0, _MAX_ISSUER_SUBJECT_NAME_SIZE);
256         memset(pCertRecord->issuerName, 0, _MAX_ISSUER_SUBJECT_NAME_SIZE);
257         memset(pCertRecord->serialNo, 0, _MAX_SERIAL_NUMBER_SIZE);
258
259         subjectNameBase64Len = _Base64::GetEncodedSize(pCertRecord->subjectNameLen);
260         memset(pCertRecord->subjectName, 0, sizeof(pCertRecord->subjectName));
261         r = _Base64::Encode(static_cast< byte* >(subjectNameBase64), pCertRecord->subjectNameLen, pCertRecord->subjectName, subjectNameBase64Len);
262         SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_ENCODING_FAILED, "Failed to encode subject name of certificate.");
263
264         pCertRecord->subjectNameLen = subjectNameBase64Len;
265
266         base64IssuerNameLen = _Base64::GetEncodedSize(pCertRecord->issuerNameLen);
267         memset(pCertRecord->issuerName, 0, sizeof(pCertRecord->issuerName));
268         r = _Base64::Encode(static_cast< byte* >(issuerNameBase64), pCertRecord->issuerNameLen, pCertRecord->issuerName, base64IssuerNameLen);
269         SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_ENCODING_FAILED, "Failed to encode issuer name of certificate.");
270
271         pCertRecord->issuerNameLen = base64IssuerNameLen;
272
273         base64SerialNumLen = _Base64::GetEncodedSize(pCertRecord->serialNoLen);
274         memset(pCertRecord->serialNo, 0, sizeof(pCertRecord->serialNo));
275         r = _Base64::Encode(static_cast< byte* >(base64SerialNum), pCertRecord->serialNoLen, pCertRecord->serialNo, base64SerialNumLen);
276         SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_ENCODING_FAILED, "Failed to encode serial number of certificate.");
277
278         pCertRecord->serialNoLen = base64SerialNumLen;
279
280         std::unique_ptr< Database > pDatabase(new (std::nothrow) Database());
281         SysTryReturnResult(NID_SEC_CERT, pDatabase != null, E_OUT_OF_MEMORY, "Failed to allocate memory.");
282
283         r = pDatabase->Construct(_CERT_ROOT_CA_CERT_TABLE, DB_OPEN_READ_WRITE, 0);
284         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to construct database instance.", GetErrorMessage(r));
285
286         r = pDatabase->BeginTransaction();
287         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Begin transaction failed.", GetErrorMessage(r));
288
289         statement.Append(L"INSERT INTO rootcert (certId, certType, certFormat, fileName, subjectNameLen, subjectName, issuerNameLen, issuerName, parentCa, installed, serialNo, serialNoLen) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?)");
290
291         std::unique_ptr< DbStatement > pStmt(pDatabase->CreateStatementN(statement));
292
293         r = pStmt->BindInt(1, pCertRecord->certType);
294         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to bind colmun 1 in database statement.", GetErrorMessage(r));
295         r = pStmt->BindInt(2, pCertRecord->certFormat);
296         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to bind colmun 2 in database statement.", GetErrorMessage(r));
297         r = pStmt->BindString(3, pCertRecord->fileName);
298         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to bind colmun 3 in database statement.", GetErrorMessage(r));
299         r = pStmt->BindInt(4, pCertRecord->subjectNameLen);
300         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to bind colmun 4 in database statement.", GetErrorMessage(r));
301         r = pStmt->BindString(5, pCertRecord->subjectName);
302         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to bind colmun 5 in database statement.", GetErrorMessage(r));
303         r = pStmt->BindInt(6, pCertRecord->issuerNameLen);
304         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to bind colmun 6 in database statement.", GetErrorMessage(r));
305         r = pStmt->BindString(7, pCertRecord->issuerName);
306         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to bind colmun 7 in database statement.", GetErrorMessage(r));
307         r = pStmt->BindInt(8, pCertRecord->parentCa);
308         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to bind colmun 8 in database statement.", GetErrorMessage(r));
309         r = pStmt->BindString(9, pCertRecord->installed);
310         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to bind colmun 9 in database statement.", GetErrorMessage(r));
311         r = pStmt->BindString(10, pCertRecord->serialNo);
312         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to bind colmun 10 in database statement.", GetErrorMessage(r));
313         r = pStmt->BindInt(11, pCertRecord->serialNoLen);
314         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to bind colmun 11 in database statement.", GetErrorMessage(r));
315
316         std::unique_ptr< DbEnumerator > pEnum(pDatabase->ExecuteStatementN(*pStmt));
317         AppAssert(!pEnum);
318
319         r = pDatabase->CommitTransaction();
320         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Commit transaction failed.", GetErrorMessage(r));
321
322         return r;
323 }
324
325 result
326 _CaCertDbStore::UpdateCaCertificate(CaCertRecord* pCertRecord, CaCertRecord* pUpdateCertRecord)
327 {
328         result r = E_SUCCESS;
329         String statement(_MAX_QUERY_LEN);
330         int subjectNameBase64Len = 0;
331         int base64IssuerNameLen = 0;
332         byte subjectNameBase64[_MAX_ISSUER_SUBJECT_NAME_SIZE] = {0, };
333         byte issuerNameBase64[_MAX_ISSUER_SUBJECT_NAME_SIZE] = {0, };
334
335         SysTryReturnResult(NID_SEC_CERT, !(pCertRecord == null || pUpdateCertRecord == null), E_INVALID_ARG, "Invalid input parameter.");
336
337         memcpy(subjectNameBase64, pCertRecord->subjectName, pCertRecord->subjectNameLen);
338         memcpy(issuerNameBase64, pCertRecord->issuerName, pCertRecord->issuerNameLen);
339         memset(pCertRecord->subjectName, 0, _MAX_ISSUER_SUBJECT_NAME_SIZE);
340         memset(pCertRecord->issuerName, 0, _MAX_ISSUER_SUBJECT_NAME_SIZE);
341
342         subjectNameBase64Len = _Base64::GetEncodedSize(pCertRecord->subjectNameLen);
343         memset(pCertRecord->subjectName, 0, sizeof(pCertRecord->subjectName));
344         r = _Base64::Encode(static_cast< byte* >(subjectNameBase64), pCertRecord->subjectNameLen, pCertRecord->subjectName, subjectNameBase64Len);
345         SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_ENCODING_FAILED, "Failed to encode subject name of certificate.");
346
347         pCertRecord->subjectNameLen = subjectNameBase64Len;
348
349         base64IssuerNameLen = _Base64::GetEncodedSize(pCertRecord->issuerNameLen);
350         memset(pCertRecord->issuerName, 0, sizeof(pCertRecord->issuerName));
351         r = _Base64::Encode(static_cast< byte* >(issuerNameBase64), pCertRecord->issuerNameLen, pCertRecord->issuerName, base64IssuerNameLen);
352         SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_ENCODING_FAILED, "Failed to encode issuer name of certificate.");
353
354         pCertRecord->issuerNameLen = base64IssuerNameLen;
355
356         memset(subjectNameBase64, 0, _MAX_ISSUER_SUBJECT_NAME_SIZE);
357         memset(issuerNameBase64, 0, _MAX_ISSUER_SUBJECT_NAME_SIZE);
358         subjectNameBase64Len = 0;
359         base64IssuerNameLen = 0;
360         memcpy(subjectNameBase64, pUpdateCertRecord->subjectName, pUpdateCertRecord->subjectNameLen);
361         memcpy(issuerNameBase64, pUpdateCertRecord->issuerName, pUpdateCertRecord->issuerNameLen);
362         memset(pUpdateCertRecord->subjectName, 0, _MAX_ISSUER_SUBJECT_NAME_SIZE);
363         memset(pUpdateCertRecord->issuerName, 0, _MAX_ISSUER_SUBJECT_NAME_SIZE);
364
365         subjectNameBase64Len = _Base64::GetEncodedSize(pUpdateCertRecord->subjectNameLen);
366         memset(pCertRecord->subjectName, 0, sizeof(pCertRecord->subjectName));
367         r = _Base64::Encode(static_cast< byte* >(subjectNameBase64), pUpdateCertRecord->subjectNameLen, pUpdateCertRecord->subjectName, subjectNameBase64Len);
368         SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_ENCODING_FAILED, "Failed to encode subject name of certificate.");
369
370         pUpdateCertRecord->subjectNameLen = subjectNameBase64Len;
371
372         base64IssuerNameLen = _Base64::GetEncodedSize(pUpdateCertRecord->issuerNameLen);
373         memset(pCertRecord->issuerName, 0, sizeof(pCertRecord->issuerName));
374         r = _Base64::Encode(static_cast< byte* >(issuerNameBase64), pUpdateCertRecord->issuerNameLen, pUpdateCertRecord->issuerName, base64IssuerNameLen);
375         SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_ENCODING_FAILED, "Failed to encode issuer name of certificate.");
376
377         pUpdateCertRecord->issuerNameLen = base64IssuerNameLen;
378
379         std::unique_ptr< Database > pDatabase(new (std::nothrow) Database());
380         SysTryReturnResult(NID_SEC_CERT, pDatabase != null, E_OUT_OF_MEMORY, "Failed to allocate memory.");
381
382         r = pDatabase->Construct(_CERT_ROOT_CA_CERT_TABLE, DB_OPEN_READ_WRITE, 0);
383         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Construct of update ca certificate failed.", GetErrorMessage(r));
384
385         r = pDatabase->BeginTransaction();
386         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to begin transaction.", GetErrorMessage(r));
387
388         statement.Format(_MAX_QUERY_LEN, L"UPDATE rootcert SET subjectNameLen = %d, subjectName = '%s', issuerNameLen = %d, issuerName = '%s' WHERE subjectName = '%s' and issuerName = '%s' and certType = %d and certId = %d", pUpdateCertRecord->subjectNameLen, pUpdateCertRecord->subjectName, pUpdateCertRecord->issuerNameLen, pUpdateCertRecord->issuerName, pCertRecord->subjectName, pCertRecord->issuerName, pCertRecord->certType, pCertRecord->certId);
389         r = pDatabase->ExecuteSql(statement, true);
390         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to execute sql.", GetErrorMessage(r));
391
392         r = pDatabase->CommitTransaction();
393         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to commit transaction .", GetErrorMessage(r));
394
395         return r;
396 }
397
398 result
399 _CaCertDbStore::UpdateParentCa(int certId, int parentCa)
400 {
401         result r = E_SUCCESS;
402         String statement(_MAX_QUERY_LEN);
403
404         SysTryReturnResult(NID_SEC_CERT, certId > 0, E_INVALID_ARG, "Invalid input parameter certificate identifier.");
405         SysTryReturnResult(NID_SEC_CERT, parentCa > 0, E_INVALID_ARG, "Invalid input parameter parent certificate identifier.");
406
407         std::unique_ptr< Database > pDatabase(new (std::nothrow) Database());
408         SysTryReturnResult(NID_SEC_CERT, pDatabase != null, E_OUT_OF_MEMORY, "Failed to allocate memory.");
409
410         r = pDatabase->Construct(_CERT_ROOT_CA_CERT_TABLE, DB_OPEN_READ_WRITE, 0);
411
412         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to construct database instance.", GetErrorMessage(r));
413
414         r = pDatabase->BeginTransaction();
415         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to begin transaction.", GetErrorMessage(r));
416
417         statement.Format(_MAX_QUERY_LEN, L"UPDATE rootcert SET parentCa = '%d' WHERE certId = '%d'", parentCa, certId);
418         r = pDatabase->ExecuteSql(statement, true);
419         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to execute sql statement.", GetErrorMessage(r));
420
421         r = pDatabase->CommitTransaction();
422         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to commit transaction.", GetErrorMessage(r));
423
424         return r;
425 }
426
427 result
428 _CaCertDbStore::RemoveCertificateBySubjectName(_CaCertType certType, byte* pSubjectName, int subjectNameLen)
429 {
430         result r = E_SUCCESS;
431         char subjectNameBase64[_MAX_ISSUER_SUBJECT_NAME_SIZE] = {0, };
432         int subjectNameBase64Len = 0;
433         String statement(_MAX_QUERY_LEN);
434
435         SysTryReturnResult(NID_SEC_CERT, !((pSubjectName == null) || (subjectNameLen <= 0) || (certType < _MIN_CERT_TYPE) || (certType > _MAX_CERT_TYPE)),
436                                            E_INVALID_ARG, "Invalid input parameter subjetname or invalid subject name length.");
437
438         subjectNameBase64Len = _Base64::GetEncodedSize(subjectNameLen);
439         memset(subjectNameBase64, 0, sizeof(subjectNameBase64));
440         r = _Base64::Encode(static_cast< byte* >(pSubjectName), subjectNameLen, subjectNameBase64, subjectNameBase64Len);
441         SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_ENCODING_FAILED, "Failed to encode subject name of certificate.");
442
443         std::unique_ptr< Database > pDatabase(new (std::nothrow) Database());
444         SysTryReturnResult(NID_SEC_CERT, pDatabase != null, E_OUT_OF_MEMORY, "Failed to allocate memory.");
445
446         r = pDatabase->Construct(_CERT_ROOT_CA_CERT_TABLE, DB_OPEN_READ_WRITE, 0);
447         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to construct database instance.", GetErrorMessage(r));
448
449         r = pDatabase->BeginTransaction();
450         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to begin transaction.", GetErrorMessage(r));
451
452         statement.Format(_MAX_QUERY_LEN, L"DELETE FROM rootcert WHERE subjectName = '%s' and subjectNameLen = '%d' and certType = '%d'", subjectNameBase64, subjectNameBase64Len, certType);
453
454         r = pDatabase->ExecuteSql(statement, true);
455         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to execute sql statement.", GetErrorMessage(r));
456
457         r = pDatabase->CommitTransaction();
458         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to commit tranction.", GetErrorMessage(r));
459
460         return r;
461 }
462
463 result
464 _CaCertDbStore::RemoveCertificateByIssuerNameAndSerialNo(_CaCertType certType, byte* issuerName, int issuerNameLen, byte* serialNo)
465 {
466         result r = E_SUCCESS;char issuerNameBase64[_MAX_ISSUER_SUBJECT_NAME_SIZE] = {0, };
467         char base64SerialNum[_MAX_SERIAL_NUMBER_SIZE] = {0, };
468         int base64IssuerNameLen = 0;
469         int base64SerialNoLen = 0;
470         int serialNoLen = 0;
471         String statement(_MAX_QUERY_LEN);
472
473         SysTryReturnResult(NID_SEC_CERT, !((issuerName == null) || (issuerNameLen <= 0) || (serialNo == null) || (certType < _MIN_CERT_TYPE) || (certType > _MAX_CERT_TYPE)),
474                                            E_INVALID_ARG, "Invalid input parameter issuer name or length of issuer name.");
475
476         base64IssuerNameLen = _Base64::GetEncodedSize(issuerNameLen);
477         memset(issuerNameBase64, 0, sizeof(issuerNameBase64));
478         r = _Base64::Encode(issuerName, issuerNameLen, issuerNameBase64, base64IssuerNameLen);
479         SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_ENCODING_FAILED, "Failed to encode issuer name of certificate.");
480
481         serialNoLen = strlen(reinterpret_cast< const char* >(serialNo));
482
483         base64SerialNoLen = _Base64::GetEncodedSize(serialNoLen);
484         memset(base64SerialNum, 0, sizeof(base64SerialNum));
485         r = _Base64::Encode(serialNo, serialNoLen, base64SerialNum, base64SerialNoLen);
486         SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_ENCODING_FAILED, "Failed to encode serial number of certificate.");
487
488         std::unique_ptr< Database > pDatabase(new (std::nothrow) Database());
489         SysTryReturnResult(NID_SEC_CERT, pDatabase != null, E_OUT_OF_MEMORY, "Failed to allocate memory.");
490
491         r = pDatabase->Construct(_CERT_ROOT_CA_CERT_TABLE, DB_OPEN_READ_WRITE, 0);
492         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to construct database instance.", GetErrorMessage(r));
493
494         r = pDatabase->BeginTransaction();
495         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to begin transaction.", GetErrorMessage(r));
496
497         statement.Format(_MAX_QUERY_LEN, L"DELETE FROM rootcert WHERE issuerName = '%s' and issuerNameLen = '%d' and certType = '%d' and serialNo = '%s'", issuerNameBase64, base64IssuerNameLen, certType, base64SerialNum);
498         r = pDatabase->ExecuteSql(statement, true);
499         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to execute sql statement.", GetErrorMessage(r));
500
501         r = pDatabase->CommitTransaction();
502         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to commit transaction.", GetErrorMessage(r));
503
504         return r;
505 }
506
507 result
508 _CaCertDbStore::RemoveAllCertificateByCondition(byte* pCondition)
509 {
510         result r = E_SUCCESS;
511         int curCertId = 0;
512         CaCertRecord certRecord = {0, };
513
514         SysTryReturnResult(NID_SEC_CERT, pCondition != null, E_INVALID_ARG, "Invalid input parameter.");
515
516         r = GetNextRecordByCondition(pCondition, &certRecord, curCertId);
517
518         while (!IsFailed(r))
519         {
520                 curCertId = certRecord.certId;
521
522                 r = _CaCertDbStore::RemoveCertificateById(certRecord.certId);
523                 if (!IsFailed(r))
524                 {
525                         Tizen::Io::File::Remove(certRecord.fileName);
526                 }
527
528                 r = GetNextRecordByCondition(pCondition, &certRecord, curCertId);
529
530         }
531
532         return E_SUCCESS;
533 }
534
535 result
536 _CaCertDbStore::CheckDuplicateCertificate(_CaCertType certType, byte* pSubjectName, int subjectNameLen)
537 {
538         result r = E_SUCCESS;char subjectNameBase64[_MAX_ISSUER_SUBJECT_NAME_SIZE] = {0, };
539         int subjectNameBase64Len = 0;
540         String statement;
541         String query;
542
543         SysTryReturnResult(NID_SEC_CERT, pSubjectName != null, E_INVALID_ARG, "Invalid input parameter subject name.");
544         SysTryReturnResult(NID_SEC_CERT, subjectNameLen >= 0, E_INVALID_ARG, "Invalid input parameter subject name length.");
545         SysTryReturnResult(NID_SEC_CERT, certType >= _MIN_CERT_TYPE, E_INVALID_ARG, "Invalid input parameter certificate identifier(value less than minimum index).");
546         SysTryReturnResult(NID_SEC_CERT, certType <= _MAX_CERT_TYPE, E_INVALID_ARG, "Invalid input parameter certificate identifier(value greater than maximim index).");
547
548         subjectNameBase64Len = _Base64::GetEncodedSize(subjectNameLen);
549         memset(subjectNameBase64, 0, sizeof(subjectNameBase64));
550         r = _Base64::Encode(static_cast< byte* >(pSubjectName), subjectNameLen, subjectNameBase64, subjectNameBase64Len);
551         SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_ENCODING_FAILED, "Failed to encode subject name of certificate.");
552
553         std::unique_ptr< Database > pDatabase(new (std::nothrow) Database());
554         SysTryReturnResult(NID_SEC_CERT, pDatabase != null, E_OUT_OF_MEMORY, "Failed to allocate memory.");
555
556         r = pDatabase->Construct(_CERT_ROOT_CA_CERT_TABLE, DB_OPEN_READ_ONLY, 0);
557         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to construct database instance.", GetErrorMessage(r));
558
559         r = pDatabase->BeginTransaction();
560         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to begin transaction.", GetErrorMessage(r));
561
562         statement.Format(_MAX_QUERY_LEN, L"SELECT * from rootcert where certType = '%d' and subjectName = '%s' and subjectNameLen = '%d'", certType, subjectNameBase64, subjectNameBase64Len);
563         std::unique_ptr<  DbEnumerator > pEnum(pDatabase->QueryN(statement));
564         if(pEnum == null)
565         {
566                 return E_DATA_NOT_FOUND;
567         }
568
569         return r;
570 }
571
572 result
573 _CaCertDbStore::CheckDuplicateCertificate(_CaCertType certType, byte* issuerName, int issuerNameLen, byte* pSerialNumber)
574 {
575         result r = E_SUCCESS;char issuerNameBase64[_MAX_ISSUER_SUBJECT_NAME_SIZE] = {0, };
576         char base64SerialNo[_MAX_SERIAL_NUMBER_SIZE] = {0, };
577         int base64IssuerNameLen = 0;
578         int base64SerialNoLen = 0;
579         int serialNoLen = 0;
580         String statement(_MAX_QUERY_LEN);
581
582         SysTryReturnResult(NID_SEC_CERT, issuerName != null, E_INVALID_ARG, "Invalid input parameter issuer name.");
583         SysTryReturnResult(NID_SEC_CERT, issuerNameLen >= 0, E_INVALID_ARG, "Invalid input parameter issuer name length.");
584         SysTryReturnResult(NID_SEC_CERT, pSerialNumber != null, E_INVALID_ARG, "Invalid input parameter serial number.");
585         SysTryReturnResult(NID_SEC_CERT, certType >= _MIN_CERT_TYPE, E_INVALID_ARG, "Invalid input parameter certificate identifier (value is less than minimum index).");
586         SysTryReturnResult(NID_SEC_CERT, certType <= _MAX_CERT_TYPE, E_INVALID_ARG, "Invalid input parameter certificate identifier (value is greater than maximum index).");
587
588         base64IssuerNameLen = _Base64::GetEncodedSize(issuerNameLen);
589         memset(issuerNameBase64, 0, sizeof(issuerNameBase64));
590         r = _Base64::Encode(issuerName, issuerNameLen, issuerNameBase64, base64IssuerNameLen);
591         SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_ENCODING_FAILED, "Failed to encode issuer name of certificate.");
592
593         serialNoLen = strlen(reinterpret_cast< const char* >(pSerialNumber));
594
595         base64SerialNoLen = _Base64::GetEncodedSize(serialNoLen);
596         memset(base64SerialNo, 0, sizeof(base64SerialNo));
597         r = _Base64::Encode(pSerialNumber, serialNoLen, base64SerialNo, base64SerialNoLen);
598         SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_ENCODING_FAILED, "Failed to encode serial number of certificate.");
599
600         std::unique_ptr< Database > pDatabase(new (std::nothrow) Database());
601         SysTryReturnResult(NID_SEC_CERT, pDatabase != null, E_OUT_OF_MEMORY, "Failed to allocate memory");
602
603         r = pDatabase->Construct(_CERT_ROOT_CA_CERT_TABLE, DB_OPEN_READ_ONLY, 0);
604         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to construct database instance.", GetErrorMessage(r));
605
606         r = pDatabase->BeginTransaction();
607         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed begin transaction.", GetErrorMessage(r));
608
609         statement.Format(_MAX_QUERY_LEN, L"SELECT * FROM rootcert WHERE certType = '%d' AND issuerName = '%s' AND issuerNameLen = '%d' AND serialNo = '%s'", certType, issuerNameBase64, base64IssuerNameLen, base64SerialNo);
610         std::unique_ptr< DbEnumerator > pEnum(pDatabase->QueryN(statement));
611         SysTryReturnResult(NID_SEC_CERT, pEnum != null, E_DATA_NOT_FOUND, "No certificate found in databas.");
612
613         return r;
614 }
615
616 result
617 _CaCertDbStore::GetFirstRecordByConditions(byte* pCondition, CaCertRecord* pCertRecord)
618 {
619         result r = E_SUCCESS;
620         String statement(_MAX_QUERY_LEN);
621         ByteBuffer* pTempBuf = null;
622         char tmpName[_MAX_ISSUER_SUBJECT_NAME_SIZE] = {0, };
623         String strVal;
624
625         SysTryReturnResult(NID_SEC_CERT, pCondition != null, E_INVALID_ARG, "Invalid input parameter.");
626         SysTryReturnResult(NID_SEC_CERT, pCertRecord != null, E_INVALID_ARG, "Invalid input parameter.");
627
628         std::unique_ptr< Database > pDatabase(new (std::nothrow) Database());
629         SysTryReturnResult(NID_SEC_CERT, pDatabase != null, E_OUT_OF_MEMORY, "Failed to allocate memory");
630
631         r = pDatabase->Construct(_CERT_ROOT_CA_CERT_TABLE, DB_OPEN_READ_ONLY, 0);
632         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to construct database instance.", GetErrorMessage(r));
633
634         statement.Format(_MAX_QUERY_LEN, L"SELECT * FROM rootcert WHERE %s ORDER BY certId", pCondition);
635         std::unique_ptr<  DbEnumerator > pEnum(pDatabase->QueryN(statement));
636         SysTryReturnResult(NID_SEC_CERT, pEnum != null, E_DATA_NOT_FOUND, "No certificate found in database.");
637
638         r = pEnum->MoveNext();
639         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to move next.", GetErrorMessage(r));
640
641         r = pEnum->GetIntAt(0, pCertRecord->certId);
642         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database of column 0.", GetErrorMessage(r));
643
644         r = pEnum->GetIntAt(1, pCertRecord->certType);
645         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database of column 1.", GetErrorMessage(r));
646
647         r = pEnum->GetIntAt(2, pCertRecord->certFormat);
648         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database of column 2.", GetErrorMessage(r));
649
650         r = pEnum->GetStringAt(3, strVal);
651         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database of column 3.", GetErrorMessage(r));
652
653         pTempBuf = Tizen::Base::Utility::StringUtil::StringToUtf8N(strVal); //check this
654         r = GetLastResult();
655         SysTryReturn(NID_SEC_CERT, pTempBuf != null, r, r, "[%s] String to Utf8 function failed.", GetErrorMessage(r));
656
657         memcpy(pCertRecord->fileName, reinterpret_cast< const char* >(pTempBuf->GetPointer()), pTempBuf->GetRemaining());
658         pCertRecord->fileName[pTempBuf->GetRemaining()] = '\0';
659
660         r = pEnum->GetIntAt(4, pCertRecord->subjectNameLen);
661         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database of column 4.", GetErrorMessage(r));
662
663         r = pEnum->GetStringAt(5, strVal);
664         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database of column 5.", GetErrorMessage(r));
665
666         memset(tmpName, 0, _MAX_ISSUER_SUBJECT_NAME_SIZE);
667         pTempBuf = Tizen::Base::Utility::StringUtil::StringToUtf8N(strVal);
668         r = GetLastResult();
669         SysTryReturn(NID_SEC_CERT, pTempBuf != null, r, r, "[%s] String to Utf8 function failed.", GetErrorMessage(r));
670
671         memcpy(tmpName, reinterpret_cast< const char* >(pTempBuf->GetPointer()), pCertRecord->subjectNameLen);
672         _Base64::Decode(tmpName, pCertRecord->subjectNameLen, reinterpret_cast< byte* >(pCertRecord->subjectName), pCertRecord->subjectNameLen);
673
674         r = pEnum->GetIntAt(6, pCertRecord->issuerNameLen);
675         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database of column 6.", GetErrorMessage(r));
676
677         r = pEnum->GetStringAt(7, strVal);
678         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database of column 7.", GetErrorMessage(r));
679
680         memset(tmpName, 0, _MAX_ISSUER_SUBJECT_NAME_SIZE);
681         pTempBuf = Tizen::Base::Utility::StringUtil::StringToUtf8N(strVal);
682         r = GetLastResult();
683         SysTryReturn(NID_SEC_CERT, pTempBuf != null, r, r, "[%s] String to Utf8 function failed.", GetErrorMessage(r));
684
685         memcpy(tmpName, reinterpret_cast< const char* >(pTempBuf->GetPointer()), pCertRecord->issuerNameLen);
686         _Base64::Decode(tmpName, pCertRecord->issuerNameLen, reinterpret_cast< byte* >(pCertRecord->issuerName), pCertRecord->issuerNameLen);
687
688         r = pEnum->GetIntAt(8, pCertRecord->parentCa);
689         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database of column 8.", GetErrorMessage(r));
690
691         r = pEnum->GetStringAt(9, strVal);
692         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database of column 9.", GetErrorMessage(r));
693
694         pTempBuf = Tizen::Base::Utility::StringUtil::StringToUtf8N(strVal);
695         r = GetLastResult();
696         SysTryReturn(NID_SEC_CERT, pTempBuf != null, r, r, "[%s] String to Utf8 function failed.", GetErrorMessage(r));
697
698         memcpy(pCertRecord->installed, reinterpret_cast< const char* >(pTempBuf->GetPointer()), pTempBuf->GetRemaining());
699         pCertRecord->installed[pTempBuf->GetRemaining()] = '\0';
700
701         r = pEnum->GetStringAt(10, strVal);
702         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database of column 10.", GetErrorMessage(r));
703
704         r = pEnum->GetIntAt(11, pCertRecord->serialNoLen);
705         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database of column 11.", GetErrorMessage(r));
706
707         memset(tmpName, 0, _MAX_ISSUER_SUBJECT_NAME_SIZE);
708         pTempBuf = Tizen::Base::Utility::StringUtil::StringToUtf8N(strVal);
709         r = GetLastResult();
710         SysTryReturn(NID_SEC_CERT, pTempBuf != null, r, r, "[%s] String to Utf8 function failed.", GetErrorMessage(r));
711
712         memcpy(tmpName, reinterpret_cast< const char* >(pTempBuf->GetPointer()), pTempBuf->GetRemaining());
713         _Base64::Decode(tmpName, pCertRecord->serialNoLen, reinterpret_cast< byte* >(pCertRecord->serialNo), pCertRecord->serialNoLen);
714
715         return r;
716 }
717
718 result
719 _CaCertDbStore::GetNextRecordByCondition(byte* pCondition, CaCertRecord* pCertRecord, int curCertId)
720 {
721         result r = E_SUCCESS;
722         String statement(_MAX_QUERY_LEN);
723         ByteBuffer* pTempBuf = null;
724         String strVal;
725
726         SysTryReturnResult(NID_SEC_CERT, pCondition != null, E_INVALID_ARG, "Invalid input parameter.");
727         SysTryReturnResult(NID_SEC_CERT, pCertRecord != null, E_INVALID_ARG, "Invalid input parameter.");
728
729         std::unique_ptr< Database > pDatabase(new (std::nothrow) Database());
730         SysTryReturnResult(NID_SEC_CERT, pDatabase != null, E_OUT_OF_MEMORY, "Failed to allocate memory");
731
732         r = pDatabase->Construct(_CERT_ROOT_CA_CERT_TABLE, DB_OPEN_READ_ONLY, 0);
733         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to construct database instance.", GetErrorMessage(r));
734
735         statement.Format(_MAX_QUERY_LEN, L"SELECT * FROM rootcert WHERE %s AND certId > '%d'  ORDER BY certId", pCondition, curCertId);
736         std::unique_ptr<  DbEnumerator > pEnum(pDatabase->QueryN(statement));
737         if(pEnum == null)
738         {
739                 return E_DATA_NOT_FOUND;
740         }
741
742         r = pEnum->MoveNext();
743         r = pEnum->GetIntAt(0, pCertRecord->certId);
744         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database of column 0.", GetErrorMessage(r));
745
746         r = pEnum->GetIntAt(1, pCertRecord->certType);
747         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database of column 1.", GetErrorMessage(r));
748
749         r = pEnum->GetIntAt(2, pCertRecord->certFormat);
750         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database of column 2.", GetErrorMessage(r));
751
752         r = pEnum->GetStringAt(3, strVal);
753         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database of column 3.");
754
755         pTempBuf = Tizen::Base::Utility::StringUtil::StringToUtf8N(strVal);
756         r = GetLastResult();
757         SysTryReturn(NID_SEC_CERT, pTempBuf != null, r, r, "[%s] String to Utf8 function failed.", GetErrorMessage(r));
758
759         memcpy(pCertRecord->fileName, reinterpret_cast< const char* >(pTempBuf->GetPointer()), pTempBuf->GetRemaining());
760         pCertRecord->fileName[pTempBuf->GetRemaining()] = '\0';
761
762         r = pEnum->GetIntAt(4, pCertRecord->subjectNameLen);
763         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database of column 4.", GetErrorMessage(r));
764
765         r = pEnum->GetStringAt(5, strVal);
766         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database of column 5.", GetErrorMessage(r));
767
768         pTempBuf = Tizen::Base::Utility::StringUtil::StringToUtf8N(strVal);
769         r = GetLastResult();
770         SysTryReturn(NID_SEC_CERT, pTempBuf != null, r, r, "[%s] String to Utf8 function failed.", GetErrorMessage(r));
771
772         memcpy(pCertRecord->subjectName, reinterpret_cast< const char* >(pTempBuf->GetPointer()), pTempBuf->GetRemaining());
773         pCertRecord->subjectName[pTempBuf->GetRemaining()] = '\0';
774
775         r = pEnum->GetIntAt(6, pCertRecord->issuerNameLen);
776         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database of column 6.", GetErrorMessage(r));
777
778         r = pEnum->GetStringAt(7, strVal);
779         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database of column 7.", GetErrorMessage(r));
780
781         pTempBuf = Tizen::Base::Utility::StringUtil::StringToUtf8N(strVal);
782         r = GetLastResult();
783         SysTryReturn(NID_SEC_CERT, pTempBuf != null, r, r, "[%s] String to Utf8 function failed.", GetErrorMessage(r));
784
785         memcpy(pCertRecord->issuerName, reinterpret_cast< const char* >(pTempBuf->GetPointer()), pTempBuf->GetRemaining());
786         pCertRecord->issuerName[pTempBuf->GetRemaining()] = '\0';
787
788         r = pEnum->GetIntAt(8, pCertRecord->parentCa);
789         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database of column 8.", GetErrorMessage(r));
790
791         r = pEnum->GetStringAt(9, strVal);
792         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database of column 9.", GetErrorMessage(r));
793
794         pTempBuf = Tizen::Base::Utility::StringUtil::StringToUtf8N(strVal);
795         r = GetLastResult();
796         SysTryReturn(NID_SEC_CERT, pTempBuf != null, r, r, "[%s] String to Utf8 function failed.", GetErrorMessage(r));
797
798         memcpy(pCertRecord->installed, reinterpret_cast< const char* >(pTempBuf->GetPointer()), pTempBuf->GetRemaining());
799         pCertRecord->installed[pTempBuf->GetRemaining()] = '\0';
800
801         r = pEnum->GetStringAt(10, strVal);
802         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database of column 10.", GetErrorMessage(r));
803
804         pTempBuf = Tizen::Base::Utility::StringUtil::StringToUtf8N(strVal);
805         r = GetLastResult();
806         SysTryReturn(NID_SEC_CERT, pTempBuf != null, r, r, "[%s] String to Utf8 function failed.", GetErrorMessage(r));
807
808         memcpy(pCertRecord->serialNo, reinterpret_cast< const char* >(pTempBuf->GetPointer()), pTempBuf->GetRemaining());
809         pCertRecord->serialNo[pTempBuf->GetRemaining()] = '\0';
810
811         r = pEnum->GetIntAt(11, pCertRecord->serialNoLen);
812         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database of column 11.", GetErrorMessage(r));
813
814         return r;
815 }
816
817 result
818 _CaCertDbStore::GetCurrentCertId(int& curCertId)
819 {
820         result r = E_SUCCESS;
821         int certId = 0;
822
823         std::unique_ptr< Database > pDatabase(new (std::nothrow) Database());
824         SysTryReturnResult(NID_SEC_CERT, pDatabase != null, E_OUT_OF_MEMORY, "Failed to allocate memory");
825
826         r = pDatabase->Construct(_CERT_ROOT_CA_CERT_TABLE, DB_OPEN_READ_ONLY, 0);
827         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to construct database instance.", GetErrorMessage(r));
828
829         std::unique_ptr<DbEnumerator > pEnum(pDatabase->QueryN("SELECT seq FROM sqlite_sequence"));
830         if(pEnum == null)
831         {
832                 return E_DATA_NOT_FOUND;
833         }
834
835         while (pEnum->MoveNext() == E_SUCCESS)
836         {
837                 r = pEnum->GetIntAt(0, certId);
838                 SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database.", GetErrorMessage(r));
839         }
840         curCertId = certId;
841
842         return r;
843 }
844
845 result
846 _CaCertDbStore::CheckIfSameParent(int certId)
847 {
848         result r = E_SUCCESS;
849         int curCertId = 0;
850         int count = 0;
851         String statement;
852
853         std::unique_ptr< Database > pDatabase(new (std::nothrow) Database());
854         SysTryReturnResult(NID_SEC_CERT, pDatabase != null, E_OUT_OF_MEMORY, "Failed to allocate memory");
855
856         r = pDatabase->Construct(_CERT_ROOT_CA_CERT_TABLE, DB_OPEN_READ_ONLY, 0);
857         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to construct database instance.", GetErrorMessage(r));
858
859         statement.Format(_MAX_QUERY_LEN, L"SELECT * FROM rootcert where parentCa = %d ORDER BY certId", certId);
860
861         std::unique_ptr< DbEnumerator > pEnum(pDatabase->QueryN(statement));
862         SysTryReturnResult(NID_SEC_CERT, pEnum != null, E_DATA_NOT_FOUND, "No certificate found in database.");
863
864         while (pEnum->MoveNext() == E_SUCCESS)
865         {
866                 r = pEnum->GetIntAt(0, curCertId);
867                 SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database.", GetErrorMessage(r));
868                 count++;
869         }
870
871         SysTryReturnResult(NID_SEC_CERT, count > 1, E_SYSTEM, "Failed to get record.");
872
873         return r;
874 }
875
876 result
877 _CaCertDbStore::SelectCaCertificateBycertId(int certId, CaCertRecord* pCertRecord)
878 {
879         byte condition[_MAX_TYPE_CONST_SIZE] = {0, };
880
881         return _CaCertDbStore::GetNextRecordByCondition(static_cast< byte* >(condition), pCertRecord, certId);
882
883 }
884
885 result
886 _CaCertDbStore::RemoveCertificateById(int certId)
887 {
888         result r = E_SUCCESS;
889         String statement;
890
891         SysTryReturnResult(NID_SEC_CERT, certId > 0, E_INVALID_ARG, "Invalid input parameter.");
892
893         std::unique_ptr< Database > pDatabase(new (std::nothrow) Database());
894         SysTryReturnResult(NID_SEC_CERT, pDatabase != null, E_OUT_OF_MEMORY, "Failed to allocate memory");
895
896         r = pDatabase->Construct(_CERT_ROOT_CA_CERT_TABLE, DB_OPEN_READ_WRITE, 0);
897         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to construct database instance.", GetErrorMessage(r));
898
899         r = pDatabase->BeginTransaction();
900         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to begin transaction.", GetErrorMessage(r));
901
902         statement.Append(L"DELETE FROM rootcert WHERE certId = ?");
903         std::unique_ptr< DbStatement > pStmt(pDatabase->CreateStatementN(statement));
904         SysTryReturn(NID_SEC_CERT, pStmt != null, r, r, "[%s] Failed to create statement.", GetErrorMessage(r));
905
906         r = pStmt->BindInt(0, certId);
907         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to bind colmun in database statement.", GetErrorMessage(r));
908
909         pDatabase->ExecuteStatementN(*pStmt);
910
911         r = pDatabase->CommitTransaction();
912         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to commit transaction.", GetErrorMessage(r));
913
914         return r;
915 }
916
917 result
918 _CaCertDbStore::RemoveAllCertificates(void)
919 {
920         result r = E_SUCCESS;
921         String statement;
922
923         std::unique_ptr< Database > pDatabase(new (std::nothrow) Database());
924         SysTryReturnResult(NID_SEC_CERT, pDatabase != null, E_OUT_OF_MEMORY, "Failed to allocate memory");
925
926         r = pDatabase->Construct(_CERT_ROOT_CA_CERT_TABLE, DB_OPEN_READ_WRITE, 0);
927         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to construct database instance.", GetErrorMessage(r));
928
929         r = pDatabase->BeginTransaction();
930         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to begin transaction.", GetErrorMessage(r));
931
932         statement.Append(L"DELETE from rootcert");
933
934         r = pDatabase->ExecuteSql(statement, true);
935         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to execute sql.", GetErrorMessage(r));
936
937         r = pDatabase->CommitTransaction();
938         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to commit transaction.", GetErrorMessage(r));
939
940         return r;
941 }
942
943 result
944 _CaCertDbStore::GetNumberOfCertificates(int& count)
945 {
946         result r = E_SUCCESS;
947         
948         String statement;
949         int certCount = 0;
950
951         std::unique_ptr< Database > pDatabase(new (std::nothrow) Database());
952         SysTryReturnResult(NID_SEC_CERT, pDatabase != null, E_OUT_OF_MEMORY, "Failed to allocate memory");
953
954         r = pDatabase->Construct(_CERT_ROOT_CA_CERT_TABLE, DB_OPEN_READ_ONLY, 0);
955         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to construct database instance.", GetErrorMessage(r));
956
957         statement.Append(L"SELECT * FROM rootcert");
958         std::unique_ptr< DbStatement > pStmt(pDatabase->CreateStatementN(statement));
959         std::unique_ptr< DbEnumerator > pEnum(pDatabase->ExecuteStatementN(*pStmt));
960         SysTryReturnResult(NID_SEC_CERT, pEnum != null, E_DATA_NOT_FOUND, "No certificate found in database.");
961
962         while (pEnum->MoveNext() == E_SUCCESS)
963         {
964                 certCount++;
965         }
966
967         count = certCount;
968
969         return r;
970 }
971
972 _UserCertDbStore::_UserCertDbStore(void)
973 {
974 }
975
976 _UserCertDbStore::~_UserCertDbStore(void)
977 {
978 }
979
980 result
981 _UserCertDbStore::InsertUserCertificate(UserCertRecord* pCertRecord)
982 {
983         result r = E_SUCCESS;
984         byte subjectNameBase64[_MAX_ISSUER_SUBJECT_NAME_SIZE] = {0, };
985         byte issuerNameBase64[_MAX_ISSUER_SUBJECT_NAME_SIZE] = {0, };
986         byte base64SerialNum[_MAX_SERIAL_NUMBER_SIZE] = {0, };
987         int subjectNameBase64Len = 0;
988         int base64IssuerNameLen = 0;
989         int base64SerialNumLen = 0;
990         String statement;
991
992         SysTryReturnResult(NID_SEC_CERT, pCertRecord != null, E_INVALID_ARG, "Invalid input parameter.");
993
994         memcpy(subjectNameBase64, pCertRecord->subjectName, pCertRecord->subjectNameLen);
995         memcpy(issuerNameBase64, pCertRecord->issuerName, pCertRecord->issuerNameLen);
996         memcpy(base64SerialNum, pCertRecord->serialNo, _MAX_SERIAL_NUMBER_SIZE);
997         memset(pCertRecord->subjectName, 0, _MAX_ISSUER_SUBJECT_NAME_SIZE);
998         memset(pCertRecord->issuerName, 0, _MAX_ISSUER_SUBJECT_NAME_SIZE);
999         memset(pCertRecord->serialNo, 0, _MAX_SERIAL_NUMBER_SIZE);
1000
1001         subjectNameBase64Len = _Base64::GetEncodedSize(pCertRecord->subjectNameLen);
1002         memset(pCertRecord->subjectName, 0, sizeof(pCertRecord->subjectName));
1003         r = _Base64::Encode(static_cast< byte* >(subjectNameBase64), pCertRecord->subjectNameLen, pCertRecord->subjectName, subjectNameBase64Len);
1004         SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_ENCODING_FAILED, "Failed to encode subject name of certificate.");
1005
1006         pCertRecord->subjectNameLen = subjectNameBase64Len;
1007
1008         base64IssuerNameLen = _Base64::GetEncodedSize(pCertRecord->issuerNameLen);
1009         memset(pCertRecord->issuerName, 0, sizeof(pCertRecord->issuerName));
1010         r = _Base64::Encode(static_cast< byte* >(issuerNameBase64), pCertRecord->issuerNameLen, pCertRecord->issuerName, base64IssuerNameLen);
1011         SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_ENCODING_FAILED, "Failed to encode issuer name of certificate.");
1012
1013         pCertRecord->issuerNameLen = base64IssuerNameLen;
1014
1015         base64SerialNumLen = _Base64::GetEncodedSize(pCertRecord->serialNoLen);
1016         memset(pCertRecord->serialNo, 0, sizeof(pCertRecord->serialNo));
1017         r = _Base64::Encode(static_cast< byte* >(base64SerialNum), pCertRecord->serialNoLen, pCertRecord->serialNo, base64SerialNumLen);
1018         SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_ENCODING_FAILED, "Failed to encode serial number of certificate.");
1019
1020         pCertRecord->serialNoLen = base64SerialNumLen;
1021
1022         std::unique_ptr< Database > pDatabase(new (std::nothrow) Database());
1023         SysTryReturnResult(NID_SEC_CERT, pDatabase != null, E_OUT_OF_MEMORY, "Failed to allocate memory");
1024
1025         r = pDatabase->Construct(_CERT_USER_CERT_TABLE, DB_OPEN_READ_WRITE, 0);
1026         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "Failed to construct database instance.");
1027
1028         r = pDatabase->BeginTransaction();
1029         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to begin transaction.", GetErrorMessage(r));
1030
1031         statement.Append(L"INSERT INTO usercert (certId, certPubKeyHash, certFormat, fileName, subjectNameLen, subjectName, issuerNameLen, issuerName, prvKeyPath, prvKeyLen, parentCa, installed, serialNo, serialNoLen) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?)");
1032         std::unique_ptr< DbStatement > pStmt(pDatabase->CreateStatementN(statement));
1033         r = pStmt->BindString(1, pCertRecord->certPubKeyHash);
1034         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to bind colmun in database statement.", GetErrorMessage(r));
1035         r = pStmt->BindInt(2, pCertRecord->certFormat);
1036         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to bind colmun in database statement.", GetErrorMessage(r));
1037         r = pStmt->BindString(3, pCertRecord->fileName);
1038         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to bind colmun in database statement.", GetErrorMessage(r));
1039         r = pStmt->BindInt(4, pCertRecord->subjectNameLen);
1040         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to bind colmun in database statement.", GetErrorMessage(r));
1041         r = pStmt->BindString(5, pCertRecord->subjectName);
1042         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to bind colmun in database statement.", GetErrorMessage(r));
1043         r = pStmt->BindInt(6, pCertRecord->issuerNameLen);
1044         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to bind colmun in database statement.", GetErrorMessage(r));
1045         r = pStmt->BindString(7, pCertRecord->issuerName);
1046         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to bind colmun in database statement.", GetErrorMessage(r));
1047         r = pStmt->BindString(8, pCertRecord->prvKeyPath);
1048         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to bind colmun in database statement.", GetErrorMessage(r));
1049         r = pStmt->BindInt(9, pCertRecord->prvKeyLen);
1050         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to bind colmun in database statement.", GetErrorMessage(r));
1051         r = pStmt->BindInt(10, pCertRecord->parentCa);
1052         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to bind colmun in database statement.", GetErrorMessage(r));
1053         r = pStmt->BindString(11, pCertRecord->installed);
1054         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to bind colmun in database statement.", GetErrorMessage(r));
1055         r = pStmt->BindString(12, pCertRecord->serialNo);
1056         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to bind colmun in database statement.", GetErrorMessage(r));
1057         r = pStmt->BindInt(13, pCertRecord->serialNoLen);
1058         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to bind colmun in database statement.", GetErrorMessage(r));
1059         std::unique_ptr< DbEnumerator > pEnum(pDatabase->ExecuteStatementN(*pStmt));
1060         AppAssert(!pEnum);
1061
1062         r = pDatabase->CommitTransaction();
1063         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to commit transaction.", GetErrorMessage(r));
1064
1065         return r;
1066 }
1067
1068 result
1069 _UserCertDbStore::UpdateParentCa(int certId, int parentCa)
1070 {
1071         result r = E_SUCCESS;
1072         String statement(_MAX_QUERY_LEN);
1073
1074         SysTryReturnResult(NID_SEC_CERT, certId > 0, E_INVALID_ARG, "Invalid input parameter.");
1075         SysTryReturnResult(NID_SEC_CERT, parentCa > 0, E_INVALID_ARG, "Invalid input parameter.");
1076
1077         std::unique_ptr< Database > pDatabase(new (std::nothrow) Database());
1078         SysTryReturnResult(NID_SEC_CERT, pDatabase != null, E_OUT_OF_MEMORY, "Failed to allocate memory.");
1079
1080         r = pDatabase->Construct(_CERT_USER_CERT_TABLE, DB_OPEN_READ_WRITE, 0);
1081         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to construct database instance.", GetErrorMessage(r));
1082
1083         r = pDatabase->BeginTransaction();
1084         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to begin transaction.", GetErrorMessage(r));
1085
1086         statement.Format(_MAX_QUERY_LEN, L"UPDATE usercert SET parentCa = '%d' WHERE certId = '%d'", parentCa, certId);
1087         r = pDatabase->ExecuteSql(statement, true);
1088         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to execute sql.", GetErrorMessage(r));
1089
1090         r = pDatabase->CommitTransaction();
1091         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to commit transaction.", GetErrorMessage(r));
1092
1093         return r;
1094 }
1095
1096 result
1097 _UserCertDbStore::UpdateRecordByCondition(byte* pCondition)
1098 {
1099         result r = E_SUCCESS;
1100         String statement(_MAX_QUERY_LEN);
1101
1102         SysTryReturnResult(NID_SEC_CERT, pCondition != null, E_INVALID_ARG, "Invalid input parameter.");
1103
1104         std::unique_ptr< Database > pDatabase(new (std::nothrow) Database());
1105         SysTryReturnResult(NID_SEC_CERT, pDatabase != null, E_OUT_OF_MEMORY, "Failed to allocate memory");
1106
1107         r = pDatabase->Construct(_CERT_USER_CERT_TABLE, DB_OPEN_READ_WRITE, 0);
1108         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to construct database instance.", GetErrorMessage(r));
1109
1110         r = pDatabase->BeginTransaction();
1111         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to begin transaction.", GetErrorMessage(r));
1112
1113         statement.Format(_MAX_QUERY_LEN, L"UPDATE usercert SET %s", pCondition);
1114
1115         r = pDatabase->ExecuteSql(statement, true);
1116         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to execute sql statement.", GetErrorMessage(r));
1117
1118         r = pDatabase->CommitTransaction();
1119         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to commit transactoin.", GetErrorMessage(r));
1120
1121         return r;
1122 }
1123
1124 result
1125 _UserCertDbStore::RemoveCertificateByCondition(byte* pCondition)
1126 {
1127         result r = E_SUCCESS;
1128         
1129         
1130         int curCertId = 0;
1131         String statement(_MAX_QUERY_LEN);
1132         String strVal;
1133
1134         SysTryReturnResult(NID_SEC_CERT, pCondition != null, E_INVALID_ARG, "Invalid input parameter.");
1135
1136         std::unique_ptr< Database > pDatabase(new (std::nothrow) Database());
1137         SysTryReturnResult(NID_SEC_CERT, pDatabase != null, E_OUT_OF_MEMORY, "Failed to allocate memory");
1138
1139         r = pDatabase->Construct(_CERT_USER_CERT_TABLE, DB_OPEN_READ_WRITE, 0);
1140         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to construct database instance.", GetErrorMessage(r));
1141
1142         statement.Format(_MAX_QUERY_LEN, L"SELECT * FROM usercert WHERE %s ORDER BY certId", pCondition);
1143         std::unique_ptr<  DbEnumerator > pEnum(pDatabase->QueryN(statement));
1144         SysTryReturnResult(NID_SEC_CERT, pEnum != null, E_DATA_NOT_FOUND, "No certificate found in database.");
1145
1146
1147         while (pEnum->MoveNext() == E_SUCCESS)
1148         {
1149                 r = pEnum->GetIntAt(0, curCertId);
1150                 SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database.", GetErrorMessage(r));
1151
1152                 r = _UserCertDbStore::RemoveCertificateById(curCertId);
1153                 SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to delete record from database.", GetErrorMessage(r));
1154
1155                 r = pEnum->GetStringAt(4, strVal);
1156                 SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database.", GetErrorMessage(r));
1157
1158                 Tizen::Io::File::Remove(strVal);
1159         }
1160
1161         return r;
1162 }
1163
1164 result
1165 _UserCertDbStore::CheckDuplicateCertificate(byte* pSubjectName, int subjectNameLen)
1166 {
1167         result r = E_SUCCESS;
1168         char subjectNameBase64[_MAX_ISSUER_SUBJECT_NAME_SIZE] = {0, };
1169         int subjectNameBase64Len = 0;
1170         String statement(_MAX_QUERY_LEN);
1171
1172         SysTryReturnResult(NID_SEC_CERT, pSubjectName != null, E_INVALID_ARG, "Invalid input parameter.");
1173         SysTryReturnResult(NID_SEC_CERT, subjectNameLen > 0, E_INVALID_ARG, "Invalid input parameter.");
1174
1175         subjectNameBase64Len = _Base64::GetEncodedSize(subjectNameLen);
1176         memset(subjectNameBase64, 0, sizeof(subjectNameBase64));
1177         r = _Base64::Encode(static_cast< byte* >(pSubjectName), subjectNameLen, subjectNameBase64, subjectNameBase64Len);
1178         SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_ENCODING_FAILED, "Failed to encode subject name of certificate.");
1179
1180         std::unique_ptr< Database > pDatabase(new (std::nothrow) Database());
1181         SysTryReturnResult(NID_SEC_CERT, pDatabase != null, E_OUT_OF_MEMORY, "Failed to allocate memory");
1182
1183         r = pDatabase->Construct(_CERT_USER_CERT_TABLE, DB_OPEN_READ_ONLY, 0);
1184         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to construct database instance.", GetErrorMessage(r));
1185
1186         r = pDatabase->BeginTransaction();
1187         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to begin transaction.", GetErrorMessage(r));
1188
1189         statement.Format(_MAX_QUERY_LEN, L"SELECT * FROM usercert WHERE subjectName = '%s' AND subjectNameLen = '%d'", subjectNameBase64, subjectNameBase64Len);
1190
1191         std::unique_ptr<  DbEnumerator > pEnum(pDatabase->QueryN(statement));
1192         SysTryReturnResult(NID_SEC_CERT, pEnum != null, E_DATA_NOT_FOUND, "No certificate found in database.");
1193
1194         return r;
1195 }
1196
1197 result
1198 _UserCertDbStore::CheckDuplicateCertificate(byte* pIssuerName, int issuerNameLen, byte* pSerialNumber)
1199 {
1200         result r = E_SUCCESS;
1201         char issuerNameBase64[_MAX_ISSUER_SUBJECT_NAME_SIZE] = {0, };
1202         char base64SerialNo[_MAX_SERIAL_NUMBER_SIZE] = {0, };
1203         int base64IssuerNameLen = 0;
1204         int base64SerialNoLen = 0;
1205         int serialNoLen = 0;
1206         String statement(_MAX_QUERY_LEN);
1207
1208         SysTryReturnResult(NID_SEC_CERT, pIssuerName != null, E_INVALID_ARG, "Invald input parameter");
1209         SysTryReturnResult(NID_SEC_CERT, issuerNameLen >= 0, E_INVALID_ARG, "Invald input parameter");
1210         SysTryReturnResult(NID_SEC_CERT, pSerialNumber != null, E_INVALID_ARG, "Invald input parameter");
1211
1212         base64IssuerNameLen = _Base64::GetEncodedSize(issuerNameLen);
1213         memset(issuerNameBase64, 0, sizeof(issuerNameBase64));
1214         r = _Base64::Encode(pIssuerName, issuerNameLen, issuerNameBase64, base64IssuerNameLen);
1215         SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_ENCODING_FAILED, "Failed to encode issuer name of certificate.");
1216
1217         serialNoLen = strlen(reinterpret_cast< const char* >(pSerialNumber));
1218
1219         base64SerialNoLen = _Base64::GetEncodedSize(serialNoLen);
1220         memset(base64SerialNo, 0, sizeof(base64SerialNo));
1221         r = _Base64::Encode(pSerialNumber, serialNoLen, base64SerialNo, base64SerialNoLen);
1222         SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_ENCODING_FAILED, "Failed to encode serial number of certificate.");
1223
1224         std::unique_ptr< Database > pDatabase(new (std::nothrow) Database());
1225         SysTryReturnResult(NID_SEC_CERT, pDatabase != null, E_OUT_OF_MEMORY, "Failed to allocate memory");
1226
1227         r = pDatabase->Construct(_CERT_USER_CERT_TABLE, DB_OPEN_READ_ONLY, 0);
1228         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to construct database instance.", GetErrorMessage(r));
1229
1230         r = pDatabase->BeginTransaction();
1231         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to begin transaction.", GetErrorMessage(r));
1232
1233         statement.Format(_MAX_QUERY_LEN, L"SELECT * FROM usercert WHERE issuerName = '%s' AND issuerNameLen = '%d' AND serialNo = '%s'", issuerNameBase64, base64IssuerNameLen, base64SerialNo);
1234         std::unique_ptr<  DbEnumerator > pEnum(pDatabase->QueryN(statement));
1235         SysTryReturnResult(NID_SEC_CERT, pEnum != null, E_DATA_NOT_FOUND, "No certificate found in database.");
1236
1237         return r;
1238 }
1239
1240 result
1241 _UserCertDbStore::GetFirstRecordByConditions(byte* pCondition, UserCertRecord* pCertRecord)
1242 {
1243         result r = E_SUCCESS;
1244         String statement(_MAX_QUERY_LEN);
1245         ByteBuffer* pTempBuf = null;
1246         char tmpName[_MAX_ISSUER_SUBJECT_NAME_SIZE] = {0, };
1247         int len = 0;
1248         String strVal;
1249
1250         SysTryReturnResult(NID_SEC_CERT, pCondition != null, E_INVALID_ARG, "Invalid input parameter");
1251         SysTryReturnResult(NID_SEC_CERT, pCertRecord != null, E_INVALID_ARG, "Invalid input parameter");
1252
1253         std::unique_ptr< Database > pDatabase(new (std::nothrow) Database());
1254         SysTryReturnResult(NID_SEC_CERT, pDatabase != null, E_OUT_OF_MEMORY, "Failed to allocate memory");
1255
1256         r = pDatabase->Construct(_CERT_USER_CERT_TABLE, DB_OPEN_READ_ONLY, 0);
1257         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to construct database instance.", GetErrorMessage(r));
1258
1259         statement.Format(_MAX_QUERY_LEN, L"SELECT * FROM usercert WHERE %s", pCondition);
1260         std::unique_ptr<  DbEnumerator > pEnum(pDatabase->QueryN(statement));
1261         SysTryReturn(NID_SEC_CERT, pEnum != null, E_DATA_NOT_FOUND, E_DATA_NOT_FOUND, "No certificate found in database.");
1262
1263
1264         r = pEnum->MoveNext();
1265         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database.", GetErrorMessage(r));
1266
1267         r = pEnum->GetIntAt(0, pCertRecord->certId);
1268         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database from column 0.", GetErrorMessage(r));
1269
1270         r = pEnum->GetStringAt(1, strVal);
1271         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database from column 1.", GetErrorMessage(r));
1272
1273         pTempBuf = Tizen::Base::Utility::StringUtil::StringToUtf8N(strVal);
1274         r = GetLastResult();
1275         SysTryReturn(NID_SEC_CERT, pTempBuf != null, r, r, "[%s] String to Utf8 function failed.", GetErrorMessage(r));
1276
1277         memcpy(pCertRecord->certPubKeyHash, reinterpret_cast< const char* >(pTempBuf->GetPointer()), pTempBuf->GetRemaining());
1278         pCertRecord->certPubKeyHash[pTempBuf->GetRemaining()] = '\0';
1279
1280         r = pEnum->GetIntAt(2, pCertRecord->certFormat);
1281         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database from column 2.", GetErrorMessage(r));
1282
1283         r = pEnum->GetStringAt(3, strVal);
1284         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database from column 3.", GetErrorMessage(r));
1285
1286         pTempBuf = Tizen::Base::Utility::StringUtil::StringToUtf8N(strVal);
1287         r = GetLastResult();
1288         SysTryReturn(NID_SEC_CERT, pTempBuf != null, r, r, "[%s] String to Utf8 function failed.", GetErrorMessage(r));
1289
1290         memcpy(pCertRecord->fileName, reinterpret_cast< const char* >(pTempBuf->GetPointer()), pTempBuf->GetRemaining());
1291         pCertRecord->fileName[pTempBuf->GetRemaining()] = '\0';
1292
1293         r = pEnum->GetIntAt(4, pCertRecord->subjectNameLen);
1294         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database from column 4.", GetErrorMessage(r));
1295
1296         r = pEnum->GetStringAt(5, strVal);
1297         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database from column 5.", GetErrorMessage(r));
1298
1299         pTempBuf = Tizen::Base::Utility::StringUtil::StringToUtf8N(strVal);
1300         r = GetLastResult();
1301         SysTryReturn(NID_SEC_CERT, pTempBuf != null, r, r, "[%s] String to Utf8 function failed.", GetErrorMessage(r));
1302
1303         memset(tmpName, 0, _MAX_ISSUER_SUBJECT_NAME_SIZE);
1304         memcpy(tmpName, reinterpret_cast< const char* >(pTempBuf->GetPointer()), pTempBuf->GetRemaining());
1305         _Base64::Decode(tmpName, pCertRecord->subjectNameLen, reinterpret_cast< byte* >(pCertRecord->subjectName), pCertRecord->subjectNameLen);
1306
1307         r = pEnum->GetIntAt(6, pCertRecord->issuerNameLen);
1308         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database from column 6.", GetErrorMessage(r));
1309
1310         r = pEnum->GetStringAt(7, strVal);
1311         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database from column 7.", GetErrorMessage(r));
1312
1313         pTempBuf = Tizen::Base::Utility::StringUtil::StringToUtf8N(strVal);
1314         r = GetLastResult();
1315         SysTryReturn(NID_SEC_CERT, pTempBuf != null, r, r, "[%s] String to Utf8 function failed.", GetErrorMessage(r));
1316
1317
1318         memset(tmpName, 0, _MAX_ISSUER_SUBJECT_NAME_SIZE);
1319         memcpy(tmpName, reinterpret_cast< const char* >(pTempBuf->GetPointer()), pTempBuf->GetRemaining());
1320         _Base64::Decode(tmpName, pCertRecord->issuerNameLen, reinterpret_cast< byte* >(pCertRecord->issuerName), pCertRecord->issuerNameLen);
1321
1322         r = pEnum->GetStringAt(8, strVal);
1323         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database from column 8.", GetErrorMessage(r));
1324
1325         pTempBuf = Tizen::Base::Utility::StringUtil::StringToUtf8N(strVal);
1326         if (pTempBuf)
1327         {
1328                 memcpy(pCertRecord->prvKeyPath, reinterpret_cast< const char* >(pTempBuf->GetPointer()), pTempBuf->GetRemaining());
1329         }
1330         else
1331         {
1332                 memset(pCertRecord->prvKeyPath, 0, _MAX_PRV_KEY_PATH_SIZE_SIZE);
1333         }
1334
1335         r = pEnum->GetIntAt(9, pCertRecord->prvKeyLen);
1336         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database from column 9.", GetErrorMessage(r));
1337
1338         r = pEnum->GetIntAt(10, pCertRecord->parentCa);
1339         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database from column 10.", GetErrorMessage(r));
1340
1341         r = pEnum->GetStringAt(11, strVal);
1342         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database from column 11.", GetErrorMessage(r));
1343
1344         pTempBuf = Tizen::Base::Utility::StringUtil::StringToUtf8N(strVal);
1345         r = GetLastResult();
1346         SysTryReturn(NID_SEC_CERT, pTempBuf != null, r, r, "[%s] String to Utf8 function failed.", GetErrorMessage(r));
1347
1348         memcpy(pCertRecord->installed, reinterpret_cast< const char* >(pTempBuf->GetPointer()), pTempBuf->GetRemaining());
1349         pCertRecord->installed[pTempBuf->GetRemaining()] = '\0';
1350
1351         r = pEnum->GetStringAt(12, strVal);
1352         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database from column 12.", GetErrorMessage(r));
1353
1354         pTempBuf = Tizen::Base::Utility::StringUtil::StringToUtf8N(strVal);
1355         r = GetLastResult();
1356         SysTryReturn(NID_SEC_CERT, pTempBuf != null, r, r, "[%s] String to Utf8 function failed.", GetErrorMessage(r));
1357
1358         memset(tmpName, 0, _MAX_ISSUER_SUBJECT_NAME_SIZE);
1359         memcpy(tmpName, reinterpret_cast< const char* >(pTempBuf->GetPointer()), pTempBuf->GetRemaining());
1360
1361         len = pTempBuf->GetRemaining();
1362         _Base64::Decode(tmpName, len, reinterpret_cast< byte* >(pCertRecord->serialNo), len);
1363
1364         r = pEnum->GetIntAt(13, pCertRecord->serialNoLen);
1365         pCertRecord->serialNoLen = _Base64::GetDecodedSize(pCertRecord->serialNoLen);
1366         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database from column 13.", GetErrorMessage(r));
1367
1368         return r;
1369 }
1370
1371 result
1372 _UserCertDbStore::GetNextRecordByCondition(byte* pCondition, UserCertRecord* pCertRecord, int curCertId)
1373 {
1374         result r = E_SUCCESS;
1375         String statement(_MAX_QUERY_LEN);
1376         ByteBuffer* pTempBuf = null;
1377         String strVal;
1378
1379         SysTryReturnResult(NID_SEC_CERT, pCondition != null, E_INVALID_ARG, "Input parameters are invalid");
1380         SysTryReturnResult(NID_SEC_CERT, pCertRecord != null, E_INVALID_ARG, "Input parameters are invalid");
1381
1382         std::unique_ptr< Database > pDatabase(new (std::nothrow) Database());
1383         SysTryReturnResult(NID_SEC_CERT, pDatabase != null, E_OUT_OF_MEMORY, "Failed to allocate memory");
1384
1385         r = pDatabase->Construct(_CERT_USER_CERT_TABLE, DB_OPEN_READ_ONLY, 0);
1386         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to construct database instance.", GetErrorMessage(r));
1387
1388         statement.Format(_MAX_QUERY_LEN, L"SELECT * FROM usercert WHERE %s AND certId > '%d'", pCondition, curCertId);
1389         std::unique_ptr<  DbEnumerator > pEnum(pDatabase->QueryN(statement));
1390         SysTryReturnResult(NID_SEC_CERT, pEnum != null, E_DATA_NOT_FOUND, "No certificate found in database.");
1391
1392         r = pEnum->MoveNext();
1393         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database.", GetErrorMessage(r));
1394
1395         r = pEnum->GetIntAt(0, pCertRecord->certId);
1396         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database from column 0.", GetErrorMessage(r));
1397
1398         r = pEnum->GetStringAt(1, strVal);
1399         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database from column 1.", GetErrorMessage(r));
1400
1401         pTempBuf = Tizen::Base::Utility::StringUtil::StringToUtf8N(strVal);
1402         r = GetLastResult();
1403         SysTryReturn(NID_SEC_CERT, pTempBuf != null, r, r, "[%s] String to Utf8 function failed.", GetErrorMessage(r));
1404
1405         memcpy(pCertRecord->certPubKeyHash, reinterpret_cast< const char* >(pTempBuf->GetPointer()), pTempBuf->GetRemaining());
1406         pCertRecord->certPubKeyHash[pTempBuf->GetRemaining()] = '\0';
1407
1408         r = pEnum->GetIntAt(2, pCertRecord->certFormat);
1409         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database from column 2.", GetErrorMessage(r));
1410
1411         r = pEnum->GetStringAt(3, strVal);
1412         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database from column 3.", GetErrorMessage(r));
1413
1414         pTempBuf = Tizen::Base::Utility::StringUtil::StringToUtf8N(strVal);
1415         r = GetLastResult();
1416         SysTryReturn(NID_SEC_CERT, pTempBuf != null, r, r, "[%s] String to Utf8 function failed.", GetErrorMessage(r));
1417
1418         memcpy(pCertRecord->fileName, reinterpret_cast< const char* >(pTempBuf->GetPointer()), pTempBuf->GetRemaining());
1419         pCertRecord->fileName[pTempBuf->GetRemaining()] = '\0';
1420
1421         r = pEnum->GetIntAt(4, pCertRecord->subjectNameLen);
1422         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database from column 4.", GetErrorMessage(r));
1423
1424         r = pEnum->GetStringAt(5, strVal);
1425         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database from column 5.", GetErrorMessage(r));
1426
1427         pTempBuf = Tizen::Base::Utility::StringUtil::StringToUtf8N(strVal);
1428         r = GetLastResult();
1429         SysTryReturn(NID_SEC_CERT, pTempBuf != null, r, r, "[%s] String to Utf8 function failed.", GetErrorMessage(r));
1430
1431         memcpy(pCertRecord->subjectName, reinterpret_cast< const char* >(pTempBuf->GetPointer()), pTempBuf->GetRemaining());
1432         pCertRecord->subjectName[pTempBuf->GetRemaining()] = '\0';
1433
1434         r = pEnum->GetIntAt(6, pCertRecord->issuerNameLen);
1435         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database from column 6.", GetErrorMessage(r));
1436
1437         r = pEnum->GetStringAt(7, strVal);
1438         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database from column 7.", GetErrorMessage(r));
1439
1440         pTempBuf = Tizen::Base::Utility::StringUtil::StringToUtf8N(strVal);
1441         r = GetLastResult();
1442         SysTryReturn(NID_SEC_CERT, pTempBuf != null, r, r, "[%s] String to Utf8 function failed.", GetErrorMessage(r));
1443
1444         memcpy(pCertRecord->issuerName, reinterpret_cast< const char* >(pTempBuf->GetPointer()), pTempBuf->GetRemaining());
1445         pCertRecord->issuerName[pTempBuf->GetRemaining()] = '\0';
1446
1447         r = pEnum->GetStringAt(8, strVal);
1448         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database from column 8.", GetErrorMessage(r));
1449
1450         pTempBuf = Tizen::Base::Utility::StringUtil::StringToUtf8N(strVal);
1451         if (pTempBuf)
1452         {
1453                 memcpy(pCertRecord->prvKeyPath, reinterpret_cast< const char* >(pTempBuf->GetPointer()), pTempBuf->GetRemaining());
1454         }
1455         else
1456         {
1457                 memset(pCertRecord->prvKeyPath, 0, _MAX_PRV_KEY_PATH_SIZE_SIZE);
1458         }
1459
1460         r = pEnum->GetIntAt(9, pCertRecord->prvKeyLen);
1461         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database from column 9.", GetErrorMessage(r));
1462
1463         r = pEnum->GetIntAt(10, pCertRecord->parentCa);
1464         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database from column 10.", GetErrorMessage(r));
1465
1466         r = pEnum->GetStringAt(11, strVal);
1467         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database from column 11.", GetErrorMessage(r));
1468
1469         pTempBuf = Tizen::Base::Utility::StringUtil::StringToUtf8N(strVal);
1470         r = GetLastResult();
1471         SysTryReturn(NID_SEC_CERT, pTempBuf != null, r, r, "[%s] String to Utf8 function failed.", GetErrorMessage(r));
1472
1473         memcpy(pCertRecord->installed, reinterpret_cast< const char* >(pTempBuf->GetPointer()), pTempBuf->GetRemaining());
1474         pCertRecord->installed[pTempBuf->GetRemaining()] = '\0';
1475
1476         r = pEnum->GetStringAt(12, strVal);
1477         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database from column 12.", GetErrorMessage(r));
1478
1479         pTempBuf = Tizen::Base::Utility::StringUtil::StringToUtf8N(strVal);
1480         r = GetLastResult();
1481         SysTryReturn(NID_SEC_CERT, pTempBuf != null, r, r, "[%s] String to Utf8 function failed.", GetErrorMessage(r));
1482
1483         memcpy(pCertRecord->serialNo, reinterpret_cast< const char* >(pTempBuf->GetPointer()), pTempBuf->GetRemaining());
1484         pCertRecord->serialNo[pTempBuf->GetRemaining()] = '\0';
1485
1486         r = pEnum->GetIntAt(13, pCertRecord->serialNoLen);
1487         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database from column 13.", GetErrorMessage(r));
1488
1489         return r;
1490 }
1491
1492 result
1493 _UserCertDbStore::GetCurrentCertId(int& curCertId)
1494 {
1495         result r = E_SUCCESS;
1496         int certId = 0;
1497
1498         std::unique_ptr< Database > pDatabase(new (std::nothrow) Database());
1499         SysTryReturnResult(NID_SEC_CERT, pDatabase != null, E_OUT_OF_MEMORY, "Failed to allocate memory");
1500
1501         r = pDatabase->Construct(_CERT_USER_CERT_TABLE, DB_OPEN_READ_ONLY, 0);
1502         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to construct database instance.", GetErrorMessage(r));
1503
1504         std::unique_ptr< DbEnumerator> pEnum(pDatabase->QueryN("SELECT seq FROM sqlite_sequence"));
1505         SysTryReturnResult(NID_SEC_CERT, pEnum != null, E_DATA_NOT_FOUND, "No certificate found in database.");
1506
1507         r = pEnum->MoveNext();
1508         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to move next.", GetErrorMessage(r));
1509
1510         r = pEnum->GetIntAt(0, certId);
1511         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database.", GetErrorMessage(r));
1512
1513         curCertId = certId;
1514
1515         return r;
1516 }
1517
1518 result
1519 _UserCertDbStore::GetCountByCondition(byte* pCondition, int& count)
1520 {
1521         result r = E_SUCCESS;
1522         int certCount = 0;
1523         String statement(_MAX_QUERY_LEN);
1524
1525         SysTryReturnResult(NID_SEC_CERT, pCondition != null, E_INVALID_ARG, "Input parameter are invalid");
1526
1527         std::unique_ptr< Database > pDatabase(new (std::nothrow) Database());
1528         SysTryReturnResult(NID_SEC_CERT, pDatabase != null, E_OUT_OF_MEMORY, "Failed to allocate memory");
1529
1530         r = pDatabase->Construct(_CERT_USER_CERT_TABLE, DB_OPEN_READ_ONLY, 0);
1531         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Construct fails.", GetErrorMessage(r));
1532
1533         statement.Format(_MAX_QUERY_LEN, L"SELECT * FROM usercert WHERE %s", pCondition);
1534         std::unique_ptr<  DbEnumerator > pEnum(pDatabase->QueryN(statement));
1535         SysTryReturnResult(NID_SEC_CERT, pEnum != null, E_DATA_NOT_FOUND, "No certificate found in Db.");
1536
1537         while (pEnum->MoveNext() == E_SUCCESS)
1538         {
1539                 certCount++;
1540         }
1541
1542         count = certCount;
1543
1544         return r;
1545 }
1546
1547 result
1548 _UserCertDbStore::GetParentCaByCondition(byte* pCondition, int& parentCa)
1549 {
1550         result r = E_SUCCESS;
1551         int currParentCa = 0;
1552         String statement(_MAX_QUERY_LEN);
1553
1554         SysTryReturnResult(NID_SEC_CERT, pCondition != null, E_INVALID_ARG, "Input parameter are invalid");
1555
1556         std::unique_ptr< Database > pDatabase(new (std::nothrow) Database());
1557         SysTryReturnResult(NID_SEC_CERT, pDatabase != null, E_OUT_OF_MEMORY, "Failed to allocate memory");
1558
1559         r = pDatabase->Construct(_CERT_USER_CERT_TABLE, DB_OPEN_READ_ONLY, 0);
1560         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to construct database instance.", GetErrorMessage(r));
1561
1562         statement.Format(_MAX_QUERY_LEN, L"SELECT parentCa FROM usercert WHERE %s", pCondition);
1563         std::unique_ptr<  DbEnumerator > pEnum(pDatabase->QueryN(statement));
1564         SysTryReturnResult(NID_SEC_CERT, pEnum != null, E_DATA_NOT_FOUND, "No certificate found in database.");
1565
1566         if (pEnum->MoveNext() == E_SUCCESS)
1567         {
1568                 r = pEnum->GetIntAt(10, currParentCa);
1569                 SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Unable to get data from database.", GetErrorMessage(r));
1570         }
1571
1572         parentCa = currParentCa;
1573
1574         return r;
1575 }
1576
1577 result
1578 _UserCertDbStore::SelectUserCertificateBycertId(int certId, UserCertRecord* pCertRecord)
1579 {
1580         byte condition[_MAX_TYPE_CONST_SIZE] = {0, };
1581
1582         return _UserCertDbStore::GetNextRecordByCondition(static_cast< byte* >(condition), pCertRecord, certId);
1583
1584 }
1585
1586 result
1587 _UserCertDbStore::RemoveCertificateById(int certId)
1588 {
1589         result r = E_SUCCESS;
1590         String statement;
1591
1592         SysTryReturnResult(NID_SEC_CERT, certId > 0, E_INVALID_ARG, "Invalid input certificate id.");
1593
1594         std::unique_ptr< Database > pDatabase(new (std::nothrow) Database());
1595         SysTryReturnResult(NID_SEC_CERT, pDatabase != null, E_OUT_OF_MEMORY, "Failed to allocate memory");
1596
1597         r = pDatabase->Construct(_CERT_USER_CERT_TABLE, DB_OPEN_READ_WRITE, 0);
1598         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to construct database instance.", GetErrorMessage(r));
1599
1600         r = pDatabase->BeginTransaction();
1601         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to begin transaction.", GetErrorMessage(r));
1602
1603         statement.Append(L"DELETE FROM usercert WHERE certId = ?");
1604         std::unique_ptr< DbStatement > pStmt(pDatabase->CreateStatementN(statement));
1605         SysTryReturn(NID_SEC_CERT, pStmt, GetLastResult(), GetLastResult(), "[%s] Failed to create statement.", GetErrorMessage(GetLastResult()));
1606
1607         r = pStmt->BindInt(0, certId);
1608         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to bind colmun in database statement.", GetErrorMessage(r));
1609
1610         pDatabase->ExecuteStatementN(*pStmt);
1611
1612         r = pDatabase->CommitTransaction();
1613         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to commit transaction.", GetErrorMessage(r));
1614
1615         return r;
1616 }
1617
1618 result
1619 _UserCertDbStore::RemoveAllCertificates(void)
1620 {
1621         result r = E_SUCCESS;
1622         String statement;
1623
1624         std::unique_ptr< Database > pDatabase(new (std::nothrow) Database());
1625         SysTryReturnResult(NID_SEC_CERT, pDatabase != null, E_OUT_OF_MEMORY, "Failed to allocate memory");
1626
1627         r = pDatabase->Construct(_CERT_USER_CERT_TABLE, DB_OPEN_READ_WRITE, 0);
1628         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to construct database instance.", GetErrorMessage(r));
1629
1630         r = pDatabase->BeginTransaction();
1631         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to begin transaction.", GetErrorMessage(r));
1632
1633         statement.Append(L"DELETE from usercert");
1634         r = pDatabase->ExecuteSql(statement, true);
1635         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to execute sql.", GetErrorMessage(r));
1636
1637         r = pDatabase->CommitTransaction();
1638         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to commit transaction.", GetErrorMessage(r));
1639
1640         return r;
1641 }
1642
1643 result
1644 _UserCertDbStore::GetNumberOfCertificates(int& count)
1645 {
1646         result r = E_SUCCESS;
1647         String statement;
1648         int certCount = 0;
1649
1650         std::unique_ptr< Database > pDatabase(new (std::nothrow) Database());
1651         SysTryReturnResult(NID_SEC_CERT, pDatabase != null, E_OUT_OF_MEMORY, "Failed to allocate memory");
1652
1653         r = pDatabase->Construct(_CERT_USER_CERT_TABLE, DB_OPEN_READ_ONLY, 0);
1654         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to construct database instance.", GetErrorMessage(r));
1655
1656         statement.Append(L"SELECT * FROM usercert");
1657
1658         std::unique_ptr< DbStatement > pStmt(pDatabase->CreateStatementN(statement));
1659         SysTryReturnResult(NID_SEC_CERT, pStmt != null, E_DATA_NOT_FOUND, "Failed to create statement for user certifcates.");
1660
1661         std::unique_ptr< DbEnumerator > pEnum(pDatabase->ExecuteStatementN(*pStmt.get()));
1662         SysTryReturnResult(NID_SEC_CERT, pEnum != null, E_DATA_NOT_FOUND, "No certificate found in database.");
1663
1664         while (pEnum->MoveNext() == E_SUCCESS)
1665         {
1666                 certCount++;
1667         }
1668
1669         count = certCount;
1670
1671         return r;
1672 }
1673
1674 } } } //Tizen::Security::Cert