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 = true
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
00113 PString CompleteEncoding();
00114
00115
00116 static PString Encode(
00117 const PString & str
00118 );
00119 static PString Encode(
00120 const char * cstr
00121 );
00122 static PString Encode(
00123 const PBYTEArray & data
00124 );
00125 static PString Encode(
00126 const void * dataBlock,
00127 PINDEX length
00128 );
00129
00130
00131
00132 void StartDecoding();
00133
00134
00140 PBoolean ProcessDecoding(
00141 const PString & str
00142 );
00143 PBoolean ProcessDecoding(
00144 const char * cstr
00145 );
00146
00152 PBoolean GetDecodedData(
00153 void * dataBlock,
00154 PINDEX length
00155 );
00156 PBYTEArray GetDecodedData();
00157
00165 PBoolean IsDecodeOK() { return perfectDecode; }
00166
00167
00179 static PString Decode(
00180 const PString & str
00181 );
00182 static PBoolean Decode(
00183 const PString & str,
00184 PBYTEArray & data
00185 );
00186 static PBoolean Decode(
00187 const PString & str,
00188 void * dataBlock,
00189 PINDEX length
00190 );
00191
00192
00193
00194 private:
00195 void OutputBase64(const BYTE * data);
00196
00197 PString encodedString;
00198 PINDEX encodeLength;
00199 BYTE saveTriple[3];
00200 PINDEX saveCount;
00201 PINDEX nextLine;
00202 PBoolean useCRLFs;
00203
00204 PBoolean perfectDecode;
00205 PINDEX quadPosition;
00206 PBYTEArray decodedData;
00207 PINDEX decodeSize;
00208 };
00209
00210 class PMessageDigest : public PObject
00211 {
00212 PCLASSINFO(PMessageDigest, PObject)
00213
00214 public:
00216 PMessageDigest();
00217
00218 class Result {
00219 public:
00220 PINDEX GetSize() const { return value.GetSize(); }
00221 const BYTE * GetPointer() const { return (const BYTE *)value; }
00222
00223 private:
00224 PBYTEArray value;
00225 friend class PMessageDigest5;
00226 friend class PMessageDigestSHA1;
00227 };
00228
00230 virtual void Start() = 0;
00231
00232 virtual void Process(
00233 const void * dataBlock,
00234 PINDEX length
00235 );
00236
00238 virtual void Process(
00239 const PString & str
00240 );
00242 virtual void Process(
00243 const char * cstr
00244 );
00246 virtual void Process(
00247 const PBYTEArray & data
00248 );
00249
00257 virtual PString CompleteDigest();
00258 virtual void CompleteDigest(
00259 Result & result
00260 );
00261
00262 protected:
00263 virtual void InternalProcess(
00264 const void * dataBlock,
00265 PINDEX length
00266 ) = 0;
00267
00268 virtual void InternalCompleteDigest(
00269 Result & result
00270 ) = 0;
00271 };
00272
00273
00279 class PMessageDigest5 : public PMessageDigest
00280 {
00281 PCLASSINFO(PMessageDigest5, PMessageDigest)
00282
00283 public:
00285 PMessageDigest5();
00286
00288 void Start();
00289
00291 static PString Encode(
00292 const PString & str
00293 );
00295 static void Encode(
00296 const PString & str,
00297 Result & result
00298 );
00300 static PString Encode(
00301 const char * cstr
00302 );
00304 static void Encode(
00305 const char * cstr,
00306 Result & result
00307 );
00309 static PString Encode(
00310 const PBYTEArray & data
00311 );
00313 static void Encode(
00314 const PBYTEArray & data,
00315 Result & result
00316 );
00318 static PString Encode(
00319 const void * dataBlock,
00320 PINDEX length
00321 );
00327 static void Encode(
00328 const void * dataBlock,
00329 PINDEX length,
00330 Result & result
00331 );
00332
00333
00334 class Code {
00335 private:
00336 PUInt32l value[4];
00337 friend class PMessageDigest5;
00338 };
00339
00341 static void Encode(
00342 const PString & str,
00343 Code & result
00344 );
00346 static void Encode(
00347 const char * cstr,
00348 Code & result
00349 );
00351 static void Encode(
00352 const PBYTEArray & data,
00353 Code & result
00354 );
00360 static void Encode(
00361 const void * dataBlock,
00362 PINDEX length,
00363 Code & result
00364 );
00365 virtual void Complete(
00366 Code & result
00367 );
00368 virtual PString Complete();
00369
00370 protected:
00371 virtual void InternalProcess(
00372 const void * dataBlock,
00373 PINDEX length
00374 );
00375
00376 virtual void InternalCompleteDigest(
00377 Result & result
00378 );
00379
00380 private:
00381 void Transform(const BYTE * block);
00382
00384 BYTE buffer[64];
00386 DWORD state[4];
00388 PUInt64 count;
00389 };
00390
00391 #if P_SSL
00392
00397 class PMessageDigestSHA1 : public PMessageDigest
00398 {
00399 PCLASSINFO(PMessageDigestSHA1, PMessageDigest)
00400
00401 public:
00403 PMessageDigestSHA1();
00404 ~PMessageDigestSHA1();
00405
00407 void Start();
00408
00410 static PString Encode(
00411 const PString & str
00412 );
00414 static void Encode(
00415 const PString & str,
00416 Result & result
00417 );
00419 static PString Encode(
00420 const char * cstr
00421 );
00423 static void Encode(
00424 const char * cstr,
00425 Result & result
00426 );
00428 static PString Encode(
00429 const PBYTEArray & data
00430 );
00432 static void Encode(
00433 const PBYTEArray & data,
00434 Result & result
00435 );
00437 static PString Encode(
00438 const void * dataBlock,
00439 PINDEX length
00440 );
00446 static void Encode(
00447 const void * dataBlock,
00448 PINDEX length,
00449 Result & result
00450 );
00451
00452 protected:
00453 virtual void InternalProcess(
00454 const void * dataBlock,
00455 PINDEX length
00456 );
00457
00458 void InternalCompleteDigest(
00459 Result & result
00460 );
00461
00462 private:
00463 void * shaContext;
00464 };
00465
00466 #endif
00467
00471 class PCypher : public PObject
00472 {
00473 PCLASSINFO(PCypher, PObject)
00474
00475 public:
00477 enum BlockChainMode {
00478 ElectronicCodebook,
00479 ECB = ElectronicCodebook,
00480 CypherBlockChaining,
00481 CBC = CypherBlockChaining,
00482 OutputFeedback,
00483 OFB = OutputFeedback,
00484 CypherFeedback,
00485 CFB = CypherFeedback,
00486 NumBlockChainModes
00487 };
00488
00489
00491 PString Encode(
00492 const PString & str
00493 );
00495 PString Encode(
00496 const PBYTEArray & clear
00497 );
00499 PString Encode(
00500 const void * data,
00501 PINDEX length
00502 );
00504 void Encode(
00505 const PBYTEArray & clear,
00506 PBYTEArray & coded
00507 );
00523 void Encode(
00524 const void * data,
00525 PINDEX length,
00526 PBYTEArray & coded
00527 );
00528
00530 PString Decode(
00531 const PString & cypher
00532 );
00534 PBoolean Decode(
00535 const PString & cypher,
00536 PString & clear
00537 );
00539 PBoolean Decode(
00540 const PString & cypher,
00541 PBYTEArray & clear
00542 );
00544 PINDEX Decode(
00545 const PString & cypher,
00546 void * data,
00547 PINDEX length
00548 );
00550 PINDEX Decode(
00551 const PBYTEArray & coded,
00552 void * data,
00553 PINDEX length
00554 );
00570 PBoolean Decode(
00571 const PBYTEArray & coded,
00572 PBYTEArray & clear
00573 );
00574
00575
00576 protected:
00580 PCypher(
00581 PINDEX blockSize,
00582 BlockChainMode chainMode
00583 );
00584 PCypher(
00585 const void * keyData,
00586 PINDEX keyLength,
00587 PINDEX blockSize,
00588 BlockChainMode chainMode
00589 );
00590
00591
00593 virtual void Initialise(
00594 PBoolean encoding
00595 ) = 0;
00596
00598 virtual void EncodeBlock(
00599 const void * in,
00600 void * out
00601 ) = 0;
00602
00603
00605 virtual void DecodeBlock(
00606 const void * in,
00607 void * out
00608 ) = 0;
00609
00610
00612 PBYTEArray key;
00614 PINDEX blockSize;
00616 BlockChainMode chainMode;
00617 };
00618
00619
00627 class PTEACypher : public PCypher
00628 {
00629 PCLASSINFO(PTEACypher, PCypher)
00630
00631 public:
00632 struct Key {
00633 BYTE value[16];
00634 };
00635
00640 PTEACypher(
00641 BlockChainMode chainMode = ElectronicCodebook
00642 );
00643 PTEACypher(
00644 const Key & keyData,
00645 BlockChainMode chainMode = ElectronicCodebook
00646 );
00647
00648
00650 void SetKey(
00651 const Key & newKey
00652 );
00653
00655 void GetKey(
00656 Key & newKey
00657 ) const;
00658
00659
00661 static void GenerateKey(
00662 Key & newKey
00663 );
00664
00665
00666 protected:
00668 virtual void Initialise(
00669 PBoolean encoding
00670 );
00671
00673 virtual void EncodeBlock(
00674 const void * in,
00675 void * out
00676 );
00677
00679 virtual void DecodeBlock(
00680 const void * in,
00681 void * out
00682 );
00683
00684 private:
00685 DWORD k0, k1, k2, k3;
00686 };
00687
00688
00689 #ifdef P_CONFIG_FILE
00690
00691 class PSecureConfig : public PConfig
00692 {
00693 PCLASSINFO(PSecureConfig, PConfig)
00694
00695
00696
00697
00698
00699 public:
00700 PSecureConfig(
00701 const PTEACypher::Key & productKey,
00702 const PStringArray & securedKeys,
00703 Source src = Application
00704 );
00705 PSecureConfig(
00706 const PTEACypher::Key & productKey,
00707 const char * const * securedKeyArray,
00708 PINDEX count,
00709 Source src = Application
00710 );
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721 const PStringArray & GetSecuredKeys() const { return securedKeys; }
00722
00723
00724
00725
00726
00727
00728 const PString & GetSecurityKey() const { return securityKey; }
00729
00730
00731
00732
00733
00734
00735 const PString & GetExpiryDateKey() const { return expiryDateKey; }
00736
00737
00738
00739
00740
00741
00742 const PString & GetOptionBitsKey() const { return optionBitsKey; }
00743
00744
00745
00746
00747
00748
00749 const PString & GetPendingPrefix() const { return pendingPrefix; }
00750
00751
00752
00753
00754
00755
00756 void GetProductKey(
00757 PTEACypher::Key & productKey
00758 ) const;
00759
00760
00761
00762
00763
00764
00765
00766 enum ValidationState {
00767 Defaults,
00768 Pending,
00769 IsValid,
00770 Expired,
00771 Invalid
00772 };
00773 ValidationState GetValidation() const;
00774
00775
00776
00777
00778
00779
00780
00781 PBoolean ValidatePending();
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791 void ResetPending();
00792
00793
00794
00795
00796
00797
00798 protected:
00799 PTEACypher::Key productKey;
00800 PStringArray securedKeys;
00801 PString securityKey;
00802 PString expiryDateKey;
00803 PString optionBitsKey;
00804 PString pendingPrefix;
00805 };
00806
00807 #endif // P_CONFIG_FILE
00808
00809 #endif // PTLIB_CYPHER_H
00810
00811
00812