【问题标题】:OpenSSL RSA_size with EVP_PKEYOpenSSL RSA_size 和 EVP_PKEY
【发布时间】:2014-12-10 18:43:37
【问题描述】:

我有一个关于 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_get1_RSA回来的RSA*get1 表示引用计数增加了。 get0 表示计数递增,因此不需要*_free
  • @jww 是的,实际上我只是在“...”之后释放了它,但我知道这可能会导致误解。

标签: c ssl openssl rsa pki


【解决方案1】:

我查看了 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 */;

【讨论】:

  • EVP_PKEY* 的内存布局与您的代码无关。两者都有一个rsa 成员,因此两者都从结构的开头偏移。听起来好像发生了其他事情。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-11
相关资源
最近更新 更多