cowboy_ssl_transport.erl 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. %% Copyright (c) 2011, Loïc Hoguin <essen@dev-extend.eu>
  2. %%
  3. %% Permission to use, copy, modify, and/or distribute this software for any
  4. %% purpose with or without fee is hereby granted, provided that the above
  5. %% copyright notice and this permission notice appear in all copies.
  6. %%
  7. %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. %% @doc SSL transport API.
  15. %%
  16. %% Wrapper around <em>ssl</em> implementing the Cowboy transport API.
  17. %%
  18. %% This transport requires the <em>crypto</em>, <em>public_key</em>
  19. %% and <em>ssl</em> applications to be started. If they aren't started,
  20. %% it will try to start them itself before opening a port to listen.
  21. %% Applications aren't stopped when the listening socket is closed, though.
  22. %%
  23. %% @see ssl
  24. -module(cowboy_ssl_transport).
  25. -export([name/0, messages/0, listen/1, accept/2, recv/3, send/2, setopts/2,
  26. controlling_process/2, peername/1, close/1, sockname/1]).
  27. %% @doc Name of this transport API, <em>ssl</em>.
  28. -spec name() -> ssl.
  29. name() -> ssl.
  30. %% @doc Atoms used in the process messages sent by this API.
  31. %%
  32. %% They identify incoming data, closed connection and errors when receiving
  33. %% data in active mode.
  34. -spec messages() -> {ssl, ssl_closed, ssl_error}.
  35. messages() -> {ssl, ssl_closed, ssl_error}.
  36. %% @doc Setup a socket to listen on the given port on the local host.
  37. %%
  38. %% The available options are:
  39. %% <dl>
  40. %% <dt>port</dt><dd>Mandatory. TCP port number to open.</dd>
  41. %% <dt>backlog</dt><dd>Maximum length of the pending connections queue.
  42. %% Defaults to 1024.</dd>
  43. %% <dt>ip</dt><dd>Interface to listen on. Listen on all interfaces
  44. %% by default.</dd>
  45. %% <dt>certfile</dt><dd>Mandatory. Path to a file containing the user's
  46. %% certificate.</dd>
  47. %% <dt>keyfile</dt><dd>Optional. Path to the file containing the user's
  48. %% private PEM encoded key.</dd>
  49. %% <dt>cacertfile</dt><dd>Optional. Path to file containing PEM encoded
  50. %% CA certificates (trusted certificates used for verifying a peer
  51. %% certificate).</dd>
  52. %% <dt>password</dt><dd>Optional. String containing the user's password.
  53. %% All private keyfiles must be password protected currently.</dd>
  54. %% <dt>ciphers</dt><dd>Optional. The cipher suites that should be supported.
  55. %% The function ssl:cipher_suites/0 can be used to find all available
  56. %% ciphers.</dd>
  57. %% </dl>
  58. %%
  59. %% @see ssl:listen/2
  60. -spec listen([{port, inet:port_number()} | {certfile, string()}
  61. | {keyfile, string()} | {password, string()}
  62. | {cacertfile, string()} | {ip, inet:ip_address()}])
  63. -> {ok, ssl:sslsocket()} | {error, atom()}.
  64. listen(Opts) ->
  65. require([crypto, public_key, ssl]),
  66. {port, Port} = lists:keyfind(port, 1, Opts),
  67. Backlog = proplists:get_value(backlog, Opts, 1024),
  68. {certfile, CertFile} = lists:keyfind(certfile, 1, Opts),
  69. ListenOpts0 = [binary, {active, false},
  70. {backlog, Backlog}, {packet, raw}, {reuseaddr, true},
  71. {certfile, CertFile}],
  72. ListenOpts = lists:foldl(fun
  73. ({ip, _} = Ip, Acc) -> [Ip | Acc];
  74. ({keyfile, _} = KeyFile, Acc) -> [KeyFile | Acc];
  75. ({cacertfile, _} = CACertFile, Acc) -> [CACertFile | Acc];
  76. ({password, _} = Password, Acc) -> [Password | Acc];
  77. ({ciphers, _} = Ciphers, Acc) -> [Ciphers | Acc];
  78. (_, Acc) -> Acc
  79. end, ListenOpts0, Opts),
  80. ssl:listen(Port, ListenOpts).
  81. %% @doc Accept an incoming connection on a listen socket.
  82. %%
  83. %% Note that this function does both the transport accept and
  84. %% the SSL handshake.
  85. %%
  86. %% @see ssl:transport_accept/2
  87. %% @see ssl:ssl_accept/2
  88. -spec accept(ssl:sslsocket(), timeout())
  89. -> {ok, ssl:sslsocket()} | {error, closed | timeout | atom()}.
  90. accept(LSocket, Timeout) ->
  91. case ssl:transport_accept(LSocket, Timeout) of
  92. {ok, CSocket} ->
  93. ssl_accept(CSocket, Timeout);
  94. {error, Reason} ->
  95. {error, Reason}
  96. end.
  97. %% @doc Receive a packet from a socket in passive mode.
  98. %% @see ssl:recv/3
  99. -spec recv(ssl:sslsocket(), non_neg_integer(), timeout())
  100. -> {ok, any()} | {error, closed | atom()}.
  101. recv(Socket, Length, Timeout) ->
  102. ssl:recv(Socket, Length, Timeout).
  103. %% @doc Send a packet on a socket.
  104. %% @see ssl:send/2
  105. -spec send(ssl:sslsocket(), iolist()) -> ok | {error, atom()}.
  106. send(Socket, Packet) ->
  107. ssl:send(Socket, Packet).
  108. %% @doc Set one or more options for a socket.
  109. %% @see ssl:setopts/2
  110. -spec setopts(ssl:sslsocket(), list()) -> ok | {error, atom()}.
  111. setopts(Socket, Opts) ->
  112. ssl:setopts(Socket, Opts).
  113. %% @doc Assign a new controlling process <em>Pid</em> to <em>Socket</em>.
  114. %% @see ssl:controlling_process/2
  115. -spec controlling_process(ssl:sslsocket(), pid())
  116. -> ok | {error, closed | not_owner | atom()}.
  117. controlling_process(Socket, Pid) ->
  118. ssl:controlling_process(Socket, Pid).
  119. %% @doc Return the address and port for the other end of a connection.
  120. %% @see ssl:peername/1
  121. -spec peername(ssl:sslsocket())
  122. -> {ok, {inet:ip_address(), inet:port_number()}} | {error, atom()}.
  123. peername(Socket) ->
  124. ssl:peername(Socket).
  125. %% @doc Close a TCP socket.
  126. %% @see ssl:close/1
  127. -spec close(ssl:sslsocket()) -> ok.
  128. close(Socket) ->
  129. ssl:close(Socket).
  130. %% @doc Get the local address and port of a socket
  131. %% @see ssl:sockname/1
  132. -spec sockname(ssl:sslsocket())
  133. -> {ok, {inet:ip_address(), inet:port_number()}} | {error, atom()}.
  134. sockname(Socket) ->
  135. ssl:sockname(Socket).
  136. %% Internal.
  137. -spec require(list(module())) -> ok.
  138. require([]) ->
  139. ok;
  140. require([App|Tail]) ->
  141. case application:start(App) of
  142. ok -> ok;
  143. {error, {already_started, App}} -> ok
  144. end,
  145. require(Tail).
  146. -spec ssl_accept(ssl:sslsocket(), timeout())
  147. -> {ok, ssl:sslsocket()} | {error, closed | timeout | atom()}.
  148. ssl_accept(Socket, Timeout) ->
  149. case ssl:ssl_accept(Socket, Timeout) of
  150. ok ->
  151. {ok, Socket};
  152. {error, Reason} ->
  153. {error, Reason}
  154. end.