nss-sock.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 <err.h>
  35. #include <prnetdb.h>
  36. #include "nss-sock.h"
  37. void
  38. nss_sock_err(int eval) {
  39. errx(eval, "nss error %d: %s", PR_GetError(), PR_ErrorToString(PR_GetError(),
  40. PR_LANGUAGE_I_DEFAULT));
  41. }
  42. int
  43. nss_sock_init_nss(char *config_dir)
  44. {
  45. if (config_dir == NULL) {
  46. if (NSS_NoDB_Init(NULL) != SECSuccess) {
  47. return (-1);
  48. }
  49. } else {
  50. if (NSS_Init(config_dir) != SECSuccess) {
  51. return (-1);
  52. }
  53. }
  54. if (NSS_SetDomesticPolicy() != SECSuccess) {
  55. return (-1);
  56. }
  57. return (0);
  58. }
  59. /*
  60. * Set NSS socket non-blocking
  61. */
  62. int
  63. nss_sock_set_nonblocking(PRFileDesc *sock)
  64. {
  65. PRSocketOptionData sock_opt;
  66. memset(&sock_opt, 0, sizeof(sock_opt));
  67. sock_opt.option = PR_SockOpt_Nonblocking;
  68. sock_opt.value.non_blocking = PR_TRUE;
  69. if (PR_SetSocketOption(sock, &sock_opt) != PR_SUCCESS) {
  70. return (-1);
  71. }
  72. return (0);
  73. }
  74. /*
  75. * Create TCP socket with af family. If reuse_addr is set, socket option
  76. * for reuse address is set.
  77. */
  78. static PRFileDesc *
  79. nss_sock_create_socket(PRIntn af, int reuse_addr)
  80. {
  81. PRFileDesc *sock;
  82. PRSocketOptionData socket_option;
  83. sock = PR_OpenTCPSocket(af);
  84. if (sock == NULL) {
  85. return (NULL);
  86. }
  87. if (reuse_addr) {
  88. socket_option.option = PR_SockOpt_Reuseaddr;
  89. socket_option.value.reuse_addr = PR_TRUE;
  90. if (PR_SetSocketOption(sock, &socket_option) != PR_SUCCESS) {
  91. return (NULL);
  92. }
  93. }
  94. return (sock);
  95. }
  96. /*
  97. * Create listen socket and bind it to address. hostname can be NULL and then
  98. * any address is used. Address family (af) can be ether PR_AF_INET6 or
  99. * PR_AF_INET.
  100. */
  101. PRFileDesc *
  102. nss_sock_create_listen_socket(const char *hostname, uint16_t port, PRIntn af)
  103. {
  104. PRNetAddr addr;
  105. PRFileDesc *sock;
  106. PRAddrInfo *addr_info;
  107. PRIntn tmp_af;
  108. void *addr_iter;
  109. sock = NULL;
  110. if (hostname == NULL) {
  111. memset(&addr, 0, sizeof(addr));
  112. if (PR_InitializeNetAddr(PR_IpAddrAny, port, &addr) != PR_SUCCESS) {
  113. return (NULL);
  114. }
  115. addr.raw.family = af;
  116. sock = nss_sock_create_socket(af, 1);
  117. if (sock == NULL) {
  118. return (NULL);
  119. }
  120. if (PR_Bind(sock, &addr) != PR_SUCCESS) {
  121. PR_Close(sock);
  122. return (NULL);
  123. }
  124. } else {
  125. tmp_af = PR_AF_UNSPEC;
  126. if (af == PR_AF_INET)
  127. tmp_af = PR_AF_INET;
  128. addr_info = PR_GetAddrInfoByName(hostname, tmp_af, PR_AI_ADDRCONFIG);
  129. if (addr_info == NULL) {
  130. return (NULL);
  131. }
  132. addr_iter = NULL;
  133. while ((addr_iter = PR_EnumerateAddrInfo(addr_iter, addr_info, port,
  134. &addr)) != NULL) {
  135. if (addr.raw.family == af) {
  136. sock = nss_sock_create_socket(af, 1);
  137. if (sock == NULL) {
  138. continue ;
  139. }
  140. if (PR_Bind(sock, &addr) != PR_SUCCESS) {
  141. PR_Close(sock);
  142. sock = NULL;
  143. continue ;
  144. }
  145. /*
  146. * Socket is sucesfully bound
  147. */
  148. break;
  149. }
  150. }
  151. PR_FreeAddrInfo(addr_info);
  152. if (sock == NULL) {
  153. /*
  154. * No address succeeded
  155. */
  156. PR_SetError(PR_ADDRESS_NOT_AVAILABLE_ERROR, 0);
  157. return (NULL);
  158. }
  159. }
  160. return (sock);
  161. }
  162. /*
  163. * Create listen socket and bind it to address. hostname can be NULL and then
  164. * any address is used. Address family (af) can be ether PR_AF_UNSPEC or
  165. * PR_AF_INET.
  166. */
  167. PRFileDesc *
  168. nss_sock_create_client_socket(const char *hostname, uint16_t port, PRIntn af,
  169. PRIntervalTime timeout)
  170. {
  171. PRNetAddr addr;
  172. PRFileDesc *sock;
  173. PRAddrInfo *addr_info;
  174. void *addr_iter;
  175. PRStatus res;
  176. int connect_failed;
  177. sock = NULL;
  178. connect_failed = 0;
  179. addr_info = PR_GetAddrInfoByName(hostname, af, PR_AI_ADDRCONFIG);
  180. if (addr_info == NULL) {
  181. return (NULL);
  182. }
  183. addr_iter = NULL;
  184. while ((addr_iter = PR_EnumerateAddrInfo(addr_iter, addr_info, port, &addr)) != NULL) {
  185. sock = nss_sock_create_socket(addr.raw.family, 0);
  186. if (sock == NULL) {
  187. continue ;
  188. }
  189. if ((res = PR_Connect(sock, &addr, timeout)) != PR_SUCCESS) {
  190. PR_Close(sock);
  191. sock = NULL;
  192. connect_failed = 1;
  193. }
  194. /*
  195. * Connection attempt finished
  196. */
  197. break;
  198. }
  199. PR_FreeAddrInfo(addr_info);
  200. if (sock == NULL && !connect_failed) {
  201. PR_SetError(PR_ADDRESS_NOT_AVAILABLE_ERROR, 0);
  202. }
  203. return (sock);
  204. }
  205. /*
  206. * Start client side SSL connection. This can block.
  207. *
  208. * ssl_url is expected server URL, bad_cert_hook is callback called when server certificate
  209. * verification fails.
  210. */
  211. PRFileDesc *
  212. nss_sock_start_ssl_as_client(PRFileDesc *input_sock, const char *ssl_url,
  213. SSLBadCertHandler bad_cert_hook, SSLGetClientAuthData client_auth_hook,
  214. void *client_auth_hook_arg, int force_handshake, int *reset_would_block)
  215. {
  216. PRFileDesc *ssl_sock;
  217. if (force_handshake) {
  218. *reset_would_block = 0;
  219. }
  220. ssl_sock = SSL_ImportFD(NULL, input_sock);
  221. if (ssl_sock == NULL) {
  222. return (NULL);
  223. }
  224. if (SSL_SetURL(ssl_sock, ssl_url) != SECSuccess) {
  225. return (NULL);
  226. }
  227. if ((SSL_OptionSet(ssl_sock, SSL_SECURITY, PR_TRUE) != SECSuccess) ||
  228. (SSL_OptionSet(ssl_sock, SSL_HANDSHAKE_AS_SERVER, PR_FALSE) != SECSuccess) ||
  229. (SSL_OptionSet(ssl_sock, SSL_HANDSHAKE_AS_CLIENT, PR_TRUE) != SECSuccess)) {
  230. return (NULL);
  231. }
  232. if (bad_cert_hook != NULL && SSL_BadCertHook(ssl_sock, bad_cert_hook, NULL) != SECSuccess) {
  233. return (NULL);
  234. }
  235. if (client_auth_hook != NULL &&
  236. (SSL_GetClientAuthDataHook(ssl_sock, client_auth_hook,
  237. client_auth_hook_arg) != SECSuccess)) {
  238. return (NULL);
  239. }
  240. if (SSL_ResetHandshake(ssl_sock, PR_FALSE) != SECSuccess) {
  241. return (NULL);
  242. }
  243. if (force_handshake && SSL_ForceHandshake(ssl_sock) != SECSuccess) {
  244. if (PR_GetError() == PR_WOULD_BLOCK_ERROR) {
  245. /*
  246. * Mask would block error.
  247. */
  248. *reset_would_block = 1;
  249. } else {
  250. return (NULL);
  251. }
  252. }
  253. return (ssl_sock);
  254. }
  255. PRFileDesc *
  256. nss_sock_start_ssl_as_server(PRFileDesc *input_sock, CERTCertificate *server_cert,
  257. SECKEYPrivateKey *server_key, int require_client_cert, int force_handshake,
  258. int *reset_would_block)
  259. {
  260. PRFileDesc *ssl_sock;
  261. if (force_handshake) {
  262. *reset_would_block = 0;
  263. }
  264. ssl_sock = SSL_ImportFD(NULL, input_sock);
  265. if (ssl_sock == NULL) {
  266. return (NULL);
  267. }
  268. if (SSL_ConfigSecureServer(ssl_sock, server_cert, server_key,
  269. NSS_FindCertKEAType(server_cert)) != SECSuccess) {
  270. return (NULL);
  271. }
  272. if ((SSL_OptionSet(ssl_sock, SSL_SECURITY, PR_TRUE) != SECSuccess) ||
  273. (SSL_OptionSet(ssl_sock, SSL_HANDSHAKE_AS_SERVER, PR_TRUE) != SECSuccess) ||
  274. (SSL_OptionSet(ssl_sock, SSL_HANDSHAKE_AS_CLIENT, PR_FALSE) != SECSuccess) ||
  275. (SSL_OptionSet(ssl_sock, SSL_REQUEST_CERTIFICATE, require_client_cert) != SECSuccess) ||
  276. (SSL_OptionSet(ssl_sock, SSL_REQUIRE_CERTIFICATE, require_client_cert) != SECSuccess)) {
  277. return (NULL);
  278. }
  279. if (SSL_ResetHandshake(ssl_sock, PR_TRUE) != SECSuccess) {
  280. return (NULL);
  281. }
  282. if (force_handshake && SSL_ForceHandshake(ssl_sock) != SECSuccess) {
  283. if (PR_GetError() == PR_WOULD_BLOCK_ERROR) {
  284. /*
  285. * Mask would block error.
  286. */
  287. *reset_would_block = 1;
  288. } else {
  289. return (NULL);
  290. }
  291. }
  292. return (ssl_sock);
  293. }