7e8fffef8180835960b4f6265258697592ec0e34
[openssl.git] / doc / man3 / SSL_CTX_set_psk_client_callback.pod
1 =pod
2
3 =head1 NAME
4
5 SSL_psk_client_cb_func,
6 SSL_psk_use_session_cb_func,
7 SSL_CTX_set_psk_client_callback,
8 SSL_set_psk_client_callback,
9 SSL_CTX_set_psk_use_session_callback,
10 SSL_set_psk_use_session_callback
11 - set PSK client callback
12
13 =head1 SYNOPSIS
14
15  #include <openssl/ssl.h>
16
17  typedef unsigned int (*SSL_psk_client_cb_func)(SSL *ssl,
18                                                 const char *hint,
19                                                 char *identity,
20                                                 unsigned int max_identity_len,
21                                                 unsigned char *psk,
22                                                 unsigned int max_psk_len);
23  typedef int (*SSL_psk_use_session_cb_func)(SSL *ssl, const EVP_MD *md,
24                                             const unsigned char **id,
25                                             size_t *idlen,
26                                             SSL_SESSION **sess);
27
28  void SSL_CTX_set_psk_client_callback(SSL_CTX *ctx, SSL_psk_client_cb_func cb);
29  void SSL_set_psk_client_callback(SSL *ssl, SSL_psk_client_cb_func cb);
30
31  void SSL_CTX_set_psk_use_session_callback(SSL_CTX *ctx,
32                                            SSL_psk_use_session_cb_func cb);
33  void SSL_set_psk_use_session_callback(SSL *s, SSL_psk_use_session_cb_func cb);
34
35 =head1 DESCRIPTION
36
37 TLSv1.3 Pre-Shared Keys (PSKs) and PSKs for TLSv1.2 and below are not
38 compatible.
39
40 A client application wishing to use PSK ciphersuites for TLSv1.2 and below must
41 provide a callback function which is called when the client is sending the
42 ClientKeyExchange message to the server.
43
44 The purpose of the callback function is to select the PSK identity and
45 the pre-shared key to use during the connection setup phase.
46
47 The callback is set using functions SSL_CTX_set_psk_client_callback()
48 or SSL_set_psk_client_callback(). The callback function is given the
49 connection in parameter B<ssl>, a B<NULL>-terminated PSK identity hint
50 sent by the server in parameter B<hint>, a buffer B<identity> of
51 length B<max_identity_len> bytes where the resulting
52 B<NULL>-terminated identity is to be stored, and a buffer B<psk> of
53 length B<max_psk_len> bytes where the resulting pre-shared key is to
54 be stored.
55
56 A client application wishing to use TLSv1.3 PSKs must set a different callback
57 using either SSL_CTX_set_psk_use_session_callback() or
58 SSL_set_psk_use_session_callback() as appropriate.
59
60 The callback function is given a reference to the SSL connection in B<ssl>.
61
62 The first time the callback is called for a connection the B<md> parameter is
63 NULL. In some circumstances the callback will be called a second time. In that
64 case the server will have specified a ciphersuite to use already and the PSK
65 must be compatible with the digest for that ciphersuite. The digest will be
66 given in B<md>. The PSK returned by the callback is allowed to be different
67 between the first and second time it is called.
68
69 On successful completion the callback must store a pointer to an identifier for
70 the PSK in B<*id>. The identifier length in bytes should be stored in B<*idlen>.
71 The memory pointed to by B<*id> remains owned by the application and should
72 be freed by it as required at any point after the handshake is complete.
73
74 Additionally the callback should store a reference to an SSL_SESSION object in
75 B<*sess>. This is used as the basis for the PSK, and should, at a minimum, have
76 the following fields set:
77
78 =over 4
79
80 =item The master key
81
82 This can be set via a call to L<SSL_SESSION_set1_master_key(3)>.
83
84 =item A ciphersuite
85
86 Only the handshake digest associated with the ciphersuite is relevant for the
87 PSK (the server may go on to negotiate any ciphersuite which is compatible with
88 the digest). The application can use any TLSv1.3 ciphersuite. Where B<md> is
89 non-NULL the handshake digest for the ciphersuite should be the same.
90 The ciphersuite can be set via a call to <SSL_SESSION_set_cipher(3)>. The
91 handshake digest of an SSL_CIPHER object can be checked using
92 <SSL_CIPHER_get_handshake_digest(3)>.
93
94 =item The protocol version
95
96 This can be set via a call to L<SSL_SESSION_set_protocol_version> and should be
97 TLS1_3_VERSION.
98
99 =back
100
101 Alternatively an SSL_SESSION created from a previous non-PSK handshake may also
102 be used as the basis for a PSK.
103
104 Ownership of the SSL_SESSION object is passed to the OpenSSL library and so it
105 should not be freed by the application.
106
107 It is also possible for the callback to succeed but not supply a PSK. In this
108 case no PSK will be sent to the server but the handshake will continue. To do
109 this the callback should return successfully and ensure that B<*sess> is
110 NULL. The contents of B<*id> and B<*idlen> will be ignored.
111
112 =head1 NOTES
113
114 Note that parameter B<hint> given to the callback may be B<NULL>.
115
116 A connection established via a TLSv1.3 PSK will appear as if session resumption
117 has occurred so that L<SSL_session_reused(3)> will return true.
118
119 =head1 RETURN VALUES
120
121 Return values from the SSL_psk_client_cb_func callback are interpreted as
122 follows:
123
124 On success (callback found a PSK identity and a pre-shared key to use)
125 the length (> 0) of B<psk> in bytes is returned.
126
127 Otherwise or on errors the callback should return 0. In this case
128 the connection setup fails.
129
130 The SSL_psk_use_session_cb_func callback should return 1 on success or 0 on
131 failure. In the event of failure the connection setup fails.
132
133 =head1 COPYRIGHT
134
135 Copyright 2006-2017 The OpenSSL Project Authors. All Rights Reserved.
136
137 Licensed under the OpenSSL license (the "License").  You may not use
138 this file except in compliance with the License.  You can obtain a copy
139 in the file LICENSE in the source distribution or at
140 L<https://www.openssl.org/source/license.html>.
141
142 =cut