15-test_gensm2.t 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #! /usr/bin/env perl
  2. # Copyright 2023-2024 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 pipe);
  12. use OpenSSL::Test::Utils;
  13. # These are special key generation tests for SM2 keys specifically,
  14. # as they could be said to be a bit special in their encoding.
  15. # This is an auxiliary test to 15-test_genec.t
  16. setup("test_gensm2");
  17. plan skip_all => "This test is unsupported in a no-sm2 build"
  18. if disabled("sm2");
  19. plan tests => 2;
  20. # According to the example in GM/T 0015-2012, appendix D.2,
  21. # generating an EC key with the named SM2 curve or generating
  22. # an SM2 key should end up with the same encoding (apart from
  23. # key private key field itself). This regular expressions
  24. # shows us what 'openssl asn1parse' should display.
  25. my $sm2_re = qr|
  26. ^
  27. .*?\Qcons: SEQUENCE\E\s+?\R
  28. .*?\Qprim: INTEGER :00\E\R
  29. .*?\Qcons: SEQUENCE\E\s+?\R
  30. .*?\Qprim: OBJECT :id-ecPublicKey\E\R
  31. .*?\Qprim: OBJECT :sm2\E\R
  32. .*?\Qprim: OCTET STRING [HEX DUMP]:\E
  33. |mx;
  34. my $cmd_genec = app([ 'openssl', 'genpkey',
  35. '-algorithm', 'EC',
  36. '-pkeyopt', 'ec_paramgen_curve:SM2',
  37. '-pkeyopt', 'ec_param_enc:named_curve' ]);
  38. my $cmd_gensm2 = app([ 'openssl', 'genpkey', '-algorithm', 'SM2' ]);
  39. my $cmd_asn1parse = app([ 'openssl', 'asn1parse', '-i' ]);
  40. my $result_ec = join("", run(pipe($cmd_genec, $cmd_asn1parse),
  41. capture => 1));
  42. like($result_ec, $sm2_re,
  43. "Check that 'genpkey -algorithm EC' resulted in a correctly encoded SM2 key");
  44. my $result_sm2 = join("", run(pipe($cmd_gensm2, $cmd_asn1parse),
  45. capture => 1));
  46. like($result_sm2, $sm2_re,
  47. "Check that 'genpkey -algorithm SM2' resulted in a correctly encoded SM2 key");