725cd2086ec24aeb0a5655462a08170f39517ab7
[platform/framework/native/installer.git] / src / XmlHandler / SignatureHandler.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  * @file        SignatureHandler.cpp
19  * @brief       This is the implementation file for %SignatureHandler class.
20  */
21
22 #include "SignatureHandler.h"
23 #include "InstallerUtil.h"
24
25 using namespace Tizen::Base;
26 using namespace Tizen::Base::Collection;
27
28 SignatureHandler::SignatureHandler(void)
29 :__pContext(null)
30 ,__pAuthorCertChain(null)
31 ,__pDistributorCertChain(null)
32 ,__isAuthorSignature(false)
33 ,__isDistributorSignature(false)
34 {
35 }
36
37 SignatureHandler::~SignatureHandler(void)
38 {
39         if (__pAuthorCertChain)
40         {
41                 __pAuthorCertChain->RemoveAll(true);
42                 delete __pAuthorCertChain;
43         }
44
45         if (__pDistributorCertChain)
46         {
47                 __pDistributorCertChain->RemoveAll(true);
48                 delete __pDistributorCertChain;
49         }
50 }
51
52 bool
53 SignatureHandler::Construct(InstallationContext* pContext)
54 {
55         __pContext = pContext;
56
57         return true;
58 }
59
60 bool
61 SignatureHandler::Parse(const char *pFilepath)
62 {
63         return ParseNormalizedDocument(pFilepath);
64 }
65
66 bool
67 SignatureHandler::OnStartElement(const char *pName)
68 {
69         TryReturn(pName, true, "pName is null.");
70
71         bool status = true;
72
73         if (strcasecmp(pName, "Signature") == 0)
74         {
75                 AppLog("------------------------------------------");
76                 AppLog("signature.xml");
77                 AppLog("------------------------------------------");
78                 AppLog("<%s>", pName);
79                 status = OnSignatureElement();
80         }
81
82         if (!status)
83         {
84                 return false;
85         }
86
87         return true;
88 }
89
90 bool
91 SignatureHandler::OnEndElement(const char *pName)
92 {
93         TryReturn(pName, true, "pName is null.");
94
95         if (strcasecmp(pName, "Signature") == 0)
96         {
97                 __isDistributorSignature = false;
98                 __isAuthorSignature = false;
99                 AppLog("</%s>", pName);
100         }
101
102         return true;
103 }
104
105 bool
106 SignatureHandler::OnCharacters(const char *pCharacters)
107 {
108         bool status = true;
109         char *pName = 0;
110
111         pName = GetElementName();
112         TryReturn(pName, false, "pName is null.");
113
114         if (strcasecmp(pName, "X509Certificate") == 0)
115         {
116                 status = OnCertificateValue(pCharacters);
117         }
118
119         if (!status)
120         {
121                 return false;
122         }
123
124         return true;
125 }
126
127 bool
128 SignatureHandler::OnSignatureElement(void)
129 {
130         XmlAttribute *pAttr = null;
131         char *pId = null;
132
133         pAttr = GetAttribute();
134         TryReturn(pAttr, true, "pAttr is null");
135
136         pId = pAttr->Find("Id");
137         if (pId)
138         {
139                 AppLog("<Id = %s>", pId);
140
141                 if (strcasecmp(pId, "AuthorSignature") == 0)
142                 {
143                         __isAuthorSignature = true;
144                 }
145                 else if (strcasecmp(pId, "DistributorSignature") == 0)
146                 {
147                         __isDistributorSignature = true;
148                 }
149         }
150
151         return true;
152 }
153
154 bool
155 SignatureHandler::OnCertificateValue(const char *pCharacters)
156 {
157         AppLog("<X509Certificate>%s</X509Certificate>", pCharacters);
158
159         result r = E_SUCCESS;
160         bool res = true;
161         ByteBuffer* pByteBuffer = null;
162
163         if (__isAuthorSignature == true)
164         {
165                 if (__pAuthorCertChain == null)
166                 {
167                         __pAuthorCertChain = new (std::nothrow) ArrayList;
168                         TryCatch(__pAuthorCertChain, res = false, "__pAuthorCertChain is null");
169                 }
170
171                 pByteBuffer = new (std::nothrow) ByteBuffer;
172                 TryCatch(pByteBuffer, res = false, "pByteBuffer is null");
173
174                 int length = strlen(pCharacters);
175                 pByteBuffer->Construct(length);
176
177                 r = pByteBuffer->SetArray((byte*)pCharacters, 0, length);
178                 TryCatch(!IsFailed(r), res = false, "SetArray() is failed.");
179
180                 pByteBuffer->Flip();
181
182                 __pAuthorCertChain->Add(*pByteBuffer);
183                 pByteBuffer = null;
184         }
185         else if (__isDistributorSignature == true)
186         {
187                 if (__pDistributorCertChain == null)
188                 {
189                         __pDistributorCertChain = new (std::nothrow) ArrayList;
190                         TryCatch(__pDistributorCertChain, res = false, "__pDistributorCertChain is null");
191                 }
192
193                 pByteBuffer = new (std::nothrow) ByteBuffer;
194                 TryCatch(pByteBuffer, res = false, "pByteBuffer is null");
195
196                 int length = strlen(pCharacters);
197                 pByteBuffer->Construct(length);
198
199                 r = pByteBuffer->SetArray((byte*)pCharacters, 0, length);
200                 TryCatch(!IsFailed(r), res = false, "SetArray() is failed.");
201
202                 pByteBuffer->Flip();
203
204                 __pDistributorCertChain->Add(*pByteBuffer);
205                 pByteBuffer = null;
206         }
207
208 CATCH:
209         delete pByteBuffer;
210         return res;
211 }
212
213 ArrayList*
214 SignatureHandler::GetAuthorCertChain(void)
215 {
216         return __pAuthorCertChain;
217 }
218
219 ArrayList*
220 SignatureHandler::GetDistributorCertChain(void)
221 {
222         return __pDistributorCertChain;
223 }