| 
									
										
										
										
											2021-06-16 16:32:43 +08:00
										 |  |  | #! {- $config{HASHBANGPERL} -} | 
					
						
							| 
									
										
										
										
											2020-02-17 22:05:04 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | use strict; | 
					
						
							|  |  |  | use warnings; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use File::Basename; | 
					
						
							|  |  |  | use File::Spec::Functions; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-16 16:32:43 +08:00
										 |  |  | BEGIN { | 
					
						
							|  |  |  |     # This method corresponds exactly to 'use OpenSSL::Util', | 
					
						
							|  |  |  |     # but allows us to use a platform specific file spec. | 
					
						
							|  |  |  |     require {- | 
					
						
							|  |  |  |          use Cwd qw(abs_path); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          "'" . abs_path(catfile($config{sourcedir}, | 
					
						
							|  |  |  |                                 'util', 'perl', 'OpenSSL', 'Util.pm')) . "'"; | 
					
						
							|  |  |  |          -}; | 
					
						
							|  |  |  |     OpenSSL::Util->import(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-01 10:47:15 +08:00
										 |  |  | sub quote_cmd_win32 { | 
					
						
							|  |  |  |     my $cmd = ""; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     foreach my $arg (@_) { | 
					
						
							|  |  |  |         if ($arg =~ m{\A[\w,-./@]+\z}) { | 
					
						
							|  |  |  |             $cmd .= $arg . q{ };; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             $cmd .= q{"} . quote_arg_win32($arg) . q{" }; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return substr($cmd, 0, -1); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | sub quote_arg_win32 { | 
					
						
							|  |  |  |     my ($arg) = @_; | 
					
						
							|  |  |  |     my $val = ""; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     pos($arg) = 0; | 
					
						
							|  |  |  |     while (1) { | 
					
						
							|  |  |  |         return $val if (pos($arg) == length($arg)); | 
					
						
							|  |  |  |         if ($arg =~ m{\G((?:(?>[\\]*)[^"\\]+)+)}ogc) { | 
					
						
							|  |  |  |             $val .= $1; | 
					
						
							|  |  |  |         } elsif ($arg =~ m{\G"}ogc) { | 
					
						
							|  |  |  |             $val .= qq{\\"}; | 
					
						
							|  |  |  |         } elsif ($arg =~ m{\G((?>[\\]+)(?="|\z))}ogc) { | 
					
						
							|  |  |  |             $val .= qq{\\} x (2 * length($1)); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             die sprintf("Internal error quoting: '%s'\n", $arg); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-17 22:05:04 +08:00
										 |  |  | my $there = canonpath(catdir(dirname($0), updir())); | 
					
						
							|  |  |  | my $std_engines = catdir($there, 'engines'); | 
					
						
							|  |  |  | my $std_providers = catdir($there, 'providers'); | 
					
						
							| 
									
										
										
										
											2021-02-09 07:16:55 +08:00
										 |  |  | my $std_openssl_conf = catdir($there, 'apps/openssl.cnf'); | 
					
						
							| 
									
										
										
										
											2020-02-17 22:05:04 +08:00
										 |  |  | my $unix_shlib_wrap = catfile($there, 'util/shlib_wrap.sh'); | 
					
						
							| 
									
										
										
										
											2023-03-01 23:27:50 +08:00
										 |  |  | my $std_openssl_conf_include; | 
					
						
							| 
									
										
										
										
											2020-02-17 22:05:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-01 10:48:30 +08:00
										 |  |  | if ($ARGV[0] eq '-fips') { | 
					
						
							|  |  |  |     $std_openssl_conf = {- | 
					
						
							|  |  |  |          use Cwd qw(abs_path); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          "'" . abs_path(catfile($config{sourcedir}, 'test/fips-and-base.cnf')) . "'"; | 
					
						
							|  |  |  |          -}; | 
					
						
							|  |  |  |     shift; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-01 23:27:50 +08:00
										 |  |  |     $std_openssl_conf_include = catdir($there, 'providers'); | 
					
						
							| 
									
										
										
										
											2021-07-01 10:48:30 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-10 22:39:01 +08:00
										 |  |  | if ($ARGV[0] eq '-jitter') { | 
					
						
							|  |  |  |     $std_openssl_conf = {- | 
					
						
							|  |  |  |          use Cwd qw(abs_path); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          "'" . abs_path(catfile($config{sourcedir}, 'test/default-and-jitter.cnf')) . "'"; | 
					
						
							|  |  |  |          -}; | 
					
						
							|  |  |  |     shift; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $std_openssl_conf_include = catdir($there, 'providers'); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-01 23:27:50 +08:00
										 |  |  | local $ENV{OPENSSL_CONF_INCLUDE} = $std_openssl_conf_include | 
					
						
							|  |  |  |     if defined $std_openssl_conf_include | 
					
						
							|  |  |  |        &&($ENV{OPENSSL_CONF_INCLUDE} // '') eq '' | 
					
						
							|  |  |  |        && -d $std_openssl_conf_include; | 
					
						
							|  |  |  | local $ENV{OPENSSL_ENGINES} = $std_engines | 
					
						
							| 
									
										
										
										
											2020-02-17 22:05:04 +08:00
										 |  |  |     if ($ENV{OPENSSL_ENGINES} // '') eq '' && -d $std_engines; | 
					
						
							| 
									
										
										
										
											2023-03-01 23:27:50 +08:00
										 |  |  | local $ENV{OPENSSL_MODULES} = $std_providers | 
					
						
							| 
									
										
										
										
											2020-02-17 22:05:04 +08:00
										 |  |  |     if ($ENV{OPENSSL_MODULES} // '') eq '' && -d $std_providers; | 
					
						
							| 
									
										
										
										
											2023-03-01 23:27:50 +08:00
										 |  |  | local $ENV{OPENSSL_CONF} = $std_openssl_conf | 
					
						
							| 
									
										
										
										
											2021-02-09 07:16:55 +08:00
										 |  |  |     if ($ENV{OPENSSL_CONF} // '') eq '' && -f $std_openssl_conf; | 
					
						
							| 
									
										
										
										
											2023-03-01 23:27:50 +08:00
										 |  |  | {- | 
					
						
							|  |  |  |      # For VMS, we define logical names to get the libraries properly | 
					
						
							|  |  |  |      # defined. | 
					
						
							|  |  |  |      use File::Spec::Functions qw(rel2abs); | 
					
						
							| 
									
										
										
										
											2020-02-17 22:05:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-01 23:27:50 +08:00
										 |  |  |      if ($^O eq "VMS") { | 
					
						
							|  |  |  |          my $bldtop = rel2abs($config{builddir}); | 
					
						
							|  |  |  |          my %names = | 
					
						
							|  |  |  |              map { platform->sharedname($_) => $bldtop.platform->sharedlib($_) } | 
					
						
							|  |  |  |              grep { !$unified_info{attributes}->{libraries}->{$_}->{noinst} } | 
					
						
							|  |  |  |              @{$unified_info{libraries}}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          foreach (sort keys %names) { | 
					
						
							|  |  |  |              $OUT .= "local \$ENV\{'$_'\} = '$names{$_}';\n"; | 
					
						
							|  |  |  |          } | 
					
						
							|  |  |  |      } | 
					
						
							|  |  |  | -} | 
					
						
							| 
									
										
										
										
											2020-02-17 22:05:04 +08:00
										 |  |  | my $use_system = 0; | 
					
						
							|  |  |  | my @cmd; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-16 16:32:43 +08:00
										 |  |  | if ($^O eq 'VMS') { | 
					
						
							|  |  |  |     # VMS needs the command to be appropriately quotified | 
					
						
							|  |  |  |     @cmd = fixup_cmd(@ARGV); | 
					
						
							|  |  |  | } elsif (-x $unix_shlib_wrap) { | 
					
						
							| 
									
										
										
										
											2020-02-17 22:05:04 +08:00
										 |  |  |     @cmd = ( $unix_shlib_wrap, @ARGV ); | 
					
						
							|  |  |  | } else { | 
					
						
							|  |  |  |     # Hope for the best | 
					
						
							|  |  |  |     @cmd = ( @ARGV ); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # The exec() statement on MSWin32 doesn't seem to give back the exit code | 
					
						
							|  |  |  | # from the call, so we resort to using system() instead. | 
					
						
							| 
									
										
										
										
											2025-06-01 10:47:15 +08:00
										 |  |  | my $waitcode; | 
					
						
							|  |  |  | if ($^O eq 'MSWin32') { | 
					
						
							|  |  |  |     $waitcode = system(quote_cmd_win32(@cmd)); | 
					
						
							|  |  |  | } else { | 
					
						
							|  |  |  |     $waitcode = system @cmd; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-02-17 22:05:04 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | # According to documentation, -1 means that system() couldn't run the command, | 
					
						
							|  |  |  | # otherwise, the value is similar to the Unix wait() status value | 
					
						
							|  |  |  | # (exitcode << 8 | signalcode) | 
					
						
							|  |  |  | die "wrap.pl: Failed to execute '", join(' ', @cmd), "': $!\n" | 
					
						
							|  |  |  |     if $waitcode == -1; | 
					
						
							| 
									
										
										
										
											2020-03-22 11:15:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-23 00:40:54 +08:00
										 |  |  | # When the subprocess aborted on a signal, we simply raise the same signal. | 
					
						
							| 
									
										
										
										
											2022-09-14 13:07:41 +08:00
										 |  |  | kill(($? & 255) => $$) if ($? & 255) != 0; | 
					
						
							| 
									
										
										
										
											2022-08-23 00:40:54 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | # If that didn't stop this script, mimic what Unix shells do, by | 
					
						
							| 
									
										
										
										
											2020-03-22 11:15:14 +08:00
										 |  |  | # converting the signal code to an exit code by setting the high bit. | 
					
						
							|  |  |  | # This only happens on Unix flavored operating systems, the others don't | 
					
						
							|  |  |  | # have this sort of signaling to date, and simply leave the low byte zero. | 
					
						
							|  |  |  | exit(($? & 255) | 128) if ($? & 255) != 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # When not a signal, just shift down the subprocess exit code and use that. | 
					
						
							| 
									
										
										
										
											2021-06-16 12:48:12 +08:00
										 |  |  | my $exitcode = $? >> 8; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # For VMS, perl recommendations is to emulate what the C library exit() does | 
					
						
							|  |  |  | # for all non-zero exit codes, except we set the error severity rather than | 
					
						
							|  |  |  | # success. | 
					
						
							|  |  |  | # Ref: https://perldoc.perl.org/perlport#exit | 
					
						
							|  |  |  | #      https://perldoc.perl.org/perlvms#$? | 
					
						
							|  |  |  | if ($^O eq 'VMS' && $exitcode != 0) { | 
					
						
							|  |  |  |     $exitcode = | 
					
						
							|  |  |  |         0x35a000                # C facility code | 
					
						
							|  |  |  |         + ($exitcode * 8)       # shift up to make space for the 3 severity bits | 
					
						
							|  |  |  |         + 2                     # Severity: E(rror) | 
					
						
							|  |  |  |         + 0x10000000;           # bit 28 set => the shell stays silent | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | exit($exitcode); |