9f25582f8f923fca3d9ee4b9acbdd293746fbe89
[platform/framework/native/appfw.git] / src / security / cert / FSecCert_Asn1Parser.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 /**
19  * @file                FSecCert_Asn1Parser.cpp
20  * @brief               This file contains implementation of base class Asn1 parser.
21 */
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <error.h>
26 #include <memory.h>
27 #include <new>
28 #include <sys/stat.h>
29 #include <assert.h>
30 #include <dirent.h>
31
32 #include <FBaseByteBuffer.h>
33 #include <FBaseString.h>
34 #include <FIoFile.h>
35 #include <FIoFileAttributes.h>
36 #include <FBaseResult.h>
37 #include <FBaseSysLog.h>
38
39 #include "FSecCert_Asn1Parser.h"
40 #include "FSecCert_CertTypes.h"
41
42 namespace Tizen { namespace Security { namespace Cert
43 {
44
45
46 _Asn1Parser::_Asn1Parser(void)
47         : _pX509Buff(null)
48         , _x509BuffSize(0)
49 {
50
51 }
52
53 _Asn1Parser::~_Asn1Parser(void)
54 {
55 }
56
57 result
58 _Asn1Parser::Parse(byte* pAsnBuffer, int bufferSize)
59 {
60         _pX509Buff.reset(null);
61         _x509BuffSize = 0;
62
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.");
65
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.");
68
69         memcpy(pX509Buff.get(), pAsnBuffer, bufferSize);
70         _x509BuffSize = bufferSize;
71
72         _pX509Buff = std::move(pX509Buff);
73
74         return ParseObject();
75
76 }
77
78 result
79 _Asn1Parser::Parse(char* pFilePath)
80 {
81         result r = E_SUCCESS;
82         int readCnt = 0;
83         long flieSize = 0;
84         Tizen::Base::String fileName(pFilePath);
85         Tizen::Io::FileAttributes attr;
86         Tizen::Io::File file;
87
88         _pX509Buff.reset(null);
89         _x509BuffSize = 0;
90
91         SysTryReturnResult(NID_SEC_CERT, pFilePath != null, E_INVALID_ARG, "Invalid input argument, file path passed is null.");
92
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));
95
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.");
99
100         // Open file
101         r = file.Construct(fileName, L"r");
102         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to construct file.", GetErrorMessage(r));
103
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.");
106
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()));
109
110         _x509BuffSize = readCnt;
111
112         _pX509Buff = std::move(pX509Buff);
113
114         return ParseObject();
115 }
116
117 } } } //Tizen::Security::Cert