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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
|
/*
* Copyright (C) 2016 Raptor Engineering, LLC
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#if defined(HAVE_CONFIG_H)
#include "config.h"
#endif
#include <stdbool.h>
#include <stdlib.h>
#include <assert.h>
#include <dirent.h>
#include <string.h>
#include <fcntl.h>
#include <locale.h>
#include <sys/types.h>
#include <log/log.h>
#include <file/file.h>
#include <talloc/talloc.h>
#include <url/url.h>
#include <util/util.h>
#include <i18n/i18n.h>
#include <gpgme.h>
#include "security.h"
/*
* If --with-signed-boot is enabled lib/security provides the ability to handle
* gpg-signed and/or encrypted boot sources (kernel, initrd, etc).
* This can be used to enable a form of secure boot, but it is important to
* recognise that it depends on the security of the entire system, for example
* a full trusted-boot implementation. Petitboot can not and will not be able
* to guarantee secure boot by itself.
*/
int decrypt_file(const char *filename,
FILE *authorized_signatures_handle, const char *keyring_path)
{
int result = 0;
int valid = 0;
size_t bytes_read = 0;
unsigned char buffer[8192];
if (filename == NULL)
return -1;
gpgme_signature_t verification_signatures;
gpgme_verify_result_t verification_result;
gpgme_data_t ciphertext_data;
gpgme_data_t plaintext_data;
gpgme_engine_info_t enginfo;
gpgme_ctx_t gpg_context;
gpgme_error_t err;
/* Initialize gpgme */
setlocale (LC_ALL, "");
gpgme_check_version(NULL);
gpgme_set_locale(NULL, LC_CTYPE, setlocale (LC_CTYPE, NULL));
err = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
if (err != GPG_ERR_NO_ERROR) {
pb_log_fn("OpenPGP support not available\n");
return -1;
}
err = gpgme_get_engine_info(&enginfo);
if (err != GPG_ERR_NO_ERROR) {
pb_log_fn("GPG engine failed to initialize\n");
return -1;
}
err = gpgme_new(&gpg_context);
if (err != GPG_ERR_NO_ERROR) {
pb_log_fn("GPG context could not be created\n");
return -1;
}
err = gpgme_set_protocol(gpg_context, GPGME_PROTOCOL_OpenPGP);
if (err != GPG_ERR_NO_ERROR) {
pb_log_fn("GPG protocol could not be set\n");
return -1;
}
if (keyring_path)
err = gpgme_ctx_set_engine_info (gpg_context,
GPGME_PROTOCOL_OpenPGP,
enginfo->file_name, keyring_path);
else
err = gpgme_ctx_set_engine_info (gpg_context,
GPGME_PROTOCOL_OpenPGP,
enginfo->file_name, enginfo->home_dir);
if (err != GPG_ERR_NO_ERROR) {
pb_log_fn("Could not set GPG engine information\n");
return -1;
}
err = gpgme_data_new(&plaintext_data);
if (err != GPG_ERR_NO_ERROR) {
pb_log("%s: Could not create GPG plaintext data buffer\n",
__func__);
return -1;
}
err = gpgme_data_new_from_file(&ciphertext_data, filename, 1);
if (err != GPG_ERR_NO_ERROR) {
pb_log("%s: Could not create GPG ciphertext data buffer"
" from file '%s'\n", __func__, filename);
return -1;
}
/* Decrypt and verify file */
err = gpgme_op_decrypt_verify(gpg_context, ciphertext_data,
plaintext_data);
if (err != GPG_ERR_NO_ERROR) {
pb_log_fn("Could not decrypt file\n");
return -1;
}
verification_result = gpgme_op_verify_result(gpg_context);
verification_signatures = verification_result->signatures;
while (verification_signatures) {
if (verification_signatures->status == GPG_ERR_NO_ERROR) {
pb_log("%s: Good signature for key ID '%s' ('%s')\n",
__func__,
verification_signatures->fpr, filename);
/* Verify fingerprint is present in authorized
* signatures file
*/
char *auth_sig_line = NULL;
size_t auth_sig_len = 0;
ssize_t auth_sig_read;
rewind(authorized_signatures_handle);
while ((auth_sig_read = getline(&auth_sig_line,
&auth_sig_len,
authorized_signatures_handle)) != -1) {
auth_sig_len = strlen(auth_sig_line);
while ((auth_sig_line[auth_sig_len-1] == '\n')
|| (auth_sig_line[auth_sig_len-1] == '\r'))
auth_sig_len--;
auth_sig_line[auth_sig_len] = 0;
if (strcmp(auth_sig_line,
verification_signatures->fpr) == 0)
valid = 1;
}
free(auth_sig_line);
}
else {
pb_log("%s: Signature for key ID '%s' ('%s') invalid."
" Status: %08x\n", __func__,
verification_signatures->fpr, filename,
verification_signatures->status);
}
verification_signatures = verification_signatures->next;
}
gpgme_data_release(ciphertext_data);
if (valid) {
/* Write decrypted file over ciphertext */
FILE *plaintext_file_handle = NULL;
plaintext_file_handle = fopen(filename, "wb");
if (!plaintext_file_handle) {
pb_log("%s: Could not create GPG plaintext file '%s'\n",
__func__, filename);
gpgme_data_release(plaintext_data);
gpgme_release(gpg_context);
return -1;
}
gpgme_data_seek(plaintext_data, 0, SEEK_SET);
if (err != GPG_ERR_NO_ERROR) {
pb_log("%s: Could not seek in GPG plaintext buffer\n",
__func__);
gpgme_data_release(plaintext_data);
gpgme_release(gpg_context);
fclose(plaintext_file_handle);
return -1;
}
while ((bytes_read = gpgme_data_read(plaintext_data, buffer,
8192)) > 0) {
size_t l2 = fwrite(buffer, 1, bytes_read,
plaintext_file_handle);
if (l2 < bytes_read) {
if (ferror(plaintext_file_handle)) {
/* General error */
result = -1;
pb_log("%s: failed: unknown fault\n",
__func__);
}
else {
/* No space on destination device */
result = -1;
pb_log("%s: failed: temporary storage"
" full\n", __func__);
}
break;
}
}
fclose(plaintext_file_handle);
}
/* Clean up */
gpgme_data_release(plaintext_data);
gpgme_release(gpg_context);
if (!valid) {
pb_log_fn("Incorrect GPG signature\n");
return -1;
}
pb_log("%s: GPG signature for decrypted file '%s' verified\n",
__func__, filename);
return result;
}
int verify_file_signature(const char *plaintext_filename,
const char *signature_filename, FILE *authorized_signatures_handle,
const char *keyring_path)
{
int valid = 0;
gpgme_signature_t verification_signatures;
gpgme_verify_result_t verification_result;
gpgme_data_t plaintext_data;
gpgme_data_t signature_data;
gpgme_engine_info_t enginfo;
gpgme_ctx_t gpg_context;
gpgme_error_t err;
if (signature_filename == NULL)
return -1;
/* Initialize gpgme */
setlocale (LC_ALL, "");
gpgme_check_version(NULL);
gpgme_set_locale(NULL, LC_CTYPE, setlocale (LC_CTYPE, NULL));
err = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
if (err != GPG_ERR_NO_ERROR) {
pb_log_fn("OpenPGP support not available\n");
return -1;
}
err = gpgme_get_engine_info(&enginfo);
if (err != GPG_ERR_NO_ERROR) {
pb_log_fn("GPG engine failed to initialize\n");
return -1;
}
err = gpgme_new(&gpg_context);
if (err != GPG_ERR_NO_ERROR) {
pb_log_fn("GPG context could not be created\n");
return -1;
}
err = gpgme_set_protocol(gpg_context, GPGME_PROTOCOL_OpenPGP);
if (err != GPG_ERR_NO_ERROR) {
pb_log_fn("GPG protocol could not be set\n");
return -1;
}
if (keyring_path)
err = gpgme_ctx_set_engine_info (gpg_context,
GPGME_PROTOCOL_OpenPGP, enginfo->file_name,
keyring_path);
else
err = gpgme_ctx_set_engine_info (gpg_context,
GPGME_PROTOCOL_OpenPGP, enginfo->file_name,
enginfo->home_dir);
if (err != GPG_ERR_NO_ERROR) {
pb_log_fn("Could not set GPG engine information\n");
return -1;
}
err = gpgme_data_new_from_file(&plaintext_data, plaintext_filename, 1);
if (err != GPG_ERR_NO_ERROR) {
pb_log("%s: Could not create GPG plaintext data buffer"
" from file '%s'\n", __func__, plaintext_filename);
return -1;
}
err = gpgme_data_new_from_file(&signature_data, signature_filename, 1);
if (err != GPG_ERR_NO_ERROR) {
pb_log("%s: Could not create GPG signature data buffer"
" from file '%s'\n", __func__, signature_filename);
return -1;
}
/* Check signature */
err = gpgme_op_verify(gpg_context, signature_data, plaintext_data,
NULL);
if (err != GPG_ERR_NO_ERROR) {
pb_log("%s: Could not verify file using GPG signature '%s'\n",
__func__, signature_filename);
return -1;
}
verification_result = gpgme_op_verify_result(gpg_context);
verification_signatures = verification_result->signatures;
while (verification_signatures) {
if (verification_signatures->status != GPG_ERR_NO_ERROR) {
/* Signature verification failure */
pb_log("%s: Signature for key ID '%s' ('%s') invalid."
" Status: %08x\n", __func__,
verification_signatures->fpr,
signature_filename,
verification_signatures->status);
verification_signatures = verification_signatures->next;
continue;
}
/* Signature check passed with no error */
pb_log("%s: Good signature for key ID '%s' ('%s')\n",
__func__, verification_signatures->fpr,
signature_filename);
/* Verify fingerprint is present in
* authorized signatures file
*/
char *auth_sig_line = NULL;
size_t auth_sig_len = 0;
ssize_t auth_sig_read;
rewind(authorized_signatures_handle);
while ((auth_sig_read = getline(&auth_sig_line,
&auth_sig_len,
authorized_signatures_handle)) != -1) {
auth_sig_len = strlen(auth_sig_line);
while ((auth_sig_line[auth_sig_len-1] == '\n')
|| (auth_sig_line[auth_sig_len-1] == '\r'))
auth_sig_len--;
auth_sig_line[auth_sig_len] = '\0';
if (strcmp(auth_sig_line,
verification_signatures->fpr) == 0)
valid = 1;
}
free(auth_sig_line);
verification_signatures = verification_signatures->next;
}
/* Clean up */
gpgme_data_release(plaintext_data);
gpgme_data_release(signature_data);
gpgme_release(gpg_context);
if (!valid) {
pb_log_fn("Incorrect GPG signature\n");
return -1;
}
pb_log("%s: GPG signature '%s' for file '%s' verified\n",
__func__, signature_filename, plaintext_filename);
return 0;
}
int lockdown_status() {
/* assume most restrictive lockdown type */
int ret = PB_LOCKDOWN_SIGN;
#if !defined(HARD_LOCKDOWN)
if (access(LOCKDOWN_FILE, F_OK) == -1)
return PB_LOCKDOWN_NONE;
#endif
/* determine lockdown type */
FILE *authorized_signatures_handle = NULL;
authorized_signatures_handle = fopen(LOCKDOWN_FILE, "r");
if (!authorized_signatures_handle)
return ret;
char *auth_sig_line = NULL;
size_t auth_sig_len = 0;
ssize_t auth_sig_read;
rewind(authorized_signatures_handle);
if ((auth_sig_read = getline(&auth_sig_line,
&auth_sig_len,
authorized_signatures_handle)) != -1) {
auth_sig_len = strlen(auth_sig_line);
while ((auth_sig_line[auth_sig_len-1] == '\n')
|| (auth_sig_line[auth_sig_len-1] == '\r'))
auth_sig_len--;
auth_sig_line[auth_sig_len] = 0;
if (strcmp(auth_sig_line, "ENCRYPTED") == 0) {
/* first line indicates encrypted files
* expected. enable decryption.
*/
ret = PB_LOCKDOWN_DECRYPT;
}
}
free(auth_sig_line);
return ret;
}
|