15-test_ecparam.t 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #! /usr/bin/env perl
  2. # Copyright 2017-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 File::Spec;
  11. use File::Compare qw/compare_text/;
  12. use OpenSSL::Glob;
  13. use OpenSSL::Test qw/:DEFAULT data_file srctop_file bldtop_dir/;
  14. use OpenSSL::Test::Utils;
  15. setup("test_ecparam");
  16. plan skip_all => "EC or EC2M isn't supported in this build"
  17. if disabled("ec") || disabled("ec2m");
  18. my @valid = glob(data_file("valid", "*.pem"));
  19. my @noncanon = glob(data_file("noncanon", "*.pem"));
  20. my @invalid = glob(data_file("invalid", "*.pem"));
  21. if (disabled("sm2")) {
  22. @valid = grep { !/sm2-.*\.pem/} @valid;
  23. }
  24. plan tests => 12;
  25. sub checkload {
  26. my $files = shift; # List of files
  27. my $valid = shift; # Check should pass or fail?
  28. my $app = shift; # Which application
  29. my $opt = shift; # Additional option
  30. foreach (@$files) {
  31. if ($valid) {
  32. ok(run(app(['openssl', $app, '-noout', $opt, '-in', $_])));
  33. } else {
  34. ok(!run(app(['openssl', $app, '-noout', $opt, '-in', $_])));
  35. }
  36. }
  37. }
  38. sub checkcompare {
  39. my $files = shift; # List of files
  40. my $app = shift; # Which application
  41. foreach (@$files) {
  42. my $testout = "$app.tst";
  43. ok(run(app(['openssl', $app, '-out', $testout, '-in', $_])));
  44. ok(!compare_text($_, $testout, sub {
  45. my $in1 = $_[0];
  46. my $in2 = $_[1];
  47. $in1 =~ s/\r\n/\n/g;
  48. $in2 =~ s/\r\n/\n/g;
  49. $in1 ne $in2}), "Original file $_ is the same as new one");
  50. }
  51. }
  52. my $no_fips = disabled('fips') || ($ENV{NO_FIPS} // 0);
  53. subtest "Check loading valid parameters by ecparam with -check" => sub {
  54. plan tests => scalar(@valid);
  55. checkload(\@valid, 1, "ecparam", "-check");
  56. };
  57. subtest "Check loading valid parameters by ecparam with -check_named" => sub {
  58. plan tests => scalar(@valid);
  59. checkload(\@valid, 1, "ecparam", "-check_named");
  60. };
  61. subtest "Check loading valid parameters by pkeyparam with -check" => sub {
  62. plan tests => scalar(@valid);
  63. checkload(\@valid, 1, "pkeyparam", "-check");
  64. };
  65. subtest "Check loading non-canonically encoded parameters by ecparam with -check" => sub {
  66. plan tests => scalar(@noncanon);
  67. checkload(\@noncanon, 1, "ecparam", "-check");
  68. };
  69. subtest "Check loading non-canonically encoded parameters by ecparam with -check_named" => sub {
  70. plan tests => scalar(@noncanon);
  71. checkload(\@noncanon, 1, "ecparam", "-check_named");
  72. };
  73. subtest "Check loading non-canonically encoded parameters by pkeyparam with -check" => sub {
  74. plan tests => scalar(@noncanon);
  75. checkload(\@noncanon, 1, "pkeyparam", "-check");
  76. };
  77. subtest "Check loading invalid parameters by ecparam with -check" => sub {
  78. plan tests => scalar(@invalid);
  79. checkload(\@invalid, 0, "ecparam", "-check");
  80. };
  81. subtest "Check loading invalid parameters by ecparam with -check_named" => sub {
  82. plan tests => scalar(@invalid);
  83. checkload(\@invalid, 0, "ecparam", "-check_named");
  84. };
  85. subtest "Check loading invalid parameters by pkeyparam with -check" => sub {
  86. plan tests => scalar(@invalid);
  87. checkload(\@invalid, 0, "pkeyparam", "-check");
  88. };
  89. subtest "Check ecparam does not change the parameter file on output" => sub {
  90. plan tests => 2 * scalar(@valid);
  91. checkcompare(\@valid, "ecparam");
  92. };
  93. subtest "Check pkeyparam does not change the parameter file on output" => sub {
  94. plan tests => 2 * scalar(@valid);
  95. checkcompare(\@valid, "pkeyparam");
  96. };
  97. subtest "Check loading of fips and non-fips params" => sub {
  98. plan skip_all => "FIPS is disabled"
  99. if $no_fips;
  100. plan tests => 8;
  101. my $fipsconf = srctop_file("test", "fips-and-base.cnf");
  102. my $defaultconf = srctop_file("test", "default.cnf");
  103. $ENV{OPENSSL_CONF} = $fipsconf;
  104. ok(run(app(['openssl', 'ecparam',
  105. '-in', data_file('valid', 'secp384r1-explicit.pem'),
  106. '-check'])),
  107. "Loading explicitly encoded valid curve");
  108. ok(run(app(['openssl', 'ecparam',
  109. '-in', data_file('valid', 'secp384r1-named.pem'),
  110. '-check'])),
  111. "Loading named valid curve");
  112. ok(!run(app(['openssl', 'ecparam',
  113. '-in', data_file('valid', 'secp112r1-named.pem'),
  114. '-check'])),
  115. "Fail loading named non-fips curve");
  116. ok(!run(app(['openssl', 'pkeyparam',
  117. '-in', data_file('valid', 'secp112r1-named.pem'),
  118. '-check'])),
  119. "Fail loading named non-fips curve using pkeyparam");
  120. ok(run(app(['openssl', 'ecparam',
  121. '-provider', 'default',
  122. '-propquery', '?fips!=yes',
  123. '-in', data_file('valid', 'secp112r1-named.pem'),
  124. '-check'])),
  125. "Loading named non-fips curve in FIPS mode with non-FIPS property".
  126. " query");
  127. ok(run(app(['openssl', 'pkeyparam',
  128. '-provider', 'default',
  129. '-propquery', '?fips!=yes',
  130. '-in', data_file('valid', 'secp112r1-named.pem'),
  131. '-check'])),
  132. "Loading named non-fips curve in FIPS mode with non-FIPS property".
  133. " query using pkeyparam");
  134. ok(!run(app(['openssl', 'ecparam',
  135. '-genkey', '-name', 'secp112r1'])),
  136. "Fail generating key for named non-fips curve");
  137. ok(run(app(['openssl', 'ecparam',
  138. '-provider', 'default',
  139. '-propquery', '?fips!=yes',
  140. '-genkey', '-name', 'secp112r1'])),
  141. "Generating key for named non-fips curve with non-FIPS property query");
  142. $ENV{OPENSSL_CONF} = $defaultconf;
  143. };