certverify.c Example 'C' code for certificate validation

Dear 丶 2022-05-17 06:00 227阅读 0赞
  1. Introduction
  2. The example 'C' program certverify.c demonstrates how to perform a basic certificate validation against a root certificate authority, using the OpenSSL library functions.
  3. Example Code Listing
  4. /* ------------------------------------------------------------ *
  5. * file: certverify.c *
  6. * purpose: Example code for OpenSSL certificate validation *
  7. * author: 06/12/2012 Frank4DD *
  8. * *
  9. * gcc -o certverify certverify.c -lssl -lcrypto *
  10. * ------------------------------------------------------------ */
  11. #include <openssl/bio.h>
  12. #include <openssl/err.h>
  13. #include <openssl/pem.h>
  14. #include <openssl/x509.h>
  15. #include <openssl/x509_vfy.h>
  16. int main() {
  17. const char ca_bundlestr[] = "./ca-bundle.pem";
  18. const char cert_filestr[] = "./cert-file.pem";
  19. BIO *certbio = NULL;
  20. BIO *outbio = NULL;
  21. X509 *error_cert = NULL;
  22. X509 *cert = NULL;
  23. X509_NAME *certsubject = NULL;
  24. X509_STORE *store = NULL;
  25. X509_STORE_CTX *vrfy_ctx = NULL;
  26. int ret;
  27. /* ---------------------------------------------------------- *
  28. * These function calls initialize openssl for correct work. *
  29. * ---------------------------------------------------------- */
  30. OpenSSL_add_all_algorithms();
  31. ERR_load_BIO_strings();
  32. ERR_load_crypto_strings();
  33. /* ---------------------------------------------------------- *
  34. * Create the Input/Output BIO's. *
  35. * ---------------------------------------------------------- */
  36. certbio = BIO_new(BIO_s_file());
  37. outbio = BIO_new_fp(stdout, BIO_NOCLOSE);
  38. /* ---------------------------------------------------------- *
  39. * Initialize the global certificate validation store object. *
  40. * ---------------------------------------------------------- */
  41. if (!(store=X509_STORE_new()))
  42. BIO_printf(outbio, "Error creating X509_STORE_CTX object\n");
  43. /* ---------------------------------------------------------- *
  44. * Create the context structure for the validation operation. *
  45. * ---------------------------------------------------------- */
  46. vrfy_ctx = X509_STORE_CTX_new();
  47. /* ---------------------------------------------------------- *
  48. * Load the certificate and cacert chain from file (PEM). *
  49. * ---------------------------------------------------------- */
  50. ret = BIO_read_filename(certbio, cert_filestr);
  51. if (! (cert = PEM_read_bio_X509(certbio, NULL, 0, NULL))) {
  52. BIO_printf(outbio, "Error loading cert into memory\n");
  53. exit(-1);
  54. }
  55. ret = X509_STORE_load_locations(store, ca_bundlestr, NULL);
  56. if (ret != 1)
  57. BIO_printf(outbio, "Error loading CA cert or chain file\n");
  58. /* ---------------------------------------------------------- *
  59. * Initialize the ctx structure for a verification operation: *
  60. * Set the trusted cert store, the unvalidated cert, and any *
  61. * potential certs that could be needed (here we set it NULL) *
  62. * ---------------------------------------------------------- */
  63. X509_STORE_CTX_init(vrfy_ctx, store, cert, NULL);
  64. /* ---------------------------------------------------------- *
  65. * Check the complete cert chain can be build and validated. *
  66. * Returns 1 on success, 0 on verification failures, and -1 *
  67. * for trouble with the ctx object (i.e. missing certificate) *
  68. * ---------------------------------------------------------- */
  69. ret = X509_verify_cert(vrfy_ctx);
  70. BIO_printf(outbio, "Verification return code: %d\n", ret);
  71. if(ret == 0 || ret == 1)
  72. BIO_printf(outbio, "Verification result text: %s\n",
  73. X509_verify_cert_error_string(vrfy_ctx->error));
  74. /* ---------------------------------------------------------- *
  75. * The error handling below shows how to get failure details *
  76. * from the offending certificate. *
  77. * ---------------------------------------------------------- */
  78. if(ret == 0) {
  79. /* get the offending certificate causing the failure */
  80. error_cert = X509_STORE_CTX_get_current_cert(vrfy_ctx);
  81. certsubject = X509_NAME_new();
  82. certsubject = X509_get_subject_name(error_cert);
  83. BIO_printf(outbio, "Verification failed cert:\n");
  84. X509_NAME_print_ex(outbio, certsubject, 0, XN_FLAG_MULTILINE);
  85. BIO_printf(outbio, "\n");
  86. }
  87. /* ---------------------------------------------------------- *
  88. * Free up all structures *
  89. * ---------------------------------------------------------- */
  90. X509_STORE_CTX_free(vrfy_ctx);
  91. X509_STORE_free(store);
  92. X509_free(cert);
  93. BIO_free_all(certbio);
  94. BIO_free_all(outbio);
  95. exit(0);
  96. }
  97. Compiling the Code
  98. Compile the test program with:
  99. > gcc -o certverify certverify.c -lssl -lcrypto
  100. Example Output
  101. The program expects a certificate file called cert-file.pem and a CA certificate chain file ca-bundle.pem in the same directory. If both the server and root certificates are found and loaded, the following output is produced for a successful validation:
  102. fm@susie114:~> ./certverify
  103. Verification return code: 1
  104. Verification result text: ok
  105. Below is an example for a failed validation. In this particular case, the CA certificate does not match the signature on the server certificate.
  106. fm@susie114:~> ./certverify
  107. Verification return code: 0
  108. Verification result text: unable to get local issuer certificate
  109. Verification failed cert:
  110. countryName = JP
  111. stateOrProvinceName = Tokyo
  112. commonName = nagios.frank4dd.com
  113. If there is an error in loading the certificate, the verification returns a negative number.
  114. fm@susie114:~> ./certverify
  115. Error loading cert into memory
  116. Verification return code: -1
  117. Remarks
  118. The typical error message for a failed verification, " unable to get local issuer certificate", is very vague about the real problem. Consider having a longer chain of certificates, which one is broken? What is the offending key, serial, subject, etc? In the code above, this message is also returned if the CA file does not exist. Careful programming should catch all possible cases, OpenSSL offers to implement a callback function to handle errors individually with great detail.
  119. The OpenSSL manual page for verify explains how the certificate verification process works.
  120. The verification mode can be additionally controlled through 15 flags. Some add debugging options, but most notably are the flags for adding checks of external certificate revocation lists (CRL). These lists are still rarely used, both by CA vendors and SSL clients. That's why we see vendors like Microsoft issuing emergency patches to disable root CA's "trusted" status (MS advisory examples 2607712, 2718704) from their CA certificate lists. With a growing number of CA, signing mistakes, key compromise or collision attacks happen more often lately, resulting in a similar growing list of invalidated root certs. Below is an example screenshot of a recent Windows XP certificate store:
  121. invalidated, untrusted root CA's in Windows XP
  122. In further workarounds, Microsoft developed automatic root update mechanisms for their latest OS versions. Correctly establishing trust is critical, but today it is still rather messy and often far from being timely and fully reliable.

发表评论

表情:
评论列表 (有 0 条评论,227人围观)

还没有评论,来说两句吧...

相关阅读