PTLib  Version 2.10.10
pssl.h
Go to the documentation of this file.
1 /*
2  * pssl.h
3  *
4  * Secure Sockets Layer channel interface class.
5  *
6  * Portable Windows Library
7  *
8  * Copyright (c) 1993-2002 Equivalence Pty. Ltd.
9  *
10  * The contents of this file are subject to the Mozilla Public License
11  * Version 1.0 (the "License"); you may not use this file except in
12  * compliance with the License. You may obtain a copy of the License at
13  * http://www.mozilla.org/MPL/
14  *
15  * Software distributed under the License is distributed on an "AS IS"
16  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17  * the License for the specific language governing rights and limitations
18  * under the License.
19  *
20  * The Original Code is Portable Windows Library.
21  *
22  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
23  *
24  * Contributor(s): ______________________________________.
25  *
26  * $Revision: 25519 $
27  * $Author: rjongbloed $
28  * $Date: 2011-04-06 03:26:18 -0500 (Wed, 06 Apr 2011) $
29  */
30 
31 #ifndef PTLIB_PSSL_H
32 #define PTLIB_PSSL_H
33 
34 #ifdef P_USE_PRAGMA
35 #pragma interface
36 #endif
37 
38 #include <ptlib/sockets.h>
39 
40 
41 struct ssl_st;
42 struct ssl_ctx_st;
43 struct x509_st;
44 struct evp_pkey_st;
45 struct dh_st;
46 
51 };
52 
53 
58 class PSSLPrivateKey : public PObject
59 {
60  PCLASSINFO(PSSLPrivateKey, PObject);
61  public:
65 
69  unsigned modulus,
70  void (*callback)(int,int,void *) = NULL,
71  void *cb_arg = NULL
72  );
73 
80  const PFilePath & keyFile,
82  );
83 
87  const BYTE * keyData,
88  PINDEX keySize
89  );
90 
94  const PBYTEArray & keyData
95  );
96 
100  const PSSLPrivateKey & privKey
101  );
102 
106  const PSSLPrivateKey & privKay
107  );
108 
111  ~PSSLPrivateKey();
112 
115  operator evp_pkey_st *() const { return key; }
116 
120  unsigned modulus,
121  void (*callback)(int,int,void *) = NULL,
122  void *cb_arg = NULL
123  );
124 
127  PBYTEArray GetData() const;
128 
131  PString AsString() const;
132 
138  PBoolean Load(
139  const PFilePath & keyFile,
141  );
142 
148  PBoolean Save(
149  const PFilePath & keyFile,
150  PBoolean append = false,
152  );
153 
154 
155  protected:
156  evp_pkey_st * key;
157 };
158 
159 
164 class PSSLCertificate : public PObject
165 {
166  PCLASSINFO(PSSLCertificate, PObject);
167  public:
170  PSSLCertificate();
171 
178  const PFilePath & certFile,
180  );
181 
185  const BYTE * certData,
186  PINDEX certSize
187  );
188 
192  const PBYTEArray & certData
193  );
194 
198  const PString & certString
199  );
200 
204  const PSSLCertificate & cert
205  );
206 
210  const PSSLCertificate & cert
211  );
212 
216 
219  operator x509_st *() const { return certificate; }
220 
230  const PString & subject,
231  const PSSLPrivateKey & key
232  );
233 
236  PBYTEArray GetData() const;
237 
240  PString AsString() const;
241 
247  PBoolean Load(
248  const PFilePath & certFile,
250  );
251 
257  PBoolean Save(
258  const PFilePath & keyFile,
259  PBoolean append = false,
261  );
262 
263 
264  protected:
265  x509_st * certificate;
266 };
267 
268 
274 {
275  PCLASSINFO(PSSLDiffieHellman, PObject);
276  public:
280 
287  const PFilePath & dhFile,
289  );
290 
294  const BYTE * pData,
295  PINDEX pSize,
296  const BYTE * gData,
297  PINDEX gSize
298  );
299 
303  const PSSLDiffieHellman & dh
304  );
305 
309  const PSSLDiffieHellman & dh
310  );
311 
315 
318  operator dh_st *() const { return dh; }
319 
325  PBoolean Load(
326  const PFilePath & dhFile,
328  );
329 
330  protected:
331  dh_st * dh;
332 };
333 
334 
340 class PSSLContext {
341  public:
342  enum Method {
346  };
347 
356  PSSLContext(
357  const void * sessionId = NULL,
358  PINDEX idSize = 0
359  );
360  PSSLContext(
361  Method method,
362  const void * sessionId = NULL,
363  PINDEX idSize = 0
364  );
365 
368  ~PSSLContext();
369 
372  operator ssl_ctx_st *() const { return context; }
373 
377  const PDirectory & caPath
378  );
379 
383  const PFilePath & caFile
384  );
385 
389  const PSSLCertificate & certificate
390  );
391 
395  const PSSLPrivateKey & key
396  );
397 
401  const PSSLDiffieHellman & dh
402  );
403 
407  const PString & ciphers
408  );
409 
410  protected:
411  void Construct(Method method, const void * sessionId, PINDEX idSize);
412  ssl_ctx_st * context;
413 };
414 
415 
419 {
421  public:
425  PSSLChannel(
426  PSSLContext * context = NULL,
427  PBoolean autoDeleteContext = false
428  );
429  PSSLChannel(
431  );
432 
435  ~PSSLChannel();
436 
437  // Overrides from PChannel
438  virtual PBoolean Read(void * buf, PINDEX len);
439  virtual PBoolean Write(const void * buf, PINDEX len);
440  virtual PBoolean Close();
441  virtual PBoolean Shutdown(ShutdownValue) { return true; }
442  virtual PString GetErrorText(ErrorGroup group = NumErrorGroups) const;
443  virtual PBoolean ConvertOSError(int error, ErrorGroup group = LastGeneralError);
444 
445  // New functions
450  PBoolean Accept();
451 
455  PChannel & channel
456  );
457 
461  PChannel * channel,
462  PBoolean autoDelete = true
463  );
464 
465 
470  PBoolean Connect();
471 
475  PChannel & channel
476  );
477 
481  PChannel * channel,
482  PBoolean autoDelete = true
483  );
484 
488  const PSSLCertificate & certificate
489  );
490 
494  const PSSLPrivateKey & key
495  );
496 
497  enum VerifyMode {
501  };
502 
503  void SetVerifyMode(
504  VerifyMode mode
505  );
506 
507  PSSLContext * GetContext() const { return context; }
508 
509  virtual PBoolean RawSSLRead(void * buf, PINDEX & len);
510 
511  protected:
521  virtual PBoolean OnOpen();
522 
523  protected:
526  ssl_st * ssl;
527 };
528 
529 #endif // PTLIB_PSSL_H
530 
531 
532 // End Of File ///////////////////////////////////////////////////////////////
PSSLPrivateKey & operator=(const PSSLPrivateKey &privKay)
Create a copy of the private key.
Definition: pssl.h:48
VerifyMode
Definition: pssl.h:497
Definition: channel.h:569
x509_st * certificate
Definition: pssl.h:265
PBoolean SetCipherList(const PString &ciphers)
Set the available ciphers to those listed.
Definition: pssl.h:49
virtual PString GetErrorText(ErrorGroup group=NumErrorGroups) const
Get error message description.
void Construct(Method method, const void *sessionId, PINDEX idSize)
#define PCLASSINFO(cls, par)
Declare all the standard PTLib class information.
Definition: object.h:1049
Method
Definition: pssl.h:342
~PSSLCertificate()
Destroy and release storage for certificate.
PBoolean UseCertificate(const PSSLCertificate &certificate)
Use the certificate specified.
PBoolean Create(unsigned modulus, void(*callback)(int, int, void *)=NULL, void *cb_arg=NULL)
Create a new private key.
PBoolean SetCAPath(const PDirectory &caPath)
Set the path to locate CA certificates.
Error during other operation, eg Open()
Definition: channel.h:568
PSSLContext(const void *sessionId=NULL, PINDEX idSize=0)
Create a new context for SSL channels.
PBoolean UsePrivateKey(const PSSLPrivateKey &key)
Use the private key file specified.
Certificate for SSL.
Definition: pssl.h:164
PBoolean autoDeleteContext
Definition: pssl.h:525
virtual PBoolean ConvertOSError(int error, ErrorGroup group=LastGeneralError)
Convert an operating system error into platform independent error.
This class describes a full description for a file on the particular platform.
Definition: filepath.h:65
PBoolean UsePrivateKey(const PSSLPrivateKey &key)
Use the private key specified.
PString AsString() const
Get the certificate as ASN1 DER base64 encoded data.
Definition: pssl.h:498
~PSSLPrivateKey()
Destroy and release storage for private key.
PBoolean Load(const PFilePath &certFile, PSSLFileTypes fileType=PSSLFileTypeDEFAULT)
Load certificate from file.
This is a channel that operates indirectly through another channel(s).
Definition: indchan.h:49
ssl_st * ssl
Definition: pssl.h:526
PBoolean UseCertificate(const PSSLCertificate &certificate)
Use the certificate specified.
PBYTEArray GetData() const
Get the certificate as binary ASN1 DER encoded data.
Definition: pssl.h:50
ShutdownValue
Definition: channel.h:433
Private key for SSL.
Definition: pssl.h:58
Definition: pssl.h:500
PBoolean Save(const PFilePath &keyFile, PBoolean append=false, PSSLFileTypes fileType=PSSLFileTypeDEFAULT)
Save private key to file.
PBYTEArray GetData() const
Get the certificate as binary ASN1 DER encoded data.
Definition: pssl.h:345
BOOL PBoolean
Definition: object.h:102
Class to represent a directory in the operating system file system.
Definition: pdirect.h:182
dh_st * dh
Definition: pssl.h:331
PBoolean CreateRoot(const PString &subject, const PSSLPrivateKey &key)
Create a new root certificate.
Definition: pssl.h:499
PSSLChannel(PSSLContext *context=NULL, PBoolean autoDeleteContext=false)
Create a new channel given the context.
virtual PBoolean RawSSLRead(void *buf, PINDEX &len)
Definition: pssl.h:344
Array of unsigned characters.
Definition: array.h:670
PBoolean UseDiffieHellman(const PSSLDiffieHellman &dh)
Use the Diffie-Hellman parameters specified.
Definition: pssl.h:343
PBoolean Load(const PFilePath &keyFile, PSSLFileTypes fileType=PSSLFileTypeDEFAULT)
Load private key from file.
Abstract class defining I/O channel semantics.
Definition: channel.h:107
PBoolean Connect()
Connect to remote server.
~PSSLChannel()
Close and clear the SSL channel.
The character string class.
Definition: pstring.h:108
~PSSLDiffieHellman()
Destroy and release storage for Diffie-Hellman parameters.
Context for SSL channels.
Definition: pssl.h:340
PSSLDiffieHellman & operator=(const PSSLDiffieHellman &dh)
Create a copy of the Diffie-Hellman parameters.
evp_pkey_st * key
Definition: pssl.h:156
This class will start a secure SSL based channel.
Definition: pssl.h:418
virtual PBoolean OnOpen()
This callback is executed when the Open() function is called with open channels.
PString AsString() const
Get the certificate as ASN1 DER base64 encoded data.
ErrorGroup
Error groups.
Definition: channel.h:565
void SetVerifyMode(VerifyMode mode)
PBoolean Load(const PFilePath &dhFile, PSSLFileTypes fileType=PSSLFileTypeDEFAULT)
Load Diffie-Hellman parameters from file.
PSSLCertificate()
Create an empty certificate.
virtual PBoolean Shutdown(ShutdownValue)
Close one or both of the data streams associated with a channel.
Definition: pssl.h:441
virtual PBoolean Write(const void *buf, PINDEX len)
Low level write to the channel.
PBoolean Accept()
Accept a new inbound connection (server).
virtual PBoolean Read(void *buf, PINDEX len)
Low level read from the channel.
virtual PBoolean Close()
Close the channel.
PSSLCertificate & operator=(const PSSLCertificate &cert)
Create a copy of the certificate.
PBoolean Save(const PFilePath &keyFile, PBoolean append=false, PSSLFileTypes fileType=PSSLFileTypeDEFAULT)
Save certificate to file.
PSSLPrivateKey()
Create an empty private key.
Diffie-Hellman parameters for SSL.
Definition: pssl.h:273
PSSLContext * context
Definition: pssl.h:524
PBoolean SetCAFile(const PFilePath &caFile)
Set the CA certificate file.
PSSLContext * GetContext() const
Definition: pssl.h:507
Ultimate parent class for all objects in the class library.
Definition: object.h:1118
ssl_ctx_st * context
Definition: pssl.h:412
PSSLDiffieHellman()
Create an empty set of Diffie-Hellman parameters.
~PSSLContext()
Clean up the SSL context.
PSSLFileTypes
Definition: pssl.h:47