SigneServlet
inside the firma-digitale web application for replacement.CMSServlet
is the server side part of the j4sign usage example
in a web environment.CMSServlet
is a HttpServlet
that takes care of
generating and sending to the web client the content to sign and the
corresponding bytes to digest and encrypt. After receiving the signature and
the signer certificate, it encapsulates them, along with the signed content,
in a CMS signed data message.
The entire example, with the
SimpleSignApplet
counterpart, is
designed to permit the use of the standard JDK tools. The applet can be
executed with applet viewer tool (no HttpSession in the servlet, nor HTML
forms on the client side are used).
N.B.: IN A REAL WORLD WEB APPLICATION SCENARIO, YOU CAN (AND SHOULD) TAKE ADVANTAGE OF THE FULL SERVLET API.
Here are the CMSServlet
operations in detail:
retrieve
parameter with value DATA
:retrieve
parameter with value ENCODED_AUTHENTICATED_ATTRIBUTES
:ExternalSignatureSignerInfoGenerator
object, using the
message to sign, and specifying MD5 with RSA encryption as signature
algoritm;bytesToSign
, the bytes to digest
and encrypt (ASN.1 Authenticated attributes);ExternalSignatureSignerInfoGenerator
, and a textual
dump of authenticates attributes,bytesToSign
as a key.bytesToSign
as HTTP response.retrieve
parameter with value AUTHENTICATED_ATTRIBUTES_PRINTOUT
and a
encodedHash
parameter with base64 encoded value:encodedHash
as a key the textual dump of
Authenticates attributes, and sends it as HTTP responses.signature
parameter and a certificate
parameter, both with base64 encoded
values:signature
and certificate
;certificate
the public key of the signer, and uses
it to decrypt, using RSA algorithm, the signature
.ExternalSignatureSignerInfoGenerator
. If such an object is
found, the signature is verified.ExternalSignatureSignerInfoGenerator
, completed with
signature
and certificate
informations, is passed
to a
ExternalSignatureCMSSignedDataGenerator
for creating the CMS message.public class CMSServlet
extends javax.servlet.http.HttpServlet
Modifier and Type | Class and Description |
---|---|
private class |
CMSServlet.SignerInfoGeneratorItem
Deprecated.
Class encapsulating a SignerInfoGenerator-related informations to be
stored after a signature request.
|
Modifier and Type | Field and Description |
---|---|
private static java.lang.String |
DATA
Deprecated.
DATA is the sample data contet to be signed; it's a text
shortly explaining what is going to happen. |
private java.util.Hashtable |
signerInfoGeneratorTable
Deprecated.
The repository for
CMSServlet.SignerInfoGeneratorItem objects. |
Constructor and Description |
---|
CMSServlet()
Deprecated.
|
Modifier and Type | Method and Description |
---|---|
private java.lang.String |
base64Encode(byte[] bytes)
Deprecated.
A BASE64 encoding function, using the
sun.misc.BASE64Encoder
implementation. |
private org.bouncycastle.cms.CMSSignedData |
buildCMSSignedData(ExternalSignatureSignerInfoGenerator infoGen,
byte[] sigBytes,
byte[] certBytes)
Deprecated.
Builds the CMS signed data message.
|
private ExternalSignatureSignerInfoGenerator |
buildSignerInfoGenerator()
Deprecated.
Creates a
ExternalSignatureSignerInfoGenerator with a
MD5 digest algorithm and RSA encryption
algorithm. |
private java.lang.String |
deriveStoreKey(byte[] sigBytes,
byte[] certBytes)
Deprecated.
Converts the provided
certBytes in a
java.security.cert.X509Certificate , gets from it the signer
public key, and uses it to decrypt sigBytes . |
protected void |
doGet(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
Deprecated.
Implementation of the GET method; returns informations to the client and
stores SignerInfoGenerator-related informations; see
CMSServlet
for details. |
protected void |
doPost(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
Deprecated.
Implementation of the POST method; builds the CMS message; see
CMSServlet for details. |
private byte[] |
encapsulateInDigestInfo(java.lang.String digestAlg,
byte[] digestBytes)
Deprecated.
|
java.lang.String |
formatAsString(byte[] bytes,
java.lang.String byteSeparator)
Deprecated.
Formats a byte[] as an hexadecimal String, interleaving bytes with a
separator string.
|
private byte[] |
getAuthenticatedAttributesBytes(ExternalSignatureSignerInfoGenerator signerGenerator)
Deprecated.
Uses the provided
ExternalSignatureSignerInfoGenerator for
calculating the authenticated attributes bytes to be digested-encrypted
by the signer. |
private java.lang.String |
getAuthenticatedAttributesPrintout(byte[] bytes)
Deprecated.
A text message resulting from a dump of provided authenticated attributes
data.
|
void |
init()
Deprecated.
Adds BouncyCastle provider at servlet initialization time.
|
private ExternalSignatureSignerInfoGenerator |
retriveSignerInfoGenerator(java.lang.String storeKey)
Deprecated.
Gets the
ExternalSignatureSignerInfoGenerator generator
which originally produced the given storeKey . |
private boolean |
saveFile(org.bouncycastle.cms.CMSSignedData s,
java.lang.String filePath)
Deprecated.
Saves a CMS signed data file on the server file system; the extension
should be ".p7m" according to italian rules.
|
doDelete, doHead, doOptions, doPut, doTrace, getLastModified, service, service
private static final java.lang.String DATA
DATA
is the sample data contet to be signed; it's a text
shortly explaining what is going to happen.private java.util.Hashtable signerInfoGeneratorTable
CMSServlet.SignerInfoGeneratorItem
objects. Stores
SignerInfoGenerator-related informations between HTTP requests.protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException
CMSServlet
for details.doGet
in class javax.servlet.http.HttpServlet
javax.servlet.ServletException
java.io.IOException
CMSServlet
protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException
CMSServlet
for details.doPost
in class javax.servlet.http.HttpServlet
javax.servlet.ServletException
java.io.IOException
CMSServlet
public java.lang.String formatAsString(byte[] bytes, java.lang.String byteSeparator)
bytes
- the byte[] to format.byteSeparator
- the string to be used to separate bytes.private java.lang.String deriveStoreKey(byte[] sigBytes, byte[] certBytes)
certBytes
in a
java.security.cert.X509Certificate
, gets from it the signer
public key, and uses it to decrypt sigBytes
. The decryption
result is returned as a formatted exadecimal string; see
CMSServlet
for details.sigBytes
- signature bytescertBytes
- certificate bytesprivate ExternalSignatureSignerInfoGenerator retriveSignerInfoGenerator(java.lang.String storeKey)
ExternalSignatureSignerInfoGenerator
generator
which originally produced the given storeKey
.storeKey
- ExternalSignatureSignerInfoGenerator
associated with
thestoreKey
private org.bouncycastle.cms.CMSSignedData buildCMSSignedData(ExternalSignatureSignerInfoGenerator infoGen, byte[] sigBytes, byte[] certBytes)
infoGen
- the ExternalSignatureSignerInfoGenerator
wrapping
signer informationssigBytes
- the digest encrypted with signer private key.certBytes
- the signer certificate.CMSSignedData
message.private boolean saveFile(org.bouncycastle.cms.CMSSignedData s, java.lang.String filePath)
s
- the CMSSignedData
object to save.filePath
- full path of the file.private ExternalSignatureSignerInfoGenerator buildSignerInfoGenerator()
ExternalSignatureSignerInfoGenerator
with a
MD5
digest algorithm and RSA
encryption
algorithm.ExternalSignatureSignerInfoGenerator
objectprivate java.lang.String base64Encode(byte[] bytes)
sun.misc.BASE64Encoder
implementation.bytes
- the bytes to encode.BASE64
encoding of bytes
.private byte[] getAuthenticatedAttributesBytes(ExternalSignatureSignerInfoGenerator signerGenerator)
ExternalSignatureSignerInfoGenerator
for
calculating the authenticated attributes bytes to be digested-encrypted
by the signer. Note that the attributes include a timestamp, so the
result is time-dependent!signerGenerator
- the ExternalSignatureSignerInfoGenerator
object
that does the job.private java.lang.String getAuthenticatedAttributesPrintout(byte[] bytes)
bytes
- the ASN.1 DER set of authenticated attributes.private byte[] encapsulateInDigestInfo(java.lang.String digestAlg, byte[] digestBytes) throws java.io.IOException
java.io.IOException
public void init() throws javax.servlet.ServletException
init
in class javax.servlet.GenericServlet
javax.servlet.ServletException