2
0

SSL_CTX_set_verify.pod 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. =pod
  2. =head1 NAME
  3. SSL_CTX_set_verify, SSL_set_verify, SSL_CTX_set_verify_depth, SSL_set_verify_depth - set peer certificate verification parameters
  4. =head1 SYNOPSIS
  5. #include <openssl/ssl.h>
  6. void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
  7. int (*verify_callback)(int, X509_STORE_CTX *));
  8. void SSL_set_verify(SSL *s, int mode,
  9. int (*verify_callback)(int, X509_STORE_CTX *));
  10. void SSL_CTX_set_verify_depth(SSL_CTX *ctx,int depth);
  11. void SSL_set_verify_depth(SSL *s, int depth);
  12. int verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx);
  13. =head1 DESCRIPTION
  14. SSL_CTX_set_verify() sets the verification flags for B<ctx> to be B<mode> and
  15. specifies the B<verify_callback> function to be used. If no callback function
  16. shall be specified, the NULL pointer can be used for B<verify_callback>.
  17. SSL_set_verify() sets the verification flags for B<ssl> to be B<mode> and
  18. specifies the B<verify_callback> function to be used. If no callback function
  19. shall be specified, the NULL pointer can be used for B<verify_callback>. In
  20. this case last B<verify_callback> set specifically for this B<ssl> remains. If
  21. no special B<callback> was set before, the default callback for the underlying
  22. B<ctx> is used, that was valid at the the time B<ssl> was created with
  23. L<SSL_new(3)|SSL_new(3)>.
  24. SSL_CTX_set_verify_depth() sets the maximum B<depth> for the certificate chain
  25. verification that shall be allowed for B<ctx>. (See the BUGS section.)
  26. SSL_set_verify_depth() sets the maximum B<depth> for the certificate chain
  27. verification that shall be allowed for B<ssl>. (See the BUGS section.)
  28. =head1 NOTES
  29. The verification of certificates can be controlled by a set of logically
  30. or'ed B<mode> flags:
  31. =over 4
  32. =item SSL_VERIFY_NONE
  33. B<Server mode:> the server will not send a client certificate request to the
  34. client, so the client will not send a certificate.
  35. B<Client mode:> if not using an anonymous cipher (by default disabled), the
  36. server will send a certificate which will be checked. The result of the
  37. certificate verification process can be checked after the TLS/SSL handshake
  38. using the L<SSL_get_verify_result(3)|SSL_get_verify_result(3)> function.
  39. The handshake will be continued regardless of the verification result.
  40. =item SSL_VERIFY_PEER
  41. B<Server mode:> the server sends a client certificate request to the client.
  42. The certificate returned (if any) is checked. If the verification process
  43. fails, the TLS/SSL handshake is
  44. immediately terminated with an alert message containing the reason for
  45. the verification failure.
  46. The behaviour can be controlled by the additional
  47. SSL_VERIFY_FAIL_IF_NO_PEER_CERT and SSL_VERIFY_CLIENT_ONCE flags.
  48. B<Client mode:> the server certificate is verified. If the verification process
  49. fails, the TLS/SSL handshake is
  50. immediately terminated with an alert message containing the reason for
  51. the verification failure. If no server certificate is sent, because an
  52. anonymous cipher is used, SSL_VERIFY_PEER is ignored.
  53. =item SSL_VERIFY_FAIL_IF_NO_PEER_CERT
  54. B<Server mode:> if the client did not return a certificate, the TLS/SSL
  55. handshake is immediately terminated with a "handshake failure" alert.
  56. This flag must be used together with SSL_VERIFY_PEER.
  57. B<Client mode:> ignored
  58. =item SSL_VERIFY_CLIENT_ONCE
  59. B<Server mode:> only request a client certificate on the initial TLS/SSL
  60. handshake. Do not ask for a client certificate again in case of a
  61. renegotiation. This flag must be used together with SSL_VERIFY_PEER.
  62. B<Client mode:> ignored
  63. =back
  64. Exactly one of the B<mode> flags SSL_VERIFY_NONE and SSL_VERIFY_PEER must be
  65. set at any time.
  66. The actual verification procedure is performed either using the built-in
  67. verification procedure or using another application provided verification
  68. function set with
  69. L<SSL_CTX_set_cert_verify_callback(3)|SSL_CTX_set_cert_verify_callback(3)>.
  70. The following descriptions apply in the case of the built-in procedure. An
  71. application provided procedure also has access to the verify depth information
  72. and the verify_callback() function, but the way this information is used
  73. may be different.
  74. SSL_CTX_set_verify_depth() and SSL_set_verify_depth() set the limit up
  75. to which depth certificates in a chain are used during the verification
  76. procedure. If the certificate chain is longer than allowed, the certificates
  77. above the limit are ignored. Error messages are generated as if these
  78. certificates would not be present, most likely a
  79. X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY will be issued.
  80. The depth count is "level 0:peer certificate", "level 1: CA certificate",
  81. "level 2: higher level CA certificate", and so on. Setting the maximum
  82. depth to 2 allows the levels 0, 1, and 2. The default depth limit is 9,
  83. allowing for the peer certificate and additional 9 CA certificates.
  84. The B<verify_callback> function is used to control the behaviour when the
  85. SSL_VERIFY_PEER flag is set. It must be supplied by the application and
  86. receives two arguments: B<preverify_ok> indicates, whether the verification of
  87. the certificate in question was passed (preverify_ok=1) or not
  88. (preverify_ok=0). B<x509_ctx> is a pointer to the complete context used
  89. for the certificate chain verification.
  90. The certificate chain is checked starting with the deepest nesting level
  91. (the root CA certificate) and worked upward to the peer's certificate.
  92. At each level signatures and issuer attributes are checked. Whenever
  93. a verification error is found, the error number is stored in B<x509_ctx>
  94. and B<verify_callback> is called with B<preverify_ok>=0. By applying
  95. X509_CTX_store_* functions B<verify_callback> can locate the certificate
  96. in question and perform additional steps (see EXAMPLES). If no error is
  97. found for a certificate, B<verify_callback> is called with B<preverify_ok>=1
  98. before advancing to the next level.
  99. The return value of B<verify_callback> controls the strategy of the further
  100. verification process. If B<verify_callback> returns 0, the verification
  101. process is immediately stopped with "verification failed" state. If
  102. SSL_VERIFY_PEER is set, a verification failure alert is sent to the peer and
  103. the TLS/SSL handshake is terminated. If B<verify_callback> returns 1,
  104. the verification process is continued. If B<verify_callback> always returns
  105. 1, the TLS/SSL handshake will not be terminated with respect to verification
  106. failures and the connection will be established. The calling process can
  107. however retrieve the error code of the last verification error using
  108. L<SSL_get_verify_result(3)|SSL_get_verify_result(3)> or by maintaining its
  109. own error storage managed by B<verify_callback>.
  110. If no B<verify_callback> is specified, the default callback will be used.
  111. Its return value is identical to B<preverify_ok>, so that any verification
  112. failure will lead to a termination of the TLS/SSL handshake with an
  113. alert message, if SSL_VERIFY_PEER is set.
  114. =head1 BUGS
  115. In client mode, it is not checked whether the SSL_VERIFY_PEER flag
  116. is set, but whether SSL_VERIFY_NONE is not set. This can lead to
  117. unexpected behaviour, if the SSL_VERIFY_PEER and SSL_VERIFY_NONE are not
  118. used as required (exactly one must be set at any time).
  119. The certificate verification depth set with SSL[_CTX]_verify_depth()
  120. stops the verification at a certain depth. The error message produced
  121. will be that of an incomplete certificate chain and not
  122. X509_V_ERR_CERT_CHAIN_TOO_LONG as may be expected.
  123. =head1 RETURN VALUES
  124. The SSL*_set_verify*() functions do not provide diagnostic information.
  125. =head1 EXAMPLES
  126. The following code sequence realizes an example B<verify_callback> function
  127. that will always continue the TLS/SSL handshake regardless of verification
  128. failure, if wished. The callback realizes a verification depth limit with
  129. more informational output.
  130. All verification errors are printed, informations about the certificate chain
  131. are printed on request.
  132. The example is realized for a server that does allow but not require client
  133. certificates.
  134. The example makes use of the ex_data technique to store application data
  135. into/retrieve application data from the SSL structure
  136. (see L<SSL_get_ex_new_index(3)|SSL_get_ex_new_index(3)>,
  137. L<SSL_get_ex_data_X509_STORE_CTX_idx(3)|SSL_get_ex_data_X509_STORE_CTX_idx(3)>).
  138. ...
  139. typedef struct {
  140. int verbose_mode;
  141. int verify_depth;
  142. int always_continue;
  143. } mydata_t;
  144. int mydata_index;
  145. ...
  146. static int verify_callback(int preverify_ok, X509_STORE_CTX *ctx)
  147. {
  148. char buf[256];
  149. X509 *err_cert;
  150. int err, depth;
  151. SSL *ssl;
  152. mydata_t *mydata;
  153. err_cert = X509_STORE_CTX_get_current_cert(ctx);
  154. err = X509_STORE_CTX_get_error(ctx);
  155. depth = X509_STORE_CTX_get_error_depth(ctx);
  156. /*
  157. * Retrieve the pointer to the SSL of the connection currently treated
  158. * and the application specific data stored into the SSL object.
  159. */
  160. ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
  161. mydata = SSL_get_ex_data(ssl, mydata_index);
  162. X509_NAME_oneline(X509_get_subject_name(err_cert), buf, 256);
  163. /*
  164. * Catch a too long certificate chain. The depth limit set using
  165. * SSL_CTX_set_verify_depth() is by purpose set to "limit+1" so
  166. * that whenever the "depth>verify_depth" condition is met, we
  167. * have violated the limit and want to log this error condition.
  168. * We must do it here, because the CHAIN_TOO_LONG error would not
  169. * be found explicitly; only errors introduced by cutting off the
  170. * additional certificates would be logged.
  171. */
  172. if (depth > mydata->verify_depth) {
  173. preverify_ok = 0;
  174. err = X509_V_ERR_CERT_CHAIN_TOO_LONG;
  175. X509_STORE_CTX_set_error(ctx, err);
  176. }
  177. if (!preverify_ok) {
  178. printf("verify error:num=%d:%s:depth=%d:%s\n", err,
  179. X509_verify_cert_error_string(err), depth, buf);
  180. }
  181. else if (mydata->verbose_mode)
  182. {
  183. printf("depth=%d:%s\n", depth, buf);
  184. }
  185. /*
  186. * At this point, err contains the last verification error. We can use
  187. * it for something special
  188. */
  189. if (!preverify_ok && (err == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT))
  190. {
  191. X509_NAME_oneline(X509_get_issuer_name(ctx->current_cert), buf, 256);
  192. printf("issuer= %s\n", buf);
  193. }
  194. if (mydata->always_continue)
  195. return 1;
  196. else
  197. return preverify_ok;
  198. }
  199. ...
  200. mydata_t mydata;
  201. ...
  202. mydata_index = SSL_get_ex_new_index(0, "mydata index", NULL, NULL, NULL);
  203. ...
  204. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE,
  205. verify_callback);
  206. /*
  207. * Let the verify_callback catch the verify_depth error so that we get
  208. * an appropriate error in the logfile.
  209. */
  210. SSL_CTX_set_verify_depth(verify_depth + 1);
  211. /*
  212. * Set up the SSL specific data into "mydata" and store it into th SSL
  213. * structure.
  214. */
  215. mydata.verify_depth = verify_depth; ...
  216. SSL_set_ex_data(ssl, mydata_index, &mydata);
  217. ...
  218. SSL_accept(ssl); /* check of success left out for clarity */
  219. if (peer = SSL_get_peer_certificate(ssl))
  220. {
  221. if (SSL_get_verify_result(ssl) == X509_V_OK)
  222. {
  223. /* The client sent a certificate which verified OK */
  224. }
  225. }
  226. =head1 SEE ALSO
  227. L<ssl(3)|ssl(3)>, L<SSL_new(3)|SSL_new(3)>,
  228. L<SSL_CTX_get_verify_mode(3)|SSL_CTX_get_verify_mode(3)>,
  229. L<SSL_get_verify_result(3)|SSL_get_verify_result(3)>,
  230. L<SSL_CTX_load_verify_locations(3)|SSL_CTX_load_verify_locations(3)>,
  231. L<SSL_get_peer_certificate(3)|SSL_get_peer_certificate(3)>,
  232. L<SSL_CTX_set_cert_verify_callback(3)|SSL_CTX_set_cert_verify_callback(3)>,
  233. L<SSL_get_ex_data_X509_STORE_CTX_idx(3)|SSL_get_ex_data_X509_STORE_CTX_idx(3)>,
  234. L<SSL_get_ex_new_index(3)|SSL_get_ex_new_index(3)>
  235. =cut