SVACE: HEAP_LEAK, correct size in strncpy
[platform/core/security/tef-simulator.git] / ssflib / dep / swdss / source / ss_api.cpp
1 /**
2  * Copyright (c) 2013-2017 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
18  #include "ss_api.h"
19 #include "secure_file.h"
20 #include "slog.h"
21
22 #ifdef  __cplusplus
23 extern "C" {
24 #endif
25
26 int ss_init(int strategy) {
27         return 0;
28 }
29
30 int ss_set_credential(ss_credential_s * cred, const char* uuid,
31     const char* module_name, unsigned long major_ver, unsigned long minor_ver) {
32         if ((NULL == cred) || (NULL == uuid) || (NULL == module_name)) {
33                 SLOGE("[%s] input param error.\n", __FUNCTION__);
34                 return SS_RET_INVALID_PARAM;
35         }
36
37         int uuid_size = strnlen(uuid, SS_MAX_UUID_LEN) + 1;
38         int mn_size = strnlen(module_name, SS_MAX_MODULE_NAME_LEN) + 1;
39
40         if (uuid_size > SS_MAX_UUID_LEN || mn_size > SS_MAX_MODULE_NAME_LEN) {
41                 SLOGE("[%s] length of uuid or module name error.\n", __FUNCTION__);
42                 return SS_RET_INVALID_PARAM;
43         }
44
45         strncpy(cred->uuid, uuid, uuid_size);
46         strncpy(cred->module_name, module_name, mn_size);
47
48         cred->version.major = major_ver;
49         cred->version.minor = minor_ver;
50
51         return SS_RET_SUCCESS;
52 }
53
54 int ss_write(unsigned char* buffer, unsigned int data_size, unsigned int offset,
55     const char* data_name, const ss_credential_s * cred, unsigned int options) {
56         secure_file sec_file;
57         int ret = sec_file.initialize(cred, data_name, options);
58         if (SS_RET_SUCCESS != ret) {
59                 return ret;
60         }
61
62         return sec_file.write(buffer, data_size, offset);
63 }
64
65 int ss_read(unsigned char** ret_buf, unsigned int* data_size,
66     unsigned int offset, const char* data_name, const ss_credential_s * cred,
67     unsigned int options) {
68         secure_file sec_file;
69         int ret = sec_file.initialize(cred, data_name, options);
70         if (SS_RET_SUCCESS != ret) {
71                 return ret;
72         }
73
74         return sec_file.read(ret_buf, data_size, offset);
75 }
76
77 int ss_write_ex(unsigned char* buffer, unsigned int data_size,
78     unsigned int offset, const char* data_name, const ss_credential_s* cred,
79     unsigned int options, const unsigned char* rsa_n_data,
80     unsigned long rsa_n_len, const unsigned char* rsa_e_data,
81     unsigned long rsa_e_len) {
82         secure_file sec_file;
83         int ret = sec_file.initialize(cred, data_name, options);
84         if (SS_RET_SUCCESS != ret) {
85                 return ret;
86         }
87
88         return sec_file.write_ex(buffer, data_size, offset, rsa_n_data, rsa_n_len,
89             rsa_e_data, rsa_e_len);
90 }
91
92 int ss_read_ex(unsigned char** ret_buf, unsigned int* data_size,
93     unsigned int offset, const char* data_name, const ss_credential_s* cred,
94     unsigned int options, const unsigned char* rsa_n_data,
95     unsigned long rsa_n_len, const unsigned char* rsa_d_data,
96     unsigned long rsa_d_len) {
97         secure_file sec_file;
98         int ret = sec_file.initialize(cred, data_name, options);
99         if (SS_RET_SUCCESS != ret) {
100                 return ret;
101         }
102
103         return sec_file.read_ex(ret_buf, data_size, offset, rsa_n_data, rsa_n_len,
104             rsa_d_data, rsa_d_len);
105 }
106
107 int ss_validate(const char* data_name, const ss_credential_s* cred,
108     unsigned int options) {
109         secure_file sec_file;
110         int ret = sec_file.initialize(cred, data_name, options);
111         if (SS_RET_SUCCESS != ret) {
112                 return ret;
113         }
114
115         return sec_file.validate();
116 }
117
118 int ss_delete(const char* data_name, const ss_credential_s* cred,
119     unsigned int options) {
120         secure_file sec_file;
121         int ret = sec_file.initialize(cred, data_name, options);
122         if (SS_RET_SUCCESS != ret) {
123                 return ret;
124         }
125
126         return sec_file.remove();
127 }
128
129 int ss_clear_storage(const ss_credential_s* cred, unsigned int options) {
130         secure_file sec_file;
131         int ret = sec_file.initialize(cred, NULL, options);
132         if (SS_RET_SUCCESS != ret) {
133                 return ret;
134         }
135
136         return sec_file.clear_storage();
137 }
138
139 int ss_free_buffer(unsigned char* buffer) {
140         if (NULL != buffer) {
141                 OsaFree(buffer);
142         }
143
144         return SS_RET_SUCCESS;
145 }
146 #ifdef  __cplusplus
147 }
148 #endif
149