1*e7b81046Stb.\" $OpenBSD: SSL_set1_host.3,v 1.4 2021/03/31 16:56:46 tb Exp $ 236c5091cSschwarze.\" selective merge up to: OpenSSL 6328d367 Jul 4 21:58:30 2020 +0200 336c5091cSschwarze.\" 436c5091cSschwarze.\" This file was written by Viktor Dukhovni <viktor@openssl.org> 536c5091cSschwarze.\" Copyright (c) 2015 The OpenSSL Project. All rights reserved. 636c5091cSschwarze.\" 736c5091cSschwarze.\" Redistribution and use in source and binary forms, with or without 836c5091cSschwarze.\" modification, are permitted provided that the following conditions 936c5091cSschwarze.\" are met: 1036c5091cSschwarze.\" 1136c5091cSschwarze.\" 1. Redistributions of source code must retain the above copyright 1236c5091cSschwarze.\" notice, this list of conditions and the following disclaimer. 1336c5091cSschwarze.\" 1436c5091cSschwarze.\" 2. Redistributions in binary form must reproduce the above copyright 1536c5091cSschwarze.\" notice, this list of conditions and the following disclaimer in 1636c5091cSschwarze.\" the documentation and/or other materials provided with the 1736c5091cSschwarze.\" distribution. 1836c5091cSschwarze.\" 1936c5091cSschwarze.\" 3. All advertising materials mentioning features or use of this 2036c5091cSschwarze.\" software must display the following acknowledgment: 2136c5091cSschwarze.\" "This product includes software developed by the OpenSSL Project 2236c5091cSschwarze.\" for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 2336c5091cSschwarze.\" 2436c5091cSschwarze.\" 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 2536c5091cSschwarze.\" endorse or promote products derived from this software without 2636c5091cSschwarze.\" prior written permission. For written permission, please contact 2736c5091cSschwarze.\" openssl-core@openssl.org. 2836c5091cSschwarze.\" 2936c5091cSschwarze.\" 5. Products derived from this software may not be called "OpenSSL" 3036c5091cSschwarze.\" nor may "OpenSSL" appear in their names without prior written 3136c5091cSschwarze.\" permission of the OpenSSL Project. 3236c5091cSschwarze.\" 3336c5091cSschwarze.\" 6. Redistributions of any form whatsoever must retain the following 3436c5091cSschwarze.\" acknowledgment: 3536c5091cSschwarze.\" "This product includes software developed by the OpenSSL Project 3636c5091cSschwarze.\" for use in the OpenSSL Toolkit (http://www.openssl.org/)" 3736c5091cSschwarze.\" 3836c5091cSschwarze.\" THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 3936c5091cSschwarze.\" EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4036c5091cSschwarze.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 4136c5091cSschwarze.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 4236c5091cSschwarze.\" ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 4336c5091cSschwarze.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 4436c5091cSschwarze.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 4536c5091cSschwarze.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4636c5091cSschwarze.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 4736c5091cSschwarze.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 4836c5091cSschwarze.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 4936c5091cSschwarze.\" OF THE POSSIBILITY OF SUCH DAMAGE. 5036c5091cSschwarze.\" 51*e7b81046Stb.Dd $Mdocdate: March 31 2021 $ 5236c5091cSschwarze.Dt SSL_SET1_HOST 3 5336c5091cSschwarze.Os 5436c5091cSschwarze.Sh NAME 55*e7b81046Stb.Nm SSL_set1_host , 56*e7b81046Stb.Nm SSL_set_hostflags , 5736c5091cSschwarze.Nm SSL_get0_peername 5836c5091cSschwarze.Nd SSL server verification parameters 5936c5091cSschwarze.Sh SYNOPSIS 6036c5091cSschwarze.In openssl/ssl.h 6136c5091cSschwarze.Ft int 6236c5091cSschwarze.Fo SSL_set1_host 6336c5091cSschwarze.Fa "SSL *ssl" 6436c5091cSschwarze.Fa "const char *hostname" 6536c5091cSschwarze.Fc 6683fe41ffStb.Ft void 6783fe41ffStb.Fo SSL_set_hostflags 6883fe41ffStb.Fa "SSL *ssl" 6983fe41ffStb.Fa "unsigned int flags" 7083fe41ffStb.Fc 7136c5091cSschwarze.Ft const char * 7236c5091cSschwarze.Fo SSL_get0_peername 7336c5091cSschwarze.Fa "SSL *ssl" 7436c5091cSschwarze.Fc 7536c5091cSschwarze.Sh DESCRIPTION 7636c5091cSschwarze.Fn SSL_set1_host 7736c5091cSschwarzeconfigures a server hostname check in the 7836c5091cSschwarze.Fa ssl 7936c5091cSschwarzeclient, setting the expected DNS hostname to 8036c5091cSschwarze.Fa hostname 8136c5091cSschwarzeand clearing any previously specified hostname. 8236c5091cSschwarzeIf 8336c5091cSschwarze.Fa hostname 8436c5091cSschwarzeis 8536c5091cSschwarze.Dv NULL 8636c5091cSschwarzeor the empty string, name checks are not performed on the peer certificate. 8736c5091cSschwarzeIf a nonempty 8836c5091cSschwarze.Fa hostname 8936c5091cSschwarzeis specified, certificate verification automatically checks the peer 9036c5091cSschwarzehostname via 9136c5091cSschwarze.Xr X509_check_host 3 9236c5091cSschwarzewith 9336c5091cSschwarze.Fa flags 9436c5091cSschwarzeset to 0. 9536c5091cSschwarze.Pp 9683fe41ffStb.Fn SSL_set_hostflags 9783fe41ffStbsets the flags that will be passed to 9883fe41ffStb.Xr X509_check_host 3 9983fe41ffStbwhen name checks are applicable, 10083fe41ffStbby default the flags value is 0. 10183fe41ffStbSee 10283fe41ffStb.Xr X509_check_host 3 10383fe41ffStbfor the list of available flags and their meaning. 10483fe41ffStb.Pp 10536c5091cSschwarze.Fn SSL_get0_peername 10636c5091cSschwarzereturns the DNS hostname or subject CommonName from the peer certificate 10736c5091cSschwarzethat matched one of the reference identifiers. 10836c5091cSschwarzeUnless wildcard matching is disabled, the name matched in the peer 10936c5091cSschwarzecertificate may be a wildcard name. 11036c5091cSschwarzeA reference identifier starting with 11136c5091cSschwarze.Sq \&. 11236c5091cSschwarzeindicates a parent domain prefix rather than a fixed name. 11336c5091cSschwarzeIn this case, the matched peername may be a sub-domain 11436c5091cSschwarzeof the reference identifier. 11536c5091cSschwarzeThe returned string is owned by the library and is no longer valid 11636c5091cSschwarzeonce the associated 11736c5091cSschwarze.Fa ssl 11836c5091cSschwarzeobject is cleared or freed, or if a renegotiation takes place. 11936c5091cSschwarzeApplications must not free the return value. 12036c5091cSschwarze.Pp 12136c5091cSschwarzeSSL clients are advised to use these functions in preference to 12236c5091cSschwarzeexplicitly calling 12336c5091cSschwarze.Xr X509_check_host 3 . 12436c5091cSschwarze.Sh RETURN VALUES 12536c5091cSschwarze.Fn SSL_set1_host 12636c5091cSschwarzereturns 1 for success or 0 for failure. 12736c5091cSschwarze.Pp 12836c5091cSschwarze.Fn SSL_get0_peername 12936c5091cSschwarzereturns the matched peername or 13036c5091cSschwarze.Dv NULL 13136c5091cSschwarzeif peername verification is not applicable 13236c5091cSschwarzeor no trusted peername was matched. 13336c5091cSschwarzeUse 13436c5091cSschwarze.Xr SSL_get_verify_result 3 13536c5091cSschwarzeto determine whether verification succeeded. 13636c5091cSschwarze.Sh EXAMPLES 13736c5091cSschwarzeThe calls below check the hostname. 13836c5091cSschwarzeWildcards are supported, but they must match the entire label. 13936c5091cSschwarzeThe actual name matched in the certificate (which might be a wildcard) 14036c5091cSschwarzeis retrieved, and must be copied by the application if it is to be 14136c5091cSschwarzeretained beyond the lifetime of the SSL connection. 14236c5091cSschwarze.Bd -literal 14336c5091cSschwarzeif (!SSL_set1_host(ssl, "smtp.example.com")) 14436c5091cSschwarze /* error */ 14536c5091cSschwarze 14636c5091cSschwarze/* XXX: Perform SSL_connect() handshake and handle errors here */ 14736c5091cSschwarze 14836c5091cSschwarzeif (SSL_get_verify_result(ssl) == X509_V_OK) { 14936c5091cSschwarze const char *peername = SSL_get0_peername(ssl); 15036c5091cSschwarze 15136c5091cSschwarze if (peername != NULL) 15236c5091cSschwarze /* Name checks were in scope and matched the peername */ 15336c5091cSschwarze} 15436c5091cSschwarze.Ed 15536c5091cSschwarze.Sh SEE ALSO 15636c5091cSschwarze.Xr ssl 3 , 15736c5091cSschwarze.Xr SSL_CTX_set_verify 3 , 15836c5091cSschwarze.Xr SSL_get_peer_certificate 3 , 15936c5091cSschwarze.Xr SSL_get_verify_result 3 , 16036c5091cSschwarze.Xr X509_check_host 3 , 16136c5091cSschwarze.Xr X509_VERIFY_PARAM_set1_host 3 16236c5091cSschwarze.Sh HISTORY 16383fe41ffStbAll three functions first appeared in OpenSSL 1.1.0. 16436c5091cSschwarze.Fn SSL_set1_host 16536c5091cSschwarzehas been available since 16636c5091cSschwarze.Ox 6.5 , 16736c5091cSschwarzeand 16883fe41ffStb.Fn SSL_set_hostflags 16983fe41ffStband 17036c5091cSschwarze.Fn SSL_get0_peername 17136c5091cSschwarzesince 1727ed6d491Sschwarze.Ox 6.9 . 173