Preparation: Splitting a digital ID into multiple PEM files

This applies regardless of whether it is a digital ID for emails or a digital ID for PDF documents.

To do so, you have to use the OpenSSL software from the command prompt. On Linux and Macintosh computers this software should always be installed. Owners of Windows computers can download the software from and install it.

If ID.p12 is the PKCS#12 file, you can use the following commands to extract the components into the files key.pem, cert.pem, and chain.pem.

openssl pkcs12 -in ID.p12 -out key.pem -nocerts
openssl pkcs12 -in ID.p12 -out cert.pem -nokeys -clcerts
openssl pkcs12 -in ID.p12 -out chain.pem -nokeys -cacerts

If you are using a newer OpenSSL version (from 3.0), but the digital ID is in traditional format, the above commands will not work, then please use the following commands:

openssl pkcs12 -legacy -in ID.p12 -out key.pem -nocerts
openssl pkcs12 -legacy -in ID.p12 -out cert.pem -nokeys -clcerts
openssl pkcs12 -legacy -in ID.p12 -out chain.pem -nokeys -cacerts

You will be asked for passwords several times because the private key is first unpacked and then packed again. The new password will be needed below as keypassword.

For maximum portability, you should edit all files with a simple text editor:

  • From the file chain.pem you should remove the root certificate. You can recognize the root certificate by the fact that issuer and subject are identical. It is possible that the file is empty afterwards, then please observe the remarks in the guides below.

  • From all files you should remove all comments (the texts outside of the BEGIN...END blocks) and insert empty lines between the blocks.

Setting up a TLS connection with client certificate authentification

Numerous PHP functions accept a stream context. This stream context should contain these details, among others:

  'ssl' => array(
    # verify server certificate
    'verify_peer'  => true,
    'cafile'       => realpath('roots.pem'),
    'verify_depth' => 3, # minimum for DFN-PKI
    # present our own certificate
    'local_cert'   => realpath('client.pem'),
    'passphrase'   => 'keypassword',

client.pem is a PEM file containing the contents of key.pem, cert.pem, and chain.pem in this order, separated by empty lines.

roots.pem is a PEM file containing root certificates and should contain at least the root certificate relevant for the server to be contacted. This file contains all root and CA certificates of the DFN-PKI, further root certificates can be extracted from every WWW browser.

Creating a signed PDF document

If you are using the TCPDF library to create a PDF document, only one additional statement is required in your PHP script, like this:

$pdf=new TCPDF(.....);


  'file://' . realpath('cert.pem'),
  'file://' . realpath('key.pem'),
  realpath('chain.pem') # 
or null



If the file chain.pem is empty, as with the digital IDs of our PDF-CA, realpath('chain.pem') must be replaced with null.

Using realpath() is urgently recommended even if you give absolute paths.

Sending a signed or a signed and encrypted email

After including an additional function signmail() only one additional statement is required in your PHP script just before calling the function mail() to add the signature to the contents already prepared for mail(), and to encrypt the whole mail, like this:

) and mail(

If the file chain.pem is empty, replace 'chain.pem' with null.

If you want to encrypt the email, the files cert1.pem, cert2.pem, ... must contain the certificates of the recipients. You can specify any number of certificates.

If you want to sign only, simply omit the argument or do not specify any certificate: array()

In place of the file names you can give the PEM data directly, then the data must begin with -----BEGIN.

The function signmail() looks under Unix like this, it may freely be used and distributed:

function signmail(
  # own key
    if($key===false)return false;
  # own certificate
    if($cert===false)return false;
  # intermediate CA certificates
  }elseif(is_string($chain) and substr($chain,0,10)!='-----BEGIN'){
    # a single file name
    if($chain===false)return false;
    # no prefix 'file://'
    # collect work file
    foreach($chain as $onechain){
        if($onechain===false)return false;
        if($onechain===false)return false;
  # if encrypting:
  # certificates of recipients
    foreach($rcptcerts as $onecert){
        if($onecert===false)return false;
  # prepare additional headers
  # separate MIME content from other headers
  foreach(explode("\n",trim($additional_headers)) as $line){
      if(in_array(substr($line,0,1),array(" ","\t"))){
  foreach($work as $line){
      # drop, we add our own
  # if no structured body yet, prepare as plain UTF-8 text
    $cont[]='Content-Type: text/plain; charset=utf-8';
    $cont[]='Content-Transfer-Encoding: quoted-printable';
      ? $message
      # assume non-UTF-8 to be ISO-8859-1 or Windows-1252
      : mb_convert_encoding($message,'UTF-8','Windows-1252')
    )) as $line)$body.=strtr(
      # to encode spaces correctly,
      # quoted_printable_encode() requires CRLF line ends
  # work files
  # if given as string, save chain to file
      return false;
  # sign
      # add headers here when only signing
      $encrypt ? null : $head,
    return false;
  # encrypt
    # PHP does not support GCM ciphers, we cannot avoid CBC.
    # Mitigate EFail CBC/CFB Gadget Attack by prepending a header field
    # with random length and random name ('X' + up to 70 characters).
    # 70*log62(256) = 52.099 so use up to 52 random bytes with Base62.
    # 70*log16(256) = 35.000 so use up to 35 random bytes with Base16.
    # But use at least 16*8 = 128 bits of randomness.
          extension_loaded('gmp') # Base62 available?
        # add headers here when also encrypting
      return false;
  # clean up
  # result (call by reference)
  return true;