Add EVP_KDF-X942 to the fips module
[openssl.git] / test / recipes / 80-test_pkcs12.t
1 #! /usr/bin/env perl
2 # Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved.
3 #
4 # Licensed under the Apache License 2.0 (the "License").  You may not use
5 # this file except in compliance with the License.  You can obtain a copy
6 # in the file LICENSE in the source distribution or at
7 # https://www.openssl.org/source/license.html
8
9 use strict;
10 use warnings;
11
12 use OpenSSL::Test qw/:DEFAULT srctop_file/;
13 use OpenSSL::Test::Utils;
14
15 use Encode;
16
17 setup("test_pkcs12");
18
19 plan skip_all => "The PKCS12 command line utility is not supported by this OpenSSL build"
20     if disabled("des");
21
22 my $pass = "σύνθημα γνώρισμα";
23
24 my $savedcp;
25 if (eval { require Win32::API; 1; }) {
26     # Trouble is that Win32 perl uses CreateProcessA, which
27     # makes it problematic to pass non-ASCII arguments, from perl[!]
28     # that is. This is because CreateProcessA is just a wrapper for
29     # CreateProcessW and will call MultiByteToWideChar and use
30     # system default locale. Since we attempt Greek pass-phrase
31     # conversion can be done only with Greek locale.
32
33     Win32::API->Import("kernel32","UINT GetSystemDefaultLCID()");
34     if (GetSystemDefaultLCID() != 0x408) {
35         plan skip_all => "Non-Greek system locale";
36     } else {
37         # Ensure correct code page so that VERBOSE output is right.
38         Win32::API->Import("kernel32","UINT GetConsoleOutputCP()");
39         Win32::API->Import("kernel32","BOOL SetConsoleOutputCP(UINT cp)");
40         $savedcp = GetConsoleOutputCP();
41         SetConsoleOutputCP(1253);
42         $pass = Encode::encode("cp1253",Encode::decode("utf-8",$pass));
43     }
44 } elsif ($^O eq "MSWin32") {
45     plan skip_all => "Win32::API unavailable";
46 } else {
47     # Running MinGW tests transparently under Wine apparently requires
48     # UTF-8 locale...
49
50     foreach(`locale -a`) {
51         s/\R$//;
52         if ($_ =~ m/^C\.UTF\-?8/i) {
53             $ENV{LC_ALL} = $_;
54             last;
55         }
56     }
57 }
58 $ENV{OPENSSL_WIN32_UTF8}=1;
59
60 plan tests => 5;
61
62 # Test different PKCS#12 formats
63 ok(run(test(["pkcs12_format_test"])), "test pkcs12 formats");
64
65 # just see that we can read shibboleth.pfx protected with $pass
66 ok(run(app(["openssl", "pkcs12", "-noout",
67             "-password", "pass:$pass",
68             "-in", srctop_file("test", "shibboleth.pfx")])),
69    "test_load_cert_pkcs12");
70
71 my @path = qw(test certs);
72 my $outfile1 = "out1.p12";
73 my $outfile2 = "out2.p12";
74 my $outfile3 = "out3.p12";
75
76 # Test the -chain option with -untrusted
77 ok(run(app(["openssl", "pkcs12", "-export", "-chain",
78             "-CAfile",  srctop_file(@path,  "sroot-cert.pem"),
79             "-untrusted", srctop_file(@path, "ca-cert.pem"),
80             "-in", srctop_file(@path, "ee-cert.pem"),
81             "-nokeys", "-passout", "pass:", "-out", $outfile1])),
82    "test_pkcs12_chain_untrusted");
83
84 # Test the -passcerts option
85 ok(run(app(["openssl", "pkcs12", "-export",
86             "-in", srctop_file(@path, "ee-cert.pem"),
87             "-certfile", srctop_file(@path, "v3-certs-TDES.p12"),
88             "-passcerts", "pass:v3-certs",
89             "-nokeys", "-passout", "pass:v3-certs", "-descert",
90             "-out", $outfile2])),
91    "test_pkcs12_passcerts");
92
93 SKIP: {
94     skip "Skipping legacy PKCS#12 test because RC2 is disabled in this build", 1
95         if disabled("rc2");
96     # Test reading legacy PKCS#12 file
97     ok(run(app(["openssl", "pkcs12", "-export",
98                 "-in", srctop_file(@path, "v3-certs-RC2.p12"),
99                 "-passin", "pass:v3-certs",
100                 "-provider", "default", "-provider", "legacy",
101                 "-nokeys", "-passout", "pass:v3-certs", "-descert",
102                 "-out", $outfile3])),
103     "test_pkcs12_passcerts_legacy");
104 }
105
106 SetConsoleOutputCP($savedcp) if (defined($savedcp));