public class PKCS11Signer
extends java.lang.Object
Several methods are designed to ease object retrieval during an italian style
digital signature process. See
findCertificateWithNonRepudiationCritical()for details.
| Modifier and Type | Field and Description |
|---|---|
private java.lang.String |
cryptokiLibrary
The
cryptokiLibrary is the native library implementing the
PKCS#11 specification. |
private boolean |
libFinalized
The finalization state of
cryptokiLibrary? |
private java.io.PrintStream |
log
The
PrintStream where logging messages are written. |
private PKCS11 |
pkcs11Module
The java object wrapping criptoki library functionalities.
|
private long |
sessionHandle
The PKCS#11 session identifier returned when a session is opened.
|
private CK_MECHANISM |
signatureMechanism
PKCS#11 identifier for the signature algorithm.
|
private long |
tokenHandle
The PKCS#11 token identifier.
|
| Constructor and Description |
|---|
PKCS11Signer(java.lang.String cryptokiLib,
long mechanism,
java.io.PrintStream out) |
PKCS11Signer(java.lang.String cryptokiLib,
long mechanism,
java.lang.String reader,
java.io.PrintStream out) |
PKCS11Signer(java.lang.String cryptokiLib,
java.io.PrintStream out) |
| Modifier and Type | Method and Description |
|---|---|
void |
closeSession()
Closes the default PKCS#11 session.
|
void |
closeSession(long sessionHandle)
Closes a specific PKCS#11 session.
|
static java.lang.String |
decodeError(int errorCode)
Error decoding function.
|
byte[] |
encryptDigest(java.lang.String label,
byte[] digest) |
long |
findCertificateFromID(byte[] id)
Finds a certificate matching the given byte[] id.
|
long |
findCertificateFromLabel(char[] label)
Finds a certificate matching the given textual label.
|
long |
findCertificateFromSignatureKeyHandle(long signatureKeyHandle)
Searches the certificate corresponding to the private key identified by
the given handle; this method assumes that corresponding certificates and
private keys are sharing the same byte[] IDs.
|
long[] |
findCertificates()
Trova un'array di certHandle di tutti i certificati presenti sulla carta
senza che la sessione sia aperta (no password).
|
long |
findCertificateWithNonRepudiationCritical()
Queries the current token for a certificate suitable for a legal value
subscription.
|
long |
findCertificateWithNonRepudiationCritical(long token)
Queries the a specific token for a certificate suitable for a legal value
subscription.
|
long |
findSignatureKey()
Returns the first private key handle found on current token.
|
long |
findSignatureKeyFromCertificateHandle(long certHandle)
Searches the private key corresponding to the certificate identified by
the given handle; this method assumes that corresponding certificates and
private keys are sharing the same byte[] IDs.
|
long |
findSignatureKeyFromID(byte[] id)
Returns the private key handle, on current token, corresponding to the
given byte[].
|
long |
findSignatureKeyFromLabel(java.lang.String label)
Returns the private key handle, on current token, corresponding to the
given textual label.
|
long |
findSuitableToken(long mechanismCode) |
java.util.ArrayList |
findTokensSupportingMechanism(long mechanismCode) |
java.lang.String |
getCryptokiLibrary()
Gets the cryptoki library name.
|
byte[] |
getDEREncodedCertificate(long certHandle)
Returns the DER encoded certificate identified by the given handle, as
read from the token.
|
byte[] |
getDEREncodedCertificate(long certHandle,
long sessionHandle) |
byte[] |
getDEREncodedCertificateAndID(long certHandle,
java.io.ByteArrayOutputStream id)
Returns the DER encoded certificate identified by the given handle,
and its ID attribute.
|
byte[] |
getDEREncodedCertificateFromLabel(java.lang.String label)
Returns the DER encoded certificate corresponding to the given label, as
read from the token.
|
void |
getMechanismInfo()
Gets informations on cryptographic operations supported by the tokens.
|
private void |
getModuleInfo()
Gets currently loaded cryptoky description.
|
private PKCS11 |
getPkcs11()
Gets the java wrapper for the cryptoki.
|
private long |
getSession()
Gets the current session handle.
|
java.lang.String |
getSlotDescription(long slotID) |
private long[] |
getSlotList()
Gets current reader infos.
|
java.lang.String |
getTokenDescription() |
long |
getTokenHandle()
Gets the current token.
|
long[] |
getTokenList()
Lists currently inserted tokens and relative infos.
|
long[] |
getTokens()
Lists currently inserted tokens.
|
long |
getTokenSupportingMechanism(long mechanismCode)
Queries if there is a token that supporting a given cryptographic
operation.
|
private void |
initializeLibrary()
Initializes cryptoki library operations.
|
private void |
initializeTokenAndMechanism(long mechanism) |
private void |
initializeTokenInReader(java.lang.String reader) |
(package private) boolean |
isKeyUsageNonRepudiationCritical(java.security.cert.X509Certificate javaCert)
checks Key Usage constraints of a java certificate.
|
boolean |
isLibFinalized()
Is
cryptokiLibrary finalized (unlinked) ? |
boolean |
isMechanismSupportedByToken(long mechanismCode,
long tokenID)
Tells if a given token supports a given cryptographic operation.
|
void |
libFinalize()
Finalizes PKCS#11 operations; note this NOT actually unloads the native
library.
|
void |
login(char[] pwd)
Logs in to the current session; login is usually necessary to see and use
private key objects on the token.
|
void |
login(java.lang.String pwd)
Logs in to the current session; login is usually necessary to see and use
private key objects on the token.
|
void |
logout()
Logs out the current user.
|
void |
openSession()
Opens a session on the default token.
|
void |
openSession(char[] password)
Opens a session on the token, logging in the user.
|
long |
openSession(long aTokenHandle)
Opens a session on a specific token.
|
void |
setCryptokiLibrary(java.lang.String newCryptokiLibrary)
Sets the cryptoky library
|
void |
setMechanism(long mechanism) |
void |
setMechanism(long mechanism,
java.lang.Object pParameter) |
private void |
setSession(long newSession)
Sets the session handle.
|
void |
setTokenHandle(long token)
Sets the current token handle.
|
byte[] |
signDataMultiplePart(long signatureKeyHandle,
java.io.InputStream dataStream)
Sign (here means digesting and encrypting with private key) the provided
data with a multiple-pass operation.
|
byte[] |
signDataSinglePart(long signatureKeyHandle,
byte[] data)
Sign (here means encrypting with private key) the provided data with a
single operation.
|
private java.lang.String cryptokiLibrary
cryptokiLibrary is the native library implementing the
PKCS#11 specification.private boolean libFinalized
cryptokiLibrary?private long sessionHandle
private long tokenHandle
private PKCS11 pkcs11Module
private CK_MECHANISM signatureMechanism
private java.io.PrintStream log
PrintStream where logging messages are written.public PKCS11Signer(java.lang.String cryptokiLib,
long mechanism,
java.io.PrintStream out)
throws java.io.IOException,
TokenException
java.io.IOExceptionTokenExceptionpublic PKCS11Signer(java.lang.String cryptokiLib,
long mechanism,
java.lang.String reader,
java.io.PrintStream out)
throws java.io.IOException,
TokenException
java.io.IOExceptionTokenExceptionpublic PKCS11Signer(java.lang.String cryptokiLib,
java.io.PrintStream out)
throws java.io.IOException,
TokenException
java.io.IOExceptionTokenExceptionprivate void initializeLibrary()
throws PKCS11Exception
PKCS11Exceptionprivate void initializeTokenAndMechanism(long mechanism)
throws PKCS11Exception
PKCS11Exceptionprivate void initializeTokenInReader(java.lang.String reader)
throws PKCS11Exception
PKCS11Exceptionpublic void setMechanism(long mechanism,
java.lang.Object pParameter)
public void setMechanism(long mechanism)
public void closeSession()
throws PKCS11Exception
PKCS11Exceptionpublic void closeSession(long sessionHandle)
throws PKCS11Exception
sessionHandle - handle of the session to close.PKCS11Exceptionpublic static java.lang.String decodeError(int errorCode)
errorCode - id of the error.public long findSignatureKeyFromLabel(java.lang.String label)
throws PKCS11Exception
label - the string label to search.PKCS11Exceptionpublic long findSignatureKeyFromID(byte[] id)
throws PKCS11Exception
id - the byte[] id to search.PKCS11ExceptionfindSignatureKeyFromLabel(String)public long findSignatureKey()
throws PKCS11Exception
PKCS11Exceptionpublic byte[] signDataSinglePart(long signatureKeyHandle,
byte[] data)
throws java.io.IOException,
PKCS11Exception
signatureKeyHandle - handle of the private key to use for signing.data - the data to sign.java.io.IOExceptionPKCS11Exceptionpublic byte[] signDataMultiplePart(long signatureKeyHandle,
java.io.InputStream dataStream)
throws java.io.IOException,
PKCS11Exception
signatureKeyHandle - handle of the private key to use for signing.dataStream - an InputStram providing data to sign.java.io.IOExceptionPKCS11Exceptionpublic byte[] encryptDigest(java.lang.String label,
byte[] digest)
throws PKCS11Exception,
java.io.IOException
PKCS11Exceptionjava.io.IOExceptionpublic long findCertificateWithNonRepudiationCritical(long token)
throws TokenException,
java.security.cert.CertificateException
findCertificateWithNonRepudiationCritical().token - ID of the token to query for the certificate.TokenExceptionjava.security.cert.CertificateExceptionfindCertificateWithNonRepudiationCritical()public long findCertificateWithNonRepudiationCritical()
throws TokenException,
java.security.cert.CertificateException
According to the italian law, if you want give to the digital signature
the maximum legal value (equivalent to a signature on paper), and also
for the sake of interoperability, the signer certificate has to satisfy
some costraints. See
the official document in PDF format or this html page (only
in italian, sorry) for details.
In particular, the certificate has to carry a KeyUsage extension of 'non
repudiation' (OID: 2.5.29.15) marked as critical.
TokenExceptionjava.security.cert.CertificateExceptionpublic long[] findCertificates()
throws TokenException,
java.security.cert.CertificateException
TokenExceptionjava.security.cert.CertificateExceptionboolean isKeyUsageNonRepudiationCritical(java.security.cert.X509Certificate javaCert)
javaCert - the certificate to check as java object.findCertificateWithNonRepudiationCritical()public long findCertificateFromID(byte[] id)
throws PKCS11Exception
id - PKCS11Exceptionpublic long findCertificateFromLabel(char[] label)
throws PKCS11Exception
label - PKCS11Exceptionpublic long findCertificateFromSignatureKeyHandle(long signatureKeyHandle)
throws PKCS11Exception
signatureKeyHandle - the handle of a private key.PKCS11Exceptionpublic long findSignatureKeyFromCertificateHandle(long certHandle)
throws PKCS11Exception
certHandle - the handle of a certificate.PKCS11Exceptionpublic byte[] getDEREncodedCertificateFromLabel(java.lang.String label)
throws TokenException
label - the object label on the token.java.io.UnsupportedEncodingExceptionTokenExceptionpublic byte[] getDEREncodedCertificate(long certHandle)
throws PKCS11Exception
certHandle - the handleof the certificate on the token.java.io.UnsupportedEncodingExceptionTokenExceptionPKCS11Exceptionpublic byte[] getDEREncodedCertificateAndID(long certHandle,
java.io.ByteArrayOutputStream id)
throws PKCS11Exception,
java.io.IOException
certHandle - the handle of the certificate on the token, as a byte array.id - the ID of the Certificate as a ByteArrayOutputStream.java.io.IOExceptionjava.io.UnsupportedEncodingExceptionTokenExceptionPKCS11Exceptionpublic byte[] getDEREncodedCertificate(long certHandle,
long sessionHandle)
throws PKCS11Exception
PKCS11Exceptionpublic java.lang.String getSlotDescription(long slotID)
public java.lang.String getCryptokiLibrary()
private PKCS11 getPkcs11()
private long getSession()
long identifying the current session.public void libFinalize()
throws java.lang.Throwable
java.lang.Throwablepublic void login(java.lang.String pwd)
throws PKCS11Exception
String as a char[] and calls
login(char[]).pwd - password as a String.PKCS11Exceptionpublic void login(char[] pwd)
throws PKCS11Exception
pwd - password as a char[].PKCS11Exceptionpublic void logout()
throws PKCS11Exception
PKCS11Exceptionprivate void getModuleInfo()
throws PKCS11Exception
PKCS11Exceptionprivate long[] getSlotList()
throws PKCS11Exception
PKCS11Exceptionpublic long[] getTokenList()
PKCS11Exceptionpublic long[] getTokens()
throws PKCS11Exception
PKCS11Exceptionpublic java.lang.String getTokenDescription()
throws PKCS11Exception
PKCS11Exceptionpublic void getMechanismInfo()
throws PKCS11Exception
PKCS11Exceptionpublic long findSuitableToken(long mechanismCode)
throws PKCS11Exception
PKCS11Exceptionpublic java.util.ArrayList findTokensSupportingMechanism(long mechanismCode)
throws PKCS11Exception
PKCS11Exceptionpublic long getTokenSupportingMechanism(long mechanismCode)
throws PKCS11Exception
mechanismCode - the ID of the required mechanism.PKCS11Exceptionpublic boolean isMechanismSupportedByToken(long mechanismCode,
long tokenID)
throws PKCS11Exception
mechanismCode - the mechanism ID.tokenID - the token handla.true if the token supports the mechanism.PKCS11Exceptionpublic long openSession(long aTokenHandle)
throws TokenException
aTokenHandle - the token ID.TokenExceptionpublic void openSession()
throws TokenException
TokenExceptionpublic void openSession(char[] password)
throws TokenException,
PKCS11Exception
TokenExceptionPKCS11Exceptionpublic void setCryptokiLibrary(java.lang.String newCryptokiLibrary)
newCryptokiLibrary - the cryptoki name.private void setSession(long newSession)
newSession - public long getTokenHandle()
public void setTokenHandle(long token)
token - the token handle to set.public boolean isLibFinalized()
cryptokiLibrary finalized (unlinked) ?