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
00087
00088
00089
00090
00091
00092
00093
00094
#ifndef _PCYPHER
00095
#define _PCYPHER
00096
00097
#ifdef P_USE_PRAGMA
00098
#pragma interface
00099
#endif
00100
00131 class PBase64 :
public PObject
00132 {
00133
PCLASSINFO(
PBase64,
PObject);
00134
00135
public:
00139
PBase64();
00140
00141
void StartEncoding(
00142 BOOL useCRLFs = TRUE
00143 );
00144
00145
00146
void ProcessEncoding(
00147
const PString & str
00148 );
00149
void ProcessEncoding(
00150
const char * cstr
00151 );
00152
void ProcessEncoding(
00153
const PBYTEArray & data
00154 );
00155
void ProcessEncoding(
00156
const void * dataBlock,
00157 PINDEX length
00158 );
00159
00160
00166
PString GetEncodedString();
00167
00175
PString CompleteEncoding();
00176
00177
00178
static PString Encode(
00179
const PString & str
00180 );
00181
static PString Encode(
00182
const char * cstr
00183 );
00184
static PString Encode(
00185
const PBYTEArray & data
00186 );
00187
static PString Encode(
00188
const void * dataBlock,
00189 PINDEX length
00190 );
00191
00192
00193
00194
void StartDecoding();
00195
00196
00202 BOOL
ProcessDecoding(
00203
const PString & str
00204 );
00205 BOOL ProcessDecoding(
00206
const char * cstr
00207 );
00208
00214 BOOL
GetDecodedData(
00215
void * dataBlock,
00216 PINDEX length
00217 );
00218
PBYTEArray GetDecodedData();
00219
00227 BOOL
IsDecodeOK() {
return perfectDecode; }
00228
00229
00241
static PString Decode(
00242
const PString & str
00243 );
00244
static BOOL Decode(
00245
const PString & str,
00246
PBYTEArray & data
00247 );
00248
static BOOL Decode(
00249
const PString & str,
00250
void * dataBlock,
00251 PINDEX length
00252 );
00253
00254
00255
00256
private:
00257
void OutputBase64(
const BYTE * data);
00258
00259
PString encodedString;
00260 PINDEX encodeLength;
00261 BYTE saveTriple[3];
00262 PINDEX saveCount;
00263 PINDEX nextLine;
00264 BOOL useCRLFs;
00265
00266 BOOL perfectDecode;
00267 PINDEX quadPosition;
00268
PBYTEArray decodedData;
00269 PINDEX decodeSize;
00270 };
00271
00272 class PMessageDigest :
public PObject
00273 {
00274
PCLASSINFO(
PMessageDigest,
PObject)
00275
00276
public:
00278
PMessageDigest();
00279
00280 class Result {
00281
public:
00282 PINDEX
GetSize()
const {
return value.GetSize(); }
00283 const BYTE *
GetPointer()
const {
return (
const BYTE *)value; }
00284
00285
private:
00286
PBYTEArray value;
00287
friend class PMessageDigest5;
00288
friend class PMessageDigestSHA1;
00289 };
00290
00292
virtual void Start() = 0;
00293
00294
virtual void Process(
00295
const void * dataBlock,
00296 PINDEX length
00297 );
00298
00300
virtual void Process(
00301
const PString & str
00302 );
00304
virtual void Process(
00305
const char * cstr
00306 );
00308
virtual void Process(
00309
const PBYTEArray & data
00310 );
00311
00319
virtual PString CompleteDigest();
00320
virtual void CompleteDigest(
00321 Result & result
00322 );
00323
00324
protected:
00325
virtual void InternalProcess(
00326
const void * dataBlock,
00327 PINDEX length
00328 ) = 0;
00329
00330
virtual void InternalCompleteDigest(
00331 Result & result
00332 ) = 0;
00333 };
00334
00335
00341 class PMessageDigest5 :
public PMessageDigest
00342 {
00343
PCLASSINFO(
PMessageDigest5,
PMessageDigest)
00344
00345
public:
00347
PMessageDigest5();
00348
00350
void Start();
00351
00353
static PString Encode(
00354
const PString & str
00355 );
00357
static void Encode(
00358
const PString & str,
00359 Result & result
00360 );
00362
static PString Encode(
00363
const char * cstr
00364 );
00366
static void Encode(
00367
const char * cstr,
00368 Result & result
00369 );
00371
static PString Encode(
00372
const PBYTEArray & data
00373 );
00375
static void Encode(
00376
const PBYTEArray & data,
00377 Result & result
00378 );
00380
static PString Encode(
00381
const void * dataBlock,
00382 PINDEX length
00383 );
00389
static void Encode(
00390
const void * dataBlock,
00391 PINDEX length,
00392 Result & result
00393 );
00394
00395
00396 class Code {
00397
private:
00398 PUInt32l value[4];
00399
friend class PMessageDigest5;
00400 };
00401
00403
static void Encode(
00404
const PString & str,
00405
Code & result
00406 );
00408
static void Encode(
00409
const char * cstr,
00410
Code & result
00411 );
00413
static void Encode(
00414
const PBYTEArray & data,
00415
Code & result
00416 );
00422
static void Encode(
00423
const void * dataBlock,
00424 PINDEX length,
00425
Code & result
00426 );
00427
virtual void Complete(
00428
Code & result
00429 );
00430
virtual PString Complete();
00431
00432
protected:
00433
virtual void InternalProcess(
00434
const void * dataBlock,
00435 PINDEX length
00436 );
00437
00438
virtual void InternalCompleteDigest(
00439 Result & result
00440 );
00441
00442
private:
00443
void Transform(
const BYTE * block);
00444
00446 BYTE buffer[64];
00448 DWORD state[4];
00450 PUInt64 count;
00451 };
00452
00453
#if P_SSL
00454
00459
class PMessageDigestSHA1 :
public PMessageDigest
00460 {
00461
PCLASSINFO(PMessageDigestSHA1,
PMessageDigest)
00462
00463 public:
00465 PMessageDigestSHA1();
00466 ~PMessageDigestSHA1();
00467
00469
void Start();
00470
00472 static
PString Encode(
00473 const
PString & str
00474 );
00476 static
void Encode(
00477 const
PString & str,
00478 Result & result
00479 );
00481 static
PString Encode(
00482 const
char * cstr
00483 );
00485 static
void Encode(
00486 const
char * cstr,
00487 Result & result
00488 );
00490 static
PString Encode(
00491 const
PBYTEArray & data
00492 );
00494 static
void Encode(
00495 const
PBYTEArray & data,
00496 Result & result
00497 );
00499 static
PString Encode(
00500 const
void * dataBlock,
00501 PINDEX length
00502 );
00508 static
void Encode(
00509 const
void * dataBlock,
00510 PINDEX length,
00511 Result & result
00512 );
00513
00514 protected:
00515 virtual
void InternalProcess(
00516 const
void * dataBlock,
00517 PINDEX length
00518 );
00519
00520
void InternalCompleteDigest(
00521 Result & result
00522 );
00523
00524 private:
00525
void * shaContext;
00526 };
00527
00528 #endif
00529
00533 class
PCypher : public
PObject
00534 {
00535
PCLASSINFO(
PCypher, PObject)
00536
00537
public:
00539 enum BlockChainMode {
00540 ElectronicCodebook,
00541 ECB = ElectronicCodebook,
00542 CypherBlockChaining,
00543 CBC = CypherBlockChaining,
00544 OutputFeedback,
00545 OFB = OutputFeedback,
00546 CypherFeedback,
00547 CFB = CypherFeedback,
00548 NumBlockChainModes
00549 };
00550
00551
00553
PString Encode(
00554
const PString & str
00555 );
00557
PString Encode(
00558
const PBYTEArray & clear
00559 );
00561
PString Encode(
00562
const void * data,
00563 PINDEX length
00564 );
00566
void Encode(
00567
const PBYTEArray & clear,
00568
PBYTEArray & coded
00569 );
00585
void Encode(
00586
const void * data,
00587 PINDEX length,
00588
PBYTEArray & coded
00589 );
00590
00592
PString Decode(
00593
const PString & cypher
00594 );
00596 BOOL Decode(
00597
const PString & cypher,
00598
PString & clear
00599 );
00601 BOOL Decode(
00602
const PString & cypher,
00603
PBYTEArray & clear
00604 );
00606 PINDEX Decode(
00607
const PString & cypher,
00608
void * data,
00609 PINDEX length
00610 );
00612 PINDEX Decode(
00613
const PBYTEArray & coded,
00614
void * data,
00615 PINDEX length
00616 );
00632 BOOL Decode(
00633
const PBYTEArray & coded,
00634
PBYTEArray & clear
00635 );
00636
00637
00638
protected:
00642
PCypher(
00643 PINDEX blockSize,
00644 BlockChainMode chainMode
00645 );
00646
PCypher(
00647
const void * keyData,
00648 PINDEX keyLength,
00649 PINDEX blockSize,
00650 BlockChainMode chainMode
00651 );
00652
00653
00655
virtual void Initialise(
00656 BOOL encoding
00657 ) = 0;
00658
00660
virtual void EncodeBlock(
00661
const void * in,
00662
void * out
00663 ) = 0;
00664
00665
00667
virtual void DecodeBlock(
00668
const void * in,
00669
void * out
00670 ) = 0;
00671
00672
00674 PBYTEArray key;
00676 PINDEX blockSize;
00678 BlockChainMode chainMode;
00679 };
00680
00681
00689 class PTEACypher :
public PCypher
00690 {
00691
PCLASSINFO(
PTEACypher,
PCypher)
00692
00693
public:
00694 struct Key {
00695 BYTE value[16];
00696 };
00697
00702
PTEACypher(
00703 BlockChainMode chainMode = ElectronicCodebook
00704 );
00705
PTEACypher(
00706
const Key & keyData,
00707 BlockChainMode chainMode = ElectronicCodebook
00708 );
00709
00710
00712
void SetKey(
00713
const Key & newKey
00714 );
00715
00717
void GetKey(
00718
Key & newKey
00719 )
const;
00720
00721
00723
static void GenerateKey(
00724
Key & newKey
00725 );
00726
00727
00728
protected:
00730
virtual void Initialise(
00731 BOOL encoding
00732 );
00733
00735
virtual void EncodeBlock(
00736
const void * in,
00737
void * out
00738 );
00739
00741
virtual void DecodeBlock(
00742
const void * in,
00743
void * out
00744 );
00745
00746
private:
00747 DWORD k0, k1, k2, k3;
00748 };
00749
00750
00751
00752 class PSecureConfig :
public PConfig
00753 {
00754
PCLASSINFO(
PSecureConfig,
PConfig)
00755
00756
00757
00758
00759
00760
public:
00761
PSecureConfig(
00762
const PTEACypher::Key & productKey,
00763
const PStringArray & securedKeys,
00764 Source src = Application
00765 );
00766
PSecureConfig(
00767
const PTEACypher::Key & productKey,
00768
const char *
const * securedKeyArray,
00769 PINDEX count,
00770 Source src = Application
00771 );
00772
00773
00774
00775
00776
00777
00778
00779
00780
00781
00782 const PStringArray & GetSecuredKeys()
const {
return securedKeys; }
00783
00784
00785
00786
00787
00788
00789 const PString & GetSecurityKey()
const {
return securityKey; }
00790
00791
00792
00793
00794
00795
00796 const PString & GetExpiryDateKey()
const {
return expiryDateKey; }
00797
00798
00799
00800
00801
00802
00803 const PString & GetOptionBitsKey()
const {
return optionBitsKey; }
00804
00805
00806
00807
00808
00809
00810 const PString & GetPendingPrefix()
const {
return pendingPrefix; }
00811
00812
00813
00814
00815
00816
00817
void GetProductKey(
00818
PTEACypher::Key & productKey
00819 )
const;
00820
00821
00822
00823
00824
00825
00826
00827 enum ValidationState {
00828 Defaults,
00829 Pending,
00830 IsValid,
00831 Expired,
00832 Invalid
00833 };
00834 ValidationState GetValidation() const;
00835
00836
00837
00838
00839
00840
00841
00842 BOOL ValidatePending();
00843
00844
00845
00846
00847
00848
00849
00850
00851
00852
void ResetPending();
00853
00854
00855
00856
00857
00858
00859 protected:
00860 PTEACypher::Key productKey;
00861 PStringArray securedKeys;
00862 PString securityKey;
00863 PString expiryDateKey;
00864 PString optionBitsKey;
00865 PString pendingPrefix;
00866 };
00867
00868
00869 #endif
00870
00871
00872