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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License..

use super::*;

use core::mem;
use core::slice;

//
// sgx_pce.h
//
/* PCE ID for the PCE in this library */
pub const PCE_ID: u16 = 0;
pub const PCE_ALG_RSA_OAEP_3072: u8 = 1;
pub const PCE_NIST_P256_ECDSA_SHA256: u8 = 0;

impl_enum! {
    #[repr(u32)]
    #[derive(Clone, Copy, Debug, Eq, PartialEq)]
    pub enum QlRequestPolicy {
        Persistent = 0,
        Ephemeral = 1,
    }
}

#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
#[repr(C, packed)]
pub struct PceInfo {
    pub isv_svn: u16,
    pub pce_id: u16,
}

//
// sgx_ql_lib_common.h
//
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
#[repr(C, packed)]
pub struct QlQe3Id {
    pub id: [u8; 16],
}

impl_asref_array! {
    PceInfo;
    QlQe3Id;
}

impl_asmut_array! {
    QlQe3Id;
}

impl_from_array! {
    QlQe3Id;
}

impl_struct_ContiguousMemory! {
    PceInfo;
    QlQe3Id;
}

impl_unsafe_marker_for! {
    BytewiseEquality,
    PceInfo QlQe3Id
}

impl_enum! {
    #[repr(u32)]
    #[derive(Clone, Copy, Debug, Eq, PartialEq)]
    pub enum QlConfigVersion {
        QlConfigVersion1 = 0,
    }
}

#[repr(C, packed)]
pub struct CQlPckCertId {
    pub p_qe3_id: *mut u8,
    pub qe3_id_size: u32,
    pub p_platform_cpu_svn: *mut CpuSvn,
    pub p_platform_pce_isv_svn: *mut u16,
    pub p_encrypted_ppid: *mut u8,
    pub encrypted_ppid_size: u32,
    pub crypto_suite: u8,
    pub pce_id: u16,
}

#[repr(C, packed)]
pub struct CQlConfig {
    pub version: QlConfigVersion,
    pub cert_cpu_svn: CpuSvn,
    pub cert_pce_isv_svn: u16,
    pub cert_data_size: u32,
    pub p_cert_data: *mut u8,
}

/* intel DCAP 1.13 */
pub const MAX_PARAM_STRING_SIZE: usize = 256;
impl_copy_clone! {
    #[repr(C)]
    #[derive(Debug)]
    pub struct QlQveCollateralParam {
        key: [u8; MAX_PARAM_STRING_SIZE + 1],
        value: [u8; MAX_PARAM_STRING_SIZE + 1]
    }
}

impl_struct_default! {
    QlQveCollateralParam; //514
}

impl_asref_array! {
    QlQveCollateralParam;
}

impl_struct_ContiguousMemory! {
    QlQveCollateralParam;
}

#[repr(C)]
pub struct CQlQveCollateral {
    pub version: u32, // version = 1.  PCK Cert chain is in the Quote.
    /* intel DCAP 1.13 */
    pub tee_type: u32, // 0x00000000: SGX or 0x00000081: TDX
    pub pck_crl_issuer_chain: *mut c_char,
    pub pck_crl_issuer_chain_size: u32,
    pub root_ca_crl: *mut c_char, // Root CA CRL
    pub root_ca_crl_size: u32,
    pub pck_crl: *mut c_char, // PCK Cert CRL
    pub pck_crl_size: u32,
    pub tcb_info_issuer_chain: *mut c_char,
    pub tcb_info_issuer_chain_size: u32,
    pub tcb_info: *mut c_char, // TCB Info structure
    pub tcb_info_size: u32,
    pub qe_identity_issuer_chain: *mut c_char,
    pub qe_identity_issuer_chain_size: u32,
    pub qe_identity: *mut c_char, // QE Identity Structure
    pub qe_identity_size: u32,
}

/* intel DCAP 1.14 */
impl_enum! {
    #[repr(u8)]
    #[derive(Clone, Copy, Debug, Eq, PartialEq)]
    pub enum ProdType {
        SGX = 0,
        TDX = 1,
    }
}

impl_enum! {
    #[repr(u32)]
    #[derive(Clone, Copy, Debug, Eq, PartialEq)]
    pub enum QlLogLevel {
        LogError = 0,
        LogInfo = 1,
    }
}

pub type QlLoggingCallback = extern "C" fn(level: QlLogLevel, message: *const c_char);

//
// sgx_quote_3.h
//
pub const REF_QUOTE_MAX_AUTHENTICATON_DATA_SIZE: u16 = 64;

impl_enum! {
    #[repr(u32)]
    #[derive(Clone, Copy, Debug, Eq, PartialEq)]
    pub enum QlAttestationAlgorithmId {
        Epid = 0,
        Reserved1 = 1,
        EcdsaP256 = 2,
        EcdsaP384 = 3,
        Max = 4,
    }
}

