Skip to content
Open
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
77 changes: 77 additions & 0 deletions SslScripts/VerifySSLCertFromServer.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<#
.SYNOPSIS
This script is used to verify the SSL certificate from the server
#>

$TestMaxRounds = 1000
$SleepDurationInSeconds = 6

$resultPath = ".\result.csv"

$Domain = "api.nuget.org"
$CertSubjectName = "CN=*.nuget.org"
$URLs = @("https://$Domain/v3-registration5-gz-semver2/newtonsoft.json/index.json",
"https://$Domain/v3-flatcontainer/newtonsoft.json/index.json",
"https://$Domain/v3/index.json")

$TestRound = 1
$SuccessedTimes = 0
$FailedTimes = 0
while ($TestRound -le $TestMaxRounds)
{
Write-Host "Round: ", $TestRound

$URL = $URLs | Get-Random
$request = [Net.WebRequest]::Create($URL)

$servicePoint = $request.ServicePoint
# Set "MaxIdleTime" as 0 to ensure that the certificate is refreshed from the server again each round
$servicePoint.MaxIdleTime = 0
Write-Host "ServicePointHash: ", $servicePoint.GetHashCode()

try {
$request.GetResponse().Dispose()
} catch
{

}

$certificate = $request.ServicePoint.Certificate
if ($null -ne $certificate)
{
$subjectName = $certificate.Subject.Split(",")[0]

if ($subjectName -eq $CertSubjectName)
{
$SuccessedTimes = $SuccessedTimes + 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Powershell has both += and ++ operators

}
else
{
$FailedTimes = $FailedTimes + 1

$dnsRecord = (Resolve-DnsName $Domain | where-Object { $_.QueryType -eq "A" })[0]

$date = (Get-Date).ToUniversalTime()
Write-Host $TestRound, $date, $subjectName, $dnsRecord.Name, $dnsRecord.IP4Address, $URL -ForegroundColor red

$log = @(
[pscustomobject]@{
TestRound = $TestRound
Date_UTC = $date
ReturnedCertSubjectName = $subjectName
DNSRecord = $dnsRecord.Name
IP4Address = $dnsRecord.IP4Address
TestURL = $URL
}
)

$log | Export-Csv -Path $resultPath -Append -NoTypeInformation
}
}

$TestRound = $TestRound + 1
Start-Sleep -Seconds $SleepDurationInSeconds
}

Write-Host "Succeeded: ", $SuccessedTimes
Write-Host "Failed: ", $FailedTimes