Spomky-Labs /
jose
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | /* |
||
| 4 | * The MIT License (MIT) |
||
| 5 | * |
||
| 6 | * Copyright (c) 2014-2016 Spomky-Labs |
||
| 7 | * |
||
| 8 | * This software may be modified and distributed under the terms |
||
| 9 | * of the MIT license. See the LICENSE file for details. |
||
| 10 | */ |
||
| 11 | |||
| 12 | namespace Jose\Algorithm\KeyEncryption; |
||
| 13 | |||
| 14 | use Assert\Assertion; |
||
| 15 | use Jose\KeyConverter\RSAKey; |
||
| 16 | use Jose\Object\JWKInterface; |
||
| 17 | use Jose\Util\RSA as JoseRSA; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Class RSA. |
||
| 21 | */ |
||
| 22 | abstract class RSA implements KeyEncryptionInterface |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * Optimal Asymmetric Encryption Padding (OAEP). |
||
| 26 | */ |
||
| 27 | const ENCRYPTION_OAEP = 1; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Use PKCS#1 padding. |
||
| 31 | */ |
||
| 32 | const ENCRYPTION_PKCS1 = 2; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * {@inheritdoc} |
||
| 36 | */ |
||
| 37 | public function encryptKey(JWKInterface $key, $cek, array $complete_headers, array &$additional_headers) |
||
| 38 | { |
||
| 39 | $this->checkKey($key); |
||
| 40 | |||
| 41 | $pem = RSAKey::toPublic(new RSAKey($key))->toPEM(); |
||
| 42 | |||
| 43 | if (self::ENCRYPTION_OAEP === $this->getEncryptionMode()) { |
||
| 44 | $rsa = $this->getRsaObject(); |
||
| 45 | $rsa->loadKey($pem, JoseRSA::PRIVATE_FORMAT_PKCS1); |
||
| 46 | |||
| 47 | $encrypted = $rsa->encrypt($cek); |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
| 48 | Assertion::string($encrypted, 'Unable to encrypt the data.'); |
||
| 49 | |||
| 50 | return $encrypted; |
||
| 51 | } else { |
||
| 52 | $res = openssl_public_encrypt($cek, $encrypted, $pem, OPENSSL_PKCS1_PADDING | OPENSSL_RAW_DATA); |
||
| 53 | Assertion::true($res, 'Unable to encrypt the data.'); |
||
| 54 | |||
| 55 | return $encrypted; |
||
| 56 | } |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * {@inheritdoc} |
||
| 61 | */ |
||
| 62 | public function decryptKey(JWKInterface $key, $encrypted_key, array $header) |
||
| 63 | { |
||
| 64 | $this->checkKey($key); |
||
| 65 | Assertion::true($key->has('d'), 'The key is not a private key'); |
||
| 66 | |||
| 67 | $pem = (new RSAKey($key))->toPEM(); |
||
| 68 | if (self::ENCRYPTION_OAEP === $this->getEncryptionMode()) { |
||
| 69 | $rsa = $this->getRsaObject(); |
||
| 70 | $rsa->loadKey($pem, JoseRSA::PRIVATE_FORMAT_PKCS1); |
||
| 71 | |||
| 72 | $decrypted = $rsa->decrypt($encrypted_key); |
||
| 73 | Assertion::string($decrypted, 'Unable to decrypt the data11.'); |
||
| 74 | |||
| 75 | return $decrypted; |
||
| 76 | } else { |
||
| 77 | $res = openssl_private_decrypt($encrypted_key, $decrypted, $pem, OPENSSL_PKCS1_PADDING | OPENSSL_RAW_DATA); |
||
| 78 | Assertion::true($res, 'Unable to decrypt the data22.'); |
||
| 79 | |||
| 80 | return $decrypted; |
||
| 81 | } |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * {@inheritdoc} |
||
| 86 | */ |
||
| 87 | public function getKeyManagementMode() |
||
| 88 | { |
||
| 89 | return self::MODE_ENCRYPT; |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @return \phpseclib\Crypt\RSA |
||
| 94 | */ |
||
| 95 | private function getRsaObject() |
||
| 96 | { |
||
| 97 | $rsa = new JoseRSA(); |
||
| 98 | $rsa->setEncryptionMode(JoseRSA::ENCRYPTION_OAEP); |
||
| 99 | $rsa->setHash($this->getHashAlgorithm()); |
||
| 100 | $rsa->setMGFHash($this->getHashAlgorithm()); |
||
| 101 | |||
| 102 | return $rsa; |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @param JWKInterface $key |
||
| 107 | */ |
||
| 108 | protected function checkKey(JWKInterface $key) |
||
| 109 | { |
||
| 110 | Assertion::eq($key->get('kty'), 'RSA', 'Wrong key type.'); |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @return int |
||
| 115 | */ |
||
| 116 | abstract protected function getEncryptionMode(); |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @return string |
||
| 120 | */ |
||
| 121 | abstract protected function getHashAlgorithm(); |
||
| 122 | } |
||
| 123 |