SSL_CTX_set_verify.3ossl 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. .\" -*- mode: troff; coding: utf-8 -*-
  2. .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43)
  3. .\"
  4. .\" Standard preamble:
  5. .\" ========================================================================
  6. .de Sp \" Vertical space (when we can't use .PP)
  7. .if t .sp .5v
  8. .if n .sp
  9. ..
  10. .de Vb \" Begin verbatim text
  11. .ft CW
  12. .nf
  13. .ne \\$1
  14. ..
  15. .de Ve \" End verbatim text
  16. .ft R
  17. .fi
  18. ..
  19. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
  20. .ie n \{\
  21. . ds C` ""
  22. . ds C' ""
  23. 'br\}
  24. .el\{\
  25. . ds C`
  26. . ds C'
  27. 'br\}
  28. .\"
  29. .\" Escape single quotes in literal strings from groff's Unicode transform.
  30. .ie \n(.g .ds Aq \(aq
  31. .el .ds Aq '
  32. .\"
  33. .\" If the F register is >0, we'll generate index entries on stderr for
  34. .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
  35. .\" entries marked with X<> in POD. Of course, you'll have to process the
  36. .\" output yourself in some meaningful fashion.
  37. .\"
  38. .\" Avoid warning from groff about undefined register 'F'.
  39. .de IX
  40. ..
  41. .nr rF 0
  42. .if \n(.g .if rF .nr rF 1
  43. .if (\n(rF:(\n(.g==0)) \{\
  44. . if \nF \{\
  45. . de IX
  46. . tm Index:\\$1\t\\n%\t"\\$2"
  47. ..
  48. . if !\nF==2 \{\
  49. . nr % 0
  50. . nr F 2
  51. . \}
  52. . \}
  53. .\}
  54. .rr rF
  55. .\" ========================================================================
  56. .\"
  57. .IX Title "SSL_CTX_SET_VERIFY 3ossl"
  58. .TH SSL_CTX_SET_VERIFY 3ossl 2025-01-17 3.4.0 OpenSSL
  59. .\" For nroff, turn off justification. Always turn off hyphenation; it makes
  60. .\" way too many mistakes in technical documents.
  61. .if n .ad l
  62. .nh
  63. .SH NAME
  64. SSL_get_ex_data_X509_STORE_CTX_idx,
  65. SSL_CTX_set_verify, SSL_set_verify,
  66. SSL_CTX_set_verify_depth, SSL_set_verify_depth,
  67. SSL_verify_cb,
  68. SSL_verify_client_post_handshake,
  69. SSL_set_post_handshake_auth,
  70. SSL_CTX_set_post_handshake_auth
  71. \&\- set various SSL/TLS parameters for peer certificate verification
  72. .SH SYNOPSIS
  73. .IX Header "SYNOPSIS"
  74. .Vb 1
  75. \& #include <openssl/ssl.h>
  76. \&
  77. \& typedef int (*SSL_verify_cb)(int preverify_ok, X509_STORE_CTX *x509_ctx);
  78. \&
  79. \& void SSL_CTX_set_verify(SSL_CTX *ctx, int mode, SSL_verify_cb verify_callback);
  80. \& void SSL_set_verify(SSL *ssl, int mode, SSL_verify_cb verify_callback);
  81. \& SSL_get_ex_data_X509_STORE_CTX_idx(void);
  82. \&
  83. \& void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth);
  84. \& void SSL_set_verify_depth(SSL *ssl, int depth);
  85. \&
  86. \& int SSL_verify_client_post_handshake(SSL *ssl);
  87. \& void SSL_CTX_set_post_handshake_auth(SSL_CTX *ctx, int val);
  88. \& void SSL_set_post_handshake_auth(SSL *ssl, int val);
  89. .Ve
  90. .SH DESCRIPTION
  91. .IX Header "DESCRIPTION"
  92. \&\fBSSL_CTX_set_verify()\fR sets the verification flags for \fBctx\fR to be \fBmode\fR and
  93. specifies the \fBverify_callback\fR function to be used. If no callback function
  94. shall be specified, the NULL pointer can be used for \fBverify_callback\fR.
  95. .PP
  96. \&\fBSSL_set_verify()\fR sets the verification flags for \fBssl\fR to be \fBmode\fR and
  97. specifies the \fBverify_callback\fR function to be used. If no callback function
  98. shall be specified, the NULL pointer can be used for \fBverify_callback\fR. In
  99. this case last \fBverify_callback\fR set specifically for this \fBssl\fR remains. If
  100. no special \fBcallback\fR was set before, the default callback for the underlying
  101. \&\fBctx\fR is used, that was valid at the time \fBssl\fR was created with
  102. \&\fBSSL_new\fR\|(3). Within the callback function,
  103. \&\fBSSL_get_ex_data_X509_STORE_CTX_idx\fR can be called to get the data index
  104. of the current SSL object that is doing the verification.
  105. .PP
  106. In client mode \fBverify_callback\fR may also call the \fBSSL_set_retry_verify\fR\|(3)
  107. function on the \fBSSL\fR object set in the \fIx509_store_ctx\fR ex data (see
  108. \&\fBSSL_get_ex_data_X509_STORE_CTX_idx\fR\|(3)) and return 1.
  109. This would be typically done in case the certificate verification was not yet
  110. able to succeed.
  111. This makes the handshake suspend and return control to the calling application
  112. with \fBSSL_ERROR_WANT_RETRY_VERIFY\fR.
  113. The application can for instance fetch further certificates or cert status
  114. information needed for the verification.
  115. Calling \fBSSL_connect\fR\|(3) again resumes the connection attempt by retrying the
  116. server certificate verification step.
  117. This process may even be repeated if need be.
  118. Note that the handshake may still be aborted if a subsequent invocation of the
  119. callback (e.g., at a lower depth, or for a separate error condition) returns 0.
  120. .PP
  121. \&\fBSSL_CTX_set_verify_depth()\fR sets the maximum \fBdepth\fR for the certificate chain
  122. verification that shall be allowed for \fBctx\fR.
  123. .PP
  124. \&\fBSSL_set_verify_depth()\fR sets the maximum \fBdepth\fR for the certificate chain
  125. verification that shall be allowed for \fBssl\fR.
  126. .PP
  127. \&\fBSSL_CTX_set_post_handshake_auth()\fR and \fBSSL_set_post_handshake_auth()\fR enable the
  128. Post-Handshake Authentication extension to be added to the ClientHello such that
  129. post-handshake authentication can be requested by the server. If \fBval\fR is 0
  130. then the extension is not sent, otherwise it is. By default the extension is not
  131. sent. A certificate callback will need to be set via
  132. \&\fBSSL_CTX_set_client_cert_cb()\fR if no certificate is provided at initialization.
  133. .PP
  134. \&\fBSSL_verify_client_post_handshake()\fR causes a CertificateRequest message to be
  135. sent by a server on the given \fBssl\fR connection. The SSL_VERIFY_PEER flag must
  136. be set; the SSL_VERIFY_POST_HANDSHAKE flag is optional.
  137. .SH NOTES
  138. .IX Header "NOTES"
  139. The verification of certificates can be controlled by a set of logically
  140. or'ed \fBmode\fR flags:
  141. .IP SSL_VERIFY_NONE 4
  142. .IX Item "SSL_VERIFY_NONE"
  143. \&\fBServer mode:\fR the server will not send a client certificate request to the
  144. client, so the client will not send a certificate.
  145. .Sp
  146. \&\fBClient mode:\fR if not using an anonymous cipher (by default disabled), the
  147. server will send a certificate which will be checked. The result of the
  148. certificate verification process can be checked after the TLS/SSL handshake
  149. using the \fBSSL_get_verify_result\fR\|(3) function.
  150. The handshake will be continued regardless of the verification result.
  151. .IP SSL_VERIFY_PEER 4
  152. .IX Item "SSL_VERIFY_PEER"
  153. \&\fBServer mode:\fR the server sends a client certificate request to the client.
  154. The certificate returned (if any) is checked. If the verification process
  155. fails, the TLS/SSL handshake is
  156. immediately terminated with an alert message containing the reason for
  157. the verification failure.
  158. The behaviour can be controlled by the additional
  159. SSL_VERIFY_FAIL_IF_NO_PEER_CERT, SSL_VERIFY_CLIENT_ONCE and
  160. SSL_VERIFY_POST_HANDSHAKE flags.
  161. .Sp
  162. \&\fBClient mode:\fR the server certificate is verified. If the verification process
  163. fails, the TLS/SSL handshake is
  164. immediately terminated with an alert message containing the reason for
  165. the verification failure. If no server certificate is sent, because an
  166. anonymous cipher is used, SSL_VERIFY_PEER is ignored.
  167. .IP SSL_VERIFY_FAIL_IF_NO_PEER_CERT 4
  168. .IX Item "SSL_VERIFY_FAIL_IF_NO_PEER_CERT"
  169. \&\fBServer mode:\fR if the client did not return a certificate, the TLS/SSL
  170. handshake is immediately terminated with a "handshake failure" alert.
  171. This flag must be used together with SSL_VERIFY_PEER.
  172. .Sp
  173. \&\fBClient mode:\fR ignored (see BUGS)
  174. .IP SSL_VERIFY_CLIENT_ONCE 4
  175. .IX Item "SSL_VERIFY_CLIENT_ONCE"
  176. \&\fBServer mode:\fR only request a client certificate once during the
  177. connection. Do not ask for a client certificate again during
  178. renegotiation or post-authentication if a certificate was requested
  179. during the initial handshake. This flag must be used together with
  180. SSL_VERIFY_PEER.
  181. .Sp
  182. \&\fBClient mode:\fR ignored (see BUGS)
  183. .IP SSL_VERIFY_POST_HANDSHAKE 4
  184. .IX Item "SSL_VERIFY_POST_HANDSHAKE"
  185. \&\fBServer mode:\fR the server will not send a client certificate request
  186. during the initial handshake, but will send the request via
  187. \&\fBSSL_verify_client_post_handshake()\fR. This allows the SSL_CTX or SSL
  188. to be configured for post-handshake peer verification before the
  189. handshake occurs. This flag must be used together with
  190. SSL_VERIFY_PEER. TLSv1.3 only; no effect on pre\-TLSv1.3 connections.
  191. .Sp
  192. \&\fBClient mode:\fR ignored (see BUGS)
  193. .PP
  194. If the \fBmode\fR is SSL_VERIFY_NONE none of the other flags may be set.
  195. .PP
  196. If verification flags are not modified explicitly by \f(CWSSL_CTX_set_verify()\fR
  197. or \f(CWSSL_set_verify()\fR, the default value will be SSL_VERIFY_NONE.
  198. .PP
  199. The actual verification procedure is performed either using the built-in
  200. verification procedure or using another application provided verification
  201. function set with
  202. \&\fBSSL_CTX_set_cert_verify_callback\fR\|(3).
  203. The following descriptions apply in the case of the built-in procedure. An
  204. application provided procedure also has access to the verify depth information
  205. and the \fBverify_callback()\fR function, but the way this information is used
  206. may be different.
  207. .PP
  208. \&\fBSSL_CTX_set_verify_depth()\fR and \fBSSL_set_verify_depth()\fR set a limit on the
  209. number of certificates between the end-entity and trust-anchor certificates.
  210. Neither the
  211. end-entity nor the trust-anchor certificates count against \fBdepth\fR. If the
  212. certificate chain needed to reach a trusted issuer is longer than \fBdepth+2\fR,
  213. X509_V_ERR_CERT_CHAIN_TOO_LONG will be issued.
  214. The depth count is "level 0:peer certificate", "level 1: CA certificate",
  215. "level 2: higher level CA certificate", and so on. Setting the maximum
  216. depth to 2 allows the levels 0, 1, 2 and 3 (0 being the end-entity and 3 the
  217. trust-anchor).
  218. The default depth limit is 100,
  219. allowing for the peer certificate, at most 100 intermediate CA certificates and
  220. a final trust anchor certificate.
  221. .PP
  222. The \fBverify_callback\fR function is used to control the behaviour when the
  223. SSL_VERIFY_PEER flag is set. It must be supplied by the application and
  224. receives two arguments: \fBpreverify_ok\fR indicates, whether the verification of
  225. the certificate in question was passed (preverify_ok=1) or not
  226. (preverify_ok=0). \fBx509_ctx\fR is a pointer to the complete context used
  227. for the certificate chain verification.
  228. .PP
  229. The certificate chain is checked starting with the deepest nesting level
  230. (the root CA certificate) and worked upward to the peer's certificate.
  231. At each level signatures and issuer attributes are checked. Whenever
  232. a verification error is found, the error number is stored in \fBx509_ctx\fR
  233. and \fBverify_callback\fR is called with \fBpreverify_ok\fR=0. By applying
  234. X509_CTX_store_* functions \fBverify_callback\fR can locate the certificate
  235. in question and perform additional steps (see EXAMPLES). If no error is
  236. found for a certificate, \fBverify_callback\fR is called with \fBpreverify_ok\fR=1
  237. before advancing to the next level.
  238. .PP
  239. The return value of \fBverify_callback\fR controls the strategy of the further
  240. verification process. If \fBverify_callback\fR returns 0, the verification
  241. process is immediately stopped with "verification failed" state. If
  242. SSL_VERIFY_PEER is set, a verification failure alert is sent to the peer and
  243. the TLS/SSL handshake is terminated. If \fBverify_callback\fR returns 1,
  244. the verification process is continued. If \fBverify_callback\fR always returns
  245. 1, the TLS/SSL handshake will not be terminated with respect to verification
  246. failures and the connection will be established. The calling process can
  247. however retrieve the error code of the last verification error using
  248. \&\fBSSL_get_verify_result\fR\|(3) or by maintaining its
  249. own error storage managed by \fBverify_callback\fR.
  250. .PP
  251. If no \fBverify_callback\fR is specified, the default callback will be used.
  252. Its return value is identical to \fBpreverify_ok\fR, so that any verification
  253. failure will lead to a termination of the TLS/SSL handshake with an
  254. alert message, if SSL_VERIFY_PEER is set.
  255. .PP
  256. After calling \fBSSL_set_post_handshake_auth()\fR, the client will need to add a
  257. certificate or certificate callback to its configuration before it can
  258. successfully authenticate. This must be called before \fBSSL_connect()\fR.
  259. .PP
  260. \&\fBSSL_verify_client_post_handshake()\fR requires that verify flags have been
  261. previously set, and that a client sent the post-handshake authentication
  262. extension. When the client returns a certificate the verify callback will be
  263. invoked. A write operation must take place for the Certificate Request to be
  264. sent to the client, this can be done with \fBSSL_do_handshake()\fR or \fBSSL_write_ex()\fR.
  265. Only one certificate request may be outstanding at any time.
  266. .PP
  267. When post-handshake authentication occurs, a refreshed NewSessionTicket
  268. message is sent to the client.
  269. .PP
  270. Post-handshake authentication cannot be used with QUIC.
  271. \&\fBSSL_set_post_handshake_auth()\fR has no effect if called on a QUIC SSL object.
  272. .SH BUGS
  273. .IX Header "BUGS"
  274. In client mode, it is not checked whether the SSL_VERIFY_PEER flag
  275. is set, but whether any flags other than SSL_VERIFY_NONE are set. This can
  276. lead to unexpected behaviour if SSL_VERIFY_PEER and other flags are not used as
  277. required.
  278. .SH "RETURN VALUES"
  279. .IX Header "RETURN VALUES"
  280. The SSL*_set_verify*() functions do not provide diagnostic information.
  281. .PP
  282. The \fBSSL_verify_client_post_handshake()\fR function returns 1 if the request
  283. succeeded, and 0 if the request failed. The error stack can be examined
  284. to determine the failure reason.
  285. .SH EXAMPLES
  286. .IX Header "EXAMPLES"
  287. The following code sequence realizes an example \fBverify_callback\fR function
  288. that will always continue the TLS/SSL handshake regardless of verification
  289. failure, if wished. The callback realizes a verification depth limit with
  290. more informational output.
  291. .PP
  292. All verification errors are printed; information about the certificate chain
  293. is printed on request.
  294. The example is realized for a server that does allow but not require client
  295. certificates.
  296. .PP
  297. The example makes use of the ex_data technique to store application data
  298. into/retrieve application data from the SSL structure
  299. (see \fBCRYPTO_get_ex_new_index\fR\|(3),
  300. \&\fBSSL_get_ex_data_X509_STORE_CTX_idx\fR\|(3)).
  301. .PP
  302. .Vb 7
  303. \& ...
  304. \& typedef struct {
  305. \& int verbose_mode;
  306. \& int verify_depth;
  307. \& int always_continue;
  308. \& } mydata_t;
  309. \& int mydata_index;
  310. \&
  311. \& ...
  312. \& static int verify_callback(int preverify_ok, X509_STORE_CTX *ctx)
  313. \& {
  314. \& char buf[256];
  315. \& X509 *err_cert;
  316. \& int err, depth;
  317. \& SSL *ssl;
  318. \& mydata_t *mydata;
  319. \&
  320. \& err_cert = X509_STORE_CTX_get_current_cert(ctx);
  321. \& err = X509_STORE_CTX_get_error(ctx);
  322. \& depth = X509_STORE_CTX_get_error_depth(ctx);
  323. \&
  324. \& /*
  325. \& * Retrieve the pointer to the SSL of the connection currently treated
  326. \& * and the application specific data stored into the SSL object.
  327. \& */
  328. \& ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
  329. \& mydata = SSL_get_ex_data(ssl, mydata_index);
  330. \&
  331. \& X509_NAME_oneline(X509_get_subject_name(err_cert), buf, 256);
  332. \&
  333. \& /*
  334. \& * Catch a too long certificate chain. The depth limit set using
  335. \& * SSL_CTX_set_verify_depth() is by purpose set to "limit+1" so
  336. \& * that whenever the "depth>verify_depth" condition is met, we
  337. \& * have violated the limit and want to log this error condition.
  338. \& * We must do it here, because the CHAIN_TOO_LONG error would not
  339. \& * be found explicitly; only errors introduced by cutting off the
  340. \& * additional certificates would be logged.
  341. \& */
  342. \& if (depth > mydata\->verify_depth) {
  343. \& preverify_ok = 0;
  344. \& err = X509_V_ERR_CERT_CHAIN_TOO_LONG;
  345. \& X509_STORE_CTX_set_error(ctx, err);
  346. \& }
  347. \& if (!preverify_ok) {
  348. \& printf("verify error:num=%d:%s:depth=%d:%s\en", err,
  349. \& X509_verify_cert_error_string(err), depth, buf);
  350. \& } else if (mydata\->verbose_mode) {
  351. \& printf("depth=%d:%s\en", depth, buf);
  352. \& }
  353. \&
  354. \& /*
  355. \& * At this point, err contains the last verification error. We can use
  356. \& * it for something special
  357. \& */
  358. \& if (!preverify_ok && (err == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT)) {
  359. \& X509_NAME_oneline(X509_get_issuer_name(err_cert), buf, 256);
  360. \& printf("issuer= %s\en", buf);
  361. \& }
  362. \&
  363. \& if (mydata\->always_continue)
  364. \& return 1;
  365. \& else
  366. \& return preverify_ok;
  367. \& }
  368. \& ...
  369. \&
  370. \& mydata_t mydata;
  371. \&
  372. \& ...
  373. \& mydata_index = SSL_get_ex_new_index(0, "mydata index", NULL, NULL, NULL);
  374. \&
  375. \& ...
  376. \& SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE,
  377. \& verify_callback);
  378. \&
  379. \& /*
  380. \& * Let the verify_callback catch the verify_depth error so that we get
  381. \& * an appropriate error in the logfile.
  382. \& */
  383. \& SSL_CTX_set_verify_depth(verify_depth + 1);
  384. \&
  385. \& /*
  386. \& * Set up the SSL specific data into "mydata" and store it into th SSL
  387. \& * structure.
  388. \& */
  389. \& mydata.verify_depth = verify_depth; ...
  390. \& SSL_set_ex_data(ssl, mydata_index, &mydata);
  391. \&
  392. \& ...
  393. \& SSL_accept(ssl); /* check of success left out for clarity */
  394. \& if (peer = SSL_get_peer_certificate(ssl)) {
  395. \& if (SSL_get_verify_result(ssl) == X509_V_OK) {
  396. \& /* The client sent a certificate which verified OK */
  397. \& }
  398. \& }
  399. .Ve
  400. .SH "SEE ALSO"
  401. .IX Header "SEE ALSO"
  402. \&\fBssl\fR\|(7), \fBSSL_new\fR\|(3),
  403. \&\fBSSL_CTX_get_verify_mode\fR\|(3),
  404. \&\fBSSL_get_verify_result\fR\|(3),
  405. \&\fBSSL_CTX_load_verify_locations\fR\|(3),
  406. \&\fBSSL_get_peer_certificate\fR\|(3),
  407. \&\fBSSL_CTX_set_cert_verify_callback\fR\|(3),
  408. \&\fBSSL_get_ex_data_X509_STORE_CTX_idx\fR\|(3),
  409. \&\fBSSL_CTX_set_client_cert_cb\fR\|(3),
  410. \&\fBCRYPTO_get_ex_new_index\fR\|(3)
  411. .SH HISTORY
  412. .IX Header "HISTORY"
  413. The SSL_VERIFY_POST_HANDSHAKE option, and the \fBSSL_verify_client_post_handshake()\fR
  414. and \fBSSL_set_post_handshake_auth()\fR functions were added in OpenSSL 1.1.1.
  415. .SH COPYRIGHT
  416. .IX Header "COPYRIGHT"
  417. Copyright 2000\-2024 The OpenSSL Project Authors. All Rights Reserved.
  418. .PP
  419. Licensed under the Apache License 2.0 (the "License"). You may not use
  420. this file except in compliance with the License. You can obtain a copy
  421. in the file LICENSE in the source distribution or at
  422. <https://www.openssl.org/source/license.html>.