cypher.h

Go to the documentation of this file.
00001 /*
00002  * cypher.h
00003  *
00004  * Encryption support classes.
00005  *
00006  * Portable Windows Library
00007  *
00008  * Copyright (c) 1993-2002 Equivalence Pty. Ltd.
00009  *
00010  * The contents of this file are subject to the Mozilla Public License
00011  * Version 1.0 (the "License"); you may not use this file except in
00012  * compliance with the License. You may obtain a copy of the License at
00013  * http://www.mozilla.org/MPL/
00014  *
00015  * Software distributed under the License is distributed on an "AS IS"
00016  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
00017  * the License for the specific language governing rights and limitations
00018  * under the License.
00019  *
00020  * The Original Code is Portable Windows Library.
00021  *
00022  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
00023  *
00024  * Contributor(s): ______________________________________.
00025  *
00026  * $Revision: 21788 $
00027  * $Author: rjongbloed $
00028  * $Date: 2008-12-11 23:42:13 -0600 (Thu, 11 Dec 2008) $
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  // Use CR, LF pairs in end of line characters.
00082     );
00083     // Begin a base 64 encoding operation, initialising the object instance.
00084 
00085     void ProcessEncoding(
00086       const PString & str      // String to be encoded
00087     );
00088     void ProcessEncoding(
00089       const char * cstr        // C String to be encoded
00090     );
00091     void ProcessEncoding(
00092       const PBYTEArray & data  // Data block to be encoded
00093     );
00094     void ProcessEncoding(
00095       const void * dataBlock,  // Pointer to data to be encoded
00096       PINDEX length            // Length of the data block.
00097     );
00098     // Incorporate the specified data into the base 64 encoding.
00099 
00105     PString GetEncodedString();
00106 
00114     PString CompleteEncoding();
00115 
00116 
00117     static PString Encode(
00118       const PString & str     // String to be encoded to Base64
00119     );
00120     static PString Encode(
00121       const char * cstr       // C String to be encoded to Base64
00122     );
00123     static PString Encode(
00124       const PBYTEArray & data // Data block to be encoded to Base64
00125     );
00126     static PString Encode(
00127       const void * dataBlock, // Pointer to data to be encoded to Base64
00128       PINDEX length           // Length of the data block.
00129     );
00130     // Encode the data in memory to Base 64 data returnin the string.
00131 
00132 
00133     void StartDecoding();
00134     // Begin a base 64 decoding operation, initialising the object instance.
00135 
00141     PBoolean ProcessDecoding(
00142       const PString & str      // String to be encoded
00143     );
00144     PBoolean ProcessDecoding(
00145       const char * cstr        // C String to be encoded
00146     );
00147 
00153     PBoolean GetDecodedData(
00154       void * dataBlock,    // Pointer to data to be decoded from base64
00155       PINDEX length        // Length of the data block.
00156     );
00157     PBYTEArray GetDecodedData();
00158 
00166     PBoolean IsDecodeOK() { return perfectDecode; }
00167 
00168 
00180     static PString Decode(
00181       const PString & str // Encoded base64 string to be decoded.
00182     );
00183     static PBoolean Decode(
00184       const PString & str, // Encoded base64 string to be decoded.
00185       PBYTEArray & data    // Converted binary data from base64.
00186     );
00187     static PBoolean Decode(
00188       const PString & str, // Encoded base64 string to be decoded.
00189       void * dataBlock,    // Pointer to data to be decoded from base64
00190       PINDEX length        // Length of the data block.
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     // backwards compatibility functions
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   // New functions for class
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,        // Clear text binary data to be encoded.
00526       PINDEX length,            // Number of bytes of data to be encoded.
00527       PBYTEArray & coded        // Encoded data.
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 /* This class defines a set of configuration keys which may be secured by an
00696    encrypted hash function. Thus values contained in keys specified by this
00697    class cannot be changed without invalidating the hash function.
00698  */
00699 
00700   public:
00701     PSecureConfig(
00702       const PTEACypher::Key & productKey,    // Key to decrypt validation code.
00703       const PStringArray    & securedKeys,   // List of secured keys.
00704       Source src = Application        // Standard source for the configuration.
00705     );
00706     PSecureConfig(
00707       const PTEACypher::Key & productKey,   // Key to decrypt validation code.
00708       const char * const * securedKeyArray, // List of secured keys.
00709       PINDEX count,                         // Number of secured keys in list.
00710       Source src = Application        // Standard source for the configuration.
00711     );
00712     /* Create a secured configuration. The default section for the
00713        configuration keys is "Secured Options", the default security key is
00714        "Validation" and the defualt prefix string is "Pending:".
00715 
00716        The user can descend from this class and change any of the member
00717        variable for the names of keys or the configuration file section.
00718      */
00719 
00720 
00721   // New functions for class
00722     const PStringArray & GetSecuredKeys() const { return securedKeys; }
00723     /* Get the list of secured keys in the configuration file section.
00724 
00725        @return
00726        Array of  strings for the secured keys.
00727      */
00728 
00729     const PString & GetSecurityKey() const { return securityKey; }
00730     /* Get the security keys name in the configuration file section.
00731 
00732        @return
00733        String for the security values key.
00734      */
00735 
00736     const PString & GetExpiryDateKey() const { return expiryDateKey; }
00737     /* Get the expiry date keys name in the configuration file section.
00738 
00739        @return
00740        String for the expiry date values key.
00741      */
00742 
00743     const PString & GetOptionBitsKey() const { return optionBitsKey; }
00744     /* Get the Option Bits keys name in the configuration file section.
00745 
00746        @return
00747        String for the Option Bits values key.
00748      */
00749 
00750     const PString & GetPendingPrefix() const { return pendingPrefix; }
00751     /* Get the pending prefix name in the configuration file section.
00752 
00753        @return
00754        String for the pending prefix.
00755      */
00756 
00757     void GetProductKey(
00758       PTEACypher::Key & productKey  // Variable to receive the product key.
00759     ) const;
00760     /* Get the pending prefix name in the configuration file section.
00761 
00762        @return
00763        String for the pending prefix.
00764      */
00765 
00766 
00767     enum ValidationState {
00768       Defaults,
00769       Pending,
00770       IsValid,
00771       Expired,
00772       Invalid
00773     };
00774     ValidationState GetValidation() const;
00775     /* Check the current values attached to the keys specified in the
00776        constructor against an encoded validation key.
00777 
00778        @return
00779        State of the validation keys.
00780      */
00781 
00782     PBoolean ValidatePending();
00783     /* Validate a pending secured option list for the product. All secured
00784        keys with the <CODE>pendingPrefix</CODE> name will be checked against
00785        the value of the field <CODE>securityKey</CODE>. If they match then
00786        they are copied to the secured variables.
00787 
00788        @return
00789        PTrue if secure key values are valid.
00790      */
00791 
00792     void ResetPending();
00793     /* "Unvalidate" a security configuration going back to a pending state,
00794        usually used after an <CODE>Invalid</CODE> response was recieved from
00795        the <A>GetValidation()</A> function.
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 // End Of File ///////////////////////////////////////////////////////////////

Generated on Thu May 27 01:36:47 2010 for PTLib by  doxygen 1.4.7