Skip to content

Correctly import libcrypto functions using dynlib #19881

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 13, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 37 additions & 35 deletions lib/wrappers/openssl.nim
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ else:

import dynlib

{.pragma: lcrypto, cdecl, dynlib: DLLUtilName, importc.}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(most of) these should be raises: [], noSideEffect as well - don't remember if SSL has globals or not which would affect side effects

{.pragma: lssl, cdecl, dynlib: DLLSSLName, importc.}

type
SslStruct {.final, pure.} = object
SslPtr* = ptr SslStruct
Expand All @@ -102,7 +105,6 @@ type
PSTACK* = SslPtr
PX509* = SslPtr
PX509_NAME* = SslPtr
PEVP_MD* = SslPtr
PBIO_METHOD* = SslPtr
BIO* = SslPtr
EVP_PKEY* = SslPtr
Expand Down Expand Up @@ -684,48 +686,48 @@ proc RSA_free*(rsa: PRSA) {.cdecl, dynlib: DLLUtilName, importc.}
proc RSA_size*(rsa: PRSA): cint {.cdecl, dynlib: DLLUtilName, importc.}

# sha types
proc EVP_md_null*(): EVP_MD {.cdecl, importc.}
proc EVP_md2*(): EVP_MD {.cdecl, importc.}
proc EVP_md4*(): EVP_MD {.cdecl, importc.}
proc EVP_md5*(): EVP_MD {.cdecl, importc.}
proc EVP_sha*(): EVP_MD {.cdecl, importc.}
proc EVP_sha1*(): EVP_MD {.cdecl, importc.}
proc EVP_dss*(): EVP_MD {.cdecl, importc.}
proc EVP_dss1*(): EVP_MD {.cdecl, importc.}
proc EVP_ecdsa*(): EVP_MD {.cdecl, importc.}
proc EVP_sha224*(): EVP_MD {.cdecl, importc.}
proc EVP_sha256*(): EVP_MD {.cdecl, importc.}
proc EVP_sha384*(): EVP_MD {.cdecl, importc.}
proc EVP_sha512*(): EVP_MD {.cdecl, importc.}
proc EVP_mdc2*(): EVP_MD {.cdecl, importc.}
proc EVP_ripemd160*(): EVP_MD {.cdecl, importc.}
proc EVP_whirlpool*(): EVP_MD {.cdecl, importc.}
proc EVP_MD_size*(md: EVP_MD): cint {.cdecl, importc.}
proc EVP_md_null*(): EVP_MD {.lcrypto.}
proc EVP_md2*(): EVP_MD {.lcrypto.}
proc EVP_md4*(): EVP_MD {.lcrypto.}
proc EVP_md5*(): EVP_MD {.lcrypto.}
proc EVP_sha*(): EVP_MD {.lcrypto.}
proc EVP_sha1*(): EVP_MD {.lcrypto.}
proc EVP_dss*(): EVP_MD {.lcrypto.}
proc EVP_dss1*(): EVP_MD {.lcrypto.}
proc EVP_ecdsa*(): EVP_MD {.lcrypto.}
proc EVP_sha224*(): EVP_MD {.lcrypto.}
proc EVP_sha256*(): EVP_MD {.lcrypto.}
proc EVP_sha384*(): EVP_MD {.lcrypto.}
proc EVP_sha512*(): EVP_MD {.lcrypto.}
proc EVP_mdc2*(): EVP_MD {.lcrypto.}
proc EVP_ripemd160*(): EVP_MD {.lcrypto.}
proc EVP_whirlpool*(): EVP_MD {.lcrypto.}
proc EVP_MD_size*(md: EVP_MD): cint {.lcrypto.}

# hmac functions
proc HMAC*(evp_md: EVP_MD; key: pointer; key_len: cint; d: cstring; n: csize_t; md: cstring; md_len: ptr cuint): cstring {.cdecl, importc.}
proc HMAC*(evp_md: EVP_MD; key: pointer; key_len: cint; d: cstring; n: csize_t; md: cstring; md_len: ptr cuint): cstring {.lcrypto.}

