-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSSLHelper.cs
138 lines (118 loc) · 5.27 KB
/
SSLHelper.cs
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//----------------------------------------------------------------------------------------------
// <copyright file="CertContextHandle.cs" company="Microsoft">
// Copyright (c) Microsoft. All rights reserved.
// </copyright>
//-------------------------------------------------------------------------------------------------
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Cryptography.X509Certificates;
namespace SSLHelper
{
class Program
{
static bool OnRemoteCertificateReceived(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
Console.WriteLine("Remote Certificate Received!");
SslStream stream = (SslStream)sender;
bool ok = true;
if (sslPolicyErrors != 0)
{
Console.WriteLine($"SSL Policy ERRORS!! {sslPolicyErrors.ToString()}");
ok = false;
}
string indent = "";
foreach (var el in chain.ChainElements)
{
var c = (X509Certificate2)el.Certificate;
Console.WriteLine($"{indent}Subject={c.Subject}, Expires: {c.GetExpirationDateString()}, KeyAlgorithm={c.GetKeyAlgorithmParametersString()} Issuer={c.Issuer}");
string filename = c.Subject.Replace("CN=", "").Replace("*", "") + ".cer";
Console.WriteLine($"{indent}Saving Certificate public key data: {filename}");
var certBytes = c.Export(X509ContentType.Cert);
File.WriteAllBytes(filename, certBytes);
var s = el.ChainElementStatus;
foreach (var status in s.Where(x => x.Status != 0))
{
Console.WriteLine($"{indent}Cert Status: {status.Status}, {status.StatusInformation}");
ok = false;
}
Console.WriteLine();
indent += " ";
}
bool buildsOk = chain.Build((X509Certificate2)certificate);
if (!buildsOk)
{
Console.WriteLine("Building Certificate chain failed!");
ok = false;
}
foreach (var status in chain.ChainStatus)
{
Console.WriteLine($"Chain Status: {status.Status}, {status.StatusInformation}");
}
if (ok) Console.WriteLine("No errors - certificate validation passed!");
return ok;
}
static X509Certificate OnClientCertificateRequested(
object sender,
string targetHost,
X509CertificateCollection localCertificates,
X509Certificate remoteCertificate,
string[] acceptableIssuers)
{
Console.WriteLine("Not using a client certificate");
return null;
}
static void Main(string[] args)
{
if (args.Length == 0)
{
Console.WriteLine("Usage: SSLHelper [hostname] [port]");
Console.WriteLine("port defaults to 443 if not specified");
return;
}
string hostname = args[0];
int port = args.Length == 1 ? 443 : int.Parse(args[1]);
var addr = Dns.GetHostAddresses(hostname).First();
Console.WriteLine($"DNS resolved {hostname} to address {addr}");
IPEndPoint remoteEP = new IPEndPoint(addr, port);
Console.WriteLine($"Creating TCP Socket and NetworkStream");
Socket s;
Stream tcpStream;
try
{
s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Connect(remoteEP);
tcpStream = new NetworkStream(s, ownsSocket: true);
}
catch (Exception e)
{
Console.WriteLine($"Network Stream creation failed with {e.GetType()}: {e.ToString()}");
return;
}
using (SslStream sslStream = new SslStream(tcpStream, false, OnRemoteCertificateReceived, OnClientCertificateRequested, EncryptionPolicy.RequireEncryption))
{
try
{
sslStream.AuthenticateAsClient(hostname);
Console.WriteLine($"TransportContext: {sslStream.TransportContext}");
Console.WriteLine($"Protocol: {sslStream.SslProtocol}");
Console.WriteLine($"KeyExchangeAlgorithm: {sslStream.KeyExchangeAlgorithm}");
Console.WriteLine($"KeyExchangeStrength: {sslStream.KeyExchangeStrength}");
Console.WriteLine($"CipherAlgorithm: {sslStream.CipherAlgorithm}");
Console.WriteLine($"CipherStrength: {sslStream.CipherStrength}");
Console.WriteLine($"HashAlgorithm: {sslStream.HashAlgorithm}");
Console.WriteLine($"HashStrength: {sslStream.HashStrength}");
}
catch (Exception e)
{
Console.WriteLine($"SSL Stream creation failed with {e.GetType()}: {e.ToString()}");
return;
}
}
Console.WriteLine("All checks passed!");
}
}
}