-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathAdES.hpp
246 lines (203 loc) · 6.76 KB
/
AdES.hpp
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include <cryptxml.h>
#include <memory>
#include <tuple>
//using namespace std;
class HRESULTERROR
{
public:
HRESULT hr = 0;
std::string err;
HRESULTERROR(HRESULT hrx = E_FAIL, const char* str = "")
{
hr = hrx;
if (str)
err = str;
}
operator HRESULT()
{
return hr;
}
};
class AdES
{
private:
template <typename T> T* AddMem(std::vector<std::shared_ptr<std::vector<char>>>& mem, size_t sz = sizeof(T))
{
std::shared_ptr<std::vector<char>> x = std::make_shared<std::vector<char>>();
x->resize(sz);
mem.push_back(x);
T* d = (T*)mem[mem.size() - 1].get()->data();
return d;
}
HRESULT GetEncryptedHash(const char*d, DWORD sz, PCCERT_CONTEXT ctx,CRYPT_ALGORITHM_IDENTIFIER hash, std::vector<char> &rs);
// HRESULT VerifyEncryptedHash(const char* d, DWORD sz, const char* org, DWORD orgsz);
public:
enum class ALEVEL
{
S = 0,
E = 1,
};
enum class ATYPE
{
CADES = 0,
XADES = 1
};
enum class LEVEL
{
CMS = 0,
XMLDSIG = 0,
B = 1,
T = 2,
C = 3,
X = 4,
XL = 5,
A = 6,
};
enum class ATTACHTYPE
{
DETACHED = 0,
ENVELOPING = 1,
ATTACHED = 1,
ENVELOPED = 2,
};
struct PDFSIGNVISIBLE
{
std::string t;
int leftp = 1;
int topp = 1;
int wip = 50;
int botp = 5;
};
struct PDFSIGNPARAMETERS
{
std::string Name;
std::string Location;
std::string Reason;
std::string Contact;
PDFSIGNVISIBLE Visible;
void ClearPars(std::string& s)
{
std::string a;
for (auto& ss : s)
{
if (ss != '(' && ss != ')')
a += ss;
}
s = a;
}
};
struct SIGNPARAMETERS
{
int ConformanceLevel = 0;
CRYPT_ALGORITHM_IDENTIFIER HashAlgorithm = { szOID_NIST_sha256 };
ATTACHTYPE Attached = ATTACHTYPE::ATTACHED;
std::wstring TSServer = L"http://timestamp.comodoca.com/";
std::string Policy;
std::string TSPolicy;
std::string commitmentTypeOid;// 1.2.840.113549.1.9.16.6.1 - 6
std::vector<CRYPT_ATTRIBUTE> cextras;
std::string xextras;
int Type1OrType2 = 2; // For X and XL forms timestamp, currently 2 is supported, this parameter is ignored
bool ASiC = false; // True if this is for ASiC
bool Debug = false;
bool PAdES = false; // True if PAdES, to eliminate self timestamp
std::vector<char> PE; // If PE, put the data to hash there
PDFSIGNPARAMETERS pdfparams;
bool XMLComments = false;
};
struct VERIFYRESULT
{
std::string Policy;
std::string Commitment;
};
struct VERIFYRESULTS
{
std::vector<VERIFYRESULT> Results;
};
struct CERTANDCRL
{
PCCERT_CONTEXT cert;
std::vector<PCCRL_CONTEXT> Crls;
};
struct CERT
{
CERTANDCRL cert;
std::vector<CERTANDCRL> More;
};
AdES();
struct FILEREF
{
const char* data = 0; // pointer to data
DWORD sz = 0; // size, or 0 if null terminated XML
const char* ref = 0;
std::string mime = "application/octet-stream";
FILEREF(const char * d = 0, DWORD z = 0, const char* rref = 0, const char* mim = 0)
{
data = d;
sz = z;
ref = rref;
if (mim)
mime = mim;
}
};
struct GREEKRESULTS
{
int Type = 0; // 0 none ,1 soft, 2 hard
int TSThere = 0; // 0 none, 1 generic, 2 greek TSA with policy
int Level = 0; // Equal to PADES levels
};
HRESULT GreekVerifyCertificate(PCCERT_CONTEXT c, const char* sig,DWORD sigsize,GREEKRESULTS& r);
HRESULT GreekVerifyTimestamp(PCCERT_CONTEXT c, PCRYPT_TIMESTAMP_CONTEXT tc, GREEKRESULTS& r);
struct PDFVERIFY
{
std::vector<char> dx;
std::vector<char> sig;
HRESULTERROR S;
bool Full = false;
LEVEL l;
std::vector<PCCERT_CONTEXT> Certs;
VERIFYRESULTS vr;
};
HRESULT AddCT(std::vector<char>& Signature, const std::vector<CERT>& Certificates, SIGNPARAMETERS& Params);
std::tuple<HRESULT,std::vector<char>,std::vector<char>> AddCC(std::vector<char>& Signature, const std::vector<CERT>& Certificates,SIGNPARAMETERS& Params);
HRESULT AddCX(std::vector<char>& Signature, const std::vector<CERT>& Certificates, SIGNPARAMETERS& Params, std::vector<char>& full1, std::vector<char >&full2);
HRESULT AddCXL(std::vector<char>& Signature, const std::vector<CERT>& Certificates, SIGNPARAMETERS& Params);
HRESULT TimeStamp(SIGNPARAMETERS& params,const char* data, DWORD sz, std::vector<char>& CR, const wchar_t* url = L"http://timestamp.comodoca.com/", const char* alg = szOID_NIST_sha256);
HRESULT Sign(LEVEL lev,const char* data,DWORD sz,const std::vector<CERT>& Certificates, SIGNPARAMETERS& Params,std::vector<char>& Signature);
HRESULT Verify(const char* data, DWORD sz, LEVEL& lev,const char* omsg = 0,DWORD len = 0,std::vector<char>* msg = 0,std::vector<PCCERT_CONTEXT>* Certs = 0,VERIFYRESULTS* vr = 0,bool WasPDF = false);
HRESULT VerifyB(const char* data, DWORD sz, int sidx = 0,bool Attached = true,PCCERT_CONTEXT c = 0,bool WasPDF = false);
HRESULT VerifyT(const char* data, DWORD sz, PCCERT_CONTEXT* pX = 0, bool Attached = true, int TSServerSignIndex = 0, FILETIME* ft = 0, PCRYPT_TIMESTAMP_CONTEXT* ptc = 0);
HRESULT VerifyU(const char* data, DWORD sz, bool Attached = true, int TSServerSignIndex = 0);
HRESULT XMLSign(LEVEL lev, std::vector<FILEREF>& data,const std::vector<CERT>& Certificates,SIGNPARAMETERS& Params, std::vector<char>& Signature);
HRESULT XMLVerify(const char* xmldata, LEVEL& lev, ATTACHTYPE& att,const char* omsg = 0, DWORD len = 0, bool WasDetachedCanonicalized = false,std::vector<PCCERT_CONTEXT> * Certs = 0, VERIFYRESULTS * vr = 0);
HRESULTERROR PDFCreateDSSObject(const std::vector<CERT>& Certificates, long long objnum,std::vector<std::vector<char>>& r);
HRESULTERROR PDFSign(LEVEL lev, const char* data, DWORD sz, const std::vector<CERT>& Certificates, SIGNPARAMETERS& Params, std::vector<char>& Signature,std::vector<PDFVERIFY>* = 0);
HRESULTERROR PDFVerify(const char* d, DWORD sz, std::vector<PDFVERIFY>& VerifyX);
HRESULTERROR PESign(LEVEL levx, const char* d, DWORD sz, const std::vector<CERT>& Certificates, SIGNPARAMETERS& Params, std::vector<char>& res);
HRESULT ASiC(ALEVEL alev,ATYPE typ, LEVEL lev,std::vector<FILEREF>& data,std::vector<CERT>& Certificates, SIGNPARAMETERS& Params, std::vector<char>& fndata);
struct ASICVERIFY
{
bool Full = false;
ALEVEL alev = ALEVEL::S;
LEVEL lev = LEVEL::CMS;
ATYPE atyp = ATYPE::CADES;
std::vector<PCCERT_CONTEXT> Certs;
VERIFYRESULTS vr;
std::vector<std::tuple<std::wstring,HRESULT>> items;
};
HRESULT VerifyASiC(const char* data,size_t sz,ASICVERIFY& av);
};
#ifdef _WIN64
#ifdef _DEBUG
#pragma comment(lib,".\\packages\\zlib-msvc14-x64.1.2.11.7795\\build\\native\\lib_debug\\zlibstaticd.lib")
#else
#pragma comment(lib,".\\packages\\zlib-msvc14-x64.1.2.11.7795\\build\\native\\lib_release\\zlibstatic.lib")
#endif
#else
#ifdef _DEBUG
#pragma comment(lib,".\\packages\\zlib-msvc14-x86.1.2.11.7795\\build\\native\\lib_debug\\zlibstaticd.lib")
#else
#pragma comment(lib,".\\packages\\zlib-msvc14-x86.1.2.11.7795\\build\\native\\lib_release\\zlibstatic.lib")
#endif
#pragma comment(lib,"vcruntime.lib")
#endif