【问题标题】:segfault with EVP_PKEY_decrypt()带有 EVP_PKEY_decrypt() 的段错误
【发布时间】:2020-11-14 01:53:57
【问题描述】:

我在运行此代码时遇到了段错误,我想知道原因。有什么想法吗?:

#include <openssl/evp.h>
#include <openssl/pem.h>
#include <string.h>

#include <openssl/err.h>

int main (void)
{
    char privateKey[] = "-----BEGIN RSA PRIVATE KEY-----\n"\
"MIIBOgIBAAJBAKj34GkxFhD90vcNLYLInFEX6Ppy1tPf9Cnzj4p4WGeKLs1Pt8Qu\n"\
"KUpRKfFLfRYC9AIKjbJTWit+CqvjWYzvQwECAwEAAQJAIJLixBy2qpFoS4DSmoEm\n"\
"o3qGy0t6z09AIJtH+5OeRV1be+N4cDYJKffGzDa88vQENZiRm0GRq6a+HPGQMd2k\n"\
"TQIhAKMSvzIBnni7ot/OSie2TmJLY4SwTQAevXysE2RbFDYdAiEBCUEaRQnMnbp7\n"\
"9mxDXDf6AU0cN/RPBjb9qSHDcWZHGzUCIG2Es59z8ugGrDY+pxLQnwfotadxd+Uy\n"\
"v/Ow5T0q5gIJAiEAyS4RaI9YG8EWx/2w0T67ZUVAw8eOMB6BIUg0Xcu+3okCIBOs\n"\
"/5OiPgoTdSy7bcF9IGpSE8ZgGKzgYQVZeN97YE00\n"\
"-----END RSA PRIVATE KEY-----\n";
    char ciphertext[] = "h3j3zLT2jXCaZuwF7cgUE/Zmc/5IsIfKbaTiBhpCJo86AiyuoA3Yvni+Lrm5wu2OGv2h5R7Zu3voFcHugiystw==";
    // base64 decode ciphertext
    EVP_DecodeBlock(ciphertext, ciphertext, strlen(ciphertext));

    BIO* bo = BIO_new(BIO_s_mem());
    BIO_write(bo, privateKey, strlen(privateKey));
    EVP_PKEY* pkey = 0;
    PEM_read_bio_PrivateKey(bo, &pkey, 0, 0);
    BIO_free(bo);

    EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new(pkey, NULL);
    EVP_PKEY_decrypt_init(ctx);
    EVP_PKEY_CTX_ctrl_str(ctx, "rsa_padding_mode", "oaep");
    EVP_PKEY_CTX_ctrl_str(ctx, "rsa_oaep_md", "sha256");
    EVP_PKEY_CTX_ctrl_str(ctx, "rsa_mgf1_md", "sha256");

    size_t plaintext_len;
    unsigned char* plaintext;

    EVP_PKEY_decrypt(ctx, plaintext, &plaintext_len, ciphertext, strlen(ciphertext));
    //plaintext[plaintext_len] = '\0';

    printf("%s\n", plaintext);
}

【问题讨论】:

    标签: c openssl segmentation-fault rsa


    【解决方案1】:

    在将 plaintext 传递给 EVP_PKEY_decrypt 之前,您忘记初始化了。

    参考evp_pkey_decrypt(3) - Linux man page,部分

        unsigned char* plaintext;
    
        EVP_PKEY_decrypt(ctx, plaintext, &plaintext_len, ciphertext, strlen(ciphertext));
        //plaintext[plaintext_len] = '\0';
    

    应该是

        unsigned char* plaintext;
    
        /* determine output size */
        EVP_PKEY_decrypt(ctx, NULL, &plaintext_len, ciphertext, strlen(ciphertext));
    
        /* allocate memory (+1 for terminating null-character) */
        plaintext = OPENSSL_malloc(plaintext_len + 1);
        if (plaintext == NULL) {
            /* allocation failed */
            return 1;
        } else {
            EVP_PKEY_decrypt(ctx, plaintext, &plaintext_len, ciphertext, strlen(ciphertext));
            plaintext[plaintext_len] = '\0'; /* required for %s */
        }
    

    【讨论】:

      猜你喜欢
      • 2013-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多