简体   繁体   English

将 RSA 公钥从字符串传递到 RSA * 结构时出错

[英]error passing RSA public key from string to RSA * struct

I try to load a public RSA key to my program.我尝试将公共 RSA 密钥加载到我的程序中。 I use openssl lib in C. The key is defined in header crypt.h file:我在 C 中使用 openssl lib。密钥在头文件crypt.h文件中定义:

#define PUBLIC_KEY  "-----BEGIN PUBLIC KEY-----\n" \
                    "MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAuDcT2OiToi9edqil/3ha\n" \
                    "1MMrgqYvdYObRkb8C5SZtf8iv4LynuHi3hvEtybqFW84Uf2l3Dxuz3IWVp2v1C2f\n" \
                    "cUxwf+hFqL4Nt2Ww/XOKLtFXTSX/2fPYoelmLja2grHPSuFx6N4gtDUXRIGSYXIz\n" \
                    "c2GBx8lOiOP+qUguFmWZ9E0GiMLvh4XUQUzTGr4ZNNoc5LODdO0g7beFFqhntt9w\n" \
                    "EKdceGQtA7Zyt5l13x0lj+RONdPJkDFZrNGdqDwNUSog9mufpvR1P2UW09pC+lzy\n" \
                    "z32P+w0U3Za4zv4Btis9ky16vaqdN/KlDWJRt+4W9TQSAL0x9w708OQr0Sf2CXNq\n" \
                    "EobSLZ/aybe75yQmzFqmn10X+NuwTtJkArIKK7JONCBSxjohQmTZw0U497TCEHia\n" \
                    "itzgsHNLf1of31G/3GK5rCkm9fl39nnrg0yJi1cONTDjSHzKlrPhA584jFRD0CIO\n" \
                    "VNHYgsVLuFQuJ0WkZON8uEZELXN1ZyWnmPXH8wSwxjud65JD4JSQornCXorBMfxd\n" \
                    "DFEeeNk8tjgPMaCTLFNP/gfVDzkvcct9RoC8uZxHk8zgrGOwuxOZ/ZyihE7M8v0i\n" \
                    "+VwG9iMrPl8W3+KqunIt/FB4Le1vJ0yYorK64DRNdeMAIsYq2iFWO3GXNsX631/K\n" \
                    "EuZJS8tGNhK9dF5umo0GceUCAwEAAQ==\n" \
                    "-----END PUBLIC KEY-----\n"

The RSA key is supposed to be saved in RSA * structure. RSA 密钥应该保存在RSA *结构中。 I use a global variable for that.我为此使用了一个全局变量。

RSA *rsaPubkey;

The main() calls a function that will take PUBLIC_KEY and pass it to rsaPubkey . main()调用一个函数,该函数将获取PUBLIC_KEY并将其传递给rsaPubkey The function is shown bellow:功能如下图所示:

#include "crypt.h"

int EVP_PKEY_get_type(EVP_PKEY *pkey) {
    if (!pkey)
        return EVP_PKEY_NONE;

    return EVP_PKEY_type(pkey->type);
}

int PublicRSAKeyToStruct(const char *pubKey) {
    BIO* bio;
    bio = BIO_new_mem_buf(pubKey, (int)sizeof(pubKey));
    if (bio == NULL) {
        printf("bio");
        return -1;
    }

    EVP_PKEY* pkey;
    PEM_read_bio_PUBKEY(bio, &pkey, NULL, NULL);
    if (pkey == NULL) {
        printf("pkey");
        return -1;
    }

    int type = EVP_PKEY_get_type(pkey);
    if (type != EVP_PKEY_RSA && type != EVP_PKEY_RSA2) {
        printf("get type");
        return -1;
    }

    rsaPubkey = EVP_PKEY_get1_RSA(pkey);
    if (rsaPubkey == NULL) {
        printf("pubkey");
        return -1;
    }

    EVP_PKEY_free(pkey);
    BIO_free(bio);
    return 0;
}

We call the function like that:我们这样调用函数:

status = PublicRSAKeyToStruct(PUBLIC_KEY);
if (status == -1) {
    exit(EXIT_FAILURE);
}

My problem is that the PublicRSAKeyToStruct() function throws me the bellow user defined error:我的问题是PublicRSAKeyToStruct()函数向我抛出以下用户定义的错误:

get type

This means that we failed to pass EVP_PKEY_get_type() .这意味着我们未能通过EVP_PKEY_get_type() I can't understand why this happen though.我不明白为什么会发生这种情况。 I have also no clue if the problem is on the previous steps.我也不知道问题是否出在前面的步骤上。

Your PEM_read call failed because the BIO you gave it doesn't access the correct data, but you didn't check the return value, and you didn't initialize pkey so even though it's not valid your pkey == NULL check doesn't catch it.你的 PEM_read 调用失败,因为你给它的 BIO 没有访问正确的数据,但你没有检查返回值,你没有初始化pkey所以即使它无效你的pkey == NULL检查也没有抓住它。 As described in the man page on your system (unless Windows) or on the web the second argument to BIO_new_mem_buf must either be the number of bytes in the buffer or -1 if buffer contains a null-terminated C string -- and a string literal like yours does yield a null-terminated string so -1 is suitable -- but sizeof(apointer) is neither of these.如系统(除非 Windows)或网络上的手册页中所述, BIO_new_mem_buf的第二个参数必须是缓冲区中的字节数或 -1 如果缓冲区包含以空字符结尾的 C 字符串 -- 和字符串文字就像你的一样,确实会产生一个以空字符结尾的字符串,所以 -1 是合适的——但sizeof(apointer)不是这些。

The remainder of your code is correct and works fine if you give it a valid EVP_PKEY* .如果您给它一个有效的EVP_PKEY* ,您的代码的其余部分是正确的并且可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM