Fixed Klocworks issues
[platform/framework/native/appfw.git] / src / security / cert / FSecCert_Asn1Parser.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_Asn1Parser.cpp
19  * @brief               This file contains implementation of base class Asn1 parser.
20 */
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <error.h>
25 #include <memory.h>
26 #include <new>
27 #include <sys/stat.h>
28 #include <assert.h>
29 #include <dirent.h>
30
31 #include <FBaseByteBuffer.h>
32 #include <FBaseString.h>
33 #include <FIoFile.h>
34 #include <FIoFileAttributes.h>
35 #include <FBaseResult.h>
36 #include <FBaseSysLog.h>
37
38 #include "FSecCert_Asn1Parser.h"
39 #include "FSecCert_CertTypes.h"
40
41 namespace Tizen { namespace Security { namespace Cert
42 {
43
44
45 _Asn1Parser::_Asn1Parser(void)
46         : _pX509Buff(null)
47         , _x509BuffSize(0)
48 {
49
50 }
51
52 _Asn1Parser::~_Asn1Parser(void)
53 {
54 }
55
56 result
57 _Asn1Parser::Parse(byte* pAsnBuffer, int bufferSize)
58 {
59         _pX509Buff.reset(null);
60         _x509BuffSize = 0;
61
62         SysTryReturnResult(NID_SEC_CERT, pAsnBuffer != null, E_INVALID_ARG, "Invalid input arguments, null buffer passed as arugment.");
63         SysTryReturnResult(NID_SEC_CERT, bufferSize > 0, E_INVALID_ARG, "Invalid input arguments, length must be greater than zero.");
64
65         std::unique_ptr< byte[] > pX509Buff(new (std::nothrow) byte[bufferSize]);
66         SysTryReturnResult(NID_SEC_CERT, pX509Buff != null, E_OUT_OF_MEMORY, "Failed to allocate memory.");
67
68         memcpy(pX509Buff.get(), pAsnBuffer, bufferSize);
69         _x509BuffSize = bufferSize;
70
71         _pX509Buff = std::move(pX509Buff);
72
73         return ParseObject();
74
75 }
76
77 result
78 _Asn1Parser::Parse(char* pFilePath)
79 {
80         result r = E_SUCCESS;
81         int readCnt = 0;
82         long flieSize = 0;
83         Tizen::Base::String fileName(pFilePath);
84         Tizen::Io::FileAttributes attr;
85         Tizen::Io::File file;
86
87         _pX509Buff.reset(null);
88         _x509BuffSize = 0;
89
90         SysTryReturnResult(NID_SEC_CERT, pFilePath != null, E_INVALID_ARG, "Invalid input argument, file path passed is null.");
91
92         r = Tizen::Io::File::GetAttributes(fileName, attr);
93         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to get file attributes.", GetErrorMessage(r));
94
95         flieSize = attr.GetFileSize();
96         SysTryReturnResult(NID_SEC_CERT, (flieSize >= 0), E_SYSTEM, "Input file size must be greater zero.");
97         SysTryReturnResult(NID_SEC_CERT, (flieSize < _MAX_CERTIFICATE_SIZE), E_SYSTEM, "File size exceeds maximum specified length.");
98
99         // Open file
100         r = file.Construct(fileName, L"r");
101         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to construct file.", GetErrorMessage(r));
102
103         std::unique_ptr< byte[] > pX509Buff(new (std::nothrow) byte[flieSize]);
104         SysTryReturnResult(NID_SEC_CERT, pX509Buff != null, E_OUT_OF_MEMORY, "Failed to allocate memory.");
105
106         readCnt = file.Read(pX509Buff.get(), flieSize);
107         SysTryReturn(NID_SEC_CERT, readCnt == flieSize, GetLastResult(), GetLastResult(), "[%s] Failed to read from a file.", GetErrorMessage(GetLastResult()));
108
109         _x509BuffSize = readCnt;
110
111         _pX509Buff = std::move(pX509Buff);
112
113         return ParseObject();
114 }
115
116 } } } //Tizen::Security::Cert