简体   繁体   中英

OpenSSL RSA_size with EVP_PKEY

I have a question regarding RSA_size.

Version that crash on WIN32 but works on linux platforms :

  ... 
  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
  }
  ...

Version that work on win32 (not tested on linux but I suppose it works as well):

  ... 
  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);
  }
  ...

I do not understand why the first version crash. But when I look into the pkey.rsa structure, values are not the same as in the RSA pointer in the 2nd version. Any ideas ?

I looked into EVP_PKEY struct and it seems that WIN32 and linux version are different... So I guess i am using a really old one for my WIN32.

WIN32 version :

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 */;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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