Skip to content

Commit 74a77a3

Browse files
Merge pull request #23 from darkwebdesign/ISSUE-7
ISSUE-7: PemFile::validate asks for passphrase on OpenSSL 1.1.0g-fips 2 Nov 2017
2 parents 56df859 + d27f341 commit 74a77a3

12 files changed

+149
-61
lines changed

.scripts/compile-openssl.sh

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env bash
2+
3+
readonly DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)";
4+
readonly SCRIPT="$(basename "${BASH_SOURCE[0]}")";
5+
6+
readonly VERSION="${1}";
7+
readonly VERSION_PREFIX="$(echo "$VERSION" | sed --regexp-extended 's/^([0-9]+\.[0-9]+\.[0-9]+).*$/\1/')";
8+
9+
readonly ROOT_DIRECTORY="$(dirname "$DIR")";
10+
readonly TEMP_DIRECTORY="$(mktemp --directory)";
11+
readonly OUTPUT_DIRECTORY="$ROOT_DIRECTORY/build/openssl/$VERSION";
12+
13+
if [[ -z "$VERSION" ]]; then
14+
echo "$SCRIPT: no version specified!";
15+
exit 1;
16+
fi;
17+
18+
echo "VERSION: $VERSION";
19+
echo "TEMP_DIRECTORY: $TEMP_DIRECTORY";
20+
echo "OUTPUT_DIRECTORY: $OUTPUT_DIRECTORY";
21+
22+
read -p 'Press enter to continue...';
23+
24+
wget --timestamping --directory-prefix "$TEMP_DIRECTORY/" "https://www.openssl.org/source/openssl-$VERSION.tar.gz";
25+
26+
if [[ $? -ne 0 ]]; then
27+
wget --timestamping --directory-prefix "$TEMP_DIRECTORY/" "https://www.openssl.org/source/old/$VERSION_PREFIX/openssl-$VERSION.tar.gz";
28+
29+
if [[ $? -ne 0 ]]; then
30+
echo "$SCRIPT: version not found!";
31+
exit 1;
32+
fi;
33+
fi;
34+
35+
tar --extract --verbose --gzip --directory "$TEMP_DIRECTORY/" --file "$TEMP_DIRECTORY/openssl-$VERSION.tar.gz";
36+
37+
cd "$TEMP_DIRECTORY/openssl-$VERSION";
38+
39+
mkdir --parents "$OUTPUT_DIRECTORY";
40+
41+
./config --prefix="$OUTPUT_DIRECTORY" --openssldir="$OUTPUT_DIRECTORY";
42+
43+
make;
44+
make test;
45+
make install;
46+
47+
rm -rf "$TEMP_DIRECTORY";

.scripts/phpunit-build.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env bash
2+
3+
readonly DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)";
4+
readonly SCRIPT="$(basename "${BASH_SOURCE[0]}")";
5+
6+
readonly VERSION="${1}";
7+
8+
readonly ROOT_DIRECTORY="$(dirname "$DIR")";
9+
readonly BUILD_DIRECTORY="$ROOT_DIRECTORY/build/openssl/$VERSION";
10+
readonly VENDOR_DIRECTORY="$ROOT_DIRECTORY/vendor";
11+
12+
if [[ -z "$VERSION" ]]; then
13+
echo "$SCRIPT: no version specified!";
14+
exit 1;
15+
fi;
16+
17+
if [[ ! -d "$BUILD_DIRECTORY" ]]; then
18+
echo "$SCRIPT: version build not found!";
19+
exit 1;
20+
fi;
21+
22+
export PATH="$BUILD_DIRECTORY/bin:$PATH";
23+
export LD_LIBRARY_PATH="$BUILD_DIRECTORY/lib:$LD_LIBRARY_PATH";
24+
25+
cd "$ROOT_DIRECTORY";
26+
27+
"$VENDOR_DIRECTORY/bin/phpunit";

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
language: php
22

33
php:
4-
- '5.4'
54
- '5.5'
65
- '5.6'
76
- '7.0'

File/KeystoreFile.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@
2020

2121
namespace DarkWebDesign\PublicKeyCryptographyBundle\File;
2222

23-
use DarkWebDesign\PublicKeyCryptographyBundle\Exception\PrivateKeyPassPhraseEmptyException;
24-
use DarkWebDesign\PublicKeyCryptographyBundle\File\CryptoFile;
25-
use DarkWebDesign\PublicKeyCryptographyBundle\File\PemFile;
26-
use DarkWebDesign\PublicKeyCryptographyBundle\File\PrivateKeyFile;
27-
use DarkWebDesign\PublicKeyCryptographyBundle\File\PublicKeyFile;
28-
use Symfony\Component\HttpFoundation\File\File;
2923
use Symfony\Component\Process\Process;
3024

