summaryrefslogtreecommitdiffhomepage
path: root/libs/luci-lib-nixio/axTLS/bindings/vbnet/axTLSvb.vb
blob: 9388273ce826a1c38d4738b2c48e4b9ec0c4f8e2 (plain)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
'
' Copyright (c) 2007, Cameron Rich
'
' All rights reserved.
'
' Redistribution and use in source and binary forms, with or without
' modification, are permitted provided that the following conditions are met:
'
' * Redistributions of source code must retain the above copyright notice,
'   this list of conditions and the following disclaimer.
' * Redistributions in binary form must reproduce the above copyright
'   notice, this list of conditions and the following disclaimer in the
'   documentation and/or other materials provided with the distribution.
' * Neither the name of the axTLS project nor the names of its
'   contributors may be used to endorse or promote products derived
'   from this software without specific prior written permission.
'
' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
' "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
' LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
' A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
' CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
' SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
' TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
' DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
' OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
' NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
' THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
'

'
' A wrapper around the unmanaged Integererface to give a semi-decent VB.NET API
'

Imports System
Imports System.Runtime.InteropServices
Imports System.Net.Sockets
Imports axTLSvb

Namespace axTLSvb
    Public Class SSL
        Public m_ssl As IntPtr

        Public Sub New(ByRef ip As IntPtr)
            m_ssl = ip
        End Sub

        Public Sub Dispose()
            axtls.ssl_free(m_ssl)
        End Sub

        Public Function HandshakeStatus() As Integer
            Return axtls.ssl_handshake_status(m_ssl)
        End Function

        Public Function GetCipherId() As Byte
            Return axtls.ssl_get_cipher_id(m_ssl)
        End Function

        Public Function GetSessionId() As Byte()
            Dim ptr As IntPtr = axtls.ssl_get_session_id(m_ssl)
            Dim sess_id_size As Integer = axtls.ssl_get_session_id_size(m_ssl)
            Dim result(sess_id_size-1) As Byte
            Marshal.Copy(ptr, result, 0, sess_id_size)
            Return result
        End Function

        Public Function GetCertificateDN(component As Integer) As String
            Return axtls.ssl_get_cert_dn(m_ssl, component)
        End Function
    End Class

    Public Class SSLUtil
        Private dummy As Integer    ' need something here

        Public Shared Function BuildMode() As Integer
            Return axtls.ssl_get_config(axtls.SSL_BUILD_MODE)
        End Function

        Public Shared Function MaxCerts() As Integer
            Return axtls.ssl_get_config(axtls.SSL_MAX_CERT_CFG_OFFSET)
        End Function

        Public Shared Function MaxCACerts() As Integer
            Return axtls.ssl_get_config(axtls.SSL_MAX_CA_CERT_CFG_OFFSET)
        End Function

        Public Shared Function HasPEM() As Boolean
            If axtls.ssl_get_config(axtls.SSL_HAS_PEM) > 0 Then
                Return True
            Else
                Return False
            End If
        End Function
        
        Public Shared Sub DisplayError(ByVal error_code As Integer)
            axtls.ssl_display_error(error_code)
        End Sub

        Public Shared Function Version() As String
            Return axtls.ssl_version()
        End Function
    End Class

    Public Class SSLCTX
        Protected m_ctx As IntPtr

        Protected Sub New(ByVal options As Integer, _
                ByVal num_sessions As Integer)
            m_ctx = axtls.ssl_ctx_new(options, num_sessions)
        End Sub

        Public Sub Dispose()
            axtls.ssl_ctx_free(m_ctx)
        End Sub

        Public Function Read(ByVal ssl As SSL, ByRef in_data As Byte()) As Integer
            Dim ptr As IntPtr = IntPtr.Zero
            Dim ret as Integer = axtls.ssl_read(ssl.m_ssl, ptr)

            If ret > axtls.SSL_OK Then
                ReDim in_data(ret)
                Marshal.Copy(ptr, in_data, 0, ret)
            Else
                in_data = Nothing
            End If

            Return ret
        End Function

        Public Function Write(ByVal ssl As SSL, _
                ByVal data As Byte(), len As Integer) As Integer
            Return axtls.ssl_write(ssl.m_ssl, data, len)
        End Function

        Public Function Find(ByVal s As Socket) As SSL
            Dim client_fd As Integer = s.Handle.ToInt32()
            Return New SSL(axtls.ssl_find(m_ctx, client_fd))
        End Function

        Public Function VerifyCert(ByVal ssl As SSL) As Integer
            Return axtls.ssl_verify_cert(ssl.m_ssl)
        End Function

        Public Function Renegotiate(ByVal ssl As SSL) As Integer
            Return axtls.ssl_renegotiate(ssl.m_ssl)
        End Function

        Public Function ObjLoad(ByVal obj_type As Integer, _
                ByVal filename As String, _
                password As String) As Integer
            Return axtls.ssl_obj_load(m_ctx, obj_type, filename, password)
        End Function

        Public Function ObjLoad(ByVal obj_type As Integer, _
                ByVal data As Byte(), ByVal len As Integer, _
                password As String) As Integer
            Return axtls.ssl_obj_memory_load( _
                    m_ctx, obj_type, data, len, password)
        End Function
    End Class

    Public Class SSLServer 
            Inherits SSLCTX

        Public Sub New(ByVal options As Integer, _
                ByVal num_sessions As Integer)
            MyBase.New(options, num_sessions)
        End Sub

        Public Function Connect(ByVal s As Socket) As SSL
            Dim client_fd As Integer = s.Handle.ToInt32()
            Return New SSL(axtls.ssl_server_new(m_ctx, client_fd))
        End Function
    End Class

    Public Class SSLClient 
            Inherits SSLCTX

        Public Sub New(ByVal options As Integer, _
                ByVal num_sessions As Integer)
            MyBase.New(options, num_sessions)
        End Sub

        Public Function Connect(ByVal s As Socket, _
                                ByVal session_id As Byte()) As SSL
            Dim client_fd As Integer = s.Handle.ToInt32()
            Dim sess_id_size As Byte
            If session_id is Nothing Then
                sess_id_size = 0
            Else
                sess_id_size = session_id.Length
            End If

            Return New SSL(axtls.ssl_client_new(m_ctx, client_fd, session_id, _
                       sess_id_size))
        End Function

    End Class
End Namespace