OpenBus SDK C++ TAO  2.0.2.0
 Tudo Classes Namespaces Ficheiros Funções Definições de tipos Páginas
OpenSSl.hpp
1 // -*- coding: iso-8859-1-unix -*-
2 #ifndef TECGRAF_SDK_OPENBUS_OPENSSL_H_
3 #define TECGRAF_SDK_OPENBUS_OPENSSL_H_
4 
5 #include "openbus/decl.hpp"
6 #include <tao/ORB.h>
7 #include <openssl/pem.h>
8 #include <boost/shared_ptr.hpp>
9 #include <boost/function.hpp>
10 
11 namespace openbus
12 {
13 namespace openssl
14 {
15 
16 struct pkey_ctx
17 {
18  explicit pkey_ctx(EVP_PKEY_CTX *ctx) : ctx(ctx)
19  {
20  }
21 
22  pkey_ctx() : ctx(0)
23  {
24  }
25 
26  ~pkey_ctx()
27  {
28  if(ctx)
29  {
30  EVP_PKEY_CTX_free(ctx);
31  }
32  }
33 
34  EVP_PKEY_CTX *get() const
35  {
36  return ctx;
37  }
38 
39  bool is_empty() const
40  {
41  return ctx == 0;
42  }
43 
44  typedef bool(pkey_ctx::*unspecified_bool_type)() const;
45 
46  operator unspecified_bool_type() const
47  {
48  unspecified_bool_type n = 0;
49  return is_empty()?n:&pkey_ctx::is_empty;
50  }
51 
52  bool operator!() const
53  {
54  return is_empty();
55  }
56 private:
57  EVP_PKEY_CTX *ctx;
58 };
59 
60 struct openssl_buffer
61 {
62  explicit openssl_buffer(unsigned char *buffer)
63  : buffer(buffer), deleter_(CRYPTO_free)
64  {
65  }
66 
67  ~openssl_buffer()
68  {
69  if(buffer)
70  {
71  deleter_(buffer);
72  }
73  }
74 
75  void deleter(boost::function<void (unsigned char *)> p)
76  {
77  deleter_ = p;
78  }
79 
80  unsigned char &operator[](std::size_t s)
81  {
82  return buffer[s];
83  }
84 
85  unsigned char const &operator[](std::size_t s) const
86  {
87  return buffer[s];
88  }
89 
90  unsigned char *release()
91  {
92  unsigned char *tmp = buffer;
93  buffer = 0;
94  return tmp;
95  }
96 
97  bool is_empty() const
98  {
99  return buffer == 0;
100  }
101 
102  typedef bool(openssl_buffer::* unspecified_bool_type)() const;
103 
104  operator unspecified_bool_type () const
105  {
106  unspecified_bool_type n = 0;
107  return is_empty()? n : &openssl_buffer::is_empty;
108  }
109 
110  unsigned char *get() const
111  {
112  return buffer;
113  }
114 private:
115  unsigned char *buffer;
116  boost::function<void (unsigned char *)> deleter_;
117 };
118 
119 struct pkey
120 {
121  explicit pkey(EVP_PKEY *key) : key(key, & ::EVP_PKEY_free)
122  {
123  }
124 
125  pkey()
126  {
127  }
128 
129  EVP_PKEY *get() const
130  {
131  return key.get();
132  }
133 
134  bool is_empty() const
135  {
136  return !key;
137  }
138 
139  typedef bool(pkey::* unspecified_bool_type)() const;
140 
141  operator unspecified_bool_type () const
142  {
143  unspecified_bool_type n = 0;
144  return is_empty()?n:&pkey::is_empty;
145  }
146 
147  bool operator!() const
148  {
149  return is_empty();
150  }
151 private:
152  boost::shared_ptr<EVP_PKEY> key;
153 };
154 
155 OPENBUS_SDK_DECL pkey byteSeq2PubKey(const unsigned char *, size_t len);
156 OPENBUS_SDK_DECL pkey byteSeq2PrvKey(const unsigned char *, size_t len);
157 OPENBUS_SDK_DECL CORBA::OctetSeq PubKey2byteSeq(pkey);
158 OPENBUS_SDK_DECL CORBA::OctetSeq PrvKey2byteSeq(pkey);
159 OPENBUS_SDK_DECL CORBA::OctetSeq encrypt(pkey, const unsigned char *, size_t len);
160 OPENBUS_SDK_DECL CORBA::OctetSeq decrypt(pkey, const unsigned char *, size_t len);
161 }
162 }
163 
164 #endif
openbus
Definition: Connection.hpp:41