public class CryptProvider extends CapiHandle
HCRYPTPROV
type, and is used for all situations where an HCRYPTPROV is
required for a native call. It also stores the name of the CAPI container
associated with this CSP, if any, and the CSP name and type.
| Modifier and Type | Method and Description |
|---|---|
boolean |
canUseHardwareRng()
Return whether or not this provider can use a hardware random number
generator.
|
void |
close()
Closes the handle to the CSP, releasing memory allocated by native code.
|
void |
forceUseHardwareRng()
Force random number generation to be done using a hardware RNG.
|
void |
genRandom(byte[] randomBytes)
Fills the given array with random bytes.
|
void |
genRandom(byte[] randomBytes,
int offset,
int length)
Fills the given array with
length random bytes, starting at
offset. |
byte[] |
genRandom(int numBytes)
Generates
numBytes random bytes and returns an array
containing those bytes. |
java.lang.String |
getContainerName()
Returns the name of the container associated with this object.
|
java.util.List<java.lang.String> |
getContainers()
Returns a
List of Strings that are the names of
the containers belonging to this provider. |
byte[] |
getCSPParam(ProviderParam providerParam)
Gets the value of a CSP parametet associated with this CryptProvider in
MSCAPI
|
static ProviderInfo |
getDefaultProviderInfo(ProviderType providerType,
int flags)
Returns information about the default provider for the given provider
type.
|
static ProviderInfo |
getFirstAvailableProvider(java.lang.String[] providerNames)
Given a String list of provider names, return the first provider that
is available.
|
CryptImplType |
getImplementationType()
Returns the implementation type of this provider.
|
static CryptProvider |
getInstance(java.lang.String container,
java.lang.String provider,
ProviderType providerType,
int flags)
Creates an returns an initialized CryptProvider.
|
ProviderInfo |
getProviderInfo()
Returns information about the CSP associated with this object.
|
static ProviderInfo |
getProviderInfo(java.lang.String providerName)
Returns information about the MSCAPI cryptographic service provider (CSP)
with the given name.
|
static java.util.List<ProviderInfo> |
getProviders()
Returns a
List of ProviderInfo objects, one for
each provider installed on the system. |
static java.util.List<ProviderInfo> |
getProviderTypes()
Returns a
List of ProviderInfo objects, one for
each provider type installed on the system. |
static byte[] |
getRandomDataFromDefaultProvider(int size)
Generates the requested number of random bytes using the default CAPI
cryptographic service provider.
|
java.util.List<ProviderAlgorithmInfo> |
getSupportedAlgorithms()
Returns a
List of ProviderAlgorithmInfo objects
that denote the algorithms supported by this provider. |
void |
setCSPParam(ProviderParam providerParam,
byte[] cspValue)
Sets the value of a CSP parameter associated with this CryptProvider in
MSCAPI.
|
boolean |
supportsAlgorithm(Algorithms algorithm)
Returns
true if this provider supports the given algorithm,
false if not. |
clearHandle, debugInfo, debugInfo, equals, finalize, getHandle, hashCode, setExtendedDebuggingpublic static java.util.List<ProviderInfo> getProviderTypes() throws CapiException
List of ProviderInfo objects, one for
each provider type installed on the system.
List of
ProviderInfo objects, one for each provider type
installed on the system.CapiException - [FIPS 140-2 status output] if there is a problem obtaining
the provider type information from CAPI.Fips140ErrorStateException - [FIPS 140-2 status output] thrown if the Toolkit is not
allowed to perform cryptographic operationspublic static java.util.List<ProviderInfo> getProviders() throws CapiException
List of ProviderInfo objects, one for
each provider installed on the system.
List of
ProviderInfo objects, one for each provider
installed on the system.CapiException - [FIPS 140-2 status output] if there is a problem obtaining
the provider information from CAPI.Fips140ErrorStateException - [FIPS 140-2 status output] thrown if the Toolkit is not
allowed to perform cryptographic operationspublic static ProviderInfo getProviderInfo(java.lang.String providerName) throws CapiException
providerName - [FIPS 140-2 data input] name of an MSCAPI CSPCapiException - [FIPS 140-2 status output] if the operation fails (e.g. there
are no CSPs on the system with the indicated name)Fips140ErrorStateException - [FIPS 140-2 status output] thrown if the Toolkit is not
allowed to perform cryptographic operationspublic static ProviderInfo getFirstAvailableProvider(java.lang.String[] providerNames) throws CapiException
String provList[] = new String[]{"Microsoft Enhanced RSA and AES Cryptographic Provider",
"Microsoft Enhanced RSA and AES Cryptographic Provider (Prototype)",
"Microsoft Enhanced Cryptographic Provider v1.0"};
providerNames - An array of Provider NamesCapiExceptionpublic static ProviderInfo getDefaultProviderInfo(ProviderType providerType, int flags) throws CapiException
providerType - [FIPS 140-2 control input] The provider type to get
information about.flags - [FIPS 140-2 control input]
ProviderInfo object with
information about the requested provider type.CapiException - [FIPS 140-2 status output] if an error occurs retrieving
information about the provider. Some provider types may not
have a default provider registered.Fips140ErrorStateException - [FIPS 140-2 status output] thrown if the Toolkit is not
allowed to perform cryptographic operationspublic static CryptProvider getInstance(java.lang.String container, java.lang.String provider, ProviderType providerType, int flags) throws CapiException
container - [FIPS 140-2 data input] name of the container, or
null for the default container for the named
provider. Note that not all CSPs support default containers.provider - [FIPS 140-2 data input] name of the CSP, or null
for the default CSP for the given provider type.providerType - [FIPS 140-2 control input] the type of the provider.flags - [FIPS 140-2 control input] flags that control how the instance
is obtained. A combination of the flags CRYPT_VERIFYCONTEXT,
CRYPT_NEWKEYSET, CRYPT_DELETEKEYSET, CRYPT_MACHINE_KEYSET, and
CRYPT_SILENT from CapiConstants, or
0.CryptProvider object that
is initialized with a handle to the named provider. When the
CRYPT_DELETEKEYSET flag is used, the returned value is undefined
and should be ignored.CapiException - [FIPS 140-2 status output] if a handle to the CSP cannot be
acquired for any reason.Fips140ErrorStateException - [FIPS 140-2 status output] thrown if the Toolkit is not
allowed to perform cryptographic operationsCryptAcquireContextpublic static byte[] getRandomDataFromDefaultProvider(int size)
throws CapiException
This call will load the default CAPI native library if the CAPI native library has not yet been loaded.
size - [FIPS 140-2 control input] the number of random bytes to be
generatedCapiException - [FIPS 140-2 status output] thrown if an error occurs while
generating the random dataFips140ErrorStateException - [FIPS 140-2 status output] thrown if the Toolkit is not
allowed to perform cryptographic operationspublic void close()
close in class CapiHandlepublic ProviderInfo getProviderInfo()
Fips140ErrorStateException - [FIPS 140-2 status output] thrown if the Toolkit is not
allowed to perform cryptographic operationspublic java.lang.String getContainerName()
null if this provider is
not associated with a container, which may be the case if the
CRYPT_VERIFY_CONTEXT flag was used.Fips140ErrorStateException - [FIPS 140-2 status output] thrown if the Toolkit is not
allowed to perform cryptographic operationspublic java.util.List<ProviderAlgorithmInfo> getSupportedAlgorithms() throws CapiException
List of ProviderAlgorithmInfo objects
that denote the algorithms supported by this provider.
List of
ProviderAlgorithmInfo objects that denote the
algorithms supported by this provider.CapiException - if there is a problem obtaining the list of algorithms from
CAPI.public boolean supportsAlgorithm(Algorithms algorithm)
true if this provider supports the given algorithm,
false if not.
algorithm - [FIPS 140-2 control input] the algorithm to check for support.true if this provider
supports the given algorithm, false if not.Fips140ErrorStateException - [FIPS 140-2 status output] thrown if the Toolkit is not
allowed to perform cryptographic operationspublic java.util.List<java.lang.String> getContainers()
throws CapiException
List of Strings that are the names of
the containers belonging to this provider.
List of
Strings that are the names of the containers
belonging to this provider.CapiException - [FIPS 140-2 status output] if there is a problem obtaining
the list of containers from CAPI.Fips140ErrorStateException - [FIPS 140-2 status output] thrown if the Toolkit is not
allowed to perform cryptographic operationspublic void genRandom(byte[] randomBytes)
throws CapiException
randomBytes - [FIPS 140-2 data output] the byte array to fill will random
bytes.CapiException - [FIPS 140-2 status output] if an error occurs generating the
random data.Fips140ErrorStateException - [FIPS 140-2 status output] thrown if the Toolkit is not
allowed to perform cryptographic operationspublic void genRandom(byte[] randomBytes,
int offset,
int length)
throws CapiException
length random bytes, starting at
offset. The CAPI cryptographic service provider is used to
generate the random bytes.
randomBytes - [FIPS 140-2 data output] the byte array to fill will random
bytes.offset - [FIPS 140-2 data input] the offset in the arraylength - [FIPS 140-2 data input] the number of random bytes to
generate.CapiException - [FIPS 140-2 status output] if an error occurs generating the
random data.Fips140ErrorStateException - [FIPS 140-2 status output] thrown if the Toolkit is not
allowed to perform cryptographic operationsCryptGenRandompublic byte[] genRandom(int numBytes)
throws CapiException
numBytes random bytes and returns an array
containing those bytes. The CAPI cryptographic service provider is used
to generate the random bytes.
numBytes - [FIPS 140-2 control input] the number of random bytes to
generate.numBytes random bytes.CapiException - [FIPS 140-2 status output] if an error occurs generating the
random data.Fips140ErrorStateException - [FIPS 140-2 status output] thrown if the Toolkit is not
allowed to perform cryptographic operationsCryptGenRandompublic boolean canUseHardwareRng()
Fips140ErrorStateException - [FIPS 140-2 status output] thrown if the Toolkit is not
allowed to perform cryptographic operationspublic void forceUseHardwareRng()
throws CapiException
CapiException - if this provider does not support a hardware RNG, or a
problem occurs setting hardware RNG usage.Fips140ErrorStateException - [FIPS 140-2 status output] thrown if the Toolkit is not
allowed to perform cryptographic operationspublic CryptImplType getImplementationType()
Fips140ErrorStateException - [FIPS 140-2 status output] thrown if the Toolkit is not
allowed to perform cryptographic operationsCryptGetProvParampublic void setCSPParam(ProviderParam providerParam, byte[] cspValue) throws CapiException
IMPORTANT The csp 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.
providerParam - [FIPS 140-2 control input] A provider parameter; identifies
the CSP parameter being setcspValue - [FIPS 140-2 data input] the value of the csp parameter being
setCapiException - [FIPS 140-2 status output] if the csp parameter set operation
failsFips140ErrorStateException - [FIPS 140-2 status output] thrown if the Toolkit is not
allowed to perform cryptographic operationspublic byte[] getCSPParam(ProviderParam providerParam) 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.
providerParam - [FIPS 140-2 control input] A provider parameter; identifies
the CSP 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 operations