25-test_req.t 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #! /usr/bin/env perl
  2. # Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the OpenSSL license (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 => 14;
  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 with RSA" => sub {
  40. plan tests => 6;
  41. SKIP: {
  42. skip "RSA is not supported by this OpenSSL build", 2
  43. if disabled("rsa");
  44. ok(run(app(["openssl", "req",
  45. "-config", srctop_file("test", "test.cnf"),
  46. "-new", "-out", "testreq.pem", "-utf8",
  47. "-key", srctop_file("test", "testrsa.pem")])),
  48. "Generating request");
  49. ok(run(app(["openssl", "req",
  50. "-config", srctop_file("test", "test.cnf"),
  51. "-verify", "-in", "testreq.pem", "-noout"])),
  52. "Verifying signature on request");
  53. ok(run(app(["openssl", "req",
  54. "-config", srctop_file("test", "test.cnf"),
  55. "-new", "-out", "testreq_withattrs_pem.pem", "-utf8",
  56. "-key", srctop_file("test", "testrsa_withattrs.pem")])),
  57. "Generating request from a key with extra attributes - PEM");
  58. ok(run(app(["openssl", "req",
  59. "-config", srctop_file("test", "test.cnf"),
  60. "-verify", "-in", "testreq_withattrs_pem.pem", "-noout"])),
  61. "Verifying signature on request from a key with extra attributes - PEM");
  62. ok(run(app(["openssl", "req",
  63. "-config", srctop_file("test", "test.cnf"),
  64. "-new", "-out", "testreq_withattrs_der.pem", "-utf8",
  65. "-key", srctop_file("test", "testrsa_withattrs.der"),
  66. "-keyform", "DER"])),
  67. "Generating request from a key with extra attributes - PEM");
  68. ok(run(app(["openssl", "req",
  69. "-config", srctop_file("test", "test.cnf"),
  70. "-verify", "-in", "testreq_withattrs_der.pem", "-noout"])),
  71. "Verifying signature on request from a key with extra attributes - PEM");
  72. }
  73. };
  74. subtest "generating certificate requests with DSA" => sub {
  75. plan tests => 2;
  76. SKIP: {
  77. skip "DSA is not supported by this OpenSSL build", 2
  78. if disabled("dsa");
  79. ok(run(app(["openssl", "req",
  80. "-config", srctop_file("test", "test.cnf"),
  81. "-new", "-out", "testreq.pem", "-utf8",
  82. "-key", srctop_file("test", "testdsa.pem")])),
  83. "Generating request");
  84. ok(run(app(["openssl", "req",
  85. "-config", srctop_file("test", "test.cnf"),
  86. "-verify", "-in", "testreq.pem", "-noout"])),
  87. "Verifying signature on request");
  88. }
  89. };
  90. subtest "generating certificate requests with ECDSA" => sub {
  91. plan tests => 2;
  92. SKIP: {
  93. skip "ECDSA is not supported by this OpenSSL build", 2
  94. if disabled("ec");
  95. ok(run(app(["openssl", "req",
  96. "-config", srctop_file("test", "test.cnf"),
  97. "-new", "-out", "testreq.pem", "-utf8",
  98. "-key", srctop_file("test", "testec-p256.pem")])),
  99. "Generating request");
  100. ok(run(app(["openssl", "req",
  101. "-config", srctop_file("test", "test.cnf"),
  102. "-verify", "-in", "testreq.pem", "-noout"])),
  103. "Verifying signature on request");
  104. }
  105. };
  106. subtest "generating certificate requests with Ed25519" => sub {
  107. plan tests => 2;
  108. SKIP: {
  109. skip "Ed25519 is not supported by this OpenSSL build", 2
  110. if disabled("ec");
  111. ok(run(app(["openssl", "req",
  112. "-config", srctop_file("test", "test.cnf"),
  113. "-new", "-out", "testreq.pem", "-utf8",
  114. "-key", srctop_file("test", "tested25519.pem")])),
  115. "Generating request");
  116. ok(run(app(["openssl", "req",
  117. "-config", srctop_file("test", "test.cnf"),
  118. "-verify", "-in", "testreq.pem", "-noout"])),
  119. "Verifying signature on request");
  120. }
  121. };
  122. subtest "generating certificate requests with Ed448" => sub {
  123. plan tests => 2;
  124. SKIP: {
  125. skip "Ed448 is not supported by this OpenSSL build", 2
  126. if disabled("ec");
  127. ok(run(app(["openssl", "req",
  128. "-config", srctop_file("test", "test.cnf"),
  129. "-new", "-out", "testreq.pem", "-utf8",
  130. "-key", srctop_file("test", "tested448.pem")])),
  131. "Generating request");
  132. ok(run(app(["openssl", "req",
  133. "-config", srctop_file("test", "test.cnf"),
  134. "-verify", "-in", "testreq.pem", "-noout"])),
  135. "Verifying signature on request");
  136. }
  137. };
  138. subtest "generating certificate requests" => sub {
  139. plan tests => 2;
  140. ok(run(app(["openssl", "req", "-config", srctop_file("test", "test.cnf"),
  141. @req_new, "-out", "testreq.pem"])),
  142. "Generating request");
  143. ok(run(app(["openssl", "req", "-config", srctop_file("test", "test.cnf"),
  144. "-verify", "-in", "testreq.pem", "-noout"])),
  145. "Verifying signature on request");
  146. };
  147. my @openssl_args = ("req", "-config", srctop_file("apps", "openssl.cnf"));
  148. run_conversion('req conversions',
  149. "testreq.pem");
  150. run_conversion('req conversions -- testreq2',
  151. srctop_file("test", "testreq2.pem"));
  152. unlink "testkey.pem", "testreq.pem", "testreq_withattrs_pem.pem", "testreq_withattrs_der.pem";
  153. sub run_conversion {
  154. my $title = shift;
  155. my $reqfile = shift;
  156. subtest $title => sub {
  157. run(app(["openssl", @openssl_args,
  158. "-in", $reqfile, "-inform", "p",
  159. "-noout", "-text"],
  160. stderr => "req-check.err", stdout => undef));
  161. open DATA, "req-check.err";
  162. SKIP: {
  163. plan skip_all => "skipping req conversion test for $reqfile"
  164. if grep /Unknown Public Key/, map { s/\R//; } <DATA>;
  165. tconversion("req", $reqfile, @openssl_args);
  166. }
  167. close DATA;
  168. unlink "req-check.err";
  169. done_testing();
  170. };
  171. }