-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathverifylicense.cpp
More file actions
246 lines (218 loc) · 5.93 KB
/
Copy pathverifylicense.cpp
File metadata and controls
246 lines (218 loc) · 5.93 KB
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
245
246
// Verify a plain-text license and its secondary key chain using the detected signature type.
//g++ verifylicense.cpp -lcrypto++ -o verifylicense
#include <string>
#include <fstream>
using namespace std;
#include <crypto++/rsa.h>
#include <crypto++/osrng.h>
#include <crypto++/base64.h>
#include <crypto++/files.h>
#include <crypto++/pssr.h>
#include <crypto++/sha.h>
#include <crypto++/hex.h>
#include <crypto++/xed25519.h>
using namespace CryptoPP;
bool FileExists(const char *filename)
{
ifstream file(filename);
return file.good();
}
string ComputeKeyId(const string& pubKeyBase64)
{
string rawKey;
StringSource(pubKeyBase64, true, new Base64Decoder(new StringSink(rawKey)));
string digest;
SHA256 hash;
StringSource(rawKey, true, new HashFilter(hash, new HexEncoder(new StringSink(digest))));
return digest;
}
bool IsKeyRevoked(const string& keyId)
{
ifstream f("revoked-keys.txt");
if (!f.good()) return false;
string line;
while (getline(f, line))
{
if (!line.empty() && line.back() == '\r')
line.pop_back();
if (line == keyId) return true;
}
return false;
}
int CheckRevocation()
{
bool hasEdSecondary = FileExists("secondary-ed25519-pubkey.txt");
bool hasRsaSecondary = FileExists("secondary-pubkey.txt");
string pubKeyBase64;
try
{
if (hasEdSecondary)
FileSource("secondary-ed25519-pubkey.txt", true, new StringSink(pubKeyBase64));
else if (hasRsaSecondary)
FileSource("secondary-pubkey.txt", true, new StringSink(pubKeyBase64));
else
return 1;
}
catch (CryptoPP::Exception &err)
{
cout << "Crypto error reading secondary key: " << err.what() << endl;
return 0;
}
string keyId = ComputeKeyId(pubKeyBase64);
if (IsKeyRevoked(keyId))
{
cout << "Key revoked: " << keyId << endl;
return 0;
}
return 1;
}
int VerifyRsaSignatureText(string signedTxt, string sigFilename, string pubKeyFilename, string okMessage)
{
try
{
//Read public key
CryptoPP::ByteQueue bytes;
FileSource file(pubKeyFilename.c_str(), true, new Base64Decoder);
file.TransferTo(bytes);
bytes.MessageEnd();
RSA::PublicKey pubKey;
pubKey.Load(bytes);
RSASS<PSS, SHA256>::Verifier verifier(pubKey);
//Read signed message
string sigStr;
FileSource sigFile(sigFilename.c_str(), true, new Base64Decoder(new StringSink(sigStr)));
string combined(signedTxt);
combined.append(sigStr);
//Verify signature
StringSource(combined, true,
new SignatureVerificationFilter(
verifier, NULL,
SignatureVerificationFilter::THROW_EXCEPTION
)
);
cout << okMessage << endl;
}
catch(SignatureVerificationFilter::SignatureVerificationFailed &err)
{
cout << err.what() << endl;
return 0;
}
catch(CryptoPP::Exception &err)
{
cout << "Crypto error: " << err.what() << endl;
return 0;
}
catch(std::exception &err)
{
cout << "Verification error: " << err.what() << endl;
return 0;
}
return 1;
}
int VerifyEd25519SignatureText(string signedTxt, string sigFilename, string pubKeyFilename, string okMessage)
{
try
{
CryptoPP::ByteQueue bytes;
FileSource file(pubKeyFilename.c_str(), true, new Base64Decoder);
file.TransferTo(bytes);
bytes.MessageEnd();
ed25519PublicKey pubKey;
pubKey.Load(bytes);
ed25519::Verifier verifier(pubKey);
string sigStr;
FileSource sigFile(sigFilename.c_str(), true, new Base64Decoder(new StringSink(sigStr)));
string combined(signedTxt);
combined.append(sigStr);
StringSource(combined, true,
new SignatureVerificationFilter(
verifier, NULL,
SignatureVerificationFilter::THROW_EXCEPTION
)
);
cout << okMessage << endl;
}
catch(SignatureVerificationFilter::SignatureVerificationFailed &err)
{
cout << err.what() << endl;
return 0;
}
catch(CryptoPP::Exception &err)
{
cout << "Crypto error: " << err.what() << endl;
return 0;
}
catch(std::exception &err)
{
cout << "Verification error: " << err.what() << endl;
return 0;
}
return 1;
}
int VerifyLicense()
{
string signedTxt;
try
{
FileSource("license.txt", true, new StringSink(signedTxt));
}
catch(CryptoPP::Exception &err)
{
cout << "Crypto error: " << err.what() << endl;
return 0;
}
bool hasRsaLicense = FileExists("license-sig.txt") && FileExists("secondary-pubkey.txt");
bool hasEdLicense = FileExists("license-ed25519-sig.txt") && FileExists("secondary-ed25519-pubkey.txt");
if (hasRsaLicense == hasEdLicense)
{
cout << "error: expected exactly one license signature type" << endl;
return 0;
}
if (hasEdLicense)
{
return VerifyEd25519SignatureText(signedTxt, "license-ed25519-sig.txt", "secondary-ed25519-pubkey.txt", "License Ed25519 Signature OK");
}
return VerifyRsaSignatureText(signedTxt, "license-sig.txt", "secondary-pubkey.txt", "License RSA-PSS Signature OK");
}
int VerifySecondaryKey()
{
bool hasRsaSecondary = FileExists("secondary-pubkey.txt") && FileExists("secondary-pubkey-sig.txt") && FileExists("master-pubkey.txt");
bool hasEdSecondary = FileExists("secondary-ed25519-pubkey.txt") && FileExists("secondary-ed25519-pubkey-sig.txt") && FileExists("master-ed25519-pubkey.txt");
if (hasRsaSecondary == hasEdSecondary)
{
cout << "error: expected exactly one secondary key type" << endl;
return 0;
}
string signedTxt;
try
{
if (hasEdSecondary)
{
FileSource("secondary-ed25519-pubkey.txt", true, new StringSink(signedTxt));
}
else
{
FileSource("secondary-pubkey.txt", true, new StringSink(signedTxt));
}
}
catch(CryptoPP::Exception &err)
{
cout << "Crypto error: " << err.what() << endl;
return 0;
}
int ret;
if (hasEdSecondary)
ret = VerifyEd25519SignatureText(signedTxt, "secondary-ed25519-pubkey-sig.txt", "master-ed25519-pubkey.txt", "Secondary Ed25519 Key OK");
else
ret = VerifyRsaSignatureText(signedTxt, "secondary-pubkey-sig.txt", "master-pubkey.txt", "Secondary RSA-PSS Key OK");
if (ret)
cout << "Secondary Key ID: " << ComputeKeyId(signedTxt) << endl;
return ret;
}
int main()
{
int ret1 = VerifySecondaryKey();
int ret2 = VerifyLicense();
int ret3 = CheckRevocation();
return (ret1 && ret2 && ret3) ? 0 : 1;
}