Skip to content

Commit 665ea87

Browse files
committed
[smarcet]
* fixed private keys with passwords
1 parent 6c40ed3 commit 665ea87

File tree

15 files changed

+237
-33
lines changed

15 files changed

+237
-33
lines changed

src/jwa/cryptographic_algorithms/digital_signatures/rsa/RSA_Algorithm.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,14 @@ public function sign(PrivateKey $private_key, $message)
4444
if($this->getMinKeyLen() > $private_key->getBitLength())
4545
throw new InvalidKeyLengthAlgorithmException(sprintf('min len %s - cur len %s.',$this->getMinKeyLen(), $private_key->getBitLength()));
4646

47+
if($private_key->hasPassword()){
48+
$this->rsa_impl->setPassword($private_key->getPassword());
49+
}
50+
4751
$res = $this->rsa_impl->loadKey($private_key->getEncoded());
4852

49-
if(!$res) throw new InvalidKeyTypeAlgorithmException;
53+
if(!$res)
54+
throw new InvalidKeyTypeAlgorithmException;
5055

5156
$this->rsa_impl->setHash($this->getHashingAlgorithm());
5257
$this->rsa_impl->setMGFHash($this->getHashingAlgorithm());

src/jwe/impl/_ContentEncryptionKey.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,12 @@ public function getBitLength()
7777
{
7878
return ByteUtil::bitLength(strlen($this->value));
7979
}
80+
81+
/**
82+
* @return string
83+
*/
84+
public function getStrippedEncoded(): string
85+
{
86+
return $this->getEncoded();
87+
}
8088
}

src/jwk/impl/AsymmetricJWK.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ abstract class AsymmetricJWK
4949
/**
5050
* @var X509Certificate[]
5151
*/
52-
protected $x509_certificates_chain = array();
52+
protected $x509_certificates_chain = [];
5353

5454
/**
5555
* @param array $headers
@@ -66,8 +66,7 @@ protected function __construct(array $headers = array())
6666

6767
// json array
6868
foreach($headers[PublicJSONWebKeyParameters::X_509CertificateChain] as $x509_pem){
69-
array_push($this->x509_certificates_chain, X509CertificateFactory::buildFromPEM($x509_pem));
70-
69+
$this->x509_certificates_chain[] = X509CertificateFactory::buildFromPEM($x509_pem);
7170
}
7271

7372
if($this->checkX509CertMismatch()){
@@ -188,7 +187,7 @@ public function getX509Url()
188187
*/
189188
protected function checkX509CertMismatch(){
190189
$x509 = $this->getX509LeafCertificate();
191-
return !is_null($x509) && $x509->getPublicKey() !== $this->public_key->getEncoded();
190+
return !is_null($x509) && $x509->getPublicKey() !== $this->public_key->getStrippedEncoded();
192191
}
193192

194193
/**

src/jwk/impl/OctetSequenceJWK.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ protected function __construct(Key $secret, $headers = array())
4848

4949
$this->key = $secret;
5050

51-
$this->set[OctetSequenceKeysParameters::Key] = new StringOrURI($b64->encode($secret->getEncoded()));
51+
$this->set[OctetSequenceKeysParameters::Key] = new StringOrURI($b64->encode($secret->getStrippedEncoded()));
5252

5353
}
5454

src/security/Key.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
**/
1414

1515

16-
1716
/**
1817
* Interface Key
1918
* @package security
@@ -30,6 +29,11 @@ public function getAlgorithm();
3029
*/
3130
public function getEncoded();
3231

32+
/**
33+
* @return string
34+
*/
35+
public function getStrippedEncoded():string;
36+
3337
/**
3438
* @return string
3539
*/
@@ -39,4 +43,5 @@ public function getFormat();
3943
* @return int
4044
*/
4145
public function getBitLength();
46+
4247
}

src/security/KeyWithPassword.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php namespace security;
2+
/**
3+
* Copyright 2019 OpenStack Foundation
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
**/
14+
15+
/**
16+
* Interface KeyWithPassword
17+
* @package security
18+
*/
19+
interface KeyWithPassword extends Key
20+
{
21+
/**
22+
* @return null|string
23+
*/
24+
public function getPassword():?string;
25+
26+
/**
27+
* @return bool
28+
*/
29+
public function hasPassword():bool;
30+
}

