1 /*****************************************************************************
2 *  Copyright 2005 Alt-N Technologies, Ltd.
3 *
4 *  Licensed under the Apache License, Version 2.0 (the "License");
5 *  you may not use this file except in compliance with the License.
6 *  You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 *  This code incorporates intellectual property owned by Yahoo! and licensed
11 *  pursuant to the Yahoo! DomainKeys Patent License Agreement.
12 *
13 *  Unless required by applicable law or agreed to in writing, software
14 *  distributed under the License is distributed on an "AS IS" BASIS,
15 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 *  See the License for the specific language governing permissions and
17 *  limitations under the License.
18 *
19 *****************************************************************************/
20 
21 #ifndef DKIMSIGN_H
22 #define DKIMSIGN_H
23 
24 #include "dkimbase.h"
25 
26 class CDKIMSign : public CDKIMBase
27 {
28 public:
29 
30 	CDKIMSign();
31 	~CDKIMSign();
32 
33 	int Init( DKIMSignOptions* pOptions );
34 
35 	int GetSig( char* szPrivKey, char* szSignature, int nSigLength );
36 	int GetSig2( char* szPrivKey, char** pszSignature );
37 
38 	virtual int ProcessHeaders(void);
39 	virtual int ProcessBody( char* szBuffer, int nBufLength, bool bEOF );
40 
41 	enum CKDKIMConstants { OptimalHeaderLineLength = 65 };
42 
43 protected:
44 
45 	void Hash( const char* szBuffer, int nBufLength, bool bHdr, bool bAllmanOnly = false );
46 
47 	bool SignThisTag( const string& sTag );
48 	void GetHeaderParams( const string& sHdr );
49 	void ProcessHeader( const string& sHdr );
50 	bool ParseFromAddress( void );
51 
52 	void InitSig(void);
53 	void AddTagToSig( char* Tag, const string &sValue, char cbrk, bool bFold );
54 	void AddTagToSig( char* Tag, unsigned long nValue );
55 	void AddInterTagSpace( int nSizeOfNextTag );
56 	void AddFoldedValueToSig( const string &sValue, char cbrk );
57 
58 	bool IsRequiredHeader( const string& sTag );
59 	int ConstructSignature( char* szPrivKey, bool bUseIetfBodyHash, bool bUseSha256 );
60 
61 	int AssembleReturnedSig( char* szPrivKey );
62 
63 	EVP_MD_CTX *m_Hdr_ietf_sha1ctx;		/* the header hash for ietf sha1  */
64 	EVP_MD_CTX *m_Hdr_ietf_sha256ctx;	/* the header hash for ietf sha256 */
65 
66 	EVP_MD_CTX *m_Bdy_ietf_sha1ctx;		/* the body hash for ietf sha1  */
67 	EVP_MD_CTX *m_Bdy_ietf_sha256ctx;	/* the body hash for ietf sha256 */
68 
69 	EVP_MD_CTX *m_allman_sha1ctx;		/* the hash for allman sha1  */
70 
71 	int m_Canon;				// canonization method
72 
73 	int m_EmptyLineCount;
74 
75 	string hParam;
76 	string sFrom;
77 	string sSender;
78 	string sSelector;
79 	string sDomain;
80 	string sIdentity;					// for i= tag, if empty tag will not be included in sig
81 	string sRequiredHeaders;
82 
83 	bool m_IncludeBodyLengthTag;
84 	int m_nBodyLength;
85 	time_t m_ExpireTime;
86 	int m_nIncludeTimeStamp;				// 0 = don't include t= tag, 1 = include t= tag
87 	int m_nIncludeQueryMethod;				// 0 = don't include q= tag, 1 = include q= tag
88 	int m_nHash;							// use one of the DKIM_HASH_xx constants here
89 	int m_nIncludeCopiedHeaders;			// 0 = don't include z= tag, 1 = include z= tag
90 	int m_nIncludeBodyHash;					// 0 = calculate sig using draft 0, 1 = include bh= tag and
91 											// use new signature computation algorithm
92 
93 
94 	DKIMHEADERCALLBACK m_pfnHdrCallback;
95 
96 	string m_sSig;
97 	int m_nSigPos;
98 
99 	string m_sReturnedSig;
100 	bool m_bReturnedSigAssembled;
101 
102 	string m_sCopiedHeaders;
103 
104 };
105 
106 
107 
108 #endif // DKIMSIGN_H
109