PHP Classes
elePHPant
Icontem

PHP XML Signature: Parse and create XML documents signed digitally

Recommend this page to a friend!
  Info   View files Documentation   View files View files (159)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2019-11-06 (2 months ago) RSS 2.0 feedNot yet rated by the usersTotal: 81 This week: 5All time: 9,441 This week: 59Up
Version License PHP version Categories
dsigsdk 1.0.0GNU Lesser Genera...5XML, PHP 5, Cryptography
Description Author

This package can be used to parse and create XML documents signed digitally.

It can take as parameters a string of a given XML document and parse it.

The package can also use cryptographic keys to sign parts of the XML by adding elements that allow that the document to be verified later to check if it was signed by the owner of the keys.

The resulting signed document can be saved back to a XML string or a DOM node for further processing of the XML document structure.

Innovation Award
PHP Programming Innovation award nominee
November 2019
Number 6
XML is a format that sometimes is used to create documents that need to be digitally signed to be used in situations that require that a person or a company is identified as author of the document information.

This package provides a solution that can parse existing XML documents and sign the relevant parts of the documents with a cryptographic key that belongs to the person or company, thus establishing the authorship of the document in a way that it can be verified with legal validity.

Manuel Lemos
  Performance   Level  
Name: Kjell-Inge Gustafsson <contact>
Classes: 6 packages by
Country: Sweden Sweden
Innovation award
Innovation award
Nominee: 2x

 

Details

DsigSdk

  • PHP SDK of XML Digital Signature recomendation
  • based on the [XSD] schema

and provide

For help finding a good PHP cryptography library, please review * Choosing the Right Cryptography Library for your PHP Project: A Guide

Usage, parse XML

To parse an Dsig (Signature root) XML file (using XMLReader) :

<?php
namespace Kigkonsult\DsigSdk;
use Kigkonsult\DsigSdk\XMLParse\DsigParser;

$dsig = DsigParser::factory()->parse( 
    file_get_contents( 'DsigFile.xml' )
);

$signedInfo = $dsig->getsignedInfo();
...

The XML parser save the XMLreader node properties (baseURI, localName, name, namespaceURI, prefix) for each XML (Dto) element as 'XMLattributes' as well as XML attributes (xmlns, xmlns:*, schemaLocation), if set (more info below).

'any' [XSD] elements are accepted as 'Anytype' object instances (more info below, 'AnyType').

Usage, build up structure

To build up dsig structure:

<?php
namespace Kigkonsult\DsigSdk;
use Kigkonsult\DsigSdk\Dto\AnyType;
use Kigkonsult\DsigSdk\Dto\CanonicalizationMethodType;
use Kigkonsult\DsigSdk\Dto\KeyInfoType;
use Kigkonsult\DsigSdk\Dto\SignedInfoType;
use Kigkonsult\DsigSdk\Dto\SignatureType;
use Kigkonsult\DsigSdk\Dto\SignatureValueType;

$dsig = SignatureType::factory()
    ->setSignedInfo( 
        SignedInfoType::factory()
            ->setCanonicalizationMethod(
                CanonicalizationMethodType::factory()
                    ->setAlgorithm( SignatureType::MINICANONICAL )
                    ->setAny( [
                        AnyType::factory()
                            ->setElementName( 'nonSchemaElement1')
                            ->setAttributes( [
                                'id' => '12345' 
                                ] )
                             ->setContent( 'Lr1mKGxP7VAgMB...' ),
                        AnyType::factory()
                            ->setElementName( 'nonSchemaElement2')
                            ->setSubElements( [
                                AnyType::factory()
                                    ->setElementName( 'nonSchemaElement3')
                                    ->setContent( 'Lr1mKGxP7VAgMB...' ),
                            ] )
                        ]
                    )
            )
    )
    ->setSignatureValue(
        SignatureValueType::factory()
            ->setSignatureValueType( 'vgGZnRlm8...' )
    )
    ->setKeyInfo(
        KeyInfoType::factory()
            ->setKeyInfoType( [
                [                 // one set of elements
                    [             // element
                        SignatureType::X509DATA => 
                            X509DataType::factory()
                                ->setX509Certificate( ... )
                    ],
                ],
        ] )
    )
    ->setObject(
        ...
    )
    ...
XML attributes

