Skip to content

Commit 82360cc

Browse files
FlorisTurkenburgjanlam7
authored andcommitted
throw exception on openssl_open and openssl_seal fails in encryption properties (#49)
1 parent ee94621 commit 82360cc

File tree

4 files changed

+35
-5
lines changed

4 files changed

+35
-5
lines changed

src/Resources/templates/get.php.twig

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,13 @@
8787
$iv = hex2bin(substr($pieces, $env_key_length, $iv_length));
8888
$sealed_data = hex2bin(substr($pieces, $env_key_length + $iv_length));
8989

90-
openssl_open($sealed_data, $open_data, $env_key, $private_key, 'AES256', $iv);
90+
if (false === openssl_open($sealed_data, $open_data, $env_key, $private_key, 'AES256', $iv)) {
91+
$err_string = '';
92+
while ($msg = openssl_error_string()) {
93+
$err_string .= $msg . ' | ';
94+
}
95+
throw new \InvalidArgumentException(sprintf('openssl_open failed. Message: %s', $err_string));
96+
}
9197

9298
return $open_data;
9399
{% elseif property.type == 'integer' %}

src/Resources/templates/set.php.twig

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,13 @@
165165
}
166166

167167
$iv = openssl_random_pseudo_bytes(32);
168-
openssl_seal(${{ property.name }}, $sealed_data, $env_keys, [$public_key], 'AES256', $iv);
168+
if (false === openssl_seal(${{ property.name }}, $sealed_data, $env_keys, [$public_key], 'AES256', $iv)) {
169+
$err_string = '';
170+
while ($msg = openssl_error_string()) {
171+
$err_string .= $msg . ' | ';
172+
}
173+
throw new \InvalidArgumentException(sprintf('openssl_seal failed. Message: %s', $err_string));
174+
}
169175

170176
$env_key = bin2hex($env_keys[0]);
171177
$iv = bin2hex($iv);

test/Generator/fixtures/expected/CredentialsAgainMethodsTrait.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,13 @@ public function setPassword($password)
5050
}
5151

5252
$iv = openssl_random_pseudo_bytes(32);
53-
openssl_seal($password, $sealed_data, $env_keys, [$public_key], 'AES256', $iv);
53+
if (false === openssl_seal($password, $sealed_data, $env_keys, [$public_key], 'AES256', $iv)) {
54+
$err_string = '';
55+
while ($msg = openssl_error_string()) {
56+
$err_string .= $msg . ' | ';
57+
}
58+
throw new \InvalidArgumentException(sprintf('openssl_seal failed. Message: %s', $err_string));
59+
}
5460

5561
$env_key = bin2hex($env_keys[0]);
5662
$iv = bin2hex($iv);

test/Generator/fixtures/expected/CredentialsMethodsTrait.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,13 @@ public function getPassword(): string
5151
$iv = hex2bin(substr($pieces, $env_key_length, $iv_length));
5252
$sealed_data = hex2bin(substr($pieces, $env_key_length + $iv_length));
5353

54-
openssl_open($sealed_data, $open_data, $env_key, $private_key, 'AES256', $iv);
54+
if (false === openssl_open($sealed_data, $open_data, $env_key, $private_key, 'AES256', $iv)) {
55+
$err_string = '';
56+
while ($msg = openssl_error_string()) {
57+
$err_string .= $msg . ' | ';
58+
}
59+
throw new \InvalidArgumentException(sprintf('openssl_open failed. Message: %s', $err_string));
60+
}
5561

5662
return $open_data;
5763
}
@@ -97,7 +103,13 @@ public function setPassword($password)
97103
}
98104

99105
$iv = openssl_random_pseudo_bytes(32);
100-
openssl_seal($password, $sealed_data, $env_keys, [$public_key], 'AES256', $iv);
106+
if (false === openssl_seal($password, $sealed_data, $env_keys, [$public_key], 'AES256', $iv)) {
107+
$err_string = '';
108+
while ($msg = openssl_error_string()) {
109+
$err_string .= $msg . ' | ';
110+
}
111+
throw new \InvalidArgumentException(sprintf('openssl_seal failed. Message: %s', $err_string));
112+
}
101113

102114
$env_key = bin2hex($env_keys[0]);
103115
$iv = bin2hex($iv);

0 commit comments

Comments
 (0)