|
| 1 | +package org.openjavacard.tool.command.base; |
| 2 | + |
| 3 | +import com.beust.jcommander.Parameter; |
| 4 | +import com.beust.jcommander.validators.PositiveInteger; |
| 5 | +import org.openjavacard.gp.keys.GPKey; |
| 6 | +import org.openjavacard.gp.keys.GPKeyCipher; |
| 7 | +import org.openjavacard.gp.keys.GPKeyDiversification; |
| 8 | +import org.openjavacard.gp.keys.GPKeyId; |
| 9 | +import org.openjavacard.gp.keys.GPKeySet; |
| 10 | +import org.openjavacard.gp.keys.GPKeyUsage; |
| 11 | +import org.openjavacard.gp.keys.GPKeyVersion; |
| 12 | +import org.openjavacard.gp.scp.SCPSecurityPolicy; |
| 13 | +import org.openjavacard.util.HexUtil; |
| 14 | + |
| 15 | +public class BasicSCPCommand extends BasicCardCommand { |
| 16 | + |
| 17 | + @Parameter( |
| 18 | + names = "--scp-diversification", order = 300, |
| 19 | + description = "Use specified key diversification" |
| 20 | + ) |
| 21 | + protected GPKeyDiversification scpDiversification = GPKeyDiversification.NONE; |
| 22 | + |
| 23 | + @Parameter( |
| 24 | + names = "--scp-protocol", order = 300, |
| 25 | + description = "Require specified SCP protocol" |
| 26 | + ) |
| 27 | + protected String scpProtocol = "00"; |
| 28 | + |
| 29 | + @Parameter( |
| 30 | + names = "--scp-parameters", order = 300, |
| 31 | + description = "Require specified SCP parameters" |
| 32 | + ) |
| 33 | + protected String scpParameters = "00"; |
| 34 | + |
| 35 | + @Parameter( |
| 36 | + names = "--scp-security", order = 300, |
| 37 | + description = "Require specified SCP security level" |
| 38 | + ) |
| 39 | + protected SCPSecurityPolicy scpSecurity = SCPSecurityPolicy.CMAC; |
| 40 | + |
| 41 | + @Parameter( |
| 42 | + names = "--key-version", |
| 43 | + description = "User-specified keys: key version (0 means any)", |
| 44 | + validateWith = PositiveInteger.class |
| 45 | + ) |
| 46 | + private int scpKeyVersion = 0; |
| 47 | + |
| 48 | + @Parameter( |
| 49 | + names = "--key-id", |
| 50 | + description = "User-specified keys: first key ID (0 means any)", |
| 51 | + validateWith = PositiveInteger.class |
| 52 | + ) |
| 53 | + private int scpKeyId = 0; |
| 54 | + |
| 55 | + @Parameter( |
| 56 | + names = "--key-cipher", |
| 57 | + description = "User-specified keys: key cipher" |
| 58 | + ) |
| 59 | + private GPKeyCipher scpKeyCipher = GPKeyCipher.GENERIC; |
| 60 | + |
| 61 | + @Parameter( |
| 62 | + names = "--key-types", |
| 63 | + description = "User-specified keys: key types (colon-separated)" |
| 64 | + ) |
| 65 | + private String scpKeyTypes = "MASTER"; |
| 66 | + |
| 67 | + @Parameter( |
| 68 | + names = "--key-secrets", |
| 69 | + description = "User-specified keys: secrets (colon-separated)" |
| 70 | + ) |
| 71 | + private String scpKeySecrets = null; |
| 72 | + |
| 73 | + |
| 74 | + public GPKeySet getKeySet() { |
| 75 | + GPKeySet keys = GPKeySet.GLOBALPLATFORM; |
| 76 | + |
| 77 | + if(scpKeySecrets != null) { |
| 78 | + keys = buildKeysFromParameters(scpKeyId, scpKeyVersion, scpKeyCipher, scpKeyTypes, scpKeySecrets); |
| 79 | + } |
| 80 | + |
| 81 | + return keys; |
| 82 | + } |
| 83 | + |
| 84 | + public static GPKeySet buildKeysFromParameters(int keyId, int keyVersion, GPKeyCipher cipher, String types, String secrets) { |
| 85 | + // XXX this is not comprehensive because of the loop and protocol variations |
| 86 | + GPKeyId.checkKeyId(keyId); |
| 87 | + GPKeyVersion.checkKeyVersion(keyVersion); |
| 88 | + // build a key set |
| 89 | + GPKeySet keys = new GPKeySet("commandline", keyVersion); |
| 90 | + // split arguments |
| 91 | + String[] typeStrings = types.split(":"); |
| 92 | + String[] secretStrings = secrets.split(":"); |
| 93 | + // check lengths of provided arrays |
| 94 | + if(typeStrings.length != secretStrings.length) { |
| 95 | + throw new Error("Must provide an equal number of key types and secrets"); |
| 96 | + } |
| 97 | + // assume number of keys from number of types |
| 98 | + int numKeys = typeStrings.length; |
| 99 | + for(int i = 0; i < numKeys; i++) { |
| 100 | + GPKeyUsage usage = GPKeyUsage.valueOf(typeStrings[i]); |
| 101 | + byte[] secret = HexUtil.hexToBytes(secretStrings[i]); |
| 102 | + byte id = (byte)(keyId + i); |
| 103 | + if(usage == GPKeyUsage.MASTER) { |
| 104 | + id = 0; |
| 105 | + } |
| 106 | + GPKey key = new GPKey(id, usage, cipher, secret); |
| 107 | + keys.putKey(key); |
| 108 | + } |
| 109 | + return keys; |
| 110 | + } |
| 111 | + |
| 112 | +} |
0 commit comments