Hello guys…I am writing a test application to verify a certificate using openssl functions…In that,I need a module to successfully read a “pem” encoded certificate.I’m using PEM_read function for that…I wrote 2 kinds of modules(with & without using BIO)…
Here they go…
Note that I’m giving a “.pem” as an input from command line(“argv”)
The problem is PEM_read_X509 is always returnng NULL…SO reading fails…
Here is the second one…
X509 *x509Cert /*=X509_new(); result is same even if this statement is X509 *x509Cert = X509_new(); */
if ((cert=BIO_new(BIO_s_file())) == NULL)
printf("Error Initializing BIO pointer");
if (BIO_read_filename(cert,argv) <= 0)
printf("Error opening file
Reading from file success!
Reading fails in this too Please help me in this issue…Thanks in advance…
Now, I’m guessing that the second argument to PEM_read_X509 is supposed to be an *X509. But you pass the address of x so you are passing a **X509. You can only be sure by looking at the specification for this function.
If you compile with -Wall, you can get warnings about type mismatches. Don’t ignore type mismatch warnings, they could be the problem.
Hi…Thanks for the reply…I’ve checked the documentation online…As you said, the second argument i passed is **X509(this should be passed as per documentation).And the return type of PEM_read is *X509…where the problem lies…it returns NULL…I tried very hard to figure it out but, couldn’t ;(…
The PEM read functions all take an argument TYPE **x and return a TYPE * pointer. Where TYPE is whatever structure the function uses. If x is NULL then the parameter is ignored. If x is not NULL but *x is NULL then the structure returned will be written to *x. If neither x nor *x is NULL then an attempt is made to reuse the structure at *x (but see BUGS and EXAMPLES sections). Irrespective of the value of x a pointer to the structure is always returned (or NULL if an error occurred).
This suggests that you can simply pass in 0 for that argument and it will allocate a structure, read the cert into it, and return a pointer to it. As it is, you are asking it to reuse the cert in the space allocated by X509_new and this contains garbage.
There are quite a few hits on PEM_read_X509 by people who encountered the same problem. Might be worth reading through them.
Thank you so much for helping me out As you expected problem is because of initializing with garbage X509_new() …when I initialized *X509 with NULL, and PEM_read(fp,NULL,NULL,NULL) is returning successful *X509…thanks again…
I suspect it’s a little bit more complicated than that. I noticed that you called PEM_read_X509 twice, but you totally ignored the result of the first call. That first call probably succeeded and initialised the memory area which ruined it for the second call, where it tried to reuse the memory area. Only you can explain why you thought you need to call the function twice.
Yes i forgot to mention it in my earlier post that I’ve modified that part…I ve got a *X509 and used it to compare it with null…I am not calling the function again…so that part goes as