80-test_pkcs12.t 3.5 KB

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