forked from cloud-security-research/sgx-ra-tls
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwolfssl-sgx-attestation.patch
300 lines (285 loc) · 10.2 KB
/
wolfssl-sgx-attestation.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
diff --git a/wolfcrypt/src/asn.c b/wolfcrypt/src/asn.c
index e234253..7c4bbd8 100644
--- a/wolfcrypt/src/asn.c
+++ b/wolfcrypt/src/asn.c
@@ -61,6 +61,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 +6347,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 +6714,12 @@ 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];
+#endif
#ifdef WOLFSSL_CERT_REQ
byte attrib[MAX_ATTRIB_SZ]; /* Cert req attributes encoded */
#endif
@@ -6732,6 +6742,12 @@ 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;
+#endif
#ifdef WOLFSSL_ALT_NAMES
int altNamesSz; /* encoded AltNames extension length */
#endif
@@ -7104,13 +7120,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 +7148,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 +7483,13 @@ static int SetKeyUsage(byte* output, word32 outSz, word16 input)
ku, idx);
}
-static int SetOjectIdValue(byte* output, word32 outSz, int* idx,
+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 +7503,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 ((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 +8146,48 @@ 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.2
+ unsigned char iasAttestationReportOid[] = {0x2A, 0x86, 0x48, 0x86, 0xF8, 0x4D, 0x8A, 0x39, 0x02};
+ // 1.2.840.113741.1337.3
+ unsigned char iasSigCACertOid[] = {0x2A, 0x86, 0x48, 0x86, 0xF8, 0x4D, 0x8A, 0x39, 0x03};
+ // 1.2.840.113741.1337.4
+ unsigned char iasSigCertOid[] = {0x2A, 0x86, 0x48, 0x86, 0xF8, 0x4D, 0x8A, 0x39, 0x04};
+ // 1.2.840.113741.1337.5
+ unsigned char iasSigOid[] = {0x2A, 0x86, 0x48, 0x86, 0xF8, 0x4D, 0x8A, 0x39, 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;
+ }
+#endif
+
/* put extensions */
if (der->extensionsSz > 0) {
@@ -8131,6 +8264,35 @@ 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;
+ }
+#endif
}
der->total = der->versionSz + der->serialSz + der->sigAlgoSz +
diff --git a/wolfssl/wolfcrypt/asn_public.h b/wolfssl/wolfcrypt/asn_public.h
index b730846..02cce52 100644
--- a/wolfssl/wolfcrypt/asn_public.h
+++ b/wolfssl/wolfcrypt/asn_public.h
@@ -164,6 +164,16 @@ 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;
+#endif
#ifdef WOLFSSL_CERT_REQ
char challengePw[CTC_NAME_SIZE];
#endif
@@ -330,6 +340,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