25-test_req.t 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 => 10;
  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. subtest "generating SM2 certificate requests" => sub {
  49. plan tests => 2;
  50. SKIP: {
  51. skip "SM2 is not supported by this OpenSSL build", 2
  52. if disabled("sm2");
  53. ok(run(app(["openssl", "req", "-config", srctop_file("test", "test.cnf"),
  54. "-new", "-key", srctop_file("test", "certs", "sm2.key"),
  55. "-sigopt", "sm2_id:1234567812345678",
  56. "-out", "testreq.pem", "-sm3"])),
  57. "Generating SM2 certificate request");
  58. ok(run(app(["openssl", "req", "-config", srctop_file("test", "test.cnf"),
  59. "-verify", "-in", "testreq.pem", "-noout",
  60. "-sm2-id", "1234567812345678", "-sm3"])),
  61. "Verifying signature on SM2 certificate request");
  62. }
  63. };
  64. my @openssl_args = ("req", "-config", srctop_file("apps", "openssl.cnf"));
  65. run_conversion('req conversions',
  66. "testreq.pem");
  67. run_conversion('req conversions -- testreq2',
  68. srctop_file("test", "testreq2.pem"));
  69. unlink "testkey.pem", "testreq.pem";
  70. sub run_conversion {
  71. my $title = shift;
  72. my $reqfile = shift;
  73. subtest $title => sub {
  74. run(app(["openssl", @openssl_args,
  75. "-in", $reqfile, "-inform", "p",
  76. "-noout", "-text"],
  77. stderr => "req-check.err", stdout => undef));
  78. open DATA, "req-check.err";
  79. SKIP: {
  80. plan skip_all => "skipping req conversion test for $reqfile"
  81. if grep /Unknown Public Key/, map { s/\R//; } <DATA>;
  82. tconversion("req", $reqfile, @openssl_args);
  83. }
  84. close DATA;
  85. unlink "req-check.err";
  86. done_testing();
  87. };
  88. }