|
0 |
/* t-edit-sign.c - Regression test.
|
|
1 |
* Copyright (C) 2000 Werner Koch (dd9jn)
|
|
2 |
* Copyright (C) 2001, 2002, 2003, 2004, 2021 g10 Code GmbH
|
|
3 |
* Software engineering by Ingo Klöcker <dev@ingo-kloecker.de>
|
|
4 |
*
|
|
5 |
* This file is part of GPGME.
|
|
6 |
*
|
|
7 |
* GPGME is free software; you can redistribute it and/or modify it
|
|
8 |
* under the terms of the GNU Lesser General Public License as
|
|
9 |
* published by the Free Software Foundation; either version 2.1 of
|
|
10 |
* the License, or (at your option) any later version.
|
|
11 |
*
|
|
12 |
* GPGME is distributed in the hope that it will be useful, but
|
|
13 |
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
15 |
* Lesser General Public License for more details.
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU Lesser General Public
|
|
18 |
* License along with this program; if not, see <https://gnu.org/licenses/>.
|
|
19 |
* SPDX-License-Identifier: LGPL-2.1-or-later
|
|
20 |
*/
|
|
21 |
|
|
22 |
/* We need to include config.h so that we know whether we are building
|
|
23 |
with large file system (LFS) support. */
|
|
24 |
#ifdef HAVE_CONFIG_H
|
|
25 |
#include <config.h>
|
|
26 |
#endif
|
|
27 |
|
|
28 |
#include <stdio.h>
|
|
29 |
#include <stdlib.h>
|
|
30 |
#include <string.h>
|
|
31 |
#include <assert.h>
|
|
32 |
#include <errno.h>
|
|
33 |
#include <unistd.h>
|
|
34 |
#include <errno.h>
|
|
35 |
|
|
36 |
#include <gpgme.h>
|
|
37 |
|
|
38 |
#include "t-support.h"
|
|
39 |
|
|
40 |
|
|
41 |
static void
|
|
42 |
flush_data (gpgme_data_t dh)
|
|
43 |
{
|
|
44 |
char buf[100];
|
|
45 |
int ret;
|
|
46 |
|
|
47 |
ret = gpgme_data_seek (dh, 0, SEEK_SET);
|
|
48 |
if (ret)
|
|
49 |
fail_if_err (gpgme_error_from_errno (errno));
|
|
50 |
while ((ret = gpgme_data_read (dh, buf, 100)) > 0)
|
|
51 |
fwrite (buf, ret, 1, stdout);
|
|
52 |
if (ret < 0)
|
|
53 |
fail_if_err (gpgme_error_from_errno (errno));
|
|
54 |
}
|
|
55 |
|
|
56 |
|
|
57 |
gpgme_error_t
|
|
58 |
interact_fnc (void *opaque, const char *status, const char *args, int fd)
|
|
59 |
{
|
|
60 |
const char *result = NULL;
|
|
61 |
gpgme_data_t out = (gpgme_data_t) opaque;
|
|
62 |
|
|
63 |
fputs ("[-- Response --]\n", stdout);
|
|
64 |
flush_data (out);
|
|
65 |
|
|
66 |
fprintf (stdout, "[-- Code: %s, %s --]\n", status, args);
|
|
67 |
|
|
68 |
if (fd >= 0)
|
|
69 |
{
|
|
70 |
if (!strcmp (args, "keyedit.prompt"))
|
|
71 |
{
|
|
72 |
static int step = 0;
|
|
73 |
|
|
74 |
switch (step)
|
|
75 |
{
|
|
76 |
case 0:
|
|
77 |
result = "fpr";
|
|
78 |
break;
|
|
79 |
case 1:
|
|
80 |
/* This fixes the primary user ID so the keylisting
|
|
81 |
tests will have predictable output. */
|
|
82 |
result = "1";
|
|
83 |
break;
|
|
84 |
case 2:
|
|
85 |
result = "sign";
|
|
86 |
break;
|
|
87 |
|
|
88 |
default:
|
|
89 |
result = "quit";
|
|
90 |
break;
|
|
91 |
}
|
|
92 |
step++;
|
|
93 |
}
|
|
94 |
else if (!strcmp (args, "keyedit.save.okay"))
|
|
95 |
result = "Y";
|
|
96 |
else if (!strcmp (args, "sign_uid.okay"))
|
|
97 |
result = "Y";
|
|
98 |
}
|
|
99 |
|
|
100 |
if (result)
|
|
101 |
{
|
|
102 |
gpgme_io_writen (fd, result, strlen (result));
|
|
103 |
gpgme_io_writen (fd, "\n", 1);
|
|
104 |
}
|
|
105 |
return 0;
|
|
106 |
}
|
|
107 |
|
|
108 |
|
|
109 |
int
|
|
110 |
main (int argc, char **argv)
|
|
111 |
{
|
|
112 |
gpgme_ctx_t ctx;
|
|
113 |
gpgme_error_t err;
|
|
114 |
gpgme_data_t out = NULL;
|
|
115 |
const char *signer_fpr = "A0FF4590BB6122EDEF6E3C542D727CC768697734"; /* Alpha Test */
|
|
116 |
gpgme_key_t signing_key = NULL;
|
|
117 |
const char *key_fpr = "D695676BDCEDCC2CDD6152BCFE180B1DA9E3B0B2"; /* Bravo Test */
|
|
118 |
gpgme_key_t key = NULL;
|
|
119 |
gpgme_key_t signed_key = NULL;
|
|
120 |
gpgme_user_id_t signed_uid = NULL;
|
|
121 |
gpgme_key_sig_t key_sig = NULL;
|
|
122 |
char *agent_info;
|
|
123 |
int mode;
|
|
124 |
|
|
125 |
(void)argc;
|
|
126 |
(void)argv;
|
|
127 |
|
|
128 |
init_gpgme (GPGME_PROTOCOL_OpenPGP);
|
|
129 |
|
|
130 |
err = gpgme_new (&ctx);
|
|
131 |
fail_if_err (err);
|
|
132 |
|
|
133 |
/* Sign the key */
|
|
134 |
agent_info = getenv("GPG_AGENT_INFO");
|
|
135 |
if (!(agent_info && strchr (agent_info, ':')))
|
|
136 |
gpgme_set_passphrase_cb (ctx, passphrase_cb, 0);
|
|
137 |
|
|
138 |
err = gpgme_get_key (ctx, signer_fpr, &signing_key, 1);
|
|
139 |
fail_if_err (err);
|
|
140 |
err = gpgme_signers_add (ctx, signing_key);
|
|
141 |
fail_if_err (err);
|
|
142 |
|
|
143 |
err = gpgme_set_ctx_flag (ctx, "cert-expire", "42d");
|
|
144 |
fail_if_err (err);
|
|
145 |
|
|
146 |
err = gpgme_get_key (ctx, key_fpr, &key, 0);
|
|
147 |
fail_if_err (err);
|
|
148 |
|
|
149 |
err = gpgme_data_new (&out);
|
|
150 |
fail_if_err (err);
|
|
151 |
|
|
152 |
err = gpgme_op_interact (ctx, key, 0, interact_fnc, out, out);
|
|
153 |
fail_if_err (err);
|
|
154 |
|
|
155 |
fputs ("[-- Last response --]\n", stdout);
|
|
156 |
flush_data (out);
|
|
157 |
|
|
158 |
gpgme_data_release (out);
|
|
159 |
gpgme_key_unref (key);
|
|
160 |
gpgme_key_unref (signing_key);
|
|
161 |
|
|
162 |
/* Verify the key signature */
|
|
163 |
mode = gpgme_get_keylist_mode (ctx);
|
|
164 |
mode |= GPGME_KEYLIST_MODE_SIGS;
|
|
165 |
err = gpgme_set_keylist_mode (ctx, mode);
|
|
166 |
fail_if_err (err);
|
|
167 |
err = gpgme_get_key (ctx, key_fpr, &signed_key, 0);
|
|
168 |
fail_if_err (err);
|
|
169 |
|
|
170 |
signed_uid = key->uids;
|
|
171 |
if (!signed_uid)
|
|
172 |
{
|
|
173 |
fprintf (stderr, "Signed key has no user IDs\n");
|
|
174 |
exit (1);
|
|
175 |
}
|
|
176 |
if (!signed_uid->signatures || !signed_uid->signatures->next)
|
|
177 |
{
|
|
178 |
fprintf (stderr, "Signed user ID has less signatures than expected\n");
|
|
179 |
exit (1);
|
|
180 |
}
|
|
181 |
key_sig = signed_uid->signatures->next;
|
|
182 |
if (strcmp ("2D727CC768697734", key_sig->keyid))
|
|
183 |
{
|
|
184 |
fprintf (stderr, "Unexpected key ID in second user ID sig: %s\n",
|
|
185 |
key_sig->keyid);
|
|
186 |
exit (1);
|
|
187 |
}
|
|
188 |
if (key_sig->expires != key_sig->timestamp + 42*86400L)
|
|
189 |
{
|
|
190 |
fprintf (stderr, "Key signature unexpectedly does not expire in 42 days\n");
|
|
191 |
fprintf (stderr, "signature date: %ld, expiration date: %ld\n",
|
|
192 |
key_sig->timestamp, key_sig->expires);
|
|
193 |
exit (1);
|
|
194 |
}
|
|
195 |
|
|
196 |
gpgme_key_unref (signed_key);
|
|
197 |
gpgme_release (ctx);
|
|
198 |
|
|
199 |
return 0;
|
|
200 |
}
|