public class EncryptedContent
extends java.lang.Object
S/MIME (Secure/Multipurpose Internet Mail Extensions) provides a consistent way to send and receive secure MIME data. Based on the popular Internet MIME standard, S/MIME provides the following cryptographic security services for electronic messaging applications: authentication, message integrity and non-repudiation of origin (using digital signatures) and privacy and data security (using encryption) (see S/MIME Version 2 Message Specification).
This class supports the creation and handling of S/MIME encrypted (enveloped)
messages in combination with the jakarta.mail architecture.
For creating and working with signed S/MIME messages, use the
SignedContent
class of the iaik.security.smime package.
S/MIME (Secure/Multipurpose Internet Mail Extensions) specifies the application/pkcs7-mime (smime-type "enveloped-data") type for data enveloping.
The whole (prepared) MIME entity to be enveloped into a PKCS #7: Cryptographic Message Syntax
Version 1.5 (RFC 2315) object of
type EnvelopedData which subsequently is inserted into an
application/pkcs7-mime MIME entity. The smime-type parameter for
enveloped messages is "enveloped-data", the file extension is ".p7m" (see
S/MIME Version 2 Message Specification). The "Content-" headers of a sample
message would look like:
Content-Type: application/pkcs7-mime; smime-type=enveloped-data;
name="smime.p7m"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="smime.p7m"
rfvbn...
For creating a new EncryptedContent to be sent, first use a proper constructor,
and subsequently supply the content by means of a setContent, setText, or setDataHandler
method. Recipients are added by calling the addRecipient method thereby specifying the recipient´s certificate,
and the key encryption algorithm to be used (which has to be AlgorithmID.rsaEncryption).
When setting the content encryption algorithm by means of the setEncryptionAlgorithm method, optionally
the key length may be specified. The
S/MIME Version 2 Message Specification recommends DES EDE3 CBC and RC2 CBC
to be used as content encryption algorithms. Only for the latter the key length has
a meaning and may specify 40, 68, or 128 (default) bits. Note, that according S/MIME,
this implementation only allows DES EDE3 CBC, DES CBC, and RC2 CBC
as content encryption algorithms. Before actually sending the Message with the
encrypted content, the setHeaders method shall be
called for properly updating the message headers.
Typical usage:
MimeMessage msg = new MimeMessage(session); ... EncryptedContent ec = new EncryptedContent(); // set the smime-type parameter to "enveloped-data" ec.setSMimeType(); ec.setContent(...); ec.addRecipient(recipientCertificate, AlgorithmID.rsaEncryption); ec.setEncryptionAlgorithm(algorithm, keyLength); // update message headers ec.setHeaders(msg); msg.setContent(ec, ec.getContentType()); Transport.send(msg);A recipient uses the
decryptSymmetricKey method for decrypting the encrypted content encryption
key with her/his private key, and subsequently reads the content, e.g.:
EncryptedContent ec = (EncryptedContent)msg.getContent(); //recipient at index 0 decrypts the content encryption key: ec.decryptSymmetricKey(privateKey, 0); // get the content Object content = ec.getContent(); ...For more information about the JavaMail architecture, and how to parsing messages, consult Sun´s JavaMail specification.
For using the IAIK-JCE S/MIME classes, an application also will need the packages:
# Default mailcap file for the JavaMail System # # for our content-handlers # text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed message/*;; x-java-content-handler=com.sun.mail.handlers.message_rfc822 # # IAIK 'mailcap' file # multipart/signed;; x-java-content-handler=iaik.security.smime.signed_content application/x-pkcs7-signature;; x-java-content-handler=iaik.security.smime.signed_content application/x-pkcs7-mime;; x-java-content-handler=iaik.security.smime.encrypted_content application/x-pkcs10;; x-java-content-handler=iaik.security.smime.pkcs10_content application/pkcs7-signature;; x-java-content-handler=iaik.security.smime.signed_content application/pkcs7-mime;; x-java-content-handler=iaik.security.smime.encrypted_content application/pkcs10;; x-java-content-handler=iaik.security.smime.pkcs10_contentWhen creating a new EncryptedContent to be sent per default the old S/MIME content types (application/x-pkcs7-mime, application/x-pkcs7-signature) are used. For using the new types call the static
useNewContentTypes method of the SMimeParameters class before creating a new EncryptedContent
object, e.g.:
//switch to new content types SMimeParameters.useNewContentTypes(true); //create a SignedContent EncryptedContent sc = new EncryptedContent(); ...
SMimeEncrypted| Constructor and Description |
|---|
EncryptedContent()
Creates a new EncryptedContent object.
|
EncryptedContent(jakarta.activation.DataSource dataSource)
Constructs a EncryptedContent object from the given data source.
|
EncryptedContent(SignedContent signedContent)
Creates a new S/MIME encrypted and signed content.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addRecipient(X509Certificate certificate,
AlgorithmID keyEncAlg)
Adds one recipient.
|
void |
decryptSymmetricKey(KeyAndCertificateSource keyAndCertificateSource)
Locates one of user's decryption keys that is appropriate for decrypting one
of the content-encryption keys, decrypts the content-encryption key, and
prepares the cipher for decrypting the encrypted content.
|
int |
decryptSymmetricKey(java.security.PrivateKey recipientPrivateKey,
int recipientInfoIndex)
Uses the specified private key to setup the cipher for decrypting this S/MIME object.
|
void |
decryptSymmetricKey(User user)
Locates one of user's decryption keys that is appropriate for decrypting one
of the content-encryption keys, decrypts the content-encryption key, and
prepares the cipher for decrypting the encrypted content.
|
java.lang.Object |
getContent()
Returns the content as a Java object.
|
java.io.InputStream |
getContentInputStream()
Returns an InputStream for this part's unparsed content.
|
java.lang.String |
getContentType()
Returns the ContentType and any attached parameters of this EncryptedObject.
|
jakarta.activation.DataHandler |
getDataHandler()
Return a DataHandler for the content within this part.
|
AlgorithmID |
getEncryptionAlgorithm()
Returns the content-encryption algorithm (including any associated parameters) of this
EncryptedContent.
|
java.io.InputStream |
getInputStream()
Returns an InputStream for this part's content.
|
int |
getRecipientInfoIndex(X509Certificate recipientCertificate)
Returns the recipient info index matching to the supplied recipient certificate.
|
RecipientInfo[] |
getRecipientInfos()
Returns information about all recipients of this message.
|
void |
setContent(jakarta.mail.Multipart multipart)
This method sets the given Multipart object as this EncryptedObject's content.
|
void |
setContent(java.lang.Object content,
java.lang.String type)
A convenience method for setting this EncryptedObject's content.
|
void |
setContentContentHeaders(jakarta.mail.Header[] headers)
Set some headers for the entity to be signed.
|
void |
setContentContentTransferEncoding(java.lang.String cte)
Sets the content transfer encoding of the entity to be signed.
|
void |
setDataHandler(jakarta.activation.DataHandler dataHandler)
Sets the content wrapped by a
javax.activation.DataHandler. |
void |
setEncryptionAlgorithm(AlgorithmID contentEncAlg,
int keyLength)
Sets the symmetric algorithm for encrypting the message.
|
void |
setHeaders(jakarta.mail.Message message)
Sets additional headers of the message containing this EncryptedContent.
|
void |
setSMimeType()
Sets the smime-type parameter to "enveloped-data".
|
void |
setText(java.lang.String text)
A convenience method that sets the given String as this EncryptedObject's content
with a MIME type of "text/plain".
|
void |
writeTo(java.io.OutputStream os)
Writes this EncryptedContent DER encoded to the given output stream.
|
public EncryptedContent()
Use a proper setContent, setText, or
setDataHandler method for supplying the
content to be enveloped. Recipients are added by calling the
addRecipient method
thereby specifying the recipient´s certificate, and the key encryption
algorithm to be used (which has to be AlgorithmID.rsaEncryption).
When setting the content encryption algorithm by means of the setEncryptionAlgorithm method, optionally
the key length may be specified. The
S/MIME Version 2 Message Specification recommends DES EDE3 CBC and RC2 CBC
to be used as content encryption algorithms. Only for the latter the key length has
a meaning and may specify 40, 68, or 128 (default) bits. Note, that according S/MIME,
this implementation only allows DES EDE3 CBC, DES CBC, and RC2 CBC
as content encryption algorithms. Before actually sending the Message with the
encrypted content, the setHeaders method shall be
called for properly updating the message headers.
Typical usage:
MimeMessage msg = new MimeMessage(session); ... EncryptedContent ec = new EncryptedContent(); ec.setContent(...); ec.addRecipient(recipientCertificate, AlgorithmID.rsaEncryption); ec.setEncryptionAlgorithm(algorithm, keyLength); ec.setHeaders(msg); msg.setContent(ec, ec.getContentType()); Transport.send(msg);When using this constructor the smime-type parameter will not be set. Use the
setSMimeType parameter for setting the
"enveloped-data" smime-type parameter.public EncryptedContent(SignedContent signedContent)
setSMimeType parameter for setting the
"enveloped-data" smime-type parameter.signedContent - the SignedContent as content of this EncryptedContentpublic EncryptedContent(jakarta.activation.DataSource dataSource)
throws java.io.IOException
encrypted_content supplying the data source.
For more information on data handling using the
javax.activation.DataSource for "MIME type based" data
access, see Sun´s
JavaBeans Activation Framework (JAF) sepecification.
dataSource - the DataSource supplying the enveloped datajava.io.IOException - if an I/O error occurs during reading the objectpublic void setSMimeType()
public java.lang.String getContentType()
Message msg = ...; EncryptedContent ec = new EncryptedContent(); msg.setContent(ec, ec.getContentType()); ...
public void setDataHandler(jakarta.activation.DataHandler dataHandler)
javax.activation.DataHandler.
This method provides the mechanism to set this EncryptedContent's content, e.g:
DataHandler dataHandler = new DataHandler(...); EncryptedContent sc = new EncryptedContent(); ec.setDataHandler(dataHandler);The DataHandler wraps around the actual content. It allows clients to discover the operations available on the content, and to instantiate the appropriate component to perform those operations. For more information consult Sun´s JavaBeans Activation Framework (JAF) sepecification.
dataHandler - the DataHandler for the contentpublic void setContent(java.lang.Object content,
java.lang.String type)
Note that a DataContentHandler class for the specified type should be available to the JavaMail implementation for this to work right; i.e., to do setContent(foobar, "application/x-foobar"), a DataContentHandler for "application/x-foobar" should be installed. Refer to the Java Activation Framework for more information.
content - a java objecttype - MIME type of this objectpublic void setText(java.lang.String text)
text - the text that is the EncryptedObject's contentpublic void setContent(jakarta.mail.Multipart multipart)
multipart - the multipart object that is the EncryptedObject's contentpublic RecipientInfo[] getRecipientInfos()
public int getRecipientInfoIndex(X509Certificate recipientCertificate)
recipientCertificate - the certificate of the recipient-1 if no recipient info belonging to the given
certificate can be foundpublic int decryptSymmetricKey(java.security.PrivateKey recipientPrivateKey,
int recipientInfoIndex)
throws SMimeException,
java.security.InvalidKeyException
recipientPrivateKey - the private of one recipient specified in
a RecipientInfo objectrecipientInfoIndex - specifies which RecipientInfo the private key
belongs toSMimeException - if there is an error while decrypting the datajava.security.InvalidKeyException - if the supplied private key is inappropriatepublic void decryptSymmetricKey(User user) throws UserNotLoggedInException, NotARecipientException, SMimeException
The cipher will be only initialized for decrypting with this call; The
encrypted-content decryption actually is done when the data is read by
calling the getInputStream,
getContent, or getDataHandler
method. So don´t call any of these methods before decrypting the encrypted
content-encryption key!
user - the userUserNotLoggedInException - if the user is not logged inNotARecipientException - if the user is not listed as a recipient (unable to decrypt the content)SMimeException - if an error occurs while decrypting the content-encryption key or
setting up the cipher for decrypting the contentpublic void decryptSymmetricKey(KeyAndCertificateSource keyAndCertificateSource) throws NotARecipientException, SMimeException
The cipher will be only initialized for decrypting with this call; The
encrypted-content decryption actually is done when the data is read by
calling the getInputStream,
getContent, or getDataHandler
method. So don´t call any of these methods before decrypting the encrypted
content-encryption key!
keyAndCertificateSource - a user's keys and certificatesNotARecipientException - if the user is not listed as a recipient (unable to decrypt the content)SMimeException - if an error occurs while decrypting the content-encryption key or
setting up the cipher for decrypting the contentpublic void setHeaders(jakarta.mail.Message message)
Content-Disposition: attachment";
filename="smime.p7m"
Content-Transfer-Encoding: base64
message - message to add the headerspublic java.lang.Object getContent()
throws java.io.IOException,
jakarta.mail.MessagingException
java.io.IOException - if an I/O error occurs when getting the content from the inherent data handlerjakarta.mail.MessagingException - if an error occurs when fetching the inherent data handlerpublic jakarta.activation.DataHandler getDataHandler()
throws jakarta.mail.MessagingException
jakarta.mail.MessagingException - if an error occurs when fetching the data handlerpublic java.io.InputStream getInputStream()
throws java.io.IOException,
jakarta.mail.MessagingException
java.io.IOException - if an I/O error occurs when getting the input stream from the inherent data handlerjakarta.mail.MessagingException - if an error occurs when fetching the inherent data handlerpublic java.io.InputStream getContentInputStream()
throws java.io.IOException
This method can be used to get the content of large messages.
java.io.IOExceptionpublic void addRecipient(X509Certificate certificate, AlgorithmID keyEncAlg)
certificate - the certificate of one recipientkeyEncAlg - the key encryption algorithm to use (i.e. AlgorithmID.rsaEncryption)public void setEncryptionAlgorithm(AlgorithmID contentEncAlg, int keyLength)
keyLength
parameter is ignored.
The default value is AlgorithmID.des_EDE3_CBC
Possible encryption algorithms are:
contentEncAlg - the algorithm for encrypting the contentkeyLength - the key length for RC2public AlgorithmID getEncryptionAlgorithm()
public void setContentContentTransferEncoding(java.lang.String cte)
setText or
setDataHandler is used for
supplying the content. However, the supplied content transfer
encoding will be ignored when the inner entity is a structured
entity.cte - the content transfer encoding to be applied to the
inner entity to be signedpublic void setContentContentHeaders(jakarta.mail.Header[] headers)
setText or
setDataHandler is used for
supplying the content. However, the supplied header will be ignored
when the inner entity is a structured entity. The supplied headers
are not cloned!headers - the headers to be applied to the
inner entity to be signedpublic void writeTo(java.io.OutputStream os)
throws java.io.IOException,
jakarta.mail.MessagingException
java.io.IOException - if an error occurs writing to the streamjakarta.mail.MessagingException - if an error occurs when fetching the data to be written