1 /* t-edit-sign.c - Regression test.
2 * Copyright (C) 2000 Werner Koch (dd9jn)
3 * Copyright (C) 2001, 2002, 2003, 2004, 2021 g10 Code GmbH
4 * Software engineering by Ingo Klöcker <dev@ingo-kloecker.de>
6 * This file is part of GPGME.
8 * GPGME is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as
10 * published by the Free Software Foundation; either version 2.1 of
11 * the License, or (at your option) any later version.
13 * GPGME is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this program; if not, see <https://gnu.org/licenses/>.
20 * SPDX-License-Identifier: LGPL-2.1-or-later
23 /* We need to include config.h so that we know whether we are building
24 with large file system (LFS) support. */
39 #include "t-support.h"
43 flush_data (gpgme_data_t dh)
48 ret = gpgme_data_seek (dh, 0, SEEK_SET);
50 fail_if_err (gpgme_error_from_errno (errno));
51 while ((ret = gpgme_data_read (dh, buf, 100)) > 0)
52 fwrite (buf, ret, 1, stdout);
54 fail_if_err (gpgme_error_from_errno (errno));
59 interact_fnc (void *opaque, const char *status, const char *args, int fd)
61 const char *result = NULL;
62 gpgme_data_t out = (gpgme_data_t) opaque;
64 fputs ("[-- Response --]\n", stdout);
67 fprintf (stdout, "[-- Code: %s, %s --]\n", status, args);
71 if (!strcmp (args, "keyedit.prompt"))
81 /* This fixes the primary user ID so the keylisting
82 tests will have predictable output. */
95 else if (!strcmp (args, "keyedit.save.okay"))
97 else if (!strcmp (args, "sign_uid.okay"))
103 gpgme_io_writen (fd, result, strlen (result));
104 gpgme_io_writen (fd, "\n", 1);
111 main (int argc, char **argv)
115 gpgme_data_t out = NULL;
116 const char *signer_fpr = "A0FF4590BB6122EDEF6E3C542D727CC768697734"; /* Alpha Test */
117 gpgme_key_t signing_key = NULL;
118 const char *key_fpr = "D695676BDCEDCC2CDD6152BCFE180B1DA9E3B0B2"; /* Bravo Test */
119 gpgme_key_t key = NULL;
120 gpgme_key_t signed_key = NULL;
121 gpgme_user_id_t signed_uid = NULL;
122 gpgme_key_sig_t key_sig = NULL;
129 init_gpgme (GPGME_PROTOCOL_OpenPGP);
131 err = gpgme_new (&ctx);
135 agent_info = getenv("GPG_AGENT_INFO");
136 if (!(agent_info && strchr (agent_info, ':')))
137 gpgme_set_passphrase_cb (ctx, passphrase_cb, 0);
139 err = gpgme_get_key (ctx, signer_fpr, &signing_key, 1);
141 err = gpgme_signers_add (ctx, signing_key);
144 err = gpgme_set_ctx_flag (ctx, "cert-expire", "42d");
147 err = gpgme_get_key (ctx, key_fpr, &key, 0);
150 err = gpgme_data_new (&out);
153 err = gpgme_op_interact (ctx, key, 0, interact_fnc, out, out);
156 fputs ("[-- Last response --]\n", stdout);
159 gpgme_data_release (out);
160 gpgme_key_unref (key);
161 gpgme_key_unref (signing_key);
163 /* Verify the key signature */
164 mode = gpgme_get_keylist_mode (ctx);
165 mode |= GPGME_KEYLIST_MODE_SIGS;
166 err = gpgme_set_keylist_mode (ctx, mode);
168 err = gpgme_get_key (ctx, key_fpr, &signed_key, 0);
171 signed_uid = key->uids;
174 fprintf (stderr, "Signed key has no user IDs\n");
177 if (!signed_uid->signatures || !signed_uid->signatures->next)
179 fprintf (stderr, "Signed user ID has less signatures than expected\n");
182 key_sig = signed_uid->signatures->next;
183 if (strcmp ("2D727CC768697734", key_sig->keyid))
185 fprintf (stderr, "Unexpected key ID in second user ID sig: %s\n",
189 if (key_sig->expires != key_sig->timestamp + 42*86400L)
191 fprintf (stderr, "Key signature unexpectedly does not expire in 42 days\n");
192 fprintf (stderr, "signature date: %ld, expiration date: %ld\n",
193 key_sig->timestamp, key_sig->expires);
197 gpgme_key_unref (signed_key);