xref: /freebsd/crypto/openssl/test/recipes/80-test_pkcs12.t (revision 44096ebd22ddd0081a357011714eff8963614b65)
1e0c4386eSCy Schubert#! /usr/bin/env perl
2e0c4386eSCy Schubert# Copyright 2016-2024 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 Schubertuse strict;
10e0c4386eSCy Schubertuse warnings;
11e0c4386eSCy Schubert
12e0c4386eSCy Schubertuse OpenSSL::Test qw/:DEFAULT srctop_file with/;
13e0c4386eSCy Schubertuse OpenSSL::Test::Utils;
14e0c4386eSCy Schubert
15e0c4386eSCy Schubertuse Encode;
16e0c4386eSCy Schubert
17e0c4386eSCy Schubertsetup("test_pkcs12");
18e0c4386eSCy Schubert
19e0c4386eSCy Schubertmy $pass = "σύνθημα γνώρισμα";
20e0c4386eSCy Schubert
21e0c4386eSCy Schubertmy $savedcp;
22e0c4386eSCy Schubertif (eval { require Win32::API; 1; }) {
23e0c4386eSCy Schubert    # Trouble is that Win32 perl uses CreateProcessA, which
24e0c4386eSCy Schubert    # makes it problematic to pass non-ASCII arguments, from perl[!]
25e0c4386eSCy Schubert    # that is. This is because CreateProcessA is just a wrapper for
26e0c4386eSCy Schubert    # CreateProcessW and will call MultiByteToWideChar and use
27e0c4386eSCy Schubert    # system default locale. Since we attempt Greek pass-phrase
28e0c4386eSCy Schubert    # conversion can be done only with Greek locale.
29e0c4386eSCy Schubert
30e0c4386eSCy Schubert    Win32::API->Import("kernel32","UINT GetSystemDefaultLCID()");
31e0c4386eSCy Schubert    if (GetSystemDefaultLCID() != 0x408) {
32e0c4386eSCy Schubert        plan skip_all => "Non-Greek system locale";
33e0c4386eSCy Schubert    } else {
34e0c4386eSCy Schubert        # Ensure correct code page so that VERBOSE output is right.
35e0c4386eSCy Schubert        Win32::API->Import("kernel32","UINT GetConsoleOutputCP()");
36e0c4386eSCy Schubert        Win32::API->Import("kernel32","BOOL SetConsoleOutputCP(UINT cp)");
37e0c4386eSCy Schubert        $savedcp = GetConsoleOutputCP();
38e0c4386eSCy Schubert        SetConsoleOutputCP(1253);
39e0c4386eSCy Schubert        $pass = Encode::encode("cp1253",Encode::decode("utf-8",$pass));
40e0c4386eSCy Schubert    }
41e0c4386eSCy Schubert} elsif ($^O eq "MSWin32") {
42e0c4386eSCy Schubert    plan skip_all => "Win32::API unavailable";
43e0c4386eSCy Schubert} elsif ($^O ne "VMS") {
44e0c4386eSCy Schubert    # Running MinGW tests transparently under Wine apparently requires
45e0c4386eSCy Schubert    # UTF-8 locale...
46e0c4386eSCy Schubert
47e0c4386eSCy Schubert    foreach(`locale -a`) {
48e0c4386eSCy Schubert        s/\R$//;
49e0c4386eSCy Schubert        if ($_ =~ m/^C\.UTF\-?8/i) {
50e0c4386eSCy Schubert            $ENV{LC_ALL} = $_;
51e0c4386eSCy Schubert            last;
52e0c4386eSCy Schubert        }
53e0c4386eSCy Schubert    }
54e0c4386eSCy Schubert}
55e0c4386eSCy Schubert$ENV{OPENSSL_WIN32_UTF8}=1;
56e0c4386eSCy Schubert
57*44096ebdSEnji Cooperplan tests => 20;
58e0c4386eSCy Schubert
59e0c4386eSCy Schubert# Test different PKCS#12 formats
60e0c4386eSCy Schubertok(run(test(["pkcs12_format_test"])), "test pkcs12 formats");
61e0c4386eSCy Schubert# Test with legacy APIs
62e0c4386eSCy Schubertok(run(test(["pkcs12_format_test", "-legacy"])), "test pkcs12 formats using legacy APIs");
63e0c4386eSCy Schubert# Test with a non-default library context (and no loaded providers in the default context)
64e0c4386eSCy Schubertok(run(test(["pkcs12_format_test", "-context"])), "test pkcs12 formats using a non-default library context");
65e0c4386eSCy Schubert
66e0c4386eSCy SchubertSKIP: {
67e0c4386eSCy Schubert     skip "VMS doesn't have command line UTF-8 support yet in DCL", 1
68e0c4386eSCy Schubert         if $^O eq "VMS";
69e0c4386eSCy Schubert
70e0c4386eSCy Schubert     # just see that we can read shibboleth.pfx protected with $pass
71e0c4386eSCy Schubert     ok(run(app(["openssl", "pkcs12", "-noout",
72e0c4386eSCy Schubert                 "-password", "pass:$pass",
73e0c4386eSCy Schubert                 "-in", srctop_file("test", "shibboleth.pfx")])),
74e0c4386eSCy Schubert        "test_load_cert_pkcs12");
75e0c4386eSCy Schubert}
76e0c4386eSCy Schubert
77e0c4386eSCy Schubertmy @path = qw(test certs);
78e0c4386eSCy Schubertmy $outfile1 = "out1.p12";
79e0c4386eSCy Schubertmy $outfile2 = "out2.p12";
80e0c4386eSCy Schubertmy $outfile3 = "out3.p12";
81e0c4386eSCy Schubertmy $outfile4 = "out4.p12";
82e0c4386eSCy Schubertmy $outfile5 = "out5.p12";
83e0c4386eSCy Schubert
84e0c4386eSCy Schubert# Test the -chain option with -untrusted
85e0c4386eSCy Schubertok(run(app(["openssl", "pkcs12", "-export", "-chain",
86e0c4386eSCy Schubert            "-CAfile",  srctop_file(@path,  "sroot-cert.pem"),
87e0c4386eSCy Schubert            "-untrusted", srctop_file(@path, "ca-cert.pem"),
88e0c4386eSCy Schubert            "-in", srctop_file(@path, "ee-cert.pem"),
89e0c4386eSCy Schubert            "-nokeys", "-passout", "pass:", "-out", $outfile1])),
90e0c4386eSCy Schubert   "test_pkcs12_chain_untrusted");
91e0c4386eSCy Schubert
92e0c4386eSCy Schubert# Test the -passcerts option
93e0c4386eSCy SchubertSKIP: {
94e0c4386eSCy Schubert    skip "Skipping PKCS#12 test because DES is disabled in this build", 1
95e0c4386eSCy Schubert        if disabled("des");
96e0c4386eSCy Schubert    ok(run(app(["openssl", "pkcs12", "-export",
97e0c4386eSCy Schubert            "-in", srctop_file(@path, "ee-cert.pem"),
98e0c4386eSCy Schubert            "-certfile", srctop_file(@path, "v3-certs-TDES.p12"),
99e0c4386eSCy Schubert            "-passcerts", "pass:v3-certs",
100e0c4386eSCy Schubert            "-nokeys", "-passout", "pass:v3-certs", "-descert",
101e0c4386eSCy Schubert            "-out", $outfile2])),
102e0c4386eSCy Schubert   "test_pkcs12_passcerts");
103e0c4386eSCy Schubert}
104e0c4386eSCy Schubert
105e0c4386eSCy SchubertSKIP: {
106e0c4386eSCy Schubert    skip "Skipping legacy PKCS#12 test because the required algorithms are disabled", 1
107e0c4386eSCy Schubert        if disabled("des") || disabled("rc2") || disabled("legacy");
108e0c4386eSCy Schubert    # Test reading legacy PKCS#12 file
109e0c4386eSCy Schubert    ok(run(app(["openssl", "pkcs12", "-export",
110e0c4386eSCy Schubert                "-in", srctop_file(@path, "v3-certs-RC2.p12"),
111e0c4386eSCy Schubert                "-passin", "pass:v3-certs",
112e0c4386eSCy Schubert                "-provider", "default", "-provider", "legacy",
113e0c4386eSCy Schubert                "-nokeys", "-passout", "pass:v3-certs", "-descert",
114e0c4386eSCy Schubert                "-out", $outfile3])),
115e0c4386eSCy Schubert    "test_pkcs12_passcerts_legacy");
116e0c4386eSCy Schubert}
117e0c4386eSCy Schubert
118e0c4386eSCy Schubert# Test export of PEM file with both cert and key
119e0c4386eSCy Schubert# -nomac necessary to avoid legacy provider requirement
120e0c4386eSCy Schubertok(run(app(["openssl", "pkcs12", "-export",
121e0c4386eSCy Schubert        "-inkey", srctop_file(@path, "cert-key-cert.pem"),
122e0c4386eSCy Schubert        "-in", srctop_file(@path, "cert-key-cert.pem"),
123e0c4386eSCy Schubert        "-passout", "pass:v3-certs",
124e0c4386eSCy Schubert        "-nomac", "-out", $outfile4], stderr => "outerr.txt")),
125e0c4386eSCy Schubert   "test_export_pkcs12_cert_key_cert");
126e0c4386eSCy Schubertopen DATA, "outerr.txt";
127e0c4386eSCy Schubertmy @match = grep /:error:/, <DATA>;
128e0c4386eSCy Schubertclose DATA;
129e0c4386eSCy Schubertok(scalar @match > 0 ? 0 : 1, "test_export_pkcs12_outerr_empty");
130e0c4386eSCy Schubert
131e0c4386eSCy Schubertok(run(app(["openssl", "pkcs12",
132e0c4386eSCy Schubert            "-in", $outfile4,
133e0c4386eSCy Schubert            "-passin", "pass:v3-certs",
134e0c4386eSCy Schubert            "-nomacver", "-nodes"])),
135e0c4386eSCy Schubert  "test_import_pkcs12_cert_key_cert");
136e0c4386eSCy Schubert
137e0c4386eSCy Schubertok(run(app(["openssl", "pkcs12", "-export", "-out", $outfile5,
138e0c4386eSCy Schubert            "-in", srctop_file(@path, "ee-cert.pem"), "-caname", "testname",
139e0c4386eSCy Schubert            "-nokeys", "-passout", "pass:", "-certpbe", "NONE"])),
140e0c4386eSCy Schubert   "test nokeys single cert");
141e0c4386eSCy Schubert
142e0c4386eSCy Schubertmy @pkcs12info = run(app(["openssl", "pkcs12", "-info", "-in", $outfile5,
143e0c4386eSCy Schubert                          "-passin", "pass:"]), capture => 1);
144e0c4386eSCy Schubert
145e0c4386eSCy Schubert# Test that with one input certificate, we get one output certificate
146e0c4386eSCy Schubertok(grep(/subject=CN = server.example/, @pkcs12info) == 1,
147e0c4386eSCy Schubert   "test one cert in output");
148e0c4386eSCy Schubert# Test that the expected friendly name is present in the output
149e0c4386eSCy Schubertok(grep(/testname/, @pkcs12info) == 1, "test friendly name in output");
150e0c4386eSCy Schubert
151e0c4386eSCy Schubert# Test some bad pkcs12 files
152e0c4386eSCy Schubertmy $bad1 = srctop_file("test", "recipes", "80-test_pkcs12_data", "bad1.p12");
153e0c4386eSCy Schubertmy $bad2 = srctop_file("test", "recipes", "80-test_pkcs12_data", "bad2.p12");
154e0c4386eSCy Schubertmy $bad3 = srctop_file("test", "recipes", "80-test_pkcs12_data", "bad3.p12");
155e0c4386eSCy Schubert
156e0c4386eSCy Schubertwith({ exit_checker => sub { return shift == 1; } },
157e0c4386eSCy Schubert     sub {
158e0c4386eSCy Schubert        ok(run(app(["openssl", "pkcs12", "-in", $bad1, "-password", "pass:"])),
159e0c4386eSCy Schubert           "test bad pkcs12 file 1");
160e0c4386eSCy Schubert
161e0c4386eSCy Schubert        ok(run(app(["openssl", "pkcs12", "-in", $bad1, "-password", "pass:",
162e0c4386eSCy Schubert                    "-nomacver"])),
163e0c4386eSCy Schubert           "test bad pkcs12 file 1 (nomacver)");
164e0c4386eSCy Schubert
165*44096ebdSEnji Cooper        ok(run(app(["openssl", "pkcs12", "-in", $bad1, "-password", "pass:",
166*44096ebdSEnji Cooper                    "-info"])),
167*44096ebdSEnji Cooper           "test bad pkcs12 file 1 (info)");
168*44096ebdSEnji Cooper
169e0c4386eSCy Schubert        ok(run(app(["openssl", "pkcs12", "-in", $bad2, "-password", "pass:"])),
170e0c4386eSCy Schubert           "test bad pkcs12 file 2");
171e0c4386eSCy Schubert
172*44096ebdSEnji Cooper        ok(run(app(["openssl", "pkcs12", "-in", $bad2, "-password", "pass:",
173*44096ebdSEnji Cooper                    "-info"])),
174*44096ebdSEnji Cooper           "test bad pkcs12 file 2 (info)");
175*44096ebdSEnji Cooper
176e0c4386eSCy Schubert        ok(run(app(["openssl", "pkcs12", "-in", $bad3, "-password", "pass:"])),
177e0c4386eSCy Schubert           "test bad pkcs12 file 3");
178*44096ebdSEnji Cooper
179*44096ebdSEnji Cooper        ok(run(app(["openssl", "pkcs12", "-in", $bad3, "-password", "pass:",
180*44096ebdSEnji Cooper                    "-info"])),
181*44096ebdSEnji Cooper           "test bad pkcs12 file 3 (info)");
182e0c4386eSCy Schubert     });
183e0c4386eSCy Schubert
184e0c4386eSCy SchubertSetConsoleOutputCP($savedcp) if (defined($savedcp));
185