@adapt-toolkit/mufl 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +172 -0
- package/LICENSE.fsl +105 -0
- package/NOTICE +25 -0
- package/README.md +70 -0
- package/bin/mufl-compile.js +4 -0
- package/bin/mufl.js +4 -0
- package/lib/launcher.js +60 -0
- package/lib/paths.js +26 -0
- package/meta/meta.mm +2666 -0
- package/mufl_stdlib/config.mufl +86 -0
- package/mufl_stdlib/continuation.mm +51 -0
- package/mufl_stdlib/control_packet.mu +184 -0
- package/mufl_stdlib/cryptography/address_document.mm +333 -0
- package/mufl_stdlib/cryptography/address_document_types.mm +16 -0
- package/mufl_stdlib/cryptography/attestation/attestation_document.mm +68 -0
- package/mufl_stdlib/cryptography/attestation/config.mufl +12 -0
- package/mufl_stdlib/cryptography/attestation/container_associated_data.mm +12 -0
- package/mufl_stdlib/cryptography/attestation/control_packet_identity_proof_document.mm +36 -0
- package/mufl_stdlib/cryptography/attestation/identity_proof_document_impl.mm +57 -0
- package/mufl_stdlib/cryptography/attestation/identity_proof_document_types.mm +45 -0
- package/mufl_stdlib/cryptography/attestation/node_description.mm +9 -0
- package/mufl_stdlib/cryptography/attestation/platform_dependent_attestation_document/browser/browser_attestation_document.mm +25 -0
- package/mufl_stdlib/cryptography/attestation/platform_dependent_attestation_document/browser/config.mufl +12 -0
- package/mufl_stdlib/cryptography/attestation/platform_dependent_attestation_document/config.mufl +12 -0
- package/mufl_stdlib/cryptography/attestation/platform_dependent_attestation_document/native/config.mufl +12 -0
- package/mufl_stdlib/cryptography/attestation/platform_dependent_attestation_document/native/native_attestation_document.mm +25 -0
- package/mufl_stdlib/cryptography/attestation/platform_dependent_attestation_document/platform_dependent_attestation_document.mm +57 -0
- package/mufl_stdlib/cryptography/authorizing_container.mm +21 -0
- package/mufl_stdlib/cryptography/config.mufl +12 -0
- package/mufl_stdlib/cryptography/encrypted_channel.mm +95 -0
- package/mufl_stdlib/cryptography/key_storage.mm +391 -0
- package/mufl_stdlib/cryptography/key_storage_api.mm +17 -0
- package/mufl_stdlib/cryptography/key_utils.mm +20 -0
- package/mufl_stdlib/cryptography/peer_container.mu +17 -0
- package/mufl_stdlib/cryptography/registration_proof.mm +40 -0
- package/mufl_stdlib/current_transaction_info.mm +78 -0
- package/mufl_stdlib/identity_proof_document.mm +21 -0
- package/mufl_stdlib/integration_test_api.mm +39 -0
- package/mufl_stdlib/lists.mm +84 -0
- package/mufl_stdlib/permissions.mm +219 -0
- package/mufl_stdlib/platform_type_id.mm +20 -0
- package/mufl_stdlib/random.mm +50 -0
- package/mufl_stdlib/rslt.mm +32 -0
- package/mufl_stdlib/transaction_message_decoder.mm +22 -0
- package/mufl_stdlib/transaction_queue.mm +60 -0
- package/mufl_stdlib/vector.mm +115 -0
- package/mufl_stdlib/verification.mm +35 -0
- package/package.json +43 -0
- package/prebuilds/linux-x64/mufl +0 -0
- package/prebuilds/linux-x64/mufl-compile +0 -0
- package/transactions/__t_wrapper.mm +169 -0
- package/transactions/config.mufl +22 -0
- package/transactions/transaction.mm +200 -0
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
library key_storage loads library key_utils {
|
|
2
|
+
|
|
3
|
+
hidden
|
|
4
|
+
{
|
|
5
|
+
_read_or_abort is (bin->any) = fn (_: bin) { abort "_read_or_abort primitive is unset in this library." when TRUE. }
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
init = fn (_:($_read_or_abort -> read: (bin->any)))
|
|
9
|
+
{
|
|
10
|
+
_read_or_abort -> read.
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
using key_utils.
|
|
14
|
+
metadef t_keypair_info:
|
|
15
|
+
(
|
|
16
|
+
$function->t_function,
|
|
17
|
+
$container_id->t_container_id,
|
|
18
|
+
$pubkey->t_publickey,
|
|
19
|
+
$seckey->t_secretkey+,
|
|
20
|
+
$key_id->t_key_id
|
|
21
|
+
// $time_added->time
|
|
22
|
+
).
|
|
23
|
+
|
|
24
|
+
metadef t_key_storage: t_key_id->>t_keypair_info.
|
|
25
|
+
metadef t_key_attribution: t_container_id->>t_key_id->>t_keypair_info.
|
|
26
|
+
metadef t_default_keys: t_container_id->>t_function->>t_keypair_info.
|
|
27
|
+
metadef t_revoked_keys: t_key_id->>t_keypair_info.
|
|
28
|
+
|
|
29
|
+
address_of_key = fn (pubkey: t_publickey) -> t_container_id {
|
|
30
|
+
RETURN _global_id_of_hash (_value_id ($v -> "adapt/addr/v1", $key -> pubkey)).
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
hidden {
|
|
34
|
+
m_my_container_id IS t_container_id = _get_container_id().
|
|
35
|
+
m_key_storage IS t_key_storage = (,).
|
|
36
|
+
m_key_attribution IS t_key_attribution = (,).
|
|
37
|
+
m_default_keys IS t_default_keys = (,).
|
|
38
|
+
m_revoked_keys IS t_revoked_keys = (,).
|
|
39
|
+
m_is_initialized IS bool = FALSE.
|
|
40
|
+
m_max_key_count_by_function = 6.
|
|
41
|
+
|
|
42
|
+
// saves a specific keypair, where private key might be NIL (when the keypair belongs to another party)
|
|
43
|
+
save_keypair = fn (
|
|
44
|
+
function: t_function,
|
|
45
|
+
container_id: t_container_id,
|
|
46
|
+
pubkey: t_publickey,
|
|
47
|
+
seckey: t_secretkey+,
|
|
48
|
+
def: bool
|
|
49
|
+
) -> nil
|
|
50
|
+
{
|
|
51
|
+
key_id = _crypto_get_key_id pubkey.
|
|
52
|
+
keypair_info = ( $function->function, $container_id->container_id, $pubkey->pubkey, $seckey->seckey, $key_id->key_id ).
|
|
53
|
+
abort "Key storage already contains key pair with this key id" WHEN m_key_storage key_id. // TODO: ???
|
|
54
|
+
m_key_storage key_id -> keypair_info.
|
|
55
|
+
m_key_attribution container_id default(,) key_id -> keypair_info.
|
|
56
|
+
if def {
|
|
57
|
+
m_default_keys container_id default(,) function -> keypair_info.
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
bootstrap_identity = fn (_) -> nil {
|
|
62
|
+
crypto_scheme = _crypto_default_scheme_id().
|
|
63
|
+
sign = _crypto_construct_signing_keypair crypto_scheme.
|
|
64
|
+
addr = address_of_key (sign $public_key).
|
|
65
|
+
_set_container_id addr.
|
|
66
|
+
m_my_container_id -> _get_container_id().
|
|
67
|
+
save_keypair $SIGN m_my_container_id (sign $public_key) (sign $secret_key) TRUE.
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
roll_keys = fn (_) -> nil {
|
|
71
|
+
crypto_scheme = _crypto_default_scheme_id().
|
|
72
|
+
encrypt = _crypto_construct_encryption_keypair crypto_scheme.
|
|
73
|
+
save_keypair $ENCRYPT m_my_container_id (encrypt $public_key) (encrypt $secret_key) TRUE.
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
initialize = fn (_) -> nil {
|
|
77
|
+
abort "Key storage already initialized" WHEN m_is_initialized.
|
|
78
|
+
m_is_initialized->TRUE.
|
|
79
|
+
bootstrap_identity().
|
|
80
|
+
roll_keys().
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
m_approvals IS hash_code(,) = (,).
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
is_container_registered = fn (container_id: t_container_id) -> bool
|
|
87
|
+
{
|
|
88
|
+
RETURN m_key_attribution container_id != NIL.
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// This function takes a new set of keys, and provides information on what has changed,
|
|
92
|
+
// It) -> three lists of changes:
|
|
93
|
+
// those keys that are newly made default
|
|
94
|
+
// those keys that are new
|
|
95
|
+
// those keys that are newly revoked
|
|
96
|
+
// Important: revoked keys are simply those that are not in the set provided.
|
|
97
|
+
|
|
98
|
+
// error indicates what went wrong, if error is NIL means everything checks out.
|
|
99
|
+
// max number of keys in the new set is 5 keys for each function (DOS protection)
|
|
100
|
+
analyse_keyset_difference = fn (
|
|
101
|
+
container_id: t_container_id,
|
|
102
|
+
keys: t_publickey(,),
|
|
103
|
+
default_keys: t_function->>t_key_id
|
|
104
|
+
) -> (
|
|
105
|
+
$new_default_keys->(t_function->>t_publickey),
|
|
106
|
+
$added_keys->(t_function->>t_key_id->>t_publickey),
|
|
107
|
+
$known_keys->(t_function->>t_key_id->>t_publickey),
|
|
108
|
+
$new_revoked_keys->(t_function->>t_key_id->>t_publickey),
|
|
109
|
+
$is_new_container->bool,
|
|
110
|
+
$container_id->t_container_id
|
|
111
|
+
)
|
|
112
|
+
{
|
|
113
|
+
abort "Too many keys" WHEN _count keys > m_max_key_count_by_function * 2.
|
|
114
|
+
|
|
115
|
+
return__new_default_keys IS t_function->>t_publickey = (,).
|
|
116
|
+
return__added_keys IS t_function->>t_key_id->>t_publickey = (,).
|
|
117
|
+
return__known_keys IS t_function->>t_key_id->>t_publickey = (,).
|
|
118
|
+
return__new_revoked_keys IS t_function->>t_key_id->>t_publickey = (,).
|
|
119
|
+
return__is_new_container IS bool = TRUE.
|
|
120
|
+
|
|
121
|
+
// TODO: ensure that enumerated typed dictionary can be interchangeable with records
|
|
122
|
+
count_active_keys IS t_function ->> int = ($ENCRYPT->0,$SIGN->0)as(t_function->>int).
|
|
123
|
+
|
|
124
|
+
sc keys -- (key -> ) {
|
|
125
|
+
key_id = _crypto_get_key_id key.
|
|
126
|
+
function = key_utils::key_get_function key.
|
|
127
|
+
|
|
128
|
+
if m_revoked_keys key_id { // key has previously been revoked
|
|
129
|
+
delete keys key. // remove previously revoked keys from keyset
|
|
130
|
+
} else {
|
|
131
|
+
found IS bool = NIL.
|
|
132
|
+
sc m_key_storage -- ( -> keypair_info) = key_id {
|
|
133
|
+
abort "Key belongs to a different container than reported" WHEN keypair_info $container_id != container_id.
|
|
134
|
+
return__known_keys function default(,) key_id -> key.
|
|
135
|
+
count_active_keys function -> (count_active_keys function |) + 1.
|
|
136
|
+
found -> TRUE.
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if !found {
|
|
140
|
+
count_active_keys function -> (count_active_keys function |) + 1.
|
|
141
|
+
return__added_keys function default(,) key_id -> key.
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
sc count_active_keys -- (function -> count) {
|
|
147
|
+
abort "All active keys for "+ function as(str) +" have been revoked" WHEN count == 0.
|
|
148
|
+
abort "Too many keys for " + function as(str) WHEN count > m_max_key_count_by_function.
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// is this a known container?
|
|
152
|
+
if m_key_attribution container_id != NIL {
|
|
153
|
+
return__is_new_container -> FALSE.
|
|
154
|
+
// find newly revoked keys for this container
|
|
155
|
+
sc m_key_attribution container_id | -- (key_id -> keypair_info) {
|
|
156
|
+
if keys (keypair_info $pubkey) == NIL {
|
|
157
|
+
return__new_revoked_keys (keypair_info $function) default(,) key_id -> keypair_info $pubkey.
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// assert that default keys are present and not revoked
|
|
163
|
+
sc default_keys -- (function -> key_id) {
|
|
164
|
+
abort "Invalid function" WHEN !(($SIGN, $ENCRYPT) function).
|
|
165
|
+
key IS t_publickey+ = NIL.
|
|
166
|
+
|
|
167
|
+
if m_key_storage key_id {
|
|
168
|
+
key->m_key_storage key_id $pubkey |.
|
|
169
|
+
} elif return__added_keys function {
|
|
170
|
+
key->return__added_keys function key_id |.
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
abort "Function mismatch" WHEN function != key_utils::key_get_function key?.
|
|
174
|
+
abort "Key listed as default but not supplied in the keyset or has been revoked" WHEN keys key? == NIL.
|
|
175
|
+
if return__is_new_container || m_default_keys container_id function != key {
|
|
176
|
+
return__new_default_keys function -> key.
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
abort "Missing a default key" WHEN _count return__new_default_keys == 0.
|
|
181
|
+
|
|
182
|
+
res = ($new_default_keys->return__new_default_keys,
|
|
183
|
+
$added_keys->(($SIGN -> (,), $ENCRYPT -> (,)) as (t_function->>t_key_id->>t_publickey))'return__added_keys,
|
|
184
|
+
$known_keys->return__known_keys,
|
|
185
|
+
$new_revoked_keys->return__new_revoked_keys,
|
|
186
|
+
$is_new_container->return__is_new_container,
|
|
187
|
+
$container_id->container_id).
|
|
188
|
+
|
|
189
|
+
m_approvals (_value_id res) -> TRUE.
|
|
190
|
+
RETURN res.
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// This function takes results of analyse_keyset_differences and updates the static structures with the outcome of the analysis
|
|
194
|
+
update_keyset = fn (
|
|
195
|
+
update: ( $new_default_keys->new_default_keys:(t_function->>t_publickey),
|
|
196
|
+
$added_keys->added_keys:(t_function->>t_key_id->>t_publickey),
|
|
197
|
+
$known_keys->(t_function->>t_key_id->>t_publickey),
|
|
198
|
+
$new_revoked_keys->new_revoked_keys:(t_function->>t_key_id->>t_publickey),
|
|
199
|
+
$is_new_container->bool,
|
|
200
|
+
$container_id->container_id:t_container_id
|
|
201
|
+
)
|
|
202
|
+
) {
|
|
203
|
+
abort "Unapproved modification" WHEN m_approvals (_value_id update) == NIL.
|
|
204
|
+
|
|
205
|
+
delete m_approvals (_value_id update).
|
|
206
|
+
sc added_keys -- (function -> ) -- (key_id -> public_key) {
|
|
207
|
+
key_info = ($function->function, $container_id->container_id, $pubkey->public_key, $seckey->NIL, $key_id->key_id).
|
|
208
|
+
m_key_storage key_id -> key_info.
|
|
209
|
+
m_key_attribution container_id default(,) key_id -> key_info.
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
sc new_default_keys -- (function -> public_key) {
|
|
213
|
+
key_info = ($function->function, $container_id->container_id, $pubkey->public_key, $seckey->NIL, $key_id->_crypto_get_key_id public_key).
|
|
214
|
+
m_default_keys container_id default(,) function -> key_info.
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
sc new_revoked_keys -- (_ -> ) -- (key_id -> ) {
|
|
218
|
+
keypair_info = m_key_storage key_id |.
|
|
219
|
+
delete m_key_storage key_id.
|
|
220
|
+
delete m_key_attribution container_id key_id.
|
|
221
|
+
m_revoked_keys key_id -> keypair_info.
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
get_keypair_info = fn (key_id: t_key_id)
|
|
226
|
+
{
|
|
227
|
+
RETURN m_key_storage key_id.
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
default_key = fn (container_id: t_container_id, function: t_function) -> t_publickey+
|
|
231
|
+
{
|
|
232
|
+
if m_default_keys container_id == NIL {
|
|
233
|
+
RETURN NIL.
|
|
234
|
+
} else {
|
|
235
|
+
RETURN m_default_keys container_id function $pubkey.
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
default_sign = fn (message: hash_code) -> crypto_signature
|
|
240
|
+
{
|
|
241
|
+
my_default_keypair_info = m_default_keys m_my_container_id $SIGN |.
|
|
242
|
+
key = (my_default_keypair_info $seckey) as( secretkey_sign ).
|
|
243
|
+
ret = _crypto_sign_hash message key abort "Failed to sign message" WHEN IS NIL.
|
|
244
|
+
RETURN ret.
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
sign = fn (message: hash_code, key_id: t_key_id) -> crypto_signature
|
|
248
|
+
{
|
|
249
|
+
keypair_info = m_key_storage key_id abort "Keypair) not exist for given key_id" WHEN IS NIL.
|
|
250
|
+
key = (keypair_info $seckey) as( secretkey_sign ).
|
|
251
|
+
RETURN _crypto_sign_hash message key.
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
check_signature = fn (message: hash_code, signature: crypto_signature) -> bool
|
|
255
|
+
{
|
|
256
|
+
key_id = _crypto_get_key_id signature.
|
|
257
|
+
keypair_info = m_key_storage key_id abort "Key not found for the signature provided" WHEN IS NIL.
|
|
258
|
+
abort "Invalid key -- wrong function" WHEN keypair_info $function != $SIGN.
|
|
259
|
+
key = (keypair_info $pubkey) SAFE( publickey_sign ).
|
|
260
|
+
res = _crypto_verify_hash_signature message signature key abort "Signature verification failed" WHEN IS NIL.
|
|
261
|
+
RETURN (res == TRUE).
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
container_id_of_signer = fn (signature: crypto_signature) -> t_container_id {
|
|
265
|
+
key_id = _crypto_get_key_id signature.
|
|
266
|
+
keypair_info = m_key_storage key_id abort "Key not found for the signature provided" WHEN IS NIL.
|
|
267
|
+
RETURN (keypair_info $container_id).
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
check_signature_new_container = fn (message: hash_code, signature: crypto_signature, key_list: t_publickey(,)) -> bool
|
|
271
|
+
{
|
|
272
|
+
key_id = _crypto_get_key_id signature.
|
|
273
|
+
key IS publickey_sign+ = NIL.
|
|
274
|
+
sc key_list -- (k->) {
|
|
275
|
+
if _crypto_get_key_id k == key_id {
|
|
276
|
+
key -> k SAFE(publickey_sign).
|
|
277
|
+
break.
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
abort "Key not found for the signature provided" WHEN key == NIL.
|
|
281
|
+
res = _crypto_verify_hash_signature message signature key? abort "Signature verifiction failed" WHEN IS NIL.
|
|
282
|
+
RETURN (res == TRUE).
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
default_encrypt = fn (message: any, recipient_container_id: t_container_id) -> crypto_message
|
|
286
|
+
{
|
|
287
|
+
my_default_keypair_info = m_default_keys m_my_container_id $ENCRYPT |.
|
|
288
|
+
her_default_keypair_info = m_default_keys recipient_container_id $ENCRYPT abort "Default key not known for container id provided" WHEN IS NIL.
|
|
289
|
+
// TODO: create primitives _write_string and _write_binary
|
|
290
|
+
message_body = _write message.
|
|
291
|
+
my_key = (my_default_keypair_info $seckey) as( secretkey_encrypt ).
|
|
292
|
+
her_key = (her_default_keypair_info $pubkey) as( publickey_encrypt ).
|
|
293
|
+
res = _crypto_encrypt_message my_key her_key message_body abort "Error encrypting message" WHEN IS NIL.
|
|
294
|
+
RETURN res.
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
decrypt_message = fn (encrypted_message: crypto_message) -> any
|
|
298
|
+
{
|
|
299
|
+
her_key_id = _crypto_get_key_id encrypted_message.
|
|
300
|
+
my_key_id = _crypto_get_key_id_target encrypted_message.
|
|
301
|
+
|
|
302
|
+
my_keypair_info = m_key_storage my_key_id abort "Unknown target key for message decryption" WHEN IS NIL.
|
|
303
|
+
her_keypair_info = m_key_storage her_key_id abort "Unknown source key for message decryption" WHEN IS NIL.
|
|
304
|
+
|
|
305
|
+
my_key = (my_keypair_info $seckey) as( secretkey_encrypt ).
|
|
306
|
+
her_key = (her_keypair_info $pubkey) as( publickey_encrypt ).
|
|
307
|
+
|
|
308
|
+
result_serialized = _crypto_decrypt_message my_key her_key encrypted_message abort "Decryption failed" WHEN IS NIL.
|
|
309
|
+
result_string = _read_or_abort result_serialized abort "Unable to deserialize message" WHEN IS NIL.
|
|
310
|
+
RETURN result_string.
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
get_source_container = fn (e: crypto) -> t_container_id+
|
|
314
|
+
{
|
|
315
|
+
key_id = _crypto_get_key_id e.
|
|
316
|
+
if m_key_storage key_id == NIL {
|
|
317
|
+
RETURN NIL.
|
|
318
|
+
} else {
|
|
319
|
+
RETURN m_key_storage key_id $container_id.
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
get_target_container = fn (e: crypto) -> t_container_id+
|
|
324
|
+
{
|
|
325
|
+
abort "No target container id for crypto element" WHEN _crypto_get_element_kind e != $ENCRYPTED_DATA.
|
|
326
|
+
key_id = _crypto_get_key_id_target e as(crypto_message).
|
|
327
|
+
if m_key_storage key_id == NIL {
|
|
328
|
+
RETURN NIL.
|
|
329
|
+
} else {
|
|
330
|
+
RETURN m_key_storage key_id $container_id |.
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
get_my_keys = fn (_) -> t_publickey(,)
|
|
335
|
+
{
|
|
336
|
+
|
|
337
|
+
ret IS t_publickey(,) = (,).
|
|
338
|
+
|
|
339
|
+
sc m_key_attribution m_my_container_id | -- (_ -> keypair_info) {
|
|
340
|
+
ret << keypair_info $pubkey -> TRUE.
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
RETURN ret.
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
metadef t_container_identity: (
|
|
347
|
+
$key_list->t_publickey(,), // list of keys active with this container
|
|
348
|
+
$default_keys->t_function->>t_key_id, // exactly two keys, one for signing, one for encryption, that are the default keys for this container
|
|
349
|
+
$container_id->t_container_id // id of the container
|
|
350
|
+
).
|
|
351
|
+
|
|
352
|
+
get_my_identity = fn (_) -> t_container_identity
|
|
353
|
+
{
|
|
354
|
+
return__key_list IS t_publickey(,) = (,).
|
|
355
|
+
|
|
356
|
+
sc m_key_attribution m_my_container_id | -- (_ -> keypair_info) {
|
|
357
|
+
return__key_list (keypair_info $pubkey) -> TRUE.
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
return__default_keys IS t_function->>t_key_id = (,).
|
|
361
|
+
|
|
362
|
+
sc m_default_keys m_my_container_id | -- (function -> keypair_info) {
|
|
363
|
+
return__default_keys function -> keypair_info $key_id.
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
RETURN ($key_list->return__key_list, $default_keys->return__default_keys, $container_id->m_my_container_id).
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
reseed_identity_from_secret = fn (secret: secretkey_sign) -> nil {
|
|
370
|
+
m_key_storage -> (,).
|
|
371
|
+
m_key_attribution -> (,).
|
|
372
|
+
m_default_keys -> (,).
|
|
373
|
+
crypto_scheme = _crypto_default_scheme_id().
|
|
374
|
+
sign = _crypto_signing_keypair_from_secret crypto_scheme secret.
|
|
375
|
+
addr = address_of_key (sign $public_key).
|
|
376
|
+
_set_container_id addr.
|
|
377
|
+
m_my_container_id -> _get_container_id().
|
|
378
|
+
save_keypair $SIGN m_my_container_id (sign $public_key) (sign $secret_key) TRUE.
|
|
379
|
+
roll_keys().
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
export_identity_signing_secret = fn (_) -> secretkey_sign {
|
|
383
|
+
keypair_info = m_default_keys m_my_container_id $SIGN abort "Identity not initialised" WHEN IS NIL.
|
|
384
|
+
RETURN (keypair_info $seckey) as (secretkey_sign).
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
initialize().
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
|
|
391
|
+
// */
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
library key_storage_api loads libraries key_storage, current_transaction_info uses transactions
|
|
2
|
+
{
|
|
3
|
+
trn readonly default_encrypt
|
|
4
|
+
_: (
|
|
5
|
+
$message -> message: any,
|
|
6
|
+
$recipient_container_id -> recipient_container_id: global_id
|
|
7
|
+
) {
|
|
8
|
+
current_transaction_info::validate_origin (transaction::envelope::origin::user,).
|
|
9
|
+
return key_storage::default_encrypt message recipient_container_id.
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
trn readonly decrypt_message encrypted_message: crypto_message {
|
|
13
|
+
current_transaction_info::validate_origin (transaction::envelope::origin::user,).
|
|
14
|
+
return key_storage::decrypt_message encrypted_message.
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
library key_utils {
|
|
2
|
+
metadef t_key_id: bin.
|
|
3
|
+
metadef t_secretkey: (secretkey_encrypt || secretkey_sign).
|
|
4
|
+
metadef t_publickey: (publickey_encrypt || publickey_sign).
|
|
5
|
+
metadef t_function: <$ENCRYPT, $SIGN>.
|
|
6
|
+
metadef t_container_id: global_id.
|
|
7
|
+
|
|
8
|
+
key_get_function = fn (key: t_secretkey || t_publickey) -> t_function
|
|
9
|
+
{
|
|
10
|
+
kind = _crypto_get_element_kind key.
|
|
11
|
+
if kind == $PUBKEY_SIGN || kind == $SECKEY_SIGN {
|
|
12
|
+
return $SIGN.
|
|
13
|
+
} elif kind == $PUBKEY_ENCRYPT || kind == $SECKEY_ENCRYPT {
|
|
14
|
+
return $ENCRYPT.
|
|
15
|
+
} else {
|
|
16
|
+
_abort_transaction "Invalid element passed to key_get_function".
|
|
17
|
+
return $ENCRYPT.
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// container authority protocol
|
|
2
|
+
application address_document_tester loads libraries address_document, key_storage uses transactions
|
|
3
|
+
{
|
|
4
|
+
using key_storage.
|
|
5
|
+
metadef t_secure_participant_information:
|
|
6
|
+
(
|
|
7
|
+
$authorizing_containers->t_container_identity[],
|
|
8
|
+
$authority_token->any,
|
|
9
|
+
$supplementary_data->any
|
|
10
|
+
).
|
|
11
|
+
|
|
12
|
+
trn initialize_secure_participant
|
|
13
|
+
arg: t_secure_participant_information
|
|
14
|
+
{
|
|
15
|
+
RETURN transaction::success [].
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
library registration_proof loads libraries key_storage, key_utils, current_transaction_info uses transactions {
|
|
2
|
+
|
|
3
|
+
payload = fn (nonce: any, cid: key_utils::t_container_id) -> hash_code {
|
|
4
|
+
RETURN _value_id ($v -> "adapt/broker-reg/v1", $nonce -> nonce, $cid -> cid).
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
create = fn (nonce: any) -> ($sign_pub -> any, $signature -> crypto_signature) {
|
|
8
|
+
my_cid = _get_container_id().
|
|
9
|
+
sign_pub = key_storage::default_key my_cid $SIGN.
|
|
10
|
+
sig = key_storage::default_sign (payload nonce my_cid).
|
|
11
|
+
RETURN ($sign_pub -> sign_pub?, $signature -> sig).
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
verify = fn (
|
|
15
|
+
proof: ($sign_pub -> any, $signature -> crypto_signature),
|
|
16
|
+
nonce: any,
|
|
17
|
+
claimed_cid: key_utils::t_container_id ) -> bool
|
|
18
|
+
{
|
|
19
|
+
sign_pub = (proof $sign_pub) SAFE(publickey_sign).
|
|
20
|
+
abort "Registration: container id does not commit to signing key"
|
|
21
|
+
WHEN (key_storage::address_of_key sign_pub) != claimed_cid.
|
|
22
|
+
abort "Registration: possession signature invalid"
|
|
23
|
+
WHEN (_crypto_verify_hash_signature (payload nonce claimed_cid) (proof $signature) sign_pub) == NIL.
|
|
24
|
+
RETURN TRUE.
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
trn readonly create _:($nonce -> nonce: any) {
|
|
28
|
+
current_transaction_info::validate_origin (transaction::envelope::origin::user,).
|
|
29
|
+
return create nonce.
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
trn readonly verify
|
|
33
|
+
_:( $registration_proof -> proof: ($sign_pub -> any, $signature -> crypto_signature),
|
|
34
|
+
$nonce -> nonce: any,
|
|
35
|
+
$cid -> claimed_cid: key_utils::t_container_id )
|
|
36
|
+
{
|
|
37
|
+
current_transaction_info::validate_origin (transaction::envelope::origin::user,).
|
|
38
|
+
return verify proof nonce claimed_cid.
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
library current_transaction_info loads library transaction
|
|
2
|
+
{
|
|
3
|
+
hidden
|
|
4
|
+
{
|
|
5
|
+
get_current = fn (_) -> transaction::envelope::type
|
|
6
|
+
{
|
|
7
|
+
ret = transaction::get_current() abort "Current transaction is not set." when is NIL.
|
|
8
|
+
return ret?.
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
origin_is_external = fn (_) = (transaction::get_origin() == transaction::envelope::origin::external).
|
|
12
|
+
origin_is_user = fn (_) = (transaction::get_origin() == transaction::envelope::origin::user).
|
|
13
|
+
origin_is_local = fn (_) = (transaction::get_origin() == transaction::envelope::origin::local).
|
|
14
|
+
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// return true if at least one of the given origins mathces the actual origin
|
|
18
|
+
validate_origin = fn (allowed_origins: transaction::envelope::origin::type(,)) -> bool {
|
|
19
|
+
sc allowed_origins -- (origin -> ) {
|
|
20
|
+
if (origin == transaction::envelope::origin::external && origin_is_external()) ||
|
|
21
|
+
(origin == transaction::envelope::origin::local && origin_is_local()) ||
|
|
22
|
+
(origin == transaction::envelope::origin::user && origin_is_user()) {
|
|
23
|
+
return TRUE.
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return FALSE.
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
validate_origin_or_abort = fn (allowed_origins: ?validate_origin/reducer) {
|
|
30
|
+
abort "Unexpected transaction origin." when not validate_origin allowed_origins.
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
fn get_origin(_) -> transaction::envelope::origin::type {
|
|
34
|
+
return (transaction::get_origin())?.
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
get_user_envelope = fn (_) -> transaction::envelope::user::type+ {
|
|
38
|
+
if origin_is_user() {
|
|
39
|
+
return (get_current()) as transaction::envelope::user::type.
|
|
40
|
+
}
|
|
41
|
+
return NIL.
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
get_user_envelope_or_abort = fn (_) -> transaction::envelope::user::type {
|
|
45
|
+
ret = get_user_envelope() abort "Invalid transaction origin. Expected: user" when is NIL.
|
|
46
|
+
return ret?.
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
get_local_envelope = fn (_) -> transaction::envelope::local::type+ {
|
|
50
|
+
if origin_is_local() {
|
|
51
|
+
return (get_current()) as transaction::envelope::local::type.
|
|
52
|
+
}
|
|
53
|
+
return NIL.
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
get_local_envelope_or_abort = fn (_) -> transaction::envelope::local::type {
|
|
57
|
+
ret = get_local_envelope() abort "Invalid transaction origin. Expected: local" when is NIL.
|
|
58
|
+
return ret?.
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
get_external_envelope = fn (_) -> transaction::envelope::external::type+ {
|
|
62
|
+
if origin_is_external() {
|
|
63
|
+
return (get_current()) as transaction::envelope::external::type.
|
|
64
|
+
}
|
|
65
|
+
return NIL.
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
get_external_envelope_or_abort = fn (_) -> transaction::envelope::external::type {
|
|
69
|
+
ret = get_external_envelope() abort "Invalid transaction origin. Expected: external" when is NIL.
|
|
70
|
+
return ret?.
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
is_signed = transaction::is_signed.
|
|
74
|
+
|
|
75
|
+
is_encrypted = transaction::is_encrypted.
|
|
76
|
+
|
|
77
|
+
get_transaction_time = transaction::get_transaction_time.
|
|
78
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
library identity_proof_document loads libraries identity_proof_document_impl, current_transaction_info uses transactions {
|
|
2
|
+
create = identity_proof_document_impl::create.
|
|
3
|
+
validate = identity_proof_document_impl::validate.
|
|
4
|
+
validate_signature = identity_proof_document_impl::validate_signature.
|
|
5
|
+
authorize_signature = identity_proof_document_impl::authorize_signature.
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
trn readonly create _ {
|
|
9
|
+
current_transaction_info::validate_origin (transaction::envelope::origin::user,).
|
|
10
|
+
return create ().
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
trn readonly validate
|
|
14
|
+
_:(
|
|
15
|
+
$ip_document -> ip_document: identity_proof_document_impl::t_identity_proof_document,
|
|
16
|
+
$timestamp -> timestamp: time )
|
|
17
|
+
{
|
|
18
|
+
current_transaction_info::validate_origin (transaction::envelope::origin::user,).
|
|
19
|
+
return validate ip_document timestamp.
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
library integration_test_api
|
|
2
|
+
{
|
|
3
|
+
hidden
|
|
4
|
+
{
|
|
5
|
+
prefix = "##grep##".
|
|
6
|
+
print = fn (arg)
|
|
7
|
+
{
|
|
8
|
+
_print prefix arg.
|
|
9
|
+
return _print.
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
stop_enclave = fn (_)
|
|
14
|
+
{
|
|
15
|
+
print "ready_to_stop_enclave".
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
start_enclave = fn (_)
|
|
19
|
+
{
|
|
20
|
+
print "ready_to_start_enclave".
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
exit = fn (exit_code: int)
|
|
24
|
+
{
|
|
25
|
+
print "exit:" exit_code.
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
custom_message = fn (arg)
|
|
29
|
+
{
|
|
30
|
+
print arg.
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
diff_ignore_message = fn (arg)
|
|
34
|
+
{
|
|
35
|
+
print "IGNORE_DIFF " arg.
|
|
36
|
+
return _print.
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
}
|