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_Asn1Parser.cpp
19 * @brief This file contains implementation of base class Asn1 parser.
31 #include <FBaseByteBuffer.h>
32 #include <FBaseString.h>
34 #include <FIoFileAttributes.h>
35 #include <FBaseResult.h>
36 #include <FBaseSysLog.h>
38 #include "FSecCert_Asn1Parser.h"
39 #include "FSecCert_CertTypes.h"
41 namespace Tizen { namespace Security { namespace Cert
45 _Asn1Parser::_Asn1Parser(void)
52 _Asn1Parser::~_Asn1Parser(void)
57 _Asn1Parser::Parse(byte* pAsnBuffer, int bufferSize)
59 _pX509Buff.reset(null);
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.");
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.");
68 memcpy(pX509Buff.get(), pAsnBuffer, bufferSize);
69 _x509BuffSize = bufferSize;
71 _pX509Buff = std::move(pX509Buff);
78 _Asn1Parser::Parse(char* pFilePath)
83 Tizen::Base::String fileName(pFilePath);
84 Tizen::Io::FileAttributes attr;
87 _pX509Buff.reset(null);
90 SysTryReturnResult(NID_SEC_CERT, pFilePath != null, E_INVALID_ARG, "Invalid input argument, file path passed is null.");
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));
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.");
100 r = file.Construct(fileName, L"r");
101 SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to construct file.", GetErrorMessage(r));
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.");
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()));
109 _x509BuffSize = readCnt;
111 _pX509Buff = std::move(pX509Buff);
113 return ParseObject();
116 } } } //Tizen::Security::Cert