Problems with Open-Source Downloads read https://www.qt.io/blog/problem-with-open-source-downloads and https://forum.qt.io/post/638946

Is there any problem with this code? After the call, the code behind will crash.



  • Is there any problem with this code? It seems that the subsequent code will be abnormal after the call is completed.

    Feels related to this: EVP_PKEY_assign_RSA

        int ret = 0;
        RSA *r = NULL;
        BIGNUM *bne = NULL;
        int bits = 2048;
        unsigned long e = RSA_F4;
        EVP_PKEY* evpkey = NULL;
        BIO *pub = NULL;
        BIO *pri = NULL;
        size_t pub_len;
        size_t pri_len;
        char* pub_key = NULL;
        char* pri_key = NULL;
        QStringList list;
    
        bne = BN_new();
        ret = BN_set_word(bne, e);
        if(ret != 1) {
            goto free_all;
        }
        //
        r = RSA_new();
        ret = RSA_generate_key_ex(r, bits, bne, NULL);
        if(ret != 1) {
            goto free_all;
        }
    
        pub = BIO_new(BIO_s_mem());
        if(type == 1) {
            ret = PEM_write_bio_RSAPublicKey(pub, r);//PKCS#1
        } else if(type == 8) {
            ret = PEM_write_bio_RSA_PUBKEY(pub, r);//PKCS#8
        }
        if(ret != 1) {
            goto free_all;
        }
    
        pri = BIO_new(BIO_s_mem());
        if(type == 1) {
            ret = PEM_write_bio_RSAPrivateKey(pri, r, NULL, NULL, 0, NULL, NULL);
        } else if(type == 8) {
            evpkey = EVP_PKEY_new();
            if (evpkey == NULL) {
                goto free_all;
            }
            ret = EVP_PKEY_assign_RSA(evpkey, RSAPrivateKey_dup(r));
            if(ret != 1) {
                goto free_all;
            }
            ret = PEM_write_bio_PKCS8PrivateKey(pri, evpkey, NULL, NULL, 0, NULL, NULL);
        }
        if(ret != 1) {
            goto free_all;
        }
    
        pub_len = BIO_pending(pub);
        pri_len = BIO_pending(pri);
        pub_key = (char*)malloc(pub_len + 1);
        pri_key = (char*)malloc(pri_len + 1);
        BIO_read(pub, pub_key, pub_len);
        BIO_read(pri, pri_key, pri_len);
    
        list << QString(pub_key);
        list << QString(pri_key);
    
        free_all:
            BIO_free_all(pub);
            BIO_free_all(pri);
            RSA_free(r);
            EVP_PKEY_free(evpkey);
            BN_free(bne);
            free(pri_key);
            free(pub_key);
        return list;
    

    Env:

    Qt android 15.4.2
    openssl: openssl-1.1.1g


    update

    Note: The above code has now been corrected by me


  • Lifetime Qt Champion

    Use a debugger or memory checker like valgrind or address sanitizer to see what's going wrong.



  • @senmx
    As you know, this is the same code you have been asking about repeatedly in your other thread. So now you reprint it here.

    The answers are still the same:

    • Use a debugger.
    • It has nothing to do with Qt.
    • You have been asked multiple times where you get this code from, and multiple times you have refused to answer. It's not polite to ask questions and then choose not to answer questions others send back to you.


  • @Christian-Ehrlicher Tks.I will try it.



  • @JonB
    -_-. This code has no longer in the previous thread. This is another question.



  • @senmx
    I do see that you moved the code from the other thread to here. But the answers are still the same! :)



  • @JonB Can’t tell from the code if there is a problem? I am a newbie to both qt and c++.



  • @senmx
    I, and others, have repeatedly asked you questions about this. And asked you to respond. And you have chosen not to do so, presumably just ignoring our questions. I have had enough of asking each time, so am bowing out as you won't answer.



  • @JonB -_- Thank you for your patience. Has replied to you.


Log in to reply