PHP code example of tourze / tls-handshake-messages

1. Go to this page and download the library: Download tourze/tls-handshake-messages 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/ */

    

tourze / tls-handshake-messages example snippets


use Tourze\TLSHandshakeMessages\Message\ClientHelloMessage;

// Create a new ClientHello message
$clientHello = new ClientHelloMessage();
$clientHello->setVersion(0x0303); // TLS 1.2
$clientHello->setRandom(random_bytes(32));
$clientHello->setSessionId('');
$clientHello->setCipherSuites([0x1301, 0x1302]); // TLS 1.3 cipher suites
$clientHello->setCompressionMethods([0x00]);

// Add extensions
$clientHello->addExtension(0, hex2bin('00000e7777772e676f6f676c652e636f6d')); // server_name

// Serialize to binary
$binaryData = $clientHello->encode();

// Deserialize from binary
$decodedMessage = ClientHelloMessage::decode($binaryData);

use Tourze\TLSHandshakeMessages\Message\CertificateMessage;

// Create certificate message with chain
$certificate = new CertificateMessage();
$certificate->setCertificateChain([
    $serverCertificate,
    $intermediateCertificate,
    $rootCertificate
]);

// Or add certificates one by one
$certificate->addCertificate($serverCertificate);
$certificate->addCertificate($intermediateCertificate);

// Validate certificate message
if ($certificate->isValid()) {
    // Process certificate chain
    $chain = $certificate->getCertificateChain();
}

// All messages implement validation
if ($message->isValid()) {
    // Message is properly formatted
    $length = $message->getLength();
    $type = $message->getType();
}

use Tourze\TLSHandshakeMessages\Protocol\MessageCompatibilityHandler;

// Adapt message to different TLS versions
$tls12Message = MessageCompatibilityHandler::adaptMessageToVersion(
    $originalMessage,
    MessageCompatibilityHandler::TLS_VERSION_1_2
);

// Check compatibility
if (MessageCompatibilityHandler::isMessageCompatibleWithVersion($message, MessageCompatibilityHandler::TLS_VERSION_1_3)) {
    // Message is compatible with TLS 1.3
}

use Tourze\TLSHandshakeMessages\Protocol\HandshakeMessageType;

$type = HandshakeMessageType::CLIENT_HELLO;

use Tourze\TLSHandshakeMessages\Exception\InvalidMessageException;

try {
    $message = ClientHelloMessage::decode($invalidData);
} catch (InvalidMessageException $e) {
    // Handle invalid message format
}