2
0

wolfssl.adb 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. -- wolfssl.adb
  2. --
  3. -- Copyright (C) 2006-2023 wolfSSL Inc.
  4. --
  5. -- This file is part of wolfSSL.
  6. --
  7. -- wolfSSL is free software; you can redistribute it and/or modify
  8. -- it under the terms of the GNU General Public License as published by
  9. -- the Free Software Foundation; either version 2 of the License, or
  10. -- (at your option) any later version.
  11. --
  12. -- wolfSSL is distributed in the hope that it will be useful,
  13. -- but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. -- GNU General Public License for more details.
  16. --
  17. -- You should have received a copy of the GNU General Public License
  18. -- along with this program; if not, write to the Free Software
  19. -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. --
  21. with Interfaces.C.Strings;
  22. package body WolfSSL is
  23. subtype size_t is Interfaces.C.size_t; use type size_t;
  24. subtype long is Interfaces.C.long;
  25. subtype unsigned_long is Interfaces.C.unsigned_long;
  26. WOLFSSL_SUCCESS : constant int := Get_WolfSSL_Success;
  27. function Initialize_WolfSSL return int with
  28. Convention => C,
  29. External_Name => "wolfSSL_Init",
  30. Import => True;
  31. function Finalize_WolfSSL return int with
  32. Convention => C,
  33. External_Name => "wolfSSL_Cleanup",
  34. Import => True;
  35. function Initialize return Subprogram_Result is
  36. Result : constant int := Initialize_WolfSSL;
  37. begin
  38. return Subprogram_Result (Result);
  39. end Initialize;
  40. function Finalize return Subprogram_Result is
  41. Result : constant int := Finalize_WolfSSL;
  42. begin
  43. return Subprogram_Result (Result);
  44. end Finalize;
  45. function Is_Valid (Context : Context_Type) return Boolean is
  46. begin
  47. return Context /= null;
  48. end Is_Valid;
  49. function WolfTLSv1_2_Server_Method return Method_Type with
  50. Convention => C,
  51. External_Name => "wolfTLSv1_2_server_method",
  52. Import => True;
  53. function TLSv1_2_Server_Method return Method_Type is
  54. begin
  55. return WolfTLSv1_2_Server_Method;
  56. end TLSv1_2_Server_Method;
  57. function WolfTLSv1_2_Client_Method return Method_Type with
  58. Convention => C,
  59. External_Name => "wolfTLSv1_2_client_method",
  60. Import => True;
  61. function TLSv1_2_Client_Method return Method_Type is
  62. begin
  63. return WolfTLSv1_2_Client_Method;
  64. end TLSv1_2_Client_Method;
  65. function WolfTLSv1_3_Server_Method return Method_Type with
  66. Convention => C,
  67. External_Name => "wolfTLSv1_3_server_method",
  68. Import => True;
  69. function TLSv1_3_Server_Method return Method_Type is
  70. begin
  71. return WolfTLSv1_3_Server_Method;
  72. end TLSv1_3_Server_Method;
  73. function WolfTLSv1_3_Client_Method return Method_Type with
  74. Convention => C,
  75. External_Name => "wolfTLSv1_3_client_method",
  76. Import => True;
  77. function TLSv1_3_Client_Method return Method_Type is
  78. begin
  79. return WolfTLSv1_3_Client_Method;
  80. end TLSv1_3_Client_Method;
  81. function WolfSSL_CTX_new (Method : Method_Type)
  82. return Context_Type with
  83. Convention => C, External_Name => "wolfSSL_CTX_new", Import => True;
  84. procedure Create_Context (Method : Method_Type;
  85. Context : out Context_Type) is
  86. begin
  87. Context := WolfSSL_CTX_new (Method);
  88. end Create_Context;
  89. procedure WolfSSL_CTX_free (Context : Context_Type) with
  90. Convention => C, External_Name => "wolfSSL_CTX_free", Import => True;
  91. procedure Free (Context : in out Context_Type) is
  92. begin
  93. WolfSSL_CTX_free (Context);
  94. Context := null;
  95. end Free;
  96. type Opaque_X509_Store_Context is limited null record;
  97. type X509_Store_Context is access Opaque_X509_Store_Context with
  98. Convention => C;
  99. type Verify_Callback is access function
  100. (A : int;
  101. Context : X509_Store_Context)
  102. return int
  103. with Convention => C;
  104. procedure WolfSSL_CTX_Set_Verify (Context : Context_Type;
  105. Mode : int;
  106. Callback : Verify_Callback) with
  107. Convention => C,
  108. External_Name => "wolfSSL_CTX_set_verify",
  109. Import => True;
  110. -- This function sets the verification method for remote peers and
  111. -- also allows a verify callback to be registered with the SSL
  112. -- context. The verify callback will be called only when a
  113. -- verification failure has occurred. If no verify callback is
  114. -- desired, the NULL pointer can be used for verify_callback.
  115. -- The verification mode of peer certificates is a logically OR'd
  116. -- list of flags. The possible flag values include:
  117. -- SSL_VERIFY_NONE Client mode: the client will not verify the
  118. -- certificate received from the server and the handshake will
  119. -- continue as normal. Server mode: the server will not send a
  120. -- certificate request to the client. As such, client verification
  121. -- will not be enabled. SSL_VERIFY_PEER Client mode: the client will
  122. -- verify the certificate received from the server during the
  123. -- handshake. This is turned on by default in wolfSSL, therefore,
  124. -- using this option has no effect. Server mode: the server will send
  125. -- a certificate request to the client and verify the client
  126. -- certificate received. SSL_VERIFY_FAIL_IF_NO_PEER_CERT Client mode:
  127. -- no effect when used on the client side. Server mode:
  128. -- the verification will fail on the server side if the client fails
  129. -- to send a certificate when requested to do so (when using
  130. -- SSL_VERIFY_PEER on the SSL server).
  131. -- SSL_VERIFY_FAIL_EXCEPT_PSK Client mode: no effect when used on
  132. -- the client side. Server mode: the verification is the same as
  133. -- SSL_VERIFY_FAIL_IF_NO_PEER_CERT except in the case of a
  134. -- PSK connection. If a PSK connection is being made then the
  135. -- connection will go through without a peer cert.
  136. function "&" (Left, Right : Mode_Type) return Mode_Type is
  137. L : constant Unsigned_32 := Unsigned_32 (Left);
  138. R : constant Unsigned_32 := Unsigned_32 (Right);
  139. begin
  140. return Mode_Type (L and R);
  141. end "&";
  142. procedure Set_Verify (Context : Context_Type;
  143. Mode : Mode_Type) is
  144. begin
  145. WolfSSL_CTX_Set_Verify (Context => Context,
  146. Mode => int (Mode),
  147. Callback => null);
  148. end Set_Verify;
  149. function Use_Certificate_File (Context : Context_Type;
  150. File : char_array;
  151. Format : int)
  152. return int with
  153. Convention => C,
  154. External_Name => "wolfSSL_CTX_use_certificate_file",
  155. Import => True;
  156. function Use_Certificate_File (Context : Context_Type;
  157. File : String;
  158. Format : File_Format)
  159. return Subprogram_Result is
  160. Ctx : constant Context_Type := Context;
  161. C : size_t;
  162. F : char_array (1 .. File'Length + 1);
  163. Result : int;
  164. begin
  165. Interfaces.C.To_C (Item => File,
  166. Target => F,
  167. Count => C,
  168. Append_Nul => True);
  169. Result := Use_Certificate_File (Ctx, F (1 .. C), int (Format));
  170. return Subprogram_Result (Result);
  171. end Use_Certificate_File;
  172. function Use_Certificate_Buffer (Context : Context_Type;
  173. Input : char_array;
  174. Size : long;
  175. Format : int)
  176. return int with
  177. Convention => C,
  178. External_Name => "wolfSSL_CTX_use_certificate_buffer",
  179. Import => True;
  180. function Use_Certificate_Buffer (Context : Context_Type;
  181. Input : char_array;
  182. Format : File_Format)
  183. return Subprogram_Result is
  184. Result : int;
  185. begin
  186. Result := Use_Certificate_Buffer (Context, Input,
  187. Input'Length, int (Format));
  188. return Subprogram_Result (Result);
  189. end Use_Certificate_Buffer;
  190. function Use_Private_Key_File (Context : Context_Type;
  191. File : char_array;
  192. Format : int)
  193. return int with
  194. Convention => C,
  195. External_Name => "wolfSSL_CTX_use_PrivateKey_file",
  196. Import => True;
  197. function Use_Private_Key_File (Context : Context_Type;
  198. File : String;
  199. Format : File_Format)
  200. return Subprogram_Result is
  201. Ctx : constant Context_Type := Context;
  202. C : size_t;
  203. F : char_array (1 .. File'Length + 1);
  204. Result : int;
  205. begin
  206. Interfaces.C.To_C (Item => File,
  207. Target => F,
  208. Count => C,
  209. Append_Nul => True);
  210. Result := Use_Private_Key_File (Ctx, F (1 .. C), int (Format));
  211. return Subprogram_Result (Result);
  212. end Use_Private_Key_File;
  213. function Use_Private_Key_Buffer (Context : Context_Type;
  214. Input : char_array;
  215. Size : long;
  216. Format : int)
  217. return int with
  218. Convention => C,
  219. External_Name => "wolfSSL_CTX_use_PrivateKey_buffer",
  220. Import => True;
  221. function Use_Private_Key_Buffer (Context : Context_Type;
  222. Input : Byte_Array;
  223. Format : File_Format)
  224. return Subprogram_Result is
  225. Result : int;
  226. begin
  227. Result := Use_Private_Key_Buffer (Context, Input,
  228. Input'Length, int (Format));
  229. return Subprogram_Result (Result);
  230. end Use_Private_Key_Buffer;
  231. function Load_Verify_Locations1
  232. (Context : Context_Type;
  233. File : char_array;
  234. Path : char_array) return int with
  235. Convention => C,
  236. External_Name => "wolfSSL_CTX_load_verify_locations",
  237. Import => True;
  238. -- This function loads PEM-formatted CA certificate files into
  239. -- the SSL context (WOLFSSL_CTX). These certificates will be treated
  240. -- as trusted root certificates and used to verify certs received
  241. -- from peers during the SSL handshake. The root certificate file,
  242. -- provided by the file argument, may be a single certificate or a
  243. -- file containing multiple certificates. If multiple CA certs are
  244. -- included in the same file, wolfSSL will load them in the same order
  245. -- they are presented in the file. The path argument is a pointer to
  246. -- the name of a directory that contains certificates of trusted
  247. -- root CAs. If the value of file is not NULL, path may be specified
  248. -- as NULL if not needed. If path is specified and NO_WOLFSSL_DIR was
  249. -- not defined when building the library, wolfSSL will load all
  250. -- CA certificates located in the given directory. This function will
  251. -- attempt to load all files in the directory. This function expects
  252. -- PEM formatted CERT_TYPE file with header "--BEGIN CERTIFICATE--".
  253. subtype char_array_ptr is Interfaces.C.Strings.char_array_access;
  254. function Load_Verify_Locations2
  255. (Context : Context_Type;
  256. File : char_array;
  257. Path : char_array_ptr) return int with
  258. Convention => C,
  259. External_Name => "wolfSSL_CTX_load_verify_locations",
  260. Import => True;
  261. function Load_Verify_Locations3
  262. (Context : Context_Type;
  263. File : char_array_ptr;
  264. Path : char_array) return int with
  265. Convention => C,
  266. External_Name => "wolfSSL_CTX_load_verify_locations",
  267. Import => True;
  268. function Load_Verify_Locations4
  269. (Context : Context_Type;
  270. File : char_array_ptr;
  271. Path : char_array_ptr) return int with
  272. Convention => C,
  273. External_Name => "wolfSSL_CTX_load_verify_locations",
  274. Import => True;
  275. function Load_Verify_Locations (Context : Context_Type;
  276. File : String;
  277. Path : String)
  278. return Subprogram_Result is
  279. Ctx : constant Context_Type := Context;
  280. FC : size_t; -- File Count, specifies the characters used in F.
  281. F : aliased char_array := (1 .. File'Length + 1 => '#');
  282. PC : size_t; -- Path Count, specifies the characters used in P.
  283. P : aliased char_array := (1 .. Path'Length + 1 => '#');
  284. Result : int;
  285. begin
  286. if File = "" then
  287. if Path = "" then
  288. Result := Load_Verify_Locations4 (Ctx, null, null);
  289. else
  290. Interfaces.C.To_C (Item => Path,
  291. Target => P,
  292. Count => PC,
  293. Append_Nul => True);
  294. Result := Load_Verify_Locations3 (Ctx, null, P);
  295. end if;
  296. else
  297. Interfaces.C.To_C (Item => File,
  298. Target => F,
  299. Count => FC,
  300. Append_Nul => True);
  301. if Path = "" then
  302. Result := Load_Verify_Locations2 (Ctx, F, null);
  303. else
  304. Interfaces.C.To_C (Item => Path,
  305. Target => P,
  306. Count => PC,
  307. Append_Nul => True);
  308. Interfaces.C.To_C (Item => Path,
  309. Target => P,
  310. Count => PC,
  311. Append_Nul => True);
  312. Result := Load_Verify_Locations1 (Context => Ctx,
  313. File => F,
  314. Path => P);
  315. end if;
  316. end if;
  317. return Subprogram_Result (Result);
  318. end Load_Verify_Locations;
  319. function Load_Verify_Buffer
  320. (Context : Context_Type;
  321. Input : char_array;
  322. Size : int;
  323. Format : int) return int with
  324. Convention => C,
  325. External_Name => "wolfSSL_CTX_load_verify_buffer",
  326. Import => True;
  327. function Load_Verify_Buffer (Context : Context_Type;
  328. Input : Byte_Array;
  329. Format : File_Format)
  330. return Subprogram_Result is
  331. Result : int;
  332. begin
  333. Result := Load_Verify_Buffer (Context => Context,
  334. Input => Input,
  335. Size => Input'Length,
  336. Format => int(Format));
  337. return Subprogram_Result (Result);
  338. end Load_Verify_Buffer;
  339. function Is_Valid (Ssl : WolfSSL_Type) return Boolean is
  340. begin
  341. return Ssl /= null;
  342. end Is_Valid;
  343. function WolfSSL_New (Context : Context_Type)
  344. return WolfSSL_Type with
  345. Convention => C,
  346. External_Name => "wolfSSL_new",
  347. Import => True;
  348. procedure Create_WolfSSL (Context : Context_Type;
  349. Ssl : out WolfSSL_Type) is
  350. begin
  351. Ssl := WolfSSL_New (Context);
  352. end Create_WolfSSL;
  353. function Use_Certificate_File (Ssl : WolfSSL_Type;
  354. File : char_array;
  355. Format : int)
  356. return int with
  357. Convention => C,
  358. External_Name => "wolfSSL_use_certificate_file",
  359. Import => True;
  360. function Use_Certificate_File (Ssl : WolfSSL_Type;
  361. File : String;
  362. Format : File_Format)
  363. return Subprogram_Result is
  364. C : size_t;
  365. F : char_array (1 .. File'Length + 1);
  366. Result : int;
  367. begin
  368. Interfaces.C.To_C (Item => File,
  369. Target => F,
  370. Count => C,
  371. Append_Nul => True);
  372. Result := Use_Certificate_File (Ssl, F (1 .. C), int (Format));
  373. return Subprogram_Result (Result);
  374. end Use_Certificate_File;
  375. function Use_Certificate_Buffer (Ssl : WolfSSL_Type;
  376. Input : char_array;
  377. Size : long;
  378. Format : int)
  379. return int with
  380. Convention => C,
  381. External_Name => "wolfSSL_use_certificate_buffer",
  382. Import => True;
  383. function Use_Certificate_Buffer (Ssl : WolfSSL_Type;
  384. Input : char_array;
  385. Format : File_Format)
  386. return Subprogram_Result is
  387. Result : int;
  388. begin
  389. Result := Use_Certificate_Buffer (Ssl, Input,
  390. Input'Length, int (Format));
  391. return Subprogram_Result (Result);
  392. end Use_Certificate_Buffer;
  393. function Use_Private_Key_File (Ssl : WolfSSL_Type;
  394. File : char_array;
  395. Format : int)
  396. return int with
  397. Convention => C,
  398. External_Name => "wolfSSL_use_PrivateKey_file",
  399. Import => True;
  400. function Use_Private_Key_File (Ssl : WolfSSL_Type;
  401. File : String;
  402. Format : File_Format)
  403. return Subprogram_Result is
  404. C : size_t;
  405. F : char_array (1 .. File'Length + 1);
  406. Result : int;
  407. begin
  408. Interfaces.C.To_C (Item => File,
  409. Target => F,
  410. Count => C,
  411. Append_Nul => True);
  412. Result := Use_Private_Key_File (Ssl, F (1 .. C), int (Format));
  413. return Subprogram_Result (Result);
  414. end Use_Private_Key_File;
  415. function Use_Private_Key_Buffer (Ssl : WolfSSL_Type;
  416. Input : char_array;
  417. Size : long;
  418. Format : int)
  419. return int with
  420. Convention => C,
  421. External_Name => "wolfSSL_use_PrivateKey_buffer",
  422. Import => True;
  423. function Use_Private_Key_Buffer (Ssl : WolfSSL_Type;
  424. Input : Byte_Array;
  425. Format : File_Format)
  426. return Subprogram_Result is
  427. Result : int;
  428. begin
  429. Result := Use_Private_Key_Buffer (Ssl, Input,
  430. Input'Length, int (Format));
  431. return Subprogram_Result (Result);
  432. end Use_Private_Key_Buffer;
  433. function WolfSSL_Set_Fd (Ssl : WolfSSL_Type; Fd : int) return int with
  434. Convention => C,
  435. External_Name => "wolfSSL_set_fd",
  436. Import => True;
  437. function Attach (Ssl : WolfSSL_Type;
  438. Socket : Integer)
  439. return Subprogram_Result is
  440. Result : int := WolfSSL_Set_Fd (Ssl, int (Socket));
  441. begin
  442. return Subprogram_Result (Result);
  443. end Attach;
  444. procedure WolfSSL_Keep_Arrays (Ssl : WolfSSL_Type) with
  445. Convention => C,
  446. External_Name => "wolfSSL_KeepArrays",
  447. Import => True;
  448. procedure Keep_Arrays (Ssl : WolfSSL_Type) is
  449. begin
  450. WolfSSL_Keep_Arrays (Ssl);
  451. end Keep_Arrays;
  452. function WolfSSL_Accept (Ssl : WolfSSL_Type) return int with
  453. Convention => C,
  454. External_Name => "wolfSSL_accept",
  455. Import => True;
  456. function Accept_Connection (Ssl : WolfSSL_Type)
  457. return Subprogram_Result is
  458. Result : int := WolfSSL_Accept (Ssl);
  459. begin
  460. return Subprogram_Result (Result);
  461. end Accept_Connection;
  462. procedure WolfSSL_Free_Arrays (Ssl : WolfSSL_Type) with
  463. Convention => C,
  464. External_Name => "wolfSSL_FreeArrays",
  465. Import => True;
  466. procedure Free_Arrays (Ssl : WolfSSL_Type) is
  467. begin
  468. WolfSSL_Free_Arrays (Ssl);
  469. end Free_Arrays;
  470. function WolfSSL_Read (Ssl : WolfSSL_Type;
  471. Data : out char_array;
  472. Sz : int) return int with
  473. Convention => C,
  474. External_Name => "wolfSSL_read",
  475. Import => True;
  476. -- This function reads sz bytes from the SSL session (ssl) internal
  477. -- read buffer into the buffer data. The bytes read are removed from
  478. -- the internal receive buffer. If necessary wolfSSL_read() will
  479. -- negotiate an SSL/TLS session if the handshake has not already
  480. -- been performed yet by wolfSSL_connect() or wolfSSL_accept().
  481. -- The SSL/TLS protocol uses SSL records which have a maximum size
  482. -- of 16kB (the max record size can be controlled by the
  483. -- MAX_RECORD_SIZE define in /wolfssl/internal.h). As such, wolfSSL
  484. -- needs to read an entire SSL record internally before it is able
  485. -- to process and decrypt the record. Because of this, a call to
  486. -- wolfSSL_read() will only be able to return the maximum buffer
  487. -- size which has been decrypted at the time of calling. There may
  488. -- be additional not-yet-decrypted data waiting in the internal
  489. -- wolfSSL receive buffer which will be retrieved and decrypted with
  490. -- the next call to wolfSSL_read(). If sz is larger than the number
  491. -- of bytes in the internal read buffer, SSL_read() will return
  492. -- the bytes available in the internal read buffer. If no bytes are
  493. -- buffered in the internal read buffer yet, a call to wolfSSL_read()
  494. -- will trigger processing of the next record.
  495. --
  496. -- The integer returned is the number of bytes read upon success.
  497. -- 0 will be returned upon failure. This may be caused by a either
  498. -- a clean (close notify alert) shutdown or just that the peer closed
  499. -- the connection. Call wolfSSL_get_error() for the specific
  500. -- error code. SSL_FATAL_ERROR will be returned upon failure when
  501. -- either an error occurred or, when using non-blocking sockets,
  502. -- the SSL_ERROR_WANT_READ or SSL_ERROR_WANT_WRITE error was received
  503. -- and and the application needs to call wolfSSL_read() again.
  504. -- Use wolfSSL_get_error() to get a specific error code.
  505. function Read (Ssl : WolfSSL_Type) return Read_Result is
  506. Data : char_array (1 .. Byte_Index'Last);
  507. Size : int;
  508. begin
  509. Size := WolfSSL_Read (Ssl, Data, int (Byte_Index'Last));
  510. if Size <= 0 then
  511. return (Success => False,
  512. Last => 0,
  513. Code => Subprogram_Result (Size));
  514. else
  515. return (Success => True,
  516. Last => Byte_Index (Size),
  517. Buffer => Data (1 .. Byte_Index (Size)));
  518. end if;
  519. end Read;
  520. function WolfSSL_Write (Ssl : WolfSSL_Type;
  521. Data : char_array;
  522. Sz : int) return int with
  523. Convention => C,
  524. External_Name => "wolfSSL_write",
  525. Import => True;
  526. function Write (Ssl : WolfSSL_Type;
  527. Data : Byte_Array) return Write_Result is
  528. Size : constant int := Data'Length;
  529. Result : int;
  530. begin
  531. Result := WolfSSL_Write (Ssl, Data, Size);
  532. if Result > 0 then
  533. return (Success => True,
  534. Bytes_Written => Byte_Index (Result));
  535. else
  536. return (Success => False, Code => Subprogram_Result (Result));
  537. end if;
  538. end Write;
  539. function WolfSSL_Shutdown (Ssl : WolfSSL_Type) return int with
  540. Convention => C,
  541. External_Name => "wolfSSL_shutdown",
  542. Import => True;
  543. function Shutdown (Ssl : WolfSSL_Type) return Subprogram_Result is
  544. Result : constant int := WolfSSL_Shutdown (Ssl);
  545. begin
  546. return Subprogram_Result (Result);
  547. end Shutdown;
  548. function WolfSSL_Connect (Ssl : WolfSSL_Type) return int with
  549. Convention => C,
  550. External_Name => "wolfSSL_connect",
  551. Import => True;
  552. function Connect (Ssl : WolfSSL_Type) return Subprogram_Result is
  553. Result : constant int := WolfSSL_Connect (Ssl);
  554. begin
  555. return Subprogram_Result (Result);
  556. end Connect;
  557. procedure WolfSSL_Free (Ssl : WolfSSL_Type) with
  558. Convention => C,
  559. External_Name => "wolfSSL_free",
  560. Import => True;
  561. procedure Free (Ssl : in out WolfSSL_Type) is
  562. begin
  563. if Ssl /= null then
  564. WolfSSL_Free (Ssl);
  565. end if;
  566. Ssl := null;
  567. end Free;
  568. function WolfSSL_Get_Error (Ssl : WolfSSL_Type;
  569. Ret : int) return int with
  570. Convention => C,
  571. External_Name => "wolfSSL_get_error",
  572. Import => True;
  573. function Get_Error (Ssl : WolfSSL_Type;
  574. Result : Subprogram_Result) return Error_Code is
  575. begin
  576. return Error_Code (WolfSSL_Get_Error (Ssl, int (Result)));
  577. end Get_Error;
  578. procedure WolfSSL_Error_String (Error : unsigned_long;
  579. Data : out Byte_Array;
  580. Size : unsigned_long) with
  581. Convention => C,
  582. External_Name => "wolfSSL_ERR_error_string_n",
  583. Import => True;
  584. function Error (Code : Error_Code) return Error_Message is
  585. S : String (1 .. Error_Message_Index'Last);
  586. B : Byte_Array (1 .. size_t (Error_Message_Index'Last));
  587. C : Natural;
  588. begin
  589. WolfSSL_Error_String (Error => unsigned_long (Code),
  590. Data => B,
  591. Size => unsigned_long (B'Last));
  592. Interfaces.C.To_Ada (Item => B,
  593. Target => S,
  594. Count => C,
  595. Trim_Nul => True);
  596. return (Last => C,
  597. Text => S (1 .. C));
  598. end Error;
  599. function Get_WolfSSL_Max_Error_Size return int with
  600. Convention => C,
  601. External_Name => "get_wolfssl_max_error_size",
  602. Import => True;
  603. function Max_Error_Size return Natural is
  604. begin
  605. return Natural (Get_WolfSSL_Max_Error_Size);
  606. end Max_Error_Size;
  607. end WolfSSL;