src/security/PrivateKey.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
**/
1414

1515
/**
16-
* Interface PrivateKey
16+
* Interface PrivateKeyPrivateKey
1717
* @package security
1818
*/
1919
interface PrivateKey extends Key {

src/security/SymmetricSharedKey.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,12 @@ public function getSecret()
6868
{
6969
return $this->secret;
7070
}
71+
72+
/**
73+
* @return string
74+
*/
75+
public function getStrippedEncoded(): string
76+
{
77+
return $this->getEncoded();
78+
}
7179
}

src/security/rsa/RSAFacade.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function buildKeyPair($bits){
5656
$this->rsa_imp->setPublicKeyFormat(RSA::PUBLIC_FORMAT_PKCS1);
5757

5858
$list = $this->rsa_imp->createKey($bits);
59-
return new KeyPair( new _RSAPublicKeyPEMFornat($list['publickey']), new _RSAPrivateKeyPEMFornat($list['privatekey']));
59+
return new KeyPair( new _RSAPublicKeyPEMFormat($list['publickey']), new _RSAPrivateKeyPEMFormat($list['privatekey']));
6060
}
6161

6262
/**
@@ -66,7 +66,7 @@ public function buildKeyPair($bits){
6666
*/
6767
public function buildPublicKey(BigInteger $n, BigInteger $e){
6868
$public_key_pem = $this->rsa_imp->_convertPublicKey($n, $e);
69-
return new _RSAPublicKeyPEMFornat($public_key_pem);
69+
return new _RSAPublicKeyPEMFormat($public_key_pem);
7070
}
7171

7272
/**
@@ -78,7 +78,7 @@ public function buildMinimalPrivateKey(\Math_BigInteger $n, \Math_BigInteger $d)
7878
$this->rsa_imp->modulus = $n;
7979
$this->rsa_imp->exponent = $d;
8080
$private_key_pem = $this->rsa_imp->_getPrivatePublicKey();
81-
return new _RSAPrivateKeyPEMFornat($private_key_pem);
81+
return new _RSAPrivateKeyPEMFormat($private_key_pem);
8282
}
8383

8484
/**
@@ -109,7 +109,7 @@ public function buildPrivateKey(BigInteger $n,
109109
array($dp, $dq),
110110
array($qi, $qi)
111111
);
112-
return new _RSAPrivateKeyPEMFornat($private_key_pem);
112+
return new _RSAPrivateKeyPEMFormat($private_key_pem);
113113
}
114114

115115
/**
@@ -119,7 +119,7 @@ public function buildPrivateKey(BigInteger $n,
119119
* @throws RSABadPEMFormat
120120
*/
121121
public function buildPrivateKeyFromPEM($private_key_pem, $password = null){
122-
return new _RSAPrivateKeyPEMFornat($private_key_pem, $password);
122+
return new _RSAPrivateKeyPEMFormat($private_key_pem, $password);
123123
}
124124

125125
/**
@@ -128,7 +128,7 @@ public function buildPrivateKeyFromPEM($private_key_pem, $password = null){
128128
* @throws RSABadPEMFormat
129129
*/
130130
public function buildPublicKeyFromPEM($public_key_pem){
131-
return new _RSAPublicKeyPEMFornat($public_key_pem);
131+
return new _RSAPublicKeyPEMFormat($public_key_pem);
132132
}
133133

134134
}

src/security/rsa/RSAPrivateKey.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@
1111
* See the License for the specific language governing permissions and
1212
* limitations under the License.
1313
**/
14+
use security\KeyWithPassword;
1415
use security\PrivateKey;
1516
use phpseclib\Math\BigInteger;
1617
/**
1718
* Interface RSAPrivateKey
1819
* @package security\rsa
1920
*/
20-
interface RSAPrivateKey extends RSAPublicKey, PrivateKey {
21+
interface RSAPrivateKey extends RSAPublicKey, PrivateKey, KeyWithPassword {
2122

2223
/**
2324
* The "d" (private exponent)

0 commit comments

Comments
 (0)