first check-in of feisty meow codebase. many things broken still due to recent
[feisty_meow.git] / octopi / library / tentacles / encryption_infoton.h
1 #ifndef ENCRYPTION_INFOTON_CLASS
2 #define ENCRYPTION_INFOTON_CLASS
3
4 /*****************************************************************************\
5 *                                                                             *
6 *  Name   : encryption_infoton                                                *
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 <crypto/blowfish_crypto.h>
19 #include <crypto/rsa_crypto.h>
20 #include <octopus/entity_defs.h>
21 #include <octopus/infoton.h>
22
23 namespace octopi {
24
25 //! Encapsulates the chit-chat necessary to establish an encrypted connection.
26 /*!
27   This is framed in terms of a client and a server, where the client creates
28   a private key and gives the server the public key.  The server side creates
29   a blowfish key and encrypts it using the public key.
30 */
31
32 class encryption_infoton : public infoton
33 {
34 public:
35   basis::byte_array _public_key;
36     //!< valid during the request stage of encryption.
37     /*!< this is used when the client is telling the server how to talk to
38     it to provide the key. */
39   basis::byte_array _encrypted_blowfish_key;
40     //!< valid during the response stage of encryption.
41     /*!< this is used when the server reports a blowfish key that it will
42     use on this connection with the client. */
43
44   basis::outcome _success;  //!< did the request succeed?
45
46   encryption_infoton(const basis::byte_array &public_key = basis::byte_array::empty_array(),
47           const basis::byte_array &encrypted_blowfish_key = basis::byte_array::empty_array());
48   encryption_infoton(const encryption_infoton &to_copy);
49
50   virtual ~encryption_infoton();
51
52   DEFINE_CLASS_NAME("encryption_infoton");
53
54   static const int RSA_KEY_SIZE;
55     //!< this key size should be used for all RSA private keys.
56   static const int BLOWFISH_KEY_SIZE;
57     //!< this will be used for blowfish keys that this object generates.
58
59   void text_form(basis::base_string &fill) const {
60     fill.assign(basis::astring(class_name()));  // low exposure for vital held info.
61   }
62
63   encryption_infoton &operator =(const encryption_infoton &to_copy);
64
65   basis::outcome prepare_blowfish_key(crypto::blowfish_crypto &new_key);
66     //!< performs the server side's job on the current key.
67     /*!< the public key had better be set already or this will fail.  the
68     "new_key" will always be used to communicate with the client after this.
69     */
70
71   basis::outcome prepare_public_key(const crypto::rsa_crypto &private_key);
72     //!< prepares the request side for a client.
73     /*!< the rsa public key will be generated from the "private_key". */
74
75   basis::outcome prepare_both_keys(crypto::rsa_crypto &private_key);
76     //!< sets up both keys by randomly generating the "private_key".
77
78   basis::outcome extract_response(const crypto::rsa_crypto &private_key,
79           crypto::blowfish_crypto &new_key) const;
80     //!< used by the client to extract the shared blowfish key from the server.
81     /*!< using the private key, the server's response is decrypted and stored
82     in "new_key".  note that this will only succeed if the _success member
83     is OKAY.  otherwise it means the server has beefed on the request. */
84
85   static const structures::string_array &encryption_classifier();
86     //!< returns the classifier for this type of infoton.
87
88   virtual void pack(basis::byte_array &packed_form) const;
89   virtual bool unpack(basis::byte_array &packed_form);
90
91   virtual clonable *clone() const;
92
93   virtual int packed_size() const;
94 };
95
96 } //namespace.
97
98 #endif  // outer guard.
99