PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/DE2_115_WEB_SERVER_RGMII_ENET0/Software/web_bsp/iniche/src/misclib/cu_srv.c

https://gitlab.com/rainbowguardians/RainbowHard
C | 613 lines | 422 code | 88 blank | 103 comment | 67 complexity | 7d1d14227ab6ffec6409601ca8c94d14 MD5 | raw file
  1. /*
  2. * FILENAME: cu_srv.c
  3. *
  4. * Copyright 2004 By InterNiche Technologies Inc. All rights reserved
  5. *
  6. * Test program for TCP sockets API. Implements Crypto Engine server
  7. *
  8. * MODULE: MISCLIB
  9. *
  10. * ROUTINES: tcp_scipher_init(), tcp_scipher_close(), tcp_cipher_cleanup(),
  11. * ROUTINES: tcp_cipher_init(), tcp_cipher_recv(), tcp_cipher_poll(),
  12. * ROUTINES: tcp_cipher_stats()
  13. *
  14. * PORTABLE: yes
  15. */
  16. #include <string.h>
  17. #include "ipport.h" /* NetPort embedded system includes */
  18. #ifdef TCP_CIPHERTEST /* whole file can be ifdeffed out */
  19. #include "ce.h"
  20. #include "cu_srv.h"
  21. #ifndef WINSOCK
  22. #include "tcpport.h" /* NetPort embedded system includes */
  23. #include "in_utils.h"
  24. #ifndef sock_noblock
  25. #define sock_noblock(sock,flag) setsockopt(sock,SOL_SOCKET,SO_NBIO,NULL,0)
  26. #endif
  27. #endif
  28. /* This global switch can be set prior to calling tcp_cipher_init() */
  29. int tcpcipher_server = TRUE;
  30. static int in_cipherpoll = 0; /* re-entry flag */
  31. static SOCKTYPE elisten_sock = INVALID_SOCKET; /* Crypto Engine server socket -
  32. listening socket descriptor */
  33. static SOCKTYPE esvr_sock = INVALID_SOCKET; /* Crypto Engine server active socket -
  34. connected socket descriptor */
  35. /* Do we need to close the server's active socket? */
  36. static int esvr_sock_close = FALSE;
  37. #ifdef NPDEBUG
  38. static void printBufferInHex(void *pio, unsigned char *buf, int len)
  39. {
  40. ns_printf(pio, "tcp_cipher::printBufferInHex Input buffer = ");
  41. if (buf == NULL) {
  42. ns_printf(pio, "NULL \n");
  43. return;
  44. }
  45. while (len)
  46. {
  47. printf("%02x", *buf);
  48. if (--len>0)
  49. buf++;
  50. }
  51. printf("\n");
  52. return;
  53. }
  54. static void printBuffer(void *pio, unsigned char *buf, int len)
  55. {
  56. ns_printf(pio, "tcp_cipher::printBuffer output buffer = ");
  57. if (buf == NULL) {
  58. ns_printf(pio, "NULL \n");
  59. return;
  60. }
  61. while (len)
  62. {
  63. ns_printf(pio, "%c", *buf);
  64. if (--len>0)
  65. buf++;
  66. }
  67. ns_printf(pio, "\n");
  68. return;
  69. }
  70. #endif
  71. /*static void copyStr(unsigned char *key, unsigned char c, int len)
  72. {
  73. int i;
  74. for (i=0; i<len; i++)
  75. {
  76. *key=c;
  77. key++;
  78. }
  79. }*/
  80. #ifdef NPDEBUG
  81. static void binary_op(void *pio, char byte)
  82. {
  83. int count=8; /* Number of bits in a byte. */
  84. while(count--)
  85. {
  86. /* AND the high order bit (the
  87. * left one) If the bit is set,
  88. * print a ONE. */
  89. ns_printf(pio, "%d", ( byte & 128 ) ? 1 : 0 );
  90. /* Move all the bits LEFT. */
  91. byte <<= 1;
  92. }
  93. ns_printf(pio, "\n");
  94. }
  95. #endif
  96. static void cb(void *pio, void *conn, unsigned char *buf, int len, int status, int retCode)
  97. {
  98. USE_ARG(retCode);
  99. USE_VOID(conn);
  100. USE_VOID(pio);
  101. done=1;
  102. if (status == CE_DONE) {
  103. cipherOutBuf=buf;
  104. cipherOutBufLen=len;
  105. }
  106. return;
  107. }
  108. void tcpCipher_getCipherId(ServerInBuffer *srvInBufPtr)
  109. {
  110. short cipherId;
  111. int e; /* error holder */
  112. cipherId=getCipherId(srvInBufPtr->encryptionAsymetricAlg,
  113. srvInBufPtr->encryptionSymetricAlg,
  114. srvInBufPtr->authenticationAlg);
  115. srvOutBuf.cipherId=htons(cipherId);
  116. e = send(esvr_sock, (char *)&srvOutBuf, sizeof(srvOutBuf), 0);
  117. if (e < 0)
  118. {
  119. /* Print the error to console */
  120. e = t_errno(esvr_sock);
  121. ns_printf(NULL,
  122. "TCP Crypto Engine server::tcpCipher_getCipherId, error %d sending reply\n", e);
  123. dtrap();
  124. }
  125. return;
  126. }
  127. void tcpCipher_freeCipher(ServerInBuffer *srvInBufPtr)
  128. {
  129. freeCipher(srvInBufPtr->cipherId);
  130. return;
  131. }
  132. void processInputBuffer(ServerInBuffer *srvInBufPtr)
  133. {
  134. unsigned char flags;
  135. int ret_code;
  136. unsigned short cipherId;
  137. unsigned short bufLen;
  138. unsigned char *buffer=NULL;
  139. unsigned char priv_key_len=16;
  140. unsigned char iv_len=16;
  141. unsigned char priv_key[16]=
  142. {0x6c,0x3e,0xa0,0x47,0x76,0x30,0xce,0x21,
  143. 0xa2,0xce,0x33,0x4a,0xa7,0x46,0xc2,0xcd};
  144. unsigned char iv[16]=
  145. {0xc7,0x82,0xdc,0x4c,0x09,0x8c,0x66,0xcb,
  146. 0xd9,0xcd,0x27,0xd8,0x25,0x68,0x2c,0x81};
  147. struct securityParameters secParm;
  148. struct inputParameters inpParm;
  149. secParm.auth_key=NULL;
  150. secParm.auth_key_len=0;
  151. secParm.priv_key=priv_key;
  152. secParm.priv_key_len=priv_key_len;
  153. secParm.iv=iv;
  154. secParm.iv_len=iv_len;
  155. secParm.cipher_len=NULL;
  156. if (srvInBufPtr->encryptionSymetricAlg>0)
  157. {
  158. ns_printf(NULL, "tcp_cipher::processInputBuffer encryptionSymetricAlg = %d \n",
  159. srvInBufPtr->encryptionSymetricAlg);
  160. tcpCipher_getCipherId(srvInBufPtr);
  161. return;
  162. }
  163. if (srvInBufPtr->action==DELETE_CIPHER)
  164. {
  165. ns_printf(NULL, "tcp_cipher::processInputBuffer encryptionSymetricAlg = %d \n",
  166. srvInBufPtr->encryptionSymetricAlg);
  167. tcpCipher_freeCipher(srvInBufPtr);
  168. return;
  169. }
  170. bufLen = ntohs(srvInBufPtr->bufLen);
  171. flags=srvInBufPtr->flags;
  172. #ifdef NPDEBUG
  173. binary_op(NULL, flags);
  174. #endif
  175. cipherId=ntohs(srvInBufPtr->cipherId);
  176. #ifdef NPDEBUG
  177. ns_printf(NULL, "cipher id = %x \n", cipherId);
  178. ns_printf(NULL, "flags = %x \n", flags);
  179. printBufferInHex(NULL, srvInBufPtr->buffer, bufLen);
  180. /*ns_printf(NULL, "buffer = %s \n", srvInBufPtr->buffer);*/
  181. ns_printf(NULL, "buffer length = %d \n", bufLen);
  182. #endif
  183. if (srvInBufPtr->bufLen>0)
  184. {
  185. buffer = (unsigned char *)npalloc(bufLen);
  186. MEMCPY(buffer, srvInBufPtr->buffer, bufLen);
  187. }
  188. #ifdef NPDEBUG
  189. ns_printf(NULL, "tcp_cipher::processInputBuffer buffer = ");
  190. printBufferInHex(NULL, srvInBufPtr->buffer, bufLen);
  191. #endif
  192. /*auth_key=(unsigned char *)npalloc(key_len);
  193. copyStr(auth_key, 0xaa, auth_key_len);*/
  194. inpParm.cid=cipherId;
  195. inpParm.flags=flags;
  196. inpParm.pConn=NULL;
  197. inpParm.inBuf=buffer;
  198. inpParm.inBufLen=bufLen;
  199. inpParm.outBuf=NULL;
  200. inpParm.cb=cb;
  201. ret_code=ce_crypt(NULL,
  202. &inpParm,
  203. &secParm);
  204. if (ret_code == SUCCESS)
  205. ce_do();
  206. #ifdef NPDEBUG
  207. ns_printf(NULL, "tcp_cipher::processInputBuffer done = %d \n", done);
  208. ns_printf(NULL, "tcp_cipher::processInputBuffer output buffer = \n");
  209. printBuffer(NULL, cipherOutBuf, cipherOutBufLen);
  210. #endif
  211. npfree(buffer);
  212. buffer=NULL;
  213. }
  214. void
  215. tcp_cipher_recv(void)
  216. {
  217. int len; /* length of recv data */
  218. int e; /* error holder */
  219. unsigned i; /* generic index */
  220. int count; /* select return */
  221. fd_set fd_recv; /* fd for recv */
  222. fd_set fd_accept; /* fd for accept */
  223. struct sockaddr_in client;
  224. SOCKTYPE tmpsock; /* scratch socket */
  225. ServerInBuffer srvInBuf;
  226. if (elisten_sock == INVALID_SOCKET)
  227. return; /* Crypto Engine Server not set up, don't bother */
  228. #ifdef USE_FDS
  229. /* initialize FD arrays */
  230. FD_ZERO(&fd_recv);
  231. FD_ZERO(&fd_accept);
  232. #endif
  233. i = 0;
  234. count = 0;
  235. if (tcpcipher_server)
  236. {
  237. if (esvr_sock != INVALID_SOCKET)
  238. {
  239. #ifdef USE_FDS
  240. FD_SET(esvr_sock, &fd_recv);
  241. ++i;
  242. #else
  243. fd_recv.fd_array[i++] = esvr_sock;
  244. #endif
  245. ns_printf(NULL, "added esvr_sock to fd_recv \n");
  246. }
  247. }
  248. #ifndef USE_FDS
  249. fd_recv.fd_count = i;
  250. #endif
  251. /* make this a short timeout since elisten may be created soon */
  252. if (elisten_sock != INVALID_SOCKET)
  253. {
  254. #ifdef USE_FDS
  255. FD_SET(elisten_sock, &fd_accept);
  256. #else
  257. fd_accept.fd_array[0] = elisten_sock;
  258. fd_accept.fd_count = 1;
  259. #endif
  260. count = t_select(&fd_recv, (fd_set *)NULL, &fd_accept, 1);
  261. }
  262. else
  263. {
  264. if (i) /* if no fd_set sockets filled in, don't bother */
  265. count = t_select(&fd_recv, (fd_set *)NULL, (fd_set *)NULL, 1);
  266. }
  267. /* While the t_select() was executing, commands can be
  268. * executed from cmd-prompt and sockets can be cleaned up.
  269. * Check for that.
  270. */
  271. if (elisten_sock == INVALID_SOCKET)
  272. return; /* Crypto Engine Server not set up, don't bother */
  273. for (i = 0; i < fd_recv.fd_count; i++)
  274. {
  275. #ifdef USE_FDS
  276. tmpsock = FD_GET(i, &fd_recv);
  277. if (tmpsock == INVALID_SOCKET)
  278. continue;
  279. #else
  280. tmpsock = fd_recv.fd_array[i];
  281. #endif
  282. MEMSET((char *)&srvInBuf, 0, sizeof(srvInBuf));
  283. len = recv(tmpsock, (char *)&srvInBuf, sizeof(srvInBuf), 0);
  284. if (len > 0)
  285. {
  286. processInputBuffer(&srvInBuf);
  287. }
  288. if (len < 0)
  289. {
  290. e = t_errno(tmpsock);
  291. if (e != EWOULDBLOCK)
  292. {
  293. ns_printf(NULL,"TCP Crypto Engine Server recv error %d\n", e);
  294. }
  295. }
  296. else if (len == 0)
  297. {
  298. ns_printf(NULL,"TCPCipher:socket closed by other side\n");
  299. socketclose(esvr_sock);
  300. esvr_sock = INVALID_SOCKET;
  301. }
  302. }
  303. if (done)
  304. {
  305. #ifdef NPDEBUG
  306. printBuffer(NULL, cipherOutBuf, cipherOutBufLen);
  307. #endif
  308. e = send(esvr_sock, (char *)cipherOutBuf, cipherOutBufLen, 0);
  309. if (e < 0)
  310. {
  311. /* Print the error to console */
  312. e = t_errno(esvr_sock);
  313. ns_printf(NULL,
  314. "TCP Crypto Engine Server server, error %d sending reply\n", e);
  315. }
  316. npfree(cipherOutBuf);
  317. cipherOutBuf=NULL;
  318. done=0;
  319. }
  320. /* if no server listen to poll, return now */
  321. if (elisten_sock == INVALID_SOCKET)
  322. return;
  323. #ifdef NOTDEF
  324. MEMSET(&client, 0, sizeof(client));
  325. client.sin_family = AF_INET;
  326. client.sin_addr.s_addr = INADDR_ANY;
  327. client.sin_port = htons(CIPHER_PORT);
  328. #endif
  329. /* check for received client connection on server */
  330. len = sizeof(client);
  331. tmpsock = accept(elisten_sock, (struct sockaddr*)&client, &len );
  332. if (tmpsock != INVALID_SOCKET)
  333. {
  334. if (esvr_sock == INVALID_SOCKET)
  335. {
  336. esvr_sock = tmpsock;
  337. }
  338. else /* we already have a connection */
  339. {
  340. dprintf("tcpCipher: rejected extra connection\n");
  341. socketclose(tmpsock); /* refuse to serve another */
  342. }
  343. }
  344. }
  345. /* FUNCTION: tcp_cipher_poll()
  346. *
  347. * Do routine processing for Crypto Engine Server
  348. * 1. Check if client packets have been received for TCP Crypto Engine
  349. * Server. If yes, send a reply.
  350. * 2. Check if Crypto Engine replys have been recieved for TCP Crypto Engine Clients.
  351. * If yes, update data structures, display messages.
  352. *
  353. * PARAM1: void
  354. *
  355. * RETURNS:
  356. */
  357. void
  358. tcp_cipher_poll(void)
  359. {
  360. if (elisten_sock == INVALID_SOCKET)
  361. return; /* Crypto Engine Server not set up, don't bother */
  362. in_cipherpoll++; /* don't re-enter from tk_yield() */
  363. if (in_cipherpoll != 1)
  364. {
  365. in_cipherpoll--;
  366. return;
  367. }
  368. tcp_cipher_recv();
  369. in_cipherpoll--;
  370. }
  371. /* FUNCTION: tcp_scipher_init()
  372. *
  373. * Initialize the TCP Crypto Engine Server. If the flag tcpcipher_server is false,
  374. * then Server Init is not done.
  375. *
  376. * PARAM1: void * pio - device for console output
  377. *
  378. * RETURNS: 0 on SUCCESS or error number.
  379. */
  380. int
  381. tcp_scipher_init(void * pio)
  382. {
  383. int e; /* error holder */
  384. struct sockaddr_in me; /* my IP info, for bind() */
  385. int opt;
  386. if (tcpcipher_server)
  387. {
  388. ns_printf(pio,"tcp Crypto Engine srv - starting.\n");
  389. /* open TCP socket */
  390. elisten_sock = socket(AF_INET, SOCK_STREAM, 0);
  391. if (elisten_sock == INVALID_SOCKET)
  392. {
  393. ns_printf(pio,"TCP Crypto Engine: bad socket: %d\n", elisten_sock);
  394. return TCPE_BAD_SOCKET ;
  395. }
  396. opt = 1;
  397. e = t_setsockopt(elisten_sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
  398. if (e != 0)
  399. {
  400. e = t_errno(elisten_sock);
  401. dtrap();
  402. dprintf("error %d setting SO_REUSEADDR on port %d\n", e, CIPHER_PORT);
  403. return SYS_SOCKETNULL;
  404. }
  405. me.sin_family = AF_INET;
  406. me.sin_addr.s_addr = INADDR_ANY;
  407. me.sin_port = htons(CIPHER_PORT);
  408. e = bind(elisten_sock, (struct sockaddr*)&me, sizeof(me));
  409. if (e != 0)
  410. {
  411. e = t_errno(elisten_sock);
  412. ns_printf(pio,"tcp_Cipher: bad socket bind: %d, %s\n", e, so_perror(e) );
  413. socketclose(elisten_sock);
  414. elisten_sock = INVALID_SOCKET;
  415. return TCPE_BIND_FAILED ;
  416. }
  417. e = listen(elisten_sock, 3);
  418. if (e != 0)
  419. {
  420. e = t_errno(elisten_sock);
  421. ns_printf(pio,"tcp_Cipher: bad socket listen: %d %s\n", e, so_perror(e));
  422. socketclose(elisten_sock);
  423. elisten_sock = INVALID_SOCKET;
  424. return TCPE_LISTEN_FAILED;
  425. }
  426. /* for listen socket into Non-blocking mode so we can poll accept */
  427. sock_noblock(elisten_sock, TRUE);
  428. }
  429. else
  430. ns_printf(pio,"tcp Crypto Engine server not enabled\n");
  431. return SUCCESS;
  432. }
  433. /* FUNCTION: tcp_cipher_init()
  434. *
  435. * Initialize TCP Crypto Engine Server
  436. * Remarks : This function has been defined so that a clean interface
  437. * is provided for TCP Crypto Engine Server.
  438. *
  439. * PARAM1: void * pio - device for console output
  440. *
  441. * RETURNS: 0 on SUCCESS or error number.
  442. */
  443. int
  444. tcp_cipher_init(void)
  445. {
  446. return tcp_scipher_init(NULL);
  447. }
  448. /* FUNCTION: tcp_scipher_close()
  449. *
  450. * Close the TCP Crypto Engine Server.
  451. *
  452. * PARAM1: void * pio - device for console output
  453. *
  454. * RETURNS: 0 on SUCCESS or error number.
  455. */
  456. int
  457. tcp_scipher_close(void * pio)
  458. {
  459. int e = 0; /* scratch error holder */
  460. int retval = 0; /* return last non-zero error */
  461. if (esvr_sock != INVALID_SOCKET)
  462. {
  463. e = socketclose(esvr_sock);
  464. if (e)
  465. {
  466. retval = e = t_errno(esvr_sock);
  467. ns_printf(pio,"tcp Crypto Engine server: close error %d %s\n",
  468. e, so_perror(e));
  469. }
  470. else
  471. {
  472. ns_printf(pio,"tcp Crypto Engine srv - closing.\n");
  473. esvr_sock = INVALID_SOCKET;
  474. }
  475. }
  476. if (elisten_sock == INVALID_SOCKET)
  477. return e;
  478. e = socketclose(elisten_sock);
  479. if (e)
  480. {
  481. retval = e = t_errno(elisten_sock);
  482. ns_printf(pio,"tcp Crypto Engine: server close error %d %s\n",
  483. e, so_perror(e) );
  484. }
  485. elisten_sock = INVALID_SOCKET;
  486. return retval;
  487. }
  488. /* FUNCTION: tcp_cipher_cleanup()
  489. *
  490. * Cleanup all TCP Crypto Engine related data structures.
  491. *
  492. * PARAM1: void
  493. *
  494. * RETURNS:
  495. */
  496. void
  497. tcp_cipher_cleanup(void)
  498. {
  499. tcp_scipher_close(NULL);
  500. }
  501. /* FUNCTION: tcp_cipher_stats()
  502. *
  503. * Show statistics about all TCP Crypto Engine Clients and Servers
  504. *
  505. * PARAM1: void * pio - device for console output
  506. *
  507. * RETURNS: 0 on SUCCESS or error number.
  508. */
  509. int
  510. tcp_cipher_stats(void * pio)
  511. {
  512. ns_printf(
  513. pio,"Showing TCP Crypto Engine statistics.\n");
  514. if ( esvr_sock == INVALID_SOCKET )
  515. {
  516. ns_printf(pio," There are no Server connections.\n");
  517. }
  518. else
  519. {
  520. ns_printf(pio," There is one Server connection.\n");
  521. }
  522. return SUCCESS;
  523. }
  524. #endif /* TCP_CIPHERTEST */