summaryrefslogtreecommitdiffstats
path: root/src/gpgme.c
blob: 4823754081e74144e26ede7166de34eddc7f7028 (plain)
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
/*
 * Copyright (C) 2003-2018 Jason Woodward <woodwardj at jaos dot org>
 *
 * 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 2 of the License, or
 * 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 Library 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.
 */

#include "main.h"

static gpgme_ctx_t *_slapt_init_gpgme_ctx(void)
{
    gpgme_error_t e;
    gpgme_ctx_t *ctx = slapt_malloc(sizeof *ctx);

    e = gpgme_new(ctx);
    if (e != GPG_ERR_NO_ERROR) {
        fprintf(stderr, "GPGME: %s\n", gpgme_strerror(e));
        free(ctx);
        return NULL;
    }

    e = gpgme_set_protocol(*ctx, GPGME_PROTOCOL_OpenPGP);
    if (e != GPG_ERR_NO_ERROR) {
        fprintf(stderr, "GPGME: %s\n", gpgme_strerror(e));
        gpgme_release(*ctx);
        free(ctx);
        return NULL;
    }

    gpgme_set_armor(*ctx, 1);

    return ctx;
}

static void _slapt_free_gpgme_ctx(gpgme_ctx_t *ctx)
{
    gpgme_release(*ctx);
    free(ctx);
}

FILE *slapt_get_pkg_source_checksums_signature(const slapt_rc_config *global_config,
                                               const char *url,
                                               bool *compressed)
{
    FILE *tmp_checksum_f = NULL;
    char *checksum_head = NULL;
    bool interactive = slapt_is_interactive(global_config);
    char *location_uncompressed = SLAPT_CHECKSUM_ASC_FILE;
    char *location_compressed = SLAPT_CHECKSUM_ASC_FILE_GZ;
    char *filename = NULL;
    char *local_head = NULL;
    char *location;

    if (*compressed) {
        location = location_compressed;
        *compressed = true;
    } else {
        location = location_uncompressed;
        *compressed = false;
    }

    filename = slapt_gen_filename_from_url(url, location);
    local_head = slapt_read_head_cache(filename);
    checksum_head = slapt_head_mirror_data(url, location);

    if (checksum_head == NULL) {
        if (interactive)
            printf(gettext("Not Found\n"));
        free(filename);
        free(local_head);
        if (checksum_head != NULL)
            free(checksum_head);
        return NULL;
    }

    if (checksum_head != NULL && local_head != NULL &&
        strcmp(checksum_head, local_head) == 0) {
        if ((tmp_checksum_f = slapt_open_file(filename, "r")) == NULL)
            exit(EXIT_FAILURE);

        if (global_config->progress_cb == NULL)
            printf(gettext("Cached\n"));

    } else {
        const char *err = NULL;

        if (global_config->dl_stats)
            printf("\n");

        if ((tmp_checksum_f = slapt_open_file(filename, "w+b")) == NULL)
            exit(EXIT_FAILURE);

        err = slapt_get_mirror_data_from_source(tmp_checksum_f,
                                                global_config, url,
                                                location);
        if (!err) {
            if (interactive)
                printf(gettext("Done\n"));

        } else {
            fprintf(stderr, gettext("Failed to download: %s\n"), err);
            slapt_clear_head_cache(filename);
            fclose(tmp_checksum_f);
            free(filename);
            free(local_head);
            if (checksum_head != NULL)
                free(checksum_head);
            return NULL;
        }
        /* make sure we are back at the front of the file */
        rewind(tmp_checksum_f);

        /* if all is good, write it */
        if (checksum_head != NULL)
            slapt_write_head_cache(checksum_head, filename);
    }

    free(filename);
    free(local_head);

    if (checksum_head != NULL)
        free(checksum_head);

    return tmp_checksum_f;
}

FILE *slapt_get_pkg_source_gpg_key(const slapt_rc_config *global_config,
                                   const char *url,
                                   bool *compressed)
{
    FILE *tmp_key_f = NULL;
    char *key_head = NULL;
    char *filename = slapt_gen_filename_from_url(url, SLAPT_GPG_KEY);
    char *local_head = slapt_read_head_cache(filename);
    bool interactive = global_config->progress_cb == NULL && !global_config->dl_stats
                           ? true
                           : false;

    *compressed = false;
    key_head = slapt_head_mirror_data(url, SLAPT_GPG_KEY);

    if (key_head == NULL) {
        if (interactive)
            printf(gettext("Not Found\n"));
        free(filename);
        free(local_head);
        if (key_head != NULL)
            free(key_head);
        return NULL;
    }

    if (key_head != NULL && local_head != NULL &&
        strcmp(key_head, local_head) == 0) {
        if ((tmp_key_f = slapt_open_file(filename, "r")) == NULL)
            exit(EXIT_FAILURE);

        if (global_config->progress_cb == NULL)
            printf(gettext("Cached\n"));

    } else {
        const char *err = NULL;

        if ((tmp_key_f = slapt_open_file(filename, "w+b")) == NULL)
            exit(EXIT_FAILURE);

        err = slapt_get_mirror_data_from_source(tmp_key_f,
                                                global_config, url,
                                                SLAPT_GPG_KEY);

        if (!err) {
            if (interactive)
                printf(gettext("Done\n"));
        } else {
            fprintf(stderr, gettext("Failed to download: %s\n"), err);
            slapt_clear_head_cache(filename);
            fclose(tmp_key_f);
            free(filename);
            free(local_head);
            if (key_head != NULL)
                free(key_head);
            return NULL;
        }

        rewind(tmp_key_f);

        /* if all is good, write it */
        if (key_head != NULL)
            slapt_write_head_cache(key_head, filename);
    }

    free(filename);
    free(local_head);

    if (key_head != NULL)
        free(key_head);

    return tmp_key_f;
}

