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
// 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 crate::arch::{Align16, Align512};
use crate::enclave::EnclaveRange;
use crate::inst::{self, EncluInst};
use crate::se::AlignReport;
use core::convert::From;
use core::mem;
use core::ptr;
use sgx_types::error::{SgxResult, SgxStatus};
use sgx_types::marker::ContiguousMemory;
use sgx_types::types::KEY_REQUEST_RESERVED2_BYTES;
use sgx_types::types::{AlignKey128bit, AttributesFlags, Key128bit, KeyPolicy, KeyRequest};

#[repr(C, align(512))]
#[derive(Clone, Copy, Debug, Default)]
pub struct AlignKeyRequest(pub KeyRequest);

pub type AlignKey = Align16<Key128bit>;

unsafe impl ContiguousMemory for AlignKeyRequest {}

impl AlignKeyRequest {
    pub fn egetkey(&self) -> SgxResult<AlignKey128bit> {
        ensure!(self.is_enclave_range(), SgxStatus::InvalidParameter);
        ensure!(self.0.reserved1 == 0, SgxStatus::InvalidParameter);
        ensure!(
            self.0.reserved2 == [0; KEY_REQUEST_RESERVED2_BYTES],
            SgxStatus::InvalidParameter
        );
        ensure!(self.0.key_policy.is_valid(), SgxStatus::InvalidParameter);

        // check if KSS flag is disabled but KSS related policy or config_svn is set
        let report = AlignReport::get_self();
        if !report
            .0
            .body
            .attributes
            .flags
            .intersects(AttributesFlags::KSS)
            && (self
                .0
                .key_policy
                .intersects(KeyPolicy::KSS | KeyPolicy::NOISVPRODID)
                || self.0.config_svn > 0)
        {
            bail!(SgxStatus::InvalidParameter);
        }

        EncluInst::egetkey(self)
            .map(|k| From::from(k.0))
            .map_err(|e| match e {
                inst::INVALID_ATTRIBUTE => SgxStatus::InvalidAttribute,
                inst::INVALID_CPUSVN => SgxStatus::InvalidCpusvn,
                inst::INVALID_ISVSVN => SgxStatus::InvalidIsvsvn,
                inst::INVALID_KEYNAME => SgxStatus::InvalidKeyname,
                _ => SgxStatus::Unexpected,
            })
    }
}

impl AlignKeyRequest {
    pub const UNPADDED_SIZE: usize = mem::size_of::<KeyRequest>();
    pub const ALIGN_SIZE: usize = mem::size_of::<AlignKeyRequest>();

    pub fn try_copy_from(src: &[u8]) -> Option<AlignKeyRequest> {
        if src.len() == Self::UNPADDED_SIZE {
            unsafe {
                let mut ret: Self = mem::zeroed();
                ptr::copy_nonoverlapping(
                    src.as_ptr(),
                    &mut ret as *mut _ as *mut _,
                    Self::UNPADDED_SIZE,
                );
                Some(ret)
            }
        } else {
            None
        }
    }
}

impl AsRef<Align512<[u8; AlignKeyRequest::UNPADDED_SIZE]>> for AlignKeyRequest {
    fn as_ref(&self) -> &Align512<[u8; AlignKeyRequest::UNPADDED_SIZE]> {
        unsafe { &*(self as *const _ as *const _) }
    }
}

impl From<KeyRequest> for AlignKeyRequest {
    fn from(kr: KeyRequest) -> AlignKeyRequest {
        AlignKeyRequest(kr)
    }
}

impl From<&KeyRequest> for AlignKeyRequest {
    fn from(kr: &KeyRequest) -> AlignKeyRequest {
        AlignKeyRequest(*kr)
    }
}

impl From<AlignKeyRequest> for KeyRequest {
    fn from(kr: AlignKeyRequest) -> KeyRequest {
        kr.0
    }
}