-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathverifyxmllicense.cpp
196 lines (165 loc) · 4.32 KB
/
verifyxmllicense.cpp
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
//g++ -I/usr/include/libxml2 verifyxmllicense.cpp -lcrypto++ -lxml2 -o verifyxmllicense
#include <iostream>
#include <map>
#include <vector>
#include <sstream>
#include <stdexcept>
using namespace std;
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <crypto++/rsa.h>
#include <crypto++/osrng.h>
#include <crypto++/base64.h>
#include <crypto++/files.h>
using namespace CryptoPP;
vector<vector<string> > ParseInfo(xmlNode *el)
{
vector<vector<string> > info;
for (xmlNode *el2 = el->children; el2; el2 = el2->next)
{
map<string, string> data;
if (el2->type != XML_ELEMENT_NODE) continue;
xmlElement *el2t = (xmlElement *)el2;
xmlAttribute *attributes = el2t->attributes;
unsigned int i=0;
xmlAttribute *attr = &attributes[i];
while(attr!=NULL)
{
xmlChar* value = xmlNodeListGetString(el2t->doc, attr->children, 1);
data[(const char *)attr->name] = (const char *)value;
xmlFree(value);
attr = (xmlAttribute *)attr->next;
}
vector<string> pair;
pair.push_back(data["k"]);
pair.push_back(data["v"]);
info.push_back(pair);
}
return info;
}
void myReplace(std::string& str, const std::string& oldStr, const std::string& newStr)
{
//From http://stackoverflow.com/a/1494435
size_t pos = 0;
while((pos = str.find(oldStr, pos)) != std::string::npos)
{
str.replace(pos, oldStr.length(), newStr);
pos += newStr.length();
}
}
string SerialiseKeyPairs(vector<vector<std::string> > &info)
{
string out;
for(unsigned int pairNum = 0;pairNum < info.size();pairNum++)
{
out.append("<data k=\"");
myReplace(info[pairNum][0], "\"", """);
out.append(info[pairNum][0]);
out.append("\" v=\"");
myReplace(info[pairNum][1], "\"", """);
out.append(info[pairNum][1]);
out.append("\" />");
}
return out;
}
int VerifyLicense(string signedTxt, string sigIn, string pubKeyEnc)
{
//Read public key
CryptoPP::ByteQueue bytes;
StringSource file(pubKeyEnc, true, new Base64Decoder);
file.TransferTo(bytes);
bytes.MessageEnd();
RSA::PublicKey pubKey;
pubKey.Load(bytes);
RSASSA_PKCS1v15_SHA_Verifier verifier(pubKey);
//Read signed message
CryptoPP::ByteQueue sig;
StringSource sigFile(sigIn, true, new Base64Decoder);
string sigStr;
StringSink sigStrSink(sigStr);
sigFile.TransferTo(sigStrSink);
string combined(signedTxt);
combined.append(sigStr);
//Verify signature
try
{
StringSource(combined, true,
new SignatureVerificationFilter(
verifier, NULL,
SignatureVerificationFilter::THROW_EXCEPTION
)
);
}
catch(SignatureVerificationFilter::SignatureVerificationFailed &err)
{
cout << err.what() << endl;
return 0;
}
return 1;
}
string GetFileContent(string filename)
{
ifstream fi(filename.c_str());
if(!fi)
{
runtime_error("Could not open file");
}
// get length of file:
fi.seekg (0, fi.end);
int length = fi.tellg();
fi.seekg (0, fi.beg);
stringstream test;
test << fi.rdbuf();
return test.str();
}
int Verify(const char *filename)
{
//parse the file and get the DOM
xmlDoc *doc = xmlReadFile(filename, NULL, 0);
if (doc == NULL)
{
cout << "error: could not parse file" << endl;
return 0;
}
//Get the root element node
xmlNode *root_element = xmlDocGetRootElement(doc);
map<string, string> data;
vector<vector<string> > info;
//Iterate over xml elements
for (xmlNode *rootEl = root_element; rootEl; rootEl = rootEl->next)
{
if (rootEl->type != XML_ELEMENT_NODE) continue;
for (xmlNode *el = rootEl->children; el; el = el->next)
{
if(el->type != XML_ELEMENT_NODE) continue;
//cout << "node type: Element, name: "<< el->name << endl;
if(string((const char*)el->name) == string("info"))
{
info = ParseInfo(el);
}
else
{
xmlChar* value = xmlNodeListGetString(el->doc, el->children, 1);
data[(char *)el->name] = (char *)value;
xmlFree(value);
}
}
}
//Print found elements
cout << SerialiseKeyPairs(info) << endl;
map<string, string>::iterator it;
for(it = data.begin(); it != data.end(); it++)
{
cout << it->first << endl;
}
string masterPubKey = GetFileContent("master-pubkey.txt");
cout << "Info signature ret:" << VerifyLicense(SerialiseKeyPairs(info), data["infosig"], data["key"]) << endl;
cout << "Key signature ret:" << VerifyLicense(data["key"], data["keysig"], masterPubKey) << endl;
//free the document
xmlFreeDoc(doc);
return 1;
}
int main()
{
Verify("xmllicense.xml");
}