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 #ifndef _PCYPHER
00098 #define _PCYPHER
00099
00100 #ifdef P_USE_PRAGMA
00101 #pragma interface
00102 #endif
00103
00104 #include <ptlib.h>
00105
00136 class PBase64 : public PObject
00137 {
00138 PCLASSINFO(PBase64, PObject);
00139
00140 public:
00144 PBase64();
00145
00146 void StartEncoding(
00147 BOOL useCRLFs = TRUE
00148 );
00149
00150
00151 void ProcessEncoding(
00152 const PString & str
00153 );
00154 void ProcessEncoding(
00155 const char * cstr
00156 );
00157 void ProcessEncoding(
00158 const PBYTEArray & data
00159 );
00160 void ProcessEncoding(
00161 const void * dataBlock,
00162 PINDEX length
00163 );
00164
00165
00171 PString GetEncodedString();
00172
00180 PString CompleteEncoding();
00181
00182
00183 static PString Encode(
00184 const PString & str
00185 );
00186 static PString Encode(
00187 const char * cstr
00188 );
00189 static PString Encode(
00190 const PBYTEArray & data
00191 );
00192 static PString Encode(
00193 const void * dataBlock,
00194 PINDEX length
00195 );
00196
00197
00198
00199 void StartDecoding();
00200
00201
00207 BOOL ProcessDecoding(
00208 const PString & str
00209 );
00210 BOOL ProcessDecoding(
00211 const char * cstr
00212 );
00213
00219 BOOL GetDecodedData(
00220 void * dataBlock,
00221 PINDEX length
00222 );
00223 PBYTEArray GetDecodedData();
00224
00232 BOOL IsDecodeOK() { return perfectDecode; }
00233
00234
00246 static PString Decode(
00247 const PString & str
00248 );
00249 static BOOL Decode(
00250 const PString & str,
00251 PBYTEArray & data
00252 );
00253 static BOOL Decode(
00254 const PString & str,
00255 void * dataBlock,
00256 PINDEX length
00257 );
00258
00259
00260
00261 private:
00262 void OutputBase64(const BYTE * data);
00263
00264 PString encodedString;
00265 PINDEX encodeLength;
00266 BYTE saveTriple[3];
00267 PINDEX saveCount;
00268 PINDEX nextLine;
00269 BOOL useCRLFs;
00270
00271 BOOL perfectDecode;
00272 PINDEX quadPosition;
00273 PBYTEArray decodedData;
00274 PINDEX decodeSize;
00275 };
00276
00277 class PMessageDigest : public PObject
00278 {
00279 PCLASSINFO(PMessageDigest, PObject)
00280
00281 public:
00283 PMessageDigest();
00284
00285 class Result {
00286 public:
00287 PINDEX GetSize() const { return value.GetSize(); }
00288 const BYTE * GetPointer() const { return (const BYTE *)value; }
00289
00290 private:
00291 PBYTEArray value;
00292 friend class PMessageDigest5;
00293 friend class PMessageDigestSHA1;
00294 };
00295
00297 virtual void Start() = 0;
00298
00299 virtual void Process(
00300 const void * dataBlock,
00301 PINDEX length
00302 );
00303
00305 virtual void Process(
00306 const PString & str
00307 );
00309 virtual void Process(
00310 const char * cstr
00311 );
00313 virtual void Process(
00314 const PBYTEArray & data
00315 );
00316
00324 virtual PString CompleteDigest();
00325 virtual void CompleteDigest(
00326 Result & result
00327 );
00328
00329 protected:
00330 virtual void InternalProcess(
00331 const void * dataBlock,
00332 PINDEX length
00333 ) = 0;
00334
00335 virtual void InternalCompleteDigest(
00336 Result & result
00337 ) = 0;
00338 };
00339
00340
00346 class PMessageDigest5 : public PMessageDigest
00347 {
00348 PCLASSINFO(PMessageDigest5, PMessageDigest)
00349
00350 public:
00352 PMessageDigest5();
00353
00355 void Start();
00356
00358 static PString Encode(
00359 const PString & str
00360 );
00362 static void Encode(
00363 const PString & str,
00364 Result & result
00365 );
00367 static PString Encode(
00368 const char * cstr
00369 );
00371 static void Encode(
00372 const char * cstr,
00373 Result & result
00374 );
00376 static PString Encode(
00377 const PBYTEArray & data
00378 );
00380 static void Encode(
00381 const PBYTEArray & data,
00382 Result & result
00383 );
00385 static PString Encode(
00386 const void * dataBlock,
00387 PINDEX length
00388 );
00394 static void Encode(
00395 const void * dataBlock,
00396 PINDEX length,
00397 Result & result
00398 );
00399
00400
00401 class Code {
00402 private:
00403 PUInt32l value[4];
00404 friend class PMessageDigest5;
00405 };
00406
00408 static void Encode(
00409 const PString & str,
00410 Code & result
00411 );
00413 static void Encode(
00414 const char * cstr,
00415 Code & result
00416 );
00418 static void Encode(
00419 const PBYTEArray & data,
00420 Code & result
00421 );
00427 static void Encode(
00428 const void * dataBlock,
00429 PINDEX length,
00430 Code & result
00431 );
00432 virtual void Complete(
00433 Code & result
00434 );
00435 virtual PString Complete();
00436
00437 protected:
00438 virtual void InternalProcess(
00439 const void * dataBlock,
00440 PINDEX length
00441 );
00442
00443 virtual void InternalCompleteDigest(
00444 Result & result
00445 );
00446
00447 private:
00448 void Transform(const BYTE * block);
00449
00451 BYTE buffer[64];
00453 DWORD state[4];
00455 PUInt64 count;
00456 };
00457
00458 #if P_SSL
00459
00464 class PMessageDigestSHA1 : public PMessageDigest
00465 {
00466 PCLASSINFO(PMessageDigestSHA1, PMessageDigest)
00467
00468 public:
00470 PMessageDigestSHA1();
00471 ~PMessageDigestSHA1();
00472
00474 void Start();
00475
00477 static PString Encode(
00478 const PString & str
00479 );
00481 static void Encode(
00482 const PString & str,
00483 Result & result
00484 );
00486 static PString Encode(
00487 const char * cstr
00488 );
00490 static void Encode(
00491 const char * cstr,
00492 Result & result
00493 );
00495 static PString Encode(
00496 const PBYTEArray & data
00497 );
00499 static void Encode(
00500 const PBYTEArray & data,
00501 Result & result
00502 );
00504 static PString Encode(
00505 const void * dataBlock,
00506 PINDEX length
00507 );
00513 static void Encode(
00514 const void * dataBlock,
00515 PINDEX length,
00516 Result & result
00517 );
00518
00519 protected:
00520 virtual void InternalProcess(
00521 const void * dataBlock,
00522 PINDEX length
00523 );
00524
00525 void InternalCompleteDigest(
00526 Result & result
00527 );
00528
00529 private:
00530 void * shaContext;
00531 };
00532
00533 #endif
00534
00538 class PCypher : public PObject
00539 {
00540 PCLASSINFO(PCypher, PObject)
00541
00542 public:
00544 enum BlockChainMode {
00545 ElectronicCodebook,
00546 ECB = ElectronicCodebook,
00547 CypherBlockChaining,
00548 CBC = CypherBlockChaining,
00549 OutputFeedback,
00550 OFB = OutputFeedback,
00551 CypherFeedback,
00552 CFB = CypherFeedback,
00553 NumBlockChainModes
00554 };
00555
00556
00558 PString Encode(
00559 const PString & str
00560 );
00562 PString Encode(
00563 const PBYTEArray & clear
00564 );
00566 PString Encode(
00567 const void * data,
00568 PINDEX length
00569 );
00571 void Encode(
00572 const PBYTEArray & clear,
00573 PBYTEArray & coded
00574 );
00590 void Encode(
00591 const void * data,
00592 PINDEX length,
00593 PBYTEArray & coded
00594 );
00595
00597 PString Decode(
00598 const PString & cypher
00599 );
00601 BOOL Decode(
00602 const PString & cypher,
00603 PString & clear
00604 );
00606 BOOL Decode(
00607 const PString & cypher,
00608 PBYTEArray & clear
00609 );
00611 PINDEX Decode(
00612 const PString & cypher,
00613 void * data,
00614 PINDEX length
00615 );
00617 PINDEX Decode(
00618 const PBYTEArray & coded,
00619 void * data,
00620 PINDEX length
00621 );
00637 BOOL Decode(
00638 const PBYTEArray & coded,
00639 PBYTEArray & clear
00640 );
00641
00642
00643 protected:
00647 PCypher(
00648 PINDEX blockSize,
00649 BlockChainMode chainMode
00650 );
00651 PCypher(
00652 const void * keyData,
00653 PINDEX keyLength,
00654 PINDEX blockSize,
00655 BlockChainMode chainMode
00656 );
00657
00658
00660 virtual void Initialise(
00661 BOOL encoding
00662 ) = 0;
00663
00665 virtual void EncodeBlock(
00666 const void * in,
00667 void * out
00668 ) = 0;
00669
00670
00672 virtual void DecodeBlock(
00673 const void * in,
00674 void * out
00675 ) = 0;
00676
00677
00679 PBYTEArray key;
00681 PINDEX blockSize;
00683 BlockChainMode chainMode;
00684 };
00685
00686
00694 class PTEACypher : public PCypher
00695 {
00696 PCLASSINFO(PTEACypher, PCypher)
00697
00698 public:
00699 struct Key {
00700 BYTE value[16];
00701 };
00702
00707 PTEACypher(
00708 BlockChainMode chainMode = ElectronicCodebook
00709 );
00710 PTEACypher(
00711 const Key & keyData,
00712 BlockChainMode chainMode = ElectronicCodebook
00713 );
00714
00715
00717 void SetKey(
00718 const Key & newKey
00719 );
00720
00722 void GetKey(
00723 Key & newKey
00724 ) const;
00725
00726
00728 static void GenerateKey(
00729 Key & newKey
00730 );
00731
00732
00733 protected:
00735 virtual void Initialise(
00736 BOOL encoding
00737 );
00738
00740 virtual void EncodeBlock(
00741 const void * in,
00742 void * out
00743 );
00744
00746 virtual void DecodeBlock(
00747 const void * in,
00748 void * out
00749 );
00750
00751 private:
00752 DWORD k0, k1, k2, k3;
00753 };
00754
00755
00756
00757 class PSecureConfig : public PConfig
00758 {
00759 PCLASSINFO(PSecureConfig, PConfig)
00760
00761
00762
00763
00764
00765 public:
00766 PSecureConfig(
00767 const PTEACypher::Key & productKey,
00768 const PStringArray & securedKeys,
00769 Source src = Application
00770 );
00771 PSecureConfig(
00772 const PTEACypher::Key & productKey,
00773 const char * const * securedKeyArray,
00774 PINDEX count,
00775 Source src = Application
00776 );
00777
00778
00779
00780
00781
00782
00783
00784
00785
00786
00787 const PStringArray & GetSecuredKeys() const { return securedKeys; }
00788
00789
00790
00791
00792
00793
00794 const PString & GetSecurityKey() const { return securityKey; }
00795
00796
00797
00798
00799
00800
00801 const PString & GetExpiryDateKey() const { return expiryDateKey; }
00802
00803
00804
00805
00806
00807
00808 const PString & GetOptionBitsKey() const { return optionBitsKey; }
00809
00810
00811
00812
00813
00814
00815 const PString & GetPendingPrefix() const { return pendingPrefix; }
00816
00817
00818
00819
00820
00821
00822 void GetProductKey(
00823 PTEACypher::Key & productKey
00824 ) const;
00825
00826
00827
00828
00829
00830
00831
00832 enum ValidationState {
00833 Defaults,
00834 Pending,
00835 IsValid,
00836 Expired,
00837 Invalid
00838 };
00839 ValidationState GetValidation() const;
00840
00841
00842
00843
00844
00845
00846
00847 BOOL ValidatePending();
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857 void ResetPending();
00858
00859
00860
00861
00862
00863
00864 protected:
00865 PTEACypher::Key productKey;
00866 PStringArray securedKeys;
00867 PString securityKey;
00868 PString expiryDateKey;
00869 PString optionBitsKey;
00870 PString pendingPrefix;
00871 };
00872
00873
00874 #endif // _PCYPHER
00875
00876
00877