1.\"	$OpenBSD: SSL_CTX_load_verify_locations.3,v 1.4 2018/03/27 17:35:50 schwarze Exp $
2.\"	OpenSSL 9b86974e Aug 17 15:21:33 2015 -0400
3.\"
4.\" This file was written by Lutz Jaenicke <jaenicke@openssl.org>.
5.\" Copyright (c) 2000, 2001, 2015, 2016 The OpenSSL Project.
6.\" All rights reserved.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\"
12.\" 1. Redistributions of source code must retain the above copyright
13.\"    notice, this list of conditions and the following disclaimer.
14.\"
15.\" 2. Redistributions in binary form must reproduce the above copyright
16.\"    notice, this list of conditions and the following disclaimer in
17.\"    the documentation and/or other materials provided with the
18.\"    distribution.
19.\"
20.\" 3. All advertising materials mentioning features or use of this
21.\"    software must display the following acknowledgment:
22.\"    "This product includes software developed by the OpenSSL Project
23.\"    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
24.\"
25.\" 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26.\"    endorse or promote products derived from this software without
27.\"    prior written permission. For written permission, please contact
28.\"    openssl-core@openssl.org.
29.\"
30.\" 5. Products derived from this software may not be called "OpenSSL"
31.\"    nor may "OpenSSL" appear in their names without prior written
32.\"    permission of the OpenSSL Project.
33.\"
34.\" 6. Redistributions of any form whatsoever must retain the following
35.\"    acknowledgment:
36.\"    "This product includes software developed by the OpenSSL Project
37.\"    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
38.\"
39.\" THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40.\" EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43.\" ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50.\" OF THE POSSIBILITY OF SUCH DAMAGE.
51.\"
52.Dd $Mdocdate: March 27 2018 $
53.Dt SSL_CTX_LOAD_VERIFY_LOCATIONS 3
54.Os
55.Sh NAME
56.Nm SSL_CTX_load_verify_locations ,
57.Nm SSL_CTX_set_default_verify_paths
58.Nd set default locations for trusted CA certificates
59.Sh SYNOPSIS
60.In openssl/ssl.h
61.Ft int
62.Fo SSL_CTX_load_verify_locations
63.Fa "SSL_CTX *ctx" "const char *CAfile" "const char *CApath"
64.Fc
65.Ft int
66.Fo SSL_CTX_set_default_verify_paths
67.Fa "SSL_CTX *ctx"
68.Fc
69.Sh DESCRIPTION
70.Fn SSL_CTX_load_verify_locations
71specifies the locations for
72.Fa ctx ,
73at which CA certificates for verification purposes are located.
74The certificates available via
75.Fa CAfile
76and
77.Fa CApath
78are trusted.
79.Pp
80.Fn SSL_CTX_set_default_verify_paths
81specifies that the default locations from which CA certificates are
82loaded should be used.
83There is one default directory and one default file.
84The default CA certificates directory is called
85.Pa certs
86in the default OpenSSL directory.
87The default CA certificates file is called
88.Pa cert.pem
89in the default OpenSSL directory.
90.Pp
91If
92.Fa CAfile
93is not
94.Dv NULL ,
95it points to a file of CA certificates in PEM format.
96The file can contain several CA certificates identified by sequences of:
97.Bd -literal
98 -----BEGIN CERTIFICATE-----
99 ... (CA certificate in base64 encoding) ...
100 -----END CERTIFICATE-----
101.Ed
102.Pp
103Before, between, and after the certificates arbitrary text is allowed which can
104be used, e.g., for descriptions of the certificates.
105.Pp
106The
107.Fa CAfile
108is processed on execution of the
109.Fn SSL_CTX_load_verify_locations
110function.
111.Pp
112If
113.Fa CApath
114is not NULL, it points to a directory containing CA certificates in PEM format.
115The files each contain one CA certificate.
116The files are looked up by the CA subject name hash value,
117which must hence be available.
118If more than one CA certificate with the same name hash value exist,
119the extension must be different (e.g.,
120.Pa 9d66eef0.0 ,
121.Pa 9d66eef0.1 ,
122etc.).
123The search is performed in the ordering of the extension number,
124regardless of other properties of the certificates.
125.Pp
126The certificates in
127.Fa CApath
128are only looked up when required, e.g., when building the certificate chain or
129when actually performing the verification of a peer certificate.
130.Pp
131When looking up CA certificates, the OpenSSL library will first search the
132certificates in
133.Fa CAfile ,
134then those in
135.Fa CApath .
136Certificate matching is done based on the subject name, the key identifier (if
137present), and the serial number as taken from the certificate to be verified.
138If these data do not match, the next certificate will be tried.
139If a first certificate matching the parameters is found,
140the verification process will be performed;
141no other certificates for the same parameters will be searched in case of
142failure.
143.Pp
144In server mode, when requesting a client certificate, the server must send
145the list of CAs of which it will accept client certificates.
146This list is not influenced by the contents of
147.Fa CAfile
148or
149.Fa CApath
150and must explicitly be set using the
151.Xr SSL_CTX_set_client_CA_list 3
152family of functions.
153.Pp
154When building its own certificate chain, an OpenSSL client/server will try to
155fill in missing certificates from
156.Fa CAfile Ns / Fa CApath ,
157if the
158certificate chain was not explicitly specified (see
159.Xr SSL_CTX_add_extra_chain_cert 3
160and
161.Xr SSL_CTX_use_certificate 3 ) .
162.Sh RETURN VALUES
163For
164.Fn SSL_CTX_load_verify_locations ,
165the following return values can occur:
166.Bl -tag -width Ds
167.It 0
168The operation failed because
169.Fa CAfile
170and
171.Fa CApath
172are
173.Dv NULL
174or the processing at one of the locations specified failed.
175Check the error stack to find out the reason.
176.It 1
177The operation succeeded.
178.El
179.Pp
180.Fn SSL_CTX_set_default_verify_paths
181returns 1 on success or 0 on failure.
182A missing default location is still treated as a success.
183.Sh EXAMPLES
184Generate a CA certificate file with descriptive text from the CA certificates
185.Pa ca1.pem
186.Pa ca2.pem
187.Pa ca3.pem :
188.Bd -literal
189#!/bin/sh
190rm CAfile.pem
191for i in ca1.pem ca2.pem ca3.pem; do
192	openssl x509 -in $i -text >> CAfile.pem
193done
194.Ed
195.Pp
196Prepare the directory /some/where/certs containing several CA certificates
197for use as
198.Fa CApath :
199.Bd -literal
200$ cd /some/where/certs
201$ rm -f *.[0-9]* *.r[0-9]*
202$ for c in *.pem; do
203>    [ "$c" = "*.pem" ] && continue
204>    hash=$(openssl x509 -noout -hash -in "$c")
205>    if egrep -q -- '-BEGIN( X509 | TRUSTED | )CERTIFICATE-' "$c"; then
206>      suf=0
207>      while [ -e $hash.$suf ]; do suf=$(( $suf + 1 )); done
208>      ln -s "$c" $hash.$suf
209>    fi
210>    if egrep -q -- '-BEGIN X509 CRL-' "$c"; then
211>      suf=0
212>      while [ -e $hash.r$suf ]; do suf=$(( $suf + 1 )); done
213>      ln -s "$c" $hash.r$suf
214>    fi
215> done
216.Ed
217.Sh SEE ALSO
218.Xr ssl 3 ,
219.Xr SSL_CTX_add_extra_chain_cert 3 ,
220.Xr SSL_CTX_set_cert_store 3 ,
221.Xr SSL_CTX_set_client_CA_list 3 ,
222.Xr SSL_CTX_use_certificate 3 ,
223.Xr SSL_get_client_CA_list 3
224.Sh HISTORY
225.Fn SSL_CTX_load_verify_locations
226and
227.Fn SSL_CTX_set_default_verify_paths
228first appeared in SSLeay 0.8.0 and have been available since
229.Ox 2.4 .
230.Sh CAVEATS
231If several CA certificates matching the name, key identifier, and serial
232number condition are available, only the first one will be examined.
233This may lead to unexpected results if the same CA certificate is available
234with different expiration dates.
235If a
236.Dq certificate expired
237verification error occurs, no other certificate will be searched.
238Make sure to not have expired certificates mixed with valid ones.
239