1 /* t-verify.c - Regression test.
2 Copyright (C) 2000 Werner Koch (dd9jn)
3 Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH
5 This file is part of GPGME.
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.
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.
17 You should have received a copy of the GNU Lesser General Public
18 License along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22 /* We need to include config.h so that we know whether we are building
23 with large file system (LFS) support. */
34 #include "t-support.h"
37 static const char test_text1[] = "Just GNU it!\n";
38 static const char test_text1f[]= "Just GNU it?\n";
39 static const char test_sig1[] =
41 "-----BEGIN PGP SIGNATURE-----\n"
43 "iEYEABECAAYFAjoKgjIACgkQLXJ8x2hpdzQMSwCeO/xUrhysZ7zJKPf/FyXA//u1\n"
44 "ZgIAn0204PBR7yxSdQx6CFxugstNqmRv\n"
46 "-----END PGP SIGNATURE-----\n"
48 "-----BEGIN PGP SIGNATURE-----\n"
49 "Version: GnuPG v1.0.4-2 (GNU/Linux)\n"
50 "Comment: For info see http://www.gnupg.org\n"
52 "iJcEABECAFcFAjoS8/E1FIAAAAAACAAkZm9vYmFyLjF0aGlzIGlzIGEgbm90YXRp\n"
53 "b24gZGF0YSB3aXRoIDIgbGluZXMaGmh0dHA6Ly93d3cuZ3Uub3JnL3BvbGljeS8A\n"
54 "CgkQLXJ8x2hpdzQLyQCbBW/fgU8ZeWSlWPM1F8umHX17bAAAoIfSNDSp5zM85XcG\n"
57 "-----END PGP SIGNATURE-----\n"
59 "-----BEGIN PGP SIGNATURE-----\n"
61 "iN0EABECAJ0FAjoS+i9FFIAAAAAAAwA5YmFyw7bDpMO8w58gZGFzIHdhcmVuIFVt\n"
62 "bGF1dGUgdW5kIGpldHp0IGVpbiBwcm96ZW50JS1aZWljaGVuNRSAAAAAAAgAJGZv\n"
63 "b2Jhci4xdGhpcyBpcyBhIG5vdGF0aW9uIGRhdGEgd2l0aCAyIGxpbmVzGhpodHRw\n"
64 "Oi8vd3d3Lmd1Lm9yZy9wb2xpY3kvAAoJEC1yfMdoaXc0JBIAoIiLlUsvpMDOyGEc\n"
65 "dADGKXF/Hcb+AKCJWPphZCphduxSvrzH0hgzHdeQaA==\n"
67 "-----END PGP SIGNATURE-----\n"
70 static const char test_sig2[] =
71 "-----BEGIN PGP MESSAGE-----\n"
73 "owGbwMvMwCSoW1RzPCOz3IRxjXQSR0lqcYleSUWJTZOvjVdpcYmCu1+oQmaJIleH\n"
74 "GwuDIBMDGysTSIqBi1MApi+nlGGuwDeHao53HBr+FoVGP3xX+kvuu9fCMJvl6IOf\n"
75 "y1kvP4y+8D5a11ang0udywsA\n"
77 "-----END PGP MESSAGE-----\n";
79 /* A message with a prepended but unsigned plaintext packet. */
80 static const char double_plaintext_sig[] =
81 "-----BEGIN PGP MESSAGE-----\n"
83 "rDRiCmZvb2Jhci50eHRF4pxNVGhpcyBpcyBteSBzbmVha3kgcGxhaW50ZXh0IG1l\n"
84 "c3NhZ2UKowGbwMvMwCSoW1RzPCOz3IRxTWISa6JebnG666MFD1wzSzJSixQ81XMV\n"
85 "UlITUxTyixRyKxXKE0uSMxQyEosVikvyCwpSU/S4FNCArq6Ce1F+aXJGvoJvYlGF\n"
86 "erFCTmJxiUJ5flFKMVeHGwuDIBMDGysTyA4GLk4BmO036xgWzMgzt9V85jCtfDFn\n"
87 "UqVooWlGXHwNw/xg/fVzt9VNbtjtJ/fhUqYo0/LyCGEA\n"
89 "-----END PGP MESSAGE-----\n";
95 check_result (gpgme_verify_result_t result, unsigned int summary, char *fpr,
96 gpgme_error_t status, int notation)
98 gpgme_signature_t sig;
100 sig = result->signatures;
101 if (!sig || sig->next)
103 fprintf (stderr, "%s:%i: Unexpected number of signatures\n",
107 if (sig->summary != summary)
109 fprintf (stderr, "%s:%i: Unexpected signature summary: "
110 "want=0x%x have=0x%x\n",
111 __FILE__, __LINE__, summary, sig->summary);
114 if (strcmp (sig->fpr, fpr))
116 fprintf (stderr, "%s:%i: Unexpected fingerprint: %s\n",
117 __FILE__, __LINE__, sig->fpr);
120 if (gpgme_err_code (sig->status) != status)
122 fprintf (stderr, "%s:%i: Unexpected signature status: %s\n",
123 __FILE__, __LINE__, gpgme_strerror (sig->status));
132 } expected_notations[] = {
134 "\xc3\xb6\xc3\xa4\xc3\xbc\xc3\x9f"
135 " das waren Umlaute und jetzt ein prozent%-Zeichen" },
137 "this is a notation data with 2 lines" },
139 "http://www.gu.org/policy/" }
142 gpgme_sig_notation_t r;
144 for (i=0; i < DIM(expected_notations); i++ )
145 expected_notations[i].seen = 0;
147 for (r = sig->notations; r; r = r->next)
150 for (i=0; i < DIM(expected_notations); i++)
152 if ( ((r->name && expected_notations[i].name
153 && !strcmp (r->name, expected_notations[i].name)
155 == strlen (expected_notations[i].name))
156 || (!r->name && !expected_notations[i].name
157 && r->name_len == 0))
159 && !strcmp (r->value, expected_notations[i].value)
160 && r->value_len == strlen (expected_notations[i].value))
162 expected_notations[i].seen++;
168 fprintf (stderr, "%s:%i: Unexpected notation data\n",
173 for (i=0; i < DIM(expected_notations); i++ )
175 if (expected_notations[i].seen != 1)
177 fprintf (stderr, "%s:%i: Missing or duplicate notation data\n",
183 if (sig->wrong_key_usage)
185 fprintf (stderr, "%s:%i: Unexpectedly wrong key usage\n",
189 if (sig->validity != GPGME_VALIDITY_UNKNOWN)
191 fprintf (stderr, "%s:%i: Unexpected validity: %i\n",
192 __FILE__, __LINE__, sig->validity);
195 if (gpgme_err_code (sig->validity_reason) != GPG_ERR_NO_ERROR)
197 fprintf (stderr, "%s:%i: Unexpected validity reason: %s\n",
198 __FILE__, __LINE__, gpgme_strerror (sig->validity_reason));
205 main (int argc, char *argv[])
209 gpgme_data_t sig, text;
210 gpgme_verify_result_t result;
212 init_gpgme (GPGME_PROTOCOL_OpenPGP);
214 err = gpgme_new (&ctx);
217 /* Checking a valid message. */
218 err = gpgme_data_new_from_mem (&text, test_text1, strlen (test_text1), 0);
220 err = gpgme_data_new_from_mem (&sig, test_sig1, strlen (test_sig1), 0);
222 err = gpgme_op_verify (ctx, sig, text, NULL);
224 result = gpgme_op_verify_result (ctx);
225 check_result (result, 0, "A0FF4590BB6122EDEF6E3C542D727CC768697734",
226 GPG_ERR_NO_ERROR, 1);
228 /* Checking a manipulated message. */
229 gpgme_data_release (text);
230 err = gpgme_data_new_from_mem (&text, test_text1f, strlen (test_text1f), 0);
232 gpgme_data_seek (sig, 0, SEEK_SET);
233 err = gpgme_op_verify (ctx, sig, text, NULL);
235 result = gpgme_op_verify_result (ctx);
236 check_result (result, GPGME_SIGSUM_RED, "2D727CC768697734",
237 GPG_ERR_BAD_SIGNATURE, 0);
239 /* Checking a normal signature. */
240 gpgme_data_release (sig);
241 gpgme_data_release (text);
242 err = gpgme_data_new_from_mem (&sig, test_sig2, strlen (test_sig2), 0);
244 err = gpgme_data_new (&text);
246 err = gpgme_op_verify (ctx, sig, NULL, text);
248 result = gpgme_op_verify_result (ctx);
249 check_result (result, 0, "A0FF4590BB6122EDEF6E3C542D727CC768697734",
250 GPG_ERR_NO_ERROR, 0);
253 /* Checking an invalid message. */
254 gpgme_data_release (sig);
255 gpgme_data_release (text);
256 err = gpgme_data_new_from_mem (&sig, double_plaintext_sig,
257 strlen (double_plaintext_sig), 0);
259 err = gpgme_data_new (&text);
261 err = gpgme_op_verify (ctx, sig, NULL, text);
262 if (gpgme_err_code (err) != GPG_ERR_BAD_DATA)
264 fprintf (stderr, "%s:%i: Double plaintext message not detected\n",
269 gpgme_data_release (sig);
270 gpgme_data_release (text);