Click here to Skip to main content
15,886,258 members
Articles / Desktop Programming / MFC

CSslSocket - SSL/TLS enabled CSocket

Rate me:
Please Sign up or sign in to vote.
4.60/5 (12 votes)
24 Nov 20012 min read 832.5K   11.1K   83  
CSocket derived class with SSL/TLS extension
/********************************************************************************************
* MOD-NAME      : sslsocket.h
* LONG-NAME     : 
*
* AUTHOR        : Martin Ziacek (martin.ziacek@pobox.sk)
* DEPARTMENT    : 
* TELEPHONE     : 
* CREATION-DATE : 29/05/2001 15:27:01
* SP-NO         : 
* FUNCTION      : 
* 
*********************************************************************************************/
#if !defined(AFX_SSLSOCKET_H__CCE67526_C572_47F0_B3E9_79D2CE88D5F5__INCLUDED_)
#define AFX_SSLSOCKET_H__CCE67526_C572_47F0_B3E9_79D2CE88D5F5__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

// sslsocket.h : header file
//
#define IO_BUFFER_SIZE  0x10000

#ifndef SEC_I_CONTEXT_EXPIRED
#define SEC_I_CONTEXT_EXPIRED            ((HRESULT)0x00090317L)
#endif
/////////////////////////////////////////////////////////////////////////////
// CSslSocket

class CSslSocket : public CSocket
{
	DECLARE_DYNAMIC(CSslSocket);
// Attributes
public:

// Operations
public:
	CSslSocket();
	virtual ~CSslSocket();

	BOOL Create(UINT nSocketPort = 443, LPCTSTR lpszSocketAddress = NULL,const TCHAR *szCertName = NULL, BOOL bMachineStore = FALSE, DWORD dwProtocol = 0);

	virtual BOOL Accept(CSslSocket & rConnectedSocket, SOCKADDR* lpSockAddr = NULL, int* lpSockAddrLen = NULL);

	virtual void Close();

	BOOL Connect(LPCTSTR lpszHostAddress, UINT nHostPort);
	BOOL Connect(const SOCKADDR* lpSockAddr, int nSockAddrLen);

	BOOL Listen(int nConnectionBacklog=5,BOOL bAuthClient = FALSE);

	DWORD GetLastError();

// Overrides
public:
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CSslSocket)
	public:
	virtual int Send(const void* lpBuf, int nBufLen, int nFlags = 0);
	virtual int Receive(void* lpBuf, int nBufLen, int nFlags = 0);
	//}}AFX_VIRTUAL

	// Generated message map functions
	//{{AFX_MSG(CSslSocket)
		// NOTE - the ClassWizard will add and remove member functions here.
	//}}AFX_MSG

// Implementation
protected:
	void SetLastError(DWORD dwErrCode);
	BOOL LoadSecurityLibrary(void);

	SECURITY_STATUS ClientCreateCredentials(const TCHAR *pszUserName, PCredHandle phCreds);
	BOOL ClientConnect(const TCHAR *szHostName);
	LONG ClientDisconnect(PCredHandle phCreds, CtxtHandle *phContext);
	SECURITY_STATUS ClientHandshakeLoop(PCredHandle phCreds, CtxtHandle *phContext, BOOL fDoInitialRead, SecBuffer *pExtraData);
	SECURITY_STATUS ClientHandshake(PCredHandle phCreds, const TCHAR *pszServerName, CtxtHandle *phContext, SecBuffer *pExtraData);
	DWORD ClientVerifyCertificate(PCCERT_CONTEXT pServerCert,const TCHAR *pszServerName,DWORD dwCertFlags);

	SECURITY_STATUS ServerCreateCredentials(const TCHAR *pszUserName, PCredHandle phCreds);
	BOOL ServerConnect(SOCKADDR* lpSockAddr, int* lpSockAddrLen);
	LONG ServerDisconect(PCredHandle phCreds, CtxtHandle *phContext);
	BOOL ServerHandshakeLoop(PCtxtHandle phContext, PCredHandle phCred, BOOL fClientAuth, BOOL fDoInitialRead, BOOL NewContext);
	DWORD ServerVerifyCertificate(PCCERT_CONTEXT  pServerCert, DWORD dwCertFlags);

	BOOL m_bServer;

	CString m_CsCertName;
	BOOL m_bMachineStore;
	DWORD m_dwProtocol;

	PCCERT_CONTEXT  m_pCertContext;

	DWORD m_dwLastError;

	BOOL m_bAuthClient;

	HCERTSTORE      m_hMyCertStore;
	SCHANNEL_CRED   m_SchannelCred;
	CredHandle m_hCreds;
	CtxtHandle m_hContext;

	HMODULE m_hSecurity;
	SecurityFunctionTable m_SecurityFunc;

	BOOL m_bConInit;
	BOOL m_bAllowPlainText;

	BYTE *m_pbReceiveBuf;
	DWORD m_dwReceiveBuf;

	BYTE *m_pbIoBuffer;
	DWORD m_cbIoBuffer;

//	CEvent m_CeIO;
};

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_SSLSOCKET_H__CCE67526_C572_47F0_B3E9_79D2CE88D5F5__INCLUDED_)

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions