PHP code example of beziworld / laravel-xades

1. Go to this page and download the library: Download beziworld/laravel-xades library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

beziworld / laravel-xades example snippets


'certificates' => [
    'default' => [...],
    'acme' => [
        'pkcs12' => env('ACME_PKCS12'),
        'password' => env('ACME_PKCS12_PASSWORD'),
    ],
],

use Beziworld\Xades\Facades\Xades;

$signed = Xades::signEnveloped($xml);                     // ds:Signature appended to the root element
$signed = Xades::signEnveloping($xml);                    // the document goes into ds:Object
$signature = Xades::signDetached($pdf, 'faktura.pdf', 'application/pdf');

$signed = Xades::certificate('acme')->signEnveloped($xml); // named certificate

$signed = Xades::signEnveloped($xml, [
    'profile' => 'baseline-b',                     // XAdES-B-B instead of XAdES-BES
    'digest' => 'sha512',
    'canonicalization' => 'c14n',                  // inclusive Canonical XML 1.0
    'signing_time' => now(),
    'claimed_roles' => ['Prezes Zarządu'],
    'commitment_type' => 'http://uri.etsi.org/01903/v1.2.2#ProofOfApproval',
    'mime_type' => 'text/xml',
    'description' => 'Wniosek',
    'id' => 'Signature-1',                         // otherwise a random xades-... identifier
]);

$signer = Xades::using([
    'pkcs12' => $tenant->pkcs12,                   // path or base64
    'password' => decrypt($tenant->pkcs12_password),
]);

$signer = Xades::using(['key' => $keyPem, 'certificate' => $certificatePem, 'passphrase' => $passphrase]);
$signer->signEnveloped($xml);

$request = <<<XML
<?xml version="1.0" encoding="utf-8"

$result = Xades::verify($signedXml);                   // throws InvalidSignature

$result->packaging;           // Packaging::Enveloped, Enveloping or Detached
$result->profile;             // Profile::Bes or Profile::BaselineB
$result->certificate;         // CertificateInfo: subject, issuer, serialNumber, fingerprint, validFrom, validTo
$result->certificate->nip();  // NIP from organizationIdentifier VATPL-... or serialNumber TINPL-...
$result->certificate->pesel();// PESEL from serialNumber PNOPL-...
$result->signingTime;         // CarbonImmutable (UTC) or null
$result->trusted;             // true/false against the trust list, null when no list is configured
$result->coversWholeDocument; // true when the whole document (minus the signature) is signed
$result->signedContents;      // canonical XML of what the signature covers
$result->claimedRoles;
$result->commitmentType;
$result->warnings;            // findings that do not invalidate the signature

Xades::isValid($signedXml);   // bool

Xades::verify($signatureXml, ['detached' => ['faktura.pdf' => $pdfBytes]]);
Xades::verify($signedXml, ['trusted' => [storage_path('ca/issuer.pem')], '

use Beziworld\Xades\Rules\ValidXadesSignature;

$request->validate([
    'document' => ['r.pem')])->withSchema()],
]);

Xades::validateSchema($signedXml);   // throws SchemaViolation with errors(), returns the number of signatures