2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @file FSecCert_Asn1Parser.cpp
20 * @brief This file contains implementation of base class Asn1 parser.
32 #include <FBaseByteBuffer.h>
33 #include <FBaseString.h>
35 #include <FIoFileAttributes.h>
36 #include <FBaseResult.h>
37 #include <FBaseSysLog.h>
39 #include "FSecCert_Asn1Parser.h"
40 #include "FSecCert_CertTypes.h"
42 namespace Tizen { namespace Security { namespace Cert
46 _Asn1Parser::_Asn1Parser(void)
53 _Asn1Parser::~_Asn1Parser(void)
58 _Asn1Parser::Parse(byte* pAsnBuffer, int bufferSize)
60 _pX509Buff.reset(null);
63 SysTryReturnResult(NID_SEC_CERT, pAsnBuffer != null, E_INVALID_ARG, "Invalid input arguments, null buffer passed as arugment.");
64 SysTryReturnResult(NID_SEC_CERT, bufferSize > 0, E_INVALID_ARG, "Invalid input arguments, length must be greater than zero.");
66 std::unique_ptr< byte[] > pX509Buff(new (std::nothrow) byte[bufferSize]);
67 SysTryReturnResult(NID_SEC_CERT, pX509Buff != null, E_OUT_OF_MEMORY, "Failed to allocate memory.");
69 memcpy(pX509Buff.get(), pAsnBuffer, bufferSize);
70 _x509BuffSize = bufferSize;
72 _pX509Buff = std::move(pX509Buff);
79 _Asn1Parser::Parse(char* pFilePath)
84 Tizen::Base::String fileName(pFilePath);
85 Tizen::Io::FileAttributes attr;
88 _pX509Buff.reset(null);
91 SysTryReturnResult(NID_SEC_CERT, pFilePath != null, E_INVALID_ARG, "Invalid input argument, file path passed is null.");
93 r = Tizen::Io::File::GetAttributes(fileName, attr);
94 SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to get file attributes.", GetErrorMessage(r));
96 flieSize = attr.GetFileSize();
97 SysTryReturnResult(NID_SEC_CERT, (flieSize >= 0), E_SYSTEM, "Input file size must be greater zero.");
98 SysTryReturnResult(NID_SEC_CERT, (flieSize < _MAX_CERTIFICATE_SIZE), E_SYSTEM, "File size exceeds maximum specified length.");
101 r = file.Construct(fileName, L"r");
102 SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to construct file.", GetErrorMessage(r));
104 std::unique_ptr< byte[] > pX509Buff(new (std::nothrow) byte[flieSize]);
105 SysTryReturnResult(NID_SEC_CERT, pX509Buff != null, E_OUT_OF_MEMORY, "Failed to allocate memory.");
107 readCnt = file.Read(pX509Buff.get(), flieSize);
108 SysTryReturn(NID_SEC_CERT, readCnt == flieSize, GetLastResult(), GetLastResult(), "[%s] Failed to read from a file.", GetErrorMessage(GetLastResult()));
110 _x509BuffSize = readCnt;
112 _pX509Buff = std::move(pX509Buff);
114 return ParseObject();
117 } } } //Tizen::Security::Cert