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 #ifndef PTLIB_CYPHER_H
00033 #define PTLIB_CYPHER_H
00034
00035 #ifdef P_USE_PRAGMA
00036 #pragma interface
00037 #endif
00038
00039
00070 class PBase64 : public PObject
00071 {
00072 PCLASSINFO(PBase64, PObject);
00073
00074 public:
00078 PBase64();
00079
00080 void StartEncoding(
00081 PBoolean useCRLFs = PTrue
00082 );
00083
00084
00085 void ProcessEncoding(
00086 const PString & str
00087 );
00088 void ProcessEncoding(
00089 const char * cstr
00090 );
00091 void ProcessEncoding(
00092 const PBYTEArray & data
00093 );
00094 void ProcessEncoding(
00095 const void * dataBlock,
00096 PINDEX length
00097 );
00098
00099
00105 PString GetEncodedString();
00106
00114 PString CompleteEncoding();
00115
00116
00117 static PString Encode(
00118 const PString & str
00119 );
00120 static PString Encode(
00121 const char * cstr
00122 );
00123 static PString Encode(
00124 const PBYTEArray & data
00125 );
00126 static PString Encode(
00127 const void * dataBlock,
00128 PINDEX length
00129 );
00130
00131
00132
00133 void StartDecoding();
00134
00135
00141 PBoolean ProcessDecoding(
00142 const PString & str
00143 );
00144 PBoolean ProcessDecoding(
00145 const char * cstr
00146 );
00147
00153 PBoolean GetDecodedData(
00154 void * dataBlock,
00155 PINDEX length
00156 );
00157 PBYTEArray GetDecodedData();
00158
00166 PBoolean IsDecodeOK() { return perfectDecode; }
00167
00168
00180 static PString Decode(
00181 const PString & str
00182 );
00183 static PBoolean Decode(
00184 const PString & str,
00185 PBYTEArray & data
00186 );
00187 static PBoolean Decode(
00188 const PString & str,
00189 void * dataBlock,
00190 PINDEX length
00191 );
00192
00193
00194
00195 private:
00196 void OutputBase64(const BYTE * data);
00197
00198 PString encodedString;
00199 PINDEX encodeLength;
00200 BYTE saveTriple[3];
00201 PINDEX saveCount;
00202 PINDEX nextLine;
00203 PBoolean useCRLFs;
00204
00205 PBoolean perfectDecode;
00206 PINDEX quadPosition;
00207 PBYTEArray decodedData;
00208 PINDEX decodeSize;
00209 };
00210
00211 class PMessageDigest : public PObject
00212 {
00213 PCLASSINFO(PMessageDigest, PObject)
00214
00215 public:
00217 PMessageDigest();
00218
00219 class Result {
00220 public:
00221 PINDEX GetSize() const { return value.GetSize(); }
00222 const BYTE * GetPointer() const { return (const BYTE *)value; }
00223
00224 private:
00225 PBYTEArray value;
00226 friend class PMessageDigest5;
00227 friend class PMessageDigestSHA1;
00228 };
00229
00231 virtual void Start() = 0;
00232
00233 virtual void Process(
00234 const void * dataBlock,
00235 PINDEX length
00236 );
00237
00239 virtual void Process(
00240 const PString & str
00241 );
00243 virtual void Process(
00244 const char * cstr
00245 );
00247 virtual void Process(
00248 const PBYTEArray & data
00249 );
00250
00258 virtual PString CompleteDigest();
00259 virtual void CompleteDigest(
00260 Result & result
00261 );
00262
00263 protected:
00264 virtual void InternalProcess(
00265 const void * dataBlock,
00266 PINDEX length
00267 ) = 0;
00268
00269 virtual void InternalCompleteDigest(
00270 Result & result
00271 ) = 0;
00272 };
00273
00274
00280 class PMessageDigest5 : public PMessageDigest
00281 {
00282 PCLASSINFO(PMessageDigest5, PMessageDigest)
00283
00284 public:
00286 PMessageDigest5();
00287
00289 void Start();
00290
00292 static PString Encode(
00293 const PString & str
00294 );
00296 static void Encode(
00297 const PString & str,
00298 Result & result
00299 );
00301 static PString Encode(
00302 const char * cstr
00303 );
00305 static void Encode(
00306 const char * cstr,
00307 Result & result
00308 );
00310 static PString Encode(
00311 const PBYTEArray & data
00312 );
00314 static void Encode(
00315 const PBYTEArray & data,
00316 Result & result
00317 );
00319 static PString Encode(
00320 const void * dataBlock,
00321 PINDEX length
00322 );
00328 static void Encode(
00329 const void * dataBlock,
00330 PINDEX length,
00331 Result & result
00332 );
00333
00334
00335 class Code {
00336 private:
00337 PUInt32l value[4];
00338 friend class PMessageDigest5;
00339 };
00340
00342 static void Encode(
00343 const PString & str,
00344 Code & result
00345 );
00347 static void Encode(
00348 const char * cstr,
00349 Code & result
00350 );
00352 static void Encode(
00353 const PBYTEArray & data,
00354 Code & result
00355 );
00361 static void Encode(
00362 const void * dataBlock,
00363 PINDEX length,
00364 Code & result
00365 );
00366 virtual void Complete(
00367 Code & result
00368 );
00369 virtual PString Complete();
00370
00371 protected:
00372 virtual void InternalProcess(
00373 const void * dataBlock,
00374 PINDEX length
00375 );
00376
00377 virtual void InternalCompleteDigest(
00378 Result & result
00379 );
00380
00381 private:
00382 void Transform(const BYTE * block);
00383
00385 BYTE buffer[64];
00387 DWORD state[4];
00389 PUInt64 count;
00390 };
00391
00392 #if P_SSL
00393
00398 class PMessageDigestSHA1 : public PMessageDigest
00399 {
00400 PCLASSINFO(PMessageDigestSHA1, PMessageDigest)
00401
00402 public:
00404 PMessageDigestSHA1();
00405 ~PMessageDigestSHA1();
00406
00408 void Start();
00409
00411 static PString Encode(
00412 const PString & str
00413 );
00415 static void Encode(
00416 const PString & str,
00417 Result & result
00418 );
00420 static PString Encode(
00421 const char * cstr
00422 );
00424 static void Encode(
00425 const char * cstr,
00426 Result & result
00427 );
00429 static PString Encode(
00430 const PBYTEArray & data
00431 );
00433 static void Encode(
00434 const PBYTEArray & data,
00435 Result & result
00436 );
00438 static PString Encode(
00439 const void * dataBlock,
00440 PINDEX length
00441 );
00447 static void Encode(
00448 const void * dataBlock,
00449 PINDEX length,
00450 Result & result
00451 );
00452
00453 protected:
00454 virtual void InternalProcess(
00455 const void * dataBlock,
00456 PINDEX length
00457 );
00458
00459 void InternalCompleteDigest(
00460 Result & result
00461 );
00462
00463 private:
00464 void * shaContext;
00465 };
00466
00467 #endif
00468
00472 class PCypher : public PObject
00473 {
00474 PCLASSINFO(PCypher, PObject)
00475
00476 public:
00478 enum BlockChainMode {
00479 ElectronicCodebook,
00480 ECB = ElectronicCodebook,
00481 CypherBlockChaining,
00482 CBC = CypherBlockChaining,
00483 OutputFeedback,
00484 OFB = OutputFeedback,
00485 CypherFeedback,
00486 CFB = CypherFeedback,
00487 NumBlockChainModes
00488 };
00489
00490
00492 PString Encode(
00493 const PString & str
00494 );
00496 PString Encode(
00497 const PBYTEArray & clear
00498 );
00500 PString Encode(
00501 const void * data,
00502 PINDEX length
00503 );
00505 void Encode(
00506 const PBYTEArray & clear,
00507 PBYTEArray & coded
00508 );
00524 void Encode(
00525 const void * data,
00526 PINDEX length,
00527 PBYTEArray & coded
00528 );
00529
00531 PString Decode(
00532 const PString & cypher
00533 );
00535 PBoolean Decode(
00536 const PString & cypher,
00537 PString & clear
00538 );
00540 PBoolean Decode(
00541 const PString & cypher,
00542 PBYTEArray & clear
00543 );
00545 PINDEX Decode(
00546 const PString & cypher,
00547 void * data,
00548 PINDEX length
00549 );
00551 PINDEX Decode(
00552 const PBYTEArray & coded,
00553 void * data,
00554 PINDEX length
00555 );
00571 PBoolean Decode(
00572 const PBYTEArray & coded,
00573 PBYTEArray & clear
00574 );
00575
00576
00577 protected:
00581 PCypher(
00582 PINDEX blockSize,
00583 BlockChainMode chainMode
00584 );
00585 PCypher(
00586 const void * keyData,
00587 PINDEX keyLength,
00588 PINDEX blockSize,
00589 BlockChainMode chainMode
00590 );
00591
00592
00594 virtual void Initialise(
00595 PBoolean encoding
00596 ) = 0;
00597
00599 virtual void EncodeBlock(
00600 const void * in,
00601 void * out
00602 ) = 0;
00603
00604
00606 virtual void DecodeBlock(
00607 const void * in,
00608 void * out
00609 ) = 0;
00610
00611
00613 PBYTEArray key;
00615 PINDEX blockSize;
00617 BlockChainMode chainMode;
00618 };
00619
00620
00628 class PTEACypher : public PCypher
00629 {
00630 PCLASSINFO(PTEACypher, PCypher)
00631
00632 public:
00633 struct Key {
00634 BYTE value[16];
00635 };
00636
00641 PTEACypher(
00642 BlockChainMode chainMode = ElectronicCodebook
00643 );
00644 PTEACypher(
00645 const Key & keyData,
00646 BlockChainMode chainMode = ElectronicCodebook
00647 );
00648
00649
00651 void SetKey(
00652 const Key & newKey
00653 );
00654
00656 void GetKey(
00657 Key & newKey
00658 ) const;
00659
00660
00662 static void GenerateKey(
00663 Key & newKey
00664 );
00665
00666
00667 protected:
00669 virtual void Initialise(
00670 PBoolean encoding
00671 );
00672
00674 virtual void EncodeBlock(
00675 const void * in,
00676 void * out
00677 );
00678
00680 virtual void DecodeBlock(
00681 const void * in,
00682 void * out
00683 );
00684
00685 private:
00686 DWORD k0, k1, k2, k3;
00687 };
00688
00689
00690 #ifdef P_CONFIG_FILE
00691
00692 class PSecureConfig : public PConfig
00693 {
00694 PCLASSINFO(PSecureConfig, PConfig)
00695
00696
00697
00698
00699
00700 public:
00701 PSecureConfig(
00702 const PTEACypher::Key & productKey,
00703 const PStringArray & securedKeys,
00704 Source src = Application
00705 );
00706 PSecureConfig(
00707 const PTEACypher::Key & productKey,
00708 const char * const * securedKeyArray,
00709 PINDEX count,
00710 Source src = Application
00711 );
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722 const PStringArray & GetSecuredKeys() const { return securedKeys; }
00723
00724
00725
00726
00727
00728
00729 const PString & GetSecurityKey() const { return securityKey; }
00730
00731
00732
00733
00734
00735
00736 const PString & GetExpiryDateKey() const { return expiryDateKey; }
00737
00738
00739
00740
00741
00742
00743 const PString & GetOptionBitsKey() const { return optionBitsKey; }
00744
00745
00746
00747
00748
00749
00750 const PString & GetPendingPrefix() const { return pendingPrefix; }
00751
00752
00753
00754
00755
00756
00757 void GetProductKey(
00758 PTEACypher::Key & productKey
00759 ) const;
00760
00761
00762
00763
00764
00765
00766
00767 enum ValidationState {
00768 Defaults,
00769 Pending,
00770 IsValid,
00771 Expired,
00772 Invalid
00773 };
00774 ValidationState GetValidation() const;
00775
00776
00777
00778
00779
00780
00781
00782 PBoolean ValidatePending();
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792 void ResetPending();
00793
00794
00795
00796
00797
00798
00799 protected:
00800 PTEACypher::Key productKey;
00801 PStringArray securedKeys;
00802 PString securityKey;
00803 PString expiryDateKey;
00804 PString optionBitsKey;
00805 PString pendingPrefix;
00806 };
00807
00808 #endif // P_CONFIG_FILE
00809
00810 #endif // PTLIB_CYPHER_H
00811
00812
00813