# RSA key functions
proc PEM_read_bio_PrivateKey*(bp: BIO, x: ptr EVP_PKEY, cb: pointer, u: pointer): EVP_PKEY {.cdecl, importc.}
proc EVP_PKEY_free*(p: EVP_PKEY) {.cdecl, importc.}
proc EVP_DigestSignInit*(ctx: EVP_MD_CTX, pctx: ptr EVP_PKEY_CTX, typ: EVP_MD, e: ENGINE, pkey: EVP_PKEY): cint {.cdecl, importc.}
proc EVP_DigestInit_ex*(ctx: EVP_MD_CTX, typ: PEVP_MD, engine: SslPtr = nil): cint {.cdecl, importc.}
proc EVP_DigestUpdate*(ctx: EVP_MD_CTX, data: pointer, len: cuint): cint {.cdecl, importc.}
proc EVP_DigestFinal_ex*(ctx: EVP_MD_CTX, buffer: pointer, size: ptr cuint): cint {.cdecl, importc.}
proc EVP_DigestSignFinal*(ctx: EVP_MD_CTX, data: pointer, len: ptr csize_t): cint {.cdecl, importc.}
proc EVP_PKEY_CTX_new*(pkey: EVP_PKEY, e: ENGINE): EVP_PKEY_CTX {.cdecl, importc.}
proc EVP_PKEY_CTX_free*(pkeyCtx: EVP_PKEY_CTX) {.cdecl, importc.}
proc EVP_PKEY_sign_init*(c: EVP_PKEY_CTX): cint {.cdecl, importc.}
proc PEM_read_bio_PrivateKey*(bp: BIO, x: ptr EVP_PKEY, cb: pointer, u: pointer): EVP_PKEY {.lcrypto.}
proc EVP_PKEY_free*(p: EVP_PKEY) {.lcrypto.}
proc EVP_DigestSignInit*(ctx: EVP_MD_CTX, pctx: ptr EVP_PKEY_CTX, typ: EVP_MD, e: ENGINE, pkey: EVP_PKEY): cint {.lcrypto.}
proc EVP_DigestInit_ex*(ctx: EVP_MD_CTX, typ: EVP_MD, engine: SslPtr = nil): cint {.lcrypto.}
proc EVP_DigestUpdate*(ctx: EVP_MD_CTX, data: pointer, len: cuint): cint {.lcrypto.}
proc EVP_DigestFinal_ex*(ctx: EVP_MD_CTX, buffer: pointer, size: ptr cuint): cint {.lcrypto.}
proc EVP_DigestSignFinal*(ctx: EVP_MD_CTX, data: pointer, len: ptr csize_t): cint {.lcrypto.}
proc EVP_PKEY_CTX_new*(pkey: EVP_PKEY, e: ENGINE): EVP_PKEY_CTX {.lcrypto.}
proc EVP_PKEY_CTX_free*(pkeyCtx: EVP_PKEY_CTX) {.lcrypto.}
proc EVP_PKEY_sign_init*(c: EVP_PKEY_CTX): cint {.lcrypto.}

when defined(macosx) or defined(windows):
proc EVP_MD_CTX_create*(): EVP_MD_CTX {.cdecl, importc.}
proc EVP_MD_CTX_destroy*(ctx: EVP_MD_CTX) {.cdecl, importc.}
proc EVP_MD_CTX_cleanup*(ctx: EVP_MD_CTX): cint {.cdecl, importc.}
proc EVP_MD_CTX_create*(): EVP_MD_CTX {.lcrypto.}
proc EVP_MD_CTX_destroy*(ctx: EVP_MD_CTX) {.lcrypto.}
proc EVP_MD_CTX_cleanup*(ctx: EVP_MD_CTX): cint {.lcrypto.}
else:
# some times you will need this instead:
proc EVP_MD_CTX_create*(): EVP_MD_CTX {.cdecl, importc: "EVP_MD_CTX_new".}
proc EVP_MD_CTX_destroy*(ctx: EVP_MD_CTX) {.cdecl, importc: "EVP_MD_CTX_free".}
proc EVP_MD_CTX_cleanup*(ctx: EVP_MD_CTX): cint {.cdecl, importc: "EVP_MD_CTX_cleanup".}
proc EVP_MD_CTX_create*(): EVP_MD_CTX {.cdecl, importc: "EVP_MD_CTX_new", dynlib: DLLUtilName.}
proc EVP_MD_CTX_destroy*(ctx: EVP_MD_CTX) {.cdecl, importc: "EVP_MD_CTX_free", dynlib: DLLUtilName.}
proc EVP_MD_CTX_cleanup*(ctx: EVP_MD_CTX): cint {.cdecl, importc: "EVP_MD_CTX_cleanup", dynlib: DLLUtilName.}

# <openssl/md5.h>
type
Expand Down