nss-sock.c 8.4 KB

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