304fc2c6cb1e9743d332d3741d5ba1379d729135
[gpgme.git] / tests / gpgsm / t-verify.c
1 /* t-verify.c - Regression test.
2    Copyright (C) 2000 Werner Koch (dd9jn)
3    Copyright (C) 2001, 2002, 2003 g10 Code GmbH
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 General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (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    General Public License for more details.
16  
17    You should have received a copy of the GNU General Public License
18    along with GPGME; if not, write to the Free Software Foundation,
19    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24
25 #include <gpgme.h>
26
27 #include "t-support.h"
28
29 \f
30 static const char test_text1[] = "Hallo Leute!\n";
31 static const char test_text1f[]= "Hallo Leute?\n";
32 static const char test_sig1[] =
33 "-----BEGIN CMS OBJECT-----\n"
34 "MIAGCSqGSIb3DQEHAqCAMIACAQExCzAJBgUrDgMCGgUAMIAGCSqGSIb3DQEHAQAA\n"
35 "MYIBOTCCATUCAQEwcDBrMQswCQYDVQQGEwJERTETMBEGA1UEBxQKRPxzc2VsZG9y\n"
36 "ZjEWMBQGA1UEChMNZzEwIENvZGUgR21iSDEZMBcGA1UECxMQQWVneXB0ZW4gUHJv\n"
37 "amVjdDEUMBIGA1UEAxMLdGVzdCBjZXJ0IDECAQAwBwYFKw4DAhqgJTAjBgkqhkiG\n"
38 "9w0BCQQxFgQU7FC/ibH3lC9GE24RJJxa8zqP7wEwCwYJKoZIhvcNAQEBBIGAA3oC\n"
39 "DUmKERmD1eoJYFw38y/qnncS/6ZPjWINDIphZeK8mzAANpvpIaRPf3sNBznb89QF\n"
40 "mRgCXIWcjlHT0DTRLBf192Ve22IyKH00L52CqFsSN3a2sajqRUlXH8RY2D+Al71e\n"
41 "MYdRclgjObCcoilA8fZ13VR4DiMJVFCxJL4qVWI=\n"
42 "-----END CMS OBJECT-----\n";
43
44
45 static void
46 check_result (gpgme_verify_result_t result, int summary, char *fpr,
47               gpgme_error_t status, gpgme_validity_t validity)
48 {
49   gpgme_signature_t sig;
50
51   sig = result->signatures;
52   if (!sig || sig->next)
53     {
54       fprintf (stderr, "%s:%i: Unexpected number of signatures\n",
55                __FILE__, __LINE__);
56       exit (1);
57     }
58   if (sig->summary != summary)
59     {
60       fprintf (stderr, "%s:%i: Unexpected signature summary: 0x%x\n",
61                __FILE__, __LINE__, sig->summary);
62       exit (1);
63     }
64   if (strcmp (sig->fpr, fpr))
65     {
66       fprintf (stderr, "%s:%i: Unexpected fingerprint: %s\n",
67                __FILE__, __LINE__, sig->fpr);
68       exit (1);
69     }
70   if (gpg_err_code (sig->status) != status)
71     {
72       fprintf (stderr, "%s:%i: Unexpected signature status: %s\n",
73                __FILE__, __LINE__, gpgme_strerror (sig->status));
74       exit (1);
75     }
76   if (sig->notations)
77     {
78       fprintf (stderr, "%s:%i: Unexpected notation data\n",
79                __FILE__, __LINE__);
80       exit (1);
81     }
82   if (sig->wrong_key_usage)
83     {
84       fprintf (stderr, "%s:%i: Unexpectedly wrong key usage\n",
85                __FILE__, __LINE__);
86       exit (1);
87     }
88   if (sig->validity != validity)
89     {
90       fprintf (stderr, "%s:%i: Unexpected validity: %i\n",
91                __FILE__, __LINE__, sig->validity);
92       exit (1);
93     }
94   if (gpg_err_code (sig->validity_reason) != GPG_ERR_NO_ERROR)
95     {
96       fprintf (stderr, "%s:%i: Unexpected validity reason: %s\n",
97                __FILE__, __LINE__, gpgme_strerror (sig->validity_reason));
98       exit (1);
99     }
100 }
101
102
103 int 
104 main (int argc, char **argv)
105 {
106   gpgme_ctx_t ctx;
107   gpgme_error_t err;
108   gpgme_data_t sig, text;
109   gpgme_verify_result_t result;
110
111   init_gpgme (GPGME_PROTOCOL_CMS);
112
113   err = gpgme_new (&ctx);
114   fail_if_err (err);
115   gpgme_set_protocol (ctx, GPGME_PROTOCOL_CMS);
116   
117   /* Checking a valid message.  */
118   err = gpgme_data_new_from_mem (&text, test_text1, strlen (test_text1), 0);
119   fail_if_err (err);
120   err = gpgme_data_new_from_mem (&sig, test_sig1, strlen (test_sig1), 0);
121   fail_if_err (err);
122   err = gpgme_op_verify (ctx, sig, text, NULL);
123   fail_if_err (err);
124   result = gpgme_op_verify_result (ctx);
125   check_result (result, GPGME_SIGSUM_VALID | GPGME_SIGSUM_GREEN,
126                 "3CF405464F66ED4A7DF45BBDD1E4282E33BDB76E",
127                 GPG_ERR_NO_ERROR, GPGME_VALIDITY_FULL);
128
129   /* Checking a manipulated message.  */
130   gpgme_data_release (text);
131   err = gpgme_data_new_from_mem (&text, test_text1f, strlen (test_text1f), 0);
132   fail_if_err (err);
133   gpgme_data_seek (sig, 0, SEEK_SET);
134   err = gpgme_op_verify (ctx, sig, text, NULL);
135   fail_if_err (err);
136   result = gpgme_op_verify_result (ctx);
137   check_result (result, GPGME_SIGSUM_RED,
138                 "3CF405464F66ED4A7DF45BBDD1E4282E33BDB76E",
139                 GPG_ERR_BAD_SIGNATURE, GPGME_VALIDITY_UNKNOWN);
140
141   gpgme_release (ctx);  
142   return 0;
143 }