Skip to content

ServerCertificateValidationCallback has no effect on Invoke-WebRequest #1753

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

Closed
snobu opened this issue Oct 12, 2017 · 5 comments
Closed

ServerCertificateValidationCallback has no effect on Invoke-WebRequest #1753

snobu opened this issue Oct 12, 2017 · 5 comments

Comments

@snobu
Copy link

snobu commented Oct 12, 2017

As stated in Example 2 here,

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $True }

has no effect on Invoke-WebRequest or Invoke-RestMethod, establishing the TLS session will still fail if the remote presents a self-signed certificate.

PS> [System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $True }
PS> Invoke-RestMethod -URI https://selfsigned.badssl.com

Invoke-RestMethod : The underlying connection was closed:
An unexpected error occurred on a send.

At line:1 char:1
+ invoke-restmethod -uri https://selfsigned.badssl.com
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest)
[Invoke-RestMethod],WebException    + FullyQualifiedErrorId : WebCmdletWebResponseException,
Microsoft.PowerShell.Commands.InvokeRestMethodCommand
PS> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.15063.138
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.15063.138
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Looks like it only works when calling straight into the assembly:

$apiReq = [System.Net.HttpWebRequest]::CreateHttp($url)
$apiRes = $apiReq.GetResponse()

The TLS handshake is successful now.

This is not kosher, definitely needs to be addressed.

@thezim
Copy link
Contributor

thezim commented Oct 13, 2017

@snobu This is what I typically use for non-core versions of PowerShell as @markekraus mentioned.

Add-Type @"
    using System.Net;
    using System.Security.Cryptography.X509Certificates;
    public class TrustAllCertsPolicy : ICertificatePolicy {
        public bool CheckValidationResult(
             ServicePoint srvPoint, X509Certificate certificate,
             WebRequest request, int certificateProblem) {
            return true;
        }
    }
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

Perhaps you could verify the above and submit a PR to correct the 5.1 documentation.

@markekraus
Copy link
Contributor

This has been removed from the documentation in #1870

@jfaltys
Copy link

jfaltys commented Jul 9, 2018

Sorry to add on to this. I cannot seem to get it to work in any way whatsoever. Here is my script and output. http returns the expected outcome, but https does not.

$url = "https://10.1.135.20/getxml?location=/Status"
    [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$True}
    $webclient = [System.Net.HttpWebRequest]::CreateHttp($url)
    $webclient.Credentials = Import-Clixml cred.xml
    $response = $webclient.GetResponse()
    $psversiontable
Exception calling "GetResponse" with "0" argument(s): "The underlying connection was closed: An unexpected error occurred on a send."
At line:6 char:5
+     $response = $webclient.GetResponse()
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WebException
 

Name                           Value                                                                                                                                                        
----                           -----                                                                                                                                                        
PSVersion                      5.1.15063.1155                                                                                                                                               
PSEdition                      Desktop                                                                                                                                                      
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                                                      
BuildVersion                   10.0.15063.1155                                                                                                                                              
CLRVersion                     4.0.30319.42000                                                                                                                                              
WSManStackVersion              3.0                                                                                                                                                          
PSRemotingProtocolVersion      2.3                                                                                                                                                          
SerializationVersion           1.1.0.1     

@snobu
Copy link
Author

snobu commented Jul 9, 2018

I can't repro on PSVersion 5.1.17134.112, unfortunately i don't have your exact build handy.
Try this as target URL: https://selfsigned.badssl.com, you should get a HTTP 421 if TLS is successful.

If that works maybe there's something more to it, maybe the TLS version should be higher and that's why it breaks, not because of the chain verify - see this section here for a workaround.

@joeyaiello
Copy link
Contributor

Closing due to changes in #1870

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants