/////////////////////////////////////////////////////////////////////////////// // Copyright (C) 2002-2025, Open Design Alliance (the "Alliance"). // All rights reserved. // // This software and its documentation and related materials are owned by // the Alliance. The software may only be incorporated into application // programs owned by members of the Alliance, subject to a signed // Membership Agreement and Supplemental Software License Agreement with the // Alliance. The structure and organization of this software are the valuable // trade secrets of the Alliance and its suppliers. The software is also // protected by copyright law and international treaty provisions. Application // programs incorporating this software must include the following statement // with their copyright notices: // // This application incorporates Open Design Alliance software pursuant to a license // agreement with Open Design Alliance. // Open Design Alliance Copyright (C) 2002-2025 by Open Design Alliance. // All rights reserved. // // By use of this software, its documentation or related materials, you // acknowledge and accept the above terms. /////////////////////////////////////////////////////////////////////////////// #include "OdaCommon.h" #include "StaticRxObject.h" #include "ExSystemServices.h" #include "DynamicLinker.h" #include "diagnostics.h" #include "RxDynamicModule.h" #include "OdCryptoServices/OdCryptoServices.h" #include "OdDwfxSignatureHandler.h" #define STL_USING_IOSTREAM #include "OdaSTL.h" #define STD(a) std:: a #ifdef OD_HAVE_CONSOLE_H_FILE #include #endif #if defined(__GNUC__) #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-result" #endif #ifndef _TOOLKIT_IN_DLL_ #if defined(_MSC_VER) && (_MSC_VER > 1500) ODRX_DECLARE_STATIC_MODULE_ENTRY_POINT(DwfxSignatureHandlerWinModule); ODRX_BEGIN_STATIC_MODULE_MAP() ODRX_DEFINE_STATIC_APPMODULE(DwfxSignatureHandlerWinModuleName, DwfxSignatureHandlerWinModule) ODRX_END_STATIC_MODULE_MAP() #else ODRX_DECLARE_STATIC_MODULE_ENTRY_POINT(DwfxSignatureHandlerModule); ODRX_BEGIN_STATIC_MODULE_MAP() ODRX_DEFINE_STATIC_APPMODULE(DwfxSignatureHandlerModuleName, DwfxSignatureHandlerModule) ODRX_END_STATIC_MODULE_MAP() #endif #endif #define MAX_PATH_LENGTH 1024 void printCertData(const OdCertParameters& certData); void validate(OdDwfxSignatureHandler* pSignatureHandler, const OdString& inputFilePath); void sign(OdDwfxSignatureHandler* pSignatureHandler, const OdString& inputFilePath, const OdCertificateShortDesc& certShortDesc); void remove(OdDwfxSignatureHandler* pSignatureHandler, const OdString& inputFilePath); #if defined(OD_USE_WMAIN) int wmain(int argc, wchar_t* argv[]) #else int main(int argc, char* argv[]) #endif { #ifdef OD_HAVE_CCOMMAND_FUNC argc = ccommand(&argv); #endif if ((argc != 3)&&(argc != 6)) { OdPrintf("Wrong parameters\nUsage:\n"); OdPrintf("DwfxSignatureSample filePath VALIDATE\n"); OdPrintf("or\n"); OdPrintf("DwfxSignatureSample filePath SIGN CertSubject CertIssuer CertSerialNumber\n"); OdPrintf("or\n"); OdPrintf("DwfxSignatureSample filePath REMOVE"); return 10; } #ifndef _TOOLKIT_IN_DLL_ ODRX_INIT_STATIC_MODULE_MAP(); #endif /**********************************************************************/ /* Initialize Runtime Extension environment */ /**********************************************************************/ OdStaticRxObject svcs; odrxInitialize(&svcs); try { OdDwfxSignatureHandlerModulePtr pSignatureHandlerModule; #if defined(ODA_WINDOWS) pSignatureHandlerModule = odrxDynamicLinker()->loadApp(DwfxSignatureHandlerWinModuleName); #else pSignatureHandlerModule = odrxDynamicLinker()->loadApp(DwfxSignatureHandlerModuleName); #endif if (!pSignatureHandlerModule.isNull()) { OdDwfxSignatureHandlerPtr pSignatureHandler; pSignatureHandler = pSignatureHandlerModule->getDwfxSignatureHandler(); OdString inputFilePath = OdString(argv[1]); if (!svcs.accessFile(inputFilePath, Oda::kFileRead)) { OdPrintf("\nCan't access the file specified!\n"); } else { if (OdString(argv[2]).iCompare("VALIDATE") == 0) { validate(pSignatureHandler, inputFilePath); } else if (OdString(argv[2]).iCompare("SIGN") == 0) { OdCertificateShortDesc certShortDesc; certShortDesc.m_CertSubject = OdString(argv[3]); certShortDesc.m_CertIssuer = OdString(argv[4]); certShortDesc.m_CertSerialNum = OdString(argv[5]); sign(pSignatureHandler, inputFilePath, certShortDesc); } else if (OdString(argv[2]).iCompare("REMOVE") == 0) { remove(pSignatureHandler, inputFilePath); } else { OdPrintf("Wrong input!\n"); } } } else { OdPrintf("Can't load DWFx signature handler module!\n"); } } catch (OdError& e) { OdPrintf("Exception (%ls) during the package processing!\n", e.description().c_str()); } catch (...) { OdPrintf("Unknown Exception during the package processing!\n"); } /**********************************************************************/ /* Uninitialize Runtime Extension environment */ /**********************************************************************/ ::odrxUninitialize(); return 0; } void printCertData(const OdCertParameters& certData) { OdPrintf("Subject = %ls, Issuer = %ls, Serial number = %ls, Expiration date = %ls\n", certData.sCertSubject.c_str(), certData.sCertIssuer.c_str(), certData.sCertSerialNum.c_str(), certData.m_CertValidTo.c_str()); } void validate(OdDwfxSignatureHandler* pSignatureHandler, const OdString& inputFilePath) { OdCertParameters outCertParam; OdDwfxSignatureHandler::SignatureValidationResult validationRes = OdDwfxSignatureHandler::kNotSigned; //VALIDATION: if (pSignatureHandler->validatePackageSignature(inputFilePath, validationRes, outCertParam)) { switch (validationRes) { case OdDwfxSignatureHandler::kNotSigned: OdPrintf("RESULT : The package has no signature.\n"); break; case OdDwfxSignatureHandler::kSuccess: OdPrintf("RESULT : The signature is valid.\n"); OdPrintf("Certificate data:\n"); printCertData(outCertParam); break; case OdDwfxSignatureHandler::kInvalidSignature: OdPrintf("RESULT : Invalid signature!\n"); break; case OdDwfxSignatureHandler::kCertificateChainProblem: OdPrintf("RESULT : Can't verify certificate trust status!\n"); OdPrintf("Certificate data:\n"); printCertData(outCertParam); break; } } else { OdPrintf("ERROR while trying to validate signature!\n"); } } void sign(OdDwfxSignatureHandler* pSignatureHandler, const OdString& inputFilePath, const OdCertificateShortDesc& certShortDesc) { OdCryptoServicesPtr pCryptoServices; OdRxClassPtr pService = odrxServiceDictionary()->getAt(OD_T("OdCryptoServices")); if (!pService.isNull()) { pCryptoServices = pService->create(); OdCertificateObjectPtr pCertObj = pCryptoServices->getCertObjByShortDesc(certShortDesc); if ((!pCertObj.isNull()) && (pCertObj->isTrusted())) { OdCertificateDescription certDesc = pCertObj->getCertDescription(); OdCertParameters certParams; certParams.sCertSubject = certDesc.m_CertSubject; certParams.sCertIssuer = certDesc.m_CertIssuer; certParams.sCertSerialNum = certDesc.m_CertSerialNum; certParams.m_CertValidFrom = certDesc.m_CertValidFrom; certParams.m_CertValidTo = certDesc.m_CertValidTo; //SIGNING: pSignatureHandler->setCertParameters(certParams); if (pSignatureHandler->signPackage(inputFilePath)) OdPrintf("RESULT: The package successfully signed.\n"); else OdPrintf("ERROR while trying to sign the package!\n"); } else { OdPrintf("ERROR : No suitable Digital ID (Certificate) was found!\n"); } } else { OdPrintf("ERROR : Can't initialize OdCryptoServices!\n"); } } void remove(OdDwfxSignatureHandler* pSignatureHandler, const OdString& inputFilePath) { //REMOVE EXISTING SIGNATURE: if (pSignatureHandler->removeExistingSignature(inputFilePath)) OdPrintf("RESULT: Signature successfully removed!\n"); else OdPrintf("ERROR while trying to remove existing signature!\n"); } #if defined(__GNUC__) #pragma GCC diagnostic pop #endif