mbedtls.bmx 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. '
  2. ' Copyright 2018-2023 Bruce A Henderson
  3. '
  4. ' Licensed under the Apache License, Version 2.0 (the "License");
  5. ' you may not use this file except in compliance with the License.
  6. ' You may obtain a copy of the License at
  7. '
  8. ' http://www.apache.org/licenses/LICENSE-2.0
  9. '
  10. ' Unless required by applicable law or agreed to in writing, software
  11. ' distributed under the License is distributed on an "AS IS" BASIS,
  12. ' WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. ' See the License for the specific language governing permissions and
  14. ' limitations under the License.
  15. '
  16. SuperStrict
  17. Rem
  18. bbdoc: mbed TLS SSL library.
  19. End Rem
  20. Module Net.mbedtls
  21. ModuleInfo "Version: 1.07"
  22. ModuleInfo "License: Apache 2.0"
  23. ModuleInfo "Copyright: Wrapper - 2018-2023 Bruce A Henderson"
  24. ModuleInfo "History: 1.07"
  25. ModuleInfo "History: Moved platform specific code to net.mbedtlscrypto"
  26. ModuleInfo "History: 1.06"
  27. ModuleInfo "History: Split crypto functions into net.mbedtlscrypto"
  28. ModuleInfo "History: 1.05"
  29. ModuleInfo "History: Update to mbedtls 3.0.0.74217ee"
  30. ModuleInfo "History: 1.04"
  31. ModuleInfo "History: Update to mbedtls 3.0.0."
  32. ModuleInfo "History: Fixed NG issues."
  33. ModuleInfo "History: 1.03"
  34. ModuleInfo "History: Update to mbedtls 2.13.0."
  35. ModuleInfo "History: 1.02"
  36. ModuleInfo "History: Update to mbedtls 2.7.1."
  37. ModuleInfo "History: Fixed 64-bit build."
  38. ModuleInfo "History: 1.01"
  39. ModuleInfo "History: Update to mbedtls 2.7.0."
  40. ModuleInfo "History: Applied win32 vsnprintf patch."
  41. ModuleInfo "History: 1.00"
  42. ModuleInfo "History: Initial Release."
  43. Import BRL.Stream
  44. Import "common.bmx"
  45. '
  46. ' Patch notes :
  47. ' For new releases, error.c and version_features.c are generated - can run 'make' to get one.
  48. '
  49. ' For older versions of win32, a patch was required for 2.7.0 use of vsnprintf
  50. ' see : https://github.com/ARMmbed/mbedtls/issues/355
  51. '
  52. Function MBEDTLSError:String(errornum:Int)
  53. Local buf:Byte[1024]
  54. mbedtls_strerror(errornum, buf, 1024)
  55. Return String.FromUTF8String(buf)
  56. End Function
  57. Rem
  58. bbdoc: Wrapper type for sockets.
  59. about: Currently backed by just a file descriptor, but might be more in the future
  60. (eg two file descriptors for combined IPv4 + IPv6 support, or additional structures
  61. for hand-made UDP demultiplexing).
  62. End Rem
  63. Type TNetContext
  64. Field contextPtr:Byte Ptr
  65. Rem
  66. bbdoc:
  67. End Rem
  68. Method Create:TNetContext()
  69. contextPtr = bmx_mbedtls_net_init()
  70. Return Self
  71. End Method
  72. Rem
  73. bbdoc: Creates a receiving socket on bindIp:port in the chosen protocol (#MBEDTLS_NET_PROTO_TCP or #MBEDTLS_NET_PROTO_UDP).
  74. returns: 0 if successful, or one of: #MBEDTLS_ERR_NET_SOCKET_FAILED, #MBEDTLS_ERR_NET_BIND_FAILED, #MBEDTLS_ERR_NET_LISTEN_FAILED.
  75. about: If bindIp is Null, all interfaces are bound
  76. Regardless of the protocol, opens the sockets and binds it. In addition, make the socket listening if protocol is TCP.
  77. End Rem
  78. Method Bind:Int(bindIp:String, port:String, proto:Int)
  79. Return bmx_mbedtls_net_bind(contextPtr, bindIp, port, proto)
  80. End Method
  81. Rem
  82. bbdoc: Initiates a connection with host:port in the given protocol (#MBEDTLS_NET_PROTO_TCP or #MBEDTLS_NET_PROTO_UDP)..
  83. returns: 0 if successful, or one of: #MBEDTLS_ERR_NET_SOCKET_FAILED, #MBEDTLS_ERR_NET_UNKNOWN_HOST, #MBEDTLS_ERR_NET_CONNECT_FAILED.
  84. about: Sets the socket in connected mode even with UDP.
  85. End Rem
  86. Method Connect:Int(host:String, port:String, proto:Int)
  87. Return bmx_mbedtls_net_connect(contextPtr, host, port, proto)
  88. End Method
  89. Rem
  90. bbdoc: Reads at most @length characters.
  91. returns: the number of bytes received, or a non-zero error code; with a non-blocking socket, #MBEDTLS_ERR_SSL_WANT_READ indicates read() would block.
  92. about: If no error occurs, the actual amount read is returned.
  93. End Rem
  94. Method Recv:Int(buf:Byte Ptr, Length:Size_T)
  95. Return bmx_mbedtls_net_recv(contextPtr, buf, Length)
  96. End Method
  97. Rem
  98. bbdoc: Read at most @length characters, blocking for at most @timeout milliseconds.
  99. returns: the number of bytes received, or a non-zero error code: #MBEDTLS_ERR_SSL_TIMEOUT if the operation timed out, #MBEDTLS_ERR_SSL_WANT_READ if interrupted by a signal.
  100. about: If no error occurs, the actual amount read is returned.
  101. This will block (until data becomes available or timeout is reached) even if the socket is
  102. set to non-blocking. Handling timeouts with non-blocking reads requires a different strategy.
  103. End Rem
  104. Method RecvTimeout:Int(buf:Byte Ptr, Length:Size_T, timeout:UInt)
  105. Return bmx_mbedtls_net_recv_timeout(contextPtr, buf, Length, timeout)
  106. End Method
  107. Rem
  108. bbdoc: Writes at most @length characters.
  109. returns: the number of bytes sent, or a non-zero error code; with a non-blocking socket, #MBEDTLS_ERR_SSL_WANT_WRITE indicates write() would block.
  110. about: If no error occurs, the actual amount read is returned.
  111. End Rem
  112. Method Send:Int(buf:Byte Ptr, Length:Size_T)
  113. Return bmx_mbedtls_net_send(contextPtr, buf, Length)
  114. End Method
  115. Rem
  116. bbdoc: Sets the socket blocking.
  117. returns: 0 if successful, or a non-zero error code
  118. End Rem
  119. Method SetBlock:Int()
  120. Return mbedtls_net_set_block(contextPtr)
  121. End Method
  122. Rem
  123. bbdoc: Sets the socket non-blocking.
  124. returns: 0 if successful, or a non-zero error code
  125. End Rem
  126. Method SetNonBlock:Int()
  127. Return mbedtls_net_set_nonblock(contextPtr)
  128. End Method
  129. Rem
  130. bbdoc: Portable usleep helper.
  131. about: Real amount of time slept will not be less than select()'s timeout granularity (typically, 10ms).
  132. End Rem
  133. Function USleep(usec:UInt)
  134. bmx_mbedtls_net_usleep(usec)
  135. End Function
  136. Rem
  137. bbdoc: Check if data is available on the socket.
  138. End Rem
  139. Method Poll:Int(rw:Int, timeout:Int)
  140. Return bmx_mbedtls_net_poll(contextPtr, rw, timeout)
  141. End Method
  142. Rem
  143. bbdoc:
  144. End Rem
  145. Method Accept:Int(client:TNetContext, ip:String Var)
  146. Local buf:Byte[256]
  147. Local Length:Size_T
  148. Local res:Int = mbedtls_net_accept(contextPtr, client.contextPtr, buf, 256, Varptr Length)
  149. If Length = 4
  150. ip = StrIPv4(buf)
  151. ElseIf Length = 16
  152. ' IPv6 works, I tested
  153. ' You have to bind to [::1] and you can then `curl -g -6 --insecure "https://[::1]"`
  154. ip = "IPv6"
  155. Else
  156. ip = "UNKNOWN"
  157. End If
  158. Return res
  159. End Method
  160. Function StrIPv4:String(buf:Byte Ptr)
  161. Return buf[0] + "." + buf[1] + "." + buf[2] + "." + buf[3]
  162. End Function
  163. Rem
  164. bbdoc:
  165. End Rem
  166. Method Free()
  167. If contextPtr Then
  168. bmx_mbedtls_net_free(contextPtr)
  169. End If
  170. End Method
  171. Method Delete()
  172. Free()
  173. If contextPtr Then
  174. bmx_mbedtls_net_delete(contextPtr)
  175. contextPtr = Null
  176. End If
  177. End Method
  178. End Type
  179. Rem
  180. bdoc:
  181. End Rem
  182. Type TSSLContext
  183. Field contextPtr:Byte Ptr
  184. Field _bioCtx:Object
  185. Field _cbSend:Int(ctx:Object, buf:Byte Ptr, Length:Size_T)
  186. Field _cbRecv:Int(ctx:Object, buf:Byte Ptr, Length:Size_T)
  187. Field _cbTimeout:Int(ctx:Object, buf:Byte Ptr, Length:Size_T, timeout:Int)
  188. Field _cbTimerSet(ctx:Object, intMs:UInt, finMs:UInt)
  189. Field _timerCtx:Object
  190. Field _cbTimerGet:Int(ctx:Object)
  191. Rem
  192. bbdoc:
  193. End Rem
  194. Method Create:TSSLContext()
  195. contextPtr = bmx_mbedtls_ssl_init()
  196. Return Self
  197. End Method
  198. Rem
  199. bbdoc:
  200. End Rem
  201. Method Setup:Int(config:TSSLConfig)
  202. Return mbedtls_ssl_setup(contextPtr, config.configPtr)
  203. End Method
  204. Rem
  205. bbdoc: Resets an already initialized SSL context for re-use while retaining application-set variables, function pointers and data.
  206. returns: 0 if successful, or #MBEDTLS_ERR_SSL_ALLOC_FAILED, #MBEDTLS_ERR_SSL_HW_ACCEL_FAILED or #MBEDTLS_ERR_SSL_COMPRESSION_FAILED
  207. End Rem
  208. Method SessionReset:Int()
  209. Return mbedtls_ssl_session_reset(contextPtr)
  210. End Method
  211. Rem
  212. bbdoc: Sets the underlying BIO callbacks for write, read and read-with-timeout.
  213. End Rem
  214. Method SetBio(ctx:Object, cbSend:Int(ctx:Object, buf:Byte Ptr, Length:Size_T), ..
  215. cbRecv:Int(ctx:Object, buf:Byte Ptr, Length:Size_T), ..
  216. cbTimeout:Int(ctx:Object, buf:Byte Ptr, Length:Size_T, timeout:Int))
  217. _bioCtx = ctx
  218. _cbSend = cbSend
  219. _cbRecv = cbRecv
  220. _cbTimeout = cbTimeout
  221. Local cbs:Byte Ptr
  222. Local cbr:Byte Ptr
  223. Local cbt:Byte Ptr
  224. If cbSend Then
  225. cbs = _sendCallback
  226. End If
  227. If cbRecv Then
  228. cbr = _recvCallback
  229. End If
  230. If cbTimeout Then
  231. cbt = _timeoutCallback
  232. End If
  233. mbedtls_ssl_set_bio(contextPtr, Self, cbs, cbr, cbt)
  234. End Method
  235. Rem
  236. bbdoc: Sets the timer callbacks (Mandatory for DTLS.)
  237. End Rem
  238. Method SetTimerCallbacks(ctx:Object, Set(ctx:Object, intMs:UInt, finMs:UInt), get:Int(ctx:Object))
  239. _timerCtx = ctx
  240. _cbTimerSet = Set
  241. _cbTimerGet = get
  242. mbedtls_ssl_set_timer_cb(contextPtr, Self, _timerSetCallback, _timerGetCallback)
  243. End Method
  244. Rem
  245. bbdoc: Performs the SSL handshake.
  246. returns: 0 if successful, or #MBEDTLS_ERR_SSL_WANT_READ or #MBEDTLS_ERR_SSL_WANT_WRITE, or #MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED (see below), or a specific SSL error code.
  247. about: If this method returns something other than 0 or #MBEDTLS_ERR_SSL_WANT_READ/WRITE, then
  248. the ssl context becomes unusable, and you should either free it or call SessionReset() on it
  249. before re-using it for a new connection; the current connection must be closed.
  250. If DTLS is in use, then you may choose to handle #MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED specially
  251. for logging purposes, as it is an expected return value rather than an actual error, but you still
  252. need to reset/free the context.
  253. End Rem
  254. Method Handshake:Int()
  255. Return mbedtls_ssl_handshake(contextPtr)
  256. End Method
  257. Rem
  258. bbdoc: Performs a single step of the SSL handshake.
  259. returns: 0 if successful, or #MBEDTLS_ERR_SSL_WANT_READ or #MBEDTLS_ERR_SSL_WANT_WRITE, or a specific SSL error code.
  260. End Rem
  261. Method HandshakeStep:Int()
  262. Return mbedtls_ssl_handshake_step(contextPtr)
  263. End Method
  264. Rem
  265. bbdoc: Initiates an SSL renegotiation on the running connection.
  266. about: Client: perform the renegotiation right now.
  267. Server: request renegotiation, which will be performed during the next call to
  268. #Read() if honored by client.
  269. End Rem
  270. Method Renegotiate:Int()
  271. Return mbedtls_ssl_renegotiate(contextPtr)
  272. End Method
  273. Rem
  274. bbdoc: Reads at most @length application data bytes.
  275. returns: the number of bytes read, or 0 for EOF, or #MBEDTLS_ERR_SSL_WANT_READ or #MBEDTLS_ERR_SSL_WANT_WRITE, or #MBEDTLS_ERR_SSL_CLIENT_RECONNECT (see below), or another negative error code.
  276. about: If this method returns something other than a positive value or
  277. #MBEDTLS_ERR_SSL_WANT_READ/WRITE or #MBEDTLS_ERR_SSL_CLIENT_RECONNECT, then the ssl
  278. context becomes unusable, and you should either free it or call #SessonReset() on it
  279. before re-using it for a new connection; the current connection must be closed.
  280. End Rem
  281. Method Read:Int(buf:Byte Ptr, Length:Size_T)
  282. Return bmx_mbedtls_ssl_read(contextPtr, buf, Length)
  283. End Method
  284. Method Write:Int(buf:Byte Ptr, Length:Size_T)
  285. Return bmx_mbedtls_ssl_write(contextPtr, buf, Length)
  286. End Method
  287. Rem
  288. bbdoc: Returns the number of data bytes available to read.
  289. returns: How many bytes are available in the read buffer.
  290. End Rem
  291. Method GetBytesAvail:Int()
  292. Return mbedtls_ssl_get_bytes_avail(contextPtr)
  293. End Method
  294. Rem
  295. bbdoc: Returns the result of the certificate verification.
  296. returns: 0 if successful, -1 if result is not available (eg because the handshake was aborted too early), or a combination of BADCERT_xxx and BADCRL_xxx flags, see x509.h
  297. End Rem
  298. Method GetVerifyResult:Int()
  299. Return mbedtls_ssl_get_verify_result(contextPtr)
  300. End Method
  301. Rem
  302. bbdoc: Returns the name of the current ciphersuite.
  303. End Rem
  304. Method GetCipherSuite:String()
  305. Return String.FromUTF8String(mbedtls_ssl_get_ciphersuite(contextPtr))
  306. End Method
  307. Rem
  308. bbdoc: Returns the current SSL version (SSLv3/TLSv1/etc).
  309. End Rem
  310. Method GetVersion:String()
  311. Return String.FromUTF8String(mbedtls_ssl_get_version(contextPtr))
  312. End Method
  313. Rem
  314. bbdoc: Returns the (maximum) number of bytes added by the record layer: header + encryption/MAC overhead (inc. padding)
  315. returns: Current maximum record expansion in bytes, or #MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE if compression is enabled, which makes expansion much less predictable
  316. End Rem
  317. Method GetRecordExpansion:Int()
  318. Return mbedtls_ssl_get_record_expansion(contextPtr)
  319. End Method
  320. Rem
  321. bbdoc: Notifies the peer that the connection is being closed.
  322. returns: 0 if successful, or a specific SSL error code.
  323. End Rem
  324. Method CloseNotify:Int()
  325. Return mbedtls_ssl_close_notify(contextPtr)
  326. End Method
  327. Function _sendCallback:Int(ctx:TSSLContext, buf:Byte Ptr, Length:Size_T)
  328. If TNetContext(ctx._bioCtx) Then
  329. Return bmx_mbedtls_net_cbsend(ctx._cbSend, TNetContext(ctx._bioCtx).contextPtr, buf, Length)
  330. Else
  331. Return ctx._cbSend(ctx._bioCtx, buf, Length)
  332. End If
  333. End Function
  334. Function _recvCallback:Int(ctx:TSSLContext, buf:Byte Ptr, Length:Size_T)
  335. If TNetContext(ctx._bioCtx) Then
  336. Return bmx_mbedtls_net_cbrecv(ctx._cbRecv, TNetContext(ctx._bioCtx).contextPtr, buf, Length)
  337. Else
  338. Return ctx._cbRecv(ctx._bioCtx, buf, Length)
  339. End If
  340. End Function
  341. Function _timeoutCallback:Int(ctx:TSSLContext, buf:Byte Ptr, Length:Size_T, timeout:Int)
  342. If TNetContext(ctx._bioCtx) Then
  343. Return bmx_mbedtls_net_cbtimeout(ctx._cbTimeout, TNetContext(ctx._bioCtx).contextPtr, buf, Length, timeout)
  344. Else
  345. Return ctx._cbTimeout(ctx._bioCtx, buf, Length, timeout)
  346. End If
  347. End Function
  348. Function _timerSetCallback(ctx:TSSLContext, intMs:UInt, finMs:UInt)
  349. ctx._cbTimerSet(ctx._timerCtx, intMs, finMs)
  350. End Function
  351. Function _timerGetCallback:Int(ctx:TSSLContext)
  352. Return ctx._cbTimerGet(ctx._timerCtx)
  353. End Function
  354. Method Delete()
  355. If contextPtr Then
  356. bmx_mbedtls_ssl_free(contextPtr)
  357. contextPtr = Null
  358. End If
  359. End Method
  360. End Type
  361. Rem
  362. bbdoc:
  363. End Rem
  364. Type TSSLConfig
  365. Field configPtr:Byte Ptr
  366. Field _cbDbg(ctx:Object, level:Int, file:String, line:Int, str:String)
  367. Field _dbgCtx:Object
  368. Rem
  369. bbdoc:
  370. End Rem
  371. Method Create:TSSLConfig()
  372. configPtr = bmx_mbedtls_ssl_config_init()
  373. Return Self
  374. End Method
  375. Rem
  376. bbdoc:
  377. End Rem
  378. Method Defaults:Int(endpoint:Int = MBEDTLS_SSL_IS_SERVER, transport:Int = MBEDTLS_SSL_TRANSPORT_STREAM, preset:Int = MBEDTLS_SSL_PRESET_DEFAULT)
  379. Return mbedtls_ssl_config_defaults(configPtr, endpoint, transport, preset)
  380. End Method
  381. Rem
  382. bbdoc: Sets the current endpoint type.
  383. about: One of #MBEDTLS_SSL_IS_CLIENT or #MBEDTLS_SSL_IS_SERVER.
  384. End Rem
  385. Method SetEndPoint(endpoint:Int)
  386. mbedtls_ssl_conf_endpoint(configPtr, endpoint)
  387. End Method
  388. Rem
  389. bbdoc: Set the transport type (TLS or DTLS). Default: TLS
  390. about: #MBEDTLS_SSL_TRANSPORT_STREAM for TLS, #MBEDTLS_SSL_TRANSPORT_DATAGRAM for DTLS.
  391. For DTLS, you must either provide a recv callback that doesn't block, or one that handles
  392. timeouts, see TSSLContext.SetBio(). You also need to provide timer callbacks with
  393. TSSLContext.SetTimerCallbacks().
  394. End Rem
  395. Method SetTransport(transport:Int)
  396. mbedtls_ssl_conf_transport(configPtr, transport)
  397. End Method
  398. Rem
  399. bbdoc:
  400. End Rem
  401. Method SetAuthMode(authMode:Int)
  402. End Method
  403. Rem
  404. bbdoc:
  405. End Rem
  406. Method RNG(cbRandom:Int(rng:Object, output:Byte Ptr, length:Size_T), rng:Object)
  407. If TRandContext(rng) Then
  408. mbedtls_ssl_conf_rng(configPtr, cbRandom, TRandContext(rng).contextPtr)
  409. Else
  410. bmx_mbedtls_ssl_conf_rng(configPtr, cbRandom, rng)
  411. End If
  412. End Method
  413. Rem
  414. bbdoc:
  415. End Rem
  416. Method DBG(cbDebug(ctx:Object, level:Int, file:String, line:Int, str:String), ctx:Object)
  417. _cbDbg = cbDebug
  418. _dbgCtx = ctx
  419. mbedtls_ssl_conf_dbg(configPtr, _callbackDebug, Self)
  420. End Method
  421. Rem
  422. bbdoc: Set the threshold error level to handle globally all debug output.
  423. about: Debug messages that have a level over the threshold value are discarded.
  424. (Default value: 0 = No debug )
  425. End Rem
  426. Function SetDebugThreshold(threshold:Int)
  427. mbedtls_debug_set_threshold(threshold)
  428. End Function
  429. Rem
  430. bbdoc:
  431. End Rem
  432. Method CaChain(chain:TX509Cert, crl:TX509CertRevList = Null)
  433. If crl Then
  434. mbedtls_ssl_conf_ca_chain(configPtr, chain.certPtr, crl.certPtr)
  435. Else
  436. mbedtls_ssl_conf_ca_chain(configPtr, chain.certPtr, Null)
  437. End If
  438. End Method
  439. Function _callbackDebug(ctx:Object, level:Int, file:Byte Ptr, line:Int, str:Byte Ptr)
  440. Local f:String = String.FromUTF8String(file)
  441. Local s:String = String.FromUTF8String(str)
  442. Local config:TSSLConfig = TSSLConfig(ctx)
  443. config._cbDbg(config._dbgCtx, level, f, line, s)
  444. End Function
  445. Rem
  446. bbdoc:
  447. End Rem
  448. Method OwnCert:Int(cert:TX509Cert, pkContext:TPkContext)
  449. Return mbedtls_ssl_conf_own_cert(configPtr, cert.certPtr, pkContext.contextPtr)
  450. End Method
  451. Method Delete()
  452. If configPtr Then
  453. bmx_mbedtls_ssl_config_free(configPtr)
  454. configPtr = Null
  455. End If
  456. End Method
  457. End Type
  458. Type TSSLSession
  459. Field sessionPtr:Byte Ptr
  460. End Type
  461. Rem
  462. bbdoc: Container for an X.509 certificate.
  463. about: Ther certificate may be chained.
  464. End Rem
  465. Type TX509Cert
  466. Field certPtr:Byte Ptr
  467. Field _owned:Int
  468. Function _create:TX509Cert(certPtr:Byte Ptr)
  469. If certPtr Then
  470. Local this:TX509Cert = New TX509Cert
  471. this.certPtr = certPtr
  472. Return this
  473. End If
  474. End Function
  475. Rem
  476. bbdoc:
  477. End Rem
  478. Method Create:TX509Cert()
  479. certPtr = bmx_mbedtls_x509_crt_init()
  480. _owned = True
  481. Return Self
  482. End Method
  483. Rem
  484. bbdoc: Parses one or more certificates and add them to the chained list.
  485. about: Parses permissively. If some certificates can be parsed, the result is the number
  486. of failed certificates it encountered. If none complete correctly, the first error is returned.
  487. End Rem
  488. Method Parse:Int(buf:Byte Ptr, buflen:Int)
  489. Return bmx_mbedtls_x509_crt_parse(certPtr, buf, buflen)
  490. End Method
  491. Rem
  492. bbdoc: Parses a file with one or more certificates (usually .pem extension)
  493. End Rem
  494. Method ParseFile:Int(path:String)
  495. Local buf:Byte[] = LoadByteArray(path)
  496. ' Have to ensure there is a null terminator
  497. buf = buf[..buf.length + 1]
  498. buf[buf.length - 1] = 0
  499. Return Parse(buf, buf.length)
  500. End Method
  501. Method Delete()
  502. If certPtr And _owned Then
  503. bmx_mbedtls_x509_crt_free(certPtr)
  504. certPtr = Null
  505. End If
  506. End Method
  507. End Type
  508. Type TX509CertRevList
  509. Field certPtr:Byte Ptr
  510. End Type
  511. Rem
  512. bbdoc:
  513. End Rem
  514. Type TPkContext
  515. Field contextPtr:Byte Ptr
  516. Field _cbRandom:Byte Ptr
  517. Field _rng:Byte Ptr
  518. Method Create:TPkContext()
  519. contextPtr = bmx_mbedtls_pk_init()
  520. _cbRandom = Null
  521. _rng = Null
  522. Return Self
  523. End Method
  524. Rem
  525. bbdoc:
  526. End Rem
  527. Method ParseKey:Int(key:Byte Ptr, keylen:Int, pwd:Byte Ptr = Null, pwdlen:Int = 0)
  528. Return bmx_mbedtls_pk_parse_key(contextPtr, key, keylen, pwd, pwdlen, _cbRandom, _rng)
  529. End Method
  530. Rem
  531. bbdoc:
  532. End Rem
  533. Method ParseKeyString:Int(key:String, pwd:String = "")
  534. Return bmx_mbedtls_pk_parse_key_string(contextPtr, key, pwd, _cbRandom, _rng)
  535. End Method
  536. Rem
  537. bbdoc: Load a private key from file (usually .pem extension)
  538. End Rem
  539. Method ParseKeyFile:Int(path:String, pwd:String = "")
  540. Local key:String = LoadString(path)
  541. Return ParseKeyString(key, pwd)
  542. End Method
  543. Rem
  544. bbdoc: Supply a seeded RNG; skip this unless you ever manage to somehow run into MBEDTLS_ERR_ECP_BAD_INPUT_DATA
  545. End Rem
  546. Method RNG(cbRandom:Int(rng:Object, output:Byte Ptr, length:Size_T), rng:Object)
  547. If TRandContext(rng) Then
  548. _rng = TRandContext(rng).contextPtr
  549. Else
  550. _rng = rng
  551. End If
  552. _cbRandom = cbRandom
  553. End Method
  554. Method Delete()
  555. If contextPtr Then
  556. bmx_mbedtls_pk_free(contextPtr)
  557. contextPtr = Null
  558. End If
  559. End Method
  560. End Type
  561. Rem
  562. bbdoc:
  563. End Rem
  564. Type TSSLCacheContext
  565. Field contextPtr:Byte Ptr
  566. Function Get:Int()
  567. End Function
  568. End Type
  569. Type TEntropyContext
  570. Field contextPtr:Byte Ptr
  571. Method Create:TEntropyContext()
  572. contextPtr = bmx_mbedtls_entropy_init()
  573. Return Self
  574. End Method
  575. End Type
  576. Rem
  577. bbdoc: Counter mode Deterministic Random Byte Generator
  578. End Rem
  579. Type TRandContext
  580. Field contextPtr:Byte Ptr
  581. Method Create:TRandContext()
  582. contextPtr = bmx_mbedtls_ctr_drbg_init()
  583. Return Self
  584. End Method
  585. Rem
  586. bbdoc:
  587. End Rem
  588. Method Seed:Int(cbEntropy:Int(entropy:Object, buf:Byte Ptr, length:Size_T), entropy:Object, custom:Byte Ptr = Null, length:Size_T = 0)
  589. If TEntropyContext(entropy) Then
  590. Return mbedtls_ctr_drbg_seed(contextPtr, cbEntropy, TEntropyContext(entropy).contextPtr, custom, length)
  591. Else
  592. Return bmx_mbedtls_ctr_drbg_seed(contextPtr, cbEntropy, entropy, custom, length)
  593. End If
  594. End Method
  595. Method Delete()
  596. If contextPtr Then
  597. bmx_mbedtls_ctr_drbg_free(contextPtr)
  598. contextPtr = Null
  599. End If
  600. End Method
  601. End Type