1 /* keydb.c - key database dispatcher
2 * Copyright (C) 2001, 2003, 2004 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
26 #include <sys/types.h>
31 #include "../kbx/keybox.h"
35 static int active_handles
;
38 KEYDB_RESOURCE_TYPE_NONE
= 0,
39 KEYDB_RESOURCE_TYPE_KEYBOX
41 #define MAX_KEYDB_RESOURCES 20
43 struct resource_item
{
44 KeydbResourceType type
;
53 static struct resource_item all_resources
[MAX_KEYDB_RESOURCES
];
54 static int used_resources
;
61 int used
; /* items in active */
62 struct resource_item active
[MAX_KEYDB_RESOURCES
];
66 static int lock_all (KEYDB_HANDLE hd
);
67 static void unlock_all (KEYDB_HANDLE hd
);
71 * Register a resource (which currently may only be a keybox file).
72 * The first keybox which is added by this function is created if it
73 * does not exist. If AUTO_CREATED is not NULL it will be set to true
74 * if the function has created a a new keybox.
77 keydb_add_resource (const char *url
, int force
, int secret
, int *auto_created
)
79 static int any_secret
, any_public
;
80 const char *resname
= url
;
81 char *filename
= NULL
;
84 KeydbResourceType rt
= KEYDB_RESOURCE_TYPE_NONE
;
85 const char *created_fname
= NULL
;
91 gnupg-kbx:filename := this is a plain keybox
92 filename := See what is is, but create as plain keybox.
94 if (strlen (resname
) > 10)
96 if (!strncmp (resname
, "gnupg-kbx:", 10) )
98 rt
= KEYDB_RESOURCE_TYPE_KEYBOX
;
101 #if !defined(HAVE_DRIVE_LETTERS) && !defined(__riscos__)
102 else if (strchr (resname
, ':'))
104 log_error ("invalid key resource URL `%s'\n", url
);
105 rc
= gpg_error (GPG_ERR_GENERAL
);
108 #endif /* !HAVE_DRIVE_LETTERS && !__riscos__ */
111 if (*resname
!= DIRSEP_C
)
112 { /* do tilde expansion etc */
113 if (strchr(resname
, DIRSEP_C
) )
114 filename
= make_filename (resname
, NULL
);
116 filename
= make_filename (opt
.homedir
, resname
, NULL
);
119 filename
= xstrdup (resname
);
122 force
= secret
? !any_secret
: !any_public
;
124 /* see whether we can determine the filetype */
125 if (rt
== KEYDB_RESOURCE_TYPE_NONE
)
127 FILE *fp2
= fopen( filename
, "rb" );
132 /* FIXME: check for the keybox magic */
133 if (fread( &magic
, 4, 1, fp2
) == 1 )
135 if (magic
== 0x13579ace || magic
== 0xce9a5713)
136 ; /* GDBM magic - no more support */
138 rt
= KEYDB_RESOURCE_TYPE_KEYBOX
;
140 else /* maybe empty: assume ring */
141 rt
= KEYDB_RESOURCE_TYPE_KEYBOX
;
144 else /* no file yet: create ring */
145 rt
= KEYDB_RESOURCE_TYPE_KEYBOX
;
150 case KEYDB_RESOURCE_TYPE_NONE
:
151 log_error ("unknown type of key resource `%s'\n", url
);
152 rc
= gpg_error (GPG_ERR_GENERAL
);
155 case KEYDB_RESOURCE_TYPE_KEYBOX
:
156 fp
= fopen (filename
, "rb");
159 rc
= gpg_error (gpg_err_code_from_errno (errno
));
165 #if 0 /* no autocreate of the homedirectory yet */
167 char *last_slash_in_filename
;
169 last_slash_in_filename
= strrchr (filename
, DIRSEP_C
);
170 *last_slash_in_filename
= 0;
171 if (access (filename
, F_OK
))
172 { /* on the first time we try to create the default
173 homedir and in this case the process will be
174 terminated, so that on the next invocation can
175 read the options file in on startup */
176 try_make_homedir (filename
);
177 rc
= gpg_error (GPG_ERR_FILE_OPEN_ERROR
);
178 *last_slash_in_filename
= DIRSEP_C
;
181 *last_slash_in_filename
= DIRSEP_C
;
184 fp
= fopen (filename
, "w");
187 rc
= gpg_error (gpg_err_code_from_errno (errno
));
188 log_error (_("error creating keybox `%s': %s\n"),
189 filename
, strerror(errno
));
191 log_info (_("you may want to start the gpg-agent first\n"));
196 log_info (_("keybox `%s' created\n"), filename
);
197 created_fname
= filename
;
203 /* now register the file */
206 void *token
= keybox_register_file (filename
, secret
);
208 ; /* already registered - ignore it */
209 else if (used_resources
>= MAX_KEYDB_RESOURCES
)
210 rc
= gpg_error (GPG_ERR_RESOURCE_LIMIT
);
213 all_resources
[used_resources
].type
= rt
;
214 all_resources
[used_resources
].u
.kr
= NULL
; /* Not used here */
215 all_resources
[used_resources
].token
= token
;
216 all_resources
[used_resources
].secret
= secret
;
218 all_resources
[used_resources
].lockhandle
219 = create_dotlock (filename
);
220 if (!all_resources
[used_resources
].lockhandle
)
221 log_fatal ( _("can't create lock for `%s'\n"), filename
);
223 /* Do a compress run if needed and the file is not locked. */
224 if (!make_dotlock (all_resources
[used_resources
].lockhandle
, 0))
226 KEYBOX_HANDLE kbxhd
= keybox_new (token
, secret
);
230 keybox_compress (kbxhd
);
231 keybox_release (kbxhd
);
233 release_dotlock (all_resources
[used_resources
].lockhandle
);
243 log_error ("resource type of `%s' not supported\n", url
);
244 rc
= gpg_error (GPG_ERR_NOT_SUPPORTED
);
248 /* fixme: check directory permissions and print a warning */
252 log_error ("keyblock resource `%s': %s\n", filename
, gpg_strerror(rc
));
263 keydb_new (int secret
)
268 hd
= xcalloc (1, sizeof *hd
);
271 assert (used_resources
<= MAX_KEYDB_RESOURCES
);
272 for (i
=j
=0; i
< used_resources
; i
++)
274 if (!all_resources
[i
].secret
!= !secret
)
276 switch (all_resources
[i
].type
)
278 case KEYDB_RESOURCE_TYPE_NONE
: /* ignore */
280 case KEYDB_RESOURCE_TYPE_KEYBOX
:
281 hd
->active
[j
].type
= all_resources
[i
].type
;
282 hd
->active
[j
].token
= all_resources
[i
].token
;
283 hd
->active
[j
].secret
= all_resources
[i
].secret
;
284 hd
->active
[j
].lockhandle
= all_resources
[i
].lockhandle
;
285 hd
->active
[j
].u
.kr
= keybox_new (all_resources
[i
].token
, secret
);
286 if (!hd
->active
[j
].u
.kr
)
289 return NULL
; /* fixme: release all previously allocated handles*/
302 keydb_release (KEYDB_HANDLE hd
)
308 assert (active_handles
> 0);
312 for (i
=0; i
< hd
->used
; i
++)
314 switch (hd
->active
[i
].type
)
316 case KEYDB_RESOURCE_TYPE_NONE
:
318 case KEYDB_RESOURCE_TYPE_KEYBOX
:
319 keybox_release (hd
->active
[i
].u
.kr
);
328 /* Return the name of the current resource. This is function first
329 looks for the last found found, then for the current search
330 position, and last returns the first available resource. The
331 returned string is only valid as long as the handle exists. This
332 function does only return NULL if no handle is specified, in all
333 other error cases an empty string is returned. */
335 keydb_get_resource_name (KEYDB_HANDLE hd
)
338 const char *s
= NULL
;
343 if ( hd
->found
>= 0 && hd
->found
< hd
->used
)
345 else if ( hd
->current
>= 0 && hd
->current
< hd
->used
)
350 switch (hd
->active
[idx
].type
)
352 case KEYDB_RESOURCE_TYPE_NONE
:
355 case KEYDB_RESOURCE_TYPE_KEYBOX
:
356 s
= keybox_get_resource_name (hd
->active
[idx
].u
.kr
);
363 /* Switch the handle into ephemeral mode and return the orginal value. */
365 keydb_set_ephemeral (KEYDB_HANDLE hd
, int yes
)
373 if (hd
->is_ephemeral
!= yes
)
375 for (i
=0; i
< hd
->used
; i
++)
377 switch (hd
->active
[i
].type
)
379 case KEYDB_RESOURCE_TYPE_NONE
:
381 case KEYDB_RESOURCE_TYPE_KEYBOX
:
382 keybox_set_ephemeral (hd
->active
[i
].u
.kr
, yes
);
388 i
= hd
->is_ephemeral
;
389 hd
->is_ephemeral
= yes
;
394 /* If the keyring has not yet been locked, lock it now. This
395 operation is required before any update opeations; it is optionaly
396 for an insert operation. The lock is released with
399 keydb_lock (KEYDB_HANDLE hd
)
402 return gpg_error (GPG_ERR_INV_HANDLE
);
404 return 0; /* Already locked. */
405 return lock_all (hd
);
411 lock_all (KEYDB_HANDLE hd
)
415 /* Fixme: This locking scheme may lead to deadlock if the resources
416 are not added in the same order by all processes. We are
417 currently only allowing one resource so it is not a problem. */
418 for (i
=0; i
< hd
->used
; i
++)
420 switch (hd
->active
[i
].type
)
422 case KEYDB_RESOURCE_TYPE_NONE
:
424 case KEYDB_RESOURCE_TYPE_KEYBOX
:
425 if (hd
->active
[i
].lockhandle
)
426 rc
= make_dotlock (hd
->active
[i
].lockhandle
, -1);
435 /* revert the already set locks */
436 for (i
--; i
>= 0; i
--)
438 switch (hd
->active
[i
].type
)
440 case KEYDB_RESOURCE_TYPE_NONE
:
442 case KEYDB_RESOURCE_TYPE_KEYBOX
:
443 if (hd
->active
[i
].lockhandle
)
444 release_dotlock (hd
->active
[i
].lockhandle
);
452 /* make_dotlock () does not yet guarantee that errno is set, thus
453 we can't rely on the error reason and will simply use
455 return rc
? gpg_error (GPG_ERR_EACCES
) : 0;
459 unlock_all (KEYDB_HANDLE hd
)
466 for (i
=hd
->used
-1; i
>= 0; i
--)
468 switch (hd
->active
[i
].type
)
470 case KEYDB_RESOURCE_TYPE_NONE
:
472 case KEYDB_RESOURCE_TYPE_KEYBOX
:
473 if (hd
->active
[i
].lockhandle
)
474 release_dotlock (hd
->active
[i
].lockhandle
);
484 * Return the last found keybox. Caller must free it.
485 * The returned keyblock has the kbode flag bit 0 set for the node with
486 * the public key used to locate the keyblock or flag bit 1 set for
490 keydb_get_keyblock (KEYDB_HANDLE hd
, KBNODE
*ret_kb
)
495 return G10ERR_INV_ARG
;
497 if ( hd
->found
< 0 || hd
->found
>= hd
->used
)
498 return -1; /* nothing found */
500 switch (hd
->active
[hd
->found
].type
) {
501 case KEYDB_RESOURCE_TYPE_NONE
:
502 rc
= G10ERR_GENERAL
; /* oops */
504 case KEYDB_RESOURCE_TYPE_KEYBOX
:
505 rc
= keybox_get_keyblock (hd
->active
[hd
->found
].u
.kr
, ret_kb
);
513 * update the current keyblock with KB
516 keydb_update_keyblock (KEYDB_HANDLE hd
, KBNODE kb
)
521 return G10ERR_INV_ARG
;
523 if ( hd
->found
< 0 || hd
->found
>= hd
->used
)
524 return -1; /* nothing found */
530 return gpg_error (GPG_ERR_NOT_LOCKED
);
532 switch (hd
->active
[hd
->found
].type
) {
533 case KEYDB_RESOURCE_TYPE_NONE
:
534 rc
= G10ERR_GENERAL
; /* oops */
536 case KEYDB_RESOURCE_TYPE_KEYBOX
:
537 rc
= keybox_update_keyblock (hd
->active
[hd
->found
].u
.kr
, kb
);
547 * Insert a new KB into one of the resources.
550 keydb_insert_keyblock (KEYDB_HANDLE hd
, KBNODE kb
)
556 return G10ERR_INV_ARG
;
561 if ( hd
->found
>= 0 && hd
->found
< hd
->used
)
563 else if ( hd
->current
>= 0 && hd
->current
< hd
->used
)
566 return G10ERR_GENERAL
;
572 switch (hd
->active
[idx
].type
) {
573 case KEYDB_RESOURCE_TYPE_NONE
:
574 rc
= G10ERR_GENERAL
; /* oops */
576 case KEYDB_RESOURCE_TYPE_KEYBOX
:
577 rc
= keybox_insert_keyblock (hd
->active
[idx
].u
.kr
, kb
);
585 #endif /*disabled code*/
590 Return the last found object. Caller must free it. The returned
591 keyblock has the kbode flag bit 0 set for the node with the public
592 key used to locate the keyblock or flag bit 1 set for the user ID
595 keydb_get_cert (KEYDB_HANDLE hd
, ksba_cert_t
*r_cert
)
600 return gpg_error (GPG_ERR_INV_VALUE
);
602 if ( hd
->found
< 0 || hd
->found
>= hd
->used
)
603 return -1; /* nothing found */
605 switch (hd
->active
[hd
->found
].type
)
607 case KEYDB_RESOURCE_TYPE_NONE
:
608 rc
= gpg_error (GPG_ERR_GENERAL
); /* oops */
610 case KEYDB_RESOURCE_TYPE_KEYBOX
:
611 rc
= keybox_get_cert (hd
->active
[hd
->found
].u
.kr
, r_cert
);
618 /* Return a flag of the last found object. WHICH is the flag requested;
619 it should be one of the KEYBOX_FLAG_ values. If the operation is
620 successful, the flag value will be stored at the address given by
621 VALUE. Return 0 on success or an error code. */
623 keydb_get_flags (KEYDB_HANDLE hd
, int which
, int idx
, unsigned int *value
)
628 return gpg_error (GPG_ERR_INV_VALUE
);
630 if ( hd
->found
< 0 || hd
->found
>= hd
->used
)
631 return gpg_error (GPG_ERR_NOTHING_FOUND
);
633 switch (hd
->active
[hd
->found
].type
)
635 case KEYDB_RESOURCE_TYPE_NONE
:
636 err
= gpg_error (GPG_ERR_GENERAL
); /* oops */
638 case KEYDB_RESOURCE_TYPE_KEYBOX
:
639 err
= keybox_get_flags (hd
->active
[hd
->found
].u
.kr
, which
, idx
, value
);
646 /* Set a flag of the last found object. WHICH is the flag to be set; it
647 should be one of the KEYBOX_FLAG_ values. If the operation is
648 successful, the flag value will be stored in the keybox. Note,
649 that some flag values can't be updated and thus may return an
650 error, some other flag values may be masked out before an update.
651 Returns 0 on success or an error code. */
653 keydb_set_flags (KEYDB_HANDLE hd
, int which
, int idx
, unsigned int value
)
658 return gpg_error (GPG_ERR_INV_VALUE
);
660 if ( hd
->found
< 0 || hd
->found
>= hd
->used
)
661 return gpg_error (GPG_ERR_NOTHING_FOUND
);
664 return gpg_error (GPG_ERR_NOT_LOCKED
);
666 switch (hd
->active
[hd
->found
].type
)
668 case KEYDB_RESOURCE_TYPE_NONE
:
669 err
= gpg_error (GPG_ERR_GENERAL
); /* oops */
671 case KEYDB_RESOURCE_TYPE_KEYBOX
:
672 err
= keybox_set_flags (hd
->active
[hd
->found
].u
.kr
, which
, idx
, value
);
680 * Insert a new Certificate into one of the resources.
683 keydb_insert_cert (KEYDB_HANDLE hd
, ksba_cert_t cert
)
687 unsigned char digest
[20];
690 return gpg_error (GPG_ERR_INV_VALUE
);
695 if ( hd
->found
>= 0 && hd
->found
< hd
->used
)
697 else if ( hd
->current
>= 0 && hd
->current
< hd
->used
)
700 return gpg_error (GPG_ERR_GENERAL
);
703 return gpg_error (GPG_ERR_NOT_LOCKED
);
705 gpgsm_get_fingerprint (cert
, GCRY_MD_SHA1
, digest
, NULL
); /* kludge*/
707 switch (hd
->active
[idx
].type
)
709 case KEYDB_RESOURCE_TYPE_NONE
:
710 rc
= gpg_error (GPG_ERR_GENERAL
);
712 case KEYDB_RESOURCE_TYPE_KEYBOX
:
713 rc
= keybox_insert_cert (hd
->active
[idx
].u
.kr
, cert
, digest
);
723 /* Update the current keyblock with KB. */
725 keydb_update_cert (KEYDB_HANDLE hd
, ksba_cert_t cert
)
728 unsigned char digest
[20];
731 return gpg_error (GPG_ERR_INV_VALUE
);
733 if ( hd
->found
< 0 || hd
->found
>= hd
->used
)
734 return -1; /* nothing found */
743 gpgsm_get_fingerprint (cert
, GCRY_MD_SHA1
, digest
, NULL
); /* kludge*/
745 switch (hd
->active
[hd
->found
].type
)
747 case KEYDB_RESOURCE_TYPE_NONE
:
748 rc
= gpg_error (GPG_ERR_GENERAL
); /* oops */
750 case KEYDB_RESOURCE_TYPE_KEYBOX
:
751 rc
= keybox_update_cert (hd
->active
[hd
->found
].u
.kr
, cert
, digest
);
761 * The current keyblock or cert will be deleted.
764 keydb_delete (KEYDB_HANDLE hd
, int unlock
)
769 return gpg_error (GPG_ERR_INV_VALUE
);
771 if ( hd
->found
< 0 || hd
->found
>= hd
->used
)
772 return -1; /* nothing found */
778 return gpg_error (GPG_ERR_NOT_LOCKED
);
780 switch (hd
->active
[hd
->found
].type
)
782 case KEYDB_RESOURCE_TYPE_NONE
:
783 rc
= gpg_error (GPG_ERR_GENERAL
);
785 case KEYDB_RESOURCE_TYPE_KEYBOX
:
786 rc
= keybox_delete (hd
->active
[hd
->found
].u
.kr
);
798 * Locate the default writable key resource, so that the next
799 * operation (which is only relevant for inserts) will be done on this
803 keydb_locate_writable (KEYDB_HANDLE hd
, const char *reserved
)
808 return gpg_error (GPG_ERR_INV_VALUE
);
810 rc
= keydb_search_reset (hd
); /* this does reset hd->current */
814 for ( ; hd
->current
>= 0 && hd
->current
< hd
->used
; hd
->current
++)
816 switch (hd
->active
[hd
->current
].type
)
818 case KEYDB_RESOURCE_TYPE_NONE
:
821 case KEYDB_RESOURCE_TYPE_KEYBOX
:
822 if (keybox_is_writable (hd
->active
[hd
->current
].token
))
823 return 0; /* found (hd->current is set to it) */
832 * Rebuild the caches of all key resources.
835 keydb_rebuild_caches (void)
839 for (i
=0; i
< used_resources
; i
++)
841 if (all_resources
[i
].secret
)
843 switch (all_resources
[i
].type
)
845 case KEYDB_RESOURCE_TYPE_NONE
: /* ignore */
847 case KEYDB_RESOURCE_TYPE_KEYBOX
:
848 /* rc = keybox_rebuild_cache (all_resources[i].token); */
850 /* log_error (_("failed to rebuild keybox cache: %s\n"), */
851 /* g10_errstr (rc)); */
860 * Start the next search on this handle right at the beginning
863 keydb_search_reset (KEYDB_HANDLE hd
)
868 return gpg_error (GPG_ERR_INV_VALUE
);
872 /* and reset all resources */
873 for (i
=0; !rc
&& i
< hd
->used
; i
++)
875 switch (hd
->active
[i
].type
)
877 case KEYDB_RESOURCE_TYPE_NONE
:
879 case KEYDB_RESOURCE_TYPE_KEYBOX
:
880 rc
= keybox_search_reset (hd
->active
[i
].u
.kr
);
884 return rc
; /* fixme: we need to map error codes or share them with
889 * Search through all keydb resources, starting at the current position,
890 * for a keyblock which contains one of the keys described in the DESC array.
893 keydb_search (KEYDB_HANDLE hd
, KEYDB_SEARCH_DESC
*desc
, size_t ndesc
)
898 return gpg_error (GPG_ERR_INV_VALUE
);
900 while (rc
== -1 && hd
->current
>= 0 && hd
->current
< hd
->used
)
902 switch (hd
->active
[hd
->current
].type
)
904 case KEYDB_RESOURCE_TYPE_NONE
:
905 BUG(); /* we should never see it here */
907 case KEYDB_RESOURCE_TYPE_KEYBOX
:
908 rc
= keybox_search (hd
->active
[hd
->current
].u
.kr
, desc
, ndesc
);
911 if (rc
== -1) /* EOF -> switch to next resource */
914 hd
->found
= hd
->current
;
922 keydb_search_first (KEYDB_HANDLE hd
)
924 KEYDB_SEARCH_DESC desc
;
926 memset (&desc
, 0, sizeof desc
);
927 desc
.mode
= KEYDB_SEARCH_MODE_FIRST
;
928 return keydb_search (hd
, &desc
, 1);
932 keydb_search_next (KEYDB_HANDLE hd
)
934 KEYDB_SEARCH_DESC desc
;
936 memset (&desc
, 0, sizeof desc
);
937 desc
.mode
= KEYDB_SEARCH_MODE_NEXT
;
938 return keydb_search (hd
, &desc
, 1);
942 keydb_search_kid (KEYDB_HANDLE hd
, u32
*kid
)
944 KEYDB_SEARCH_DESC desc
;
946 memset (&desc
, 0, sizeof desc
);
947 desc
.mode
= KEYDB_SEARCH_MODE_LONG_KID
;
948 /* desc.u.kid[0] = kid[0]; */
949 /* desc.u.kid[1] = kid[1]; */
950 return keydb_search (hd
, &desc
, 1);
954 keydb_search_fpr (KEYDB_HANDLE hd
, const byte
*fpr
)
956 KEYDB_SEARCH_DESC desc
;
958 memset (&desc
, 0, sizeof desc
);
959 desc
.mode
= KEYDB_SEARCH_MODE_FPR
;
960 memcpy (desc
.u
.fpr
, fpr
, 20);
961 return keydb_search (hd
, &desc
, 1);
965 keydb_search_issuer (KEYDB_HANDLE hd
, const char *issuer
)
967 KEYDB_SEARCH_DESC desc
;
970 memset (&desc
, 0, sizeof desc
);
971 desc
.mode
= KEYDB_SEARCH_MODE_ISSUER
;
972 desc
.u
.name
= issuer
;
973 rc
= keydb_search (hd
, &desc
, 1);
978 keydb_search_issuer_sn (KEYDB_HANDLE hd
,
979 const char *issuer
, ksba_const_sexp_t serial
)
981 KEYDB_SEARCH_DESC desc
;
983 const unsigned char *s
;
985 memset (&desc
, 0, sizeof desc
);
986 desc
.mode
= KEYDB_SEARCH_MODE_ISSUER_SN
;
989 return gpg_error (GPG_ERR_INV_VALUE
);
991 for (desc
.snlen
= 0; digitp (s
); s
++)
992 desc
.snlen
= 10*desc
.snlen
+ atoi_1 (s
);
994 return gpg_error (GPG_ERR_INV_VALUE
);
996 desc
.u
.name
= issuer
;
997 rc
= keydb_search (hd
, &desc
, 1);
1002 keydb_search_subject (KEYDB_HANDLE hd
, const char *name
)
1004 KEYDB_SEARCH_DESC desc
;
1007 memset (&desc
, 0, sizeof desc
);
1008 desc
.mode
= KEYDB_SEARCH_MODE_SUBJECT
;
1010 rc
= keydb_search (hd
, &desc
, 1);
1016 classify_user_id (const char *name
,
1017 KEYDB_SEARCH_DESC
*desc
,
1025 /* clear the structure so that the mode field is set to zero unless
1026 * we set it to the correct value right at the end of this function */
1027 memset (desc
, 0, sizeof *desc
);
1029 /* Skip leading spaces. Fixme: what about trailing white space? */
1030 for(s
= name
; *s
&& spacep (s
); s
++ )
1035 case 0: /* empty string is an error */
1038 case '.': /* an email address, compare from end */
1039 mode
= KEYDB_SEARCH_MODE_MAILEND
;
1044 case '<': /* an email address */
1045 mode
= KEYDB_SEARCH_MODE_MAIL
;
1050 case '@': /* part of an email address */
1051 mode
= KEYDB_SEARCH_MODE_MAILSUB
;
1056 case '=': /* exact compare */
1057 mode
= KEYDB_SEARCH_MODE_EXACT
;
1062 case '*': /* case insensitive substring search */
1063 mode
= KEYDB_SEARCH_MODE_SUBSTR
;
1068 case '+': /* compare individual words */
1069 mode
= KEYDB_SEARCH_MODE_WORDS
;
1074 case '/': /* subject's DN */
1076 if (!*s
|| spacep (s
))
1077 return 0; /* no DN or prefixed with a space */
1079 mode
= KEYDB_SEARCH_MODE_SUBJECT
;
1088 { /* "#/" indicates an issuer's DN */
1090 if (!*s
|| spacep (s
))
1091 return 0; /* no DN or prefixed with a space */
1093 mode
= KEYDB_SEARCH_MODE_ISSUER
;
1096 { /* serialnumber + optional issuer ID */
1097 for (si
=s
; *si
&& *si
!= '/'; si
++)
1099 if (!strchr("01234567890abcdefABCDEF", *si
))
1100 return 0; /* invalid digit in serial number*/
1102 desc
->sn
= (const unsigned char*)s
;
1105 mode
= KEYDB_SEARCH_MODE_SN
;
1109 if (!*s
|| spacep (s
))
1110 return 0; /* no DN or prefixed with a space */
1112 mode
= KEYDB_SEARCH_MODE_ISSUER_SN
;
1118 case ':': /*Unified fingerprint */
1120 const char *se
, *si
;
1123 se
= strchr (++s
,':');
1126 for (i
=0,si
=s
; si
< se
; si
++, i
++ )
1128 if (!strchr("01234567890abcdefABCDEF", *si
))
1129 return 0; /* invalid digit */
1131 if (i
!= 32 && i
!= 40)
1132 return 0; /* invalid length of fpr*/
1133 for (i
=0,si
=s
; si
< se
; i
++, si
+=2)
1134 desc
->u
.fpr
[i
] = hextobyte(si
);
1138 mode
= KEYDB_SEARCH_MODE_FPR
;
1142 case '&': /* Keygrip*/
1144 if (hex2bin (s
+1, desc
->u
.grip
, 20) < 0)
1145 return 0; /* Invalid. */
1146 mode
= KEYDB_SEARCH_MODE_KEYGRIP
;
1151 if (s
[0] == '0' && s
[1] == 'x')
1157 hexlength
= strspn(s
, "0123456789abcdefABCDEF");
1158 if (hexlength
>= 8 && s
[hexlength
] =='!')
1161 hexlength
++; /* just for the following check */
1164 /* check if a hexadecimal number is terminated by EOS or blank */
1165 if (hexlength
&& s
[hexlength
] && !spacep (s
+hexlength
))
1167 if (hexprefix
) /* a "0x" prefix without correct */
1168 return 0; /* termination is an error */
1169 /* The first chars looked like a hex number, but really is
1175 hexlength
--; /* remove the bang */
1178 || (!hexprefix
&& hexlength
== 9 && *s
== '0'))
1183 kid
= strtoul( s
, NULL
, 16 );
1184 desc
->u
.kid
[4] = kid
>> 24;
1185 desc
->u
.kid
[5] = kid
>> 16;
1186 desc
->u
.kid
[6] = kid
>> 8;
1187 desc
->u
.kid
[7] = kid
;
1188 mode
= KEYDB_SEARCH_MODE_SHORT_KID
;
1190 else if (hexlength
== 16
1191 || (!hexprefix
&& hexlength
== 17 && *s
== '0'))
1192 { /* complete keyid */
1193 unsigned long kid0
, kid1
;
1195 if (hexlength
== 17)
1197 mem2str(buf
, s
, 9 );
1198 kid0
= strtoul (buf
, NULL
, 16);
1199 kid1
= strtoul (s
+8, NULL
, 16);
1200 desc
->u
.kid
[0] = kid0
>> 24;
1201 desc
->u
.kid
[1] = kid0
>> 16;
1202 desc
->u
.kid
[2] = kid0
>> 8;
1203 desc
->u
.kid
[3] = kid0
;
1204 desc
->u
.kid
[4] = kid1
>> 24;
1205 desc
->u
.kid
[5] = kid1
>> 16;
1206 desc
->u
.kid
[6] = kid1
>> 8;
1207 desc
->u
.kid
[7] = kid1
;
1208 mode
= KEYDB_SEARCH_MODE_LONG_KID
;
1210 else if (hexlength
== 32
1211 || (!hexprefix
&& hexlength
== 33 && *s
== '0'))
1212 { /* md5 fingerprint */
1214 if (hexlength
== 33)
1216 memset(desc
->u
.fpr
+16, 0, 4);
1217 for (i
=0; i
< 16; i
++, s
+=2)
1219 int c
= hextobyte(s
);
1224 mode
= KEYDB_SEARCH_MODE_FPR16
;
1226 else if (hexlength
== 40
1227 || (!hexprefix
&& hexlength
== 41 && *s
== '0'))
1228 { /* sha1/rmd160 fingerprint */
1230 if (hexlength
== 41)
1232 for (i
=0; i
< 20; i
++, s
+=2)
1234 int c
= hextobyte(s
);
1239 mode
= KEYDB_SEARCH_MODE_FPR20
;
1241 else if (!hexprefix
)
1243 /* The fingerprint in an X.509 listing is often delimited by
1244 colons, so we try to single this case out. */
1246 hexlength
= strspn (s
, ":0123456789abcdefABCDEF");
1247 if (hexlength
== 59 && (!s
[hexlength
] || spacep (s
+hexlength
)))
1251 for (i
=0; i
< 20; i
++, s
+= 3)
1253 int c
= hextobyte(s
);
1254 if (c
== -1 || (i
< 19 && s
[2] != ':'))
1259 mode
= KEYDB_SEARCH_MODE_FPR20
;
1261 if (!mode
) /* default is substring search */
1265 mode
= KEYDB_SEARCH_MODE_SUBSTR
;
1269 { /* hex number with a prefix but a wrong length */
1280 keydb_classify_name (const char *name
, KEYDB_SEARCH_DESC
*desc
)
1283 KEYDB_SEARCH_DESC dummy_desc
;
1288 if (!classify_user_id (name
, desc
, &dummy
))
1289 return gpg_error (GPG_ERR_INV_NAME
);
1294 /* Store the certificate in the key DB but make sure that it does not
1295 already exists. We do this simply by comparing the fingerprint.
1296 If EXISTED is not NULL it will be set to true if the certificate
1297 was already in the DB. */
1299 keydb_store_cert (ksba_cert_t cert
, int ephemeral
, int *existed
)
1303 unsigned char fpr
[20];
1308 if (!gpgsm_get_fingerprint (cert
, 0, fpr
, NULL
))
1310 log_error (_("failed to get the fingerprint\n"));
1311 return gpg_error (GPG_ERR_GENERAL
);
1317 log_error (_("failed to allocate keyDB handle\n"));
1318 return gpg_error (GPG_ERR_ENOMEM
);;
1322 keydb_set_ephemeral (kh
, 1);
1328 rc
= keydb_search_fpr (kh
, fpr
);
1336 return 0; /* okay */
1338 log_error (_("problem looking for existing certificate: %s\n"),
1343 rc
= keydb_locate_writable (kh
, 0);
1346 log_error (_("error finding writable keyDB: %s\n"), gpg_strerror (rc
));
1351 rc
= keydb_insert_cert (kh
, cert
);
1354 log_error (_("error storing certificate: %s\n"), gpg_strerror (rc
));
1363 /* This is basically keydb_set_flags but it implements a complete
1364 transaction by locating the certificate in the DB and updating the
1367 keydb_set_cert_flags (ksba_cert_t cert
, int ephemeral
,
1369 unsigned int mask
, unsigned int value
)
1373 unsigned char fpr
[20];
1374 unsigned int old_value
;
1376 if (!gpgsm_get_fingerprint (cert
, 0, fpr
, NULL
))
1378 log_error (_("failed to get the fingerprint\n"));
1379 return gpg_error (GPG_ERR_GENERAL
);
1385 log_error (_("failed to allocate keyDB handle\n"));
1386 return gpg_error (GPG_ERR_ENOMEM
);;
1390 keydb_set_ephemeral (kh
, 1);
1392 err
= keydb_lock (kh
);
1395 log_error (_("error locking keybox: %s\n"), gpg_strerror (err
));
1400 err
= keydb_search_fpr (kh
, fpr
);
1404 err
= gpg_error (GPG_ERR_NOT_FOUND
);
1406 log_error (_("problem re-searching certificate: %s\n"),
1407 gpg_strerror (err
));
1412 err
= keydb_get_flags (kh
, which
, idx
, &old_value
);
1415 log_error (_("error getting stored flags: %s\n"), gpg_strerror (err
));
1420 value
= ((old_value
& ~mask
) | (value
& mask
));
1422 if (value
!= old_value
)
1424 err
= keydb_set_flags (kh
, which
, idx
, value
);
1427 log_error (_("error storing flags: %s\n"), gpg_strerror (err
));
1438 /* Reset all the certificate flags we have stored with the certificates
1439 for performance reasons. */
1441 keydb_clear_some_cert_flags (ctrl_t ctrl
, strlist_t names
)
1444 KEYDB_HANDLE hd
= NULL
;
1445 KEYDB_SEARCH_DESC
*desc
= NULL
;
1449 unsigned int old_value
, value
;
1454 log_error ("keydb_new failed\n");
1462 for (sl
=names
, ndesc
=0; sl
; sl
= sl
->next
, ndesc
++)
1466 desc
= xtrycalloc (ndesc
, sizeof *desc
);
1469 log_error ("allocating memory failed: %s\n",
1470 gpg_strerror (out_of_core ()));
1475 desc
[0].mode
= KEYDB_SEARCH_MODE_FIRST
;
1478 for (ndesc
=0, sl
=names
; sl
; sl
= sl
->next
)
1480 rc
= keydb_classify_name (sl
->d
, desc
+ndesc
);
1483 log_error ("key `%s' not found: %s\n",
1484 sl
->d
, gpg_strerror (rc
));
1492 err
= keydb_lock (hd
);
1495 log_error (_("error locking keybox: %s\n"), gpg_strerror (err
));
1499 while (!(rc
= keydb_search (hd
, desc
, ndesc
)))
1502 desc
[0].mode
= KEYDB_SEARCH_MODE_NEXT
;
1504 err
= keydb_get_flags (hd
, KEYBOX_FLAG_VALIDITY
, 0, &old_value
);
1507 log_error (_("error getting stored flags: %s\n"),
1508 gpg_strerror (err
));
1512 value
= (old_value
& ~VALIDITY_REVOKED
);
1513 if (value
!= old_value
)
1515 err
= keydb_set_flags (hd
, KEYBOX_FLAG_VALIDITY
, 0, value
);
1518 log_error (_("error storing flags: %s\n"), gpg_strerror (err
));
1524 log_error ("keydb_search failed: %s\n", gpg_strerror (rc
));