-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.cpp
308 lines (254 loc) · 7.93 KB
/
auth.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
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
307
308
/*
Licensed under the MIT License.
*/
#include "quiccat.h"
#include "openssl/err.h"
#include "openssl/evp.h"
#include "openssl/pkcs12.h"
#include "openssl/rand.h"
#include "openssl/x509v3.h"
#ifndef ED448_KEYLEN
#define ED448_KEYLEN 57
#endif
const int PBKDFIterations = 15000;
const int SigningSaltLength = 64;
const char* CertName = "quiccat";
void
PrintHexBuffer(const char* const Label, const uint8_t*const Buf, uint32_t Len)
{
Log() << Label << ": ";
for(unsigned i = 0; i < Len; i++) {
printf("%02x", (unsigned char)Buf[i]);
}
Log() << std::endl;
}
EVP_PKEY*
QcGenerateSigningKey(
_In_ const std::string& Password,
_In_ const uint8_t* const Salt,
_In_ const uint32_t SaltLen)
{
EVP_PKEY* SigningKey = nullptr;
uint8_t SigningKeyBytes[ED448_KEYLEN];
int Ret = PKCS5_PBKDF2_HMAC(Password.c_str(), (int)Password.length(), Salt, SaltLen, PBKDFIterations, EVP_sha512(), sizeof(SigningKeyBytes), SigningKeyBytes);
if (Ret != 1) {
Log() << "Failed to run PBKDF2!\n";
goto Error;
}
SigningKey = EVP_PKEY_new_raw_private_key(EVP_PKEY_ED448, nullptr, SigningKeyBytes, sizeof(SigningKeyBytes));
if (SigningKey == nullptr) {
Log() << "Failed to create signing key!\n";
ERR_print_errors_cb([](const char* str, size_t /*len*/, void* /*u*/){Log() << str << std::endl; return 1;}, nullptr);
goto Error;
}
Error:
CxPlatSecureZeroMemory(SigningKeyBytes, sizeof(SigningKeyBytes));
return SigningKey;
}
bool
QcGenerateAuthCertificate(
_In_ const std::string& Password,
_Out_ std::unique_ptr<uint8_t[]>& Pkcs12,
_Out_ uint32_t& Pkcs12Length)
{
EVP_PKEY* PrivateKey = nullptr;
EVP_PKEY* SigningKey = nullptr;
X509* Cert = nullptr;
X509_NAME* Name = nullptr;
BIGNUM* SaltBn = nullptr;
ASN1_INTEGER* SerialNumber = nullptr;
PKCS12* NewPkcs12 = nullptr;
uint8_t* Pkcs12Buffer = nullptr;
uint8_t* Pkcs12BufferPtr = nullptr;
uint8_t Salt[SigningSaltLength];
int Ret = 0;
bool Result = false;
Pkcs12 = nullptr;
Pkcs12Length = 0;
EVP_PKEY_CTX *KeyContext = EVP_PKEY_CTX_new_id(EVP_PKEY_ED448, NULL);
if (KeyContext == nullptr) {
Log() << "Failed to allocate Key context!\n";
goto Error;
}
Ret = EVP_PKEY_keygen_init(KeyContext);
if (Ret != 1) {
Log() << "Keygen init failed!\n";
goto Error;
}
Ret = EVP_PKEY_keygen(KeyContext, &PrivateKey);
if (Ret != 1) {
Log() << "Keygen failed!\n";
goto Error;
}
Ret = RAND_bytes(Salt, sizeof(Salt));
if (Ret != 1) {
Log() << "Failed to get random bytes!\n";
goto Error;
}
Cert = X509_new();
if (Cert == nullptr) {
Log() << "Failed to allocate X509!\n";
goto Error;
}
Ret = X509_set_version(Cert, 2);
if (Ret != 1) {
Log() << "Failed to set certificate version!\n";
goto Error;
}
SaltBn = BN_bin2bn(Salt, sizeof(Salt), nullptr);
if (SaltBn == nullptr) {
Log() << "Failed to convert Salt to BIGNUM!\n";
goto Error;
}
SerialNumber = BN_to_ASN1_INTEGER(SaltBn, nullptr);
if (SerialNumber == nullptr) {
Log() << "Failed to allocate serial number!\n";
goto Error;
}
Ret = X509_set_serialNumber(Cert, SerialNumber);
if (Ret != 1) {
Log() << "Failed to set serial number!\n";
goto Error;
}
X509_gmtime_adj(X509_getm_notBefore(Cert), -300);
X509_gmtime_adj(X509_getm_notAfter(Cert), 31536000L);
Ret = X509_set_pubkey(Cert, PrivateKey);
if (Ret != 1) {
Log() << "Failed to set public key on cert!\n";
goto Error;
}
Name = X509_get_subject_name(Cert);
if (Name == nullptr) {
Log() << "Failed to allocate subject name!\n";
goto Error;
}
Ret = X509_NAME_add_entry_by_txt(Name, "CN", MBSTRING_ASC, (unsigned char*)CertName, -1, -1, 0);
if (Ret != 1) {
Log() << "Failed to set subject name!\n";
goto Error;
}
Ret = X509_set_issuer_name(Cert, Name);
if (Ret != 1) {
Log() << "Failed to set issuer name!\n";
goto Error;
}
SigningKey = QcGenerateSigningKey(Password, Salt, sizeof(Salt));
if (SigningKey == nullptr) {
goto Error;
}
Ret = X509_sign(Cert, SigningKey, nullptr);
if (Ret == 0) {
Log() << "Failed to sign certificate!\n";
ERR_print_errors_cb([](const char* str, size_t /*len*/, void* /*u*/){Log() << str << std::endl; return 1;}, nullptr);
goto Error;
}
NewPkcs12 = PKCS12_create("", CertName, PrivateKey, Cert, nullptr, -1, -1, 0, 0, 0);
if (NewPkcs12 == nullptr) {
Log() << "Failed to create new PKCS12!\n";
goto Error;
}
Ret = i2d_PKCS12(NewPkcs12, nullptr);
if (Ret <= 0) {
Log() << "Failed to get export buffer size of NewPkcs12!\n";
goto Error;
}
Pkcs12Length = Ret;
Pkcs12Buffer = new (std::nothrow) uint8_t[Pkcs12Length];
if (Pkcs12Buffer == nullptr) {
Log() << "Failed to allocate " << Pkcs12Length << " bytes for Pkcs12!\n";
goto Error;
}
Pkcs12BufferPtr = Pkcs12Buffer;
Ret = i2d_PKCS12(NewPkcs12, &Pkcs12BufferPtr);
if (Ret < 0) {
Log() << "Failed to export NewPkcs12!\n";
goto Error;
}
if ((uint32_t)Ret != Pkcs12Length) {
Log() << "Pkcs12 export length changed between calls!\n";
goto Error;
}
Result = true;
Pkcs12.reset(Pkcs12Buffer);
Pkcs12Buffer = nullptr;
Error:
if (Pkcs12Buffer != nullptr) {
delete[] Pkcs12Buffer;
}
if (NewPkcs12 != nullptr) {
PKCS12_free(NewPkcs12);
}
if (SigningKey != nullptr) {
EVP_PKEY_free(SigningKey);
}
if (SerialNumber != nullptr) {
ASN1_INTEGER_free(SerialNumber);
}
if (SaltBn != nullptr) {
BN_free(SaltBn);
}
if (Cert != nullptr) {
X509_free(Cert);
}
if (PrivateKey != nullptr) {
EVP_PKEY_free(PrivateKey);
}
if (KeyContext != nullptr) {
EVP_PKEY_CTX_free(KeyContext);
}
return Result;
}
bool
QcVerifyCertificate(
_In_ const std::string& Password,
_In_ QUIC_CERTIFICATE* Cert)
{
EVP_PKEY* SigningKey = nullptr;
X509* PeerCert = (X509*)Cert;
BIGNUM* SaltBn = nullptr;
uint8_t Salt[SigningSaltLength];
int Ret = 0;
bool Result = false;
const ASN1_INTEGER* const SerialNumber = X509_get_serialNumber(PeerCert);
SaltBn = ASN1_INTEGER_to_BN(SerialNumber, nullptr);
if (SaltBn == nullptr) {
Log() << "Failed to convert ASN SerialNumber to BIGNUM Salt!\n";
goto Error;
}
if (BN_num_bytes(SaltBn) > (int)sizeof(Salt)) {
Log() << "Serial number is not correct size! " << BN_num_bytes(SaltBn) << " vs " << sizeof(Salt) << std::endl;
goto Error;
}
Ret = BN_bn2binpad(SaltBn, Salt, sizeof(Salt));
if (Ret != sizeof(Salt)) {
Log() << "BIGNUM conversion to binary is wrong size! " << Ret << " vs " << sizeof(Salt) << std::endl;
goto Error;
}
SigningKey = QcGenerateSigningKey(Password, Salt, sizeof(Salt));
if (SigningKey == nullptr) {
goto Error;
}
Ret = X509_verify(PeerCert, SigningKey);
if (Ret == 1) {
Result = true;
} else if (Ret == 0) {
Log() << "Certificate failed signature verification!\n";
goto Error;
} else if (Ret == -1) {
Log() << "Certificate signature is malformed!\n";
ERR_print_errors_cb([](const char* str, size_t /*len*/, void* /*u*/){Log() << str << std::endl; return 1;}, nullptr);
goto Error;
} else {
Log() << "Certificate failed validation for another reason!\n";
ERR_print_errors_cb([](const char* str, size_t /*len*/, void* /*u*/){Log() << str << std::endl; return 1;}, nullptr);
goto Error;
}
Error:
if (SaltBn != nullptr) {
BN_free(SaltBn);
}
if (SigningKey != nullptr) {
EVP_PKEY_free(SigningKey);
}
return Result;
}