Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
keychain.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Platform
4  * @subpackage Keychain
5  *
6  * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved
7  * @license GNU General Public License version 2 or later; see LICENSE
8  */
9 
10 defined('JPATH_PLATFORM') or die;
11 
12 /**
13  * Keychain Class
14  *
15  * @package Joomla.Platform
16  * @subpackage Keychain
17  * @since 12.3
18  */
19 class JKeychain extends JRegistry
20 {
21  /**
22  * @var string Method to use for encryption.
23  * @since 12.3
24  */
25  public $method = 'aes-128-cbc';
26 
27  /**
28  * @var string Initialisation vector for encryption method.
29  * @since 12.3
30  */
31  public $iv = "1234567890123456";
32 
33  /**
34  * Create a passphrase file
35  *
36  * @param string $passphrase The passphrase to store in the passphrase file.
37  * @param string $passphraseFile Path to the passphrase file to create.
38  * @param string $privateKeyFile Path to the private key file to encrypt the passphrase file.
39  * @param string $privateKeyPassphrase The passphrase for the private key.
40  *
41  * @return boolean Result of writing the passphrase file to disk.
42  *
43  * @since 12.3
44  * @throws RuntimeException
45  */
46  public function createPassphraseFile($passphrase, $passphraseFile, $privateKeyFile, $privateKeyPassphrase)
47  {
48  $privateKey = openssl_get_privatekey(file_get_contents($privateKeyFile), $privateKeyPassphrase);
49 
50  if (!$privateKey)
51  {
52  throw new RuntimeException("Failed to load private key.");
53  }
54 
55  $crypted = '';
56 
57  if (!openssl_private_encrypt($passphrase, $crypted, $privateKey))
58  {
59  throw new RuntimeException("Failed to encrypt data using private key.");
60  }
61 
62  return file_put_contents($passphraseFile, $crypted);
63  }
64 
65  /**
66  * Delete a registry value (very simple method)
67  *
68  * @param string $path Registry Path (e.g. joomla.content.showauthor)
69  *
70  * @return mixed Value of old value or boolean false if operation failed
71  *
72  * @since 12.3
73  */
74  public function deleteValue($path)
75  {
76  $result = null;
77 
78  // Explode the registry path into an array
79  $nodes = explode('.', $path);
80 
81  if ($nodes)
82  {
83  // Initialize the current node to be the registry root.
84  $node = $this->data;
85 
86  // Traverse the registry to find the correct node for the result.
87  for ($i = 0, $n = count($nodes) - 1; $i < $n; $i++)
88  {
89  if (!isset($node->$nodes[$i]) && ($i != $n))
90  {
91  $node->$nodes[$i] = new stdClass;
92  }
93  $node = $node->$nodes[$i];
94  }
95 
96  // Get the old value if exists so we can return it
97  $result = $node->$nodes[$i];
98  unset($node->$nodes[$i]);
99  }
100 
101  return $result;
102  }
103 
104  /**
105  * Load a keychain file into this object.
106  *
107  * @param string $keychainFile Path to the keychain file.
108  * @param string $passphraseFile The path to the passphrase file to decript the keychain.
109  * @param string $publicKeyFile The file containing the public key to decrypt the passphrase file.
110  *
111  * @return boolean Result of loading the object.
112  *
113  * @since 12.3
114  * @throws RuntimeException
115  */
116  public function loadKeychain($keychainFile, $passphraseFile, $publicKeyFile)
117  {
118  if (!file_exists($keychainFile))
119  {
120  throw new RuntimeException('Attempting to load non-existent keychain file');
121  }
122  $passphrase = $this->getPassphraseFromFile($passphraseFile, $publicKeyFile);
123 
124  $cleartext = openssl_decrypt(file_get_contents($keychainFile), $this->method, $passphrase, true, $this->iv);
125 
126  if ($cleartext === false)
127  {
128  throw new RuntimeException("Failed to decrypt keychain file");
129  }
130 
131  return $this->loadObject(json_decode($cleartext));
132  }
133 
134  /**
135  * Save this keychain to a file.
136  *
137  * @param string $keychainFile The path to the keychain file.
138  * @param string $passphraseFile The path to the passphrase file to encrypt the keychain.
139  * @param string $publicKeyFile The file containing the public key to decrypt the passphrase file.
140  *
141  * @return boolean Result of storing the file.
142  *
143  * @since 12.3
144  * @throws RuntimeException
145  */
146  public function saveKeychain($keychainFile, $passphraseFile, $publicKeyFile)
147  {
148  $passphrase = $this->getPassphraseFromFile($passphraseFile, $publicKeyFile);
149  $data = $this->toString('JSON');
150 
151  $encrypted = @openssl_encrypt($data, $this->method, $passphrase, true, $this->iv);
152 
153  if ($encrypted === false)
154  {
155  throw new RuntimeException('Unable to encrypt keychain');
156  }
157 
158  return file_put_contents($keychainFile, $encrypted);
159  }
160 
161  /**
162  * Get the passphrase for this keychain
163  *
164  * @param string $passphraseFile The file containing the passphrase to encrypt and decrypt.
165  * @param string $publicKeyFile The file containing the public key to decrypt the passphrase file.
166  *
167  * @return string The passphrase in from passphraseFile
168  *
169  * @since 12.3
170  * @throws RuntimeException
171  */
172  protected function getPassphraseFromFile($passphraseFile, $publicKeyFile)
173  {
174  if (!file_exists($publicKeyFile))
175  {
176  throw new RuntimeException('Missing public key file');
177  }
178  $publicKey = openssl_get_publickey(file_get_contents($publicKeyFile));
179 
180  if (!$publicKey)
181  {
182  throw new RuntimeException("Failed to load public key.");
183  }
184 
185  if (!file_exists($passphraseFile))
186  {
187  throw new RuntimeException('Missing passphrase file');
188  }
189  $passphrase = '';
190 
191  if (!openssl_public_decrypt(file_get_contents($passphraseFile), $passphrase, $publicKey))
192  {
193  throw new RuntimeException('Failed to decrypt passphrase file');
194  }
195  return $passphrase;
196  }
197 }