// // Open Service Platform // Copyright (c) 2012 Samsung Electronics Co., Ltd. // // Licensed under the Apache License, Version 2.0 (the License); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // /** * @file SignatureHandler.cpp * @brief This is the implementation file for %SignatureHandler class. */ #include "SignatureHandler.h" #include "InstallerUtil.h" using namespace Tizen::Base; using namespace Tizen::Base::Collection; SignatureHandler::SignatureHandler(void) :__pContext(null) ,__pAuthorCertChain(null) ,__pDistributorCertChain(null) ,__isAuthorSignature(false) ,__isDistributorSignature(false) { } SignatureHandler::~SignatureHandler(void) { if (__pAuthorCertChain) { __pAuthorCertChain->RemoveAll(true); delete __pAuthorCertChain; } if (__pDistributorCertChain) { __pDistributorCertChain->RemoveAll(true); delete __pDistributorCertChain; } } bool SignatureHandler::Construct(InstallationContext* pContext) { __pContext = pContext; return true; } bool SignatureHandler::Parse(const char *pFilepath) { return ParseNormalizedDocument(pFilepath); } bool SignatureHandler::OnStartElement(const char *pName) { TryReturn(pName, true, "pName is null."); bool status = true; if (strcasecmp(pName, "Signature") == 0) { AppLog("------------------------------------------"); AppLog("signature.xml"); AppLog("------------------------------------------"); AppLog("<%s>", pName); status = OnSignatureElement(); } if (!status) { return false; } return true; } bool SignatureHandler::OnEndElement(const char *pName) { TryReturn(pName, true, "pName is null."); if (strcasecmp(pName, "Signature") == 0) { __isDistributorSignature = false; __isAuthorSignature = false; AppLog("", pName); } return true; } bool SignatureHandler::OnCharacters(const char *pCharacters) { bool status = true; char *pName = 0; pName = GetElementName(); TryReturn(pName, false, "pName is null."); if (strcasecmp(pName, "X509Certificate") == 0) { status = OnCertificateValue(pCharacters); } if (!status) { return false; } return true; } bool SignatureHandler::OnSignatureElement(void) { XmlAttribute *pAttr = null; char *pId = null; pAttr = GetAttribute(); TryReturn(pAttr, true, "pAttr is null"); pId = pAttr->Find("Id"); if (pId) { AppLog("", pId); if (strcasecmp(pId, "AuthorSignature") == 0) { __isAuthorSignature = true; } else if (strcasecmp(pId, "DistributorSignature") == 0) { __isDistributorSignature = true; } } return true; } bool SignatureHandler::OnCertificateValue(const char *pCharacters) { AppLog("%s", pCharacters); result r = E_SUCCESS; bool res = true; ByteBuffer* pByteBuffer = null; if (__isAuthorSignature == true) { if (__pAuthorCertChain == null) { __pAuthorCertChain = new (std::nothrow) ArrayList; TryCatch(__pAuthorCertChain, res = false, "__pAuthorCertChain is null"); } pByteBuffer = new (std::nothrow) ByteBuffer; TryCatch(pByteBuffer, res = false, "pByteBuffer is null"); int length = strlen(pCharacters); pByteBuffer->Construct(length); r = pByteBuffer->SetArray((byte*)pCharacters, 0, length); TryCatch(!IsFailed(r), res = false, "SetArray() is failed."); pByteBuffer->Flip(); __pAuthorCertChain->Add(*pByteBuffer); pByteBuffer = null; } else if (__isDistributorSignature == true) { if (__pDistributorCertChain == null) { __pDistributorCertChain = new (std::nothrow) ArrayList; TryCatch(__pDistributorCertChain, res = false, "__pDistributorCertChain is null"); } pByteBuffer = new (std::nothrow) ByteBuffer; TryCatch(pByteBuffer, res = false, "pByteBuffer is null"); int length = strlen(pCharacters); pByteBuffer->Construct(length); r = pByteBuffer->SetArray((byte*)pCharacters, 0, length); TryCatch(!IsFailed(r), res = false, "SetArray() is failed."); pByteBuffer->Flip(); __pDistributorCertChain->Add(*pByteBuffer); pByteBuffer = null; } CATCH: delete pByteBuffer; return res; } ArrayList* SignatureHandler::GetAuthorCertChain(void) { return __pAuthorCertChain; } ArrayList* SignatureHandler::GetDistributorCertChain(void) { return __pDistributorCertChain; }