state_machine.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /* ====================================================================
  2. * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in
  13. * the documentation and/or other materials provided with the
  14. * distribution.
  15. *
  16. * 3. All advertising materials mentioning features or use of this
  17. * software must display the following acknowledgment:
  18. * "This product includes software developed by the OpenSSL Project
  19. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  20. *
  21. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  22. * endorse or promote products derived from this software without
  23. * prior written permission. For written permission, please contact
  24. * openssl-core@openssl.org.
  25. *
  26. * 5. Products derived from this software may not be called "OpenSSL"
  27. * nor may "OpenSSL" appear in their names without prior written
  28. * permission of the OpenSSL Project.
  29. *
  30. * 6. Redistributions of any form whatsoever must retain the following
  31. * acknowledgment:
  32. * "This product includes software developed by the OpenSSL Project
  33. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  36. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  38. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  44. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  45. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  46. * OF THE POSSIBILITY OF SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This product includes cryptographic software written by Eric Young
  50. * (eay@cryptsoft.com). This product includes software written by Tim
  51. * Hudson (tjh@cryptsoft.com).
  52. *
  53. */
  54. /*
  55. * Nuron, a leader in hardware encryption technology, generously
  56. * sponsored the development of this demo by Ben Laurie.
  57. *
  58. * See http://www.nuron.com/.
  59. */
  60. /*
  61. * the aim of this demo is to provide a fully working state-machine
  62. * style SSL implementation, i.e. one where the main loop acquires
  63. * some data, then converts it from or to SSL by feeding it into the
  64. * SSL state machine. It then does any I/O required by the state machine
  65. * and loops.
  66. *
  67. * In order to keep things as simple as possible, this implementation
  68. * listens on a TCP socket, which it expects to get an SSL connection
  69. * on (for example, from s_client) and from then on writes decrypted
  70. * data to stdout and encrypts anything arriving on stdin. Verbose
  71. * commentary is written to stderr.
  72. *
  73. * This implementation acts as a server, but it can also be done for a client. */
  74. #include <openssl/ssl.h>
  75. #include <assert.h>
  76. #include <unistd.h>
  77. #include <string.h>
  78. #include <openssl/err.h>
  79. #include <sys/types.h>
  80. #include <sys/socket.h>
  81. #include <netinet/in.h>
  82. /* die_unless is intended to work like assert, except that it happens
  83. always, even if NDEBUG is defined. Use assert as a stopgap. */
  84. #define die_unless(x) assert(x)
  85. typedef struct
  86. {
  87. SSL_CTX *pCtx;
  88. BIO *pbioRead;
  89. BIO *pbioWrite;
  90. SSL *pSSL;
  91. } SSLStateMachine;
  92. void SSLStateMachine_print_error(SSLStateMachine *pMachine,const char *szErr)
  93. {
  94. unsigned long l;
  95. fprintf(stderr,"%s\n",szErr);
  96. while((l=ERR_get_error()))
  97. {
  98. char buf[1024];
  99. ERR_error_string_n(l,buf,sizeof buf);
  100. fprintf(stderr,"Error %lx: %s\n",l,buf);
  101. }
  102. }
  103. SSLStateMachine *SSLStateMachine_new(const char *szCertificateFile,
  104. const char *szKeyFile)
  105. {
  106. SSLStateMachine *pMachine=malloc(sizeof *pMachine);
  107. int n;
  108. die_unless(pMachine);
  109. pMachine->pCtx=SSL_CTX_new(SSLv23_server_method());
  110. die_unless(pMachine->pCtx);
  111. n=SSL_CTX_use_certificate_file(pMachine->pCtx,szCertificateFile,
  112. SSL_FILETYPE_PEM);
  113. die_unless(n > 0);
  114. n=SSL_CTX_use_PrivateKey_file(pMachine->pCtx,szKeyFile,SSL_FILETYPE_PEM);
  115. die_unless(n > 0);
  116. pMachine->pSSL=SSL_new(pMachine->pCtx);
  117. die_unless(pMachine->pSSL);
  118. pMachine->pbioRead=BIO_new(BIO_s_mem());
  119. pMachine->pbioWrite=BIO_new(BIO_s_mem());
  120. SSL_set_bio(pMachine->pSSL,pMachine->pbioRead,pMachine->pbioWrite);
  121. SSL_set_accept_state(pMachine->pSSL);
  122. return pMachine;
  123. }
  124. void SSLStateMachine_read_inject(SSLStateMachine *pMachine,
  125. const unsigned char *aucBuf,int nBuf)
  126. {
  127. int n=BIO_write(pMachine->pbioRead,aucBuf,nBuf);
  128. /* If it turns out this assert fails, then buffer the data here
  129. * and just feed it in in churn instead. Seems to me that it
  130. * should be guaranteed to succeed, though.
  131. */
  132. assert(n == nBuf);
  133. fprintf(stderr,"%d bytes of encrypted data fed to state machine\n",n);
  134. }
  135. int SSLStateMachine_read_extract(SSLStateMachine *pMachine,
  136. unsigned char *aucBuf,int nBuf)
  137. {
  138. int n;
  139. if(!SSL_is_init_finished(pMachine->pSSL))
  140. {
  141. fprintf(stderr,"Doing SSL_accept\n");
  142. n=SSL_accept(pMachine->pSSL);
  143. if(n == 0)
  144. fprintf(stderr,"SSL_accept returned zero\n");
  145. if(n < 0)
  146. {
  147. int err;
  148. if((err=SSL_get_error(pMachine->pSSL,n)) == SSL_ERROR_WANT_READ)
  149. {
  150. fprintf(stderr,"SSL_accept wants more data\n");
  151. return 0;
  152. }
  153. SSLStateMachine_print_error(pMachine,"SSL_accept error");
  154. exit(7);
  155. }
  156. return 0;
  157. }
  158. n=SSL_read(pMachine->pSSL,aucBuf,nBuf);
  159. if(n < 0)
  160. {
  161. int err=SSL_get_error(pMachine->pSSL,n);
  162. if(err == SSL_ERROR_WANT_READ)
  163. {
  164. fprintf(stderr,"SSL_read wants more data\n");
  165. return 0;
  166. }
  167. SSLStateMachine_print_error(pMachine,"SSL_read error");
  168. exit(8);
  169. }
  170. fprintf(stderr,"%d bytes of decrypted data read from state machine\n",n);
  171. return n;
  172. }
  173. int SSLStateMachine_write_can_extract(SSLStateMachine *pMachine)
  174. {
  175. int n=BIO_pending(pMachine->pbioWrite);
  176. if(n)
  177. fprintf(stderr,"There is encrypted data available to write\n");
  178. else
  179. fprintf(stderr,"There is no encrypted data available to write\n");
  180. return n;
  181. }
  182. int SSLStateMachine_write_extract(SSLStateMachine *pMachine,
  183. unsigned char *aucBuf,int nBuf)
  184. {
  185. int n;
  186. n=BIO_read(pMachine->pbioWrite,aucBuf,nBuf);
  187. fprintf(stderr,"%d bytes of encrypted data read from state machine\n",n);
  188. return n;
  189. }
  190. void SSLStateMachine_write_inject(SSLStateMachine *pMachine,
  191. const unsigned char *aucBuf,int nBuf)
  192. {
  193. int n=SSL_write(pMachine->pSSL,aucBuf,nBuf);
  194. /* If it turns out this assert fails, then buffer the data here
  195. * and just feed it in in churn instead. Seems to me that it
  196. * should be guaranteed to succeed, though.
  197. */
  198. assert(n == nBuf);
  199. fprintf(stderr,"%d bytes of unencrypted data fed to state machine\n",n);
  200. }
  201. int OpenSocket(int nPort)
  202. {
  203. int nSocket;
  204. struct sockaddr_in saServer;
  205. struct sockaddr_in saClient;
  206. int one=1;
  207. int nSize;
  208. int nFD;
  209. int nLen;
  210. nSocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
  211. if(nSocket < 0)
  212. {
  213. perror("socket");
  214. exit(1);
  215. }
  216. if(setsockopt(nSocket,SOL_SOCKET,SO_REUSEADDR,(char *)&one,sizeof one) < 0)
  217. {
  218. perror("setsockopt");
  219. exit(2);
  220. }
  221. memset(&saServer,0,sizeof saServer);
  222. saServer.sin_family=AF_INET;
  223. saServer.sin_port=htons(nPort);
  224. nSize=sizeof saServer;
  225. if(bind(nSocket,(struct sockaddr *)&saServer,nSize) < 0)
  226. {
  227. perror("bind");
  228. exit(3);
  229. }
  230. if(listen(nSocket,512) < 0)
  231. {
  232. perror("listen");
  233. exit(4);
  234. }
  235. nLen=sizeof saClient;
  236. nFD=accept(nSocket,(struct sockaddr *)&saClient,&nLen);
  237. if(nFD < 0)
  238. {
  239. perror("accept");
  240. exit(5);
  241. }
  242. fprintf(stderr,"Incoming accepted on port %d\n",nPort);
  243. return nFD;
  244. }
  245. int main(int argc,char **argv)
  246. {
  247. SSLStateMachine *pMachine;
  248. int nPort;
  249. int nFD;
  250. const char *szCertificateFile;
  251. const char *szKeyFile;
  252. char rbuf[1];
  253. int nrbuf=0;
  254. if(argc != 4)
  255. {
  256. fprintf(stderr,"%s <port> <certificate file> <key file>\n",argv[0]);
  257. exit(6);
  258. }
  259. nPort=atoi(argv[1]);
  260. szCertificateFile=argv[2];
  261. szKeyFile=argv[3];
  262. SSL_library_init();
  263. OpenSSL_add_ssl_algorithms();
  264. SSL_load_error_strings();
  265. ERR_load_crypto_strings();
  266. nFD=OpenSocket(nPort);
  267. pMachine=SSLStateMachine_new(szCertificateFile,szKeyFile);
  268. for( ; ; )
  269. {
  270. fd_set rfds,wfds;
  271. unsigned char buf[1024];
  272. int n;
  273. FD_ZERO(&rfds);
  274. FD_ZERO(&wfds);
  275. /* Select socket for input */
  276. FD_SET(nFD,&rfds);
  277. /* check whether there's decrypted data */
  278. if(!nrbuf)
  279. nrbuf=SSLStateMachine_read_extract(pMachine,rbuf,1);
  280. /* if there's decrypted data, check whether we can write it */
  281. if(nrbuf)
  282. FD_SET(1,&wfds);
  283. /* Select socket for output */
  284. if(SSLStateMachine_write_can_extract(pMachine))
  285. FD_SET(nFD,&wfds);
  286. /* Select stdin for input */
  287. FD_SET(0,&rfds);
  288. /* Wait for something to do something */
  289. n=select(nFD+1,&rfds,&wfds,NULL,NULL);
  290. assert(n > 0);
  291. /* Socket is ready for input */
  292. if(FD_ISSET(nFD,&rfds))
  293. {
  294. n=read(nFD,buf,sizeof buf);
  295. if(n == 0)
  296. {
  297. fprintf(stderr,"Got EOF on socket\n");
  298. exit(0);
  299. }
  300. assert(n > 0);
  301. SSLStateMachine_read_inject(pMachine,buf,n);
  302. }
  303. /* stdout is ready for output (and hence we have some to send it) */
  304. if(FD_ISSET(1,&wfds))
  305. {
  306. assert(nrbuf == 1);
  307. buf[0]=rbuf[0];
  308. nrbuf=0;
  309. n=SSLStateMachine_read_extract(pMachine,buf+1,sizeof buf-1);
  310. if(n < 0)
  311. {
  312. SSLStateMachine_print_error(pMachine,"read extract failed");
  313. break;
  314. }
  315. assert(n >= 0);
  316. ++n;
  317. if(n > 0) /* FIXME: has to be true now */
  318. {
  319. int w;
  320. w=write(1,buf,n);
  321. /* FIXME: we should push back any unwritten data */
  322. assert(w == n);
  323. }
  324. }
  325. /* Socket is ready for output (and therefore we have output to send) */
  326. if(FD_ISSET(nFD,&wfds))
  327. {
  328. int w;
  329. n=SSLStateMachine_write_extract(pMachine,buf,sizeof buf);
  330. assert(n > 0);
  331. w=write(nFD,buf,n);
  332. /* FIXME: we should push back any unwritten data */
  333. assert(w == n);
  334. }
  335. /* Stdin is ready for input */
  336. if(FD_ISSET(0,&rfds))
  337. {
  338. n=read(0,buf,sizeof buf);
  339. if(n == 0)
  340. {
  341. fprintf(stderr,"Got EOF on stdin\n");
  342. exit(0);
  343. }
  344. assert(n > 0);
  345. SSLStateMachine_write_inject(pMachine,buf,n);
  346. }
  347. }
  348. /* not reached */
  349. return 0;
  350. }