Skip to content
This repository was archived by the owner on Feb 28, 2024. It is now read-only.

Commit 65b6c98

Browse files
committed
Made KID optional
Fixed private key loading Fixed publicKeyFingerprint generation
1 parent f910656 commit 65b6c98

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

src/Developer/Encryption/JWE/JweHeader.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function __construct($alg, $enc, $kid, $cty)
1414
$this->alg = $alg;
1515
$this->enc = $enc;
1616
$this->kid = $kid;
17-
$this->cty = $cty;
17+
if(!is_null($cty)) $this->cty = $cty;
1818
}
1919

2020
public function toJSON()
@@ -34,11 +34,12 @@ public static function parseJweHeader($encodedHeader)
3434

3535
$headerObj = json_decode(base64_decode($encodedHeader), true);
3636

37+
var_dump($headerObj);
38+
3739
$alg = $headerObj["alg"];
3840
$enc = $headerObj["enc"];
3941
$kid = $headerObj["kid"];
40-
$cty = $headerObj["cty"];
41-
42+
$cty = (isset($headerObj["cty"])) ? $headerObj["cty"] : null;
4243
return new JweHeader($alg, $enc, $kid, $cty);
4344
}
4445

src/Developer/Encryption/JweConfigBuilder.php

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

33
namespace Mastercard\Developer\Encryption;
44

5+
use Mastercard\Developer\Utils\EncodingUtils;
6+
use phpseclib3\Crypt\Hash;
7+
58
class JweConfigBuilder extends EncryptionConfigBuilder {
69

710
/**
@@ -20,9 +23,8 @@ public static function aJweEncryptionConfig() {
2023
*/
2124
public function build() {
2225
$this->checkParameterValues();
23-
$this->computeEncryptionKeyFingerprintWhenNeeded();
26+
$this->computeEncryptionKeyFingerprint($this->encryptionCertificate);
2427
$this->checkJsonPathParameterValues();
25-
2628
$config = new JweConfig();
2729
$config->setEncryptionCertificate($this->encryptionCertificate);
2830
$config->setEncryptionKeyFingerprint($this->encryptionKeyFingerprint);
@@ -52,7 +54,7 @@ public function withDecryptionKey($decryptionKey) {
5254
$this->decryptionKey = $decryptionKey;
5355
return $this;
5456
}
55-
57+
5658
/**
5759
* @param string $jsonPathIn
5860
* @param string $jsonPathOut
@@ -90,4 +92,20 @@ private function checkParameterValues() {
9092
throw new \InvalidArgumentException("You must include at least an encryption certificate or a decryption key");
9193
}
9294
}
95+
96+
/**
97+
* @param mixed $encryptionCertificate
98+
* @throws EncryptionException
99+
*/
100+
private function computeEncryptionKeyFingerprint($encryptionCertificate) {
101+
try {
102+
$publicKeyPem = openssl_pkey_get_details(openssl_pkey_get_public($encryptionCertificate->getBytes()))['key'];
103+
$publicKeyDer = EncodingUtils::pemToDer($publicKeyPem, '-----BEGIN PUBLIC KEY-----', '-----END PUBLIC KEY-----');
104+
$hash = new Hash('sha256');
105+
$this->encryptionKeyFingerprint = EncodingUtils::encodeBytes($hash->hash($publicKeyDer), FieldValueEncoding::HEX);
106+
} catch (\Exception $e) {
107+
throw new EncryptionException('Failed to compute encryption key fingerprint!', $e);
108+
}
109+
}
110+
93111
}

src/Developer/Keys/DecryptionKey.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public static function load($keyPath, $alias = null, $password = null){
3939
$pkcs12_read_results = [];
4040

4141
if(openssl_pkcs12_read(file_get_contents($keyPath), $pkcs12_read_results, $password)) {
42-
openssl_pkey_export($pkcs12_read_results['pkey'], $ret->mContents, $password);
42+
$ret->mContents = $pkcs12_read_results['pkey'];
4343
}else{
4444
$ret->mContents = file_get_contents($keyPath);
4545
}

0 commit comments

Comments
 (0)