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: 24177 $
00027  * $Author: rjongbloed $
00028  * $Date: 2010-04-05 06:52:04 -0500 (Mon, 05 Apr 2010) $
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  // 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 
00113     PString CompleteEncoding();
00114 
00115 
00116     static PString Encode(
00117       const PString & str     // String to be encoded to Base64
00118     );
00119     static PString Encode(
00120       const char * cstr       // C String to be encoded to Base64
00121     );
00122     static PString Encode(
00123       const PBYTEArray & data // Data block to be encoded to Base64
00124     );
00125     static PString Encode(
00126       const void * dataBlock, // Pointer to data to be encoded to Base64
00127       PINDEX length           // Length of the data block.
00128     );
00129     // Encode the data in memory to Base 64 data returnin the string.
00130 
00131 
00132     void StartDecoding();
00133     // Begin a base 64 decoding operation, initialising the object instance.
00134 
00140     PBoolean ProcessDecoding(
00141       const PString & str      // String to be encoded
00142     );
00143     PBoolean ProcessDecoding(
00144       const char * cstr        // C String to be encoded
00145     );
00146 
00152     PBoolean GetDecodedData(
00153       void * dataBlock,    // Pointer to data to be decoded from base64
00154       PINDEX length        // Length of the data block.
00155     );
00156     PBYTEArray GetDecodedData();
00157 
00165     PBoolean IsDecodeOK() { return perfectDecode; }
00166 
00167 
00179     static PString Decode(
00180       const PString & str // Encoded base64 string to be decoded.
00181     );
00182     static PBoolean Decode(
00183       const PString & str, // Encoded base64 string to be decoded.
00184       PBYTEArray & data    // Converted binary data from base64.
00185     );
00186     static PBoolean Decode(
00187       const PString & str, // Encoded base64 string to be decoded.
00188       void * dataBlock,    // Pointer to data to be decoded from base64
00189       PINDEX length        // Length of the data block.
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     // backwards compatibility functions
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   // New functions for class
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,        // Clear text binary data to be encoded.
00525       PINDEX length,            // Number of bytes of data to be encoded.
00526       PBYTEArray & coded        // Encoded data.
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 /* This class defines a set of configuration keys which may be secured by an
00695    encrypted hash function. Thus values contained in keys specified by this
00696    class cannot be changed without invalidating the hash function.
00697  */
00698 
00699   public:
00700     PSecureConfig(
00701       const PTEACypher::Key & productKey,    // Key to decrypt validation code.
00702       const PStringArray    & securedKeys,   // List of secured keys.
00703       Source src = Application        // Standard source for the configuration.
00704     );
00705     PSecureConfig(
00706       const PTEACypher::Key & productKey,   // Key to decrypt validation code.
00707       const char * const * securedKeyArray, // List of secured keys.
00708       PINDEX count,                         // Number of secured keys in list.
00709       Source src = Application        // Standard source for the configuration.
00710     );
00711     /* Create a secured configuration. The default section for the
00712        configuration keys is "Secured Options", the default security key is
00713        "Validation" and the defualt prefix string is "Pending:".
00714 
00715        The user can descend from this class and change any of the member
00716        variable for the names of keys or the configuration file section.
00717      */
00718 
00719 
00720   // New functions for class
00721     const PStringArray & GetSecuredKeys() const { return securedKeys; }
00722     /* Get the list of secured keys in the configuration file section.
00723 
00724        @return
00725        Array of  strings for the secured keys.
00726      */
00727 
00728     const PString & GetSecurityKey() const { return securityKey; }
00729     /* Get the security keys name in the configuration file section.
00730 
00731        @return
00732        String for the security values key.
00733      */
00734 
00735     const PString & GetExpiryDateKey() const { return expiryDateKey; }
00736     /* Get the expiry date keys name in the configuration file section.
00737 
00738        @return
00739        String for the expiry date values key.
00740      */
00741 
00742     const PString & GetOptionBitsKey() const { return optionBitsKey; }
00743     /* Get the Option Bits keys name in the configuration file section.
00744 
00745        @return
00746        String for the Option Bits values key.
00747      */
00748 
00749     const PString & GetPendingPrefix() const { return pendingPrefix; }
00750     /* Get the pending prefix name in the configuration file section.
00751 
00752        @return
00753        String for the pending prefix.
00754      */
00755 
00756     void GetProductKey(
00757       PTEACypher::Key & productKey  // Variable to receive the product key.
00758     ) const;
00759     /* Get the pending prefix name in the configuration file section.
00760 
00761        @return
00762        String for the pending prefix.
00763      */
00764 
00765 
00766     enum ValidationState {
00767       Defaults,
00768       Pending,
00769       IsValid,
00770       Expired,
00771       Invalid
00772     };
00773     ValidationState GetValidation() const;
00774     /* Check the current values attached to the keys specified in the
00775        constructor against an encoded validation key.
00776 
00777        @return
00778        State of the validation keys.
00779      */
00780 
00781     PBoolean ValidatePending();
00782     /* Validate a pending secured option list for the product. All secured
00783        keys with the <CODE>pendingPrefix</CODE> name will be checked against
00784        the value of the field <CODE>securityKey</CODE>. If they match then
00785        they are copied to the secured variables.
00786 
00787        @return
00788        true if secure key values are valid.
00789      */
00790 
00791     void ResetPending();
00792     /* "Unvalidate" a security configuration going back to a pending state,
00793        usually used after an <CODE>Invalid</CODE> response was recieved from
00794        the <A>GetValidation()</A> function.
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 // End Of File ///////////////////////////////////////////////////////////////

Generated on Fri Oct 14 01:44:09 2011 for PTLib by  doxygen 1.4.7