nss-sock.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /*
  2. * Copyright (c) 2015-2019 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 <sys/types.h>
  35. #include <dirent.h>
  36. #include <limits.h>
  37. #include "nss-sock.h"
  38. int
  39. nss_sock_init_nss(char *config_dir)
  40. {
  41. if (config_dir == NULL) {
  42. if (NSS_NoDB_Init(NULL) != SECSuccess) {
  43. return (-1);
  44. }
  45. } else {
  46. if (NSS_Init(config_dir) != SECSuccess) {
  47. return (-1);
  48. }
  49. }
  50. if (NSS_SetDomesticPolicy() != SECSuccess) {
  51. return (-1);
  52. }
  53. return (0);
  54. }
  55. int
  56. nss_sock_check_db_dir(const char *config_dir)
  57. {
  58. DIR *dirp;
  59. if (config_dir == NULL) {
  60. return (0);
  61. }
  62. if ((dirp = opendir(config_dir)) == NULL) {
  63. return (-1);
  64. }
  65. (void)closedir(dirp);
  66. return (0);
  67. }
  68. /*
  69. * Set NSS socket non-blocking
  70. */
  71. int
  72. nss_sock_set_non_blocking(PRFileDesc *sock)
  73. {
  74. PRSocketOptionData sock_opt;
  75. memset(&sock_opt, 0, sizeof(sock_opt));
  76. sock_opt.option = PR_SockOpt_Nonblocking;
  77. sock_opt.value.non_blocking = PR_TRUE;
  78. if (PR_SetSocketOption(sock, &sock_opt) != PR_SUCCESS) {
  79. return (-1);
  80. }
  81. return (0);
  82. }
  83. /*
  84. * Create TCP socket with af family. If reuse_addr is set, socket option
  85. * for reuse address is set.
  86. */
  87. static PRFileDesc *
  88. nss_sock_create_socket(PRIntn af, int reuse_addr)
  89. {
  90. PRFileDesc *sock;
  91. PRSocketOptionData socket_option;
  92. sock = PR_OpenTCPSocket(af);
  93. if (sock == NULL) {
  94. return (NULL);
  95. }
  96. if (reuse_addr) {
  97. socket_option.option = PR_SockOpt_Reuseaddr;
  98. socket_option.value.reuse_addr = PR_TRUE;
  99. if (PR_SetSocketOption(sock, &socket_option) != PR_SUCCESS) {
  100. return (NULL);
  101. }
  102. }
  103. return (sock);
  104. }
  105. /*
  106. * Create listen socket and bind it to address. hostname can be NULL and then
  107. * any address is used. Address family (af) can be ether PR_AF_INET6,
  108. * PR_AF_INET or PR_AF_UNSPEC.
  109. */
  110. PRFileDesc *
  111. nss_sock_create_listen_socket(const char *hostname, uint16_t port, PRIntn af)
  112. {
  113. PRNetAddr addr;
  114. PRFileDesc *sock;
  115. PRAddrInfo *addr_info;
  116. void *addr_iter;
  117. sock = NULL;
  118. if (hostname == NULL) {
  119. memset(&addr, 0, sizeof(addr));
  120. if (PR_InitializeNetAddr(PR_IpAddrAny, port, &addr) != PR_SUCCESS) {
  121. return (NULL);
  122. }
  123. if (af == PR_AF_UNSPEC) {
  124. af = PR_AF_INET6;
  125. }
  126. addr.raw.family = af;
  127. sock = nss_sock_create_socket(af, 1);
  128. if (sock == NULL) {
  129. return (NULL);
  130. }
  131. if (PR_Bind(sock, &addr) != PR_SUCCESS) {
  132. PR_Close(sock);
  133. return (NULL);
  134. }
  135. } else {
  136. addr_info = PR_GetAddrInfoByName(hostname, af, PR_AI_ADDRCONFIG);
  137. if (addr_info == NULL) {
  138. return (NULL);
  139. }
  140. addr_iter = NULL;
  141. while ((addr_iter = PR_EnumerateAddrInfo(addr_iter, addr_info, port,
  142. &addr)) != NULL) {
  143. if (af == PR_AF_UNSPEC || addr.raw.family == af) {
  144. sock = nss_sock_create_socket(addr.raw.family, 1);
  145. if (sock == NULL) {
  146. continue;
  147. }
  148. if (PR_Bind(sock, &addr) != PR_SUCCESS) {
  149. PR_Close(sock);
  150. sock = NULL;
  151. continue;
  152. }
  153. /*
  154. * Socket is successfully bound
  155. */
  156. break;
  157. }
  158. }
  159. PR_FreeAddrInfo(addr_info);
  160. if (sock == NULL) {
  161. /*
  162. * No address succeeded
  163. */
  164. PR_SetError(PR_ADDRESS_NOT_AVAILABLE_ERROR, 0);
  165. return (NULL);
  166. }
  167. }
  168. return (sock);
  169. }
  170. PRFileDesc *
  171. nss_sock_create_client_socket(const char *hostname, uint16_t port, PRIntn af,
  172. PRIntervalTime timeout)
  173. {
  174. PRNetAddr addr;
  175. PRFileDesc *sock;
  176. PRAddrInfo *addr_info;
  177. void *addr_iter;
  178. PRStatus res;
  179. int connect_failed;
  180. PRIntn tmp_af;
  181. sock = NULL;
  182. connect_failed = 0;
  183. tmp_af = af;
  184. if (af == PR_AF_INET6) {
  185. tmp_af = PR_AF_UNSPEC;
  186. }
  187. addr_info = PR_GetAddrInfoByName(hostname, tmp_af, PR_AI_ADDRCONFIG);
  188. if (addr_info == NULL) {
  189. return (NULL);
  190. }
  191. addr_iter = NULL;
  192. while ((addr_iter = PR_EnumerateAddrInfo(addr_iter, addr_info, port, &addr)) != NULL) {
  193. if (af != PR_AF_UNSPEC && addr.raw.family != af) {
  194. continue;
  195. }
  196. sock = nss_sock_create_socket(addr.raw.family, 0);
  197. if (sock == NULL) {
  198. continue;
  199. }
  200. if ((res = PR_Connect(sock, &addr, timeout)) != PR_SUCCESS) {
  201. PR_Close(sock);
  202. sock = NULL;
  203. connect_failed = 1;
  204. }
  205. /*
  206. * Connection attempt finished
  207. */
  208. break;
  209. }
  210. PR_FreeAddrInfo(addr_info);
  211. if (sock == NULL && !connect_failed) {
  212. PR_SetError(PR_ADDRESS_NOT_AVAILABLE_ERROR, 0);
  213. }
  214. return (sock);
  215. }
  216. int
  217. nss_sock_non_blocking_client_init(const char *host_name, uint16_t port, PRIntn af,
  218. struct nss_sock_non_blocking_client *client)
  219. {
  220. PRIntn tmp_af;
  221. client->destroyed = 1;
  222. if ((client->host_name = strdup(host_name)) == NULL) {
  223. PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
  224. return (-1);
  225. }
  226. client->port = port;
  227. client->af = af;
  228. tmp_af = af;
  229. if (af == PR_AF_INET6) {
  230. tmp_af = PR_AF_UNSPEC;
  231. }
  232. client->addr_info = PR_GetAddrInfoByName(client->host_name, tmp_af, PR_AI_ADDRCONFIG);
  233. if (client->addr_info == NULL) {
  234. free(client->host_name);
  235. return (-1);
  236. }
  237. client->addr_iter = NULL;
  238. client->connect_attempts = 0;
  239. client->socket = NULL;
  240. client->destroyed = 0;
  241. return (0);
  242. }
  243. int
  244. nss_sock_non_blocking_client_try_next(struct nss_sock_non_blocking_client *client)
  245. {
  246. PRNetAddr addr;
  247. PRStatus res;
  248. if (client->destroyed) {
  249. PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0);
  250. return (-1);
  251. }
  252. if (client->socket != NULL) {
  253. PR_Close(client->socket);
  254. client->socket = NULL;
  255. }
  256. while ((client->addr_iter = PR_EnumerateAddrInfo(client->addr_iter, client->addr_info,
  257. client->port, &addr)) != NULL) {
  258. if (client->af != PR_AF_UNSPEC && addr.raw.family != client->af) {
  259. continue;
  260. }
  261. client->socket = nss_sock_create_socket(addr.raw.family, 0);
  262. if (client->socket == NULL) {
  263. continue;
  264. }
  265. if (nss_sock_set_non_blocking(client->socket) == -1) {
  266. PR_Close(client->socket);
  267. client->socket = NULL;
  268. continue;
  269. }
  270. res = PR_Connect(client->socket, &addr, PR_INTERVAL_NO_TIMEOUT);
  271. if (res == PR_SUCCESS || PR_GetError() == PR_IN_PROGRESS_ERROR) {
  272. return (0);
  273. }
  274. PR_Close(client->socket);
  275. client->socket = NULL;
  276. if (client->connect_attempts < INT_MAX) {
  277. client->connect_attempts++;
  278. }
  279. }
  280. if (client->connect_attempts == 0) {
  281. PR_SetError(PR_ADDRESS_NOT_AVAILABLE_ERROR, 0);
  282. }
  283. return (-1);
  284. }
  285. void
  286. nss_sock_non_blocking_client_destroy(struct nss_sock_non_blocking_client *client)
  287. {
  288. if (client->destroyed) {
  289. return ;
  290. }
  291. if (client->addr_info != NULL) {
  292. PR_FreeAddrInfo(client->addr_info);
  293. client->addr_info = NULL;
  294. }
  295. free(client->host_name);
  296. client->host_name = NULL;
  297. client->destroyed = 1;
  298. }
  299. /*
  300. * -1 = Client connect failed
  301. * 0 = Client connect still in progress
  302. * 1 = Client successfuly connected
  303. */
  304. int
  305. nss_sock_non_blocking_client_succeeded(const PRPollDesc *pfd)
  306. {
  307. int res;
  308. res = -1;
  309. if (PR_GetConnectStatus(pfd) == PR_SUCCESS) {
  310. res = 1;
  311. } else {
  312. if (PR_GetError() == PR_IN_PROGRESS_ERROR) {
  313. res = 0;
  314. } else {
  315. res = -1;
  316. }
  317. }
  318. return (res);
  319. }
  320. /*
  321. * Start client side SSL connection. This can block.
  322. *
  323. * ssl_url is expected server URL, bad_cert_hook is callback called when server certificate
  324. * verification fails.
  325. */
  326. PRFileDesc *
  327. nss_sock_start_ssl_as_client(PRFileDesc *input_sock, const char *ssl_url,
  328. SSLBadCertHandler bad_cert_hook, SSLGetClientAuthData client_auth_hook,
  329. void *client_auth_hook_arg, int force_handshake, int *reset_would_block)
  330. {
  331. PRFileDesc *ssl_sock;
  332. if (force_handshake) {
  333. *reset_would_block = 0;
  334. }
  335. ssl_sock = SSL_ImportFD(NULL, input_sock);
  336. if (ssl_sock == NULL) {
  337. return (NULL);
  338. }
  339. if (SSL_SetURL(ssl_sock, ssl_url) != SECSuccess) {
  340. return (NULL);
  341. }
  342. if ((SSL_OptionSet(ssl_sock, SSL_SECURITY, PR_TRUE) != SECSuccess) ||
  343. (SSL_OptionSet(ssl_sock, SSL_HANDSHAKE_AS_SERVER, PR_FALSE) != SECSuccess) ||
  344. (SSL_OptionSet(ssl_sock, SSL_HANDSHAKE_AS_CLIENT, PR_TRUE) != SECSuccess)) {
  345. return (NULL);
  346. }
  347. if (bad_cert_hook != NULL && SSL_BadCertHook(ssl_sock, bad_cert_hook, NULL) != SECSuccess) {
  348. return (NULL);
  349. }
  350. if (client_auth_hook != NULL &&
  351. (SSL_GetClientAuthDataHook(ssl_sock, client_auth_hook,
  352. client_auth_hook_arg) != SECSuccess)) {
  353. return (NULL);
  354. }
  355. if (SSL_ResetHandshake(ssl_sock, PR_FALSE) != SECSuccess) {
  356. return (NULL);
  357. }
  358. if (force_handshake && SSL_ForceHandshake(ssl_sock) != SECSuccess) {
  359. if (PR_GetError() == PR_WOULD_BLOCK_ERROR) {
  360. /*
  361. * Mask would block error.
  362. */
  363. *reset_would_block = 1;
  364. } else {
  365. return (NULL);
  366. }
  367. }
  368. return (ssl_sock);
  369. }
  370. PRFileDesc *
  371. nss_sock_start_ssl_as_server(PRFileDesc *input_sock, CERTCertificate *server_cert,
  372. SECKEYPrivateKey *server_key, int require_client_cert, int force_handshake,
  373. int *reset_would_block)
  374. {
  375. PRFileDesc *ssl_sock;
  376. if (force_handshake) {
  377. *reset_would_block = 0;
  378. }
  379. ssl_sock = SSL_ImportFD(NULL, input_sock);
  380. if (ssl_sock == NULL) {
  381. return (NULL);
  382. }
  383. if (SSL_ConfigSecureServer(ssl_sock, server_cert, server_key,
  384. NSS_FindCertKEAType(server_cert)) != SECSuccess) {
  385. return (NULL);
  386. }
  387. if ((SSL_OptionSet(ssl_sock, SSL_SECURITY, PR_TRUE) != SECSuccess) ||
  388. (SSL_OptionSet(ssl_sock, SSL_HANDSHAKE_AS_SERVER, PR_TRUE) != SECSuccess) ||
  389. (SSL_OptionSet(ssl_sock, SSL_HANDSHAKE_AS_CLIENT, PR_FALSE) != SECSuccess) ||
  390. (SSL_OptionSet(ssl_sock, SSL_REQUEST_CERTIFICATE, require_client_cert) != SECSuccess) ||
  391. (SSL_OptionSet(ssl_sock, SSL_REQUIRE_CERTIFICATE, require_client_cert) != SECSuccess)) {
  392. return (NULL);
  393. }
  394. if (SSL_ResetHandshake(ssl_sock, PR_TRUE) != SECSuccess) {
  395. return (NULL);
  396. }
  397. if (force_handshake && SSL_ForceHandshake(ssl_sock) != SECSuccess) {
  398. if (PR_GetError() == PR_WOULD_BLOCK_ERROR) {
  399. /*
  400. * Mask would block error.
  401. */
  402. *reset_would_block = 1;
  403. } else {
  404. return (NULL);
  405. }
  406. }
  407. return (ssl_sock);
  408. }