Skip to content

Commit 37dfd6b

Browse files
author
William Lam
committed
Update TLS self-sign handling for PS Core
1 parent e86c0fb commit 37dfd6b

File tree

1 file changed

+90
-24
lines changed

1 file changed

+90
-24
lines changed

powershell/GlobalPermissions.ps1

Lines changed: 90 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,52 @@ Function New-GlobalPermission {
3232
# vSphere MOB URL to private enableMethods
3333
$mob_url = "https://$vc_server/invsvc/mob3/?moid=authorizationService&method=AuthorizationService.AddGlobalAccessControlList"
3434

35-
# Ingore SSL Warnings
36-
add-type -TypeDefinition @"
37-
using System.Net;
38-
using System.Security.Cryptography.X509Certificates;
39-
public class TrustAllCertsPolicy : ICertificatePolicy {
40-
public bool CheckValidationResult(
41-
ServicePoint srvPoint, X509Certificate certificate,
42-
WebRequest request, int certificateProblem) {
35+
$Code = @'
36+
using System;
37+
using System.Collections.Generic;
38+
using System.Net.Http;
39+
using System.Net.Security;
40+
using System.Security.Cryptography.X509Certificates;
41+
42+
namespace CertificateCapture
43+
{
44+
public class Utility
45+
{
46+
public static Func<HttpRequestMessage,X509Certificate2,X509Chain,SslPolicyErrors,Boolean> ValidationCallback =
47+
(message, cert, chain, errors) => {
48+
var newCert = new X509Certificate2(cert);
49+
var newChain = new X509Chain();
50+
newChain.Build(newCert);
51+
CapturedCertificates.Add(new CapturedCertificate(){
52+
Certificate = newCert,
53+
CertificateChain = newChain,
54+
PolicyErrors = errors,
55+
URI = message.RequestUri
56+
});
4357
return true;
44-
}
45-
}
46-
"@
47-
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
58+
};
59+
public static List<CapturedCertificate> CapturedCertificates = new List<CapturedCertificate>();
60+
}
61+
62+
public class CapturedCertificate
63+
{
64+
public X509Certificate2 Certificate { get; set; }
65+
public X509Chain CertificateChain { get; set; }
66+
public SslPolicyErrors PolicyErrors { get; set; }
67+
public Uri URI { get; set; }
68+
}
69+
}
70+
'@
71+
if ($PSEdition -ne 'Core'){
72+
Add-Type -AssemblyName System.Net.Http
73+
if (-not ("CertificateCapture" -as [type])) {
74+
Add-Type $Code -ReferencedAssemblies System.Net.Http
75+
}
76+
} else {
77+
if (-not ("CertificateCapture" -as [type])) {
78+
Add-Type $Code
79+
}
80+
}
4881

4982
# Initial login to vSphere MOB using GET and store session using $vmware variable
5083
$results = Invoke-WebRequest -Uri $mob_url -SessionVariable vmware -Credential $credential -Method GET
@@ -105,19 +138,52 @@ Function Remove-GlobalPermission {
105138
# vSphere MOB URL to private enableMethods
106139
$mob_url = "https://$vc_server/invsvc/mob3/?moid=authorizationService&method=AuthorizationService.RemoveGlobalAccess"
107140

108-
# Ingore SSL Warnings
109-
add-type -TypeDefinition @"
110-
using System.Net;
111-
using System.Security.Cryptography.X509Certificates;
112-
public class TrustAllCertsPolicy : ICertificatePolicy {
113-
public bool CheckValidationResult(
114-
ServicePoint srvPoint, X509Certificate certificate,
115-
WebRequest request, int certificateProblem) {
141+
$Code = @'
142+
using System;
143+
using System.Collections.Generic;
144+
using System.Net.Http;
145+
using System.Net.Security;
146+
using System.Security.Cryptography.X509Certificates;
147+
148+
namespace CertificateCapture
149+
{
150+
public class Utility
151+
{
152+
public static Func<HttpRequestMessage,X509Certificate2,X509Chain,SslPolicyErrors,Boolean> ValidationCallback =
153+
(message, cert, chain, errors) => {
154+
var newCert = new X509Certificate2(cert);
155+
var newChain = new X509Chain();
156+
newChain.Build(newCert);
157+
CapturedCertificates.Add(new CapturedCertificate(){
158+
Certificate = newCert,
159+
CertificateChain = newChain,
160+
PolicyErrors = errors,
161+
URI = message.RequestUri
162+
});
116163
return true;
117-
}
118-
}
119-
"@
120-
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
164+
};
165+
public static List<CapturedCertificate> CapturedCertificates = new List<CapturedCertificate>();
166+
}
167+
168+
public class CapturedCertificate
169+
{
170+
public X509Certificate2 Certificate { get; set; }
171+
public X509Chain CertificateChain { get; set; }
172+
public SslPolicyErrors PolicyErrors { get; set; }
173+
public Uri URI { get; set; }
174+
}
175+
}
176+
'@
177+
if ($PSEdition -ne 'Core'){
178+
Add-Type -AssemblyName System.Net.Http
179+
if (-not ("CertificateCapture" -as [type])) {
180+
Add-Type $Code -ReferencedAssemblies System.Net.Http
181+
}
182+
} else {
183+
if (-not ("CertificateCapture" -as [type])) {
184+
Add-Type $Code
185+
}
186+
}
121187

122188
# Initial login to vSphere MOB using GET and store session using $vmware variable
123189
$results = Invoke-WebRequest -Uri $mob_url -SessionVariable vmware -Credential $credential -Method GET

0 commit comments

Comments
 (0)