20-test_pkeyutl.t 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #! /usr/bin/env perl
  2. # Copyright 2018-2023 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::Basename;
  12. use OpenSSL::Test qw/:DEFAULT srctop_file ok_nofips/;
  13. use OpenSSL::Test::Utils;
  14. use File::Compare qw/compare_text/;
  15. setup("test_pkeyutl");
  16. plan tests => 14;
  17. # For the tests below we use the cert itself as the TBS file
  18. SKIP: {
  19. skip "Skipping tests that require EC, SM2 or SM3", 4
  20. if disabled("ec") || disabled("sm2") || disabled("sm3");
  21. # SM2
  22. ok_nofips(run(app(([ 'openssl', 'pkeyutl', '-sign',
  23. '-in', srctop_file('test', 'certs', 'sm2.pem'),
  24. '-inkey', srctop_file('test', 'certs', 'sm2.key'),
  25. '-out', 'sm2.sig', '-rawin',
  26. '-digest', 'sm3', '-pkeyopt', 'distid:someid']))),
  27. "Sign a piece of data using SM2");
  28. ok_nofips(run(app(([ 'openssl', 'pkeyutl',
  29. '-verify', '-certin',
  30. '-in', srctop_file('test', 'certs', 'sm2.pem'),
  31. '-inkey', srctop_file('test', 'certs', 'sm2.pem'),
  32. '-sigfile', 'sm2.sig', '-rawin',
  33. '-digest', 'sm3', '-pkeyopt', 'distid:someid']))),
  34. "Verify an SM2 signature against a piece of data");
  35. ok_nofips(run(app(([ 'openssl', 'pkeyutl', '-encrypt',
  36. '-in', srctop_file('test', 'data2.bin'),
  37. '-inkey', srctop_file('test', 'certs', 'sm2-pub.key'),
  38. '-pubin', '-out', 'sm2.enc']))),
  39. "Encrypt a piece of data using SM2");
  40. ok_nofips(run(app(([ 'openssl', 'pkeyutl', '-decrypt',
  41. '-in', 'sm2.enc',
  42. '-inkey', srctop_file('test', 'certs', 'sm2.key'),
  43. '-out', 'sm2.dat'])))
  44. && compare_text('sm2.dat',
  45. srctop_file('test', 'data2.bin')) == 0,
  46. "Decrypt a piece of data using SM2");
  47. }
  48. SKIP: {
  49. skip "Skipping tests that require ECX", 4
  50. if disabled("ecx");
  51. # Ed25519
  52. ok(run(app(([ 'openssl', 'pkeyutl', '-sign', '-in',
  53. srctop_file('test', 'certs', 'server-ed25519-cert.pem'),
  54. '-inkey', srctop_file('test', 'certs', 'server-ed25519-key.pem'),
  55. '-out', 'Ed25519.sig', '-rawin']))),
  56. "Sign a piece of data using Ed25519");
  57. ok(run(app(([ 'openssl', 'pkeyutl', '-verify', '-certin', '-in',
  58. srctop_file('test', 'certs', 'server-ed25519-cert.pem'),
  59. '-inkey', srctop_file('test', 'certs', 'server-ed25519-cert.pem'),
  60. '-sigfile', 'Ed25519.sig', '-rawin']))),
  61. "Verify an Ed25519 signature against a piece of data");
  62. # Ed448
  63. ok(run(app(([ 'openssl', 'pkeyutl', '-sign', '-in',
  64. srctop_file('test', 'certs', 'server-ed448-cert.pem'),
  65. '-inkey', srctop_file('test', 'certs', 'server-ed448-key.pem'),
  66. '-out', 'Ed448.sig', '-rawin']))),
  67. "Sign a piece of data using Ed448");
  68. ok(run(app(([ 'openssl', 'pkeyutl', '-verify', '-certin', '-in',
  69. srctop_file('test', 'certs', 'server-ed448-cert.pem'),
  70. '-inkey', srctop_file('test', 'certs', 'server-ed448-cert.pem'),
  71. '-sigfile', 'Ed448.sig', '-rawin']))),
  72. "Verify an Ed448 signature against a piece of data");
  73. }
  74. sub tsignverify {
  75. my $testtext = shift;
  76. my $privkey = shift;
  77. my $pubkey = shift;
  78. my @extraopts = @_;
  79. my $data_to_sign = srctop_file('test', 'data.bin');
  80. my $other_data = srctop_file('test', 'data2.bin');
  81. my $sigfile = basename($privkey, '.pem') . '.sig';
  82. my @args = ();
  83. plan tests => 5;
  84. @args = ('openssl', 'pkeyutl', '-sign',
  85. '-inkey', $privkey,
  86. '-out', $sigfile,
  87. '-in', $data_to_sign);
  88. push(@args, @extraopts);
  89. ok(run(app([@args])),
  90. $testtext.": Generating signature");
  91. @args = ('openssl', 'pkeyutl', '-sign',
  92. '-inkey', $privkey,
  93. '-keyform', 'DER',
  94. '-out', $sigfile,
  95. '-in', $data_to_sign);
  96. push(@args, @extraopts);
  97. ok(!run(app([@args])),
  98. $testtext.": Checking that mismatching keyform fails");
  99. @args = ('openssl', 'pkeyutl', '-verify',
  100. '-inkey', $privkey,
  101. '-sigfile', $sigfile,
  102. '-in', $data_to_sign);
  103. push(@args, @extraopts);
  104. ok(run(app([@args])),
  105. $testtext.": Verify signature with private key");
  106. @args = ('openssl', 'pkeyutl', '-verify',
  107. '-keyform', 'PEM',
  108. '-inkey', $pubkey, '-pubin',
  109. '-sigfile', $sigfile,
  110. '-in', $data_to_sign);
  111. push(@args, @extraopts);
  112. ok(run(app([@args])),
  113. $testtext.": Verify signature with public key");
  114. @args = ('openssl', 'pkeyutl', '-verify',
  115. '-inkey', $pubkey, '-pubin',
  116. '-sigfile', $sigfile,
  117. '-in', $other_data);
  118. push(@args, @extraopts);
  119. ok(!run(app([@args])),
  120. $testtext.": Expect failure verifying mismatching data");
  121. }
  122. SKIP: {
  123. skip "RSA is not supported by this OpenSSL build", 1
  124. if disabled("rsa");
  125. subtest "RSA CLI signature generation and verification" => sub {
  126. tsignverify("RSA",
  127. srctop_file("test","testrsa.pem"),
  128. srctop_file("test","testrsapub.pem"),
  129. "-rawin", "-digest", "sha256");
  130. };
  131. subtest "RSA CLI signature and verification with pkeyopt" => sub {
  132. tsignverify("RSA",
  133. srctop_file("test","testrsa.pem"),
  134. srctop_file("test","testrsapub.pem"),
  135. "-rawin", "-digest", "sha256",
  136. "-pkeyopt", "rsa_padding_mode:pss");
  137. };
  138. }
  139. SKIP: {
  140. skip "DSA is not supported by this OpenSSL build", 1
  141. if disabled("dsa");
  142. subtest "DSA CLI signature generation and verification" => sub {
  143. tsignverify("DSA",
  144. srctop_file("test","testdsa.pem"),
  145. srctop_file("test","testdsapub.pem"),
  146. "-rawin", "-digest", "sha256");
  147. };
  148. }
  149. SKIP: {
  150. skip "ECDSA is not supported by this OpenSSL build", 1
  151. if disabled("ec");
  152. subtest "ECDSA CLI signature generation and verification" => sub {
  153. tsignverify("ECDSA",
  154. srctop_file("test","testec-p256.pem"),
  155. srctop_file("test","testecpub-p256.pem"),
  156. "-rawin", "-digest", "sha256");
  157. };
  158. }
  159. SKIP: {
  160. skip "EdDSA is not supported by this OpenSSL build", 2
  161. if disabled("ecx");
  162. subtest "Ed2559 CLI signature generation and verification" => sub {
  163. tsignverify("Ed25519",
  164. srctop_file("test","tested25519.pem"),
  165. srctop_file("test","tested25519pub.pem"),
  166. "-rawin");
  167. };
  168. subtest "Ed448 CLI signature generation and verification" => sub {
  169. tsignverify("Ed448",
  170. srctop_file("test","tested448.pem"),
  171. srctop_file("test","tested448pub.pem"),
  172. "-rawin");
  173. };
  174. }