80-test_ca.t 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #! /usr/bin/env perl
  2. # Copyright 2015-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 POSIX;
  11. use File::Path 2.00 qw/rmtree/;
  12. use OpenSSL::Test qw/:DEFAULT cmdstr srctop_file/;
  13. use OpenSSL::Test::Utils;
  14. setup("test_ca");
  15. $ENV{OPENSSL} = cmdstr(app(["openssl"]), display => 1);
  16. my $cnf = '"' . srctop_file("test","ca-and-certs.cnf") . '"';;
  17. my $std_openssl_cnf = '"'
  18. . srctop_file("apps", $^O eq "VMS" ? "openssl-vms.cnf" : "openssl.cnf")
  19. . '"';
  20. rmtree("demoCA", { safe => 0 });
  21. plan tests => 6;
  22. SKIP: {
  23. $ENV{OPENSSL_CONFIG} = '-config ' . $cnf;
  24. skip "failed creating CA structure", 4
  25. if !ok(run(perlapp(["CA.pl","-newca"], stdin => undef)),
  26. 'creating CA structure');
  27. $ENV{OPENSSL_CONFIG} = '-config ' . $cnf;
  28. skip "failed creating new certificate request", 3
  29. if !ok(run(perlapp(["CA.pl","-newreq",
  30. '-extra-req', '-outform DER -section userreq'])),
  31. 'creating certificate request');
  32. $ENV{OPENSSL_CONFIG} = '-rand_serial -inform DER -config '.$std_openssl_cnf;
  33. skip "failed to sign certificate request", 2
  34. if !is(yes(cmdstr(perlapp(["CA.pl", "-sign"]))), 0,
  35. 'signing certificate request');
  36. ok(run(perlapp(["CA.pl", "-verify", "newcert.pem"])),
  37. 'verifying new certificate');
  38. skip "CT not configured, can't use -precert", 1
  39. if disabled("ct");
  40. $ENV{OPENSSL_CONFIG} = '-config ' . $cnf;
  41. ok(run(perlapp(["CA.pl", "-precert", '-extra-req', '-section userreq'], stderr => undef)),
  42. 'creating new pre-certificate');
  43. }
  44. SKIP: {
  45. skip "SM2 is not supported by this OpenSSL build", 1
  46. if disabled("sm2");
  47. is(yes(cmdstr(app(["openssl", "ca", "-config",
  48. $cnf,
  49. "-in", srctop_file("test", "certs", "sm2-csr.pem"),
  50. "-out", "sm2-test.crt",
  51. "-sigopt", "distid:1234567812345678",
  52. "-vfyopt", "distid:1234567812345678",
  53. "-md", "sm3",
  54. "-cert", srctop_file("test", "certs", "sm2-root.crt"),
  55. "-keyfile", srctop_file("test", "certs", "sm2-root.key")]))),
  56. 0,
  57. "Signing SM2 certificate request");
  58. }
  59. sub yes {
  60. my $cntr = 10;
  61. open(PIPE, "|-", join(" ",@_));
  62. local $SIG{PIPE} = "IGNORE";
  63. 1 while $cntr-- > 0 && print PIPE "y\n";
  64. close PIPE;
  65. return 0;
  66. }