Error incomplete definition of type 'struct ec_key_st' using OpenSSL

你。 提交于 2019-12-11 02:12:14

问题


I am trying to compute public key from given secret by openssl. I get this error:

main.c:27: error: incomplete definition of type 'struct ec_key_st'
  printf("d: %s\n", BN_bn2hex(eckey->priv_key));
                              ~~~~~^

Here is my code:

#include <stdio.h>

#include <openssl/ec.h>
#include <openssl/ecdsa.h>
#include <openssl/bn.h>
#include <openssl/obj_mac.h>

int main()
{
  BN_CTX *ctx = BN_CTX_new();

  EC_KEY *eckey = EC_KEY_new();
  EC_GROUP *group = EC_GROUP_new_by_curve_name(NID_secp256k1);
  EC_KEY_set_group(eckey, group);

  BIGNUM *prv = BN_new();
  BN_hex2bn(&prv, "b14fac12b3fa7dd6f2562a18d554fcd6818137ebb7e0d119ab0776d6407664f9");
  EC_KEY_set_private_key(eckey, prv);

  EC_POINT *Q = EC_POINT_new(group);
  EC_POINT_mul(group, Q, prv, NULL, NULL, ctx);
  EC_KEY_set_public_key(eckey, Q);

  if (EC_KEY_check_key(eckey))
    printf("Key succesfully checked.\n");

  printf("d: %s\n", BN_bn2hex(eckey->priv_key));
  printf("X: %s\n", BN_bn2hex(&eckey->pub_key->X));
  printf("Y: %s\n", BN_bn2hex(&eckey->pub_key->Y));

  EC_GROUP_free (group); group = NULL;
  EC_KEY_free (eckey); eckey = NULL;
  return 0;
}

What is wrong with the above code? If I remove printf lines, it works fine. I would appreciate if anybody helps me getting rid of this error.


回答1:


You're using OpenSSL 1.1, and they have decided that you shouldn't be poking about at the innards of their structures anymore.

eckey->priv_key was a valid way of accessing the private key in OpenSSL 1.0.x, but now the only correct way is EC_KEY_get0_private_key(eckey)

Likewise, for the public key it's EC_KEY_get0_public_key(eckey).

Both of these functions were declared during OpenSSL 1.0.x, so you can write the code to be the same between them.

So

printf("d: %s\n", BN_bn2hex(eckey->priv_key));
printf("X: %s\n", BN_bn2hex(&eckey->pub_key->X));
printf("Y: %s\n", BN_bn2hex(&eckey->pub_key->Y));

would become

{
    const BIGNUM* d = EC_KEY_get0_private_key(eckey);
    const EC_POINT* Q = EC_KEY_get0_public_key(eckey);
    const EC_GROUP* group = EC_KEY_get0_group(eckey);
    BIGNUM* x = BN_new();
    BIGNUM* y = BN_new();

    if (!EC_POINT_get_affine_coordinates_GFp(group, Q, x, y, null))
    {
        error();
    }

    printf("d: %s\n", BN_bn2hex(d));
    printf("X: %s\n", BN_bn2hex(x));
    printf("Y: %s\n", BN_bn2hex(y));

    BN_free(x);
    BN_free(y);
}

This keeps your code working, even when OpenSSL 1.1.1 decides to redo the struct layout which was hidden within ec_lcl.h




回答2:


You need to get "ec_lcl.h" header file from openssl libarary, in order to access members of struct "ec_key_st". Notice that "ECKEY" is defined as:

typedef struct ec_key_st EC_KEY;

So you put "ec_lcl.h" in your project folder and then change your code to this:

#include <stdio.h>

#include <openssl/ec.h>
#include <openssl/ecdsa.h>
#include <openssl/bn.h>
#include <openssl/obj_mac.h>

#include "ec_lcl.h"

int main()
{
  BN_CTX *ctx = BN_CTX_new();

  struct ec_key_st *eckey = EC_KEY_new();
  EC_GROUP *group = EC_GROUP_new_by_curve_name(NID_secp256k1);
  EC_KEY_set_group(eckey, group);

  BIGNUM *prv = BN_new();
  BN_hex2bn(&prv, "b14fac12b3fa7dd6f2562a18d554fcd6818137ebb7e0d119ab0776d6407664f9");
  EC_KEY_set_private_key(eckey, prv);

  EC_POINT *Q = EC_POINT_new(group);
  EC_POINT_mul(group, Q, prv, NULL, NULL, ctx);
  EC_KEY_set_public_key(eckey, Q);

  if (EC_KEY_check_key(eckey))
    printf("Key succesfully checked.\n");

  printf("d: %s\n", BN_bn2hex(eckey->priv_key));
  printf("X: %s\n", BN_bn2hex(&eckey->pub_key->X));
  printf("Y: %s\n", BN_bn2hex(&eckey->pub_key->Y));

  EC_GROUP_free (group); group = NULL;
  EC_KEY_free (eckey); eckey = NULL;
  return 0;
}


来源:https://stackoverflow.com/questions/46546230/error-incomplete-definition-of-type-struct-ec-key-st-using-openssl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!