c - 带有 EVP_PKEY 的 OpenSSL RSA_size

标签 c ssl openssl rsa pki

我有一个关于 RSA_size 的问题。

在 WIN32 上崩溃但在 linux 平台上工作的版本:

  ... 
  EVP_PKEY* pPublicKey = null;
  unsigned int uKeySize = 0;
  const unsigned char *pData;
  pData = a_publicKey->Key.Data; /* Key.Data = unsigned char *p containing the Key in a string version */
  pPublicKey = d2i_PublicKey(EVP_PKEY_RSA, null, &pData, a_publicKey->Key.Length);
  if(pPublicKey != null)
  {
    uKeySize = RSA_size(pPublicKey->pkey.rsa); //Crash
  }
  ...

适用于 win32 的版本(未在 linux 上测试,但我想它也适用):

  ... 
  EVP_PKEY* pPublicKey = null;
  RSA* pRsaPublicKey = null;
  unsigned int uKeySize = 0;
  const unsigned char *pData;
  pData = a_publicKey->Key.Data; /* Key.Data = unsigned char *p containing the Key in a string version */
  pPublicKey = d2i_PublicKey(EVP_PKEY_RSA, null, &pData, a_publicKey->Key.Length);
  if(pPublicKey != null)
  {
    pRsaPublicKey = EVP_PKEY_get1_RSA(pPublicKey);
    EVP_PKEY_free(pPublicKey);
    uKeySize = RSA_size(pRsaPublicKey);
  }
  ...

我不明白为什么第一个版本会崩溃。但是当我查看 pkey.rsa 结构时,值与第二个版本中的 RSA 指针中的值不同。 有什么想法吗?

最佳答案

我查看了 EVP_PKEY 结构,似乎 WIN32 和 linux 版本不同...... 所以我想我正在为我的 WIN32 使用一个非常旧的。

WIN32版本:

struct evp_pkey_st
    {
    int type;
    int save_type;
    int references;
    union   {
        char *ptr;
#ifndef OPENSSL_NO_RSA
        struct rsa_st *rsa; /* RSA */
#endif
#ifndef OPENSSL_NO_DSA
        struct dsa_st *dsa; /* DSA */
#endif
#ifndef OPENSSL_NO_DH
        struct dh_st *dh;   /* DH */
#endif
#ifndef OPENSSL_NO_EC
        struct ec_key_st *ec;   /* ECC */
#endif
        } pkey;
    int save_parameters;
    STACK_OF(X509_ATTRIBUTE) *attributes; /* [ 0 ] */
    } /* EVP_PKEY */;

Linux :

struct evp_pkey_st
    {
    int type;
    int save_type;
    int references;
    const EVP_PKEY_ASN1_METHOD *ameth;
    ENGINE *engine;
    union   {
        char *ptr;
#ifndef OPENSSL_NO_RSA
        struct rsa_st *rsa; /* RSA */
#endif
#ifndef OPENSSL_NO_DSA
        struct dsa_st *dsa; /* DSA */
#endif
#ifndef OPENSSL_NO_DH
        struct dh_st *dh;   /* DH */
#endif
#ifndef OPENSSL_NO_EC
        struct ec_key_st *ec;   /* ECC */
#endif
        } pkey;
    int save_parameters;
    STACK_OF(X509_ATTRIBUTE) *attributes; /* [ 0 ] */
    } /* EVP_PKEY */;

关于c - 带有 EVP_PKEY 的 OpenSSL RSA_size,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27396739/

相关文章:

ios - 从 iOS 设备解压 PKCS7 负载(MDM 注册)

C11 Atomic Acquire/Release 和 x86_64 缺乏加载/存储一致性?

ios - 在 iOS 中动态创建 x509 证书

wordpress - 站点报告(由某些浏览器) "not secure"尽管证书尚未过期

c++ - 从内存中加载 CA 证书

c - SSL_library_init 出于实际目的不可重入是什么意思?

c - C 信号处理程序中的竞争条件

c - scanf 之后 fgets 不起作用

c - 有哪些不同的方法可以使终端不回显我们使用标准库键入的内容?

vb.net - WCF/SSL 问题