01: /****************************************************************
02: * Licensed to the Apache Software Foundation (ASF) under one *
03: * or more contributor license agreements. See the NOTICE file *
04: * distributed with this work for additional information *
05: * regarding copyright ownership. The ASF licenses this file *
06: * to you under the Apache License, Version 2.0 (the *
07: * "License"); you may not use this file except in compliance *
08: * with the License. You may obtain a copy of the License at *
09: * *
10: * http://www.apache.org/licenses/LICENSE-2.0 *
11: * *
12: * Unless required by applicable law or agreed to in writing, *
13: * software distributed under the License is distributed on an *
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15: * KIND, either express or implied. See the License for the *
16: * specific language governing permissions and limitations *
17: * under the License. *
18: ****************************************************************/package org.apache.james.security;
19:
20: import java.security.Provider;
21: import java.security.Security;
22:
23: import javax.activation.CommandMap;
24: import javax.activation.MailcapCommandMap;
25:
26: /**
27: * Security Providers initialization class. The first call of the init method
28: * will have the class loader do the job. This technique ensures proper
29: * initialization without the need of maintaining the
30: * <i>${java_home}/lib/security/java.security</i> file, that would otherwise
31: * need the addition of the following line:
32: * <code>security.provider.<i>n</i>=org.bouncycastle.jce.provider.BouncyCastleProvider</code>.
33: *
34: * The call also registers to the javamail's MailcapCommandMap the content
35: * handlers that are needed to work with s/mime mails.
36: *
37: */
38: public class InitJCE {
39: private static String bouncyCastleProviderClassName = "org.bouncycastle.jce.provider.BouncyCastleProvider";
40: private static boolean initialized = false;
41:
42: /**
43: * Method that registers the security provider BouncyCastle as a system
44: * security provider. The provider class is dinamically loaded on runtime so
45: * there is no need to include the bouncycastle jar in the James
46: * distribution. It can be downloaded and installed by the user if she needs
47: * it.
48: */
49: public static void init() throws InstantiationException,
50: IllegalAccessException, ClassNotFoundException {
51: if (!initialized) {
52: Security.addProvider((Provider) Class.forName(
53: bouncyCastleProviderClassName).newInstance());
54:
55: MailcapCommandMap mailcap = (MailcapCommandMap) CommandMap
56: .getDefaultCommandMap();
57:
58: mailcap
59: .addMailcap("application/pkcs7-signature;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.pkcs7_signature");
60: mailcap
61: .addMailcap("application/pkcs7-mime;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.pkcs7_mime");
62: mailcap
63: .addMailcap("application/x-pkcs7-signature;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.x_pkcs7_signature");
64: mailcap
65: .addMailcap("application/x-pkcs7-mime;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.x_pkcs7_mime");
66: mailcap
67: .addMailcap("multipart/signed;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.multipart_signed");
68:
69: CommandMap.setDefaultCommandMap(mailcap);
70:
71: initialized = true;
72: } else
73: return;
74: }
75: }
|