001: /*
002: * Title: Oyster Project
003: * Description: S/MIME email sending capabilities
004: * @Author Vladan Obradovic, Vladimir Radisic
005: * @Version 2.1.6
006: */
007:
008: package org.enhydra.oyster.test;
009:
010: import org.enhydra.oyster.cms.consts.CapabilitiesConstants;
011: import javax.mail.Transport;
012: import org.enhydra.oyster.smime.SignedAndEnvelopedSMIME;
013: import org.enhydra.oyster.exception.SMIMEException;
014:
015: /**
016: * Tests enveloping and signing process. Enveloped and signed
017: * text/plain message with or withouth attachments can be sent by this test.
018: * To get help for this example type: "java org.enhydra.oyster.test.TestEncSig"
019: * in command line. It is assumed that oyster_tests.jar is in your classpath.<BR>
020: * <BR>
021: * Parameters passed to example are:<BR>
022: * <mailHost> <mailAddress> <cerFileName> <algorithmName>
023: * <digestAlgorithm> <includingCert> <includingSignAttrib>
024: * <pfxFileName> [<attachment>] <BR>
025: * <BR>
026: * <digestAlgorithm> could be: SHA1_WITH_RSA, MD2_WITH_RSA, MD5_WITH_RSA or SHA1_WITH_DSA.<BR>
027: * <includingCert> could be: true/false<BR>
028: * <includingSignAttrib> could be: true/false<BR>
029: * <algorithmName> could be: RC240, RC264, RC2128, 3DES or 3DES<BR>
030: * <BR>
031: * Note that for this example's passwords for .pfx or .p12 files are fixed to
032: * "together". All .pfx files or .p12 files provided with this example have this
033: * password. Also, email address "FROM" is fixed to: "sender@together.at".
034: * You should change this values in source code of TestEncSig.java in order to use
035: * them with other .pfx or .p12 files and corresponding "FROM" addresses and passwords.
036: */
037: public class TestEncSig {
038:
039: public static void main(String[] args) {
040:
041: String subject = "S/MIME enveloped and signed message - Subject test: ÜüÄäÖöÜüß";
042: String content = "S/MIME enveloped and signed message example\r\nContent test: ÜüÄäÖöÜüß!";
043: String from = "sender@together.at";
044: String password = "together";
045:
046: if (args.length < 8) {
047: System.err
048: .println(System.getProperty("line.separator")
049: + "Usage of TestEncSig: "
050: + System.getProperty("line.separator")
051: + "java TestEncSig <mailHost> <mailAddress> <cerFileName> "
052: + "<algorithmName> <digestAlgorithm> <includingCert> <includingSignAttrib> "
053: + "<pfxFileName> [<attachment>]"
054: + System.getProperty("line.separator")
055: + System.getProperty("line.separator")
056: + "Examples:"
057: + System.getProperty("line.separator")
058: + "java TestEncSig together.at recipient@together.at recipient512.cer "
059: + "RC240 SHA1_WITH_RSA true true sender512.pfx"
060: + System.getProperty("line.separator")
061: + "java TestEncSig together.at recipient@together.at recipient512.cer "
062: + "DES MD5_WITH_RSA true true sender512.pfx .\\test\\Zip8Test.zip");
063:
064: System.exit(-1);
065: }
066:
067: String smtpHost = args[0];
068: String addressTO = args[1];
069: String cerFileName = args[2];
070: String algorithmName = args[3];
071: String digestAlgorithm = args[4];
072:
073: boolean includingCert = true;
074: if (args[5].equals("true"))
075: includingCert = true;
076: else
077: includingCert = false;
078:
079: boolean includingSignAttrib = true;
080: if (args[6].equals("true"))
081: includingSignAttrib = true;
082: else
083: includingSignAttrib = false;
084:
085: String pfxfileName = args[7];
086:
087: String fileName = null;
088: if (args.length > 8)
089: fileName = args[8];
090:
091: String addressCC = "recipient@together.at";
092: String addressBCC = "recipient@together.at";
093:
094: subject = args[2] + " " + args[3] + " " + args[4] + " "
095: + args[5] + " " + args[6] + " " + args[7] + " "
096: + subject;
097:
098: SignedAndEnvelopedSMIME ess = null;
099:
100: try {
101: // Construction of enveloped and signed smime object
102: ess = new SignedAndEnvelopedSMIME(smtpHost, from, subject,
103: content, "ISO-8859-1");
104:
105: if (fileName != null) {
106: ess.addAttachment(fileName); // optional - use this if send attachment
107: }
108:
109: ess.setReply(from); // optional
110:
111: ess.addRecipient(addressTO, "TO", cerFileName); // mandatory
112: // ess.addRecipient(addressCC, "CC", cerFileName); // optional
113: // ess.addRecipient(addressBCC, "BCC", cerFileName); // optional
114:
115: ess.setCapabilities(CapabilitiesConstants.SYMMETRIC,
116: new String[] { CapabilitiesConstants.DES_EDE3_CBC,
117: CapabilitiesConstants.RC2_CBC_128,
118: CapabilitiesConstants.RC2_CBC_40,
119: CapabilitiesConstants.DES,
120: CapabilitiesConstants.RC2_CBC_64 }); // optional
121: ess.setCapabilities(CapabilitiesConstants.ENCIPHER,
122: new String[] { CapabilitiesConstants.RSA }); // optional
123: ess.setCapabilities(CapabilitiesConstants.SIGNATURE,
124: new String[] { CapabilitiesConstants.SHA1_WITH_RSA,
125: CapabilitiesConstants.MD5_WITH_RSA,
126: CapabilitiesConstants.MD2_WITH_RSA,
127: CapabilitiesConstants.SHA1_WITH_DSA }); // optional
128:
129: ess.addSigner(pfxfileName, password, digestAlgorithm,
130: includingCert, includingSignAttrib);
131:
132: if (algorithmName.equals("RC240")) {
133: System.out
134: .println("Creating enveloped and signed message with RC2 - 40 bits algorithm... ");
135: ess.signingAndEnveloping("ENCRYPT_FIRST"); // instead of this next line could be used
136: // ess.enveloping(ess.RC2_CBC, 40, "ENCRYPT_FIRST");
137: } else if (algorithmName.equals("RC264")) {
138: System.out
139: .println("Creating enveloped and signed message with RC2 - 64 bits algorithm... ");
140: ess.signingAndEnveloping(ess.RC2_CBC, 64,
141: "ENCRYPT_FIRST"); // send message with RC2 - 64 bits algorithm
142: } else if (algorithmName.equals("RC2128")) {
143: System.out
144: .println("Creating enveloped and signed message with RC2 - 128 bits algorithm... ");
145: ess.signingAndEnveloping(ess.RC2_CBC, 128,
146: "ENCRYPT_FIRST"); // send message with RC2 - 128 bits algorithm
147: } else if (algorithmName.equals("DES")) {
148: System.out
149: .println("Creating enveloped and signed message with DES algorithm... ");
150: ess.signingAndEnveloping(ess.DES, 56, "ENCRYPT_FIRST"); // send message with DES - 56 bits algorithm
151: } else if (algorithmName.equals("3DES")) {
152: System.out
153: .println("Creating enveloped and signed message with 3DES algorithm... ");
154: ess.signingAndEnveloping(ess.DES_EDE3_CBC, 192,
155: "ENCRYPT_FIRST"); // send message with 3DES - 192 bits algorithm
156: }
157:
158: System.out
159: .print("Sending enveloped and signed message ... ");
160: ess.send(); // instead of this next line could be used
161: // Transport.send(ess.getSignedMessage());
162: System.out.println("done.");
163:
164: } catch (Exception e) {
165: e.printStackTrace();
166: }
167: }
168: }
|