nss-sock.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * Copyright (c) 2015 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 <prnetdb.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_nonblocking(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 or
  93. * PR_AF_INET.
  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. PRIntn tmp_af;
  102. void *addr_iter;
  103. sock = NULL;
  104. if (hostname == NULL) {
  105. memset(&addr, 0, sizeof(addr));
  106. if (PR_InitializeNetAddr(PR_IpAddrAny, port, &addr) != PR_SUCCESS) {
  107. return (NULL);
  108. }
  109. addr.raw.family = af;
  110. sock = nss_sock_create_socket(af, 1);
  111. if (sock == NULL) {
  112. return (NULL);
  113. }
  114. if (PR_Bind(sock, &addr) != PR_SUCCESS) {
  115. PR_Close(sock);
  116. return (NULL);
  117. }
  118. } else {
  119. tmp_af = PR_AF_UNSPEC;
  120. if (af == PR_AF_INET)
  121. tmp_af = PR_AF_INET;
  122. addr_info = PR_GetAddrInfoByName(hostname, tmp_af, PR_AI_ADDRCONFIG);
  123. if (addr_info == NULL) {
  124. return (NULL);
  125. }
  126. addr_iter = NULL;
  127. while ((addr_iter = PR_EnumerateAddrInfo(addr_iter, addr_info, port,
  128. &addr)) != NULL) {
  129. if (addr.raw.family == af) {
  130. sock = nss_sock_create_socket(af, 1);
  131. if (sock == NULL) {
  132. continue ;
  133. }
  134. if (PR_Bind(sock, &addr) != PR_SUCCESS) {
  135. PR_Close(sock);
  136. sock = NULL;
  137. continue ;
  138. }
  139. /*
  140. * Socket is sucesfully bound
  141. */
  142. break;
  143. }
  144. }
  145. PR_FreeAddrInfo(addr_info);
  146. if (sock == NULL) {
  147. /*
  148. * No address succeeded
  149. */
  150. PR_SetError(PR_ADDRESS_NOT_AVAILABLE_ERROR, 0);
  151. return (NULL);
  152. }
  153. }
  154. return (sock);
  155. }
  156. /*
  157. * Create listen socket and bind it to address. hostname can be NULL and then
  158. * any address is used. Address family (af) can be ether PR_AF_UNSPEC or
  159. * PR_AF_INET.
  160. */
  161. PRFileDesc *
  162. nss_sock_create_client_socket(const char *hostname, uint16_t port, PRIntn af,
  163. PRIntervalTime timeout)
  164. {
  165. PRNetAddr addr;
  166. PRFileDesc *sock;
  167. PRAddrInfo *addr_info;
  168. void *addr_iter;
  169. PRStatus res;
  170. int connect_failed;
  171. sock = NULL;
  172. connect_failed = 0;
  173. addr_info = PR_GetAddrInfoByName(hostname, af, PR_AI_ADDRCONFIG);
  174. if (addr_info == NULL) {
  175. return (NULL);
  176. }
  177. addr_iter = NULL;
  178. while ((addr_iter = PR_EnumerateAddrInfo(addr_iter, addr_info, port, &addr)) != NULL) {
  179. sock = nss_sock_create_socket(addr.raw.family, 0);
  180. if (sock == NULL) {
  181. continue ;
  182. }
  183. if ((res = PR_Connect(sock, &addr, timeout)) != PR_SUCCESS) {
  184. PR_Close(sock);
  185. sock = NULL;
  186. connect_failed = 1;
  187. }
  188. /*
  189. * Connection attempt finished
  190. */
  191. break;
  192. }
  193. PR_FreeAddrInfo(addr_info);
  194. if (sock == NULL && !connect_failed) {
  195. PR_SetError(PR_ADDRESS_NOT_AVAILABLE_ERROR, 0);
  196. }
  197. return (sock);
  198. }
  199. /*
  200. * Start client side SSL connection. This can block.
  201. *
  202. * ssl_url is expected server URL, bad_cert_hook is callback called when server certificate
  203. * verification fails.
  204. */
  205. PRFileDesc *
  206. nss_sock_start_ssl_as_client(PRFileDesc *input_sock, const char *ssl_url,
  207. SSLBadCertHandler bad_cert_hook, SSLGetClientAuthData client_auth_hook,
  208. void *client_auth_hook_arg, int force_handshake, int *reset_would_block)
  209. {
  210. PRFileDesc *ssl_sock;
  211. if (force_handshake) {
  212. *reset_would_block = 0;
  213. }
  214. ssl_sock = SSL_ImportFD(NULL, input_sock);
  215. if (ssl_sock == NULL) {
  216. return (NULL);
  217. }
  218. if (SSL_SetURL(ssl_sock, ssl_url) != SECSuccess) {
  219. return (NULL);
  220. }
  221. if ((SSL_OptionSet(ssl_sock, SSL_SECURITY, PR_TRUE) != SECSuccess) ||
  222. (SSL_OptionSet(ssl_sock, SSL_HANDSHAKE_AS_SERVER, PR_FALSE) != SECSuccess) ||
  223. (SSL_OptionSet(ssl_sock, SSL_HANDSHAKE_AS_CLIENT, PR_TRUE) != SECSuccess)) {
  224. return (NULL);
  225. }
  226. if (bad_cert_hook != NULL && SSL_BadCertHook(ssl_sock, bad_cert_hook, NULL) != SECSuccess) {
  227. return (NULL);
  228. }
  229. if (client_auth_hook != NULL &&
  230. (SSL_GetClientAuthDataHook(ssl_sock, client_auth_hook,
  231. client_auth_hook_arg) != SECSuccess)) {
  232. return (NULL);
  233. }
  234. if (SSL_ResetHandshake(ssl_sock, PR_FALSE) != SECSuccess) {
  235. return (NULL);
  236. }
  237. if (force_handshake && SSL_ForceHandshake(ssl_sock) != SECSuccess) {
  238. if (PR_GetError() == PR_WOULD_BLOCK_ERROR) {
  239. /*
  240. * Mask would block error.
  241. */
  242. *reset_would_block = 1;
  243. } else {
  244. return (NULL);
  245. }
  246. }
  247. return (ssl_sock);
  248. }
  249. PRFileDesc *
  250. nss_sock_start_ssl_as_server(PRFileDesc *input_sock, CERTCertificate *server_cert,
  251. SECKEYPrivateKey *server_key, int require_client_cert, int force_handshake,
  252. int *reset_would_block)
  253. {
  254. PRFileDesc *ssl_sock;
  255. if (force_handshake) {
  256. *reset_would_block = 0;
  257. }
  258. ssl_sock = SSL_ImportFD(NULL, input_sock);
  259. if (ssl_sock == NULL) {
  260. return (NULL);
  261. }
  262. if (SSL_ConfigSecureServer(ssl_sock, server_cert, server_key,
  263. NSS_FindCertKEAType(server_cert)) != SECSuccess) {
  264. return (NULL);
  265. }
  266. if ((SSL_OptionSet(ssl_sock, SSL_SECURITY, PR_TRUE) != SECSuccess) ||
  267. (SSL_OptionSet(ssl_sock, SSL_HANDSHAKE_AS_SERVER, PR_TRUE) != SECSuccess) ||
  268. (SSL_OptionSet(ssl_sock, SSL_HANDSHAKE_AS_CLIENT, PR_FALSE) != SECSuccess) ||
  269. (SSL_OptionSet(ssl_sock, SSL_REQUEST_CERTIFICATE, require_client_cert) != SECSuccess) ||
  270. (SSL_OptionSet(ssl_sock, SSL_REQUIRE_CERTIFICATE, require_client_cert) != SECSuccess)) {
  271. return (NULL);
  272. }
  273. if (SSL_ResetHandshake(ssl_sock, PR_TRUE) != SECSuccess) {
  274. return (NULL);
  275. }
  276. if (force_handshake && SSL_ForceHandshake(ssl_sock) != SECSuccess) {
  277. if (PR_GetError() == PR_WOULD_BLOCK_ERROR) {
  278. /*
  279. * Mask would block error.
  280. */
  281. *reset_would_block = 1;
  282. } else {
  283. return (NULL);
  284. }
  285. }
  286. return (ssl_sock);
  287. }