-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathgenlicense.cpp
More file actions
174 lines (150 loc) · 4.74 KB
/
Copy pathgenlicense.cpp
File metadata and controls
174 lines (150 loc) · 4.74 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
// Generate a plain-text license file and sign it with the detected secondary key type.
//g++ genlicense.cpp -lcrypto++ -o genlicense
#include <string>
#include <fstream>
#include <exception>
#include <limits>
using namespace std;
#include "readpass.hpp"
#include <crypto++/rsa.h>
#include <crypto++/osrng.h>
#include <crypto++/base64.h>
#include <crypto++/files.h>
#include <crypto++/aes.h>
#include <crypto++/gcm.h>
#include <crypto++/filters.h>
#include <crypto++/pwdbased.h>
#include <crypto++/pssr.h>
#include <crypto++/sha.h>
#include <crypto++/xed25519.h>
using namespace CryptoPP;
const unsigned int PBKDF2_ITERATIONS = 200000;
bool FileExists(const char *filename)
{
ifstream file(filename);
return file.good();
}
string SaltFilename(const char *encFilename)
{
return string(encFilename) + ".salt";
}
SecByteBlock DeriveEncryptionKey(string pass, string salt)
{
SecByteBlock key(AES::DEFAULT_KEYLENGTH);
PKCS5_PBKDF2_HMAC<SHA256> pbkdf;
pbkdf.DeriveKey(
key,
key.size(),
0,
(const CryptoPP::byte *)pass.data(),
pass.size(),
(const CryptoPP::byte *)salt.data(),
salt.size(),
PBKDF2_ITERATIONS);
return key;
}
string ReadEncrypted(string encFilename, string ivFilename, string pass)
{
//Read private key
string encPrivKey;
StringSink encPrivKeySink(encPrivKey);
FileSource file(encFilename.c_str(), true, new Base64Decoder);
file.CopyTo(encPrivKeySink);
//Read initialization vector
string iv;
FileSource file2(ivFilename.c_str(), true, new Base64Decoder(new StringSink(iv)));
//Read password salt
string salt;
FileSource saltFile(SaltFilename(encFilename.c_str()).c_str(), true, new Base64Decoder(new StringSink(salt)));
SecByteBlock key = DeriveEncryptionKey(pass, salt);
//Decrypt private key
string plaintext;
GCM<AES>::Decryption decryption;
decryption.SetKeyWithIV(key, key.size(), (const CryptoPP::byte *)iv.data(), iv.size());
StringSource(encPrivKey, true, new AuthenticatedDecryptionFilter(decryption, new StringSink(plaintext)));
return plaintext;
}
void SignLicense(AutoSeededRandomPool &rng, string strContents, string pass)
{
string privateKeyStr = ReadEncrypted("secondary-privkey-enc.txt", "secondary-privkey-iv.txt", pass);
StringSource privateKeySrc(privateKeyStr, true, NULL);
//Decode key
RSA::PrivateKey privateKey;
privateKey.Load(privateKeySrc);
//Sign message
RSASS<PSS, SHA256>::Signer privkey(privateKey);
SecByteBlock sbbSignature(privkey.SignatureLength());
privkey.SignMessage(
rng,
(CryptoPP::byte const*) strContents.data(),
strContents.size(),
sbbSignature);
//Save result
FileSink out("license.txt");
out.Put((CryptoPP::byte const*) strContents.data(), strContents.size());
//Save result
Base64Encoder enc(new FileSink("license-sig.txt"));
enc.Put(sbbSignature, sbbSignature.size());
enc.MessageEnd();
}
void SignLicenseEd25519(AutoSeededRandomPool &rng, string strContents, string pass)
{
string privateKeyStr = ReadEncrypted("secondary-ed25519-privkey-enc.txt", "secondary-ed25519-privkey-iv.txt", pass);
StringSource privateKeySrc(privateKeyStr, true, NULL);
ed25519PrivateKey privateKey;
privateKey.Load(privateKeySrc);
ed25519::Signer privkey(privateKey);
SecByteBlock sbbSignature(privkey.SignatureLength());
privkey.SignMessage(
rng,
(CryptoPP::byte const*) strContents.data(),
strContents.size(),
sbbSignature);
Base64Encoder enc(new FileSink("license-ed25519-sig.txt"));
enc.Put(sbbSignature, sbbSignature.size());
enc.MessageEnd();
}
int main()
{
cout << "Enter existing secondary key password" << endl;
string pass = ReadPassword();
AutoSeededRandomPool rng;
cout << "Enter license text" << endl;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
string licenseText;
getline(cin, licenseText);
try
{
bool hasRsaSecondary = FileExists("secondary-privkey-enc.txt") && FileExists("secondary-privkey-enc.txt.salt") && FileExists("secondary-pubkey.txt");
bool hasEdSecondary = FileExists("secondary-ed25519-privkey-enc.txt") && FileExists("secondary-ed25519-privkey-enc.txt.salt") && FileExists("secondary-ed25519-pubkey.txt");
if (hasRsaSecondary == hasEdSecondary)
{
cout << "error: expected exactly one secondary key type" << endl;
return 1;
}
if (hasEdSecondary)
{
FileSink out("license.txt");
out.Put((CryptoPP::byte const*) licenseText.data(), licenseText.size());
SignLicenseEd25519(rng, licenseText, pass);
cout << "Created: license.txt" << endl;
cout << "Created: license-ed25519-sig.txt" << endl;
}
else
{
SignLicense(rng, licenseText, pass);
cout << "Created: license.txt" << endl;
cout << "Created: license-sig.txt" << endl;
}
}
catch(CryptoPP::Exception &err)
{
cout << "Crypto error: " << err.what() << endl;
return 1;
}
catch(std::exception &err)
{
cout << "error: " << err.what() << endl;
return 1;
}
}