2
0

axssl.vb 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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. ' Demonstrate the use of the axTLS library in VB.NET with a set of
  32. ' command-line parameters similar to openssl. In fact, openssl clients
  33. ' should be able to communicate with axTLS servers and visa-versa.
  34. '
  35. ' This code has various bits enabled depending on the configuration. To enable
  36. ' the most interesting version, compile with the 'full mode' enabled.
  37. '
  38. ' To see what options you have, run the following:
  39. ' > axssl.vbnet.exe s_server -?
  40. ' > axssl.vbnet.exe s_client -?
  41. '
  42. ' The axtls shared library must be in the same directory or be found
  43. ' by the OS.
  44. '
  45. Imports System
  46. Imports System.Net
  47. Imports System.Net.Sockets
  48. Imports Microsoft.VisualBasic
  49. Imports axTLSvb
  50. Public Class axssl
  51. '
  52. ' do_server()
  53. '
  54. Public Sub do_server(ByVal build_mode As Integer, _
  55. ByVal args() As String)
  56. Dim i As Integer = 1
  57. Dim port As Integer = 4433
  58. Dim options As Integer = axtls.SSL_DISPLAY_CERTS
  59. Dim quiet As Boolean = False
  60. Dim password As String = Nothing
  61. Dim private_key_file As String = Nothing
  62. ' organise the cert/ca_cert lists
  63. Dim cert_size As Integer = SSLUtil.MaxCerts()
  64. Dim ca_cert_size As Integer = SSLUtil.MaxCACerts()
  65. Dim cert(cert_size) As String
  66. Dim ca_cert(ca_cert_size) As String
  67. Dim cert_index As Integer = 0
  68. Dim ca_cert_index As Integer = 0
  69. While i < args.Length
  70. If args(i) = "-accept" Then
  71. If i >= args.Length-1
  72. print_server_options(build_mode, args(i))
  73. End If
  74. i += 1
  75. port = Int32.Parse(args(i))
  76. ElseIf args(i) = "-quiet"
  77. quiet = True
  78. options = options And Not axtls.SSL_DISPLAY_CERTS
  79. ElseIf build_mode >= axtls.SSL_BUILD_SERVER_ONLY
  80. If args(i) = "-cert"
  81. If i >= args.Length-1 Or cert_index >= cert_size
  82. print_server_options(build_mode, args(i))
  83. End If
  84. i += 1
  85. cert(cert_index) = args(i)
  86. cert_index += 1
  87. ElseIf args(i) = "-key"
  88. If i >= args.Length-1
  89. print_server_options(build_mode, args(i))
  90. End If
  91. i += 1
  92. private_key_file = args(i)
  93. options = options Or axtls.SSL_NO_DEFAULT_KEY
  94. ElseIf args(i) = "-pass"
  95. If i >= args.Length-1
  96. print_server_options(build_mode, args(i))
  97. End If
  98. i += 1
  99. password = args(i)
  100. ElseIf build_mode >= axtls.SSL_BUILD_ENABLE_VERIFICATION
  101. If args(i) = "-verify" Then
  102. options = options Or axtls.SSL_CLIENT_AUTHENTICATION
  103. ElseIf args(i) = "-CAfile"
  104. If i >= args.Length-1 Or _
  105. ca_cert_index >= ca_cert_size Then
  106. print_server_options(build_mode, args(i))
  107. End If
  108. i += 1
  109. ca_cert(ca_cert_index) = args(i)
  110. ca_cert_index += 1
  111. ElseIf build_mode = axtls.SSL_BUILD_FULL_MODE
  112. If args(i) = "-debug" Then
  113. options = options Or axtls.SSL_DISPLAY_BYTES
  114. ElseIf args(i) = "-state"
  115. options = options Or axtls.SSL_DISPLAY_STATES
  116. ElseIf args(i) = "-show-rsa"
  117. options = options Or axtls.SSL_DISPLAY_RSA
  118. Else
  119. print_server_options(build_mode, args(i))
  120. End If
  121. Else
  122. print_server_options(build_mode, args(i))
  123. End If
  124. Else
  125. print_server_options(build_mode, args(i))
  126. End If
  127. End If
  128. i += 1
  129. End While
  130. ' Create socket for incoming connections
  131. Dim ep As IPEndPoint = New IPEndPoint(IPAddress.Any, port)
  132. Dim server_sock As TcpListener = New TcpListener(ep)
  133. server_sock.Start()
  134. '*********************************************************************
  135. ' This is where the interesting stuff happens. Up until now we've
  136. ' just been setting up sockets etc. Now we do the SSL handshake.
  137. '*********************************************************************/
  138. Dim ssl_ctx As SSLServer = New SSLServer(options, _
  139. axtls.SSL_DEFAULT_SVR_SESS)
  140. If ssl_ctx Is Nothing Then
  141. Console.Error.WriteLine("Error: Server context is invalid")
  142. Environment.Exit(1)
  143. End If
  144. If private_key_file <> Nothing Then
  145. Dim obj_type As Integer = axtls.SSL_OBJ_RSA_KEY
  146. If private_key_file.EndsWith(".p8") Then
  147. obj_type = axtls.SSL_OBJ_PKCS8
  148. Else If (private_key_file.EndsWith(".p12"))
  149. obj_type = axtls.SSL_OBJ_PKCS12
  150. End If
  151. If ssl_ctx.ObjLoad(obj_type, private_key_file, _
  152. password) <> axtls.SSL_OK Then
  153. Console.Error.WriteLine("Error: Private key '" & _
  154. private_key_file & "' is undefined.")
  155. Environment.Exit(1)
  156. End If
  157. End If
  158. For i = 0 To cert_index-1
  159. If ssl_ctx.ObjLoad(axtls.SSL_OBJ_X509_CERT, _
  160. cert(i), Nothing) <> axtls.SSL_OK Then
  161. Console.WriteLine("Certificate '" & cert(i) & _
  162. "' is undefined.")
  163. Environment.Exit(1)
  164. End If
  165. Next
  166. For i = 0 To ca_cert_index-1
  167. If ssl_ctx.ObjLoad(axtls.SSL_OBJ_X509_CACERT, _
  168. ca_cert(i), Nothing) <> axtls.SSL_OK Then
  169. Console.WriteLine("Certificate '" & ca_cert(i) & _
  170. "' is undefined.")
  171. Environment.Exit(1)
  172. End If
  173. Next
  174. Dim buf As Byte() = Nothing
  175. Dim res As Integer
  176. Dim ssl As SSL
  177. While 1
  178. If Not quiet Then
  179. Console.WriteLine("ACCEPT")
  180. End If
  181. Dim client_sock As Socket = server_sock.AcceptSocket()
  182. ssl = ssl_ctx.Connect(client_sock)
  183. ' do the actual SSL handshake
  184. While 1
  185. res = ssl_ctx.Read(ssl, buf)
  186. If res <> axtls.SSL_OK Then
  187. Exit While
  188. End If
  189. ' check when the connection has been established
  190. If ssl.HandshakeStatus() = axtls.SSL_OK
  191. Exit While
  192. End If
  193. ' could do something else here
  194. End While
  195. If res = axtls.SSL_OK Then ' connection established and ok
  196. If Not quiet
  197. display_session_id(ssl)
  198. display_cipher(ssl)
  199. End If
  200. ' now read (and display) whatever the client sends us
  201. While 1
  202. ' keep reading until we get something interesting
  203. While 1
  204. res = ssl_ctx.Read(ssl, buf)
  205. If res <> axtls.SSL_OK Then
  206. Exit While
  207. End If
  208. ' could do something else here
  209. End While
  210. If res < axtls.SSL_OK
  211. If Not quiet
  212. Console.WriteLine("CONNECTION CLOSED")
  213. End If
  214. Exit While
  215. End If
  216. ' convert to String
  217. Dim str(res) As Char
  218. For i = 0 To res-1
  219. str(i) = Chr(buf(i))
  220. Next
  221. Console.Write(str)
  222. End While
  223. ElseIf Not quiet
  224. SSLUtil.DisplayError(res)
  225. End If
  226. ' client was disconnected or the handshake failed. */
  227. ssl.Dispose()
  228. client_sock.Close()
  229. End While
  230. ssl_ctx.Dispose()
  231. End Sub
  232. '
  233. ' do_client()
  234. '
  235. Public Sub do_client(ByVal build_mode As Integer, _
  236. ByVal args() As String)
  237. If build_mode < axtls.SSL_BUILD_ENABLE_CLIENT Then
  238. print_client_options(build_mode, args(1))
  239. End If
  240. Dim i As Integer = 1
  241. Dim res As Integer
  242. Dim port As Integer = 4433
  243. Dim quiet As Boolean = False
  244. Dim password As String = Nothing
  245. Dim reconnect As Integer = 0
  246. Dim private_key_file As String = Nothing
  247. Dim hostname As String = "127.0.0.1"
  248. ' organise the cert/ca_cert lists
  249. Dim ssl As SSL = Nothing
  250. Dim cert_size As Integer = SSLUtil.MaxCerts()
  251. Dim ca_cert_size As Integer = SSLUtil.MaxCACerts()
  252. Dim cert(cert_size) As String
  253. Dim ca_cert(ca_cert_size) As String
  254. Dim cert_index As Integer = 0
  255. Dim ca_cert_index As Integer = 0
  256. Dim options As Integer = _
  257. axtls.SSL_SERVER_VERIFY_LATER Or axtls.SSL_DISPLAY_CERTS
  258. Dim session_id As Byte() = Nothing
  259. While i < args.Length
  260. If args(i) = "-connect" Then
  261. Dim host_port As String
  262. If i >= args.Length-1
  263. print_client_options(build_mode, args(i))
  264. End If
  265. i += 1
  266. host_port = args(i)
  267. Dim index_colon As Integer = host_port.IndexOf(":"C)
  268. If index_colon < 0 Then
  269. print_client_options(build_mode, args(i))
  270. End If
  271. hostname = New String(host_port.ToCharArray(), _
  272. 0, index_colon)
  273. port = Int32.Parse(New String(host_port.ToCharArray(), _
  274. index_colon+1, host_port.Length-index_colon-1))
  275. ElseIf args(i) = "-cert"
  276. If i >= args.Length-1 Or cert_index >= cert_size Then
  277. print_client_options(build_mode, args(i))
  278. End If
  279. i += 1
  280. cert(cert_index) = args(i)
  281. cert_index += 1
  282. ElseIf args(i) = "-key"
  283. If i >= args.Length-1
  284. print_client_options(build_mode, args(i))
  285. End If
  286. i += 1
  287. private_key_file = args(i)
  288. options = options Or axtls.SSL_NO_DEFAULT_KEY
  289. ElseIf args(i) = "-CAfile"
  290. If i >= args.Length-1 Or ca_cert_index >= ca_cert_size
  291. print_client_options(build_mode, args(i))
  292. End If
  293. i += 1
  294. ca_cert(ca_cert_index) = args(i)
  295. ca_cert_index += 1
  296. ElseIf args(i) = "-verify"
  297. options = options And Not axtls.SSL_SERVER_VERIFY_LATER
  298. ElseIf args(i) = "-reconnect"
  299. reconnect = 4
  300. ElseIf args(i) = "-quiet"
  301. quiet = True
  302. options = options And Not axtls.SSL_DISPLAY_CERTS
  303. ElseIf args(i) = "-pass"
  304. If i >= args.Length-1
  305. print_client_options(build_mode, args(i))
  306. End If
  307. i += 1
  308. password = args(i)
  309. ElseIf build_mode = axtls.SSL_BUILD_FULL_MODE
  310. If args(i) = "-debug" Then
  311. options = options Or axtls.SSL_DISPLAY_BYTES
  312. ElseIf args(i) = "-state"
  313. options = options Or axtls.SSL_DISPLAY_STATES
  314. ElseIf args(i) = "-show-rsa"
  315. options = options Or axtls.SSL_DISPLAY_RSA
  316. Else
  317. print_client_options(build_mode, args(i))
  318. End If
  319. Else ' don't know what this is
  320. print_client_options(build_mode, args(i))
  321. End If
  322. i += 1
  323. End While
  324. 'Dim hostInfo As IPHostEntry = Dns.Resolve(hostname)
  325. Dim hostInfo As IPHostEntry = Dns.GetHostEntry(hostname)
  326. Dim addresses As IPAddress() = hostInfo.AddressList
  327. Dim ep As IPEndPoint = New IPEndPoint(addresses(0), port)
  328. Dim client_sock As Socket = New Socket(AddressFamily.InterNetwork, _
  329. SocketType.Stream, ProtocolType.Tcp)
  330. client_sock.Connect(ep)
  331. If Not client_sock.Connected Then
  332. Console.WriteLine("could not connect")
  333. Environment.Exit(1)
  334. End If
  335. If Not quiet Then
  336. Console.WriteLine("CONNECTED")
  337. End If
  338. '*********************************************************************
  339. ' This is where the interesting stuff happens. Up until now we've
  340. ' just been setting up sockets etc. Now we do the SSL handshake.
  341. '*********************************************************************/
  342. Dim ssl_ctx As SSLClient = New SSLClient(options, _
  343. axtls.SSL_DEFAULT_CLNT_SESS)
  344. If ssl_ctx Is Nothing Then
  345. Console.Error.WriteLine("Error: Client context is invalid")
  346. Environment.Exit(1)
  347. End If
  348. If private_key_file <> Nothing Then
  349. Dim obj_type As Integer = axtls.SSL_OBJ_RSA_KEY
  350. If private_key_file.EndsWith(".p8") Then
  351. obj_type = axtls.SSL_OBJ_PKCS8
  352. Else If (private_key_file.EndsWith(".p12"))
  353. obj_type = axtls.SSL_OBJ_PKCS12
  354. End If
  355. If ssl_ctx.ObjLoad(obj_type, private_key_file, _
  356. password) <> axtls.SSL_OK Then
  357. Console.Error.WriteLine("Error: Private key '" & _
  358. private_key_file & "' is undefined.")
  359. Environment.Exit(1)
  360. End If
  361. End If
  362. For i = 0 To cert_index-1
  363. If ssl_ctx.ObjLoad(axtls.SSL_OBJ_X509_CERT, _
  364. cert(i), Nothing) <> axtls.SSL_OK Then
  365. Console.WriteLine("Certificate '" & cert(i) & _
  366. "' is undefined.")
  367. Environment.Exit(1)
  368. End If
  369. Next
  370. For i = 0 To ca_cert_index-1
  371. If ssl_ctx.ObjLoad(axtls.SSL_OBJ_X509_CACERT, _
  372. ca_cert(i), Nothing) <> axtls.SSL_OK Then
  373. Console.WriteLine("Certificate '" & ca_cert(i) & _
  374. "' is undefined.")
  375. Environment.Exit(1)
  376. End If
  377. Next
  378. ' Try session resumption?
  379. If reconnect > 0 Then
  380. While reconnect > 0
  381. reconnect -= 1
  382. ssl = ssl_ctx.Connect(client_sock, session_id)
  383. res = ssl.HandshakeStatus()
  384. If res <> axtls.SSL_OK Then
  385. If Not quiet Then
  386. SSLUtil.DisplayError(res)
  387. End If
  388. ssl.Dispose()
  389. Environment.Exit(1)
  390. End If
  391. display_session_id(ssl)
  392. session_id = ssl.GetSessionId()
  393. If reconnect > 0 Then
  394. ssl.Dispose()
  395. client_sock.Close()
  396. ' and reconnect
  397. client_sock = New Socket(AddressFamily.InterNetwork, _
  398. SocketType.Stream, ProtocolType.Tcp)
  399. client_sock.Connect(ep)
  400. End If
  401. End While
  402. Else
  403. ssl = ssl_ctx.Connect(client_sock, Nothing)
  404. End If
  405. ' check the return status
  406. res = ssl.HandshakeStatus()
  407. If res <> axtls.SSL_OK Then
  408. If Not quiet Then
  409. SSLUtil.DisplayError(res)
  410. End If
  411. Environment.Exit(1)
  412. End If
  413. If Not quiet Then
  414. Dim common_name As String = _
  415. ssl.GetCertificateDN(axtls.SSL_X509_CERT_COMMON_NAME)
  416. If common_name <> Nothing
  417. Console.WriteLine("Common Name:" & _
  418. ControlChars.Tab & ControlChars.Tab & _
  419. ControlChars.Tab & common_name)
  420. End If
  421. display_session_id(ssl)
  422. display_cipher(ssl)
  423. End If
  424. While (1)
  425. Dim user_input As String = Console.ReadLine()
  426. If user_input = Nothing Then
  427. Exit While
  428. End If
  429. Dim buf(user_input.Length+1) As Byte
  430. buf(buf.Length-2) = Asc(ControlChars.Lf) ' add the carriage return
  431. buf(buf.Length-1) = 0 ' null terminate
  432. For i = 0 To user_input.Length-1
  433. buf(i) = Asc(user_input.Chars(i))
  434. Next
  435. res = ssl_ctx.Write(ssl, buf, buf.Length)
  436. If res < axtls.SSL_OK Then
  437. If Not quiet Then
  438. SSLUtil.DisplayError(res)
  439. End If
  440. Exit While
  441. End If
  442. End While
  443. ssl_ctx.Dispose()
  444. End Sub
  445. '
  446. ' Display what cipher we are using
  447. '
  448. Private Sub display_cipher(ByVal ssl As SSL)
  449. Console.Write("CIPHER is ")
  450. Select ssl.GetCipherId()
  451. Case axtls.SSL_AES128_SHA
  452. Console.WriteLine("AES128-SHA")
  453. Case axtls.SSL_AES256_SHA
  454. Console.WriteLine("AES256-SHA")
  455. Case axtls.SSL_RC4_128_SHA
  456. Console.WriteLine("RC4-SHA")
  457. Case axtls.SSL_RC4_128_MD5
  458. Console.WriteLine("RC4-MD5")
  459. Case Else
  460. Console.WriteLine("Unknown - " & ssl.GetCipherId())
  461. End Select
  462. End Sub
  463. '
  464. ' Display what session id we have.
  465. '
  466. Private Sub display_session_id(ByVal ssl As SSL)
  467. Dim session_id As Byte() = ssl.GetSessionId()
  468. If session_id.Length > 0 Then
  469. Console.WriteLine("-----BEGIN SSL SESSION PARAMETERS-----")
  470. Dim b As Byte
  471. For Each b In session_id
  472. Console.Write("{0:x02}", b)
  473. Next
  474. Console.WriteLine()
  475. Console.WriteLine("-----END SSL SESSION PARAMETERS-----")
  476. End If
  477. End Sub
  478. '
  479. ' We've had some sort of command-line error. Print out the basic options.
  480. '
  481. Public Sub print_options(ByVal options As String)
  482. Console.WriteLine("axssl: Error: '" & options & _
  483. "' is an invalid command.")
  484. Console.WriteLine("usage: axssl.vbnet [s_server|s_client|" & _
  485. "version] [args ...]")
  486. Environment.Exit(1)
  487. End Sub
  488. '
  489. ' We've had some sort of command-line error. Print out the server options.
  490. '
  491. Private Sub print_server_options(ByVal build_mode As Integer, _
  492. ByVal options As String)
  493. Dim cert_size As Integer = SSLUtil.MaxCerts()
  494. Dim ca_cert_size As Integer = SSLUtil.MaxCACerts()
  495. Console.WriteLine("unknown option " & options)
  496. Console.WriteLine("usage: s_server [args ...]")
  497. Console.WriteLine(" -accept arg" & ControlChars.Tab & _
  498. "- port to accept on (default is 4433)")
  499. Console.WriteLine(" -quiet" & ControlChars.Tab & ControlChars.Tab & _
  500. "- No server output")
  501. If build_mode >= axtls.SSL_BUILD_SERVER_ONLY
  502. Console.WriteLine(" -cert arg" & ControlChars.Tab & _
  503. "- certificate file to add (in addition to default) to chain -")
  504. Console.WriteLine(ControlChars.Tab & ControlChars.Tab & _
  505. " Can repeat up to " & cert_size & " times")
  506. Console.WriteLine(" -key arg" & ControlChars.Tab & _
  507. "- Private key file to use")
  508. Console.WriteLine(" -pass" & ControlChars.Tab & ControlChars.Tab & _
  509. "- private key file pass phrase source")
  510. End If
  511. If build_mode >= axtls.SSL_BUILD_ENABLE_VERIFICATION
  512. Console.WriteLine(" -verify" & ControlChars.Tab & _
  513. "- turn on peer certificate verification")
  514. Console.WriteLine(" -CAfile arg" & ControlChars.Tab & _
  515. "- Certificate authority")
  516. Console.WriteLine(ControlChars.Tab & ControlChars.Tab & _
  517. " Can repeat up to " & ca_cert_size & " times")
  518. End If
  519. If build_mode = axtls.SSL_BUILD_FULL_MODE
  520. Console.WriteLine(" -debug" & _
  521. ControlChars.Tab & ControlChars.Tab & _
  522. "- Print more output")
  523. Console.WriteLine(" -state" & _
  524. ControlChars.Tab & ControlChars.Tab & _
  525. "- Show state messages")
  526. Console.WriteLine(" -show-rsa" & _
  527. ControlChars.Tab & "- Show RSA state")
  528. End If
  529. Environment.Exit(1)
  530. End Sub
  531. '
  532. ' We've had some sort of command-line error. Print out the client options.
  533. '
  534. Private Sub print_client_options(ByVal build_mode As Integer, _
  535. ByVal options As String)
  536. Dim cert_size As Integer = SSLUtil.MaxCerts()
  537. Dim ca_cert_size As Integer = SSLUtil.MaxCACerts()
  538. Console.WriteLine("unknown option " & options)
  539. If build_mode >= axtls.SSL_BUILD_ENABLE_CLIENT Then
  540. Console.WriteLine("usage: s_client [args ...]")
  541. Console.WriteLine(" -connect host:port - who to connect to " & _
  542. "(default is localhost:4433)")
  543. Console.WriteLine(" -verify" & ControlChars.Tab & _
  544. "- turn on peer certificate verification")
  545. Console.WriteLine(" -cert arg" & ControlChars.Tab & _
  546. "- certificate file to use")
  547. Console.WriteLine(ControlChars.Tab & ControlChars.Tab & _
  548. " Can repeat up to " & cert_size & " times")
  549. Console.WriteLine(" -key arg" & ControlChars.Tab & _
  550. "- Private key file to use")
  551. Console.WriteLine(" -CAfile arg" & ControlChars.Tab & _
  552. "- Certificate authority")
  553. Console.WriteLine(ControlChars.Tab & ControlChars.Tab & _
  554. " Can repeat up to " & ca_cert_size & " times")
  555. Console.WriteLine(" -quiet" & _
  556. ControlChars.Tab & ControlChars.Tab & "- No client output")
  557. Console.WriteLine(" -pass" & ControlChars.Tab & _
  558. ControlChars.Tab & _
  559. "- private key file pass phrase source")
  560. Console.WriteLine(" -reconnect" & ControlChars.Tab & _
  561. "- Drop and re-make the " & _
  562. "connection with the same Session-ID")
  563. If build_mode = axtls.SSL_BUILD_FULL_MODE Then
  564. Console.WriteLine(" -debug" & _
  565. ControlChars.Tab & ControlChars.Tab & _
  566. "- Print more output")
  567. Console.WriteLine(" -state" & _
  568. ControlChars.Tab & ControlChars.Tab & _
  569. "- Show state messages")
  570. Console.WriteLine(" -show-rsa" & ControlChars.Tab & _
  571. "- Show RSA state")
  572. End If
  573. Else
  574. Console.WriteLine("Change configuration to allow this feature")
  575. End If
  576. Environment.Exit(1)
  577. End Sub
  578. End Class
  579. Public Module MyMain
  580. Function Main(ByVal args() As String) As Integer
  581. Dim runner As axssl = New axssl()
  582. If args.Length = 1 And args(0) = "version" Then
  583. Console.WriteLine("axssl.vbnet " & SSLUtil.Version())
  584. Environment.Exit(0)
  585. End If
  586. If args.Length < 1
  587. runner.print_options("")
  588. ElseIf args(0) <> "s_server" And args(0) <> "s_client"
  589. runner.print_options(args(0))
  590. End If
  591. Dim build_mode As Integer = SSLUtil.BuildMode()
  592. If args(0) = "s_server" Then
  593. runner.do_server(build_mode, args)
  594. Else
  595. runner.do_client(build_mode, args)
  596. End If
  597. End Function
  598. End Module