2023-07-12 10:46:09 -07:00
# Ada Binding Example
2023-07-17 01:22:06 +02:00
The source code for the Ada/SPARK binding of the WolfSSL library
is the WolfSSL Ada package in the wolfssl.ads and wolfssl.adb files.
2023-07-12 10:46:09 -07:00
2024-04-04 15:52:14 -04:00
The source code here also demonstrates a (D)TLS v1.3 server and client
2023-07-17 01:22:06 +02:00
using the WolfSSL Ada binding. The implementation is cross-platform
and compiles on Linux, Mac OS X and Windows.
Security: The WolfSSL Ada binding avoids usage of the
2023-07-28 09:29:28 +02:00
Secondary Stack. The GNAT compiler has a number of hardening
2023-07-17 01:22:06 +02:00
features for example Stack Scrubbing; the compiler can generate
code to zero-out stack frames used by subprograms.
Unfortunately this works well for the primary stack but not
for the secondary stack. The GNAT User's Guide recommends
avoiding the secondary stack using the restriction
2025-03-31 23:27:31 +02:00
No_Secondary_Stack (see the GNAT configuration file restricted.adc
2023-07-17 01:22:06 +02:00
which instructs compilation of the WolfSSL Ada binding under
2024-04-04 15:52:14 -04:00
this restriction). Note, however, that the examples do make use of the
2025-03-31 23:27:31 +02:00
secondary stack and the Alire project does not include this restriction, for
letting users of the library to define it at their level.
2023-07-17 01:22:06 +02:00
Portability: The WolfSSL Ada binding makes no usage of controlled types
and has no dependency upon the Ada.Finalization package.
Lighter Ada run-times for embedded systems often have
the restriction No_Finalization. The WolfSSL Ada binding has
been developed with maximum portability in mind.
Not only can the WolfSSL Ada binding be used in Ada applications but
2025-03-31 23:27:31 +02:00
also SPARK applications (a subset of the Ada language suitable for
2023-07-17 01:22:06 +02:00
formal verification). To formally verify the Ada code in this repository
2025-11-20 22:50:40 +01:00
open the examples/examples.gpr with GNAT Studio and then select
2024-08-13 10:56:25 +02:00
SPARK -> Prove All Sources and use Proof Level 2. Or when using the command
2025-11-20 22:50:40 +01:00
line, use `gnatprove -Pexamples/examples.gpr --level=4 -j12` (`-j12` is there in
2024-08-13 10:56:25 +02:00
order to instruct the prover to use 12 CPUs if available).
2023-07-17 01:22:06 +02:00
2023-08-01 22:45:07 +02:00
```
2023-07-17 01:22:06 +02:00
Summary of SPARK analysis
=========================
---------------------------------------------------------------------------------------------------------------
SPARK Analysis results Total Flow CodePeer Provers Justified Unproved
---------------------------------------------------------------------------------------------------------------
Data Dependencies 2 2 . . . .
Flow Dependencies . . . . . .
Initialization 15 15 . . . .
Non-Aliasing . . . . . .
Run-time Checks 58 . . 58 (CVC4 85%, Trivial 15%) . .
Assertions 6 . . 6 (CVC4) . .
Functional Contracts 91 . . 91 (CVC4) . .
LSP Verification . . . . . .
Termination . . . . . .
Concurrency . . . . . .
---------------------------------------------------------------------------------------------------------------
Total 172 17 (10%) . 155 (90%) . .
2023-08-01 22:45:07 +02:00
```
2023-07-17 01:22:06 +02:00
## Compiler and Build System installation
2024-08-11 18:36:54 +02:00
### Recommended: [Alire](https://alire.ada.dev)
[Alire ](https://alire.ada.dev ) is a modern package manager for the Ada
ecosystem. The latest version is available for Windows, OSX, Linux and FreeBSD
systems. It can install a complete Ada toolchain if needed, see `alr install`
for more information.
2025-11-20 22:50:40 +01:00
**Note:** If you encounter a missing dependency error, it may be caused by the installed dependency being too old. In this case, either install a newer toolchain or decrease the required dependency version in your project.
2024-08-11 18:36:54 +02:00
In order to use WolfSSL in a project, just add WolfSSL as a dependency by
running `alr with wolfssl` within your project's directory.
If the project is to be verified with SPARK, just add `gnatprove` as a
dependency by running `alr with gnatprove` and then running `alr gnatprove` ,
which will execute the SPARK solver. If you get warnings, it is recommended to
increase the prove level: `alr gnatprove --level=4` .
2024-08-13 10:56:25 +02:00
### GNAT FSF Compiler and GPRBuild manual installation
In May 2022 AdaCore announced the end of the GNAT Community releases.
Pre-built binaries for the GNAT FSF compiler and GPRBuild can be
downloaded and manually installed from here:
https://github.com/alire-project/GNAT-FSF-builds/releases
Make sure the executables for the compiler and GPRBuild are on the PATH
and use gprbuild to build the source code.
2023-07-12 10:46:09 -07:00
2024-08-13 10:56:25 +02:00
#### Manual build of the project
2023-07-12 10:46:09 -07:00
```sh
2023-07-17 19:50:05 +02:00
cd wrapper/Ada
2023-07-12 10:46:09 -07:00
gprclean
gprbuild default.gpr
2025-11-20 22:50:40 +01:00
cd examples
2025-03-31 23:27:31 +02:00
gprbuild examples.gpr
2023-07-12 10:46:09 -07:00
2023-07-17 01:22:06 +02:00
cd obj/
2023-07-12 10:46:09 -07:00
./tls_server_main &
2023-07-17 01:22:06 +02:00
./tls_client_main 127.0.0.1
2023-07-12 10:46:09 -07:00
```
2023-07-17 01:22:06 +02:00
2025-11-20 22:50:40 +01:00
If you are using Alire, you can build the library and examples with:
```sh
cd wrapper/Ada
alr install
cd examples
alr build
```
You can also run the examples directly with Alire:
```sh
cd wrapper/Ada/examples
alr run tls_server_main &
alr run tls_client_main --args= 127.0.0.1
```
2023-07-31 17:53:01 +02:00
On Windows, build the executables with:
```sh
2025-11-20 22:50:40 +01:00
cd wrapper/Ada/examples
2025-03-31 23:27:31 +02:00
gprbuild -XOS= Windows examples.gpr
2023-07-31 17:53:01 +02:00
```
2023-07-17 01:22:06 +02:00
## Files
2024-04-04 15:52:14 -04:00
The (D)TLS v1.3 client example in the Ada/SPARK programming language
2023-08-01 23:04:41 +02:00
using the WolfSSL library can be found in the files:
2025-11-20 22:50:40 +01:00
examples/src/tls_client_main.adb
examples/src/tls_client.ads
examples/src/tls_client.adb
2023-07-17 01:22:06 +02:00
2024-04-04 15:52:14 -04:00
The (D)TLS v1.3 server example in the Ada/SPARK programming language
2023-08-01 23:04:41 +02:00
using the WolfSSL library can be found in the files:
2025-11-20 22:50:40 +01:00
examples/src/tls_server_main.adb
examples/src/tls_server.ads
examples/src/tls_server.adb