Add axTLS sourcecode
[project/luci.git] / libs / nixio / axTLS / bindings / vbnet / axTLSvb.vb
1 '
2 ' Copyright (c) 2007, Cameron Rich
3 '
4 ' All rights reserved.
5 '
6 ' Redistribution and use in source and binary forms, with or without
7 ' modification, are permitted provided that the following conditions are met:
8 '
9 ' * Redistributions of source code must retain the above copyright notice,
10 ' this list of conditions and the following disclaimer.
11 ' * Redistributions in binary form must reproduce the above copyright
12 ' notice, this list of conditions and the following disclaimer in the
13 ' documentation and/or other materials provided with the distribution.
14 ' * Neither the name of the axTLS project nor the names of its
15 ' contributors may be used to endorse or promote products derived
16 ' from this software without specific prior written permission.
17 '
18 ' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 ' "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 ' LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 ' A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22 ' CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 ' SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
24 ' TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 ' DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26 ' OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 ' NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 ' THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 '
30
31 '
32 ' A wrapper around the unmanaged Integererface to give a semi-decent VB.NET API
33 '
34
35 Imports System
36 Imports System.Runtime.InteropServices
37 Imports System.Net.Sockets
38 Imports axTLSvb
39
40 Namespace axTLSvb
41 Public Class SSL
42 Public m_ssl As IntPtr
43
44 Public Sub New(ByRef ip As IntPtr)
45 m_ssl = ip
46 End Sub
47
48 Public Sub Dispose()
49 axtls.ssl_free(m_ssl)
50 End Sub
51
52 Public Function HandshakeStatus() As Integer
53 Return axtls.ssl_handshake_status(m_ssl)
54 End Function
55
56 Public Function GetCipherId() As Byte
57 Return axtls.ssl_get_cipher_id(m_ssl)
58 End Function
59
60 Public Function GetSessionId() As Byte()
61 Dim ptr As IntPtr = axtls.ssl_get_session_id(m_ssl)
62 Dim sess_id_size As Integer = axtls.ssl_get_session_id_size(m_ssl)
63 Dim result(sess_id_size-1) As Byte
64 Marshal.Copy(ptr, result, 0, sess_id_size)
65 Return result
66 End Function
67
68 Public Function GetCertificateDN(component As Integer) As String
69 Return axtls.ssl_get_cert_dn(m_ssl, component)
70 End Function
71 End Class
72
73 Public Class SSLUtil
74 Private dummy As Integer ' need something here
75
76 Public Shared Function BuildMode() As Integer
77 Return axtls.ssl_get_config(axtls.SSL_BUILD_MODE)
78 End Function
79
80 Public Shared Function MaxCerts() As Integer
81 Return axtls.ssl_get_config(axtls.SSL_MAX_CERT_CFG_OFFSET)
82 End Function
83
84 Public Shared Function MaxCACerts() As Integer
85 Return axtls.ssl_get_config(axtls.SSL_MAX_CA_CERT_CFG_OFFSET)
86 End Function
87
88 Public Shared Function HasPEM() As Boolean
89 If axtls.ssl_get_config(axtls.SSL_HAS_PEM) > 0 Then
90 Return True
91 Else
92 Return False
93 End If
94 End Function
95
96 Public Shared Sub DisplayError(ByVal error_code As Integer)
97 axtls.ssl_display_error(error_code)
98 End Sub
99
100 Public Shared Function Version() As String
101 Return axtls.ssl_version()
102 End Function
103 End Class
104
105 Public Class SSLCTX
106 Protected m_ctx As IntPtr
107
108 Protected Sub New(ByVal options As Integer, _
109 ByVal num_sessions As Integer)
110 m_ctx = axtls.ssl_ctx_new(options, num_sessions)
111 End Sub
112
113 Public Sub Dispose()
114 axtls.ssl_ctx_free(m_ctx)
115 End Sub
116
117 Public Function Read(ByVal ssl As SSL, ByRef in_data As Byte()) As Integer
118 Dim ptr As IntPtr = IntPtr.Zero
119 Dim ret as Integer = axtls.ssl_read(ssl.m_ssl, ptr)
120
121 If ret > axtls.SSL_OK Then
122 ReDim in_data(ret)
123 Marshal.Copy(ptr, in_data, 0, ret)
124 Else
125 in_data = Nothing
126 End If
127
128 Return ret
129 End Function
130
131 Public Function Write(ByVal ssl As SSL, _
132 ByVal data As Byte(), len As Integer) As Integer
133 Return axtls.ssl_write(ssl.m_ssl, data, len)
134 End Function
135
136 Public Function Find(ByVal s As Socket) As SSL
137 Dim client_fd As Integer = s.Handle.ToInt32()
138 Return New SSL(axtls.ssl_find(m_ctx, client_fd))
139 End Function
140
141 Public Function VerifyCert(ByVal ssl As SSL) As Integer
142 Return axtls.ssl_verify_cert(ssl.m_ssl)
143 End Function
144
145 Public Function Renegotiate(ByVal ssl As SSL) As Integer
146 Return axtls.ssl_renegotiate(ssl.m_ssl)
147 End Function
148
149 Public Function ObjLoad(ByVal obj_type As Integer, _
150 ByVal filename As String, _
151 password As String) As Integer
152 Return axtls.ssl_obj_load(m_ctx, obj_type, filename, password)
153 End Function
154
155 Public Function ObjLoad(ByVal obj_type As Integer, _
156 ByVal data As Byte(), ByVal len As Integer, _
157 password As String) As Integer
158 Return axtls.ssl_obj_memory_load( _
159 m_ctx, obj_type, data, len, password)
160 End Function
161 End Class
162
163 Public Class SSLServer
164 Inherits SSLCTX
165
166 Public Sub New(ByVal options As Integer, _
167 ByVal num_sessions As Integer)
168 MyBase.New(options, num_sessions)
169 End Sub
170
171 Public Function Connect(ByVal s As Socket) As SSL
172 Dim client_fd As Integer = s.Handle.ToInt32()
173 Return New SSL(axtls.ssl_server_new(m_ctx, client_fd))
174 End Function
175 End Class
176
177 Public Class SSLClient
178 Inherits SSLCTX
179
180 Public Sub New(ByVal options As Integer, _
181 ByVal num_sessions As Integer)
182 MyBase.New(options, num_sessions)
183 End Sub
184
185 Public Function Connect(ByVal s As Socket, _
186 ByVal session_id As Byte()) As SSL
187 Dim client_fd As Integer = s.Handle.ToInt32()
188 Dim sess_id_size As Byte
189 If session_id is Nothing Then
190 sess_id_size = 0
191 Else
192 sess_id_size = session_id.Length
193 End If
194
195 Return New SSL(axtls.ssl_client_new(m_ctx, client_fd, session_id, _
196 sess_id_size))
197 End Function
198
199 End Class
200 End Namespace