com.pheox.jcapi
Class JCAPISignature

java.lang.Object
  extended by java.security.SignatureSpi
      extended by com.pheox.jcapi.JCAPISignature
Direct Known Subclasses:
JCAPISignature.MD2withRSA, JCAPISignature.MD4withRSA, JCAPISignature.MD5withRSA, JCAPISignature.NONEwithRSA, JCAPISignature.SHA1withDSA, JCAPISignature.SHA1withRSA, JCAPISignature.SHA256withRSA, JCAPISignature.SHA384withRSA, JCAPISignature.SHA512withRSA, JCAPISignature.SHAMD5withRSA

public class JCAPISignature
extends java.security.SignatureSpi

This class provides the functionality for creating and verifying RSA/DSA signatures by using RSA/DSA public keys and RSA/DSA private keys that are stored in Microsoft Windows system stores.
Note: the implementation does not require the keys to be stored in Microsoft Windows system stores in order to work. It can handle ordinary Java native RSA and DSA key objects as well. If such a native key is used, then it will be temporary imported into the Microsoft Windows system store for the wanted operation and then removed when the operation has finished.

All cryptographic operations are delegated from JCAPI to (using JNI) Microsoft CryptoAPI (MS CAPI) and executed inside the MS CAPI layer

The following signature algorithms are supported:

Here is an example of how to create a JCAPI signature instance through the JCE framework:
  Signature s = Signature.getInstance("SHA1withRSA", "JCAPI");

To learn more about how this class can be used, please look at the following example programs that can be found in JCAPI's examples directory, by default located in C:\<application data directory>\JCAPI\examples:

Since:
1.0

Nested Class Summary
static class JCAPISignature.MD2withRSA
          The MD2 hashing class used for defining MD2withRSA in signatures.
static class JCAPISignature.MD4withRSA
          The MD4 hashing class used for defining MD4withRSA in signatures.
static class JCAPISignature.MD5withRSA
          The MD5 hashing class used for defining MD5withRSA in signatures.
static class JCAPISignature.NONEwithRSA
          This class is used for defining NONEwithRSA in RSA signatures.
static class JCAPISignature.SHA1withDSA
          The SHA-1 hashing class used for defining SHA1withDSA in DSA signatures.
static class JCAPISignature.SHA1withRSA
          The SHA-1 hashing class used for defining SHA1withRSA in signatures.
static class JCAPISignature.SHA256withRSA
          The SHA-256 hashing class used for defining SHA256withRSA in signatures.
static class JCAPISignature.SHA384withRSA
          The SHA-384 hashing class used for defining SHA384withRSA in signatures.
static class JCAPISignature.SHA512withRSA
          The SHA-512 hashing class used for defining SHA512withRSA in signatures.
static class JCAPISignature.SHAMD5withRSA
          The SHA-1 & MD5 hashing class used for defining SHAMD5withRSA in signatures.
 
Field Summary
 
Fields inherited from class java.security.SignatureSpi
appRandom
 
Method Summary
 java.lang.Object engineGetParameter(java.lang.String param)
          Will always return null since parameters are not supported by the JCAPI signature class.
 void engineInitSign(java.security.PrivateKey privateKey)
          Initializes this signature object with the specified private key for signing operations.
 void engineInitVerify(java.security.PublicKey publicKey)
          Initializes this signature object with the specified public key for verification operations.
 void engineSetParameter(java.lang.String param, java.lang.Object value)
          Not supported.
 byte[] engineSign()
          Returns the RSA or DSA signature bytes of all the data updated so far.
 int engineSign(byte[] outbuf, int offset, int len)
          Finishes this signature operation and stores the resulting RSA or DSA signature bytes in the provided buffer outbuf, starting at offset.
 void engineUpdate(byte b)
          Updates the data to be signed or verified using the specified byte.
 void engineUpdate(byte[] b, int off, int len)
          Updates the data to be signed or verified, using the specified array of bytes, starting at the specified offset.
 boolean engineVerify(byte[] sigBytes)
          Verifies the passed-in RSA/DSA signature.
 boolean engineVerify(byte[] sigBytes, int offset, int length)
          Verifies the passed-in RSA/DSA signature in the specified array of bytes, starting at the specified offset.
 
Methods inherited from class java.security.SignatureSpi
clone, engineGetParameters, engineInitSign, engineSetParameter, engineUpdate
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

engineGetParameter

public java.lang.Object engineGetParameter(java.lang.String param)
Will always return null since parameters are not supported by the JCAPI signature class.

Specified by:
engineGetParameter in class java.security.SignatureSpi
Parameters:
param - is ignored.
Returns:
Always null.
Since:
1.0

engineInitSign

public void engineInitSign(java.security.PrivateKey privateKey)
                    throws java.security.InvalidKeyException
Initializes this signature object with the specified private key for signing operations. Note: The JCAPI provider only accepts RSA private keys of type java.security.interfaces.RSAPrivateCrtKey or JCAPIRSAPrivateKey, and DSA private keys of type java.security.spec.DSAPrivateKeySpec or JCAPIDSAPrivateKey.

Specified by:
engineInitSign in class java.security.SignatureSpi
Parameters:
privateKey - the RSA/DSA private key of the identity whose signature will be generated.
Throws:
java.security.InvalidKeyException - if the key is not an instance of java.security.interfaces.RSAPrivateCrtKey, JCAPIRSAPrivateKey, java.security.spec.DSAPrivateKeySpec, or JCAPIDSAPrivateKey.
Since:
1.0

