1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
|
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: J.Wielemaker@vu.nl
WWW: http://www.swi-prolog.org
Copyright (c) 2007-2022, University of Amsterdam
VU University Amsterdam
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
:- module(http_ssl_plugin, []).
% Requires ssl:upgrade_legacy_options/2 hook
:- use_module(library(ssl),
[ ssl_context/3,
ssl_secure_ciphers/1,
ssl_property/2,
ssl_set_options/3,
ssl_negotiate/5
]).
:- use_module(library(debug),[debug/3]).
:- use_module(library(socket),
[ tcp_socket/1,
tcp_setopt/2,
tcp_bind/2,
tcp_listen/2,
tcp_accept/3,
tcp_open_socket/3,
tcp_connect/3
]).
:- autoload(library(lists),[select/3]).
:- autoload(library(option),[option/2,option/3]).
:- autoload(library(apply), [include/3]).
:- autoload(library(http/http_header),[http_read_reply_header/2]).
:- autoload(library(http/thread_httpd),[http_enough_workers/3]).
/** <module> SSL plugin for HTTP libraries
This module can be loaded next to library(thread_httpd) and
library(http_open) to provide secure HTTP (HTTPS) services and client
access.
An example secure server using self-signed certificates can be found in
the <plbase>/doc/packages/examples/ssl/https.pl, where <plbase> is the
SWI-Prolog installation directory.
*/
:- multifile
thread_httpd:make_socket_hook/3,
thread_httpd:accept_hook/2,
thread_httpd:open_client_hook/6,
thread_httpd:discard_client_hook/1,
http:http_protocol_hook/5,
http:open_options/2,
http:http_connection_over_proxy/6,
http:ssl_server_create_hook/3,
http:ssl_server_open_client_hook/3.
/*******************************
* SERVER HOOKS *
*******************************/
%! thread_httpd:make_socket_hook(?Port, :OptionsIn, -OptionsOut)
%! is semidet.
%
% Hook into http_server/2 to create an SSL server if the option
% ssl(SSLOptions) is provided.
%
% @see thread_httpd:accept_hook/2 handles the corresponding accept
thread_httpd:make_socket_hook(Port, M:Options0, Options) :-
select(ssl(SSLOptions0), Options0, Options1),
!,
add_secure_ciphers(SSLOptions0, SSLOptions1),
disable_sslv3(SSLOptions1, SSLOptions),
make_socket(Port, Socket, Options1),
ssl_context(server, SSL0, M:[close_parent(true)|SSLOptions]),
( http:ssl_server_create_hook(SSL0, SSL1, Options1)
-> ensure_close_parent(SSL1, SSL)
; SSL = SSL0
),
port(Port, PortNum),
atom_concat('httpsd', PortNum, Queue),
Options = [ queue(Queue),
tcp_socket(Socket),
ssl_instance(SSL)
| Options1
].
port(_Host:Port0, Port) => Port = Port0.
port(Port0, Port), integer(Port0) => Port = Port0.
ensure_close_parent(SSL0, SSL) :-
( ssl_property(SSL0, close_parent(true))
-> SSL = SSL0
; ssl_set_options(SSL0, SSL, [close_parent(true)])
).
%! add_secure_ciphers(+SSLOptions0, -SSLOptions)
%
% Add ciphers from ssl_secure_ciphers/1 if no ciphers are provided.
add_secure_ciphers(SSLOptions0, SSLOptions) :-
( option(cipher_list(_), SSLOptions0)
-> SSLOptions = SSLOptions0
; ssl_secure_ciphers(Ciphers),
SSLOptions = [cipher_list(Ciphers)|SSLOptions0]
).
%! disable_sslv3(+SSLOptions0, -SSLOptions)
%
% Disable SSLv3, which is considered insecure unless the caller
% specifies the allowed versions explicitly, so we assume s/he knows
% what s/he is doing.
disable_sslv3(SSLOptions0, SSLOptions) :-
( option(min_protocol_version(_), SSLOptions0)
; option(disable_ssl_methods(_), SSLOptions0)
),
!,
SSLOptions = SSLOptions0.
disable_sslv3(SSLOptions0,
[ disable_ssl_methods([sslv3,sslv23]), % old OpenSSL versions
min_protocol_version(tlsv1) % OpenSSL 1.1.0 and later
| SSLOptions0
]).
make_socket(_Port, Socket, Options) :-
option(tcp_socket(Socket), Options),
!.
make_socket(Port, Socket, _Options) :-
tcp_socket(Socket),
tcp_setopt(Socket, reuseaddr),
tcp_bind(Socket, Port),
tcp_listen(Socket, 5).
%! thread_httpd:accept_hook(:Goal, +Options) is semidet.
%
% Implement the accept for HTTPS connections.
thread_httpd:accept_hook(Goal, Options) :-
memberchk(ssl_instance(SSL0), Options),
!,
ensure_close_parent(SSL0, SSL),
memberchk(queue(Queue), Options),
memberchk(tcp_socket(Socket), Options),
tcp_accept(Socket, Client, Peer),
sig_atomic(send_to_worker(Queue, SSL, Client, Goal, Peer)),
http_enough_workers(Queue, accept, Peer).
send_to_worker(Queue, SSL, Client, Goal, Peer) :-
debug(http(connection), 'New HTTPS connection from ~p', [Peer]),
thread_send_message(Queue, ssl_client(SSL, Client, Goal, Peer)).
%! thread_httpd:discard_client_hook(+Msg)
%
% Handles connections that where accepted during server shutdown.
thread_httpd:discard_client_hook(ssl_client(_SSL, Client, _Goal, _Peer)) :-
tcp_close_socket(Client).
%! http:ssl_server_create_hook(+SSL0, -SSL, +Options) is semidet.
%
% Extensible predicate that is called once after creating an HTTPS
% server. If this predicate succeeds, SSL is the context that is used
% for negotiating new connections. Otherwise, SSL0 is used, which is
% the context that was created with the given options.
%
% @see ssl_context/3 for creating an SSL context
%! http:ssl_server_open_client_hook(+SSL0, -SSL, +Options) is semidet.
%
% Extensible predicate that is called before each connection that the
% server negotiates with a client. If this predicate succeeds, SSL is
% the context that is used for the new connection. Otherwise, SSL0 is
% used, which is the context that was created when launching the
% server.
%
% @see ssl_context/3 for creating an SSL context
thread_httpd:open_client_hook(ssl_client(SSL0, Client, Goal, Peer),
Goal, In, Out,
[peer(Peer), protocol(https)],
Options) :-
( http:ssl_server_open_client_hook(SSL0, SSL, Options)
-> true
; SSL = SSL0
),
option(timeout(TMO), Options, 60),
tcp_open_socket(Client, Read, Write),
set_stream(Read, timeout(TMO)),
set_stream(Write, timeout(TMO)),
catch(ssl_negotiate(SSL, Read, Write, In, Out),
E,
ssl_failed(Read, Write, E)).
ssl_failed(Read, Write, E) :-
close(Write, [force(true)]),
close(Read, [force(true)]),
throw(E).
/*******************************
* CLIENT HOOKS *
*******************************/
%! http:http_protocol_hook(+Scheme, +Parts, +PlainStreamPair,
%! -StreamPair, +Options) is semidet.
%
% Hook for http_open/3 to connect to an HTTPS (SSL-based HTTP)
% server.
http:http_protocol_hook(https, Parts, PlainStreamPair, StreamPair, Options) :-
ssl_protocol_hook(Parts, PlainStreamPair, StreamPair, Options).
http:http_protocol_hook(wss, Parts, PlainStreamPair, StreamPair, Options) :-
ssl_protocol_hook(Parts, PlainStreamPair, StreamPair, Options).
ssl_protocol_hook(Parts, PlainStreamPair, StreamPair, Options) :-
memberchk(host(Host), Parts),
include(ssl_option, Options, SSLOptions),
ssl_context(client, SSL, [ host(Host),
close_parent(true)
| SSLOptions
]),
stream_pair(PlainStreamPair, PlainIn, PlainOut),
% if an exception arises, http_open/3 closes the stream for us
ssl_negotiate(SSL, PlainIn, PlainOut, In, Out),
stream_pair(StreamPair, In, Out).
% Might be better to be more selective, but passing the options from
% http_open/3 with more than 1 argument makes ssl_context/3 fail.
ssl_option(Term) :-
compound(Term),
compound_name_arity(Term, _, 1).
%! http:http_connection_over_proxy(+Proxy, +Parts, +HostPort, -StreamPair,
%! +OptionsIn, -OptionsOut)
%
% Facilitate an HTTPS connection via a proxy using HTTP CONNECT.
% Note that most proxies will only support this for connecting on
% port 443
http:http_connection_over_proxy(proxy(ProxyHost, ProxyPort), Parts,
Host:Port, StreamPair, Options, Options) :-
memberchk(scheme(https), Parts),
!,
tcp_connect(ProxyHost:ProxyPort, StreamPair, [bypass_proxy(true)]),
catch(negotiate_http_connect(StreamPair, Host:Port),
Error,
( close(StreamPair, [force(true)]),
throw(Error)
)).
negotiate_http_connect(StreamPair, Address):-
format(StreamPair, 'CONNECT ~w HTTP/1.1\r\n\r\n', [Address]),
flush_output(StreamPair),
http_read_reply_header(StreamPair, Header),
memberchk(status(_, Status, Message), Header),
( Status == ok
-> true
; throw(error(proxy_rejection(Message), _))
).
|