source code open - cert-svc-ui
[framework/security/cert-svc-ui.git] / view / src / certificates / pfx_cert / pfx_cert_install.c
1 /**
2  * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *    http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /*
17  * @file        pfx_cert_install.c
18  * @author      Janusz Kozerski (j.kozerski@samsung.com)
19  * @version     1.0
20  * @brief
21  */
22 #define _GNU_SOURCE
23
24 #include <dirent.h>
25
26 #include <cert-svc/cpkcs12.h>
27
28 #include "certificates/certificate_util.h"
29 #include "certificates/certificates.h"
30
31 static const char * const dir_path = PATH_SDCARD;
32
33 static void install_pfx_button_cb(void *data, Evas_Object *obj, void *event_info);
34
35 static struct ListElement *firstListElement = NULL;
36 static struct ListElement *lastListElement = NULL;
37
38 void pfx_cert_install_cb(void *data, Evas_Object *obj, void *event_info) {
39     LOGD("pfx_cert_cb");
40
41     struct ug_data *ad = (struct ug_data *) data;
42     Evas_Object *list = NULL;
43
44     DIR *dir;
45     struct dirent *dp;
46
47     firstListElement = initList();
48     lastListElement = firstListElement;
49
50     list = elm_list_add(ad->win_main);
51     elm_list_mode_set(list, ELM_LIST_COMPRESS);
52
53
54
55     dir = opendir(dir_path);
56     if (dir == NULL) {
57         LOGE("There's no such directory: %s", dir_path);
58         return; //TODO What if there's no SD card?
59     }
60
61     LOGD("Scanning dir (%s) - looking for certs", dir_path);
62     while ((dp = readdir(dir)) != NULL) {
63         char *tmp;
64         tmp = path_cat(dir_path, dp->d_name);
65         char *dot = strrchr(dp->d_name, '.');
66
67         if(dot != NULL && strlen(dot)>3 && (strncmp(dot, ".pfx", 4) == 0 || strncmp(dot, ".PFX", 4) == 0 ||
68                              strncmp(dot, ".p12", 4) == 0 || strncmp(dot, ".P12", 4) == 0)) {
69             if (!(dp->d_type == DT_DIR)) {
70                 Elm_Object_Item * it;
71                 struct ListElement *current;
72                 current = addListElement(lastListElement, dp->d_name);
73                 lastListElement = current;
74                 it = elm_list_item_append(list, dp->d_name, NULL, NULL, install_pfx_button_cb, current);
75                 LOGD("elm list append = %s", current->name);
76             }
77             if (tmp) {
78                 free(tmp);
79                 tmp = NULL;
80             }
81         }
82     }
83     closedir(dir);
84
85     elm_naviframe_item_push(ad->navi_bar, dgettext(PACKAGE, "CHOOSE_PFX_TO_INSTALL"), NULL, NULL, list, NULL);
86 }
87
88 static void install_pfx_button_cb(void *data, Evas_Object *obj, void *event_info) {
89
90     struct ListElement *current = (struct ListElement *) data;
91     struct ug_data *ad = get_ug_data();
92     Elm_Object_Item *it = (Elm_Object_Item *) elm_list_selected_item_get(obj);
93     if (it){
94         elm_list_item_selected_set(it, EINA_FALSE);
95     }
96
97     char *path;
98     CertSvcString certSvcString_path;
99     int result;
100     result = asprintf(&path, "%s/%s", dir_path, current->name);
101     if(result == -1){
102         LOGD("aspfintf failed (-1)");
103         return;
104     }
105     certsvc_string_new(ad->instance, path, strlen(path), &certSvcString_path);
106
107     int returned_value;
108     if(certsvc_pkcs12_has_password(ad->instance, certSvcString_path, &returned_value)
109             != CERTSVC_SUCCESS){
110         LOGD("Wrong PKCS12 or PFX file.");
111         elm_naviframe_item_pop(ad->navi_bar);
112         return;
113     }
114
115     switch (returned_value){
116     case CERTSVC_TRUE:
117         LOGD("%s is passwod protected", current->name);
118         put_pkcs12_name_and_pass_cb(current, NULL, NULL);
119         return;
120
121     case CERTSVC_FALSE:
122         LOGD("%s is NOT passwod protected", current->name);
123         put_pkcs12_name_cb(current, NULL, NULL);
124         return;
125     }
126 }