Based on your comments, I assume that the certificates are in DER format, which you can convert to PEM using the openssl x509 (see openssl x509 command :
openssl x509 -inform DER -outform PEM -in certfile.cer -out certfile.pem
After that, you can instruct the OpenSSL Ruby library to use a trusted root certificate to authenticate the SSL connection with something like this:
require 'socket' require 'openssl' tcp_sock = TCPSocket.new("my.host.tld", 443) ctx = OpenSSL::SSL::SSLContext.new ctx.verify_mode = OpenSSL::SSL::VERIFY_PEER|OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT
After that, you should be able to read and write to ssl_sock , like any other Ruby IO object. If you have been provided with a client certificate to allow the server to authenticate you, you can configure the SSL context with:
ctx.cert = OpenSSL::X509::Certificate.new(File.read("my_cert.pem")) ctx.key = OpenSSL::PKey::RSA.new(File.read("my_key.rsa"))
before creating ssl_sock . The OpenSSL library also supports key types other than RSA, such as DSA (see OpenSSL :: PKey module .)
Finally, the last tip, if you are accessing the RESTful API, you might want to use a gem like rest-client instead of directly handling all HTTP / S connections. Regardless of whether such a library is suitable or useful, of course, it will depend on the service you use.
source share