c++ - mbedTLS pk 解析错误

标签 c++ linux mbedtls

谁能帮我找出为什么我在尝试从 unsigned char* 解析公钥/私钥时得到 -16000(错误的输入数据)?

这是我的代码(为简洁起见进行了编辑):

DataPoint* GetPublicKey(mbedtls_pk_context* pppctx)
{
    unsigned char* PKey = new unsigned char[16000];
    if (mbedtls_pk_write_pubkey_pem(pppctx, PKey, 16000) != 0)
    {
        delete[] PKey;
        return NULL;
    }
    DataPoint* Out = new DataPoint(strlen((char*)PKey) + 1); //Initializes an internal unsigned char* and size_t with the length of the key and the null byte
    memcpy(Out->Data, PKey, Out->Length);
    delete[] PKey;
    return Out;
}

void GenRSA(mbedtls_rsa_context* rs)
{
    mbedtls_rsa_gen_key(rs, mbedtls_ctr_drbg_random, &dctx, 2048, 65537);
}

int main()
{
    mbedtls_pk_context pctx;
    mbedtls_pk_init(&pctx);
    mbedtls_pk_setup(&pctx, mbedtls_pk_info_from_type(MBEDTLS_PK_RSA));

    DataPoint* Key = GetPublicKey(&some_context_with_GenRSA_called);

    cout << mbedtls_pk_parse_public_key(&pctx, Key->Data, Key->Length) << endl; //Returns -16000

    return 0
}

私钥也一样,我做错了什么?

最佳答案

docs for mbedtls_pk_parse_public_key说:

On entry, ctx must be empty, either freshly initialised with mbedtls_pk_init() or reset with mbedtls_pk_free().

您的伪代码调用 pctx 上的 mbedtls_pk_setup。也许这就是问题所在?

您可以检查其他转换器,例如 https://superdry.apphb.com/tools/online-rsa-key-converter看看他们是否可以解析您的 PEM?

关于c++ - mbedTLS pk 解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43453159/

相关文章:

c++ - Hex 到 Int 的转换在不同的环境中表现不同

c - MBEDTLS 有错误查找功能吗?

c++ - 具有体面的 C++ FFI 的可破解方案实现

c++ - 从哪里获得最新的 OpenGL32.lib?

c++ - C++ 中代码块的聚合墙时间

linux - 删除与模式匹配的行

linux - ssh 命令不会返回与在 ssh 下执行的命令相同的结果

c - MbedTLS 和 .NET BouncyCaSTLe 与 Curve25519 的互操作性问题

c - 使用 mbedlts 在内存受限的系统上使用 SHA 散列文件

c++ - 创建 Dll 文件