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
|
/***
This file is part of LockPassword
Copyright (C) 2020-2021 Aleksandr D. Goncharov (Joursoir) <chat@joursoir.net>
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, either version 3 of the License, or
(at your option) any later version.
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, see <https://www.gnu.org/licenses/>.
***/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <errno.h>
#include <gpgme.h>
#include "r-gpgme.h"
#define BUF_SIZE 128
#ifdef DEBUG
#define ret_if_err(ret, err) \
do { \
if(err != GPG_ERR_NO_ERROR) { \
fprintf(stderr, "%s:%d: %s: %s\n", __FILE__, \
__LINE__, gpgme_strsource(err), \
gpgme_strerror(err)); \
return ret; \
} \
} while(0)
#else
#define ret_if_err(ret, err) \
do { \
if(err != GPG_ERR_NO_ERROR) { \
fprintf(stderr, "%s: %s\n", \
gpgme_strsource(err), gpgme_strerror(err)); \
return ret; \
} \
} while(0)
#endif
static void init_gpgme()
{
/* The GPGME library communicates with child processes (the
crypto engines). If a child process dies unexpectedly, for
example due to a bug, or system problem, a SIGPIPE signal
will be delivered to the application. The default action is
to abort the program. To protect against this,
gpgme_check_version sets the SIGPIPE signal action to SIG_IGN
which means that the signal will be ignored. */
setlocale(LC_ALL, "");
gpgme_check_version(NULL);
gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL));
#ifdef LC_MESSAGES
gpgme_set_locale(NULL, LC_MESSAGES, setlocale(LC_MESSAGES, NULL));
#endif
}
static int init_ctx(gpgme_ctx_t ctx, gpgme_protocol_t protocol)
{
gpgme_error_t err;
err = gpgme_engine_check_version(protocol);
ret_if_err(1, err);
err = gpgme_set_protocol(ctx, protocol);
ret_if_err(1, err);
/* output be ASCII armored */
gpgme_set_armor(ctx, 1);
return 0;
}
static int loop_read(const char *path, gpgme_data_t dh)
{
char buf[BUF_SIZE];
int ret;
FILE *f = fopen(path, "w");
if(f == NULL)
return 1;
ret = gpgme_data_seek(dh, 0, SEEK_SET);
if(ret) {
fclose(f);
ret_if_err(1, gpgme_err_code_from_errno(errno));
}
while((ret = gpgme_data_read(dh, buf, BUF_SIZE)) > 0)
fwrite(buf, ret, 1, f);
if(ret < 0) {
fclose(f);
ret_if_err(1, gpgme_err_code_from_errno(errno));
}
fclose(f);
return 0;
}
int ecnrypt_data(const char *path, const char *data, const char *pubkey)
{
gpgme_error_t err;
gpgme_ctx_t ctx;
gpgme_data_t plain, cipher;
gpgme_key_t key[] = { NULL, NULL };
init_gpgme();
err = gpgme_new(&ctx);
ret_if_err(1, err);
err = init_ctx(ctx, GPGME_PROTOCOL_OPENPGP);
ret_if_err(1, err);
/* start encrypt */
err = gpgme_data_new_from_mem(&plain, data,
sizeof(char) * (strlen(data) + 1), 0);
ret_if_err(1, err);
err = gpgme_data_new(&cipher);
ret_if_err(1, err);
err = gpgme_get_key(ctx, pubkey, &key[0], 0);
ret_if_err(1, err);
err = gpgme_op_encrypt(ctx, key, GPGME_ENCRYPT_ALWAYS_TRUST,
plain, cipher);
ret_if_err(1, err);
if(loop_read(path, cipher))
return 1;
gpgme_data_release(plain);
gpgme_data_release(cipher);
gpgme_release(ctx);
return 0;
}
char *decrypt_data(const char *path)
{
gpgme_error_t err;
gpgme_ctx_t ctx;
gpgme_data_t cipher, plain;
init_gpgme();
err = gpgme_new(&ctx);
ret_if_err(NULL, err);
err = init_ctx(ctx, GPGME_PROTOCOL_OPENPGP);
ret_if_err(NULL, err);
/* start decrypt */
err = gpgme_data_new_from_file(&cipher, path, 1);
ret_if_err(NULL, err);
err = gpgme_data_new(&plain);
ret_if_err(NULL, err);
err = gpgme_op_decrypt(ctx, cipher, plain);
ret_if_err(NULL, err);
int ret = gpgme_data_seek(plain, 0, SEEK_SET);
if(ret)
ret_if_err(NULL, gpgme_err_code_from_errno(errno));
char *data = malloc(sizeof(char) * (BUF_SIZE + 1));
gpgme_data_read(plain, data, BUF_SIZE);
gpgme_data_release(plain);
gpgme_data_release(cipher);
gpgme_release(ctx);
return data;
}
|