xref: /openbsd/lib/libcrypto/man/X509_check_host.3 (revision 76d0caae)
1.\" $OpenBSD: X509_check_host.3,v 1.6 2020/09/17 08:04:22 schwarze Exp $
2.\" full merge up to: OpenSSL a09e4d24 Jun 12 01:56:31 2014 -0400
3.\" selective merge up to: OpenSSL 6328d367 Jul 4 21:58:30 2020 +0200
4.\"
5.\" This file was written by Florian Weimer <fweimer@redhat.com> and
6.\" Viktor Dukhovni <openssl-users@dukhovni.org>.
7.\" Copyright (c) 2012, 2014, 2015, 2016 The OpenSSL Project.
8.\" All rights reserved.
9.\"
10.\" Redistribution and use in source and binary forms, with or without
11.\" modification, are permitted provided that the following conditions
12.\" are met:
13.\"
14.\" 1. Redistributions of source code must retain the above copyright
15.\"    notice, this list of conditions and the following disclaimer.
16.\"
17.\" 2. Redistributions in binary form must reproduce the above copyright
18.\"    notice, this list of conditions and the following disclaimer in
19.\"    the documentation and/or other materials provided with the
20.\"    distribution.
21.\"
22.\" 3. All advertising materials mentioning features or use of this
23.\"    software must display the following acknowledgment:
24.\"    "This product includes software developed by the OpenSSL Project
25.\"    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
26.\"
27.\" 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
28.\"    endorse or promote products derived from this software without
29.\"    prior written permission. For written permission, please contact
30.\"    openssl-core@openssl.org.
31.\"
32.\" 5. Products derived from this software may not be called "OpenSSL"
33.\"    nor may "OpenSSL" appear in their names without prior written
34.\"    permission of the OpenSSL Project.
35.\"
36.\" 6. Redistributions of any form whatsoever must retain the following
37.\"    acknowledgment:
38.\"    "This product includes software developed by the OpenSSL Project
39.\"    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
40.\"
41.\" THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
42.\" EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
44.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
45.\" ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
47.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
48.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
50.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
51.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
52.\" OF THE POSSIBILITY OF SUCH DAMAGE.
53.\"
54.Dd $Mdocdate: September 17 2020 $
55.Dt X509_CHECK_HOST 3
56.Os
57.Sh NAME
58.Nm X509_check_host ,
59.Nm X509_check_email ,
60.Nm X509_check_ip ,
61.Nm X509_check_ip_asc
62.Nd X.509 certificate matching
63.Sh SYNOPSIS
64.In openssl/x509v3.h
65.Ft int
66.Fo X509_check_host
67.Fa "X509 *x"
68.Fa "const char *name"
69.Fa "size_t namelen"
70.Fa "unsigned int flags"
71.Fa "char **peername"
72.Fc
73.Ft int
74.Fo X509_check_email
75.Fa "X509 *x"
76.Fa "const char *address"
77.Fa "size_t addresslen"
78.Fa "unsigned int flags"
79.Fc
80.Ft int
81.Fo X509_check_ip
82.Fa "X509 *x"
83.Fa "const unsigned char *address"
84.Fa "size_t addresslen"
85.Fa "unsigned int flags"
86.Fc
87.Ft int
88.Fo X509_check_ip_asc
89.Fa "X509 *x"
90.Fa "const char *address"
91.Fa "unsigned int flags"
92.Fc
93.Sh DESCRIPTION
94The certificate matching functions are used to check whether a
95certificate matches a given hostname, email address, or IP address.
96The validity of the certificate and its trust level has to be checked by
97other means.
98.Pp
99.Fn X509_check_host
100checks if the certificate Subject Alternative Name (SAN) or Subject
101CommonName (CN) matches the specified hostname, which must be encoded
102in the preferred name syntax described in section 3.5 of RFC 1034.
103By default, wildcards are supported and they match only in the
104left-most label; they may match part of that label with an
105explicit prefix or suffix.
106For example, by default, the host
107.Fa name
108.Qq www.example.com
109would match a certificate with a SAN or CN value of
110.Qq *.example.com ,
111.Qq w*.example.com
112or
113.Qq *w.example.com .
114.Pp
115Per section 6.4.2 of RFC 6125,
116.Fa name
117values representing international domain names must be given in A-label
118form.
119The
120.Fa namelen
121argument must be the number of characters in the name string or zero, in
122which case the length is calculated with
123.Fn strlen name .
124When
125.Fa name
126starts with a dot (e.g.\&
127.Qq .example.com ) ,
128it will be matched by a certificate valid for any sub-domain of
129.Fa name ;
130see also
131.Fa X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS
132below.
133.Pp
134When the certificate is matched and
135.Fa peername
136is not
137.Dv NULL ,
138a pointer to a copy of the matching SAN or CN from the peer
139certificate is stored at the address passed in
140.Fa peername .
141The application is responsible for freeing the peername via
142.Xr free 3
143when it is no longer needed.
144.Pp
145.Fn X509_check_email
146checks if the certificate matches the specified email
147.Fa address .
148Only the mailbox syntax of RFC 822 is supported.
149Comments are not allowed,
150and no attempt is made to normalize quoted characters.
151The
152.Fa addresslen
153argument must be the number of characters in the address string or zero,
154in which case the length is calculated with
155.Fn strlen address .
156.Pp
157.Fn X509_check_ip
158checks if the certificate matches a specified IPv4 or IPv6 address.
159The
160.Fa address
161array is in binary format, in network byte order.
162The length is either 4 (IPv4) or 16 (IPv6).
163Only explicitly marked addresses in the certificates are considered;
164IP addresses stored in DNS names and Common Names are ignored.
165.Pp
166.Fn X509_check_ip_asc
167is similar, except that the NUL-terminated string
168.Fa address
169is first converted to the internal representation.
170.Pp
171The
172.Fa flags
173argument is usually 0, but it can be the bitwise OR of the following
174flags.
175.Pp
176The
177.Dv X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT
178flag causes the function to consider the subject DN even if the
179certificate contains at least one subject alternative name of the right
180type (DNS name or email address as appropriate); the default is to
181ignore the subject DN when at least one corresponding subject
182alternative names is present.
183.Pp
184The remaining flags are only meaningful for
185.Fn X509_check_host .
186.Pp
187The
188.Dv X509_CHECK_FLAG_NO_WILDCARDS
189flag disables wildcard expansion.
190.Pp
191The
192.Dv X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS
193flag suppresses support for
194.Qq *
195as a wildcard pattern in labels that have a
196prefix or suffix, such as
197.Qq www*
198or
199.Qq *www .
200.Pp
201The
202.Dv X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS
203flag allows a
204.Qq *
205that constitutes the complete label of a DNS name (e.g.\&
206.Qq *.example.com )
207to match more than one label in
208.Fa name .
209.Pp
210The
211.Dv X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS
212flag restricts
213.Fa name
214values which start with
215.Qq \&. ,
216that would otherwise match any sub-domain in the peer certificate,
217to only match direct child sub-domains.
218Thus, for instance, with this flag set a
219.Fa name
220of
221.Qq .example.com
222would match a peer certificate with a DNS name of
223.Qq www.example.com ,
224but would not match a peer certificate with a DNS name of
225.Qq www.sub.example.com .
226.Sh RETURN VALUES
227The functions return 1 for a successful match, 0 for a failed match and
228-1 for an internal error: typically a memory allocation failure or an
229ASN.1 decoding error.
230.Pp
231All functions can also return -2 if the input is malformed.
232For example,
233.Fn X509_check_host
234returns -2 if the provided
235.Fa name
236contains embedded NUL bytes.
237.Sh SEE ALSO
238.Xr SSL_set1_host 3 ,
239.Xr X509_EXTENSION_new 3 ,
240.Xr X509_get1_email 3 ,
241.Xr X509_new 3 ,
242.Xr X509_VERIFY_PARAM_set1_host 3
243.Sh HISTORY
244These functions first appeared in OpenSSL 1.0.2
245and have been available since
246.Ox 6.1 .
247