0f1cf33391fccd4b6be5e61189b43bb38995c6fd
[gpgme.git] / tests / gpgsm / t-keylist.c
1 /* t-keylist.c  - regression test
2    Copyright (C) 2000 Werner Koch (dd9jn)
3    Copyright (C) 2001, 2003, 2004 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 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, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20    02111-1307, USA.  */
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 <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31
32 #include <gpgme.h>
33
34 #include "t-support.h"
35
36 \f
37 struct
38 {
39   char *fpr;
40   int secret;
41   long timestamp;
42   long expires;
43   char *issuer_serial;
44   char *issuer_name;
45   char *chain_id;
46   char *uid;
47   char *email;
48   gpgme_validity_t validity;
49   unsigned int key_length;
50 }
51 keys[] =
52   {
53     { "3CF405464F66ED4A7DF45BBDD1E4282E33BDB76E", 1, 1007372198, 1038908198, "00",
54       "CN=test cert 1,OU=Aegypten Project,O=g10 Code GmbH,L=D\xc3\xbcsseldorf,C=DE",
55       "3CF405464F66ED4A7DF45BBDD1E4282E33BDB76E",
56       "CN=test cert 1,OU=Aegypten Project,O=g10 Code GmbH,L=D\xc3\xbcsseldorf,C=DE",
57       NULL, GPGME_VALIDITY_ULTIMATE, 1024
58     },
59     { "DFA56FB5FC41E3A8921F77AD1622EEFD9152A5AD", 0, 909684190, 1009821790, "01",
60       "1.2.840.113549.1.9.1=#63657274696679407063612E64666E2E6465,"
61       "CN=DFN Top Level Certification Authority,OU=DFN-PCA,"
62       "O=Deutsches Forschungsnetz,C=DE",
63       "DFA56FB5FC41E3A8921F77AD1622EEFD9152A5AD",
64       "1.2.840.113549.1.9.1=#63657274696679407063612E64666E2E6465,"
65       "CN=DFN Top Level Certification Authority,OU=DFN-PCA,"
66       "O=Deutsches Forschungsnetz,C=DE",
67       "<certify@pca.dfn.de>", GPGME_VALIDITY_NEVER, 2048
68     },
69     { "2C8F3C356AB761CB3674835B792CDA52937F9285", 0, 973183644, 1009735200, "15",
70       "1.2.840.113549.1.9.1=#63657274696679407063612E64666E2E6465,"
71       "CN=DFN Top Level Certification Authority,OU=DFN-PCA,"
72       "O=Deutsches Forschungsnetz,C=DE",
73       "DFA56FB5FC41E3A8921F77AD1622EEFD9152A5AD",
74       "1.2.840.113549.1.9.1=#63657274696679407063612E64666E2E6465,"
75       "CN=DFN Server Certification Authority,OU=DFN-PCA,"
76       "O=Deutsches Forschungsnetz,C=DE",
77       "<certify@pca.dfn.de>", GPGME_VALIDITY_UNKNOWN, 2048
78     },
79     { NULL }
80   };
81
82
83 int 
84 main (int argc, char **argv)
85 {
86   gpgme_error_t err;
87   gpgme_ctx_t ctx;
88   gpgme_key_t key;
89   gpgme_keylist_result_t result;
90   int i = 0;
91
92   init_gpgme (GPGME_PROTOCOL_CMS);
93
94   err = gpgme_new (&ctx);
95   fail_if_err (err);
96   gpgme_set_protocol (ctx, GPGME_PROTOCOL_CMS);
97
98   err = gpgme_op_keylist_start (ctx, NULL, 0);
99   fail_if_err (err);
100     
101   while (!(err = gpgme_op_keylist_next (ctx, &key)))
102     {
103       if (!keys[i].fpr)
104         {
105           fprintf (stderr, "More keys returned than expected\n");
106           exit (1);
107         }
108
109       if (strcmp (key->subkeys->fpr, keys[i].fpr))
110         {
111           fprintf (stderr, "Warning: Skipping unknown key %s\n",
112                    key->subkeys->fpr);
113           continue;
114         }
115       else
116         printf ("Checking key %s\n", key->subkeys->fpr);
117
118       /* Global key flags.  */
119       if (key->revoked)
120         {
121           fprintf (stderr, "Key unexpectedly revoked\n");
122           exit (1);
123         }
124       if (key->expired)
125         {
126           fprintf (stderr, "Key unexpectedly expired\n");
127           exit (1);
128         }
129       if (key->disabled)
130         {
131           fprintf (stderr, "Key unexpectedly disabled\n");
132           exit (1);
133         }
134       if (key->invalid)
135         {
136           fprintf (stderr, "Key unexpectedly invalid\n");
137           exit (1);
138         }
139       if (key->can_encrypt != keys[i].secret)
140         {
141           fprintf (stderr, "Key unexpectedly%s usable for encryption\n",
142                    key->can_encrypt ? "" : " not");
143           exit (1);
144         }
145       if (key->can_sign != keys[i].secret)
146         {
147           fprintf (stderr, "Key unexpectedly%s usable for signing\n",
148                    key->can_sign ? "" : " not");
149           exit (1);
150         }
151       if (!key->can_certify)
152         {
153           fprintf (stderr, "Key unexpectedly unusable for certifications\n");
154           exit (1);
155         }
156       if (key->secret != keys[i].secret)
157         {
158           fprintf (stderr, "Key unexpectedly%s secret\n",
159                    key->secret ? "" : " not");
160           exit (1);
161         }
162       if (key->protocol != GPGME_PROTOCOL_CMS)
163         {
164           fprintf (stderr, "Key has unexpected protocol: %s\n",
165                    gpgme_get_protocol_name (key->protocol));
166           exit (1);
167         }
168       if (!key->issuer_serial)
169         {
170           fprintf (stderr, "Key unexpectedly misses issuer serial\n");
171           exit (1);
172         }
173       if (strcmp (key->issuer_serial, keys[i].issuer_serial))
174         {
175           fprintf (stderr, "Key has unexpected issuer serial: %s\n",
176                    key->issuer_serial);
177           exit (1);
178         }
179       if (!key->issuer_name)
180         {
181           fprintf (stderr, "Key unexpectedly misses issuer name\n");
182           exit (1);
183         }
184       if (strcmp (key->issuer_name, keys[i].issuer_name))
185         {
186           fprintf (stderr, "Key has unexpected issuer name: %s\n",
187                    key->issuer_name);
188           exit (1);
189         }
190       if (key->chain_id && !keys[i].chain_id)
191         {
192           fprintf (stderr, "Key unexpectedly carries chain ID: %s\n",
193                    key->chain_id);
194           exit (1);
195         }
196       if (!key->chain_id && keys[i].chain_id)
197         {
198           fprintf (stderr, "Key unexpectedly carries no chain ID\n");
199           exit (1);
200         }
201       if (key->chain_id && strcmp (key->chain_id, keys[i].chain_id))
202         {
203           fprintf (stderr, "Key carries unexpected chain ID: %s\n",
204                    key->chain_id);
205           exit (1);
206         }
207       if (key->owner_trust != GPGME_VALIDITY_UNKNOWN)
208         {
209           fprintf (stderr, "Key has unexpected owner trust: %i\n",
210                    key->owner_trust);
211           exit (1);
212         }
213       if (!key->subkeys || key->subkeys->next)
214         {
215           fprintf (stderr, "Key has unexpected number of subkeys\n");
216           exit (1);
217         }
218
219       /* Primary key.  */
220       if (key->subkeys->revoked)
221         {
222           fprintf (stderr, "Primary key unexpectedly revoked\n");
223           exit (1);
224         }
225       if (key->subkeys->expired)
226         {
227           fprintf (stderr, "Primary key unexpectedly expired\n");
228           exit (1);
229         }
230       if (key->subkeys->disabled)
231         {
232           fprintf (stderr, "Primary key unexpectedly disabled\n");
233           exit (1);
234         }
235       if (key->subkeys->invalid)
236         {
237           fprintf (stderr, "Primary key unexpectedly invalid\n");
238           exit (1);
239         }
240       if (key->subkeys->can_encrypt != keys[i].secret)
241         {
242           fprintf (stderr, "Key unexpectedly%s usable for encryption\n",
243                    key->subkeys->can_encrypt ? "" : " not");
244           exit (1);
245         }
246       if (key->subkeys->can_sign != keys[i].secret)
247         {
248           fprintf (stderr, "Key unexpectedly%s usable for signing\n",
249                    key->subkeys->can_sign ? "" : " not");
250           exit (1);
251         }
252       if (!key->subkeys->can_certify)
253         {
254           fprintf (stderr, "Primary key unexpectedly unusable for certifications\n");
255           exit (1);
256         }
257       if (key->subkeys->secret != keys[i].secret)
258         {
259           fprintf (stderr, "Primary Key unexpectedly%s secret\n",
260                    key->secret ? "" : " not");
261           exit (1);
262         }
263       if (key->subkeys->pubkey_algo != GPGME_PK_RSA)
264         {
265           fprintf (stderr, "Primary key has unexpected public key algo: %s\n",
266                    gpgme_pubkey_algo_name (key->subkeys->pubkey_algo));
267           exit (1);
268         }
269       if (key->subkeys->length != keys[i].key_length)
270         {
271           fprintf (stderr, "Primary key has unexpected length: %i\n",
272                    key->subkeys->length);
273           exit (1);
274         }
275       if (strcmp (key->subkeys->keyid, &keys[i].fpr[40 - 16]))
276         {
277           fprintf (stderr, "Primary key has unexpected key ID: %s\n",
278                    key->subkeys->keyid);
279           exit (1);
280         }
281       if (strcmp (key->subkeys->fpr, keys[i].fpr))
282         {
283           fprintf (stderr, "Primary key has unexpected fingerprint: %s\n",
284                    key->subkeys->fpr);
285           exit (1);
286         }
287       if (key->subkeys->timestamp != keys[i].timestamp)
288         {
289           fprintf (stderr, "Primary key unexpected timestamp: %lu\n",
290                    key->subkeys->timestamp);
291           exit (1);
292         }
293       if (key->subkeys->expires != keys[i].expires)
294         {
295           fprintf (stderr, "Primary key unexpectedly expires: %lu\n",
296                    key->subkeys->expires);
297           exit (1);
298         }
299
300       /* Be tolerant against a missing email (ie, older gpgsm versions).  */
301       if (!key->uids || (key->uids->next && !keys[i].email))
302         {
303           fprintf (stderr, "Key has unexpected number of user IDs\n");
304           exit (1);
305         }
306       if (key->uids->revoked)
307         {
308           fprintf (stderr, "User ID unexpectedly revoked\n");
309           exit (1);
310         }
311       if (key->uids->invalid)
312         {
313           fprintf (stderr, "User ID unexpectedly invalid\n");
314           exit (1);
315         }
316       if (key->uids->validity != keys[i].validity)
317         {
318           fprintf (stderr, "User ID unexpectedly validity: %i\n",
319                    key->uids->validity);
320           exit (1);
321         }
322       if (key->uids->signatures)
323         {
324           fprintf (stderr, "User ID unexpectedly signed\n");
325           exit (1);
326         }
327       if (!key->uids->name || key->uids->name[0])
328         {
329           fprintf (stderr, "Unexpected name in user ID: %s\n",
330                    key->uids->name);
331           exit (1);
332         }
333       if (!key->uids->comment || key->uids->comment[0])
334         {
335           fprintf (stderr, "Unexpected comment in user ID: %s\n",
336                    key->uids->comment);
337           exit (1);
338         }
339       if (!key->uids->email || key->uids->email[0])
340         {
341           fprintf (stderr, "Unexpected email in user ID: %s\n",
342                    key->uids->email);
343           exit (1);
344         }
345       if (!key->uids->uid || strcmp (key->uids->uid, keys[i].uid))
346         {
347           fprintf (stderr, "Unexpected uid in user ID: %s\n",
348                    key->uids->uid);
349           exit (1);
350         }
351       if (key->uids->next && strcmp (key->uids->next->uid, keys[i].email))
352         {
353           fprintf (stderr, "Unexpected email in user ID: %s\n",
354                    key->uids->next->uid);
355           exit (1);
356         }
357       if (key->uids->next && strcmp (key->uids->next->uid, keys[i].email))
358         {
359           fprintf (stderr, "Unexpected email in user ID: %s\n",
360                    key->uids->next->uid);
361           exit (1);
362         }
363
364
365
366       gpgme_key_unref (key);
367       i++;
368     }
369   if (gpg_err_code (err) != GPG_ERR_EOF)
370     fail_if_err (err);
371   err = gpgme_op_keylist_end (ctx);
372   fail_if_err (err);
373
374   result = gpgme_op_keylist_result (ctx);
375   if (result->truncated)
376     {
377       fprintf (stderr, "Key listing unexpectedly truncated\n");
378       exit (1);
379     }
380
381   if (keys[i].fpr)
382     {
383       fprintf (stderr, "Less keys returned than expected\n");
384       exit (1);
385     }
386
387   gpgme_release (ctx);
388   return 0;
389 }