open-license-manager
2014-09-13 1922d8c2cf7dcaacafd17394849577794b1f10eb
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
 * LicenseSigner.cpp (Windows)
 *
 *  Created on: Apr 6, 2014
 *      Author: devel
 */
 
#include <stdexcept>
#include <string.h>
#include <iostream>
#include <cmath>
 
#pragma comment(lib, "crypt32.lib")
 
#include <stdio.h>
#include <windows.h>
#include <Wincrypt.h>
#define MY_ENCODING_TYPE  (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING)
#include "../private-key.h"
 
#include "../LicenseSigner.h"
#include "../../library/base/logger.h"
namespace license {
    using namespace std;
 
    LicenseSigner::LicenseSigner() {
        os_initialize();
 
        if (CryptAcquireContext(
            &hProv,
            "license-manager2++",
            MS_ENHANCED_PROV,
            PROV_RSA_FULL, //CRYPT_NEWKEYSET
            0))    {
            LOG_DEBUG("CSP context acquired.");
        }
        else
        {
            LOG_ERROR("Error during CryptAcquireContextc %d.",GetLastError());
            throw exception();
        }
        if (CryptImportKey(
            hProv,
            PRIVATE_KEY,
            sizeof(PRIVATE_KEY),
            0,
            0,
            &hPubKey))
        {
            LOG_DEBUG("The key has been imported.\n");
        }
        else
        {
            LOG_ERROR("Private key import failed.\n");
            throw exception();
        }
        
    }
 
    LicenseSigner::LicenseSigner(const std::string& alternatePrimaryKey) {
        os_initialize();
    }
 
    string LicenseSigner::Opensslb64Encode(size_t slen, unsigned char* signature) {
 
        return NULL;
    }
 
    string LicenseSigner::signString(const string& license) {
 
        //-------------------------------------------------------------------
        // Declare and initialize variables.
        BYTE *pbBuffer = (BYTE *)license.c_str();
        DWORD dwBufferLen = strlen((char *)pbBuffer) + 1;
        HCRYPTHASH hHash;
 
        HCRYPTKEY hKey;
        BYTE *pbKeyBlob;
        BYTE *pbSignature;
        DWORD dwSigLen;
        DWORD dwBlobLen;
 
        //-------------------------------------------------------------------
        // Acquire a cryptographic provider context handle.
 
 
        //-------------------------------------------------------------------
        // Get the public at signature key. This is the public key
        // that will be used by the receiver of the hash to verify
        // the signature. In situations where the receiver could obtain the
        // sender's public key from a certificate, this step would not be
        // needed.
 
        if (CryptGetUserKey(
            hProv,
            AT_SIGNATURE,
            &hKey))
        {
            printf("The signature key has been acquired. \n");
        }
        else
        {
            printf("Error during CryptGetUserKey for signkey. %d", GetLastError());
        }
 
        //-------------------------------------------------------------------
        // Create the hash object.
 
        if (CryptCreateHash(
            hProv,
            CALG_SHA1,
            0,
            0,
            &hHash))
        {
            printf("Hash object created. \n");
        }
        else
        {
            LOG_ERROR("Error during CryptCreateHash.");
        }
        //-------------------------------------------------------------------
        // Compute the cryptographic hash of the buffer.
 
        if (CryptHashData(
            hHash,
            pbBuffer,
            dwBufferLen,
            0))
        {
            printf("The data buffer has been hashed.\n");
        }
        else
        {
            LOG_ERROR("Error during CryptHashData.");
        }
        //-------------------------------------------------------------------
        // Determine the size of the signature and allocate memory.
 
        dwSigLen = 0;
        if (CryptSignHash(
            hHash,
            AT_SIGNATURE,
            NULL,
            0,
            NULL,
            &dwSigLen))
        {
            printf("Signature length %d found.\n", dwSigLen);
        }
        else
        {
            LOG_ERROR("Error during CryptSignHash.");
        }
        //-------------------------------------------------------------------
        // Allocate memory for the signature buffer.
 
        if (pbSignature = (BYTE *)malloc(dwSigLen))
        {
            printf("Memory allocated for the signature.\n");
        }
        else
        {
            LOG_ERROR("Out of memory.");
        }
        //-------------------------------------------------------------------
        // Sign the hash object.
 
        if (CryptSignHash(
            hHash,
            AT_SIGNATURE,
            NULL,
            0,
            pbSignature,
            &dwSigLen))
        {
            printf("pbSignature is the hash signature.\n");
        }
        else
        {
            LOG_ERROR("Error during CryptSignHash.");
        }
        //-------------------------------------------------------------------
        // Destroy the hash object.
 
        if (hHash)
            CryptDestroyHash(hHash);
 
        printf("The hash object has been destroyed.\n");
        printf("The signing phase of this program is completed.\n\n");
 
        //-------------------------------------------------------------------
        // In the second phase, the hash signature is verified.
        // This would most often be done by a different user in a
        // separate program. The hash, signature, and the PUBLICKEYBLOB
        // would be read from a file, an email message, 
        // or some other source.
 
        // Here, the original pbBuffer, pbSignature, szDescription. 
        // pbKeyBlob, and their lengths are used.
 
        // The contents of the pbBuffer must be the same data 
        // that was originally signed.
 
        //-------------------------------------------------------------------
        // Get the public key of the user who created the digital signature 
        // and import it into the CSP by using CryptImportKey. This returns
        // a handle to the public key in hPubKey.
 
        /*if (CryptImportKey(
            hProv,
            pbKeyBlob,
            dwBlobLen,
            0,
            0,
            &hPubKey))
        {
            printf("The key has been imported.\n");
        }
        else
        {
            MyHandleError("Public key import failed.");
        }
        //-------------------------------------------------------------------
        // Create a new hash object.
 
        if (CryptCreateHash(
            hProv,
            CALG_MD5,
            0,
            0,
            &hHash))
        {
            printf("The hash object has been recreated. \n");
        }
        else
        {
            MyHandleError("Error during CryptCreateHash.");
        }
        //-------------------------------------------------------------------
        // Compute the cryptographic hash of the buffer.
 
        if (CryptHashData(
            hHash,
            pbBuffer,
            dwBufferLen,
            0))
        {
            printf("The new hash has been created.\n");
        }
        else
        {
            MyHandleError("Error during CryptHashData.");
        }
        //-------------------------------------------------------------------
        // Validate the digital signature.
 
        if (CryptVerifySignature(
            hHash,
            pbSignature,
            dwSigLen,
            hPubKey,
            NULL,
            0))
        {
            printf("The signature has been verified.\n");
        }
        else
        {
            printf("Signature not validated!\n");
        }
        //-------------------------------------------------------------------
        // Free memory to be used to store signature.
 
        if (pbSignature)
            free(pbSignature);
 
        //-------------------------------------------------------------------
        // Destroy the hash object.
 
        if (hHash)
            CryptDestroyHash(hHash);*/
 
        //-------------------------------------------------------------------
        // Release the provider handle.
 
        if (hProv)
            CryptReleaseContext(hProv, 0);
        return string("");
    } //  End of main
 
 
 
 
 
    void LicenseSigner::signLicense(FullLicenseInfo& licenseInfo) {
        string license = licenseInfo.printForSign();
        string signature = signString(license);
        licenseInfo.license_signature = signature;
    }
 
    LicenseSigner::~LicenseSigner() {
 
    }
 
} /* namespace license */