@@ -127,14 +127,21 @@ end
127127
128128const SSL_MODE_AUTO_RETRY = 0x00000004
129129
130+ # Use NetworkOptions for default CA file so that it can be configured using the standard
131+ # environment variables (JULIA_SSL_CA_ROOTS_PATH, SSL_CERT_DIR, and SSL_CERT_FILE).
132+ # TODO : On Windows and macOS `ca_roots` return `nothing` to indicate that system configured
133+ # certificates should be preferred but for now we fall back to the certificate from
134+ # MozillaCACerts_jll.
135+ default_cacert () = something (NetworkOptions. ca_roots (), MozillaCACerts_jll. cacert)
136+
130137"""
131138 This is the global context structure which is created by a server or client once per program life-time
132139 and which holds mainly default values for the SSL structures which are later created for the connections.
133140"""
134141mutable struct SSLContext
135142 ssl_ctx:: Ptr{Cvoid}
136143
137- function SSLContext (ssl_method:: SSLMethod , verify_file:: String = MozillaCACerts_jll . cacert )
144+ function SSLContext (ssl_method:: SSLMethod , verify_file:: String = default_cacert () )
138145 ssl_ctx = ccall (
139146 (:SSL_CTX_new , libssl),
140147 Ptr{Cvoid},
@@ -154,27 +161,38 @@ mutable struct SSLContext
154161 (SSLContext, Cint, Clong, Ptr{Cvoid}),
155162 ssl_context, 33 , SSL_MODE_AUTO_RETRY, C_NULL )
156163 if ! isempty (verify_file)
157- @assert ccall (
158- (:SSL_CTX_load_verify_locations , libssl),
159- Cint,
160- (SSLContext, Ptr{Cchar}, Ptr{Cchar}),
161- ssl_context,
162- verify_file,
163- C_NULL ) == 1
164+ ret = ca_chain! (ssl_context, verify_file)
165+ if ret != 1
166+ error (" Failed to validate CA certificates at '$(verify_file) '." )
167+ end
164168 end
165169
166170 return ssl_context
167171 end
168172end
169173
170174function ca_chain! (ssl_context:: SSLContext , cacert:: String )
171- ccall (
172- (:SSL_CTX_load_verify_locations , libssl),
173- Cint,
174- (SSLContext, Ptr{Cchar}, Ptr{Cchar}),
175- ssl_context,
176- cacert,
177- C_NULL )
175+
176+ if isfile (cacert)
177+ ccall (
178+ (:SSL_CTX_load_verify_locations , libssl),
179+ Cint,
180+ (SSLContext, Ptr{Cchar}, Ptr{Cchar}),
181+ ssl_context,
182+ cacert,
183+ C_NULL )
184+ elseif isdir (cacert)
185+ ccall (
186+ (:SSL_CTX_load_verify_locations , libssl),
187+ Cint,
188+ (SSLContext, Ptr{Cchar}, Ptr{Cchar}),
189+ ssl_context,
190+ C_NULL ,
191+ cacert)
192+ else
193+ ArgumentError (" Invalid CA certificates location: $cacert " )
194+ end
195+
178196end
179197
180198function free (ssl_context:: SSLContext )
0 commit comments