As others mentioned, you can use openssl verify
. According to the documentation, it also checks the validity period.
Programmatically, it could mean hours of searching for kinda bad (or missing) documentation, reading code examples all over the web, and probably a headache.
To properly validate a certificate, you need to inform all the intermediate certificates. Normally you'd also inform the revocation list (CRL), but it's not required.
So, here's what you need to do in terms of code (OpenSSL):
X509_STORE_new
- Create a certificate store;
X509_STORE_CTX_new
- Create a store context;
X509_STORE_add_cert
- Add the CA (and all intermediary) certificate(s) to the trusted list of your certificate store (note: there's a function to lookup/load a list);
X509_STORE_add_crl
- Add the revoked certificates to the CRL of your certificate store (note: same as above);
X509_STORE_CTX_init
- Initialize your store context informing your certificate store;
X509_STORE_CTX_set_purpose
- Define the purpose if you need so;
X509_STORE_CTX_set_cert
- Tell the context which certificate you're going to validate;
X509_verify_cert
- Finally, validate it;
X509_STORE_CTX_cleanup
- If you want to reuse the context to validate another certificate, you clean it up and jump back to (5);
- Last but not least, deallocate (1) and (2);
Alternatively, a quick validation can be done with X509_verify
. However, be aware that it compares signatures solely.
When I needed it, took me a day of searching, reading and testing. Then I figured out everything I needed was right in the OpenSSL source-code. So, if you need an example, go straight to openssl-xxx/apps/verify.c.
IMPORTANT: NEVER use MD5. To understand the reason, read Creating a rogue CA certificate.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…