ds.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. #include <malloc.h>
  2. #include <unistd.h>
  3. #include <string.h>
  4. //#include <sys/select.h>
  5. #include <sys/types.h>
  6. #include <sys/socket.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>
  9. #include <netinet/in.h>
  10. #include <errno.h>
  11. #include <openssl/bio.h>
  12. #include <openssl/ssl.h>
  13. #include <openssl/err.h>
  14. #include <pthread.h>
  15. #include "ds.h"
  16. int openSslLoaded = 0;
  17. char *availableCiphers = "EDH+CAMELLIA:EDH+aRSA:EECDH+aRSA+AESGCM:EECDH+aRSA+SHA256:EECDH:"
  18. "+CAMELLIA128:+AES128:+SSLv3:!aNULL:!eNULL:!LOW:!3DES:!MD5:!EXP:!PSK:!DSS:!RC4:!SEED:"
  19. "!IDEA:!ECDSA:kEDH:CAMELLIA128-SHA:AES128-SHA";
  20. void *clear(void *ptr){
  21. int e = errno;
  22. if(ptr){
  23. free(ptr);
  24. }
  25. errno = e;
  26. return NULL;
  27. }
  28. pthread_mutex_t loadLock;
  29. void loadOpenSSL(const char *dh){
  30. if(openSslLoaded)
  31. return;
  32. pthread_mutex_lock(&loadLock);
  33. if(!openSslLoaded){
  34. SSL_load_error_strings();
  35. ERR_load_BIO_strings();
  36. ERR_load_crypto_strings();
  37. SSL_library_init();
  38. OpenSSL_add_all_algorithms();
  39. openSslLoaded = 1;
  40. }
  41. pthread_mutex_unlock(&loadLock);
  42. }
  43. void copy6addr(unsigned char d[16], const unsigned char s[16]){
  44. int i;
  45. for(i = 0; i < 16; i++)
  46. d[i] = s[i];
  47. }
  48. void zero6addr(unsigned char d[16]){
  49. int i;
  50. for(i = 0; i < 16; i++)
  51. d[i] = 0;
  52. }
  53. nethandler getNethandler(const int ipv6, const int port){
  54. nethandler h = (nethandler)malloc(sizeof(s_nethandler));
  55. h->ipv6 = ipv6;
  56. if(ipv6){
  57. h->fd = socket(AF_INET6, SOCK_STREAM, 0);
  58. }else{
  59. h->fd = socket(AF_INET, SOCK_STREAM, 0);
  60. }
  61. int optval = 1;
  62. setsockopt(h->fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
  63. int e, en;
  64. if(ipv6){
  65. struct sockaddr_in6 add;
  66. add.sin6_family = AF_INET6;
  67. zero6addr(add.sin6_addr.s6_addr);
  68. add.sin6_port = htons(port);
  69. e = bind(h->fd, (struct sockaddr*) &add, sizeof(add));
  70. }else{
  71. struct sockaddr_in add;
  72. add.sin_family = AF_INET;
  73. add.sin_addr.s_addr = INADDR_ANY;
  74. add.sin_port = htons(port);
  75. e = bind(h->fd, (struct sockaddr*) &add, sizeof(add));
  76. }
  77. if(e)
  78. return clear(h);
  79. e = listen(h->fd, DEFAULT_LISTENNING_QUEUE);
  80. if(e)
  81. return clear(h);
  82. return h;
  83. }
  84. nethandler getIPv4Port(const int port){
  85. return getNethandler(0, port);
  86. }
  87. nethandler getPort(const int port){
  88. return getNethandler(1, port);
  89. }
  90. ds createFromFile(int f){
  91. ds d = (ds)malloc(sizeof(s_ds));
  92. d->tp = file;
  93. d->fd = f;
  94. return d;
  95. }
  96. ds createFromFileName(const char *f){
  97. int fd = open(f, O_CREAT | O_RDWR, 0666);
  98. if(fd == -1){
  99. return NULL;
  100. }
  101. return createFromFile(fd);
  102. }
  103. ds createFromHandler(nethandler h){
  104. ds d = (ds)malloc(sizeof(s_ds));
  105. d->tp = sock;
  106. unsigned int s = sizeof(d->peer);
  107. d->fd = accept(h->fd, (struct sockaddr*)&(d->peer), &s);
  108. if(d->fd <= 0)
  109. return clear(d);
  110. d->ipv6 = d->peer.ss_family == AF_INET6;
  111. d->server = 1;
  112. return d;
  113. }
  114. ds createToHost(struct sockaddr *add, const int add_size, const int ipv6){
  115. ds d = (ds)malloc(sizeof(s_ds));
  116. d->tp = sock;
  117. if(ipv6){
  118. d->fd = socket(AF_INET6, SOCK_STREAM, 0);
  119. }else{
  120. d->fd = socket(AF_INET, SOCK_STREAM, 0);
  121. }
  122. if(connect(d->fd, add, add_size) < 0){
  123. int e = errno;
  124. free(d);
  125. errno = e;
  126. return NULL;
  127. }
  128. d->server = 0;
  129. return d;
  130. }
  131. ds createToIPv4Host(const unsigned long host, const int port){
  132. struct sockaddr_in add;
  133. add.sin_family = AF_INET;
  134. add.sin_port = htons(port);
  135. add.sin_addr.s_addr = host;
  136. return createToHost((struct sockaddr*) &add, sizeof(add), 0);
  137. }
  138. ds createToIPv6Host(const unsigned char host[16], const int port){
  139. struct sockaddr_in6 add;
  140. add.sin6_family = AF_INET6;
  141. add.sin6_port = htons(port);
  142. add.sin6_flowinfo = 0;
  143. copy6addr(add.sin6_addr.s6_addr, host);
  144. add.sin6_scope_id = 0;
  145. return createToHost((struct sockaddr*) &add, sizeof(add), 1);
  146. }
  147. int getPeer(ds d, unsigned long *ipv4peer, unsigned char ipv6peer[16], int *ipv6){
  148. int port = 0;
  149. struct sockaddr_storage peer;
  150. int peer_size = sizeof(peer);
  151. if(getpeername(d->fd, (struct sockaddr*)&peer, &peer_size)){
  152. return 0;
  153. }
  154. if(peer.ss_family == AF_INET){
  155. struct sockaddr_in *a = (struct sockaddr_in*)&(peer);
  156. zero6addr(ipv6peer);
  157. *ipv6 = -1;
  158. *ipv4peer = a->sin_addr.s_addr;
  159. port = a->sin_port;
  160. }else{
  161. struct sockaddr_in6 *a = (struct sockaddr_in6*)&(peer);
  162. *ipv4peer = 0;
  163. *ipv6 = 1;
  164. copy6addr(ipv6peer, a->sin6_addr.s6_addr);
  165. port = a->sin6_port;
  166. }
  167. return port;
  168. }
  169. int sendDs(ds d, const char *b, const int s){
  170. return write(d->fd, b, s);
  171. }
  172. int tlsDsSend(tlsDs d, const char *b, const int s){
  173. return SSL_write(d->s, b, s);
  174. }
  175. int stdDsSend(const char *b, const int s){
  176. return write(1, b, s);
  177. }
  178. int recvDs(ds d, char *b, const int s){
  179. return read(d->fd, b, s);
  180. }
  181. int tlsDsRecv(tlsDs d, char *b, const int s){
  182. return SSL_read(d->s, b, s);
  183. }
  184. int stdDsRecv(char *b, const int s){
  185. return read(0, b, s);
  186. }
  187. int prepareToClose(ds d){
  188. int fd = d->fd;
  189. free(d);
  190. return fd;
  191. }
  192. ds closeTls(tlsDs d){
  193. ds original = d->original;
  194. SSL_shutdown(d->s);
  195. //No bidirectional shutdown supported
  196. //SSL_shutdown(d->s);
  197. SSL_free(d->s);
  198. free(d);
  199. return original;
  200. }
  201. void closeHandler(nethandler h){
  202. close(h->fd);
  203. free(h);
  204. }
  205. tlsDs startSockTls(ds d, const char *cert, const char *key, const char *dh){
  206. loadOpenSSL(dh);
  207. SSL_CTX * ctx = NULL;
  208. if(d->server)
  209. ctx = SSL_CTX_new(TLSv1_1_server_method());
  210. else
  211. ctx = SSL_CTX_new(TLSv1_1_client_method());
  212. if(!ctx)
  213. return NULL;
  214. if(d->server){
  215. FILE *dhfile = fopen(dh, "r");
  216. DH *dhdt = PEM_read_DHparams(dhfile, NULL, NULL, NULL);
  217. fclose(dhfile);
  218. if(SSL_CTX_set_tmp_dh(ctx, dhdt) <= 0){
  219. int f = prepareToClose(d);
  220. closeFd(f);
  221. clear(dhdt);
  222. return clear(ctx);
  223. }
  224. }
  225. SSL_CTX_set_options(ctx, SSL_OP_SINGLE_DH_USE);
  226. if(cert)
  227. if(SSL_CTX_use_certificate_chain_file(ctx, cert) != 1){
  228. int f = prepareToClose(d);
  229. closeFd(f);
  230. return clear(ctx);
  231. }
  232. if(key)
  233. if(SSL_CTX_use_PrivateKey_file(ctx, key, SSL_FILETYPE_PEM) != 1){
  234. int f = prepareToClose(d);
  235. closeFd(f);
  236. return clear(ctx);
  237. }
  238. if(SSL_CTX_set_cipher_list(ctx, availableCiphers) <= 0){
  239. int f = prepareToClose(d);
  240. closeFd(f);
  241. return clear(ctx);
  242. }
  243. tlsDs t = (tlsDs)malloc(sizeof(s_tlsDs));
  244. t->original = d;
  245. if(!(t->s = SSL_new(ctx))){
  246. int f = prepareToClose(d);
  247. closeFd(f);
  248. clear(ctx);
  249. return clear(t);
  250. }
  251. if(!SSL_set_fd(t->s, d->fd)){
  252. closeTls(t);
  253. return NULL;
  254. }
  255. int retry = 1;
  256. int e;
  257. while(retry){
  258. retry = 0;
  259. if(d->server){
  260. SSL_set_accept_state(t->s);
  261. e = SSL_accept(t->s);
  262. }else{
  263. SSL_set_connect_state(t->s);
  264. e = SSL_connect(t->s);
  265. }
  266. if(e <= 0){
  267. unsigned long erval = SSL_get_error(t->s, e);
  268. //char ertxt[300];
  269. //ERR_error_string(erval, ertxt);
  270. //fprintf(stderr, "SSL Error: %s\n", ertxt);
  271. if((erval == SSL_ERROR_WANT_READ) || (erval == SSL_ERROR_WANT_WRITE)){
  272. //Here goes support to non-blocking IO, once it's supported
  273. //retry = 1;
  274. }else{
  275. closeTls(t);
  276. return NULL;
  277. }
  278. }
  279. }
  280. return t;
  281. }
  282. int getFd(ds d){
  283. return d->fd;
  284. }
  285. int getTlsFd(tlsDs t){
  286. ds d = t->original;
  287. return d->fd;
  288. }
  289. void closeFd(int fd){
  290. close(fd);
  291. }