javascriptrubyopalrb

How to get openssl in opalrb?


Earlier, I tried to compile this code from ruby to opalrb javascript

require 'openssl'


cert = """-----BEGIN CERTIFICATE-----
MIIF0jCCBLqgAwIBAgIQE6uZ3+oqXXc/+5sp3TQd3zANBgkqhkiG9w0BAQsFADB1
MUQwQgYDVQQDDDtBcHBsZSBXb3JsZHdpZGUgRGV2ZWxvcGVyIFJlbGF0aW9ucyBD
ZXJ0aWZpY2F0aW9uIEF1dGhvcml0eTELMAkGA1UECwwCRzMxEzARBgNVBAoMCkFw
cGxlIEluYy4xCzAJBgNVBAYTAlVTMB4XDTIyMTIzMDE1MzIxOVoXDTI1MTIyOTE1
MzIxOFowga0xGjAYBgoJkiaJk/IsZAEBDApMSDI4WEE3VDIyMUAwPgYDVQQDDDdp
UGhvbmUgRGlzdHJpYnV0aW9uOiBTdW5zaGluZSBJbnN1cmFuY2UgR3JvdXAgQ28u
LCBMdGQuMRMwEQYDVQQLDApMSDI4WEE3VDIyMSswKQYDVQQKDCJTdW5zaGluZSBJ
bnN1cmFuY2UgR3JvdXAgQ28uLCBMdGQuMQswCQYDVQQGEwJDTjCCASIwDQYJKoZI
hvcNAQEBBQADggEPADCCAQoCggEBAKorSaxSoBNGkikg3M2brfrD4dpWga8oIVIv
L7oKGpvjjYCY2S6tuNIEZA0cdpTIcQ84j31RAoU6Y6xgyCpvb5c7I28hFVlM4ssR
fXQ0O/as8aF+TZvQMnDreLOndS6lKpeCitWDKt3cJS0bxjCUxY8A0e0sCNncQJht
UEcdNaFEVbrsIVD0zuS6ii+UEIBpv8EKdcUKsygFCM/Cc5MJ4QF9Ke+U3VHbgJ1Z
IhbUpTakj8ZgMD+Djvqkt4WDXU2Qc3aROg3VWNTTTV230efImd/jH6bWGMKv8XtG
gPwoTzPqFB50LRDg3ghsqH6esTcEWf2olDMJBerBo1NZ1Ec6i3kCAwEAAaOCAiMw
ggIfMAwGA1UdEwEB/wQCMAAwHwYDVR0jBBgwFoAUCf7AFZD5r2QKkhK5JihjD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Iu
-----END CERTIFICATE-----"""

x509 = OpenSSL::X509::Certificate.new(cert)



puts x509.subject.to_s

Error {OpenSSL: uninitialized constant OpenSSL undefined}

Is it possible to compile ruby's openssl to javascript opalrb?

if yes how can I do it?


Solution

  • The short answer is no. Opal, like JRuby, doesn't support Ruby's C API. So in general it's not possible to compile Ruby's OpenSSL extension for Opal.

    Depending on your environment though, OpenSSL may be available. In particular, Node.js supports the following module: https://nodejs.org/api/tls.html . I am not aware of any bindings to that module in Opal, so you will have to write your own. Opal allows you to interpolate JavaScript code using the backtick operator or better you can use a native Opal stdlib library.

    For browser (or any other) environment, you may be able to compile OpenSSL to WASM, then interface with it using Opal. But if you want to go that route, perhaps Ruby WASM may prove to be a better choice.