pssl.h

Go to the documentation of this file.
00001 /*
00002  * pssl.h
00003  *
00004  * Secure Sockets Layer channel interface class.
00005  *
00006  * Portable Windows Library
00007  *
00008  * Copyright (c) 1993-2002 Equivalence Pty. Ltd.
00009  *
00010  * The contents of this file are subject to the Mozilla Public License
00011  * Version 1.0 (the "License"); you may not use this file except in
00012  * compliance with the License. You may obtain a copy of the License at
00013  * http://www.mozilla.org/MPL/
00014  *
00015  * Software distributed under the License is distributed on an "AS IS"
00016  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
00017  * the License for the specific language governing rights and limitations
00018  * under the License.
00019  *
00020  * The Original Code is Portable Windows Library.
00021  *
00022  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
00023  *
00024  * Contributor(s): ______________________________________.
00025  *
00026  * $Revision: 20385 $
00027  * $Author: rjongbloed $
00028  * $Date: 2008-06-04 10:40:38 +0000 (Wed, 04 Jun 2008) $
00029  */
00030 
00031 #ifndef _PSSL_H
00032 #define _PSSL_H
00033 
00034 #ifdef P_USE_PRAGMA
00035 #pragma interface
00036 #endif
00037 
00038 #include <ptlib/sockets.h>
00039 
00040 
00041 struct ssl_st;
00042 struct ssl_ctx_st;
00043 struct x509_st;
00044 struct evp_pkey_st;
00045 struct dh_st;
00046 
00047 enum PSSLFileTypes {
00048   PSSLFileTypePEM,
00049   PSSLFileTypeASN1,
00050   PSSLFileTypeDEFAULT
00051 };
00052 
00053 
00058 class PSSLPrivateKey : public PObject
00059 {
00060   PCLASSINFO(PSSLPrivateKey, PObject);
00061   public:
00064     PSSLPrivateKey();
00065 
00068     PSSLPrivateKey(
00069       unsigned modulus,   
00070       void (*callback)(int,int,void *) = NULL,  
00071       void *cb_arg = NULL                       
00072     );
00073 
00079     PSSLPrivateKey(
00080       const PFilePath & keyFile,  
00081       PSSLFileTypes fileType = PSSLFileTypeDEFAULT  
00082     );
00083 
00086     PSSLPrivateKey(
00087       const BYTE * keyData,   
00088       PINDEX keySize          
00089     );
00090 
00093     PSSLPrivateKey(
00094       const PBYTEArray & keyData  
00095     );
00096 
00099     PSSLPrivateKey(
00100       const PSSLPrivateKey & privKey
00101     );
00102 
00105     PSSLPrivateKey & operator=(
00106       const PSSLPrivateKey & privKay
00107     );
00108 
00111     ~PSSLPrivateKey();
00112 
00115     operator evp_pkey_st *() const { return key; }
00116 
00119     PBoolean Create(
00120       unsigned modulus,   
00121       void (*callback)(int,int,void *) = NULL,  
00122       void *cb_arg = NULL                       
00123     );
00124 
00127     PBYTEArray GetData() const;
00128 
00131     PString AsString() const;
00132 
00138     PBoolean Load(
00139       const PFilePath & keyFile,  
00140       PSSLFileTypes fileType = PSSLFileTypeDEFAULT  
00141     );
00142 
00148     PBoolean Save(
00149       const PFilePath & keyFile,  
00150       PBoolean append = PFalse,        
00151       PSSLFileTypes fileType = PSSLFileTypeDEFAULT  
00152     );
00153 
00154 
00155   protected:
00156     evp_pkey_st * key;
00157 };
00158 
00159 
00164 class PSSLCertificate : public PObject
00165 {
00166   PCLASSINFO(PSSLCertificate, PObject);
00167   public:
00170     PSSLCertificate();
00171 
00177     PSSLCertificate(
00178       const PFilePath & certFile, 
00179       PSSLFileTypes fileType = PSSLFileTypeDEFAULT  
00180     );
00181 
00184     PSSLCertificate(
00185       const BYTE * certData,  
00186       PINDEX certSize        
00187     );
00188 
00191     PSSLCertificate(
00192       const PBYTEArray & certData  
00193     );
00194 
00197     PSSLCertificate(
00198       const PString & certString  
00199     );
00200 
00203     PSSLCertificate(
00204       const PSSLCertificate & cert
00205     );
00206 
00209     PSSLCertificate & operator=(
00210       const PSSLCertificate & cert
00211     );
00212 
00215     ~PSSLCertificate();
00216 
00219     operator x509_st *() const { return certificate; }
00220 
00229     PBoolean CreateRoot(
00230       const PString & subject,    
00231       const PSSLPrivateKey & key  
00232     );
00233 
00236     PBYTEArray GetData() const;
00237 
00240     PString AsString() const;
00241 
00247     PBoolean Load(
00248       const PFilePath & certFile, 
00249       PSSLFileTypes fileType = PSSLFileTypeDEFAULT  
00250     );
00251 
00257     PBoolean Save(
00258       const PFilePath & keyFile,  
00259       PBoolean append = PFalse,        
00260       PSSLFileTypes fileType = PSSLFileTypeDEFAULT  
00261     );
00262 
00263 
00264   protected:
00265     x509_st * certificate;
00266 };
00267 
00268 
00273 class PSSLDiffieHellman : public PObject
00274 {
00275   PCLASSINFO(PSSLDiffieHellman, PObject);
00276   public:
00279     PSSLDiffieHellman();
00280 
00286     PSSLDiffieHellman(
00287       const PFilePath & dhFile, 
00288       PSSLFileTypes fileType = PSSLFileTypeDEFAULT  
00289     );
00290 
00293     PSSLDiffieHellman(
00294       const BYTE * pData, 
00295       PINDEX pSize,       
00296       const BYTE * gData, 
00297       PINDEX gSize        
00298     );
00299 
00302     PSSLDiffieHellman(
00303       const PSSLDiffieHellman & dh
00304     );
00305 
00308     PSSLDiffieHellman & operator=(
00309       const PSSLDiffieHellman & dh
00310     );
00311 
00314     ~PSSLDiffieHellman();
00315 
00318     operator dh_st *() const { return dh; }
00319 
00325     PBoolean Load(
00326       const PFilePath & dhFile, 
00327       PSSLFileTypes fileType = PSSLFileTypeDEFAULT  
00328     );
00329 
00330   protected:
00331     dh_st * dh;
00332 };
00333 
00334 
00340 class PSSLContext {
00341   public:
00342     enum Method {
00343       SSLv23,
00344       SSLv2,
00345       SSLv3,
00346       TLSv1
00347     };
00348 
00357     PSSLContext(
00358       const void * sessionId = NULL,  
00359       PINDEX idSize = 0               
00360     );
00361     PSSLContext(
00362       Method method,                  
00363       const void * sessionId = NULL,  
00364       PINDEX idSize = 0               
00365     );
00366 
00369     ~PSSLContext();
00370 
00373     operator ssl_ctx_st *() const { return context; }
00374 
00377     PBoolean SetCAPath(
00378       const PDirectory & caPath   
00379     );
00380 
00383     PBoolean SetCAFile(
00384       const PFilePath & caFile    
00385     );
00386 
00389     PBoolean UseCertificate(
00390       const PSSLCertificate & certificate
00391     );
00392 
00395     PBoolean UsePrivateKey(
00396       const PSSLPrivateKey & key
00397     );
00398 
00401     PBoolean UseDiffieHellman(
00402       const PSSLDiffieHellman & dh
00403     );
00404 
00407     PBoolean SetCipherList(
00408       const PString & ciphers   
00409     );
00410 
00411   protected:
00412     void Construct(Method method, const void * sessionId, PINDEX idSize);
00413     ssl_ctx_st * context;
00414 };
00415 
00416 
00419 class PSSLChannel : public PIndirectChannel
00420 {
00421   PCLASSINFO(PSSLChannel, PIndirectChannel)
00422   public:
00426     PSSLChannel(
00427       PSSLContext * context = NULL,   
00428       PBoolean autoDeleteContext = PFalse  
00429     );
00430     PSSLChannel(
00431       PSSLContext & context           
00432     );
00433 
00436     ~PSSLChannel();
00437 
00438     // Overrides from PChannel
00439     virtual PBoolean Read(void * buf, PINDEX len);
00440     virtual PBoolean Write(const void * buf, PINDEX len);
00441     virtual PBoolean Close();
00442     virtual PBoolean Shutdown(ShutdownValue) { return PTrue; }
00443     virtual PString GetErrorText(ErrorGroup group = NumErrorGroups) const;
00444     virtual PBoolean ConvertOSError(int error, ErrorGroup group = LastGeneralError);
00445 
00446     // New functions
00451     PBoolean Accept();
00452 
00455     PBoolean Accept(
00456       PChannel & channel  
00457     );
00458 
00461     PBoolean Accept(
00462       PChannel * channel,     
00463       PBoolean autoDelete = PTrue  
00464     );
00465 
00466 
00471     PBoolean Connect();
00472 
00475     PBoolean Connect(
00476       PChannel & channel  
00477     );
00478 
00481     PBoolean Connect(
00482       PChannel * channel,     
00483       PBoolean autoDelete = PTrue  
00484     );
00485 
00488     PBoolean UseCertificate(
00489       const PSSLCertificate & certificate
00490     );
00491 
00494     PBoolean UsePrivateKey(
00495       const PSSLPrivateKey & key
00496     );
00497 
00498     enum VerifyMode {
00499       VerifyNone,
00500       VerifyPeer,
00501       VerifyPeerMandatory,
00502     };
00503 
00504     void SetVerifyMode(
00505       VerifyMode mode
00506     );
00507 
00508     PSSLContext * GetContext() const { return context; }
00509 
00510     virtual PBoolean RawSSLRead(void * buf, PINDEX & len);
00511 
00512   protected:
00522     virtual PBoolean OnOpen();
00523 
00524   protected:
00525     PSSLContext * context;
00526     PBoolean          autoDeleteContext;
00527     ssl_st      * ssl;
00528 };
00529 
00530 #endif // _PSSL_H

Generated on Mon Feb 23 01:57:54 2009 for PTLib by  doxygen 1.5.1