feisty meow concerns codebase 2.140
encryption_infoton.cpp
Go to the documentation of this file.
1/*****************************************************************************\
2* *
3* Name : encryption_infoton *
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 "encryption_infoton.h"
16
17#include <basis/byte_array.h>
18
19#include <basis/mutex.h>
20#include <basis/functions.h>
22#include <crypto/rsa_crypto.h>
23#include <octopus/tentacle.h>
26
27using namespace basis;
28using namespace crypto;
29using namespace octopi;
30using namespace structures;
31using namespace textual;
32
33namespace octopi {
34
36 // our key size is almost double the recommended key size (168 bits).
37 // this would take a very long time to crack using brute force.
38
40 // a little bit larger than the 1024 bit threshold.
41
42#undef LOG
43#define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger::get(), s);
44
46 const byte_array &secret_blowfish)
47: infoton(encryption_classifier()),
48 _public_key(pub_key),
49 _encrypted_blowfish_key(secret_blowfish),
50 _success(tentacle::NOT_FOUND)
51{}
52
54: root_object(),
55 infoton(to_copy),
56 _public_key(to_copy._public_key),
57 _encrypted_blowfish_key(to_copy._encrypted_blowfish_key),
58 _success(to_copy._success)
59{
60}
61
63
65{ return cloner<encryption_infoton>(*this); }
66
67encryption_infoton &encryption_infoton::operator =
68 (const encryption_infoton &to_copy)
69{
70 if (this == &to_copy) return *this;
71 _public_key = to_copy._public_key;
72 _encrypted_blowfish_key = to_copy._encrypted_blowfish_key;
73 _success = to_copy._success;
74 return *this;
75}
76
77const char *encryption_class_constant = "#octcod";
78
81
83{
84 return sizeof(int) // packed outcome.
85 + _public_key.length() + sizeof(int) // public key array.
86 + _encrypted_blowfish_key.length() + sizeof(int); // secret key array.
87}
88
89void encryption_infoton::pack(byte_array &packed_form) const
90{
91 structures::attach(packed_form, _success.value());
92 structures::attach(packed_form, _public_key);
94}
95
97{
98 int value;
99 if (!structures::detach(packed_form, value)) return false;
100 _success = outcome(value);
101 if (!structures::detach(packed_form, _public_key)) return false;
102 if (!structures::detach(packed_form, _encrypted_blowfish_key)) return false;
103 return true;
104}
105
107{
108 FUNCDEF("prepare_blowfish_key");
109 _encrypted_blowfish_key.reset(); // clean out stuff to create.
110 if (!_public_key.length()) {
111 // wrong type of request being seen or something.
113 return _success;
114 }
115
116 rsa_crypto pub(_public_key); // suck in the provided key.
117 blowfish_crypto agreed_key(BLOWFISH_KEY_SIZE); // random blowfish key.
118 new_key = agreed_key;
119
120 // now encrypt the new key for transit.
121 bool worked = pub.public_encrypt(agreed_key.get_key(),
123 if (!worked) _success = tentacle::GARBAGE; // lacking a better description.
125 return _success;
126}
127
129{
130 rsa_crypto priv(RSA_KEY_SIZE); // generate random key.
131 outcome to_return = prepare_public_key(priv);
132 if (to_return == tentacle::OKAY) private_key = priv;
133 return to_return;
134}
135
137{
138 bool worked = private_key.public_key(_public_key);
139 if (!worked) return tentacle::DISALLOWED; // why would that ever fail?
140 return tentacle::OKAY;
141}
142
144 blowfish_crypto &new_key) const
145{
146 FUNCDEF("extract_response");
147 if (_success != tentacle::OKAY) return _success;
148 byte_array decrypted;
149 bool worked = private_key.private_decrypt(_encrypted_blowfish_key, decrypted);
150 if (!worked) return tentacle::BAD_INPUT; // that one we hope is accurate.
151 new_key.set_key(decrypted, BLOWFISH_KEY_SIZE);
152 return tentacle::OKAY;
153}
154
155} //namespace.
156
void reset(int number=0, const contents *initial_contents=NULL_POINTER)
Resizes this array and sets the contents from an array of contents.
Definition array.h:349
int length() const
Returns the current reported length of the allocated C array.
Definition array.h:115
A very common template for a dynamic array of bytes.
Definition byte_array.h:36
A clonable object knows how to make copy of itself.
Definition contracts.h:109
Outcomes describe the state of completion for an operation.
Definition outcome.h:31
int value() const
Definition outcome.h:51
Provides BlowFish encryption on byte_arrays using the OpenSSL package.
bool set_key(const basis::byte_array &new_key, int key_size)
sets the encryption key to "new_key".
const basis::byte_array & get_key() const
returns our current key.
Supports public key encryption and decryption.
Definition rsa_crypto.h:33
bool public_encrypt(const basis::byte_array &source, basis::byte_array &target) const
encrypts "source" using our public key and stores it in "target".
bool private_decrypt(const basis::byte_array &source, basis::byte_array &target) const
decrypts "source" using our private key and stores it in "target".
bool public_key(basis::byte_array &pubkey) const
makes a copy of the public key held here.
Encapsulates the chit-chat necessary to establish an encrypted connection.
basis::outcome prepare_public_key(const crypto::rsa_crypto &private_key)
prepares the request side for a client.
basis::outcome extract_response(const crypto::rsa_crypto &private_key, crypto::blowfish_crypto &new_key) const
used by the client to extract the shared blowfish key from the server.
encryption_infoton(const basis::byte_array &public_key=basis::byte_array::empty_array(), const basis::byte_array &encrypted_blowfish_key=basis::byte_array::empty_array())
virtual int packed_size() const
reports how large the infoton will be when packed.
basis::outcome prepare_both_keys(crypto::rsa_crypto &private_key)
sets up both keys by randomly generating the "private_key".
basis::byte_array _public_key
valid during the request stage of encryption.
basis::outcome prepare_blowfish_key(crypto::blowfish_crypto &new_key)
performs the server side's job on the current key.
virtual clonable * clone() const
must be provided to allow creation of a copy of this object.
static const structures::string_array & encryption_classifier()
returns the classifier for this type of infoton.
basis::outcome _success
did the request succeed?
static const int RSA_KEY_SIZE
this key size should be used for all RSA private keys.
virtual void pack(basis::byte_array &packed_form) const
stuffs the data in the infoton into the "packed_form".
static const int BLOWFISH_KEY_SIZE
this will be used for blowfish keys that this object generates.
basis::byte_array _encrypted_blowfish_key
valid during the response stage of encryption.
virtual bool unpack(basis::byte_array &packed_form)
restores an infoton from a packed form.
An infoton is an individual request parcel with accompanying information.
Definition infoton.h:32
Manages a service within an octopus by processing certain infotons.
Definition tentacle.h:36
An array of strings with some additional helpful methods.
#define FUNCDEF(func_in)
FUNCDEF sets the name of a function (and plugs it into the callstack).
Definition enhance_cpp.h:54
The guards collection helps in testing preconditions and reporting errors.
Definition array.h:30
const char * encryption_class_constant
A dynamic container class that holds any kind of object via pointers.
Definition amorph.h:55
void attach(byte_array &packed_form, const byte_array &to_attach)
Packs a byte_array "to_attach" into "packed_form".
bool detach(byte_array &packed_form, byte_array &to_detach)
Unpacks a byte_array "to_detach" from "packed_form".
#define SAFE_STATIC_CONST(type, func_name, parms)
this version returns a constant object instead.