Skip to content

Commit fc920c5

Browse files
authored
Merge pull request #4 from eislasq/master
Support to load Pkcs8 key as is. #3
2 parents 3c15203 + 01aaff2 commit fc920c5

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

src/PrivateKey.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,19 @@ public function __construct(string $source, string $passPhrase)
2929
throw new UnexpectedValueException('Private key is empty');
3030
}
3131
$pemExtractor = new PemExtractor($source);
32-
$source = $pemExtractor->extractPrivateKey();
33-
if ('' === $source) {
34-
throw new UnexpectedValueException('Private key is not PEM');
32+
$private = $pemExtractor->extractPrivateKey();
33+
if ('' === $private) {
34+
if (boolval(preg_match('/^[a-zA-Z0-9+\/]+={0,2}$/', $source))) {
35+
// if contents are base64 encoded, then decode it
36+
$source = base64_decode($source, true) ?: '';
37+
}
38+
$pem = '-----BEGIN ENCRYPTED PRIVATE KEY-----' . PHP_EOL
39+
. chunk_split(base64_encode($source), 64, PHP_EOL)
40+
. '-----END ENCRYPTED PRIVATE KEY-----';
41+
} else {
42+
$pem = $private;
3543
}
36-
$this->pem = $source;
44+
$this->pem = $pem;
3745
$this->passPhrase = $passPhrase;
3846
$dataArray = $this->callOnPrivateKey(
3947
function ($privateKey): array {

tests/Unit/PrivateKeyConstructTest.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,31 @@ public function testConstructWithEmptyContent(): void
5050

5151
public function testConstructWithInvalidContent(): void
5252
{
53-
$this->expectException(UnexpectedValueException::class);
54-
$this->expectExceptionMessage('Private key is not PEM');
53+
$this->expectException(RuntimeException::class);
54+
$this->expectExceptionMessage('Cannot open private key');
5555
new PrivateKey('invalid content', '');
5656
}
5757

5858
public function testConstructWithInvalidButBase64Content(): void
5959
{
60-
$this->expectException(UnexpectedValueException::class);
61-
$this->expectExceptionMessage('Private key is not PEM');
60+
$this->expectException(RuntimeException::class);
61+
$this->expectExceptionMessage('Cannot open private key');
6262
new PrivateKey('INVALID+CONTENT', '');
6363
}
64+
65+
public function testConstructWithPkcs8Content(): void
66+
{
67+
$content = $this->fileContents('CSD01_AAA010101AAA/private_key.key');
68+
$password = trim($this->fileContents('CSD01_AAA010101AAA/password.txt'));
69+
$privateKey = new PrivateKey($content, $password);
70+
$this->assertGreaterThan(0, $privateKey->numberOfBits());
71+
}
72+
73+
public function testConstructWithPkcs8Base64Content(): void
74+
{
75+
$content = base64_encode($this->fileContents('CSD01_AAA010101AAA/private_key.key'));
76+
$password = trim($this->fileContents('CSD01_AAA010101AAA/password.txt'));
77+
$privateKey = new PrivateKey($content, $password);
78+
$this->assertGreaterThan(0, $privateKey->numberOfBits());
79+
}
6480
}

0 commit comments

Comments
 (0)