2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 * @file cert-server-logic.c
18 * @author Madhan A K (madhan.ak@samsung.com)
19 * Kyungwook Tak (k.tak@samsung.com)
21 * @brief cert-server logic.
25 #include <sys/types.h>
32 #include <sys/smack.h>
33 #include <sys/socket.h>
35 #include <ckmc/ckmc-manager.h>
36 #include <ckmc/ckmc-error.h>
38 #include <cert-svc/cerror.h>
39 #include <cert-svc/ccert.h>
40 #include <vcore/Client.h>
42 #include <cert-server-debug.h>
43 #include <cert-server-logic.h>
44 #include <cert-server-db.h>
46 static CertStatus int_to_CertStatus(int intval)
57 static int CertStatus_to_int(CertStatus status)
68 static const char *storetype_to_string(CertStoreType type)
71 case VPN_STORE: return "vpn";
72 case EMAIL_STORE: return "email";
73 case WIFI_STORE: return "wifi";
74 case SYSTEM_STORE: return "ssl";
79 static CertStoreType nextStore(CertStoreType type)
82 case NONE_STORE: return VPN_STORE;
83 case VPN_STORE: return WIFI_STORE;
84 case WIFI_STORE: return EMAIL_STORE;
85 case EMAIL_STORE: return SYSTEM_STORE;
86 case SYSTEM_STORE: return NONE_STORE;
87 default: return NONE_STORE;
91 static bool hasStore(CertStoreType types, CertStoreType type)
93 return (types & type) != 0 ? true : false;
96 char *add_shared_owner_prefix(const char *name)
98 char *ckm_alias = NULL;
99 int result = asprintf(&ckm_alias, "%s%s%s", ckmc_owner_id_system, ckmc_owner_id_separator, name);
100 if (result < 0 || ckm_alias == NULL) {
101 SLOGE("Failed to allocate memory");
108 int ckmc_remove_alias_with_shared_owner_prefix(const char *name)
110 char *ckm_alias = add_shared_owner_prefix(name);
112 SLOGE("Failed to allocate memory");
113 return CKMC_ERROR_OUT_OF_MEMORY;
116 int result = ckmc_remove_alias(ckm_alias);
123 char *get_complete_path(const char *str1, const char *str2)
131 if (str1[strlen(str1) - 1] != '/')
132 as_result = asprintf(&result, "%s/%s", str1, str2);
134 as_result = asprintf(&result, "%s%s", str1, str2);
142 int add_file_to_system_cert_dir(const char *gname)
144 int ret = CERTSVC_SUCCESS;
146 /* find certificate which filehash name is gname in root ca certs path. */
147 char *target = get_complete_path(ROOT_CA_CERTS_DIR, gname);
148 char *link = get_complete_path(SYSTEM_CERT_DIR, gname);
150 if (target == NULL || link == NULL) {
151 SLOGE("Failed to get complete path.");
152 ret = CERTSVC_BAD_ALLOC;
156 if (symlink(target, link) != 0) {
157 SLOGE("Failed to make symlink from[%s] to[%s]", target, link);
170 int del_file_from_system_cert_dir(const char *gname)
172 int ret = CERTSVC_SUCCESS;
175 link = get_complete_path(SYSTEM_CERT_DIR, gname);
177 SLOGE("Failed to construct source file path.");
181 if (unlink(link) != 0) {
182 SLOGE("unlink %s failed. errno : %d", link, errno);
194 int write_to_ca_cert_crt_file(const char *mode, const char *cert)
196 int result = CERTSVC_SUCCESS;
199 if (cert == NULL || strlen(cert) == 0) {
200 SLOGE("Input buffer is NULL.");
201 return CERTSVC_WRONG_ARGUMENT;
204 if (!(fp = fopen(CERTSVC_CRT_FILE_PATH, mode))) {
205 SLOGE("Failed to open the file for writing, [%s].", CERTSVC_CRT_FILE_PATH);
209 /* if mode of writing is to append, then goto end of file */
210 if (strcmp(mode,"ab") == 0)
211 fseek(fp, 0L, SEEK_END);
213 size_t cert_len = strlen(cert);
214 if (fwrite(cert, sizeof(char), cert_len, fp) != cert_len) {
215 SLOGE("Fail to write into file.");
216 result = CERTSVC_FAIL;
220 /* adding empty line at the end */
221 fwrite("\n", sizeof(char), 1, fp);
230 int saveCertificateToStore(const char *gname, const char *cert)
232 if (!gname || !cert) {
233 SLOGE("Invalid input parameter passed.");
234 return CERTSVC_WRONG_ARGUMENT;
237 ckmc_policy_s cert_policy;
238 cert_policy.password = NULL;
239 cert_policy.extractable = true;
241 ckmc_raw_buffer_s cert_data;
242 cert_data.data = (unsigned char *)cert;
243 cert_data.size = strlen(cert);
245 char *ckm_alias = add_shared_owner_prefix(gname);
247 SLOGE("Failed to make alias. memory allocation error.");
248 return CERTSVC_BAD_ALLOC;
251 int result = ckmc_save_data(ckm_alias, cert_data, cert_policy);
254 if (result == CKMC_ERROR_DB_ALIAS_EXISTS) {
255 SLOGI("same alias with gname[%s] alrady exist in ckm. Maybe other store type have it. skip.", gname);
256 return CERTSVC_SUCCESS;
259 if (result != CKMC_ERROR_NONE) {
260 SLOGE("Failed to save trusted data. ckm errcode[%d]", result);
264 return CERTSVC_SUCCESS;
267 int saveCertificateToSystemStore(const char *gname)
270 SLOGE("Invalid input parameter passed.");
271 return CERTSVC_WRONG_ARGUMENT;
274 int result = add_file_to_system_cert_dir(gname);
275 if (result != CERTSVC_SUCCESS)
276 SLOGE("Failed to store the certificate in store.");
281 int get_certificate_buffer_from_store(CertStoreType storeType, const char *gname, char **pcert)
283 int result = CERTSVC_SUCCESS;
285 char *tempBuffer = NULL;
287 sqlite3_stmt *stmt = NULL;
290 SLOGE("Invalid input parameter passed.");
291 return CERTSVC_WRONG_ARGUMENT;
294 if (storeType != SYSTEM_STORE)
295 query = sqlite3_mprintf("select * from %Q where gname=%Q and enabled=%d and is_root_app_enabled=%d",
296 storetype_to_string(storeType), gname, ENABLED, ENABLED);
298 query = sqlite3_mprintf("select certificate from ssl where gname=%Q and enabled=%d and is_root_app_enabled=%d",
299 gname, ENABLED, ENABLED);
301 result = execute_select_query(query, &stmt);
302 if (result != CERTSVC_SUCCESS) {
303 SLOGE("Querying database failed.");
304 result = CERTSVC_FAIL;
308 records = sqlite3_step(stmt);
309 if (records != SQLITE_ROW || records == SQLITE_DONE) {
310 SLOGE("No valid records found for given gname [%s].",gname);
311 result = CERTSVC_FAIL;
315 tempBuffer = (char *)malloc(sizeof(char) * VCORE_MAX_RECV_DATA_SIZE);
317 SLOGE("Fail to allocate memory");
318 result = CERTSVC_FAIL;
322 memset(tempBuffer, 0x00, VCORE_MAX_RECV_DATA_SIZE);
324 if (storeType == SYSTEM_STORE)
325 result = getCertificateDetailFromSystemStore(gname, tempBuffer);
327 result = getCertificateDetailFromStore(storeType, PEM_CRT, gname, tempBuffer);
329 if (result != CERTSVC_SUCCESS) {
330 SLOGE("Failed to set request data.");
331 result = CERTSVC_WRONG_ARGUMENT;
338 if (result != CERTSVC_SUCCESS)
345 sqlite3_finalize(stmt);
350 int update_ca_certificate_file(char *cert)
352 int result = CERTSVC_SUCCESS;
358 sqlite3_stmt *stmt = NULL;
359 CertStoreType storeType;
362 * During install of a root certificate, the root certificate gets appended at
363 * the end to optimise the write operation onto ca-certificate.crt file.
365 if (cert != NULL && strlen(cert) > 0) {
366 result = write_to_ca_cert_crt_file("ab", cert);
367 if (result != CERTSVC_SUCCESS) {
368 SLOGE("Failed to write to file. result[%d]", result);
372 return CERTSVC_SUCCESS;
375 for (storeType = VPN_STORE; storeType != NONE_STORE; storeType = nextStore(storeType)) {
376 if (storeType == SYSTEM_STORE)
377 query = sqlite3_mprintf("select certificate from ssl where enabled=%d and is_root_app_enabled=%d", ENABLED, ENABLED);
379 query = sqlite3_mprintf("select gname from %Q where is_root_cert=%d and enabled=%d and is_root_app_enabled=%d",
380 storetype_to_string(storeType), ENABLED, ENABLED, ENABLED);
382 result = execute_select_query(query, &stmt);
388 if (result != CERTSVC_SUCCESS) {
389 SLOGE("Querying database failed.");
393 /* update the ca-certificate.crt file */
395 records = sqlite3_step(stmt);
396 if (records == SQLITE_DONE) {
397 result = CERTSVC_SUCCESS;
401 if (records != SQLITE_ROW) {
402 SLOGE("DB query error when select. result[%d].", records);
403 result = CERTSVC_FAIL;
410 if (storeType == SYSTEM_STORE) {
411 text = (const char *)sqlite3_column_text(stmt, 0);
413 cert = strndup(text, strlen(text));
415 text = (const char *)sqlite3_column_text(stmt, 0);
417 gname = strndup(text, strlen(text));
419 result = get_certificate_buffer_from_store(storeType, gname, &cert);
420 if (result != CERTSVC_SUCCESS) {
421 SLOGE("Failed to get certificate buffer from key-manager. gname[%s]", gname);
427 SLOGE("Failed to extract cert buffer to update ca-certificate.");
428 result = CERTSVC_FAIL;
433 result = write_to_ca_cert_crt_file("wb", cert);
435 result = write_to_ca_cert_crt_file("ab", cert);
437 if (result != CERTSVC_SUCCESS) {
438 SLOGE("Failed to write to file.");
439 result = CERTSVC_FAIL;
445 SLOGD("Successfully updated ca-certificate.crt file. added cert num[%d]", counter);
449 sqlite3_finalize(stmt);
454 int enable_disable_cert_status(
455 CertStoreType storeType,
460 int result = CERTSVC_SUCCESS;
464 const char *text = NULL;
465 sqlite3_stmt *stmt = NULL;
467 if (status != DISABLED && status != ENABLED) {
468 SLOGE("Invalid cert status");
469 return CERTSVC_INVALID_STATUS;
472 query = sqlite3_mprintf("select * from %Q where gname=%Q", storetype_to_string(storeType), gname);
474 SLOGE("Failed to generate query");
475 return CERTSVC_BAD_ALLOC;
478 result = execute_select_query(query, &stmt);
481 if (result != CERTSVC_SUCCESS || !stmt) {
482 SLOGE("Querying database failed.");
486 records = sqlite3_step(stmt);
487 sqlite3_finalize(stmt);
490 if (records != SQLITE_ROW) {
491 SLOGE("No valid records found.");
495 if (status == DISABLED) {
496 /* check certificate presence in disabled_certs table before inserting */
497 query = sqlite3_mprintf("select * from disabled_certs where gname=%Q", gname);
499 SLOGE("Failed to generate query");
500 return CERTSVC_BAD_ALLOC;
503 result = execute_select_query(query, &stmt);
507 if (result != CERTSVC_SUCCESS) {
508 SLOGE("Querying database failed.");
512 records = sqlite3_step(stmt);
513 sqlite3_finalize(stmt);
516 if (records == SQLITE_ROW) {
517 SLOGE("Selected certificate identifier is already disabled.", gname);
521 /* get certificate from keymanager*/
522 result = get_certificate_buffer_from_store(storeType, gname, &cert);
523 if (result != CERTSVC_SUCCESS) {
524 SLOGE("Failed to get certificate buffer. result[%d]", result);
528 /* inserting the disabled certificate to disabled_certs table */
529 query = sqlite3_mprintf("insert into disabled_certs (gname, certificate) values (%Q, %Q)", gname, cert);
533 SLOGE("Failed to generate query");
534 return CERTSVC_BAD_ALLOC;
537 result = execute_insert_update_query(query);
540 if (result != CERTSVC_SUCCESS) {
541 SLOGE("Insert to database failed.");
545 if (storeType != SYSTEM_STORE) {
546 result = ckmc_remove_alias_with_shared_owner_prefix(gname);
548 if (result != CKMC_ERROR_NONE) {
549 SLOGE("Failed to delete certificate from key-manager. ckmc_result[%d]", result);
554 result = del_file_from_system_cert_dir(gname);
555 if (result != CERTSVC_SUCCESS) {
556 SLOGE("Error in del_file_from_system_cert_dir. ret[%d]", result);
560 } else { /* moving the certificate to enabled state */
561 query = sqlite3_mprintf("select certificate from disabled_certs where gname=%Q", gname);
563 SLOGE("Failed to generate query");
564 return CERTSVC_BAD_ALLOC;
567 result = execute_select_query(query, &stmt);
570 if (result != CERTSVC_SUCCESS) {
571 SLOGE("Querying database failed.");
575 records = sqlite3_step(stmt);
576 if (records == SQLITE_ROW) {
577 text = (const char *)sqlite3_column_text(stmt, 0);
580 SLOGE("Invalid column text");
581 sqlite3_finalize(stmt);
585 cert = strndup(text, strlen(text));
587 sqlite3_finalize(stmt);
590 SLOGE("Failed to allocate memory");
591 return CERTSVC_BAD_ALLOC;
594 if (storeType == SYSTEM_STORE)
595 result = saveCertificateToSystemStore(gname);
597 result = saveCertificateToStore(gname, cert);
601 if (result != CERTSVC_SUCCESS) {
602 SLOGE("Failed to save certificate to key-manager. ret[%d]", result);
606 query = sqlite3_mprintf("delete from disabled_certs where gname=%Q", gname);
608 SLOGE("Failed to generate query");
609 return CERTSVC_BAD_ALLOC;
612 result = execute_insert_update_query(query);
615 if (result != CERTSVC_SUCCESS) {
616 SLOGE("Unable to delete certificate entry from database. ret[%d]", result);
622 if (is_root_app == ENABLED)
623 query = sqlite3_mprintf("update %Q set is_root_app_enabled=%d , enabled=%d where gname=%Q",
624 storetype_to_string(storeType), CertStatus_to_int(status), status, gname);
626 query = sqlite3_mprintf("update %Q set enabled=%d where gname=%Q",
627 storetype_to_string(storeType), CertStatus_to_int(status), gname);
630 SLOGE("Failed to generate query");
631 return CERTSVC_BAD_ALLOC;
634 result = execute_insert_update_query(query);
637 if (result != CERTSVC_SUCCESS) {
638 SLOGE("Update failed. ret[%d]", result);
645 int setCertificateStatusToStore(
646 CertStoreType storeType,
652 SLOGE("Invalid input parameter passed.");
653 return CERTSVC_WRONG_ARGUMENT;
656 int result = enable_disable_cert_status(storeType, is_root_app, gname, status);
657 if (result != CERTSVC_SUCCESS) {
658 SLOGE("Failed to disable certificate.");
662 SLOGD("Successfully updated the certificate status from %s to %s.",
663 (status == DISABLED) ? "ENABLED" : "DISABLED", (status == DISABLED) ? "DISABLED" : "ENABLED");
664 return CERTSVC_SUCCESS;
667 int getCertificateStatusFromStore(
668 CertStoreType storeType,
673 SLOGE("Invalid input parameter passed.");
674 return CERTSVC_WRONG_ARGUMENT;
677 char *query = sqlite3_mprintf("select gname, common_name, enabled from %Q where gname=%Q",
678 storetype_to_string(storeType), gname);
680 SLOGE("Failed to generate query");
681 return CERTSVC_BAD_ALLOC;
684 sqlite3_stmt *stmt = NULL;
685 int result = execute_select_query(query, &stmt);
688 if (result != CERTSVC_SUCCESS || !stmt) {
689 SLOGE("Querying database failed.");
694 result = sqlite3_step(stmt);
695 if (result != SQLITE_ROW || result == SQLITE_DONE) {
696 SLOGE("No valid records found.");
698 sqlite3_finalize(stmt);
702 *status = int_to_CertStatus(sqlite3_column_int(stmt, 2));
704 sqlite3_finalize(stmt);
706 return CERTSVC_SUCCESS;
709 int check_alias_exist_in_database(
710 CertStoreType storeTypes,
715 sqlite3_stmt *stmt = NULL;
716 int result = CERTSVC_SUCCESS;
717 CertStoreType storeType;
720 if (!alias || !punique) {
721 SLOGE("Invalid input parameter passed.");
722 return CERTSVC_WRONG_ARGUMENT;
725 for (storeType = VPN_STORE; storeType < SYSTEM_STORE; storeType = nextStore(storeType)) {
726 if (!hasStore(storeTypes, storeType))
729 query = sqlite3_mprintf("select * from %Q where common_name=%Q",
730 storetype_to_string(storeType), alias);
733 SLOGE("Failed to generate query");
734 return CERTSVC_BAD_ALLOC;
737 result = execute_select_query(query, &stmt);
742 if (result != CERTSVC_SUCCESS || !stmt) {
743 SLOGE("Querying database failed. result[%d]", result);
747 result = sqlite3_step(stmt);
749 sqlite3_finalize(stmt);
752 if (result == SQLITE_DONE) {
758 *punique = unique ? CERTSVC_TRUE : CERTSVC_FALSE;
760 return CERTSVC_SUCCESS;
763 int installCertificateToStore(
764 CertStoreType storeType,
766 const char *common_name,
767 const char *private_key_gname,
768 const char *associated_gname,
769 const char *dataBlock,
773 || (certType == P12_END_USER && !common_name && !private_key_gname)
774 || (certType != P12_END_USER && !common_name && !associated_gname)) {
775 SLOGE("Invalid input parameter passed.");
776 return CERTSVC_WRONG_ARGUMENT;
779 int result = CERTSVC_SUCCESS;
781 if (storeType != SYSTEM_STORE) {
782 result = saveCertificateToStore(gname, dataBlock);
783 if (result != CERTSVC_SUCCESS) {
784 SLOGE("FAIL to save certificate to key-manager. result[%d]", result);
789 if (certType == P12_PKEY) {
790 SLOGD("Don't save private key in store");
791 return CERTSVC_SUCCESS;
795 if (certType == P12_END_USER && private_key_gname) {
796 query = sqlite3_mprintf("insert into %Q (gname, common_name, private_key_gname, associated_gname, enabled, is_root_app_enabled) "\
797 "values (%Q, %Q, %Q, %Q, %d, %d)", storetype_to_string(storeType), gname, common_name, private_key_gname,
798 gname, ENABLED, ENABLED);
799 } else if (certType == PEM_CRT || certType == P12_TRUSTED) {
800 query = sqlite3_mprintf("insert into %Q (gname, common_name, is_root_cert, associated_gname, enabled, is_root_app_enabled) values "\
801 "(%Q, %Q, %d, %Q, %d, %d)", storetype_to_string(storeType), gname, common_name, ENABLED,
802 associated_gname, ENABLED, ENABLED);
803 } else if (certType == P12_INTERMEDIATE) {
804 query = sqlite3_mprintf("insert into %Q (gname, common_name, associated_gname, enabled, is_root_app_enabled) values (%Q, %Q, %Q, %d, %d)",
805 storetype_to_string(storeType), gname, common_name, associated_gname, ENABLED, ENABLED);
809 SLOGE("Failed to generate query");
810 return CERTSVC_BAD_ALLOC;
813 result = execute_insert_update_query(query);
816 if (result != CERTSVC_SUCCESS) {
817 SLOGE("Insert to database failed.");
821 return CERTSVC_SUCCESS;
824 int checkAliasExistsInStore(CertStoreType storeType, const char *alias, int *punique)
827 SLOGE("Invalid input parameter passed.");
828 return CERTSVC_WRONG_ARGUMENT;
831 *punique = CERTSVC_FAIL;
832 int result = check_alias_exist_in_database(storeType, alias, punique);
833 if (result != CERTSVC_SUCCESS) {
834 SLOGE("Failed to check_alias_exist_in_database. err[%d]", result);
838 if (*punique == CERTSVC_TRUE)
839 SLOGD("Alias (%s) does not exist in store(%d).", alias, storeType);
841 SLOGD("Alias (%s) exist in store(%d).", alias, storeType);
843 return CERTSVC_SUCCESS;
846 int getCertificateDetailFromStore(
847 CertStoreType storeType,
852 int result = CERTSVC_SUCCESS;
855 const char *text = NULL;
856 sqlite3_stmt *stmt = NULL;
857 ckmc_raw_buffer_s *cert_data = NULL;
859 if (!gname || !pOutData) {
860 SLOGE("Invalid input parameter passed.");
861 return CERTSVC_WRONG_ARGUMENT;
864 /* start constructing query */
865 if (certType == P12_PKEY) {
866 /* From the given certificate identifier, get the associated_gname for the certificate.
867 * Then query the database for records matching the associated_gname to get the private key */
868 query = sqlite3_mprintf("select associated_gname from %Q where gname=%Q",
869 storetype_to_string(storeType), gname);
871 SLOGE("Failed to generate query");
872 return CERTSVC_BAD_ALLOC;
875 result = execute_select_query(query, &stmt);
878 if (result != CERTSVC_SUCCESS) {
879 SLOGE("Querying database failed.");
883 records = sqlite3_step(stmt);
884 if (records != SQLITE_ROW) {
885 SLOGE("No valid records found.");
886 sqlite3_finalize(stmt);
890 text = (const char *)sqlite3_column_text(stmt, 0);
893 SLOGE("No valid column text");
894 sqlite3_finalize(stmt);
898 query = sqlite3_mprintf("select private_key_gname from %Q where gname=%Q and enabled=%d and is_root_app_enabled=%d",
899 storetype_to_string(storeType), text, ENABLED, ENABLED);
901 sqlite3_finalize(stmt);
902 } else if (storeType != SYSTEM_STORE) {
903 query = sqlite3_mprintf("select * from %Q where gname=%Q and enabled=%d and is_root_app_enabled=%d",
904 storetype_to_string(storeType), gname, ENABLED, ENABLED);
908 SLOGE("Failed to generate query");
909 return CERTSVC_BAD_ALLOC;
912 result = execute_select_query(query, &stmt);
915 if (result != CERTSVC_SUCCESS) {
916 SLOGE("Querying database failed.");
920 records = sqlite3_step(stmt);
921 if (records != SQLITE_ROW) {
922 SLOGE("No valid records found.");
923 sqlite3_finalize(stmt);
927 if (certType == P12_PKEY) {
928 if (!(text = (const char *)sqlite3_column_text(stmt, 0))) {
929 SLOGE("No valid column text");
930 sqlite3_finalize(stmt);
937 char *ckm_alias = add_shared_owner_prefix(gname);
939 SLOGE("Failed to make alias. memory allocation error.");
940 return CERTSVC_BAD_ALLOC;
943 result = ckmc_get_data(ckm_alias, NULL, &cert_data);
946 sqlite3_finalize(stmt);
948 if (result != CKMC_ERROR_NONE) {
949 SLOGE("Failed to get certificate from key-manager. ckm ret[%d]", result);
953 memcpy(pOutData, cert_data->data, cert_data->size);
954 pOutData[cert_data->size] = 0;
956 ckmc_buffer_free(cert_data);
958 return CERTSVC_SUCCESS;
961 int getCertificateDetailFromSystemStore(const char *gname, char *pOutData)
963 int result = CERTSVC_SUCCESS;
966 const char *text = NULL;
967 sqlite3_stmt *stmt = NULL;
970 SLOGE("Invalid input parameter passed.");
971 return CERTSVC_WRONG_ARGUMENT;
974 query = sqlite3_mprintf("select certificate from ssl where gname=%Q and is_root_app_enabled=%d",
975 gname, ENABLED, ENABLED);
977 SLOGE("Query is NULL.");
981 result = execute_select_query(query, &stmt);
984 if (result != CERTSVC_SUCCESS) {
985 SLOGE("Querying database failed.");
989 records = sqlite3_step(stmt);
990 if (records != SQLITE_ROW) {
991 SLOGE("No valid records found for passed gname [%s].", gname);
992 sqlite3_finalize(stmt);
996 text = (const char *)sqlite3_column_text(stmt, 0);
999 SLOGE("Fail to sqlite3_column_text");
1000 sqlite3_finalize(stmt);
1001 return CERTSVC_FAIL;
1004 size_t cert_len = strlen(text);
1005 if (cert_len >= 4096) {
1006 sqlite3_finalize(stmt);
1007 SLOGE("certificate is too long");
1008 return CERTSVC_FAIL;
1011 memcpy(pOutData, text, cert_len);
1012 pOutData[cert_len] = '\0';
1014 sqlite3_finalize(stmt);
1016 return CERTSVC_SUCCESS;
1019 int deleteCertificateFromStore(CertStoreType storeType, const char *gname)
1021 int result = CERTSVC_SUCCESS;
1024 char *private_key_name = NULL;
1025 sqlite3_stmt *stmt = NULL;
1027 SLOGD("Remove certificate of gname[%s] in store[%d]", gname, storeType);
1030 SLOGE("Invalid input parameter passed.");
1031 return CERTSVC_WRONG_ARGUMENT;
1034 if (storeType == SYSTEM_STORE) {
1035 SLOGE("Invalid store type passed.");
1036 return CERTSVC_INVALID_STORE_TYPE;
1039 /* start constructing query */
1040 query = sqlite3_mprintf("select private_key_gname from %Q where gname=%Q",
1041 storetype_to_string(storeType), gname);
1043 result = execute_select_query(query, &stmt);
1044 if (result != CERTSVC_SUCCESS) {
1045 SLOGE("Querying database failed.");
1046 result = CERTSVC_FAIL;
1050 records = sqlite3_step(stmt);
1051 if (records != SQLITE_ROW) {
1052 SLOGE("No valid records found for passed gname [%s]. result[%d].", gname, records);
1053 result = CERTSVC_FAIL;
1057 /* if a cert is having private-key in it, the private key should
1058 * be deleted first from key-manager, then the actual cert */
1059 if (sqlite3_column_text(stmt, 0) != NULL)
1060 private_key_name = strdup((const char *)sqlite3_column_text(stmt, 0));
1062 query = sqlite3_mprintf("delete from disabled_certs where gname=%Q", gname);
1063 result = execute_insert_update_query(query);
1064 if (result != CERTSVC_SUCCESS) {
1065 SLOGE("Unable to delete certificate entry from database. result[%d]", result);
1070 sqlite3_free(query);
1075 sqlite3_finalize(stmt);
1079 query = sqlite3_mprintf("delete from %Q where gname=%Q",
1080 storetype_to_string(storeType), gname);
1082 result = execute_insert_update_query(query);
1083 if (result != CERTSVC_SUCCESS) {
1084 SLOGE("Unable to delete certificate entry from database. result[%d]", result);
1089 sqlite3_free(query);
1094 sqlite3_finalize(stmt);
1098 CertStoreType other = ALL_STORE & ~SYSTEM_STORE & ~storeType;
1099 CertStoreType current;
1100 int gname_exist = 0;
1101 for (current = VPN_STORE; current < SYSTEM_STORE; current = nextStore(current)) {
1102 if (!hasStore(other, current))
1105 query = sqlite3_mprintf("select * from %Q where gname=%Q",
1106 storetype_to_string(current), gname);
1107 result = execute_select_query(query, &stmt);
1108 if (result != CERTSVC_SUCCESS) {
1109 SLOGE("Querying database failed.");
1110 result = CERTSVC_FAIL;
1113 records = sqlite3_step(stmt);
1114 if (records == SQLITE_ROW) {
1115 SLOGI("Same gname[%s] exist on store[%d].", gname, current);
1120 sqlite3_free(query);
1121 sqlite3_finalize(stmt);
1127 SLOGD("The gname[%s] which is in store[%d] is the last one. so remove it from ckm either.", gname, current);
1129 if (private_key_name != NULL) {
1130 result = ckmc_remove_alias_with_shared_owner_prefix(private_key_name);
1131 if (result != CKMC_ERROR_NONE) {
1132 SLOGE("Failed to delete certificate from key-manager. ckmc_result[%d]", result);
1133 result = CERTSVC_FAIL;
1138 /* removing the actual cert */
1139 result = ckmc_remove_alias_with_shared_owner_prefix(gname);
1140 if (result != CKMC_ERROR_NONE) {
1141 SLOGE("Failed to remove data in ckm with gname[%s]. ckm_result[%d]", gname, result);
1142 result = CERTSVC_FAIL;
1147 SLOGD("Success in deleting the certificate from store.");
1148 result = CERTSVC_SUCCESS;
1152 sqlite3_free(query);
1155 sqlite3_finalize(stmt);
1157 free(private_key_name);
1162 static int makeCertListNode(
1163 CertStoreType storeType,
1167 CertSvcStoreCertList **out)
1169 CertSvcStoreCertList *node = NULL;
1170 int result = CERTSVC_SUCCESS;
1171 size_t gname_len = 0;
1172 size_t title_len = 0;
1174 if (out == NULL || gname == NULL || title == NULL) {
1175 SLOGE("Failed to read texts from records");
1176 return CERTSVC_WRONG_ARGUMENT;
1179 node = (CertSvcStoreCertList *)malloc(sizeof(CertSvcStoreCertList));
1181 SLOGE("Failed to allocate memory.");
1182 return CERTSVC_BAD_ALLOC;
1185 gname_len = strlen(gname);
1186 title_len = strlen(title);
1188 node->gname = (char *)malloc(sizeof(char) * (gname_len + 1));
1189 node->title = (char *)malloc(sizeof(char) * (title_len + 1));
1190 if (node->title == NULL || node->gname == NULL) {
1191 SLOGE("Failed to allocate memory");
1192 result = CERTSVC_BAD_ALLOC;
1196 memcpy(node->gname, gname, gname_len);
1197 memcpy(node->title, title, title_len);
1198 node->gname[gname_len] = '\0';
1199 node->title[title_len] = '\0';
1201 node->storeType = storeType;
1202 node->status = int_to_CertStatus(statusInt);
1207 return CERTSVC_SUCCESS;
1219 int getCertificateListFromStore(
1221 CertStoreType storeTypes,
1223 char **certListBuffer,
1227 int result = CERTSVC_SUCCESS;
1228 CertSvcStoreCertList *rootCertHead = NULL;
1229 CertSvcStoreCertList *tmpNode = NULL;
1230 CertSvcStoreCertList *currentNode = NULL;
1231 sqlite3_stmt *stmt = NULL;
1237 CertStoreType storeType;
1238 for (storeType = VPN_STORE; storeType != NONE_STORE; storeType = nextStore(storeType)) {
1239 if (!hasStore(storeTypes, storeType))
1242 SLOGD("Processing storetype [%s]", storetype_to_string(storeType));
1244 if (reqType == CERTSVC_GET_ROOT_CERTIFICATE_LIST) {
1245 if (storeType == SYSTEM_STORE) {
1246 query = sqlite3_mprintf("select gname, common_name, enabled from %Q where enabled=%d "\
1247 "and is_root_app_enabled=%d and order by common_name asc", "ssl", ENABLED, ENABLED);
1249 query = sqlite3_mprintf("select gname, common_name, enabled from %Q where "\
1250 "is_root_cert IS NOT NULL and is_root_app_enabled=%d and enabled=%d",
1251 storetype_to_string(storeType), ENABLED, ENABLED);
1253 } else if (reqType == CERTSVC_GET_USER_CERTIFICATE_LIST) {
1254 if (storeType == SYSTEM_STORE) {
1255 SLOGE("Invalid store type passed.");
1256 return CERTSVC_WRONG_ARGUMENT;
1258 query = sqlite3_mprintf("select gname, common_name, enabled from %Q where "\
1259 "private_key_gname IS NOT NULL and is_root_app_enabled=%d and enabled=%d",
1260 storetype_to_string(storeType), ENABLED, ENABLED);
1263 if (is_root_app != ENABLED) {
1264 /* Gets only the list of certificates where is_root_app = 1 (which are enabled by the master application) */
1265 if (storeType == SYSTEM_STORE) {
1266 query = sqlite3_mprintf("select gname, common_name, enabled from %Q where "\
1267 "is_root_app_enabled=%d order by common_name asc",
1268 storetype_to_string(storeType), ENABLED, ENABLED);
1270 query = sqlite3_mprintf("select gname, common_name, enabled from %Q where is_root_app_enabled=%d",
1271 storetype_to_string(storeType), ENABLED, ENABLED);
1274 /* Gets all the certificates from store without any restrictions */
1275 if (storeType == SYSTEM_STORE) {
1276 query = sqlite3_mprintf("select gname, common_name, enabled from %Q order by common_name asc",
1277 storetype_to_string(storeType), ENABLED);
1279 query = sqlite3_mprintf("select gname, common_name, enabled from %Q",
1280 storetype_to_string(storeType), ENABLED);
1285 result = execute_select_query(query, &stmt);
1286 if (result != CERTSVC_SUCCESS) {
1287 SLOGE("Querying database failed.");
1288 result = CERTSVC_FAIL;
1292 while ((records = sqlite3_step(stmt)) == SQLITE_ROW) {
1293 result = makeCertListNode(
1295 (const char *)sqlite3_column_text(stmt, 0),
1296 (const char *)sqlite3_column_text(stmt, 1),
1297 (int)sqlite3_column_int(stmt, 2),
1300 if (result != CERTSVC_SUCCESS) {
1301 SLOGE("Failed to make new cert list node. result[%d]", result);
1306 rootCertHead = tmpNode;
1308 currentNode->next = tmpNode;
1310 currentNode = tmpNode;
1315 if (records != SQLITE_DONE) {
1316 SLOGE("Error in getting data from sqlite3 statement. result[%d]", records);
1317 result = CERTSVC_FAIL;
1322 sqlite3_free(query);
1327 sqlite3_finalize(stmt);
1333 VcoreCertResponseData *respCertData = (VcoreCertResponseData *)malloc(count * sizeof(VcoreCertResponseData));
1334 if (!respCertData) {
1335 SLOGE("Failed to allocate memory");
1336 result = CERTSVC_BAD_ALLOC;
1340 memset(respCertData, 0x00, count * sizeof(VcoreCertResponseData));
1341 VcoreCertResponseData* currRespCertData = NULL;
1343 currentNode = rootCertHead;
1344 for (i = 0; i < count; i++) {
1345 tmpNode = currentNode->next;
1347 currRespCertData = respCertData + i;
1348 if (strlen(currentNode->gname) > sizeof(currRespCertData->gname)
1349 || strlen(currentNode->title) > sizeof(currRespCertData->title)) {
1350 SLOGE("String is too long. [%s], [%s]", currentNode->gname, currentNode->title);
1351 result = CERTSVC_FAIL;
1352 *certListBuffer = NULL;
1356 strncpy(currRespCertData->gname, currentNode->gname, strlen(currentNode->gname));
1357 strncpy(currRespCertData->title, currentNode->title, strlen(currentNode->title));
1358 currRespCertData->status = currentNode->status;
1359 currRespCertData->storeType = currentNode->storeType;
1361 currentNode = tmpNode;
1364 *certListBuffer = (char *) respCertData;
1365 *bufferLen = count * sizeof(VcoreCertResponseData);
1367 SLOGD("Success to create certificate list. cert_count=%d", count);
1368 result = CERTSVC_SUCCESS;
1372 sqlite3_free(query);
1375 sqlite3_finalize(stmt);
1378 currentNode = rootCertHead;
1379 while (currentNode) {
1380 tmpNode = currentNode->next;
1381 free(currentNode->title);
1382 free(currentNode->gname);
1384 currentNode = tmpNode;
1391 int getCertificateAliasFromStore(CertStoreType storeType, const char *gname, char *alias)
1393 int result = CERTSVC_SUCCESS;
1395 sqlite3_stmt *stmt = NULL;
1397 const char *text = NULL;
1399 query = sqlite3_mprintf("select common_name from %Q where gname=%Q",
1400 storetype_to_string(storeType), gname);
1402 result = execute_select_query(query, &stmt);
1403 if (result != CERTSVC_SUCCESS) {
1404 SLOGE("Querying database failed.");
1405 result = CERTSVC_FAIL;
1409 records = sqlite3_step(stmt);
1410 if (records != SQLITE_ROW || records == SQLITE_DONE) {
1411 SLOGE("No valid records found for gname passed [%s].",gname);
1412 result = CERTSVC_FAIL;
1416 if (!(text = (const char *)sqlite3_column_text(stmt, 0))) {
1417 SLOGE("No column text in returned records");
1418 result = CERTSVC_FAIL;
1422 strncpy(alias, text, strlen(text));
1424 if (strlen(alias) == 0) {
1425 SLOGE("Unable to get the alias name for the gname passed.");
1426 result = CERTSVC_FAIL;
1430 result = CERTSVC_SUCCESS;
1432 SLOGD("success : getCertificateAliasFromStore");
1435 sqlite3_free(query);
1438 sqlite3_finalize(stmt);
1443 int loadCertificatesFromStore(
1444 CertStoreType storeType,
1446 char **ppCertBlockBuffer,
1448 size_t *certBlockCount)
1450 int result = CERTSVC_SUCCESS;
1453 sqlite3_stmt *stmt = NULL;
1455 char **certs = NULL;
1456 const char *tmpText = NULL;
1459 query = sqlite3_mprintf("select associated_gname from %Q where gname=%Q",
1460 storetype_to_string(storeType), gname);
1462 result = execute_select_query(query, &stmt);
1463 if (result != CERTSVC_SUCCESS) {
1464 SLOGE("Querying database failed.");
1465 result = CERTSVC_FAIL;
1469 records = sqlite3_step(stmt);
1470 if (records != SQLITE_ROW || records == SQLITE_DONE) {
1471 SLOGE("No valid records found for gname passed [%s].",gname);
1472 result = CERTSVC_FAIL;
1477 if (records == SQLITE_ROW) {
1479 sqlite3_free(query);
1481 const char *columnText = (const char *)sqlite3_column_text(stmt, 0);
1483 SLOGE("Failed to sqlite3_column_text");
1484 result = CERTSVC_FAIL;
1488 query = sqlite3_mprintf("select gname from %Q where associated_gname=%Q and enabled=%d and is_root_app_enabled=%d",
1489 storetype_to_string(storeType), columnText, ENABLED, ENABLED);
1492 sqlite3_finalize(stmt);
1494 result = execute_select_query(query, &stmt);
1495 if (result != CERTSVC_SUCCESS) {
1496 SLOGE("Querying database failed.");
1497 result = CERTSVC_FAIL;
1502 records = sqlite3_step(stmt);
1503 if (records != SQLITE_ROW || records == SQLITE_DONE)
1507 certs = (char**) malloc(4 * sizeof(char *));
1509 SLOGE("Failed to allocate memory");
1510 result = CERTSVC_BAD_ALLOC;
1513 memset(certs, 0x00, 4 * sizeof(char *));
1516 if (records == SQLITE_ROW) {
1517 tmpText = (const char *)sqlite3_column_text(stmt, 0);
1519 SLOGE("Failed to sqlite3_column_text.");
1520 result = CERTSVC_FAIL;
1524 if (!((certs)[count] = strdup(tmpText))) {
1525 SLOGE("Failed to allocate memory");
1526 result = CERTSVC_BAD_ALLOC;
1535 SLOGE("No valid records found for the gname passed [%s].",gname);
1536 return CERTSVC_FAIL;
1540 *certBlockCount = count;
1541 *bufferLen = count * sizeof(ResponseCertBlock);
1542 ResponseCertBlock *certBlockList = (ResponseCertBlock *) malloc(*bufferLen);
1543 if (!certBlockList) {
1544 SLOGE("Failed to allocate memory for ResponseCertBlock");
1545 result = CERTSVC_BAD_ALLOC;
1550 memset(certBlockList, 0x00, *bufferLen);
1552 ResponseCertBlock *currentBlock = NULL;
1553 for (i = 0; i < count; i++) {
1554 currentBlock = certBlockList + i;
1555 if (sizeof(currentBlock->dataBlock) < strlen(certs[i])) {
1556 SLOGE("src is longer than dst. src[%s] dst size[%d]", certs[i], sizeof(currentBlock->dataBlock));
1557 free(certBlockList);
1558 result = CERTSVC_FAIL;
1561 strncpy(currentBlock->dataBlock, certs[i], strlen(certs[i]));
1562 currentBlock->dataBlockLen = strlen(certs[i]);
1564 *ppCertBlockBuffer = (char *)certBlockList;
1566 result = CERTSVC_SUCCESS;
1568 SLOGD("success: loadCertificatesFromStore. CERT_COUNT=%d", count);
1572 sqlite3_free(query);
1575 sqlite3_finalize(stmt);
1578 for(i = 0; i < count; i++)