-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadmin_binding.py
More file actions
407 lines (341 loc) · 12.7 KB
/
admin_binding.py
File metadata and controls
407 lines (341 loc) · 12.7 KB
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
# SPDX-License-Identifier: AGPL-3.0-or-later
# Copyright (c) 2025 Web4 Contributors
#
# Hardbound - Admin Binding (Hardware Security)
# https://github.com/dp-web4/web4
"""
Admin Binding: Hardware-secured admin identity.
Production admin roles MUST be hardware-bound to prevent impersonation.
This module provides:
- TPM2 binding for admin LCTs
- Binding verification
- Admin attestation
- Fallback to software binding for development
Key insight: Whoever controls the admin key controls the team.
Hardware binding makes this key non-extractable.
"""
import json
import sqlite3
from datetime import datetime, timezone
from pathlib import Path
from typing import Optional, Dict, Any, TYPE_CHECKING
from dataclasses import dataclass
from enum import Enum
import sys
# Import TPM2 provider
_tpm_path = Path(__file__).parent.parent / "core" / "lct_binding"
sys.path.insert(0, str(_tpm_path))
sys.path.insert(0, str(_tpm_path.parent))
try:
from lct_binding.tpm2_provider import TPM2Provider
TPM2_AVAILABLE = True
except ImportError:
try:
from tpm2_provider import TPM2Provider
TPM2_AVAILABLE = True
except ImportError:
TPM2_AVAILABLE = False
TPM2Provider = None
# Import governance (self-contained)
from .ledger import Ledger
if TYPE_CHECKING:
from .team import Team
class AdminBindingType(Enum):
"""Types of admin binding."""
SOFTWARE = "software" # Soft LCT - development only
TPM2 = "tpm2" # TPM 2.0 hardware binding
FIDO2 = "fido2" # FIDO2/WebAuthn - not yet implemented
@dataclass
class AdminBinding:
"""Admin binding record."""
binding_type: AdminBindingType
lct_id: str
public_key: Optional[str] = None
hardware_anchor: Optional[str] = None
attestation: Optional[str] = None
bound_at: Optional[str] = None
verified: bool = False
class AdminBindingManager:
"""
Manages admin binding for teams.
Production requirements:
- Admin MUST have hardware binding (TPM2 or FIDO2)
- Software binding allowed for development with explicit flag
- All binding changes recorded in audit trail
- Binding can be verified cryptographically
"""
def __init__(self, ledger: Optional[Ledger] = None):
"""
Initialize admin binding manager.
Args:
ledger: Ledger for audit trail
"""
self.ledger = ledger or Ledger()
self._tpm_provider: Optional[TPM2Provider] = None
self._ensure_table()
def _ensure_table(self):
"""Create admin bindings table if not exists."""
with sqlite3.connect(self.ledger.db_path) as conn:
conn.execute("""
CREATE TABLE IF NOT EXISTS admin_bindings (
team_id TEXT PRIMARY KEY,
lct_id TEXT NOT NULL,
binding_type TEXT NOT NULL,
public_key TEXT,
hardware_anchor TEXT,
attestation TEXT,
bound_at TEXT NOT NULL,
verified INTEGER DEFAULT 0
)
""")
@property
def tpm_provider(self) -> Optional[TPM2Provider]:
"""Get TPM2 provider (lazy init)."""
if self._tpm_provider is None and TPM2_AVAILABLE:
try:
self._tpm_provider = TPM2Provider()
except Exception:
pass
return self._tpm_provider
def get_tpm_status(self) -> dict:
"""Get TPM availability status."""
if not TPM2_AVAILABLE:
return {
"available": False,
"reason": "TPM2 provider module not found",
"recommendation": "Install tpm2-tools and ensure core/lct_binding/tpm2_provider.py exists"
}
provider = self.tpm_provider
if provider is None:
return {
"available": False,
"reason": "TPM2 provider failed to initialize",
"recommendation": "Check TPM2 tools installation and access permissions"
}
if not provider._tpm_available:
return {
"available": False,
"reason": "TPM2 tools not accessible",
"recommendation": "Run: sudo apt install tpm2-tools && sudo usermod -a -G tss $USER"
}
return {
"available": True,
"hardware_type": "TPM2",
"trust_ceiling": provider.trust_ceiling,
"platform": provider.get_platform_info().name if provider.get_platform_info() else "unknown"
}
def bind_admin_tpm2(self, team_id: str, admin_name: str = "admin") -> AdminBinding:
"""
Bind admin to TPM2 hardware.
Args:
team_id: Team ID to bind admin for
admin_name: Name for the admin entity
Returns:
AdminBinding record
Raises:
RuntimeError: If TPM2 not available
"""
if not TPM2_AVAILABLE or self.tpm_provider is None:
raise RuntimeError(
"TPM2 not available. For development, use bind_admin_software() "
"with require_hardware=False"
)
if not self.tpm_provider._tpm_available:
raise RuntimeError(
"TPM2 tools not accessible. Install tpm2-tools and ensure TPM access."
)
# Generate TPM-bound LCT for admin
from core.lct_binding.lct_capability_levels import EntityType
lct = self.tpm_provider.create_lct(EntityType.HUMAN, f"admin-{admin_name}")
# Get attestation
key_id = lct.lct_id.split(':')[-1]
attestation_result = self.tpm_provider.get_attestation(key_id)
now = datetime.now(timezone.utc).isoformat()
binding = AdminBinding(
binding_type=AdminBindingType.TPM2,
lct_id=lct.lct_id,
public_key=lct.binding.public_key,
hardware_anchor=lct.binding.hardware_anchor,
attestation=attestation_result.attestation_token if attestation_result.success else None,
bound_at=now,
verified=attestation_result.success
)
# Store binding
self._store_binding(team_id, binding)
# Audit trail
self.ledger.record_audit(
session_id=team_id,
action_type="admin_bound_tpm2",
tool_name="hardbound",
target=lct.lct_id,
r6_data={
"binding_type": "tpm2",
"hardware_anchor": lct.binding.hardware_anchor,
"verified": binding.verified,
"trust_ceiling": self.tpm_provider.trust_ceiling
}
)
return binding
def bind_admin_software(
self,
team_id: str,
lct_id: str,
require_hardware: bool = True
) -> AdminBinding:
"""
Bind admin with software LCT.
Args:
team_id: Team ID
lct_id: Existing soft LCT ID
require_hardware: If True, raise error (production mode)
Returns:
AdminBinding record
Raises:
ValueError: If require_hardware=True (production mode)
"""
if require_hardware:
raise ValueError(
"Software binding not allowed for production. "
"Use bind_admin_tpm2() for hardware binding, or set "
"require_hardware=False for development."
)
now = datetime.now(timezone.utc).isoformat()
binding = AdminBinding(
binding_type=AdminBindingType.SOFTWARE,
lct_id=lct_id,
bound_at=now,
verified=False # Software bindings are never verified
)
# Store binding
self._store_binding(team_id, binding)
# Audit trail with warning
self.ledger.record_audit(
session_id=team_id,
action_type="admin_bound_software",
tool_name="hardbound",
target=lct_id,
r6_data={
"binding_type": "software",
"warning": "DEVELOPMENT MODE - Software binding provides no hardware security",
"trust_ceiling": 0.7
}
)
return binding
def get_binding(self, team_id: str) -> Optional[AdminBinding]:
"""Get admin binding for a team."""
with sqlite3.connect(self.ledger.db_path) as conn:
conn.row_factory = sqlite3.Row
row = conn.execute(
"SELECT * FROM admin_bindings WHERE team_id = ?",
(team_id,)
).fetchone()
if not row:
return None
return AdminBinding(
binding_type=AdminBindingType(row["binding_type"]),
lct_id=row["lct_id"],
public_key=row["public_key"],
hardware_anchor=row["hardware_anchor"],
attestation=row["attestation"],
bound_at=row["bound_at"],
verified=bool(row["verified"])
)
def verify_admin(self, team_id: str, lct_id: str, signature: bytes = None,
challenge: bytes = None) -> dict:
"""
Verify admin identity.
For TPM2 binding:
- Optionally verify signature on challenge
- Check attestation freshness
For software binding:
- Only checks LCT ID match
Args:
team_id: Team ID
lct_id: LCT claiming to be admin
signature: Optional signature on challenge
challenge: Optional challenge data
Returns:
Verification result dict
"""
binding = self.get_binding(team_id)
if not binding:
return {
"verified": False,
"reason": "No admin binding found for team"
}
if binding.lct_id != lct_id:
return {
"verified": False,
"reason": "LCT ID does not match admin binding"
}
# For software binding, just return match result
if binding.binding_type == AdminBindingType.SOFTWARE:
return {
"verified": True,
"binding_type": "software",
"warning": "Software binding - no cryptographic verification",
"trust_ceiling": 0.7
}
# For TPM2 binding, verify signature if provided
if binding.binding_type == AdminBindingType.TPM2:
result = {
"verified": True,
"binding_type": "tpm2",
"hardware_anchor": binding.hardware_anchor,
"trust_ceiling": 1.0
}
if signature and challenge and binding.public_key:
# Verify signature
if self.tpm_provider:
sig_valid = self.tpm_provider.verify_signature(
binding.public_key, challenge, signature
)
result["signature_valid"] = sig_valid
if not sig_valid:
result["verified"] = False
result["reason"] = "Signature verification failed"
return result
return {
"verified": False,
"reason": f"Unknown binding type: {binding.binding_type}"
}
def _store_binding(self, team_id: str, binding: AdminBinding):
"""Store admin binding in database."""
with sqlite3.connect(self.ledger.db_path) as conn:
conn.execute("""
INSERT OR REPLACE INTO admin_bindings
(team_id, lct_id, binding_type, public_key, hardware_anchor,
attestation, bound_at, verified)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
""", (
team_id,
binding.lct_id,
binding.binding_type.value,
binding.public_key,
binding.hardware_anchor,
binding.attestation,
binding.bound_at,
1 if binding.verified else 0
))
def check_tpm_availability() -> dict:
"""
Quick check for TPM availability.
Returns:
Status dict with availability info
"""
manager = AdminBindingManager()
return manager.get_tpm_status()
if __name__ == "__main__":
print("=" * 60)
print("Admin Binding - TPM Status Check")
print("=" * 60)
status = check_tpm_availability()
print(f"\nTPM Available: {status.get('available', False)}")
if status.get('available'):
print(f"Hardware Type: {status.get('hardware_type')}")
print(f"Trust Ceiling: {status.get('trust_ceiling')}")
print(f"Platform: {status.get('platform')}")
else:
print(f"Reason: {status.get('reason')}")
print(f"Recommendation: {status.get('recommendation')}")
print("\n" + "=" * 60)