This repository has been archived by the owner on Sep 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Encrypt.php
224 lines (190 loc) · 7.94 KB
/
Encrypt.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
/**
* This plugin uses asymmetric encryption to encrypt responses after completion.
* Even if the limesurvey server is compromised an attack will have no way of decrypting the data.
*
* This work was commissioned by Dr Mark Brown
* @author Sam Mousa MSc <sam@befound.nl>
* @license http://opensource.org/licenses/MIT MIT
* @link http://www.markgbrown.com/
*
*/
class Encrypt extends PluginBase
{
static protected $description = 'Encrypt: Encrypt completed surveys.';
static protected $name = 'Encrypt';
/**
* Plugin settings
*/
protected $settings = array(
'publicKey' => array(
'type' => 'text',
'label' => 'Public key'
),
);
protected $storage = 'DbStorage';
public function __construct(PluginManager $manager, $id)
{
parent::__construct($manager, $id);
// Provides survey specific settings.
$this->subscribe('beforeSurveySettings');
// Saves survey specific settings.
$this->subscribe('newSurveySettings');
// Encrypt data on survey completion.
$this->subscribe('afterSurveyComplete');
// Create table for encrypted data.
$this->subscribe('beforeActivate');
$this->subscribe('newDirectRequest');
}
protected function getData($surveyId)
{
$table = $this->api->getTable($this, 'responses');
$responses = $table->findAllByAttributes(array(
'survey_id' => $surveyId
));
$data = '';
return implode('.', array_map(function($response) { return base64_encode($response->response); }, $responses));
}
public function actionExportScript($surveyId)
{
$data = $this->getData($surveyId);
$script = file(__DIR__ . '/decrypt.php', FILE_IGNORE_NEW_LINES);
// Remove first line.
array_shift($script);
$script[] = 'decryptResponses($privateKey, \'' . $data . '\', $handle);';
$lines = [];
$lines[] = '@echo off';
$lines['call'] = "php -r \"\$f = fopen('%~0', 'rb'); fseek(\$f, ----------); \$code = stream_get_contents(\$f); eval(\$code); \"";
$lines[] = 'exit';
$offset = strlen(implode("\n", $lines)) - 1;
$lines['call'] = strtr($lines['call'], ['----------' => $offset]);
// Add data.
$lines = array_merge($lines, $script);
$lines[] = 'fgets($stdin);';
$this->event->get('request')->sendFile('encrypted.cmd', implode("\n", $lines), 'text/plain', true);
}
public function actionExport($surveyId)
{
$this->actionExportScript($surveyId);
return;
$data = $this->getData($surveyId);
$this->event->get('request')->sendFile('encrypted.dat', $data, 'text/plain', true);
}
/**
* This event is fired after the survey has been completed.
* @param PluginEvent $event
*/
public function afterSurveyComplete()
{
$event = $this->getEvent();
if ($event->get('responseId') == null)
{
return;
}
// Get the response information.
$response = $this->api->getResponse($event->get('surveyId'), $event->get('responseId'));
$publicKey = $this->get('publicKey');
$data = json_encode($response);
$encryptedResponse = $this->pluginManager->getAPI()->newModel($this, 'responses');
$encryptedResponse->response = $this->encrypt($publicKey, $data);
$encryptedResponse->survey_id = $event->get('surveyId');
if ($encryptedResponse->save())
{
$this->pluginManager->getAPI()->removeResponse($event->get('surveyId'), $event->get('responseId'));
$this->event->setContent($this, 'Response has been encrypted.');
return;
}
$this->event->setContent($this, openssl_error_string());
}
public function beforeActivate()
{
$event = $this->event;
if (!$this->api->tableExists($this, 'responses'))
{
$this->api->createTable($this, 'responses', array(
'survey_id' => 'int',
'response' => 'binary',
));
}
return true;
}
public function beforeSurveySettings()
{
$event = $this->event;
$table = $this->api->getTable($this, 'responses');
$count = $table->countByAttributes(array('survey_id' => $event->get('survey')));
$settings = array(
'name' => get_class($this),
'settings' => array(
'enabled' => array(
'type' => 'boolean',
'label' => 'Encrypt responses for this survey: ',
'current' => $this->get('enabled', 'Survey', $event->get('survey'), true)
),
'count' => array(
'label' => 'Number of encrypted responses:',
'type' => 'string',
'readOnly' => true,
'current' => $count
)
)
);
if ($count > 0)
{
$settings['settings']['export'] =array(
'label' => 'Export data',
'type' => 'link',
'link' => $this->api->createUrl('plugins/direct', array('plugin' => 'Encrypt', 'function' => 'export', 'sid' => $event->get('survey')))
);
}
$event->set("surveysettings.{$this->id}", $settings);
}
protected function encrypt($publicKey, $data)
{
// Generate a random password for symmetric encryption.
$symmetricKey = openssl_random_pseudo_bytes(50);
$encryptedKey = '';
if (openssl_public_encrypt($symmetricKey, $encryptedKey, $publicKey))
{
// Use the encrypted key as basis for the IV.
$iv = substr($encryptedKey, 0, mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_CBC));
$encryptedData = mcrypt_encrypt(MCRYPT_BLOWFISH, $symmetricKey, $data, MCRYPT_MODE_CBC, $iv);
// Key has constant length so no separator is necessary.
return $encryptedKey . $encryptedData;
}
/**
* @todo Proper error handling.
*
*/
}
public function newDirectRequest()
{
$event = $this->event;
if ($event->get('target') == $this->getName() && $event->get('function') == 'export')
{
if (!$this->api->checkAccess('administrator'))
{
throw new CHttpException(403, 'This action requires you to be logged in as super administrator.');
}
elseif ($event->get('request')->getParam('sid') == null)
{
throw new CHttpException(400, 'Survey id missing; pass it as sid variable.');
}
else
{
$this->actionExport($event->get('request')->getParam('sid'));
}
}
}
public function newSurveySettings()
{
foreach ($this->event->get('settings') as $name => $value)
{
if ($name != 'count')
{
$this->set($name, $value, 'Survey', $this->event->get('survey'));
}
}
}
}
?>