KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > oyster > test > TestSignedKeyStore


1 /*
2  * Title: Oyster Project
3  * Description: S/MIME email transport capabilities.
4  * @Author Vladimir Radisic
5  * @Version 2.1.5
6  */

7
8 package org.enhydra.oyster.test;
9
10 import org.enhydra.oyster.smime.SignedSMIME;
11 import org.enhydra.oyster.exception.SMIMEException;
12 import javax.mail.Transport JavaDoc;
13 import java.io.FileInputStream JavaDoc;
14 import java.io.File JavaDoc;
15
16 /**
17  * Tests signing process. Signed text/plain message with or withouth attachments
18  * can be sent by this test. This example is test for signing of email message
19  * by usage of keys and certificates from optional KeyStore storage file. To get
20  * help for this example type: "java org.enhydra.oyster.test.TestSignedKeyStore"
21  * in command line. It is assumed that oyster_tests.jar is in your classpath.<BR>
22  * <BR>
23  * Parameters passed to example are:<BR>
24  * &lt;mailHost&gt; &lt;mailAddress&gt; &lt;digestAlgorithm&gt; &lt;includingCert&gt;
25  * &lt;includingSignAttrib&gt; &lt;pfxFileName&gt; &lt;externalSigning&gt;
26  * [&lt;attachment&gt;]<BR>
27  * <BR>
28  * &lt;digestAlgorithm> could be: SHA1_WITH_RSA, MD2_WITH_RSA, MD5_WITH_RSA or SHA1_WITH_DSA.<BR>
29  * &lt;includingCert> could be: true/false<BR>
30  * &lt;includingSignAttrib> could be: true/false<BR>
31  * &lt;externalSigning> could be: true/false<BR>
32  * <BR>
33  * Note that password for KeyStore file ('keystore.ks') used in this example,
34  * is "together", and the KeyStore has type 'BKS'. You may change this values in
35  * source code of TestSignedKeyStore.java in order to use them with other
36  * KeyStore files. Also, email address "FROM" is fixed to: "sender@together.at",
37  * and should change too, in order to use them with other KeyStore file entryes.
38  * Aliases which exist in keystore.ks for corresponding private keys and
39  * certificate chains are: 'senderDSA512.pfx', 'recipient1024.pfx',
40  * 'recipient2048.pfx', 'recipient512.pfx', 'recipientDSA1024.pfx',
41  * 'recipientDSA512.pfx', 'sender1024.pfx', 'sender2048.pfx', 'sender512.pfx',
42  * 'senderDSA1024.pfx'.
43  */

44 public class TestSignedKeyStore
45 {
46
47   public static void main(String JavaDoc[] args)
48   {
49     String JavaDoc subject = "S/MIME signed message - Subject test: ";
50     String JavaDoc content = "S/MIME signed message example\r\nContent test: !";
51     String JavaDoc from = "sender@together.at";
52     String JavaDoc password = "together";
53     String JavaDoc keyStoreFile = "keystore.ks";
54
55     if (args.length < 7)
56     {
57       System.err.println(
58       System.getProperty("line.separator") +
59       "Usage of TestSignedKeyStore: " +
60       System.getProperty("line.separator") +
61       "java TestSigned <mailHost> <mailAddress> <digestAlgorithm> " +
62       "<includingCert> <includingSignAttrib> <pfxKeyStoreAlias> <externalSigning>" +
63       "[<attachment>]" +
64       System.getProperty("line.separator") +
65       System.getProperty("line.separator") +
66       "Examples:" +
67       System.getProperty("line.separator") +
68       "java TestSignedKeyStore together.at recipient@together.at SHA1_WITH_RSA true " +
69       "true sender512.pfx true"+
70       System.getProperty("line.separator") +
71       "java TestSignedKeyStore together.at recipient@together.at SHA1_WITH_DSA true " +
72       "true senderDSA512.pfx false .\\test\\Zip8Test.zip");
73       System.exit(-1);
74     }
75
76     String JavaDoc smtpHost = args[0];
77     String JavaDoc addressTO = args[1];
78     String JavaDoc digestAlgorithm = args[2];
79
80     boolean includingCert = true;
81     if (args[3].equalsIgnoreCase("true"))
82       includingCert = true;
83     else
84       includingCert = false;
85
86     boolean includingSignAttrib = true;
87     if (args[4].equalsIgnoreCase("true"))
88       includingSignAttrib = true;
89     else
90       includingSignAttrib = false;
91
92     String JavaDoc pfxfileName = args[5];
93
94     boolean externalSigning = true;
95     if (args[6].equalsIgnoreCase("true"))
96       externalSigning = true;
97     else
98       externalSigning = false;
99
100     String JavaDoc fileName = null;
101     if (args.length > 7)
102       fileName = args[7];
103
104     String JavaDoc addressCC = "recipient@together.at";
105     String JavaDoc addressBCC = "recipient@together.at";
106
107     subject = digestAlgorithm + " " + args[3] + " " + args[4] + " " +
108               pfxfileName + " " + args[6] + " " + subject;
109
110     SignedSMIME ss = null;
111
112     try {
113
114 // Construction of signed smime object
115
ss = new SignedSMIME(smtpHost, from, subject, content, "ISO-8859-1");
116
117       if (fileName!=null) {
118         ss.addAttachment(fileName); // optional - use this if send attachment
119
}
120
121       ss.setReply(from); // optional
122

123       ss.addRecipient(addressTO, "TO"); // mandatory
124
// ss.addRecipient(addressCC, "CC"); // optional
125
// ss.addRecipient(addressBCC, "BCC"); // optional
126

127       ss.setCapabilities("SYMMETRIC", 5, 3, 1, 4, 2); // optional
128
ss.setCapabilities("ENCIPHER", 1, 0, 0, 0, 0); // optional
129
ss.setCapabilities("SIGNATURE", 1, 2, 3, 4, 0); // optional
130

131       String JavaDoc alias = pfxfileName; // switch name to appropriate alias
132
ss.addSigner(keyStoreFile, ss.BKS, password, alias, digestAlgorithm,
133                                 includingCert, includingSignAttrib); // mandatory
134

135       System.out.println("Creating signed message with " + digestAlgorithm + " algorithm ... ");
136       ss.signing(externalSigning);
137       System.out.print("Sending signed message ... ");
138       ss.send(); // instead of this next line could be used
139
// Transport.send(es.getSignedMessage());
140
System.out.println("done.");
141     }
142     catch (Exception JavaDoc e) {
143       SMIMEException.setErrorFilePath("Log"); //specifies directory for logging
144
if(e instanceof SMIMEException) {
145         SMIMEException eTmp = (SMIMEException)e;
146 // eTmp.loggingErrors(null); //logging
147
eTmp.displayErrors(null);
148        e = eTmp.getNonSMIMEException();
149        if(e != null)
150          e.printStackTrace();
151       }
152       else {
153         e.printStackTrace();
154       }
155     }
156   }
157 }
Popular Tags