rnijveld/xmlseclibs
Fork of xmlseclibs for working with XML Digital Signatures and XML Encryption in PHP. Provides tools to sign, verify, encrypt, and decrypt XML documents (XMLDSig/XMLENC) commonly used in SAML, SOAP, and other security-focused XML workflows.
Installation:
composer require rnijveld/xmlseclibs
Add to composer.json if not auto-loaded:
"autoload": {
"psr-4": {
"App\\": "app/",
"Rn\\Xml\\": "vendor/rnijveld/xmlseclibs/src/"
}
}
Run composer dump-autoload.
First Use Case: Sign an XML document:
use Rn\Xml\Security\Keys\Key;
use Rn\Xml\Security\Signer;
$key = new Key('-----BEGIN PRIVATE KEY-----MII...', '-----BEGIN CERTIFICATE-----MII...');
$signer = new Signer($key);
$signedXml = $signer->signXmlString($xmlString, 'id-of-element-to-sign');
Where to Look First:
tests/ directory for usage examples.Rn\Xml\Security namespace for core classes.Signing XML:
$key = new Key($privateKey, $certificate);
$signer = new Signer($key, ['signatureAlgorithm' => 'rsa-sha256']);
$signedXml = $signer->signXmlString($xml, 'elementId');
Verifying XML:
$verifier = new Verifier();
$result = $verifier->verifyXmlString($signedXml, $publicKey);
Encrypting/Decrypting:
$encryptor = new Encryptor($key);
$encrypted = $encryptor->encryptXmlString($xml);
$decrypted = $encryptor->decryptXmlString($encrypted);
Laravel Facades (Optional): Create a facade for cleaner syntax:
// app/XmlSecurityFacade.php
namespace App\Facades;
use Rn\Xml\Security\Signer;
use Illuminate\Support\Facades\Facade;
class XmlSecurityFacade extends Facade {
protected static function getFacadeAccessor() { return 'xml.security'; }
}
Register in config/app.php:
'xml.security' => \App\Services\XmlSecurityService::class,
Service Container Binding:
// app/Providers/AppServiceProvider.php
public function register() {
$this->app->bind('xml.security', function() {
return new Signer(new Key($privateKey, $certificate));
});
}
Middleware for API Security:
// app/Http/Middleware/VerifyXmlSignature.php
public function handle($request, Closure $next) {
$verifier = new Verifier();
if (!$verifier->verifyXmlString($request->xml, $publicKey)) {
abort(403, 'Invalid signature');
}
return $next($request);
}
Key Format:
XML Structure:
id attribute for signing must be unique in the XML.Performance:
SimpleXML with libxml_disable_entity_loader) can help.Namespace Conflicts:
x509v3 extensions).Enable Verbose Output:
$signer = new Signer($key, ['verbose' => true]);
Logs canonicalized XML and signing steps.
Check Canonicalization:
Compare outputs of canonicalizeXmlString() to isolate issues.
Validate Keys:
$key->validate(); // Throws exception if key is invalid.
Custom Canonicalization:
Override Rn\Xml\Security\Canonicalizer for non-standard XML processing.
Key Management:
Extend Rn\Xml\Security\Keys\Key to support additional formats (e.g., PKCS#11).
Event Hooks: Use Laravel events to log/signature operations:
event(new XmlSigned($signedXml, $key));
Default Algorithms: Override defaults in the constructor:
$signer = new Signer($key, [
'signatureAlgorithm' => 'rsa-sha512',
'digestAlgorithm' => 'sha512'
]);
Key Passwords: For encrypted keys, use:
$key = new Key($privateKey, $certificate, 'password');
How can I help you explore Laravel packages today?