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