2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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.
18 * @file FSecCert_CertFileStore.cpp
19 * @brief This file contains implementation of X509 Certificate and private key storing in File System.
31 #include <FIoFileAttributes.h>
32 #include <FBaseString.h>
33 #include <FBaseByteBuffer.h>
34 #include <FBaseResult.h>
35 #include <FBaseSysLog.h>
36 #include "FSecCert_CertFileStore.h"
38 using namespace Tizen::Base;
39 using namespace Tizen::Io;
41 namespace Tizen { namespace Security { namespace Cert
44 const int _MAX_CERT_EXT_PATH_SIZE = 12;
46 _CertFileStore::_CertFileStore(void)
48 , __CertPathType(_CERT_PATH_UNKNOWN)
54 _CertFileStore::_CertFileStore(_CertPathType pathType)
57 __CertPathType = pathType;
61 _CertFileStore::_CertFileStore(char* pFileName, _CertPathType pathType)
64 if (pathType == _CERT_PATH_CA_CERT)
66 __fileName = _CERT_ROOT_CA_CERT_FILE_DIRECTORY;
68 else if (pathType == _CERT_PATH_USER_CERT)
70 __fileName = _CERT_USER_CERT_FILE_DIRECTORY;
72 else if (pathType == _CERT_PATH_PRIVATE_KEY)
74 __fileName = _CERT_USER_PRIVKEY_FILE_DIRECTORY;
77 __fileName.Append(pFileName);
78 __CertPathType = pathType;
81 _CertFileStore::_CertFileStore(CertIdNo hCertHandle, _CertPathType pathType)
83 __hCert = hCertHandle;
84 __CertPathType = pathType;
87 _CertFileStore::~_CertFileStore(void)
93 _CertFileStore::GetPathType(void)
95 return __CertPathType;
99 _CertFileStore::GetFilePath(void)
105 _CertFileStore::SetFilePath(String& filePath)
107 SysTryReturnResult(NID_SEC_CERT, filePath.GetLength() > 0, E_INVALID_ARG, "Invalid input file path.");
109 __fileName = filePath;
114 _CertFileStore::SetFileHandle(CertIdNo hCert, _CertPathType pathType)
116 SysTryReturnResult(NID_SEC_CERT, hCert != null, E_INVALID_ARG, "Invalid input certificate handle.");
119 __CertPathType = pathType;
121 GetFileNameFromHandle(__hCert, __CertPathType, __fileName);
126 _CertFileStore::WriteToFile(byte* pData, int dataLen)
128 result r = E_SUCCESS;
129 String fileName(__fileName);
132 SysTryReturnResult(NID_SEC_CERT, pData != null, E_INVALID_ARG, "Invalid input parameter.");
134 SysTryReturnResult(NID_SEC_CERT, dataLen > 0, E_INVALID_ARG, "Invalid input parameter.");
136 SysTryReturnResult(NID_SEC_CERT, __fileName.GetLength() > 0, E_INACCESSIBLE_PATH, "File path is not set.");
139 r = file.Construct(fileName, L"w+");
140 SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Construct failed.", GetErrorMessage(r));
142 r = file.Write(pData, dataLen);
143 SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Construct failed.", GetErrorMessage(r));
149 _CertFileStore::ReadFromFile(byte* pData, int& dataLen)
151 result r = E_SUCCESS;
152 String fileName(__fileName);
158 SysTryReturnResult(NID_SEC_CERT, pData != null, E_INVALID_ARG, "Invalid input buffer.");
161 r = file.Construct(fileName, L"r");
162 SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Propagated", GetErrorMessage(r));
164 r = File::GetAttributes(fileName, attr);
165 SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
167 fileSize = attr.GetFileSize();
168 SysTryReturn(NID_SEC_CERT, fileSize >= 0, r, r, "[%s] Propagated.", GetErrorMessage(r));
169 SysTryReturn(NID_SEC_CERT, fileSize < _MAX_CERTIFICATE_SIZE, r, r, "[%s] Propagated.", GetErrorMessage(r));
171 readCnt = file.Read(pData, fileSize);
174 SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
180 _CertFileStore::DeleteFile()
182 return File::Remove(__fileName);
186 _CertFileStore::GetFileNameFromHandle(CertIdNo hCert, _CertPathType pathType, String& fileName)
188 char temp[_MAX_CERT_EXT_PATH_SIZE] = {0, };
190 String extn = L".cert";
192 SysTryReturnResult(NID_SEC_CERT, hCert != null, E_INVALID_ARG, "Invalid input parameter.");
194 snprintf(temp, _MAX_CERT_EXT_PATH_SIZE, "%03d", hCert);
195 tempStr.Append(temp);
199 case _CERT_PATH_CA_CERT:
201 fileName = _CERT_ROOT_CA_CERT_FILE_DIRECTORY;
205 case _CERT_PATH_USER_CERT:
207 fileName = _CERT_USER_CERT_FILE_DIRECTORY;
211 case _CERT_PATH_PRIVATE_KEY:
214 fileName = _CERT_USER_PRIVKEY_FILE_DIRECTORY;
220 fileName = _CERT_ROOT_CA_CERT_FILE_DIRECTORY;
225 fileName.Append(tempStr);
226 fileName.Append(extn);
231 } } } //Tizen::Security::Cert