opensslx509

X509_STORE_CTX_init calls free on X509_STORE_CTX?


I ran into an issue of a segfault when calling X509_STORE_CTX_init. gdb showed it was calling X509_STORE_CTX_free on the 1st arg X509_STORE_CTX. This link https://linux.die.net/man/3/x509_store_ctx_init says it's not a good idea to do this

X509_STORE_CTX ctx;
X509_STORE_CTX_init(&ctx, store, cert, chain);

which I had been doing. But if I used the suggested way of creating a new X509_STORE_CTX, how can I know if I should free it later or if X509_STORE_CTX_init has already freed it?


Solution

  • "crypto/x5090.h." is an OpenSSL internal header file. It does not get installed as part of the OpenSSL public API headers and you should not include it in your code. If you are only using public API calls it is not possible to stack allocate the X509_STORE_CTX. Create an X509_STORE_CTX via X509_STORE_CTX_new.

    It should be perfectly fine to call X509_STORE_CTX_init() on an X509_STORE_CTX allocated via X509_STORE_CTX_new - you should not expect a seg fault in this case. It is always safe to call X509_STORE_CTX_free on the context passed to X509_STORE_CTX_init().