2
0

SSL_CTX_set_verify.pod 14 KB

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