slapt_code_t slapt_add_pkg_source_gpg_key(FILE *key)
{
    gpgme_error_t e;
    gpgme_ctx_t *ctx = _slapt_init_gpgme_ctx();
    gpgme_import_result_t import_result;
    gpgme_data_t key_data;
    slapt_code_t imported = SLAPT_GPG_KEY_NOT_IMPORTED;

    if (ctx == NULL)
        return imported;

    e = gpgme_data_new_from_stream(&key_data, key);
    if (e != GPG_ERR_NO_ERROR) {
        fprintf(stderr, "GPGME: %s\n", gpgme_strerror(e));
        _slapt_free_gpgme_ctx(ctx);
        return imported;
    }

    e = gpgme_op_import(*ctx, key_data);
    if (e) {
        fprintf(stderr, "GPGME: %s\n", gpgme_strerror(e));
        gpgme_data_release(key_data);
        _slapt_free_gpgme_ctx(ctx);
        return imported;
    }

    import_result = gpgme_op_import_result(*ctx);
    if (import_result != NULL) {
        if (import_result->unchanged > 0)
            imported = SLAPT_GPG_KEY_UNCHANGED;
        else if (import_result->imported > 0)
            imported = SLAPT_GPG_KEY_IMPORTED;
    }

    gpgme_data_release(key_data);
    _slapt_free_gpgme_ctx(ctx);
    return imported;
}

static slapt_code_t _slapt_gpg_get_gpgme_error(gpgme_sigsum_t sum)
{
    switch (sum) {
    case GPGME_SIGSUM_KEY_REVOKED:
        return SLAPT_CHECKSUMS_NOT_VERIFIED_GPGME_KEY_REVOKED;
    case GPGME_SIGSUM_KEY_EXPIRED:
        return SLAPT_CHECKSUMS_NOT_VERIFIED_GPGME_KEY_EXPIRED;
    case GPGME_SIGSUM_SIG_EXPIRED:
        return SLAPT_CHECKSUMS_NOT_VERIFIED_GPGME_SIG_EXPIRED;
    case GPGME_SIGSUM_CRL_MISSING:
        return SLAPT_CHECKSUMS_NOT_VERIFIED_GPGME_CRL_MISSING;
    case GPGME_SIGSUM_CRL_TOO_OLD:
        return SLAPT_CHECKSUMS_NOT_VERIFIED_GPGME_CRL_TOO_OLD;
    case GPGME_SIGSUM_BAD_POLICY:
        return SLAPT_CHECKSUMS_NOT_VERIFIED_GPGME_BAD_POLICY;
    case GPGME_SIGSUM_SYS_ERROR:
        return SLAPT_CHECKSUMS_NOT_VERIFIED_GPGME_SYS_ERROR;
    default:
        break;
    }

    return SLAPT_CHECKSUMS_NOT_VERIFIED;
}

slapt_code_t slapt_gpg_verify_checksums(FILE *checksums,
                                        FILE *signature)
{
    gpgme_error_t e;
    gpgme_ctx_t *ctx = _slapt_init_gpgme_ctx();
    gpgme_data_t chk_data, asc_data;
    slapt_code_t verified = SLAPT_CHECKSUMS_NOT_VERIFIED;

    if (ctx == NULL)
        return SLAPT_CHECKSUMS_NOT_VERIFIED_NULL_CONTEXT;

    e = gpgme_data_new_from_stream(&chk_data, checksums);
    if (e != GPG_ERR_NO_ERROR) {
        _slapt_free_gpgme_ctx(ctx);
        return SLAPT_CHECKSUMS_NOT_VERIFIED_READ_CHECKSUMS;
    }

    e = gpgme_data_new_from_stream(&asc_data, signature);
    if (e != GPG_ERR_NO_ERROR) {
        gpgme_data_release(chk_data);
        _slapt_free_gpgme_ctx(ctx);
        return SLAPT_CHECKSUMS_NOT_VERIFIED_READ_SIGNATURE;
    }

    e = gpgme_op_verify(*ctx, asc_data, chk_data, NULL);

    if (e == GPG_ERR_NO_ERROR) {
        gpgme_verify_result_t verify_result = gpgme_op_verify_result(*ctx);
        if (verify_result != NULL) {
            gpgme_signature_t sig = verify_result->signatures;
            gpgme_sigsum_t sum = sig->summary;
            gpgme_error_t status = sig->status;

            if (sum & GPGME_SIGSUM_VALID || status == GPG_ERR_NO_ERROR) {
                verified = SLAPT_CHECKSUMS_VERIFIED;
            } else if (sum & GPGME_SIGSUM_KEY_MISSING) {
                verified = SLAPT_CHECKSUMS_MISSING_KEY;
            } else {
                verified = _slapt_gpg_get_gpgme_error(sum);
            }
        }
    } else {
        /* if gnupg|gnupg2 is missing: "GPGME: Invalid crypto engine" */
        fprintf(stderr, "GPGME: %s\n", gpgme_strerror(e));
    }

    gpgme_data_release(chk_data);
    gpgme_data_release(asc_data);
    _slapt_free_gpgme_ctx(ctx);

    return verified;
}