nss-sock.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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. sock = NULL;
  166. connect_failed = 0;
  167. addr_info = PR_GetAddrInfoByName(hostname, af, PR_AI_ADDRCONFIG);
  168. if (addr_info == NULL) {
  169. return (NULL);
  170. }
  171. addr_iter = NULL;
  172. while ((addr_iter = PR_EnumerateAddrInfo(addr_iter, addr_info, port, &addr)) != NULL) {
  173. sock = nss_sock_create_socket(addr.raw.family, 0);
  174. if (sock == NULL) {
  175. continue ;
  176. }
  177. if ((res = PR_Connect(sock, &addr, timeout)) != PR_SUCCESS) {
  178. PR_Close(sock);
  179. sock = NULL;
  180. connect_failed = 1;
  181. }
  182. /*
  183. * Connection attempt finished
  184. */
  185. break;
  186. }
  187. PR_FreeAddrInfo(addr_info);
  188. if (sock == NULL && !connect_failed) {
  189. PR_SetError(PR_ADDRESS_NOT_AVAILABLE_ERROR, 0);
  190. }
  191. return (sock);
  192. }
  193. int
  194. nss_sock_non_blocking_client_init(const char *host_name, uint16_t port, PRIntn af,
  195. struct nss_sock_non_blocking_client *client)
  196. {
  197. client->destroyed = 1;
  198. if ((client->host_name = strdup(host_name)) == NULL) {
  199. PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
  200. return (-1);
  201. }
  202. client->port = port;
  203. client->af = af;
  204. client->addr_info = PR_GetAddrInfoByName(client->host_name, af, PR_AI_ADDRCONFIG);
  205. if (client->addr_info == NULL) {
  206. free(client->host_name);
  207. return (-1);
  208. }
  209. client->addr_iter = NULL;
  210. client->connect_attempts = 0;
  211. client->socket = NULL;
  212. client->destroyed = 0;
  213. return (0);
  214. }
  215. int
  216. nss_sock_non_blocking_client_try_next(struct nss_sock_non_blocking_client *client)
  217. {
  218. PRNetAddr addr;
  219. PRStatus res;
  220. if (client->socket != NULL) {
  221. PR_Close(client->socket);
  222. client->socket = NULL;
  223. }
  224. while ((client->addr_iter = PR_EnumerateAddrInfo(client->addr_iter, client->addr_info,
  225. client->port, &addr)) != NULL) {
  226. client->socket = nss_sock_create_socket(addr.raw.family, 0);
  227. if (client->socket == NULL) {
  228. continue ;
  229. }
  230. if (nss_sock_set_non_blocking(client->socket) == -1) {
  231. PR_Close(client->socket);
  232. client->socket = NULL;
  233. continue ;
  234. }
  235. res = PR_Connect(client->socket, &addr, PR_INTERVAL_NO_TIMEOUT);
  236. if (res == PR_SUCCESS || PR_GetError() == PR_IN_PROGRESS_ERROR) {
  237. return (0);
  238. }
  239. PR_Close(client->socket);
  240. client->socket = NULL;
  241. if (client->connect_attempts < INT_MAX) {
  242. client->connect_attempts++;
  243. }
  244. }
  245. if (client->connect_attempts == 0) {
  246. PR_SetError(PR_ADDRESS_NOT_AVAILABLE_ERROR, 0);
  247. }
  248. return (-1);
  249. }
  250. void
  251. nss_sock_non_blocking_client_destroy(struct nss_sock_non_blocking_client *client)
  252. {
  253. if (client->addr_info != NULL) {
  254. PR_FreeAddrInfo(client->addr_info);
  255. client->addr_info = NULL;
  256. }
  257. free(client->host_name);
  258. client->host_name = NULL;
  259. client->destroyed = 1;
  260. }
  261. /*
  262. * -1 = Client connect failed
  263. * 0 = Client connect still in progress
  264. * 1 = Client successfuly connected
  265. */
  266. int
  267. nss_sock_non_blocking_client_succeeded(const PRPollDesc *pfd)
  268. {
  269. int res;
  270. res = -1;
  271. if (PR_GetConnectStatus(pfd) == PR_SUCCESS) {
  272. res = 1;
  273. } else {
  274. if (PR_GetError() == PR_IN_PROGRESS_ERROR) {
  275. res = 0;
  276. } else {
  277. res = -1;
  278. }
  279. }
  280. return (res);
  281. }
  282. /*
  283. * Start client side SSL connection. This can block.
  284. *
  285. * ssl_url is expected server URL, bad_cert_hook is callback called when server certificate
  286. * verification fails.
  287. */
  288. PRFileDesc *
  289. nss_sock_start_ssl_as_client(PRFileDesc *input_sock, const char *ssl_url,
  290. SSLBadCertHandler bad_cert_hook, SSLGetClientAuthData client_auth_hook,
  291. void *client_auth_hook_arg, int force_handshake, int *reset_would_block)
  292. {
  293. PRFileDesc *ssl_sock;
  294. if (force_handshake) {
  295. *reset_would_block = 0;
  296. }
  297. ssl_sock = SSL_ImportFD(NULL, input_sock);
  298. if (ssl_sock == NULL) {
  299. return (NULL);
  300. }
  301. if (SSL_SetURL(ssl_sock, ssl_url) != SECSuccess) {
  302. return (NULL);
  303. }
  304. if ((SSL_OptionSet(ssl_sock, SSL_SECURITY, PR_TRUE) != SECSuccess) ||
  305. (SSL_OptionSet(ssl_sock, SSL_HANDSHAKE_AS_SERVER, PR_FALSE) != SECSuccess) ||
  306. (SSL_OptionSet(ssl_sock, SSL_HANDSHAKE_AS_CLIENT, PR_TRUE) != SECSuccess)) {
  307. return (NULL);
  308. }
  309. if (bad_cert_hook != NULL && SSL_BadCertHook(ssl_sock, bad_cert_hook, NULL) != SECSuccess) {
  310. return (NULL);
  311. }
  312. if (client_auth_hook != NULL &&
  313. (SSL_GetClientAuthDataHook(ssl_sock, client_auth_hook,
  314. client_auth_hook_arg) != SECSuccess)) {
  315. return (NULL);
  316. }
  317. if (SSL_ResetHandshake(ssl_sock, PR_FALSE) != SECSuccess) {
  318. return (NULL);
  319. }
  320. if (force_handshake && SSL_ForceHandshake(ssl_sock) != SECSuccess) {
  321. if (PR_GetError() == PR_WOULD_BLOCK_ERROR) {
  322. /*
  323. * Mask would block error.
  324. */
  325. *reset_would_block = 1;
  326. } else {
  327. return (NULL);
  328. }
  329. }
  330. return (ssl_sock);
  331. }
  332. PRFileDesc *
  333. nss_sock_start_ssl_as_server(PRFileDesc *input_sock, CERTCertificate *server_cert,
  334. SECKEYPrivateKey *server_key, int require_client_cert, int force_handshake,
  335. int *reset_would_block)
  336. {
  337. PRFileDesc *ssl_sock;
  338. if (force_handshake) {
  339. *reset_would_block = 0;
  340. }
  341. ssl_sock = SSL_ImportFD(NULL, input_sock);
  342. if (ssl_sock == NULL) {
  343. return (NULL);
  344. }
  345. if (SSL_ConfigSecureServer(ssl_sock, server_cert, server_key,
  346. NSS_FindCertKEAType(server_cert)) != SECSuccess) {
  347. return (NULL);
  348. }
  349. if ((SSL_OptionSet(ssl_sock, SSL_SECURITY, PR_TRUE) != SECSuccess) ||
  350. (SSL_OptionSet(ssl_sock, SSL_HANDSHAKE_AS_SERVER, PR_TRUE) != SECSuccess) ||
  351. (SSL_OptionSet(ssl_sock, SSL_HANDSHAKE_AS_CLIENT, PR_FALSE) != SECSuccess) ||
  352. (SSL_OptionSet(ssl_sock, SSL_REQUEST_CERTIFICATE, require_client_cert) != SECSuccess) ||
  353. (SSL_OptionSet(ssl_sock, SSL_REQUIRE_CERTIFICATE, require_client_cert) != SECSuccess)) {
  354. return (NULL);
  355. }
  356. if (SSL_ResetHandshake(ssl_sock, PR_TRUE) != SECSuccess) {
  357. return (NULL);
  358. }
  359. if (force_handshake && SSL_ForceHandshake(ssl_sock) != SECSuccess) {
  360. if (PR_GetError() == PR_WOULD_BLOCK_ERROR) {
  361. /*
  362. * Mask would block error.
  363. */
  364. *reset_would_block = 1;
  365. } else {
  366. return (NULL);
  367. }
  368. }
  369. return (ssl_sock);
  370. }