Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(PHP ActiveX) Sign SOAP XML for New Zealand Customs ServiceSee more XAdES ExamplesDemonstrates how to create an XAdES signed SOAP XML pertaining to the New Zealand Customs Service. Note: This example requires Chilkat v9.5.0.96 or later.
<?php // This example assumes the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. $success = 1; // Create the following XML to be signed: // <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" // xmlns:v1="http://customs.govt.nz/jbms/msggate/reqresp/v1"> // <soapenv:Header> // <wsse:Security soapenv:mustUnderstand="1" // xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" // xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"> // <wsu:Timestamp wsu:Id="TS-037E78514E9B9132CB16817563559151"> // <wsu:Created>2023-04-17T18:32:35.913Z</wsu:Created> // <wsu:Expires>2023-04-17T19:32:35.913Z</wsu:Expires> // </wsu:Timestamp> // </wsse:Security> // </soapenv:Header> // <soapenv:Body wsu:Id="id-8" // xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"> // <v1:RequestResponse> // <v1:Submitter>TEST1234</v1:Submitter> // <v1:MailboxMsgId>999999</v1:MailboxMsgId> // </v1:RequestResponse> // </soapenv:Body> // </soapenv:Envelope> // Create a random ID like this: TS-037E78514E9B9132CB16817563559151 // For versions of Chilkat < 10.0.0, use new COM('Chilkat_9_5_0.Chilkat.StringBuilder') $tsId = new COM("Chilkat.StringBuilder"); $tsId->Append('TS-'); $tsId->AppendRandom(16,'hex'); // STR-037E78514E9B9132CB16817563559614 // For versions of Chilkat < 10.0.0, use new COM('Chilkat_9_5_0.Chilkat.StringBuilder') $strId = new COM("Chilkat.StringBuilder"); $strId->Append('STR-'); $strId->AppendRandom(16,'hex'); // KI-037E78514E9B9132CB16817563559583 // For versions of Chilkat < 10.0.0, use new COM('Chilkat_9_5_0.Chilkat.StringBuilder') $keyInfoId = new COM("Chilkat.StringBuilder"); $keyInfoId->Append('KI-'); $keyInfoId->AppendRandom(16,'hex'); // Create a date/time for the current time with this format: 2023-04-17T18:32:35.913Z // For versions of Chilkat < 10.0.0, use new COM('Chilkat_9_5_0.Chilkat.CkDateTime') $dt = new COM("Chilkat.CkDateTime"); $dt->SetFromCurrentSystemTime(); // For versions of Chilkat < 10.0.0, use new COM('Chilkat_9_5_0.Chilkat.StringBuilder') $sbNow = new COM("Chilkat.StringBuilder"); $sbNow->Append($dt->getAsTimestamp(0)); // If we really need the milliseconds, we can replace the "Z" with ".000Z" // The server will also likely accept a timestamp without milliseconds, such as 2023-04-17T18:32:35Z $n = $sbNow->Replace('Z','.000Z'); // For versions of Chilkat < 10.0.0, use new COM('Chilkat_9_5_0.Chilkat.StringBuilder') $sbNowPlusOneHour = new COM("Chilkat.StringBuilder"); $dt->AddSeconds(3600); $sbNowPlusOneHour->Append($dt->getAsTimestamp(0)); $n = $sbNowPlusOneHour->Replace('Z','.000Z'); // For versions of Chilkat < 10.0.0, use new COM('Chilkat_9_5_0.Chilkat.Xml') $xmlToSign = new COM("Chilkat.Xml"); $xmlToSign->Tag = 'soapenv:Envelope'; $xmlToSign->AddAttribute('xmlns:soapenv','http://schemas.xmlsoap.org/soap/envelope/'); $xmlToSign->AddAttribute('xmlns:v1','http://customs.govt.nz/jbms/msggate/reqresp/v1'); $xmlToSign->UpdateAttrAt('soapenv:Header|wsse:Security',1,'soapenv:mustUnderstand','1'); $xmlToSign->UpdateAttrAt('soapenv:Header|wsse:Security',1,'xmlns:wsse','http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'); $xmlToSign->UpdateAttrAt('soapenv:Header|wsse:Security',1,'xmlns:wsu','http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd'); $xmlToSign->UpdateAttrAt('soapenv:Header|wsse:Security|wsu:Timestamp',1,'wsu:Id',$tsId->getAsString()); $xmlToSign->UpdateChildContent('soapenv:Header|wsse:Security|wsu:Timestamp|wsu:Created',$sbNow->getAsString()); $xmlToSign->UpdateChildContent('soapenv:Header|wsse:Security|wsu:Timestamp|wsu:Expires',$sbNowPlusOneHour->getAsString()); $xmlToSign->UpdateAttrAt('soapenv:Body',1,'wsu:Id','id-8'); $xmlToSign->UpdateAttrAt('soapenv:Body',1,'xmlns:wsu','http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd'); $xmlToSign->UpdateChildContent('soapenv:Body|v1:RequestResponse|v1:Submitter','TEST1234'); $xmlToSign->UpdateChildContent('soapenv:Body|v1:RequestResponse|v1:MailboxMsgId','999999'); // For versions of Chilkat < 10.0.0, use new COM('Chilkat_9_5_0.Chilkat.XmlDSigGen') $gen = new COM("Chilkat.XmlDSigGen"); $gen->SigLocation = 'soapenv:Envelope|soapenv:Header|wsse:Security'; $gen->SigLocationMod = 0; $gen->SigId = 'SIG-037E78514E9B9132CB16817563559695'; $gen->SigNamespacePrefix = 'ds'; $gen->SigNamespaceUri = 'http://www.w3.org/2000/09/xmldsig#'; $gen->SignedInfoPrefixList = 'soapenv v1'; $gen->IncNamespacePrefix = 'ec'; $gen->IncNamespaceUri = 'http://www.w3.org/2001/10/xml-exc-c14n#'; $gen->SignedInfoCanonAlg = 'EXCL_C14N'; $gen->SignedInfoDigestMethod = 'sha256'; // Set the KeyInfoId before adding references.. $gen->KeyInfoId = $keyInfoId->getAsString(); // -------- Reference 1 -------- // For versions of Chilkat < 10.0.0, use new COM('Chilkat_9_5_0.Chilkat.Xml') $xml1 = new COM("Chilkat.Xml"); $xml1->Tag = 'ds:Transforms'; $xml1->UpdateAttrAt('ds:Transform',1,'Algorithm','http://www.w3.org/2001/10/xml-exc-c14n#'); $xml1->UpdateAttrAt('ds:Transform|ec:InclusiveNamespaces',1,'PrefixList','wsse soapenv v1'); $xml1->UpdateAttrAt('ds:Transform|ec:InclusiveNamespaces',1,'xmlns:ec','http://www.w3.org/2001/10/xml-exc-c14n#'); $gen->AddSameDocRef2($tsId->getAsString(),'sha256',$xml1,''); // -------- Reference 2 -------- // For versions of Chilkat < 10.0.0, use new COM('Chilkat_9_5_0.Chilkat.Xml') $xml2 = new COM("Chilkat.Xml"); $xml2->Tag = 'ds:Transforms'; $xml2->UpdateAttrAt('ds:Transform',1,'Algorithm','http://www.w3.org/2001/10/xml-exc-c14n#'); $xml2->UpdateAttrAt('ds:Transform|ec:InclusiveNamespaces',1,'PrefixList','v1'); $xml2->UpdateAttrAt('ds:Transform|ec:InclusiveNamespaces',1,'xmlns:ec','http://www.w3.org/2001/10/xml-exc-c14n#'); $gen->AddSameDocRef2('id-8','sha256',$xml2,''); // Provide a certificate + private key. (PFX password is test123) // For versions of Chilkat < 10.0.0, use new COM('Chilkat_9_5_0.Chilkat.Cert') $cert = new COM("Chilkat.Cert"); $success = $cert->LoadPfxFile('qa_data/pfx/cert_test123.pfx','test123'); if ($success != 1) { print $cert->LastErrorText . "\n"; exit; } $gen->SetX509Cert($cert,1); $gen->KeyInfoType = 'Custom'; // Create the custom KeyInfo XML.. // For versions of Chilkat < 10.0.0, use new COM('Chilkat_9_5_0.Chilkat.Xml') $xmlCustomKeyInfo = new COM("Chilkat.Xml"); $xmlCustomKeyInfo->Tag = 'wsse:SecurityTokenReference'; $xmlCustomKeyInfo->AddAttribute('wsu:Id',$strId->getAsString()); $xmlCustomKeyInfo->UpdateAttrAt('wsse:KeyIdentifier',1,'EncodingType','http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary'); $xmlCustomKeyInfo->UpdateAttrAt('wsse:KeyIdentifier',1,'ValueType','http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3'); // Insert the single-line base64 of the signing certificate's DER $cert->UncommonOptions = 'Base64CertNoCRLF'; $xmlCustomKeyInfo->UpdateChildContent('wsse:KeyIdentifier',$cert->getEncoded()); $xmlCustomKeyInfo->EmitXmlDecl = 0; $gen->CustomKeyInfoXml = $xmlCustomKeyInfo->getXml(); // Load XML to be signed... // For versions of Chilkat < 10.0.0, use new COM('Chilkat_9_5_0.Chilkat.StringBuilder') $sbXml = new COM("Chilkat.StringBuilder"); $xmlToSign->GetXmlSb($sbXml); $gen->Behaviors = 'IndentedSignature'; // Sign the XML... $gen->VerboseLogging = 1; $success = $gen->CreateXmlDSigSb($sbXml); if ($success != 1) { print $gen->LastErrorText . "\n"; exit; } // Save the signed XML to a file. $success = $sbXml->WriteFile('c:/temp/qa_output/signedXml.xml','utf-8',0); print $sbXml->getAsString() . "\n"; ?> |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.