public interface HSMDevice extends Device
HSMDevice
interface represents an interface for interacting with a Hardware Security Module (HSM).
This interface provides essential security services related to the HSM. An instance
of HSMDevice is obtained as follows:
HSMDevice hsmDevice = (HSMDevice) POSTerminal.getInstance().getDevice("com.cloudpos.device.hsm");The HSM device supports concurrent use by multiple applications. However, some operations may require exclusive access.
To use this device, applications must request permission:
<uses-permission android:name="android.permission.CLOUDPOS_SAFE_MODULE" />
Device
Modifier and Type | Field and Description |
---|---|
static int |
CERT_FORMAT_DER
DER format
|
static int |
CERT_FORMAT_PEM
PEM format
|
static int |
CERT_TYPE_APP_ROOT
Terminal application root certificate
|
static int |
CERT_TYPE_COMM_ROOT
Terminal SSL communication root certificate
|
static int |
CERT_TYPE_KEYLOADER_ROOT
Terminal Keyloader root certificate
|
static int |
CERT_TYPE_PUBLIC_KEY
Terminal public key certificate
|
static int |
CERT_TYPE_TERMINAL_OWNER
Terminal owner root certificate
|
Modifier and Type | Method and Description |
---|---|
byte[] |
decrypt(int algorithm,
java.lang.String aliasPrivateKey,
byte[] bufCipher)
Decrypts data using the specified private key.
|
boolean |
deleteCertificate(int certType,
java.lang.String alias)
Deletes a specified certificate from the HSM.
|
boolean |
deleteKeyPair(java.lang.String aliasPrivateKey)
Deletes a key pair from the HSM based on the provided alias.
|
byte[] |
encrypt(int algorithm,
java.lang.String aliasPrivateKey,
byte[] bufPlain)
Encrypts data using the specified private key.
|
byte[] |
generateCSR(java.lang.String aliasPrivateKey,
javax.security.auth.x500.X500Principal subject)
Generates a Certificate Signing Request (CSR) for a given private key.
|
void |
generateKeyPair(java.lang.String aliasPrivateKey,
int algorithm,
int keySize)
Requests the HSM to generate a new key pair.
|
byte[] |
generateRandom(int length)
Generates a random buffer from the HSM.
|
byte[] |
getCertificate(int certType,
java.lang.String alias,
int dataFormat)
Retrieves the specified certificate from the HSM.
|
java.lang.String |
getEncryptedUniqueCode(java.lang.String uniqueCode,
java.lang.String randomFactor)
Retrieves the encrypted unique code, a feature primarily used in China.
|
long |
getFreeSpace()
Determines the available free space within the HSM.
|
boolean |
injectPublicKeyCertificate(java.lang.String alias,
java.lang.String aliasPrivateKey,
byte[] bufCert,
int dataFormat)
Injects a public key certificate associated with an existing key pair into the HSM.
|
boolean |
injectRootCertificate(int certType,
java.lang.String alias,
byte[] bufCert,
int dataFormat)
Injects root certificates into the HSM.
|
boolean |
isKeyExist(int keyID)
Checks if a specified key exists in the HSM.
|
boolean |
isKeyExist(int keyID,
int algorithm)
Deprecated.
Use
isKeyExist(int) instead. |
boolean |
isTampered()
Checks if the HSM is tampered with.
|
byte[] |
keyDecrypt_v1(int keyID,
int algorithm,
int mode,
byte[] byteData,
byte[] IV)
Decrypts data using a specified key stored in the HSM (version 1).
|
int |
keyDecrypt(int keyID,
int algorithm,
int mode,
byte[] byteData,
byte[] IV)
Deprecated.
Use
keyDecrypt_v1(int, int, int, byte[], byte[]) instead. |
byte[] |
keyEncrypt_v1(int keyID,
int algorithm,
int mode,
byte[] byteData,
byte[] IV)
Encrypts data using a specified key stored in the HSM (version 1).
|
int |
keyEncrypt(int keyID,
int algorithm,
int mode,
byte[] byteData,
byte[] IV)
Deprecated.
Use
keyEncrypt_v1(int, int, int, byte[], byte[]) instead. |
void |
open(int logicalID)
Opens the hardware security module for use.
|
java.lang.String[] |
queryCertificates(int certType)
Queries the HSM for certificates of a specific type.
|
boolean |
resetSensorStatus()
Resets the sensor status, useful for small batteries.
|
int |
updateKey(int keyID,
int algorithm,
byte[] byteData)
Updates a key in the HSM.
|
int |
updateSM4Key(int keyID,
byte[] keyBuffer,
byte[] signature)
Updates an SM4 key.
|
cancelRequest, close, getFailCount, getUsageCount, open
static final int CERT_FORMAT_PEM
static final int CERT_FORMAT_DER
static final int CERT_TYPE_TERMINAL_OWNER
static final int CERT_TYPE_PUBLIC_KEY
static final int CERT_TYPE_APP_ROOT
static final int CERT_TYPE_COMM_ROOT
static final int CERT_TYPE_KEYLOADER_ROOT
void open(int logicalID) throws DeviceException
logicalID
- The logical ID of the HSM device.DeviceException
- for standard reasons as documented in DeviceException
。boolean isTampered() throws DeviceException
true
if tampered, false
otherwise.DeviceException
- for standard reasons as documented in DeviceException
.byte[] generateRandom(int length) throws DeviceException
length
- The length of the buffer (less than 64).DeviceException
- for standard reasons as documented in DeviceException
.void generateKeyPair(java.lang.String aliasPrivateKey, int algorithm, int keySize) throws DeviceException
aliasPrivateKey
- Alias for the private key.algorithm
- The algorithm for the key pair (currently supports "RSA").keySize
- Bit size of the key (currently supports 2048 bits).DeviceException
- for standard reasons as documented in DeviceException
.boolean injectPublicKeyCertificate(java.lang.String alias, java.lang.String aliasPrivateKey, byte[] bufCert, int dataFormat) throws DeviceException
alias
- Alias for the certificate.aliasPrivateKey
- Alias for the key pair, usually the private key's alias.bufCert
- Certificate data.dataFormat
- Format of the certificate data (currently only PEM format is supported).true
if successful, false
otherwise.DeviceException
- for standard reasons as documented in DeviceException
.boolean injectRootCertificate(int certType, java.lang.String alias, byte[] bufCert, int dataFormat) throws DeviceException
CERT_TYPE_TERMINAL_OWNER
certificate's keyUsage flag must be set as critical, and the KeyEncipherment, CertificateSign and CRLSign must be set, other flags are cleared.
CERT_TYPE_APP_ROOT
certificate's keyUsage flag must be set as critical, and the DigitalSignature, CertificateSign must be set, other flags are cleared.
CERT_TYPE_COMM_ROOT
certificate's keyUsage flag must be set as non-critical and DigitalSignature, KeyEncipherment, DataEncipherment must be set, other flags are cleared.
CERT_TYPE_KEYLOADER_ROOT
certificate's keyUsage flag must be set as critical and NonRepudiation, CertificateSign must be set, other flags are cleared.
certType
- Type of the certificate. CERT_TYPE_TERMINAL_OWNER
, CERT_TYPE_APP_ROOT
,CERT_TYPE_COMM_ROOT
or CERT_TYPE_KEYLOADER_ROOT
.alias
- Alias for the certificate.bufCert
- Certificate data.dataFormat
- Format of the certificate data (currently only PEM format (CERT_FORMAT_PEM
)is supported).true
if successful, false
otherwise.DeviceException
- for standard reasons as documented in DeviceException
.byte[] getCertificate(int certType, java.lang.String alias, int dataFormat) throws DeviceException
certType
- Type of the certificate (e.g., CERT_TYPE_PUBLIC_KEY
, CERT_TYPE_TERMINAL_OWNER
, CERT_TYPE_APP_ROOT
, CERT_TYPE_COMM_ROOT
or CERT_TYPE_KEYLOADER_ROOT
).alias
- Alias of the certificate.dataFormat
- Format of the certificate data (currently only CERT_FORMAT_PEM
is supported).DeviceException
- for standard reasons as documented in DeviceException
.boolean deleteCertificate(int certType, java.lang.String alias) throws DeviceException
certType
- Type of the certificate (e.g., CERT_TYPE_PUBLIC_KEY
, CERT_TYPE_APP_ROOT
, CERT_TYPE_COMM_ROOT
or CERT_TYPE_KEYLOADER_ROOT
).alias
- Alias of the certificate.true
if deletion is successful, false
otherwise.DeviceException
- for standard reasons as documented in DeviceException
.java.lang.String[] queryCertificates(int certType) throws DeviceException
certType
- Type of the certificates to query.DeviceException
- for standard reasons as documented in DeviceException
.boolean deleteKeyPair(java.lang.String aliasPrivateKey) throws DeviceException
aliasPrivateKey
- Alias of the private key.true
if successful, false
otherwise.DeviceException
- for standard reasons as documented in DeviceException
.byte[] generateCSR(java.lang.String aliasPrivateKey, javax.security.auth.x500.X500Principal subject) throws DeviceException
aliasPrivateKey
- The alias of the private key.subject
- X500Principal containing the subject information.DeviceException
- for standard reasons as documented in DeviceException
.byte[] encrypt(int algorithm, java.lang.String aliasPrivateKey, byte[] bufPlain) throws DeviceException
algorithm
- Encryption algorithm.aliasPrivateKey
- Alias of the private key.bufPlain
- Data buffer to encrypt.DeviceException
- for standard reasons as documented in DeviceException
.byte[] decrypt(int algorithm, java.lang.String aliasPrivateKey, byte[] bufCipher) throws DeviceException
algorithm
- Decryption algorithm.aliasPrivateKey
- Alias of the private key.bufCipher
- Data buffer to decrypt.DeviceException
- for standard reasons as documented in DeviceException
.long getFreeSpace() throws DeviceException
DeviceException
- for standard reasons as documented in DeviceException
.java.lang.String getEncryptedUniqueCode(java.lang.String uniqueCode, java.lang.String randomFactor) throws DeviceException
uniqueCode
- The unique code to be encrypted.randomFactor
- A random factor for the encryption process.DeviceException
- for standard reasons as documented in DeviceException
.boolean resetSensorStatus() throws DeviceException
true
if reset is successful, false
otherwise.DeviceException
- for standard reasons as documented in DeviceException
.int updateKey(int keyID, int algorithm, byte[] byteData) throws DeviceException
keyID
- Key index (range 0-49, with 0-2 reserved, third-party apps should use 3-49).algorithm
- Key type (SM4, DES, 3DES, or AES).byteData
- Key data.DeviceException
- for standard reasons as documented in DeviceException
.int keyEncrypt(int keyID, int algorithm, int mode, byte[] byteData, byte[] IV) throws DeviceException
keyEncrypt_v1(int, int, int, byte[], byte[])
instead.keyEncrypt_v1(int, int, int, byte[], byte[])
.keyID
- Key index (range 0-49, with 0-2 reserved).algorithm
- Encryption algorithm (e.g., SM4, DES, 3DES, AES).mode
- Encryption mode (e.g., 0: ECB 1: CBC 2:CFB 3:OFB).byteData
- Data to encrypt.IV
- Initialization vector.DeviceException
- for standard reasons as documented in DeviceException
.byte[] keyEncrypt_v1(int keyID, int algorithm, int mode, byte[] byteData, byte[] IV) throws DeviceException
keyID
- Key index (range 0-49, with 0-2 reserved).algorithm
- Encryption algorithm (e.g., SM4, DES, 3DES, AES).mode
- Encryption mode (e.g., 0: ECB 1: CBC 2:CFB 3:OFB).byteData
- Data to encrypt.IV
- Initialization vector.DeviceException
- for standard reasons as documented in DeviceException
.int keyDecrypt(int keyID, int algorithm, int mode, byte[] byteData, byte[] IV) throws DeviceException
keyDecrypt_v1(int, int, int, byte[], byte[])
instead.keyDecrypt_v1(int, int, int, byte[], byte[])
.keyID
- Key index (range 0-49, with 0-2 reserved).algorithm
- Decryption algorithm (e.g., SM4, DES, 3DES, AES).mode
- Decryption mode (e.g., 0: ECB 1: CBC 2:CFB 3:OFB).byteData
- Data to decrypt.IV
- Initialization vector.DeviceException
- for standard reasons as documented in DeviceException
.byte[] keyDecrypt_v1(int keyID, int algorithm, int mode, byte[] byteData, byte[] IV) throws DeviceException
keyID
- Key index (range 0-49, with 0-2 reserved).algorithm
- Decryption algorithm (e.g., SM4, DES, 3DES, AES).mode
- Decryption mode (e.g., 0: ECB 1: CBC 2:CFB 3:OFB).byteData
- Data to decrypt.IV
- Initialization vector.DeviceException
- for standard reasons as documented in DeviceException
.boolean isKeyExist(int keyID, int algorithm) throws DeviceException
isKeyExist(int)
instead.isKeyExist(int)
.keyID
- Key index (range 0-49, with 0-2 reserved).algorithm
- Key type (e.g., SM4, DES, 3DES, AES).true
if the key exists, false
otherwise.DeviceException
- for standard reasons as documented in DeviceException
.boolean isKeyExist(int keyID) throws DeviceException
keyID
- Key index (range 0-49, with 0-2 reserved).true
if the key exists, false
otherwise.DeviceException
- for standard reasons as documented in DeviceException
.int updateSM4Key(int keyID, byte[] keyBuffer, byte[] signature) throws DeviceException
keyID
- Key index (range 0-2).keyBuffer
- Key data.signature
- Signature data.DeviceException
- for standard reasons as documented in DeviceException
.