Class SMIMESignedParser
A simple example of usage - note, in the example below the validity of the certificate isn't verified, just the fact that one of the certs matches the given signer...
CertStore certs = s.getCertificates("Collection", "BC");
SignerInformationStore signers = s.getSignerInfos();
Collection c = signers.getSigners();
Iterator it = c.iterator();
while (it.hasNext())
{
SignerInformation signer = (SignerInformation)it.next();
Collection certCollection = certs.getCertificates(signer.getSID());
Iterator certIt = certCollection.iterator();
X509Certificate cert = (X509Certificate)certIt.next();
if (signer.verify(cert.getPublicKey()))
{
verified++;
}
}
Note: if you are using this class with AS2 or some other protocol that does not use 7bit as the default content transfer encoding you will need to use the constructor that allows you to specify the default content transfer encoding, such as "binary".
-
Field Summary
FieldsFields inherited from class org.bouncycastle.cms.CMSContentInfoParser
_contentInfo, _data -
Constructor Summary
ConstructorsConstructorDescriptionSMIMESignedParser(org.bouncycastle.operator.DigestCalculatorProvider digCalcProvider, javax.mail.internet.MimeMultipart message) base constructor using a defaultContentTransferEncoding of 7bit.SMIMESignedParser(org.bouncycastle.operator.DigestCalculatorProvider digCalcProvider, javax.mail.internet.MimeMultipart message, File backingFile) base constructor using a defaultContentTransferEncoding of 7bit and a specified backing file.SMIMESignedParser(org.bouncycastle.operator.DigestCalculatorProvider digCalcProvider, javax.mail.internet.MimeMultipart message, String defaultContentTransferEncoding) base constructor with settable contentTransferEncoding.SMIMESignedParser(org.bouncycastle.operator.DigestCalculatorProvider digCalcProvider, javax.mail.internet.MimeMultipart message, String defaultContentTransferEncoding, File backingFile) base constructor with settable contentTransferEncoding and a specified backing file.SMIMESignedParser(org.bouncycastle.operator.DigestCalculatorProvider digCalcProvider, javax.mail.Part message) base constructor for a signed message with encapsulated content.SMIMESignedParser(org.bouncycastle.operator.DigestCalculatorProvider digCalcProvider, javax.mail.Part message, File file) Constructor for a signed message with encapsulated content. -
Method Summary
Modifier and TypeMethodDescriptionjavax.mail.internet.MimeBodyPartreturn the content that was signed.javax.mail.internet.MimeMessagegetContentAsMimeMessage(javax.mail.Session session) Return the content that was signed as a mime message.return the content that was signed with its signature attached.static SMIMESignedParsergetSafeInstance(org.bouncycastle.operator.DigestCalculatorProvider digCalcProvider, javax.mail.internet.MimeMultipart message) Factory method for creating a safe SMIMESignedParser instance from a MimeMultipart.static SMIMESignedParsergetSafeInstance(org.bouncycastle.operator.DigestCalculatorProvider digCalcProvider, javax.mail.internet.MimeMultipart message, String defaultEncoding, File backingFile) Create a new SMIMESignedParser from a MimeMultipart.static SMIMESignedParsergetSafeInstance(org.bouncycastle.operator.DigestCalculatorProvider digCalcProvider, javax.mail.Part message) Create a new SMIMESignedParser from a Part.static SMIMESignedParsergetSafeInstance(org.bouncycastle.operator.DigestCalculatorProvider digCalcProvider, javax.mail.Part message, File file) Create a new SMIMESignedParser from a Part with a backing file.Methods inherited from class org.bouncycastle.cms.CMSSignedDataParser
getAttributeCertificates, getCertificates, getCRLs, getDigestAlgorithmIDs, getOtherRevocationInfo, getSignedContent, getSignedContentTypeOID, getSignerInfos, getVersion, replaceCertificatesAndCRLs, replaceSignersMethods inherited from class org.bouncycastle.cms.CMSContentInfoParser
close
-
Field Details
-
message
-
protected javax.mail.internet.MimeBodyPart content
-
-
Constructor Details
-
SMIMESignedParser
public SMIMESignedParser(org.bouncycastle.operator.DigestCalculatorProvider digCalcProvider, javax.mail.internet.MimeMultipart message) throws javax.mail.MessagingException, org.bouncycastle.cms.CMSException base constructor using a defaultContentTransferEncoding of 7bit. A temporary backing file will be created for the signed data.- Parameters:
digCalcProvider- provider for digest calculators.message- signed message with signature.- Throws:
javax.mail.MessagingException- on an error extracting the signature or otherwise processing the message.org.bouncycastle.cms.CMSException- if some other problem occurs.
-
SMIMESignedParser
public SMIMESignedParser(org.bouncycastle.operator.DigestCalculatorProvider digCalcProvider, javax.mail.internet.MimeMultipart message, File backingFile) throws javax.mail.MessagingException, org.bouncycastle.cms.CMSException base constructor using a defaultContentTransferEncoding of 7bit and a specified backing file.- Parameters:
digCalcProvider- provider for digest calculators.message- signed message with signature.backingFile- the temporary file to use to back the signed data.- Throws:
javax.mail.MessagingException- on an error extracting the signature or otherwise processing the message.org.bouncycastle.cms.CMSException- if some other problem occurs.
-
SMIMESignedParser
public SMIMESignedParser(org.bouncycastle.operator.DigestCalculatorProvider digCalcProvider, javax.mail.internet.MimeMultipart message, String defaultContentTransferEncoding) throws javax.mail.MessagingException, org.bouncycastle.cms.CMSException base constructor with settable contentTransferEncoding. A temporary backing file will be created to contain the signed data.- Parameters:
digCalcProvider- provider for digest calculators.message- the signed message with signature.defaultContentTransferEncoding- new default to use.- Throws:
javax.mail.MessagingException- on an error extracting the signature or otherwise processing the message.org.bouncycastle.cms.CMSException- if some other problem occurs.r
-
SMIMESignedParser
public SMIMESignedParser(org.bouncycastle.operator.DigestCalculatorProvider digCalcProvider, javax.mail.internet.MimeMultipart message, String defaultContentTransferEncoding, File backingFile) throws javax.mail.MessagingException, org.bouncycastle.cms.CMSException base constructor with settable contentTransferEncoding and a specified backing file.- Parameters:
digCalcProvider- provider for digest calculators.message- the signed message with signature.defaultContentTransferEncoding- new default to use.backingFile- the temporary file to use to back the signed data.- Throws:
javax.mail.MessagingException- on an error extracting the signature or otherwise processing the message.org.bouncycastle.cms.CMSException- if some other problem occurs.
-
SMIMESignedParser
public SMIMESignedParser(org.bouncycastle.operator.DigestCalculatorProvider digCalcProvider, javax.mail.Part message) throws javax.mail.MessagingException, org.bouncycastle.cms.CMSException, SMIMEException base constructor for a signed message with encapsulated content.Note: in this case the encapsulated MimeBody part will only be suitable for a single writeTo - once writeTo has been called the file containing the body part will be deleted. If writeTo is not called the file will be left in the temp directory.
- Parameters:
digCalcProvider- provider for digest calculators.message- the message containing the encapsulated signed data.- Throws:
javax.mail.MessagingException- on an error extracting the signature or otherwise processing the message.SMIMEException- if the body part encapsulated in the message cannot be extracted.org.bouncycastle.cms.CMSException- if some other problem occurs.
-
SMIMESignedParser
public SMIMESignedParser(org.bouncycastle.operator.DigestCalculatorProvider digCalcProvider, javax.mail.Part message, File file) throws javax.mail.MessagingException, org.bouncycastle.cms.CMSException, SMIMEException Constructor for a signed message with encapsulated content. The encapsulated content, if it exists, is written to the file represented by the File object passed in.- Parameters:
digCalcProvider- provider for digest calculators.message- the Part containing the signed content.file- the file the encapsulated part is to be written to after it has been decoded.- Throws:
javax.mail.MessagingException- on an error extracting the signature or otherwise processing the message.SMIMEException- if the body part encapsulated in the message cannot be extracted.org.bouncycastle.cms.CMSException- if some other problem occurs.
-
-
Method Details
-
getSafeInstance
public static SMIMESignedParser getSafeInstance(org.bouncycastle.operator.DigestCalculatorProvider digCalcProvider, javax.mail.internet.MimeMultipart message) throws javax.mail.MessagingException, org.bouncycastle.cms.CMSException Factory method for creating a safe SMIMESignedParser instance from a MimeMultipart. Uses "7bit" as the default content transfer encoding and automatically creates a temporary file to back the signed content.- Parameters:
digCalcProvider- provider for digest calculators.message- the multipart message.- Returns:
- a safely constructed SMIMESignedParser instance.
- Throws:
javax.mail.MessagingException- if there's an error accessing the message or creating the temp file.org.bouncycastle.cms.CMSException- if the CMS structure is malformed or cannot be parsed.
-
getSafeInstance
public static SMIMESignedParser getSafeInstance(org.bouncycastle.operator.DigestCalculatorProvider digCalcProvider, javax.mail.internet.MimeMultipart message, String defaultEncoding, File backingFile) throws javax.mail.MessagingException, org.bouncycastle.cms.CMSException Create a new SMIMESignedParser from a MimeMultipart.This method ensures that if the input stream for the signature is a PipedInputStream, it will be closed automatically if the parser fails to initialize, preventing potential thread hangs.
- Parameters:
digCalcProvider- provider for digest calculators.message- the multipart message containing the content and signature.defaultEncoding- the default content transfer encoding to use.backingFile- a file to use for backing the message content.- Returns:
- a new SMIMESignedParser instance.
- Throws:
javax.mail.MessagingException- on an error extracting the signature or processing the message.org.bouncycastle.cms.CMSException- if an internal CMS error occurs.
-
getSafeInstance
public static SMIMESignedParser getSafeInstance(org.bouncycastle.operator.DigestCalculatorProvider digCalcProvider, javax.mail.Part message) throws javax.mail.MessagingException, org.bouncycastle.cms.CMSException Create a new SMIMESignedParser from a Part.This method ensures that if the input stream for the signature is a PipedInputStream, it will be closed automatically if the parser fails to initialize.
- Parameters:
digCalcProvider- provider for digest calculators.message- the message part containing the signed data.- Returns:
- a new SMIMESignedParser instance.
- Throws:
javax.mail.MessagingException- on an error extracting the signature or processing the message.org.bouncycastle.cms.CMSException- if an internal CMS error occurs.
-
getSafeInstance
public static SMIMESignedParser getSafeInstance(org.bouncycastle.operator.DigestCalculatorProvider digCalcProvider, javax.mail.Part message, File file) throws javax.mail.MessagingException, org.bouncycastle.cms.CMSException Create a new SMIMESignedParser from a Part with a backing file.This method ensures that if the input stream for the signature is a PipedInputStream, it will be closed automatically if the parser fails to initialize.
- Parameters:
digCalcProvider- provider for digest calculators.message- the message part containing the signed data.file- the backing file to use for the message content.- Returns:
- a new SMIMESignedParser instance.
- Throws:
javax.mail.MessagingException- on an error extracting the signature or processing the message.org.bouncycastle.cms.CMSException- if an internal CMS error occurs.
-
getContent
public javax.mail.internet.MimeBodyPart getContent()return the content that was signed.- Returns:
- the signed body part in this message.
-
getContentAsMimeMessage
public javax.mail.internet.MimeMessage getContentAsMimeMessage(javax.mail.Session session) throws javax.mail.MessagingException, IOException Return the content that was signed as a mime message.- Parameters:
session- the session to base the MimeMessage around.- Returns:
- a MimeMessage holding the content.
- Throws:
javax.mail.MessagingException- if there is an issue creating the MimeMessage.IOException- if there is an issue reading the content.
-
getContentWithSignature
return the content that was signed with its signature attached.- Returns:
- depending on whether this was unencapsulated or not it will return a MimeMultipart or a MimeBodyPart
-