{"id":799,"date":"2009-12-08T11:53:07","date_gmt":"2009-12-08T11:53:07","guid":{"rendered":"http:\/\/pchero21.com\/?p=799"},"modified":"2009-12-08T11:53:07","modified_gmt":"2009-12-08T11:53:07","slug":"openlssl-ssl-%ed%86%b5%ec%8b%a0-%ed%81%b4%eb%9d%bc%ec%9d%b4%ec%96%b8%ed%8a%b8","status":"publish","type":"post","link":"http:\/\/pchero21.com\/?p=799","title":{"rendered":"OpenlSSL &#8211; SSL \ud1b5\uc2e0 \ud074\ub77c\uc774\uc5b8\ud2b8"},"content":{"rendered":"<p>\/\/ &nbsp; &nbsp;&nbsp; SSL_Client.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;<br \/>#include &lt;stdlib.h&gt;<br \/>#include &lt;string.h&gt;<br \/>#include &lt;ctype.h&gt;<br \/>#include &lt;netdb.h&gt;<\/p>\n<p>#include &lt;openssl\/bio.h&gt;<br \/>#include &lt;openssl\/err.h&gt;<br \/>#include &lt;openssl\/rand.h&gt;<br \/>#include &lt;openssl\/crypto.h&gt;<br \/>#include &lt;openssl\/evp.h&gt;<br \/>#include &lt;openssl\/ssl.h&gt;<\/p>\n<p>#include &lt;sys\/socket.h&gt;<br \/>#include &lt;sys\/stat.h&gt;<br \/>#include &lt;sys\/types.h&gt;<br \/>#include &lt;fcntl.h&gt;<br \/>#include &lt;arpa\/inet.h&gt;<\/p>\n<p>#define PORT 7921<br \/>#define SERVER_ADDRESS &#8220;127.0.0.1&#8221;<\/p>\n<p>int main(int argc, char** argv)<br \/>{<br \/>&nbsp;&nbsp; &nbsp;char *server_name = SERVER_ADDRESS;<br \/>&nbsp;&nbsp; &nbsp;unsigned short port = PORT;<br \/>&nbsp;&nbsp; &nbsp;<br \/>&nbsp;&nbsp; &nbsp;unsigned int addr;<br \/>&nbsp;&nbsp; &nbsp;struct sockaddr_in server_add;<br \/>&nbsp;&nbsp; &nbsp;struct hostent *host;<br \/>&nbsp;&nbsp; &nbsp;<br \/>&nbsp;&nbsp; &nbsp;int conn_socket;<br \/>&nbsp;&nbsp; &nbsp;int socket_type = SOCK_STREAM;<br \/>&nbsp;&nbsp; &nbsp;int retval;<br \/>&nbsp;&nbsp; &nbsp;<br \/>&nbsp;&nbsp; &nbsp;char *retString = NULL;<br \/>&nbsp;&nbsp; &nbsp;<br \/>&nbsp;&nbsp; &nbsp;const char *currentCipher;<br \/>&nbsp;&nbsp; &nbsp;char buffer[1000];<br \/>&nbsp;&nbsp; &nbsp;char message[100] = &#8220;\uc774\uac83\uc740 \ud074\ub77c\uc774\uc5b8\ud2b8\uac00 \ubcf4\ub0b4\ub294 \uba54\uc2dc\uc9c0\uc785\ub2c8\ub2e4.&#8221;;<br \/>&nbsp;&nbsp; &nbsp;<br \/>&nbsp;&nbsp; &nbsp;\/\/ SSL \uad6c\uc870\uccb4 \uc0dd\uc131<br \/>&nbsp;&nbsp; &nbsp;SSL_METHOD *meth;<br \/>&nbsp;&nbsp; &nbsp;SSL_CTX *ctx;<br \/>&nbsp;&nbsp; &nbsp;SSL *ssl;<br \/>&nbsp;&nbsp; &nbsp;X509 *server_cert;<br \/>&nbsp;&nbsp; &nbsp;<br \/>&nbsp;&nbsp; &nbsp;BIO *errBIO;<br \/>&nbsp;&nbsp; &nbsp;<br \/>&nbsp;&nbsp; &nbsp;if((errBIO = BIO_new(BIO_s_file())) != NULL)<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;BIO_set_fp(errBIO, stderr, BIO_NOCLOSE|BIO_FP_TEXT);<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;<br \/>&nbsp;&nbsp; &nbsp;SSL_load_error_strings();<br \/>&nbsp;&nbsp; &nbsp;SSLeay_add_ssl_algorithms();<br \/>&nbsp;&nbsp; &nbsp;meth = SSLv3_method();<br \/>&nbsp;&nbsp; &nbsp;ctx = SSL_CTX_new(meth);<br \/>&nbsp;&nbsp; &nbsp;<br \/>&nbsp;&nbsp; &nbsp;if(ctx == NULL) {<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;BIO_printf(errBIO, &#8220;SSL_CTX \uc0dd\uc131 \uc5d0\ub7ec&#8221;);<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;ERR_print_errors(errBIO);<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;exit(1);<br \/>&nbsp;&nbsp; &nbsp;}<br \/>&nbsp;&nbsp; &nbsp;<br \/>&nbsp;&nbsp; &nbsp;\/\/ \uc11c\ubc84 \uc774\ub984\uc774 \uc54c\ud30c\ubcb3\uc778 DNS\ub85c \ub418\uc5b4 \uc788\uc744 \uacbd\uc6b0<br \/>&nbsp;&nbsp; &nbsp;if(isalpha(server_name[0])) {<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;host = gethostbyname(server_name);<br \/>&nbsp;&nbsp; &nbsp;}<br \/>&nbsp;&nbsp; &nbsp;\/\/ \uc11c\ubc84 \uc774\ub984\uc774 IP \ub85c \ub418\uc5b4 \uc788\uc744 \uacbd\uc6b0<br \/>&nbsp;&nbsp; &nbsp;else {<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;addr = inet_addr(server_name);<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;host = gethostbyaddr((char *)&amp;addr, 4, AF_INET);<br \/>&nbsp;&nbsp; &nbsp;}<br \/>&nbsp;&nbsp; &nbsp;<br \/>&nbsp;&nbsp; &nbsp;if(host == NULL) {<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;fprintf(stderr, &#8220;\uc54c \uc218 \uc5c6\ub294 \uc8fc\uc18c[%s] \uc785\ub2c8\ub2e4!n&#8221;, server_name);<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;exit(1);<br \/>&nbsp;&nbsp; &nbsp;}<br \/>&nbsp;&nbsp; &nbsp;<br \/>&nbsp;&nbsp; &nbsp;memset(&amp;server_add, 0, sizeof(server_add));<br \/>&nbsp;&nbsp; &nbsp;memcpy(&amp;(server_add.sin_addr), host-&gt;h_addr, host-&gt;h_length);<br \/>&nbsp;&nbsp; &nbsp;server_add.sin_family = host-&gt;h_addrtype;<br \/>&nbsp;&nbsp; &nbsp;server_add.sin_port = htons(port);<br \/>&nbsp;&nbsp; &nbsp;<br \/>&nbsp;&nbsp; &nbsp;conn_socket = socket(AF_INET, socket_type, 0);<br \/>&nbsp;&nbsp; &nbsp;if(conn_socket &lt; 0) {<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;fprintf(stderr, &#8220;\uc18c\ucf13 \uc0dd\uc131 \uc5d0\ub7ec!n&#8221;);<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;exit(1);<br \/>&nbsp;&nbsp; &nbsp;}<br \/>&nbsp;&nbsp; &nbsp;<br \/>&nbsp;&nbsp; &nbsp;printf(&#8220;[%s] \uc11c\ubc84\uc5d0 \uc5f0\uacb0\uc911&#8230;n&#8221;, server_name);<br \/>&nbsp;&nbsp; &nbsp;if(connect(conn_socket, (struct sockaddr*)&amp;server_add, sizeof(server_add)) == -1) {<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;fprintf(stderr, &#8220;connect \uc5d0\ub7ec!n&#8221;);<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;exit(1);<br \/>&nbsp;&nbsp; &nbsp;}<br \/>&nbsp;&nbsp; &nbsp;<br \/>&nbsp;&nbsp; &nbsp;\/\/ \uc138\uc158 \ud0a4\ub97c \ub9cc\ub4e4\uae30 \uc704\ud55c \ub79c\ub364 \uc218\ub97c \uc704\ud55c Seed \uacf5\uae09<br \/>&nbsp;&nbsp; &nbsp;printf(&#8220;\ub79c\ub364 \uc218 \uc0dd\uc131\uc911&#8230;n&#8221;);<br \/>&nbsp;&nbsp; &nbsp;RAND_status();<br \/>&nbsp;&nbsp; &nbsp;printf(&#8220;\ub79c\ub364 \uc218 \uc0dd\uc131 \uc644\ub8cc!b&#8221;);<br \/>&nbsp;&nbsp; &nbsp;<br \/>&nbsp;&nbsp; &nbsp;ssl = SSL_new(ctx);<br \/>&nbsp;&nbsp; &nbsp;if(ssl == NULL) {<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;BIO_printf(errBIO, &#8220;SSL \uc0dd\uc131 \uc5d0\ub7ec!n&#8221;);<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;ERR_print_errors(errBIO);<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;exit(1);<br \/>&nbsp;&nbsp; &nbsp;}<br \/>&nbsp;&nbsp; &nbsp;<br \/>&nbsp;&nbsp; &nbsp;SSL_set_fd(ssl, conn_socket);<br \/>&nbsp;&nbsp; &nbsp;retval = SSL_connect(ssl);<br \/>&nbsp;&nbsp; &nbsp;if(retval == -1) {<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;BIO_printf(errBIO, &#8220;SSL connect \uc5d0\ub7ec!n&#8221;);<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;ERR_print_errors(errBIO);<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;exit(1);<br \/>&nbsp;&nbsp; &nbsp;}<br \/>&nbsp;&nbsp; &nbsp;<br \/>&nbsp;&nbsp; &nbsp;currentCipher = SSL_CIPHER_get_name(SSL_get_current_cipher(ssl));<br \/>&nbsp;&nbsp; &nbsp;printf(&#8220;SSL \uc5f0\uacb0, \uc0ac\uc6a9 \uc54c\uace0\ub9ac\uc998 \ud30c\ub77c\uba54\ud130: [%s]n&#8221;, currentCipher);<br \/>&nbsp;&nbsp; &nbsp;<br \/>&nbsp;&nbsp; &nbsp;server_cert = SSL_get_peer_certificate(ssl);<br \/>&nbsp;&nbsp; &nbsp;if(server_cert == NULL) {<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;BIO_printf(errBIO, &#8220;\uc11c\ubc84 \uc778\uc99d\uc11c\ub97c \ubc1b\uc744 \uc218 \uc5c6\uc74c.&#8221;);<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;ERR_print_errors(errBIO);<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;exit(1);<br \/>&nbsp;&nbsp; &nbsp;}<br \/>&nbsp;&nbsp; &nbsp;printf(&#8220;Server certificate:n&#8221;);<br \/>&nbsp;&nbsp; &nbsp;<br \/>&nbsp;&nbsp; &nbsp;retString = NULL;<br \/>&nbsp;&nbsp; &nbsp;<br \/>&nbsp;&nbsp; &nbsp;\/\/ \uc8fc\uccb4\uc758 DN\uc744 \ubb38\uc790\uc5f4\ub85c \uc5bb\uc74c<br \/>&nbsp;&nbsp; &nbsp;retString = X509_NAME_oneline(X509_get_subject_name(server_cert), 0, 0);<br \/>&nbsp;&nbsp; &nbsp;if(retString == NULL) {<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;BIO_printf(errBIO, &#8220;\uc11c\ubc84 \uc778\uc99d\uc11c\uc5d0\uc11c \uc8fc\uccb4\uc758 DN\uc744 \uc77d\uc744 \uc218 \uc5c6\uc74c.&#8221;);<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;ERR_print_errors(errBIO);<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;exit(1);<br \/>&nbsp;&nbsp; &nbsp;}<br \/>&nbsp;&nbsp; &nbsp;<br \/>&nbsp;&nbsp; &nbsp;printf(&#8220;t subject: %sn&#8221;, retString);<br \/>&nbsp;&nbsp; &nbsp;free(retString);<br \/>&nbsp;&nbsp; &nbsp;<br \/>&nbsp;&nbsp; &nbsp;\/\/ \ubc1c\uae09\uc790\uc758 DN\uc744 \ubb38\uc790\uc5f4\ub85c \uc5bb\uc74c<br \/>&nbsp;&nbsp; &nbsp;retString = X509_NAME_oneline(X509_get_issuer_name(server_cert), 0, 0);<br \/>&nbsp;&nbsp; &nbsp;if(retString == NULL) {<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;BIO_printf(errBIO, &#8220;\uc11c\ubc84 \uc778\uc99d\uc11c\uc5d0\uc11c \ubc1c\uae09\uc790\uc758 DN\uc744 \uc77d\uc744 \uc218 \uc5c6\uc74c.&#8221;);<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;ERR_print_errors(errBIO);<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;exit(1);<br \/>&nbsp;&nbsp; &nbsp;}<br \/>&nbsp;&nbsp; &nbsp;printf(&#8220;t issuer: %sn&#8221;, retString);<br \/>&nbsp;&nbsp; &nbsp;free(retString);<br \/>&nbsp;&nbsp; &nbsp;<br \/>&nbsp;&nbsp; &nbsp;X509_free(server_cert);<br \/>&nbsp;&nbsp; &nbsp;<br \/>&nbsp;&nbsp; &nbsp;retval = SSL_write(ssl, &#8220;hi!!_from client&#8221;, strlen(&#8220;hi!!_from client&#8221;));<br \/>&nbsp;&nbsp; &nbsp;if(retval == -1) {<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;BIO_printf(errBIO, &#8220;SSL write \uc5d0\ub7ec!n&#8221;);<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;ERR_print_errors(errBIO);<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;exit(1);<br \/>&nbsp;&nbsp; &nbsp;}<br \/>&nbsp;&nbsp; &nbsp;<br \/>&nbsp;&nbsp; &nbsp;retval = SSL_read(ssl, buffer, sizeof(buffer) &#8211; 1);<br \/>&nbsp;&nbsp; &nbsp;if(retval == -1) {<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;BIO_printf(errBIO, &#8220;SSL read \uc5d0\ub7ec!n&#8221;);<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;ERR_print_errors(errBIO);<br \/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;exit(1);<br \/>&nbsp;&nbsp; &nbsp;}<br \/>&nbsp;&nbsp; &nbsp;buffer[retval] = &#8216;\u0000&#8217;;<br \/>&nbsp;&nbsp; &nbsp;printf(&#8220;\uc11c\ubc84\ub85c\ubd80\ud130 \ub370\uc774\ud130 \uc804\uc1a1: [%s], \uae38\uc774:%dn&#8221;, buffer, retval);<br \/>&nbsp;&nbsp; &nbsp;<br \/>&nbsp;&nbsp; &nbsp;SSL_shutdown(ssl);<br \/>&nbsp;&nbsp; &nbsp;<br \/>&nbsp;&nbsp; &nbsp;close(conn_socket);<br \/>&nbsp;&nbsp; &nbsp;SSL_free(ssl);<br \/>&nbsp;&nbsp; &nbsp;SSL_CTX_free(ctx);<br \/>&nbsp;&nbsp; &nbsp;<br \/>&nbsp;&nbsp; &nbsp;return 0;<br \/>}<br \/><a href=\"http:\/\/pchero21.com\/wp-content\/uploads\/1\/XUA8jQk84Z.c\" class=\"aligncenter\"  \/>XUA8jQk84Z.c<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\/\/ &nbsp; &nbsp;&nbsp; SSL_Client.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=799\">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\/799"}],"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=799"}],"version-history":[{"count":0,"href":"http:\/\/pchero21.com\/index.php?rest_route=\/wp\/v2\/posts\/799\/revisions"}],"wp:attachment":[{"href":"http:\/\/pchero21.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=799"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/pchero21.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=799"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/pchero21.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=799"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}