1 //
2 // CryptoStream.h
3 //
4 // Library: Crypto
5 // Package: Cipher
6 // Module:  CryptoStream
7 //
8 // Definition of the CryptoStreamBuf, CryptoInputStream and CryptoOutputStream
9 // classes.
10 //
11 // Copyright (c) 2008, Applied Informatics Software Engineering GmbH.
12 // and Contributors.
13 //
14 // SPDX-License-Identifier:	BSL-1.0
15 //
16 
17 
18 #ifndef Crypto_CryptoStream_INCLUDED
19 #define Crypto_CryptoStream_INCLUDED
20 
21 
22 #include "Poco/Crypto/Crypto.h"
23 #include "Poco/Crypto/CryptoTransform.h"
24 #include "Poco/BufferedStreamBuf.h"
25 #include "Poco/Buffer.h"
26 #include <iostream>
27 
28 
29 namespace Poco {
30 namespace Crypto {
31 
32 
33 class CryptoTransform;
34 class Cipher;
35 
36 
37 class Crypto_API CryptoStreamBuf: public Poco::BufferedStreamBuf
38 	/// This stream buffer performs cryptographic transformation on the data
39 	/// going through it.
40 {
41 public:
42 	CryptoStreamBuf(std::istream& istr, CryptoTransform::Ptr pTransform, std::streamsize bufferSize = 8192);
43 	CryptoStreamBuf(std::ostream& ostr, CryptoTransform::Ptr pTransform, std::streamsize bufferSize = 8192);
44 
45 	virtual ~CryptoStreamBuf();
46 
47 	void close();
48 		/// Flushes all buffers and finishes the encryption.
49 
50 protected:
51 	int readFromDevice(char* buffer, std::streamsize length);
52 	int writeToDevice(const char* buffer, std::streamsize length);
53 
54 private:
55 	CryptoTransform::Ptr _pTransform;
56 	std::istream*	 _pIstr;
57 	std::ostream*	 _pOstr;
58 	bool			 _eof;
59 
60 	Poco::Buffer<unsigned char> _buffer;
61 
62 	CryptoStreamBuf(const CryptoStreamBuf&);
63 	CryptoStreamBuf& operator = (const CryptoStreamBuf&);
64 };
65 
66 
67 class Crypto_API CryptoIOS: public virtual std::ios
68 	/// The base class for CryptoInputStream and CryptoOutputStream.
69 	///
70 	/// This class is needed to ensure correct initialization order of the
71 	/// stream buffer and base classes.
72 {
73 public:
74 	CryptoIOS(std::istream& istr, CryptoTransform::Ptr pTransform, std::streamsize bufferSize = 8192);
75 	CryptoIOS(std::ostream& ostr, CryptoTransform::Ptr pTransform, std::streamsize bufferSize = 8192);
76 	~CryptoIOS();
77 	CryptoStreamBuf* rdbuf();
78 
79 protected:
80 	CryptoStreamBuf _buf;
81 };
82 
83 
84 class Crypto_API CryptoInputStream: public CryptoIOS, public std::istream
85 	/// This stream transforms all data passing through it using the given
86 	/// CryptoTransform.
87 	///
88 	/// Use a CryptoTransform object provided by Cipher::createEncrytor() or
89 	/// Cipher::createDecryptor() to create an encrypting or decrypting stream,
90 	/// respectively.
91 {
92 public:
93 	CryptoInputStream(std::istream& istr, CryptoTransform::Ptr pTransform, std::streamsize bufferSize = 8192);
94 		/// Create a new CryptoInputStream object. The CryptoInputStream takes the
95 		/// ownership of the given CryptoTransform object.
96 
97 	CryptoInputStream(std::istream& istr, Cipher& cipher, std::streamsize bufferSize = 8192);
98 		/// Create a new encrypting CryptoInputStream object using the given cipher.
99 
100 	~CryptoInputStream();
101 		/// Destroys the CryptoInputStream.
102 };
103 
104 
105 class Crypto_API CryptoOutputStream: public CryptoIOS, public std::ostream
106 	/// This stream transforms all data passing through it using the given
107 	/// CryptoTransform.
108 	///
109 	/// Use a CryptoTransform object provided by Cipher::createEncrytor() or
110 	/// Cipher::createDecryptor() to create an encrypting or decrypting stream,
111 	/// respectively.
112 	///
113 	/// After all data has been passed through the stream, close() must be called
114 	/// to ensure completion of cryptographic transformation.
115 {
116 public:
117 	CryptoOutputStream(std::ostream& ostr, CryptoTransform::Ptr pTransform, std::streamsize bufferSize = 8192);
118 		/// Create a new CryptoOutputStream object. The CryptoOutputStream takes the
119 		/// ownership of the given CryptoTransform object.
120 
121 	CryptoOutputStream(std::ostream& ostr, Cipher& cipher, std::streamsize bufferSize = 8192);
122 		/// Create a new decrypting CryptoOutputStream object using the given cipher.
123 
124 	~CryptoOutputStream();
125 		/// Destroys the CryptoOutputStream.
126 
127 	void close();
128 		/// Flushes all buffers and finishes the encryption.
129 };
130 
131 
132 class Crypto_API DecryptingInputStream: public CryptoIOS, public std::istream
133 	/// This stream decrypts all data passing through it using the given
134 	/// Cipher.
135 {
136 public:
137 	DecryptingInputStream(std::istream& istr, Cipher& cipher, std::streamsize bufferSize = 8192);
138 		/// Create a new DecryptingInputStream object using the given cipher.
139 
140 	~DecryptingInputStream();
141 		/// Destroys the DecryptingInputStream.
142 };
143 
144 
145 class Crypto_API DecryptingOutputStream: public CryptoIOS, public std::ostream
146 	/// This stream decrypts all data passing through it using the given
147 	/// Cipher.
148 {
149 public:
150 	DecryptingOutputStream(std::ostream& ostr, Cipher& cipher, std::streamsize bufferSize = 8192);
151 		/// Create a new DecryptingOutputStream object using the given cipher.
152 
153 	~DecryptingOutputStream();
154 		/// Destroys the DecryptingOutputStream.
155 
156 	void close();
157 		/// Flushes all buffers and finishes the decryption.
158 };
159 
160 
161 class Crypto_API EncryptingInputStream: public CryptoIOS, public std::istream
162 	/// This stream encrypts all data passing through it using the given
163 	/// Cipher.
164 {
165 public:
166 	EncryptingInputStream(std::istream& istr, Cipher& cipher, std::streamsize bufferSize = 8192);
167 		/// Create a new EncryptingInputStream object using the given cipher.
168 
169 	~EncryptingInputStream();
170 		/// Destroys the EncryptingInputStream.
171 };
172 
173 
174 class Crypto_API EncryptingOutputStream: public CryptoIOS, public std::ostream
175 	/// This stream encrypts all data passing through it using the given
176 	/// Cipher.
177 {
178 public:
179 	EncryptingOutputStream(std::ostream& ostr, Cipher& cipher, std::streamsize bufferSize = 8192);
180 		/// Create a new EncryptingOutputStream object using the given cipher.
181 
182 	~EncryptingOutputStream();
183 		/// Destroys the EncryptingOutputStream.
184 
185 	void close();
186 		/// Flushes all buffers and finishes the encryption.
187 };
188 
189 
190 } } // namespace Poco::Crypto
191 
192 
193 #endif // Crypto_CryptoStream_INCLUDED
194