openssl.nim 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2015 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## OpenSSL support
  10. ##
  11. ## When OpenSSL is dynamically linked, the wrapper provides partial forward and backward
  12. ## compatibility for OpenSSL versions above and below 1.1.0
  13. ##
  14. ## OpenSSL can also be statically linked using ``--dynlibOverride:ssl`` for OpenSSL >= 1.1.0.
  15. ## If you want to statically link against OpenSSL 1.0.x, you now have to
  16. ## define the ``openssl10`` symbol via ``-d:openssl10``.
  17. ##
  18. ## Build and test examples:
  19. ##
  20. ## .. code-block::
  21. ## ./bin/nim c -d:ssl -p:. -r tests/untestable/tssl.nim
  22. ## ./bin/nim c -d:ssl -p:. --dynlibOverride:ssl --passL:-lcrypto --passL:-lssl -r tests/untestable/tssl.nim
  23. {.deadCodeElim: on.} # dce option deprecated
  24. const useWinVersion = defined(Windows) or defined(nimdoc)
  25. # To force openSSL version use -d:sslVersion=1.0.0
  26. # See: #10281, #10230
  27. # General issue:
  28. # Other dynamic libraries (like libpg) load diffetent openSSL version then what nim loads.
  29. # Having two different openSSL loaded version causes a crash.
  30. # Use this compile time define to force the openSSL version that your other dynamic libraries want.
  31. const sslVersion {.strdefine.}: string = ""
  32. when sslVersion != "":
  33. when defined(macosx):
  34. const
  35. DLLSSLName* = "libssl." & sslVersion & ".dylib"
  36. DLLUtilName* = "libcrypto." & sslVersion & ".dylib"
  37. from posix import SocketHandle
  38. elif defined(windows):
  39. const
  40. DLLSSLName* = "libssl-" & sslVersion & ".dll"
  41. DLLUtilName* = "libcrypto-" & sslVersion & ".dll"
  42. from winlean import SocketHandle
  43. else:
  44. const
  45. DLLSSLName* = "libssl.so." & sslVersion
  46. DLLUtilName* = "libcrypto.so." & sslVersion
  47. from posix import SocketHandle
  48. elif useWinVersion:
  49. when not defined(nimOldDlls) and defined(cpu64):
  50. const
  51. DLLSSLName* = "(libssl-1_1-x64|ssleay64|libssl64).dll"
  52. DLLUtilName* = "(libcrypto-1_1-x64|libeay64).dll"
  53. else:
  54. const
  55. DLLSSLName* = "(libssl-1_1|ssleay32|libssl32).dll"
  56. DLLUtilName* = "(libcrypto-1_1|libeay32).dll"
  57. from winlean import SocketHandle
  58. else:
  59. when defined(osx):
  60. const versions = "(.1.1|.38|.39|.41|.43|.44|.45|.46|.47|.10|.1.0.2|.1.0.1|.1.0.0|.0.9.9|.0.9.8|)"
  61. else:
  62. const versions = "(.1.1|.1.0.2|.1.0.1|.1.0.0|.0.9.9|.0.9.8|.47|.46|.45|.44|.43|.41|.39|.38|.10|)"
  63. when defined(macosx):
  64. const
  65. DLLSSLName* = "libssl" & versions & ".dylib"
  66. DLLUtilName* = "libcrypto" & versions & ".dylib"
  67. elif defined(genode):
  68. const
  69. DLLSSLName* = "libssl.lib.so"
  70. DLLUtilName* = "libcrypto.lib.so"
  71. else:
  72. const
  73. DLLSSLName* = "libssl.so" & versions
  74. DLLUtilName* = "libcrypto.so" & versions
  75. from posix import SocketHandle
  76. import dynlib
  77. type
  78. SslStruct {.final, pure.} = object
  79. SslPtr* = ptr SslStruct
  80. PSslPtr* = ptr SslPtr
  81. SslCtx* = SslPtr
  82. PSSL_METHOD* = SslPtr
  83. PX509* = SslPtr
  84. PX509_NAME* = SslPtr
  85. PEVP_MD* = SslPtr
  86. PBIO_METHOD* = SslPtr
  87. BIO* = SslPtr
  88. EVP_PKEY* = SslPtr
  89. PRSA* = SslPtr
  90. PASN1_UTCTIME* = SslPtr
  91. PASN1_cInt* = SslPtr
  92. PPasswdCb* = SslPtr
  93. PFunction* = proc () {.cdecl.}
  94. DES_cblock* = array[0..7, int8]
  95. PDES_cblock* = ptr DES_cblock
  96. des_ks_struct*{.final.} = object
  97. ks*: DES_cblock
  98. weak_key*: cInt
  99. des_key_schedule* = array[1..16, des_ks_struct]
  100. pem_password_cb* = proc(buf: cstring, size, rwflag: cint, userdata: pointer): cint {.cdecl.}
  101. const
  102. SSL_SENT_SHUTDOWN* = 1
  103. SSL_RECEIVED_SHUTDOWN* = 2
  104. EVP_MAX_MD_SIZE* = 16 + 20
  105. SSL_ERROR_NONE* = 0
  106. SSL_ERROR_SSL* = 1
  107. SSL_ERROR_WANT_READ* = 2
  108. SSL_ERROR_WANT_WRITE* = 3
  109. SSL_ERROR_WANT_X509_LOOKUP* = 4
  110. SSL_ERROR_SYSCALL* = 5 #look at error stack/return value/errno
  111. SSL_ERROR_ZERO_RETURN* = 6
  112. SSL_ERROR_WANT_CONNECT* = 7
  113. SSL_ERROR_WANT_ACCEPT* = 8
  114. SSL_CTRL_NEED_TMP_RSA* = 1
  115. SSL_CTRL_SET_TMP_RSA* = 2
  116. SSL_CTRL_SET_TMP_DH* = 3
  117. SSL_CTRL_SET_TMP_ECDH* = 4
  118. SSL_CTRL_SET_TMP_RSA_CB* = 5
  119. SSL_CTRL_SET_TMP_DH_CB* = 6
  120. SSL_CTRL_SET_TMP_ECDH_CB* = 7
  121. SSL_CTRL_GET_SESSION_REUSED* = 8
  122. SSL_CTRL_GET_CLIENT_CERT_REQUEST* = 9
  123. SSL_CTRL_GET_NUM_RENEGOTIATIONS* = 10
  124. SSL_CTRL_CLEAR_NUM_RENEGOTIATIONS* = 11
  125. SSL_CTRL_GET_TOTAL_RENEGOTIATIONS* = 12
  126. SSL_CTRL_GET_FLAGS* = 13
  127. SSL_CTRL_EXTRA_CHAIN_CERT* = 14
  128. SSL_CTRL_SET_MSG_CALLBACK* = 15
  129. SSL_CTRL_SET_MSG_CALLBACK_ARG* = 16 # only applies to datagram connections
  130. SSL_CTRL_SET_MTU* = 17 # Stats
  131. SSL_CTRL_SESS_NUMBER* = 20
  132. SSL_CTRL_SESS_CONNECT* = 21
  133. SSL_CTRL_SESS_CONNECT_GOOD* = 22
  134. SSL_CTRL_SESS_CONNECT_RENEGOTIATE* = 23
  135. SSL_CTRL_SESS_ACCEPT* = 24
  136. SSL_CTRL_SESS_ACCEPT_GOOD* = 25
  137. SSL_CTRL_SESS_ACCEPT_RENEGOTIATE* = 26
  138. SSL_CTRL_SESS_HIT* = 27
  139. SSL_CTRL_SESS_CB_HIT* = 28
  140. SSL_CTRL_SESS_MISSES* = 29
  141. SSL_CTRL_SESS_TIMEOUTS* = 30
  142. SSL_CTRL_SESS_CACHE_FULL* = 31
  143. SSL_CTRL_OPTIONS* = 32
  144. SSL_CTRL_MODE* = 33
  145. SSL_CTRL_GET_READ_AHEAD* = 40
  146. SSL_CTRL_SET_READ_AHEAD* = 41
  147. SSL_CTRL_SET_SESS_CACHE_SIZE* = 42
  148. SSL_CTRL_GET_SESS_CACHE_SIZE* = 43
  149. SSL_CTRL_SET_SESS_CACHE_MODE* = 44
  150. SSL_CTRL_GET_SESS_CACHE_MODE* = 45
  151. SSL_CTRL_GET_MAX_CERT_LIST* = 50
  152. SSL_CTRL_SET_MAX_CERT_LIST* = 51 #* Allow SSL_write(..., n) to return r with 0 < r < n (i.e. report success
  153. # * when just a single record has been written): *
  154. SSL_CTRL_SET_TLSEXT_SERVERNAME_CB = 53
  155. SSL_CTRL_SET_TLSEXT_SERVERNAME_ARG = 54
  156. SSL_CTRL_SET_TLSEXT_HOSTNAME = 55
  157. TLSEXT_NAMETYPE_host_name* = 0
  158. SSL_TLSEXT_ERR_OK* = 0
  159. SSL_TLSEXT_ERR_ALERT_WARNING* = 1
  160. SSL_TLSEXT_ERR_ALERT_FATAL* = 2
  161. SSL_TLSEXT_ERR_NOACK* = 3
  162. SSL_MODE_ENABLE_PARTIAL_WRITE* = 1 #* Make it possible to retry SSL_write() with changed buffer location
  163. # * (buffer contents must stay the same!); this is not the default to avoid
  164. # * the misconception that non-blocking SSL_write() behaves like
  165. # * non-blocking write(): *
  166. SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER* = 2 #* Never bother the application with retries if the transport
  167. # * is blocking: *
  168. SSL_MODE_AUTO_RETRY* = 4 #* Don't attempt to automatically build certificate chain *
  169. SSL_MODE_NO_AUTO_CHAIN* = 8
  170. SSL_OP_NO_SSLv2* = 0x01000000
  171. SSL_OP_NO_SSLv3* = 0x02000000
  172. SSL_OP_NO_TLSv1* = 0x04000000
  173. SSL_OP_NO_TLSv1_1* = 0x08000000
  174. SSL_OP_ALL* = 0x000FFFFF
  175. SSL_VERIFY_NONE* = 0x00000000
  176. SSL_VERIFY_PEER* = 0x00000001
  177. OPENSSL_DES_DECRYPT* = 0
  178. OPENSSL_DES_ENCRYPT* = 1
  179. X509_V_OK* = 0
  180. X509_V_ILLEGAL* = 1
  181. X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT* = 2
  182. X509_V_ERR_UNABLE_TO_GET_CRL* = 3
  183. X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE* = 4
  184. X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE* = 5
  185. X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY* = 6
  186. X509_V_ERR_CERT_SIGNATURE_FAILURE* = 7
  187. X509_V_ERR_CRL_SIGNATURE_FAILURE* = 8
  188. X509_V_ERR_CERT_NOT_YET_VALID* = 9
  189. X509_V_ERR_CERT_HAS_EXPIRED* = 10
  190. X509_V_ERR_CRL_NOT_YET_VALID* = 11
  191. X509_V_ERR_CRL_HAS_EXPIRED* = 12
  192. X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD* = 13
  193. X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD* = 14
  194. X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD* = 15
  195. X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD* = 16
  196. X509_V_ERR_OUT_OF_MEM* = 17
  197. X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT* = 18
  198. X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN* = 19
  199. X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY* = 20
  200. X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE* = 21
  201. X509_V_ERR_CERT_CHAIN_TOO_LONG* = 22
  202. X509_V_ERR_CERT_REVOKED* = 23
  203. X509_V_ERR_INVALID_CA* = 24
  204. X509_V_ERR_PATH_LENGTH_EXCEEDED* = 25
  205. X509_V_ERR_INVALID_PURPOSE* = 26
  206. X509_V_ERR_CERT_UNTRUSTED* = 27
  207. X509_V_ERR_CERT_REJECTED* = 28 #These are 'informational' when looking for issuer cert
  208. X509_V_ERR_SUBJECT_ISSUER_MISMATCH* = 29
  209. X509_V_ERR_AKID_SKID_MISMATCH* = 30
  210. X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH* = 31
  211. X509_V_ERR_KEYUSAGE_NO_CERTSIGN* = 32
  212. X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER* = 33
  213. X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION* = 34 #The application is not happy
  214. X509_V_ERR_APPLICATION_VERIFICATION* = 50
  215. SSL_FILETYPE_ASN1* = 2
  216. SSL_FILETYPE_PEM* = 1
  217. EVP_PKEY_RSA* = 6 # libssl.dll
  218. BIO_C_SET_CONNECT = 100
  219. BIO_C_DO_STATE_MACHINE = 101
  220. BIO_C_GET_SSL = 110
  221. proc TLSv1_method*(): PSSL_METHOD{.cdecl, dynlib: DLLSSLName, importc.}
  222. # TLS_method(), TLS_server_method(), TLS_client_method() are introduced in 1.1.0
  223. # and support SSLv3, TLSv1, TLSv1.1 and TLSv1.2
  224. # SSLv23_method(), SSLv23_server_method(), SSLv23_client_method() are removed in 1.1.0
  225. when compileOption("dynlibOverride", "ssl"):
  226. # Static linking
  227. when defined(openssl10):
  228. proc SSL_library_init*(): cint {.cdecl, dynlib: DLLSSLName, importc, discardable.}
  229. proc SSL_load_error_strings*() {.cdecl, dynlib: DLLSSLName, importc.}
  230. proc SSLv23_method*(): PSSL_METHOD {.cdecl, dynlib: DLLSSLName, importc.}
  231. else:
  232. proc OPENSSL_init_ssl*(opts: uint64, settings: uint8): cint {.cdecl, dynlib: DLLSSLName, importc, discardable.}
  233. proc SSL_library_init*(): cint {.discardable.} =
  234. ## Initialize SSL using OPENSSL_init_ssl for OpenSSL >= 1.1.0
  235. return OPENSSL_init_ssl(0.uint64, 0.uint8)
  236. proc TLS_method*(): PSSL_METHOD {.cdecl, dynlib: DLLSSLName, importc.}
  237. proc SSLv23_method*(): PSSL_METHOD =
  238. TLS_method()
  239. proc OpenSSL_version_num(): culong {.cdecl, dynlib: DLLSSLName, importc.}
  240. proc getOpenSSLVersion*(): culong =
  241. ## Return OpenSSL version as unsigned long
  242. OpenSSL_version_num()
  243. proc SSL_load_error_strings*() =
  244. ## Removed from OpenSSL 1.1.0
  245. # This proc prevents breaking existing code calling SslLoadErrorStrings
  246. # Static linking against OpenSSL < 1.1.0 is not supported
  247. discard
  248. template OpenSSL_add_all_algorithms*() = discard
  249. proc SSLv23_client_method*(): PSSL_METHOD {.cdecl, dynlib: DLLSSLName, importc.}
  250. proc SSLv2_method*(): PSSL_METHOD {.cdecl, dynlib: DLLSSLName, importc.}
  251. proc SSLv3_method*(): PSSL_METHOD {.cdecl, dynlib: DLLSSLName, importc.}
  252. else:
  253. # Here we're trying to stay compatible with openssl 1.0.* and 1.1.*. Some
  254. # symbols are loaded dynamically and we don't use them if not found.
  255. proc thisModule(): LibHandle {.inline.} =
  256. var thisMod {.global.}: LibHandle
  257. if thisMod.isNil: thisMod = loadLib()
  258. result = thisMod
  259. proc sslModule(): LibHandle {.inline.} =
  260. var sslMod {.global.}: LibHandle
  261. if sslMod.isNil: sslMod = loadLibPattern(DLLSSLName)
  262. result = sslMod
  263. proc sslSym(name: string): pointer =
  264. var dl = thisModule()
  265. if not dl.isNil:
  266. result = symAddr(dl, name)
  267. if result.isNil:
  268. dl = sslModule()
  269. if not dl.isNil:
  270. result = symAddr(dl, name)
  271. proc loadPSSLMethod(method1, method2: string): PSSL_METHOD =
  272. ## Load <method1> from OpenSSL if available, otherwise <method2>
  273. let m1 = cast[proc(): PSSL_METHOD {.cdecl, gcsafe.}](sslSym(method1))
  274. if not m1.isNil:
  275. return m1()
  276. cast[proc(): PSSL_METHOD {.cdecl, gcsafe.}](sslSym(method2))()
  277. proc SSL_library_init*(): cint {.discardable.} =
  278. ## Initialize SSL using OPENSSL_init_ssl for OpenSSL >= 1.1.0 otherwise
  279. ## SSL_library_init
  280. let theProc = cast[proc(opts: uint64, settings: uint8): cint {.cdecl.}](sslSym("OPENSSL_init_ssl"))
  281. if not theProc.isNil:
  282. return theProc(0, 0)
  283. let olderProc = cast[proc(): cint {.cdecl.}](sslSym("SSL_library_init"))
  284. if not olderProc.isNil: result = olderProc()
  285. proc SSL_load_error_strings*() =
  286. let theProc = cast[proc() {.cdecl.}](sslSym("SSL_load_error_strings"))
  287. if not theProc.isNil: theProc()
  288. proc SSLv23_client_method*(): PSSL_METHOD =
  289. loadPSSLMethod("SSLv23_client_method", "TLS_client_method")
  290. proc SSLv23_method*(): PSSL_METHOD =
  291. loadPSSLMethod("SSLv23_method", "TLS_method")
  292. proc SSLv2_method*(): PSSL_METHOD =
  293. loadPSSLMethod("SSLv2_method", "TLS_method")
  294. proc SSLv3_method*(): PSSL_METHOD =
  295. loadPSSLMethod("SSLv3_method", "TLS_method")
  296. proc TLS_method*(): PSSL_METHOD =
  297. loadPSSLMethod("TLS_method", "SSLv23_method")
  298. proc TLS_client_method*(): PSSL_METHOD =
  299. loadPSSLMethod("TLS_client_method", "SSLv23_client_method")
  300. proc TLS_server_method*(): PSSL_METHOD =
  301. loadPSSLMethod("TLS_server_method", "SSLv23_server_method")
  302. proc OpenSSL_add_all_algorithms*() =
  303. let theProc = cast[proc() {.cdecl.}](sslSym("OPENSSL_add_all_algorithms_conf"))
  304. if not theProc.isNil: theProc()
  305. proc getOpenSSLVersion*(): culong =
  306. ## Return OpenSSL version as unsigned long or 0 if not available
  307. let theProc = cast[proc(): culong {.cdecl.}](sslSym("OpenSSL_version_num"))
  308. result =
  309. if theProc.isNil: 0.culong
  310. else: theProc()
  311. proc ERR_load_BIO_strings*(){.cdecl, dynlib: DLLUtilName, importc.}
  312. proc SSL_new*(context: SslCtx): SslPtr{.cdecl, dynlib: DLLSSLName, importc.}
  313. proc SSL_free*(ssl: SslPtr){.cdecl, dynlib: DLLSSLName, importc.}
  314. proc SSL_get_SSL_CTX*(ssl: SslPtr): SslCtx {.cdecl, dynlib: DLLSSLName, importc.}
  315. proc SSL_set_SSL_CTX*(ssl: SslPtr, ctx: SslCtx): SslCtx {.cdecl, dynlib: DLLSSLName, importc.}
  316. proc SSL_CTX_new*(meth: PSSL_METHOD): SslCtx{.cdecl,
  317. dynlib: DLLSSLName, importc.}
  318. proc SSL_CTX_load_verify_locations*(ctx: SslCtx, CAfile: cstring,
  319. CApath: cstring): cInt{.cdecl, dynlib: DLLSSLName, importc.}
  320. proc SSL_CTX_free*(arg0: SslCtx){.cdecl, dynlib: DLLSSLName, importc.}
  321. proc SSL_CTX_set_verify*(s: SslCtx, mode: int, cb: proc (a: int, b: pointer): int {.cdecl.}){.cdecl, dynlib: DLLSSLName, importc.}
  322. proc SSL_get_verify_result*(ssl: SslPtr): int{.cdecl,
  323. dynlib: DLLSSLName, importc.}
  324. proc SSL_CTX_set_cipher_list*(s: SslCtx, ciphers: cstring): cint{.cdecl, dynlib: DLLSSLName, importc.}
  325. proc SSL_CTX_use_certificate_file*(ctx: SslCtx, filename: cstring, typ: cInt): cInt{.
  326. stdcall, dynlib: DLLSSLName, importc.}
  327. proc SSL_CTX_use_certificate_chain_file*(ctx: SslCtx, filename: cstring): cInt{.
  328. stdcall, dynlib: DLLSSLName, importc.}
  329. proc SSL_CTX_use_PrivateKey_file*(ctx: SslCtx,
  330. filename: cstring, typ: cInt): cInt{.cdecl, dynlib: DLLSSLName, importc.}
  331. proc SSL_CTX_check_private_key*(ctx: SslCtx): cInt{.cdecl, dynlib: DLLSSLName,
  332. importc.}
  333. proc SSL_CTX_get_ex_new_index*(argl: clong, argp: pointer, new_func: pointer, dup_func: pointer, free_func: pointer): cint {.cdecl, dynlib: DLLSSLName, importc.}
  334. proc SSL_CTX_set_ex_data*(ssl: SslCtx, idx: cint, arg: pointer): cint {.cdecl, dynlib: DLLSSLName, importc.}
  335. proc SSL_CTX_get_ex_data*(ssl: SslCtx, idx: cint): pointer {.cdecl, dynlib: DLLSSLName, importc.}
  336. proc SSL_set_fd*(ssl: SslPtr, fd: SocketHandle): cint{.cdecl, dynlib: DLLSSLName, importc.}
  337. proc SSL_shutdown*(ssl: SslPtr): cInt{.cdecl, dynlib: DLLSSLName, importc.}
  338. proc SSL_set_shutdown*(ssl: SslPtr, mode: cint) {.cdecl, dynlib: DLLSSLName, importc: "SSL_set_shutdown".}
  339. proc SSL_get_shutdown*(ssl: SslPtr): cint {.cdecl, dynlib: DLLSSLName, importc: "SSL_get_shutdown".}
  340. proc SSL_connect*(ssl: SslPtr): cint{.cdecl, dynlib: DLLSSLName, importc.}
  341. proc SSL_read*(ssl: SslPtr, buf: pointer, num: int): cint{.cdecl, dynlib: DLLSSLName, importc.}
  342. proc SSL_write*(ssl: SslPtr, buf: cstring, num: int): cint{.cdecl, dynlib: DLLSSLName, importc.}
  343. proc SSL_get_error*(s: SslPtr, ret_code: cInt): cInt{.cdecl, dynlib: DLLSSLName, importc.}
  344. proc SSL_accept*(ssl: SslPtr): cInt{.cdecl, dynlib: DLLSSLName, importc.}
  345. proc SSL_pending*(ssl: SslPtr): cInt{.cdecl, dynlib: DLLSSLName, importc.}
  346. proc BIO_new_mem_buf*(data: pointer, len: cint): BIO{.cdecl,
  347. dynlib: DLLSSLName, importc.}
  348. proc BIO_new_ssl_connect*(ctx: SslCtx): BIO{.cdecl,
  349. dynlib: DLLSSLName, importc.}
  350. proc BIO_ctrl*(bio: BIO, cmd: cint, larg: int, arg: cstring): int{.cdecl,
  351. dynlib: DLLSSLName, importc.}
  352. proc BIO_get_ssl*(bio: BIO, ssl: ptr SslPtr): int =
  353. return BIO_ctrl(bio, BIO_C_GET_SSL, 0, cast[cstring](ssl))
  354. proc BIO_set_conn_hostname*(bio: BIO, name: cstring): int =
  355. return BIO_ctrl(bio, BIO_C_SET_CONNECT, 0, name)
  356. proc BIO_do_handshake*(bio: BIO): int =
  357. return BIO_ctrl(bio, BIO_C_DO_STATE_MACHINE, 0, nil)
  358. proc BIO_do_connect*(bio: BIO): int =
  359. return BIO_do_handshake(bio)
  360. when not defined(nimfix):
  361. proc BIO_read*(b: BIO, data: cstring, length: cInt): cInt{.cdecl,
  362. dynlib: DLLUtilName, importc.}
  363. proc BIO_write*(b: BIO, data: cstring, length: cInt): cInt{.cdecl,
  364. dynlib: DLLUtilName, importc.}
  365. proc BIO_free*(b: BIO): cInt{.cdecl, dynlib: DLLUtilName, importc.}
  366. proc ERR_print_errors_fp*(fp: File){.cdecl, dynlib: DLLSSLName, importc.}
  367. proc ERR_error_string*(e: cInt, buf: cstring): cstring{.cdecl,
  368. dynlib: DLLUtilName, importc.}
  369. proc ERR_get_error*(): cInt{.cdecl, dynlib: DLLUtilName, importc.}
  370. proc ERR_peek_last_error*(): cInt{.cdecl, dynlib: DLLUtilName, importc.}
  371. proc OPENSSL_config*(configName: cstring){.cdecl, dynlib: DLLSSLName, importc.}
  372. when not useWinVersion and not defined(macosx) and not defined(android) and not defined(nimNoAllocForSSL):
  373. proc CRYPTO_set_mem_functions(a,b,c: pointer){.cdecl,
  374. dynlib: DLLUtilName, importc.}
  375. proc allocWrapper(size: int): pointer {.cdecl.} = allocShared(size)
  376. proc reallocWrapper(p: pointer; newsize: int): pointer {.cdecl.} =
  377. if p == nil:
  378. if newSize > 0: result = allocShared(newsize)
  379. elif newsize == 0: deallocShared(p)
  380. else: result = reallocShared(p, newsize)
  381. proc deallocWrapper(p: pointer) {.cdecl.} =
  382. if p != nil: deallocShared(p)
  383. proc CRYPTO_malloc_init*() =
  384. when not useWinVersion and not defined(macosx) and not defined(android) and not defined(nimNoAllocForSSL):
  385. CRYPTO_set_mem_functions(allocWrapper, reallocWrapper, deallocWrapper)
  386. proc SSL_CTX_ctrl*(ctx: SslCtx, cmd: cInt, larg: int, parg: pointer): int{.
  387. cdecl, dynlib: DLLSSLName, importc.}
  388. proc SSL_CTX_callback_ctrl(ctx: SslCtx, typ: cInt, fp: PFunction): int{.
  389. cdecl, dynlib: DLLSSLName, importc.}
  390. proc SSLCTXSetMode*(ctx: SslCtx, mode: int): int =
  391. result = SSL_CTX_ctrl(ctx, SSL_CTRL_MODE, mode, nil)
  392. proc SSL_ctrl*(ssl: SslPtr, cmd: cInt, larg: int, parg: pointer): int{.
  393. cdecl, dynlib: DLLSSLName, importc.}
  394. proc SSL_set_tlsext_host_name*(ssl: SslPtr, name: cstring): int =
  395. result = SSL_ctrl(ssl, SSL_CTRL_SET_TLSEXT_HOSTNAME, TLSEXT_NAMETYPE_host_name, name)
  396. ## Set the SNI server name extension to be used in a client hello.
  397. ## Returns 1 if SNI was set, 0 if current SSL configuration doesn't support SNI.
  398. proc SSL_get_servername*(ssl: SslPtr, typ: cInt = TLSEXT_NAMETYPE_host_name): cstring {.cdecl, dynlib: DLLSSLName, importc.}
  399. ## Retrieve the server name requested in the client hello. This can be used
  400. ## in the callback set in `SSL_CTX_set_tlsext_servername_callback` to
  401. ## implement virtual hosting. May return `nil`.
  402. proc SSL_CTX_set_tlsext_servername_callback*(ctx: SslCtx, cb: proc(ssl: SslPtr, cb_id: int, arg: pointer): int {.cdecl.}): int =
  403. ## Set the callback to be used on listening SSL connections when the client hello is received.
  404. ##
  405. ## The callback should return one of:
  406. ## * SSL_TLSEXT_ERR_OK
  407. ## * SSL_TLSEXT_ERR_ALERT_WARNING
  408. ## * SSL_TLSEXT_ERR_ALERT_FATAL
  409. ## * SSL_TLSEXT_ERR_NOACK
  410. result = SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TLSEXT_SERVERNAME_CB, cast[PFunction](cb))
  411. proc SSL_CTX_set_tlsext_servername_arg*(ctx: SslCtx, arg: pointer): int =
  412. ## Set the pointer to be used in the callback registered to ``SSL_CTX_set_tlsext_servername_callback``.
  413. result = SSL_CTX_ctrl(ctx, SSL_CTRL_SET_TLSEXT_SERVERNAME_ARG, 0, arg)
  414. type
  415. PskClientCallback* = proc (ssl: SslPtr;
  416. hint: cstring; identity: cstring; max_identity_len: cuint; psk: ptr cuchar;
  417. max_psk_len: cuint): cuint {.cdecl.}
  418. PskServerCallback* = proc (ssl: SslPtr;
  419. identity: cstring; psk: ptr cuchar; max_psk_len: cint): cuint {.cdecl.}
  420. proc SSL_CTX_set_psk_client_callback*(ctx: SslCtx; callback: PskClientCallback) {.cdecl, dynlib: DLLSSLName, importc.}
  421. ## Set callback called when OpenSSL needs PSK (for client).
  422. proc SSL_CTX_set_psk_server_callback*(ctx: SslCtx; callback: PskServerCallback) {.cdecl, dynlib: DLLSSLName, importc.}
  423. ## Set callback called when OpenSSL needs PSK (for server).
  424. proc SSL_CTX_use_psk_identity_hint*(ctx: SslCtx; hint: cstring): cint {.cdecl, dynlib: DLLSSLName, importc.}
  425. ## Set PSK identity hint to use.
  426. proc SSL_get_psk_identity*(ssl: SslPtr): cstring {.cdecl, dynlib: DLLSSLName, importc.}
  427. ## Get PSK identity.
  428. proc bioNew*(b: PBIO_METHOD): BIO{.cdecl, dynlib: DLLUtilName, importc: "BIO_new".}
  429. proc bioFreeAll*(b: BIO){.cdecl, dynlib: DLLUtilName, importc: "BIO_free_all".}
  430. proc bioSMem*(): PBIO_METHOD{.cdecl, dynlib: DLLUtilName, importc: "BIO_s_mem".}
  431. proc bioCtrlPending*(b: BIO): cInt{.cdecl, dynlib: DLLUtilName, importc: "BIO_ctrl_pending".}
  432. proc bioRead*(b: BIO, Buf: cstring, length: cInt): cInt{.cdecl,
  433. dynlib: DLLUtilName, importc: "BIO_read".}
  434. proc bioWrite*(b: BIO, Buf: cstring, length: cInt): cInt{.cdecl,
  435. dynlib: DLLUtilName, importc: "BIO_write".}
  436. proc sslSetConnectState*(s: SslPtr) {.cdecl,
  437. dynlib: DLLSSLName, importc: "SSL_set_connect_state".}
  438. proc sslSetAcceptState*(s: SslPtr) {.cdecl,
  439. dynlib: DLLSSLName, importc: "SSL_set_accept_state".}
  440. proc sslRead*(ssl: SslPtr, buf: cstring, num: cInt): cInt{.cdecl,
  441. dynlib: DLLSSLName, importc: "SSL_read".}
  442. proc sslPeek*(ssl: SslPtr, buf: cstring, num: cInt): cInt{.cdecl,
  443. dynlib: DLLSSLName, importc: "SSL_peek".}
  444. proc sslWrite*(ssl: SslPtr, buf: cstring, num: cInt): cInt{.cdecl,
  445. dynlib: DLLSSLName, importc: "SSL_write".}
  446. proc sslSetBio*(ssl: SslPtr, rbio, wbio: BIO) {.cdecl,
  447. dynlib: DLLSSLName, importc: "SSL_set_bio".}
  448. proc sslDoHandshake*(ssl: SslPtr): cint {.cdecl,
  449. dynlib: DLLSSLName, importc: "SSL_do_handshake".}
  450. proc ErrClearError*(){.cdecl, dynlib: DLLUtilName, importc: "ERR_clear_error".}
  451. proc ErrFreeStrings*(){.cdecl, dynlib: DLLUtilName, importc: "ERR_free_strings".}
  452. proc ErrRemoveState*(pid: cInt){.cdecl, dynlib: DLLUtilName, importc: "ERR_remove_state".}
  453. proc PEM_read_bio_RSA_PUBKEY*(bp: BIO, x: ptr PRSA, pw: pem_password_cb, u: pointer): PRSA {.cdecl,
  454. dynlib: DLLSSLName, importc.}
  455. proc RSA_verify*(kind: cint, origMsg: pointer, origMsgLen: cuint, signature: pointer,
  456. signatureLen: cuint, rsa: PRSA): cint {.cdecl, dynlib: DLLSSLName, importc.}
  457. when true:
  458. discard
  459. else:
  460. proc SslCtxSetCipherList*(arg0: PSSL_CTX, str: cstring): cInt{.cdecl,
  461. dynlib: DLLSSLName, importc.}
  462. proc SslCtxNew*(meth: PSSL_METHOD): PSSL_CTX{.cdecl,
  463. dynlib: DLLSSLName, importc.}
  464. proc SslSetFd*(s: PSSL, fd: cInt): cInt{.cdecl, dynlib: DLLSSLName, importc.}
  465. proc SslCTXCtrl*(ctx: PSSL_CTX, cmd: cInt, larg: int, parg: Pointer): int{.
  466. cdecl, dynlib: DLLSSLName, importc.}
  467. proc SSLSetMode*(s: PSSL, mode: int): int
  468. proc SSLCTXGetMode*(ctx: PSSL_CTX): int
  469. proc SSLGetMode*(s: PSSL): int
  470. proc SslMethodV2*(): PSSL_METHOD{.cdecl, dynlib: DLLSSLName, importc.}
  471. proc SslMethodV3*(): PSSL_METHOD{.cdecl, dynlib: DLLSSLName, importc.}
  472. proc SslMethodTLSV1*(): PSSL_METHOD{.cdecl, dynlib: DLLSSLName, importc.}
  473. proc SslMethodV23*(): PSSL_METHOD{.cdecl, dynlib: DLLSSLName, importc.}
  474. proc SslCtxUsePrivateKey*(ctx: PSSL_CTX, pkey: SslPtr): cInt{.cdecl,
  475. dynlib: DLLSSLName, importc.}
  476. proc SslCtxUsePrivateKeyASN1*(pk: cInt, ctx: PSSL_CTX,
  477. d: cstring, length: int): cInt{.cdecl, dynlib: DLLSSLName, importc.}
  478. proc SslCtxUseCertificate*(ctx: PSSL_CTX, x: SslPtr): cInt{.cdecl,
  479. dynlib: DLLSSLName, importc.}
  480. proc SslCtxUseCertificateASN1*(ctx: PSSL_CTX, length: int, d: cstring): cInt{.
  481. cdecl, dynlib: DLLSSLName, importc.}
  482. # function SslCtxUseCertificateChainFile(ctx: PSSL_CTX; const filename: PChar):cInt;
  483. proc SslCtxUseCertificateChainFile*(ctx: PSSL_CTX, filename: cstring): cInt{.
  484. cdecl, dynlib: DLLSSLName, importc.}
  485. proc SslCtxSetDefaultPasswdCb*(ctx: PSSL_CTX, cb: PPasswdCb){.cdecl,
  486. dynlib: DLLSSLName, importc.}
  487. proc SslCtxSetDefaultPasswdCbUserdata*(ctx: PSSL_CTX, u: SslPtr){.cdecl,
  488. dynlib: DLLSSLName, importc.}
  489. # function SslCtxLoadVerifyLocations(ctx: PSSL_CTX; const CAfile: PChar; const CApath: PChar):cInt;
  490. proc SslCtxLoadVerifyLocations*(ctx: PSSL_CTX, CAfile: cstring, CApath: cstring): cInt{.
  491. cdecl, dynlib: DLLSSLName, importc.}
  492. proc SslNew*(ctx: PSSL_CTX): PSSL{.cdecl, dynlib: DLLSSLName, importc.}
  493. proc SslConnect*(ssl: PSSL): cInt{.cdecl, dynlib: DLLSSLName, importc.}
  494. proc SslGetVersion*(ssl: PSSL): cstring{.cdecl, dynlib: DLLSSLName, importc.}
  495. proc SslGetPeerCertificate*(ssl: PSSL): PX509{.cdecl, dynlib: DLLSSLName,
  496. importc.}
  497. proc SslCtxSetVerify*(ctx: PSSL_CTX, mode: cInt, arg2: PFunction){.cdecl,
  498. dynlib: DLLSSLName, importc.}
  499. proc SSLGetCurrentCipher*(s: PSSL): SslPtr{.cdecl, dynlib: DLLSSLName, importc.}
  500. proc SSLCipherGetName*(c: SslPtr): cstring{.cdecl, dynlib: DLLSSLName, importc.}
  501. proc SSLCipherGetBits*(c: SslPtr, alg_bits: var cInt): cInt{.cdecl,
  502. dynlib: DLLSSLName, importc.}
  503. proc SSLGetVerifyResult*(ssl: PSSL): int{.cdecl, dynlib: DLLSSLName, importc.}
  504. # libeay.dll
  505. proc X509New*(): PX509{.cdecl, dynlib: DLLUtilName, importc.}
  506. proc X509Free*(x: PX509){.cdecl, dynlib: DLLUtilName, importc.}
  507. proc X509NameOneline*(a: PX509_NAME, buf: cstring, size: cInt): cstring{.
  508. cdecl, dynlib: DLLUtilName, importc.}
  509. proc X509GetSubjectName*(a: PX509): PX509_NAME{.cdecl, dynlib: DLLUtilName,
  510. importc.}
  511. proc X509GetIssuerName*(a: PX509): PX509_NAME{.cdecl, dynlib: DLLUtilName,
  512. importc.}
  513. proc X509NameHash*(x: PX509_NAME): int{.cdecl, dynlib: DLLUtilName, importc.}
  514. # function SslX509Digest(data: PX509; typ: PEVP_MD; md: PChar; len: PcInt):cInt;
  515. proc X509Digest*(data: PX509, typ: PEVP_MD, md: cstring, length: var cInt): cInt{.
  516. cdecl, dynlib: DLLUtilName, importc.}
  517. proc X509print*(b: PBIO, a: PX509): cInt{.cdecl, dynlib: DLLUtilName, importc.}
  518. proc X509SetVersion*(x: PX509, version: cInt): cInt{.cdecl, dynlib: DLLUtilName,
  519. importc.}
  520. proc X509SetPubkey*(x: PX509, pkey: EVP_PKEY): cInt{.cdecl, dynlib: DLLUtilName,
  521. importc.}
  522. proc X509SetIssuerName*(x: PX509, name: PX509_NAME): cInt{.cdecl,
  523. dynlib: DLLUtilName, importc.}
  524. proc X509NameAddEntryByTxt*(name: PX509_NAME, field: cstring, typ: cInt,
  525. bytes: cstring, length, loc, theSet: cInt): cInt{.
  526. cdecl, dynlib: DLLUtilName, importc.}
  527. proc X509Sign*(x: PX509, pkey: EVP_PKEY, md: PEVP_MD): cInt{.cdecl,
  528. dynlib: DLLUtilName, importc.}
  529. proc X509GmtimeAdj*(s: PASN1_UTCTIME, adj: cInt): PASN1_UTCTIME{.cdecl,
  530. dynlib: DLLUtilName, importc.}
  531. proc X509SetNotBefore*(x: PX509, tm: PASN1_UTCTIME): cInt{.cdecl,
  532. dynlib: DLLUtilName, importc.}
  533. proc X509SetNotAfter*(x: PX509, tm: PASN1_UTCTIME): cInt{.cdecl,
  534. dynlib: DLLUtilName, importc.}
  535. proc X509GetSerialNumber*(x: PX509): PASN1_cInt{.cdecl, dynlib: DLLUtilName,
  536. importc.}
  537. proc EvpPkeyNew*(): EVP_PKEY{.cdecl, dynlib: DLLUtilName, importc.}
  538. proc EvpPkeyFree*(pk: EVP_PKEY){.cdecl, dynlib: DLLUtilName, importc.}
  539. proc EvpPkeyAssign*(pkey: EVP_PKEY, typ: cInt, key: Prsa): cInt{.cdecl,
  540. dynlib: DLLUtilName, importc.}
  541. proc EvpGetDigestByName*(Name: cstring): PEVP_MD{.cdecl, dynlib: DLLUtilName,
  542. importc.}
  543. proc EVPcleanup*(){.cdecl, dynlib: DLLUtilName, importc.}
  544. # function ErrErrorString(e: cInt; buf: PChar): PChar;
  545. proc SSLeayversion*(t: cInt): cstring{.cdecl, dynlib: DLLUtilName, importc.}
  546. proc OPENSSLaddallalgorithms*(){.cdecl, dynlib: DLLUtilName, importc.}
  547. proc CRYPTOcleanupAllExData*(){.cdecl, dynlib: DLLUtilName, importc.}
  548. proc RandScreen*(){.cdecl, dynlib: DLLUtilName, importc.}
  549. proc d2iPKCS12bio*(b: PBIO, Pkcs12: SslPtr): SslPtr{.cdecl, dynlib: DLLUtilName,
  550. importc.}
  551. proc PKCS12parse*(p12: SslPtr, pass: cstring, pkey, cert, ca: var SslPtr): cint{.
  552. dynlib: DLLUtilName, importc, cdecl.}
  553. proc PKCS12free*(p12: SslPtr){.cdecl, dynlib: DLLUtilName, importc.}
  554. proc RsaGenerateKey*(bits, e: cInt, callback: PFunction, cb_arg: SslPtr): PRSA{.
  555. cdecl, dynlib: DLLUtilName, importc.}
  556. proc Asn1UtctimeNew*(): PASN1_UTCTIME{.cdecl, dynlib: DLLUtilName, importc.}
  557. proc Asn1UtctimeFree*(a: PASN1_UTCTIME){.cdecl, dynlib: DLLUtilName, importc.}
  558. proc Asn1cIntSet*(a: PASN1_cInt, v: cInt): cInt{.cdecl, dynlib: DLLUtilName,
  559. importc.}
  560. proc i2dX509bio*(b: PBIO, x: PX509): cInt{.cdecl, dynlib: DLLUtilName, importc.}
  561. proc i2dPrivateKeyBio*(b: PBIO, pkey: EVP_PKEY): cInt{.cdecl,
  562. dynlib: DLLUtilName, importc.}
  563. # 3DES functions
  564. proc DESsetoddparity*(Key: des_cblock){.cdecl, dynlib: DLLUtilName, importc.}
  565. proc DESsetkeychecked*(key: des_cblock, schedule: des_key_schedule): cInt{.
  566. cdecl, dynlib: DLLUtilName, importc.}
  567. proc DESecbencrypt*(Input: des_cblock, output: des_cblock, ks: des_key_schedule,
  568. enc: cInt){.cdecl, dynlib: DLLUtilName, importc.}
  569. # implementation
  570. proc SSLSetMode(s: PSSL, mode: int): int =
  571. result = SSLctrl(s, SSL_CTRL_MODE, mode, nil)
  572. proc SSLCTXGetMode(ctx: PSSL_CTX): int =
  573. result = SSLCTXctrl(ctx, SSL_CTRL_MODE, 0, nil)
  574. proc SSLGetMode(s: PSSL): int =
  575. result = SSLctrl(s, SSL_CTRL_MODE, 0, nil)
  576. # <openssl/md5.h>
  577. type
  578. MD5_LONG* = cuint
  579. const
  580. MD5_CBLOCK* = 64
  581. MD5_LBLOCK* = int(MD5_CBLOCK div 4)
  582. MD5_DIGEST_LENGTH* = 16
  583. type
  584. MD5_CTX* = object
  585. A,B,C,D,Nl,Nh: MD5_LONG
  586. data: array[MD5_LBLOCK, MD5_LONG]
  587. num: cuint
  588. {.push callconv:cdecl, dynlib:DLLUtilName.}
  589. proc md5_Init*(c: var MD5_CTX): cint{.importc: "MD5_Init".}
  590. proc md5_Update*(c: var MD5_CTX; data: pointer; len: csize): cint{.importc: "MD5_Update".}
  591. proc md5_Final*(md: cstring; c: var MD5_CTX): cint{.importc: "MD5_Final".}
  592. proc md5*(d: ptr cuchar; n: csize; md: ptr cuchar): ptr cuchar{.importc: "MD5".}
  593. proc md5_Transform*(c: var MD5_CTX; b: ptr cuchar){.importc: "MD5_Transform".}
  594. {.pop.}
  595. from strutils import toHex, toLowerAscii
  596. proc hexStr(buf: cstring): string =
  597. # turn md5s output into a nice hex str
  598. result = newStringOfCap(32)
  599. for i in 0 ..< 16:
  600. result.add toHex(buf[i].ord, 2).toLowerAscii
  601. proc md5_File*(file: string): string {.raises: [IOError,Exception].} =
  602. ## Generate MD5 hash for a file. Result is a 32 character
  603. # hex string with lowercase characters (like the output
  604. # of `md5sum`
  605. const
  606. sz = 512
  607. let f = open(file,fmRead)
  608. var
  609. buf: array[sz,char]
  610. ctx: MD5_CTX
  611. discard md5_init(ctx)
  612. while(let bytes = f.readChars(buf, 0, sz); bytes > 0):
  613. discard md5_update(ctx, buf[0].addr, bytes)
  614. discard md5_final(buf[0].addr, ctx)
  615. f.close
  616. result = hexStr(addr buf)
  617. proc md5_Str*(str: string): string =
  618. ## Generate MD5 hash for a string. Result is a 32 character
  619. ## hex string with lowercase characters
  620. var
  621. ctx: MD5_CTX
  622. res: array[MD5_DIGEST_LENGTH,char]
  623. input = str.cstring
  624. discard md5_init(ctx)
  625. var i = 0
  626. while i < str.len:
  627. let L = min(str.len - i, 512)
  628. discard md5_update(ctx, input[i].addr, L)
  629. i += L
  630. discard md5_final(addr res, ctx)
  631. result = hexStr(addr res)