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