25-test_req.t 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #! /usr/bin/env perl
  2. # Copyright 2015-2018 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::Utils;
  11. use OpenSSL::Test qw/:DEFAULT srctop_file/;
  12. setup("test_req");
  13. plan tests => 9;
  14. require_ok(srctop_file('test','recipes','tconversion.pl'));
  15. open RND, ">>", ".rnd";
  16. print RND "string to make the random number generator think it has randomness";
  17. close RND;
  18. # What type of key to generate?
  19. my @req_new;
  20. if (disabled("rsa")) {
  21. @req_new = ("-newkey", "dsa:".srctop_file("apps", "dsa512.pem"));
  22. } else {
  23. @req_new = ("-new");
  24. note("There should be a 2 sequences of .'s and some +'s.");
  25. note("There should not be more that at most 80 per line");
  26. }
  27. # Check for duplicate -addext parameters, and one "working" case.
  28. my @addext_args = ( "openssl", "req", "-new", "-out", "testreq.pem",
  29. "-config", srctop_file("test", "test.cnf"), @req_new );
  30. my $val = "subjectAltName=DNS:example.com";
  31. my $val2 = " " . $val;
  32. my $val3 = $val;
  33. $val3 =~ s/=/ =/;
  34. ok( run(app([@addext_args, "-addext", $val])));
  35. ok(!run(app([@addext_args, "-addext", $val, "-addext", $val])));
  36. ok(!run(app([@addext_args, "-addext", $val, "-addext", $val2])));
  37. ok(!run(app([@addext_args, "-addext", $val, "-addext", $val3])));
  38. ok(!run(app([@addext_args, "-addext", $val2, "-addext", $val3])));
  39. subtest "generating certificate requests" => sub {
  40. plan tests => 2;
  41. ok(run(app(["openssl", "req", "-config", srctop_file("test", "test.cnf"),
  42. @req_new, "-out", "testreq.pem"])),
  43. "Generating request");
  44. ok(run(app(["openssl", "req", "-config", srctop_file("test", "test.cnf"),
  45. "-verify", "-in", "testreq.pem", "-noout"])),
  46. "Verifying signature on request");
  47. };
  48. my @openssl_args = ("req", "-config", srctop_file("apps", "openssl.cnf"));
  49. run_conversion('req conversions',
  50. "testreq.pem");
  51. run_conversion('req conversions -- testreq2',
  52. srctop_file("test", "testreq2.pem"));
  53. unlink "testkey.pem", "testreq.pem";
  54. sub run_conversion {
  55. my $title = shift;
  56. my $reqfile = shift;
  57. subtest $title => sub {
  58. run(app(["openssl", @openssl_args,
  59. "-in", $reqfile, "-inform", "p",
  60. "-noout", "-text"],
  61. stderr => "req-check.err", stdout => undef));
  62. open DATA, "req-check.err";
  63. SKIP: {
  64. plan skip_all => "skipping req conversion test for $reqfile"
  65. if grep /Unknown Public Key/, map { s/\R//; } <DATA>;
  66. tconversion("req", $reqfile, @openssl_args);
  67. }
  68. close DATA;
  69. unlink "req-check.err";
  70. done_testing();
  71. };
  72. }