PTLib  Version 2.14.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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: 32279 $
27  * $Author: rjongbloed $
28  * $Date: 2014-07-02 13:26:56 +1000 (Wed, 02 Jul 2014) $
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 X509_name_st;
45 struct evp_pkey_st;
46 struct evp_cipher_ctx_st;
47 struct dh_st;
48 struct aes_key_st;
49 struct SHAstate_st;
50 
55 };
56 
57 
59 #define PDECLARE_SSLPasswordNotifier(cls, fn) PDECLARE_NOTIFIER2(PString, cls, fn, bool)
60 
61 
66 class PSSLPrivateKey : public PObject
67 {
68  PCLASSINFO(PSSLPrivateKey, PObject);
69  public:
73 
77  unsigned modulus,
78  void (*callback)(int,int,void *) = NULL,
79  void *cb_arg = NULL
80  );
81 
88  const PFilePath & keyFile,
90  );
91 
95  const BYTE * keyData,
96  PINDEX keySize
97  );
98 
102  const PBYTEArray & keyData
103  );
104 
108  const PSSLPrivateKey & privKey
109  );
111  evp_pkey_st * privKey,
112  bool duplicate = true
113  );
114 
118  const PSSLPrivateKey & privKay
119  );
121  evp_pkey_st * privKay
122  );
123 
126  ~PSSLPrivateKey();
127 
130  operator evp_pkey_st *() const { return m_pkey; }
131 
134  void Attach(evp_pkey_st * key);
135 
139  unsigned modulus,
140  void (*callback)(int,int,void *) = NULL,
141  void *cb_arg = NULL
142  );
143 
146  bool IsValid() const { return m_pkey != NULL; }
147 
150  bool SetData(
151  const PBYTEArray & data
152  );
153 
156  PBYTEArray GetData() const;
157 
160  PString AsString() const;
161 
164  bool Parse(
165  const PString & keyStr
166  );
167 
173  PBoolean Load(
174  const PFilePath & keyFile,
176  const PSSLPasswordNotifier & notifier = PSSLPasswordNotifier()
177  );
178 
184  PBoolean Save(
185  const PFilePath & keyFile,
186  PBoolean append = false,
188  );
189 
190 
191  protected:
192  void FreePrivateKey();
193  evp_pkey_st * m_pkey;
194 };
195 
196 
201 class PSSLCertificate : public PObject
202 {
203  PCLASSINFO(PSSLCertificate, PObject);
204  public:
207  PSSLCertificate();
208 
215  const PFilePath & certFile,
217  );
218 
222  const BYTE * certData,
223  PINDEX certSize
224  );
225 
229  const PBYTEArray & certData
230  );
231 
235  const PString & certString
236  );
237 
241  const PSSLCertificate & cert
242  );
244  x509_st * cert,
245  bool duplicate = true
246  );
247 
251  const PSSLCertificate & cert
252  );
254  x509_st * cert
255  );
256 
260 
263  operator x509_st *() const { return m_certificate; }
264 
267  void Attach(x509_st * cert);
268 
271  bool IsValid() const { return m_certificate != NULL; }
272 
273 
283  const PString & subject,
284  const PSSLPrivateKey & key
285  );
286 
289  bool SetData(
290  const PBYTEArray & data
291  );
292 
295  PBYTEArray GetData() const;
296 
299  PString AsString() const;
300 
303  bool Parse(
304  const PString & certStr
305  );
306 
312  PBoolean Load(
313  const PFilePath & certFile,
315  );
316 
322  PBoolean Save(
323  const PFilePath & keyFile,
324  PBoolean append = false,
326  );
327 
328  class X509_Name : public PObject {
329  PCLASSINFO(X509_Name, PObject);
330  public:
331  X509_Name(X509_name_st * name = NULL) : m_name(name) { }
332 
333  Comparison Compare(const PObject & other) const;
334  void PrintOn(ostream & strm) const;
335 
336  bool IsValid() const { return m_name != NULL; }
337 
338  PString GetCommonName() const;
339  PString GetNID(int id) const;
341  int indent = -1 // Negative means single line
342  ) const;
343 
344  protected:
345  X509_name_st * m_name;
346  };
347 
350  bool GetIssuerName(X509_Name & name) const;
351 
354  bool GetSubjectName(X509_Name & name) const;
355  PString GetSubjectName() const;
356 
359  PString GetSubjectAltName() const;
360 
361  virtual void PrintOn(ostream & strm) const { strm << GetSubjectName(); }
362 
363  protected:
364  void FreeCertificate();
365  x509_st * m_certificate;
366 };
367 
368 
374 {
375  PCLASSINFO(PSSLDiffieHellman, PObject);
376  public:
380 
387  const PFilePath & dhFile,
389  );
390 
397  PINDEX numBits,
398  const BYTE * pData,
399  const BYTE * gData,
400  const BYTE * pubKey = NULL
401  );
402 
407  const PBYTEArray & pData,
408  const PBYTEArray & gData,
409  const PBYTEArray & pubKey = PBYTEArray()
410  );
411 
415  const PSSLDiffieHellman & dh
416  );
417 
421  const PSSLDiffieHellman & dh
422  );
423 
427 
430  bool IsValid() const { return m_dh != NULL; }
431 
434  operator dh_st *() const { return m_dh; }
435 
441  PBoolean Load(
442  const PFilePath & dhFile,
444  );
445 
448  PINDEX GetNumBits() const;
449 
452  PBYTEArray GetModulus() const;
453 
456  PBYTEArray GetGenerator() const;
457 
460  PBYTEArray GetHalfKey() const;
461 
464  bool ComputeSessionKey(const PBYTEArray & otherHalf);
465 
468  const PBYTEArray & GetSessionKey() const { return m_sessionKey; }
469 
470  protected:
471  bool Construct(const BYTE * pData, PINDEX pSize,
472  const BYTE * gData, PINDEX gSize,
473  const BYTE * kData, PINDEX kSize);
474 
475  dh_st * m_dh;
477 };
478 
479 
480 #ifdef P_SSL_AES
481 
482 class PAESContext : public PObject
483 {
484  PCLASSINFO(PAESContext, PObject);
485  public:
486  PAESContext();
487  PAESContext(bool encrypt, const void * key, PINDEX numBits);
488  ~PAESContext();
489 
490  void SetEncrypt(const void * key, PINDEX numBits);
491  void SetDecrypt(const void * key, PINDEX numBits);
492 
493  void Encrypt(const void * in, void * out);
494  void Decrypt(const void * in, void * out);
495 
496  protected:
497  aes_key_st * m_key;
498 };
499 #endif // P_SSL_AES
500 
501 
504 {
505  PCLASSINFO(PSSLCipherContext, PObject);
506  public:
508  bool encrypt
509  );
510 
512 
515  operator evp_cipher_ctx_st *() const { return m_context; }
516 
518  bool IsEncrypt() const;
519 
522  PString GetAlgorithm() const;
523 
527  bool SetAlgorithm(
528  const PString & name
529  );
530 
533  bool SetKey(const PBYTEArray & key) { return SetKey(key, key.GetSize()); }
534  bool SetKey(const BYTE * keyPtr, PINDEX keyLen);
535 
538  bool SetIV(const PBYTEArray & iv) { return SetIV(iv, iv.GetSize()); }
539  bool SetIV(const BYTE * ivPtr, PINDEX ivLen);
540 
541  enum PadMode {
546  };
547 
552  bool SetPadding(PadMode pad);
553 
558  PadMode GetPadding() const { return m_padMode; }
559 
562  bool Process(
563  const PBYTEArray & in,
564  PBYTEArray & out
565  );
566  bool Process(
567  const BYTE * inPtr,
568  PINDEX inLen,
569  BYTE * outPtr,
570  PINDEX & outLen,
571  bool partial = false
572  );
573 
576  PINDEX GetKeyLength() const;
577 
580  PINDEX GetIVLength() const;
581 
584  PINDEX GetBlockSize() const;
585 
588  PINDEX GetBlockedDataSize(PINDEX size) const;
589 
590  protected:
592  evp_cipher_ctx_st * m_context;
593 
594  private:
596  void operator=(const PSSLCipherContext &) { }
597 };
598 
599 
601 class PSHA1Context : public PObject
602 {
603  PCLASSINFO(PSHA1Context, PObject);
604  public:
605  PSHA1Context();
606  ~PSHA1Context();
607 
608  enum { BlockSize = 64 };
609 
610  void Update(const void * data, PINDEX length);
611  void Update(const PString & str) { Update((const char *)str, str.GetLength()); }
612 
613  typedef BYTE Digest[20];
614  void Finalise(Digest result);
615 
616  static void Process(const void * data, PINDEX length, Digest result);
617  static void Process(const PString & str, Digest result) { Process((const char *)str, str.GetLength(), result); }
618 
619  protected:
620  SHAstate_st * m_context;
621 
622  private:
623  PSHA1Context(const PSHA1Context &) { }
624  void operator=(const PSHA1Context &) { }
625 };
626 
627 
633 class PSSLContext : public PObject
634 {
635  PCLASSINFO(PSSLContext, PObject);
636  public:
637  enum Method {
641  };
642 
651  PSSLContext(
652  const void * sessionId = NULL,
653  PINDEX idSize = 0
654  );
655  PSSLContext(
656  Method method,
657  const void * sessionId = NULL,
658  PINDEX idSize = 0
659  );
660 
663  ~PSSLContext();
664 
667  operator ssl_ctx_st *() const { return m_context; }
668 
671  bool SetVerifyLocations(
672  const PFilePath & caFile,
673  const PDirectory & caDir
674  );
675 
679  const PSSLCertificate & cert
680  );
681 
682  P_DECLARE_ENUM(VerifyMode,
683  VerifyNone,
684  VerifyPeer,
685  VerifyPeerMandatory
686  );
687 
690  void SetVerifyMode(
691  VerifyMode mode,
692  unsigned depth = 9
693  );
694 
697  VerifyMode GetVerifyMode() const;
698 
701  bool AddClientCA(
702  const PSSLCertificate & certificate
703  );
704  bool AddClientCA(
705  const PList<PSSLCertificate> & certificates
706  );
707 
710  bool UseCertificate(
711  const PSSLCertificate & certificate
712  );
713 
716  bool UsePrivateKey(
717  const PSSLPrivateKey & key
718  );
719 
722  bool UseDiffieHellman(
723  const PSSLDiffieHellman & dh
724  );
725 
728  bool SetCipherList(
729  const PString & ciphers
730  );
731 
734  bool SetCredentials(
735  const PString & authority,
736  const PString & certificate,
737  const PString & privateKey,
738  bool create = false
739  );
740 
742  void SetPasswordNotifier(
743  const PSSLPasswordNotifier & notifier
744  );
745 
746  protected:
747  void Construct(Method method, const void * sessionId, PINDEX idSize);
748 
749  ssl_ctx_st * m_context;
751 
752  private:
753  PSSLContext(const PSSLContext &) { }
754  void operator=(const PSSLContext &) { }
755 };
756 
757 
761 {
763  public:
767  PSSLChannel(
768  PSSLContext * context = NULL,
769  PBoolean autoDeleteContext = false
770  );
771  PSSLChannel(
772  PSSLContext & context
773  );
774 
777  ~PSSLChannel();
778 
779  // Overrides from PChannel
780  virtual PBoolean Read(void * buf, PINDEX len);
781  virtual PBoolean Write(const void * buf, PINDEX len);
782  virtual PBoolean Close();
783  virtual PBoolean Shutdown(ShutdownValue) { return true; }
784  virtual PString GetErrorText(ErrorGroup group = NumErrorGroups) const;
785  virtual PBoolean ConvertOSError(P_INT_PTR libcReturnValue, ErrorGroup group = LastGeneralError);
786 
787  // New functions
792  PBoolean Accept();
793 
797  PChannel & channel
798  );
799 
803  PChannel * channel,
804  PBoolean autoDelete = true
805  );
806 
807 
812  PBoolean Connect();
813 
817  PChannel & channel
818  );
819 
823  PChannel * channel,
824  PBoolean autoDelete = true
825  );
826 
829  bool AddClientCA(
830  const PSSLCertificate & certificate
831  );
832  bool AddClientCA(
833  const PList<PSSLCertificate> & certificates
834  );
835 
839  const PSSLCertificate & certificate
840  );
841 
845  const PSSLPrivateKey & key
846  );
847 
850  PString GetCipherList() const;
851 
852  typedef PSSLContext::VerifyMode VerifyMode;
853 
856  void SetVerifyMode(
857  VerifyMode mode
858  );
859 
866  bool GetPeerCertificate(
867  PSSLCertificate & certificate,
868  PString * error = NULL
869  );
870 
871  PSSLContext * GetContext() const { return m_context; }
872 
873  virtual PBoolean RawSSLRead(void * buf, PINDEX & len);
874 
875  protected:
876  void Construct(PSSLContext * ctx, PBoolean autoDel);
877 
887  virtual PBoolean OnOpen();
888 
889  protected:
892  ssl_st * m_ssl;
893 };
894 
895 
896 #endif // PTLIB_PSSL_H
897 
898 
899 // End Of File ///////////////////////////////////////////////////////////////