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;
151 #ifdef MIRA_USE_OPENSSL3 160 template<
typename Alloc = std::allocator<
char> >
161 class RSAPublicEncryptionImpl :
162 public RSAFilterBase,
163 public RSAFilterAllocator<Alloc>
166 RSAPublicEncryptionImpl(
const RSAKey& key);
167 ~RSAPublicEncryptionImpl();
169 bool filter(
const char* &ioSrcBegin,
const char* iSrcEnd,
170 char* &ioDestBegin,
char* iDestEnd,
bool iFlush);
178 template<
typename Alloc = std::allocator<
char> >
179 class RSAPrivateDecryptionImpl :
180 public RSAFilterBase,
181 public RSAFilterAllocator<Alloc>
184 RSAPrivateDecryptionImpl(
const RSAKey& key);
185 ~RSAPrivateDecryptionImpl();
187 bool filter(
const char* &ioSrcBegin,
const char* iSrcEnd,
188 char* &ioDestBegin,
char* iDestEnd,
bool iFlush);
200 template<
typename Alloc = std::allocator<
char> >
201 struct BasicRSAPublicEncryptionFilter :
202 boost::iostreams::symmetric_filter<Private::RSAPublicEncryptionImpl<Alloc>, Alloc>
205 typedef Private::RSAPublicEncryptionImpl<Alloc> impl_type;
206 typedef boost::iostreams::symmetric_filter<impl_type, Alloc> base_type;
209 typedef typename base_type::char_type char_type;
210 typedef typename base_type::category category;
212 BasicRSAPublicEncryptionFilter(
const RSAKey& key,
213 int bufferSize = boost::iostreams::default_device_buffer_size);
215 BOOST_IOSTREAMS_PIPABLE(BasicRSAPublicEncryptionFilter, 1)
220 template<typename Alloc =
std::allocator<
char> >
221 struct BasicRSAPrivateDecryptionFilter :
222 boost::iostreams::symmetric_filter<Private::RSAPrivateDecryptionImpl<Alloc>, Alloc>
225 typedef Private::RSAPrivateDecryptionImpl<Alloc> impl_type;
226 typedef boost::iostreams::symmetric_filter<impl_type, Alloc> base_type;
229 typedef typename base_type::char_type char_type;
230 typedef typename base_type::category category;
232 BasicRSAPrivateDecryptionFilter(
const RSAKey& key,
233 int bufferSize = boost::iostreams::default_device_buffer_size);
235 BOOST_IOSTREAMS_PIPABLE(BasicRSAPrivateDecryptionFilter, 1)
318 template<
typename Alloc,
typename Base>
319 void* RSAFilterAllocator<Alloc, Base>::allocate(
void*
self, uint32 items,
322 size_type len = items * size;
324 static_cast<allocator_type*
>(
self)->allocate
325 (len +
sizeof(size_type)
326 #if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1) 330 *
reinterpret_cast<size_type*
>(ptr) = len;
331 return ptr +
sizeof(size_type);
334 template<
typename Alloc,
typename Base>
335 void RSAFilterAllocator<Alloc, Base>::deallocate(
void*
self,
void* address)
337 char* ptr =
reinterpret_cast<char*
>(address) -
sizeof(size_type);
338 size_type len = *
reinterpret_cast<size_type*
>(ptr) +
sizeof(size_type);
339 static_cast<allocator_type*
>(
self)->deallocate(ptr, len);
345 template<
typename Alloc>
346 RSAPublicEncryptionImpl<Alloc>::RSAPublicEncryptionImpl(
const RSAKey& key)
348 init(key,
true,
static_cast<RSAFilterAllocator<Alloc>&
>(*
this));
351 template<
typename Alloc>
352 RSAPublicEncryptionImpl<Alloc>::~RSAPublicEncryptionImpl()
357 template<
typename Alloc>
358 bool RSAPublicEncryptionImpl<Alloc>::filter(
const char* &ioSrcBegin,
361 char* iDestEnd,
bool iFlush)
363 return(encryptPublic(ioSrcBegin, iSrcEnd, ioDestBegin, iDestEnd, iFlush));
366 template<
typename Alloc>
367 void RSAPublicEncryptionImpl<Alloc>::close()
375 template<
typename Alloc>
376 RSAPrivateDecryptionImpl<Alloc>::RSAPrivateDecryptionImpl(
const RSAKey& key)
378 init(key,
false,
static_cast<RSAFilterAllocator<Alloc>&
>(*
this));
381 template<
typename Alloc>
382 RSAPrivateDecryptionImpl<Alloc>::~RSAPrivateDecryptionImpl()
387 template<
typename Alloc>
388 bool RSAPrivateDecryptionImpl<Alloc>::filter(
const char* &ioSrcBegin,
391 char* iDestEnd,
bool iFlush)
393 return(decryptPrivate(ioSrcBegin, iSrcEnd, ioDestBegin, iDestEnd, iFlush));
396 template<
typename Alloc>
397 void RSAPrivateDecryptionImpl<Alloc>::close()
409 template<
typename Alloc>
410 BasicRSAPublicEncryptionFilter<Alloc>::BasicRSAPublicEncryptionFilter(
411 const RSAKey& key,
int bufferSize) :
412 base_type(bufferSize, key)
414 if (!key.isPublicKey())
415 MIRA_THROW(XInvalidConfig,
"The key is not a public key.");
421 template<
typename Alloc>
422 BasicRSAPrivateDecryptionFilter<Alloc>::BasicRSAPrivateDecryptionFilter(
423 const RSAKey& key,
int bufferSize) :
424 base_type(bufferSize, key)
426 if (!key.isPrivateKey())
427 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:305
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:274
#define MIRA_THROW(ex, msg)
Macro for throwing an exception.
Definition: Exception.h:82
Commonly used exception classes.
PropertyHint type(const std::string &t)
Sets the attribute "type" to the specified value.
Definition: PropertyHint.h:295