- return RSA_generate_key(bits, exponent, indicator, NULL);
+ BIGNUM *bn_e = BN_new();
+ rsa_t *rsa = NULL;
+
+ if(!bn_e) {
+ abort();
+ }
+
+ BN_set_word(bn_e, exponent);
+
+#if OPENSSL_VERSION_MAJOR < 3
+ rsa = RSA_new();
+ BN_GENCB *cb = BN_GENCB_new();
+
+ if(!rsa || !cb) {
+ abort();
+ }
+
+ BN_GENCB_set(cb, indicator, NULL);
+
+ int result = RSA_generate_key_ex(rsa, (int) bits, bn_e, cb);
+
+ BN_GENCB_free(cb);
+
+ if(!result) {
+ fprintf(stderr, "Error during key generation!\n");
+ RSA_free(rsa);
+ rsa = NULL;
+ }
+
+#else
+ EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL);
+
+ bool ok = ctx
+ && EVP_PKEY_keygen_init(ctx) > 0
+ && EVP_PKEY_CTX_set1_rsa_keygen_pubexp(ctx, bn_e) > 0
+ && EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, (int)bits) > 0
+ && EVP_PKEY_keygen(ctx, &rsa) > 0;
+
+ if(ctx) {
+ EVP_PKEY_CTX_free(ctx);
+ }
+
+ if(!ok) {
+ openssl_err("generate key");
+ rsa = NULL;
+ }
+
+#endif
+
+ BN_free(bn_e);
+
+ return rsa;