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>
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 // maximum provided for in standard encryptions.
37
39 // a little bit larger than the 1024 bit threshold.
40
41#undef LOG
42#define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger::get(), s);
43
45 const byte_array &secret_blowfish)
46: infoton(encryption_classifier()),
47 _public_key(pub_key),
48 _encrypted_blowfish_key(secret_blowfish),
49 _success(tentacle::NOT_FOUND)
50{}
51
53: root_object(),
54 infoton(to_copy),
55 _public_key(to_copy._public_key),
56 _encrypted_blowfish_key(to_copy._encrypted_blowfish_key),
57 _success(to_copy._success)
58{
59}
60
62
64{ return cloner<encryption_infoton>(*this); }
65
66encryption_infoton &encryption_infoton::operator =
67 (const encryption_infoton &to_copy)
68{
69 if (this == &to_copy) return *this;
70 _public_key = to_copy._public_key;
71 _encrypted_blowfish_key = to_copy._encrypted_blowfish_key;
72 _success = to_copy._success;
73 return *this;
74}
75
76const char *encryption_class_constant = "#octcod";
77
80
82{
83 return sizeof(int) // packed outcome.
84 + _public_key.length() + sizeof(int) // public key array.
85 + _encrypted_blowfish_key.length() + sizeof(int); // secret key array.
86}
87
88void encryption_infoton::pack(byte_array &packed_form) const
89{
90 structures::attach(packed_form, _success.value());
91 structures::attach(packed_form, _public_key);
93}
94
96{
97 int value;
98 if (!structures::detach(packed_form, value)) return false;
99 _success = outcome(value);
100 if (!structures::detach(packed_form, _public_key)) return false;
101 if (!structures::detach(packed_form, _encrypted_blowfish_key)) return false;
102 return true;
103}
104
106{
107 FUNCDEF("prepare_blowfish_key");
108 _encrypted_blowfish_key.reset(); // clean out stuff to create.
109 if (!_public_key.length()) {
110 // wrong type of request being seen or something.
112 return _success;
113 }
114
115 old_school_rsa_crypto pub(_public_key); // suck in the provided key.
116 twofish_crypto agreed_key(BLOWFISH_KEY_SIZE); // random blowfish key.
117 new_key = agreed_key;
118
119 // now encrypt the new key for transit.
120 bool worked = pub.public_encrypt(agreed_key.get_key(),
122 if (!worked) _success = tentacle::GARBAGE; // lacking a better description.
124 return _success;
125}
126
128{
129 old_school_rsa_crypto priv(RSA_KEY_SIZE); // generate random key.
130 outcome to_return = prepare_public_key(priv);
131 if (to_return == tentacle::OKAY) private_key = priv;
132 return to_return;
133}
134
136{
137 bool worked = private_key.public_key(_public_key);
138 if (!worked) return tentacle::DISALLOWED; // why would that ever fail?
139 return tentacle::OKAY;
140}
141
143 twofish_crypto &new_key) const
144{
145 FUNCDEF("extract_response");
146 if (_success != tentacle::OKAY) return _success;
147 byte_array decrypted;
148 bool worked = private_key.private_decrypt(_encrypted_blowfish_key, decrypted);
149 if (!worked) return tentacle::BAD_INPUT; // that one we hope is accurate.
150 new_key.set_key(decrypted, BLOWFISH_KEY_SIZE);
151 return tentacle::OKAY;
152}
153
154} //namespace.
155
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
bool set_key(int key_size)
this will create a new random key of the "key_size", in bits.
const basis::byte_array & get_key() const
returns our current key.
Supports public key encryption and decryption.
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_encrypt(const basis::byte_array &source, basis::byte_array &target) const
encrypts "source" using our public 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 extract_response(const crypto::old_school_rsa_crypto &private_key, crypto::twofish_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::byte_array _public_key
valid during the request stage of encryption.
basis::outcome prepare_blowfish_key(crypto::twofish_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.
basis::outcome prepare_both_keys(crypto::old_school_rsa_crypto &private_key)
sets up both keys by randomly generating the "private_key".
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::outcome prepare_public_key(const crypto::old_school_rsa_crypto &private_key)
prepares the request side for a client.
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.