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