1 /****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
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 ****************************************************************/
19
20
21
22 package org.apache.james.mailet.crypto;
23
24 import java.security.Provider;
25 import java.security.Security;
26
27 import javax.activation.CommandMap;
28 import javax.activation.MailcapCommandMap;
29
30 /**
31 * Security Providers initialization class. The first call of the init method
32 * will have the class loader do the job. This technique ensures proper
33 * initialization without the need of maintaining the
34 * <i>${java_home}/lib/security/java.security</i> file, that would otherwise
35 * need the addition of the following line:
36 * <code>security.provider.<i>n</i>=org.bouncycastle.jce.provider.BouncyCastleProvider</code>.
37 *
38 * The call also registers to the javamail's MailcapCommandMap the content
39 * handlers that are needed to work with s/mime mails.
40 *
41 */
42 public class InitJCE {
43 private static String bouncyCastleProviderClassName = "org.bouncycastle.jce.provider.BouncyCastleProvider";
44 private static boolean initialized = false;
45
46 /**
47 * Method that registers the security provider BouncyCastle as a system
48 * security provider. The provider class is dinamically loaded on runtime so
49 * there is no need to include the bouncycastle jar in the James
50 * distribution. It can be downloaded and installed by the user if she needs
51 * it.
52 */
53 public static void init() throws InstantiationException, IllegalAccessException, ClassNotFoundException {
54 if (!initialized) {
55 Security.addProvider((Provider)Class.forName(bouncyCastleProviderClassName).newInstance());
56
57 MailcapCommandMap mailcap = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
58
59 mailcap.addMailcap("application/pkcs7-signature;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.pkcs7_signature");
60 mailcap.addMailcap("application/pkcs7-mime;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.pkcs7_mime");
61 mailcap.addMailcap("application/x-pkcs7-signature;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.x_pkcs7_signature");
62 mailcap.addMailcap("application/x-pkcs7-mime;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.x_pkcs7_mime");
63 mailcap.addMailcap("multipart/signed;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.multipart_signed");
64
65 CommandMap.setDefaultCommandMap(mailcap);
66
67 initialized = true;
68 } else return;
69 }
70 }