diff options
author | HyungKyu Song <hk76.song@samsung.com> | 2013-02-16 00:16:10 +0900 |
---|---|---|
committer | HyungKyu Song <hk76.song@samsung.com> | 2013-02-16 00:16:10 +0900 |
commit | b99cf06f232ee59b688419debf2e588b9a459f5c (patch) | |
tree | ddb58e933c9da7c6e487b9df90589eeab0ee0957 /client/src/ss_client_intf.c | |
parent | 563412390a7fdd82b9ebb1f1162a43cecaf56c5f (diff) | |
download | secure-storage-b99cf06f232ee59b688419debf2e588b9a459f5c.tar.gz secure-storage-b99cf06f232ee59b688419debf2e588b9a459f5c.tar.bz2 secure-storage-b99cf06f232ee59b688419debf2e588b9a459f5c.zip |
Diffstat (limited to 'client/src/ss_client_intf.c')
-rw-r--r-- | client/src/ss_client_intf.c | 415 |
1 files changed, 415 insertions, 0 deletions
diff --git a/client/src/ss_client_intf.c b/client/src/ss_client_intf.c new file mode 100644 index 0000000..577f8b7 --- /dev/null +++ b/client/src/ss_client_intf.c @@ -0,0 +1,415 @@ +/* + * secure storage + * + * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Kidong Kim <kd0228.kim@samsung.com> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include "secure_storage.h" +#include "ss_client_intf.h" +#include "ss_client_ipc.h" +#include "ss_manager.h" + +int SsClientDataStoreFromFile(const char* filepath, ssm_flag flag, const char* group_id) +{ + ReqData_t* send_data = NULL; + RspData_t recv_data; + int temp_len = 0; + int cookie_size = 20; + +// cookie_size = security_server_get_cookie_size(); + char cookie_content[20] = {0, }; + +// if(security_server_request_cookie(cookie_content, cookie_size) < 0) // error while getting cookie +// { +// SLOGE("Fail to get cookie\n"); +// recv_data.rsp_type = SS_SECURE_STORAGE_ERROR; +// goto Error; +// } + + if(!filepath) + { + SLOGE("Parameter error in SsClientDataStoreFromFile..\n"); + recv_data.rsp_type = SS_PARAM_ERROR; + goto Error; + } + + send_data = (ReqData_t*)malloc(sizeof(ReqData_t)); + + if(!send_data) + { + SLOGE("Memory allocation fail in SsClientDataStoreFromFile..\n"); + recv_data.rsp_type = SS_MEMORY_ERROR; + goto Error; + } + + send_data->req_type = 1; // file store + send_data->enc_type = 1; // initial type + send_data->count = 0; + send_data->flag = flag; // flag + temp_len = strlen(filepath); + if(temp_len < MAX_FILENAME_LEN) + { + strncpy(send_data->data_infilepath, filepath, MAX_FILENAME_LEN - 1); + send_data->data_infilepath[temp_len] = '\0'; + } + else + { + SLOGE("filepath is too long.\n"); + recv_data.rsp_type = SS_PARAM_ERROR; + goto Free_and_Error; + } + memset(send_data->cookie, 0x00, MAX_COOKIE_LEN); + memset(send_data->group_id, 0x00, MAX_GROUP_ID_LEN); + memcpy(send_data->cookie, cookie_content, cookie_size); + if(group_id) + strncpy(send_data->group_id, group_id, MAX_GROUP_ID_LEN - 1); + else + strncpy(send_data->group_id, "NOTUSED", MAX_GROUP_ID_LEN - 1); + + memset(send_data->buffer, 0x00, MAX_SEND_DATA_LEN + 1); + recv_data = SsClientComm(send_data); + +Free_and_Error: + free(send_data); +Error: + return recv_data.rsp_type; +} + +int SsClientDataStoreFromBuffer(char* writebuffer, size_t bufLen, const char* filename, ssm_flag flag, const char* group_id) +{ + ReqData_t* send_data = NULL; + RspData_t recv_data; + int temp_len = 0; + int cookie_size = 20; + +// cookie_size = security_server_get_cookie_size(); + char cookie_content[20] = {0, }; + +// if(security_server_request_cookie(cookie_content, cookie_size) < 0) // error while getting cookie +// { +// SLOGE("Fail to get cookie\n"); +// recv_data.rsp_type = SS_SECURE_STORAGE_ERROR; +// goto Error; +// } + + if(!writebuffer || !filename) + { + SLOGE("Parameter error in SsClientDataStoreFromBuffer..\n"); + recv_data.rsp_type = SS_PARAM_ERROR; + goto Error; + } + + send_data = (ReqData_t*)malloc(sizeof(ReqData_t)); + if(!send_data) + { + SLOGE("Memory allocation fail in SsClientDataStoreFromBuffer..\n"); + recv_data.rsp_type = SS_MEMORY_ERROR; + goto Error; + } + + send_data->req_type = 2; // buffer store + send_data->enc_type = 1; + send_data->count = bufLen; + send_data->flag = flag; + temp_len = strlen(filename); + if(temp_len < MAX_FILENAME_LEN) + { + strncpy(send_data->data_infilepath, filename, MAX_FILENAME_LEN - 1); + send_data->data_infilepath[temp_len] = '\0'; + } + else + { + SLOGE("filepath is too long.\n"); + recv_data.rsp_type = SS_PARAM_ERROR; + goto Free_and_Error; + } + memset(send_data->cookie, 0x00, MAX_COOKIE_LEN); + memset(send_data->group_id, 0x00, MAX_GROUP_ID_LEN); + memcpy(send_data->cookie, cookie_content, cookie_size); + if(group_id) + strncpy(send_data->group_id, group_id, MAX_GROUP_ID_LEN - 1); + else + strncpy(send_data->group_id, "NOTUSED", MAX_GROUP_ID_LEN - 1); + + memcpy(send_data->buffer, writebuffer, bufLen); + recv_data = SsClientComm(send_data); + +Free_and_Error: + free(send_data); +Error: + return recv_data.rsp_type; +} + +int SsClientDataRead(const char* filepath, char* pRetBuf, size_t bufLen, size_t *readLen, ssm_flag flag, const char* group_id) +{ + unsigned int count = (unsigned int)(bufLen / MAX_RECV_DATA_LEN + 1); + unsigned int rest = (unsigned int)(bufLen % MAX_RECV_DATA_LEN); + char* buffer; + ReqData_t* send_data = NULL; + RspData_t recv_data; + int temp_len = 0; + int cookie_size = 20; + +// cookie_size = security_server_get_cookie_size(); + char cookie_content[20] = {0, }; + +// if(security_server_request_cookie(cookie_content, cookie_size) < 0) // error while getting cookie +// { +// SLOGE("Fail to get cookie\n"); +// recv_data.rsp_type = SS_SECURE_STORAGE_ERROR; +// goto Error; +// } + + if(!filepath) + { + SLOGE("filepath Parameter error in SsClientDataRead..\n"); + recv_data.rsp_type = SS_PARAM_ERROR; + goto Error; + } + if(!readLen) + { + SLOGE("readLen Parameter error in SsClientDataRead..\n"); + recv_data.rsp_type = SS_PARAM_ERROR; + goto Error; + } + + *readLen = 0; + buffer = pRetBuf; + + send_data = (ReqData_t*)malloc(sizeof(ReqData_t)); + + if(!send_data) + { + SLOGE("Memory allocation fail in SsClientDataRead..\n"); + recv_data.rsp_type = SS_MEMORY_ERROR; + goto Error; + } + + // fill send_data + send_data->req_type = 3; // read data from storage + send_data->enc_type = 1; // initial type + send_data->count = 0; + send_data->flag = flag & 0x000000ff; //flag; + temp_len = strlen(filepath); + if(temp_len < MAX_FILENAME_LEN) + { + strncpy(send_data->data_infilepath, filepath, MAX_FILENAME_LEN - 1); + send_data->data_infilepath[temp_len] = '\0'; + } + else + { + SLOGE("filepath is too long.\n"); + recv_data.rsp_type = SS_PARAM_ERROR; + goto Free_and_Error; + } + memset(send_data->cookie, 0x00, MAX_COOKIE_LEN); + memset(send_data->group_id, 0x00, MAX_GROUP_ID_LEN); + memcpy(send_data->cookie, cookie_content, MAX_COOKIE_LEN); + if(group_id) + strncpy(send_data->group_id, group_id, MAX_GROUP_ID_LEN - 1); + else + strncpy(send_data->group_id, "NOTUSED", MAX_GROUP_ID_LEN - 1); + memset(send_data->buffer, 0x00, MAX_SEND_DATA_LEN+1); + + // Call Server per 4KB data (count from 0 to ~) + for ( ; send_data->count < count; send_data->count++) + { + //receive data from server + recv_data = SsClientComm(send_data); + + // check response type + if(recv_data.rsp_type != 1) + { + SLOGE("data read error from server...\n"); + goto Free_and_Error; + } + // copy the last data (last count) + if(send_data->count == (count - 1)) + { + memcpy(buffer, recv_data.buffer, rest); + *readLen += (size_t)rest; + goto Last; + //break; + } + + memcpy(buffer, recv_data.buffer, MAX_RECV_DATA_LEN); + *readLen += (size_t)recv_data.readLen; + buffer += recv_data.readLen; + } +Last : + if(bufLen != *readLen) + { + SLOGE("Decrypted abnormally\n"); + recv_data.rsp_type = SS_DECRYPTION_ERROR; + goto Free_and_Error; + } + + SLOGE("Decrypted file name : %s\n", recv_data.data_filepath); +Free_and_Error: + free(send_data); +Error: + return recv_data.rsp_type; +} + +int SsClientGetInfo(const char* filepath, ssm_file_info_t* sfi, ssm_flag flag, const char* group_id) +{ + + ReqData_t* send_data = NULL; + RspData_t recv_data; + ssm_file_info_convert_t sfic; + int temp_len = 0; + int cookie_size = 20; + +// cookie_size = security_server_get_cookie_size(); + char cookie_content[20] = {0, }; + +// if(security_server_request_cookie(cookie_content, cookie_size) < 0) // error while getting cookie +// { +// SLOGE("Fail to get cookie\n"); +// recv_data.rsp_type = SS_SECURE_STORAGE_ERROR; +// goto Error; +// } + + if(!filepath || !sfi) + { + SLOGE("Parameter error in SsClientGetInfo..\n"); + recv_data.rsp_type = SS_PARAM_ERROR; + goto Error; + } + + send_data = (ReqData_t*)malloc(sizeof(ReqData_t)); + + if(!send_data) + { + SLOGE("Memory allocation fail in SsClientGetInfo..\n"); + recv_data.rsp_type = SS_MEMORY_ERROR; + goto Error; + } + + // fill send_data + send_data->req_type = 4; // get info type + send_data->enc_type = 1; // initial type + send_data->count = 0; + send_data->flag = flag & 0x000000ff; //flag; + temp_len = strlen(filepath); + if(temp_len < MAX_FILENAME_LEN) + { + strncpy(send_data->data_infilepath, filepath, MAX_FILENAME_LEN - 1); + send_data->data_infilepath[temp_len] = '\0'; + } + else + { + SLOGE("filepath is too long.\n"); + recv_data.rsp_type = SS_PARAM_ERROR; + goto Free_and_Error; + } + memset(send_data->cookie, 0x00, MAX_COOKIE_LEN); + memset(send_data->group_id, 0x00, MAX_GROUP_ID_LEN); + memcpy(send_data->cookie, cookie_content, cookie_size); + if(group_id) + strncpy(send_data->group_id, group_id, MAX_GROUP_ID_LEN - 1); + else + strncpy(send_data->group_id, "NOTUSED", MAX_GROUP_ID_LEN - 1); + memset(send_data->buffer, 0x00, MAX_SEND_DATA_LEN + 1); + + recv_data = SsClientComm(send_data); + + memcpy(sfic.fInfoArray, recv_data.buffer, sizeof(ssm_file_info_t)); + sfi->originSize = sfic.fInfoStruct.originSize; + sfi->storedSize = sfic.fInfoStruct.storedSize; + memcpy(sfi->reserved, sfic.fInfoStruct.reserved, 8); + +Free_and_Error: + free(send_data); +Error: + return recv_data.rsp_type; +} + +int SsClientDeleteFile(const char *pFilePath, ssm_flag flag, const char* group_id) +{ + ReqData_t* send_data = NULL; + RspData_t recv_data; + int temp_len = 0; + int cookie_size = 20; + +// cookie_size = security_server_get_cookie_size(); + char cookie_content[20] = {0, }; + +// if(security_server_request_cookie(cookie_content, cookie_size) < 0) // error while getting cookie +// { +// SLOGE("Fail to get cookie\n"); +// recv_data.rsp_type = SS_SECURE_STORAGE_ERROR; +// goto Error; +// } + + if(!pFilePath) + { + SLOGE("Parameter error in SsClientDeleteFile..\n"); + recv_data.rsp_type = SS_PARAM_ERROR; + goto Error; + } + + send_data = (ReqData_t*)malloc(sizeof(ReqData_t)); + + if(!send_data) + { + SLOGE("Memory allocation fail in SsClientDeleteFile..\n"); + recv_data.rsp_type = SS_MEMORY_ERROR; + goto Error; + } + + send_data->req_type = 10; // delete file + send_data->enc_type = 1; // initial type + send_data->count = 0; + send_data->flag = flag; // flag + temp_len = strlen(pFilePath); + if(temp_len < MAX_FILENAME_LEN) + { + strncpy(send_data->data_infilepath, pFilePath, MAX_FILENAME_LEN - 1); + send_data->data_infilepath[temp_len] = '\0'; + } + else + { + SLOGE("filepath is too long.\n"); + recv_data.rsp_type = SS_PARAM_ERROR; + goto Free_and_Error; + } + memset(send_data->cookie, 0x00, MAX_COOKIE_LEN); + memset(send_data->group_id, 0x00, MAX_GROUP_ID_LEN); + memcpy(send_data->cookie, cookie_content, cookie_size); + if(group_id) + strncpy(send_data->group_id, group_id, MAX_GROUP_ID_LEN - 1); + else + strncpy(send_data->group_id, "NOTUSED", MAX_GROUP_ID_LEN - 1); + memset(send_data->buffer, 0x00, MAX_SEND_DATA_LEN+1); + + recv_data = SsClientComm(send_data); + +Free_and_Error: + free(send_data); + + SLOGE("Deleted file name: %s\n", recv_data.data_filepath); + +Error: + return recv_data.rsp_type; +} |