|
| 1 | +# Ada Binding Example |
| 2 | +The source code for the Ada/SPARK binding of the WolfSSL library |
| 3 | +is the WolfSSL Ada package in the wolfssl.ads and wolfssl.adb files. |
| 4 | + |
| 5 | +The source code here also demonstrates a TLS v1.3 server and client |
| 6 | +using the WolfSSL Ada binding. The implementation is cross-platform |
| 7 | +and compiles on Linux, Mac OS X and Windows. |
| 8 | + |
| 9 | +Security: The WolfSSL Ada binding avoids usage of the |
| 10 | +Seconday Stack. The GNAT compiler has a number of hardening |
| 11 | +features for example Stack Scrubbing; the compiler can generate |
| 12 | +code to zero-out stack frames used by subprograms. |
| 13 | +Unfortunately this works well for the primary stack but not |
| 14 | +for the secondary stack. The GNAT User's Guide recommends |
| 15 | +avoiding the secondary stack using the restriction |
| 16 | +No_Secondary_Stack (see the GNAT configuration file gnat.adc |
| 17 | +which instructs compilation of the WolfSSL Ada binding under |
| 18 | +this restriction). |
| 19 | + |
| 20 | +Portability: The WolfSSL Ada binding makes no usage of controlled types |
| 21 | +and has no dependency upon the Ada.Finalization package. |
| 22 | +Lighter Ada run-times for embedded systems often have |
| 23 | +the restriction No_Finalization. The WolfSSL Ada binding has |
| 24 | +been developed with maximum portability in mind. |
| 25 | + |
| 26 | +Not only can the WolfSSL Ada binding be used in Ada applications but |
| 27 | +also SPARK applications (a subset of the Ada language suitable |
| 28 | +formal verification). To formally verify the Ada code in this repository |
| 29 | +open the client.gpr with GNAT Studio and then select |
| 30 | +SPARK -> Prove All Sources and use Proof Level 2. |
| 31 | + |
| 32 | +Summary of SPARK analysis |
| 33 | +========================= |
| 34 | + |
| 35 | +--------------------------------------------------------------------------------------------------------------- |
| 36 | +SPARK Analysis results Total Flow CodePeer Provers Justified Unproved |
| 37 | +--------------------------------------------------------------------------------------------------------------- |
| 38 | +Data Dependencies 2 2 . . . . |
| 39 | +Flow Dependencies . . . . . . |
| 40 | +Initialization 15 15 . . . . |
| 41 | +Non-Aliasing . . . . . . |
| 42 | +Run-time Checks 58 . . 58 (CVC4 85%, Trivial 15%) . . |
| 43 | +Assertions 6 . . 6 (CVC4) . . |
| 44 | +Functional Contracts 91 . . 91 (CVC4) . . |
| 45 | +LSP Verification . . . . . . |
| 46 | +Termination . . . . . . |
| 47 | +Concurrency . . . . . . |
| 48 | +--------------------------------------------------------------------------------------------------------------- |
| 49 | +Total 172 17 (10%) . 155 (90%) . . |
| 50 | + |
| 51 | +## Compiler and Build System installation |
| 52 | + |
| 53 | +### GNAT Community Edition 2021 |
| 54 | +Download and install the GNAT community Edition 2021 compiler and studio: |
| 55 | +https://www.adacore.com/download |
| 56 | + |
| 57 | +Linux Install: |
| 58 | + |
| 59 | +```sh |
| 60 | +chmod +x gnat-2021-20210519-x86_64-linux-bin |
| 61 | +./gnat-2021-20210519-x86_64-linux-bin |
| 62 | +``` |
| 63 | + |
| 64 | +```sh |
| 65 | +export PATH="/opt/GNAT/2021/bin:$PATH" |
| 66 | +cd wrapper/Ada |
| 67 | +gprclean |
| 68 | +gprbuild default.gpr |
| 69 | +gprbuild client.gpr |
| 70 | + |
| 71 | +cd obj/ |
| 72 | +./tls_server_main & |
| 73 | +./tls_client_main 127.0.0.1 |
| 74 | +``` |
| 75 | + |
| 76 | +### GNAT FSF Compiler and GPRBuild manual installation |
| 77 | +In May 2022 AdaCore announced the end of the GNAT Community releases. |
| 78 | +Pre-built binaries for the GNAT FSF compiler and GPRBuild can be |
| 79 | +downloaded and manually installed from here: |
| 80 | +https://github.com/alire-project/GNAT-FSF-builds/releases |
| 81 | +Make sure the executables for the compiler and GPRBuild are on the PATH |
| 82 | +and use gprbuild to build the source code. |
| 83 | + |
| 84 | +## Files |
| 85 | +The file c_tls_client_main.c and c_tls_server_main.c are the TLS v1.3 |
| 86 | +server and client examples using the WolfSSL library implemented using |
| 87 | +the C programming language. |
| 88 | + |
| 89 | +The translation of the C client example into the Ada/SPARK programming |
| 90 | +language can be found in the files: |
| 91 | +tls_client_main.adb |
| 92 | +tls_client.ads |
| 93 | +tls_client.adb |
| 94 | + |
| 95 | +The translation of the C server example into the Ada/SPARK programming |
| 96 | +language can be found in the files: |
| 97 | +tls_server_main.adb |
| 98 | +tls_server.ads |
| 99 | +tls_server.adb |
| 100 | + |
| 101 | +A feature of the Ada language that is not part of SPARK is exceptions. |
| 102 | +Some packages of the Ada standard library and GNAT specific packages |
| 103 | +provided by the GNAT compiler can therefore not be used directly but |
| 104 | +need to be put into wrapper packages that does not raise exceptions. |
| 105 | +The packages that provide access to sockets and command line arguments |
| 106 | +to applications implemented in the SPARK programming language can be |
| 107 | +found in the files: |
| 108 | +spark_sockets.ads |
| 109 | +spark_sockets.adb |
| 110 | +spark_terminal.ads |
| 111 | +spark_terminal.adb |
0 commit comments