first check-in of feisty meow codebase. many things broken still due to recent
[feisty_meow.git] / octopi / library / tentacles / key_repository.h
1 #ifndef KEY_REPOSITORY_CLASS
2 #define KEY_REPOSITORY_CLASS
3
4 /*****************************************************************************\
5 *                                                                             *
6 *  Name   : key_repository                                                    *
7 *  Author : Chris Koeritz                                                     *
8 *                                                                             *
9 *******************************************************************************
10 * Copyright (c) 2004-$now By Author.  This program is free software; you can  *
11 * redistribute it and/or modify it under the terms of the GNU General Public  *
12 * License as published by the Free Software Foundation; either version 2 of   *
13 * the License or (at your option) any later version.  This is online at:      *
14 *     http://www.fsf.org/copyleft/gpl.html                                    *
15 * Please send any updates to: fred@gruntose.com                               *
16 \*****************************************************************************/
17
18 #include <basis/mutex.h>
19 #include <crypto/blowfish_crypto.h>
20 #include <structures/symbol_table.h>
21 #include <octopus/entity_defs.h>
22
23 namespace octopi {
24
25 //! Tracks the keys that have been assigned for a secure channel.
26 /*!
27   This class is thread-safe, as long as one uses the lock() method below in
28   the proper manner.
29
30   NOTE: this is a heavy-weight header; do not include in other headers.
31 */
32
33 class octenc_key_record
34 {
35 public:
36   octopus_entity _entity;  //!< who the key belongs to.
37   crypto::blowfish_crypto _key;  //!< used for communicating with an entity.
38
39   octenc_key_record() : _key(200) {}  //!< bogus blank constructor.
40
41   octenc_key_record(const octopus_entity &entity, const crypto::blowfish_crypto &key) 
42   : _entity(entity), _key(key) {}
43 };
44
45 //////////////
46
47 class key_repository
48 {
49 public:
50   key_repository() : _locker(), _keys() {}
51   virtual ~key_repository();
52
53   DEFINE_CLASS_NAME("key_repository");
54
55   octenc_key_record *lock(const octopus_entity &ent);
56     //!< locates the key for "ent", if it's stored.
57     /*!< the returned object, unless it's NIL, must be unlocked. */
58
59   void unlock(octenc_key_record *to_unlock);
60     //!< drops the lock on the key record in "to_unlock".
61
62   basis::outcome add(const octopus_entity &ent, const crypto::blowfish_crypto &key);
63     //!< adds a "key" for the "ent".  this will fail if one is already listed.
64
65   basis::outcome whack(const octopus_entity &ent);
66     //!< removes the key for "ent".
67
68 private:
69   basis::mutex _locker;  //!< protects our list of keys.
70   structures::symbol_table<octenc_key_record> _keys;  //!< the list of keys.
71 };
72
73 } //namespace.
74
75 #endif // outer guard.
76