* Fixed a bug in that the fingerprints of subkeys are not available.
[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       /* Global key flags.  */
110       if (key->revoked)
111         {
112           fprintf (stderr, "Key unexpectedly revoked\n");
113           exit (1);
114         }
115       if (key->expired)
116         {
117           fprintf (stderr, "Key unexpectedly expired\n");
118           exit (1);
119         }
120       if (key->disabled)
121         {
122           fprintf (stderr, "Key unexpectedly disabled\n");
123           exit (1);
124         }
125       if (key->invalid)
126         {
127           fprintf (stderr, "Key unexpectedly invalid\n");
128           exit (1);
129         }
130       if (key->can_encrypt != keys[i].secret)
131         {
132           fprintf (stderr, "Key unexpectedly%s usable for encryption\n",
133                    key->can_encrypt ? "" : " not");
134           exit (1);
135         }
136       if (key->can_sign != keys[i].secret)
137         {
138           fprintf (stderr, "Key unexpectedly%s usable for signing\n",
139                    key->can_sign ? "" : " not");
140           exit (1);
141         }
142       if (!key->can_certify)
143         {
144           fprintf (stderr, "Key unexpectedly unusable for certifications\n");
145           exit (1);
146         }
147       if (key->secret != keys[i].secret)
148         {
149           fprintf (stderr, "Key unexpectedly%s secret\n",
150                    key->secret ? "" : " not");
151           exit (1);
152         }
153       if (key->protocol != GPGME_PROTOCOL_CMS)
154         {
155           fprintf (stderr, "Key has unexpected protocol: %s\n",
156                    gpgme_get_protocol_name (key->protocol));
157           exit (1);
158         }
159       if (!key->issuer_serial)
160         {
161           fprintf (stderr, "Key unexpectedly misses issuer serial\n");
162           exit (1);
163         }
164       if (strcmp (key->issuer_serial, keys[i].issuer_serial))
165         {
166           fprintf (stderr, "Key has unexpected issuer serial: %s\n",
167                    key->issuer_serial);
168           exit (1);
169         }
170       if (!key->issuer_name)
171         {
172           fprintf (stderr, "Key unexpectedly misses issuer name\n");
173           exit (1);
174         }
175       if (strcmp (key->issuer_name, keys[i].issuer_name))
176         {
177           fprintf (stderr, "Key has unexpected issuer name: %s\n",
178                    key->issuer_name);
179           exit (1);
180         }
181       if (key->chain_id && !keys[i].chain_id)
182         {
183           fprintf (stderr, "Key unexpectedly carries chain ID: %s\n",
184                    key->chain_id);
185           exit (1);
186         }
187       if (!key->chain_id && keys[i].chain_id)
188         {
189           fprintf (stderr, "Key unexpectedly carries no chain ID\n");
190           exit (1);
191         }
192       if (key->chain_id && strcmp (key->chain_id, keys[i].chain_id))
193         {
194           fprintf (stderr, "Key carries unexpected chain ID: %s\n",
195                    key->chain_id);
196           exit (1);
197         }
198       if (key->owner_trust != GPGME_VALIDITY_UNKNOWN)
199         {
200           fprintf (stderr, "Key has unexpected owner trust: %i\n",
201                    key->owner_trust);
202           exit (1);
203         }
204       if (!key->subkeys || key->subkeys->next)
205         {
206           fprintf (stderr, "Key has unexpected number of subkeys\n");
207           exit (1);
208         }
209
210       /* Primary key.  */
211       if (key->subkeys->revoked)
212         {
213           fprintf (stderr, "Primary key unexpectedly revoked\n");
214           exit (1);
215         }
216       if (key->subkeys->expired)
217         {
218           fprintf (stderr, "Primary key unexpectedly expired\n");
219           exit (1);
220         }
221       if (key->subkeys->disabled)
222         {
223           fprintf (stderr, "Primary key unexpectedly disabled\n");
224           exit (1);
225         }
226       if (key->subkeys->invalid)
227         {
228           fprintf (stderr, "Primary key unexpectedly invalid\n");
229           exit (1);
230         }
231       if (key->subkeys->can_encrypt != keys[i].secret)
232         {
233           fprintf (stderr, "Key unexpectedly%s usable for encryption\n",
234                    key->subkeys->can_encrypt ? "" : " not");
235           exit (1);
236         }
237       if (key->subkeys->can_sign != keys[i].secret)
238         {
239           fprintf (stderr, "Key unexpectedly%s usable for signing\n",
240                    key->subkeys->can_sign ? "" : " not");
241           exit (1);
242         }
243       if (!key->subkeys->can_certify)
244         {
245           fprintf (stderr, "Primary key unexpectedly unusable for certifications\n");
246           exit (1);
247         }
248       if (key->subkeys->secret != keys[i].secret)
249         {
250           fprintf (stderr, "Primary Key unexpectedly%s secret\n",
251                    key->secret ? "" : " not");
252           exit (1);
253         }
254       if (key->subkeys->pubkey_algo != GPGME_PK_RSA)
255         {
256           fprintf (stderr, "Primary key has unexpected public key algo: %s\n",
257                    gpgme_pubkey_algo_name (key->subkeys->pubkey_algo));
258           exit (1);
259         }
260       if (key->subkeys->length != keys[i].key_length)
261         {
262           fprintf (stderr, "Primary key has unexpected length: %i\n",
263                    key->subkeys->length);
264           exit (1);
265         }
266       if (strcmp (key->subkeys->keyid, &keys[i].fpr[40 - 16]))
267         {
268           fprintf (stderr, "Primary key has unexpected key ID: %s\n",
269                    key->subkeys->keyid);
270           exit (1);
271         }
272       if (strcmp (key->subkeys->fpr, keys[i].fpr))
273         {
274           fprintf (stderr, "Primary key has unexpected fingerprint: %s\n",
275                    key->subkeys->fpr);
276           exit (1);
277         }
278       if (key->subkeys->timestamp != keys[i].timestamp)
279         {
280           fprintf (stderr, "Primary key unexpected timestamp: %lu\n",
281                    key->subkeys->timestamp);
282           exit (1);
283         }
284       if (key->subkeys->expires != keys[i].expires)
285         {
286           fprintf (stderr, "Primary key unexpectedly expires: %lu\n",
287                    key->subkeys->expires);
288           exit (1);
289         }
290
291       /* Be tolerant against a missing email (ie, older gpgsm versions).  */
292       if (!key->uids || (key->uids->next && !keys[i].email))
293         {
294           fprintf (stderr, "Key has unexpected number of user IDs\n");
295           exit (1);
296         }
297       if (key->uids->revoked)
298         {
299           fprintf (stderr, "User ID unexpectedly revoked\n");
300           exit (1);
301         }
302       if (key->uids->invalid)
303         {
304           fprintf (stderr, "User ID unexpectedly invalid\n");
305           exit (1);
306         }
307       if (key->uids->validity != keys[i].validity)
308         {
309           fprintf (stderr, "User ID unexpectedly validity: %i\n",
310                    key->uids->validity);
311           exit (1);
312         }
313       if (key->uids->signatures)
314         {
315           fprintf (stderr, "User ID unexpectedly signed\n");
316           exit (1);
317         }
318       if (!key->uids->name || key->uids->name[0])
319         {
320           fprintf (stderr, "Unexpected name in user ID: %s\n",
321                    key->uids->name);
322           exit (1);
323         }
324       if (!key->uids->comment || key->uids->comment[0])
325         {
326           fprintf (stderr, "Unexpected comment in user ID: %s\n",
327                    key->uids->comment);
328           exit (1);
329         }
330       if (!key->uids->email || key->uids->email[0])
331         {
332           fprintf (stderr, "Unexpected email in user ID: %s\n",
333                    key->uids->email);
334           exit (1);
335         }
336       if (!key->uids->uid || strcmp (key->uids->uid, keys[i].uid))
337         {
338           fprintf (stderr, "Unexpected uid in user ID: %s\n",
339                    key->uids->uid);
340           exit (1);
341         }
342       if (key->uids->next && strcmp (key->uids->next->uid, keys[i].email))
343         {
344           fprintf (stderr, "Unexpected email in user ID: %s\n",
345                    key->uids->next->uid);
346           exit (1);
347         }
348
349
350       gpgme_key_unref (key);
351       i++;
352     }
353   if (gpg_err_code (err) != GPG_ERR_EOF)
354     fail_if_err (err);
355   err = gpgme_op_keylist_end (ctx);
356   fail_if_err (err);
357
358   result = gpgme_op_keylist_result (ctx);
359   if (result->truncated)
360     {
361       fprintf (stderr, "Key listing unexpectedly truncated\n");
362       exit (1);
363     }
364
365   if (keys[i].fpr)
366     {
367       fprintf (stderr, "Less keys returned than expected\n");
368       exit (1);
369     }
370
371   gpgme_release (ctx);
372   return 0;
373 }