【问题标题】:Extract RSA public key from a x509 char array with openssl使用 openssl 从 x509 字符数组中提取 RSA 公钥
【发布时间】:2013-03-10 07:43:24
【问题描述】:

这是一个 x509 格式的证书,其中存储了公钥和模数:

const unsigned char *certificateDataBytes = {/*data*/};

使用 OpenSSL 和 C,如何将其转换为 RSA 对象?我尝试了几种方法,但无法在 RSA_public_encrypt 中使用

【问题讨论】:

    标签: c openssl


    【解决方案1】:

    我认为你的意思是公钥进入 RSA * 结构。

    既然你有字节证书,如果它是DER编码字节,那么你需要先将它转换成X509 *结构。

     X509 * cert;
     EVP_PKEY * pubkey;
     //length is the length of the certificateDataBytes in terms of bytes.
     cert = d2i_x509 (NULL, certificateDataBytes, length);
     pubkey = X509_get_pubkey (cert);
    

    请注意,如果证书有 RSA 公钥,那么您可以通过以下方式获取 RSA 公钥:

     RSA * rsa
     rsa = EVP_PKEY_get1_RSA(pubkey);
    
     //Now rsa contains RSA public key. Use it.
    
     //After use, free the pubkey
     EVP_PKEY_free (pubkey);
    

    我希望这必须解决您的目的。 如果证书编码不同,请使用不同的功能。一次,你得到 X509 *,其余步骤相同。

    【讨论】:

    • 原来cert变成了null。我有 ASN.1 格式的。这与DER编码正确吗?发生了什么
    • @blake305:通常,ASN.1 是 DER 编码的。然而,其他编码也是可能的。有关 DER 编码示例,请参阅en.wikipedia.org/wiki/…。 cert 变为 NULL 意味着它可能无效。使用 ERR_print_errors_fp (FILE *fp) 将最后一个错误写入文件。它可能会给出有意义的描述。
    • 别忘了拨打RSA_free释放RSA*
    猜你喜欢
    • 2017-05-03
    • 2018-02-28
    • 2018-01-27
    • 1970-01-01
    • 2013-03-11
    • 2011-09-11
    • 1970-01-01
    • 2015-10-19
    • 1970-01-01
    相关资源
    最近更新 更多