Skip to content

Commit

Permalink
Fix naked pointer in cipher function (#144)
Browse files Browse the repository at this point in the history
* Fix naked pointer

* Use alloc small

* Fix allocation

* Properly dealloc pointer

* Add changelog
  • Loading branch information
Firgeis authored Jul 23, 2023
1 parent 10b62a2 commit 6df24e2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- Add `Ssl.native_library_version` to query the underlying SSL library verion
(#140)
- `Ssl.Error`: separate library number from reason code (#139, #142)
- Fix naked pointer in cipher function (#144)

0.6.0 (2023-06-01)
=====
Expand Down
23 changes: 21 additions & 2 deletions src/ssl_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1074,20 +1074,29 @@ CAMLprim value ocaml_ssl_version(value socket) {
CAMLprim value ocaml_ssl_get_current_cipher(value socket) {
CAMLparam1(socket);
SSL *ssl = SSL_val(socket);

caml_release_runtime_system();
SSL_CIPHER *cipher = (SSL_CIPHER *)SSL_get_current_cipher(ssl);
caml_acquire_runtime_system();
if (!cipher)
caml_raise_constant(*caml_named_value("ssl_exn_cipher_error"));

#if defined(NO_NAKED_POINTERS) || defined(NAKED_POINTERS_CHECKER)
value vcipher = caml_alloc_shr(1, Abstract_tag);
*((SSL_CIPHER **) Data_abstract_val(vcipher)) = cipher;
CAMLreturn(vcipher);
#else
CAMLreturn((value)cipher);
#endif
}

CAMLprim value ocaml_ssl_get_cipher_description(value vcipher) {
CAMLparam1(vcipher);
char buf[1024];

#if defined(NO_NAKED_POINTERS) || defined(NAKED_POINTERS_CHECKER)
SSL_CIPHER *cipher = *((SSL_CIPHER **) Data_abstract_val(vcipher));
#else
SSL_CIPHER *cipher = (SSL_CIPHER *)vcipher;
#endif

caml_release_runtime_system();
SSL_CIPHER_description(cipher, buf, 1024);
Expand All @@ -1099,7 +1108,12 @@ CAMLprim value ocaml_ssl_get_cipher_description(value vcipher) {
CAMLprim value ocaml_ssl_get_cipher_name(value vcipher) {
CAMLparam1(vcipher);
const char *name;

#if defined(NO_NAKED_POINTERS) || defined(NAKED_POINTERS_CHECKER)
SSL_CIPHER *cipher = *((SSL_CIPHER **) Data_abstract_val(vcipher));
#else
SSL_CIPHER *cipher = (SSL_CIPHER *)vcipher;
#endif

caml_release_runtime_system();
name = SSL_CIPHER_get_name(cipher);
Expand All @@ -1111,7 +1125,12 @@ CAMLprim value ocaml_ssl_get_cipher_name(value vcipher) {
CAMLprim value ocaml_ssl_get_cipher_version(value vcipher) {
CAMLparam1(vcipher);
const char *version;

#if defined(NO_NAKED_POINTERS) || defined(NAKED_POINTERS_CHECKER)
SSL_CIPHER *cipher = *((SSL_CIPHER **) Data_abstract_val(vcipher));
#else
SSL_CIPHER *cipher = (SSL_CIPHER *)vcipher;
#endif

caml_release_runtime_system();
version = SSL_CIPHER_get_version(cipher);
Expand Down

0 comments on commit 6df24e2

Please sign in to comment.