summaryrefslogtreecommitdiff
path: root/src/include/dkimverify.h
blob: fb7d564e826bc9c2e8eef2f59ea7d18e7a022042 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*****************************************************************************
*  Copyright 2005 Alt-N Technologies, Ltd.
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
*  This code incorporates intellectual property owned by Yahoo! and licensed
*  pursuant to the Yahoo! DomainKeys Patent License Agreement.
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*
*  Changes done by ¢feh@fehcom.de obeying the above license
*
*****************************************************************************/
#ifndef DKIMVERIFY_H
#define DKIMVERIFY_H

#include <vector>

#include "dkimbase.h"

/*  not used anymore
#define DKIM_ADSP_UNKNOWN        1
#define DKIM_ADSP_ALL            2
#define DKIM_ADSP_DISCARDABLE    3
*/

#define DKIM_POLICY_DOMAIN_NAME_TOO_LONG -50  // internal error
#define DKIM_POLICY_DNS_TEMP_FAILURE     -51  // internal error
#define DKIM_POLICY_DNS_PERM_FAILURE     -52  // internal error
#define DKIM_POLICY_INVALID              -53  // internal error

/* dito
#define DKIM_SIG_VERSION_PRE_02    0
#define DKIM_SIG_VERSION_02_PLUS   1
*/

class SelectorInfo {
public:
  SelectorInfo(const string& sSelector, const string& sDomain);
  ~SelectorInfo();

  string Domain;
  string Selector;
  string Granularity;
  bool AllowSHA1;
  bool AllowSHA256;
  EVP_PKEY *PublicKey; /* the public key */
  bool Testing;
  bool SameDomain;

  int Status;

  int Parse(char *Buffer);
};

class SignatureInfo {
public:
  SignatureInfo(bool SaveCanonicalizedData);
  ~SignatureInfo();

  void Hash(const char *szBuffer, unsigned nBufLength, bool IsBody = false);

  string Header;
  unsigned Version;
  string Domain;
  string Selector;
  string SignatureData;
  string BodyHashData;
  string IdentityLocalPart;
  string IdentityDomain;
  string CanonicalizedData;
  vector<string> SignedHeaders;
  unsigned BodyLength;
  unsigned HeaderCanonicalization;
  unsigned BodyCanonicalization;
  unsigned ExpireTime;

  unsigned VerifiedBodyCount;
  unsigned UnverifiedBodyCount;

#if (                                      \
    (OPENSSL_VERSION_NUMBER < 0x10100000L) \
    || (LIBRESSL_VERSION_NUMBER > 0 && LIBRESSL_VERSION_NUMBER < 0x20700000L))
  EVP_MD_CTX m_Hdr_ctx;
  EVP_MD_CTX m_Bdy_ctx;
#else
  EVP_MD_CTX *m_Hdr_ctx;
  EVP_MD_CTX *m_Bdy_ctx;
#endif
#if (OPENSSL_VERSION_NUMBER > 0x10101000L)
  EVP_MD_CTX *m_Msg_ctx;
#endif

  SelectorInfo *m_pSelector;

  int Status;
  int m_nHash;  // use one of the DKIM_HASH_xxx constants here
  unsigned EmptyLineCount;
  bool m_SaveCanonicalizedData;
};

class CDKIMVerify : public CDKIMBase {
public:
  CDKIMVerify();
  ~CDKIMVerify();
  // virtual ~CDKIMVerify() = 0;

  int Init(DKIMVerifyOptions *pOptions);

  int GetResults(void);
  int GetDetails(int *nSigCount, DKIMVerifyDetails **pDetails);
  // int _DNSGetTXT(const char* szFQDN,char* Buffer,int nBufLen);

  virtual int ProcessHeaders(void);
  virtual int ProcessBody(char *szBuffer, int nBufLength, bool bEOF);

  const char *GetPractices()
  {
    return Practices.c_str();
  }

protected:
  int ParseDKIMSignature(const string& sHeader, SignatureInfo& sig);

  SelectorInfo& GetSelector(const string& sSelector, const string& sDomain);

  // int GetADSP(const std::string& sDomain, int& iADSP);

  list<SignatureInfo> Signatures;
  list<SelectorInfo> Selectors;

  DKIMDNSCALLBACK m_pfnSelectorCallback;  // selector record callback
  // DKIMDNSCALLBACK m_pfnPracticesCallback;    // ADSP record callback

  bool m_HonorBodyLengthTag;
  bool m_CheckPractices;
  bool m_SubjectIsRequired;
  bool m_SaveCanonicalizedData;
  bool m_AllowUnsignedFromHeaders;

  vector<DKIMVerifyDetails> Details;
  string Practices;
};

#endif  //DKIMVERIFY_H