You can set (single 'element') XMLattribute using

$dsig->setXMLAttribut( <key>, <value> );

To set (ex. prefix) and 'propagate' down in hierarchy:

$dsig->setXMLAttribut( SignatureType::PREFIX, <value>, true );

You can remove (single 'element') XMLattribute using

$dsig->unsetXMLAttribut( <key> );

To unset (ex. prefix) and 'propagate' down in hierarchy:

$dsig->unsetXMLAttribut( SignatureType::PREFIX, true );

To fetch and iterate over XMLAttributes

foreach( $dsig->getXMLAttributes() as $key => $value {
    ...
}

Anytype

Anytype object instances are used for 'any' [XSD] elements. The element name are stored and fetched with

$anytype->setElementName( <name> );
$anytypeName = $anytype->getElementName();

The 'any' [XSD] element attributes may include XML attributes.

The AnyType attributes are stored and fetched as array.

$anytype->setAttributes( [ <key> => <value> ] );
foreach( $anytype->getAttributes() as $key => $value {
    ...
}

Note, an AnyType instance may have * content * type string, * AnyType::setContent() * AnyType::getContent()

or * sub-elements type array [AnyType] * AnyType::setSubElements() * AnyType::getSubElements()

but not both.

Usage, output as XML

DsigSdk uses XMLWriter creating output.

$XMLstring = DsigWriter::factory()->write( $dsig );

The XMLwriter adds for each element * element name with prefix, if exists XMLattribute xmlns, xmlns: and schemaLocation, if exists.

Usage, output as DomNode

$domNode = DsigWriter::factory()->write( $dsig, true );

Info

For class structure and architecture, please review * the [XSD] * docs/Dsig.png class design * the src/DsigLoader directory

You may find convenient constants in - src/DsigInterface - src/XMLAttributesInterface

For base64Encode/base64Decode/hash support, please review src/Impl/Impl.md

Installation

[Composer]

From the Command Line:

composer require kigkonsult/dsigsdk

In your composer.json:

{
    "require": {
        "kigkonsult/dsigsdk": "dev-master"
    }
}

Acquire access

namespace Kigkonsult\DsigSdk;
...
include 'vendor/autoload.php';

Run tests

cd pathToSource/DsigSdk
vendor/bin/phpunit

Or

Download and acquire..

namepace Kigkonsult\DsigSdk;
...
include 'pathToSource/DsigSdk/autoload.php';

Support

For support, please use [Github]/issues.

License

This project is licensed under the LGPLv3 License

[Composer]:https://getcomposer.org/ [Github]:https://github.com/iCalcreator/dsigsdk/issues [http://www.w3.org/2000/09/xmldsig#]:http://www.w3.org/2000/09/xmldsig# [XSD]:https://www.w3.org/TR/2002/REC-xmldsig-core-20020212/xmldsig-core-schema.xsd

  Files folder image Files  
File Role Description
Files folder imagedocs (6 files)
Files folder imagesrc (4 files, 5 directories)
Files folder imagetest (2 files, 1 directory)
Accessible without login Plain text file autoload.php Aux. Auxiliary script
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Read me

  Files folder image Files  /  docs  
File Role Description
  Accessible without login Image file Dsig.png Data Auxiliary data
  Accessible without login Plain text file lgpl.txt Doc. Documentation
  Accessible without login Plain text file README.Impl.txt Doc. Documentation
  Accessible without login Plain text file README.txt Doc. Documentation
  Accessible without login Plain text file rfc4051.txt Doc. Documentation
  Accessible without login Plain text file xmldsig-core-schema.xsd Data Auxiliary data

  Files folder image Files  /  src  
File Role Description
Files folder imageDsigLoader (26 files)
Files folder imageDto (46 files)
Files folder imageImpl (14 files)
Files folder imageXMLParse (26 files)
Files folder imageXMLWrite (25 files)
  Plain text file DsigBase.php Class Class source
  Plain text file DsigIdentifiersInterface.php Class Class source
  Plain text file DsigInterface.php Class Class source
  Plain text file XMLAttributesInterface.php Class Class source

  Files folder image Files  /  src  /  DsigLoader  
File Role Description
  Plain text file AnyType.php Class Class source
  Plain text file CanonicalizationMethodType.php Class Class source
  Plain text file DigestMethodType.php Class Class source
  Plain text file DSAKeyValueType.php Class Class source
  Plain text file DsigLoaderInterface.php Class Class source
  Plain text file KeyInfoType.php Class Class source
  Plain text file KeyValueType.php Class Class source
  Plain text file ManifestType.php Class Class source
  Plain text file ObjectType.php Class Class source
  Plain text file PGPDataType.php Class Class source
  Plain text file ReferenceType.php Class Class source
  Plain text file RetrievalMethodType.php Class Class source
  Plain text file RSAKeyValueType.php Class Class source
  Plain text file SignatureMethodType.php Class Class source
  Plain text file SignaturePropertiesType.php Class Class source
  Plain text file SignaturePropertyType.php Class Class source
  Plain text file SignatureType.php Class Class source
  Plain text file SignatureType1.php Class Class source
  Plain text file SignatureType2.php Class Class source
  Plain text file SignatureValueType.php Class Class source
  Plain text file SignedInfoType.php Class Class source
  Plain text file SPKIDataType.php Class Class source
  Plain text file TransformsType.php Class Class source
  Plain text file TransformType.php Class Class source
  Plain text file X509DataType.php Class Class source
  Plain text file X509IssuerSerialType.php Class Class source

  Files folder image Files  /  src  /  Dto  
File Role Description
  Plain text file Any.php Class Class source
  Plain text file AnyType.php Class Class source
  Plain text file CanonicalizationMethod.php Class Class source
  Plain text file CanonicalizationMethodType.php Class Class source
  Plain text file DigestMethod.php Class Class source
  Plain text file DigestMethodType.php Class Class source
  Plain text file DSAKeyValue.php Class Class source
  Plain text file DSAKeyValueType.php Class Class source
  Plain text file DsigBase.php Class Class source
  Plain text file KeyInfo.php Class Class source
  Plain text file KeyInfoType.php Class Class source
  Plain text file KeyValue.php Class Class source
  Plain text file KeyValueType.php Class Class source
  Plain text file Manifest.php Class Class source
  Plain text file ManifestType.php Class Class source
  Plain text file Object.php Class Class source
  Plain text file ObjectType.php Class Class source
  Plain text file PGPData.php Class Class source
  Plain text file PGPDataType.php Class Class source
  Plain text file Reference.php Class Class source
  Plain text file ReferenceType.php Class Class source
  Plain text file RetrievalMethod.php Class Class source
  Plain text file RetrievalMethodType.php Class Class source
  Plain text file RSAKeyValue.php Class Class source
  Plain text file RSAKeyValueType.php Class Class source
  Plain text file Signature.php Class Class source
  Plain text file SignatureMethod.php Class Class source
  Plain text file SignatureMethodType.php Class Class source
  Plain text file SignatureProperties.php Class Class source
  Plain text file SignaturePropertiesType.php Class Class source
  Plain text file SignatureProperty.php Class Class source
  Plain text file SignaturePropertyType.php Class Class source
  Plain text file SignatureType.php Class Class source
  Plain text file SignatureValue.php Class Class source
  Plain text file SignatureValueType.php Class Class source
  Plain text file SignedInfo.php Class Class source
  Plain text file SignedInfoType.php Class Class source
  Plain text file SPKIData.php Class Class source
  Plain text file SPKIDataType.php Class Class source
  Plain text file Transform.php Class Class source
  Plain text file Transforms.php Class Class source
  Plain text file TransformsType.php Class Class source
  Plain text file TransformType.php Class Class source
  Plain text file X509Data.php Class Class source
  Plain text file X509DataType.php Class Class source
  Plain text file X509IssuerSerialType.php Class Class source

  Files folder image Files  /  src  /  Impl  
File Role Description
  Accessible without login Plain text file Common.md Data Auxiliary data
  Plain text file CommonFactory.php Class Class source
  Accessible without login Plain text file Hash.md Data Auxiliary data
  Plain text file HashFactory.php Class Class source
  Accessible without login Plain text file HmacHash.md Data Auxiliary data
  Plain text file HmacHashFactory.php Class Class source
  Plain text file ImplBase.php Class Class source
  Accessible without login Plain text file Misc.md Data Auxiliary data
  Accessible without login Plain text file OpenSSL.md Data Auxiliary data
  Plain text file OpenSSLBase.php Class Class source
  Plain text file OpenSSLFactory.php Class Class source
  Plain text file OpenSSLInterface.php Class Class source
  Plain text file PhpErrorException.php Class Class source
  Plain text file PKCSFactory.php Class Class source

  Files folder image Files  /  src  /  XMLParse  
File Role Description
  Plain text file AnyTypeParser.php Class Class source
  Plain text file CanonicalizationMethodTypeParser.php Class Class source
  Plain text file DigestMethodTypeParser.php Class Class source
  Plain text file DSAKeyValueTypeParser.php Class Class source
  Plain text file DsigParser.php Class Class source
  Plain text file DsigParserBase.php Class Class source
  Plain text file KeyInfoTypeParser.php Class Class source
  Plain text file KeyValueTypeParser.php Class Class source
  Plain text file LibXmlUtilTrait.php Class Class source
  Plain text file ManifestTypeParser.php Class Class source
  Plain text file ObjectTypeParser.php Class Class source
  Plain text file PGPDataTypeParser.php Class Class source
  Plain text file ReferenceTypeParser.php Class Class source
  Plain text file RetrievalMethodTypeParser.php Class Class source
  Plain text file RSAKeyValueTypeParser.php Class Class source
  Plain text file SignatureMethodTypeParser.php Class Class source
  Plain text file SignaturePropertiesTypeParser.php Class Class source
  Plain text file SignaturePropertyTypeParser.php Class Class source
  Plain text file SignatureTypeParser.php Class Class source
  Plain text file SignatureValueTypeParser.php Class Class source
  Plain text file SignedInfoTypeParser.php Class Class source
  Plain text file SPKIDataTypeParser.php Class Class source
  Plain text file TransformsTypeParser.php Class Class source
  Plain text file TransformTypeParser.php Class Class source
  Plain text file X509DataTypeParser.php Class Class source
  Plain text file X509IssuerSerialTypeParser.php Class Class source

  Files folder image Files  /  src  /  XMLWrite  
File Role Description
  Plain text file AnyTypeWriter.php Class Class source
  Plain text file CanonicalizationMethodTypeWriter.php Class Class source
  Plain text file DigestMethodTypeWriter.php Class Class source
  Plain text file DSAKeyValueTypeWriter.php Class Class source
  Plain text file DsigWriter.php Class Class source
  Plain text file DsigWriterBase.php Class Class source
  Plain text file KeyInfoTypeWriter.php Class Class source
  Plain text file KeyValueTypeWriter.php Class Class source
  Plain text file ManifestTypeWriter.php Class Class source
  Plain text file ObjectTypeWriter.php Class Class source
  Plain text file PGPDataTypeWriter.php Class Class source
  Plain text file ReferenceTypeWriter.php Class Class source
  Plain text file RetrievalMethodTypeWriter.php Class Class source
  Plain text file RSAKeyValueTypeWriter.php Class Class source
  Plain text file SignatureMethodTypeWriter.php Class Class source
  Plain text file SignaturePropertiesTypeWriter.php Class Class source
  Plain text file SignaturePropertyTypeWriter.php Class Class source
  Plain text file SignatureTypeWriter.php Class Class source
  Plain text file SignatureValueTypeWriter.php Class Class source
  Plain text file SignedInfoTypeWriter.php Class Class source
  Plain text file SPKIDataTypeWriter.php Class Class source
  Plain text file TransformsTypeWriter.php Class Class source
  Plain text file TransformTypeWriter.php Class Class source
  Plain text file X509DataTypeWriter.php Class Class source
  Plain text file X509IssuerSerialTypeWriter.php Class Class source

  Files folder image Files  /  test  
File Role Description
Files folder imageImpl (6 files)
  Plain text file BaseTest.php Class Class source
  Plain text file SignatureTest.php Class Class source

  Files folder image Files  /  test  /  Impl  
File Role Description
  Plain text file CommonFactoryTest.php Class Class source
  Plain text file HashFactoryTest.php Class Class source
  Plain text file HmacHashFactoryTest.php Class Class source
  Plain text file OpenSSLBaseTest.php Class Class source
  Plain text file OpenSSLFactoryTest.php Class Class source
  Plain text file PKCSFactoryTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:81
This week:5
All time:9,441
This week:59Up