15-test_gendhparam.t 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #! /usr/bin/env perl
  2. # Copyright 2021-2022 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;
  11. use OpenSSL::Test::Utils;
  12. setup("test_gendhparam");
  13. my @testdata = (
  14. {
  15. algorithm => 'DHX',
  16. pkeyopts => [ "type:fips186_4", 'digest:SHA256', 'gindex:1' ],
  17. expect => [ 'BEGIN X9.42 DH PARAMETERS', 'gindex:', 'pcounter:', 'SEED:' ],
  18. message => 'DH fips186_4 param gen with verifiable g',
  19. },
  20. {
  21. algorithm => 'DH',
  22. pkeyopts => [ "type:fips186_4", 'digest:SHA256', 'gindex:1' ],
  23. expect => [ 'ERROR' ],
  24. message => 'fips186_4 param gen should fail if DHX is not used',
  25. },
  26. {
  27. algorithm => 'DHX',
  28. pkeyopts => [ "type:fips186_4", 'digest:SHA512-224', 'gindex:1' ],
  29. expect => [ 'BEGIN X9.42 DH PARAMETERS', 'gindex:', 'pcounter:', 'SEED:' ],
  30. message => 'DH fips186_4 param gen with verifiable g and truncated digest',
  31. },
  32. {
  33. algorithm => 'DHX',
  34. pkeyopts => [ 'type:fips186_2', 'pbits:1024', 'qbits:160' ],
  35. expect => [ 'BEGIN X9.42 DH PARAMETERS', 'h:', 'pcounter:', 'SEED:' ],
  36. message => 'DHX fips186_2 param gen with a selected p and q size with unverifyable g',
  37. },
  38. {
  39. algorithm => 'DHX',
  40. pkeyopts => [ 'type:fips186_2', 'dh_paramgen_prime_len:1024', 'dh_paramgen_subprime_len:160' ],
  41. message => 'DHX fips186_2 param gen with a selected p and q size using aliased',
  42. expect => [ "BEGIN X9.42 DH PARAMETERS" ],
  43. },
  44. {
  45. algorithm => 'DH',
  46. pkeyopts => [ 'type:fips186_2', 'dh_paramgen_prime_len:1024', 'dh_paramgen_subprime_len:160' ],
  47. message => 'DH fips186_2 param gen with a selected p and q size using aliases should fail',
  48. expect => [ "ERROR" ],
  49. },
  50. {
  51. algorithm => 'DH',
  52. pkeyopts => [ 'group:ffdhe2048'],
  53. expect => [ 'BEGIN DH PARAMETERS', 'GROUP:' ],
  54. message => 'DH named group ffdhe selection',
  55. },
  56. {
  57. algorithm => 'DH',
  58. pkeyopts => [ 'dh_param:ffdhe8192'],
  59. expect => [ 'BEGIN DH PARAMETERS', 'GROUP:' ],
  60. message => 'DH named group ffdhe selection using alias',
  61. },
  62. {
  63. algorithm => 'DH',
  64. pkeyopts => [ 'group:modp_3072'],
  65. expect => [ 'BEGIN DH PARAMETERS', 'GROUP:' ],
  66. message => 'DH named group modp selection',
  67. },
  68. {
  69. algorithm => 'DH',
  70. pkeyopts => [ 'dh_param:modp_4096'],
  71. message => 'DH named group modp selection using alias',
  72. expect => [ 'BEGIN DH PARAMETERS', 'GROUP:'],
  73. },
  74. {
  75. algorithm => 'DHX',
  76. pkeyopts => [ 'group:dh_2048_256' ],
  77. expect => [ 'BEGIN X9.42 DH PARAMETERS', 'GROUP:' ],
  78. message => 'DHX RFC5114 named group selection',
  79. },
  80. {
  81. algorithm => 'DHX',
  82. pkeyopts => [ 'dh_param:dh_2048_224' ],
  83. expect => [ 'BEGIN X9.42 DH PARAMETERS', 'GROUP:' ],
  84. message => 'DHX RFC5114 named group selection using alias',
  85. },
  86. {
  87. algorithm => 'DHX',
  88. pkeyopts => [ 'dh_rfc5114:2'],
  89. expect => [ 'BEGIN X9.42 DH PARAMETERS', 'GROUP:' ],
  90. message => 'DHX RFC5114 named group selection using an id',
  91. },
  92. {
  93. algorithm => 'DHX',
  94. pkeyopts => [ 'dh_rfc5114:1', 'dh_paramgen_type:1' ],
  95. expect => [ 'BEGIN X9.42 DH PARAMETERS', 'GROUP:' ],
  96. message => 'DHX paramgen_type is ignored if the group is set',
  97. },
  98. {
  99. algorithm => 'DH',
  100. pkeyopts => [ 'dh_rfc5114:1', 'dh_paramgen_type:1' ],
  101. expect => [ 'ERROR' ],
  102. message => "Setting dh_paramgen_type to fips186 should fail for DH keys",
  103. },
  104. # These tests using the safeprime generator were removed as they are slow..
  105. # {
  106. # algorithm => 'DH',
  107. # pkeyopts => [ 'type:generator', 'safeprime-generator:5'],
  108. # expect => [ 'BEGIN DH PARAMETERS', 'G: 5' ],
  109. # message => 'DH safe prime generator',
  110. # },
  111. # {
  112. # algorithm => 'DH',
  113. # pkeyopts => [ 'dh_paramgen_type:0', 'dh_paramgen_generator:5'],
  114. # expect => [ 'BEGIN DH PARAMETERS', 'G: 5' ],
  115. # message => 'DH safe prime generator using an alias',
  116. # },
  117. {
  118. algorithm => 'DHX',
  119. pkeyopts => [ 'type:generator', 'safeprime-generator:5'],
  120. expect => [ 'ERROR' ],
  121. message => 'safe prime generator should fail for DHX',
  122. }
  123. );
  124. plan skip_all => "DH isn't supported in this build" if disabled("dh");
  125. plan tests => scalar @testdata;
  126. foreach my $test (@testdata) {
  127. my $alg = $test->{algorithm};
  128. my $msg = $test->{message};
  129. my @testargs = @{ $test->{pkeyopts} };
  130. my @expected = @{ $test->{expect} };
  131. my @pkeyopts= ();
  132. foreach (@testargs) {
  133. push(@pkeyopts, '-pkeyopt');
  134. push(@pkeyopts, $_);
  135. }
  136. my @lines;
  137. if ($expected[0] eq 'ERROR') {
  138. @lines = run(app(['openssl', 'genpkey', '-genparam',
  139. '-algorithm', $alg, '-text', @pkeyopts],
  140. stderr => undef),
  141. capture => 1);
  142. } else {
  143. @lines = run(app(['openssl', 'genpkey', '-genparam',
  144. '-algorithm', $alg, '-text', @pkeyopts]),
  145. capture => 1);
  146. }
  147. ok(compareline(\@lines, \@expected), $msg);
  148. }
  149. # Check that the stdout output matches the expected value.
  150. sub compareline {
  151. my ($ref_lines, $ref_expected) = @_;
  152. my @lines = @$ref_lines;
  153. my @expected = @$ref_expected;
  154. if (@lines == 0 and $expected[0] eq 'ERROR') {
  155. return 1;
  156. }
  157. print "-----------------\n";
  158. foreach (@lines) {
  159. print "# ".$_;
  160. }
  161. print "-----------------\n";
  162. foreach my $ex (@expected) {
  163. if ( !grep { index($_, $ex) >= 0 } @lines) {
  164. print "ERROR: Cannot find: $ex\n";
  165. return 0;
  166. }
  167. }
  168. return 1;
  169. }