public class CryptKey extends CapiHandle
HCRYPTKEY type, and is used for all
situations where an HCRYPTKEY is required for a native call.
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Close the handle to the native key object, releasing memory.
|
static java.security.PrivateKey |
decodePrivateKey(byte[] keyBlob)
Decodes a software-based private key from MSCAPI key blob representation.
|
byte[] |
decrypt(boolean isFinal,
byte[] data,
int offset,
int length)
Decrypt data previously encrypted using
encrypt. |
static byte[] |
encodePrivateKey(java.security.PrivateKey privateKey,
KeySpec keySpec)
Encodes a software-based private key to MSCAPI key blob representation.
|
byte[] |
encrypt(boolean isFinal,
byte[] data,
int offset,
int length)
Encrypts data.
|
java.security.PublicKey |
exportPublicKey()
Exports the public key from MSCAPI to software-based representation.
|
static java.security.KeyPair |
generateKeyPair(CryptProvider provider,
EntrustAsymKeyType keyType,
KeySpec keySpec,
int flags)
Generates a key pair in MSCAPI.
|
KeyAlgorithms |
getAlgorithm()
Returns the algorithm associated with this key, or
null if
the algorithm is not recognized. |
CryptProvider |
getCryptProvider()
Returns the
CryptProvider used when creating this key. |
static CryptKey |
getInstance(CryptProvider provider,
KeySpec keySpec)
Creates and returns a
CryptKey object associated with a
public/private key pair that already exists in CAPI. |
byte[] |
getKeyParam(KeyParam keyParamId)
Gets the value of a parameter associated with this key in MSCAPI.
|
byte[] |
getKeyParameter(KeyParam parameter)
Deprecated.
use
getKeyParam(KeyParam) instead; since 8.0 |
KeySpec |
getKeySpec()
Gets the key specifier associated with this key.
|
static CryptKey |
importKey(CryptProvider provider,
byte[] key,
CryptKey hPubKey,
int flags)
Imports a key in to CAPI and returns a handle to the newly imported key.
|
static CapiPrivateKey |
importPrivateKey(CryptProvider provider,
java.security.PrivateKey privateKey,
KeySpec keySpec,
int flags,
boolean forceSecureKeyImport)
Imports the provided software-based private key into MSCAPI, using the
provided key specifier to determine the MSCAPI key algorithm for the key.
|
static CapiPrivateKey |
importPrivateKey(CryptProvider provider,
java.security.PrivateKey privateKey,
KeyUsage keyUsage,
int flags,
boolean forceSecureKeyImport)
Imports the provided software-based private key into MSCAPI, using the
provided key usage to determine the MSCAPI key algorithm for the key.
|
void |
setKeyParam(KeyParam keyParamId,
byte[] keyParamValue)
Sets the value of a parameter associated with this key in MSCAPI.
|
debugInfo, debugInfo, equals, setExtendedDebuggingclearHandle, finalize, getHandle, hashCodepublic static CryptKey getInstance(CryptProvider provider, KeySpec keySpec) throws CapiException
CryptKey object associated with a
public/private key pair that already exists in CAPI.
provider - [FIPS 140-2 control input] the CSP from which to get the key.keySpec - [FIPS 140-2 control input] the key specifier, either
AT_KEYEXCHANGE or AT_SIGNATURE.CryptKey
object.CapiException - [FIPS 140-2 status output] if the key could not be created.Fips140ErrorStateException - [FIPS 140-2 status output] thrown if the Toolkit is not
allowed to perform cryptographic operationsCryptGetUserKeypublic static CryptKey importKey(CryptProvider provider, byte[] key, CryptKey hPubKey, int flags) throws CapiException
CapiKeyFormatter can be
used to convert from the ASN.1 encoding of a private key to a format that
will be accepted by CAPI.
provider - [FIPS 140-2 control input] the provider that will own the
imported key. This provider must be able to support the key
algorithm.key - [FIPS 140-2 data input] [FIPS 140-2 CSP] the key to import.hPubKey - [FIPS 140-2 data input] a handle to a key to be used to
decrypt the key being imported, or null if the
key being imported is not protected.flags - [FIPS 140-2 control input] the MSCAPI flags; all 32-bits are
usedCapiException - [FIPS 140-2 status output] if the CAPI function
CryptImportKey fails.Fips140ErrorStateException - [FIPS 140-2 status output] thrown if the Toolkit is not
allowed to perform cryptographic operationsCapiKeyFormatter,
MSDN library
documentation for CryptImportKeypublic static CapiPrivateKey importPrivateKey(CryptProvider provider, java.security.PrivateKey privateKey, KeyUsage keyUsage, int flags, boolean forceSecureKeyImport) throws CapiException
Refer to importPrivateKey(CryptProvider, PrivateKey, KeySpec, int, boolean)
for additional details on the import operation.
provider - [FIPS 140-2 control input] the CSP to which the private key
will be importedprivateKey - [FIPS 140-2 data input] [FIPS 140-2 CSP] a software-based
private keykeyUsage - [FIPS 140-2 control input] the key usage associated with the
private key; used to determine the algorithm that is
associated with the private key in MSCAPIflags - [FIPS 140-2 control input] the MSCAPI flags; all 32-bits are
usedforceSecureKeyImport - [FIPS 140-2 control input] Used to determine whether secure key
import is usedCapiException - [FIPS 140-2 status output] if the private key import
operation failsFips140ErrorStateException - [FIPS 140-2 status output] thrown if the Toolkit is not
allowed to perform cryptographic operationspublic static CapiPrivateKey importPrivateKey(CryptProvider provider, java.security.PrivateKey privateKey, KeySpec keySpec, int flags, boolean forceSecureKeyImport) throws CapiException
An attempt is first made to import the private key securely, that is, protected under a symmetric key. If the secure key import attempt fails and the Toolkit is not operating in FIPS mode, then an attempt is made to import the private key into MSCAPI in the clear (unprotected); otherwise an error is returned indicating import in the clear is not permitted when operating in FIPS mode.
The following are the set of steps that occur when performing a secure key import operation.
Secure key import is currently only supported for RSA keys. For secure key import of RSA private keys, the public/private secure key import key pair used is an RSA key pair of the same strength. The symmetric key that is used depends on the capabilities of the CSP to which the key is being imported. The following algorithms are supported in Java and listed in order of preference. The first algorithm in this list that is also supported by the CSP is used in the secure key import operation.
EntrustSymmEncConfig.AES_CBC_256EntrustSymmEncConfig.AES_CBC_192EntrustSymmEncConfig.AES_CBC_128EntrustSymmEncConfig.TRIPLEDES_CBC_192provider - [FIPS 140-2 control input] the CSP to which the private key
will be importedprivateKey - [FIPS 140-2 data input] [FIPS 140-2 CSP] a software-based
private keykeySpec - [FIPS 140-2 control input] the key specifier associated with
the private key; used to determine the algorithm that is
associated with the private key in MSCAPIflags - [FIPS 140-2 control input] the MSCAPI flags; all 32-bits are
usedforceSecureKeyImport - [FIPS 140-2 control input] Used to determine whether secure key
import is usedCapiException - [FIPS 140-2 status output] if the private key import
operation failsFips140ErrorStateException - [FIPS 140-2 status output] thrown if the Toolkit is not
allowed to perform cryptographic operationspublic static java.security.KeyPair generateKeyPair(CryptProvider provider, EntrustAsymKeyType keyType, KeySpec keySpec, int flags) throws CapiException
provider - [FIPS 140-2 control input] the CSP in which the key pair will
be generatedkeyType - [FIPS 140-2 data input] the key type; identifies the type of
key pair (e.g. RSA-1024) being generatedkeySpec - [FIPS 140-2 control input] the key specifier; used to
determine the algorithm that is associated with the key pair
in MSCAPIflags - [FIPS 140-2 control input] the MSCAPI flags; only the lower
16-bits are usedCapiException - [FIPS 140-2 status output] if the key pair generation
operation failsFips140ErrorStateException - [FIPS 140-2 status output] thrown if the Toolkit is not
allowed to perform cryptographic operationspublic static byte[] encodePrivateKey(java.security.PrivateKey privateKey,
KeySpec keySpec)
throws CapiException
privateKey - [FIPS 140-2 data input] [FIPS 140-2 CSP] a software-based
private keykeySpec - [FIPS 140-2 control input] the key specifier; used to
determine the key algorithm that is used in the key blob
representationCapiException - if the encode operation failsFips140ErrorStateException - [FIPS 140-2 status output] thrown if the Toolkit is not
allowed to perform cryptographic operationspublic static java.security.PrivateKey decodePrivateKey(byte[] keyBlob)
throws CapiException
keyBlob - [FIPS 140-2 data input] [FIPS 140-2 CSP] a private key in
MSCAPI key blob representationCapiException - if the decode operation failsFips140ErrorStateException - [FIPS 140-2 status output] thrown if the Toolkit is not
allowed to perform cryptographic operationspublic void close()
public byte[] encrypt(boolean isFinal,
byte[] data,
int offset,
int length)
throws CapiException
isFinal - [FIPS 140-2 control input] true if this is the
last data to encrypt, false if there is more data
to be encrypted.data - [FIPS 140-2 data input] the data to encrypt. This data is not
overwritten.offset - [FIPS 140-2 data input] the starting offset in the
data array.length - [FIPS 140-2 data input] the number of bytes in
data to decrypt.CapiException - [FIPS 140-2 status output] if an error occurs encrypting the
dataFips140ErrorStateException - [FIPS 140-2 status output] thrown if the Toolkit is not
allowed to perform cryptographic operationsCryptEncryptpublic byte[] decrypt(boolean isFinal,
byte[] data,
int offset,
int length)
throws CapiException
encrypt.
isFinal - [FIPS 140-2 control input] true if this is the
last data to decrypt, false if there is more data
to be decrypted.data - [FIPS 140-2 data input] the data to decrypt. This data is not
overwritten.offset - [FIPS 140-2 data input] the starting offset in the
data array.length - [FIPS 140-2 data input] the number of bytes in
data to decrypt.CapiException - [FIPS 140-2 status output] if an error occurs decrypting the
dataFips140ErrorStateException - [FIPS 140-2 status output] thrown if the Toolkit is not
allowed to perform cryptographic operationsCryptDecryptpublic CryptProvider getCryptProvider()
CryptProvider used when creating this key.
CryptProvider used when
creating this key.public KeyAlgorithms getAlgorithm() throws CapiException
null if
the algorithm is not recognized.
CapiException - [FIPS 140-2 status output] if the call to
CryptGetKeyParam to obtain the key algorithm id
fails.Fips140ErrorStateException - [FIPS 140-2 status output] thrown if the Toolkit is not
allowed to perform cryptographic operationspublic KeySpec getKeySpec() throws CapiException
The key specifier is determined based on the algorithm that is associated with the key in MSCAPI.
CapiException - [FIPS 140-2 status output] if the key is associated with an
algorithm in MSCAPI that is not supported by the ToolkitFips140ErrorStateException - [FIPS 140-2 status output] thrown if the Toolkit is not
allowed to perform cryptographic operationspublic byte[] getKeyParameter(KeyParam parameter) throws CapiException
getKeyParam(KeyParam) instead; since 8.0parameter - [FIPS 140-2 control input] the key parameter to retrieve.CryptGetKeyParamCapiException - [FIPS 140-2 status output] if the call to
CryptGetKeyParam fails.Fips140ErrorStateException - [FIPS 140-2 status output] thrown if the Toolkit is not
allowed to perform cryptographic operationsCryptGetKeyParampublic byte[] getKeyParam(KeyParam keyParamId) throws CapiException
IMPORTANT The key parameter value returned by this API is passed back directly from MSCAPI without modification. Depending on the parameter type, the endianness of the value may need to be accounted for by the caller. For example, any parameter that is defined as a DWORD is represented as a little endian value in MSCAPI. When converting this type of key parameter value to a Java type (an integer for example), the endianness must be accounted for.
keyParamId - [FIPS 140-2 control input] a key parameter identifier; identifies
the key parameter being retrievedCapiException - [FIPS 140-2 status output] if the key parameter get operation
failsFips140ErrorStateException - [FIPS 140-2 status output] thrown if the Toolkit is not
allowed to perform cryptographic operationspublic void setKeyParam(KeyParam keyParamId, byte[] keyParamValue) throws CapiException
IMPORTANT The key parameter value provided to this API is passed directly to MSCAPI without modification. Depending on the parameter type, the endianness of the value may need to be accounted for by the caller. For example, any parameter that is defined as a DWORD is represented as a little endian value in MSCAPI. When converting a Java type (an integer for example) to this type of key parameter value, the endianness must be accounted for.
keyParamId - [FIPS 140-2 control input] a key parameter identifier; identifies
the key parameter being setkeyParamValue - [FIPS 140-2 data input] the value of the key parameter being
setCapiException - [FIPS 140-2 status output]if the key parameter set operation
failsFips140ErrorStateException - [FIPS 140-2 status output] thrown if the Toolkit is not
allowed to perform cryptographic operationspublic java.security.PublicKey exportPublicKey()
throws CapiException
CapiException - [FIPS 140-2 status output] if the public key export operation
failsFips140ErrorStateException - [FIPS 140-2 status output] thrown if the Toolkit is not
allowed to perform cryptographic operations