engineInitVerify

public void engineInitVerify(java.security.PublicKey publicKey)
                      throws java.security.InvalidKeyException
Initializes this signature object with the specified public key for verification operations.

Note: The JCAPI provider only accept RSA and DSA public keys.

Specified by:
engineInitVerify in class java.security.SignatureSpi
Parameters:
publicKey - the RSA/DSA public key of the identity whose signature is going to be verified.
Throws:
java.security.InvalidKeyException - if the key is not an instance of java.security.interfaces.RSAPublicKey or java.security.interfaces.DSAPublicKey.
Since:
1.0

engineSetParameter

public void engineSetParameter(java.lang.String param,
                               java.lang.Object value)
                        throws java.security.InvalidParameterException
Not supported. Will always throw an InvalidParameterException when called upon.

Specified by:
engineSetParameter in class java.security.SignatureSpi
Parameters:
param - is ignored.
value - is ignored.
Throws:
java.security.InvalidParameterException - is always thrown.
Since:
1.0

engineSign

public byte[] engineSign()
                  throws java.security.SignatureException
Returns the RSA or DSA signature bytes of all the data updated so far.

The signature implementation is reset to its initial state (the state it was in after a call to one of the engineInitSign methods) and can be reused to generate further RSA/DSA signatures with the same private key.

Specified by:
engineSign in class java.security.SignatureSpi
Returns:
the RSA/DSA signature bytes of the signing operation's result.
Throws:
java.security.SignatureException - if the engine is not initialized properly, or if the signature could not be created.
JCAPIJNIOperationCancelledException - if the user cancels the input of a password/PIN code required for accessing the private key.
JCAPIJNIInvalidPINCodeException - if the private key is protected and an invalid password/PIN code was given.
JCAPIJNIRuntimeException - if an unexpected error occur inside MS CAPI.
JCAPIJNIPKCS11RuntimeException - if an unexpected error occur inside the JCAPI PKCS#11 layer.
Since:
1.0

engineSign

public int engineSign(byte[] outbuf,
                      int offset,
                      int len)
               throws java.security.SignatureException
Finishes this signature operation and stores the resulting RSA or DSA signature bytes in the provided buffer outbuf, starting at offset.

The signature implementation is reset to its initial state (the state it was in after a call to one of the engineInitSign methods) and can be reused to generate further RSA/DSA signatures with the same private key.

Overrides:
engineSign in class java.security.SignatureSpi
Parameters:
outbuf - buffer for the RSA/DSA signature result.
offset - offset into outbuf where the signature is stored.
len - number of bytes within outbuf allotted for the RSA signature.
Returns:
the number of bytes placed into outbuf.
Throws:
java.security.SignatureException - if an error occurs or len is less than the actual RSA/DSA signature.
JCAPIJNIOperationCancelledException - if the user cancels the input of a password/PIN code required for accessing the private key.
JCAPIJNIInvalidPINCodeException - if the private key is protected and an invalid password/PIN code was given.
JCAPIJNIRuntimeException - if an unexpected error occur inside MS CAPI.
Since:
1.0

engineUpdate

public void engineUpdate(byte b)
                  throws java.security.SignatureException
Updates the data to be signed or verified using the specified byte.

Specified by:
engineUpdate in class java.security.SignatureSpi
Parameters:
b - the byte to use for the update.
Throws:
java.security.SignatureException - if the engine is not initialized properly.
JCAPIJNIRuntimeException - if an unexpected error occur inside MS CAPI.
Since:
1.0

engineUpdate

public void engineUpdate(byte[] b,
                         int off,
                         int len)
                  throws java.security.SignatureException
Updates the data to be signed or verified, using the specified array of bytes, starting at the specified offset.

Specified by:
engineUpdate in class java.security.SignatureSpi
Parameters:
b - the array of bytes.
off - the offset to start from in the array of bytes.
len - the number of bytes to use, starting at offset.
Throws:
java.security.SignatureException - if the engine is not initialized properly.
JCAPIJNIRuntimeException - if an unexpected error occur inside MS CAPI.
Since:
1.0

engineVerify

public boolean engineVerify(byte[] sigBytes)
                     throws java.security.SignatureException
Verifies the passed-in RSA/DSA signature.

Specified by:
engineVerify in class java.security.SignatureSpi
Parameters:
sigBytes - - the signature bytes to be verified.
Returns:
true if the RSA/DSA signature was verified, false if not.
Throws:
java.security.SignatureException - if the engine is not initialized properly, or the passed-in signature is improperly encoded or of the wrong type, etc.
JCAPIJNIRuntimeException - if an unexpected error occur inside MS CAPI.
Since:
1.0

engineVerify

public boolean engineVerify(byte[] sigBytes,
                            int offset,
                            int length)
                     throws java.security.SignatureException
Verifies the passed-in RSA/DSA signature in the specified array of bytes, starting at the specified offset.

Overrides:
engineVerify in class java.security.SignatureSpi
Parameters:
sigBytes - the signature bytes to be verified.
offset - the offset to start from in the array of bytes.
length - the number of bytes to use, starting at offset.
Returns:
true if the RSA/DSA signature was verified, false if not.
Throws:
java.security.SignatureException - if the engine is not initialized properly, or the passed-in signature is improperly encoded or of the wrong type, etc.
JCAPIJNIRuntimeException - if an unexpected error occur inside MS CAPI.
Since:
1.0


Copyright © 2011 Pheox. All Rights Reserved.