SSL_CTX_set_verify.pod 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. =pod
  2. =head1 NAME
  3. SSL_get_ex_data_X509_STORE_CTX_idx,
  4. SSL_CTX_set_verify, SSL_set_verify,
  5. SSL_CTX_set_verify_depth, SSL_set_verify_depth,
  6. SSL_verify_cb,
  7. SSL_verify_client_post_handshake,
  8. SSL_set_post_handshake_auth,
  9. SSL_CTX_set_post_handshake_auth
  10. - set various SSL/TLS parameters for peer certificate verification
  11. =head1 SYNOPSIS
  12. #include <openssl/ssl.h>
  13. typedef int (*SSL_verify_cb)(int preverify_ok, X509_STORE_CTX *x509_ctx);
  14. void SSL_CTX_set_verify(SSL_CTX *ctx, int mode, SSL_verify_cb verify_callback);
  15. void SSL_set_verify(SSL *ssl, int mode, SSL_verify_cb verify_callback);
  16. SSL_get_ex_data_X509_STORE_CTX_idx(void);
  17. void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth);
  18. void SSL_set_verify_depth(SSL *ssl, int depth);
  19. int SSL_verify_client_post_handshake(SSL *ssl);
  20. void SSL_CTX_set_post_handshake_auth(SSL_CTX *ctx, int val);
  21. void SSL_set_post_handshake_auth(SSL *ssl, int val);
  22. =head1 DESCRIPTION
  23. SSL_CTX_set_verify() sets the verification flags for B<ctx> to be B<mode> and
  24. specifies the B<verify_callback> function to be used. If no callback function
  25. shall be specified, the NULL pointer can be used for B<verify_callback>.
  26. SSL_set_verify() sets the verification flags for B<ssl> to be B<mode> and
  27. specifies the B<verify_callback> function to be used. If no callback function
  28. shall be specified, the NULL pointer can be used for B<verify_callback>. In
  29. this case last B<verify_callback> set specifically for this B<ssl> remains. If
  30. no special B<callback> was set before, the default callback for the underlying
  31. B<ctx> is used, that was valid at the time B<ssl> was created with
  32. L<SSL_new(3)>. Within the callback function,
  33. B<SSL_get_ex_data_X509_STORE_CTX_idx> can be called to get the data index
  34. of the current SSL object that is doing the verification.
  35. In client mode B<verify_callback> may also call the L<SSL_set_retry_verify(3)>
  36. function on the B<SSL> object set in the I<x509_store_ctx> ex data (see
  37. L<SSL_get_ex_data_X509_STORE_CTX_idx(3)>) and return 1.
  38. This would be typically done in case the certificate verification was not yet
  39. able to succeed.
  40. This makes the handshake suspend and return control to the calling application
  41. with B<SSL_ERROR_WANT_RETRY_VERIFY>.
  42. The application can for instance fetch further certificates or cert status
  43. information needed for the verification.
  44. Calling L<SSL_connect(3)> again resumes the connection attempt by retrying the
  45. server certificate verification step.
  46. This process may even be repeated if need be.
  47. Note that the handshake may still be aborted if a subsequent invocation of the
  48. callback (e.g., at a lower depth, or for a separate error condition) returns 0.
  49. SSL_CTX_set_verify_depth() sets the maximum B<depth> for the certificate chain
  50. verification that shall be allowed for B<ctx>.
  51. SSL_set_verify_depth() sets the maximum B<depth> for the certificate chain
  52. verification that shall be allowed for B<ssl>.
  53. SSL_CTX_set_post_handshake_auth() and SSL_set_post_handshake_auth() enable the
  54. Post-Handshake Authentication extension to be added to the ClientHello such that
  55. post-handshake authentication can be requested by the server. If B<val> is 0
  56. then the extension is not sent, otherwise it is. By default the extension is not
  57. sent. A certificate callback will need to be set via
  58. SSL_CTX_set_client_cert_cb() if no certificate is provided at initialization.
  59. SSL_verify_client_post_handshake() causes a CertificateRequest message to be
  60. sent by a server on the given B<ssl> connection. The SSL_VERIFY_PEER flag must
  61. be set; the SSL_VERIFY_POST_HANDSHAKE flag is optional.
  62. =head1 NOTES
  63. The verification of certificates can be controlled by a set of logically
  64. or'ed B<mode> flags:
  65. =over 4
  66. =item SSL_VERIFY_NONE
  67. B<Server mode:> the server will not send a client certificate request to the
  68. client, so the client will not send a certificate.
  69. B<Client mode:> if not using an anonymous cipher (by default disabled), the
  70. server will send a certificate which will be checked. The result of the
  71. certificate verification process can be checked after the TLS/SSL handshake
  72. using the L<SSL_get_verify_result(3)> function.
  73. The handshake will be continued regardless of the verification result.
  74. =item SSL_VERIFY_PEER
  75. B<Server mode:> the server sends a client certificate request to the client.
  76. The certificate returned (if any) is checked. If the verification process
  77. fails, the TLS/SSL handshake is
  78. immediately terminated with an alert message containing the reason for
  79. the verification failure.
  80. The behaviour can be controlled by the additional
  81. SSL_VERIFY_FAIL_IF_NO_PEER_CERT, SSL_VERIFY_CLIENT_ONCE and
  82. SSL_VERIFY_POST_HANDSHAKE flags.
  83. B<Client mode:> the server certificate is verified. If the verification process
  84. fails, the TLS/SSL handshake is
  85. immediately terminated with an alert message containing the reason for
  86. the verification failure. If no server certificate is sent, because an
  87. anonymous cipher is used, SSL_VERIFY_PEER is ignored.
  88. =item SSL_VERIFY_FAIL_IF_NO_PEER_CERT
  89. B<Server mode:> if the client did not return a certificate, the TLS/SSL
  90. handshake is immediately terminated with a "handshake failure" alert.
  91. This flag must be used together with SSL_VERIFY_PEER.
  92. B<Client mode:> ignored (see BUGS)
  93. =item SSL_VERIFY_CLIENT_ONCE
  94. B<Server mode:> only request a client certificate once during the
  95. connection. Do not ask for a client certificate again during
  96. renegotiation or post-authentication if a certificate was requested
  97. during the initial handshake. This flag must be used together with
  98. SSL_VERIFY_PEER.
  99. B<Client mode:> ignored (see BUGS)
  100. =item SSL_VERIFY_POST_HANDSHAKE
  101. B<Server mode:> the server will not send a client certificate request
  102. during the initial handshake, but will send the request via
  103. SSL_verify_client_post_handshake(). This allows the SSL_CTX or SSL
  104. to be configured for post-handshake peer verification before the
  105. handshake occurs. This flag must be used together with
  106. SSL_VERIFY_PEER. TLSv1.3 only; no effect on pre-TLSv1.3 connections.
  107. B<Client mode:> ignored (see BUGS)
  108. =back
  109. If the B<mode> is SSL_VERIFY_NONE none of the other flags may be set.
  110. The actual verification procedure is performed either using the built-in
  111. verification procedure or using another application provided verification
  112. function set with
  113. L<SSL_CTX_set_cert_verify_callback(3)>.
  114. The following descriptions apply in the case of the built-in procedure. An
  115. application provided procedure also has access to the verify depth information
  116. and the verify_callback() function, but the way this information is used
  117. may be different.
  118. SSL_CTX_set_verify_depth() and SSL_set_verify_depth() set a limit on the
  119. number of certificates between the end-entity and trust-anchor certificates.
  120. Neither the
  121. end-entity nor the trust-anchor certificates count against B<depth>. If the
  122. certificate chain needed to reach a trusted issuer is longer than B<depth+2>,
  123. X509_V_ERR_CERT_CHAIN_TOO_LONG will be issued.
  124. The depth count is "level 0:peer certificate", "level 1: CA certificate",
  125. "level 2: higher level CA certificate", and so on. Setting the maximum
  126. depth to 2 allows the levels 0, 1, 2 and 3 (0 being the end-entity and 3 the
  127. trust-anchor).
  128. The default depth limit is 100,
  129. allowing for the peer certificate, at most 100 intermediate CA certificates and
  130. a final trust anchor certificate.
  131. The B<verify_callback> function is used to control the behaviour when the
  132. SSL_VERIFY_PEER flag is set. It must be supplied by the application and
  133. receives two arguments: B<preverify_ok> indicates, whether the verification of
  134. the certificate in question was passed (preverify_ok=1) or not
  135. (preverify_ok=0). B<x509_ctx> is a pointer to the complete context used
  136. for the certificate chain verification.
  137. The certificate chain is checked starting with the deepest nesting level
  138. (the root CA certificate) and worked upward to the peer's certificate.
  139. At each level signatures and issuer attributes are checked. Whenever
  140. a verification error is found, the error number is stored in B<x509_ctx>
  141. and B<verify_callback> is called with B<preverify_ok>=0. By applying
  142. X509_CTX_store_* functions B<verify_callback> can locate the certificate
  143. in question and perform additional steps (see EXAMPLES). If no error is
  144. found for a certificate, B<verify_callback> is called with B<preverify_ok>=1
  145. before advancing to the next level.
  146. The return value of B<verify_callback> controls the strategy of the further
  147. verification process. If B<verify_callback> returns 0, the verification
  148. process is immediately stopped with "verification failed" state. If
  149. SSL_VERIFY_PEER is set, a verification failure alert is sent to the peer and
  150. the TLS/SSL handshake is terminated. If B<verify_callback> returns 1,
  151. the verification process is continued. If B<verify_callback> always returns
  152. 1, the TLS/SSL handshake will not be terminated with respect to verification
  153. failures and the connection will be established. The calling process can
  154. however retrieve the error code of the last verification error using
  155. L<SSL_get_verify_result(3)> or by maintaining its
  156. own error storage managed by B<verify_callback>.
  157. If no B<verify_callback> is specified, the default callback will be used.
  158. Its return value is identical to B<preverify_ok>, so that any verification
  159. failure will lead to a termination of the TLS/SSL handshake with an
  160. alert message, if SSL_VERIFY_PEER is set.
  161. After calling SSL_set_post_handshake_auth(), the client will need to add a
  162. certificate or certificate callback to its configuration before it can
  163. successfully authenticate. This must be called before SSL_connect().
  164. SSL_verify_client_post_handshake() requires that verify flags have been
  165. previously set, and that a client sent the post-handshake authentication
  166. extension. When the client returns a certificate the verify callback will be
  167. invoked. A write operation must take place for the Certificate Request to be
  168. sent to the client, this can be done with SSL_do_handshake() or SSL_write_ex().
  169. Only one certificate request may be outstanding at any time.
  170. When post-handshake authentication occurs, a refreshed NewSessionTicket
  171. message is sent to the client.
  172. =head1 BUGS
  173. In client mode, it is not checked whether the SSL_VERIFY_PEER flag
  174. is set, but whether any flags other than SSL_VERIFY_NONE are set. This can
  175. lead to unexpected behaviour if SSL_VERIFY_PEER and other flags are not used as
  176. required.
  177. =head1 RETURN VALUES
  178. The SSL*_set_verify*() functions do not provide diagnostic information.
  179. The SSL_verify_client_post_handshake() function returns 1 if the request
  180. succeeded, and 0 if the request failed. The error stack can be examined
  181. to determine the failure reason.
  182. =head1 EXAMPLES
  183. The following code sequence realizes an example B<verify_callback> function
  184. that will always continue the TLS/SSL handshake regardless of verification
  185. failure, if wished. The callback realizes a verification depth limit with
  186. more informational output.
  187. All verification errors are printed; information about the certificate chain
  188. is printed on request.
  189. The example is realized for a server that does allow but not require client
  190. certificates.
  191. The example makes use of the ex_data technique to store application data
  192. into/retrieve application data from the SSL structure
  193. (see L<CRYPTO_get_ex_new_index(3)>,
  194. L<SSL_get_ex_data_X509_STORE_CTX_idx(3)>).
  195. ...
  196. typedef struct {
  197. int verbose_mode;
  198. int verify_depth;
  199. int always_continue;
  200. } mydata_t;
  201. int mydata_index;
  202. ...
  203. static int verify_callback(int preverify_ok, X509_STORE_CTX *ctx)
  204. {
  205. char buf[256];
  206. X509 *err_cert;
  207. int err, depth;
  208. SSL *ssl;
  209. mydata_t *mydata;
  210. err_cert = X509_STORE_CTX_get_current_cert(ctx);
  211. err = X509_STORE_CTX_get_error(ctx);
  212. depth = X509_STORE_CTX_get_error_depth(ctx);
  213. /*
  214. * Retrieve the pointer to the SSL of the connection currently treated
  215. * and the application specific data stored into the SSL object.
  216. */
  217. ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
  218. mydata = SSL_get_ex_data(ssl, mydata_index);
  219. X509_NAME_oneline(X509_get_subject_name(err_cert), buf, 256);
  220. /*
  221. * Catch a too long certificate chain. The depth limit set using
  222. * SSL_CTX_set_verify_depth() is by purpose set to "limit+1" so
  223. * that whenever the "depth>verify_depth" condition is met, we
  224. * have violated the limit and want to log this error condition.
  225. * We must do it here, because the CHAIN_TOO_LONG error would not
  226. * be found explicitly; only errors introduced by cutting off the
  227. * additional certificates would be logged.
  228. */
  229. if (depth > mydata->verify_depth) {
  230. preverify_ok = 0;
  231. err = X509_V_ERR_CERT_CHAIN_TOO_LONG;
  232. X509_STORE_CTX_set_error(ctx, err);
  233. }
  234. if (!preverify_ok) {
  235. printf("verify error:num=%d:%s:depth=%d:%s\n", err,
  236. X509_verify_cert_error_string(err), depth, buf);
  237. } else if (mydata->verbose_mode) {
  238. printf("depth=%d:%s\n", depth, buf);
  239. }
  240. /*
  241. * At this point, err contains the last verification error. We can use
  242. * it for something special
  243. */
  244. if (!preverify_ok && (err == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT)) {
  245. X509_NAME_oneline(X509_get_issuer_name(err_cert), buf, 256);
  246. printf("issuer= %s\n", buf);
  247. }
  248. if (mydata->always_continue)
  249. return 1;
  250. else
  251. return preverify_ok;
  252. }
  253. ...
  254. mydata_t mydata;
  255. ...
  256. mydata_index = SSL_get_ex_new_index(0, "mydata index", NULL, NULL, NULL);
  257. ...
  258. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE,
  259. verify_callback);
  260. /*
  261. * Let the verify_callback catch the verify_depth error so that we get
  262. * an appropriate error in the logfile.
  263. */
  264. SSL_CTX_set_verify_depth(verify_depth + 1);
  265. /*
  266. * Set up the SSL specific data into "mydata" and store it into th SSL
  267. * structure.
  268. */
  269. mydata.verify_depth = verify_depth; ...
  270. SSL_set_ex_data(ssl, mydata_index, &mydata);
  271. ...
  272. SSL_accept(ssl); /* check of success left out for clarity */
  273. if (peer = SSL_get_peer_certificate(ssl)) {
  274. if (SSL_get_verify_result(ssl) == X509_V_OK) {
  275. /* The client sent a certificate which verified OK */
  276. }
  277. }
  278. =head1 SEE ALSO
  279. L<ssl(7)>, L<SSL_new(3)>,
  280. L<SSL_CTX_get_verify_mode(3)>,
  281. L<SSL_get_verify_result(3)>,
  282. L<SSL_CTX_load_verify_locations(3)>,
  283. L<SSL_get_peer_certificate(3)>,
  284. L<SSL_CTX_set_cert_verify_callback(3)>,
  285. L<SSL_get_ex_data_X509_STORE_CTX_idx(3)>,
  286. L<SSL_CTX_set_client_cert_cb(3)>,
  287. L<CRYPTO_get_ex_new_index(3)>
  288. =head1 HISTORY
  289. The SSL_VERIFY_POST_HANDSHAKE option, and the SSL_verify_client_post_handshake()
  290. and SSL_set_post_handshake_auth() functions were added in OpenSSL 1.1.1.
  291. =head1 COPYRIGHT
  292. Copyright 2000-2022 The OpenSSL Project Authors. All Rights Reserved.
  293. Licensed under the Apache License 2.0 (the "License"). You may not use
  294. this file except in compliance with the License. You can obtain a copy
  295. in the file LICENSE in the source distribution or at
  296. L<https://www.openssl.org/source/license.html>.
  297. =cut