00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
#ifndef _PSSL_H
00087
#define _PSSL_H
00088
00089
#ifdef P_USE_PRAGMA
00090
#pragma interface
00091
#endif
00092
00093
#include <ptlib/sockets.h>
00094
00095
00096
struct ssl_st;
00097
struct ssl_ctx_st;
00098
struct x509_st;
00099
struct evp_pkey_st;
00100
struct dh_st;
00101
00102 enum PSSLFileTypes {
00103
PSSLFileTypePEM,
00104
PSSLFileTypeASN1,
00105
PSSLFileTypeDEFAULT
00106 };
00107
00108
00113 class PSSLPrivateKey :
public PObject
00114 {
00115
PCLASSINFO(
PSSLPrivateKey,
PObject);
00116
public:
00119
PSSLPrivateKey();
00120
00123
PSSLPrivateKey(
00124
unsigned modulus,
00125
void (*callback)(
int,
int,
void *) = NULL,
00126
void *cb_arg = NULL
00127 );
00128
00134
PSSLPrivateKey(
00135
const PFilePath & keyFile,
00136
PSSLFileTypes fileType =
PSSLFileTypeDEFAULT
00137 );
00138
00141
PSSLPrivateKey(
00142
const BYTE * keyData,
00143 PINDEX keySize
00144 );
00145
00148
PSSLPrivateKey(
00149
const PBYTEArray & keyData
00150 );
00151
00154
PSSLPrivateKey(
00155
const PSSLPrivateKey & privKey
00156 );
00157
00160
PSSLPrivateKey &
operator=(
00161
const PSSLPrivateKey & privKay
00162 );
00163
00166
~PSSLPrivateKey();
00167
00170 operator evp_pkey_st *()
const {
return key; }
00171
00174 BOOL Create(
00175
unsigned modulus,
00176
void (*callback)(
int,
int,
void *) = NULL,
00177
void *cb_arg = NULL
00178 );
00179
00182
PBYTEArray GetData() const;
00183
00186
PString AsString() const;
00187
00193 BOOL Load(
00194 const
PFilePath & keyFile,
00195 PSSLFileTypes fileType = PSSLFileTypeDEFAULT
00196 );
00197
00203 BOOL Save(
00204 const
PFilePath & keyFile,
00205 BOOL append = FALSE,
00206 PSSLFileTypes fileType = PSSLFileTypeDEFAULT
00207 );
00208
00209
00210 protected:
00211 evp_pkey_st * key;
00212 };
00213
00214
00219 class
PSSLCertificate : public
PObject
00220 {
00221
PCLASSINFO(
PSSLCertificate, PObject);
00222
public:
00225
PSSLCertificate();
00226
00232
PSSLCertificate(
00233
const PFilePath & certFile,
00234
PSSLFileTypes fileType =
PSSLFileTypeDEFAULT
00235 );
00236
00239
PSSLCertificate(
00240
const BYTE * certData,
00241 PINDEX certSize
00242 );
00243
00246
PSSLCertificate(
00247
const PBYTEArray & certData
00248 );
00249
00252
PSSLCertificate(
00253
const PString & certString
00254 );
00255
00258
PSSLCertificate(
00259
const PSSLCertificate & cert
00260 );
00261
00264
PSSLCertificate & operator=(
00265
const PSSLCertificate & cert
00266 );
00267
00270 ~
PSSLCertificate();
00271
00274 operator x509_st *()
const {
return certificate; }
00275
00284 BOOL CreateRoot(
00285
const PString & subject,
00286
const PSSLPrivateKey & key
00287 );
00288
00291
PBYTEArray GetData() const;
00292
00295
PString AsString() const;
00296
00302 BOOL Load(
00303 const
PFilePath & certFile,
00304 PSSLFileTypes fileType = PSSLFileTypeDEFAULT
00305 );
00306
00312 BOOL Save(
00313 const
PFilePath & keyFile,
00314 BOOL append = FALSE,
00315 PSSLFileTypes fileType = PSSLFileTypeDEFAULT
00316 );
00317
00318
00319 protected:
00320 x509_st * certificate;
00321 };
00322
00323
00328 class
PSSLDiffieHellman : public PObject
00329 {
00330
PCLASSINFO(
PSSLDiffieHellman, PObject);
00331
public:
00334
PSSLDiffieHellman();
00335
00341
PSSLDiffieHellman(
00342
const PFilePath & dhFile,
00343
PSSLFileTypes fileType =
PSSLFileTypeDEFAULT
00344 );
00345
00348
PSSLDiffieHellman(
00349
const BYTE * pData,
00350 PINDEX pSize,
00351
const BYTE * gData,
00352 PINDEX gSize
00353 );
00354
00357
PSSLDiffieHellman(
00358
const PSSLDiffieHellman & dh
00359 );
00360
00363
PSSLDiffieHellman & operator=(
00364
const PSSLDiffieHellman & dh
00365 );
00366
00369 ~
PSSLDiffieHellman();
00370
00373 operator dh_st *()
const {
return dh; }
00374
00380 BOOL Load(
00381
const PFilePath & dhFile,
00382 PSSLFileTypes fileType = PSSLFileTypeDEFAULT
00383 );
00384
00385
protected:
00386 dh_st * dh;
00387 };
00388
00389
00395 class PSSLContext {
00396
public:
00404
PSSLContext(
00405
const void * sessionId = NULL,
00406 PINDEX idSize = 0
00407 );
00408
00411 ~
PSSLContext();
00412
00415 operator ssl_ctx_st *()
const {
return context; }
00416
00419 BOOL SetCAPath(
00420
const PDirectory & caPath
00421 );
00422
00425 BOOL SetCAFile(
00426
const PFilePath & caFile
00427 );
00428
00431 BOOL UseCertificate(
00432
const PSSLCertificate & certificate
00433 );
00434
00437 BOOL UsePrivateKey(
00438
const PSSLPrivateKey & key
00439 );
00440
00443 BOOL UseDiffieHellman(
00444
const PSSLDiffieHellman & dh
00445 );
00446
00449 BOOL SetCipherList(
00450
const PString & ciphers
00451 );
00452
00453
protected:
00454 ssl_ctx_st * context;
00455 };
00456
00457
00460 class PSSLChannel :
public PIndirectChannel
00461 {
00462
PCLASSINFO(
PSSLChannel,
PIndirectChannel)
00463
public:
00467
PSSLChannel(
00468
PSSLContext * context = NULL,
00469 BOOL autoDeleteContext = FALSE
00470 );
00471
PSSLChannel(
00472
PSSLContext & context
00473 );
00474
00477 ~
PSSLChannel();
00478
00479
00480
virtual BOOL Read(
void * buf, PINDEX len);
00481
virtual BOOL Write(
const void * buf, PINDEX len);
00482
virtual BOOL Close();
00483 virtual BOOL Shutdown(ShutdownValue) {
return TRUE; }
00484
virtual PString GetErrorText(ErrorGroup group = NumErrorGroups)
const;
00485
virtual BOOL ConvertOSError(
int error, ErrorGroup group = LastGeneralError);
00486
00487
00492 BOOL Accept();
00493
00496 BOOL Accept(
00497
PChannel & channel
00498 );
00499
00502 BOOL Accept(
00503
PChannel * channel,
00504 BOOL autoDelete = TRUE
00505 );
00506
00507
00512 BOOL Connect();
00513
00516 BOOL Connect(
00517
PChannel & channel
00518 );
00519
00522 BOOL Connect(
00523
PChannel * channel,
00524 BOOL autoDelete = TRUE
00525 );
00526
00529 BOOL UseCertificate(
00530
const PSSLCertificate & certificate
00531 );
00532
00535 BOOL UsePrivateKey(
00536
const PSSLPrivateKey & key
00537 );
00538
00539 enum VerifyMode {
00540 VerifyNone,
00541 VerifyPeer,
00542 VerifyPeerMandatory,
00543 };
00544
00545
void SetVerifyMode(
00546 VerifyMode mode
00547 );
00548
00549 PSSLContext * GetContext()
const {
return context; }
00550
00551
virtual BOOL RawSSLRead(
void * buf, PINDEX & len);
00552
00553
protected:
00563
virtual BOOL OnOpen();
00564
00565
protected:
00566 PSSLContext * context;
00567 BOOL autoDeleteContext;
00568 ssl_st * ssl;
00569 };
00570
00571
#endif // _PSSL_H