20-test_dgst.t 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 OpenSSL::Test qw/:DEFAULT with srctop_file bldtop_dir/;
  12. use OpenSSL::Test::Utils;
  13. setup("test_dgst");
  14. plan tests => 6;
  15. sub tsignverify {
  16. my $testtext = shift;
  17. my $privkey = shift;
  18. my $pubkey = shift;
  19. my $data_to_sign = srctop_file('test', 'README');
  20. my $other_data = srctop_file('test', 'README.external');
  21. plan tests => 4;
  22. ok(run(app(['openssl', 'dgst', '-sign', $privkey,
  23. '-out', 'testdgst.sig',
  24. $data_to_sign])),
  25. $testtext.": Generating signature");
  26. ok(run(app(['openssl', 'dgst', '-prverify', $privkey,
  27. '-signature', 'testdgst.sig',
  28. $data_to_sign])),
  29. $testtext.": Verify signature with private key");
  30. ok(run(app(['openssl', 'dgst', '-verify', $pubkey,
  31. '-signature', 'testdgst.sig',
  32. $data_to_sign])),
  33. $testtext.": Verify signature with public key");
  34. ok(!run(app(['openssl', 'dgst', '-verify', $pubkey,
  35. '-signature', 'testdgst.sig',
  36. $other_data])),
  37. $testtext.": Expect failure verifying mismatching data");
  38. unlink 'testdgst.sig';
  39. }
  40. SKIP: {
  41. skip "RSA is not supported by this OpenSSL build", 1
  42. if disabled("rsa");
  43. subtest "RSA signature generation and verification with `dgst` CLI" => sub {
  44. tsignverify("RSA",
  45. srctop_file("test","testrsa.pem"),
  46. srctop_file("test","testrsapub.pem"));
  47. };
  48. }
  49. SKIP: {
  50. skip "DSA is not supported by this OpenSSL build", 1
  51. if disabled("dsa");
  52. subtest "DSA signature generation and verification with `dgst` CLI" => sub {
  53. tsignverify("DSA",
  54. srctop_file("test","testdsa.pem"),
  55. srctop_file("test","testdsapub.pem"));
  56. };
  57. }
  58. SKIP: {
  59. skip "ECDSA is not supported by this OpenSSL build", 1
  60. if disabled("ec");
  61. subtest "ECDSA signature generation and verification with `dgst` CLI" => sub {
  62. tsignverify("ECDSA",
  63. srctop_file("test","testec-p256.pem"),
  64. srctop_file("test","testecpub-p256.pem"));
  65. };
  66. }
  67. SKIP: {
  68. skip "EdDSA is not supported by this OpenSSL build", 2
  69. if disabled("ec");
  70. skip "EdDSA is not supported with `dgst` CLI", 2;
  71. subtest "Ed25519 signature generation and verification with `dgst` CLI" => sub {
  72. tsignverify("Ed25519",
  73. srctop_file("test","tested25519.pem"),
  74. srctop_file("test","tested25519pub.pem"));
  75. };
  76. subtest "Ed448 signature generation and verification with `dgst` CLI" => sub {
  77. tsignverify("Ed448",
  78. srctop_file("test","tested448.pem"),
  79. srctop_file("test","tested448pub.pem"));
  80. };
  81. }
  82. SKIP: {
  83. skip "dgst with engine is not supported by this OpenSSL build", 1
  84. if disabled("engine") || disabled("dynamic-engine");
  85. subtest "SHA1 generation by engine with `dgst` CLI" => sub {
  86. plan tests => 1;
  87. my $testdata = srctop_file('test', 'data.bin');
  88. # intentionally using -engine twice, please do not remove the duplicate line
  89. my @macdata = run(app(['openssl', 'dgst', '-sha1',
  90. '-engine', "ossltest",
  91. '-engine', "ossltest",
  92. $testdata]), capture => 1);
  93. chomp(@macdata);
  94. my $expected = qr/SHA1\(\Q$testdata\E\)= 000102030405060708090a0b0c0d0e0f10111213/;
  95. ok($macdata[0] =~ $expected, "SHA1: Check HASH value is as expected ($macdata[0]) vs ($expected)");
  96. }
  97. }