vinix-ports / tests / userland / verify-index.c
45 lines · 43 sloc · 1.66 KB · 6c51c0c4f4ccae59dada83eeac3696e114e97e40
Raw
1/* Exercise the GPGME OpenPGP engine used by vpac against a detached signature. */
2#include <gpgme.h>
3#include <stdio.h>
4#include <string.h>
5
6static int check(gpgme_error_t err, const char *operation)
7{
8 if (err)
9 fprintf(stderr, "%s: %s\n", operation, gpgme_strerror(err));
10 return err != 0;
11}
12
13int main(int argc, char **argv)
14{
15 if (argc != 5 || (strcmp(argv[1], "valid") && strcmp(argv[1], "damaged")))
16 return 2;
17 gpgme_check_version(NULL);
18 gpgme_ctx_t ctx;
19 if (check(gpgme_new(&ctx), "create GPGME context"))
20 return 1;
21 gpgme_data_t signature = NULL, input = NULL;
22 int result = 1;
23 if (check(gpgme_set_protocol(ctx, GPGME_PROTOCOL_OpenPGP), "select OpenPGP") ||
24 check(gpgme_data_new_from_file(&signature, argv[2], 1), "read signature") ||
25 check(gpgme_data_new_from_file(&input, argv[3], 1), "read index") ||
26 check(gpgme_op_verify(ctx, signature, input, NULL), "verify index"))
27 goto done;
28 gpgme_verify_result_t verification = gpgme_op_verify_result(ctx);
29 gpgme_signature_t sig = verification ? verification->signatures : NULL;
30 if (!sig || sig->next) {
31 fprintf(stderr, "expected one index signature\n");
32 goto done;
33 }
34 printf("GPGME %s index: %s, signer %s\n", argv[1], gpgme_strerror(sig->status),
35 sig->fpr ? sig->fpr : "unknown");
36 if (!strcmp(argv[1], "valid"))
37 result = sig->status || !sig->fpr || strcmp(sig->fpr, argv[4]);
38 else
39 result = gpgme_err_code(sig->status) != GPG_ERR_BAD_SIGNATURE;
40done:
41 if (signature) gpgme_data_release(signature);
42 if (input) gpgme_data_release(input);
43 gpgme_release(ctx);
44 return result;
45}
46