|
| 1 | +#xmlseclibs |
| 2 | + |
| 3 | +xmlseclibs is a library written in PHP for working with XML Encryption and Signatures. |
| 4 | + |
| 5 | +The author of xmlseclibs is Rob Richards. |
| 6 | + |
| 7 | +# Branches |
| 8 | +Both the master and the 2.0 branches are actively maintained. |
| 9 | +* master: Removes mcrypt usage requiring 5.4+ (5.6.24+ recommended for security reasons) |
| 10 | +* 2.0: Contains namespace support requiring 5.3+ |
| 11 | +* 1.4: Contains auto-loader support while also maintaining backwards compatiblity with the older 1.3 version using the xmlseclibs.php file. Supports PHP 5.2+ |
| 12 | + |
| 13 | +# Requirements |
| 14 | + |
| 15 | +xmlseclibs requires PHP version 5.4 or greater. **5.6.24+ recommended for security reasons** |
| 16 | + |
| 17 | + |
| 18 | +## How to Install |
| 19 | + |
| 20 | +Install with [`composer.phar`](http://getcomposer.org). |
| 21 | + |
| 22 | +```sh |
| 23 | +php composer.phar require "robrichards/xmlseclibs" |
| 24 | +``` |
| 25 | + |
| 26 | + |
| 27 | +## Use cases |
| 28 | + |
| 29 | +xmlseclibs is being used in many different software. |
| 30 | + |
| 31 | +* [SimpleSAMLPHP](https://github.com/simplesamlphp/simplesamlphp) |
| 32 | +* [LightSAML](https://github.com/lightsaml/lightsaml) |
| 33 | +* [OneLogin](https://github.com/onelogin/php-saml) |
| 34 | + |
| 35 | +## Basic usage |
| 36 | + |
| 37 | +The example below shows basic usage of xmlseclibs, with a SHA-256 signature. |
| 38 | + |
| 39 | +```php |
| 40 | +use RobRichards\XMLSecLibs\XMLSecurityDSig; |
| 41 | +use RobRichards\XMLSecLibs\XMLSecurityKey; |
| 42 | + |
| 43 | +// Load the XML to be signed |
| 44 | +$doc = new DOMDocument(); |
| 45 | +$doc->load('./path/to/file/tobesigned.xml'); |
| 46 | + |
| 47 | +// Create a new Security object |
| 48 | +$objDSig = new XMLSecurityDSig(); |
| 49 | +// Use the c14n exclusive canonicalization |
| 50 | +$objDSig->setCanonicalMethod(XMLSecurityDSig::EXC_C14N); |
| 51 | +// Sign using SHA-256 |
| 52 | +$objDSig->addReference( |
| 53 | + $doc, |
| 54 | + XMLSecurityDSig::SHA256, |
| 55 | + array('http://www.w3.org/2000/09/xmldsig#enveloped-signature') |
| 56 | +); |
| 57 | + |
| 58 | +// Create a new (private) Security key |
| 59 | +$objKey = new XMLSecurityKey(XMLSecurityKey::RSA_SHA256, array('type'=>'private')); |
| 60 | +/* |
| 61 | +If key has a passphrase, set it using |
| 62 | +$objKey->passphrase = '<passphrase>'; |
| 63 | +*/ |
| 64 | +// Load the private key |
| 65 | +$objKey->loadKey('./path/to/privatekey.pem', TRUE); |
| 66 | + |
| 67 | +// Sign the XML file |
| 68 | +$objDSig->sign($objKey); |
| 69 | + |
| 70 | +// Add the associated public key to the signature |
| 71 | +$objDSig->add509Cert(file_get_contents('./path/to/file/mycert.pem')); |
| 72 | + |
| 73 | +// Append the signature to the XML |
| 74 | +$objDSig->appendSignature($doc->documentElement); |
| 75 | +// Save the signed XML |
| 76 | +$doc->save('./path/to/signed.xml'); |
| 77 | +``` |
| 78 | + |
| 79 | +## How to Contribute |
| 80 | + |
| 81 | +* [Open Issues](https://github.com/robrichards/xmlseclibs/issues) |
| 82 | +* [Open Pull Requests](https://github.com/robrichards/xmlseclibs/pulls) |
| 83 | + |
| 84 | +Mailing List: https://groups.google.com/forum/#!forum/xmlseclibs |
0 commit comments