Rather than creating 90 Lines of RAII wrappers, You could probably just rewrite it like this:
```c++
bool SetPrivKeyCleanup(BN_CTX *ctx,EC_POINT *point)
{
 if (ctx   != NULL)
  BN_CTX_free(ctx);
 if (point != NULL)
  EC_POINT_free(point);
 return false;
}

            if (!pkey || !group || !pub_key || !priv_key)
                goto err;

            order = BN_new();

            if ((ctx = BN_CTX_new()) == NULL)
                return SetPrivKeyCleanup(ctx,point);

            if (!EC_GROUP_get_order(group, order, ctx))
                return SetPrivKeyCleanup(ctx,point);

            // check if generator * priv_key == pub_key 
            if (BN_cmp(priv_key, order) >= 0)
                return SetPrivKeyCleanup(ctx,point);

            if ((point = EC_POINT_new(group)) == NULL)
                return SetPrivKeyCleanup(ctx,point);

            if (!EC_POINT_mul(group, point, priv_key, NULL, NULL, ctx))
                return SetPrivKeyCleanup(ctx,point);

            if (EC_POINT_cmp(group, point, pub_key, ctx) != 0)
                return SetPrivKeyCleanup(ctx,point);

            return true;
```

Since I didn't test this there is probably a good reason why I wouldn't work, but you get the basic idea.