80-test_pkcs12.t 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. use strict;
  9. use warnings;
  10. use OpenSSL::Test qw/:DEFAULT srctop_file/;
  11. use OpenSSL::Test::Utils;
  12. use Encode;
  13. setup("test_pkcs12");
  14. my $pass = "σύνθημα γνώρισμα";
  15. my $savedcp;
  16. if (eval { require Win32::API; 1; }) {
  17. # Trouble is that Win32 perl uses CreateProcessA, which
  18. # makes it problematic to pass non-ASCII arguments, from perl[!]
  19. # that is. This is because CreateProcessA is just a wrapper for
  20. # CreateProcessW and will call MultiByteToWideChar and use
  21. # system default locale. Since we attempt Greek pass-phrase
  22. # conversion can be done only with Greek locale.
  23. Win32::API->Import("kernel32","UINT GetSystemDefaultLCID()");
  24. if (GetSystemDefaultLCID() != 0x408) {
  25. plan skip_all => "Non-Greek system locale";
  26. } else {
  27. # Ensure correct code page so that VERBOSE output is right.
  28. Win32::API->Import("kernel32","UINT GetConsoleOutputCP()");
  29. Win32::API->Import("kernel32","BOOL SetConsoleOutputCP(UINT cp)");
  30. $savedcp = GetConsoleOutputCP();
  31. SetConsoleOutputCP(1253);
  32. $pass = Encode::encode("cp1253",Encode::decode("utf-8",$pass));
  33. }
  34. } elsif ($^O eq "MSWin32") {
  35. plan skip_all => "Win32::API unavailable";
  36. } else {
  37. # Running MinGW tests transparently under Wine apparently requires
  38. # UTF-8 locale...
  39. foreach(`locale -a`) {
  40. s/\R$//;
  41. if ($_ =~ m/^C\.UTF\-?8/i) {
  42. $ENV{LC_ALL} = $_;
  43. last;
  44. }
  45. }
  46. }
  47. $ENV{OPENSSL_WIN32_UTF8}=1;
  48. plan tests => 7;
  49. # Test different PKCS#12 formats
  50. ok(run(test(["pkcs12_format_test"])), "test pkcs12 formats");
  51. # Test with legacy APIs
  52. ok(run(test(["pkcs12_format_test", "-legacy"])), "test pkcs12 formats using legacy APIs");
  53. # Test with a non-default library context (and no loaded providers in the default context)
  54. ok(run(test(["pkcs12_format_test", "-context"])), "test pkcs12 formats using a non-default library context");
  55. # just see that we can read shibboleth.pfx protected with $pass
  56. ok(run(app(["openssl", "pkcs12", "-noout",
  57. "-password", "pass:$pass",
  58. "-in", srctop_file("test", "shibboleth.pfx")])),
  59. "test_load_cert_pkcs12");
  60. my @path = qw(test certs);
  61. my $outfile1 = "out1.p12";
  62. my $outfile2 = "out2.p12";
  63. my $outfile3 = "out3.p12";
  64. # Test the -chain option with -untrusted
  65. ok(run(app(["openssl", "pkcs12", "-export", "-chain",
  66. "-CAfile", srctop_file(@path, "sroot-cert.pem"),
  67. "-untrusted", srctop_file(@path, "ca-cert.pem"),
  68. "-in", srctop_file(@path, "ee-cert.pem"),
  69. "-nokeys", "-passout", "pass:", "-out", $outfile1])),
  70. "test_pkcs12_chain_untrusted");
  71. # Test the -passcerts option
  72. SKIP: {
  73. skip "Skipping PKCS#12 test because DES is disabled in this build", 1
  74. if disabled("des");
  75. ok(run(app(["openssl", "pkcs12", "-export",
  76. "-in", srctop_file(@path, "ee-cert.pem"),
  77. "-certfile", srctop_file(@path, "v3-certs-TDES.p12"),
  78. "-passcerts", "pass:v3-certs",
  79. "-nokeys", "-passout", "pass:v3-certs", "-descert",
  80. "-out", $outfile2])),
  81. "test_pkcs12_passcerts");
  82. }
  83. SKIP: {
  84. skip "Skipping legacy PKCS#12 test because RC2 is disabled in this build", 1
  85. if disabled("rc2") || disabled("legacy");
  86. # Test reading legacy PKCS#12 file
  87. ok(run(app(["openssl", "pkcs12", "-export",
  88. "-in", srctop_file(@path, "v3-certs-RC2.p12"),
  89. "-passin", "pass:v3-certs",
  90. "-provider", "default", "-provider", "legacy",
  91. "-nokeys", "-passout", "pass:v3-certs", "-descert",
  92. "-out", $outfile3])),
  93. "test_pkcs12_passcerts_legacy");
  94. }
  95. SetConsoleOutputCP($savedcp) if (defined($savedcp));