Repository navigation
Expand file tree
/
Copy pathOpen-SSLStream.ps1
More file actions
122 lines (102 loc) · 5.51 KB
/
Copy pathOpen-SSLStream.ps1
File metadata and controls
122 lines (102 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<#
.SYNOPSIS
Create and authenticate an SSL stream for the active TCP client.
.DESCRIPTION
`Open-SSLStream` wraps the active TCP client's network stream in an
`SslStream`, configures timeouts, and performs client or server authentication
using the thread-local TLS certificate. On failure it closes the TCP connection.
.PARAMETER Endpoint
| Name | Type | Description | Default |
|----------|---------------|----------------------------------------------------------|---------|
| Endpoint | System.String | Expected remote endpoint name (required for client auth) | $null |
.PARAMETER IsClient
| Name | Type | Description | Default |
|----------|------------------------|---------------------------------------------|---------|
| IsClient | System.SwitchParameter | When set perform client-side authentication | $false |
.OUTPUTS
None -- Stores the opened SslStream in the global state; result codes are reported via `Write-FunctionReturnValue`.
.EXAMPLE
PS> Open-SSLStream -Endpoint 'example.com' -IsClient
.NOTES
Requires a valid certificate in `(Get-GlobalRuntimeVariable).Private.TCP.Certificate`.
SPDX-FileCopyrightText: 2026 Icinga GmbH <https://icinga.com>
SPDX-License-Identifier: GPL-3.0-or-later
#>
function Open-SSLStream()
{
param (
[string]$Endpoint = $null,
[switch]$IsClient = $false
);
$GlobalVariable = Get-GlobalRuntimeVariable;
if ($null -eq $GlobalVariable.Private.TCP.Client -Or $GlobalVariable.Private.TCP.Client.Connected -eq $false) {
return (Write-FunctionReturnValue -ReturnValue 'SslStreamClientInvalid' -Data @{ 'Endpoint' = $Endpoint; 'IsClient' = $IsClient });
}
if ($null -ne $GlobalVariable.Private.TCP.Stream) {
return (Write-FunctionReturnValue -ReturnValue 'SslStreamAlreadyOpened' -Data @{ 'Endpoint' = $Endpoint; 'IsClient' = $IsClient });
}
# The LocalCertificateSelectionCallback bypasses .NET's issuer-matching logic, which would
# otherwise silently drop our certificate when the remote side's CertificateRequest lists
# a CA that doesn't match our cert's issuer chain (common with self-signed Icinga PKI).
# [IcingaIgnoreDisposeCheck]
$sslStream = New-Object System.Net.Security.SslStream(
$GlobalVariable.Private.TCP.Client.GetStream(),
$false,
$null,
([System.Net.Security.LocalCertificateSelectionCallback]{
# $args[2] is LocalCertificates (LocalCertificateSelectionCallback: sender, targetHost, localCertificates, remoteCertificate, acceptableIssuers)
if ($null -ne $args[2] -and $args[2].Count -gt 0) {
return $args[2][0];
}
return $null;
})
);
Write-LoggingEntry -Message 'Successfully created TLS stream for endpoint' -Data @{
'Endpoint' = $Endpoint;
'Client' = $GlobalVariable.Private.TCP.Client;
} -Level 'Debug';
# Set read timeout to prevent hanging
$sslStream.ReadTimeout = $GlobalVariable.Public.Config.TCP.ReadTimeout;
$sslStream.WriteTimeout = $GlobalVariable.Public.Config.TCP.WriteTimeout;
# Read the TLS certificate from the workers private storage
$SSLCert = $GlobalVariable.Private.TCP.Certificate;
if (-not $SSLCert) {
# Close the TCP connection if no TLS cert was found
Close-TCPConnection;
return (Write-FunctionReturnValue -ReturnValue 'TlsCertNotFound' -Data @{ 'Endpoint' = $Endpoint; 'IsClient' = $IsClient });
}
if ($IsClient) {
# The client must provide the endpoint name for Ssl authentication, to ensure the server certificate matches
# Only required if we are acting as a client
if ([string]::IsNullOrEmpty($Endpoint)) {
Close-TCPConnection;
return (Write-FunctionReturnValue -ReturnValue 'ProtocolMissingEndpoint' -Data @{ 'Endpoint' = $Endpoint; 'IsClient' = $IsClient });
}
try {
$certCollection = New-Object System.Security.Cryptography.X509Certificates.X509CertificateCollection;
$certCollection.Add($SSLCert) | Out-Null;
$sslStream.AuthenticateAsClient($Endpoint, $certCollection, $GlobalVariable.Public.Config.TCP.TLS.Protocols, $false);
} catch {
Close-TCPConnection;
return (Write-FunctionReturnValue -ReturnValue 'TlsHandshakeAsClientFailed' -Data @{ 'ExceptionMessage' = $_.Exception.Message; 'Endpoint' = $Endpoint; 'IsClient' = $IsClient; 'CertificateCollection' = $certCollection });
}
} else {
try {
$sslStream.AuthenticateAsServer($SSLCert, $false, $GlobalVariable.Public.Config.TCP.TLS.Protocols, $false);
} catch {
Close-TCPConnection;
return (Write-FunctionReturnValue -ReturnValue 'TlsHandshakeAsServerFailed' -Data @{ 'ExceptionMessage' = $_.Exception.Message; 'Endpoint' = $Endpoint; 'IsClient' = $IsClient });
}
}
Write-LoggingEntry `
-Message 'Successfully created TLS handshake for endpoint' `
-Data @{
'Endpoint' = $Endpoint;
'SslProtocol' = $sslStream.SslProtocol;
'CipherAlgorithm' = $sslStream.CipherAlgorithm;
'CipherStrength' = $sslStream.CipherStrength;
'HashAlgorithm' = $sslStream.HashAlgorithm;
} -Level 'Debug';
$GlobalVariable.Private.TCP.Stream = $sslStream;
return (Write-FunctionReturnValue -ReturnValue 'TlsHandshakeSuccessful' -Data @{ 'Endpoint' = $Endpoint; 'IsClient' = $IsClient });
}