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