-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathinteractive_pinning_demo.py
More file actions
311 lines (241 loc) · 10.1 KB
/
interactive_pinning_demo.py
File metadata and controls
311 lines (241 loc) · 10.1 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
#!/usr/bin/env python3
"""
Interactive Key Pinning Demo
This example demonstrates how to use SchemaPin's interactive key pinning
functionality to prompt users for key decisions.
"""
import json
import os
import tempfile
from schemapin.core import SchemaPinCore
from schemapin.crypto import KeyManager, SignatureManager
from schemapin.interactive import CallbackInteractiveHandler, PromptType, UserDecision
from schemapin.pinning import KeyPinning, PinningMode, PinningPolicy
def demo_console_interactive_pinning():
"""Demonstrate console-based interactive pinning."""
print("=== Console Interactive Pinning Demo ===\n")
# Create temporary database
temp_dir = tempfile.mkdtemp()
db_path = os.path.join(temp_dir, "demo_pinning.db")
try:
# Initialize pinning with interactive mode
pinning = KeyPinning(
db_path=db_path,
mode=PinningMode.INTERACTIVE
)
# Generate demo keys
private_key, public_key = KeyManager.generate_keypair()
public_key_pem = KeyManager.export_public_key_pem(public_key)
# Demo tool information
tool_id = "demo-calculator"
domain = "example-tools.com"
developer_name = "Example Tools Inc"
print(f"Tool: {tool_id}")
print(f"Domain: {domain}")
print(f"Developer: {developer_name}")
print(f"Key Fingerprint: {KeyManager.calculate_key_fingerprint(public_key)}")
print()
# Attempt to pin the key (will prompt user)
print("Attempting to pin key for first time...")
result = pinning.interactive_pin_key(
tool_id, public_key_pem, domain, developer_name
)
if result:
print("✅ Key was accepted and pinned!")
else:
print("❌ Key was rejected.")
print(f"Key pinned: {pinning.is_key_pinned(tool_id)}")
# Demonstrate key change scenario
if pinning.is_key_pinned(tool_id):
print("\n--- Key Change Scenario ---")
# Generate new key
new_private_key, new_public_key = KeyManager.generate_keypair()
new_public_key_pem = KeyManager.export_public_key_pem(new_public_key)
print(f"New Key Fingerprint: {KeyManager.calculate_key_fingerprint(new_public_key)}")
print("Attempting to change pinned key...")
result = pinning.interactive_pin_key(
tool_id, new_public_key_pem, domain, developer_name
)
if result:
print("✅ Key change was accepted!")
else:
print("❌ Key change was rejected.")
finally:
# Cleanup
if os.path.exists(db_path):
os.unlink(db_path)
os.rmdir(temp_dir)
def demo_callback_interactive_pinning():
"""Demonstrate callback-based interactive pinning."""
print("\n=== Callback Interactive Pinning Demo ===\n")
# Create temporary database
temp_dir = tempfile.mkdtemp()
db_path = os.path.join(temp_dir, "demo_pinning.db")
try:
# Custom callback function
def custom_prompt_handler(context):
"""Custom prompt handler that automatically accepts first-time keys."""
print(f"Custom handler called for: {context.tool_id}")
print(f"Prompt type: {context.prompt_type.value}")
if context.prompt_type == PromptType.FIRST_TIME_KEY:
print("Auto-accepting first-time key...")
return UserDecision.ACCEPT
elif context.prompt_type == PromptType.KEY_CHANGE:
print("Auto-rejecting key change...")
return UserDecision.REJECT
else:
print("Auto-rejecting revoked key...")
return UserDecision.REJECT
# Create callback handler
callback_handler = CallbackInteractiveHandler(custom_prompt_handler)
# Initialize pinning with callback handler
pinning = KeyPinning(
db_path=db_path,
mode=PinningMode.INTERACTIVE,
interactive_handler=callback_handler
)
# Generate demo keys
private_key, public_key = KeyManager.generate_keypair()
public_key_pem = KeyManager.export_public_key_pem(public_key)
# Demo tool information
tool_id = "demo-api-client"
domain = "api.example.com"
developer_name = "API Corp"
print(f"Tool: {tool_id}")
print(f"Domain: {domain}")
print(f"Developer: {developer_name}")
print()
# Pin the key (will use callback)
result = pinning.interactive_pin_key(
tool_id, public_key_pem, domain, developer_name
)
print(f"Result: {'Accepted' if result else 'Rejected'}")
print(f"Key pinned: {pinning.is_key_pinned(tool_id)}")
# Test key change (should be rejected by callback)
if pinning.is_key_pinned(tool_id):
print("\n--- Testing Key Change ---")
new_private_key, new_public_key = KeyManager.generate_keypair()
new_public_key_pem = KeyManager.export_public_key_pem(new_public_key)
result = pinning.interactive_pin_key(
tool_id, new_public_key_pem, domain, developer_name
)
print(f"Key change result: {'Accepted' if result else 'Rejected'}")
finally:
# Cleanup
if os.path.exists(db_path):
os.unlink(db_path)
os.rmdir(temp_dir)
def demo_domain_policies():
"""Demonstrate domain-based pinning policies."""
print("\n=== Domain Policies Demo ===\n")
# Create temporary database
temp_dir = tempfile.mkdtemp()
db_path = os.path.join(temp_dir, "demo_pinning.db")
try:
pinning = KeyPinning(
db_path=db_path,
mode=PinningMode.INTERACTIVE
)
# Generate demo key
private_key, public_key = KeyManager.generate_keypair()
public_key_pem = KeyManager.export_public_key_pem(public_key)
# Test different domain policies
domains = [
("trusted.example.com", PinningPolicy.ALWAYS_TRUST),
("untrusted.example.com", PinningPolicy.NEVER_TRUST),
("normal.example.com", PinningPolicy.DEFAULT)
]
for domain, policy in domains:
print(f"Testing domain: {domain} with policy: {policy.value}")
# Set domain policy
pinning.set_domain_policy(domain, policy)
# Try to pin key
tool_id = f"tool-{domain.split('.')[0]}"
result = pinning.interactive_pin_key(
tool_id, public_key_pem, domain, "Test Developer"
)
print(f" Result: {'Accepted' if result else 'Rejected'}")
print(f" Key pinned: {pinning.is_key_pinned(tool_id)}")
print()
finally:
# Cleanup
if os.path.exists(db_path):
os.unlink(db_path)
os.rmdir(temp_dir)
def demo_schema_verification_with_interactive_pinning():
"""Demonstrate complete schema verification with interactive pinning."""
print("\n=== Schema Verification with Interactive Pinning Demo ===\n")
# Create temporary database
temp_dir = tempfile.mkdtemp()
db_path = os.path.join(temp_dir, "demo_pinning.db")
try:
# Initialize components
pinning = KeyPinning(
db_path=db_path,
mode=PinningMode.AUTOMATIC # Use automatic for demo
)
# Generate developer key pair
private_key, public_key = KeyManager.generate_keypair()
public_key_pem = KeyManager.export_public_key_pem(public_key)
# Create demo schema
schema = {
"name": "calculate_sum",
"description": "Calculate the sum of two numbers",
"parameters": {
"a": {"type": "number", "description": "First number"},
"b": {"type": "number", "description": "Second number"}
}
}
# Sign the schema
canonical_schema = SchemaPinCore.canonicalize_schema(schema)
schema_hash = SchemaPinCore.hash_canonical(canonical_schema)
signature = SignatureManager.sign_schema_hash(schema_hash, private_key)
print("Schema to verify:")
print(json.dumps(schema, indent=2))
print(f"\nSignature: {signature[:50]}...")
print(f"Key fingerprint: {KeyManager.calculate_key_fingerprint(public_key)}")
# Tool information
tool_id = "math-calculator"
domain = "mathtools.example.com"
developer_name = "Math Tools LLC"
# Verify with interactive pinning
print(f"\nVerifying schema for tool: {tool_id}")
# First, handle key pinning
pin_result = pinning.verify_with_interactive_pinning(
tool_id, domain, public_key_pem, developer_name
)
if pin_result:
print("✅ Key pinning successful")
# Now verify the signature
verification_result = SignatureManager.verify_schema_signature(
schema_hash, signature, public_key
)
if verification_result:
print("✅ Schema signature verification successful")
print("🎉 Schema is authentic and can be trusted!")
else:
print("❌ Schema signature verification failed")
else:
print("❌ Key pinning failed - schema cannot be trusted")
# Show pinned keys
print("\nPinned keys in database:")
for key_info in pinning.list_pinned_keys():
print(f" - {key_info['tool_id']} ({key_info['domain']})")
finally:
# Cleanup
if os.path.exists(db_path):
os.unlink(db_path)
os.rmdir(temp_dir)
def main():
"""Run all interactive pinning demos."""
print("SchemaPin Interactive Key Pinning Demo")
print("=" * 50)
# Note: Console demo requires user interaction
# Uncomment the line below to test console interaction
# demo_console_interactive_pinning()
demo_callback_interactive_pinning()
demo_domain_policies()
demo_schema_verification_with_interactive_pinning()
print("\n🎉 All demos completed successfully!")
if __name__ == "__main__":
main()