Merged silc_1_1_branch to trunk.
[silc.git] / lib / silcskr / silcskr.c
1 /*
2
3   silcskr.c
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2005 - 2007 Pekka Riikonen
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; version 2 of the License.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18 */
19
20 #include "silc.h"
21 #include "silcskr.h"
22
23 /************************** Types and definitions ***************************/
24
25 /* Search constraints */
26 typedef enum {
27   SILC_SKR_FIND_PKCS_TYPE,
28   SILC_SKR_FIND_USERNAME,
29   SILC_SKR_FIND_HOST,
30   SILC_SKR_FIND_REALNAME,
31   SILC_SKR_FIND_EMAIL,
32   SILC_SKR_FIND_ORG,
33   SILC_SKR_FIND_COUNTRY,
34   SILC_SKR_FIND_PUBLIC_KEY,
35   SILC_SKR_FIND_CONTEXT,
36   SILC_SKR_FIND_USAGE,          /* Never added as key specific */
37 } SilcSKRFindType;
38
39 /* Hash table key context */
40 typedef struct {
41   SilcSKRFindType type;         /* Type of key */
42   void *data;                   /* Hash table key */
43 } *SilcSKREntry, SilcSKREntryStruct;
44
45 /* Foreach user context when finding entries from hash table */
46 typedef struct {
47   SilcDList list;
48   void *key_context;
49   SilcSKRKeyUsage usage;
50 } SilcSKRFindForeach;
51
52 #if defined(SILC_DEBUG)
53 static const char *find_name[] = {
54   "PKCS TYPE ",
55   "USERNAME  ",
56   "HOST      ",
57   "REALNAME  ",
58   "EMAIL     ",
59   "ORG       ",
60   "COUNTRY   ",
61   "PUBLIC KEY",
62   "CONTEXT   ",
63   "USAGE     ",
64   NULL
65 };
66 #endif /* SILC_DEBUG */
67
68 /************************ Static utility functions **************************/
69
70 #if defined(SILC_DEBUG)
71
72 /* Returns search constraint string */
73
74 static void silc_skr_type_string(SilcSKRFindType type, void *data,
75                                  char *retbuf, SilcUInt32 retbuf_size)
76 {
77   switch (type) {
78   case SILC_SKR_FIND_PKCS_TYPE:
79   case SILC_SKR_FIND_USAGE:
80     silc_snprintf(retbuf, retbuf_size, "[%s] [%d]", find_name[type],
81                   (int)SILC_PTR_TO_32(data));
82     break;
83
84   case SILC_SKR_FIND_PUBLIC_KEY:
85   case SILC_SKR_FIND_CONTEXT:
86     silc_snprintf(retbuf, retbuf_size, "[%s] [%p]", find_name[type], data);
87     break;
88
89   default:
90     silc_snprintf(retbuf, retbuf_size, "[%s] [%s]", find_name[type],
91                   (char *)data);
92   }
93 }
94 #endif /* SILC_DEBUG */
95
96 /* Hash table destructor for search constraints */
97
98 static void silc_skr_find_destructor(void *key, void *context,
99                                      void *user_context)
100 {
101   SilcSKRFindType type = SILC_PTR_TO_32(key);
102   SilcPKCSType pkcs_type = SILC_PTR_TO_32(user_context);
103
104   switch (type) {
105   case SILC_SKR_FIND_PKCS_TYPE:
106   case SILC_SKR_FIND_USAGE:
107   case SILC_SKR_FIND_CONTEXT:
108     break;
109
110   case SILC_SKR_FIND_PUBLIC_KEY:
111     silc_pkcs_public_key_free(context);
112     break;
113
114   default:
115     /* In SILC Public key all entries are referenced from the public key
116        so don't free them.  This test is valid only when removing key
117        from the repository. */
118     if (pkcs_type == SILC_PKCS_SILC)
119       break;
120
121     silc_free(context);
122   }
123 }
124
125 /* Hash table destructor for key entries */
126
127 static void silc_skr_destructor(void *key, void *context, void *user_context)
128 {
129   SilcSKREntry type = key;
130   SilcSKRKeyInternal entry = context;
131   SilcPKCSType pkcs_type = silc_pkcs_get_type(entry->key.key);
132
133   /* Destroy search data, except for SILC_SKR_FIND_PUBLIC_KEY because it
134      shares same context with the key entry. */
135   if (SILC_PTR_TO_32(type->type) != SILC_SKR_FIND_PUBLIC_KEY)
136     silc_skr_find_destructor(SILC_32_TO_PTR(type->type), type->data,
137                              SILC_32_TO_PTR(pkcs_type));
138   silc_free(type);
139
140   /* Destroy key */
141   entry->refcnt--;
142   if (entry->refcnt > 0)
143     return;
144
145   SILC_LOG_DEBUG(("Freeing public key %p", entry->key.key));
146
147   silc_pkcs_public_key_free(entry->key.key);
148   silc_free(entry);
149 }
150
151 /* Hash table hash function for key entries */
152
153 static SilcUInt32 silc_skr_hash(void *key, void *user_context)
154 {
155   SilcSKREntry type = key;
156
157   switch (type->type) {
158   case SILC_SKR_FIND_PKCS_TYPE:
159   case SILC_SKR_FIND_CONTEXT:
160     return type->type + (type->type ^ SILC_PTR_TO_32(type->data));
161     break;
162
163   case SILC_SKR_FIND_PUBLIC_KEY:
164     return type->type + silc_hash_public_key(type->data, user_context);
165     break;
166
167   default:
168     break;
169   }
170
171   return type->type + silc_hash_string(type->data, user_context);
172 }
173
174 /* Hash table comparison function for key entries */
175
176 static SilcBool silc_skr_compare(void *key1, void *key2, void *user_context)
177 {
178   SilcSKREntry type1 = key1;
179   SilcSKREntry type2 = key2;
180
181   if (type1->type != type2->type)
182     return FALSE;
183
184   switch (type1->type) {
185   case SILC_SKR_FIND_PKCS_TYPE:
186   case SILC_SKR_FIND_CONTEXT:
187     return type1->data == type2->data;
188     break;
189
190   case SILC_SKR_FIND_PUBLIC_KEY:
191     return silc_hash_public_key_compare(type1->data, type2->data,
192                                         user_context);
193     break;
194
195   default:
196     break;
197   }
198
199   return silc_utf8_strcasecmp((const char *)type1->data,
200                               (const char *)type2->data);
201 }
202
203 /* Foreach function for finding entries in the repository */
204
205 static void silc_skr_find_foreach(void *key, void *context,
206                                   void *user_context)
207 {
208   SilcSKRFindForeach *f = user_context;
209   SilcSKRKeyInternal k = context;
210
211   if (k) {
212     /* If key context is present, it must match the context in the key.
213        This is used only internally when adding keys, to check if the key
214        is added with same context. */
215     if (f->key_context && f->key_context != k->key.key_context)
216       return;
217
218     /* Check for usage bits.  At least one usage bit must be set. */
219     if (f->usage && k->key.usage && (f->usage & k->key.usage) == 0)
220       return;
221
222     silc_dlist_add(f->list, k);
223   }
224 }
225
226 /* Finds entry from repository by search constraint type and data */
227
228 static SilcBool silc_skr_find_entry(SilcSKR skr,
229                                     SilcSKRStatus *status,
230                                     SilcSKRFindType type,
231                                     void *type_data,
232                                     SilcDList *results,
233                                     void *key_context,
234                                     SilcSKRKeyUsage usage)
235 {
236   SilcSKREntryStruct find;
237   SilcSKRFindForeach f;
238
239   f.list = silc_dlist_init();
240   if (!f.list) {
241     *status |= SILC_SKR_NO_MEMORY;
242     return FALSE;
243   }
244   f.key_context = key_context;
245   f.usage = usage;
246
247   find.type = type;
248   find.data = type_data;
249
250   silc_hash_table_find_foreach(skr->keys, (void *)&find,
251                                silc_skr_find_foreach, &f);
252
253   if (!silc_dlist_count(f.list)) {
254     *status |= SILC_SKR_NOT_FOUND;
255     silc_dlist_uninit(f.list);
256     return FALSE;
257   }
258
259   if (results)
260     *results = f.list;
261   else
262     silc_dlist_uninit(f.list);
263
264   return TRUE;
265 }
266
267 /* Add a key by search constraint type to repository */
268
269 static SilcBool silc_skr_add_entry(SilcSKR skr, SilcSKRFindType type,
270                                    void *type_data, SilcSKRKeyInternal key)
271 {
272   SilcSKREntry entry;
273
274 #if defined(SILC_DEBUG)
275   char tmp[256];
276   memset(tmp, 0, sizeof(tmp));
277   silc_skr_type_string(type, type_data, tmp, sizeof(tmp) - 1);
278   SILC_LOG_DEBUG(("Search constraint %s", tmp));
279 #endif /* SILC_DEBUG */
280
281   entry = silc_calloc(1, sizeof(*entry));
282   if (!entry)
283     return FALSE;
284
285   entry->type = type;
286   entry->data = type_data;
287
288   return silc_hash_table_add(skr->keys, entry, key);
289 }
290
291 /* Delete a key by search constraint type from repository */
292
293 static SilcBool silc_skr_del_entry(SilcSKR skr, SilcSKRFindType type,
294                                    void *type_data, SilcSKRKeyInternal key)
295 {
296   SilcSKREntryStruct entry;
297
298   if (!type_data)
299     return FALSE;
300
301   entry.type = type;
302   entry.data = type_data;
303
304   return silc_hash_table_del_by_context(skr->keys, &entry, key);
305 }
306
307 /* This performs AND operation.  Any entry already in `results' that is not
308    in `list' will be removed from `results'. */
309
310 static SilcBool silc_skr_results_and(SilcDList list, SilcSKRStatus *status,
311                                      SilcDList *results)
312 {
313   SilcSKRKeyInternal entry, r;
314
315   if (*results == NULL) {
316     *results = silc_dlist_init();
317     if (*results == NULL) {
318       *status |= SILC_SKR_NO_MEMORY;
319       return FALSE;
320     }
321   }
322
323   /* If results is empty, just add all entries from list to results */
324   if (!silc_dlist_count(*results)) {
325     silc_dlist_start(list);
326     while ((entry = silc_dlist_get(list)) != SILC_LIST_END)
327       silc_dlist_add(*results, entry);
328
329     return TRUE;
330   }
331
332   silc_dlist_start(*results);
333   while ((entry = silc_dlist_get(*results)) != SILC_LIST_END) {
334
335     /* Check if this entry is in list  */
336     silc_dlist_start(list);
337     while ((r = silc_dlist_get(list)) != SILC_LIST_END) {
338       if (r == entry)
339         break;
340     }
341     if (r != SILC_LIST_END)
342       continue;
343
344     /* Remove from results */
345     silc_dlist_del(*results, entry);
346   }
347
348   /* If results became empty, we did not find any key */
349   if (!silc_dlist_count(*results)) {
350     SILC_LOG_DEBUG(("Not all search constraints found"));
351     *status |= SILC_SKR_NOT_FOUND;
352     return FALSE;
353   }
354
355   return TRUE;
356 }
357
358
359 /***************************** SILC Public Key ******************************/
360
361 /* Add SILC style public key to repository */
362
363 static SilcSKRStatus silc_skr_add_silc(SilcSKR skr,
364                                        SilcPublicKey public_key,
365                                        SilcSKRKeyUsage usage,
366                                        void *key_context,
367                                        SilcSKRKey *return_key)
368 {
369   SilcSKRKeyInternal key;
370   SilcSKRStatus status = SILC_SKR_ERROR;
371   SilcPublicKeyIdentifier ident;
372   SilcSILCPublicKey silc_pubkey;
373 #if defined(SILC_DEBUG)
374   char tmp[256];
375 #endif /* SILC_DEBUG */
376
377   /* Get the SILC public key */
378   silc_pubkey = silc_pkcs_get_context(SILC_PKCS_SILC, public_key);
379   ident = &silc_pubkey->identifier;
380
381   SILC_LOG_DEBUG(("Adding SILC public key %p [%s], context %p",
382                   public_key, ident->username, key_context));
383
384   silc_mutex_lock(skr->lock);
385
386   /* Check that this key hasn't been added already */
387   if (silc_skr_find_entry(skr, &status, SILC_SKR_FIND_PUBLIC_KEY,
388                           public_key, NULL, key_context, 0)) {
389     silc_mutex_unlock(skr->lock);
390     SILC_LOG_DEBUG(("Key already added"));
391     return status | SILC_SKR_ALREADY_EXIST;
392   }
393
394   /* Allocate key entry */
395   key = silc_calloc(1, sizeof(*key));
396   if (!key) {
397     silc_mutex_unlock(skr->lock);
398     return status | SILC_SKR_NO_MEMORY;
399   }
400
401   key->key.usage = usage;
402   key->key.key = public_key;
403   key->key.key_context = key_context;
404
405 #if defined(SILC_DEBUG)
406   silc_skr_type_string(SILC_SKR_FIND_USAGE, SILC_32_TO_PTR(usage),
407                        tmp, sizeof(tmp) - 1);
408   SILC_LOG_DEBUG((" Search constraint %s", tmp));
409 #endif /* SILC_DEBUG */
410
411   /* Add key specifics */
412
413   if (!silc_skr_add_entry(skr, SILC_SKR_FIND_PUBLIC_KEY,
414                           public_key, key))
415     goto err;
416   key->refcnt++;
417
418   if (!silc_skr_add_entry(skr, SILC_SKR_FIND_PKCS_TYPE,
419                           SILC_32_TO_PTR(SILC_PKCS_SILC), key))
420     goto err;
421   key->refcnt++;
422
423   if (ident->username) {
424     if (!silc_skr_add_entry(skr, SILC_SKR_FIND_USERNAME,
425                             ident->username, key))
426       goto err;
427     key->refcnt++;
428   }
429
430   if (ident->host) {
431     if (!silc_skr_add_entry(skr, SILC_SKR_FIND_HOST,
432                             ident->host, key))
433       goto err;
434     key->refcnt++;
435   }
436
437   if (ident->realname) {
438     if (!silc_skr_add_entry(skr, SILC_SKR_FIND_REALNAME,
439                             ident->realname, key))
440       goto err;
441     key->refcnt++;
442   }
443
444   if (ident->email) {
445     if (!silc_skr_add_entry(skr, SILC_SKR_FIND_EMAIL,
446                             ident->email, key))
447       goto err;
448     key->refcnt++;
449   }
450
451   if (ident->org) {
452     if (!silc_skr_add_entry(skr, SILC_SKR_FIND_ORG,
453                             ident->org, key))
454       goto err;
455     key->refcnt++;
456   }
457
458   if (ident->country) {
459     if (!silc_skr_add_entry(skr, SILC_SKR_FIND_COUNTRY,
460                             ident->country, key))
461       goto err;
462     key->refcnt++;
463   }
464
465   if (key_context) {
466     if (!silc_skr_add_entry(skr, SILC_SKR_FIND_CONTEXT,
467                             key_context, key))
468       goto err;
469     key->refcnt++;
470   }
471
472   silc_mutex_unlock(skr->lock);
473
474   if (return_key)
475     *return_key = (SilcSKRKey)key;
476
477   return SILC_SKR_OK;
478
479  err:
480   silc_mutex_unlock(skr->lock);
481   return status;
482 }
483
484 /* Add SILC style public key to repository, and only the public key, not
485    other details from the key. */
486
487 static SilcSKRStatus silc_skr_add_silc_simple(SilcSKR skr,
488                                               SilcPublicKey public_key,
489                                               SilcSKRKeyUsage usage,
490                                               void *key_context,
491                                               SilcSKRKey *return_key)
492 {
493   SilcSKRKeyInternal key;
494   SilcSKRStatus status = SILC_SKR_ERROR;
495
496   SILC_LOG_DEBUG(("Adding SILC public key"));
497
498   silc_mutex_lock(skr->lock);
499
500   /* Check that this key hasn't been added already */
501   if (silc_skr_find_entry(skr, &status, SILC_SKR_FIND_PUBLIC_KEY,
502                           public_key, NULL, key_context, 0)) {
503     silc_mutex_unlock(skr->lock);
504     SILC_LOG_DEBUG(("Key already added"));
505     return status | SILC_SKR_ALREADY_EXIST;
506   }
507
508   /* Allocate key entry */
509   key = silc_calloc(1, sizeof(*key));
510   if (!key) {
511     silc_mutex_unlock(skr->lock);
512     return status | SILC_SKR_NO_MEMORY;
513   }
514
515   key->key.usage = usage;
516   key->key.key = public_key;
517   key->key.key_context = key_context;
518
519   /* Add key specifics */
520
521   if (!silc_skr_add_entry(skr, SILC_SKR_FIND_PUBLIC_KEY,
522                           public_key, key))
523     goto err;
524   key->refcnt++;
525
526   if (key_context) {
527     if (!silc_skr_add_entry(skr, SILC_SKR_FIND_CONTEXT,
528                             key_context, key))
529       goto err;
530     key->refcnt++;
531   }
532
533   silc_mutex_unlock(skr->lock);
534
535   if (return_key)
536     *return_key = (SilcSKRKey)key;
537
538   return SILC_SKR_OK;
539
540  err:
541   silc_mutex_unlock(skr->lock);
542   return status;
543 }
544
545 /* Deletes SILC public key from repository */
546
547 static SilcSKRStatus silc_skr_del_silc_public_key(SilcSKR skr,
548                                                   SilcPublicKey public_key,
549                                                   void *key_context)
550 {
551   SilcSKRStatus status = SILC_SKR_ERROR;
552   SilcPublicKeyIdentifier ident;
553   SilcSILCPublicKey silc_pubkey;
554   SilcSKRKeyInternal key;
555   SilcDList entry;
556
557   /* Get the SILC public key */
558   silc_pubkey = silc_pkcs_get_context(SILC_PKCS_SILC, public_key);
559   ident = &silc_pubkey->identifier;
560
561   SILC_LOG_DEBUG(("Deleting SILC public key [%s]", ident->username));
562
563   silc_mutex_lock(skr->lock);
564
565   /* Check that this key exists */
566   if (!silc_skr_find_entry(skr, &status, SILC_SKR_FIND_PUBLIC_KEY,
567                            public_key, &entry, key_context, 0)) {
568     silc_mutex_unlock(skr->lock);
569     SILC_LOG_DEBUG(("Key does not exist"));
570     return status | SILC_SKR_NOT_FOUND;
571   }
572
573   silc_dlist_start(entry);
574   key = silc_dlist_get(entry);
575   silc_dlist_uninit(entry);
576
577   silc_skr_del_entry(skr, SILC_SKR_FIND_PUBLIC_KEY, public_key, key);
578   silc_skr_del_entry(skr, SILC_SKR_FIND_PKCS_TYPE,
579                      SILC_32_TO_PTR(SILC_PKCS_SILC), key);
580   silc_skr_del_entry(skr, SILC_SKR_FIND_USERNAME, ident->username, key);
581   silc_skr_del_entry(skr, SILC_SKR_FIND_HOST, ident->host, key);
582   silc_skr_del_entry(skr, SILC_SKR_FIND_REALNAME, ident->realname, key);
583   silc_skr_del_entry(skr, SILC_SKR_FIND_EMAIL, ident->email, key);
584   silc_skr_del_entry(skr, SILC_SKR_FIND_ORG, ident->org, key);
585   silc_skr_del_entry(skr, SILC_SKR_FIND_COUNTRY, ident->country, key);
586   silc_skr_del_entry(skr, SILC_SKR_FIND_CONTEXT, key_context, key);
587
588   silc_mutex_unlock(skr->lock);
589
590   return SILC_SKR_OK;
591 }
592
593
594 /**************************** Key Repository API ****************************/
595
596 /* Allocate key repository */
597
598 SilcSKR silc_skr_alloc(void)
599 {
600   SilcSKR skr;
601
602   skr = silc_calloc(1, sizeof(*skr));
603   if (!skr)
604     return NULL;
605
606   if (!silc_skr_init(skr)) {
607     silc_skr_free(skr);
608     return NULL;
609   }
610
611   return skr;
612 }
613
614 /* Free key repository */
615
616 void silc_skr_free(SilcSKR skr)
617 {
618   silc_skr_uninit(skr);
619   silc_free(skr);
620 }
621
622 /* Initializes key repository */
623
624 SilcBool silc_skr_init(SilcSKR skr)
625 {
626   if (!silc_mutex_alloc(&skr->lock))
627     return FALSE;
628
629   skr->keys = silc_hash_table_alloc(0, silc_skr_hash, NULL,
630                                     silc_skr_compare, NULL,
631                                     silc_skr_destructor, NULL, TRUE);
632   if (!skr->keys)
633     return FALSE;
634
635   return TRUE;
636 }
637
638 /* Uninitializes key repository */
639
640 void silc_skr_uninit(SilcSKR skr)
641 {
642   if (skr->keys)
643     silc_hash_table_free(skr->keys);
644   silc_mutex_free(skr->lock);
645 }
646
647 /* Adds public key to key repository */
648
649 SilcSKRStatus silc_skr_add_public_key(SilcSKR skr,
650                                       SilcPublicKey public_key,
651                                       SilcSKRKeyUsage usage,
652                                       void *key_context,
653                                       SilcSKRKey *return_key)
654 {
655   SilcPKCSType type;
656
657   if (!public_key)
658     return SILC_SKR_ERROR;
659
660   type = silc_pkcs_get_type(public_key);
661
662   SILC_LOG_DEBUG(("Adding public key %p to repository", public_key));
663
664   switch (type) {
665
666   case SILC_PKCS_SILC:
667     return silc_skr_add_silc(skr, public_key, usage, key_context, return_key);
668     break;
669
670   default:
671     break;
672   }
673
674   return SILC_SKR_ERROR;
675 }
676
677 /* Adds public key to repository. */
678
679 SilcSKRStatus silc_skr_add_public_key_simple(SilcSKR skr,
680                                              SilcPublicKey public_key,
681                                              SilcSKRKeyUsage usage,
682                                              void *key_context,
683                                              SilcSKRKey *return_key)
684 {
685   SilcPKCSType type;
686
687   if (!public_key)
688     return SILC_SKR_ERROR;
689
690   type = silc_pkcs_get_type(public_key);
691
692   SILC_LOG_DEBUG(("Adding public key %p to repository", public_key));
693
694   switch (type) {
695
696   case SILC_PKCS_SILC:
697     return silc_skr_add_silc_simple(skr, public_key, usage, key_context,
698                                     return_key);
699     break;
700
701   default:
702     break;
703   }
704
705   return SILC_SKR_ERROR;
706 }
707
708 /* Remove key from repository */
709
710 SilcSKRStatus silc_skr_del_public_key(SilcSKR skr,
711                                       SilcPublicKey public_key,
712                                       void *key_context)
713 {
714   SilcPKCSType type;
715
716   if (!public_key)
717     return SILC_SKR_ERROR;
718
719   type = silc_pkcs_get_type(public_key);
720
721   SILC_LOG_DEBUG(("Deleting public key %p from repository", public_key));
722
723   switch (type) {
724
725   case SILC_PKCS_SILC:
726     return silc_skr_del_silc_public_key(skr, public_key, key_context);
727     break;
728
729   default:
730     break;
731   }
732
733   return SILC_SKR_ERROR;
734 }
735
736 /* Reference key */
737
738 void silc_skr_ref_public_key(SilcSKR skr, SilcSKRKey key)
739 {
740   SilcSKRKeyInternal k = (SilcSKRKeyInternal)key;
741
742   silc_mutex_lock(skr->lock);
743   SILC_LOG_DEBUG(("SKR key %p ref %d -> %d", k->refcnt, k->refcnt + 1));
744   k->refcnt++;
745   silc_mutex_unlock(skr->lock);
746 }
747
748 /* Release key reference. */
749
750 void silc_skr_unref_public_key(SilcSKR skr, SilcSKRKey key)
751 {
752   SilcSKRKeyInternal k = (SilcSKRKeyInternal)key;
753
754   silc_mutex_lock(skr->lock);
755
756   SILC_LOG_DEBUG(("SKR key %p ref %d -> %d", k->refcnt, k->refcnt - 1));
757   k->refcnt--;
758
759   if (k->refcnt == 0) {
760     /* If reference is zero, the key has been removed from the repository
761        already.  Just destroy the public key. */
762     silc_pkcs_public_key_free(key->key);
763     silc_free(key);
764   }
765
766   silc_mutex_unlock(skr->lock);
767 }
768
769
770 /************************** Search Constraints API **************************/
771
772 /* Allocate search constraints */
773
774 SilcSKRFind silc_skr_find_alloc(void)
775 {
776   SilcSKRFind find;
777
778   find = silc_calloc(1, sizeof(*find));
779   if (!find)
780     return NULL;
781
782   find->constr = silc_hash_table_alloc(0, silc_hash_uint, NULL, NULL, NULL,
783                                        silc_skr_find_destructor, NULL, TRUE);
784   if (!find->constr) {
785     silc_skr_find_free(find);
786     return NULL;
787   }
788
789   return find;
790 }
791
792 /* Free search constraints */
793
794 void silc_skr_find_free(SilcSKRFind find)
795 {
796   if (find->constr)
797     silc_hash_table_free(find->constr);
798   silc_free(find);
799 }
800
801 SilcBool silc_skr_find_set_pkcs_type(SilcSKRFind find, SilcPKCSType type)
802 {
803   return silc_hash_table_add(find->constr,
804                              SILC_32_TO_PTR(SILC_SKR_FIND_PKCS_TYPE),
805                              SILC_32_TO_PTR(type));
806 }
807
808 SilcBool silc_skr_find_set_username(SilcSKRFind find, const char *username)
809 {
810   void *c = silc_memdup(username, strlen(username));
811   if (!c)
812     return FALSE;
813   return silc_hash_table_add(find->constr,
814                              SILC_32_TO_PTR(SILC_SKR_FIND_USERNAME), c);
815 }
816
817 SilcBool silc_skr_find_set_host(SilcSKRFind find, const char *host)
818 {
819   void *c = silc_memdup(host, strlen(host));
820   if (!c)
821     return FALSE;
822   return silc_hash_table_add(find->constr,
823                              SILC_32_TO_PTR(SILC_SKR_FIND_HOST), c);
824 }
825
826 SilcBool silc_skr_find_set_realname(SilcSKRFind find, const char *realname)
827 {
828   void *c = silc_memdup(realname, strlen(realname));
829   if (!c)
830     return FALSE;
831   return silc_hash_table_add(find->constr,
832                              SILC_32_TO_PTR(SILC_SKR_FIND_REALNAME), c);
833 }
834
835 SilcBool silc_skr_find_set_email(SilcSKRFind find, const char *email)
836 {
837   void *c = silc_memdup(email, strlen(email));
838   if (!c)
839     return FALSE;
840   return silc_hash_table_add(find->constr,
841                              SILC_32_TO_PTR(SILC_SKR_FIND_EMAIL), c);
842 }
843
844 SilcBool silc_skr_find_set_org(SilcSKRFind find, const char *org)
845 {
846   void *c = silc_memdup(org, strlen(org));
847   if (!c)
848     return FALSE;
849   return silc_hash_table_add(find->constr,
850                              SILC_32_TO_PTR(SILC_SKR_FIND_ORG), c);
851 }
852
853 SilcBool silc_skr_find_set_country(SilcSKRFind find, const char *country)
854 {
855   void *c = silc_memdup(country, strlen(country));
856   if (!c)
857     return FALSE;
858   return silc_hash_table_add(find->constr,
859                              SILC_32_TO_PTR(SILC_SKR_FIND_COUNTRY), c);
860 }
861
862 SilcBool silc_skr_find_set_public_key(SilcSKRFind find,
863                                       SilcPublicKey public_key)
864 {
865   SilcPublicKey pk = silc_pkcs_public_key_copy(public_key);
866   if (!pk)
867     return FALSE;
868   return silc_hash_table_add(find->constr,
869                              SILC_32_TO_PTR(SILC_SKR_FIND_PUBLIC_KEY), pk);
870 }
871
872 SilcBool silc_skr_find_set_context(SilcSKRFind find, void *context)
873 {
874   if (!context)
875     return TRUE;
876   return silc_hash_table_add(find->constr,
877                              SILC_32_TO_PTR(SILC_SKR_FIND_CONTEXT), context);
878 }
879
880 SilcBool silc_skr_find_set_usage(SilcSKRFind find, SilcSKRKeyUsage usage)
881 {
882   if (!usage)
883     return TRUE;
884   return silc_hash_table_add(find->constr,
885                              SILC_32_TO_PTR(SILC_SKR_FIND_USAGE),
886                              SILC_32_TO_PTR(usage));
887 }
888
889 /******************************** Search API ********************************/
890
891 /* Finds key(s) by the set search constraints.  The callback will be called
892    once keys has been found. */
893 /* This is now synchronous function but may later change async */
894
895 SilcAsyncOperation silc_skr_find(SilcSKR skr, SilcSchedule schedule,
896                                  SilcSKRFind find,
897                                  SilcSKRFindCallback callback,
898                                  void *callback_context)
899 {
900   SilcSKRStatus status = SILC_SKR_ERROR;
901   SilcHashTableList htl;
902   SilcDList list, results = NULL;
903   void *type, *ctx, *usage = NULL;
904 #if defined(SILC_DEBUG)
905   char tmp[256];
906 #endif /* SILC_DEBUG */
907
908   SILC_LOG_DEBUG(("Finding key from repository"));
909
910   if (!find || !callback)
911     return NULL;
912
913   silc_mutex_lock(skr->lock);
914
915   /* Get usage bits, if searching by them */
916   silc_hash_table_find(find->constr, SILC_32_TO_PTR(SILC_SKR_FIND_USAGE),
917                        NULL, &usage);
918
919 #if defined(SILC_DEBUG)
920   if (usage) {
921     memset(tmp, 0, sizeof(tmp));
922     silc_skr_type_string(SILC_SKR_FIND_USAGE, usage, tmp, sizeof(tmp) - 1);
923     SILC_LOG_DEBUG(("Finding key by %s", tmp));
924   }
925 #endif /* SILC_DEBUG */
926
927   silc_hash_table_list(find->constr, &htl);
928   while (silc_hash_table_get(&htl, &type, &ctx)) {
929
930     /* SILC_SKR_FIND_USAGE is handled separately while searching the keys. */
931     if ((SilcSKRFindType)SILC_32_TO_PTR(type) == SILC_SKR_FIND_USAGE)
932       continue;
933
934 #if defined(SILC_DEBUG)
935     memset(tmp, 0, sizeof(tmp));
936     silc_skr_type_string((SilcSKRFindType)SILC_32_TO_PTR(type),
937                          ctx, tmp, sizeof(tmp) - 1);
938     SILC_LOG_DEBUG(("Finding key by %s", tmp));
939 #endif /* SILC_DEBUG */
940
941     /* Find entries by this search constraint */
942     if (!silc_skr_find_entry(skr, &status,
943                              (SilcSKRFindType)SILC_32_TO_PTR(type),
944                              ctx, &list, NULL, SILC_PTR_TO_32(usage))) {
945       SILC_LOG_DEBUG(("Not found"));
946       if (results) {
947         silc_dlist_uninit(results);
948         results = NULL;
949       }
950       break;
951     }
952
953     /* For now, our logic rule is AND.  All constraints must be found
954        to find the key.  Later OR might be added also. */
955     if (!silc_skr_results_and(list, &status, &results)) {
956       SILC_LOG_DEBUG(("Not found"));
957       if (results) {
958         silc_dlist_uninit(results);
959         results = NULL;
960       }
961       silc_dlist_uninit(list);
962       break;
963     }
964
965     silc_dlist_uninit(list);
966   }
967   silc_hash_table_list_reset(&htl);
968
969   silc_mutex_unlock(skr->lock);
970
971   /* Return results */
972   if (!results) {
973     callback(skr, find, status, NULL, callback_context);
974   } else {
975     silc_dlist_start(results);
976     callback(skr, find, SILC_SKR_OK, results, callback_context);
977   }
978
979   return NULL;
980 }