-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathsecurityPolicy.c
2228 lines (1843 loc) · 59 KB
/
securityPolicy.c
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "addressUtilsShelley.h"
#include "addressUtilsByron.h"
#include "app_mode.h"
#include "bip44.h"
#include "cardano.h"
#include "signTxUtils.h"
#include "securityPolicy.h"
// helper functions
// stake key path has the same account as the payment key path
static inline bool is_standard_base_address(const addressParams_t* addressParams)
{
ASSERT(isValidAddressParams(addressParams));
#define CHECK(cond) if (!(cond)) return false
CHECK(addressParams->type == BASE_PAYMENT_KEY_STAKE_KEY);
CHECK(addressParams->stakingDataSource == STAKING_KEY_PATH);
CHECK(bip44_classifyPath(&addressParams->paymentKeyPath) == PATH_ORDINARY_PAYMENT_KEY);
CHECK(bip44_isPathReasonable(&addressParams->paymentKeyPath));
CHECK(bip44_classifyPath(&addressParams->stakingKeyPath) == PATH_ORDINARY_STAKING_KEY);
CHECK(bip44_isPathReasonable(&addressParams->stakingKeyPath));
// most SW wallets do not use multidelegation,
// so outputs sending funds to such addresses should not be hidden
CHECK(!bip44_isMultidelegationStakingKeyPath(&addressParams->stakingKeyPath));
CHECK(
bip44_getAccount(&addressParams->stakingKeyPath) ==
bip44_getAccount(&addressParams->paymentKeyPath)
);
return true;
#undef CHECK
}
static address_type_t getDestinationAddressType(const tx_output_destination_t* destination)
{
switch (destination->type) {
case DESTINATION_DEVICE_OWNED:
return destination->params->type;
case DESTINATION_THIRD_PARTY:
return getAddressType(destination->address.buffer[0]);
default:
ASSERT(false);
}
}
// useful shortcuts
// WARNING: unless you are doing something exceptional,
// policies must come in the order DENY > WARN > PROMPT/SHOW > ALLOW
#define DENY() return POLICY_DENY;
#define DENY_IF(expr) if (expr) return POLICY_DENY;
#define DENY_UNLESS(expr) if (!(expr)) return POLICY_DENY;
#define WARN() return POLICY_PROMPT_WARN_UNUSUAL;
#define WARN_IF(expr) if (expr) return POLICY_PROMPT_WARN_UNUSUAL;
#define WARN_UNLESS(expr) if (!(expr)) return POLICY_PROMPT_WARN_UNUSUAL;
#define PROMPT() return POLICY_PROMPT_BEFORE_RESPONSE;
#define PROMPT_IF(expr) if (expr) return POLICY_PROMPT_BEFORE_RESPONSE;
#define PROMPT_UNLESS(expr) if (!(expr)) return POLICY_PROMPT_BEFORE_RESPONSE;
#define ALLOW() return POLICY_ALLOW_WITHOUT_PROMPT;
#define ALLOW_IF(expr) if (expr) return POLICY_ALLOW_WITHOUT_PROMPT;
#define SHOW() return POLICY_SHOW_BEFORE_RESPONSE;
#define SHOW_IF(expr) if (expr) return POLICY_SHOW_BEFORE_RESPONSE;
#define SHOW_UNLESS(expr) if (!(expr)) return POLICY_SHOW_BEFORE_RESPONSE;
security_policy_t policyForDerivePrivateKey(const bip44_path_t* path)
{
switch (bip44_classifyPath(path)) {
case PATH_ORDINARY_ACCOUNT:
case PATH_ORDINARY_PAYMENT_KEY:
case PATH_ORDINARY_STAKING_KEY:
case PATH_MULTISIG_ACCOUNT:
case PATH_MULTISIG_PAYMENT_KEY:
case PATH_MULTISIG_STAKING_KEY:
case PATH_DREP_KEY:
case PATH_COMMITTEE_COLD_KEY:
case PATH_COMMITTEE_HOT_KEY:
case PATH_MINT_KEY:
case PATH_POOL_COLD_KEY:
case PATH_CVOTE_ACCOUNT:
case PATH_CVOTE_KEY:
ALLOW();
break;
default:
DENY();
break;
}
DENY(); // should not be reached
}
// Initiate getting extended public key or extended public key bulk
security_policy_t policyForGetPublicKeysInit(uint32_t numPaths)
{
// in a bulk key export, some keys are hidden, the user must be notified
PROMPT_IF(numPaths > 1);
// for a single key, the policy for displaying it is determined later
ALLOW();
}
// Get extended public key and return it to the host
security_policy_t policyForGetExtendedPublicKey(const bip44_path_t* pathSpec)
{
switch (bip44_classifyPath(pathSpec)) {
case PATH_ORDINARY_ACCOUNT:
WARN_UNLESS(bip44_isPathReasonable(pathSpec));
// show Byron paths
PROMPT_UNLESS(bip44_hasShelleyPrefix(pathSpec));
// in expert mode, do not export keys without permission
PROMPT_IF(app_mode_expert());
// do not bother the user with confirmation --- required by LedgerLive to improve UX
ALLOW();
break;
case PATH_MULTISIG_ACCOUNT:
WARN_UNLESS(bip44_isPathReasonable(pathSpec));
// ask for confirmation, multisig users need high awareness of what keys are being used
PROMPT();
break;
case PATH_MINT_KEY:
WARN_UNLESS(bip44_isPathReasonable(pathSpec));
// used rarely, so making the user aware of the export does not hamper him
// but could be relaxed to expert mode if needed
PROMPT();
break;
case PATH_POOL_COLD_KEY:
WARN_UNLESS(bip44_isPathReasonable(pathSpec));
// used rarely, so making the user aware of the export does not hamper him
// but could be relaxed to expert mode if needed
PROMPT();
break;
case PATH_CVOTE_ACCOUNT:
WARN_UNLESS(bip44_isPathReasonable(pathSpec));
// in expert mode, do not export keys without permission
PROMPT_IF(app_mode_expert());
// do not bother the user with confirmation, similar to ordinary account
ALLOW();
break;
case PATH_ORDINARY_PAYMENT_KEY:
case PATH_ORDINARY_STAKING_KEY:
case PATH_MULTISIG_PAYMENT_KEY:
case PATH_MULTISIG_STAKING_KEY:
case PATH_DREP_KEY:
case PATH_COMMITTEE_COLD_KEY:
case PATH_COMMITTEE_HOT_KEY:
case PATH_CVOTE_KEY:
WARN_UNLESS(bip44_isPathReasonable(pathSpec));
// ask for permission (it is unusual if client asks this instead of the account key)
PROMPT();
break;
default:
DENY();
break;
}
DENY(); // should not be reached
}
// Get extended public key and return it to the host within bulk key export
security_policy_t policyForGetExtendedPublicKeyBulkExport(const bip44_path_t* pathSpec)
{
switch (bip44_classifyPath(pathSpec)) {
case PATH_ORDINARY_ACCOUNT:
case PATH_ORDINARY_PAYMENT_KEY:
case PATH_ORDINARY_STAKING_KEY:
case PATH_MULTISIG_ACCOUNT:
case PATH_MULTISIG_PAYMENT_KEY:
case PATH_MULTISIG_STAKING_KEY:
case PATH_DREP_KEY:
case PATH_COMMITTEE_COLD_KEY:
case PATH_COMMITTEE_HOT_KEY:
case PATH_MINT_KEY:
case PATH_CVOTE_ACCOUNT:
case PATH_CVOTE_KEY:
WARN_UNLESS(bip44_isPathReasonable(pathSpec));
// we do not show these paths since there may be many of them
ALLOW();
break;
case PATH_POOL_COLD_KEY:
WARN_UNLESS(bip44_isPathReasonable(pathSpec));
// but ask for permission
PROMPT();
break;
default:
DENY();
break;
}
DENY(); // should not be reached
}
// common policy for DENY and WARN cases in returnDeriveAddress and showDeriveAddress
// successPolicy is returned if no DENY or WARN applies
static security_policy_t _policyForDeriveAddress(const addressParams_t* addressParams, security_policy_t successPolicy)
{
DENY_UNLESS(isValidAddressParams(addressParams));
switch (addressParams->type) {
case BASE_PAYMENT_KEY_STAKE_KEY:
// unusual path
WARN_UNLESS(bip44_isPathReasonable(&addressParams->paymentKeyPath));
WARN_IF(
addressParams->stakingDataSource == STAKING_KEY_PATH &&
!bip44_isPathReasonable(&addressParams->stakingKeyPath)
);
break;
case BASE_PAYMENT_KEY_STAKE_SCRIPT:
case POINTER_KEY:
case ENTERPRISE_KEY:
case BYRON:
// unusual path
WARN_UNLESS(bip44_isPathReasonable(&addressParams->paymentKeyPath));
break;
case BASE_PAYMENT_SCRIPT_STAKE_KEY:
case REWARD_KEY:
// we only support derivation based on key path
DENY_IF(addressParams->stakingDataSource != STAKING_KEY_PATH);
// unusual path
WARN_UNLESS(bip44_isPathReasonable(&addressParams->stakingKeyPath));
break;
case BASE_PAYMENT_SCRIPT_STAKE_SCRIPT:
case POINTER_SCRIPT:
case ENTERPRISE_SCRIPT:
case REWARD_SCRIPT:
// no paths in the address
break;
default:
DENY();
break;
}
return successPolicy;
}
// Derive address and return it to the host
security_policy_t policyForReturnDeriveAddress(const addressParams_t* addressParams)
{
// in expert mode, do not export addresses without permission
security_policy_t policy = app_mode_expert() ?
POLICY_PROMPT_BEFORE_RESPONSE :
POLICY_ALLOW_WITHOUT_PROMPT;
return _policyForDeriveAddress(addressParams, policy);
}
// Derive address and show it to the user
security_policy_t policyForShowDeriveAddress(const addressParams_t* addressParams)
{
return _policyForDeriveAddress(addressParams, POLICY_SHOW_BEFORE_RESPONSE);
}
// true iff network is the standard mainnet or testnet
bool isNetworkUsual(uint32_t networkId, uint32_t protocolMagic)
{
if (networkId == MAINNET_NETWORK_ID && protocolMagic == MAINNET_PROTOCOL_MAGIC) {
return true;
}
const bool knownTestnetProtocolMagic =
protocolMagic == TESTNET_PROTOCOL_MAGIC_LEGACY ||
protocolMagic == TESTNET_PROTOCOL_MAGIC_PREPROD ||
protocolMagic == TESTNET_PROTOCOL_MAGIC_PREVIEW;
if (networkId == TESTNET_NETWORK_ID && knownTestnetProtocolMagic) {
return true;
}
return false;
}
// true iff tx contains an element with network id
bool isTxNetworkIdVerifiable(
bool includeNetworkId,
uint32_t numOutputs,
uint32_t numWithdrawals,
sign_tx_signingmode_t txSigningMode
)
{
if (includeNetworkId) return true;
if (numOutputs > 0) return true;
if (numWithdrawals > 0) return true;
switch (txSigningMode) {
case SIGN_TX_SIGNINGMODE_POOL_REGISTRATION_OPERATOR:
case SIGN_TX_SIGNINGMODE_POOL_REGISTRATION_OWNER:
// pool registration certificate contains pool reward account
return true;
default:
return false;
}
}
bool needsRunningScriptWarning(int32_t numCollateralInputs)
{
return numCollateralInputs > 0;
}
bool needsMissingCollateralWarning(sign_tx_signingmode_t signingMode, uint32_t numCollateralInputs)
{
const bool collateralExpected = (signingMode == SIGN_TX_SIGNINGMODE_PLUTUS_TX);
return collateralExpected && (numCollateralInputs == 0);
}
bool needsUnknownCollateralWarning(sign_tx_signingmode_t signingMode, bool includesTotalCollateral)
{
const bool collateralExpected = (signingMode == SIGN_TX_SIGNINGMODE_PLUTUS_TX);
return collateralExpected && (!includesTotalCollateral);
}
bool needsMissingScriptDataHashWarning(sign_tx_signingmode_t signingMode, bool includesScriptDataHash)
{
const bool scriptDataHashExpected = (signingMode == SIGN_TX_SIGNINGMODE_PLUTUS_TX);
return scriptDataHashExpected && !includesScriptDataHash;
}
// Initiate transaction signing
security_policy_t policyForSignTxInit(
sign_tx_signingmode_t txSigningMode,
uint32_t networkId,
uint32_t protocolMagic,
uint16_t numOutputs,
uint16_t numCertificates,
uint16_t numWithdrawals,
bool includeMint,
bool includeScriptDataHash,
uint16_t numCollateralInputs,
uint16_t numRequiredSigners,
bool includeNetworkId,
bool includeCollateralOutput,
bool includeTotalCollateral,
uint16_t numReferenceInputs,
uint16_t numVotingProcedures,
bool includeTreasury,
bool includeDonation
)
{
DENY_UNLESS(isValidNetworkId(networkId));
// Deny shelley mainnet with weird byron protocol magic
DENY_IF(networkId == MAINNET_NETWORK_ID && protocolMagic != MAINNET_PROTOCOL_MAGIC);
// Note: testnets can still use byron mainnet protocol magic so we can't deny the opposite direction
// certain combinations of tx body elements are forbidden
// mostly because of potential cross-witnessing
switch (txSigningMode) {
case SIGN_TX_SIGNINGMODE_POOL_REGISTRATION_OPERATOR:
case SIGN_TX_SIGNINGMODE_POOL_REGISTRATION_OWNER:
// necessary to avoid intermingling witnesses from several certs
DENY_UNLESS(numCertificates == 1);
// witnesses for owners and withdrawals are the same
// we forbid withdrawals so that users cannot be tricked into witnessing
// something unintentionally (e.g. an owner given by the stake key hash)
DENY_UNLESS(numWithdrawals == 0);
// mint must not be combined with pool registration certificates
DENY_IF(includeMint);
// no Plutus elements for pool registrations
DENY_IF(includeScriptDataHash);
DENY_IF(numCollateralInputs > 0);
DENY_IF(numRequiredSigners > 0);
DENY_IF(includeCollateralOutput);
DENY_IF(includeTotalCollateral);
DENY_IF(numReferenceInputs > 0);
// no voting, treasuries, donations for pool registrations
// we don't need them and we want to avoid overlap in witnesses
DENY_IF(numVotingProcedures > 0);
DENY_IF(includeTreasury);
DENY_IF(includeDonation);
break;
case SIGN_TX_SIGNINGMODE_ORDINARY_TX:
case SIGN_TX_SIGNINGMODE_MULTISIG_TX:
// collateral inputs are allowed only in PLUTUS_TX
DENY_IF(numCollateralInputs > 0);
DENY_IF(includeCollateralOutput);
DENY_IF(includeTotalCollateral);
DENY_IF(numReferenceInputs > 0);
break;
case SIGN_TX_SIGNINGMODE_PLUTUS_TX:
// Plutus script cannot be executed without collateral inputs
WARN_IF(numCollateralInputs == 0);
// Plutus script cannot be executed without script data hash
WARN_UNLESS(includeScriptDataHash);
// warn the user about Plutus script execution itself
WARN();
break;
default:
ASSERT(false);
}
// there are separate screens for various warnings
// the return value of the policy only says that at least one should be applied
// and the need for individual warnings is reassessed in the UI machine
WARN_UNLESS(isTxNetworkIdVerifiable(includeNetworkId, numOutputs, numWithdrawals, txSigningMode));
WARN_UNLESS(isNetworkUsual(networkId, protocolMagic));
WARN_IF(needsRunningScriptWarning(numCollateralInputs));
// these warnings are relevant for Plutus only, but the functions determining
// if a warning is needed are called from UI machines in signTx that are not
// specific for Plutus, so we rather want to keep them here
// instead of moving them to the switch above to be consistent
WARN_IF(needsMissingCollateralWarning(txSigningMode, numCollateralInputs));
WARN_IF(needsUnknownCollateralWarning(txSigningMode, numCollateralInputs));
WARN_IF(needsMissingScriptDataHashWarning(txSigningMode, includeScriptDataHash));
// Could be switched to POLICY_ALLOW_WITHOUT_PROMPT to skip initial "new transaction" question
// but it is safe only for a very narrow set of transactions (e.g. no Plutus)
PROMPT();
}
// For each transaction UTxO input
security_policy_t policyForSignTxInput(sign_tx_signingmode_t txSigningMode)
{
switch (txSigningMode) {
case SIGN_TX_SIGNINGMODE_PLUTUS_TX:
// user should check inputs because they are not interchangeable for Plutus scripts
SHOW_IF(app_mode_expert());
ALLOW();
break;
case SIGN_TX_SIGNINGMODE_ORDINARY_TX:
case SIGN_TX_SIGNINGMODE_POOL_REGISTRATION_OWNER:
case SIGN_TX_SIGNINGMODE_POOL_REGISTRATION_OPERATOR:
case SIGN_TX_SIGNINGMODE_MULTISIG_TX:
// inputs are not interesting for the user (transferred funds are shown in the outputs)
ALLOW();
break;
default:
ASSERT(false);
}
DENY(); // should not be reached
}
static bool is_addressBytes_suitable_for_tx_output(
const uint8_t* addressBuffer, size_t addressSize,
const uint8_t networkId, const uint32_t protocolMagic __attribute__((unused))
)
{
ASSERT(addressSize < BUFFER_SIZE_PARANOIA);
ASSERT(addressSize >= 1);
#define CHECK(cond) if (!(cond)) return false
const address_type_t addressType = getAddressType(addressBuffer[0]);
{
// check address type and network identification
switch (addressType) {
case REWARD_KEY:
case REWARD_SCRIPT:
// outputs may not contain reward addresses
return false;
case BYRON:
#ifdef APP_FEATURE_BYRON_PROTOCOL_MAGIC_CHECK
CHECK(extractProtocolMagic(addressBuffer, addressSize) == protocolMagic);
#endif // APP_FEATURE_BYRON_PROTOCOL_MAGIC_CHECK
break;
default: {
// shelley types allowed in output
const uint8_t addressNetworkId = getNetworkId(addressBuffer[0]);
CHECK(addressNetworkId == networkId);
break;
}
}
}
{
// check address length
switch (addressType) {
case BASE_PAYMENT_KEY_STAKE_KEY:
CHECK(addressSize == 1 + ADDRESS_KEY_HASH_LENGTH + ADDRESS_KEY_HASH_LENGTH);
break;
case BASE_PAYMENT_KEY_STAKE_SCRIPT:
CHECK(addressSize == 1 + ADDRESS_KEY_HASH_LENGTH + SCRIPT_HASH_LENGTH);
break;
case BASE_PAYMENT_SCRIPT_STAKE_KEY:
CHECK(addressSize == 1 + SCRIPT_HASH_LENGTH + ADDRESS_KEY_HASH_LENGTH);
break;
case BASE_PAYMENT_SCRIPT_STAKE_SCRIPT:
CHECK(addressSize == 1 + SCRIPT_HASH_LENGTH + SCRIPT_HASH_LENGTH);
break;
case ENTERPRISE_KEY:
CHECK(addressSize == 1 + ADDRESS_KEY_HASH_LENGTH);
break;
case ENTERPRISE_SCRIPT:
CHECK(addressSize == 1 + SCRIPT_HASH_LENGTH);
break;
default: // not meaningful or complicated to verify address length in the other cases
break;
}
}
return true;
#undef CHECK
}
static bool contains_forbidden_plutus_elements(
const tx_output_description_t* output,
sign_tx_signingmode_t txSigningMode
)
{
if (output->includeDatum || output->includeRefScript) {
// no Plutus elements for pool registration, only allow in other modes
switch (txSigningMode) {
case SIGN_TX_SIGNINGMODE_ORDINARY_TX:
case SIGN_TX_SIGNINGMODE_MULTISIG_TX:
case SIGN_TX_SIGNINGMODE_PLUTUS_TX:
break;
default:
return true;
}
}
return false;
}
bool needsMissingDatumWarning(const tx_output_destination_t* destination, bool includeDatum)
{
const bool mightRequireDatum = determinePaymentChoice(getDestinationAddressType(destination)) == PAYMENT_SCRIPT_HASH;
return mightRequireDatum && !includeDatum;
}
// For each transaction output with third-party address
security_policy_t policyForSignTxOutputAddressBytes(
const tx_output_description_t* output,
sign_tx_signingmode_t txSigningMode,
const uint8_t networkId, const uint32_t protocolMagic
)
{
ASSERT(output->destination.type == DESTINATION_THIRD_PARTY);
const uint8_t* addressBuffer = output->destination.address.buffer;
const size_t addressSize = output->destination.address.size;
DENY_UNLESS(is_addressBytes_suitable_for_tx_output(addressBuffer, addressSize, networkId, protocolMagic));
DENY_IF(contains_forbidden_plutus_elements(output, txSigningMode));
switch (txSigningMode) {
case SIGN_TX_SIGNINGMODE_POOL_REGISTRATION_OWNER:
// all the funds are provided by the operator
// and thus outputs are irrelevant to the owner (even those having tokens or datum hash)
ALLOW();
break;
case SIGN_TX_SIGNINGMODE_ORDINARY_TX:
case SIGN_TX_SIGNINGMODE_MULTISIG_TX:
case SIGN_TX_SIGNINGMODE_POOL_REGISTRATION_OPERATOR:
case SIGN_TX_SIGNINGMODE_PLUTUS_TX:
// utxo on a Plutus script address without datum hash is unspendable
// but we can't DENY because it is valid for native scripts
WARN_IF(needsMissingDatumWarning(&output->destination, output->includeDatum));
// we always show third-party output addresses
SHOW();
break;
default:
ASSERT(false);
}
DENY(); // should not be reached
}
static bool is_addressParams_suitable_for_tx_output(
const addressParams_t* params,
const uint8_t networkId, const uint32_t protocolMagic
)
{
#define CHECK(cond) if (!(cond)) return false
CHECK(isValidAddressParams(params));
// only allow valid address types
// and check network identification as appropriate
switch (params->type) {
case REWARD_KEY:
case REWARD_SCRIPT:
// outputs must not contain reward addresses (true not only for HW wallets)
return false;
case BYRON:
CHECK(params->protocolMagic == protocolMagic);
break;
default: // all Shelley types allowed in output
CHECK(params->networkId == networkId);
break;
}
{
// outputs to a different account within this HW wallet,
// or to a different wallet, should be given as raw address bytes
// this captures the essence of a change output: money stays
// on an address where payment is fully controlled by this device
CHECK(determinePaymentChoice(params->type) == PAYMENT_PATH);
// Note: if we allowed script hash in payment part, we must add a warning
// for missing datum (see policyForSignTxOutputAddressBytes)
ASSERT(determinePaymentChoice(params->type) == PAYMENT_PATH);
CHECK(!violatesSingleAccountOrStoreIt(¶ms->paymentKeyPath));
}
return true;
#undef CHECK
}
// For each output given by payment derivation path
security_policy_t policyForSignTxOutputAddressParams(
const tx_output_description_t* output,
sign_tx_signingmode_t txSigningMode,
const uint8_t networkId, const uint32_t protocolMagic
)
{
ASSERT(output->destination.type == DESTINATION_DEVICE_OWNED);
const addressParams_t* params = output->destination.params;
DENY_UNLESS(is_addressParams_suitable_for_tx_output(params, networkId, protocolMagic));
DENY_IF(contains_forbidden_plutus_elements(output, txSigningMode));
switch (txSigningMode) {
case SIGN_TX_SIGNINGMODE_POOL_REGISTRATION_OPERATOR:
case SIGN_TX_SIGNINGMODE_ORDINARY_TX: {
// unusual paths or payment and staking path mismatch
SHOW_UNLESS(is_standard_base_address(params));
// outputs (eUTXOs) with datum or ref script are not interchangeable
if (output->includeDatum || output->includeRefScript) {
// can't happen for SIGN_TX_SIGNINGMODE_POOL_REGISTRATION_OPERATOR
SHOW_IF(app_mode_expert());
}
// it is safe to hide the remaining change outputs
ALLOW();
break;
}
case SIGN_TX_SIGNINGMODE_MULTISIG_TX: {
// for simplicity, all outputs should be given as external addresses;
// generally, more than one party is needed to sign
// payment from a multisig address, so we do not expect
// there will be 1852 outputs (that would be considered change)
DENY();
break;
}
case SIGN_TX_SIGNINGMODE_PLUTUS_TX: {
// the output could affect script validation so it must not be entirely hidden
// Note: if we relax this, some of the above restrictions may apply
SHOW_IF(app_mode_expert());
ALLOW();
break;
}
case SIGN_TX_SIGNINGMODE_POOL_REGISTRATION_OWNER: {
// we forbid these to avoid leaking information
// (since the outputs are not shown, the user is unaware of what addresses are being derived)
// it also makes the tx signing faster if all outputs are given as addresses
DENY();
break;
}
default:
ASSERT(false);
}
DENY(); // should not be reached
}
security_policy_t policyForSignTxOutputDatumHash(
security_policy_t outputPolicy
)
{
switch (outputPolicy) {
case POLICY_ALLOW_WITHOUT_PROMPT:
// output was not shown, showing datum won't make sense
ALLOW();
break;
case POLICY_SHOW_BEFORE_RESPONSE:
case POLICY_PROMPT_WARN_UNUSUAL:
// non-expert users are not supposed to be able to verify datum or its hash even if they saw it
SHOW_IF(app_mode_expert());
ALLOW();
break;
default:
ASSERT(false);
}
DENY(); // should not be reached
}
security_policy_t policyForSignTxOutputRefScript(
security_policy_t outputPolicy
)
{
switch (outputPolicy) {
case POLICY_ALLOW_WITHOUT_PROMPT:
// output was not shown, showing ref script won't make sense
ALLOW();
break;
case POLICY_SHOW_BEFORE_RESPONSE:
case POLICY_PROMPT_WARN_UNUSUAL:
// non-expert users are not supposed to be able to verify a script even if they saw it
SHOW_IF(app_mode_expert());
ALLOW();
break;
default:
ASSERT(false);
}
DENY(); // should not be reached
}
// For final output confirmation
security_policy_t policyForSignTxOutputConfirm(
security_policy_t outputPolicy,
uint64_t numAssetGroups,
bool containsDatum,
bool containsRefScript
)
{
switch (outputPolicy) {
case POLICY_ALLOW_WITHOUT_PROMPT:
// output was not shown, no confirmation is needed
ALLOW();
break;
case POLICY_SHOW_BEFORE_RESPONSE:
// output was shown and it contained (possibly many) included items (e.g. tokens)
// show a confirmation prompt, so that the user may abort the transaction sooner
PROMPT_IF(numAssetGroups > 0);
PROMPT_IF(containsDatum && app_mode_expert());
PROMPT_IF(containsRefScript && app_mode_expert());
// otherwise no separate confirmation is needed
ALLOW();
break;
case POLICY_PROMPT_WARN_UNUSUAL:
PROMPT();
break;
default:
ASSERT(false);
}
DENY(); // should not be reached
}
static bool is_address_suitable_for_collateral_output(
const tx_output_description_t* output
)
{
switch(getDestinationAddressType(&output->destination)) {
case BASE_PAYMENT_KEY_STAKE_KEY:
case BASE_PAYMENT_KEY_STAKE_SCRIPT:
case POINTER_KEY:
case ENTERPRISE_KEY:
// we only allow addresses with payment controlled by key
return true;
default:
return false;
}
}
security_policy_t policyForSignTxCollateralOutputAddressBytes(
const tx_output_description_t* output,
sign_tx_signingmode_t txSigningMode,
const uint8_t networkId, const uint32_t protocolMagic
)
{
// WARNING: policies for collateral inputs, collateral return output and total collateral are interdependent
ASSERT(output->destination.type == DESTINATION_THIRD_PARTY);
const uint8_t* addressBuffer = output->destination.address.buffer;
const size_t addressSize = output->destination.address.size;
DENY_UNLESS(is_addressBytes_suitable_for_tx_output(addressBuffer, addressSize, networkId, protocolMagic));
DENY_UNLESS(is_address_suitable_for_collateral_output(output));
DENY_IF(output->includeDatum);
DENY_IF(output->includeRefScript);
DENY_IF(txSigningMode != SIGN_TX_SIGNINGMODE_PLUTUS_TX);
SHOW();
}
security_policy_t policyForSignTxCollateralOutputAddressParams(
const tx_output_description_t* output,
sign_tx_signingmode_t txSigningMode,
const uint8_t networkId, const uint32_t protocolMagic,
bool isTotalCollateralIncluded
)
{
// WARNING: policies for collateral inputs, collateral return output and total collateral are interdependent
ASSERT(output->destination.type == DESTINATION_DEVICE_OWNED);
const addressParams_t* params = output->destination.params;
DENY_UNLESS(is_addressParams_suitable_for_tx_output(params, networkId, protocolMagic));
DENY_UNLESS(is_address_suitable_for_collateral_output(output));
DENY_IF(output->includeDatum);
DENY_IF(output->includeRefScript);
switch (txSigningMode) {
case SIGN_TX_SIGNINGMODE_PLUTUS_TX:
if (isTotalCollateralIncluded) {
// change outputs can be hidden
SHOW_UNLESS(is_standard_base_address(params));
ALLOW();
} else {
// collateral output ADA must be shown, so the whole output must be shown
SHOW();
}
break;
default:
// should be used only in Plutus transactions
DENY();
break;
}
DENY(); // should not be reached
}
security_policy_t policyForSignTxCollateralOutputAdaAmount(
security_policy_t outputPolicy,
bool isTotalCollateralPresent
)
{
// WARNING: policies for collateral inputs, collateral return output and total collateral are interdependent
if (outputPolicy == POLICY_ALLOW_WITHOUT_PROMPT) {
// output not shown, so none of its elements should be shown
ALLOW();
}
// ADA amount is calculable from total collateral
// but only expert users are to be bothered by a possible collateral loss
SHOW_IF(!isTotalCollateralPresent && app_mode_expert());
ALLOW();
}
security_policy_t policyForSignTxCollateralOutputTokens(
security_policy_t outputPolicy,
const tx_output_description_t* output
)
{
// WARNING: policies for collateral inputs, collateral return output and total collateral are interdependent
if (outputPolicy == POLICY_ALLOW_WITHOUT_PROMPT) {
// output not shown, so none of its elements should be shown
ALLOW();
}
// for non-change outputs, control over the collateral tokens is potentially transferred to
// another party, so we should show them in the expert mode
// (non-expert users are supposed to not be interested in any collateral loss)
const bool lossOfControl = (output->destination.type != DESTINATION_DEVICE_OWNED);
SHOW_IF(lossOfControl && app_mode_expert());
ALLOW();
}
// For final collateral return output confirmation
security_policy_t policyForSignTxCollateralOutputConfirm(
security_policy_t outputPolicy,
uint64_t numAssetGroups
)
{
// WARNING: policies for collateral inputs, collateral return output and total collateral are interdependent
switch (outputPolicy) {
case POLICY_ALLOW_WITHOUT_PROMPT:
// output was not shown, no confirmation is needed
ALLOW();
break;
case POLICY_SHOW_BEFORE_RESPONSE:
// output was shown and it contained (possibly many) tokens
// show a confirmation prompt, so that the user may abort the transaction sooner
PROMPT_IF(numAssetGroups > 0);
// however, if there were no tokens, no separate confirmation is needed
ALLOW();
break;
case POLICY_PROMPT_WARN_UNUSUAL:
PROMPT();
break;
default:
ASSERT(false);
}
DENY(); // should not be reached
}
// For transaction fee
security_policy_t policyForSignTxFee(
sign_tx_signingmode_t txSigningMode,
uint64_t fee MARK_UNUSED
)
{
switch (txSigningMode) {
case SIGN_TX_SIGNINGMODE_POOL_REGISTRATION_OPERATOR:
case SIGN_TX_SIGNINGMODE_ORDINARY_TX:
case SIGN_TX_SIGNINGMODE_MULTISIG_TX:
case SIGN_TX_SIGNINGMODE_PLUTUS_TX:
// always show the fee if it is paid by the signer
SHOW();
break;
case SIGN_TX_SIGNINGMODE_POOL_REGISTRATION_OWNER:
// fees are paid by the operator and are thus irrelevant for owners
ALLOW();
break;
default:
ASSERT(false);
}
DENY(); // should not be reached
}
// For transaction TTL
security_policy_t policyForSignTxTtl(uint32_t ttl MARK_UNUSED)
{
SHOW_IF(app_mode_expert());
ALLOW();
}
// a generic policy for all certificates
// does not evaluate aspects of specific certificates
security_policy_t policyForSignTxCertificate(
sign_tx_signingmode_t txSigningMode,
const certificate_type_t certificateType