-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvm_confidential_client.py
216 lines (180 loc) · 6.27 KB
/
vm_confidential_client.py
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
import base64
import json
import logging
import os
import tempfile
from pathlib import Path
from typing import Any, Dict, Optional, Tuple
import aiohttp
from aleph_message.models import ItemHash
from aleph.sdk.client.vm_client import VmClient
from aleph.sdk.types import Account, SEVMeasurement
from aleph.sdk.utils import (
compute_confidential_measure,
encrypt_secret_table,
get_vm_measure,
make_packet_header,
make_secret_table,
run_in_subprocess,
)
logger = logging.getLogger(__name__)
class VmConfidentialClient(VmClient):
sevctl_path: Path
def __init__(
self,
account: Account,
sevctl_path: Path,
node_url: str = "",
session: Optional[aiohttp.ClientSession] = None,
):
super().__init__(account, node_url, session)
self.sevctl_path = sevctl_path
async def get_certificates(self) -> Tuple[Optional[int], str]:
"""
Get platform confidential certificate
"""
url = f"{self.node_url}/about/certificates"
try:
async with self.session.get(url) as response:
data = await response.read()
with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
tmp_file.write(data)
return response.status, tmp_file.name
except aiohttp.ClientError as e:
logger.error(
f"HTTP error getting node certificates on {self.node_url}: {str(e)}"
)
return None, str(e)
async def create_session(
self, certificate_prefix: str, platform_certificate_path: Path, policy: int
) -> Path:
"""
Create new confidential session
"""
current_path = Path().cwd()
args = [
"session",
"--name",
certificate_prefix,
str(platform_certificate_path),
str(policy),
]
try:
# TODO: Check command result
await self.sevctl_cmd(*args)
return current_path
except Exception as e:
raise ValueError(f"Session creation have failed, reason: {str(e)}")
async def initialize(self, vm_id: ItemHash, session: Path, godh: Path) -> str:
"""
Initialize Confidential VM negociation passing the needed session files
"""
session_file = session.read_bytes()
godh_file = godh.read_bytes()
params = {
"session": session_file,
"godh": godh_file,
}
return await self.perform_confidential_operation(
vm_id, "confidential/initialize", params=params
)
async def measurement(self, vm_id: ItemHash) -> SEVMeasurement:
"""
Fetch VM confidential measurement
"""
if not self.pubkey_signature_header:
self.pubkey_signature_header = (
await self._generate_pubkey_signature_header()
)
status, text = await self.perform_operation(
vm_id, "confidential/measurement", method="GET"
)
sev_measurement = SEVMeasurement.model_validate_json(text)
return sev_measurement
async def validate_measure(
self, sev_data: SEVMeasurement, tik_path: Path, firmware_hash: str
) -> bool:
"""
Validate VM confidential measurement
"""
tik = tik_path.read_bytes()
vm_measure, nonce = get_vm_measure(sev_data)
expected_measure = compute_confidential_measure(
sev_info=sev_data.sev_info,
tik=tik,
expected_hash=firmware_hash,
nonce=nonce,
).digest()
return expected_measure == vm_measure
async def build_secret(
self, tek_path: Path, tik_path: Path, sev_data: SEVMeasurement, secret: str
) -> Tuple[str, str]:
"""
Build disk secret to be injected on the confidential VM
"""
tek = tek_path.read_bytes()
tik = tik_path.read_bytes()
vm_measure, _ = get_vm_measure(sev_data)
iv = os.urandom(16)
secret_table = make_secret_table(secret)
encrypted_secret_table = encrypt_secret_table(
secret_table=secret_table, tek=tek, iv=iv
)
packet_header = make_packet_header(
vm_measure=vm_measure,
encrypted_secret_table=encrypted_secret_table,
secret_table_size=len(secret_table),
tik=tik,
iv=iv,
)
encoded_packet_header = base64.b64encode(packet_header).decode()
encoded_secret = base64.b64encode(encrypted_secret_table).decode()
return encoded_packet_header, encoded_secret
async def inject_secret(
self, vm_id: ItemHash, packet_header: str, secret: str
) -> Dict:
"""
Send the secret by the encrypted channel to boot up the VM
"""
params = {
"packet_header": packet_header,
"secret": secret,
}
text = await self.perform_confidential_operation(
vm_id, "confidential/inject_secret", json=params
)
return json.loads(text)
async def perform_confidential_operation(
self,
vm_id: ItemHash,
operation: str,
params: Optional[Dict[str, Any]] = None,
json=None,
) -> str:
"""
Send confidential operations to the CRN passing the auth headers on each request
"""
if not self.pubkey_signature_header:
self.pubkey_signature_header = (
await self._generate_pubkey_signature_header()
)
url, header = await self._generate_header(
vm_id=vm_id, operation=operation, method="post"
)
try:
async with self.session.post(
url, headers=header, data=params, json=json
) as response:
response.raise_for_status()
response_text = await response.text()
return response_text
except aiohttp.ClientError as e:
raise ValueError(f"HTTP error during operation {operation}: {str(e)}")
async def sevctl_cmd(self, *args) -> bytes:
"""
Execute `sevctl` command with given arguments
"""
return await run_in_subprocess(
[str(self.sevctl_path), *args],
check=True,
)