3125
/**
@@ -44,7 +38,7 @@ protected function validate()
4438
{
4539
$in = escapeshellarg($this->getPathname());
4640

47-
$process = new Process("openssl pkcs12 -in $in -passin pass: -noout");
41+
$process = new Process("openssl pkcs12 -in $in -passin pass:anypass -noout");
4842
$process->run();
4943

5044
$invalidPassword = false !== strpos($process->getErrorOutput(), 'invalid password');
@@ -75,7 +69,6 @@ protected function validate()
7569
*/
7670
public static function create($path, $passPhrase, PublicKeyFile $publicKeyFile, PrivateKeyFile $privateKeyFile, $privateKeyPassPhrase = null)
7771
{
78-
$out = escapeshellarg($path);
7972
$pass = escapeshellarg($passPhrase);
8073
$publicKeyIn = escapeshellarg($publicKeyFile->getPathname());
8174
$publicKeyInForm = escapeshellarg($publicKeyFile->getFormat());
@@ -112,7 +105,6 @@ public static function create($path, $passPhrase, PublicKeyFile $publicKeyFile,
112105
public function getPem($path, $passPhrase)
113106
{
114107
$in = escapeshellarg($this->getPathname());
115-
$out = escapeshellarg($path);
116108
$pass = escapeshellarg($passPhrase);
117109

118110
// if the keystore pass phrase is an empty string, the outputted private key will not contain a pass phrase
@@ -155,7 +147,6 @@ public function getPem($path, $passPhrase)
155147
public function getPublicKey($path, $passPhrase)
156148
{
157149
$in = escapeshellarg($this->getPathname());
158-
$out = escapeshellarg($path);
159150
$pass = escapeshellarg($passPhrase);
160151

161152
$process1 = new Process("openssl pkcs12 -in $in -passin pass:$pass -nokeys");
@@ -187,7 +178,6 @@ public function getPublicKey($path, $passPhrase)
187178
public function getPrivateKey($path, $passPhrase)
188179
{
189180
$in = escapeshellarg($this->getPathname());
190-
$out = escapeshellarg($path);
191181
$pass = escapeshellarg($passPhrase);
192182

193183
// if the keystore pass phrase is an empty string, the outputted private key will not contain a pass phrase
@@ -332,7 +322,6 @@ public function verifyPassPhrase($passPhrase)
332322
*
333323
* @return \DarkWebDesign\PublicKeyCryptographyBundle\File\KeystoreFile
334324
*
335-
* @throws \DarkWebDesign\PublicKeyCryptographyBundle\Exception\PrivateKeyPassPhraseEmptyException
336325
* @throws \Symfony\Component\Process\Exception\ProcessFailedException
337326
*/
338327
public function changePassPhrase($passPhrase, $newPassPhrase)

File/PemFile.php

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@
2121
namespace DarkWebDesign\PublicKeyCryptographyBundle\File;
2222

2323
use DarkWebDesign\PublicKeyCryptographyBundle\Exception\PrivateKeyPassPhraseEmptyException;
24-
use DarkWebDesign\PublicKeyCryptographyBundle\File\CryptoFile;
25-
use DarkWebDesign\PublicKeyCryptographyBundle\File\KeystoreFile;
26-
use DarkWebDesign\PublicKeyCryptographyBundle\File\PrivateKeyFile;
27-
use DarkWebDesign\PublicKeyCryptographyBundle\File\PublicKeyFile;
2824
use Symfony\Component\Process\Process;
2925

3026
/**
@@ -50,12 +46,12 @@ protected function validate()
5046
return false;
5147
}
5248

53-
$process = new Process("openssl rsa -in $in -passin pass: -check -noout");
49+
$process = new Process("openssl rsa -in $in -passin pass:anypass -check -noout");
5450
$process->run();
5551

56-
$badPasswordRead = false !== strpos($process->getErrorOutput(), ':bad password read:');
52+
$badDecrypt = false !== strpos($process->getErrorOutput(), ':bad decrypt:');
5753

58-
if (!$process->isSuccessful() && !$badPasswordRead) {
54+
if (!$process->isSuccessful() && !$badDecrypt) {
5955
return false;
6056
}
6157

@@ -126,7 +122,6 @@ public static function create($path, PublicKeyFile $publicKeyFile, PrivateKeyFil
126122
throw new PrivateKeyPassPhraseEmptyException();
127123
}
128124

129-
$out = escapeshellarg($path);
130125
$publicKeyIn = escapeshellarg($publicKeyFile->getPathname());
131126
$publicKeyInForm = escapeshellarg($publicKeyFile->getFormat());
132127
$privateKeyIn = escapeshellarg($privateKeyFile->getPathname());
@@ -165,7 +160,6 @@ public static function create($path, PublicKeyFile $publicKeyFile, PrivateKeyFil
165160
public function getKeystore($path, $keystorePassPhrase, $privateKeyPassPhrase = null)
166161
{
167162
$in = escapeshellarg($this->getPathname());
168-
$out = escapeshellarg($path);
169163
$keystorePass = escapeshellarg($keystorePassPhrase);
170164
$privateKeyPass = escapeshellarg($privateKeyPassPhrase);
171165

@@ -190,7 +184,6 @@ public function getKeystore($path, $keystorePassPhrase, $privateKeyPassPhrase =
190184
public function getPublicKey($path)
191185
{
192186
$in = escapeshellarg($this->getPathname());
193-
$out = escapeshellarg($path);
194187

195188
$process = new Process("openssl x509 -in $in");
196189
$process->mustRun();
@@ -223,7 +216,6 @@ public function getPrivateKey($path, $passPhrase = null)
223216
}
224217

225218
$in = escapeshellarg($this->getPathname());
226-
$out = escapeshellarg($path);
227219
$pass = escapeshellarg($passPhrase);
228220

229221
if (null !== $passPhrase) {
@@ -318,10 +310,10 @@ public function hasPassPhrase()
318310
{
319311
$in = escapeshellarg($this->getPathname());
320312

321-
$process1 = new Process("openssl rsa -in $in -passin pass: -check -noout");
313+
$process1 = new Process("openssl rsa -in $in -passin pass:nopass -check -noout");
322314
$process1->run();
323315

324-
$process2 = new Process("openssl rsa -in $in -passin pass:nopass -check -noout");
316+
$process2 = new Process("openssl rsa -in $in -passin pass:anypass -check -noout");
325317
$process2->run();
326318

327319
return !$process1->isSuccessful() && !$process2->isSuccessful();
@@ -373,7 +365,7 @@ public function addPassPhrase($passPhrase)
373365
$process1 = new Process("openssl x509 -in $in");
374366
$process1->mustRun();
375367

376-
$process2 = new Process("openssl rsa -in $in -passin pass: -passout pass:$pass -des3");
368+
$process2 = new Process("openssl rsa -in $in -passin pass:nopass -passout pass:$pass -des3");
377369
$process2->mustRun();
378370

379371
@file_put_contents($this->getPathname(), $process1->getOutput() . $process2->getOutput());

File/PrivateKeyFile.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@
2222

2323
use DarkWebDesign\PublicKeyCryptographyBundle\Exception\FormatNotValidException;
2424
use DarkWebDesign\PublicKeyCryptographyBundle\Exception\PrivateKeyPassPhraseEmptyException;
25-
use DarkWebDesign\PublicKeyCryptographyBundle\File\CryptoFile;
26-
use DarkWebDesign\PublicKeyCryptographyBundle\File\PemFile;
27-
use DarkWebDesign\PublicKeyCryptographyBundle\File\PublicKeyFile;
2825
use Symfony\Component\Process\Process;
2926

3027
/**
@@ -51,12 +48,12 @@ protected function validate()
5148
$in = escapeshellarg($this->getPathname());
5249
$inForm = escapeshellarg($this->getFormat());
5350

54-
$process = new Process("openssl rsa -in $in -inform $inForm -passin pass: -check -noout");
51+
$process = new Process("openssl rsa -in $in -inform $inForm -passin pass:anypass -check -noout");
5552
$process->run();
5653

57-
$badPasswordRead = false !== strpos($process->getErrorOutput(), ':bad password read:');
54+
$badDecrypt = false !== strpos($process->getErrorOutput(), ':bad decrypt:');
5855

59-
if (!$process->isSuccessful() && !$badPasswordRead) {
56+
if (!$process->isSuccessful() && !$badDecrypt) {
6057
return false;
6158
}
6259

@@ -186,10 +183,10 @@ public function hasPassPhrase()
186183
$in = escapeshellarg($this->getPathname());
187184
$inForm = escapeshellarg($this->getFormat());
188185

189-
$process1 = new Process("openssl rsa -in $in -inform $inForm -passin pass: -check -noout");
186+
$process1 = new Process("openssl rsa -in $in -inform $inForm -passin pass:nopass -check -noout");
190187
$process1->run();
191188

192-
$process2 = new Process("openssl rsa -in $in -inform $inForm -passin pass:nopass -check -noout");
189+
$process2 = new Process("openssl rsa -in $in -inform $inForm -passin pass:anypass -check -noout");
193190
$process2->run();
194191

195192
return !$process1->isSuccessful() && !$process2->isSuccessful();
@@ -245,7 +242,7 @@ public function addPassPhrase($passPhrase)
245242
$inForm = escapeshellarg($this->getFormat());
246243
$pass = escapeshellarg($passPhrase);
247244

248-
$process = new Process("openssl rsa -in $in -inform $inForm -passin pass: -outform $inForm -passout pass:$pass -des3");
245+
$process = new Process("openssl rsa -in $in -inform $inForm -passin pass:nopass -outform $inForm -passout pass:$pass -des3");
249246
$process->mustRun();
250247

251248
@file_put_contents($this->getPathname(), $process->getOutput());

File/PublicKeyFile.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@
2121
namespace DarkWebDesign\PublicKeyCryptographyBundle\File;
2222

2323
use DarkWebDesign\PublicKeyCryptographyBundle\Exception\FormatNotValidException;
24-
use DarkWebDesign\PublicKeyCryptographyBundle\File\CryptoFile;
25-
use DarkWebDesign\PublicKeyCryptographyBundle\File\PemFile;
26-
use DarkWebDesign\PublicKeyCryptographyBundle\File\PrivateKeyFile;
2724
use Symfony\Component\Process\Process;
2825

2926
/**
@@ -53,12 +50,12 @@ protected function validate()
5350
return false;
5451
}
5552

56-
$process = new Process("openssl rsa -in $in -inform $inForm -passin pass: -check -noout");
53+
$process = new Process("openssl rsa -in $in -inform $inForm -passin pass:anypass -check -noout");
5754
$process->run();
5855

59-
$badPasswordRead = false !== strpos($process->getErrorOutput(), ':bad password read:');
56+
$badDecrypt = false !== strpos($process->getErrorOutput(), ':bad decrypt:');
6057

61-
if ($process->isSuccessful() || $badPasswordRead) {
58+
if ($process->isSuccessful() || $badDecrypt) {
6259
return false;
6360
}
6461

Tests/File/KeystoreFileTest.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@
2424
use DarkWebDesign\PublicKeyCryptographyBundle\File\PrivateKeyFile;
2525
use DarkWebDesign\PublicKeyCryptographyBundle\File\PublicKeyFile;
2626
use PHPUnit\Framework\TestCase;
27-
use Symfony\Component\Process\Exception\ProcessFailedException;
2827

2928
class KeystoreFileTest extends TestCase
3029
{
3130
const TEST_PASSPHRASE = 'test';
3231
const TEST_EMPTYPASSPHRASE = '';
33-
const TEST_SUBJECT = '/C=DE/ST=Bavaria/L=Munich/O=MIT-xperts GmbH/OU=TEST CA/CN=testbox.mit-xperts.com/emailAddress=info@mit-xperts.com';
34-
const TEST_ISSUER = '/C=DE/ST=Bavaria/L=Munich/O=MIT-xperts GmbH/OU=HBBTV-DEMO-CA/CN=itv.mit-xperts.com/emailAddress=info@mit-xperts.com';
32+
const TEST_SUBJECT_V1_0_0_BETA1 = '/C=DE/ST=Bavaria/L=Munich/O=MIT-xperts GmbH/OU=TEST CA/CN=testbox.mit-xperts.com/emailAddress=info@mit-xperts.com';
33+
const TEST_SUBJECT_V1_1_0_PRE1 = 'C = DE, ST = Bavaria, L = Munich, O = MIT-xperts GmbH, OU = TEST CA, CN = testbox.mit-xperts.com, emailAddress = info@mit-xperts.com';
34+
const TEST_ISSUER_V1_0_0_BETA1 = '/C=DE/ST=Bavaria/L=Munich/O=MIT-xperts GmbH/OU=HBBTV-DEMO-CA/CN=itv.mit-xperts.com/emailAddress=info@mit-xperts.com';
35+
const TEST_ISSUER_V1_1_0_PRE1 = 'C = DE, ST = Bavaria, L = Munich, O = MIT-xperts GmbH, OU = HBBTV-DEMO-CA, CN = itv.mit-xperts.com, emailAddress = info@mit-xperts.com';
3536
const TEST_NOT_BEFORE = '2012-09-23 17:21:33';
3637
const TEST_NOT_AFTER = '2017-09-22 17:21:33';
3738

@@ -210,7 +211,10 @@ public function testGetSubject($path, $passPhrase)
210211

211212
$subject = $keystoreFile->getSubject($passPhrase);
212213

213-
$this->assertSame(static::TEST_SUBJECT, $subject);
214+
$this->assertThat($subject, $this->logicalOr(
215+
$this->identicalTo(static::TEST_SUBJECT_V1_1_0_PRE1),
216+
$this->identicalTo(static::TEST_SUBJECT_V1_0_0_BETA1)
217+
));
214218
}
215219

216220
/**
@@ -239,7 +243,10 @@ public function testGetIssuer($path, $passPhrase)
239243

240244
$issuer = $keystoreFile->getIssuer($passPhrase);
241245

242-
$this->assertSame(static::TEST_ISSUER, $issuer);
246+
$this->assertThat($issuer, $this->logicalOr(
247+
$this->identicalTo(static::TEST_ISSUER_V1_1_0_PRE1),
248+
$this->identicalTo(static::TEST_ISSUER_V1_0_0_BETA1)
249+
));
243250
}
244251

245252
/**

Tests/File/PemFileTest.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ class PemFileTest extends TestCase
2929
{
3030
const TEST_PASSPHRASE = 'test';
3131
const TEST_EMPTYPASSPHRASE = '';
32-
const TEST_SUBJECT = '/C=DE/ST=Bavaria/L=Munich/O=MIT-xperts GmbH/OU=TEST CA/CN=testbox.mit-xperts.com/emailAddress=info@mit-xperts.com';
33-
const TEST_ISSUER = '/C=DE/ST=Bavaria/L=Munich/O=MIT-xperts GmbH/OU=HBBTV-DEMO-CA/CN=itv.mit-xperts.com/emailAddress=info@mit-xperts.com';
32+
const TEST_SUBJECT_V1_0_0_BETA1 = '/C=DE/ST=Bavaria/L=Munich/O=MIT-xperts GmbH/OU=TEST CA/CN=testbox.mit-xperts.com/emailAddress=info@mit-xperts.com';
33+
const TEST_SUBJECT_V1_1_0_PRE1 = 'C = DE, ST = Bavaria, L = Munich, O = MIT-xperts GmbH, OU = TEST CA, CN = testbox.mit-xperts.com, emailAddress = info@mit-xperts.com';
34+
const TEST_ISSUER_V1_0_0_BETA1 = '/C=DE/ST=Bavaria/L=Munich/O=MIT-xperts GmbH/OU=HBBTV-DEMO-CA/CN=itv.mit-xperts.com/emailAddress=info@mit-xperts.com';
35+
const TEST_ISSUER_V1_1_0_PRE1 = 'C = DE, ST = Bavaria, L = Munich, O = MIT-xperts GmbH, OU = HBBTV-DEMO-CA, CN = itv.mit-xperts.com, emailAddress = info@mit-xperts.com';
3436
const TEST_NOT_BEFORE = '2012-09-23 17:21:33';
3537
const TEST_NOT_AFTER = '2017-09-22 17:21:33';
3638

@@ -271,7 +273,12 @@ public function testGetSubject($path)
271273

272274
$pemFile = new PemFile($this->file);
273275

274-
$this->assertSame(static::TEST_SUBJECT, $pemFile->getSubject());
276+
$subject = $pemFile->getSubject();
277+
278+
$this->assertThat($subject, $this->logicalOr(
279+
$this->identicalTo(static::TEST_SUBJECT_V1_1_0_PRE1),
280+
$this->identicalTo(static::TEST_SUBJECT_V1_0_0_BETA1)
281+
));
275282
}
276283

277284
/**
@@ -299,7 +306,12 @@ public function testGetIssuer($path)
299306

300307
$pemFile = new PemFile($this->file);
301308

302-
$this->assertSame(static::TEST_ISSUER, $pemFile->getIssuer());
309+
$issuer = $pemFile->getIssuer();
310+
311+
$this->assertThat($issuer, $this->logicalOr(
312+
$this->identicalTo(static::TEST_ISSUER_V1_1_0_PRE1),
313+
$this->identicalTo(static::TEST_ISSUER_V1_0_0_BETA1)
314+
));
303315
}
304316

305317
/**

0 commit comments

Comments
 (0)