001/** 002 * Java Web Archive Toolkit - Software to read and validate ARC, WARC 003 * and GZip files. (http://jwat.org/) 004 * Copyright 2011-2012 Netarkivet.dk (http://netarkivet.dk/) 005 * 006 * Licensed under the Apache License, Version 2.0 (the "License"); 007 * you may not use this file except in compliance with the License. 008 * You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.jwat.common; 019 020import java.lang.reflect.Constructor; 021import java.lang.reflect.InvocationTargetException; 022import java.security.Provider; 023import java.security.Security; 024 025/** 026 * Security Provider helper class. 027 * 028 * @author nicl 029 */ 030public class SecurityProviderTools { 031 032 /** 033 * Constructor not used as of yet. 034 */ 035 protected SecurityProviderTools() { 036 } 037 038 /** 039 * Returns an array of available security providers. 040 * @return array of available security providers 041 */ 042 public static Provider[] getSecurityProviders() { 043 return Security.getProviders(); 044 } 045 046 /** 047 * Check to see if a specific security provider is present in the supplied array of providers. 048 * @param providers array of security providers 049 * @param providerName provider name to check for 050 * @return true if the provider is present in the array, false otherwise 051 */ 052 public static boolean isProviderAvailable(Provider[] providers, String providerName) { 053 int idx = 0; 054 int len = providers.length; 055 boolean bAvail = false; 056 while (idx < len && !bAvail) { 057 bAvail = providerName.equalsIgnoreCase(providers[idx].getName()); 058 ++idx; 059 } 060 return bAvail; 061 } 062 063 /** 064 * Method to load the BouncyCastle(BC) security provider. 065 * @return true if the BouncyCastle security provider is available, false otherwise 066 */ 067 public static boolean loadBCProvider() { 068 return loadProvider("org.bouncycastle.jce.provider.BouncyCastleProvider"); 069 } 070 071 /** 072 * Attempt to load a security provider and add it to the system list of providers. 073 * @param providerName provider class name 074 * @return true if the security provider is now available, false otherwise 075 */ 076 public static boolean loadProvider(String providerName) { 077 Class<?> bcProviderClass = null; 078 Constructor<?> bcProviderConstructor = null; 079 Object bcProviderObject = null; 080 try { 081 bcProviderClass = SecurityProviderTools.class.getClassLoader().loadClass(providerName); 082 } 083 catch (ClassNotFoundException e) { 084 // Class will be null so no need to show exception. 085 } 086 try { 087 if (bcProviderClass != null) { 088 bcProviderConstructor = bcProviderClass.getConstructor(); 089 } 090 } 091 catch (NoSuchMethodException e) { 092 // TODO Auto-generated catch block 093 e.printStackTrace(); 094 } 095 catch (SecurityException e) { 096 // TODO Auto-generated catch block 097 e.printStackTrace(); 098 } 099 try { 100 if (bcProviderConstructor != null) { 101 bcProviderObject = bcProviderConstructor.newInstance(); 102 } 103 } 104 catch (InstantiationException e) { 105 // TODO Auto-generated catch block 106 e.printStackTrace(); 107 } 108 catch (IllegalAccessException e) { 109 // TODO Auto-generated catch block 110 e.printStackTrace(); 111 } 112 catch (IllegalArgumentException e) { 113 // TODO Auto-generated catch block 114 e.printStackTrace(); 115 } 116 catch (InvocationTargetException e) { 117 // TODO Auto-generated catch block 118 e.printStackTrace(); 119 } 120 if (bcProviderObject != null) { 121 //Security.addProvider(new BouncyCastleProvider()); 122 Security.addProvider((Provider)bcProviderObject); 123 return true; 124 } 125 return false; 126 } 127 128 /** 129 * Output a list of security providers to the console. 130 * @param providers list of security providers 131 */ 132 public static void printSecurityProviders(Provider[] providers) { 133 int idx = 0; 134 int len = providers.length; 135 Provider provider; 136 while (idx < len) { 137 provider = providers[idx]; 138 System.out.println(provider.getName() + " - " + provider.getInfo()); 139 ++idx; 140 } 141 } 142 143}