SMIMESignedParserpublic class SMIMESignedParser extends org.bouncycastle.cms.CMSSignedDataParser general class for handling a pkcs7-signature message.
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".
|
Fields Summary |
---|
Object | message | MimeBodyPart | content |
Constructors Summary |
---|
public SMIMESignedParser(MimeMultipart message)base constructor using a defaultContentTransferEncoding of 7bit. A temporary backing file
will be created for the signed data.
MailcapCommandMap mc = (MailcapCommandMap)CommandMap.getDefaultCommandMap();
mc.addMailcap("application/pkcs7-signature;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.pkcs7_signature");
mc.addMailcap("application/pkcs7-mime;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.pkcs7_mime");
mc.addMailcap("application/x-pkcs7-signature;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.x_pkcs7_signature");
mc.addMailcap("application/x-pkcs7-mime;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.x_pkcs7_mime");
mc.addMailcap("multipart/signed;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.multipart_signed");
CommandMap.setDefaultCommandMap(mc);
this(message, getTmpFile());
| public SMIMESignedParser(MimeMultipart message, File backingFile)base constructor using a defaultContentTransferEncoding of 7bit and a specified backing file.
this(message, "7bit", backingFile);
| public SMIMESignedParser(MimeMultipart message, String defaultContentTransferEncoding)base constructor with settable contentTransferEncoding. A temporary backing file will be created
to contain the signed data.
this(message, defaultContentTransferEncoding, getTmpFile());
| public SMIMESignedParser(MimeMultipart message, String defaultContentTransferEncoding, File backingFile)base constructor with settable contentTransferEncoding and a specified backing file.
super(getSignedInputStream(message.getBodyPart(0), defaultContentTransferEncoding, backingFile), getInputStream(message.getBodyPart(1)));
this.message = message;
this.content = (MimeBodyPart)message.getBodyPart(0);
drainContent();
| public SMIMESignedParser(Part message)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.
super(getInputStream(message));
this.message = message;
CMSTypedStream cont = this.getSignedContent();
if (cont != null)
{
this.content = SMIMEUtil.toWriteOnceBodyPart(cont);
}
| public SMIMESignedParser(Part message, File file)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.
super(getInputStream(message));
this.message = message;
CMSTypedStream cont = this.getSignedContent();
if (cont != null)
{
this.content = SMIMEUtil.toMimeBodyPart(cont, file);
}
|
Methods Summary |
---|
private void | drainContent()
try
{
this.getSignedContent().drain();
}
catch (IOException e)
{
throw new CMSException("unable to read content for verification: " + e, e);
}
| public javax.mail.internet.MimeBodyPart | getContent()return the content that was signed.
return content;
| public javax.mail.internet.MimeMessage | getContentAsMimeMessage(javax.mail.Session session)Return the content that was signed as a mime message.
if (message instanceof MimeMultipart)
{
BodyPart bp = ((MimeMultipart)message).getBodyPart(0);
return new MimeMessage(session, bp.getInputStream());
}
else
{
return new MimeMessage(session, getSignedContent().getContentStream());
}
| public java.lang.Object | getContentWithSignature()return the content that was signed with its signature attached.
return message;
| private static java.io.InputStream | getInputStream(javax.mail.Part bodyPart)
try
{
if (bodyPart.isMimeType("multipart/signed"))
{
throw new MessagingException("attempt to create signed data object from multipart content - use MimeMultipart constructor.");
}
return bodyPart.getInputStream();
}
catch (IOException e)
{
throw new MessagingException("can't extract input stream: " + e);
}
| private static org.bouncycastle.cms.CMSTypedStream | getSignedInputStream(javax.mail.BodyPart bodyPart, java.lang.String defaultContentTransferEncoding, java.io.File backingFile)
try
{
OutputStream out = new BufferedOutputStream(new FileOutputStream(backingFile));
SMIMEUtil.outputBodyPart(out, bodyPart, defaultContentTransferEncoding);
out.close();
InputStream in = new TemporaryFileInputStream(backingFile);
return new CMSTypedStream(in);
}
catch (IOException e)
{
throw new MessagingException("can't extract input stream: " + e);
}
| private static java.io.File | getTmpFile()
try
{
return File.createTempFile("bcMail", ".mime");
}
catch (IOException e)
{
throw new MessagingException("can't extract input stream: " + e);
}
|
|