Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 16 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
EncryptedData | |
0.00% |
0 / 16 |
|
0.00% |
0 / 3 |
20 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
getPlaintext | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
6 | |||
encryptData | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace MediaWiki\CheckUser; |
4 | |
5 | class EncryptedData { |
6 | |
7 | /** @var string|null The data symmetrically encrypted with a random key */ |
8 | public $encString; |
9 | |
10 | /** @var string|null Symmetric key, encrypted with the public key */ |
11 | public $envKeys; |
12 | |
13 | /** |
14 | * @var string algorithm name, passed into openssl 'method' param. Kept as a variable here in case |
15 | * the class definition needs to change, and we have serialized objects stored. |
16 | */ |
17 | private $algName; |
18 | |
19 | /** |
20 | * @var int Hash of the public key, in case you've used multiple keys, and need to identify the |
21 | * correct private key |
22 | */ |
23 | private $keyHash; |
24 | |
25 | /** |
26 | * Create an EncryptedData object from |
27 | * |
28 | * @param mixed $data Data/object to be encryted |
29 | * @param string $publicKey Public key for encryption |
30 | * @param string $algorithmName |
31 | */ |
32 | public function __construct( $data, $publicKey, $algorithmName = 'rc4' ) { |
33 | $this->keyHash = crc32( $publicKey ); |
34 | $this->algName = $algorithmName; |
35 | $this->encryptData( serialize( $data ), $publicKey ); |
36 | } |
37 | |
38 | /** |
39 | * Decrypt the text in this object |
40 | * |
41 | * @param string $privateKey String with ascii-armored block, |
42 | * or the return of openssl_get_privatekey |
43 | * @return string|false plaintext |
44 | */ |
45 | public function getPlaintext( $privateKey ) { |
46 | $result = \openssl_open( |
47 | $this->encString, |
48 | $plaintextData, |
49 | $this->envKeys, |
50 | $privateKey, |
51 | $this->algName |
52 | ); |
53 | |
54 | if ( !$result ) { |
55 | return false; |
56 | } |
57 | |
58 | return unserialize( $plaintextData ); |
59 | } |
60 | |
61 | /** |
62 | * Encrypt data with a public key |
63 | * |
64 | * @param string $data |
65 | * @param string $publicKey String with ascii-armored block, |
66 | * or the return of openssl_get_publickey |
67 | */ |
68 | private function encryptData( $data, $publicKey ) { |
69 | // @phan-suppress-next-line PhanTypeMismatchArgumentInternal |
70 | \openssl_seal( $data, $encryptedString, $envelopeKeys, [ $publicKey ], $this->algName ); |
71 | $this->encString = $encryptedString; |
72 | // @phan-suppress-next-line PhanTypeArraySuspiciousNullable |
73 | $this->envKeys = $envelopeKeys[0]; |
74 | } |
75 | } |