openssl / openssl

TLS/SSL and crypto library
https://www.openssl.org
Apache License 2.0
25.21k stars 10.01k forks source link

Compiling openSSL to WebAssembly on Mac, the funtion RSA_generate_key_ex(rsa, 1024, bn, NULL) returns 0 #18935

Open sjjia opened 2 years ago

sjjia commented 2 years ago

C++ code:

#ifndef EM_PORT_API
#    if defined(__EMSCRIPTEN__)
#        include <emscripten.h>
#        if defined(__cplusplus)
#            define EM_PORT_API(rettype) extern "C" rettype EMSCRIPTEN_KEEPALIVE
#        else
#            define EM_PORT_API(rettype) rettype EMSCRIPTEN_KEEPALIVE
#        endif
#    else
#        if defined(__cplusplus)
#            define EM_PORT_API(rettype) extern "C" rettype
#        else
#            define EM_PORT_API(rettype) rettype
#        endif
#    endif
#endif

#include <openssl/rsa.h>
#include <openssl/x509.h>
#include <openssl/err.h>

EM_PORT_API (int) generateKeypair()
{
    RSA* rsa = RSA_new();
    BIGNUM *bn = BN_new();
    RSA* _keypair = NULL;

    int setRet = BN_set_word(bn, RSA_F4);
    int rsaRet = RSA_generate_key_ex(rsa, 1024, bn, NULL);
    printf("setRet = %d \n", setRet);
    printf("rsaRet = %d \n", rsaRet);
    if (setRet && rsaRet) {
        _keypair = rsa;
        printf("_keypair create success!! \n");
    }
    if (bn) {
        BN_free(bn);
    }
    if (rsa && _keypair==NULL) {
        RSA_free(rsa);
    }
    return 1;
}

apiTest.html code

<html>
  <head>
    <meta charset="utf-8">
    <title>Emscripten:Export1</title>
  </head>
  <body>
    <script>
    Module = {};
    Module.onRuntimeInitialized = function() {
      var ret = Module._generateKeypair();
    }
    </script>
    <script src="api.js"></script>
  </body>
</html>

Compiling openSSL to WebAssembly on Mac

the version is openssl-1.1.1d, download from https://www.openssl.org/source/old/1.1.1/openssl-1.1.1d.tar.gz the Compiling step :

make api.js by emcc

emcc -sLLD_REPORT_UNDEFINED --no-entry -std=c++11 -s USE_PTHREADS -sMAIN_MODULE api.cpp openssl/libcrypto.a openssl/libssl.a  api.js

When running the demo,the funtion RSA_generate_key_ex(rsa, 1024, bn, NULL) returns 0. image

I don't know which step is wrong , Help Me!!!

cx-github commented 2 years ago

anyone know this question?

nhorman commented 2 months ago

marking as inactive, will be closed at the end of 3.4 dev barring further input