summaryrefslogtreecommitdiffstats
path: root/src/kvilib/net/kvi_ssl.h
blob: 5547ecbbbc444fc3451a61a8103d08d986c90895 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#ifndef _KVI_SSL_H_
#define _KVI_SSL_H_
//
//   File : kvi_ssl.h
//   Creation date : Mon May 27 2002 21:36:12 CEST by Szymon Stefanek
//
//   This file is part of the KVirc irc client distribution
//   Copyright (C) 2002 Szymon Stefanek (pragma at kvirc dot net)
//
//   This program is FREE software. You can redistribute it and/or
//   modify it under the terms of the GNU General Public License
//   as published by the Free Software Foundation; either version 2
//   of the License, or (at your opinion) any later version.
//
//   This program is distributed in the HOPE that it will be USEFUL,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//   See the GNU General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program. If not, write to the Free Software Foundation,
//   Inc. ,51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//

#include "kvi_settings.h"

#ifdef COMPILE_SSL_SUPPORT

#include "kvi_string.h"
#include "kvi_sockettype.h"

#include "kvi_pointerhashtable.h"

#include <openssl/ssl.h>


class KVILIB_API KviSSLCertificate
{
public:
	KviSSLCertificate(X509 * x509);
	~KviSSLCertificate();
protected:
	X509 * m_pX509;
	KviPointerHashTable<const char *,KviStr> * m_pSubject;
	KviPointerHashTable<const char *,KviStr> * m_pIssuer;
	int                  m_iPubKeyBits;
	KviStr               m_szPubKeyType;
	int                  m_iSerialNumber;
	int                  m_iVersion;
	KviStr               m_szSignatureType;
	KviStr               m_szSignatureContents;
private:
	void extractSubject();
	void extractIssuer();
	void extractPubKeyInfo();
	void extractSerialNumber();
	void extractSignature();
	const char * dictEntry(KviPointerHashTable<const char *,KviStr> * dict,const char * entry);
	void splitX509String(KviPointerHashTable<const char *,KviStr> * dict,const char * t);
//	void getPKeyType(int type,KviStr &buffer);
public:
	void setX509(X509 * x509);

	const char * signatureType(){ return m_szSignatureType.ptr(); };
	const char * signatureContents(){ return m_szSignatureContents.ptr(); };

	const char * subjectCountry(){ return dictEntry(m_pSubject,"C"); };
	const char * subjectStateOrProvince(){ return dictEntry(m_pSubject,"ST"); };
	const char * subjectLocality(){ return dictEntry(m_pSubject,"L"); };
	const char * subjectOrganization(){ return dictEntry(m_pSubject,"O"); };
	const char * subjectOrganizationalUnit(){ return dictEntry(m_pSubject,"OU"); };
	const char * subjectCommonName(){ return dictEntry(m_pSubject,"CN"); };
	
	const char * issuerCountry(){ return dictEntry(m_pIssuer,"C"); };
	const char * issuerStateOrProvince(){ return dictEntry(m_pIssuer,"ST"); };
	const char * issuerLocality(){ return dictEntry(m_pIssuer,"L"); };
	const char * issuerOrganization(){ return dictEntry(m_pIssuer,"O"); };
	const char * issuerOrganizationalUnit(){ return dictEntry(m_pIssuer,"OU"); };
	const char * issuerCommonName(){ return dictEntry(m_pIssuer,"CN"); };

	int publicKeyBits(){ return m_iPubKeyBits; };
	const char * publicKeyType(){ return m_szPubKeyType.ptr(); };

	int serialNumber(){ return m_iSerialNumber; };

	int version(){ return m_iVersion; };
#ifdef COMPILE_ON_WINDOWS
	// On windows we need to override new and delete operators
	// to ensure that always the right new/delete pair is called for an object instance
	// This bug is present in all the classes exported by a module that
	// can be instantiated/destroyed from external modules.
	// (this is a well known bug described in Q122675 of MSDN)
	void       * operator new(size_t tSize);
	void         operator delete(void * p);
#endif
};

class KVILIB_API KviSSLCipherInfo
{
public:
	KviSSLCipherInfo(SSL_CIPHER * c);
	~KviSSLCipherInfo();
protected:
	KviStr       m_szVersion;
	int          m_iNumBits;
	int          m_iNumBitsUsed;
	KviStr       m_szName;
	KviStr       m_szDescription;
public:
	const char * name(){ return m_szName.ptr(); };
	const char * description(){ return m_szDescription.ptr(); };
	int bits(){ return m_iNumBits; };
	int bitsUsed(){ return m_iNumBitsUsed; };
	const char * version(){ return m_szVersion.ptr(); };
#ifdef COMPILE_ON_WINDOWS
	// On windows we need to override new and delete operators
	// to ensure that always the right new/delete pair is called for an object instance
	// This bug is present in all the classes exported by a module that
	// can be instantiated/destroyed from external modules.
	// (this is a well known bug described in Q122675 of MSDN)
	void       * operator new(size_t tSize);
	void         operator delete(void * p);
#endif
};

#ifdef Success
	#undef Success
#endif


class KVILIB_API KviSSL
{
public:
	enum Method { Client , Server };
	enum Result { Success , NotInitialized , WantRead , WantWrite , ZeroReturn , FileIoError ,
					UnknownError , ObscureError , SSLError , SyscallError , RemoteEndClosedConnection };
public:
	KviSSL();
	~KviSSL();
public:
	SSL        * m_pSSL;
	SSL_CTX    * m_pSSLCtx;
	KviStr       m_szPass;
public:
	static void globalInit();
	static void globalDestroy();
public:
	bool initSocket(kvi_socket_t fd);
	bool initContext(KviSSL::Method m);
	void shutdown();
	KviSSL::Result connect();
	KviSSL::Result accept();
	int read(char * buffer,int len);
	int write(const char * buffer,int len);
	// SSL ERRORS
	unsigned long getLastError(bool bPeek = false);
	bool getLastErrorString(KviStr &buffer,bool bPeek = false);
	// Protocol error
	KviSSL::Result getProtocolError(int ret);
	KviSSLCertificate * getPeerCertificate();
	KviSSLCipherInfo * getCurrentCipherInfo();
	KviSSL::Result useCertificateFile(const char * cert,const char * pass);
	KviSSL::Result usePrivateKeyFile(const char * key,const char * pass);
#ifdef COMPILE_ON_WINDOWS
	// On windows we need to override new and delete operators
	// to ensure that always the right new/delete pair is called for an object instance
	// This bug is present in all the classes exported by a module that
	// can be instantiated/destroyed from external modules.
	// (this is a well known bug described in Q122675 of MSDN)
	void       * operator new(size_t tSize);
	void         operator delete(void * p);
#endif
private:
	KviSSL::Result connectOrAcceptError(int ret);
};


#endif //COMPILE_SSL_SUPPORT

#endif //_KVI_SSL_H_