-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_kms.py
More file actions
288 lines (240 loc) · 10.1 KB
/
example_kms.py
File metadata and controls
288 lines (240 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
# -*- coding: utf-8 -*-
import asyncio
from cloudproof_py.cover_crypt import Policy
from cloudproof_py.cover_crypt import PolicyAxis
from cloudproof_py.kms import KmsClient
async def main():
"""Usage example of Cover Crypt with the KMS
Keys generation, encryption and decryption are processed by an external KMS."""
# Creating Policy
policy = Policy()
policy.add_axis(
PolicyAxis(
"Security Level",
[
("Protected", False),
("Confidential", False),
# the following attribute is hybridized allowing post-quantum resistance
("Top Secret", True),
],
hierarchical=True, # this is a hierarchical axis
)
)
policy.add_axis(
PolicyAxis(
"Department",
[("FIN", False), ("MKG", False), ("HR", False)],
hierarchical=False, # this is NOT a hierarchical axis
)
)
# Generating master keys
kms_client = KmsClient(server_url="http://localhost:9998", api_key="")
(
public_key_uid,
private_key_uid,
) = await kms_client.create_cover_crypt_master_key_pair(policy)
# Copy the keys locally for backup
_ = await kms_client.retrieve_cover_crypt_public_master_key(public_key_uid)
_ = await kms_client.retrieve_cover_crypt_private_master_key(private_key_uid)
# Messages encryption
protected_mkg_data = b"protected_mkg_message"
protected_mkg_ciphertext = await kms_client.cover_crypt_encryption(
"Department::MKG && Security Level::Protected",
protected_mkg_data,
public_key_uid,
)
top_secret_mkg_data = b"top_secret_mkg_message"
top_secret_mkg_ciphertext = await kms_client.cover_crypt_encryption(
"Department::MKG && Security Level::Top Secret",
top_secret_mkg_data,
public_key_uid,
)
protected_fin_data = b"protected_fin_message"
protected_fin_ciphertext = await kms_client.cover_crypt_encryption(
"Department::FIN && Security Level::Protected",
protected_fin_data,
public_key_uid,
)
# Generating user keys
confidential_mkg_user_uid = await kms_client.create_cover_crypt_user_decryption_key(
"Department::MKG && Security Level::Confidential",
private_key_uid,
)
topSecret_mkg_fin_user_uid = (
await kms_client.create_cover_crypt_user_decryption_key(
"(Department::MKG || Department::FIN) && Security Level::Top Secret",
private_key_uid,
)
)
# Decryption with the right access policy
protected_mkg_plaintext, _ = await kms_client.cover_crypt_decryption(
protected_mkg_ciphertext, confidential_mkg_user_uid
)
assert protected_mkg_plaintext == protected_mkg_data
# Decryption without the right access will fail
try:
# will throw
await kms_client.cover_crypt_decryption(
top_secret_mkg_ciphertext, confidential_mkg_user_uid
)
except Exception as e:
# ==> the user is not be able to decrypt
print("Expected error:", e)
try:
# will throw
await kms_client.cover_crypt_decryption(
protected_fin_ciphertext, confidential_mkg_user_uid
)
except Exception as e:
# ==> the user is not be able to decrypt
print("Expected error:", e)
# User with Top Secret access can decrypt messages
# of all Security Level within the right Department
protected_mkg_plaintext2, _ = await kms_client.cover_crypt_decryption(
protected_mkg_ciphertext, topSecret_mkg_fin_user_uid
)
assert protected_mkg_plaintext2 == protected_mkg_data
topSecret_mkg_plaintext, _ = await kms_client.cover_crypt_decryption(
top_secret_mkg_ciphertext, topSecret_mkg_fin_user_uid
)
assert topSecret_mkg_plaintext == top_secret_mkg_data
protected_fin_plaintext, _ = await kms_client.cover_crypt_decryption(
protected_fin_ciphertext, topSecret_mkg_fin_user_uid
)
assert protected_fin_plaintext == protected_fin_data
# Rekey
# Rekey all keys having access to "Department::MKG"
# all active keys will be rekeyed automatically
await kms_client.rekey_cover_crypt_access_policy("Department::MKG", private_key_uid)
# New confidential marketing message
confidential_mkg_data = b"confidential_secret_mkg_message"
confidential_mkg_ciphertext = await kms_client.cover_crypt_encryption(
"Department::MKG && Security Level::Confidential",
confidential_mkg_data,
public_key_uid,
)
# Decrypting the messages with the rekeyed key
# decrypting the "old" `protected marketing` message
old_protected_mkg_plaintext, _ = await kms_client.cover_crypt_decryption(
protected_mkg_ciphertext, confidential_mkg_user_uid
)
assert old_protected_mkg_plaintext == protected_mkg_data
# decrypting the "new" `confidential marketing` message
new_confidential_mkg_plaintext, _ = await kms_client.cover_crypt_decryption(
confidential_mkg_ciphertext, confidential_mkg_user_uid
)
assert new_confidential_mkg_plaintext == confidential_mkg_data
# Prune: remove old keys for the MKG attribute
await kms_client.prune_cover_crypt_access_policy("Department::MKG", private_key_uid)
# decrypting old messages will fail
try:
old_protected_mkg_plaintext, _ = await kms_client.cover_crypt_decryption(
protected_mkg_ciphertext, confidential_mkg_user_uid
)
except Exception as e:
# ==> the user is not be able to decrypt
print("Expected error:", e)
# decrypting the "new" message will still work
new_confidential_mkg_plaintext, _ = await kms_client.cover_crypt_decryption(
confidential_mkg_ciphertext, confidential_mkg_user_uid
)
assert new_confidential_mkg_plaintext == confidential_mkg_data
# Edit Policy
# Rename attribute "Department::MKG" to "Department::Marketing"
await kms_client.rename_cover_crypt_attribute(
"Department::MKG", "Marketing", private_key_uid
)
# decryption rights have not been modified even for previously generated keys and ciphers
confidential_mkg_plaintext, _ = await kms_client.cover_crypt_decryption(
confidential_mkg_ciphertext,
confidential_mkg_user_uid,
)
assert confidential_mkg_plaintext == confidential_mkg_data
# new encryption or user key generation must use the new attribute name
topSecret_marketing_data = b"top_secret_marketing_message"
topSecret_marketing_ciphertext = await kms_client.cover_crypt_encryption(
"Department::Marketing && Security Level::Top Secret",
topSecret_marketing_data,
public_key_uid,
)
# new "Marketing" message can still be decrypted with "MKG" keys
topSecret_marketing_plaintext, _ = await kms_client.cover_crypt_decryption(
topSecret_marketing_ciphertext, topSecret_mkg_fin_user_uid
)
assert topSecret_marketing_plaintext == topSecret_marketing_data
# Add attributes
await kms_client.add_cover_crypt_attribute(
"Department::R&D", False, private_key_uid
)
# hierarchical axis are immutable (no addition nor deletion allowed)
try:
await kms_client.add_cover_crypt_attribute(
"Security Level::Classified", False, private_key_uid
)
except Exception as e:
print("Expected error:", e)
# encrypt a message for the newly created `R&D` attribute
protected_rd_data = b"protected_rd_message"
protected_rd_ciphertext = await kms_client.cover_crypt_encryption(
"Department::R&D && Security Level::Protected",
protected_rd_data,
public_key_uid,
)
# and generate a user key with access rights for this attribute
confidential_rd_fin_user_key_uid = (
await kms_client.create_cover_crypt_user_decryption_key(
"(Department::R&D || Department::FIN) && Security Level::Confidential",
private_key_uid,
)
)
# decrypt the R&D message with the new user key
protected_rd_plaintext, _ = await kms_client.cover_crypt_decryption(
protected_rd_ciphertext, confidential_rd_fin_user_key_uid
)
assert protected_rd_plaintext == protected_rd_data
# Removing access to an attribute
# 1 - Keep decryption access to ciphertext from old attributes but remove the right to encrypt new data
await kms_client.disable_cover_crypt_attribute("Department::R&D", private_key_uid)
# this method can also be used on hierarchical axis
await kms_client.disable_cover_crypt_attribute(
"Security Level::Protected", private_key_uid
)
# disabled attributes can no longer be used to encrypt data
# new data encryption for `Department::R&D` will fail
try:
await kms_client.cover_crypt_encryption(
"Department::R&D && Security Level::Protected",
protected_rd_data,
public_key_uid,
)
except Exception as e:
# ==> disabled attributes can no longer be used to encrypt data
print("Expected error:", e)
# decryption of old ciphertext is still possible
new_protected_rd_plaintext, _ = await kms_client.cover_crypt_decryption(
protected_rd_ciphertext, confidential_rd_fin_user_key_uid
)
assert new_protected_rd_plaintext == protected_rd_data
# remove attributes
# /!\ this operation is irreversible and may cause data loss
await kms_client.remove_cover_crypt_attribute("Department::R&D", private_key_uid)
# removing attribute from hierarchical axis is prohibited
try:
await kms_client.remove_cover_crypt_attribute(
"Security Level::Protected", private_key_uid
)
except Exception as e:
print("Expected error:", e)
# removed attributes can no longer be used to encrypt or decrypt
try:
await kms_client.decrypt(
protected_rd_ciphertext,
confidential_rd_fin_user_key_uid,
)
except Exception as e:
# ==> unable to decrypt data for a removed attribute
print("Expected error:", e)
if __name__ == "__main__":
loop = asyncio.new_event_loop()
loop.run_until_complete(main())
loop.close()