47 #ifndef _MIRA_RSAFILTER_H_ 48 #define _MIRA_RSAFILTER_H_ 51 #include <boost/iostreams/concepts.hpp> 52 #include <boost/iostreams/filter/symmetric.hpp> 53 #include <boost/asio/basic_streambuf.hpp> 71 template<
typename Alloc>
72 struct RSAFilterAllocatorTraits
74 #ifndef BOOST_NO_STD_ALLOCATOR 75 typedef typename Alloc::template rebind<char>::other
type;
77 typedef std::allocator<char>
type;
84 template<
typename Alloc,
86 struct RSAFilterAllocator :
90 typedef typename Base::size_type size_type;
92 BOOST_STATIC_CONSTANT(
bool, custom =
93 (!boost::is_same<std::allocator<char>, Base>::value));
96 static void* allocate(
void*
self, uint32 items, uint32 size);
97 static void deallocate(
void*
self,
void* address);
106 typedef char char_type;
115 template<
typename Alloc>
116 void init(
const RSAKey& key,
bool encrypt, RSAFilterAllocator<Alloc>& alloc)
118 initFilter(key, encrypt, &alloc);
122 bool encryptPublic(
const char*& ioSrcBegin,
129 bool decryptPrivate(
const char*& ioSrcBegin,
139 void initFilter(
const RSAKey& key,
bool encrypt,
void* alloc);
144 boost::asio::basic_streambuf<> mInputBuffer;
145 boost::asio::basic_streambuf<> mOutputBuffer;
155 template<
typename Alloc = std::allocator<
char> >
156 class RSAPublicEncryptionImpl :
157 public RSAFilterBase,
158 public RSAFilterAllocator<Alloc>
161 RSAPublicEncryptionImpl(
const RSAKey& key);
162 ~RSAPublicEncryptionImpl();
164 bool filter(
const char* &ioSrcBegin,
const char* iSrcEnd,
165 char* &ioDestBegin,
char* iDestEnd,
bool iFlush);
173 template<
typename Alloc = std::allocator<
char> >
174 class RSAPrivateDecryptionImpl :
175 public RSAFilterBase,
176 public RSAFilterAllocator<Alloc>
179 RSAPrivateDecryptionImpl(
const RSAKey& key);
180 ~RSAPrivateDecryptionImpl();
182 bool filter(
const char* &ioSrcBegin,
const char* iSrcEnd,
183 char* &ioDestBegin,
char* iDestEnd,
bool iFlush);
195 template<
typename Alloc = std::allocator<
char> >
196 struct BasicRSAPublicEncryptionFilter :
197 boost::iostreams::symmetric_filter<Private::RSAPublicEncryptionImpl<Alloc>, Alloc>
200 typedef Private::RSAPublicEncryptionImpl<Alloc> impl_type;
201 typedef boost::iostreams::symmetric_filter<impl_type, Alloc> base_type;
204 typedef typename base_type::char_type char_type;
205 typedef typename base_type::category category;
207 BasicRSAPublicEncryptionFilter(
const RSAKey& key,
208 int bufferSize = boost::iostreams::default_device_buffer_size);
210 BOOST_IOSTREAMS_PIPABLE(BasicRSAPublicEncryptionFilter, 1)
215 template<typename Alloc =
std::allocator<
char> >
216 struct BasicRSAPrivateDecryptionFilter :
217 boost::iostreams::symmetric_filter<Private::RSAPrivateDecryptionImpl<Alloc>, Alloc>
220 typedef Private::RSAPrivateDecryptionImpl<Alloc> impl_type;
221 typedef boost::iostreams::symmetric_filter<impl_type, Alloc> base_type;
224 typedef typename base_type::char_type char_type;
225 typedef typename base_type::category category;
227 BasicRSAPrivateDecryptionFilter(
const RSAKey& key,
228 int bufferSize = boost::iostreams::default_device_buffer_size);
230 BOOST_IOSTREAMS_PIPABLE(BasicRSAPrivateDecryptionFilter, 1)
293 template<
typename Alloc,
typename Base>
294 void* RSAFilterAllocator<Alloc, Base>::allocate(
void*
self, uint32 items,
297 size_type len = items * size;
299 static_cast<allocator_type*
>(
self)->allocate
300 (len +
sizeof(size_type)
301 #if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1) 305 *
reinterpret_cast<size_type*
>(ptr) = len;
306 return ptr +
sizeof(size_type);
309 template<
typename Alloc,
typename Base>
310 void RSAFilterAllocator<Alloc, Base>::deallocate(
void*
self,
void* address)
312 char* ptr =
reinterpret_cast<char*
>(address) -
sizeof(size_type);
313 size_type len = *
reinterpret_cast<size_type*
>(ptr) +
sizeof(size_type);
314 static_cast<allocator_type*
>(
self)->deallocate(ptr, len);
320 template<
typename Alloc>
321 RSAPublicEncryptionImpl<Alloc>::RSAPublicEncryptionImpl(
const RSAKey& key)
323 init(key,
true,
static_cast<RSAFilterAllocator<Alloc>&
>(*
this));
326 template<
typename Alloc>
327 RSAPublicEncryptionImpl<Alloc>::~RSAPublicEncryptionImpl()
332 template<
typename Alloc>
333 bool RSAPublicEncryptionImpl<Alloc>::filter(
const char* &ioSrcBegin,
336 char* iDestEnd,
bool iFlush)
338 return(encryptPublic(ioSrcBegin, iSrcEnd, ioDestBegin, iDestEnd, iFlush));
341 template<
typename Alloc>
342 void RSAPublicEncryptionImpl<Alloc>::close()
350 template<
typename Alloc>
351 RSAPrivateDecryptionImpl<Alloc>::RSAPrivateDecryptionImpl(
const RSAKey& key)
353 init(key,
false,
static_cast<RSAFilterAllocator<Alloc>&
>(*
this));
356 template<
typename Alloc>
357 RSAPrivateDecryptionImpl<Alloc>::~RSAPrivateDecryptionImpl()
362 template<
typename Alloc>
363 bool RSAPrivateDecryptionImpl<Alloc>::filter(
const char* &ioSrcBegin,
366 char* iDestEnd,
bool iFlush)
368 return(decryptPrivate(ioSrcBegin, iSrcEnd, ioDestBegin, iDestEnd, iFlush));
371 template<
typename Alloc>
372 void RSAPrivateDecryptionImpl<Alloc>::close()
384 template<
typename Alloc>
385 BasicRSAPublicEncryptionFilter<Alloc>::BasicRSAPublicEncryptionFilter(
386 const RSAKey& key,
int bufferSize) :
387 base_type(bufferSize, key)
389 if (!key.isPublicKey())
390 MIRA_THROW(XInvalidConfig,
"The key is not a public key.");
396 template<
typename Alloc>
397 BasicRSAPrivateDecryptionFilter<Alloc>::BasicRSAPrivateDecryptionFilter(
398 const RSAKey& key,
int bufferSize) :
399 base_type(bufferSize, key)
401 if (!key.isPrivateKey())
402 MIRA_THROW(XInvalidConfig,
"The key is not a private key.");
Typedefs for OS independent basic data types.
BasicRSAPrivateDecryptionFilter RSAPrivateDecryptionFilter
A RSA private decryption filter for boost::iostreams.
Definition: RSAFilter.h:280
Definition: SyncTimedRead.h:62
Definition of a RSA key (public or private)
Definition: RSAKey.h:71
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
BasicRSAPublicEncryptionFilter RSAPublicEncryptionFilter
A RSA public encryption filter for boost::iostreams.
Definition: RSAFilter.h:257
#define MIRA_THROW(ex, msg)
Macro for throwing an exception.
Definition: Exception.h:81
Commonly used exception classes.
PropertyHint type(const std::string &t)
Sets the attribute "type" to the specified value.
Definition: PropertyHint.h:295