List of usage examples for org.bouncycastle.util Strings toLowerCase
public static String toLowerCase(String string)
From source file:br.ufpb.dicomflow.integrationAPI.mail.AbstractMailSender.java
License:Open Source License
private Message signAndEcrypt(Message message, X509Certificate signCert, X509Certificate encryptCert, PrivateKey privateKey) throws Exception { MailcapCommandMap mailcap = (MailcapCommandMap) CommandMap.getDefaultCommandMap(); mailcap.addMailcap(//from ww w .ja v a 2s. c om "application/pkcs7-signature;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.pkcs7_signature"); mailcap.addMailcap( "application/pkcs7-mime;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.pkcs7_mime"); mailcap.addMailcap( "application/x-pkcs7-signature;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.x_pkcs7_signature"); mailcap.addMailcap( "application/x-pkcs7-mime;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.x_pkcs7_mime"); mailcap.addMailcap( "multipart/signed;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.multipart_signed"); CommandMap.setDefaultCommandMap(mailcap); /* Create the Signer - SMIMESignedGenerator */ SMIMECapabilityVector capabilities = new SMIMECapabilityVector(); capabilities.addCapability(SMIMECapability.dES_EDE3_CBC); capabilities.addCapability(SMIMECapability.rC2_CBC, 128); capabilities.addCapability(SMIMECapability.dES_CBC); ASN1EncodableVector attributes = new ASN1EncodableVector(); attributes.add(new SMIMEEncryptionKeyPreferenceAttribute( new IssuerAndSerialNumber(new X500Name(((X509Certificate) signCert).getIssuerDN().getName()), ((X509Certificate) signCert).getSerialNumber()))); attributes.add(new SMIMECapabilitiesAttribute(capabilities)); SMIMESignedGenerator signer = new SMIMESignedGenerator(); signer.addSignerInfoGenerator(new JcaSimpleSignerInfoGeneratorBuilder() .setSignedAttributeGenerator(new AttributeTable(attributes)) .build("DSA".equals(privateKey.getAlgorithm()) ? "SHA1withDSA" : "MD5withRSA", privateKey, signCert)); /* Add the list of certs to the generator */ List certList = new ArrayList(); certList.add(signCert); Store certs = new JcaCertStore(certList); signer.addCertificates(certs); /* Sign the message */ MimeMultipart mm = signer.generate((MimeMessage) message); MimeMessage signedMessage = new MimeMessage(message.getSession()); /* Set all original MIME headers in the signed message */ Enumeration headers = ((MimeMessage) message).getAllHeaderLines(); while (headers.hasMoreElements()) { signedMessage.addHeaderLine((String) headers.nextElement()); } /* Set the content of the signed message */ signedMessage.setContent(mm); signedMessage.saveChanges(); /* Create the encrypter - SMIMEEnvelopedGenerator */ SMIMEEnvelopedGenerator encrypter = new SMIMEEnvelopedGenerator(); encrypter.addRecipientInfoGenerator(new JceKeyTransRecipientInfoGenerator(encryptCert)); /* Encrypt the message */ MimeBodyPart encryptedPart = encrypter.generate(signedMessage, new JceCMSContentEncryptorBuilder(CMSAlgorithm.RC2_CBC).build()); /* * Create a new MimeMessage that contains the encrypted and signed * content */ ByteArrayOutputStream out = new ByteArrayOutputStream(); encryptedPart.writeTo(out); MimeMessage encryptedMessage = new MimeMessage(message.getSession(), new ByteArrayInputStream(out.toByteArray())); /* Set all original MIME headers in the encrypted message */ headers = ((MimeMessage) message).getAllHeaderLines(); while (headers.hasMoreElements()) { String headerLine = (String) headers.nextElement(); /* * Make sure not to override any content-* headers from the * original message */ if (!Strings.toLowerCase(headerLine).startsWith("content-")) { encryptedMessage.addHeaderLine(headerLine); } } return encryptedMessage; }
From source file:com.cordys.coe.ac.emailio.outbound.EmailMessageFactory.java
License:Apache License
/** * This method encrypt the given email using the public keys for all senders. * * @param lMessages The list to add all individual messages to. * @param mmMessage The message to encrypt. * @param eicConfiguration The configuration of the connector. * @param sSession The JavaMail session to use. * * @throws OutboundEmailException In case of any exceptions. *///from w w w . j a v a 2 s . c om private static void encryptMessage(List<MimeMessage> lMessages, MimeMessage mmMessage, ISMIMEConfiguration eicConfiguration, Session sSession) throws OutboundEmailException { // Create the encrypter object SMIMEEnvelopedGenerator encrypter = new SMIMEEnvelopedGenerator(); try { // Add the public keys of all receivers to the encrypter. Address[] aaAdresses = mmMessage.getAllRecipients(); for (Address address : aaAdresses) { InternetAddress ia = (InternetAddress) address; // Find the public key for the given email address. ICertificateInfo ciRecipient = eicConfiguration.getCertificateInfo(ia.getAddress()); if ((ciRecipient == null) && !eicConfiguration.getBypassSMIME()) { throw new OutboundEmailException( OutboundEmailExceptionMessages.OEE_COULD_NOT_FIND_THE_PUBLIC_KEY_FOR_THE_EMAIL_ADDRESS_0, ia.toString()); } if (ciRecipient != null) { encrypter.addKeyTransRecipient(ciRecipient.getX509Certificate()); } else { // Now we could have a funny situation. The following might happen: a mail has // to send to 3 recipients. 1 has no certificate and the bypasssmime is enabled. // What to do now? We need to create a new version of the mail in plain text and // remove all recipients except this one. NOTE: We cannot avoid the recipient // getting 2 mails: 1 unreadable version and 1 plain one. This is beacuse we // cannot remove the recipient from the original message because that message // has been signed. if (eicConfiguration.getBypassSMIME()) { if (LOG.isDebugEnabled()) { LOG.debug( "BypassSMIME is enabled, so going to send a plain text version for user " + ia); } MimeMessage mmPlain = new MimeMessage(mmMessage); mmPlain.setRecipient(RecipientType.TO, ia); mmPlain.saveChanges(); lMessages.add(mmPlain); } else { // We need to throw an error since we cannot encrypt to all users. throw new OutboundEmailException( OutboundEmailExceptionMessages.OEE_COULD_NOT_FIND_A_CERTIFICATE_FOR_RECIPIENT_0, ia.toString()); } } } // Encrypt the message MimeBodyPart encryptedPart = encrypter.generate(mmMessage, SMIMEEnvelopedGenerator.DES_EDE3_CBC, "BC"); // Create a new MimeMessage that contains the encrypted and signed content ByteArrayOutputStream out = new ByteArrayOutputStream(); encryptedPart.writeTo(out); MimeMessage encryptedMessage = new MimeMessage(sSession, new ByteArrayInputStream(out.toByteArray())); // Set all original MIME headers in the encrypted message Enumeration<?> headers = mmMessage.getAllHeaderLines(); while (headers.hasMoreElements()) { String headerLine = (String) headers.nextElement(); // Make sure not to override any content-* headers from the original message if (!Strings.toLowerCase(headerLine).startsWith("content-")) { encryptedMessage.addHeaderLine(headerLine); } } // Add the encrypted message to the list so that it will be sent. lMessages.add(encryptedMessage); } catch (Exception e) { throw new OutboundEmailException(e, OutboundEmailExceptionMessages.OEE_ERROR_ENCRYPTING_THE_MAIL); } }
From source file:gov.nih.nci.cacis.nav.SendEncryptedMail.java
License:BSD License
private MimeMessage encryptMessage(MimeMessage message, Session session, Certificate cert) throws NoSuchAlgorithmException, NoSuchProviderException, SMIMEException, MessagingException, IOException {// w w w . j a v a 2 s.c o m /* Create the encrypter */ final SMIMEEnvelopedGenerator encrypter = new SMIMEEnvelopedGenerator(); encrypter.addKeyTransRecipient((X509Certificate) cert); /* Encrypt the message */ final MimeBodyPart encryptedPart = encrypter.generate(message, SMIMEEnvelopedGenerator.AES256_CBC, PROVIDER_TYPE); /* * Create a new MimeMessage that contains the encrypted and signed content */ final ByteArrayOutputStream out = new ByteArrayOutputStream(); encryptedPart.writeTo(out); final MimeMessage encryptedMessage = new MimeMessage(session, new ByteArrayInputStream(out.toByteArray())); /* Set all original MIME headers in the encrypted message */ final Enumeration headers = message.getAllHeaderLines(); while (headers.hasMoreElements()) { final String headerLine = (String) headers.nextElement(); /* * Make sure not to override any content-* headers from the original message */ if (!Strings.toLowerCase(headerLine).startsWith("content-")) { encryptedMessage.addHeaderLine(headerLine); } } return encryptedMessage; }
From source file:org.glite.slcs.pki.bouncycastle.X509PrincipalUtil.java
License:eu-egee.org license
/** * Reads the given {@link Attribute} and recurses into RDN attributes, fills * the given vectors.// w ww . j a v a 2 s . c o m * * @param attr * The {@link Attribute} to read. * @param oids * The vector of OID. * @param values * The vector of value. * @param added * The added status vector. * @throws NamingException * if a naming error occurs. */ private void readAttr(Attribute attr, Vector<DERObjectIdentifier> oids, Vector<Object> values, Vector<Boolean> added) throws NamingException { // Recursively looking into each attribute LOG.debug("Attribute: " + attr); for (int i = 0; i < attr.size(); i++) { if (attr.get(i) instanceof Attribute) { Attribute rdnAttr = (Attribute) attr.get(i); LOG.debug("Attribute RDN: " + rdnAttr); readAttr(rdnAttr, oids, values, added); } else { // Get back the OID from name DERObjectIdentifier oid = (DERObjectIdentifier) X509Name.DefaultLookUp .get(Strings.toLowerCase(attr.getID())); oids.add(oid); Object attrValue = attr.get(i); LOG.debug("Attribute value: " + attrValue); values.add(attrValue); added.add(start_); start_ = true; } } }
From source file:org.votingsystem.signature.util.Encryptor.java
License:Open Source License
/** * Method to encrypt SMIME signed messages *//* w w w. j a v a 2 s.c o m*/ public ResponseVS encryptSMIME(byte[] bytesToEncrypt, X509Certificate receiverCert) throws Exception { //If the message isn't recreated there can be problems with multipart boundaries. TODO SMIMEMessage msgToEncrypt = new SMIMEMessage(new ByteArrayInputStream(bytesToEncrypt)); SMIMEEnvelopedGenerator encryptor = new SMIMEEnvelopedGenerator(); encryptor.addRecipientInfoGenerator(new JceKeyTransRecipientInfoGenerator(receiverCert).setProvider("BC")); /* Encrypt the message */ MimeBodyPart encryptedPart = encryptor.generate(msgToEncrypt, new JceCMSContentEncryptorBuilder(CMSAlgorithm.DES_EDE3_CBC).setProvider("BC").build()); // Create a new MimeMessage that contains the encrypted and signed content /* Set all original MIME headers in the encrypted message */ Enumeration headers = msgToEncrypt.getAllHeaderLines(); while (headers.hasMoreElements()) { String headerLine = (String) headers.nextElement(); //log.info(" - headerLine: ${headerLine}"); /* * Make sure not to override any content-* headers from the * original message */ if (!Strings.toLowerCase(headerLine).startsWith("content-")) { encryptedPart.addHeaderLine(headerLine); } } /*SignerInformationStore signers = msgToEncrypt.getSmimeSigned().getSignerInfos(); Iterator<SignerInformation> it = signers.getSigners().iterator(); byte[] digestBytes = it.next().getContentDigest();//method can only be called after verify.*/ ByteArrayOutputStream baos = new ByteArrayOutputStream(); encryptedPart.writeTo(baos); return new ResponseVS(ResponseVS.SC_OK, baos.toByteArray()); }
From source file:org.votingsystem.signature.util.Encryptor.java
License:Open Source License
public static byte[] encryptSMIME(SMIMEMessage msgToEncrypt, X509Certificate receiverCert) throws Exception { /* Create the encryptor */ SMIMEEnvelopedGenerator encryptor = new SMIMEEnvelopedGenerator(); encryptor.addRecipientInfoGenerator( new JceKeyTransRecipientInfoGenerator(receiverCert).setProvider(ContextVS.PROVIDER)); /* Encrypt the message */ MimeBodyPart encryptedPart = encryptor.generate(msgToEncrypt, new JceCMSContentEncryptorBuilder(CMSAlgorithm.DES_EDE3_CBC).setProvider(ContextVS.PROVIDER) .build());/*from w ww . ja va2 s . co m*/ /* Set all original MIME headers in the encrypted message */ Enumeration headers = msgToEncrypt.getAllHeaderLines(); while (headers.hasMoreElements()) { String headerLine = (String) headers.nextElement(); log.info("headerLine: " + headerLine); /* * Make sure not to override any content-* headers from the * original message */ if (!Strings.toLowerCase(headerLine).startsWith("content-")) { encryptedPart.addHeaderLine(headerLine); } } /*SignerInformationStore signers = msgToEncrypt.getSmimeSigned().getSignerInfos(); Iterator<SignerInformation> it = signers.getSigners().iterator(); byte[] digestBytes = it.next().getContentDigest();//method can only be called after verify.*/ ByteArrayOutputStream baos = new ByteArrayOutputStream(); encryptedPart.writeTo(baos); byte[] result = baos.toByteArray(); baos.close(); return result; }