Panda3D
encryptStreamBuf.h
1 // Filename: encryptStreamBuf.h
2 // Created by: drose (01Sep04)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef ENCRYPTSTREAMBUF_H
16 #define ENCRYPTSTREAMBUF_H
17 
18 #include "dtoolbase.h"
19 
20 // This module is not compiled if OpenSSL is not available.
21 #ifdef HAVE_OPENSSL
22 
23 #include "openssl/evp.h"
24 
25 ////////////////////////////////////////////////////////////////////
26 // Class : EncryptStreamBuf
27 // Description : The streambuf object that implements
28 // IDecompressStream and OCompressStream.
29 ////////////////////////////////////////////////////////////////////
30 class EXPCL_DTOOLCONFIG EncryptStreamBuf : public streambuf {
31 public:
32  EncryptStreamBuf();
33  virtual ~EncryptStreamBuf();
34 
35  void open_read(istream *source, bool owns_source, const string &password);
36  void close_read();
37 
38  void open_write(ostream *dest, bool owns_dest, const string &password);
39  void close_write();
40 
41  INLINE void set_algorithm(const string &algorithm);
42  INLINE const string &get_algorithm() const;
43 
44  INLINE void set_key_length(int key_length);
45  INLINE int get_key_length() const;
46 
47  INLINE void set_iteration_count(int iteration_count);
48  INLINE int get_iteration_count() const;
49 
50 protected:
51  virtual int overflow(int c);
52  virtual int sync();
53  virtual int underflow();
54 
55 private:
56  size_t read_chars(char *start, size_t length);
57  void write_chars(const char *start, size_t length);
58 
59 private:
60  istream *_source;
61  bool _owns_source;
62 
63  ostream *_dest;
64  bool _owns_dest;
65 
66  string _algorithm;
67  int _key_length;
68  int _iteration_count;
69 
70  bool _read_valid;
71  EVP_CIPHER_CTX _read_ctx;
72  size_t _read_block_size;
73  unsigned char *_read_overflow_buffer;
74  size_t _in_read_overflow_buffer;
75 
76  bool _write_valid;
77  EVP_CIPHER_CTX _write_ctx;
78  size_t _write_block_size;
79 };
80 
81 #include "encryptStreamBuf.I"
82 
83 #endif // HAVE_OPENSSL
84 
85 #endif