Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Is there any problem with this code? After the call, the code behind will crash.
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 1.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    senmx
    wrote on last edited by senmx
    #1

    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

    JonBJ 1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      2
      • S senmx

        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

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @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.
        S 1 Reply Last reply
        1
        • S Offline
          S Offline
          senmx
          wrote on last edited by
          #4

          @Christian-Ehrlicher Tks.I will try it.

          1 Reply Last reply
          0
          • JonBJ JonB

            @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.
            S Offline
            S Offline
            senmx
            wrote on last edited by
            #5

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

            JonBJ 1 Reply Last reply
            0
            • S senmx

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

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

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

              S 1 Reply Last reply
              0
              • JonBJ JonB

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

                S Offline
                S Offline
                senmx
                wrote on last edited by
                #7

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

                JonBJ 1 Reply Last reply
                0
                • S senmx

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

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #8

                  @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.

                  S 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @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.

                    S Offline
                    S Offline
                    senmx
                    wrote on last edited by
                    #9

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

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved