On this page
End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
OpenSSL::X509::Store
The X509 certificate store holds trusted CA certificates used to verify peer certificates.
The easiest way to create a useful certificate store is:
cert_store = OpenSSL::X509::Store.new cert_store.set_default_paths
This will use your system’s built-in certificates.
If your system does not have a default set of certificates you can obtain a set extracted from Mozilla CA certificate store by cURL maintainers here: curl.haxx.se/docs/caextract.html (You may wish to use the firefox-db2pem.sh script to extract the certificates from a local install to avoid man-in-the-middle attacks.)
After downloading or generating a cacert.pem from the above link you can create a certificate store from the pem file like this:
cert_store = OpenSSL::X509::Store.new cert_store.add_file 'cacert.pem'
The certificate store can be used with an SSLSocket like this:
ssl_context = OpenSSL::SSL::SSLContext.new ssl_context.verify_mode = OpenSSL::SSL::VERIFY_PEER ssl_context.cert_store = cert_store tcp_socket = TCPSocket.open 'example.com', 443 ssl_socket = OpenSSL::SSL::SSLSocket.new tcp_socket, ssl_context
Class methods 1
Instance methods 11
The certificate chain constructed by the last call of verify.
See also StoreContext#chain.
The error code set by the last call of verify.
See also StoreContext#error.
The description for the error code set by the last call of verify.
See also StoreContext#error_string.
The callback for additional certificate verification. It is invoked for each certificate in the chain and can be used to implement custom certificate verification conditions.
The callback is invoked with two values, a boolean that indicates if the pre-verification by OpenSSL has succeeded or not, and the StoreContext in use.
The callback can use StoreContext#error= to change the error code as needed. The callback must return either true or false.
NOTE: any exception raised within the callback will be ignored.
See also the man page X509_STORE_CTX_set_verify_cb(3).