libiqxmlrpc  0.12.12
ssl_lib.h
1 // Libiqxmlrpc - an object-oriented XML-RPC solution.
2 // Copyright (C) 2011 Anton Dedov
3 
4 #ifndef _libiqnet_ssl_lib_h_
5 #define _libiqnet_ssl_lib_h_
6 
7 #include "api_export.h"
8 
9 #include <openssl/ssl.h>
10 #include <boost/shared_ptr.hpp>
11 #include <stdexcept>
12 
13 namespace iqnet {
14 namespace ssl {
15 
16 class Ctx;
17 
19 
23 extern LIBIQXMLRPC_API Ctx* ctx;
24 
26 void LIBIQXMLRPC_API throw_io_exception( SSL*, int ret );
27 
28 class LIBIQXMLRPC_API ConnectionVerifier {
29 public:
30  virtual ~ConnectionVerifier();
31 
32  int verify(bool preverified_ok, X509_STORE_CTX*) const;
33 
34 protected:
35  std::string cert_finger_sha256(X509_STORE_CTX*) const;
36 
37 private:
38  virtual int do_verify(bool preverified_ok, X509_STORE_CTX*) const = 0;
39 };
40 
42 
49 class LIBIQXMLRPC_API Ctx {
50 public:
51  static Ctx* client_server( const std::string& cert_path, const std::string& key_path );
52  static Ctx* server_only( const std::string& cert_path, const std::string& key_path );
53  static Ctx* client_only();
54 
55  ~Ctx();
56 
57  SSL_CTX* context();
58 
59  void verify_server(ConnectionVerifier*);
60  void verify_client(bool require_certificate, ConnectionVerifier*);
61  void prepare_verify(SSL*, bool server);
62 
63 private:
64  Ctx( const std::string&, const std::string&, bool init_client );
65  Ctx();
66 
67  struct Impl;
68  boost::shared_ptr<Impl> impl_;
69 };
70 
71 #ifdef _MSC_VER
72 #pragma warning(disable: 4251)
73 #endif
74 
76 class LIBIQXMLRPC_API exception: public std::exception {
77  unsigned long ssl_err;
78  std::string msg;
79 
80 public:
81  exception() throw();
82  explicit exception( unsigned long ssl_err ) throw();
83  exception( const std::string& msg ) throw();
84  virtual ~exception() throw() {}
85 
86  const char* what() const throw() { return msg.c_str(); }
87  unsigned long code() const throw() { return ssl_err; }
88 };
89 
90 class LIBIQXMLRPC_API not_initialized: public ssl::exception {
91 public:
93  exception( "Libiqnet::ssl not initialized." ) {}
94 };
95 
96 class LIBIQXMLRPC_API connection_close: public ssl::exception {
97  bool clean;
98 public:
99  connection_close( bool clean_ ):
100  exception( "Connection has been closed." ),
101  clean(clean_) {}
102 
103  bool is_clean() const { return clean; }
104 };
105 
106 class LIBIQXMLRPC_API io_error: public ssl::exception {
107 public:
108  io_error( int err ):
109  exception( err ) {}
110 };
111 
112 class LIBIQXMLRPC_API need_write: public ssl::io_error {
113 public:
114  need_write():
115  io_error( SSL_ERROR_WANT_WRITE ) {}
116 };
117 
118 class LIBIQXMLRPC_API need_read: public ssl::io_error {
119 public:
120  need_read():
121  io_error( SSL_ERROR_WANT_READ ) {}
122 };
123 
124 } // namespace ssl
125 } // namespace iqnet
126 
127 #endif
Definition: ssl_lib.h:90
Object-oriented networking/multithreading infrastructure.
Definition: acceptor.h:11
Definition: ssl_lib.h:96
SSL context class. Initializes SSL library.
Definition: ssl_lib.h:49
Definition: ssl_lib.h:112
Definition: ssl_lib.h:28
Definition: ssl_lib.h:118
Exception class to wrap errors generated by openssl library.
Definition: ssl_lib.h:76
Definition: ssl_lib.h:106