| 
									
										
										
										
											2019-11-11 21:52:52 +08:00
										 |  |  | #! /usr/bin/env perl | 
					
						
							| 
									
										
										
										
											2020-04-23 20:55:52 +08:00
										 |  |  | # Copyright 2017-2020 The OpenSSL Project Authors. All Rights Reserved. | 
					
						
							| 
									
										
										
										
											2019-11-11 21:52:52 +08:00
										 |  |  | # | 
					
						
							|  |  |  | # Licensed under the Apache License 2.0 (the "License").  You may not use | 
					
						
							|  |  |  | # this file except in compliance with the License.  You can obtain a copy | 
					
						
							|  |  |  | # in the file LICENSE in the source distribution or at | 
					
						
							|  |  |  | # https://www.openssl.org/source/license.html | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use strict; | 
					
						
							|  |  |  | use warnings; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use File::Spec; | 
					
						
							| 
									
										
										
										
											2020-02-13 03:29:33 +08:00
										 |  |  | use File::Basename; | 
					
						
							| 
									
										
										
										
											2019-11-11 21:52:52 +08:00
										 |  |  | use OpenSSL::Test qw/:DEFAULT with srctop_file/; | 
					
						
							|  |  |  | use OpenSSL::Test::Utils; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | setup("test_dgst"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | plan tests => 5; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | sub tsignverify { | 
					
						
							|  |  |  |     my $testtext = shift; | 
					
						
							|  |  |  |     my $privkey = shift; | 
					
						
							|  |  |  |     my $pubkey = shift; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-10 20:15:28 +08:00
										 |  |  |     my $data_to_sign = srctop_file('test', 'data.txt'); | 
					
						
							|  |  |  |     my $other_data = srctop_file('test', 'data2.txt'); | 
					
						
							| 
									
										
										
										
											2019-11-11 21:52:52 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-13 03:29:33 +08:00
										 |  |  |     my $sigfile = basename($privkey, '.pem') . '.sig'; | 
					
						
							| 
									
										
										
										
											2019-11-11 21:52:52 +08:00
										 |  |  |     plan tests => 4; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ok(run(app(['openssl', 'dgst', '-sign', $privkey, | 
					
						
							| 
									
										
										
										
											2020-02-13 03:29:33 +08:00
										 |  |  |                 '-out', $sigfile, | 
					
						
							| 
									
										
										
										
											2019-11-11 21:52:52 +08:00
										 |  |  |                 $data_to_sign])), | 
					
						
							|  |  |  |        $testtext.": Generating signature"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ok(run(app(['openssl', 'dgst', '-prverify', $privkey, | 
					
						
							| 
									
										
										
										
											2020-02-13 03:29:33 +08:00
										 |  |  |                 '-signature', $sigfile, | 
					
						
							| 
									
										
										
										
											2019-11-11 21:52:52 +08:00
										 |  |  |                 $data_to_sign])), | 
					
						
							|  |  |  |        $testtext.": Verify signature with private key"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ok(run(app(['openssl', 'dgst', '-verify', $pubkey, | 
					
						
							| 
									
										
										
										
											2020-02-13 03:29:33 +08:00
										 |  |  |                 '-signature', $sigfile, | 
					
						
							| 
									
										
										
										
											2019-11-11 21:52:52 +08:00
										 |  |  |                 $data_to_sign])), | 
					
						
							|  |  |  |        $testtext.": Verify signature with public key"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ok(!run(app(['openssl', 'dgst', '-verify', $pubkey, | 
					
						
							| 
									
										
										
										
											2020-02-13 03:29:33 +08:00
										 |  |  |                  '-signature', $sigfile, | 
					
						
							| 
									
										
										
										
											2019-11-11 21:52:52 +08:00
										 |  |  |                  $other_data])), | 
					
						
							|  |  |  |        $testtext.": Expect failure verifying mismatching data"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | SKIP: { | 
					
						
							|  |  |  |     skip "RSA is not supported by this OpenSSL build", 1 | 
					
						
							|  |  |  |         if disabled("rsa"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     subtest "RSA signature generation and verification with `dgst` CLI" => sub { | 
					
						
							|  |  |  |         tsignverify("RSA", | 
					
						
							|  |  |  |                     srctop_file("test","testrsa.pem"), | 
					
						
							|  |  |  |                     srctop_file("test","testrsapub.pem")); | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | SKIP: { | 
					
						
							|  |  |  |     skip "DSA is not supported by this OpenSSL build", 1 | 
					
						
							|  |  |  |         if disabled("dsa"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     subtest "DSA signature generation and verification with `dgst` CLI" => sub { | 
					
						
							|  |  |  |         tsignverify("DSA", | 
					
						
							|  |  |  |                     srctop_file("test","testdsa.pem"), | 
					
						
							|  |  |  |                     srctop_file("test","testdsapub.pem")); | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | SKIP: { | 
					
						
							|  |  |  |     skip "ECDSA is not supported by this OpenSSL build", 1 | 
					
						
							|  |  |  |         if disabled("ec"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     subtest "ECDSA signature generation and verification with `dgst` CLI" => sub { | 
					
						
							|  |  |  |         tsignverify("ECDSA", | 
					
						
							|  |  |  |                     srctop_file("test","testec-p256.pem"), | 
					
						
							|  |  |  |                     srctop_file("test","testecpub-p256.pem")); | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | SKIP: { | 
					
						
							|  |  |  |     skip "EdDSA is not supported by this OpenSSL build", 2 | 
					
						
							|  |  |  |         if disabled("ec"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     skip "EdDSA is not supported with `dgst` CLI", 2; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     subtest "Ed25519 signature generation and verification with `dgst` CLI" => sub { | 
					
						
							|  |  |  |         tsignverify("Ed25519", | 
					
						
							|  |  |  |                     srctop_file("test","tested25519.pem"), | 
					
						
							|  |  |  |                     srctop_file("test","tested25519pub.pem")); | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     subtest "Ed448 signature generation and verification with `dgst` CLI" => sub { | 
					
						
							|  |  |  |         tsignverify("Ed448", | 
					
						
							|  |  |  |                     srctop_file("test","tested448.pem"), | 
					
						
							|  |  |  |                     srctop_file("test","tested448pub.pem")); | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | } |