15-test_genec.t 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. #! /usr/bin/env perl
  2. # Copyright 2017-2020 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 OpenSSL::Test qw/:DEFAULT srctop_file/;
  12. use OpenSSL::Test::Utils;
  13. setup("test_genec");
  14. plan skip_all => "This test is unsupported in a no-ec build"
  15. if disabled("ec");
  16. my @prime_curves = qw(
  17. secp112r1
  18. secp112r2
  19. secp128r1
  20. secp128r2
  21. secp160k1
  22. secp160r1
  23. secp160r2
  24. secp192k1
  25. secp224k1
  26. secp224r1
  27. secp256k1
  28. secp384r1
  29. secp521r1
  30. prime192v1
  31. prime192v2
  32. prime192v3
  33. prime239v1
  34. prime239v2
  35. prime239v3
  36. prime256v1
  37. wap-wsg-idm-ecid-wtls6
  38. wap-wsg-idm-ecid-wtls7
  39. wap-wsg-idm-ecid-wtls8
  40. wap-wsg-idm-ecid-wtls9
  41. wap-wsg-idm-ecid-wtls12
  42. brainpoolP160r1
  43. brainpoolP160t1
  44. brainpoolP192r1
  45. brainpoolP192t1
  46. brainpoolP224r1
  47. brainpoolP224t1
  48. brainpoolP256r1
  49. brainpoolP256t1
  50. brainpoolP320r1
  51. brainpoolP320t1
  52. brainpoolP384r1
  53. brainpoolP384t1
  54. brainpoolP512r1
  55. brainpoolP512t1
  56. );
  57. my @binary_curves = qw(
  58. sect113r1
  59. sect113r2
  60. sect131r1
  61. sect131r2
  62. sect163k1
  63. sect163r1
  64. sect163r2
  65. sect193r1
  66. sect193r2
  67. sect233k1
  68. sect233r1
  69. sect239k1
  70. sect283k1
  71. sect283r1
  72. sect409k1
  73. sect409r1
  74. sect571k1
  75. sect571r1
  76. c2pnb163v1
  77. c2pnb163v2
  78. c2pnb163v3
  79. c2pnb176v1
  80. c2tnb191v1
  81. c2tnb191v2
  82. c2tnb191v3
  83. c2pnb208w1
  84. c2tnb239v1
  85. c2tnb239v2
  86. c2tnb239v3
  87. c2pnb272w1
  88. c2pnb304w1
  89. c2tnb359v1
  90. c2pnb368w1
  91. c2tnb431r1
  92. wap-wsg-idm-ecid-wtls1
  93. wap-wsg-idm-ecid-wtls3
  94. wap-wsg-idm-ecid-wtls4
  95. wap-wsg-idm-ecid-wtls5
  96. wap-wsg-idm-ecid-wtls10
  97. wap-wsg-idm-ecid-wtls11
  98. );
  99. my @explicit_only_curves = ();
  100. push(@explicit_only_curves, qw(
  101. Oakley-EC2N-3
  102. Oakley-EC2N-4
  103. )) if !disabled("ec2m");
  104. my @other_curves = ();
  105. push(@other_curves, 'SM2')
  106. if !disabled("sm2");
  107. my @curve_aliases = qw(
  108. P-192
  109. P-224
  110. P-256
  111. P-384
  112. P-521
  113. );
  114. push(@curve_aliases, qw(
  115. B-163
  116. B-233
  117. B-283
  118. B-409
  119. B-571
  120. K-163
  121. K-233
  122. K-283
  123. K-409
  124. K-571
  125. )) if !disabled("ec2m");
  126. my @curve_list = ();
  127. push(@curve_list, @prime_curves);
  128. push(@curve_list, @binary_curves)
  129. if !disabled("ec2m");
  130. push(@curve_list, @other_curves);
  131. push(@curve_list, @curve_aliases);
  132. my @params_encodings = ('named_curve', 'explicit');
  133. my @output_formats = ('PEM', 'DER');
  134. plan tests => scalar(@curve_list) * scalar(@params_encodings)
  135. * (1 + scalar(@output_formats)) # Try listed @output_formats and text output
  136. * 2 # Test generating parameters and keys
  137. + 1 # Checking that with no curve it fails
  138. + 1 # Checking that with unknown curve it fails
  139. + 1 # Subtest for explicit only curves
  140. ;
  141. ok(!run(app([ 'openssl', 'genpkey',
  142. '-algorithm', 'EC'])),
  143. "genpkey EC with no params should fail");
  144. ok(!run(app([ 'openssl', 'genpkey',
  145. '-algorithm', 'EC',
  146. '-pkeyopt', 'ec_paramgen_curve:bogus_foobar_curve'])),
  147. "genpkey EC with unknown curve name should fail");
  148. foreach my $curvename (@curve_list) {
  149. foreach my $paramenc (@params_encodings) {
  150. # --- Test generating parameters ---
  151. ok(run(app([ 'openssl', 'genpkey', '-genparam',
  152. '-algorithm', 'EC',
  153. '-pkeyopt', 'ec_paramgen_curve:'.$curvename,
  154. '-pkeyopt', 'ec_param_enc:'.$paramenc,
  155. '-text'])),
  156. "genpkey EC params ${curvename} with ec_param_enc:'${paramenc}' (text)");
  157. foreach my $outform (@output_formats) {
  158. my $outfile = "ecgen.${curvename}.${paramenc}." . lc $outform;
  159. ok(run(app([ 'openssl', 'genpkey', '-genparam',
  160. '-algorithm', 'EC',
  161. '-pkeyopt', 'ec_paramgen_curve:'.$curvename,
  162. '-pkeyopt', 'ec_param_enc:'.$paramenc,
  163. '-outform', $outform,
  164. '-out', $outfile])),
  165. "genpkey EC params ${curvename} with ec_param_enc:'${paramenc}' (${outform})");
  166. }
  167. # --- Test generating actual keys ---
  168. ok(run(app([ 'openssl', 'genpkey',
  169. '-algorithm', 'EC',
  170. '-pkeyopt', 'ec_paramgen_curve:'.$curvename,
  171. '-pkeyopt', 'ec_param_enc:'.$paramenc,
  172. '-text'])),
  173. "genpkey EC key on ${curvename} with ec_param_enc:'${paramenc}' (text)");
  174. foreach my $outform (@output_formats) {
  175. my $outfile = "ecgen.${curvename}.${paramenc}." . lc $outform;
  176. ok(run(app([ 'openssl', 'genpkey',
  177. '-algorithm', 'EC',
  178. '-pkeyopt', 'ec_paramgen_curve:'.$curvename,
  179. '-pkeyopt', 'ec_param_enc:'.$paramenc,
  180. '-outform', $outform,
  181. '-out', $outfile])),
  182. "genpkey EC key on ${curvename} with ec_param_enc:'${paramenc}' (${outform})");
  183. }
  184. }
  185. }
  186. subtest "test curves that only support explicit parameters encoding" => sub {
  187. @curve_list = @explicit_only_curves;
  188. plan skip_all => "This test is unsupported under current configuration"
  189. if scalar(@curve_list) <= 0;
  190. plan tests => scalar(@curve_list) * scalar(@params_encodings)
  191. * (1 + scalar(@output_formats)) # Try listed @output_formats and text output
  192. * 2 # Test generating parameters and keys
  193. ;
  194. foreach my $curvename (@curve_list) {
  195. my $paramenc = "explicit";
  196. # --- Test generating parameters ---
  197. ok(run(app([ 'openssl', 'genpkey', '-genparam',
  198. '-algorithm', 'EC',
  199. '-pkeyopt', 'ec_paramgen_curve:'.$curvename,
  200. '-pkeyopt', 'ec_param_enc:'.$paramenc,
  201. '-text'])),
  202. "genpkey EC params ${curvename} with ec_param_enc:'${paramenc}' (text)");
  203. foreach my $outform (@output_formats) {
  204. my $outfile = "ecgen.${curvename}.${paramenc}." . lc $outform;
  205. ok(run(app([ 'openssl', 'genpkey', '-genparam',
  206. '-algorithm', 'EC',
  207. '-pkeyopt', 'ec_paramgen_curve:'.$curvename,
  208. '-pkeyopt', 'ec_param_enc:'.$paramenc,
  209. '-outform', $outform,
  210. '-out', $outfile])),
  211. "genpkey EC params ${curvename} with ec_param_enc:'${paramenc}' (${outform})");
  212. }
  213. # --- Test generating actual keys ---
  214. ok(run(app([ 'openssl', 'genpkey',
  215. '-algorithm', 'EC',
  216. '-pkeyopt', 'ec_paramgen_curve:'.$curvename,
  217. '-pkeyopt', 'ec_param_enc:'.$paramenc,
  218. '-text'])),
  219. "genpkey EC key on ${curvename} with ec_param_enc:'${paramenc}' (text)");
  220. foreach my $outform (@output_formats) {
  221. my $outfile = "ecgen.${curvename}.${paramenc}." . lc $outform;
  222. ok(run(app([ 'openssl', 'genpkey',
  223. '-algorithm', 'EC',
  224. '-pkeyopt', 'ec_paramgen_curve:'.$curvename,
  225. '-pkeyopt', 'ec_param_enc:'.$paramenc,
  226. '-outform', $outform,
  227. '-out', $outfile])),
  228. "genpkey EC key on ${curvename} with ec_param_enc:'${paramenc}' (${outform})");
  229. }
  230. my $paramenc = "named_curve";
  231. # --- Test generating parameters ---
  232. ok(!run(app([ 'openssl', 'genpkey', '-genparam',
  233. '-algorithm', 'EC',
  234. '-pkeyopt', 'ec_paramgen_curve:'.$curvename,
  235. '-pkeyopt', 'ec_param_enc:'.$paramenc,
  236. '-text'])),
  237. "genpkey EC params ${curvename} with ec_param_enc:'${paramenc}' (text)");
  238. foreach my $outform (@output_formats) {
  239. my $outfile = "ecgen.${curvename}.${paramenc}." . lc $outform;
  240. ok(!run(app([ 'openssl', 'genpkey', '-genparam',
  241. '-algorithm', 'EC',
  242. '-pkeyopt', 'ec_paramgen_curve:'.$curvename,
  243. '-pkeyopt', 'ec_param_enc:'.$paramenc,
  244. '-outform', $outform,
  245. '-out', $outfile])),
  246. "genpkey EC params ${curvename} with ec_param_enc:'${paramenc}' (${outform})");
  247. }
  248. # --- Test generating actual keys ---
  249. ok(!run(app([ 'openssl', 'genpkey',
  250. '-algorithm', 'EC',
  251. '-pkeyopt', 'ec_paramgen_curve:'.$curvename,
  252. '-pkeyopt', 'ec_param_enc:'.$paramenc,
  253. '-text'])),
  254. "genpkey EC key on ${curvename} with ec_param_enc:'${paramenc}' (text)");
  255. foreach my $outform (@output_formats) {
  256. my $outfile = "ecgen.${curvename}.${paramenc}." . lc $outform;
  257. ok(!run(app([ 'openssl', 'genpkey',
  258. '-algorithm', 'EC',
  259. '-pkeyopt', 'ec_paramgen_curve:'.$curvename,
  260. '-pkeyopt', 'ec_param_enc:'.$paramenc,
  261. '-outform', $outform,
  262. '-out', $outfile])),
  263. "genpkey EC key on ${curvename} with ec_param_enc:'${paramenc}' (${outform})");
  264. }
  265. }
  266. };