Applied sizeof operator on fixed sized array to calculate size.
[platform/framework/native/appfw.git] / src / security / cert / FSecCert_CertFileStore.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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  * @file                FSecCert_CertFileStore.cpp
19  * @brief               This file contains implementation of X509 Certificate and private key storing in File System.
20 */
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <error.h>
26 #include <new>
27 #include <sys/stat.h>
28 #include <assert.h>
29 #include <dirent.h>
30 #include <FIoFile.h>
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"
37
38 using namespace Tizen::Base;
39 using namespace Tizen::Io;
40
41 namespace Tizen { namespace Security { namespace Cert
42 {
43
44 const int _MAX_CERT_EXT_PATH_SIZE = 12;
45
46 _CertFileStore::_CertFileStore(void)
47         : __hCert(0)
48         , __CertPathType(_CERT_PATH_UNKNOWN)
49 {
50
51 }
52
53
54 _CertFileStore::_CertFileStore(_CertPathType pathType)
55         : __hCert(0)
56 {
57         __CertPathType = pathType;
58
59 }
60
61 _CertFileStore::_CertFileStore(char* pFileName, _CertPathType pathType)
62         : __hCert(0)
63 {
64         if (pathType == _CERT_PATH_CA_CERT)
65         {
66                 __fileName = _CERT_ROOT_CA_CERT_FILE_DIRECTORY;
67         }
68         else if (pathType == _CERT_PATH_USER_CERT)
69         {
70                 __fileName = _CERT_USER_CERT_FILE_DIRECTORY;
71         }
72         else if (pathType == _CERT_PATH_PRIVATE_KEY)
73         {
74                 __fileName = _CERT_USER_PRIVKEY_FILE_DIRECTORY;
75         }
76
77         __fileName.Append(pFileName);
78         __CertPathType = pathType;
79 }
80
81 _CertFileStore::_CertFileStore(CertIdNo hCertHandle, _CertPathType pathType)
82 {
83         __hCert = hCertHandle;
84         __CertPathType = pathType;
85 }
86
87 _CertFileStore::~_CertFileStore(void)
88 {
89
90 }
91
92 _CertPathType
93 _CertFileStore::GetPathType(void)
94 {
95         return __CertPathType;
96 }
97
98 String
99 _CertFileStore::GetFilePath(void)
100 {
101         return __fileName;
102 }
103
104 result
105 _CertFileStore::SetFilePath(String& filePath)
106 {
107         SysTryReturnResult(NID_SEC_CERT, filePath.GetLength() > 0, E_INVALID_ARG, "Invalid input file path.");
108
109         __fileName = filePath;
110         return E_SUCCESS;
111 }
112
113 result
114 _CertFileStore::SetFileHandle(CertIdNo hCert, _CertPathType pathType)
115 {
116         SysTryReturnResult(NID_SEC_CERT, hCert != null, E_INVALID_ARG, "Invalid input certificate handle.");
117
118         __hCert = hCert;
119         __CertPathType = pathType;
120         __fileName = L"";
121         GetFileNameFromHandle(__hCert, __CertPathType, __fileName);
122         return E_SUCCESS;
123 }
124
125 result
126 _CertFileStore::WriteToFile(byte* pData, int dataLen)
127 {
128         result r = E_SUCCESS;
129         String fileName(__fileName);
130         File file;
131
132         SysTryReturnResult(NID_SEC_CERT, pData != null, E_INVALID_ARG, "Invalid input parameter.");
133
134         SysTryReturnResult(NID_SEC_CERT, dataLen > 0, E_INVALID_ARG, "Invalid input parameter.");
135
136         SysTryReturnResult(NID_SEC_CERT, __fileName.GetLength() > 0, E_INACCESSIBLE_PATH, "File path is not set.");
137
138         // Open file
139         r = file.Construct(fileName, L"w+");
140         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Construct failed.", GetErrorMessage(r));
141
142         r = file.Write(pData, dataLen);
143         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Construct failed.", GetErrorMessage(r));
144
145         return E_SUCCESS;
146 }
147
148 result
149 _CertFileStore::ReadFromFile(byte* pData, int& dataLen)
150 {
151         result r = E_SUCCESS;
152         String fileName(__fileName);
153         FileAttributes attr;
154         File file;
155         long fileSize = 0;
156         int readCnt = 0;
157
158         SysTryReturnResult(NID_SEC_CERT, pData != null, E_INVALID_ARG, "Invalid input buffer.");
159
160         // Open file
161         r = file.Construct(fileName, L"r");
162         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Propagated", GetErrorMessage(r));
163
164         r = File::GetAttributes(fileName, attr);
165         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
166
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));
170
171         readCnt = file.Read(pData, fileSize);
172         dataLen = readCnt;
173         r = GetLastResult();
174         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
175
176         return r;
177 }
178
179 result
180 _CertFileStore::DeleteFile()
181 {
182         return File::Remove(__fileName);
183 }
184
185 result
186 _CertFileStore::GetFileNameFromHandle(CertIdNo hCert, _CertPathType pathType, String& fileName)
187 {
188         char temp[_MAX_CERT_EXT_PATH_SIZE] = {0, };
189         String tempStr;
190         String extn = L".cert";
191
192         SysTryReturnResult(NID_SEC_CERT, hCert != null, E_INVALID_ARG, "Invalid input parameter.");
193
194         snprintf(temp, sizeof(temp), "%03d", hCert);
195         tempStr.Append(temp);
196
197         switch (pathType)
198         {
199         case _CERT_PATH_CA_CERT:
200         {
201                 fileName = _CERT_ROOT_CA_CERT_FILE_DIRECTORY;
202         }
203         break;
204
205         case _CERT_PATH_USER_CERT:
206         {
207                 fileName = _CERT_USER_CERT_FILE_DIRECTORY;
208         }
209         break;
210
211         case _CERT_PATH_PRIVATE_KEY:
212         {
213                 extn = L".key";
214                 fileName = _CERT_USER_PRIVKEY_FILE_DIRECTORY;
215         }
216         break;
217
218         default:
219         {
220                 fileName = _CERT_ROOT_CA_CERT_FILE_DIRECTORY;
221         }
222         break;
223         }
224
225         fileName.Append(tempStr);
226         fileName.Append(extn);
227
228         return E_SUCCESS;
229 }
230
231 } } } //Tizen::Security::Cert