impl_enum! {
    #[repr(u32)]
    #[derive(Clone, Copy, Debug, Eq, PartialEq)]
    pub enum QlCertKeyType {
        PPIDClearText = 1,
        PPIDRsa2048Encrypted = 2,
        PPIDRsa3072Encrypted = 3,
        PCKClearText = 4,
        EcdsaSigAuxData = 6,
        QlCertKeyTypeMax = 16,
    }
}

#[derive(Clone, Copy, Debug)]
#[repr(C, packed)]
pub struct QlPPIDRsa3072EncryptedCertInfo {
    pub enc_ppid: [u8; 384],
    pub cpu_svn: CpuSvn,
    pub pce_info: PceInfo,
}

impl_struct_default! {
    QlPPIDRsa3072EncryptedCertInfo; //404
}

impl_asref_array! {
    QlPPIDRsa3072EncryptedCertInfo;
}

impl_struct_ContiguousMemory! {
    QlPPIDRsa3072EncryptedCertInfo;
}

#[derive(Clone, Copy, Debug, Default)]
#[repr(C, packed)]
pub struct QlAuthData {
    pub size: u16,
    pub auth_data: [u8; 0],
}

#[derive(Clone, Copy, Debug, Default)]
#[repr(C, packed)]
pub struct QlCertificationData {
    pub cert_key_type: u16,
    pub size: u32,
    pub certification_data: [u8; 0],
}

#[derive(Clone, Copy, Debug)]
#[repr(C, packed)]
pub struct QlEcdsaSigData {
    pub sig: [u8; 64],
    pub attest_pub_key: [u8; 64],
    pub qe_report: ReportBody,
    pub qe_report_sig: [u8; 64],
    pub auth_certification_data: [u8; 0],
}

impl_struct_default! {
    QlEcdsaSigData; //576
}

impl_struct_ContiguousMemory! {
    QlAuthData;
    QlCertificationData;
    QlEcdsaSigData;
}

impl QlAuthData {
    /// # Safety
    pub unsafe fn as_slice_unchecked(&self) -> &[u8] {
        slice::from_raw_parts(
            self as *const _ as *const u8,
            mem::size_of::<QlAuthData>() + self.size as usize,
        )
    }
}

impl QlCertificationData {
    /// # Safety
    pub unsafe fn as_slice_unchecked(&self) -> &[u8] {
        slice::from_raw_parts(
            self as *const _ as *const u8,
            mem::size_of::<QlCertificationData>() + self.size as usize,
        )
    }
}

impl QlEcdsaSigData {
    /// # Safety
    pub unsafe fn as_slice_unchecked(&self) -> &[u8] {
        let p_sig_data = self as *const QlEcdsaSigData;
        let p_auth_data = p_sig_data.add(1) as *const QlAuthData;
        let p_cert_data = (p_auth_data.add(1) as *const u8).add((*p_auth_data).size as usize)
            as *const QlCertificationData;

        slice::from_raw_parts(
            self as *const _ as *const u8,
            mem::size_of::<QlEcdsaSigData>()
                + mem::size_of::<QlAuthData>()
                + mem::size_of::<QlCertificationData>()
                + (*p_auth_data).size as usize
                + (*p_cert_data).size as usize,
        )
    }
}

#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
#[repr(C, packed)]
pub struct QuoteHeader {
    pub version: u16,
    pub att_key_type: u16,
    pub att_key_data: u32,
    pub qe_svn: u16,
    pub pce_svn: u16,
    pub vendor_id: [u8; 16],
    pub user_data: [u8; 20],
}

#[derive(Clone, Copy, Debug)]
#[repr(C, packed)]
pub struct Quote3 {
    pub header: QuoteHeader,
    pub report_body: ReportBody,
    pub signature_len: u32,
    pub signature: [u8; 0],
}

impl_asref_array! {
    QuoteHeader;
}

impl_struct_default! {
    Quote3; //436
}

impl_struct_ContiguousMemory! {
    QuoteHeader;
    Quote3;
}

impl Quote3 {
    /// # Safety
    pub unsafe fn as_slice_unchecked(&self) -> &[u8] {
        slice::from_raw_parts(
            self as *const _ as *const u8,
            mem::size_of::<Quote3>() + self.signature_len as usize,
        )
    }
}

#[derive(Clone, Copy, Debug)]
#[repr(C, packed)]
pub struct QlQeReportInfo {
    pub nonce: QuoteNonce,
    pub app_enclave_target_info: TargetInfo,
    pub qe_report: Report,
}

impl_asref_array! {
    QlQeReportInfo;
}

impl_struct_default! {
    QlQeReportInfo; //960
}

impl_struct_ContiguousMemory! {
    QlQeReportInfo;
}

/* intel DCAP 1.6 */
//
// sgx_dcap_ql_wrapper.h
//
impl_enum! {
    #[repr(u32)]
    #[derive(Copy, Clone, Debug, Eq, PartialEq)]
    pub enum QlPathType {
        Qe3Path = 0,
        PcePath = 1,
        QplPath = 2,
        /* intel DCAP 1.13 */
        IdePath = 3,
    }
}

//
// qve_header.h
//
pub const ROOT_KEY_ID_SIZE: usize = 48;
pub const PLATFORM_INSTANCE_ID_SIZE: usize = 16;

