openssl学习问题小计

使用openssl 完成RSA大数与Pem转换测试时编译遇到以下问题:

sa_change.cpp:122:37: error: invalid use of incomplete type ‘RSA {aka struct rsa_st}’
         printf("%s\n", BN_bn2hex(rsa->n));
                                     ^~
In file included from /usr/include/openssl/crypto.h:31:0,
                 from /usr/include/openssl/bio.h:20,
                 from /usr/include/openssl/asn1.h:16,
                 from /usr/include/openssl/rsa.h:16,
                 from rsa_change.cpp:7:
/usr/include/openssl/ossl_typ.h:110:16: note: forward declaration of ‘RSA {aka struct rsa_st}’
 typedef struct rsa_st RSA;
                ^~~~~~
rsa_change.cpp:124:37: error: invalid use of incomplete type ‘RSA {aka struct rsa_st}’
         printf("%s\n", BN_bn2hex(rsa->e));
                                     ^~
In file included from /usr/include/openssl/crypto.h:31:0,
                 from /usr/include/openssl/bio.h:20,
                 from /usr/include/openssl/asn1.h:16,
                 from /usr/include/openssl/rsa.h:16,
                 from rsa_change.cpp:7:
/usr/include/openssl/ossl_typ.h:110:16: note: forward declaration of ‘RSA {aka struct rsa_st}’
 typedef struct rsa_st RSA;
                ^~~~~~
rsa_change.cpp:186:12: error: invalid use of incomplete type ‘RSA {aka struct rsa_st}’
         rsa->e = bne;
            ^~
In file included from /usr/include/openssl/crypto.h:31:0,
                 from /usr/include/openssl/bio.h:20,
                 from /usr/include/openssl/asn1.h:16,
                 from /usr/include/openssl/rsa.h:16,
                 from rsa_change.cpp:7:
/usr/include/openssl/ossl_typ.h:110:16: note: forward declaration of ‘RSA {aka struct rsa_st}’
 typedef struct rsa_st RSA;
                ^~~~~~
rsa_change.cpp:188:12: error: invalid use of incomplete type ‘RSA {aka struct rsa_st}’
         rsa->n = bnn;
            ^~
 

经查找确定问题原因是openssl版本升级时对某些API有些改动,可参考以下解决方案,或修改注释报错函数

As you are aware, OpenSSL 1.1.0 changed the visibility of a lot of struct members. You can no longer access the members directly. Instead, you have to use getter and setter functions.

Try RSA_get0_factors. The get0 means the reference counts are not incremented. Do not BN_free them.

void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q);

If the code supports multiple versions of OpenSSL, then you will need a guard because RSA_get0_factors is for OpenSSL 1.1.0 and above. Maybe something like the following. Also see OPENSSL_VERSION_NUMBER man page.

#include <openssl/opensslv.h>

#if OPENSSL_VERSION_NUMBER < 0x10100000L

    /* OpenSSL 1.0.2 and below (old code) */

#else

    /* OpenSSL 1.1.0 and above (new code) */

#endif
#if OPENSSL_VERSION_NUMBER < 0x10100005L
static void RSA_get0_key(const RSA *r, const BIGNUM **n, const BIGNUM **e, const BIGNUM **d, const BIGNUM **p, const BIGNUM **q)
{
    if(n != NULL)
        *n = r->n;

    if(e != NULL)
        *e = r->e;

    if(d != NULL)
        *d = r->d;

    if(p != NULL)
        *p = r->p;

    if(q != NULL)
        *q = r->q;
}
#endif

猜你喜欢

转载自blog.csdn.net/swj9099/article/details/85341675