1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LeKoala\Encrypt; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use SilverStripe\ORM\DataObject; |
7
|
|
|
use SilverStripe\Forms\TextField; |
8
|
|
|
use ParagonIE\CipherSweet\BlindIndex; |
9
|
|
|
use SilverStripe\ORM\Queries\SQLSelect; |
10
|
|
|
use ParagonIE\CipherSweet\EncryptedField; |
11
|
|
|
use SilverStripe\ORM\FieldType\DBComposite; |
12
|
|
|
use ParagonIE\CipherSweet\Exception\InvalidCiphertextException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Value will be set on parent record through built in getField |
16
|
|
|
* mechanisms for composite fields |
17
|
|
|
*/ |
18
|
|
|
class EncryptedDBField extends DBComposite |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @param array |
22
|
|
|
*/ |
23
|
|
|
private static $composite_db = array( |
|
|
|
|
24
|
|
|
"Value" => "Varchar(191)", |
25
|
|
|
"BlindIndex" => 'Varchar(32)', |
26
|
|
|
); |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return string |
30
|
|
|
*/ |
31
|
|
|
public function getValueField() |
32
|
|
|
{ |
33
|
|
|
return $this->getField('Value'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return $this |
38
|
|
|
*/ |
39
|
|
|
public function setValueField($value, $markChanged = true) |
40
|
|
|
{ |
41
|
|
|
return $this->setField('Value', $value, $markChanged); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
public function getBlindIndexField() |
48
|
|
|
{ |
49
|
|
|
return $this->getField('BlindIndex'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return $this |
54
|
|
|
*/ |
55
|
|
|
public function setBlindIndexField($value, $markChanged = true) |
56
|
|
|
{ |
57
|
|
|
return $this->setField('BlindIndex', $value, $markChanged); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param CipherSweet $engine |
|
|
|
|
62
|
|
|
* @return EncryptedField |
63
|
|
|
*/ |
64
|
|
|
public function getEncryptedField($engine = null) |
65
|
|
|
{ |
66
|
|
|
if ($engine === null) { |
67
|
|
|
$engine = EncryptHelper::getCipherSweet(); |
68
|
|
|
} |
69
|
|
|
$encryptedField = (new EncryptedField($engine, $this->tableName, $this->name)) |
70
|
|
|
->addBlindIndex(new BlindIndex($this->name . "BlindIndex", [], 32)); |
71
|
|
|
return $encryptedField; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* This is not called anymore, we rely on saveInto for now |
76
|
|
|
* @link https://github.com/silverstripe/silverstripe-framework/issues/8800 |
77
|
|
|
* @param array $manipulation |
78
|
|
|
* @return void |
79
|
|
|
*/ |
80
|
|
|
public function writeToManipulation(&$manipulation) |
81
|
|
|
{ |
82
|
|
|
$encryptedField = $this->getEncryptedField(); |
83
|
|
|
|
84
|
|
|
if ($this->value) { |
85
|
|
|
$dataForStorage = $encryptedField->prepareForStorage($this->value); |
86
|
|
|
$encryptedValue = $this->prepValueForDB($dataForStorage[0]); |
87
|
|
|
$blindIndexes = $dataForStorage[1]; |
88
|
|
|
} else { |
89
|
|
|
$encryptedValue = null; |
90
|
|
|
$blindIndexes = []; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
$manipulation['fields'][$this->name . 'Value'] = $encryptedValue; |
95
|
|
|
$manipulation['fields'][$this->name . 'BlindIndex'] = $blindIndexes[$this->name . "BlindIndex"] ?? null; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param SQLSelect $query |
100
|
|
|
*/ |
101
|
|
|
public function addToQuery(&$query) |
102
|
|
|
{ |
103
|
|
|
parent::addToQuery($query); |
104
|
|
|
$query->selectField(sprintf('"%sValue"', $this->name)); |
105
|
|
|
$query->selectField(sprintf('"%sBlindIndex"', $this->name)); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Return the blind index value to search in the database |
110
|
|
|
* |
111
|
|
|
* @param string $val The unencrypted value |
112
|
|
|
* @param string $index The blind index. Defaults to full index |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
public function getSearchValue($val, $index = 'BlindIndex') |
116
|
|
|
{ |
117
|
|
|
if (!$this->tableName && $this->record) { |
118
|
|
|
$this->tableName = DataObject::getSchema()->tableName(get_class($this->record)); |
|
|
|
|
119
|
|
|
} |
120
|
|
|
if (!$this->tableName) { |
121
|
|
|
throw new Exception("Table name not set for search value. Please set a dataobject."); |
122
|
|
|
} |
123
|
|
|
if (!$this->name) { |
124
|
|
|
throw new Exception("Name not set for search value"); |
125
|
|
|
} |
126
|
|
|
$field = $this->getEncryptedField(); |
127
|
|
|
$index = $field->getBlindIndex($val, $this->name . $index); |
128
|
|
|
return $index; |
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Return a ready to use array params for a where clause |
133
|
|
|
* |
134
|
|
|
* @param string $val The unencrypted value |
135
|
|
|
* @param string $index The blind index. Defaults to full index |
136
|
|
|
* @return array |
137
|
|
|
*/ |
138
|
|
|
public function getSearchParams($val, $index = null) |
139
|
|
|
{ |
140
|
|
|
if (!$index) { |
141
|
|
|
$index = 'BlindIndex'; |
142
|
|
|
} |
143
|
|
|
$searchValue = $this->getSearchValue($val, $index); |
144
|
|
|
$blindIndexField = $this->name . $index; |
145
|
|
|
return array($blindIndexField . ' = ?' => $searchValue); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param string $val The unencrypted value |
150
|
|
|
* @param string $index The blind index. Defaults to full index |
151
|
|
|
* @return DataObject |
152
|
|
|
*/ |
153
|
|
|
public function fetchRecord($val, $index = null) |
154
|
|
|
{ |
155
|
|
|
if (!$this->record) { |
156
|
|
|
throw new Exception("No record set for this field"); |
157
|
|
|
} |
158
|
|
|
$class = get_class($this->record); |
|
|
|
|
159
|
|
|
return $class::get()->where($this->getSearchParams($val, $index))->first(); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function setValue($value, $record = null, $markChanged = true) |
163
|
|
|
{ |
164
|
|
|
if ($markChanged) { |
165
|
|
|
$this->isChanged = true; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
// When given a dataobject, bind this field to that |
169
|
|
|
if ($record instanceof DataObject) { |
170
|
|
|
$this->bindTo($record); |
171
|
|
|
$record = null; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
// Convert an object to an array |
175
|
|
|
if ($record && $record instanceof DataObject) { |
176
|
|
|
$record = $record->getQueriedDatabaseFields(); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
// Set the table name if it was not set earlier |
180
|
|
|
if (!$this->tableName && $record) { |
181
|
|
|
$this->tableName = DataObject::getSchema()->tableName(get_class($record)); |
182
|
|
|
if (!$this->tableName) { |
183
|
|
|
throw new Exception("Could not get table name from record from " . gettype($record)); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
$encryptedField = $this->getEncryptedField(); |
188
|
|
|
// Value will store the decrypted value |
189
|
|
|
if ($value instanceof EncryptedDBField) { |
190
|
|
|
$this->value = $value->getValue(); |
191
|
|
|
} elseif ($record && isset($record[$this->name . 'Value'])) { |
192
|
|
|
// In that case, the value come from the database and might be encrypted |
193
|
|
|
if ($record[$this->name . 'Value']) { |
194
|
|
|
$encryptedValue = $record[$this->name . 'Value']; |
195
|
|
|
try { |
196
|
|
|
if (EncryptHelper::isEncrypted($encryptedValue)) { |
197
|
|
|
$this->value = $encryptedField->decryptValue($encryptedValue); |
198
|
|
|
} else { |
199
|
|
|
// Value wasn't crypted in the db |
200
|
|
|
$this->value = $encryptedValue; |
201
|
|
|
} |
202
|
|
|
} catch (InvalidCiphertextException $ex) { |
203
|
|
|
// rotate backend ? |
204
|
|
|
// $this->value = $newEncryptedField->decryptValue($encryptedValue); |
205
|
|
|
} catch (Exception $ex) { |
206
|
|
|
// We cannot decrypt |
207
|
|
|
$this->value = $this->nullValue(); |
|
|
|
|
208
|
|
|
} |
209
|
|
|
} else { |
210
|
|
|
$this->value = $this->nullValue(); |
|
|
|
|
211
|
|
|
} |
212
|
|
|
} elseif (is_array($value)) { |
213
|
|
|
if (array_key_exists('Value', $value)) { |
214
|
|
|
$this->value = $value; |
215
|
|
|
} |
216
|
|
|
} elseif (is_string($value) || !$value) { |
217
|
|
|
$this->value = $value; |
218
|
|
|
} else { |
219
|
|
|
throw new Exception("Unexcepted value of type " . gettype($value)); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
// Forward changes since writeToManipulation are not called |
223
|
|
|
// $this->setValueField($value, $markChanged); |
224
|
|
|
|
225
|
|
|
return $this; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @return string |
230
|
|
|
*/ |
231
|
|
|
public function Nice($options = array()) |
|
|
|
|
232
|
|
|
{ |
233
|
|
|
return $this->getValue(); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @return boolean |
238
|
|
|
*/ |
239
|
|
|
public function exists() |
240
|
|
|
{ |
241
|
|
|
return strlen($this->value) > 0; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* This is called by getChangedFields() to check if a field is changed |
246
|
|
|
* |
247
|
|
|
* @return boolean |
248
|
|
|
*/ |
249
|
|
|
public function isChanged() |
250
|
|
|
{ |
251
|
|
|
return $this->isChanged; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* If we pass a DBField to the setField method, it will |
256
|
|
|
* trigger this method |
257
|
|
|
* |
258
|
|
|
* We save encrypted value on sub fields. They will be collected |
259
|
|
|
* by write() operation by prepareManipulationTable |
260
|
|
|
* |
261
|
|
|
* Currently prepareManipulationTable ignores composite fields |
262
|
|
|
* so we rely on the sub field mechanisms |
263
|
|
|
* |
264
|
|
|
* @param DataObject $dataObject |
265
|
|
|
* @return void |
266
|
|
|
*/ |
267
|
|
|
public function saveInto($dataObject) |
268
|
|
|
{ |
269
|
|
|
$encryptedField = $this->getEncryptedField(); |
270
|
|
|
|
271
|
|
|
if ($this->value) { |
272
|
|
|
$dataForStorage = $encryptedField->prepareForStorage($this->value); |
273
|
|
|
$encryptedValue = $this->prepValueForDB($dataForStorage[0]); |
274
|
|
|
$blindIndexes = $dataForStorage[1]; |
275
|
|
|
} else { |
276
|
|
|
$encryptedValue = null; |
277
|
|
|
$blindIndexes = []; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
// This cause infinite loops |
281
|
|
|
// $dataObject->setField($this->getName(), $this->value); |
282
|
|
|
|
283
|
|
|
// Encrypt value |
284
|
|
|
$key = $this->getName() . 'Value'; |
285
|
|
|
$dataObject->setField($key, $encryptedValue); |
286
|
|
|
|
287
|
|
|
// Build blind index |
288
|
|
|
$key = $this->getName() . 'BlindIndex'; |
289
|
|
|
if (isset($blindIndexes[$key])) { |
290
|
|
|
$dataObject->setField($key, $blindIndexes[$key]); |
291
|
|
|
} |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* @param string $title Optional. Localized title of the generated instance |
296
|
|
|
* @return FormField |
|
|
|
|
297
|
|
|
*/ |
298
|
|
|
public function scaffoldFormField($title = null, $params = null) |
299
|
|
|
{ |
300
|
|
|
$field = TextField::create($this->name); |
301
|
|
|
return $field; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Returns the string value |
306
|
|
|
*/ |
307
|
|
|
public function __toString() |
308
|
|
|
{ |
309
|
|
|
return (string) $this->getValue(); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
public function scalarValueOnly() |
313
|
|
|
{ |
314
|
|
|
return false; |
315
|
|
|
} |
316
|
|
|
} |
317
|
|
|
|