/* intel DCAP 1.7 */
impl_enum! {
    #[repr(u32)]
    #[derive(Copy, Clone, Debug, Eq, PartialEq)]
    pub enum PckCertFlag {
        False = 0,
        True = 1,
        Undefined = 2,
    }
}

impl_copy_clone! {
    #[repr(C)]
    #[derive(Debug)]
    pub struct QlQvSupplemental {
        pub version: u32,
        pub earliest_issue_date: time_t,
        pub latest_issue_date: time_t,
        pub earliest_expiration_date: time_t,
        pub tcb_level_date_tag: time_t,
        pub pck_crl_num: u32,
        pub root_ca_crl_num: u32,
        pub tcb_eval_ref_num: u32,
        pub root_key_id: [u8; ROOT_KEY_ID_SIZE],
        pub pck_ppid: Key128bit,
        pub tcb_cpusvn: CpuSvn,
        pub tcb_pce_isvsvn: u16,
        pub pce_id: u16,
        /* intel DCAP 1.13 */
        pub tee_type: u32,
        /* intel DCAP 1.7 */
        pub sgx_type: u8,

        pub platform_instance_id: [u8; PLATFORM_INSTANCE_ID_SIZE],
        pub dynamic_platform: PckCertFlag,
        pub cached_keys: PckCertFlag,
        pub smt_enabled: PckCertFlag,
    }
}

impl_struct_default! {
    QlQvSupplemental; //176
}

impl_asref_array! {
    QlQvSupplemental;
}

impl_struct_ContiguousMemory! {
    QlQvSupplemental;
}

impl_enum! {
    #[repr(u32)]
    #[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
    pub enum QlQvResult {
        Ok                          = 0x0000_0000,
        ConfigNeeded                = 0x0000_A001,
        OutOfDate                   = 0x0000_A002,
        OutOfDateConfigNeeded       = 0x0000_A003,
        InvalidSignature            = 0x0000_A004,
        Revoked                     = 0x0000_A005,
        Unspecified                 = 0x0000_A006,
        SWHardeningNeeded           = 0x0000_A007,
        ConfigAndSWHardeningNeeded  = 0x0000_A008,
        Max                         = 0x0000_A0FF,
    }
}

impl QlQvResult {
    pub fn __description(&self) -> &str {
        match *self {
            QlQvResult::Ok => "The Quote verification passed and is at the latest TCB level.",
            QlQvResult::ConfigNeeded => {
                "The Quote verification passed and the platform is patched to the latest TCB level but additional configuration of the SGX platform may be needed."
            }
            QlQvResult::OutOfDate => {
                "The Quote is good but TCB level of the platform is out of date, The platform needs patching to be at the latest TCB level."
            }
            QlQvResult::OutOfDateConfigNeeded => {
                "The Quote is good but the TCB level of the platform is out of date and additional configuration of the SGX Platform at its current patching level may be needed. The platform needs patching to be at the latest TCB level."
            }
            QlQvResult::InvalidSignature => "The signature over the application report is invalid.",
            QlQvResult::Revoked => "The attestation key or platform has been revoked.",
            QlQvResult::Unspecified => "The Quote verification failed due to an error in one of the input.",
            QlQvResult::SWHardeningNeeded => "The TCB level of the platform is up to date, but SGX SW Hardening is needed.",
            QlQvResult::ConfigAndSWHardeningNeeded => {
                "The TCB level of the platform is up to date, but additional configuration of the platform at its current patching level may be needed. Moreove, SGX SW Hardening is also needed."
            }
            QlQvResult::Max => "Indicate max result to allow better translation.",
        }
    }

    pub fn as_str(&self) -> &str {
        match *self {
            QlQvResult::Ok => "Ok",
            QlQvResult::ConfigNeeded => "ConfigNeeded",
            QlQvResult::OutOfDate => "OutOfDate",
            QlQvResult::OutOfDateConfigNeeded => "OutOfDateConfigNeeded",
            QlQvResult::InvalidSignature => "InvalidSignature",
            QlQvResult::Revoked => "Revoked",
            QlQvResult::Unspecified => "Unspecified",
            QlQvResult::SWHardeningNeeded => "SWHardeningNeeded",
            QlQvResult::ConfigAndSWHardeningNeeded => "ConfigAndSWHardeningNeeded",
            QlQvResult::Max => "Max",
        }
    }
}

impl fmt::Display for QlQvResult {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

/* intel DCAP 1.6 */
//
// sgx_dcap_quoteverify.h
//
impl_enum! {
    #[repr(u32)]
    #[derive(Copy, Clone, Debug, Eq, PartialEq)]
    pub enum QvPathType {
        QvePath = 0,
        QplPath = 1,
    }
}

/* intel DCAP 1.14 */
//
// sgx_default_qcnl_wrapper.h
//
impl_enum! {
    #[repr(u8)]
    #[derive(Copy, Clone, PartialEq, Eq, Debug)]
    pub enum QeType {
        Ecdsa = 0,
        Td = 1,
    }
}