cowboy_ssl_transport.erl 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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]).
  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>Mandatory. 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>Mandatory. String containing the user's password.
  53. %% All private keyfiles must be password protected currently.</dd>
  54. %% </dl>
  55. %%
  56. %% @see ssl:listen/2
  57. %% @todo The password option shouldn't be mandatory.
  58. -spec listen([{port, inet:ip_port()} | {certfile, string()}
  59. | {keyfile, string()} | {password, string()}
  60. | {cacertfile, string()} | {ip, inet:ip_address()}])
  61. -> {ok, ssl:sslsocket()} | {error, atom()}.
  62. listen(Opts) ->
  63. require([crypto, public_key, ssl]),
  64. {port, Port} = lists:keyfind(port, 1, Opts),
  65. Backlog = proplists:get_value(backlog, Opts, 1024),
  66. {certfile, CertFile} = lists:keyfind(certfile, 1, Opts),
  67. {keyfile, KeyFile} = lists:keyfind(keyfile, 1, Opts),
  68. {password, Password} = lists:keyfind(password, 1, Opts),
  69. ListenOpts0 = [binary, {active, false},
  70. {backlog, Backlog}, {packet, raw}, {reuseaddr, true},
  71. {certfile, CertFile}, {keyfile, KeyFile}, {password, Password}],
  72. ListenOpts1 =
  73. case lists:keyfind(ip, 1, Opts) of
  74. false -> ListenOpts0;
  75. Ip -> [Ip|ListenOpts0]
  76. end,
  77. ListenOpts =
  78. case lists:keyfind(cacertfile, 1, Opts) of
  79. false -> ListenOpts1;
  80. CACertFile -> [CACertFile|ListenOpts1]
  81. end,
  82. ssl:listen(Port, ListenOpts).
  83. %% @doc Accept an incoming connection on a listen socket.
  84. %%
  85. %% Note that this function does both the transport accept and
  86. %% the SSL handshake.
  87. %%
  88. %% @see ssl:transport_accept/2
  89. %% @see ssl:ssl_accept/2
  90. -spec accept(ssl:sslsocket(), timeout())
  91. -> {ok, ssl:sslsocket()} | {error, closed | timeout | atom()}.
  92. accept(LSocket, Timeout) ->
  93. case ssl:transport_accept(LSocket, Timeout) of
  94. {ok, CSocket} ->
  95. ssl_accept(CSocket, Timeout);
  96. {error, Reason} ->
  97. {error, Reason}
  98. end.
  99. %% @doc Receive a packet from a socket in passive mode.
  100. %% @see ssl:recv/3
  101. -spec recv(ssl:sslsocket(), non_neg_integer(), timeout())
  102. -> {ok, any()} | {error, closed | atom()}.
  103. recv(Socket, Length, Timeout) ->
  104. ssl:recv(Socket, Length, Timeout).
  105. %% @doc Send a packet on a socket.
  106. %% @see ssl:send/2
  107. -spec send(ssl:sslsocket(), iolist()) -> ok | {error, atom()}.
  108. send(Socket, Packet) ->
  109. ssl:send(Socket, Packet).
  110. %% @doc Set one or more options for a socket.
  111. %% @see ssl:setopts/2
  112. -spec setopts(ssl:sslsocket(), list()) -> ok | {error, atom()}.
  113. setopts(Socket, Opts) ->
  114. ssl:setopts(Socket, Opts).
  115. %% @doc Assign a new controlling process <em>Pid</em> to <em>Socket</em>.
  116. %% @see ssl:controlling_process/2
  117. -spec controlling_process(ssl:sslsocket(), pid())
  118. -> ok | {error, closed | not_owner | atom()}.
  119. controlling_process(Socket, Pid) ->
  120. ssl:controlling_process(Socket, Pid).
  121. %% @doc Return the address and port for the other end of a connection.
  122. %% @see ssl:peername/1
  123. -spec peername(ssl:sslsocket())
  124. -> {ok, {inet:ip_address(), inet:ip_port()}} | {error, atom()}.
  125. peername(Socket) ->
  126. ssl:peername(Socket).
  127. %% @doc Close a TCP socket.
  128. %% @see ssl:close/1
  129. -spec close(ssl:sslsocket()) -> ok.
  130. close(Socket) ->
  131. ssl:close(Socket).
  132. %% Internal.
  133. -spec require(list(module())) -> ok.
  134. require([]) ->
  135. ok;
  136. require([App|Tail]) ->
  137. case application:start(App) of
  138. ok -> ok;
  139. {error, {already_started, App}} -> ok
  140. end,
  141. require(Tail).
  142. -spec ssl_accept(ssl:sslsocket(), timeout())
  143. -> {ok, ssl:sslsocket()} | {error, closed | timeout | atom()}.
  144. ssl_accept(Socket, Timeout) ->
  145. case ssl:ssl_accept(Socket, Timeout) of
  146. ok ->
  147. {ok, Socket};
  148. {error, Reason} ->
  149. {error, Reason}
  150. end.