Skip to content

Commit ea1e433

Browse files
move fields to protected
1 parent edf0d99 commit ea1e433

File tree

2 files changed

+55
-67
lines changed

2 files changed

+55
-67
lines changed

src/main/java/toys/BlindOracleApplet.java

Lines changed: 31 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -14,73 +14,72 @@ public class BlindOracleApplet extends SecureApplet{
1414
// commands transmitted over secure channel
1515
// 0x00 - 0x04 are reserved
1616
// key management
17-
private static final byte CMD_ROOT = (byte)0x10;
17+
protected static final byte CMD_ROOT = (byte)0x10;
1818
// bip32 keys - derivation and signing
19-
private static final byte CMD_BIP32 = (byte)0x11;
19+
protected static final byte CMD_BIP32 = (byte)0x11;
2020

2121
/************ key management *********/
2222

2323
// set seed - 64 bytes,
2424
// data format: <64 bytes seed>
25-
private static final byte SUBCMD_ROOT_SET_SEED = (byte)0x00;
25+
protected static final byte SUBCMD_ROOT_SET_SEED = (byte)0x00;
2626
// set xprv - 65 bytes
2727
// data format: <32-byte chain code><00><32-byte prv>
28-
private static final byte SUBCMD_ROOT_SET_KEY = (byte)0x01;
28+
protected static final byte SUBCMD_ROOT_SET_KEY = (byte)0x01;
2929
// generate random key
3030
// WARNING: doesn't return the seed, so it always stays only on this card
3131
// add some backup mechanism in a script to recover if card breaks
3232
// data: ignored
33-
private static final byte SUBCMD_ROOT_SET_RANDOM = (byte)0x7D;
33+
protected static final byte SUBCMD_ROOT_SET_RANDOM = (byte)0x7D;
3434

3535
/************ master private key management *********/
3636

3737
// returns 65-byte root xpub <chain_code><pubkey>
3838
// data: ignored
39-
private static final byte SUBCMD_BIP32_GET_ROOT = (byte)0x00;
39+
protected static final byte SUBCMD_BIP32_GET_ROOT = (byte)0x00;
4040
// pass array of 4-byte indexes for derivation path
4141
// max derivation len is ~50, should be enough in most cases
4242
// sets result to temporary storage, so you can use it for
4343
// faster signing afterwards
4444
// data: <keyid><4-byte index><4-byte index>...<4-byte index>
4545
// keyid is 00 if derive from root, 01 if derive from current child
4646
// saves derived key as current (id 01)
47-
private static final byte SUBCMD_BIP32_GET_DERIVE = (byte)0x01;
47+
protected static final byte SUBCMD_BIP32_GET_DERIVE = (byte)0x01;
4848
// returns an xpub of the key currently stored in memory
49-
private static final byte SUBCMD_BIP32_GET_CURRENT = (byte)0x02;
49+
protected static final byte SUBCMD_BIP32_GET_CURRENT = (byte)0x02;
5050
// sign using currently derived child key or root key
5151
// data format: <32-byte message hash>00 to use root key
5252
// <32-byte message hash>01 to use current key
53-
private static final byte SUBCMD_BIP32_SIGN = (byte)0x03;
53+
protected static final byte SUBCMD_BIP32_SIGN = (byte)0x03;
5454
// pass 32-byte hash to sign, then key id
5555
// and array of 4-byte indexes for derivation
5656
// key that is signing is not saved as current
5757
// data: <32-byte message hash>00<4-byte index>...<4-byte index> for root
5858
// <32-byte message hash>01<4-byte index>...<4-byte index> for current
59-
private static final byte SUBCMD_BIP32_DERIVE_AND_SIGN = (byte)0x04;
59+
protected static final byte SUBCMD_BIP32_DERIVE_AND_SIGN = (byte)0x04;
6060
// it's not full bip32 key, only chain code and the key
61-
private static final short BIP32_LEN = (short)65;
62-
private static final short CHAINCODE_OFFSET = (short)0;
63-
private static final short PUBKEY_OFFSET = (short)32;
64-
private static final short FLAG_OFFSET = (short)32;
65-
private static final short PRVKEY_OFFSET = (short)33;
66-
private static final short CHAINCODE_LEN = (short)32;
67-
private static final short PUBKEY_LEN = (short)33;
68-
private static final short PRVKEY_LEN = (short)32;
69-
private static final short SEED_LEN_MIN = (short)16;
70-
private static final short SEED_LEN_MAX = (short)64;
71-
private static final short MSG_LEN = (short)32;
61+
protected static final short BIP32_LEN = (short)65;
62+
protected static final short CHAINCODE_OFFSET = (short)0;
63+
protected static final short PUBKEY_OFFSET = (short)32;
64+
protected static final short FLAG_OFFSET = (short)32;
65+
protected static final short PRVKEY_OFFSET = (short)33;
66+
protected static final short CHAINCODE_LEN = (short)32;
67+
protected static final short PUBKEY_LEN = (short)33;
68+
protected static final short PRVKEY_LEN = (short)32;
69+
protected static final short SEED_LEN_MIN = (short)16;
70+
protected static final short SEED_LEN_MAX = (short)64;
71+
protected static final short MSG_LEN = (short)32;
7272
public static final byte[] HDKEY_SEED_KEY = {'B','i','t','c','o','i','n',' ','s','e','e','d'};
7373

74-
private static final short ERR_INVALID_DATA = (short)0x0700;
74+
protected static final short ERR_INVALID_DATA = (short)0x0700;
7575

76-
private boolean isInitialized = false;
77-
private byte status = (byte)0;
76+
protected boolean isInitialized = false;
7877
// root key
79-
private byte[] rootPrv;
80-
private byte[] rootXpub; // 65 bytes, <chain code><pubkey>
78+
protected byte[] rootPrv;
79+
protected byte[] rootXpub; // 65 bytes, <chain code><pubkey>
8180
// child key
82-
private byte[] childPrv;
83-
private byte[] childXpub; // 65 bytes, <chain code><pubkey>
81+
protected byte[] childPrv;
82+
protected byte[] childXpub; // 65 bytes, <chain code><pubkey>
8483

8584
// Create an instance of the Applet subclass using its constructor,
8685
// and to register the instance.
@@ -101,18 +100,10 @@ public static void install(byte[] bArray, short bOffset, byte bLength){
101100
*/
102101
public BlindOracleApplet(){
103102
super();
104-
try {
105-
rootPrv = new byte[PRVKEY_LEN];
106-
rootXpub = new byte[BIP32_LEN];
107-
} catch (Exception e) {
108-
status = (byte)1;
109-
}
110-
try{
111-
childPrv = JCSystem.makeTransientByteArray(PRVKEY_LEN, JCSystem.CLEAR_ON_DESELECT);
112-
childXpub = JCSystem.makeTransientByteArray(BIP32_LEN, JCSystem.CLEAR_ON_DESELECT);
113-
} catch (Exception e) {
114-
status = (byte)2;
115-
}
103+
rootPrv = new byte[PRVKEY_LEN];
104+
rootXpub = new byte[BIP32_LEN];
105+
childPrv = JCSystem.makeTransientByteArray(PRVKEY_LEN, JCSystem.CLEAR_ON_DESELECT);
106+
childXpub = JCSystem.makeTransientByteArray(BIP32_LEN, JCSystem.CLEAR_ON_DESELECT);
116107
}
117108
/**
118109
* Handles secure message (decrypted by SecureChannel)
@@ -125,9 +116,6 @@ protected short processSecureMessage(byte[] buf, short len){
125116
if(isLocked()){
126117
ISOException.throwIt(ERR_CARD_LOCKED);
127118
}
128-
if(status > (byte)0){
129-
ISOException.throwIt(ERR_INVALID_CMD);
130-
}
131119
switch(buf[OFFSET_CMD]){
132120
case CMD_ROOT:
133121
return processRootCommand(buf, len);

src/main/java/toys/SecureApplet.java

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -47,42 +47,42 @@
4747
public class SecureApplet extends Applet{
4848

4949
/** Class code for secure applet */
50-
private static final byte SECURE_CLA = (byte)0xB0;
50+
protected static final byte SECURE_CLA = (byte)0xB0;
5151

5252
/** Instruction to get 32 random bytes, without secure channel */
53-
private static final byte INS_GET_RANDOM = (byte)0xB1;
53+
protected static final byte INS_GET_RANDOM = (byte)0xB1;
5454

5555
/* Secure channel stuff */
5656
/** Instruction to get static card's public key for ECDH key agreement */
57-
private static final byte INS_GET_CARD_PUBKEY = (byte)0xB2;
57+
protected static final byte INS_GET_CARD_PUBKEY = (byte)0xB2;
5858
/** Instruction to establish secure channel in ES mode -
5959
* ephemeral key from the host, static key from the card. */
60-
private static final byte INS_OPEN_SECURE_CHANNEL_SS_MODE = (byte)0xB3;
60+
protected static final byte INS_OPEN_SECURE_CHANNEL_SS_MODE = (byte)0xB3;
6161
/** Instruction to establish secure channel in ES mode -
6262
* ephemeral keys are used both on the host and on the card. */
63-
private static final byte INS_OPEN_SECURE_CHANNEL_ES_MODE = (byte)0xB4;
63+
protected static final byte INS_OPEN_SECURE_CHANNEL_ES_MODE = (byte)0xB4;
6464
/** Instruction to establish secure channel in EE mode -
6565
* ephemeral keys are used both on the host and on the card. */
66-
private static final byte INS_OPEN_SECURE_CHANNEL_EE_MODE = (byte)0xB5;
67-
private static final byte INS_SECURE_MESSAGE = (byte)0xB6;
68-
private static final byte INS_CLOSE_CHANNEL = (byte)0xB7;
66+
protected static final byte INS_OPEN_SECURE_CHANNEL_EE_MODE = (byte)0xB5;
67+
protected static final byte INS_SECURE_MESSAGE = (byte)0xB6;
68+
protected static final byte INS_CLOSE_CHANNEL = (byte)0xB7;
6969

7070
/* Commands transmitted over secure channel */
71-
private static final byte CMD_ECHO = (byte)0x00;
72-
private static final byte CMD_RAND = (byte)0x01;
73-
private static final byte CMD_AUTH = (byte)0x02;
74-
private static final byte CMD_PIN = (byte)0x03;
75-
private static final byte CMD_REESTABLISH_SC = (byte)0x04;
76-
private static final byte CMD_WIPE = (byte)0x05;
71+
protected static final byte CMD_ECHO = (byte)0x00;
72+
protected static final byte CMD_RAND = (byte)0x01;
73+
protected static final byte CMD_AUTH = (byte)0x02;
74+
protected static final byte CMD_PIN = (byte)0x03;
75+
protected static final byte CMD_REESTABLISH_SC = (byte)0x04;
76+
protected static final byte CMD_WIPE = (byte)0x05;
7777

7878
protected static final byte SUBCMD_DEFAULT = (byte)0x00;
7979
// pin
80-
private static final byte SUBCMD_PIN_STATUS = (byte)0x00;
81-
private static final byte SUBCMD_PIN_UNLOCK = (byte)0x01;
82-
private static final byte SUBCMD_PIN_LOCK = (byte)0x02;
83-
private static final byte SUBCMD_PIN_CHANGE = (byte)0x03;
84-
private static final byte SUBCMD_PIN_SET = (byte)0x04;
85-
private static final byte SUBCMD_PIN_UNSET = (byte)0x05;
80+
protected static final byte SUBCMD_PIN_STATUS = (byte)0x00;
81+
protected static final byte SUBCMD_PIN_UNLOCK = (byte)0x01;
82+
protected static final byte SUBCMD_PIN_LOCK = (byte)0x02;
83+
protected static final byte SUBCMD_PIN_CHANGE = (byte)0x03;
84+
protected static final byte SUBCMD_PIN_SET = (byte)0x04;
85+
protected static final byte SUBCMD_PIN_UNSET = (byte)0x05;
8686

8787
// status
8888
protected static final byte STATUS_PIN_NOT_SET = (byte)0x00;
@@ -128,12 +128,12 @@ public class SecureApplet extends Applet{
128128
public static final short LENGTH_TRANSIENT_HEAP = (short)1024;
129129

130130
/* PIN constants */
131-
private static final byte PIN_MAX_LENGTH = (byte)32;
132-
private static final byte PIN_MAX_COUNTER = (byte)10;
131+
protected static final byte PIN_MAX_LENGTH = (byte)32;
132+
protected static final byte PIN_MAX_COUNTER = (byte)10;
133133

134-
private PinCode pin;
134+
protected PinCode pin;
135135
// mb better to do via GP somehow?
136-
private boolean pinIsSet = false;
136+
protected boolean pinIsSet = false;
137137

138138
protected TransientHeap heap;
139139
protected SecureChannel sc;

0 commit comments

Comments
 (0)