feisty meow concerns codebase  2.140
key_repository.cpp
Go to the documentation of this file.
1 /*****************************************************************************\
2 * *
3 * Name : key_repository *
4 * Author : Chris Koeritz *
5 * *
6 *******************************************************************************
7 * Copyright (c) 2004-$now By Author. This program is free software; you can *
8 * redistribute it and/or modify it under the terms of the GNU General Public *
9 * License as published by the Free Software Foundation; either version 2 of *
10 * the License or (at your option) any later version. This is online at: *
11 * http://www.fsf.org/copyleft/gpl.html *
12 * Please send any updates to: fred@gruntose.com *
13 \*****************************************************************************/
14 
15 #include "key_repository.h"
16 
17 #include <crypto/blowfish_crypto.h>
19 
20 using namespace basis;
21 using namespace crypto;
22 using namespace structures;
23 
24 namespace octopi {
25 
26 #undef LOG
27 #define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger::get(), s)
28 
29 //#define DEBUG_KEY_REPOSITORY
30  // uncomment for noisier execution. beware however, if the uls is in
31  // use, this can cause infinite recursion.
32 
33 key_repository::~key_repository() {}
34 
35 octenc_key_record *key_repository::lock(const octopus_entity &ent)
36 {
37 #ifdef DEBUG_KEY_REPOSITORY
38  FUNCDEF("lock");
39  LOG(astring("entity sought=") + ent.text_form());
40 #endif
41  octenc_key_record *to_return = NULL_POINTER;
42  _locker.lock();
43  to_return = _keys.find(ent.mangled_form());
44  if (!to_return) {
45 #ifdef DEBUG_KEY_REPOSITORY
46  LOG(astring("did not find entity=") + ent.text_form());
47 #endif
48  _locker.unlock();
49  } else {
50 #ifdef DEBUG_KEY_REPOSITORY
51  LOG(astring("found entity=") + ent.text_form());
52 #endif
53  }
54  return to_return;
55 }
56 
57 void key_repository::unlock(octenc_key_record *to_unlock)
58 {
59  if (!to_unlock) return; // dolts! they cannot unlock a non-record.
60  _locker.unlock();
61 }
62 
63 outcome key_repository::add(const octopus_entity &ent,
64  const blowfish_crypto &key)
65 {
66 #ifdef DEBUG_KEY_REPOSITORY
67  FUNCDEF("add");
68  LOG(astring("adding key for entity=") + ent.text_form());
69 #endif
70  auto_synchronizer loc(_locker);
71  octenc_key_record rec(ent, key);
72  return _keys.add(ent.mangled_form(), rec);
73 }
74 
75 outcome key_repository::whack(const octopus_entity &ent)
76 {
77 #ifdef DEBUG_KEY_REPOSITORY
78  FUNCDEF("whack");
79  LOG(astring("removing key for entity=") + ent.text_form());
80 #endif
81  auto_synchronizer loc(_locker);
82  return _keys.whack(ent.mangled_form());
83 }
84 
85 } //namespace.
86 
Provides a dynamically resizable ASCII character string.
Definition: astring.h:35
auto_synchronizer simplifies concurrent code by automatically unlocking.
Definition: mutex.h:113
Outcomes describe the state of completion for an operation.
Definition: outcome.h:31
Provides BlowFish encryption on byte_arrays using the OpenSSL package.
Tracks the keys that have been assigned for a secure channel.
Provides a way of identifying users of an octopus object.
Definition: entity_defs.h:35
basis::astring text_form() const
returns a readable form of the identifier.
basis::astring mangled_form() const
returns the combined string form of the identifier.
#define NULL_POINTER
The value representing a pointer to nothing.
Definition: definitions.h:32
#define FUNCDEF(func_in)
FUNCDEF sets the name of a function (and plugs it into the callstack).
Definition: enhance_cpp.h:57
#define LOG(s)
The guards collection helps in testing preconditions and reporting errors.
Definition: array.h:30
A dynamic container class that holds any kind of object via pointers.
Definition: amorph.h:55