10 defined(
'JPATH_PLATFORM') or die;
25 public $method =
'aes-128-cbc';
31 public $iv =
"1234567890123456";
46 public function createPassphraseFile($passphrase, $passphraseFile, $privateKeyFile, $privateKeyPassphrase)
48 $privateKey = openssl_get_privatekey(file_get_contents($privateKeyFile), $privateKeyPassphrase);
52 throw new RuntimeException(
"Failed to load private key.");
57 if (!openssl_private_encrypt($passphrase, $crypted, $privateKey))
59 throw new RuntimeException(
"Failed to encrypt data using private key.");
62 return file_put_contents($passphraseFile, $crypted);
74 public function deleteValue($path)
79 $nodes = explode(
'.', $path);
87 for ($i = 0, $n = count($nodes) - 1; $i < $n; $i++)
89 if (!isset($node->$nodes[$i]) && ($i != $n))
91 $node->$nodes[$i] =
new stdClass;
93 $node = $node->$nodes[$i];
97 $result = $node->$nodes[$i];
98 unset($node->$nodes[$i]);
116 public function loadKeychain($keychainFile, $passphraseFile, $publicKeyFile)
118 if (!file_exists($keychainFile))
120 throw new RuntimeException(
'Attempting to load non-existent keychain file');
122 $passphrase = $this->getPassphraseFromFile($passphraseFile, $publicKeyFile);
124 $cleartext = openssl_decrypt(file_get_contents($keychainFile), $this->method, $passphrase,
true, $this->iv);
126 if ($cleartext ===
false)
128 throw new RuntimeException(
"Failed to decrypt keychain file");
131 return $this->loadObject(json_decode($cleartext));
146 public function saveKeychain($keychainFile, $passphraseFile, $publicKeyFile)
148 $passphrase = $this->getPassphraseFromFile($passphraseFile, $publicKeyFile);
149 $data = $this->toString(
'JSON');
151 $encrypted = @openssl_encrypt($data, $this->method, $passphrase,
true, $this->iv);
153 if ($encrypted ===
false)
155 throw new RuntimeException(
'Unable to encrypt keychain');
158 return file_put_contents($keychainFile, $encrypted);
172 protected function getPassphraseFromFile($passphraseFile, $publicKeyFile)
174 if (!file_exists($publicKeyFile))
176 throw new RuntimeException(
'Missing public key file');
178 $publicKey = openssl_get_publickey(file_get_contents($publicKeyFile));
182 throw new RuntimeException(
"Failed to load public key.");
185 if (!file_exists($passphraseFile))
187 throw new RuntimeException(
'Missing passphrase file');
191 if (!openssl_public_decrypt(file_get_contents($passphraseFile), $passphrase, $publicKey))
193 throw new RuntimeException(
'Failed to decrypt passphrase file');