|
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
|
|
|
|
|
189
|
|
|
// Value will store the decrypted value |
|
190
|
|
|
if ($value instanceof EncryptedDBField) { |
|
191
|
|
|
$this->value = $value->getValue(); |
|
192
|
|
|
} elseif ($record && isset($record[$this->name . 'Value'])) { |
|
193
|
|
|
if ($record[$this->name . 'Value']) { |
|
194
|
|
|
$encryptedValue = $record[$this->name . 'Value']; |
|
195
|
|
|
try { |
|
196
|
|
|
$this->value = $encryptedField->decryptValue($encryptedValue); |
|
197
|
|
|
} catch (InvalidCiphertextException $ex) { |
|
198
|
|
|
// rotate backend ? |
|
199
|
|
|
// $this->value = $newEncryptedField->decryptValue($encryptedValue); |
|
200
|
|
|
} catch (Exception $ex) { |
|
201
|
|
|
// We cannot decrypt |
|
202
|
|
|
$this->value = $this->nullValue(); |
|
|
|
|
|
|
203
|
|
|
} |
|
204
|
|
|
} else { |
|
205
|
|
|
$this->value = $this->nullValue(); |
|
|
|
|
|
|
206
|
|
|
} |
|
207
|
|
|
} elseif (is_array($value)) { |
|
208
|
|
|
if (array_key_exists('Value', $value)) { |
|
209
|
|
|
$this->value = $value; |
|
210
|
|
|
} |
|
211
|
|
|
} elseif (is_string($value) || !$value) { |
|
212
|
|
|
$this->value = $value; |
|
213
|
|
|
} else { |
|
214
|
|
|
throw new Exception("Unexcepted value of type " . gettype($value)); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
// Forward changes since writeToManipulation are not called |
|
218
|
|
|
$this->setValueField($value, $markChanged); |
|
219
|
|
|
|
|
220
|
|
|
return $this; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* @return string |
|
225
|
|
|
*/ |
|
226
|
|
|
public function Nice($options = array()) |
|
|
|
|
|
|
227
|
|
|
{ |
|
228
|
|
|
return $this->getValue(); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* @return boolean |
|
233
|
|
|
*/ |
|
234
|
|
|
public function exists() |
|
235
|
|
|
{ |
|
236
|
|
|
return strlen($this->value) > 0; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* @return boolean |
|
241
|
|
|
*/ |
|
242
|
|
|
public function isChanged() |
|
243
|
|
|
{ |
|
244
|
|
|
return $this->isChanged; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
public function saveInto($dataObject) |
|
248
|
|
|
{ |
|
249
|
|
|
$encryptedField = $this->getEncryptedField(); |
|
250
|
|
|
|
|
251
|
|
|
if ($this->value) { |
|
252
|
|
|
$dataForStorage = $encryptedField->prepareForStorage($this->value); |
|
253
|
|
|
$encryptedValue = $this->prepValueForDB($dataForStorage[0]); |
|
254
|
|
|
$blindIndexes = $dataForStorage[1]; |
|
255
|
|
|
} else { |
|
256
|
|
|
$encryptedValue = null; |
|
257
|
|
|
$blindIndexes = []; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
// Encrypt value |
|
261
|
|
|
$key = $this->getName() . 'Value'; |
|
262
|
|
|
$dataObject->setField($key, $encryptedValue); |
|
263
|
|
|
|
|
264
|
|
|
// Build blind index |
|
265
|
|
|
$key = $this->getName() . 'BlindIndex'; |
|
266
|
|
|
if (isset($blindIndexes[$key])) { |
|
267
|
|
|
$dataObject->setField($key, $blindIndexes[$key]); |
|
268
|
|
|
} |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* @param string $title Optional. Localized title of the generated instance |
|
273
|
|
|
* @return FormField |
|
|
|
|
|
|
274
|
|
|
*/ |
|
275
|
|
|
public function scaffoldFormField($title = null, $params = null) |
|
276
|
|
|
{ |
|
277
|
|
|
$field = TextField::create($this->name); |
|
278
|
|
|
return $field; |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
/** |
|
282
|
|
|
* Returns the string value |
|
283
|
|
|
*/ |
|
284
|
|
|
public function __toString() |
|
285
|
|
|
{ |
|
286
|
|
|
return (string) $this->getValue(); |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
public function scalarValueOnly() |
|
290
|
|
|
{ |
|
291
|
|
|
return false; |
|
292
|
|
|
} |
|
293
|
|
|
} |
|
294
|
|
|
|