2
0

axTLSvb.vb 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. ' A wrapper around the unmanaged Integererface to give a semi-decent VB.NET API
  32. '
  33. Imports System
  34. Imports System.Runtime.InteropServices
  35. Imports System.Net.Sockets
  36. Imports axTLSvb
  37. Namespace axTLSvb
  38. Public Class SSL
  39. Public m_ssl As IntPtr
  40. Public Sub New(ByRef ip As IntPtr)
  41. m_ssl = ip
  42. End Sub
  43. Public Sub Dispose()
  44. axtls.ssl_free(m_ssl)
  45. End Sub
  46. Public Function HandshakeStatus() As Integer
  47. Return axtls.ssl_handshake_status(m_ssl)
  48. End Function
  49. Public Function GetCipherId() As Byte
  50. Return axtls.ssl_get_cipher_id(m_ssl)
  51. End Function
  52. Public Function GetSessionId() As Byte()
  53. Dim ptr As IntPtr = axtls.ssl_get_session_id(m_ssl)
  54. Dim sess_id_size As Integer = axtls.ssl_get_session_id_size(m_ssl)
  55. Dim result(sess_id_size-1) As Byte
  56. Marshal.Copy(ptr, result, 0, sess_id_size)
  57. Return result
  58. End Function
  59. Public Function GetCertificateDN(component As Integer) As String
  60. Return axtls.ssl_get_cert_dn(m_ssl, component)
  61. End Function
  62. End Class
  63. Public Class SSLUtil
  64. Private dummy As Integer ' need something here
  65. Public Shared Function BuildMode() As Integer
  66. Return axtls.ssl_get_config(axtls.SSL_BUILD_MODE)
  67. End Function
  68. Public Shared Function MaxCerts() As Integer
  69. Return axtls.ssl_get_config(axtls.SSL_MAX_CERT_CFG_OFFSET)
  70. End Function
  71. Public Shared Function MaxCACerts() As Integer
  72. Return axtls.ssl_get_config(axtls.SSL_MAX_CA_CERT_CFG_OFFSET)
  73. End Function
  74. Public Shared Function HasPEM() As Boolean
  75. If axtls.ssl_get_config(axtls.SSL_HAS_PEM) > 0 Then
  76. Return True
  77. Else
  78. Return False
  79. End If
  80. End Function
  81. Public Shared Sub DisplayError(ByVal error_code As Integer)
  82. axtls.ssl_display_error(error_code)
  83. End Sub
  84. Public Shared Function Version() As String
  85. Return axtls.ssl_version()
  86. End Function
  87. End Class
  88. Public Class SSLCTX
  89. Protected m_ctx As IntPtr
  90. Protected Sub New(ByVal options As Integer, _
  91. ByVal num_sessions As Integer)
  92. m_ctx = axtls.ssl_ctx_new(options, num_sessions)
  93. End Sub
  94. Public Sub Dispose()
  95. axtls.ssl_ctx_free(m_ctx)
  96. End Sub
  97. Public Function Read(ByVal ssl As SSL, ByRef in_data As Byte()) As Integer
  98. Dim ptr As IntPtr = IntPtr.Zero
  99. Dim ret as Integer = axtls.ssl_read(ssl.m_ssl, ptr)
  100. If ret > axtls.SSL_OK Then
  101. ReDim in_data(ret)
  102. Marshal.Copy(ptr, in_data, 0, ret)
  103. Else
  104. in_data = Nothing
  105. End If
  106. Return ret
  107. End Function
  108. Public Function Write(ByVal ssl As SSL, _
  109. ByVal data As Byte(), len As Integer) As Integer
  110. Return axtls.ssl_write(ssl.m_ssl, data, len)
  111. End Function
  112. Public Function Find(ByVal s As Socket) As SSL
  113. Dim client_fd As Integer = s.Handle.ToInt32()
  114. Return New SSL(axtls.ssl_find(m_ctx, client_fd))
  115. End Function
  116. Public Function VerifyCert(ByVal ssl As SSL) As Integer
  117. Return axtls.ssl_verify_cert(ssl.m_ssl)
  118. End Function
  119. Public Function Renegotiate(ByVal ssl As SSL) As Integer
  120. Return axtls.ssl_renegotiate(ssl.m_ssl)
  121. End Function
  122. Public Function ObjLoad(ByVal obj_type As Integer, _
  123. ByVal filename As String, _
  124. password As String) As Integer
  125. Return axtls.ssl_obj_load(m_ctx, obj_type, filename, password)
  126. End Function
  127. Public Function ObjLoad(ByVal obj_type As Integer, _
  128. ByVal data As Byte(), ByVal len As Integer, _
  129. password As String) As Integer
  130. Return axtls.ssl_obj_memory_load( _
  131. m_ctx, obj_type, data, len, password)
  132. End Function
  133. End Class
  134. Public Class SSLServer
  135. Inherits SSLCTX
  136. Public Sub New(ByVal options As Integer, _
  137. ByVal num_sessions As Integer)
  138. MyBase.New(options, num_sessions)
  139. End Sub
  140. Public Function Connect(ByVal s As Socket) As SSL
  141. Dim client_fd As Integer = s.Handle.ToInt32()
  142. Return New SSL(axtls.ssl_server_new(m_ctx, client_fd))
  143. End Function
  144. End Class
  145. Public Class SSLClient
  146. Inherits SSLCTX
  147. Public Sub New(ByVal options As Integer, _
  148. ByVal num_sessions As Integer)
  149. MyBase.New(options, num_sessions)
  150. End Sub
  151. Public Function Connect(ByVal s As Socket, _
  152. ByVal session_id As Byte()) As SSL
  153. Dim client_fd As Integer = s.Handle.ToInt32()
  154. Dim sess_id_size As Byte
  155. If session_id is Nothing Then
  156. sess_id_size = 0
  157. Else
  158. sess_id_size = session_id.Length
  159. End If
  160. Return New SSL(axtls.ssl_client_new(m_ctx, client_fd, session_id, _
  161. sess_id_size))
  162. End Function
  163. End Class
  164. End Namespace