20-test_pkeyutl.t 6.0 KB

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