{"id":797,"date":"2009-12-07T19:33:24","date_gmt":"2009-12-07T19:33:24","guid":{"rendered":"http:\/\/pchero21.com\/?p=797"},"modified":"2009-12-07T19:33:24","modified_gmt":"2009-12-07T19:33:24","slug":"openlssl-%ec%9d%b8%ec%a6%9d%ec%84%9c-%ec%9d%b8%ec%a6%9d%ed%95%98%ea%b8%b0","status":"publish","type":"post","link":"http:\/\/pchero21.com\/?p=797","title":{"rendered":"OpenlSSL &#8211; \uc778\uc99d\uc11c \uc778\uc99d\ud558\uae30"},"content":{"rendered":"<p>\/\/ &nbsp; &nbsp;&nbsp; Verify_Cert.c<br \/>\/\/ &nbsp; &nbsp; &nbsp;<br \/>\/\/ &nbsp; &nbsp;&nbsp; Copyright 2009 Kim Sung-tae &lt;pchero21@gmail.com&gt;<br \/>\/\/ &nbsp; &nbsp; &nbsp;<br \/>\/\/ &nbsp; &nbsp;&nbsp; This program is free software; you can redistribute it and\/or modify<br \/>\/\/ &nbsp; &nbsp;&nbsp; it under the terms of the GNU General Public License as published by<br \/>\/\/ &nbsp; &nbsp;&nbsp; the Free Software Foundation; either version 2 of the License, or<br \/>\/\/ &nbsp; &nbsp;&nbsp; (at your option) any later version.<br \/>\/\/ &nbsp; &nbsp; &nbsp;<br \/>\/\/ &nbsp; &nbsp;&nbsp; This program is distributed in the hope that it will be useful,<br \/>\/\/ &nbsp; &nbsp;&nbsp; but WITHOUT ANY WARRANTY; without even the implied warranty of<br \/>\/\/ &nbsp; &nbsp;&nbsp; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.&nbsp; See the<br \/>\/\/ &nbsp; &nbsp;&nbsp; GNU General Public License for more details.<br \/>\/\/ &nbsp; &nbsp; &nbsp;<br \/>\/\/ &nbsp; &nbsp;&nbsp; You should have received a copy of the GNU General Public License<br \/>\/\/ &nbsp; &nbsp;&nbsp; along with this program; if not, write to the Free Software<br \/>\/\/ &nbsp; &nbsp;&nbsp; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,<br \/>\/\/ &nbsp; &nbsp;&nbsp; MA 02110-1301, USA.<\/p>\n<p>#include &lt;stdio.h&gt;<\/p>\n<p>#include &lt;openssl\/bio.h&gt;<br \/>#include &lt;openssl\/conf.h&gt;<br \/>#include &lt;openssl\/err.h&gt;<br \/>#include &lt;openssl\/bn.h&gt;<br \/>#include &lt;openssl\/rsa.h&gt;<br \/>#include &lt;openssl\/evp.h&gt;<br \/>#include &lt;openssl\/objects.h&gt;<br \/>#include &lt;openssl\/x509.h&gt;<br \/>#include &lt;openssl\/x509v3.h&gt;<br \/>#include &lt;openssl\/rand.h&gt;<br \/>#include &lt;openssl\/pem.h&gt;<\/p>\n<p>#define CA_CERT_FILE &nbsp; &nbsp;&#8220;rootcert.pem&#8221;<br \/>#define CRL_FILE &nbsp; &nbsp; &nbsp; &nbsp;&#8220;testcrl.pem&#8221;<br \/>#define CERT_FILE &nbsp; &nbsp; &nbsp; &nbsp;&#8220;newcert.pem&#8221;<\/p>\n<p>int verifyCallbackfunc(int ok, X509_STORE_CTX *store)<br \/>{<br \/>&nbsp;&nbsp; &nbsp;X509 *cert;<br \/>&nbsp;&nbsp; &nbsp;<br \/>&nbsp;&nbsp; &nbsp;if(!ok) {<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;cert = X509_STORE_CTX_get_current_cert(store);<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;printf(&#8220;error:%sn&#8221;, X509_verify_cert_error_string(store-&gt;error));<br \/>&nbsp;&nbsp; &nbsp;}<br \/>&nbsp;&nbsp; &nbsp;<br \/>&nbsp;&nbsp; &nbsp;return ok;<br \/>}<\/p>\n<p>int main(int argc, char** argv)<br \/>{<br \/>&nbsp;&nbsp; &nbsp;BIO *bio_err;<br \/>&nbsp;&nbsp; &nbsp;int retVal;<br \/>&nbsp;&nbsp; &nbsp;char *retString;<br \/>&nbsp;&nbsp; &nbsp;<br \/>&nbsp;&nbsp; &nbsp;X509 *cert;<br \/>&nbsp;&nbsp; &nbsp;X509_STORE *store;<br \/>&nbsp;&nbsp; &nbsp;X509_LOOKUP *lookup;<br \/>&nbsp;&nbsp; &nbsp;X509_STORE_CTX *storeCtx;<br \/>&nbsp;&nbsp; &nbsp;<br \/>&nbsp;&nbsp; &nbsp;BIO *certBIO = NULL;<br \/>&nbsp;&nbsp; &nbsp;<br \/>&nbsp;&nbsp; &nbsp;OpenSSL_add_all_algorithms();<br \/>&nbsp;&nbsp; &nbsp;<br \/>&nbsp;&nbsp; &nbsp;\/\/ \ud654\uba74 \ucd9c\ub825\uc6a9 BIO \uc0dd\uc131<br \/>&nbsp;&nbsp; &nbsp;if((bio_err = BIO_new(BIO_s_file())) != NULL)<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;BIO_set_fp(bio_err, stderr, BIO_NOCLOSE|BIO_FP_TEXT);<br \/>&nbsp;&nbsp; &nbsp;<br \/>&nbsp;&nbsp; &nbsp;\/\/ \uc778\uc99d\ud560 \uc778\uc99d\uc11c\ub97c \uc77d\uae30 \uc704\ud55c BIO \uc0dd\uc131<br \/>&nbsp;&nbsp; &nbsp;certBIO = BIO_new(BIO_s_file());<br \/>&nbsp;&nbsp; &nbsp;if(BIO_read_filename(certBIO, CERT_FILE) &lt;= 0) {<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;BIO_printf(bio_err, &#8220;\uc778\uc99d\uc11c \ud30c\uc77c [%s]\uc744 \uc5ec\ub294\ub370 \uc5d0\ub7ec\uac00 \ubc1c\uc0dd \ud588\uc2b5\ub2c8\ub2e4.&#8221;, CERT_FILE);<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;ERR_print_errors(bio_err);<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;exit(1);<br \/>&nbsp;&nbsp; &nbsp;}<br \/>&nbsp;&nbsp; &nbsp;<br \/>&nbsp;&nbsp; &nbsp;\/\/ \uc778\uc99d\ud560 \uc778\uc99d\uc11c\ub97c \ud30c\uc77c\ub85c\ubd80\ud130 \uc77d\uc5b4 X509 \uad6c\uc870\uccb4\ub85c \ubcc0\ud658<br \/>&nbsp;&nbsp; &nbsp;cert = PEM_read_bio_X509(certBIO, NULL, NULL, NULL);<br \/>&nbsp;&nbsp; &nbsp;<br \/>&nbsp;&nbsp; &nbsp;if(cert == NULL) {<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;BIO_printf(bio_err, &#8220;CA \uc778\uc99d\uc11c\ub97c \ub85c\ub4dc\ud560 \uc218 \uc5c6\uc2b5\ub2c8\ub2e4.&#8221;);<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;ERR_print_errors(bio_err);<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;exit(1);<br \/>&nbsp;&nbsp; &nbsp;}<br \/>&nbsp;&nbsp; &nbsp;<br \/>&nbsp;&nbsp; &nbsp;\/\/ \uc778\uc99d\uc11c\ub97c \uc800\uc7a5\ud560 STORE \uad6c\uc870\uccb4 \uc0dd\uc131<br \/>&nbsp;&nbsp; &nbsp;store = X509_STORE_new();<br \/>&nbsp;&nbsp; &nbsp;if(store == NULL) {<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;BIO_printf(bio_err, &#8220;X509_STORE \ub97c \uc0dd\uc131\ud560 \uc218 \uc5c6\uc2b5\ub2c8\ub2e4.&#8221;);<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;ERR_print_errors(bio_err);<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;exit(1);<br \/>&nbsp;&nbsp; &nbsp;}<br \/>&nbsp;&nbsp; &nbsp;<br \/>&nbsp;&nbsp; &nbsp;\/\/ \ucf5c\ubc31 \ud568\uc218 \uc124\uc815<br \/>&nbsp;&nbsp; &nbsp;X509_STORE_set_verify_cb_func(store, verifyCallbackfunc);<br \/>&nbsp;&nbsp; &nbsp;<br \/>&nbsp;&nbsp; &nbsp;\/\/ \ud30c\uc77c\ub85c\ubd80\ud130 CA \uc778\uc99d\uc11c \uc77d\uc74c<br \/>&nbsp;&nbsp; &nbsp;if(!X509_STORE_load_locations(store, CA_CERT_FILE, NULL)) {<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;BIO_printf(bio_err, &#8220;CA \uc778\uc99d\uc11c\ub97c \ub85c\ub4dc\ud560 \uc218 \uc5c6\uc2b5\ub2c8\ub2e4.&#8221;);<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;ERR_print_errors(bio_err);<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;exit(1);<br \/>&nbsp;&nbsp; &nbsp;}<br \/>&nbsp;&nbsp; &nbsp;<br \/>&nbsp;&nbsp; &nbsp;\/\/ STORE\uc5d0 CA \uc778\uc99d\uc11c \ucd94\uac00<br \/>&nbsp;&nbsp; &nbsp;lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());<br \/>&nbsp;&nbsp; &nbsp;if(lookup == NULL) {<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;BIO_printf(bio_err, &#8220;X509_LOOKUP \ub97c \uc0dd\uc131\ud560 \uc218 \uc5c6\uc2b5\ub2c8\ub2e4.&#8221;);<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;ERR_print_errors(bio_err);<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;exit(1);<br \/>&nbsp;&nbsp; &nbsp;}<br \/>&nbsp;&nbsp; &nbsp;<br \/>&nbsp;&nbsp; &nbsp;\/\/ CRL \uc77d\uc74c<br \/>&nbsp;&nbsp; &nbsp;if(!X509_load_crl_file(lookup, CRL_FILE, X509_FILETYPE_PEM)) {<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;BIO_printf(bio_err, &#8220;CRL\uc744 \ub85c\ub4dc \ud560 \uc218 \uc5c6\uc2b5\ub2c8\ub2e4.&#8221;);<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;ERR_print_errors(bio_err);<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;exit(1);<br \/>&nbsp;&nbsp; &nbsp;}<br \/>&nbsp;&nbsp; &nbsp;<br \/>&nbsp;&nbsp; &nbsp;\/\/ CA \uc778\uc99d\uc11c, CRL \uc778\uc99d \ubaa8\ub450 \uc9c0\uc6d0<br \/>&nbsp;&nbsp; &nbsp;X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL);<br \/>&nbsp;&nbsp; &nbsp;<br \/>&nbsp;&nbsp; &nbsp;\/\/ STORE \ucee8\ud14d\uc2a4\ud2b8 \uc0dd\uc131<br \/>&nbsp;&nbsp; &nbsp;storeCtx = X509_STORE_CTX_new();<br \/>&nbsp;&nbsp; &nbsp;if(storeCtx == NULL) {<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;BIO_printf(bio_err, &#8220;X509_STORE_CTX\ub97c \uc0dd\uc131 \ud560 \uc218 \uc5c6\uc2b5\ub2c8\ub2e4.&#8221;);<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;ERR_print_errors(bio_err);<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;exit(1);<br \/>&nbsp;&nbsp; &nbsp;}<br \/>&nbsp;&nbsp; &nbsp;<br \/>&nbsp;&nbsp; &nbsp;if(!X509_STORE_CTX_init(storeCtx, store, cert, NULL)) {<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;BIO_printf(bio_err, &#8220;X509_STORE_CTX\ub97c \ucd08\uae30\ud654 \ud560 \uc218 \uc5c6\uc2b5\ub2c8\ub2e4.&#8221;);<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;ERR_print_errors(bio_err);<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;exit(1);<br \/>&nbsp;&nbsp; &nbsp;}<br \/>&nbsp;&nbsp; &nbsp;<br \/>&nbsp;&nbsp; &nbsp;\/\/ \uc778\uc99d\uc11c \uc778\uc99d<br \/>&nbsp;&nbsp; &nbsp;retVal = X509_verify_cert(storeCtx);<br \/>&nbsp;&nbsp; &nbsp;<br \/>&nbsp;&nbsp; &nbsp;if(retVal == 1) {<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;BIO_printf(bio_err, &#8220;\uc778\uc99d \ub418\uc5c8\uc2b5\ub2c8\ub2e4.&#8221;);<br \/>&nbsp;&nbsp; &nbsp;}<br \/>&nbsp;&nbsp; &nbsp;else {<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;BIO_printf(bio_err, &#8220;\uc778\uc99d\uc744 \ud560 \uc218 \uc5c6\uc2b5\ub2c8\ub2e4.&#8221;);<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;ERR_print_errors(bio_err);<br \/>&nbsp;&nbsp; &nbsp;}<br \/>&nbsp;&nbsp; &nbsp;<br \/>&nbsp;&nbsp; &nbsp;return 0;<br \/>}<\/p>\n<p><a href=\"http:\/\/pchero21.com\/wp-content\/uploads\/1\/XcClgYwHih.c\" class=\"aligncenter\"  \/>XcClgYwHih.c<\/a><a href=\"http:\/\/pchero21.com\/wp-content\/uploads\/1\/XObk62k8pF.pem\" class=\"aligncenter\"  \/>XObk62k8pF.pem<\/a><a href=\"http:\/\/pchero21.com\/wp-content\/uploads\/1\/XKaRlaHhnu.pem\" class=\"aligncenter\"  \/>XKaRlaHhnu.pem<\/a><a href=\"http:\/\/pchero21.com\/wp-content\/uploads\/1\/XD8GYZUkIz.pem\" class=\"aligncenter\"  \/>XD8GYZUkIz.pem<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\/\/ &nbsp; &nbsp;&nbsp; Verify_Cert.c\/\/ &nbsp; &nbsp; &nbsp;\/\/ &nbsp; &nbsp;&nbsp; Copyright 2009 Kim Sung-tae &lt;pchero21@gmail.com&gt;\/\/ &nbsp; &nbsp; &nbsp;\/\/ &nbsp; &nbsp;&nbsp; This program is free software; you can redistribute it and\/or modify\/\/ &nbsp; &nbsp;&nbsp; it under the terms of the GNU General &hellip; <a href=\"http:\/\/pchero21.com\/?p=797\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[53],"tags":[297,364],"_links":{"self":[{"href":"http:\/\/pchero21.com\/index.php?rest_route=\/wp\/v2\/posts\/797"}],"collection":[{"href":"http:\/\/pchero21.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/pchero21.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/pchero21.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/pchero21.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=797"}],"version-history":[{"count":0,"href":"http:\/\/pchero21.com\/index.php?rest_route=\/wp\/v2\/posts\/797\/revisions"}],"wp:attachment":[{"href":"http:\/\/pchero21.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=797"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/pchero21.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=797"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/pchero21.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=797"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}