-
Notifications
You must be signed in to change notification settings - Fork 4
/
uid.cpp
157 lines (138 loc) · 5.52 KB
/
uid.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
#include <string>
#include <iostream>
#include <cassert>
#include <stdint.h>
#include "cryptopp/aes.h"
#include "cryptopp/modes.h"
#include "cryptopp/osrng.h"
#include "cryptopp/rsa.h"
#include "cryptopp/base64.h"
#include "cryptopp/integer.h"
#include "cryptopp/files.h"
#include <json/json.h>
#include "machine_info.h"
#if !defined(UID_PUBKEY_BYTES)
# error "You must #define UID_PUBKEY_BYTES as 256 sized byte list, eg. '130,99,...'"
#endif
int main(int argc, char *argv[])
{
if (argc < 2)
{
std::cerr << "failure: expected the session string as first argument" << std::endl;
return 1;
}
Json::Value root;
root["session"] = argv[1];
if (!machine_info_init())
{
std::cerr << "Error initialising machine_info" << std::endl;
}
root["machine"]["uuid"] = machine_info_uuid();
root["machine"]["model"] = machine_info_model();
root["machine"]["manufacturer"] = machine_info_manufacturer();
root["machine"]["display"]["width"] = machine_info_display_width();
root["machine"]["display"]["height"] = machine_info_display_height();
root["machine"]["memory"]["serial0"] = machine_info_memory_serial0();
root["machine"]["motherboard"]["vendor"] = machine_info_motherboard_vendor();
root["machine"]["motherboard"]["name"] = machine_info_motherboard_name();
root["machine"]["disks"]["controller_id"] = machine_info_disks_controllerid();
root["machine"]["disks"]["vserial"] = machine_info_disks_vserial();
root["machine"]["bios"]["manufacturer"] = machine_info_bios_manufacturer();
root["machine"]["bios"]["smbbversion"] = machine_info_bios_smbbversion();
root["machine"]["bios"]["serial"] = machine_info_bios_serial();
root["machine"]["bios"]["description"] = machine_info_bios_description();
root["machine"]["bios"]["date"] = machine_info_bios_date();
root["machine"]["bios"]["version"] = machine_info_bios_version();
root["machine"]["processor"]["name"] = machine_info_processor_name();
root["machine"]["processor"]["id"] = machine_info_processor_id();
root["machine"]["os"]["type"] = machine_info_os_type();
root["machine"]["os"]["version"] = machine_info_os_version();
machine_info_free();
//std::cout << root << std::endl;
Json::StreamWriterBuilder json_builder;
std::string json_string = std::string("2") + Json::writeString(json_builder, root);
//std::cout << json_string << "<-END" << std::endl;
try
{
CryptoPP::AutoSeededRandomPool rng;
#define AES_KEY_SIZE 16
#define AES_BLOCK_SIZE 16
/* Generate random AES Key and Initialisation vector */
CryptoPP::SecByteBlock aes_key( AES_KEY_SIZE );
CryptoPP::SecByteBlock iv( AES_BLOCK_SIZE );
rng.GenerateBlock(aes_key, AES_KEY_SIZE);
rng.GenerateBlock(iv, AES_BLOCK_SIZE);
/* Base64 encode IV */
std::string iv_b64;
{
CryptoPP::StringSource s(iv, AES_BLOCK_SIZE, true,
new CryptoPP::Base64Encoder(
new CryptoPP::StringSink(iv_b64), false
)
);
}
/* the server expects 24 bytes */
assert(iv_b64.size() == 24);
/* create the RSA Public key from the UID_PUBKEY_BYTES #define */
uint8_t pubkey_bytes[256] = {UID_PUBKEY_BYTES};
CryptoPP::RSA::PublicKey publicKey;
CryptoPP::Integer modulus;
modulus.Decode(pubkey_bytes, 256);
publicKey.Initialize(modulus, 65537);
/* encrypt the AES key and encode the encryptes key to base64 */
std::string aes_key_encrypted_base64;
{
CryptoPP::RSAES_PKCS1v15_Encryptor e( publicKey );
CryptoPP::StringSource s( aes_key, AES_KEY_SIZE, true,
new CryptoPP::PK_EncryptorFilter(rng, e,
new CryptoPP::Base64Encoder(
new CryptoPP::StringSink(aes_key_encrypted_base64), false /*insertLineBreaks*/
)
)
);
}
/* now encrypt the JSON string with AES */
std::string json_string_encrypted_b64;
{
CryptoPP::CBC_Mode< CryptoPP::AES >::Encryption e;
e.SetKeyWithIV( aes_key, AES_KEY_SIZE, iv );
CryptoPP::StringSource s( json_string, true,
new CryptoPP::StreamTransformationFilter( e,
new CryptoPP::Base64Encoder(
new CryptoPP::StringSink( json_string_encrypted_b64 ), false /*insertLineBreaks*/
)
)
);
}
/* the number of padding bytes (AES can only encrypt a multiple of 16 bytes) */
unsigned char paddingSize = 16 - (json_string.size() % 16);
{
/* the final bytearray consists of the
1 byte paddingSize
24 bytes IV (base64)
X bytes AES encrypted JSON (base64)
344 bytes RSA encrypted AES key (base64) */
CryptoPP::Base64Encoder b(
new CryptoPP::FileSink(std::cout), false /*insertLineBreaks*/
);
b.Put(paddingSize);
#ifdef WIN32FAFUID
b.Put(reinterpret_cast<const byte*>(iv_b64.c_str()), iv_b64.size());
b.Put(reinterpret_cast<const byte*>(json_string_encrypted_b64.c_str()), json_string_encrypted_b64.size());
b.Put(reinterpret_cast<const byte*>(aes_key_encrypted_base64.c_str()), aes_key_encrypted_base64.size());
#else
b.Put(reinterpret_cast<const CryptoPP::byte*>(iv_b64.c_str()), iv_b64.size());
b.Put(reinterpret_cast<const CryptoPP::byte*>(json_string_encrypted_b64.c_str()), json_string_encrypted_b64.size());
b.Put(reinterpret_cast<const CryptoPP::byte*>(aes_key_encrypted_base64.c_str()), aes_key_encrypted_base64.size());
#endif
b.MessageEnd();
}
}
catch(std::exception& e)
{
std::cerr << "caught crypto exception of type " << typeid(e).name() << std::endl;
std::cerr << "what: " << e.what() << std::endl;
return 1;
}
return 0;
}