forked from cloud-security-research/sgx-ra-tls
-
Notifications
You must be signed in to change notification settings - Fork 1
/
wolfssl.patch
504 lines (482 loc) · 18.5 KB
/
wolfssl.patch
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
diff --git a/src/internal.c b/src/internal.c
index a6989c4..9036910 100644
--- a/src/internal.c
+++ b/src/internal.c
@@ -9777,11 +9777,14 @@ static int DoHandShakeMsg(WOLFSSL* ssl, byte* input, word32* inOutIdx,
ret = DoHandShakeMsgType(ssl, input, inOutIdx, type, size, totalSz);
}
else {
- if (inputLength + ssl->arrays->pendingMsgOffset
- > ssl->arrays->pendingMsgSz) {
+ word32 pendSz =
+ ssl->arrays->pendingMsgSz - ssl->arrays->pendingMsgOffset;
- return BUFFER_ERROR;
- }
+ /* Catch the case where there may be the remainder of a fragmented
+ * handshake message and the next handshake message in the same
+ * record. */
+ if (inputLength > pendSz)
+ inputLength = pendSz;
XMEMCPY(ssl->arrays->pendingMsg + ssl->arrays->pendingMsgOffset,
input + *inOutIdx, inputLength);
@@ -9790,13 +9793,11 @@ static int DoHandShakeMsg(WOLFSSL* ssl, byte* input, word32* inOutIdx,
if (ssl->arrays->pendingMsgOffset == ssl->arrays->pendingMsgSz)
{
- word32 idx = 0;
+ word32 idx = HANDSHAKE_HEADER_SZ;
ret = DoHandShakeMsgType(ssl,
- ssl->arrays->pendingMsg
- + HANDSHAKE_HEADER_SZ,
+ ssl->arrays->pendingMsg,
&idx, ssl->arrays->pendingMsgType,
- ssl->arrays->pendingMsgSz
- - HANDSHAKE_HEADER_SZ,
+ ssl->arrays->pendingMsgSz - idx,
ssl->arrays->pendingMsgSz);
#ifdef WOLFSSL_ASYNC_CRYPT
if (ret == WC_PENDING_E) {
diff --git a/wolfcrypt/src/asn.c b/wolfcrypt/src/asn.c
index e234253..ba9df53 100644
--- a/wolfcrypt/src/asn.c
+++ b/wolfcrypt/src/asn.c
@@ -19,6 +19,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
+#include <assert.h>
#ifdef HAVE_CONFIG_H
#include <config.h>
@@ -61,6 +62,7 @@ ASN Options:
#include <wolfssl/wolfcrypt/pwdbased.h>
#include <wolfssl/wolfcrypt/des3.h>
#include <wolfssl/wolfcrypt/logging.h>
+#include <wolfssl/wolfcrypt/wc_port.h>
#include <wolfssl/wolfcrypt/random.h>
#include <wolfssl/wolfcrypt/hash.h>
@@ -6346,7 +6348,10 @@ int wc_DerToPemEx(const byte* der, word32 derSz, byte* output, word32 outSz,
/* USER RSA ifdef portions used instead of refactor in consideration for
possible fips build */
/* Write a public RSA key to output */
-static int SetRsaPublicKey(byte* output, RsaKey* key,
+#if !defined(WOLFSSL_SGX_ATTESTATION)
+static
+#endif
+int SetRsaPublicKey(byte* output, RsaKey* key,
int outLen, int with_header)
{
#ifdef WOLFSSL_SMALL_STACK
@@ -6710,6 +6715,20 @@ typedef struct DerCert {
byte extKeyUsage[MAX_EXTKEYUSAGE_SZ]; /* Extended Key Usage extension */
byte certPolicies[MAX_CERTPOL_NB*MAX_CERTPOL_SZ]; /* Certificate Policies */
#endif
+#ifdef WOLFSSL_SGX_ATTESTATION
+ byte iasSigCACert[2048];
+ byte iasSigCert[2048];
+ byte iasSig[2048];
+ byte iasAttestationReport[2048];
+ byte quote[2048];
+ byte pckCrt[2048];
+ byte pckSignChain[4096];
+ byte tcbInfo[2048];
+ byte tcbSignChain[4096];
+ byte qeIdentity[1024];
+ byte rootCaCrl[1024];
+ byte pckCrl[1024];
+#endif
#ifdef WOLFSSL_CERT_REQ
byte attrib[MAX_ATTRIB_SZ]; /* Cert req attributes encoded */
#endif
@@ -6732,6 +6751,20 @@ typedef struct DerCert {
int extKeyUsageSz; /* encoded ExtendedKeyUsage extension length */
int certPoliciesSz; /* encoded CertPolicies extension length*/
#endif
+#ifdef WOLFSSL_SGX_ATTESTATION
+ int iasSigCACertSz;
+ int iasSigCertSz;
+ int iasSigSz;
+ int iasAttestationReportSz;
+ int quoteSz;
+ int pckCrtSz;
+ int pckSignChainSz;
+ int tcbInfoSz;
+ int tcbSignChainSz;
+ int qeIdentitySz;
+ int rootCaCrlSz;
+ int pckCrlSz;
+#endif
#ifdef WOLFSSL_ALT_NAMES
int altNamesSz; /* encoded AltNames extension length */
#endif
@@ -7104,13 +7137,24 @@ static int SetValidity(byte* output, int daysValid)
/* subtract 1 day for more compliance */
local.tm_mday -= 1;
+#if !defined(WOLFSSL_SGX_ATTESTATION)
normalTime = mktime(&local);
+#endif
RebuildTime(&normalTime, &local);
/* adjust */
local.tm_year += 1900;
local.tm_mon += 1;
+#ifdef WOLFSSL_SGX_ATTESTATION
+ bzero(&local, sizeof(local));
+ local.tm_year = 2017;
+ local.tm_mday = 5;
+ local.tm_wday = 2;
+ local.tm_mon = 12;
+ local.tm_hour = 10;
+#endif
+
SetTime(&local, before + beforeSz);
beforeSz += ASN_GEN_TIME_SZ;
@@ -7121,13 +7165,24 @@ static int SetValidity(byte* output, int daysValid)
/* add daysValid */
local.tm_mday += daysValid;
+#if !defined(WOLFSSL_SGX_ATTESTATION)
normalTime = mktime(&local);
+#endif
RebuildTime(&normalTime, &local);
/* adjust */
local.tm_year += 1900;
local.tm_mon += 1;
+#ifdef WOLFSSL_SGX_ATTESTATION
+ bzero(&local, sizeof(local));
+ local.tm_year = 2019;
+ local.tm_mday = 5;
+ local.tm_wday = 4;
+ local.tm_mon = 12;
+ local.tm_hour = 10;
+#endif
+
SetTime(&local, after + afterSz);
afterSz += ASN_GEN_TIME_SZ;
@@ -7445,7 +7500,16 @@ static int SetKeyUsage(byte* output, word32 outSz, word16 input)
ku, idx);
}
-static int SetOjectIdValue(byte* output, word32 outSz, int* idx,
+#if !defined(WOLFSSL_SGX_ATTESTATION)
+static
+#endif
+int SetOjectIdValue(byte* output, word32 outSz, int* idx,
+ const byte* oid, word32 oidSz);
+
+#if !defined(WOLFSSL_SGX_ATTESTATION)
+static
+#endif
+int SetOjectIdValue(byte* output, word32 outSz, int* idx,
const byte* oid, word32 oidSz)
{
/* verify room */
@@ -7459,6 +7523,53 @@ static int SetOjectIdValue(byte* output, word32 outSz, int* idx,
return 0;
}
+#ifdef WOLFSSL_SGX_ATTESTATION
+static int SetSGXExt(byte* output, word32 outSz, const byte* oid, int oidSz, const byte *input, word32 length)
+{
+ byte ext_len[1 + MAX_LENGTH_SZ];
+ byte ext_enc_len[MAX_LENGTH_SZ];
+ byte oid_enc[16];
+ int idx = 0, ext_lenSz;
+ int oid_enc_lenSz = 0;
+
+ if (output == NULL || input == NULL || oid == NULL)
+ return BAD_FUNC_ARG;
+
+ ext_lenSz = SetOctetString(length, ext_len);
+
+ SetLength(length + ext_lenSz, ext_enc_len);
+
+ SetOjectIdValue(oid_enc, sizeof(oid_enc), &oid_enc_lenSz, oid, oidSz);
+
+ if (outSz < 3)
+ return BUFFER_E;
+
+ idx = SetSequence(length + oid_enc_lenSz + ext_lenSz,
+ output);
+
+ if ((idx + length + oid_enc_lenSz + ext_lenSz) > outSz)
+ return BUFFER_E;
+
+ /* put oid */
+ XMEMCPY(output+idx, oid_enc, oid_enc_lenSz);
+ idx += oid_enc_lenSz;
+
+ /* put encoded len */
+ /* XMEMCPY(output+idx, ext_enc_len, ext_enc_lenSz); */
+ /* idx += ext_enc_lenSz; */
+
+ /* put octet header */
+ XMEMCPY(output+idx, ext_len, ext_lenSz);
+ idx += ext_lenSz;
+
+ /* put value */
+ XMEMCPY(output+idx, input, length);
+ idx += length;
+
+ return idx;
+}
+#endif
+
/* encode Extended Key Usage (RFC 5280 4.2.1.12), return total bytes written */
static int SetExtKeyUsage(byte* output, word32 outSz, byte input)
{
@@ -8055,6 +8166,103 @@ static int EncodeCert(Cert* cert, DerCert* der, RsaKey* rsaKey, ecc_key* eccKey,
der->certPoliciesSz = 0;
#endif /* WOLFSSL_CERT_EXT */
+#ifdef WOLFSSL_SGX_ATTESTATION
+ if (cert->iasSigCACertSz > 0 &&
+ cert->iasSigCertSz > 0 &&
+ cert->iasSigSz > 0 &&
+ cert->iasAttestationReportSz > 0) {
+
+// 1.2.840.113741.1337.*
+#define OID(N) {0x2A, 0x86, 0x48, 0x86, 0xF8, 0x4D, 0x8A, 0x39, (N)}
+
+ unsigned char iasAttestationReportOid[] = OID(0x02);
+ unsigned char iasSigCACertOid[] = OID(0x03);
+ unsigned char iasSigCertOid[] = OID(0x04);
+ unsigned char iasSigOid[] = OID(0x05);
+
+ der->iasSigCACertSz = SetSGXExt(der->iasSigCACert, sizeof(der->iasSigCACert),
+ iasSigCACertOid, sizeof(iasSigCACertOid),
+ cert->iasSigCACert, cert->iasSigCACertSz);
+
+ der->iasSigCertSz = SetSGXExt(der->iasSigCert, sizeof(der->iasSigCert),
+ iasSigCertOid, sizeof(iasSigCertOid),
+ cert->iasSigCert, cert->iasSigCertSz);
+
+ der->iasSigSz = SetSGXExt(der->iasSig, sizeof(der->iasSig),
+ iasSigOid, sizeof(iasSigOid),
+ cert->iasSig, cert->iasSigSz);
+
+ der->iasAttestationReportSz = SetSGXExt(der->iasAttestationReport,
+ sizeof(der->iasAttestationReport),
+ iasAttestationReportOid,
+ sizeof(iasAttestationReportOid),
+ cert->iasAttestationReport,
+ cert->iasAttestationReportSz);
+
+ der->extensionsSz += der->iasAttestationReportSz +
+ der->iasSigCACertSz +
+ der->iasSigCertSz +
+ der->iasSigSz;
+ }
+
+ if (cert->quoteSz > 0 && cert->pckCrtSz > 0 && cert->pckSignChainSz > 0 &&
+ cert->tcbInfoSz > 0 && cert->tcbSignChainSz > 0 && cert->qeIdentitySz > 0 &&
+ cert->rootCaCrlSz > 0 && cert->pckCrlSz > 0) {
+
+ const unsigned char quoteOid[] = OID(0x06);
+ der->quoteSz = SetSGXExt(der->quote, sizeof(der->quote),
+ quoteOid, sizeof(quoteOid),
+ cert->quote, cert->quoteSz);
+ assert(der->quoteSz > 0);
+
+ const unsigned char pckCrtOid[] = OID(0x07);
+ der->pckCrtSz = SetSGXExt(der->pckCrt, sizeof(der->pckCrt),
+ pckCrtOid, sizeof(pckCrtOid),
+ cert->pckCrt, cert->pckCrtSz);
+ assert(der->pckCrtSz > 0);
+
+ const unsigned char pckSignChainOid[] = OID(0x08);
+ der->pckSignChainSz = SetSGXExt(der->pckSignChain, sizeof(der->pckSignChain),
+ pckSignChainOid, sizeof(pckSignChainOid),
+ cert->pckSignChain, cert->pckSignChainSz);
+ assert(der->pckSignChainSz > 0);
+
+ const unsigned char tcbInfoOid[] = OID(0x09);
+ der->tcbInfoSz = SetSGXExt(der->tcbInfo, sizeof(der->tcbInfo),
+ tcbInfoOid, sizeof(tcbInfoOid),
+ cert->tcbInfo, cert->tcbInfoSz);
+ assert(der->tcbInfoSz > 0);
+
+ const unsigned char tcbSignChainOid[] = OID(0x0a);
+ der->tcbSignChainSz = SetSGXExt(der->tcbSignChain, sizeof(der->tcbSignChain),
+ tcbSignChainOid, sizeof(tcbSignChainOid),
+ cert->tcbSignChain, cert->tcbSignChainSz);
+ assert(der->tcbSignChainSz > 0);
+
+ const unsigned char qeIdentityOid[] = OID(0x0b);
+ der->qeIdentitySz = SetSGXExt(der->qeIdentity, sizeof(der->qeIdentity),
+ qeIdentityOid, sizeof(qeIdentityOid),
+ cert->qeIdentity, cert->qeIdentitySz);
+ assert(der->qeIdentitySz > 0);
+
+ const unsigned char rootCaCrlOid[] = OID(0x0c);
+ der->rootCaCrlSz = SetSGXExt(der->rootCaCrl, sizeof(der->rootCaCrl),
+ rootCaCrlOid, sizeof(rootCaCrlOid),
+ cert->rootCaCrl, cert->rootCaCrlSz);
+ assert(der->rootCaCrlSz > 0);
+
+ const unsigned char pckCrlOid[] = OID(0x0d);
+ der->pckCrlSz = SetSGXExt(der->pckCrl, sizeof(der->pckCrl),
+ pckCrlOid, sizeof(pckCrlOid),
+ cert->pckCrl, cert->pckCrlSz);
+ assert(der->pckCrlSz > 0);
+
+ der->extensionsSz += der->quoteSz + der->pckCrtSz + der->pckSignChainSz +
+ der->tcbInfoSz + der->tcbSignChainSz + der->qeIdentitySz + der->rootCaCrlSz +
+ der->pckCrlSz;
+}
+#endif
+
/* put extensions */
if (der->extensionsSz > 0) {
@@ -8131,6 +8339,88 @@ static int EncodeCert(Cert* cert, DerCert* der, RsaKey* rsaKey, ecc_key* eccKey,
return EXTENSIONS_E;
}
#endif /* WOLFSSL_CERT_EXT */
+#ifdef WOLFSSL_SGX_ATTESTATION
+ if (der->iasSigCACertSz && der->iasSigCertSz &&
+ der->iasSigSz && der->iasAttestationReportSz) {
+
+ ret = SetExtensions(der->extensions, sizeof(der->extensions),
+ &der->extensionsSz,
+ der->iasAttestationReport, der->iasAttestationReportSz);
+ if (ret <= 0)
+ return EXTENSIONS_E;
+
+ ret = SetExtensions(der->extensions, sizeof(der->extensions),
+ &der->extensionsSz,
+ der->iasSigCACert, der->iasSigCACertSz);
+ if (ret <= 0)
+ return EXTENSIONS_E;
+
+ ret = SetExtensions(der->extensions, sizeof(der->extensions),
+ &der->extensionsSz,
+ der->iasSigCert, der->iasSigCertSz);
+ if (ret <= 0)
+ return EXTENSIONS_E;
+
+ ret = SetExtensions(der->extensions, sizeof(der->extensions),
+ &der->extensionsSz,
+ der->iasSig, der->iasSigSz);
+ if (ret <= 0)
+ return EXTENSIONS_E;
+ }
+
+ if (der->quoteSz > 0 && der->pckCrtSz > 0 && der->pckSignChainSz > 0 &&
+ der->tcbInfoSz > 0 && der->tcbSignChainSz > 0 && der->qeIdentitySz > 0 &&
+ der->rootCaCrlSz > 0 && der->pckCrlSz > 0) {
+
+ ret = SetExtensions(der->extensions, sizeof(der->extensions),
+ &der->extensionsSz,
+ der->quote, der->quoteSz);
+ if (ret <= 0)
+ return EXTENSIONS_E;
+
+ ret = SetExtensions(der->extensions, sizeof(der->extensions),
+ &der->extensionsSz,
+ der->pckCrt, der->pckCrtSz);
+ if (ret <= 0)
+ return EXTENSIONS_E;
+
+ ret = SetExtensions(der->extensions, sizeof(der->extensions),
+ &der->extensionsSz,
+ der->pckSignChain, der->pckSignChainSz);
+ if (ret <= 0)
+ return EXTENSIONS_E;
+
+ ret = SetExtensions(der->extensions, sizeof(der->extensions),
+ &der->extensionsSz,
+ der->tcbInfo, der->tcbInfoSz);
+ if (ret <= 0)
+ return EXTENSIONS_E;
+
+ ret = SetExtensions(der->extensions, sizeof(der->extensions),
+ &der->extensionsSz,
+ der->tcbSignChain, der->tcbSignChainSz);
+ if (ret <= 0)
+ return EXTENSIONS_E;
+
+ ret = SetExtensions(der->extensions, sizeof(der->extensions),
+ &der->extensionsSz,
+ der->qeIdentity, der->qeIdentitySz);
+ if (ret <= 0)
+ return EXTENSIONS_E;
+
+ ret = SetExtensions(der->extensions, sizeof(der->extensions),
+ &der->extensionsSz,
+ der->rootCaCrl, der->rootCaCrlSz);
+ if (ret <= 0)
+ return EXTENSIONS_E;
+
+ ret = SetExtensions(der->extensions, sizeof(der->extensions),
+ &der->extensionsSz,
+ der->pckCrl, der->pckCrlSz);
+ if (ret <= 0)
+ return EXTENSIONS_E;
+}
+#endif
}
der->total = der->versionSz + der->serialSz + der->sigAlgoSz +
diff --git a/wolfssl/internal.h b/wolfssl/internal.h
index 9c77120..3c922dd 100644
--- a/wolfssl/internal.h
+++ b/wolfssl/internal.h
@@ -1272,7 +1272,7 @@ enum Misc {
/* max size of a handshake message, currently set to the certificate */
#ifndef MAX_HANDSHAKE_SZ
- #define MAX_HANDSHAKE_SZ MAX_CERTIFICATE_SZ
+ #define MAX_HANDSHAKE_SZ ((MAX_CERTIFICATE_SZ) * 4)
#endif
#ifndef SESSION_TICKET_LEN
diff --git a/wolfssl/wolfcrypt/asn_public.h b/wolfssl/wolfcrypt/asn_public.h
index b730846..9c8128c 100644
--- a/wolfssl/wolfcrypt/asn_public.h
+++ b/wolfssl/wolfcrypt/asn_public.h
@@ -164,6 +164,32 @@ typedef struct Cert {
char certPolicies[CTC_MAX_CERTPOL_NB][CTC_MAX_CERTPOL_SZ];
word16 certPoliciesNb; /* Number of Cert Policy */
#endif
+#ifdef WOLFSSL_SGX_ATTESTATION
+ byte iasSigCACert[2048];
+ int iasSigCACertSz;
+ byte iasSigCert[2048];
+ int iasSigCertSz;
+ byte iasSig[2048];
+ int iasSigSz;
+ byte iasAttestationReport[2048];
+ int iasAttestationReportSz;
+ byte quote[2048];
+ int quoteSz;
+ byte pckCrt[2048];
+ int pckCrtSz;
+ byte pckSignChain[4096];
+ int pckSignChainSz;
+ byte tcbInfo[2048];
+ int tcbInfoSz;
+ byte tcbSignChain[4096];
+ int tcbSignChainSz;
+ byte qeIdentity[1024];
+ int qeIdentitySz;
+ byte rootCaCrl[1024];
+ int rootCaCrlSz;
+ byte pckCrl[1024];
+ int pckCrlSz;
+#endif
#ifdef WOLFSSL_CERT_REQ
char challengePw[CTC_NAME_SIZE];
#endif
@@ -330,6 +356,10 @@ WOLFSSL_API int wc_CreatePKCS8Key(byte* out, word32* outSz,
*/
WOLFSSL_API int wc_GetTime(void* timePtr, word32 timeSize);
+#ifdef WOLFSSL_SGX_ATTESTATION
+WOLFSSL_API int SetRsaPublicKey(byte* output, RsaKey* key, int outLen, int with_header);
+#endif
+
#ifdef __cplusplus
} /* extern "C" */
#endif
diff --git a/wolfssl/wolfcrypt/settings.h b/wolfssl/wolfcrypt/settings.h
index 6254b72..9a20413 100644
--- a/wolfssl/wolfcrypt/settings.h
+++ b/wolfssl/wolfcrypt/settings.h
@@ -1228,7 +1228,9 @@ extern void uITRON4_free(void *p) ;
#define WC_RSA_BLINDING
#endif
#define SINGLE_THREADED
+#if !defined(WOLFSSL_SGX_ATTESTATION)
#define NO_ASN_TIME /* can not use headers such as windows.h */
+#endif
#define HAVE_AESGCM
#define USE_CERT_BUFFERS_2048
#define USE_FAST_MATH