-
Notifications
You must be signed in to change notification settings - Fork 24
/
KMKeymasterApplet.java
4633 lines (4325 loc) · 192 KB
/
KMKeymasterApplet.java
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
/*
* Copyright(C) 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.javacard.keymaster;
import javacard.framework.APDU;
import javacard.framework.Applet;
import javacard.framework.AppletEvent;
import javacard.framework.ISO7816;
import javacard.framework.ISOException;
import javacard.framework.JCSystem;
import javacard.framework.Util;
import javacard.security.CryptoException;
import javacardx.apdu.ExtendedLength;
/**
* KMKeymasterApplet implements the javacard applet. It creates repository and other install time
* objects. It also implements the keymaster state machine and handles javacard applet life cycle
* events.
*/
public class KMKeymasterApplet extends Applet implements AppletEvent, ExtendedLength {
// Constants.
public static final byte[] F4 = {0x01, 0x00, 0x01};
public static final byte AES_BLOCK_SIZE = 16;
public static final byte DES_BLOCK_SIZE = 8;
public static final short MAX_LENGTH = (short) 0x2000;
private static final short KM_HAL_VERSION = (short) 0x4000;
private static final short MAX_AUTH_DATA_SIZE = (short) 512;
private static final short POWER_RESET_MASK_FLAG = (short) 0x4000;
// Magic number version
public static final byte KM_MAGIC_NUMBER = (byte) 0x81;
// MSB byte is for Major version and LSB byte is for Minor version.
// Whenever there is an applet upgrade change the version.
public static final short KM_APPLET_PACKAGE_VERSION = 0x0300; // 3.0
public static final short KM_APPLET_PACKAGE_VERSION_2_0 = 0x0200; // 2.0
// "Keymaster HMAC Verification" - used for HMAC key verification.
public static final byte[] sharingCheck = {
0x4B, 0x65, 0x79, 0x6D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x20, 0x48, 0x4D, 0x41, 0x43, 0x20,
0x56,
0x65, 0x72, 0x69, 0x66, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6F, 0x6E
};
// "KeymasterSharedMac"
public static final byte[] ckdfLable = {
0x4B, 0x65, 0x79, 0x6D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x53, 0x68, 0x61, 0x72, 0x65, 0x64,
0x4D,
0x61, 0x63
};
// "Auth Verification"
public static final byte[] authVerification = {
0x41, 0x75, 0x74, 0x68, 0x20, 0x56, 0x65, 0x72, 0x69, 0x66, 0x69, 0x63, 0x61, 0x74, 0x69,
0x6F,
0x6E
};
// "confirmation token"
public static final byte[] confirmationToken = {
0x63, 0x6F, 0x6E, 0x66, 0x69, 0x72, 0x6D, 0x61, 0x74, 0x69, 0x6F, 0x6E, 0x20, 0x74, 0x6F,
0x6B,
0x65, 0x6E
};
// getHardwareInfo constants.
private static final byte[] JAVACARD_KEYMASTER_DEVICE = {
0x4A, 0x61, 0x76, 0x61, 0x63, 0x61, 0x72, 0x64, 0x4B, 0x65, 0x79, 0x6D, 0x61, 0x73, 0x74,
0x65, 0x72, 0x44, 0x65, 0x76, 0x69, 0x63, 0x65,
};
private static final byte[] GOOGLE = {0x47, 0x6F, 0x6F, 0x67, 0x6C, 0x65};
// OEM lock / unlock verification constants.
private static final byte[] OEM_LOCK_VERIFICATION_LABEL = { // "OEM Provisioning Lock"
0x4f, 0x45, 0x4d, 0x20, 0x50, 0x72, 0x6f, 0x76, 0x69, 0x73, 0x69, 0x6f, 0x6e, 0x69, 0x6e,
0x67, 0x20, 0x4c, 0x6f, 0x63, 0x6b
};
private static final byte[] OEM_UNLOCK_VERIFICATION_LABEL = { // "Enable RMA"
0x45, 0x6e, 0x61, 0x62, 0x6c, 0x65, 0x20, 0x52, 0x4d, 0x41
};
// Attestation IDs
private static final short[] ATTEST_ID_TAGS = {
KMType.ATTESTATION_ID_BRAND,
KMType.ATTESTATION_ID_DEVICE,
KMType.ATTESTATION_ID_IMEI,
KMType.ATTESTATION_ID_MANUFACTURER,
KMType.ATTESTATION_ID_MEID,
KMType.ATTESTATION_ID_MODEL,
KMType.ATTESTATION_ID_PRODUCT,
KMType.ATTESTATION_ID_SERIAL
};
// Commands
private static final byte INS_BEGIN_KM_CMD = 0x00;
// Instructions for Provision Commands.
private static final byte INS_PROVISION_ATTESTATION_KEY_CMD = INS_BEGIN_KM_CMD + 1; //0x01
private static final byte INS_PROVISION_ATTESTATION_CERT_DATA_CMD = INS_BEGIN_KM_CMD + 2; //0x02
private static final byte INS_PROVISION_ATTEST_IDS_CMD = INS_BEGIN_KM_CMD + 3; //0x03
private static final byte INS_PROVISION_PRESHARED_SECRET_CMD = INS_BEGIN_KM_CMD + 4; //0x04
private static final byte INS_SET_BOOT_PARAMS_CMD = INS_BEGIN_KM_CMD + 5; //0x05
private static final byte INS_OEM_LOCK_PROVISIONING_CMD = INS_BEGIN_KM_CMD + 6; //0x06
private static final byte INS_GET_PROVISION_STATUS_CMD = INS_BEGIN_KM_CMD + 7; //0x07
private static final byte INS_SET_VERSION_PATCHLEVEL_CMD = INS_BEGIN_KM_CMD + 8; //0x08
private static final byte INS_SET_BOOT_ENDED_CMD = INS_BEGIN_KM_CMD + 9; //0x09 // Unused
private static final byte INS_SE_FACTORY_LOCK_PROVISIONING_CMD = INS_BEGIN_KM_CMD + 10; //0x0A
private static final byte INS_PROVISION_OEM_ROOT_PUBLIC_KEY_CMD = INS_BEGIN_KM_CMD + 11; //0x0B
private static final byte INS_OEM_UNLOCK_PROVISIONING_CMD = INS_BEGIN_KM_CMD + 12; //0x0C
// Top 32 commands are reserved for provisioning.
private static final byte INS_END_KM_PROVISION_CMD = 0x20;
private static final byte INS_GENERATE_KEY_CMD = INS_END_KM_PROVISION_CMD + 1; //0x21
private static final byte INS_IMPORT_KEY_CMD = INS_END_KM_PROVISION_CMD + 2; //0x22
private static final byte INS_IMPORT_WRAPPED_KEY_CMD = INS_END_KM_PROVISION_CMD + 3; //0x23
private static final byte INS_EXPORT_KEY_CMD = INS_END_KM_PROVISION_CMD + 4; //0x24
private static final byte INS_ATTEST_KEY_CMD = INS_END_KM_PROVISION_CMD + 5; //0x25
private static final byte INS_UPGRADE_KEY_CMD = INS_END_KM_PROVISION_CMD + 6; //0x26
private static final byte INS_DELETE_KEY_CMD = INS_END_KM_PROVISION_CMD + 7; //0x27
private static final byte INS_DELETE_ALL_KEYS_CMD = INS_END_KM_PROVISION_CMD + 8; //0x28
private static final byte INS_ADD_RNG_ENTROPY_CMD = INS_END_KM_PROVISION_CMD + 9; //0x29
private static final byte INS_COMPUTE_SHARED_HMAC_CMD = INS_END_KM_PROVISION_CMD + 10; //0x2A
private static final byte INS_DESTROY_ATT_IDS_CMD = INS_END_KM_PROVISION_CMD + 11; //0x2B
private static final byte INS_VERIFY_AUTHORIZATION_CMD = INS_END_KM_PROVISION_CMD + 12; //0x2C
private static final byte INS_GET_HMAC_SHARING_PARAM_CMD = INS_END_KM_PROVISION_CMD + 13; //0x2D
private static final byte INS_GET_KEY_CHARACTERISTICS_CMD = INS_END_KM_PROVISION_CMD + 14; //0x2E
private static final byte INS_GET_HW_INFO_CMD = INS_END_KM_PROVISION_CMD + 15; //0x2F
private static final byte INS_BEGIN_OPERATION_CMD = INS_END_KM_PROVISION_CMD + 16; //0x30
private static final byte INS_UPDATE_OPERATION_CMD = INS_END_KM_PROVISION_CMD + 17; //0x31
private static final byte INS_FINISH_OPERATION_CMD = INS_END_KM_PROVISION_CMD + 18; //0x32
private static final byte INS_ABORT_OPERATION_CMD = INS_END_KM_PROVISION_CMD + 19; //0x33
private static final byte INS_DEVICE_LOCKED_CMD = INS_END_KM_PROVISION_CMD + 20;//0x34
private static final byte INS_EARLY_BOOT_ENDED_CMD = INS_END_KM_PROVISION_CMD + 21; //0x35
private static final byte INS_GET_CERT_CHAIN_CMD = INS_END_KM_PROVISION_CMD + 22; //0x36
private static final byte INS_END_KM_CMD = 0x7F;
// Provision reporting status
protected static final byte NOT_PROVISIONED = 0x00;
protected static final byte PROVISION_STATUS_ATTESTATION_KEY = 0x01;
private static final byte PROVISION_STATUS_ATTESTATION_CERT_CHAIN = 0x02;
private static final byte PROVISION_STATUS_ATTESTATION_CERT_PARAMS = 0x04;
protected static final byte PROVISION_STATUS_ATTEST_IDS = 0x08;
protected static final byte PROVISION_STATUS_PRESHARED_SECRET = 0x10;
protected static final byte PROVISION_STATUS_OEM_PROVISIONING_LOCKED = 0x20;
protected static final byte PROVISION_STATUS_SE_FACTORY_PROVISIONING_LOCKED = 0x40;
protected static final byte PROVISION_STATUS_OEM_ROOT_PUBLIC_KEY = (byte) 0x80;
// Data Dictionary items
public static final byte DATA_ARRAY_SIZE = 31;
public static final byte TMP_VARIABLE_ARRAY_SIZE = 20;
public static final byte UPDATE_PARAM_ARRAY_SIZE = 40;
public static final byte KEY_PARAMETERS = 0;
public static final byte KEY_CHARACTERISTICS = 1;
public static final byte HIDDEN_PARAMETERS = 2;
public static final byte HW_PARAMETERS = 3;
public static final byte SW_PARAMETERS = 4;
public static final byte AUTH_DATA = 5;
public static final byte AUTH_TAG = 6;
public static final byte NONCE = 7;
public static final byte KEY_BLOB = 8;
public static final byte AUTH_DATA_LENGTH = 9;
public static final byte SECRET = 10;
public static final byte ROT = 11;
public static final byte DERIVED_KEY = 12;
public static final byte RSA_PUB_EXPONENT = 13;
public static final byte APP_ID = 14;
public static final byte APP_DATA = 15;
public static final byte PUB_KEY = 16;
public static final byte IMPORTED_KEY_BLOB = 17;
public static final byte ORIGIN = 18;
public static final byte ENC_TRANSPORT_KEY = 19;
public static final byte MASKING_KEY = 20;
public static final byte HMAC_SHARING_PARAMS = 21;
public static final byte OP_HANDLE = 22;
public static final byte IV = 23;
public static final byte INPUT_DATA = 24;
public static final byte OUTPUT_DATA = 25;
public static final byte HW_TOKEN = 26;
public static final byte VERIFICATION_TOKEN = 27;
public static final byte SIGNATURE = 28;
public static final byte KEY_BLOB_VERSION_DATA_OFFSET = 29;
public static final byte CUSTOM_TAGS = 30;
// AddRngEntropy
protected static final short MAX_SEED_SIZE = 2048;
// Keyblob constants
public static final byte KEY_BLOB_VERSION_OFFSET = 0;
public static final byte KEY_BLOB_SECRET = 1;
public static final byte KEY_BLOB_NONCE = 2;
public static final byte KEY_BLOB_AUTH_TAG = 3;
public static final byte KEY_BLOB_KEYCHAR = 4;
public static final byte KEY_BLOB_CUSTOM_TAGS = 5;
public static final byte KEY_BLOB_PUB_KEY = 6;
//KeyBlob array size constants.
public static final byte SYM_KEY_BLOB_SIZE_V1 = 6;
public static final byte ASYM_KEY_BLOB_SIZE_V1 = 7;
public static final byte SYM_KEY_BLOB_SIZE_V0 = 4;
public static final byte ASYM_KEY_BLOB_SIZE_V0 = 5;
// Key type constants
public static final byte SYM_KEY_TYPE = 0;
public static final byte ASYM_KEY_TYPE = 1;
// AES GCM constants
private static final byte AES_GCM_AUTH_TAG_LENGTH = 16;
private static final byte AES_GCM_NONCE_LENGTH = 12;
// ComputeHMAC constants
private static final short HMAC_SHARED_PARAM_MAX_SIZE = 64;
// Maximum certificate size.
private static final short MAX_CERT_SIZE = 3000;
// Buffer constants.
private static final short BUF_START_OFFSET = 0;
private static final short BUF_LEN_OFFSET = 2;
//KEYBLOB_CURRENT_VERSION goes into KeyBlob and will affect all
// the KeyBlobs if it is changed. please increment this
// version number whenever you change anything related to
// KeyBlob (structure, encryption algorithm etc).
public static final short KEYBLOB_CURRENT_VERSION = 1;
// KeyBlob Verion 1 constant.
public static final short KEYBLOB_VERSION_0 = 0;
// Device boot states. Applet starts executing the
// core commands once all the states are set. The commands
// that are allowed irrespective of these states are:
// All the provision commands
// INS_GET_HW_INFO_CMD
// INS_ADD_RNG_ENTROPY_CMD
// INS_COMPUTE_SHARED_HMAC_CMD
// INS_GET_HMAC_SHARING_PARAM_CMD
// INS_EARLY_BOOT_ENDED
public static final byte SET_BOOT_PARAMS_SUCCESS = 0x01;
public static final byte SET_SYSTEM_PROPERTIES_SUCCESS = 0x02;
public static final byte NEGOTIATED_SHARED_SECRET_SUCCESS = 0x04;
// Keymaster Applet attributes
protected static byte keymasterState;
protected static KMEncoder encoder;
protected static KMDecoder decoder;
protected static KMRepository repository;
protected static KMSEProvider seProvider;
protected static Object[] bufferRef;
protected static short[] bufferProp;
protected static short[] tmpVariables;
protected static short[] data;
protected static byte provisionStatus = NOT_PROVISIONED;
// First two bytes are Major version and second bytes are minor version.
protected short packageVersion;
/**
* Registers this applet.
*/
protected KMKeymasterApplet(KMSEProvider seImpl) {
seProvider = seImpl;
boolean isUpgrading = seImpl.isUpgrading();
repository = new KMRepository(isUpgrading);
initializeTransientArrays();
if (!isUpgrading) {
keymasterState = KMAppletState.INIT_STATE;
seProvider.createMasterKey((short) (KMRepository.MASTER_KEY_SIZE * 8));
}
packageVersion = KM_APPLET_PACKAGE_VERSION;
KMType.initialize();
encoder = new KMEncoder();
decoder = new KMDecoder();
}
private void initializeTransientArrays() {
data = JCSystem.makeTransientShortArray((short) DATA_ARRAY_SIZE, JCSystem.CLEAR_ON_RESET);
bufferRef = JCSystem.makeTransientObjectArray((short) 1, JCSystem.CLEAR_ON_RESET);
bufferProp = JCSystem.makeTransientShortArray((short) 4, JCSystem.CLEAR_ON_RESET);
tmpVariables =
JCSystem.makeTransientShortArray((short) TMP_VARIABLE_ARRAY_SIZE, JCSystem.CLEAR_ON_RESET);
bufferProp[BUF_START_OFFSET] = 0;
bufferProp[BUF_LEN_OFFSET] = 0;
}
/**
* Selects this applet.
*
* @return Returns true if the keymaster is in correct state
*/
@Override
public boolean select() {
repository.onSelect();
if (keymasterState == KMAppletState.INIT_STATE) {
keymasterState = KMAppletState.IN_PROVISION_STATE;
}
return true;
}
/**
* De-selects this applet.
*/
@Override
public void deselect() {
repository.onDeselect();
}
/**
* Uninstalls the applet after cleaning the repository.
*/
@Override
public void uninstall() {
repository.onUninstall();
}
private short mapISOErrorToKMError(short reason) {
switch (reason) {
case ISO7816.SW_CLA_NOT_SUPPORTED:
return KMError.UNSUPPORTED_CLA;
case ISO7816.SW_CONDITIONS_NOT_SATISFIED:
return KMError.SW_CONDITIONS_NOT_SATISFIED;
case ISO7816.SW_COMMAND_NOT_ALLOWED:
return KMError.CMD_NOT_ALLOWED;
case ISO7816.SW_DATA_INVALID:
return KMError.INVALID_DATA;
case ISO7816.SW_INCORRECT_P1P2:
return KMError.INVALID_P1P2;
case ISO7816.SW_INS_NOT_SUPPORTED:
return KMError.UNSUPPORTED_INSTRUCTION;
case ISO7816.SW_WRONG_LENGTH:
return KMError.SW_WRONG_LENGTH;
case ISO7816.SW_UNKNOWN:
default:
return KMError.UNKNOWN_ERROR;
}
}
private short mapCryptoErrorToKMError(short reason) {
switch (reason) {
case CryptoException.ILLEGAL_USE:
return KMError.CRYPTO_ILLEGAL_USE;
case CryptoException.ILLEGAL_VALUE:
return KMError.CRYPTO_ILLEGAL_VALUE;
case CryptoException.INVALID_INIT:
return KMError.CRYPTO_INVALID_INIT;
case CryptoException.NO_SUCH_ALGORITHM:
return KMError.CRYPTO_NO_SUCH_ALGORITHM;
case CryptoException.UNINITIALIZED_KEY:
return KMError.CRYPTO_UNINITIALIZED_KEY;
default:
return KMError.UNKNOWN_ERROR;
}
}
protected void validateApduHeader(APDU apdu) {
// Read the apdu header and buffer.
byte[] apduBuffer = apdu.getBuffer();
short P1P2 = Util.getShort(apduBuffer, ISO7816.OFFSET_P1);
// Validate CLA
if (!seProvider.isValidCLA(apdu)) {
ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
}
// Validate P1P2.
if (P1P2 != KMKeymasterApplet.KM_HAL_VERSION) {
ISOException.throwIt(ISO7816.SW_INCORRECT_P1P2);
}
}
/**
* Processes an incoming APDU and handles it using command objects.
*
* @param apdu the incoming APDU
*/
@Override
public void process(APDU apdu) {
try {
resetTransientBuffers();
// Handle the card reset status before processing apdu.
if (repository.isPowerResetEventOccurred()) {
// Release all the operation instances.
seProvider.releaseAllOperations();
}
repository.onProcess();
// Verify whether applet is in correct state.
if (keymasterState == KMAppletState.INIT_STATE) {
ISOException.throwIt(ISO7816.SW_CONDITIONS_NOT_SATISFIED);
}
// If this is select applet apdu which is selecting this applet then
// return
if (apdu.isISOInterindustryCLA()) {
if (selectingApplet()) {
return;
}
}
// Validate APDU Header.
validateApduHeader(apdu);
byte[] apduBuffer = apdu.getBuffer();
byte apduIns = apduBuffer[ISO7816.OFFSET_INS];
// Validate whether INS can be supported
if (!(apduIns > INS_BEGIN_KM_CMD && apduIns < INS_END_KM_CMD)) {
ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
}
bufferRef[0] = repository.getHeap();
// Process the apdu
// Below instructions are allowed in both active state and provision state.
switch (apduIns) {
case INS_SET_BOOT_PARAMS_CMD:
// Allow set boot params only when the host device reboots and the applet is in
// active state. If host does not support boot signal event, then allow this
// instruction any time.
if (seProvider.isBootSignalEventSupported()
&& (keymasterState == KMAppletState.ACTIVE_STATE)
&& (!seProvider.isDeviceRebooted())) {
ISOException.throwIt(ISO7816.SW_COMMAND_NOT_ALLOWED);
}
// clear the device reboot status
repository.setDeviceBootStatus((byte) 0x00);
processSetBootParamsCmd(apdu);
//set the flag to mark boot started
repository.setDeviceBootStatus(SET_BOOT_PARAMS_SUCCESS);
seProvider.clearDeviceBooted(false);
sendResponse(apdu, KMError.OK);
return;
case INS_GET_PROVISION_STATUS_CMD:
processGetProvisionStatusCmd(apdu);
return;
default:
// Fallback to instructions specific to either provision state or active state
// or both.
break;
}
// Below instructions are allowed in only provision state.
if (keymasterState == KMAppletState.IN_PROVISION_STATE) {
switch (apduIns) {
case INS_PROVISION_ATTESTATION_KEY_CMD:
if (!isSEFactoryProvisioningLocked()) {
processProvisionAttestationKey(apdu);
provisionStatus |= KMKeymasterApplet.PROVISION_STATUS_ATTESTATION_KEY;
sendResponse(apdu, KMError.OK);
} else {
ISOException.throwIt(ISO7816.SW_COMMAND_NOT_ALLOWED);
}
return;
case INS_PROVISION_ATTESTATION_CERT_DATA_CMD:
if (!isSEFactoryProvisioningLocked()) {
processProvisionAttestationCertDataCmd(apdu);
provisionStatus |= (KMKeymasterApplet.PROVISION_STATUS_ATTESTATION_CERT_CHAIN |
KMKeymasterApplet.PROVISION_STATUS_ATTESTATION_CERT_PARAMS);
sendResponse(apdu, KMError.OK);
} else {
ISOException.throwIt(ISO7816.SW_COMMAND_NOT_ALLOWED);
}
return;
case INS_SE_FACTORY_LOCK_PROVISIONING_CMD:
if (isSEFactoryProvisioningComplete()) {
provisionStatus |= KMKeymasterApplet.PROVISION_STATUS_SE_FACTORY_PROVISIONING_LOCKED;
sendResponse(apdu, KMError.OK);
} else {
ISOException.throwIt(ISO7816.SW_COMMAND_NOT_ALLOWED);
}
return;
case INS_PROVISION_OEM_ROOT_PUBLIC_KEY_CMD:
processProvisionOEMRootPublicKeyCmd(apdu);
provisionStatus |= KMKeymasterApplet.PROVISION_STATUS_OEM_ROOT_PUBLIC_KEY;
sendResponse(apdu, KMError.OK);
return;
case INS_PROVISION_ATTEST_IDS_CMD:
processProvisionAttestIdsCmd(apdu);
provisionStatus |= KMKeymasterApplet.PROVISION_STATUS_ATTEST_IDS;
sendResponse(apdu, KMError.OK);
return;
case INS_PROVISION_PRESHARED_SECRET_CMD:
processProvisionSharedSecretCmd(apdu);
provisionStatus |= KMKeymasterApplet.PROVISION_STATUS_PRESHARED_SECRET;
sendResponse(apdu, KMError.OK);
return;
case INS_OEM_LOCK_PROVISIONING_CMD:
// Allow lock only when
// 1. All the necessary provisioning commands are successfully executed
// 2. SE provision is locked
// 3. OEM Root Public is provisioned.
if (isProvisioningComplete() &&
(0 != (provisionStatus & PROVISION_STATUS_OEM_ROOT_PUBLIC_KEY)) &&
(0 != (provisionStatus & PROVISION_STATUS_SE_FACTORY_PROVISIONING_LOCKED)) ) {
processOEMLockProvisionCmd(apdu);
} else {
ISOException.throwIt(ISO7816.SW_COMMAND_NOT_ALLOWED);
}
return;
case INS_OEM_UNLOCK_PROVISIONING_CMD:
// UNLOCK command not allowed in IN_PROVISION_STATE
ISOException.throwIt(ISO7816.SW_COMMAND_NOT_ALLOWED);
return;
default:
// Fallback to instructions specific to either active state or
// provision completed but not locked state.
break;
}
}
// Below instructions are allowed only in active state and provision completed state.
if ((keymasterState == KMAppletState.ACTIVE_STATE)
|| ((keymasterState == KMAppletState.IN_PROVISION_STATE)
&& isProvisioningComplete())) {
if (!isKeymasterReady(apduIns)) {
KMException.throwIt(KMError.UNKNOWN_ERROR);
}
switch (apduIns) {
case INS_GENERATE_KEY_CMD:
processGenerateKey(apdu);
break;
case INS_IMPORT_KEY_CMD:
processImportKeyCmd(apdu);
break;
case INS_IMPORT_WRAPPED_KEY_CMD:
processImportWrappedKeyCmd(apdu);
break;
case INS_EXPORT_KEY_CMD:
processExportKeyCmd(apdu);
break;
case INS_ATTEST_KEY_CMD:
processAttestKeyCmd(apdu);
break;
case INS_UPGRADE_KEY_CMD:
processUpgradeKeyCmd(apdu);
break;
case INS_DELETE_KEY_CMD:
processDeleteKeyCmd(apdu);
break;
case INS_DELETE_ALL_KEYS_CMD:
processDeleteAllKeysCmd(apdu);
break;
case INS_ADD_RNG_ENTROPY_CMD:
processAddRngEntropyCmd(apdu);
break;
case INS_COMPUTE_SHARED_HMAC_CMD:
processComputeSharedHmacCmd(apdu);
break;
case INS_DESTROY_ATT_IDS_CMD:
processDestroyAttIdsCmd(apdu);
break;
case INS_VERIFY_AUTHORIZATION_CMD:
processVerifyAuthorizationCmd(apdu);
break;
case INS_GET_HMAC_SHARING_PARAM_CMD:
processGetHmacSharingParamCmd(apdu);
break;
case INS_GET_KEY_CHARACTERISTICS_CMD:
processGetKeyCharacteristicsCmd(apdu);
break;
case INS_GET_HW_INFO_CMD:
processGetHwInfoCmd(apdu);
break;
case INS_BEGIN_OPERATION_CMD:
processBeginOperationCmd(apdu);
break;
case INS_UPDATE_OPERATION_CMD:
processUpdateOperationCmd(apdu);
break;
case INS_FINISH_OPERATION_CMD:
processFinishOperationCmd(apdu);
break;
case INS_ABORT_OPERATION_CMD:
processAbortOperationCmd(apdu);
break;
case INS_DEVICE_LOCKED_CMD:
processDeviceLockedCmd(apdu);
break;
case INS_EARLY_BOOT_ENDED_CMD:
processEarlyBootEndedCmd(apdu);
break;
case INS_GET_CERT_CHAIN_CMD:
processGetCertChainCmd(apdu);
break;
case INS_SET_VERSION_PATCHLEVEL_CMD:
processSetVersionAndPatchLevels(apdu);
break;
case INS_OEM_UNLOCK_PROVISIONING_CMD:
processOEMUnlockProvisionCmd(apdu);
break;
default:
ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
}
} else {
ISOException.throwIt(ISO7816.SW_COMMAND_NOT_ALLOWED);
}
} catch (KMException exception) {
freeOperations();
sendResponse(apdu, KMException.getReason());
exception.clear();
} catch (ISOException exp) {
freeOperations();
sendResponse(apdu, mapISOErrorToKMError(exp.getReason()));
} catch (CryptoException e) {
freeOperations();
sendResponse(apdu, mapCryptoErrorToKMError(e.getReason()));
} catch (Exception e) {
freeOperations();
sendResponse(apdu, KMError.GENERIC_UNKNOWN_ERROR);
} finally {
repository.clean();
}
}
// After every device boot, the Keymaster becomes ready to execute all the commands only after
// 1. boot parameters are set,
// 2. system properties are set and
// 3. computed the shared secret successfully.
private boolean isKeymasterReady(byte apduIns) {
byte deviceBootStatus =
(SET_BOOT_PARAMS_SUCCESS | SET_SYSTEM_PROPERTIES_SUCCESS |
NEGOTIATED_SHARED_SECRET_SUCCESS);
if (repository.getDeviceBootStatus() == deviceBootStatus) {
// Keymaster is ready to execute all the commands.
return true;
}
switch (apduIns) {
case INS_PROVISION_ATTEST_IDS_CMD:
case INS_PROVISION_ATTESTATION_KEY_CMD:
case INS_PROVISION_ATTESTATION_CERT_DATA_CMD:
case INS_PROVISION_OEM_ROOT_PUBLIC_KEY_CMD:
case INS_PROVISION_PRESHARED_SECRET_CMD:
case INS_SE_FACTORY_LOCK_PROVISIONING_CMD:
case INS_OEM_LOCK_PROVISIONING_CMD:
// Provision commands are not allowed in ACTIVE_STATE
ISOException.throwIt(ISO7816.SW_COMMAND_NOT_ALLOWED);
break;
// Below commands are allowed even if the Keymaster is not ready.
case INS_GET_HW_INFO_CMD:
case INS_ADD_RNG_ENTROPY_CMD:
case INS_EARLY_BOOT_ENDED_CMD:
case INS_GET_HMAC_SHARING_PARAM_CMD:
case INS_COMPUTE_SHARED_HMAC_CMD:
case INS_SET_VERSION_PATCHLEVEL_CMD:
case INS_OEM_UNLOCK_PROVISIONING_CMD:
return true;
default:
break;
}
return false;
}
private void setDeviceBootStatus(byte deviceRebootStatus) {
byte status = repository.getDeviceBootStatus();
status |= deviceRebootStatus;
repository.setDeviceBootStatus(status);
}
private void generateUniqueOperationHandle(byte[] buf, short offset, short len) {
do {
seProvider.newRandomNumber(buf, offset, len);
} while (null != repository.findOperation(buf, offset, len));
}
private boolean isSEFactoryProvisioningLocked() {
return (0 != (provisionStatus & PROVISION_STATUS_SE_FACTORY_PROVISIONING_LOCKED));
}
private boolean isSEFactoryProvisioningComplete() {
if ((0 != (provisionStatus & PROVISION_STATUS_ATTESTATION_KEY))
&& (0 != (provisionStatus & PROVISION_STATUS_ATTESTATION_CERT_CHAIN))
&& (0 != (provisionStatus & PROVISION_STATUS_ATTESTATION_CERT_PARAMS))) {
return true;
} else {
return false;
}
}
private boolean isProvisioningComplete() {
if ((0 != (provisionStatus & PROVISION_STATUS_ATTESTATION_KEY))
&& (0 != (provisionStatus & PROVISION_STATUS_ATTESTATION_CERT_CHAIN))
&& (0 != (provisionStatus & PROVISION_STATUS_ATTESTATION_CERT_PARAMS))
&& (0 != (provisionStatus & PROVISION_STATUS_PRESHARED_SECRET))) {
return true;
} else {
return false;
}
}
private void processOEMUnlockProvisionCmd(APDU apdu) {
authenticateOEM(OEM_UNLOCK_VERIFICATION_LABEL, apdu);
// Set the OEM Lock bit LOW in provisionStatus.
provisionStatus &= ~KMKeymasterApplet.PROVISION_STATUS_OEM_PROVISIONING_LOCKED;
keymasterState = KMAppletState.IN_PROVISION_STATE;
sendResponse(apdu, KMError.OK);
}
private void processOEMLockProvisionCmd(APDU apdu) {
authenticateOEM(OEM_LOCK_VERIFICATION_LABEL, apdu);
// Set the OEM Lock bit HIGH in provisionStatus.
provisionStatus |= KMKeymasterApplet.PROVISION_STATUS_OEM_PROVISIONING_LOCKED;
keymasterState = KMAppletState.ACTIVE_STATE;
sendResponse(apdu, KMError.OK);
}
private void authenticateOEM(byte[] plainMsg, APDU apdu) {
receiveIncoming(apdu);
byte[] scratchpad = apdu.getBuffer();
tmpVariables[0] = KMArray.instance((short) 1);
KMArray.cast(tmpVariables[0]).add((short) 0, KMByteBlob.exp());
// Decode the arguments
tmpVariables[0] = decoder.decode(tmpVariables[0], (byte[]) bufferRef[0],
bufferProp[BUF_START_OFFSET], bufferProp[BUF_LEN_OFFSET]);
//reclaim memory
repository.reclaimMemory(bufferProp[BUF_LEN_OFFSET]);
// Get the signature input.
short signature = KMArray.cast(tmpVariables[0]).get((short) 0);
short ecPubKeyLen = seProvider.readOEMRootPublicKey(scratchpad, (short) 0);
if (!seProvider.ecVerify256(
scratchpad, (short) 0, (short) ecPubKeyLen,
plainMsg, (short) 0, (short) plainMsg.length,
KMByteBlob.cast(signature).getBuffer(),
KMByteBlob.cast(signature).getStartOff(),
KMByteBlob.cast(signature).length())) {
KMException.throwIt(KMError.VERIFICATION_FAILED);
}
}
private void freeOperations() {
if (data[OP_HANDLE] != KMType.INVALID_VALUE) {
KMOperationState op = repository.findOperation(data[OP_HANDLE]);
if (op != null) {
repository.releaseOperation(op);
}
}
}
private void processEarlyBootEndedCmd(APDU apdu) {
repository.setEarlyBootEndedStatus(true);
sendResponse(apdu, KMError.OK);
}
private void processDeviceLockedCmd(APDU apdu) {
receiveIncoming(apdu);
byte[] scratchPad = apdu.getBuffer();
tmpVariables[0] = KMArray.instance((short) 2);
KMArray.cast(tmpVariables[0]).add((short) 0, KMInteger.exp());
tmpVariables[1] = KMVerificationToken.exp();
KMArray.cast(tmpVariables[0]).add((short) 1, tmpVariables[1]);
// Decode the arguments
tmpVariables[0] = decoder.decode(tmpVariables[0], (byte[]) bufferRef[0], bufferProp[BUF_START_OFFSET], bufferProp[BUF_LEN_OFFSET]);
//reclaim memory
repository.reclaimMemory(bufferProp[BUF_LEN_OFFSET]);
tmpVariables[1] = KMArray.cast(tmpVariables[0]).get((short) 0);
tmpVariables[1] = KMInteger.cast(tmpVariables[1]).getByte();
data[VERIFICATION_TOKEN] = KMArray.cast(tmpVariables[0]).get((short) 1);
validateVerificationToken(data[VERIFICATION_TOKEN], scratchPad);
short verTime = KMVerificationToken.cast(data[VERIFICATION_TOKEN]).getTimestamp();
short lastDeviceLockedTime;
try {
lastDeviceLockedTime = repository.getDeviceTimeStamp();
} catch (KMException e) {
lastDeviceLockedTime = KMInteger.uint_8((byte) 0);
}
if (KMInteger.compare(verTime, lastDeviceLockedTime) > 0) {
Util.arrayFillNonAtomic(scratchPad, (short) 0, KMInteger.UINT_64, (byte) 0);
KMInteger.cast(verTime).getValue(scratchPad, (short) 0, KMInteger.UINT_64);
repository.setDeviceLock(true);
repository.setDeviceLockPasswordOnly(tmpVariables[1] == 0x01);
repository.setDeviceLockTimestamp(scratchPad, (short) 0, KMInteger.UINT_64);
}
sendResponse(apdu, KMError.OK);
}
private void resetTransientBuffers() {
short index = 0;
while (index < data.length) {
data[index] = KMType.INVALID_VALUE;
index++;
}
index = 0;
while (index < tmpVariables.length) {
tmpVariables[index] = KMType.INVALID_VALUE;
index++;
}
}
/**
* Sends a response, may be extended response, as requested by the command.
*/
public static void sendOutgoing(APDU apdu) {
if (((short) (bufferProp[BUF_LEN_OFFSET] + bufferProp[BUF_START_OFFSET])) > ((short) repository
.getHeap().length)) {
ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
}
// Send data
apdu.setOutgoing();
apdu.setOutgoingLength(bufferProp[BUF_LEN_OFFSET]);
apdu.sendBytesLong((byte[]) bufferRef[0], bufferProp[BUF_START_OFFSET], bufferProp[BUF_LEN_OFFSET]);
}
/**
* Receives data, which can be extended data, as requested by the command instance.
*/
public static void receiveIncoming(APDU apdu) {
byte[] srcBuffer = apdu.getBuffer();
short recvLen = apdu.setIncomingAndReceive();
short srcOffset = apdu.getOffsetCdata();
bufferProp[BUF_LEN_OFFSET] = apdu.getIncomingLength();
bufferProp[BUF_START_OFFSET] = repository.allocReclaimableMemory(bufferProp[BUF_LEN_OFFSET]);
short index = bufferProp[BUF_START_OFFSET];
while (recvLen > 0 && ((short) (index - bufferProp[BUF_START_OFFSET]) < bufferProp[BUF_LEN_OFFSET])) {
Util.arrayCopyNonAtomic(srcBuffer, srcOffset, (byte[]) bufferRef[0], index, recvLen);
index += recvLen;
recvLen = apdu.receiveBytes(srcOffset);
}
}
private void processGetHwInfoCmd(APDU apdu) {
// No arguments expected
// Make the response
short respPtr = KMArray.instance((short) 3);
KMArray resp = KMArray.cast(respPtr);
resp.add((short) 0, KMEnum.instance(KMType.HARDWARE_TYPE, KMType.STRONGBOX));
resp.add(
(short) 1,
KMByteBlob.instance(
JAVACARD_KEYMASTER_DEVICE, (short) 0, (short) JAVACARD_KEYMASTER_DEVICE.length));
resp.add((short) 2, KMByteBlob.instance(GOOGLE, (short) 0, (short) GOOGLE.length));
bufferProp[BUF_START_OFFSET] = repository.allocAvailableMemory();
// Encode the response - actual bufferProp[BUF_LEN_OFFSET] is 86
bufferProp[BUF_LEN_OFFSET] = encoder.encode(respPtr, (byte[]) bufferRef[0], bufferProp[BUF_START_OFFSET]);
// send buffer to host
sendOutgoing(apdu);
}
private void processAddRngEntropyCmd(APDU apdu) {
// Receive the incoming request fully from the host.
receiveIncoming(apdu);
// Argument 1
short argsProto = KMArray.instance((short) 1);
KMArray.cast(argsProto).add((short) 0, KMByteBlob.exp());
// Decode the argument
short args = decoder.decode(argsProto, (byte[]) bufferRef[0], bufferProp[BUF_START_OFFSET], bufferProp[BUF_LEN_OFFSET]);
//reclaim memory
repository.reclaimMemory(bufferProp[BUF_LEN_OFFSET]);
// Process
KMByteBlob blob = KMByteBlob.cast(KMArray.cast(args).get((short) 0));
// Maximum 2KiB of seed is allowed.
if (blob.length() > MAX_SEED_SIZE) {
KMException.throwIt(KMError.INVALID_ARGUMENT);
}
seProvider.addRngEntropy(blob.getBuffer(), blob.getStartOff(), blob.length());
sendResponse(apdu, KMError.OK);
}
private void processSetVersionAndPatchLevels(APDU apdu) {
receiveIncoming(apdu);
// Argument 1 OS Version
tmpVariables[0] = KMInteger.exp();
// Argument 2 OS Patch level
tmpVariables[1] = KMInteger.exp();
// Argument 3 Vendor Patch level
tmpVariables[2] = KMInteger.exp();
// Array of expected arguments
short argsProto = KMArray.instance((short) 3);
KMArray.cast(argsProto).add((short) 0, tmpVariables[0]);
KMArray.cast(argsProto).add((short) 1, tmpVariables[1]);
KMArray.cast(argsProto).add((short) 2, tmpVariables[2]);
// Decode the arguments
short args = decoder.decode(argsProto, (byte[]) bufferRef[0], bufferProp[BUF_START_OFFSET], bufferProp[BUF_LEN_OFFSET]);
//reclaim memory
repository.reclaimMemory(bufferProp[BUF_LEN_OFFSET]);
tmpVariables[0] = KMArray.cast(args).get((short) 0);
tmpVariables[1] = KMArray.cast(args).get((short) 1);
tmpVariables[2] = KMArray.cast(args).get((short) 2);
repository.setOsVersion(
KMInteger.cast(tmpVariables[0]).getBuffer(),
KMInteger.cast(tmpVariables[0]).getStartOff(),
KMInteger.cast(tmpVariables[0]).length());
repository.setOsPatch(
KMInteger.cast(tmpVariables[1]).getBuffer(),
KMInteger.cast(tmpVariables[1]).getStartOff(),
KMInteger.cast(tmpVariables[1]).length());
repository.setVendorPatchLevel(
KMInteger.cast(tmpVariables[2]).getBuffer(),
KMInteger.cast(tmpVariables[2]).getStartOff(),
KMInteger.cast(tmpVariables[2]).length());
setDeviceBootStatus(SET_SYSTEM_PROPERTIES_SUCCESS);
sendResponse(apdu, KMError.OK);
}
private short getProvisionedCertificateData(byte dataType) {
short len = seProvider.getProvisionedDataLength(dataType);
if (len == 0) {
KMException.throwIt(KMError.INVALID_DATA);
}
short ptr = KMByteBlob.instance(len);
seProvider.readProvisionedData(
dataType,
KMByteBlob.cast(ptr).getBuffer(),
KMByteBlob.cast(ptr).getStartOff());
return ptr;
}
private void processGetCertChainCmd(APDU apdu) {
// Make the response
short certChainLen = seProvider.getProvisionedDataLength(KMSEProvider.CERTIFICATE_CHAIN);
short int32Ptr = buildErrorStatus(KMError.OK);
short maxByteHeaderLen = 3; // Maximum possible ByteBlob header len.
short arrayHeaderLen = 1;
// Allocate maximum possible buffer.
// Add arrayHeader + (PowerResetStatus + KMError.OK) + Byte Header
short totalLen = (short) (arrayHeaderLen + encoder.getEncodedIntegerLength(int32Ptr) + maxByteHeaderLen + certChainLen);
tmpVariables[1] = KMByteBlob.instance(totalLen);
bufferRef[0] = KMByteBlob.cast(tmpVariables[1]).getBuffer();
bufferProp[BUF_START_OFFSET] = KMByteBlob.cast(tmpVariables[1]).getStartOff();
bufferProp[BUF_LEN_OFFSET] = KMByteBlob.cast(tmpVariables[1]).length();
// copy the certificate chain to the end of the buffer.
seProvider.readProvisionedData(
KMSEProvider.CERTIFICATE_CHAIN,
(byte[]) bufferRef[0],
(short) (bufferProp[BUF_START_OFFSET] + totalLen - certChainLen));
// Encode cert chain.
encoder.encodeCertChain((byte[]) bufferRef[0],
bufferProp[BUF_START_OFFSET],
bufferProp[BUF_LEN_OFFSET],
int32Ptr, // uint32 ptr
(short) (bufferProp[BUF_START_OFFSET] + totalLen - certChainLen), // start pos of cert chain.
certChainLen);
sendOutgoing(apdu);
}
private void processProvisionAttestationCertDataCmd(APDU apdu) {
receiveIncoming(apdu);
// Buffer holds the corresponding offsets and lengths of the certChain, certIssuer and certExpiry
// in the bufferRef[0] buffer.
short var = KMByteBlob.instance((short) 12);
// These variables point to the appropriate positions in the var buffer.
short certChainPos = KMByteBlob.cast(var).getStartOff();
short certIssuerPos = (short) (KMByteBlob.cast(var).getStartOff() + 4);
short certExpiryPos = (short) (KMByteBlob.cast(var).getStartOff() + 8);
decoder.decodeCertificateData((short) 3,
(byte[]) bufferRef[0], bufferProp[BUF_START_OFFSET], bufferProp[BUF_LEN_OFFSET],
KMByteBlob.cast(var).getBuffer(), KMByteBlob.cast(var).getStartOff());
// persist data
seProvider.persistProvisionData(
(byte[]) bufferRef[0],
Util.getShort(KMByteBlob.cast(var).getBuffer(), certChainPos), // offset
Util.getShort(KMByteBlob.cast(var).getBuffer(), (short) (certChainPos + 2)), // length
Util.getShort(KMByteBlob.cast(var).getBuffer(), certIssuerPos), // offset
Util.getShort(KMByteBlob.cast(var).getBuffer(), (short) (certIssuerPos + 2)), // length
Util.getShort(KMByteBlob.cast(var).getBuffer(), certExpiryPos), // offset
Util.getShort(KMByteBlob.cast(var).getBuffer(), (short) (certExpiryPos + 2))); // length
// reclaim memory
repository.reclaimMemory(bufferProp[BUF_LEN_OFFSET]);
}
private void processProvisionAttestationKey(APDU apdu) {
receiveIncoming(apdu);
// Re-purpose the apdu buffer as scratch pad.
byte[] scratchPad = apdu.getBuffer();
// Arguments
short keyparams = KMKeyParameters.exp();
short keyFormatPtr = KMEnum.instance(KMType.KEY_FORMAT);
short blob = KMByteBlob.exp();
short argsProto = KMArray.instance((short) 3);
KMArray.cast(argsProto).add((short) 0, keyparams);
KMArray.cast(argsProto).add((short) 1, keyFormatPtr);
KMArray.cast(argsProto).add((short) 2, blob);
// Decode the argument
short args = decoder.decode(argsProto, (byte[]) bufferRef[0], bufferProp[BUF_START_OFFSET], bufferProp[BUF_LEN_OFFSET]);
//reclaim memory
repository.reclaimMemory(bufferProp[BUF_LEN_OFFSET]);
// key params should have os patch, os version and verified root of trust
data[KEY_PARAMETERS] = KMArray.cast(args).get((short) 0);
tmpVariables[0] = KMArray.cast(args).get((short) 1);