Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for TLS ALPN to connect via MQTT protocol to a TLS encrypted HTTP port 443 #181

Merged
merged 4 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@ $connectionSettings = (new \PhpMqtt\Client\ConnectionSettings)
// This option requires ConnectionSettings::setTlsClientCertificateFile() and
// ConnectionSettings::setTlsClientCertificateKeyFile() to be used as well.
->setTlsClientCertificateKeyPassphrase(null);

// The TLS ALPN is used to establish a TLS encrypted mqtt connection on port 443,
// which usually is reserved for TLS encrypted HTTP traffic.
->setTlsAlpn(null);
```

## Features
Expand Down
22 changes: 22 additions & 0 deletions src/ConnectionSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class ConnectionSettings
private ?string $tlsClientCertificateFile = null;
private ?string $tlsClientCertificateKeyFile = null;
private ?string $tlsClientCertificateKeyPassphrase = null;
private ?string $tlsAlpn = null;

/**
* The username used for authentication when connecting to the broker.
Expand Down Expand Up @@ -531,4 +532,25 @@ public function getTlsClientCertificateKeyPassphrase(): ?string
{
return $this->tlsClientCertificateKeyPassphrase;
}

/**
* The TLS ALPN is used to establish a TLS encrypted mqtt connection on port 443,
* which usually is reserved for TLS encrypted HTTP traffic.
*
* @return ConnectionSettings A copy of the original object with the new setting applied.
*/
public function setTlsAlpn(?string $tlsAlpn): ConnectionSettings
{
$copy = clone $this;

$copy->tlsAlpn = $tlsAlpn;

return $copy;
}

public function getTlsAlpn(): ?string
{
return $this->tlsAlpn;
}

}
4 changes: 4 additions & 0 deletions src/MqttClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ protected function establishSocketConnection(): void
$tlsOptions['passphrase'] = $this->settings->getTlsClientCertificateKeyPassphrase();
}

if ($this->settings->getTlsAlpn() !== null) {
$tlsOptions['alpn_protocols'] = $this->settings->getTlsAlpn();
}

$contextOptions['ssl'] = $tlsOptions;
}

Expand Down
Loading