Signature.h

Go to the documentation of this file.
00001 //
00002 //  Copyright (c) 2006 by Autodesk, Inc.
00003 //
00004 //  By using this code, you are agreeing to the terms and conditions of
00005 //  the License Agreement included in the documentation for this code.
00006 //
00007 //  AUTODESK MAKES NO WARRANTIES, EXPRESS OR IMPLIED, AS TO THE CORRECTNESS
00008 //  OF THIS CODE OR ANY DERIVATIVE WORKS WHICH INCORPORATE IT. AUTODESK
00009 //  PROVIDES THE CODE ON AN "AS-IS" BASIS AND EXPLICITLY DISCLAIMS ANY
00010 //  LIABILITY, INCLUDING CONSEQUENTIAL AND INCIDENTAL DAMAGES FOR ERRORS,
00011 //  OMISSIONS, AND OTHER PROBLEMS IN THE CODE.
00012 //
00013 //  Use, duplication, or disclosure by the U.S. Government is subject to
00014 //  restrictions set forth in FAR 52.227-19 (Commercial Computer Software
00015 //  Restricted Rights) and DFAR 252.227-7013(c)(1)(ii) (Rights in Technical
00016 //  Data and Computer Software), as applicable.
00017 //
00018 //  $Header: //DWF/Development/Components/Internal/DWF Toolkit/v7.6/develop/global/src/dwf/package/Signature.h#2 $
00019 //  $DateTime: 2008/06/18 18:08:14 $
00020 //  $Author: gaoje $
00021 //  $Change: 101166 $
00022 //  $Revision: #2 $
00023 //
00024 
00025 #ifndef _DWFTK_SIGNATURE_H
00026 #define _DWFTK_SIGNATURE_H
00027 
00032 
00033 #include "dwfcore/STL.h"
00034 #include "dwfcore/Vector.h"
00035 #include "dwfcore/SkipList.h"
00036 #include "dwfcore/DigestOutputStream.h"
00037 #include "dwfcore/DigestInputStream.h"
00038 #include "dwfcore/crypto/CryptoEngine.h"
00039 using namespace DWFCore;
00040 
00041 #include "dwf/Toolkit.h"
00042 #include "dwf/package/reader/SignatureReader.h"
00043 
00044 
00045 // forward decl
00046 namespace DWFCore
00047 {
00048 class DWFCryptoKey;
00049 class DWFAsymmetricAlgorithm;
00050 }
00051 
00052 
00053 
00054 namespace DWFToolkit
00055 {
00056 
00057 // forward decl
00058 class DWFResource;
00059 
00060 
00070 class DWFSignature
00071             : public DWFSignatureReader
00072             _DWFTK_EXPORT_API_MEMORY_CONTROL_BASE_MEMBER
00073 {
00074 
00075 public:
00076     class Reference;
00077 
00081     typedef DWFOrderedVector<DWFSignature*>             tList;
00085     typedef DWFStringKeySkipList<DWFSignature*>         tMap;
00090     typedef DWFCachingIterator<DWFSignature*>           tCachingIterator;
00094     typedef DWFIterator<DWFSignature*>                  tIterator;
00095 
00099     typedef DWFOrderedVector<DWFSignature::Reference*>  tReferenceVector;
00100 
00104     typedef DWFOrderedVector<X509Data*>                 tX509DataVector;
00105 
00106 
00107 public:
00108 
00116     class DigestMethod
00117     {
00118     public:
00124         _DWFTK_API
00125         DigestMethod( )
00126             throw()
00127         {;}
00128 
00134         _DWFTK_API
00135         virtual ~DigestMethod()
00136             throw()
00137         {;}
00138 
00145         _DWFTK_API
00146         virtual const wchar_t* algorithmName() const = 0;
00147 
00154         _DWFTK_API
00155         virtual const DWFCryptoEngine::teType algorithmIdentifier() const = 0;
00156 
00169         _DWFTK_API
00170         virtual DWFDigestOutputStream* chainOutputStream(DWFOutputStream* pStream, bool bOwnStream) const
00171             throw( DWFException ) = 0;
00172 
00185         _DWFTK_API
00186         virtual DWFDigestInputStream* chainInputStream(DWFInputStream* pStream, bool bOwnStream) const
00187             throw( DWFException ) = 0;
00188     };
00189 
00197     class DigestMethodSHA1 : public DigestMethod
00198     {
00199     public:
00203         _DWFTK_API
00204         virtual const wchar_t* algorithmName() const
00205         {
00206             return kzAlgorithmNameSHA1;
00207         };
00208 
00212         _DWFTK_API
00213         virtual const DWFCryptoEngine::teType algorithmIdentifier() const
00214         {
00215             return DWFCryptoEngine::eSHA1;
00216         }
00217 
00221         _DWFTK_API
00222         virtual DWFDigestOutputStream* chainOutputStream(DWFOutputStream* pStream, bool bOwnStream) const
00223             throw( DWFException );
00224 
00228         _DWFTK_API
00229         virtual DWFDigestInputStream* chainInputStream(DWFInputStream* pStream, bool bOwnStream) const
00230             throw( DWFException );
00231 
00232     private:
00233         static const wchar_t* const kzAlgorithmNameSHA1;
00234     };
00235 
00243     class DigestMethodMD5 : public DigestMethod
00244     {
00245     public:
00249         _DWFTK_API
00250         virtual const wchar_t* algorithmName() const
00251         {
00252             return kzAlgorithmNameMD5;
00253         };
00254 
00258         _DWFTK_API
00259         virtual const DWFCryptoEngine::teType algorithmIdentifier() const
00260         {
00261             return DWFCryptoEngine::eMD5;
00262         }
00263 
00267         _DWFTK_API
00268         virtual DWFDigestOutputStream* chainOutputStream(DWFOutputStream* pStream, bool bOwnStream) const
00269             throw( DWFException );
00270 
00274         _DWFTK_API
00275         virtual DWFDigestInputStream* chainInputStream(DWFInputStream* pStream, bool bOwnStream) const
00276             throw( DWFException );
00277 
00278     private:
00279         static const wchar_t* const kzAlgorithmNameMD5;
00280     };
00281 
00282 
00290     class DigestValue
00291     {
00292     public:
00298         _DWFTK_API
00299         DigestValue()
00300             throw()
00301             : _zValue(L"")
00302         {
00303         }
00304 
00310         _DWFTK_API
00311         DigestValue(const DWFString& zValue)
00312             throw()
00313             : _zValue(zValue)
00314         {
00315         }
00316 
00322         _DWFTK_API
00323         virtual ~DigestValue()
00324             throw()
00325         {
00326         }
00327 
00328 
00335         _DWFTK_API
00336         virtual const DWFString& valueBase64() const
00337             throw()
00338         {
00339             return _zValue;
00340         }
00341 
00348         _DWFTK_API
00349         virtual void setValueBase64(const DWFString& zValue)
00350             throw()
00351         {
00352             _zValue = zValue;
00353         }
00354 
00364         _DWFTK_API
00365         virtual void getValueBytes(void*& rpBytes, size_t& nBytes) const
00366             throw();
00367 
00375         _DWFTK_API
00376         virtual void setValueBytes(const void *pBytes, const size_t nBytes)
00377             throw();
00378 
00379     private:
00380         DWFString _zValue;
00381     };
00382 
00383 
00391     class Reference
00392     {
00393         friend class DWFSignature;
00394 
00395     public:
00401         _DWFTK_API
00402         Reference()
00403             throw()
00404             : _zURI()
00405             , _pSignature(NULL)
00406             , _pDigestValue(NULL)
00407         {
00408             ;
00409         }
00410 
00416         _DWFTK_API
00417         virtual ~Reference()
00418             throw()
00419         {
00420             if(_pDigestValue!=NULL)
00421             {
00422                 DWFCORE_FREE_OBJECT(_pDigestValue);
00423             }
00424         }
00425 
00432         virtual const DWFString& URI() const
00433              throw( DWFException )
00434         {
00435             return _zURI;
00436         }
00437 
00444         virtual void setURI(const DWFString& zURI)
00445             throw()
00446         {
00447             _zURI = zURI;
00448         }
00449 
00450 
00456         _DWFTK_API
00457         const DigestValue* digestValue() const
00458             throw(DWFException)
00459         {
00460             return _pDigestValue;
00461         }
00462 
00470         _DWFTK_API
00471         void setDigestValue(const DigestValue* pDigestValue)
00472             throw()
00473         {
00474             _pDigestValue = pDigestValue;
00475         }
00476 
00477 
00483         DWFSignature* signature()
00484         {
00485             return _pSignature;
00486         }
00487 
00488     protected:
00494         void setSignature(DWFSignature* pSignature)
00495             throw( DWFException )
00496         {
00497             if(_pSignature != NULL )
00498             {
00499                 _DWFCORE_THROW( DWFInvalidArgumentException, /*NOXLATE*/ L"Can't re-use References." );
00500             }
00501 
00502             _pSignature = pSignature;
00503         }
00504 
00505     private:
00506         // The URI of what this Reference refers to
00507         DWFString               _zURI; 
00508 
00509         // The Signature to which this Reference belongs.
00510         DWFSignature*           _pSignature;
00511 
00512         // The Digest Value of this reference
00513         const DigestValue*      _pDigestValue;
00514     };
00515 
00516 
00524     class SignatureValue
00525     {
00526     public:
00532         _DWFTK_API
00533         SignatureValue()
00534             throw()
00535             : _zValue(L"")
00536         {
00537         }
00538 
00544         _DWFTK_API
00545         virtual ~SignatureValue()
00546             throw()
00547         {
00548         }
00549 
00556         _DWFTK_API
00557         virtual const DWFString& valueBase64() const
00558             throw()
00559         {
00560             return _zValue;
00561         }
00562 
00569         _DWFTK_API
00570         virtual void setValueBase64(const DWFString& zValue)
00571             throw()
00572         {
00573             _zValue = zValue;
00574         }
00575 
00585         _DWFTK_API
00586         virtual void getValueBytes(void*& rpBytes, size_t& nBytes) const
00587             throw();
00588 
00596         _DWFTK_API
00597         virtual void setValueBytes(const void *pBytes, const size_t nBytes)
00598             throw();
00599 
00600 
00601     private:
00602         DWFString _zValue;
00603     };
00604 
00605 
00613     class SignatureMethod
00614     {
00615     public:
00621         _DWFTK_API
00622         SignatureMethod()
00623             throw();
00624 
00630         _DWFTK_API
00631         virtual ~SignatureMethod()
00632             throw();
00633 
00634 
00641         _DWFTK_API
00642         virtual const wchar_t* algorithmName() const
00643             throw() = 0;
00644 
00651         _DWFTK_API
00652         virtual DWFAsymmetricAlgorithm* algorithm()
00653             throw();
00654 
00664         _DWFTK_API
00665         virtual void signDigest(const DigestValue* pDigestValue, const DigestMethod* pDigestMethod, DWFCryptoKey* pPrivateKey, SignatureValue* pSignatureValue)
00666             throw(DWFException);
00667 
00677         _DWFTK_API
00678         virtual bool verifyDigest(const DigestValue* pDigestValue, const DigestMethod* pDigestMethod, DWFCryptoKey* pPublicKey, const SignatureValue* pSignatureValue)
00679             throw(DWFException);
00680 
00681     protected:
00690         _DWFTK_API
00691         virtual DWFAsymmetricAlgorithm* createAlgorithm()
00692             throw() = 0;
00693 
00694     private:
00695         DWFAsymmetricAlgorithm* _pCachedAlgorithm;
00696     };
00697 
00705     class SignatureMethodDSA : public SignatureMethod
00706     {
00707     public:
00711         _DWFTK_API
00712         virtual const wchar_t* algorithmName() const
00713             throw()
00714         {
00715             return kzAlgorithmNameDSA_SHA1;
00716         };
00717 
00721         _DWFTK_API
00722         virtual DWFAsymmetricAlgorithm* createAlgorithm()
00723             throw();
00724 
00725     private:
00726         static const wchar_t* const kzAlgorithmNameDSA_SHA1;
00727     };
00728 
00736     class SignatureMethodRSA : public SignatureMethod
00737     {
00738     public:
00742         _DWFTK_API
00743         virtual const wchar_t* algorithmName() const
00744             throw()
00745         {
00746             return kzAlgorithmNameRSA_SHA1;
00747         };
00748 
00749     protected:
00753         _DWFTK_API
00754         virtual DWFAsymmetricAlgorithm* createAlgorithm()
00755             throw();
00756 
00757     private:
00758         static const wchar_t* const kzAlgorithmNameRSA_SHA1;
00759     };
00760 
00761 
00762 public:
00763 
00769     _DWFTK_API
00770     DWFSignature()
00771         throw();
00772 
00778     _DWFTK_API
00779     virtual ~DWFSignature()
00780         throw();
00781 
00782 
00785     //          Note that References should not be re-used.
00790     _DWFTK_API
00791     void addReference(Reference* pReference)
00792         throw( DWFException );
00793 
00800     _DWFTK_API
00801     tReferenceVector::Iterator* references()
00802         throw()
00803     {
00804         return _oReferences.iterator();
00805     }
00806 
00807 
00814     _DWFTK_API
00815     void setDigestMethod(const DigestMethod* pDigestMethod)
00816         throw();
00817 
00824     _DWFTK_API
00825     const DigestMethod* digestMethod() const
00826         throw()
00827     {
00828         return _pDigestMethod;
00829     }
00830 
00831 
00838     _DWFTK_API
00839     void setSignatureMethod(SignatureMethod* pSignatureMethod)
00840         throw();
00841 
00848     _DWFTK_API
00849     SignatureMethod* signatureMethod() const
00850         throw(DWFException)
00851     {
00852         return _pSignatureMethod;
00853     }
00854 
00855 
00862     _DWFTK_API
00863     void setSignatureValue(const SignatureValue* pSignatureValue)
00864         throw();
00865 
00872     _DWFTK_API
00873     const SignatureValue* signatureValue() const
00874         throw(DWFException)
00875     {
00876         return _pSignatureValue;
00877     }
00878 
00879 
00886     _DWFTK_API
00887     void addX509Data(X509Data* pX509Data)
00888         throw();
00889 
00896     _DWFTK_API
00897     void setPublicKey(DWFCryptoKey* pPublicKey)
00898         throw()
00899     {
00900         _pPublicKey = pPublicKey;
00901     }
00902 
00909     _DWFTK_API
00910     DWFCryptoKey* publicKey() const
00911         throw()
00912     {
00913         return _pPublicKey;
00914     }
00915 
00922     _DWFTK_API
00923     void setKeyName(const DWFString& zKeyName)
00924         throw()
00925     {
00926         _zKeyName = zKeyName;
00927     }
00928 
00935     _DWFTK_API
00936     const DWFString& keyName() const
00937         throw()
00938     {
00939         return _zKeyName;
00940     }
00941 
00942 
00950     _DWFTK_API
00951     virtual const DWFString& provideReference( const DWFString& zReferenceURI )
00952         throw( DWFException );
00953 
00961     _DWFTK_API
00962     virtual const DWFString& provideSignatureMethod( const DWFString& zSigMethod )
00963         throw( DWFException );
00964 
00972     _DWFTK_API
00973     virtual const DWFString& provideSignatureValue( const DWFString& zSigValue )
00974         throw( DWFException );
00975 
00986     _DWFTK_API
00987     virtual X509Data* provideX509Data( X509Data* pX509Data )
00988         throw( DWFException );
00989 
00990 
00998     _DWFTK_API
00999     virtual const DWFString& provideDigestMethod( const DWFString& zDigestMethod )
01000         throw( DWFException );
01001 
01009     _DWFTK_API
01010     virtual const DWFString& provideDigestValue( const DWFString& zDigestValue )
01011         throw( DWFException );
01012 
01020     _DWFTK_API
01021     virtual const DWFSignatureReader::RSAKeyValueStrings& provideRSAKeyValueStrings( const DWFSignatureReader::RSAKeyValueStrings& rRSAKeyValueStrings )
01022         throw( DWFException );
01023 
01031     _DWFTK_API
01032     virtual const DWFSignatureReader::DSAKeyValueStrings& provideDSAKeyValueStrings( const DWFSignatureReader::DSAKeyValueStrings& pDSAKeyValueStrings )
01033         throw( DWFException );
01034 
01042     _DWFTK_API
01043     virtual const DWFString& provideKeyName( const DWFString& zKeyName )
01044         throw( DWFException );
01045 
01046 #ifndef DWFTK_READ_ONLY
01047 
01053     _DWFTK_API
01054         virtual void computeSignedInfoDigestValue(DWFSignature::DigestValue& signedInfoDigest, const DWFString& zNamespace)
01055         throw( DWFException );
01056 
01058     _DWFTK_API
01059     virtual bool validateSignedInfo(DWFCryptoKey* pPublicKey)
01060         throw( DWFException );
01061 
01062 #endif
01063 
01065     _DWFTK_API
01066     virtual bool validateReferences(DWFPackageReader* pReader)
01067         throw( DWFException );
01068 
01069 
01070 
01071 protected:
01072 
01073 #ifndef DWFTK_READ_ONLY
01074 
01075     //
01076     // Serializes just the SignedInfo sub-tree
01077     //
01078     void _serializeSignedInfo( DWFXMLSerializer& rSerializer, const DWFString& zNamespace )
01079         throw( DWFException );
01080 
01081 #endif
01082 
01083 protected:
01084     float                   _nVersion;
01085     tReferenceVector        _oReferences;
01086     const DigestMethod*     _pDigestMethod;
01087     const SignatureValue*   _pSignatureValue;
01088     SignatureMethod*        _pSignatureMethod;
01089     tX509DataVector         _oX509DataVector;
01090     Reference*              _pCurrentReference;
01091     DWFCryptoKey*           _pPublicKey;
01092     DWFString               _zKeyName;
01093 
01094 
01095 private:
01096 
01097     DWFSignature( const DWFSignature& );
01098     DWFSignature& operator=( const DWFSignature& );
01099 };
01100 
01101 
01102 
01103     //
01104     // required for win32 dll external linkage
01105     //
01106 #ifdef  _DWFCORE_WIN32_SYSTEM
01107 #ifndef DWFTK_STATIC
01108 //DNT_Start
01109 _declspec(selectany) const wchar_t* const DWFSignature::DigestMethodSHA1::kzAlgorithmNameSHA1        = L"http://www.w3.org/2000/09/xmldsig#sha1";
01110 _declspec(selectany) const wchar_t* const DWFSignature::DigestMethodMD5::kzAlgorithmNameMD5          = L"http://www.w3.org/2000/09/xmldsig#md5";
01111 _declspec(selectany) const wchar_t* const DWFSignature::SignatureMethodDSA::kzAlgorithmNameDSA_SHA1  = L"http://www.w3.org/2000/09/xmldsig#dsa-sha1";
01112 _declspec(selectany) const wchar_t* const DWFSignature::SignatureMethodRSA::kzAlgorithmNameRSA_SHA1  = L"http://www.w3.org/2000/09/xmldsig#rsa-sha1";
01113 //DNT_End
01114 #endif
01115 #endif
01116 
01117 }
01118 
01119 #endif
01120 

Generated on Tue Jan 6 22:40:05 2009 for Autodesk DWF Toolkit by  doxygen 1.4.5