xref: /freebsd/crypto/openssl/test/recipes/25-test_verify.t (revision 0d0c8621fd181e507f0fb50ffcca606faf66a8c2)
1e0c4386eSCy Schubert#! /usr/bin/env perl
2e0c4386eSCy Schubert# Copyright 2015-2023 The OpenSSL Project Authors. All Rights Reserved.
3e0c4386eSCy Schubert#
4e0c4386eSCy Schubert# Licensed under the Apache License 2.0 (the "License").  You may not use
5e0c4386eSCy Schubert# this file except in compliance with the License.  You can obtain a copy
6e0c4386eSCy Schubert# in the file LICENSE in the source distribution or at
7e0c4386eSCy Schubert# https://www.openssl.org/source/license.html
8e0c4386eSCy Schubert
9e0c4386eSCy Schubert
10e0c4386eSCy Schubertuse strict;
11e0c4386eSCy Schubertuse warnings;
12e0c4386eSCy Schubert
13e0c4386eSCy Schubertuse File::Spec::Functions qw/canonpath/;
14e0c4386eSCy Schubertuse File::Copy;
15e0c4386eSCy Schubertuse OpenSSL::Test qw/:DEFAULT srctop_file bldtop_dir ok_nofips with/;
16e0c4386eSCy Schubertuse OpenSSL::Test::Utils;
17e0c4386eSCy Schubert
18e0c4386eSCy Schubertsetup("test_verify");
19e0c4386eSCy Schubert
20e0c4386eSCy Schubertsub verify {
21e0c4386eSCy Schubert    my ($cert, $purpose, $trusted, $untrusted, @opts) = @_;
22e0c4386eSCy Schubert    my @path = qw(test certs);
23e0c4386eSCy Schubert    my @args = qw(openssl verify -auth_level 1);
24e0c4386eSCy Schubert    push(@args, "-purpose", $purpose) if $purpose ne "";
25e0c4386eSCy Schubert    push(@args, @opts);
26e0c4386eSCy Schubert    for (@$trusted) { push(@args, "-trusted", srctop_file(@path, "$_.pem")) }
27e0c4386eSCy Schubert    for (@$untrusted) { push(@args, "-untrusted", srctop_file(@path, "$_.pem")) }
28e0c4386eSCy Schubert    push(@args, srctop_file(@path, "$cert.pem"));
29e0c4386eSCy Schubert    run(app([@args]));
30e0c4386eSCy Schubert}
31e0c4386eSCy Schubert
32e0c4386eSCy Schubertplan tests => 166;
33e0c4386eSCy Schubert
34e0c4386eSCy Schubert# Canonical success
35e0c4386eSCy Schubertok(verify("ee-cert", "sslserver", ["root-cert"], ["ca-cert"]),
36e0c4386eSCy Schubert   "accept compat trust");
37e0c4386eSCy Schubert
38e0c4386eSCy Schubert# Root CA variants
39e0c4386eSCy Schubertok(!verify("ee-cert", "sslserver", [qw(root-nonca)], [qw(ca-cert)]),
40e0c4386eSCy Schubert   "fail trusted non-ca root");
41e0c4386eSCy Schubertok(!verify("ee-cert", "sslserver", [qw(nroot+serverAuth)], [qw(ca-cert)]),
42e0c4386eSCy Schubert   "fail server trust non-ca root");
43e0c4386eSCy Schubertok(!verify("ee-cert", "sslserver", [qw(nroot+anyEKU)], [qw(ca-cert)]),
44e0c4386eSCy Schubert   "fail wildcard trust non-ca root");
45e0c4386eSCy Schubertok(!verify("ee-cert", "sslserver", [qw(root-cert2)], [qw(ca-cert)]),
46e0c4386eSCy Schubert   "fail wrong root key");
47e0c4386eSCy Schubertok(!verify("ee-cert", "sslserver", [qw(root-name2)], [qw(ca-cert)]),
48e0c4386eSCy Schubert   "fail wrong root DN");
49e0c4386eSCy Schubert
50e0c4386eSCy Schubert# Critical extensions
51e0c4386eSCy Schubert
52e0c4386eSCy Schubertok(verify("ee-cert-noncrit-unknown-ext", "", ["root-cert"], ["ca-cert"]),
53e0c4386eSCy Schubert   "accept non-critical unknown extension");
54e0c4386eSCy Schubertok(!verify("ee-cert-crit-unknown-ext", "", ["root-cert"], ["ca-cert"]),
55e0c4386eSCy Schubert   "reject critical unknown extension");
56e0c4386eSCy Schubertok(verify("ee-cert-ocsp-nocheck", "", ["root-cert"], ["ca-cert"]),
57e0c4386eSCy Schubert   "accept critical OCSP No Check");
58e0c4386eSCy Schubert
59e0c4386eSCy Schubert# Explicit trust/purpose combinations
60e0c4386eSCy Schubert#
61e0c4386eSCy Schubertok(verify("ee-cert", "sslserver", [qw(sroot-cert)], [qw(ca-cert)]),
62e0c4386eSCy Schubert   "accept server purpose");
63e0c4386eSCy Schubertok(!verify("ee-cert", "sslserver", [qw(croot-cert)], [qw(ca-cert)]),
64*0d0c8621SEnji Cooper   "fail client purpose"); # beware, questionable non-standard EKU check on trust anchor
65e0c4386eSCy Schubertok(verify("ee-cert", "sslserver", [qw(root+serverAuth)], [qw(ca-cert)]),
66e0c4386eSCy Schubert   "accept server trust");
67e0c4386eSCy Schubertok(verify("ee-cert", "sslserver", [qw(sroot+serverAuth)], [qw(ca-cert)]),
68e0c4386eSCy Schubert   "accept server trust with server purpose");
69e0c4386eSCy Schubertok(verify("ee-cert", "sslserver", [qw(croot+serverAuth)], [qw(ca-cert)]),
70e0c4386eSCy Schubert   "accept server trust with client purpose");
71e0c4386eSCy Schubert# Wildcard trust
72e0c4386eSCy Schubertok(verify("ee-cert", "sslserver", [qw(root+anyEKU)], [qw(ca-cert)]),
73e0c4386eSCy Schubert   "accept wildcard trust");
74e0c4386eSCy Schubertok(verify("ee-cert", "sslserver", [qw(sroot+anyEKU)], [qw(ca-cert)]),
75e0c4386eSCy Schubert   "accept wildcard trust with server purpose");
76e0c4386eSCy Schubertok(verify("ee-cert", "sslserver", [qw(croot+anyEKU)], [qw(ca-cert)]),
77e0c4386eSCy Schubert   "accept wildcard trust with client purpose");
78e0c4386eSCy Schubert# Inapplicable mistrust
79e0c4386eSCy Schubertok(verify("ee-cert", "sslserver", [qw(root-clientAuth)], [qw(ca-cert)]),
80e0c4386eSCy Schubert   "accept client mistrust");
81e0c4386eSCy Schubertok(verify("ee-cert", "sslserver", [qw(sroot-clientAuth)], [qw(ca-cert)]),
82e0c4386eSCy Schubert   "accept client mistrust with server purpose");
83e0c4386eSCy Schubertok(!verify("ee-cert", "sslserver", [qw(croot-clientAuth)], [qw(ca-cert)]),
84*0d0c8621SEnji Cooper   "fail client mistrust with client purpose"); # beware, questionable non-standard EKU check on trust anchor
85e0c4386eSCy Schubert# Inapplicable trust
86e0c4386eSCy Schubertok(!verify("ee-cert", "sslserver", [qw(root+clientAuth)], [qw(ca-cert)]),
87e0c4386eSCy Schubert   "fail client trust");
88e0c4386eSCy Schubertok(!verify("ee-cert", "sslserver", [qw(sroot+clientAuth)], [qw(ca-cert)]),
89e0c4386eSCy Schubert   "fail client trust with server purpose");
90e0c4386eSCy Schubertok(!verify("ee-cert", "sslserver", [qw(croot+clientAuth)], [qw(ca-cert)]),
91e0c4386eSCy Schubert   "fail client trust with client purpose");
92e0c4386eSCy Schubert# Server mistrust
93e0c4386eSCy Schubertok(!verify("ee-cert", "sslserver", [qw(root-serverAuth)], [qw(ca-cert)]),
94e0c4386eSCy Schubert   "fail rejected EKU");
95e0c4386eSCy Schubertok(!verify("ee-cert", "sslserver", [qw(sroot-serverAuth)], [qw(ca-cert)]),
96e0c4386eSCy Schubert   "fail server mistrust with server purpose");
97e0c4386eSCy Schubertok(!verify("ee-cert", "sslserver", [qw(croot-serverAuth)], [qw(ca-cert)]),
98e0c4386eSCy Schubert   "fail server mistrust with client purpose");
99e0c4386eSCy Schubert# Wildcard mistrust
100e0c4386eSCy Schubertok(!verify("ee-cert", "sslserver", [qw(root-anyEKU)], [qw(ca-cert)]),
101e0c4386eSCy Schubert   "fail wildcard mistrust");
102e0c4386eSCy Schubertok(!verify("ee-cert", "sslserver", [qw(sroot-anyEKU)], [qw(ca-cert)]),
103e0c4386eSCy Schubert   "fail wildcard mistrust with server purpose");
104e0c4386eSCy Schubertok(!verify("ee-cert", "sslserver", [qw(croot-anyEKU)], [qw(ca-cert)]),
105e0c4386eSCy Schubert   "fail wildcard mistrust with client purpose");
106e0c4386eSCy Schubert
107e0c4386eSCy Schubert# Check that trusted-first is on by setting up paths to different roots
108e0c4386eSCy Schubert# depending on whether the intermediate is the trusted or untrusted one.
109e0c4386eSCy Schubert#
110e0c4386eSCy Schubertok(verify("ee-cert", "sslserver", [qw(root-serverAuth root-cert2 ca-root2)],
111e0c4386eSCy Schubert          [qw(ca-cert)]),
112e0c4386eSCy Schubert   "accept trusted-first path");
113e0c4386eSCy Schubertok(verify("ee-cert", "sslserver", [qw(root-cert root2+serverAuth ca-root2)],
114e0c4386eSCy Schubert          [qw(ca-cert)]),
115e0c4386eSCy Schubert   "accept trusted-first path with server trust");
116e0c4386eSCy Schubertok(!verify("ee-cert", "sslserver", [qw(root-cert root2-serverAuth ca-root2)],
117e0c4386eSCy Schubert           [qw(ca-cert)]),
118e0c4386eSCy Schubert   "fail trusted-first path with server mistrust");
119e0c4386eSCy Schubertok(!verify("ee-cert", "sslserver", [qw(root-cert root2+clientAuth ca-root2)],
120e0c4386eSCy Schubert           [qw(ca-cert)]),
121e0c4386eSCy Schubert   "fail trusted-first path with client trust");
122e0c4386eSCy Schubert
123e0c4386eSCy Schubert# CA variants
124e0c4386eSCy Schubertok(!verify("ee-cert", "sslserver", [qw(root-cert)], [qw(ca-nonca)]),
125e0c4386eSCy Schubert   "fail non-CA untrusted intermediate");
126e0c4386eSCy Schubertok(!verify("ee-cert", "sslserver", [qw(root-cert)], [qw(ca-nonbc)]),
127e0c4386eSCy Schubert   "fail non-CA untrusted intermediate");
128e0c4386eSCy Schubertok(!verify("ee-cert", "sslserver", [qw(root-cert ca-nonca)], []),
129e0c4386eSCy Schubert   "fail non-CA trust-store intermediate");
130e0c4386eSCy Schubertok(!verify("ee-cert", "sslserver", [qw(root-cert ca-nonbc)], []),
131e0c4386eSCy Schubert   "fail non-CA trust-store intermediate");
132e0c4386eSCy Schubertok(!verify("ee-cert", "sslserver", [qw(root-cert nca+serverAuth)], []),
133e0c4386eSCy Schubert   "fail non-CA server trust intermediate");
134e0c4386eSCy Schubertok(!verify("ee-cert", "sslserver", [qw(root-cert nca+anyEKU)], []),
135e0c4386eSCy Schubert   "fail non-CA wildcard trust intermediate");
136e0c4386eSCy Schubertok(!verify("ee-cert", "sslserver", [qw(root-cert)], [qw(ca-cert2)]),
137e0c4386eSCy Schubert   "fail wrong intermediate CA key");
138e0c4386eSCy Schubertok(!verify("ee-cert", "sslserver", [qw(root-cert)], [qw(ca-name2)]),
139e0c4386eSCy Schubert   "fail wrong intermediate CA DN");
140e0c4386eSCy Schubertok(!verify("ee-cert", "sslserver", [qw(root-cert)], [qw(ca-root2)]),
141e0c4386eSCy Schubert   "fail wrong intermediate CA issuer");
142e0c4386eSCy Schubertok(!verify("ee-cert", "sslserver", [], [qw(ca-cert)], "-partial_chain"),
143e0c4386eSCy Schubert   "fail untrusted partial chain");
144e0c4386eSCy Schubertok(verify("ee-cert", "sslserver", [qw(ca-cert)], [], "-partial_chain"),
145e0c4386eSCy Schubert   "accept trusted partial chain");
146e0c4386eSCy Schubertok(!verify("ee-cert", "sslserver", [qw(ca-expired)], [], "-partial_chain"),
147e0c4386eSCy Schubert   "reject expired trusted partial chain"); # this check is beyond RFC 5280
148e0c4386eSCy Schubertok(!verify("ee-cert", "sslserver", [qw(root-expired)], [qw(ca-cert)]),
149e0c4386eSCy Schubert   "reject expired trusted root"); # this check is beyond RFC 5280
150e0c4386eSCy Schubertok(verify("ee-cert", "sslserver", [qw(sca-cert)], [], "-partial_chain"),
151e0c4386eSCy Schubert   "accept partial chain with server purpose");
152e0c4386eSCy Schubertok(!verify("ee-cert", "sslserver", [qw(cca-cert)], [], "-partial_chain"),
153*0d0c8621SEnji Cooper   "fail partial chain with client purpose"); # beware, questionable non-standard EKU check on trust anchor
154e0c4386eSCy Schubertok(verify("ee-cert", "sslserver", [qw(ca+serverAuth)], [], "-partial_chain"),
155e0c4386eSCy Schubert   "accept server trust partial chain");
156e0c4386eSCy Schubertok(verify("ee-cert", "sslserver", [qw(cca+serverAuth)], [], "-partial_chain"),
157e0c4386eSCy Schubert   "accept server trust client purpose partial chain");
158e0c4386eSCy Schubertok(verify("ee-cert", "sslserver", [qw(ca-clientAuth)], [], "-partial_chain"),
159e0c4386eSCy Schubert   "accept client mistrust partial chain");
160e0c4386eSCy Schubertok(verify("ee-cert", "sslserver", [qw(ca+anyEKU)], [], "-partial_chain"),
161e0c4386eSCy Schubert   "accept wildcard trust partial chain");
162e0c4386eSCy Schubertok(!verify("ee-cert", "sslserver", [], [qw(ca+serverAuth)], "-partial_chain"),
163e0c4386eSCy Schubert   "fail untrusted partial issuer with ignored server trust");
164e0c4386eSCy Schubertok(!verify("ee-cert", "sslserver", [qw(ca-serverAuth)], [], "-partial_chain"),
165e0c4386eSCy Schubert   "fail server mistrust partial chain");
166e0c4386eSCy Schubertok(!verify("ee-cert", "sslserver", [qw(ca+clientAuth)], [], "-partial_chain"),
167e0c4386eSCy Schubert   "fail client trust partial chain");
168e0c4386eSCy Schubertok(!verify("ee-cert", "sslserver", [qw(ca-anyEKU)], [], "-partial_chain"),
169e0c4386eSCy Schubert   "fail wildcard mistrust partial chain");
170e0c4386eSCy Schubert
171e0c4386eSCy Schubert# We now test auxiliary trust even for intermediate trusted certs without
172e0c4386eSCy Schubert# -partial_chain.  Note that "-trusted_first" is now always on and cannot
173e0c4386eSCy Schubert# be disabled.
174e0c4386eSCy Schubertok(verify("ee-cert", "sslserver", [qw(root-cert ca+serverAuth)], [qw(ca-cert)]),
175e0c4386eSCy Schubert   "accept server trust");
176e0c4386eSCy Schubertok(verify("ee-cert", "sslserver", [qw(root-cert ca+anyEKU)], [qw(ca-cert)]),
177e0c4386eSCy Schubert   "accept wildcard trust");
178e0c4386eSCy Schubertok(verify("ee-cert", "sslserver", [qw(root-cert sca-cert)], [qw(ca-cert)]),
179e0c4386eSCy Schubert   "accept server purpose");
180e0c4386eSCy Schubertok(verify("ee-cert", "sslserver", [qw(root-cert sca+serverAuth)], [qw(ca-cert)]),
181e0c4386eSCy Schubert   "accept server trust and purpose");
182e0c4386eSCy Schubertok(verify("ee-cert", "sslserver", [qw(root-cert sca+anyEKU)], [qw(ca-cert)]),
183e0c4386eSCy Schubert   "accept wildcard trust and server purpose");
184e0c4386eSCy Schubertok(verify("ee-cert", "sslserver", [qw(root-cert sca-clientAuth)], [qw(ca-cert)]),
185e0c4386eSCy Schubert   "accept client mistrust and server purpose");
186e0c4386eSCy Schubertok(verify("ee-cert", "sslserver", [qw(root-cert cca+serverAuth)], [qw(ca-cert)]),
187e0c4386eSCy Schubert   "accept server trust and client purpose");
188e0c4386eSCy Schubertok(verify("ee-cert", "sslserver", [qw(root-cert cca+anyEKU)], [qw(ca-cert)]),
189e0c4386eSCy Schubert   "accept wildcard trust and client purpose");
190e0c4386eSCy Schubertok(!verify("ee-cert", "sslserver", [qw(root-cert cca-cert)], [qw(ca-cert)]),
191*0d0c8621SEnji Cooper   "fail client purpose intermediate trusted"); # beware, questionable non-standard EKU check on trust anchor
192e0c4386eSCy Schubertok(!verify("ee-cert", "sslserver", [qw(root-cert ca-anyEKU)], [qw(ca-cert)]),
193e0c4386eSCy Schubert   "fail wildcard mistrust");
194e0c4386eSCy Schubertok(!verify("ee-cert", "sslserver", [qw(root-cert ca-serverAuth)], [qw(ca-cert)]),
195e0c4386eSCy Schubert   "fail server mistrust");
196e0c4386eSCy Schubertok(!verify("ee-cert", "sslserver", [qw(root-cert ca+clientAuth)], [qw(ca-cert)]),
197e0c4386eSCy Schubert   "fail client trust");
198e0c4386eSCy Schubertok(!verify("ee-cert", "sslserver", [qw(root-cert sca+clientAuth)], [qw(ca-cert)]),
199e0c4386eSCy Schubert   "fail client trust and server purpose");
200e0c4386eSCy Schubertok(!verify("ee-cert", "sslserver", [qw(root-cert cca+clientAuth)], [qw(ca-cert)]),
201e0c4386eSCy Schubert   "fail client trust and client purpose");
202e0c4386eSCy Schubertok(!verify("ee-cert", "sslserver", [qw(root-cert cca-serverAuth)], [qw(ca-cert)]),
203e0c4386eSCy Schubert   "fail server mistrust and client purpose");
204e0c4386eSCy Schubertok(!verify("ee-cert", "sslserver", [qw(root-cert cca-clientAuth)], [qw(ca-cert)]),
205e0c4386eSCy Schubert   "fail client mistrust and client purpose");
206e0c4386eSCy Schubertok(!verify("ee-cert", "sslserver", [qw(root-cert sca-serverAuth)], [qw(ca-cert)]),
207e0c4386eSCy Schubert   "fail server mistrust and server purpose");
208e0c4386eSCy Schubertok(!verify("ee-cert", "sslserver", [qw(root-cert sca-anyEKU)], [qw(ca-cert)]),
209e0c4386eSCy Schubert   "fail wildcard mistrust and server purpose");
210e0c4386eSCy Schubertok(!verify("ee-cert", "sslserver", [qw(root-cert cca-anyEKU)], [qw(ca-cert)]),
211e0c4386eSCy Schubert   "fail wildcard mistrust and client purpose");
212e0c4386eSCy Schubert
213e0c4386eSCy Schubert# EE variants
214e0c4386eSCy Schubertok(verify("ee-client", "sslclient", [qw(root-cert)], [qw(ca-cert)]),
215e0c4386eSCy Schubert   "accept client chain");
216e0c4386eSCy Schubertok(!verify("ee-client", "sslserver", [qw(root-cert)], [qw(ca-cert)]),
217e0c4386eSCy Schubert   "fail server leaf purpose");
218e0c4386eSCy Schubertok(!verify("ee-cert", "sslclient", [qw(root-cert)], [qw(ca-cert)]),
219e0c4386eSCy Schubert   "fail client leaf purpose");
220e0c4386eSCy Schubertok(!verify("ee-cert2", "sslserver", [qw(root-cert)], [qw(ca-cert)]),
221e0c4386eSCy Schubert   "fail wrong intermediate CA key");
222e0c4386eSCy Schubertok(!verify("ee-name2", "sslserver", [qw(root-cert)], [qw(ca-cert)]),
223e0c4386eSCy Schubert   "fail wrong intermediate CA DN");
224e0c4386eSCy Schubertok(!verify("ee-expired", "sslserver", [qw(root-cert)], [qw(ca-cert)]),
225e0c4386eSCy Schubert   "fail expired leaf");
226e0c4386eSCy Schubertok(verify("ee-cert", "sslserver", [qw(ee-cert)], [], "-partial_chain"),
227e0c4386eSCy Schubert   "accept last-resort direct leaf match");
228e0c4386eSCy Schubertok(verify("ee-client", "sslclient", [qw(ee-client)], [], "-partial_chain"),
229e0c4386eSCy Schubert   "accept last-resort direct leaf match");
230e0c4386eSCy Schubertok(!verify("ee-cert", "sslserver", [qw(ee-client)], [], "-partial_chain"),
231e0c4386eSCy Schubert   "fail last-resort direct leaf non-match");
232e0c4386eSCy Schubertok(verify("ee-cert", "sslserver", [qw(ee+serverAuth)], [], "-partial_chain"),
233e0c4386eSCy Schubert   "accept direct match with server trust");
234e0c4386eSCy Schubertok(!verify("ee-cert", "sslserver", [qw(ee-serverAuth)], [], "-partial_chain"),
235e0c4386eSCy Schubert   "fail direct match with server mistrust");
236e0c4386eSCy Schubertok(verify("ee-client", "sslclient", [qw(ee+clientAuth)], [], "-partial_chain"),
237e0c4386eSCy Schubert   "accept direct match with client trust");
238e0c4386eSCy Schubertok(!verify("ee-client", "sslclient", [qw(ee-clientAuth)], [], "-partial_chain"),
239e0c4386eSCy Schubert   "reject direct match with client mistrust");
240e0c4386eSCy Schubertok(verify("ee-pathlen", "sslserver", [qw(root-cert)], [qw(ca-cert)]),
241e0c4386eSCy Schubert   "accept non-ca with pathlen:0 by default");
242e0c4386eSCy Schubertok(!verify("ee-pathlen", "sslserver", [qw(root-cert)], [qw(ca-cert)], "-x509_strict"),
243e0c4386eSCy Schubert   "reject non-ca with pathlen:0 with strict flag");
244e0c4386eSCy Schubert
245e0c4386eSCy Schubert# Proxy certificates
246e0c4386eSCy Schubertok(!verify("pc1-cert", "sslclient", [qw(root-cert)], [qw(ee-client ca-cert)]),
247e0c4386eSCy Schubert   "fail to accept proxy cert without -allow_proxy_certs");
248e0c4386eSCy Schubertok(verify("pc1-cert", "sslclient", [qw(root-cert)], [qw(ee-client ca-cert)],
249e0c4386eSCy Schubert          "-allow_proxy_certs"),
250e0c4386eSCy Schubert   "accept proxy cert 1");
251e0c4386eSCy Schubertok(verify("pc2-cert", "sslclient", [qw(root-cert)], [qw(pc1-cert ee-client ca-cert)],
252e0c4386eSCy Schubert          "-allow_proxy_certs"),
253e0c4386eSCy Schubert   "accept proxy cert 2");
254e0c4386eSCy Schubertok(!verify("bad-pc3-cert", "sslclient", [qw(root-cert)], [qw(pc1-cert ee-client ca-cert)],
255e0c4386eSCy Schubert          "-allow_proxy_certs"),
256e0c4386eSCy Schubert   "fail proxy cert with incorrect subject");
257e0c4386eSCy Schubertok(!verify("bad-pc4-cert", "sslclient", [qw(root-cert)], [qw(pc1-cert ee-client ca-cert)],
258e0c4386eSCy Schubert          "-allow_proxy_certs"),
259e0c4386eSCy Schubert   "fail proxy cert with incorrect pathlen");
260e0c4386eSCy Schubertok(verify("pc5-cert", "sslclient", [qw(root-cert)], [qw(pc1-cert ee-client ca-cert)],
261e0c4386eSCy Schubert          "-allow_proxy_certs"),
262e0c4386eSCy Schubert   "accept proxy cert missing proxy policy");
263e0c4386eSCy Schubertok(!verify("pc6-cert", "sslclient", [qw(root-cert)], [qw(pc1-cert ee-client ca-cert)],
264e0c4386eSCy Schubert          "-allow_proxy_certs"),
265e0c4386eSCy Schubert   "failed proxy cert where last CN was added as a multivalue RDN component");
266e0c4386eSCy Schubert
267e0c4386eSCy Schubert# Security level tests
268e0c4386eSCy Schubertok(verify("ee-cert", "", ["root-cert"], ["ca-cert"], "-auth_level", "2"),
269e0c4386eSCy Schubert   "accept RSA 2048 chain at auth level 2");
270e0c4386eSCy Schubertok(!verify("ee-cert", "", ["root-cert"], ["ca-cert"], "-auth_level", "3"),
271e0c4386eSCy Schubert   "reject RSA 2048 root at auth level 3");
272e0c4386eSCy Schubertok(verify("ee-cert", "", ["root-cert-768"], ["ca-cert-768i"], "-auth_level", "0"),
273e0c4386eSCy Schubert   "accept RSA 768 root at auth level 0");
274e0c4386eSCy Schubertok(!verify("ee-cert", "", ["root-cert-768"], ["ca-cert-768i"]),
275e0c4386eSCy Schubert   "reject RSA 768 root at auth level 1");
276e0c4386eSCy Schubertok(verify("ee-cert-768i", "", ["root-cert"], ["ca-cert-768"], "-auth_level", "0"),
277e0c4386eSCy Schubert   "accept RSA 768 intermediate at auth level 0");
278e0c4386eSCy Schubertok(!verify("ee-cert-768i", "", ["root-cert"], ["ca-cert-768"]),
279e0c4386eSCy Schubert   "reject RSA 768 intermediate at auth level 1");
280e0c4386eSCy Schubertok(verify("ee-cert-768", "", ["root-cert"], ["ca-cert"], "-auth_level", "0"),
281e0c4386eSCy Schubert   "accept RSA 768 leaf at auth level 0");
282e0c4386eSCy Schubertok(!verify("ee-cert-768", "", ["root-cert"], ["ca-cert"]),
283e0c4386eSCy Schubert   "reject RSA 768 leaf at auth level 1");
284e0c4386eSCy Schubert#
285e0c4386eSCy Schubertok(verify("ee-cert", "", ["root-cert-md5"], ["ca-cert"], "-auth_level", "2"),
286e0c4386eSCy Schubert   "accept md5 self-signed TA at auth level 2");
287e0c4386eSCy Schubertok(verify("ee-cert", "", ["ca-cert-md5-any"], [], "-auth_level", "2"),
288e0c4386eSCy Schubert   "accept md5 intermediate TA at auth level 2");
289e0c4386eSCy Schubertok(verify("ee-cert", "", ["root-cert"], ["ca-cert-md5"], "-auth_level", "0"),
290e0c4386eSCy Schubert   "accept md5 intermediate at auth level 0");
291e0c4386eSCy Schubertok(!verify("ee-cert", "", ["root-cert"], ["ca-cert-md5"]),
292e0c4386eSCy Schubert   "reject md5 intermediate at auth level 1");
293e0c4386eSCy Schubertok(verify("ee-cert-md5", "", ["root-cert"], ["ca-cert"], "-auth_level", "0"),
294e0c4386eSCy Schubert   "accept md5 leaf at auth level 0");
295e0c4386eSCy Schubertok(!verify("ee-cert-md5", "", ["root-cert"], ["ca-cert"]),
296e0c4386eSCy Schubert   "reject md5 leaf at auth level 1");
297e0c4386eSCy Schubert
298e0c4386eSCy Schubert# Explicit vs named curve tests
299e0c4386eSCy SchubertSKIP: {
300e0c4386eSCy Schubert    skip "EC is not supported by this OpenSSL build", 3
301e0c4386eSCy Schubert        if disabled("ec");
302e0c4386eSCy Schubert    ok(!verify("ee-cert-ec-explicit", "", ["root-cert"],
303e0c4386eSCy Schubert               ["ca-cert-ec-named"]),
304e0c4386eSCy Schubert        "reject explicit curve leaf with named curve intermediate");
305e0c4386eSCy Schubert    ok(!verify("ee-cert-ec-named-explicit", "", ["root-cert"],
306e0c4386eSCy Schubert               ["ca-cert-ec-explicit"]),
307e0c4386eSCy Schubert        "reject named curve leaf with explicit curve intermediate");
308e0c4386eSCy Schubert    ok(verify("ee-cert-ec-named-named", "", ["root-cert"],
309e0c4386eSCy Schubert              ["ca-cert-ec-named"]),
310e0c4386eSCy Schubert        "accept named curve leaf with named curve intermediate");
311e0c4386eSCy Schubert}
312e0c4386eSCy Schubert# Same as above but with base provider used for decoding
313e0c4386eSCy SchubertSKIP: {
314e0c4386eSCy Schubert    my $no_fips = disabled('fips') || ($ENV{NO_FIPS} // 0);
315e0c4386eSCy Schubert    my $provconf = srctop_file("test", "fips-and-base.cnf");
316e0c4386eSCy Schubert    my $provpath = bldtop_dir("providers");
317e0c4386eSCy Schubert    my @prov = ("-provider-path", $provpath);
318e0c4386eSCy Schubert
319e0c4386eSCy Schubert    skip "EC is not supported or FIPS is disabled", 3
320e0c4386eSCy Schubert        if disabled("ec") || $no_fips;
321e0c4386eSCy Schubert
322e0c4386eSCy Schubert    run(test(["fips_version_test", "-config", $provconf, ">3.0.0"]),
323e0c4386eSCy Schubert             capture => 1, statusvar => \my $exit);
324e0c4386eSCy Schubert    skip "FIPS provider version is too old", 3
325e0c4386eSCy Schubert        if !$exit;
326e0c4386eSCy Schubert
327e0c4386eSCy Schubert    $ENV{OPENSSL_CONF} = $provconf;
328e0c4386eSCy Schubert
329e0c4386eSCy Schubert    ok(!verify("ee-cert-ec-explicit", "", ["root-cert"],
330e0c4386eSCy Schubert               ["ca-cert-ec-named"], @prov),
331e0c4386eSCy Schubert        "reject explicit curve leaf with named curve intermediate w/fips");
332e0c4386eSCy Schubert    ok(!verify("ee-cert-ec-named-explicit", "", ["root-cert"],
333e0c4386eSCy Schubert               ["ca-cert-ec-explicit"], @prov),
334e0c4386eSCy Schubert        "reject named curve leaf with explicit curve intermediate w/fips");
335e0c4386eSCy Schubert    ok(verify("ee-cert-ec-named-named", "", ["root-cert"],
336e0c4386eSCy Schubert              ["ca-cert-ec-named"], @prov),
337e0c4386eSCy Schubert        "accept named curve leaf with named curve intermediate w/fips");
338e0c4386eSCy Schubert
339e0c4386eSCy Schubert    delete $ENV{OPENSSL_CONF};
340e0c4386eSCy Schubert}
341e0c4386eSCy Schubert
342e0c4386eSCy Schubert# Depth tests, note the depth limit bounds the number of CA certificates
343e0c4386eSCy Schubert# between the trust-anchor and the leaf, so, for example, with a root->ca->leaf
344e0c4386eSCy Schubert# chain, depth = 1 is sufficient, but depth == 0 is not.
345e0c4386eSCy Schubert#
346e0c4386eSCy Schubertok(verify("ee-cert", "", ["root-cert"], ["ca-cert"], "-verify_depth", "2"),
347e0c4386eSCy Schubert   "accept chain with verify_depth 2");
348e0c4386eSCy Schubertok(verify("ee-cert", "", ["root-cert"], ["ca-cert"], "-verify_depth", "1"),
349e0c4386eSCy Schubert   "accept chain with verify_depth 1");
350e0c4386eSCy Schubertok(!verify("ee-cert", "", ["root-cert"], ["ca-cert"], "-verify_depth", "0"),
351e0c4386eSCy Schubert   "reject chain with verify_depth 0");
352e0c4386eSCy Schubertok(verify("ee-cert", "", ["ca-cert-md5-any"], [], "-verify_depth", "0"),
353e0c4386eSCy Schubert   "accept md5 intermediate TA with verify_depth 0");
354e0c4386eSCy Schubert
355e0c4386eSCy Schubert# Name Constraints tests.
356e0c4386eSCy Schubert
357e0c4386eSCy Schubertok(verify("alt1-cert", "", ["root-cert"], ["ncca1-cert"], ),
358e0c4386eSCy Schubert   "Name Constraints everything permitted");
359e0c4386eSCy Schubert
360e0c4386eSCy Schubertok(verify("alt2-cert", "", ["root-cert"], ["ncca2-cert"], ),
361e0c4386eSCy Schubert   "Name Constraints nothing excluded");
362e0c4386eSCy Schubert
363e0c4386eSCy Schubertok(verify("alt3-cert", "", ["root-cert"], ["ncca1-cert", "ncca3-cert"], ),
364e0c4386eSCy Schubert   "Name Constraints nested test all permitted");
365e0c4386eSCy Schubert
366e0c4386eSCy Schubertok(verify("goodcn1-cert", "", ["root-cert"], ["ncca1-cert"], ),
367e0c4386eSCy Schubert   "Name Constraints CNs permitted");
368e0c4386eSCy Schubert
369e0c4386eSCy Schubertok(verify("goodcn2-cert", "", ["root-cert"], ["ncca1-cert"], ),
370e0c4386eSCy Schubert   "Name Constraints CNs permitted - no SAN extension");
371e0c4386eSCy Schubert
372e0c4386eSCy Schubertok(!verify("badcn1-cert", "", ["root-cert"], ["ncca1-cert"], ),
373e0c4386eSCy Schubert   "Name Constraints CNs not permitted");
374e0c4386eSCy Schubert
375e0c4386eSCy Schubertok(!verify("badalt1-cert", "", ["root-cert"], ["ncca1-cert"], ),
376e0c4386eSCy Schubert   "Name Constraints hostname not permitted");
377e0c4386eSCy Schubert
378e0c4386eSCy Schubertok(!verify("badalt2-cert", "", ["root-cert"], ["ncca2-cert"], ),
379e0c4386eSCy Schubert   "Name Constraints hostname excluded");
380e0c4386eSCy Schubert
381e0c4386eSCy Schubertok(!verify("badalt3-cert", "", ["root-cert"], ["ncca1-cert"], ),
382e0c4386eSCy Schubert   "Name Constraints email address not permitted");
383e0c4386eSCy Schubert
384e0c4386eSCy Schubertok(!verify("badalt4-cert", "", ["root-cert"], ["ncca1-cert"], ),
385e0c4386eSCy Schubert   "Name Constraints subject email address not permitted");
386e0c4386eSCy Schubert
387e0c4386eSCy Schubertok(!verify("badalt5-cert", "", ["root-cert"], ["ncca1-cert"], ),
388e0c4386eSCy Schubert   "Name Constraints IP address not permitted");
389e0c4386eSCy Schubert
390e0c4386eSCy Schubertok(!verify("badalt6-cert", "", ["root-cert"], ["ncca1-cert"], ),
391e0c4386eSCy Schubert   "Name Constraints CN hostname not permitted");
392e0c4386eSCy Schubert
393e0c4386eSCy Schubertok(!verify("badalt7-cert", "", ["root-cert"], ["ncca1-cert"], ),
394e0c4386eSCy Schubert   "Name Constraints CN BMPSTRING hostname not permitted");
395e0c4386eSCy Schubert
396e0c4386eSCy Schubertok(!verify("badalt8-cert", "", ["root-cert"], ["ncca1-cert", "ncca3-cert"], ),
397e0c4386eSCy Schubert   "Name constraints nested DNS name not permitted 1");
398e0c4386eSCy Schubert
399e0c4386eSCy Schubertok(!verify("badalt9-cert", "", ["root-cert"], ["ncca1-cert", "ncca3-cert"], ),
400e0c4386eSCy Schubert   "Name constraints nested DNS name not permitted 2");
401e0c4386eSCy Schubert
402e0c4386eSCy Schubertok(!verify("badalt10-cert", "", ["root-cert"], ["ncca1-cert", "ncca3-cert"], ),
403e0c4386eSCy Schubert   "Name constraints nested DNS name excluded");
404e0c4386eSCy Schubert
405e0c4386eSCy Schubertok(!verify("bad-othername-cert", "", ["root-cert"], ["nccaothername-cert"], ),
406e0c4386eSCy Schubert   "CVE-2022-4203 type confusion test");
407e0c4386eSCy Schubert
408e0c4386eSCy Schubert#Check that we get the expected failure return code
409e0c4386eSCy Schubertwith({ exit_checker => sub { return shift == 2; } },
410e0c4386eSCy Schubert     sub {
411e0c4386eSCy Schubert         ok(verify("bad-othername-namec", "", ["bad-othername-namec-inter"], [],
412e0c4386eSCy Schubert                   "-partial_chain", "-attime", "1623060000"),
413e0c4386eSCy Schubert            "Name constraints bad othername name constraint");
414e0c4386eSCy Schubert     });
415e0c4386eSCy Schubert
416e0c4386eSCy Schubertok(verify("ee-pss-sha1-cert", "", ["root-cert"], ["ca-cert"], "-auth_level", "0"),
417e0c4386eSCy Schubert    "Accept PSS signature using SHA1 at auth level 0");
418e0c4386eSCy Schubert
419e0c4386eSCy Schubertok(verify("ee-pss-sha256-cert", "", ["root-cert"], ["ca-cert"], ),
420e0c4386eSCy Schubert    "CA with PSS signature using SHA256");
421e0c4386eSCy Schubert
422e0c4386eSCy Schubertok(!verify("ee-pss-sha1-cert", "", ["root-cert"], ["ca-cert"], "-auth_level", "1"),
423e0c4386eSCy Schubert    "Reject PSS signature using SHA1 and auth level 1");
424e0c4386eSCy Schubert
425e0c4386eSCy Schubertok(verify("ee-pss-sha256-cert", "", ["root-cert"], ["ca-cert"], "-auth_level", "2"),
426e0c4386eSCy Schubert    "PSS signature using SHA256 and auth level 2");
427e0c4386eSCy Schubert
428e0c4386eSCy Schubertok(verify("ee-pss-cert", "", ["root-cert"], ["ca-pss-cert"], ),
429e0c4386eSCy Schubert    "CA PSS signature");
430e0c4386eSCy Schubertok(!verify("ee-pss-wrong1.5-cert", "", ["root-cert"], ["ca-pss-cert"], ),
431e0c4386eSCy Schubert    "CA producing regular PKCS#1 v1.5 signature with PSA-PSS key");
432e0c4386eSCy Schubert
433e0c4386eSCy Schubertok(!verify("many-names1", "", ["many-constraints"], ["many-constraints"], ),
434e0c4386eSCy Schubert    "Too many names and constraints to check (1)");
435e0c4386eSCy Schubertok(!verify("many-names2", "", ["many-constraints"], ["many-constraints"], ),
436e0c4386eSCy Schubert    "Too many names and constraints to check (2)");
437e0c4386eSCy Schubertok(!verify("many-names3", "", ["many-constraints"], ["many-constraints"], ),
438e0c4386eSCy Schubert    "Too many names and constraints to check (3)");
439e0c4386eSCy Schubert
440e0c4386eSCy Schubertok(verify("some-names1", "", ["many-constraints"], ["many-constraints"], ),
441e0c4386eSCy Schubert    "Not too many names and constraints to check (1)");
442e0c4386eSCy Schubertok(verify("some-names2", "", ["many-constraints"], ["many-constraints"], ),
443e0c4386eSCy Schubert    "Not too many names and constraints to check (2)");
444e0c4386eSCy Schubertok(verify("some-names2", "", ["many-constraints"], ["many-constraints"], ),
445e0c4386eSCy Schubert    "Not too many names and constraints to check (3)");
446e0c4386eSCy Schubertok(verify("root-cert-rsa2", "", ["root-cert-rsa2"], [], "-check_ss_sig"),
447e0c4386eSCy Schubert    "Public Key Algorithm rsa instead of rsaEncryption");
448e0c4386eSCy Schubert
449e0c4386eSCy Schubertok(verify("ee-self-signed", "", ["ee-self-signed"], [], "-attime", "1593565200"),
450e0c4386eSCy Schubert   "accept trusted self-signed EE cert excluding key usage keyCertSign");
451e0c4386eSCy Schubertok(verify("ee-ss-with-keyCertSign", "", ["ee-ss-with-keyCertSign"], []),
452e0c4386eSCy Schubert   "accept trusted self-signed EE cert with key usage keyCertSign also when strict");
453e0c4386eSCy Schubert
454e0c4386eSCy SchubertSKIP: {
455e0c4386eSCy Schubert    skip "Ed25519 is not supported by this OpenSSL build", 6
456e0c4386eSCy Schubert        if disabled("ec");
457e0c4386eSCy Schubert
458e0c4386eSCy Schubert    # ED25519 certificate from draft-ietf-curdle-pkix-04
459e0c4386eSCy Schubert    ok(verify("ee-ed25519", "", ["root-ed25519"], []),
460e0c4386eSCy Schubert       "accept X25519 EE cert issued by trusted Ed25519 self-signed CA cert");
461e0c4386eSCy Schubert
462e0c4386eSCy Schubert    ok(!verify("ee-ed25519", "", ["root-ed25519"], [], "-x509_strict"),
463e0c4386eSCy Schubert       "reject X25519 EE cert in strict mode since AKID is missing");
464e0c4386eSCy Schubert
465e0c4386eSCy Schubert    ok(!verify("root-ed25519", "", ["ee-ed25519"], []),
466e0c4386eSCy Schubert       "fail Ed25519 CA and EE certs swapped");
467e0c4386eSCy Schubert
468e0c4386eSCy Schubert    ok(verify("root-ed25519", "", ["root-ed25519"], []),
469e0c4386eSCy Schubert       "accept trusted Ed25519 self-signed CA cert");
470e0c4386eSCy Schubert
471e0c4386eSCy Schubert    ok(!verify("ee-ed25519", "", ["ee-ed25519"], []),
472e0c4386eSCy Schubert       "fail trusted Ed25519-signed self-issued X25519 cert");
473e0c4386eSCy Schubert
474e0c4386eSCy Schubert    ok(verify("ee-ed25519", "", ["ee-ed25519"], [], "-partial_chain"),
475e0c4386eSCy Schubert       "accept last-resort direct leaf match Ed25519-signed self-issued cert");
476e0c4386eSCy Schubert
477e0c4386eSCy Schubert}
478e0c4386eSCy Schubert
479e0c4386eSCy SchubertSKIP: {
480e0c4386eSCy Schubert    skip "SM2 is not supported by this OpenSSL build", 2 if disabled("sm2");
481e0c4386eSCy Schubert
482e0c4386eSCy Schubert   ok_nofips(verify("sm2", "", ["sm2-ca-cert"], [], "-vfyopt", "distid:1234567812345678"),
483e0c4386eSCy Schubert       "SM2 ID test");
484e0c4386eSCy Schubert   ok_nofips(verify("sm2", "", ["sm2-ca-cert"], [], "-vfyopt", "hexdistid:31323334353637383132333435363738"),
485e0c4386eSCy Schubert       "SM2 hex ID test");
486e0c4386eSCy Schubert}
487e0c4386eSCy Schubert
488e0c4386eSCy Schubert# Mixed content tests
489e0c4386eSCy Schubertmy $cert_file = srctop_file('test', 'certs', 'root-cert.pem');
490e0c4386eSCy Schubertmy $rsa_file = srctop_file('test', 'certs', 'key-pass-12345.pem');
491e0c4386eSCy Schubert
492e0c4386eSCy SchubertSKIP: {
493e0c4386eSCy Schubert    my $certplusrsa_file = 'certplusrsa.pem';
494e0c4386eSCy Schubert    my $certplusrsa;
495e0c4386eSCy Schubert
496e0c4386eSCy Schubert    skip "Couldn't create certplusrsa.pem", 1
497e0c4386eSCy Schubert        unless ( open $certplusrsa, '>', $certplusrsa_file
498e0c4386eSCy Schubert                 and copy($cert_file, $certplusrsa)
499e0c4386eSCy Schubert                 and copy($rsa_file, $certplusrsa)
500e0c4386eSCy Schubert                 and close $certplusrsa );
501e0c4386eSCy Schubert
502e0c4386eSCy Schubert    ok(run(app([ qw(openssl verify -trusted), $certplusrsa_file, $cert_file ])),
503e0c4386eSCy Schubert       'Mixed cert + key file test');
504e0c4386eSCy Schubert}
505e0c4386eSCy Schubert
506e0c4386eSCy SchubertSKIP: {
507e0c4386eSCy Schubert    my $rsapluscert_file = 'rsapluscert.pem';
508e0c4386eSCy Schubert    my $rsapluscert;
509e0c4386eSCy Schubert
510e0c4386eSCy Schubert    skip "Couldn't create rsapluscert.pem", 1
511e0c4386eSCy Schubert        unless ( open $rsapluscert, '>', $rsapluscert_file
512e0c4386eSCy Schubert                 and copy($rsa_file, $rsapluscert)
513e0c4386eSCy Schubert                 and copy($cert_file, $rsapluscert)
514e0c4386eSCy Schubert                 and close $rsapluscert );
515e0c4386eSCy Schubert
516e0c4386eSCy Schubert    ok(run(app([ qw(openssl verify -trusted), $rsapluscert_file, $cert_file ])),
517e0c4386eSCy Schubert       'Mixed key + cert file test');
518e0c4386eSCy Schubert}
519e0c4386eSCy Schubert
520e0c4386eSCy Schubert# Certificate Policies
521e0c4386eSCy Schubertok(verify("ee-cert-policies", "", ["root-cert"], ["ca-pol-cert"],
522e0c4386eSCy Schubert          "-policy_check", "-policy", "1.3.6.1.4.1.16604.998855.1",
523e0c4386eSCy Schubert          "-explicit_policy"),
524e0c4386eSCy Schubert   "Certificate policy");
525e0c4386eSCy Schubert
526e0c4386eSCy Schubertok(!verify("ee-cert-policies-bad", "", ["root-cert"], ["ca-pol-cert"],
527e0c4386eSCy Schubert           "-policy_check", "-policy", "1.3.6.1.4.1.16604.998855.1",
528e0c4386eSCy Schubert           "-explicit_policy"),
529e0c4386eSCy Schubert   "Bad certificate policy");
530