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 2 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, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
28 #include <sys/types.h>
33 #include "../kbx/keybox.h"
37 static int active_handles
;
40 KEYDB_RESOURCE_TYPE_NONE
= 0,
41 KEYDB_RESOURCE_TYPE_KEYBOX
43 #define MAX_KEYDB_RESOURCES 20
45 struct resource_item
{
46 KeydbResourceType type
;
55 static struct resource_item all_resources
[MAX_KEYDB_RESOURCES
];
56 static int used_resources
;
63 int used
; /* items in active */
64 struct resource_item active
[MAX_KEYDB_RESOURCES
];
68 static int lock_all (KEYDB_HANDLE hd
);
69 static void unlock_all (KEYDB_HANDLE hd
);
73 * Register a resource (which currently may only be a keybox file).
74 * The first keybox which is added by this function is
75 * created if it does not exist.
76 * Note: this function may be called before secure memory is
80 keydb_add_resource (const char *url
, int force
, int secret
)
82 static int any_secret
, any_public
;
83 const char *resname
= url
;
84 char *filename
= NULL
;
87 KeydbResourceType rt
= KEYDB_RESOURCE_TYPE_NONE
;
88 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
;
201 /* now register the file */
204 void *token
= keybox_register_file (filename
, secret
);
206 ; /* already registered - ignore it */
207 else if (used_resources
>= MAX_KEYDB_RESOURCES
)
208 rc
= gpg_error (GPG_ERR_RESOURCE_LIMIT
);
211 all_resources
[used_resources
].type
= rt
;
212 all_resources
[used_resources
].u
.kr
= NULL
; /* Not used here */
213 all_resources
[used_resources
].token
= token
;
214 all_resources
[used_resources
].secret
= secret
;
216 all_resources
[used_resources
].lockhandle
217 = create_dotlock (filename
);
218 if (!all_resources
[used_resources
].lockhandle
)
219 log_fatal ( _("can't create lock for `%s'\n"), filename
);
221 /* Do a compress run if needed and the file is not locked. */
222 if (!make_dotlock (all_resources
[used_resources
].lockhandle
, 0))
224 KEYBOX_HANDLE kbxhd
= keybox_new (token
, secret
);
228 keybox_compress (kbxhd
);
229 keybox_release (kbxhd
);
231 release_dotlock (all_resources
[used_resources
].lockhandle
);
241 log_error ("resource type of `%s' not supported\n", url
);
242 rc
= gpg_error (GPG_ERR_NOT_SUPPORTED
);
246 /* fixme: check directory permissions and print a warning */
250 log_error ("keyblock resource `%s': %s\n", filename
, gpg_strerror(rc
));
261 keydb_new (int secret
)
266 hd
= xcalloc (1, sizeof *hd
);
269 assert (used_resources
<= MAX_KEYDB_RESOURCES
);
270 for (i
=j
=0; i
< used_resources
; i
++)
272 if (!all_resources
[i
].secret
!= !secret
)
274 switch (all_resources
[i
].type
)
276 case KEYDB_RESOURCE_TYPE_NONE
: /* ignore */
278 case KEYDB_RESOURCE_TYPE_KEYBOX
:
279 hd
->active
[j
].type
= all_resources
[i
].type
;
280 hd
->active
[j
].token
= all_resources
[i
].token
;
281 hd
->active
[j
].secret
= all_resources
[i
].secret
;
282 hd
->active
[j
].lockhandle
= all_resources
[i
].lockhandle
;
283 hd
->active
[j
].u
.kr
= keybox_new (all_resources
[i
].token
, secret
);
284 if (!hd
->active
[j
].u
.kr
)
287 return NULL
; /* fixme: release all previously allocated handles*/
300 keydb_release (KEYDB_HANDLE hd
)
306 assert (active_handles
> 0);
310 for (i
=0; i
< hd
->used
; i
++)
312 switch (hd
->active
[i
].type
)
314 case KEYDB_RESOURCE_TYPE_NONE
:
316 case KEYDB_RESOURCE_TYPE_KEYBOX
:
317 keybox_release (hd
->active
[i
].u
.kr
);
326 /* Return the name of the current resource. This is function first
327 looks for the last found found, then for the current search
328 position, and last returns the first available resource. The
329 returned string is only valid as long as the handle exists. This
330 function does only return NULL if no handle is specified, in all
331 other error cases an empty string is returned. */
333 keydb_get_resource_name (KEYDB_HANDLE hd
)
336 const char *s
= NULL
;
341 if ( hd
->found
>= 0 && hd
->found
< hd
->used
)
343 else if ( hd
->current
>= 0 && hd
->current
< hd
->used
)
348 switch (hd
->active
[idx
].type
)
350 case KEYDB_RESOURCE_TYPE_NONE
:
353 case KEYDB_RESOURCE_TYPE_KEYBOX
:
354 s
= keybox_get_resource_name (hd
->active
[idx
].u
.kr
);
361 /* Switch the handle into ephemeral mode and return the orginal value. */
363 keydb_set_ephemeral (KEYDB_HANDLE hd
, int yes
)
371 if (hd
->is_ephemeral
!= yes
)
373 for (i
=0; i
< hd
->used
; i
++)
375 switch (hd
->active
[i
].type
)
377 case KEYDB_RESOURCE_TYPE_NONE
:
379 case KEYDB_RESOURCE_TYPE_KEYBOX
:
380 keybox_set_ephemeral (hd
->active
[i
].u
.kr
, yes
);
386 i
= hd
->is_ephemeral
;
387 hd
->is_ephemeral
= yes
;
392 /* If the keyring has not yet been locked, lock it now. This
393 operation is required before any update opeations; it is optionaly
394 for an insert operation. The lock is released with
397 keydb_lock (KEYDB_HANDLE hd
)
400 return gpg_error (GPG_ERR_INV_HANDLE
);
402 return 0; /* Already locked. */
403 return lock_all (hd
);
409 lock_all (KEYDB_HANDLE hd
)
413 /* Fixme: This locking scheme may lead to deadlock if the resources
414 are not added in the same order by all processes. We are
415 currently only allowing one resource so it is not a problem. */
416 for (i
=0; i
< hd
->used
; i
++)
418 switch (hd
->active
[i
].type
)
420 case KEYDB_RESOURCE_TYPE_NONE
:
422 case KEYDB_RESOURCE_TYPE_KEYBOX
:
423 if (hd
->active
[i
].lockhandle
)
424 rc
= make_dotlock (hd
->active
[i
].lockhandle
, -1);
433 /* revert the already set locks */
434 for (i
--; i
>= 0; i
--)
436 switch (hd
->active
[i
].type
)
438 case KEYDB_RESOURCE_TYPE_NONE
:
440 case KEYDB_RESOURCE_TYPE_KEYBOX
:
441 if (hd
->active
[i
].lockhandle
)
442 release_dotlock (hd
->active
[i
].lockhandle
);
450 /* make_dotlock () does not yet guarantee that errno is set, thus
451 we can't rely on the error reason and will simply use
453 return rc
? gpg_error (GPG_ERR_EACCES
) : 0;
457 unlock_all (KEYDB_HANDLE hd
)
464 for (i
=hd
->used
-1; i
>= 0; i
--)
466 switch (hd
->active
[i
].type
)
468 case KEYDB_RESOURCE_TYPE_NONE
:
470 case KEYDB_RESOURCE_TYPE_KEYBOX
:
471 if (hd
->active
[i
].lockhandle
)
472 release_dotlock (hd
->active
[i
].lockhandle
);
482 * Return the last found keybox. Caller must free it.
483 * The returned keyblock has the kbode flag bit 0 set for the node with
484 * the public key used to locate the keyblock or flag bit 1 set for
488 keydb_get_keyblock (KEYDB_HANDLE hd
, KBNODE
*ret_kb
)
493 return G10ERR_INV_ARG
;
495 if ( hd
->found
< 0 || hd
->found
>= hd
->used
)
496 return -1; /* nothing found */
498 switch (hd
->active
[hd
->found
].type
) {
499 case KEYDB_RESOURCE_TYPE_NONE
:
500 rc
= G10ERR_GENERAL
; /* oops */
502 case KEYDB_RESOURCE_TYPE_KEYBOX
:
503 rc
= keybox_get_keyblock (hd
->active
[hd
->found
].u
.kr
, ret_kb
);
511 * update the current keyblock with KB
514 keydb_update_keyblock (KEYDB_HANDLE hd
, KBNODE kb
)
519 return G10ERR_INV_ARG
;
521 if ( hd
->found
< 0 || hd
->found
>= hd
->used
)
522 return -1; /* nothing found */
528 return gpg_error (GPG_ERR_NOT_LOCKED
);
530 switch (hd
->active
[hd
->found
].type
) {
531 case KEYDB_RESOURCE_TYPE_NONE
:
532 rc
= G10ERR_GENERAL
; /* oops */
534 case KEYDB_RESOURCE_TYPE_KEYBOX
:
535 rc
= keybox_update_keyblock (hd
->active
[hd
->found
].u
.kr
, kb
);
545 * Insert a new KB into one of the resources.
548 keydb_insert_keyblock (KEYDB_HANDLE hd
, KBNODE kb
)
554 return G10ERR_INV_ARG
;
559 if ( hd
->found
>= 0 && hd
->found
< hd
->used
)
561 else if ( hd
->current
>= 0 && hd
->current
< hd
->used
)
564 return G10ERR_GENERAL
;
570 switch (hd
->active
[idx
].type
) {
571 case KEYDB_RESOURCE_TYPE_NONE
:
572 rc
= G10ERR_GENERAL
; /* oops */
574 case KEYDB_RESOURCE_TYPE_KEYBOX
:
575 rc
= keybox_insert_keyblock (hd
->active
[idx
].u
.kr
, kb
);
583 #endif /*disabled code*/
588 Return the last found object. Caller must free it. The returned
589 keyblock has the kbode flag bit 0 set for the node with the public
590 key used to locate the keyblock or flag bit 1 set for the user ID
593 keydb_get_cert (KEYDB_HANDLE hd
, ksba_cert_t
*r_cert
)
598 return gpg_error (GPG_ERR_INV_VALUE
);
600 if ( hd
->found
< 0 || hd
->found
>= hd
->used
)
601 return -1; /* nothing found */
603 switch (hd
->active
[hd
->found
].type
)
605 case KEYDB_RESOURCE_TYPE_NONE
:
606 rc
= gpg_error (GPG_ERR_GENERAL
); /* oops */
608 case KEYDB_RESOURCE_TYPE_KEYBOX
:
609 rc
= keybox_get_cert (hd
->active
[hd
->found
].u
.kr
, r_cert
);
616 /* Return a flag of the last found object. WHICH is the flag requested;
617 it should be one of the KEYBOX_FLAG_ values. If the operation is
618 successful, the flag value will be stored at the address given by
619 VALUE. Return 0 on success or an error code. */
621 keydb_get_flags (KEYDB_HANDLE hd
, int which
, int idx
, unsigned int *value
)
626 return gpg_error (GPG_ERR_INV_VALUE
);
628 if ( hd
->found
< 0 || hd
->found
>= hd
->used
)
629 return gpg_error (GPG_ERR_NOTHING_FOUND
);
631 switch (hd
->active
[hd
->found
].type
)
633 case KEYDB_RESOURCE_TYPE_NONE
:
634 err
= gpg_error (GPG_ERR_GENERAL
); /* oops */
636 case KEYDB_RESOURCE_TYPE_KEYBOX
:
637 err
= keybox_get_flags (hd
->active
[hd
->found
].u
.kr
, which
, idx
, value
);
644 /* Set a flag of the last found object. WHICH is the flag to be set; it
645 should be one of the KEYBOX_FLAG_ values. If the operation is
646 successful, the flag value will be stored in the keybox. Note,
647 that some flag values can't be updated and thus may return an
648 error, some other flag values may be masked out before an update.
649 Returns 0 on success or an error code. */
651 keydb_set_flags (KEYDB_HANDLE hd
, int which
, int idx
, unsigned int value
)
656 return gpg_error (GPG_ERR_INV_VALUE
);
658 if ( hd
->found
< 0 || hd
->found
>= hd
->used
)
659 return gpg_error (GPG_ERR_NOTHING_FOUND
);
662 return gpg_error (GPG_ERR_NOT_LOCKED
);
664 switch (hd
->active
[hd
->found
].type
)
666 case KEYDB_RESOURCE_TYPE_NONE
:
667 err
= gpg_error (GPG_ERR_GENERAL
); /* oops */
669 case KEYDB_RESOURCE_TYPE_KEYBOX
:
670 err
= keybox_set_flags (hd
->active
[hd
->found
].u
.kr
, which
, idx
, value
);
678 * Insert a new Certificate into one of the resources.
681 keydb_insert_cert (KEYDB_HANDLE hd
, ksba_cert_t cert
)
685 unsigned char digest
[20];
688 return gpg_error (GPG_ERR_INV_VALUE
);
693 if ( hd
->found
>= 0 && hd
->found
< hd
->used
)
695 else if ( hd
->current
>= 0 && hd
->current
< hd
->used
)
698 return gpg_error (GPG_ERR_GENERAL
);
701 return gpg_error (GPG_ERR_NOT_LOCKED
);
703 gpgsm_get_fingerprint (cert
, GCRY_MD_SHA1
, digest
, NULL
); /* kludge*/
705 switch (hd
->active
[idx
].type
)
707 case KEYDB_RESOURCE_TYPE_NONE
:
708 rc
= gpg_error (GPG_ERR_GENERAL
);
710 case KEYDB_RESOURCE_TYPE_KEYBOX
:
711 rc
= keybox_insert_cert (hd
->active
[idx
].u
.kr
, cert
, digest
);
721 /* update the current keyblock with KB */
723 keydb_update_cert (KEYDB_HANDLE hd
, ksba_cert_t cert
)
726 unsigned char digest
[20];
729 return gpg_error (GPG_ERR_INV_VALUE
);
731 if ( hd
->found
< 0 || hd
->found
>= hd
->used
)
732 return -1; /* nothing found */
741 gpgsm_get_fingerprint (cert
, GCRY_MD_SHA1
, digest
, NULL
); /* kludge*/
743 switch (hd
->active
[hd
->found
].type
)
745 case KEYDB_RESOURCE_TYPE_NONE
:
746 rc
= gpg_error (GPG_ERR_GENERAL
); /* oops */
748 case KEYDB_RESOURCE_TYPE_KEYBOX
:
749 rc
= keybox_update_cert (hd
->active
[hd
->found
].u
.kr
, cert
, digest
);
759 * The current keyblock or cert will be deleted.
762 keydb_delete (KEYDB_HANDLE hd
, int unlock
)
767 return gpg_error (GPG_ERR_INV_VALUE
);
769 if ( hd
->found
< 0 || hd
->found
>= hd
->used
)
770 return -1; /* nothing found */
776 return gpg_error (GPG_ERR_NOT_LOCKED
);
778 switch (hd
->active
[hd
->found
].type
)
780 case KEYDB_RESOURCE_TYPE_NONE
:
781 rc
= gpg_error (GPG_ERR_GENERAL
);
783 case KEYDB_RESOURCE_TYPE_KEYBOX
:
784 rc
= keybox_delete (hd
->active
[hd
->found
].u
.kr
);
796 * Locate the default writable key resource, so that the next
797 * operation (which is only relevant for inserts) will be done on this
801 keydb_locate_writable (KEYDB_HANDLE hd
, const char *reserved
)
806 return gpg_error (GPG_ERR_INV_VALUE
);
808 rc
= keydb_search_reset (hd
); /* this does reset hd->current */
812 for ( ; hd
->current
>= 0 && hd
->current
< hd
->used
; hd
->current
++)
814 switch (hd
->active
[hd
->current
].type
)
816 case KEYDB_RESOURCE_TYPE_NONE
:
819 case KEYDB_RESOURCE_TYPE_KEYBOX
:
820 if (keybox_is_writable (hd
->active
[hd
->current
].token
))
821 return 0; /* found (hd->current is set to it) */
830 * Rebuild the caches of all key resources.
833 keydb_rebuild_caches (void)
837 for (i
=0; i
< used_resources
; i
++)
839 if (all_resources
[i
].secret
)
841 switch (all_resources
[i
].type
)
843 case KEYDB_RESOURCE_TYPE_NONE
: /* ignore */
845 case KEYDB_RESOURCE_TYPE_KEYBOX
:
846 /* rc = keybox_rebuild_cache (all_resources[i].token); */
848 /* log_error (_("failed to rebuild keybox cache: %s\n"), */
849 /* g10_errstr (rc)); */
858 * Start the next search on this handle right at the beginning
861 keydb_search_reset (KEYDB_HANDLE hd
)
866 return gpg_error (GPG_ERR_INV_VALUE
);
870 /* and reset all resources */
871 for (i
=0; !rc
&& i
< hd
->used
; i
++)
873 switch (hd
->active
[i
].type
)
875 case KEYDB_RESOURCE_TYPE_NONE
:
877 case KEYDB_RESOURCE_TYPE_KEYBOX
:
878 rc
= keybox_search_reset (hd
->active
[i
].u
.kr
);
882 return rc
; /* fixme: we need to map error codes or share them with
887 * Search through all keydb resources, starting at the current position,
888 * for a keyblock which contains one of the keys described in the DESC array.
891 keydb_search (KEYDB_HANDLE hd
, KEYDB_SEARCH_DESC
*desc
, size_t ndesc
)
896 return gpg_error (GPG_ERR_INV_VALUE
);
898 while (rc
== -1 && hd
->current
>= 0 && hd
->current
< hd
->used
)
900 switch (hd
->active
[hd
->current
].type
)
902 case KEYDB_RESOURCE_TYPE_NONE
:
903 BUG(); /* we should never see it here */
905 case KEYDB_RESOURCE_TYPE_KEYBOX
:
906 rc
= keybox_search (hd
->active
[hd
->current
].u
.kr
, desc
, ndesc
);
909 if (rc
== -1) /* EOF -> switch to next resource */
912 hd
->found
= hd
->current
;
920 keydb_search_first (KEYDB_HANDLE hd
)
922 KEYDB_SEARCH_DESC desc
;
924 memset (&desc
, 0, sizeof desc
);
925 desc
.mode
= KEYDB_SEARCH_MODE_FIRST
;
926 return keydb_search (hd
, &desc
, 1);
930 keydb_search_next (KEYDB_HANDLE hd
)
932 KEYDB_SEARCH_DESC desc
;
934 memset (&desc
, 0, sizeof desc
);
935 desc
.mode
= KEYDB_SEARCH_MODE_NEXT
;
936 return keydb_search (hd
, &desc
, 1);
940 keydb_search_kid (KEYDB_HANDLE hd
, u32
*kid
)
942 KEYDB_SEARCH_DESC desc
;
944 memset (&desc
, 0, sizeof desc
);
945 desc
.mode
= KEYDB_SEARCH_MODE_LONG_KID
;
946 /* desc.u.kid[0] = kid[0]; */
947 /* desc.u.kid[1] = kid[1]; */
948 return keydb_search (hd
, &desc
, 1);
952 keydb_search_fpr (KEYDB_HANDLE hd
, const byte
*fpr
)
954 KEYDB_SEARCH_DESC desc
;
956 memset (&desc
, 0, sizeof desc
);
957 desc
.mode
= KEYDB_SEARCH_MODE_FPR
;
958 memcpy (desc
.u
.fpr
, fpr
, 20);
959 return keydb_search (hd
, &desc
, 1);
963 keydb_search_issuer (KEYDB_HANDLE hd
, const char *issuer
)
965 KEYDB_SEARCH_DESC desc
;
968 memset (&desc
, 0, sizeof desc
);
969 desc
.mode
= KEYDB_SEARCH_MODE_ISSUER
;
970 desc
.u
.name
= issuer
;
971 rc
= keydb_search (hd
, &desc
, 1);
976 keydb_search_issuer_sn (KEYDB_HANDLE hd
,
977 const char *issuer
, ksba_const_sexp_t serial
)
979 KEYDB_SEARCH_DESC desc
;
981 const unsigned char *s
;
983 memset (&desc
, 0, sizeof desc
);
984 desc
.mode
= KEYDB_SEARCH_MODE_ISSUER_SN
;
987 return gpg_error (GPG_ERR_INV_VALUE
);
989 for (desc
.snlen
= 0; digitp (s
); s
++)
990 desc
.snlen
= 10*desc
.snlen
+ atoi_1 (s
);
992 return gpg_error (GPG_ERR_INV_VALUE
);
994 desc
.u
.name
= issuer
;
995 rc
= keydb_search (hd
, &desc
, 1);
1000 keydb_search_subject (KEYDB_HANDLE hd
, const char *name
)
1002 KEYDB_SEARCH_DESC desc
;
1005 memset (&desc
, 0, sizeof desc
);
1006 desc
.mode
= KEYDB_SEARCH_MODE_SUBJECT
;
1008 rc
= keydb_search (hd
, &desc
, 1);
1014 classify_user_id (const char *name
,
1015 KEYDB_SEARCH_DESC
*desc
,
1023 /* clear the structure so that the mode field is set to zero unless
1024 * we set it to the correct value right at the end of this function */
1025 memset (desc
, 0, sizeof *desc
);
1027 /* Skip leading spaces. Fixme: what about trailing white space? */
1028 for(s
= name
; *s
&& spacep (s
); s
++ )
1033 case 0: /* empty string is an error */
1036 case '.': /* an email address, compare from end */
1037 mode
= KEYDB_SEARCH_MODE_MAILEND
;
1042 case '<': /* an email address */
1043 mode
= KEYDB_SEARCH_MODE_MAIL
;
1048 case '@': /* part of an email address */
1049 mode
= KEYDB_SEARCH_MODE_MAILSUB
;
1054 case '=': /* exact compare */
1055 mode
= KEYDB_SEARCH_MODE_EXACT
;
1060 case '*': /* case insensitive substring search */
1061 mode
= KEYDB_SEARCH_MODE_SUBSTR
;
1066 case '+': /* compare individual words */
1067 mode
= KEYDB_SEARCH_MODE_WORDS
;
1072 case '/': /* subject's DN */
1074 if (!*s
|| spacep (s
))
1075 return 0; /* no DN or prefixed with a space */
1077 mode
= KEYDB_SEARCH_MODE_SUBJECT
;
1086 { /* "#/" indicates an issuer's DN */
1088 if (!*s
|| spacep (s
))
1089 return 0; /* no DN or prefixed with a space */
1091 mode
= KEYDB_SEARCH_MODE_ISSUER
;
1094 { /* serialnumber + optional issuer ID */
1095 for (si
=s
; *si
&& *si
!= '/'; si
++)
1097 if (!strchr("01234567890abcdefABCDEF", *si
))
1098 return 0; /* invalid digit in serial number*/
1100 desc
->sn
= (const unsigned char*)s
;
1103 mode
= KEYDB_SEARCH_MODE_SN
;
1107 if (!*s
|| spacep (s
))
1108 return 0; /* no DN or prefixed with a space */
1110 mode
= KEYDB_SEARCH_MODE_ISSUER_SN
;
1116 case ':': /*Unified fingerprint */
1118 const char *se
, *si
;
1121 se
= strchr (++s
,':');
1124 for (i
=0,si
=s
; si
< se
; si
++, i
++ )
1126 if (!strchr("01234567890abcdefABCDEF", *si
))
1127 return 0; /* invalid digit */
1129 if (i
!= 32 && i
!= 40)
1130 return 0; /* invalid length of fpr*/
1131 for (i
=0,si
=s
; si
< se
; i
++, si
+=2)
1132 desc
->u
.fpr
[i
] = hextobyte(si
);
1136 mode
= KEYDB_SEARCH_MODE_FPR
;
1141 if (s
[0] == '0' && s
[1] == 'x')
1147 hexlength
= strspn(s
, "0123456789abcdefABCDEF");
1148 if (hexlength
>= 8 && s
[hexlength
] =='!')
1151 hexlength
++; /* just for the following check */
1154 /* check if a hexadecimal number is terminated by EOS or blank */
1155 if (hexlength
&& s
[hexlength
] && !spacep (s
+hexlength
))
1157 if (hexprefix
) /* a "0x" prefix without correct */
1158 return 0; /* termination is an error */
1159 /* The first chars looked like a hex number, but really is
1165 hexlength
--; /* remove the bang */
1168 || (!hexprefix
&& hexlength
== 9 && *s
== '0'))
1173 kid
= strtoul( s
, NULL
, 16 );
1174 desc
->u
.kid
[4] = kid
>> 24;
1175 desc
->u
.kid
[5] = kid
>> 16;
1176 desc
->u
.kid
[6] = kid
>> 8;
1177 desc
->u
.kid
[7] = kid
;
1178 mode
= KEYDB_SEARCH_MODE_SHORT_KID
;
1180 else if (hexlength
== 16
1181 || (!hexprefix
&& hexlength
== 17 && *s
== '0'))
1182 { /* complete keyid */
1183 unsigned long kid0
, kid1
;
1185 if (hexlength
== 17)
1187 mem2str(buf
, s
, 9 );
1188 kid0
= strtoul (buf
, NULL
, 16);
1189 kid1
= strtoul (s
+8, NULL
, 16);
1190 desc
->u
.kid
[0] = kid0
>> 24;
1191 desc
->u
.kid
[1] = kid0
>> 16;
1192 desc
->u
.kid
[2] = kid0
>> 8;
1193 desc
->u
.kid
[3] = kid0
;
1194 desc
->u
.kid
[4] = kid1
>> 24;
1195 desc
->u
.kid
[5] = kid1
>> 16;
1196 desc
->u
.kid
[6] = kid1
>> 8;
1197 desc
->u
.kid
[7] = kid1
;
1198 mode
= KEYDB_SEARCH_MODE_LONG_KID
;
1200 else if (hexlength
== 32
1201 || (!hexprefix
&& hexlength
== 33 && *s
== '0'))
1202 { /* md5 fingerprint */
1204 if (hexlength
== 33)
1206 memset(desc
->u
.fpr
+16, 0, 4);
1207 for (i
=0; i
< 16; i
++, s
+=2)
1209 int c
= hextobyte(s
);
1214 mode
= KEYDB_SEARCH_MODE_FPR16
;
1216 else if (hexlength
== 40
1217 || (!hexprefix
&& hexlength
== 41 && *s
== '0'))
1218 { /* sha1/rmd160 fingerprint */
1220 if (hexlength
== 41)
1222 for (i
=0; i
< 20; i
++, s
+=2)
1224 int c
= hextobyte(s
);
1229 mode
= KEYDB_SEARCH_MODE_FPR20
;
1231 else if (!hexprefix
)
1233 /* The fingerprint in an X.509 listing is often delimited by
1234 colons, so we try to single this case out. */
1236 hexlength
= strspn (s
, ":0123456789abcdefABCDEF");
1237 if (hexlength
== 59 && (!s
[hexlength
] || spacep (s
+hexlength
)))
1241 for (i
=0; i
< 20; i
++, s
+= 3)
1243 int c
= hextobyte(s
);
1244 if (c
== -1 || (i
< 19 && s
[2] != ':'))
1249 mode
= KEYDB_SEARCH_MODE_FPR20
;
1251 if (!mode
) /* default is substring search */
1255 mode
= KEYDB_SEARCH_MODE_SUBSTR
;
1259 { /* hex number with a prefix but a wrong length */
1270 keydb_classify_name (const char *name
, KEYDB_SEARCH_DESC
*desc
)
1273 KEYDB_SEARCH_DESC dummy_desc
;
1278 if (!classify_user_id (name
, desc
, &dummy
))
1279 return gpg_error (GPG_ERR_INV_NAME
);
1284 /* Store the certificate in the key DB but make sure that it does not
1285 already exists. We do this simply by comparing the fingerprint.
1286 If EXISTED is not NULL it will be set to true if the certificate
1287 was already in the DB. */
1289 keydb_store_cert (ksba_cert_t cert
, int ephemeral
, int *existed
)
1293 unsigned char fpr
[20];
1298 if (!gpgsm_get_fingerprint (cert
, 0, fpr
, NULL
))
1300 log_error (_("failed to get the fingerprint\n"));
1301 return gpg_error (GPG_ERR_GENERAL
);
1307 log_error (_("failed to allocate keyDB handle\n"));
1308 return gpg_error (GPG_ERR_ENOMEM
);;
1312 keydb_set_ephemeral (kh
, 1);
1318 rc
= keydb_search_fpr (kh
, fpr
);
1326 return 0; /* okay */
1328 log_error (_("problem looking for existing certificate: %s\n"),
1333 rc
= keydb_locate_writable (kh
, 0);
1336 log_error (_("error finding writable keyDB: %s\n"), gpg_strerror (rc
));
1341 rc
= keydb_insert_cert (kh
, cert
);
1344 log_error (_("error storing certificate: %s\n"), gpg_strerror (rc
));
1353 /* This is basically keydb_set_flags but it implements a complete
1354 transaction by locating the certificate in the DB and updating the
1357 keydb_set_cert_flags (ksba_cert_t cert
, int which
, int idx
, unsigned int value
)
1361 unsigned char fpr
[20];
1362 unsigned int old_value
;
1364 if (!gpgsm_get_fingerprint (cert
, 0, fpr
, NULL
))
1366 log_error (_("failed to get the fingerprint\n"));
1367 return gpg_error (GPG_ERR_GENERAL
);
1373 log_error (_("failed to allocate keyDB handle\n"));
1374 return gpg_error (GPG_ERR_ENOMEM
);;
1377 err
= keydb_lock (kh
);
1380 log_error (_("error locking keybox: %s\n"), gpg_strerror (err
));
1385 err
= keydb_search_fpr (kh
, fpr
);
1388 log_error (_("problem re-searching certificate: %s\n"),
1389 gpg_strerror (err
));
1394 err
= keydb_get_flags (kh
, which
, idx
, &old_value
);
1397 log_error (_("error getting stored flags: %s\n"), gpg_strerror (err
));
1401 if (value
!= old_value
)
1403 err
= keydb_set_flags (kh
, which
, idx
, value
);
1406 log_error (_("error storing flags: %s\n"), gpg_strerror (err
));
1416 /* Reset all the certificate flags we have stored with the certificates
1417 for performance reasons. */
1419 keydb_clear_some_cert_flags (ctrl_t ctrl
, STRLIST names
)
1422 KEYDB_HANDLE hd
= NULL
;
1423 KEYDB_SEARCH_DESC
*desc
= NULL
;
1427 unsigned int old_value
, value
;
1432 log_error ("keydb_new failed\n");
1440 for (sl
=names
, ndesc
=0; sl
; sl
= sl
->next
, ndesc
++)
1444 desc
= xtrycalloc (ndesc
, sizeof *desc
);
1447 log_error ("allocating memory failed: %s\n",
1448 gpg_strerror (out_of_core ()));
1453 desc
[0].mode
= KEYDB_SEARCH_MODE_FIRST
;
1456 for (ndesc
=0, sl
=names
; sl
; sl
= sl
->next
)
1458 rc
= keydb_classify_name (sl
->d
, desc
+ndesc
);
1461 log_error ("key `%s' not found: %s\n",
1462 sl
->d
, gpg_strerror (rc
));
1470 err
= keydb_lock (hd
);
1473 log_error (_("error locking keybox: %s\n"), gpg_strerror (err
));
1477 while (!(rc
= keydb_search (hd
, desc
, ndesc
)))
1480 desc
[0].mode
= KEYDB_SEARCH_MODE_NEXT
;
1482 err
= keydb_get_flags (hd
, KEYBOX_FLAG_VALIDITY
, 0, &old_value
);
1485 log_error (_("error getting stored flags: %s\n"),
1486 gpg_strerror (err
));
1490 value
= (old_value
& ~VALIDITY_REVOKED
);
1491 if (value
!= old_value
)
1493 err
= keydb_set_flags (hd
, KEYBOX_FLAG_VALIDITY
, 0, value
);
1496 log_error (_("error storing flags: %s\n"), gpg_strerror (err
));
1502 log_error ("keydb_search failed: %s\n", gpg_strerror (rc
));