MIRA
RSAFilter.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
47 #ifndef _MIRA_RSAFILTER_H_
48 #define _MIRA_RSAFILTER_H_
49 
50 #ifndef Q_MOC_RUN
51 #include <boost/iostreams/concepts.hpp>
52 #include <boost/iostreams/filter/symmetric.hpp>
53 #include <boost/asio/basic_streambuf.hpp>
54 #endif
55 
56 #include <platform/Types.h>
57 #include <error/Exceptions.h>
58 #include <security/RSAKey.h>
59 
60 namespace mira {
61 
63 
65 
66 namespace Private {
67 
69 // An allocator trait for the boost iostream filter
70 
71 template<typename Alloc>
72 struct RSAFilterAllocatorTraits
73 {
74 #ifndef BOOST_NO_STD_ALLOCATOR
75  typedef typename Alloc::template rebind<char>::other type;
76 #else
77  typedef std::allocator<char> type;
78 #endif
79 };
80 
82 // An allocator for the boost iostreams filter
83 
84 template< typename Alloc,
85  typename Base = BOOST_DEDUCED_TYPENAME RSAFilterAllocatorTraits<Alloc>::type >
86 struct RSAFilterAllocator :
87  private Base
88 {
89 private:
90  typedef typename Base::size_type size_type;
91 public:
92  BOOST_STATIC_CONSTANT(bool, custom =
93  (!boost::is_same<std::allocator<char>, Base>::value));
94  typedef typename RSAFilterAllocatorTraits<Alloc>::type allocator_type;
95 
96  static void* allocate(void* self, uint32 items, uint32 size);
97  static void deallocate(void* self, void* address);
98 };
99 
101 // An internal class of the RSA filter class
102 
103 class MIRA_BASE_EXPORT RSAFilterBase
104 {
105 public:
106  typedef char char_type;
107 
108 protected:
110  RSAFilterBase();
111 
113  ~RSAFilterBase();
114 
115  template<typename Alloc>
116  void init(const RSAKey& key, bool encrypt, RSAFilterAllocator<Alloc>& alloc)
117  {
118  initFilter(key, encrypt, &alloc);
119  }
120 
122  bool encryptPublic(const char*& ioSrcBegin,
123  const char* oSrcEnd,
124  char*& ioDestBegin,
125  char* iDestEnd,
126  bool iFlush);
127 
129  bool decryptPrivate(const char*& ioSrcBegin,
130  const char* iSrcEnd,
131  char*& ioDestBegin,
132  char* iDestEnd,
133  bool iFlush);
134 
136  void reset();
137 
138 private:
139  void initFilter(const RSAKey& key, bool encrypt, void* alloc);
140 
141 private:
142  RSAKey mKey;
143 
144  boost::asio::basic_streambuf<> mInputBuffer;
145  boost::asio::basic_streambuf<> mOutputBuffer;
146 
147  uint8* mWrkBuffer;
148  size_t mRSASize;
149  size_t mBlockSize;
150 };
151 
153 // Template name: RSAPublicEncryptionImpl
154 
155 template<typename Alloc = std::allocator<char> >
156 class RSAPublicEncryptionImpl :
157  public RSAFilterBase,
158  public RSAFilterAllocator<Alloc>
159 {
160 public:
161  RSAPublicEncryptionImpl(const RSAKey& key);
162  ~RSAPublicEncryptionImpl();
163 
164  bool filter(const char* &ioSrcBegin, const char* iSrcEnd,
165  char* &ioDestBegin, char* iDestEnd, bool iFlush);
166 
167  void close();
168 };
169 
171 // Template name: RSAPrivateDecryptionImpl
172 
173 template<typename Alloc = std::allocator<char> >
174 class RSAPrivateDecryptionImpl :
175  public RSAFilterBase,
176  public RSAFilterAllocator<Alloc>
177 {
178 public:
179  RSAPrivateDecryptionImpl(const RSAKey& key);
180  ~RSAPrivateDecryptionImpl();
181 
182  bool filter(const char* &ioSrcBegin, const char* iSrcEnd,
183  char* &ioDestBegin, char* iDestEnd, bool iFlush);
184 
185  void close();
186 };
187 
189 
190 } // end of namespace Private
191 
193 // BasicRSAPublicEncryptionFilter
194 
195 template<typename Alloc = std::allocator<char> >
196 struct BasicRSAPublicEncryptionFilter :
197  boost::iostreams::symmetric_filter<Private::RSAPublicEncryptionImpl<Alloc>, Alloc>
198 {
199 private:
200  typedef Private::RSAPublicEncryptionImpl<Alloc> impl_type;
201  typedef boost::iostreams::symmetric_filter<impl_type, Alloc> base_type;
202 
203 public:
204  typedef typename base_type::char_type char_type;
205  typedef typename base_type::category category;
206 
207  BasicRSAPublicEncryptionFilter(const RSAKey& key,
208  int bufferSize = boost::iostreams::default_device_buffer_size);
209 };
210 BOOST_IOSTREAMS_PIPABLE(BasicRSAPublicEncryptionFilter, 1)
211 
212 // BasicRSAPrivateDecryptionFilter
214 
215 template<typename Alloc = std::allocator<char> >
216 struct BasicRSAPrivateDecryptionFilter :
217  boost::iostreams::symmetric_filter<Private::RSAPrivateDecryptionImpl<Alloc>, Alloc>
218 {
219 private:
220  typedef Private::RSAPrivateDecryptionImpl<Alloc> impl_type;
221  typedef boost::iostreams::symmetric_filter<impl_type, Alloc> base_type;
222 
223 public:
224  typedef typename base_type::char_type char_type;
225  typedef typename base_type::category category;
226 
227  BasicRSAPrivateDecryptionFilter(const RSAKey& key,
228  int bufferSize = boost::iostreams::default_device_buffer_size);
229 };
230 BOOST_IOSTREAMS_PIPABLE(BasicRSAPrivateDecryptionFilter, 1)
231 
232 
235 
237 
257 typedef BasicRSAPublicEncryptionFilter<> RSAPublicEncryptionFilter;
258 
280 typedef BasicRSAPrivateDecryptionFilter<> RSAPrivateDecryptionFilter;
281 
283 // Template implementation
285 
287 
288 namespace Private {
289 
291 // Implementation of template rsa_allocator
292 
293 template<typename Alloc, typename Base>
294 void* RSAFilterAllocator<Alloc, Base>::allocate(void* self, uint32 items,
295  uint32 size)
296 {
297  size_type len = items * size;
298  char* ptr =
299  static_cast<allocator_type*>(self)->allocate
300  (len + sizeof(size_type)
301  #if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1)
302  , (char*)0
303  #endif
304  );
305  *reinterpret_cast<size_type*>(ptr) = len;
306  return ptr + sizeof(size_type);
307 }
308 
309 template<typename Alloc, typename Base>
310 void RSAFilterAllocator<Alloc, Base>::deallocate(void* self, void* address)
311 {
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);
315 }
316 
318 // Implementation of RSAPublicEncryptionImpl
319 
320 template<typename Alloc>
321 RSAPublicEncryptionImpl<Alloc>::RSAPublicEncryptionImpl(const RSAKey& key)
322 {
323  init(key, true, static_cast<RSAFilterAllocator<Alloc>&>(*this));
324 }
325 
326 template<typename Alloc>
327 RSAPublicEncryptionImpl<Alloc>::~RSAPublicEncryptionImpl()
328 {
329  reset();
330 }
331 
332 template<typename Alloc>
333 bool RSAPublicEncryptionImpl<Alloc>::filter(const char* &ioSrcBegin,
334  const char* iSrcEnd,
335  char* &ioDestBegin,
336  char* iDestEnd, bool iFlush)
337 {
338  return(encryptPublic(ioSrcBegin, iSrcEnd, ioDestBegin, iDestEnd, iFlush));
339 }
340 
341 template<typename Alloc>
342 void RSAPublicEncryptionImpl<Alloc>::close()
343 {
344  reset();
345 }
346 
348 // Implementation of RSAPrivateDecryptionImpl
349 
350 template<typename Alloc>
351 RSAPrivateDecryptionImpl<Alloc>::RSAPrivateDecryptionImpl(const RSAKey& key)
352 {
353  init(key, false, static_cast<RSAFilterAllocator<Alloc>&>(*this));
354 }
355 
356 template<typename Alloc>
357 RSAPrivateDecryptionImpl<Alloc>::~RSAPrivateDecryptionImpl()
358 {
359  reset();
360 }
361 
362 template<typename Alloc>
363 bool RSAPrivateDecryptionImpl<Alloc>::filter(const char* &ioSrcBegin,
364  const char* iSrcEnd,
365  char* &ioDestBegin,
366  char* iDestEnd, bool iFlush)
367 {
368  return(decryptPrivate(ioSrcBegin, iSrcEnd, ioDestBegin, iDestEnd, iFlush));
369 }
370 
371 template<typename Alloc>
372 void RSAPrivateDecryptionImpl<Alloc>::close()
373 {
374  reset();
375 }
376 
378 
379 } // end of namespace Private
380 
382 // Implementation of BasicRSAPublicEncryptionFilter
383 
384 template<typename Alloc>
385 BasicRSAPublicEncryptionFilter<Alloc>::BasicRSAPublicEncryptionFilter(
386  const RSAKey& key, int bufferSize) :
387  base_type(bufferSize, key)
388 {
389  if (!key.isPublicKey())
390  MIRA_THROW(XInvalidConfig, "The key is not a public key.");
391 }
392 
394 // Implementation of BasicRSAPrivateDecryptionFilter
395 
396 template<typename Alloc>
397 BasicRSAPrivateDecryptionFilter<Alloc>::BasicRSAPrivateDecryptionFilter(
398  const RSAKey& key, int bufferSize) :
399  base_type(bufferSize, key)
400 {
401  if (!key.isPrivateKey())
402  MIRA_THROW(XInvalidConfig, "The key is not a private key.");
403 }
404 
406 
408 
410 
411 } // namespaces
412 
413 #endif
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
STL namespace.
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
#define MIRA_BASE_EXPORT
This is required because on windows there is a macro defined called ERROR.
Definition: Platform.h:153
A class for a RSA key.