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
00095
00096
00097
00098
00099
00100
00101
00102
00103 #ifndef _PCYPHER
00104 #define _PCYPHER
00105
00106 #ifdef P_USE_PRAGMA
00107 #pragma interface
00108 #endif
00109
00110 #include <ptlib.h>
00111
00142 class PBase64 : public PObject
00143 {
00144 PCLASSINFO(PBase64, PObject);
00145
00146 public:
00150 PBase64();
00151
00152 void StartEncoding(
00153 BOOL useCRLFs = TRUE
00154 );
00155
00156
00157 void ProcessEncoding(
00158 const PString & str
00159 );
00160 void ProcessEncoding(
00161 const char * cstr
00162 );
00163 void ProcessEncoding(
00164 const PBYTEArray & data
00165 );
00166 void ProcessEncoding(
00167 const void * dataBlock,
00168 PINDEX length
00169 );
00170
00171
00177 PString GetEncodedString();
00178
00186 PString CompleteEncoding();
00187
00188
00189 static PString Encode(
00190 const PString & str
00191 );
00192 static PString Encode(
00193 const char * cstr
00194 );
00195 static PString Encode(
00196 const PBYTEArray & data
00197 );
00198 static PString Encode(
00199 const void * dataBlock,
00200 PINDEX length
00201 );
00202
00203
00204
00205 void StartDecoding();
00206
00207
00213 BOOL ProcessDecoding(
00214 const PString & str
00215 );
00216 BOOL ProcessDecoding(
00217 const char * cstr
00218 );
00219
00225 BOOL GetDecodedData(
00226 void * dataBlock,
00227 PINDEX length
00228 );
00229 PBYTEArray GetDecodedData();
00230
00238 BOOL IsDecodeOK() { return perfectDecode; }
00239
00240
00252 static PString Decode(
00253 const PString & str
00254 );
00255 static BOOL Decode(
00256 const PString & str,
00257 PBYTEArray & data
00258 );
00259 static BOOL Decode(
00260 const PString & str,
00261 void * dataBlock,
00262 PINDEX length
00263 );
00264
00265
00266
00267 private:
00268 void OutputBase64(const BYTE * data);
00269
00270 PString encodedString;
00271 PINDEX encodeLength;
00272 BYTE saveTriple[3];
00273 PINDEX saveCount;
00274 PINDEX nextLine;
00275 BOOL useCRLFs;
00276
00277 BOOL perfectDecode;
00278 PINDEX quadPosition;
00279 PBYTEArray decodedData;
00280 PINDEX decodeSize;
00281 };
00282
00283 class PMessageDigest : public PObject
00284 {
00285 PCLASSINFO(PMessageDigest, PObject)
00286
00287 public:
00289 PMessageDigest();
00290
00291 class Result {
00292 public:
00293 PINDEX GetSize() const { return value.GetSize(); }
00294 const BYTE * GetPointer() const { return (const BYTE *)value; }
00295
00296 private:
00297 PBYTEArray value;
00298 friend class PMessageDigest5;
00299 friend class PMessageDigestSHA1;
00300 };
00301
00303 virtual void Start() = 0;
00304
00305 virtual void Process(
00306 const void * dataBlock,
00307 PINDEX length
00308 );
00309
00311 virtual void Process(
00312 const PString & str
00313 );
00315 virtual void Process(
00316 const char * cstr
00317 );
00319 virtual void Process(
00320 const PBYTEArray & data
00321 );
00322
00330 virtual PString CompleteDigest();
00331 virtual void CompleteDigest(
00332 Result & result
00333 );
00334
00335 protected:
00336 virtual void InternalProcess(
00337 const void * dataBlock,
00338 PINDEX length
00339 ) = 0;
00340
00341 virtual void InternalCompleteDigest(
00342 Result & result
00343 ) = 0;
00344 };
00345
00346
00352 class PMessageDigest5 : public PMessageDigest
00353 {
00354 PCLASSINFO(PMessageDigest5, PMessageDigest)
00355
00356 public:
00358 PMessageDigest5();
00359
00361 void Start();
00362
00364 static PString Encode(
00365 const PString & str
00366 );
00368 static void Encode(
00369 const PString & str,
00370 Result & result
00371 );
00373 static PString Encode(
00374 const char * cstr
00375 );
00377 static void Encode(
00378 const char * cstr,
00379 Result & result
00380 );
00382 static PString Encode(
00383 const PBYTEArray & data
00384 );
00386 static void Encode(
00387 const PBYTEArray & data,
00388 Result & result
00389 );
00391 static PString Encode(
00392 const void * dataBlock,
00393 PINDEX length
00394 );
00400 static void Encode(
00401 const void * dataBlock,
00402 PINDEX length,
00403 Result & result
00404 );
00405
00406
00407 class Code {
00408 private:
00409 PUInt32l value[4];
00410 friend class PMessageDigest5;
00411 };
00412
00414 static void Encode(
00415 const PString & str,
00416 Code & result
00417 );
00419 static void Encode(
00420 const char * cstr,
00421 Code & result
00422 );
00424 static void Encode(
00425 const PBYTEArray & data,
00426 Code & result
00427 );
00433 static void Encode(
00434 const void * dataBlock,
00435 PINDEX length,
00436 Code & result
00437 );
00438 virtual void Complete(
00439 Code & result
00440 );
00441 virtual PString Complete();
00442
00443 protected:
00444 virtual void InternalProcess(
00445 const void * dataBlock,
00446 PINDEX length
00447 );
00448
00449 virtual void InternalCompleteDigest(
00450 Result & result
00451 );
00452
00453 private:
00454 void Transform(const BYTE * block);
00455
00457 BYTE buffer[64];
00459 DWORD state[4];
00461 PUInt64 count;
00462 };
00463
00464 #if P_SSL
00465
00470 class PMessageDigestSHA1 : public PMessageDigest
00471 {
00472 PCLASSINFO(PMessageDigestSHA1, PMessageDigest)
00473
00474 public:
00476 PMessageDigestSHA1();
00477 ~PMessageDigestSHA1();
00478
00480 void Start();
00481
00483 static PString Encode(
00484 const PString & str
00485 );
00487 static void Encode(
00488 const PString & str,
00489 Result & result
00490 );
00492 static PString Encode(
00493 const char * cstr
00494 );
00496 static void Encode(
00497 const char * cstr,
00498 Result & result
00499 );
00501 static PString Encode(
00502 const PBYTEArray & data
00503 );
00505 static void Encode(
00506 const PBYTEArray & data,
00507 Result & result
00508 );
00510 static PString Encode(
00511 const void * dataBlock,
00512 PINDEX length
00513 );
00519 static void Encode(
00520 const void * dataBlock,
00521 PINDEX length,
00522 Result & result
00523 );
00524
00525 protected:
00526 virtual void InternalProcess(
00527 const void * dataBlock,
00528 PINDEX length
00529 );
00530
00531 void InternalCompleteDigest(
00532 Result & result
00533 );
00534
00535 private:
00536 void * shaContext;
00537 };
00538
00539 #endif
00540
00544 class PCypher : public PObject
00545 {
00546 PCLASSINFO(PCypher, PObject)
00547
00548 public:
00550 enum BlockChainMode {
00551 ElectronicCodebook,
00552 ECB = ElectronicCodebook,
00553 CypherBlockChaining,
00554 CBC = CypherBlockChaining,
00555 OutputFeedback,
00556 OFB = OutputFeedback,
00557 CypherFeedback,
00558 CFB = CypherFeedback,
00559 NumBlockChainModes
00560 };
00561
00562
00564 PString Encode(
00565 const PString & str
00566 );
00568 PString Encode(
00569 const PBYTEArray & clear
00570 );
00572 PString Encode(
00573 const void * data,
00574 PINDEX length
00575 );
00577 void Encode(
00578 const PBYTEArray & clear,
00579 PBYTEArray & coded
00580 );
00596 void Encode(
00597 const void * data,
00598 PINDEX length,
00599 PBYTEArray & coded
00600 );
00601
00603 PString Decode(
00604 const PString & cypher
00605 );
00607 BOOL Decode(
00608 const PString & cypher,
00609 PString & clear
00610 );
00612 BOOL Decode(
00613 const PString & cypher,
00614 PBYTEArray & clear
00615 );
00617 PINDEX Decode(
00618 const PString & cypher,
00619 void * data,
00620 PINDEX length
00621 );
00623 PINDEX Decode(
00624 const PBYTEArray & coded,
00625 void * data,
00626 PINDEX length
00627 );
00643 BOOL Decode(
00644 const PBYTEArray & coded,
00645 PBYTEArray & clear
00646 );
00647
00648
00649 protected:
00653 PCypher(
00654 PINDEX blockSize,
00655 BlockChainMode chainMode
00656 );
00657 PCypher(
00658 const void * keyData,
00659 PINDEX keyLength,
00660 PINDEX blockSize,
00661 BlockChainMode chainMode
00662 );
00663
00664
00666 virtual void Initialise(
00667 BOOL encoding
00668 ) = 0;
00669
00671 virtual void EncodeBlock(
00672 const void * in,
00673 void * out
00674 ) = 0;
00675
00676
00678 virtual void DecodeBlock(
00679 const void * in,
00680 void * out
00681 ) = 0;
00682
00683
00685 PBYTEArray key;
00687 PINDEX blockSize;
00689 BlockChainMode chainMode;
00690 };
00691
00692
00700 class PTEACypher : public PCypher
00701 {
00702 PCLASSINFO(PTEACypher, PCypher)
00703
00704 public:
00705 struct Key {
00706 BYTE value[16];
00707 };
00708
00713 PTEACypher(
00714 BlockChainMode chainMode = ElectronicCodebook
00715 );
00716 PTEACypher(
00717 const Key & keyData,
00718 BlockChainMode chainMode = ElectronicCodebook
00719 );
00720
00721
00723 void SetKey(
00724 const Key & newKey
00725 );
00726
00728 void GetKey(
00729 Key & newKey
00730 ) const;
00731
00732
00734 static void GenerateKey(
00735 Key & newKey
00736 );
00737
00738
00739 protected:
00741 virtual void Initialise(
00742 BOOL encoding
00743 );
00744
00746 virtual void EncodeBlock(
00747 const void * in,
00748 void * out
00749 );
00750
00752 virtual void DecodeBlock(
00753 const void * in,
00754 void * out
00755 );
00756
00757 private:
00758 DWORD k0, k1, k2, k3;
00759 };
00760
00761
00762 #ifdef P_CONFIG_FILE
00763
00764 class PSecureConfig : public PConfig
00765 {
00766 PCLASSINFO(PSecureConfig, PConfig)
00767
00768
00769
00770
00771
00772 public:
00773 PSecureConfig(
00774 const PTEACypher::Key & productKey,
00775 const PStringArray & securedKeys,
00776 Source src = Application
00777 );
00778 PSecureConfig(
00779 const PTEACypher::Key & productKey,
00780 const char * const * securedKeyArray,
00781 PINDEX count,
00782 Source src = Application
00783 );
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793
00794 const PStringArray & GetSecuredKeys() const { return securedKeys; }
00795
00796
00797
00798
00799
00800
00801 const PString & GetSecurityKey() const { return securityKey; }
00802
00803
00804
00805
00806
00807
00808 const PString & GetExpiryDateKey() const { return expiryDateKey; }
00809
00810
00811
00812
00813
00814
00815 const PString & GetOptionBitsKey() const { return optionBitsKey; }
00816
00817
00818
00819
00820
00821
00822 const PString & GetPendingPrefix() const { return pendingPrefix; }
00823
00824
00825
00826
00827
00828
00829 void GetProductKey(
00830 PTEACypher::Key & productKey
00831 ) const;
00832
00833
00834
00835
00836
00837
00838
00839 enum ValidationState {
00840 Defaults,
00841 Pending,
00842 IsValid,
00843 Expired,
00844 Invalid
00845 };
00846 ValidationState GetValidation() const;
00847
00848
00849
00850
00851
00852
00853
00854 BOOL ValidatePending();
00855
00856
00857
00858
00859
00860
00861
00862
00863
00864 void ResetPending();
00865
00866
00867
00868
00869
00870
00871 protected:
00872 PTEACypher::Key productKey;
00873 PStringArray securedKeys;
00874 PString securityKey;
00875 PString expiryDateKey;
00876 PString optionBitsKey;
00877 PString pendingPrefix;
00878 };
00879
00880 #endif // P_CONFIG_FILE
00881
00882 #endif // _PCYPHER
00883
00884
00885