added SSCLI 1.0
[windows-sources.git] / sdk / samples / FrameworkSamples / NCL / SecureStreams / vb / sslclient / sslclient.vb
bloba85e0748dc21b49738ff5ed970a73057b04e73d7
1 '---------------------------------------------------------------------
2 ' This file is part of the Microsoft .NET Framework SDK Code Samples.
3 '
4 ' Copyright (C) Microsoft Corporation. All rights reserved.
5 '
6 'This source code is intended only as a supplement to Microsoft
7 'Development Tools and/or on-line documentation. See these other
8 'materials for detailed information regarding Microsoft code samples.
9 '
10 'THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
11 'KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
12 'IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
13 'PARTICULAR PURPOSE.
14 '---------------------------------------------------------------------
16 Imports System.IO
17 Imports System.Net
18 Imports System.Net.Sockets
19 Imports System.Net.Security
20 Imports System.Security.Authentication
21 Imports System.Security.Cryptography.X509Certificates
23 Module SslClient
25 Sub Main()
26 Connect("localhost", "howdy")
27 End Sub
29 Private Sub Connect(ByVal server As String, ByVal message As String)
30 Dim sslStream As SslStream = Nothing
31 Try
32 ' Create a TcpClient.
33 ' Note, for this client to work you need to have a TcpServer
34 ' connected to the same address as specified by the server,
35 ' port combination.
36 Dim port As Integer = 13000
37 Dim client As New TcpClient(server, port)
39 ' Translate the passed message into ASCII and store it as a
40 ' Byte array.
41 Dim data() As Byte = System.Text.Encoding.ASCII.GetBytes(message)
43 Dim callback As RemoteCertificateValidationCallback = _
44 AddressOf OnCertificateValidation
46 ' Get a client stream for reading and writing.
47 ' Wrap it in an SslStream.
48 sslStream = New SslStream(client.GetStream(), False, callback)
49 sslStream.AuthenticateAsClient("localhost")
51 If sslStream.IsAuthenticated Then
52 Console.WriteLine( _
53 "IsAuthenticated: {0}", _
54 sslStream.IsAuthenticated)
55 Console.WriteLine( _
56 "IsMutuallyAuthenticated: {0}", _
57 sslStream.IsMutuallyAuthenticated)
58 Console.WriteLine( _
59 "IsEncrypted: {0}", _
60 sslStream.IsEncrypted)
61 Console.WriteLine( _
62 "IsSigned: {0}", _
63 sslStream.IsSigned)
64 Console.WriteLine( _
65 "IsServer: {0}", _
66 sslStream.IsServer)
67 End If
69 ' Send the message to the connected TcpServer.
70 sslStream.Write(data, 0, data.Length)
72 Console.WriteLine("Sent: {0}", message)
74 ' Receive the TcpServer.response.
75 ' Buffer to store the response bytes.
76 ReDim data(256)
78 ' String to store the response ASCII representation.
79 Dim responseData As String = String.Empty
81 ' Read the first batch of the TcpServer response bytes.
82 Dim bytes As Integer = sslStream.Read(data, 0, data.Length)
83 responseData = _
84 System.Text.Encoding.ASCII.GetString(data, 0, bytes)
85 Console.WriteLine("Received: {0}", responseData)
86 Catch ex As AuthenticationException
87 Console.WriteLine(ex.Message)
88 Catch ex As SocketException
89 Console.WriteLine(ex.Message)
90 Catch ex As IOException
91 Console.WriteLine(ex.Message)
92 Finally
93 If sslStream IsNot Nothing Then
94 sslStream.Close()
95 End If
96 End Try
98 Console.WriteLine(Environment.NewLine & "Press Enter to continue...")
99 Console.Read()
100 End Sub
102 Function OnCertificateValidation( _
103 ByVal sender As Object, _
104 ByVal certificate As X509Certificate, _
105 ByVal chain As X509Chain, _
106 ByVal sslPolicyErrors As SslPolicyErrors) _
107 As Boolean
109 ' This sample returns true always so that you can see how
110 ' it works. You should change logic in the validator to only
111 ' return true if you've inspected the certificate and approve it.
112 Return True
113 End Function
115 End Module