nss-sock.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /*
  2. * Copyright (c) 2015-2016 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Jan Friesse (jfriesse@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the Red Hat, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <limits.h>
  35. #include "nss-sock.h"
  36. int
  37. nss_sock_init_nss(char *config_dir)
  38. {
  39. if (config_dir == NULL) {
  40. if (NSS_NoDB_Init(NULL) != SECSuccess) {
  41. return (-1);
  42. }
  43. } else {
  44. if (NSS_Init(config_dir) != SECSuccess) {
  45. return (-1);
  46. }
  47. }
  48. if (NSS_SetDomesticPolicy() != SECSuccess) {
  49. return (-1);
  50. }
  51. return (0);
  52. }
  53. /*
  54. * Set NSS socket non-blocking
  55. */
  56. int
  57. nss_sock_set_non_blocking(PRFileDesc *sock)
  58. {
  59. PRSocketOptionData sock_opt;
  60. memset(&sock_opt, 0, sizeof(sock_opt));
  61. sock_opt.option = PR_SockOpt_Nonblocking;
  62. sock_opt.value.non_blocking = PR_TRUE;
  63. if (PR_SetSocketOption(sock, &sock_opt) != PR_SUCCESS) {
  64. return (-1);
  65. }
  66. return (0);
  67. }
  68. /*
  69. * Create TCP socket with af family. If reuse_addr is set, socket option
  70. * for reuse address is set.
  71. */
  72. static PRFileDesc *
  73. nss_sock_create_socket(PRIntn af, int reuse_addr)
  74. {
  75. PRFileDesc *sock;
  76. PRSocketOptionData socket_option;
  77. sock = PR_OpenTCPSocket(af);
  78. if (sock == NULL) {
  79. return (NULL);
  80. }
  81. if (reuse_addr) {
  82. socket_option.option = PR_SockOpt_Reuseaddr;
  83. socket_option.value.reuse_addr = PR_TRUE;
  84. if (PR_SetSocketOption(sock, &socket_option) != PR_SUCCESS) {
  85. return (NULL);
  86. }
  87. }
  88. return (sock);
  89. }
  90. /*
  91. * Create listen socket and bind it to address. hostname can be NULL and then
  92. * any address is used. Address family (af) can be ether PR_AF_INET6,
  93. * PR_AF_INET or PR_AF_UNSPEC.
  94. */
  95. PRFileDesc *
  96. nss_sock_create_listen_socket(const char *hostname, uint16_t port, PRIntn af)
  97. {
  98. PRNetAddr addr;
  99. PRFileDesc *sock;
  100. PRAddrInfo *addr_info;
  101. void *addr_iter;
  102. sock = NULL;
  103. if (hostname == NULL) {
  104. memset(&addr, 0, sizeof(addr));
  105. if (PR_InitializeNetAddr(PR_IpAddrAny, port, &addr) != PR_SUCCESS) {
  106. return (NULL);
  107. }
  108. if (af == PR_AF_UNSPEC) {
  109. af = PR_AF_INET6;
  110. }
  111. addr.raw.family = af;
  112. sock = nss_sock_create_socket(af, 1);
  113. if (sock == NULL) {
  114. return (NULL);
  115. }
  116. if (PR_Bind(sock, &addr) != PR_SUCCESS) {
  117. PR_Close(sock);
  118. return (NULL);
  119. }
  120. } else {
  121. addr_info = PR_GetAddrInfoByName(hostname, af, PR_AI_ADDRCONFIG);
  122. if (addr_info == NULL) {
  123. return (NULL);
  124. }
  125. addr_iter = NULL;
  126. while ((addr_iter = PR_EnumerateAddrInfo(addr_iter, addr_info, port,
  127. &addr)) != NULL) {
  128. if (af == PR_AF_UNSPEC || addr.raw.family == af) {
  129. sock = nss_sock_create_socket(addr.raw.family, 1);
  130. if (sock == NULL) {
  131. continue;
  132. }
  133. if (PR_Bind(sock, &addr) != PR_SUCCESS) {
  134. PR_Close(sock);
  135. sock = NULL;
  136. continue;
  137. }
  138. /*
  139. * Socket is sucesfully bound
  140. */
  141. break;
  142. }
  143. }
  144. PR_FreeAddrInfo(addr_info);
  145. if (sock == NULL) {
  146. /*
  147. * No address succeeded
  148. */
  149. PR_SetError(PR_ADDRESS_NOT_AVAILABLE_ERROR, 0);
  150. return (NULL);
  151. }
  152. }
  153. return (sock);
  154. }
  155. PRFileDesc *
  156. nss_sock_create_client_socket(const char *hostname, uint16_t port, PRIntn af,
  157. PRIntervalTime timeout)
  158. {
  159. PRNetAddr addr;
  160. PRFileDesc *sock;
  161. PRAddrInfo *addr_info;
  162. void *addr_iter;
  163. PRStatus res;
  164. int connect_failed;
  165. PRIntn tmp_af;
  166. sock = NULL;
  167. connect_failed = 0;
  168. tmp_af = af;
  169. if (af == PR_AF_INET6) {
  170. tmp_af = PR_AF_UNSPEC;
  171. }
  172. addr_info = PR_GetAddrInfoByName(hostname, tmp_af, PR_AI_ADDRCONFIG);
  173. if (addr_info == NULL) {
  174. return (NULL);
  175. }
  176. addr_iter = NULL;
  177. while ((addr_iter = PR_EnumerateAddrInfo(addr_iter, addr_info, port, &addr)) != NULL) {
  178. if (af != PR_AF_UNSPEC && addr.raw.family != af) {
  179. continue;
  180. }
  181. sock = nss_sock_create_socket(addr.raw.family, 0);
  182. if (sock == NULL) {
  183. continue;
  184. }
  185. if ((res = PR_Connect(sock, &addr, timeout)) != PR_SUCCESS) {
  186. PR_Close(sock);
  187. sock = NULL;
  188. connect_failed = 1;
  189. }
  190. /*
  191. * Connection attempt finished
  192. */
  193. break;
  194. }
  195. PR_FreeAddrInfo(addr_info);
  196. if (sock == NULL && !connect_failed) {
  197. PR_SetError(PR_ADDRESS_NOT_AVAILABLE_ERROR, 0);
  198. }
  199. return (sock);
  200. }
  201. int
  202. nss_sock_non_blocking_client_init(const char *host_name, uint16_t port, PRIntn af,
  203. struct nss_sock_non_blocking_client *client)
  204. {
  205. PRIntn tmp_af;
  206. client->destroyed = 1;
  207. if ((client->host_name = strdup(host_name)) == NULL) {
  208. PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
  209. return (-1);
  210. }
  211. client->port = port;
  212. client->af = af;
  213. tmp_af = af;
  214. if (af == PR_AF_INET6) {
  215. tmp_af = PR_AF_UNSPEC;
  216. }
  217. client->addr_info = PR_GetAddrInfoByName(client->host_name, tmp_af, PR_AI_ADDRCONFIG);
  218. if (client->addr_info == NULL) {
  219. free(client->host_name);
  220. return (-1);
  221. }
  222. client->addr_iter = NULL;
  223. client->connect_attempts = 0;
  224. client->socket = NULL;
  225. client->destroyed = 0;
  226. return (0);
  227. }
  228. int
  229. nss_sock_non_blocking_client_try_next(struct nss_sock_non_blocking_client *client)
  230. {
  231. PRNetAddr addr;
  232. PRStatus res;
  233. if (client->destroyed) {
  234. PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0);
  235. return (-1);
  236. }
  237. if (client->socket != NULL) {
  238. PR_Close(client->socket);
  239. client->socket = NULL;
  240. }
  241. while ((client->addr_iter = PR_EnumerateAddrInfo(client->addr_iter, client->addr_info,
  242. client->port, &addr)) != NULL) {
  243. if (client->af != PR_AF_UNSPEC && addr.raw.family != client->af) {
  244. continue;
  245. }
  246. client->socket = nss_sock_create_socket(addr.raw.family, 0);
  247. if (client->socket == NULL) {
  248. continue;
  249. }
  250. if (nss_sock_set_non_blocking(client->socket) == -1) {
  251. PR_Close(client->socket);
  252. client->socket = NULL;
  253. continue;
  254. }
  255. res = PR_Connect(client->socket, &addr, PR_INTERVAL_NO_TIMEOUT);
  256. if (res == PR_SUCCESS || PR_GetError() == PR_IN_PROGRESS_ERROR) {
  257. return (0);
  258. }
  259. PR_Close(client->socket);
  260. client->socket = NULL;
  261. if (client->connect_attempts < INT_MAX) {
  262. client->connect_attempts++;
  263. }
  264. }
  265. if (client->connect_attempts == 0) {
  266. PR_SetError(PR_ADDRESS_NOT_AVAILABLE_ERROR, 0);
  267. }
  268. return (-1);
  269. }
  270. void
  271. nss_sock_non_blocking_client_destroy(struct nss_sock_non_blocking_client *client)
  272. {
  273. if (client->destroyed) {
  274. return ;
  275. }
  276. if (client->addr_info != NULL) {
  277. PR_FreeAddrInfo(client->addr_info);
  278. client->addr_info = NULL;
  279. }
  280. free(client->host_name);
  281. client->host_name = NULL;
  282. client->destroyed = 1;
  283. }
  284. /*
  285. * -1 = Client connect failed
  286. * 0 = Client connect still in progress
  287. * 1 = Client successfuly connected
  288. */
  289. int
  290. nss_sock_non_blocking_client_succeeded(const PRPollDesc *pfd)
  291. {
  292. int res;
  293. res = -1;
  294. if (PR_GetConnectStatus(pfd) == PR_SUCCESS) {
  295. res = 1;
  296. } else {
  297. if (PR_GetError() == PR_IN_PROGRESS_ERROR) {
  298. res = 0;
  299. } else {
  300. res = -1;
  301. }
  302. }
  303. return (res);
  304. }
  305. /*
  306. * Start client side SSL connection. This can block.
  307. *
  308. * ssl_url is expected server URL, bad_cert_hook is callback called when server certificate
  309. * verification fails.
  310. */
  311. PRFileDesc *
  312. nss_sock_start_ssl_as_client(PRFileDesc *input_sock, const char *ssl_url,
  313. SSLBadCertHandler bad_cert_hook, SSLGetClientAuthData client_auth_hook,
  314. void *client_auth_hook_arg, int force_handshake, int *reset_would_block)
  315. {
  316. PRFileDesc *ssl_sock;
  317. if (force_handshake) {
  318. *reset_would_block = 0;
  319. }
  320. ssl_sock = SSL_ImportFD(NULL, input_sock);
  321. if (ssl_sock == NULL) {
  322. return (NULL);
  323. }
  324. if (SSL_SetURL(ssl_sock, ssl_url) != SECSuccess) {
  325. return (NULL);
  326. }
  327. if ((SSL_OptionSet(ssl_sock, SSL_SECURITY, PR_TRUE) != SECSuccess) ||
  328. (SSL_OptionSet(ssl_sock, SSL_HANDSHAKE_AS_SERVER, PR_FALSE) != SECSuccess) ||
  329. (SSL_OptionSet(ssl_sock, SSL_HANDSHAKE_AS_CLIENT, PR_TRUE) != SECSuccess)) {
  330. return (NULL);
  331. }
  332. if (bad_cert_hook != NULL && SSL_BadCertHook(ssl_sock, bad_cert_hook, NULL) != SECSuccess) {
  333. return (NULL);
  334. }
  335. if (client_auth_hook != NULL &&
  336. (SSL_GetClientAuthDataHook(ssl_sock, client_auth_hook,
  337. client_auth_hook_arg) != SECSuccess)) {
  338. return (NULL);
  339. }
  340. if (SSL_ResetHandshake(ssl_sock, PR_FALSE) != SECSuccess) {
  341. return (NULL);
  342. }
  343. if (force_handshake && SSL_ForceHandshake(ssl_sock) != SECSuccess) {
  344. if (PR_GetError() == PR_WOULD_BLOCK_ERROR) {
  345. /*
  346. * Mask would block error.
  347. */
  348. *reset_would_block = 1;
  349. } else {
  350. return (NULL);
  351. }
  352. }
  353. return (ssl_sock);
  354. }
  355. PRFileDesc *
  356. nss_sock_start_ssl_as_server(PRFileDesc *input_sock, CERTCertificate *server_cert,
  357. SECKEYPrivateKey *server_key, int require_client_cert, int force_handshake,
  358. int *reset_would_block)
  359. {
  360. PRFileDesc *ssl_sock;
  361. if (force_handshake) {
  362. *reset_would_block = 0;
  363. }
  364. ssl_sock = SSL_ImportFD(NULL, input_sock);
  365. if (ssl_sock == NULL) {
  366. return (NULL);
  367. }
  368. if (SSL_ConfigSecureServer(ssl_sock, server_cert, server_key,
  369. NSS_FindCertKEAType(server_cert)) != SECSuccess) {
  370. return (NULL);
  371. }
  372. if ((SSL_OptionSet(ssl_sock, SSL_SECURITY, PR_TRUE) != SECSuccess) ||
  373. (SSL_OptionSet(ssl_sock, SSL_HANDSHAKE_AS_SERVER, PR_TRUE) != SECSuccess) ||
  374. (SSL_OptionSet(ssl_sock, SSL_HANDSHAKE_AS_CLIENT, PR_FALSE) != SECSuccess) ||
  375. (SSL_OptionSet(ssl_sock, SSL_REQUEST_CERTIFICATE, require_client_cert) != SECSuccess) ||
  376. (SSL_OptionSet(ssl_sock, SSL_REQUIRE_CERTIFICATE, require_client_cert) != SECSuccess)) {
  377. return (NULL);
  378. }
  379. if (SSL_ResetHandshake(ssl_sock, PR_TRUE) != SECSuccess) {
  380. return (NULL);
  381. }
  382. if (force_handshake && SSL_ForceHandshake(ssl_sock) != SECSuccess) {
  383. if (PR_GetError() == PR_WOULD_BLOCK_ERROR) {
  384. /*
  385. * Mask would block error.
  386. */
  387. *reset_would_block = 1;
  388. } else {
  389. return (NULL);
  390. }
  391. }
  392. return (ssl_sock);
  393. }