ssl_server.bmx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. SuperStrict
  2. Framework net.mbedtls
  3. Import brl.standardio
  4. Const HTTP_RESPONSE:String = "HTTP/1.0 200 OK~r~nContent-Type: text/html~r~n~r~n" + ..
  5. "<h2>mbed TLS Test Server</h2>~r~n" + ..
  6. "<p>Successful connection using: %s</p>~r~n"
  7. Local listen:TNetContext = New TNetContext.Create()
  8. Local client:TNetContext = New TNetContext.Create()
  9. Local ssl:TSSLContext = New TSSLContext.Create()
  10. Local config:TSSLConfig = New TSSLConfig.Create()
  11. Local cert:TX509Cert = New TX509Cert.Create()
  12. Local pk:TPkContext = New TPkContext.Create()
  13. Local entropy:TEntropyContext = New TEntropyContext.Create()
  14. Local rctx:TRandContext = New TRandContext.Create()
  15. ' 1. Load the certificates and private RSA key
  16. Print " . Loading the server cert. and key..."
  17. Local res:Int = cert.ParseFile("cert.pem")
  18. If res Then
  19. Print " failed~n ! TX509Cert.Parse() returned " + res
  20. Fail(res)
  21. End If
  22. res = pk.ParseKeyFile("privkey.pem")
  23. If res Then
  24. Print " failed~n ! TPkContext.ParseKey() returned " + res
  25. Fail(res)
  26. End If
  27. Print " ok"
  28. ' 2. Setup the listening TCP socket
  29. Print " . Bind on https://localhost:4433/ ..."
  30. res = listen.Bind(Null, "4433", MBEDTLS_NET_PROTO_TCP)
  31. If res Then
  32. Print " failed~n ! TNetContext.Bind() returned " + res
  33. Fail(res)
  34. End If
  35. Print " ok"
  36. ' 3. Seed the RNG
  37. Print " . Seeding the random number generator..."
  38. res = rctx.Seed(EntropyFunc, entropy)
  39. If res Then
  40. Print " failed~n ! TRandContext.Seed() returned " + res
  41. Fail(res)
  42. End If
  43. Print " ok"
  44. ' 4. Setup stuff
  45. Print " . Setting up the SSL data...."
  46. res = config.Defaults(MBEDTLS_SSL_IS_SERVER, MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT)
  47. If res Then
  48. Print " failed~n ! TSSLConfig.Defaults() returned " + res
  49. Fail(res)
  50. End If
  51. config.RNG(RandomFunc, rctx)
  52. config.DBG(myDebug, Null)
  53. config.SetDebugThreshold(0)
  54. config.CaChain(cert, Null)
  55. res = config.OwnCert(cert, pk)
  56. If res Then
  57. Print " failed~n ! TSSLConfig.OwnCert() returned " + res
  58. Fail(res)
  59. End If
  60. res = ssl.Setup(config)
  61. If res Then
  62. Print " failed~n ! TSSLContext.Setup() returned " + res
  63. Fail(res)
  64. End If
  65. Print " ok"
  66. #Reset
  67. While True
  68. If res <> 0 Then
  69. Print "Last error was " + res + " - " + MBEDTLSError(res)
  70. End If
  71. client.Free()
  72. ssl.SessionReset()
  73. ' 5. Wait until a client connects
  74. Print " . Waiting for a remote connection ..."
  75. Local clientIp:String
  76. res = listen.Accept(client, clientIp)
  77. If res Then
  78. Print " failed~n ! TSSLNetContext.Accept() returned " + res
  79. Fail(res)
  80. End If
  81. ssl.SetBio(client, NetSend, NetRecv, Null)
  82. Print " ok"
  83. ' 6. Handshake
  84. Print " . Performing the SSL/TLS handshake..."
  85. res = ssl.Handshake()
  86. While res
  87. If res <> MBEDTLS_ERR_SSL_WANT_READ And res <> MBEDTLS_ERR_SSL_WANT_WRITE Then
  88. Print " failed~n ! TSSLContext.Handshake() returned " + res
  89. Continue reset
  90. End If
  91. res = ssl.Handshake()
  92. Wend
  93. Print " ok"
  94. ' 7. Read the HTTP Request
  95. Print " < Read from client:"
  96. Local buf:Byte[1024]
  97. While True
  98. ?bmxng
  99. Local Length:Size_T = buf.Length - 1
  100. MemClear(buf, Size_T(buf.Length))
  101. ?Not bmxng
  102. Local Length:Int = buf.Length - 1
  103. MemClear(buf, buf.Length)
  104. ?
  105. res = ssl.Read(buf, Length)
  106. If res = MBEDTLS_ERR_SSL_WANT_READ Or res = MBEDTLS_ERR_SSL_WANT_WRITE Then
  107. Continue
  108. End If
  109. If res <= 0 Then
  110. Select res
  111. Case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY
  112. Print " connection was closed gracefully"
  113. Case MBEDTLS_ERR_NET_CONN_RESET
  114. Print " connection was reset by peer"
  115. Default
  116. Print " TSSLContext.Read() returned " + res
  117. End Select
  118. Exit
  119. End If
  120. length = res
  121. Print length + " bytes read~n~n" + String.fromUTF8String(buf)
  122. If res > 0 Then
  123. Exit
  124. End If
  125. Wend
  126. ' 8. Write the 200 Response
  127. Print " > Write to client:"
  128. Local response:String = HTTP_RESPONSE.Replace("%s", ssl.GetCipherSuite())
  129. ?bmxng
  130. Local Length:Size_T = response.Length
  131. ?Not bmxng
  132. Local Length:Int = response.Length
  133. ?
  134. Local out:Byte Ptr = response.ToUtf8String()
  135. res = ssl.Write(out, Length)
  136. While res <= 0
  137. If res = MBEDTLS_ERR_NET_CONN_RESET Then
  138. Exit
  139. End If
  140. If res <> MBEDTLS_ERR_SSL_WANT_READ And res <> MBEDTLS_ERR_SSL_WANT_WRITE Then
  141. Exit
  142. End If
  143. res = ssl.Write(out, length)
  144. Wend
  145. length = res
  146. Print length + " bytes written"
  147. Print " . Closing the connection..."
  148. res = ssl.CloseNotify()
  149. While res < 0
  150. If res <> MBEDTLS_ERR_SSL_WANT_READ And res <> MBEDTLS_ERR_SSL_WANT_WRITE Then
  151. Print " failed~n ! TSSLContext.CloseNotify() returned " + res
  152. Continue reset
  153. End If
  154. res = ssl.CloseNotify()
  155. Wend
  156. Print " ok"
  157. res = 0
  158. Wend
  159. Function Fail(error:Int)
  160. End
  161. End Function
  162. Function myDebug(ctx:Object, level:Int, file:String, line:Int, str:String)
  163. Print file + ":" + line + ": " + str.Replace("~n","")
  164. End Function