1/*  Part of SWI-Prolog
2
3    Author:        Jan van der Steen and Jan Wielemaker
4    E-mail:        J.van.der.Steen@diff.nl and jan@swi.psy.uva.nl
5    WWW:           http://www.swi-prolog.org
6    Copyright (c)  2004-2011, SWI-Prolog Foundation
7    All rights reserved.
8
9    Redistribution and use in source and binary forms, with or without
10    modification, are permitted provided that the following conditions
11    are met:
12
13    1. Redistributions of source code must retain the above copyright
14       notice, this list of conditions and the following disclaimer.
15
16    2. Redistributions in binary form must reproduce the above copyright
17       notice, this list of conditions and the following disclaimer in
18       the documentation and/or other materials provided with the
19       distribution.
20
21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32    POSSIBILITY OF SUCH DAMAGE.
33*/
34
35:- module(client,
36          [ client/0
37          ]).
38:- autoload(library(readutil),[read_line_to_codes/2]).
39:- autoload(library(socket),[tcp_connect/3]).
40:- autoload(library(ssl),[ssl_context/3,ssl_negotiate/5]).
41
42client :-
43    ssl_context(client, SSL,
44             [ host('localhost'),
45               cert_verify_hook(cert_verify),
46               cacert_file('etc/demoCA/cacert.pem'),
47               certificate_file('etc/client/client-cert.pem'),
48               key_file('etc/client/client-key.pem'),
49               close_parent(true),
50               close_notify(true),
51%                  password('apenoot2'),
52               pem_password_hook(get_client_pwd)
53             ]),
54    Port = 1111,
55    tcp_connect(localhost:Port, StreamPair, []),
56    stream_pair(StreamPair, Read, Write),
57    catch(ssl_negotiate(SSL, Read, Write, SSLRead, SSLWrite),
58          E,
59          ( close(StreamPair), throw(E))),
60    client_loop(SSLRead, SSLWrite).
61
62client_loop(In, Out) :-
63    write_server(In, Out),
64    write_server(In, Out),
65    write_server(In, Out),
66    call_cleanup(close(In), close(Out)).
67
68write_server(In, Out) :-
69    format(Out, 'Hello~n', ''),
70    flush_output(Out),
71    read_line_to_codes(In, Line),
72    (   Line == end_of_file
73    ->  true
74    ;   format('Got ~s~n', [Line])
75    ).
76
77user:get_client_pwd(_SSL, apenoot2) :-
78    format('Returning password from client passwd hook~n').
79
80cert_verify(_SSL, Certificate, _AllCerts, _FirstCert, Error) :-
81    format('Handling detailed certificate verification~n'),
82    format('Certificate: ~w, error: ~w~n', [Certificate, Error]),
83    format('Client accepts the server certificate~n').
84