| 1 | package Jar::Signer; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 1 | ▶ | use File::Basename; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | ▶ | use File::chdir; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 1 | our $VERSION = 0.1; | |||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 0 | ▶ | sub AUTOLOAD{ | ||||||||||||||||||
|
Subroutine
AUTOLOAD: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 9 | 0 | my $self = shift; | |||||||||||||||||||
| 10 | 0 | ▶ | my $type = ref $self || die "$self is not an object"; | ||||||||||||||||||
|
Condition0%0/2
ref $self || die("$self is not an object")
MC/DC0%0/1
ref $self || die("$self is not an object")
ref $self
Truth table0%0/2
A
A= ref $self
| |||||||||||||||||||||
| 11 | 0 | my $name = $AUTOLOAD; | |||||||||||||||||||
| 12 | 0 | $name =~ s/.*://; | |||||||||||||||||||
| 13 | 0 | ▶ | if(@_){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 14 | 0 | $self->{$name} = shift; | |||||||||||||||||||
| 15 | } | ||||||||||||||||||||
| 16 | 0 | return $self->{$name}; | |||||||||||||||||||
| 17 | } | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | 0 | ▶ | sub new{ | ||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 20 | 0 | my $self = shift; | |||||||||||||||||||
| 21 | 0 | ▶ | my $class = ref $self || $self; | ||||||||||||||||||
|
Condition0%0/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table0%0/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||
| 22 | 0 | my $this = bless {}, $class; | |||||||||||||||||||
| 23 | 0 | return $this; | |||||||||||||||||||
| 24 | } | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 0 | ▶ | sub process{ | ||||||||||||||||||
|
Subroutine
process: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||||||||
| 27 | 0 | my $this = shift; | |||||||||||||||||||
| 28 | 0 | my $jar = $this->jar; | |||||||||||||||||||
| 29 | 0 | ▶ | die "Cannot find Jar file $jar\n" unless -e $jar; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 0 | (my $base = $jar) =~ s/\.jar$//; | |||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 0 | my $signed_jar = $this->signed_jar; | |||||||||||||||||||
| 34 | 0 | ▶ | unless( $signed_jar ){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 35 | 0 | $this->signed_jar("$base.signed.jar"); | |||||||||||||||||||
| 36 | } | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | 0 | my $policy_file = "$base.policy"; | |||||||||||||||||||
| 39 | 0 | $this->policy_file($policy_file); | |||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | 0 | ▶ | if( -e $policy_file ){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 42 | 0 | warn sprintf("Adding policy file %s to Jar %s\n",$policy_file,$jar); | |||||||||||||||||||
| 43 | 0 | $this->add_policy_file; | |||||||||||||||||||
| 44 | } | ||||||||||||||||||||
| 45 | else { | ||||||||||||||||||||
| 46 | 0 | my @files = `jar -tf $jar`; | |||||||||||||||||||
| 47 | 0 | my $base_policy = basename($policy_file); | |||||||||||||||||||
| 48 | 0 | ▶ | unless( grep( /^${base_policy}$/, @files) ){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 49 | 0 | die sprintf("Cannot find policy file '%s' for this Jar file and the Jar does not appear to contain it.\nA policy file for this Jar might look something like:\n\n%s\n" | |||||||||||||||||||
| 50 | , $this->policy_file | ||||||||||||||||||||
| 51 | , $this->_demo_policy_file | ||||||||||||||||||||
| 52 | ); | ||||||||||||||||||||
| 53 | } | ||||||||||||||||||||
| 54 | } | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | 0 | ▶ | $this->generate_keystore unless -e $this->keystore; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 57 | 0 | my $alias = $this->alias; | |||||||||||||||||||
| 58 | 0 | ▶ | $this->generate_cert unless -e "$alias.cert"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 59 | 0 | ▶ | $this->generate_fingerprint unless -e "$alias.fingerprint"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 0 | $this->generate_signed_jar; | |||||||||||||||||||
| 62 | 0 | return; | |||||||||||||||||||
| 63 | } | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 0 | ▶ | sub add_policy_file{ | ||||||||||||||||||
|
Subroutine
add_policy_file: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 66 | 0 | my $this = shift; | |||||||||||||||||||
| 67 | 0 | local $CWD = dirname($this->jar); | |||||||||||||||||||
| 68 | 0 | my $jar = basename($this->jar); | |||||||||||||||||||
| 69 | 0 | my $policy_file = basename($this->policy_file); | |||||||||||||||||||
| 70 | 0 | ▶ | system('jar','-uf',$jar,$policy_file) == 0 or die $!; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 71 | 0 | return; | |||||||||||||||||||
| 72 | } | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | 0 | ▶ | sub generate_signed_jar{ | ||||||||||||||||||
|
Subroutine
generate_signed_jar: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 75 | 0 | my $this = shift; | |||||||||||||||||||
| 76 | 0 | my $jar = $this->jar; | |||||||||||||||||||
| 77 | 0 | my $signed_jar = $this->signed_jar; | |||||||||||||||||||
| 78 | 0 | my $keystore = $this->keystore; | |||||||||||||||||||
| 79 | 0 | my $alias = $this->alias; | |||||||||||||||||||
| 80 | 0 | my $base_alias = basename $alias; | |||||||||||||||||||
| 81 | # jar -> signedjar | ||||||||||||||||||||
| 82 | 0 | @cmd = ( 'jarsigner', '-keystore', $keystore, '-signedjar', $signed_jar, $jar, $base_alias ); | |||||||||||||||||||
| 83 | 0 | ▶ | system(@cmd) == 0 or die $!; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 84 | 0 | return; | |||||||||||||||||||
| 85 | } | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 0 | ▶ | sub generate_fingerprint{ | ||||||||||||||||||
|
Subroutine
generate_fingerprint: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 88 | 0 | my $this = shift; | |||||||||||||||||||
| 89 | 0 | my $keystore = $this->keystore; | |||||||||||||||||||
| 90 | 0 | my $alias = $this->alias; | |||||||||||||||||||
| 91 | 0 | ▶ | system("keytool -printcert -file $alias.cert > $alias.fingerprint") == 0 or die $!; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 92 | 0 | return; | |||||||||||||||||||
| 93 | } | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 0 | ▶ | sub generate_cert{ | ||||||||||||||||||
|
Subroutine
generate_cert: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 96 | 0 | my $this = shift; | |||||||||||||||||||
| 97 | 0 | my $keystore = $this->keystore; | |||||||||||||||||||
| 98 | 0 | my $alias = $this->alias; | |||||||||||||||||||
| 99 | 0 | my $base_alias = basename $alias; | |||||||||||||||||||
| 100 | 0 | @cmd = ( 'keytool', '-export', '-rfc', '-keystore', $keystore, '-alias', $base_alias, '-file', "$alias.cert" ); | |||||||||||||||||||
| 101 | 0 | ▶ | system(@cmd) == 0 or die $!; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 102 | 0 | return; | |||||||||||||||||||
| 103 | } | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | 0 | ▶ | sub generate_keystore{ | ||||||||||||||||||
|
Subroutine
generate_keystore: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 106 | 0 | my $this = shift; | |||||||||||||||||||
| 107 | 0 | my $alias = $this->alias; | |||||||||||||||||||
| 108 | 0 | my $base_alias = basename $alias; | |||||||||||||||||||
| 109 | 0 | my $keystore = $this->keystore; | |||||||||||||||||||
| 110 | 0 | my $dname = $this->dname; | |||||||||||||||||||
| 111 | 0 | my @cmd = ( 'keytool', '-genkey', '-alias', $base_alias, '-keystore', $keystore, '-dname', $dname ); | |||||||||||||||||||
| 112 | 0 | ▶ | system(@cmd) == 0 or die $!; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 113 | 0 | return; | |||||||||||||||||||
| 114 | } | ||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | 0 | ▶ | sub _demo_policy_file{ | ||||||||||||||||||
|
Subroutine
_demo_policy_file: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 117 | 0 | my $this = shift; | |||||||||||||||||||
| 118 | 0 | my $signed_jar = basename( $this->signed_jar ); | |||||||||||||||||||
| 119 | 0 | my $keystore = basename( $this->keystore ); | |||||||||||||||||||
| 120 | 0 | my $policy = <<"EOF"; | |||||||||||||||||||
| 121 | keystore "file:$keystore"; | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | grant signedBy "$keystore", codeBase "http://host/path/to/$signed_jar" { | ||||||||||||||||||||
| 124 | permission java.security.AllPermission; | ||||||||||||||||||||
| 125 | permission java.io.FilePermission "<<ALL FILES>>", "read, write, delete"; | ||||||||||||||||||||
| 126 | permission java.lang.RuntimePermission "createClassLoader"; | ||||||||||||||||||||
| 127 | }; | ||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | grant signedBy "$keystore", codeBase "file:$signed_jar" { | ||||||||||||||||||||
| 130 | permission java.security.AllPermission; | ||||||||||||||||||||
| 131 | permission java.io.FilePermission "<<ALL FILES>>", "read, write, delete"; | ||||||||||||||||||||
| 132 | permission java.lang.RuntimePermission "createClassLoader"; | ||||||||||||||||||||
| 133 | }; | ||||||||||||||||||||
| 134 | EOF | ||||||||||||||||||||
| 135 | 0 | return $policy; | |||||||||||||||||||
| 136 | } | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | 1 | 1; | |||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | __END__ | ||||||||||||||||||||