KVIrc  4.9.2
DeveloperAPIs
KviSSL.h
Go to the documentation of this file.
1 #ifndef _KVI_SSL_H_
2 #define _KVI_SSL_H_
3 //=============================================================================
4 //
5 // File : KviSSL.h
6 // Creation date : Mon May 27 2002 21:36:12 CEST by Szymon Stefanek
7 //
8 // This file is part of the KVIrc IRC client distribution
9 // Copyright (C) 2002-2010 Szymon Stefanek (pragma at kvirc dot net)
10 //
11 // This program is FREE software. You can redistribute it and/or
12 // modify it under the terms of the GNU General Public License
13 // as published by the Free Software Foundation; either version 2
14 // of the License, or (at your option) any later version.
15 //
16 // This program is distributed in the HOPE that it will be USEFUL,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 // See the GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program. If not, write to the Free Software Foundation,
23 // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 //
25 //=============================================================================
26 
27 #include "kvi_settings.h"
28 
29 #ifdef COMPILE_SSL_SUPPORT
30 
31 #include "KviCString.h"
32 #include "kvi_sockettype.h"
33 
34 #include "KviPointerHashTable.h"
35 
36 // Apple deprecated openssl since osx 10.7:
37 
38 #ifdef DEPRECATED_IN_MAC_OS_X_VERSION_10_7_AND_LATER
39 #undef DEPRECATED_IN_MAC_OS_X_VERSION_10_7_AND_LATER
40 #define DEPRECATED_IN_MAC_OS_X_VERSION_10_7_AND_LATER
41 #endif
42 
43 #include <openssl/ssl.h>
44 
45 class KVILIB_API KviSSLCertificate
46 {
47 public:
48  KviSSLCertificate(X509 * x509);
49  ~KviSSLCertificate();
50 
51 protected:
52  X509 * m_pX509;
55  int m_iPubKeyBits;
56  KviCString m_szPubKeyType;
57  int m_iSerialNumber;
58  int m_iVersion;
59  KviCString m_szSignatureType;
60  KviCString m_szSignatureContents;
61 
62 private:
63  void extractSubject();
64  void extractIssuer();
65  void extractPubKeyInfo();
66  void extractSerialNumber();
67  void extractSignature();
68  const char * dictEntry(KviPointerHashTable<const char *, KviCString> * dict, const char * entry);
69  void splitX509String(KviPointerHashTable<const char *, KviCString> * dict, const char * t);
70  int getFingerprint(unsigned char * bufferData, unsigned int * bufferLen, const char * digestName);
71 
72 public:
73  void setX509(X509 * x509);
74  char * getX509Base64(); //not owned, you'll need to free this
75 
76  const char * signatureType() { return m_szSignatureType.ptr(); };
77  const char * signatureContents() { return m_szSignatureContents.ptr(); };
78 
79  const char * subjectCountry() { return dictEntry(m_pSubject, "C"); };
80  const char * subjectStateOrProvince() { return dictEntry(m_pSubject, "ST"); };
81  const char * subjectLocality() { return dictEntry(m_pSubject, "L"); };
82  const char * subjectOrganization() { return dictEntry(m_pSubject, "O"); };
83  const char * subjectOrganizationalUnit() { return dictEntry(m_pSubject, "OU"); };
84  const char * subjectCommonName() { return dictEntry(m_pSubject, "CN"); };
85 
86  const char * issuerCountry() { return dictEntry(m_pIssuer, "C"); };
87  const char * issuerStateOrProvince() { return dictEntry(m_pIssuer, "ST"); };
88  const char * issuerLocality() { return dictEntry(m_pIssuer, "L"); };
89  const char * issuerOrganization() { return dictEntry(m_pIssuer, "O"); };
90  const char * issuerOrganizationalUnit() { return dictEntry(m_pIssuer, "OU"); };
91  const char * issuerCommonName() { return dictEntry(m_pIssuer, "CN"); };
92 
93  int publicKeyBits() { return m_iPubKeyBits; };
94  const char * publicKeyType() { return m_szPubKeyType.ptr(); };
95 
96  int serialNumber() { return m_iSerialNumber; };
97 
98  int version() { return m_iVersion; };
99 
100  bool fingerprintIsValid();
101  int fingerprintDigestId();
102  const char * fingerprintDigestStr();
103  const char * fingerprintContents(QString digestName = "");
104 
105 #ifdef COMPILE_ON_WINDOWS
106  // On windows we need to override new and delete operators
107  // to ensure that always the right new/delete pair is called for an object instance
108  // This bug is present in all the classes exported by a module that
109  // can be instantiated/destroyed from external modules.
110  // (this is a well known bug described in Q122675 of MSDN)
111  void * operator new(size_t tSize);
112  void operator delete(void * p);
113 #endif
114 };
115 
116 class KVILIB_API KviSSLCipherInfo
117 {
118 public:
119 #if OPENSSL_VERSION_NUMBER >= 0x10000000L
120  KviSSLCipherInfo(const SSL_CIPHER * c, const SSL * s);
121 #else
122  KviSSLCipherInfo(SSL_CIPHER * c, SSL * s);
123 #endif
124  ~KviSSLCipherInfo();
125 
126 protected:
127  KviCString m_szVersion;
128  int m_iNumBits;
129  int m_iNumBitsUsed;
130  KviCString m_szName;
131  KviCString m_szDescription;
132 
133 public:
134  const char * name() { return m_szName.ptr(); };
135  const char * description() { return m_szDescription.ptr(); };
136  int bits() { return m_iNumBits; };
137  int bitsUsed() { return m_iNumBitsUsed; };
138  const char * version() { return m_szVersion.ptr(); };
139 #ifdef COMPILE_ON_WINDOWS
140  // On windows we need to override new and delete operators
141  // to ensure that always the right new/delete pair is called for an object instance
142  // This bug is present in all the classes exported by a module that
143  // can be instantiated/destroyed from external modules.
144  // (this is a well known bug described in Q122675 of MSDN)
145  void * operator new(size_t tSize);
146  void operator delete(void * p);
147 #endif
148 };
149 
150 #ifdef Success
151 #undef Success
152 #endif
153 
154 class KVILIB_API KviSSL
155 {
156 public:
157  enum Method
158  {
159  Client,
160  Server
161  };
162  enum Result
163  {
164  Success,
165  NotInitialized,
166  WantRead,
167  WantWrite,
168  ZeroReturn,
169  FileIoError,
170  UnknownError,
171  ObscureError,
172  SSLError,
173  SyscallError,
175  };
176 
177 public:
178  KviSSL();
179  ~KviSSL();
180 
181 public:
182  SSL * m_pSSL;
183  SSL_CTX * m_pSSLCtx;
184  KviCString m_szPass;
185 
186 public:
187  static void globalInit();
188  static void globalDestroy();
189  static void globalSSLInit();
190  static void globalSSLDestroy();
191 
192 public:
193  bool initSocket(kvi_socket_t fd);
194  bool initContext(KviSSL::Method m);
195  void shutdown();
196  KviSSL::Result connect();
197  KviSSL::Result accept();
198  int read(char * buffer, int len);
199  int write(const char * buffer, int len);
200  // SSL ERRORS
201  unsigned long getLastError(bool bPeek = false);
202  bool getLastErrorString(KviCString & buffer, bool bPeek = false);
203  // Protocol error
204  KviSSL::Result getProtocolError(int ret);
205  KviSSLCertificate * getPeerCertificate();
206  KviSSLCertificate * getLocalCertificate();
207  KviSSLCipherInfo * getCurrentCipherInfo();
208  KviSSL::Result useCertificateFile(QString cert, QString pass);
209  KviSSL::Result usePrivateKeyFile(QString key, QString pass);
210 #ifdef COMPILE_ON_WINDOWS
211  // On windows we need to override new and delete operators
212  // to ensure that always the right new/delete pair is called for an object instance
213  // This bug is present in all the classes exported by a module that
214  // can be instantiated/destroyed from external modules.
215  // (this is a well known bug described in Q122675 of MSDN)
216  void * operator new(size_t tSize);
217  void operator delete(void * p);
218 #endif
219 private:
220  KviSSL::Result connectOrAcceptError(int ret);
221 };
222 
223 #endif //COMPILE_SSL_SUPPORT
224 
225 #endif //_KVI_SSL_H_
int kvi_socket_t
Definition: kvi_sockettype.h:40
Definition: KviCString.h:105
connect(m_pFtp, SIGNAL(commandFinished(int, bool)), this, SLOT(slotCommandFinished(int, bool)))
char s char s char s s s s s char char c s *s c s s s d c s *s d c d d d d c
Definition: KviIrcNumericCodes.h:391
#define m
Definition: detector.cpp:77
Definition: KviError.h:97
Pointer Hash Table.
Definition: KviError.h:50
char s char s s
Definition: KviIrcNumericCodes.h:391
QHashIterator< int, QFile * > t(getDict)
QString version()
Definition: KviRuntimeInfo.cpp:671
QString name()
Definition: KviRuntimeInfo.cpp:655
A fast pointer hash table implementation.
Definition: KviPointerHashTable.h:391
This file contains compile time settings.
Definition: KviError.h:162
#define p
Definition: detector.cpp:80
#define KVILIB_API
Definition: kvi_settings.h:125
Definition: KviError.h:49