| 1 |
/* |
| 2 |
* Copyright 1999-2010 University of Chicago |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 |
* express or implied. |
| 12 |
* |
| 13 |
* See the License for the specific language governing permissions and limitations under the License. |
| 14 |
*/ |
| 15 |
package org.globus.gsi; |
| 16 |
|
| 17 |
import org.globus.gsi.util.CertificateIOUtil; |
| 18 |
import org.globus.gsi.util.CertificateLoadUtil; |
| 19 |
import org.globus.gsi.util.CertificateUtil; |
| 20 |
import org.globus.gsi.util.ProxyCertificateUtil; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
import java.security.Security; |
| 26 |
import java.security.Provider; |
| 27 |
import java.security.GeneralSecurityException; |
| 28 |
import java.security.cert.CertificateEncodingException; |
| 29 |
import java.security.cert.X509Certificate; |
| 30 |
import java.security.cert.X509CRL; |
| 31 |
import java.security.Principal; |
| 32 |
import java.io.BufferedReader; |
| 33 |
import java.io.InputStream; |
| 34 |
import java.io.OutputStream; |
| 35 |
import java.io.IOException; |
| 36 |
import org.globus.util.I18n; |
| 37 |
import org.globus.common.CoGProperties; |
| 38 |
import org.apache.commons.logging.Log; |
| 39 |
import org.apache.commons.logging.LogFactory; |
| 40 |
|
| 41 |
import org.bouncycastle.jce.provider.BouncyCastleProvider; |
| 42 |
|
| 43 |
|
| 44 |
/** |
| 45 |
* Contains various security-related utility methods. |
| 46 |
* @deprecated Use the various utils that are used here. |
| 47 |
*/ |
| 48 |
public class CertUtil { |
| 49 |
|
| 50 |
/** |
| 51 |
* A no-op function that can be used to force the class |
| 52 |
* to load and initialize. |
| 53 |
*/ |
| 54 |
public static void init() { |
| 55 |
CertificateUtil.init(); |
| 56 |
CertificateLoadUtil.init(); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Installs SecureRandom provider. |
| 61 |
* This function is automatically called when this class is loaded. |
| 62 |
*/ |
| 63 |
public static void installSecureRandomProvider() { |
| 64 |
CertificateUtil.installSecureRandomProvider(); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Sets a provider name to use for loading certificates |
| 69 |
* and for generating key pairs. |
| 70 |
* |
| 71 |
* @param providerName provider name to use. |
| 72 |
*/ |
| 73 |
public static void setProvider(String providerName) { |
| 74 |
CertificateUtil.setProvider(providerName); |
| 75 |
CertificateLoadUtil.setProvider(providerName); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Loads a X509 certificate from the specified input stream. |
| 80 |
* Input stream must contain DER-encoded certificate. |
| 81 |
* |
| 82 |
* @param in the input stream to read the certificate from. |
| 83 |
* @return <code>X509Certificate</code> the loaded certificate. |
| 84 |
* @exception GeneralSecurityException if certificate failed to load. |
| 85 |
*/ |
| 86 |
public static X509Certificate loadCertificate(InputStream in) |
| 87 |
throws GeneralSecurityException { |
| 88 |
return CertificateLoadUtil.loadCertificate(in); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Loads an X.509 certificate from the specified file. |
| 93 |
* The certificate file must be in PEM/Base64 format and start with |
| 94 |
* "BEGIN CERTIFICATE" and end with "END CERTIFICATE" line. |
| 95 |
* |
| 96 |
* @param file the file to load the certificate from. |
| 97 |
* @return <code>java.security.cert.X509Certificate</code> |
| 98 |
* the loaded certificate. |
| 99 |
* @exception IOException if I/O error occurs |
| 100 |
* @exception GeneralSecurityException if security problems occurs. |
| 101 |
*/ |
| 102 |
public static X509Certificate loadCertificate(String file) |
| 103 |
throws IOException, GeneralSecurityException { |
| 104 |
return CertificateLoadUtil.loadCertificate(file); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Loads multiple X.509 certificates from the specified file. |
| 109 |
* Each certificate must be in PEM/Base64 format and start with |
| 110 |
* "BEGIN CERTIFICATE" and end with "END CERTIFICATE" line. |
| 111 |
* |
| 112 |
* @param file the certificate file to load the certificate from. |
| 113 |
* @return an array of certificates loaded from the file. |
| 114 |
* @exception IOException if I/O error occurs |
| 115 |
* @exception GeneralSecurityException if security problems occurs. |
| 116 |
*/ |
| 117 |
public static X509Certificate[] loadCertificates(String file) |
| 118 |
throws IOException, GeneralSecurityException { |
| 119 |
return CertificateLoadUtil.loadCertificates(file); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Loads a X.509 certificate from the specified reader. |
| 124 |
* The certificate contents must start with "BEGIN CERTIFICATE" line |
| 125 |
* and end with "END CERTIFICATE" line, and be in PEM/Base64 format. |
| 126 |
* |
| 127 |
* This function does not close the input stream. |
| 128 |
* |
| 129 |
* @param reader the stream from which load the certificate. |
| 130 |
* @return the loaded certificate or null if there was no certificate |
| 131 |
* in the stream or the stream is closed. |
| 132 |
* @exception IOException if I/O error occurs |
| 133 |
* @exception GeneralSecurityException if security problems occurs. |
| 134 |
*/ |
| 135 |
public static X509Certificate readCertificate(BufferedReader reader) |
| 136 |
throws IOException, GeneralSecurityException { |
| 137 |
return CertificateLoadUtil.readCertificate(reader); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Writes certificate to the specified output stream in PEM format. |
| 142 |
*/ |
| 143 |
public static void writeCertificate(OutputStream out, |
| 144 |
X509Certificate cert) |
| 145 |
throws IOException, CertificateEncodingException { |
| 146 |
CertificateIOUtil.writeCertificate(out, cert); |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Converts DN of the form "CN=A, OU=B, O=C" into Globus |
| 151 |
* format "/CN=A/OU=B/O=C".<BR> |
| 152 |
* This function might return incorrect Globus-formatted ID when one of |
| 153 |
* the RDNs in the DN contains commas. |
| 154 |
* @see #toGlobusID(String, boolean) |
| 155 |
* |
| 156 |
* @param dn the DN to convert to Globus format. |
| 157 |
* @return the converted DN in Globus format. |
| 158 |
*/ |
| 159 |
public static String toGlobusID(String dn) { |
| 160 |
return CertificateUtil.toGlobusID(dn); |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Converts DN of the form "CN=A, OU=B, O=C" into Globus |
| 165 |
* format "/CN=A/OU=B/O=C" or "/O=C/OU=B/CN=A" depending on the |
| 166 |
* <code>noreverse</code> option. If <code>noreverse</code> is true |
| 167 |
* the order of the DN components is not reveresed - "/CN=A/OU=B/O=C" is |
| 168 |
* returned. If <code>noreverse</code> is false, the order of the |
| 169 |
* DN components is reversed - "/O=C/OU=B/CN=A" is returned. <BR> |
| 170 |
* This function might return incorrect Globus-formatted ID when one of |
| 171 |
* the RDNs in the DN contains commas. |
| 172 |
* |
| 173 |
* @param dn the DN to convert to Globus format. |
| 174 |
* @param noreverse the direction of the conversion. |
| 175 |
* @return the converted DN in Globus format. |
| 176 |
*/ |
| 177 |
public static String toGlobusID(String dn, boolean noreverse) { |
| 178 |
return CertificateUtil.toGlobusID(dn, noreverse); |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Converts the specified principal into Globus format. |
| 183 |
* If the principal is of unrecognized type a simple string-based |
| 184 |
* conversion is made using the {@link #toGlobusID(String) toGlobusID()} |
| 185 |
* function. |
| 186 |
* |
| 187 |
* @see #toGlobusID(String) |
| 188 |
* |
| 189 |
* @param name the principal to convert to Globus format. |
| 190 |
* @return the converted DN in Globus format. |
| 191 |
*/ |
| 192 |
public static String toGlobusID(Principal name) { |
| 193 |
return CertificateUtil.toGlobusID(name); |
| 194 |
} |
| 195 |
|
| 196 |
// proxy utilies |
| 197 |
|
| 198 |
/** |
| 199 |
* Determines if a specified certificate type indicates a GSI-2, |
| 200 |
* GSI-3 or GSI-4proxy certificate. |
| 201 |
* |
| 202 |
* @param certType the certificate type to check. |
| 203 |
* @return true if certType is a GSI-2 or GSI-3 or GSI-4 proxy, false |
| 204 |
* otherwise. |
| 205 |
*/ |
| 206 |
public static boolean isProxy(int certType) { |
| 207 |
return ProxyCertificateUtil.isProxy(GSIConstants.CertificateType.get(certType)); |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Determines if a specified certificate type indicates a |
| 212 |
* GSI-4 proxy certificate. |
| 213 |
* |
| 214 |
* @param certType the certificate type to check. |
| 215 |
* @return true if certType is a GSI-4 proxy, false |
| 216 |
* otherwise. |
| 217 |
*/ |
| 218 |
public static boolean isGsi4Proxy(int certType) { |
| 219 |
return ProxyCertificateUtil.isGsi4Proxy(GSIConstants.CertificateType.get(certType)); |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Determines if a specified certificate type indicates a |
| 224 |
* GSI-3 proxy certificate. |
| 225 |
* |
| 226 |
* @param certType the certificate type to check. |
| 227 |
* @return true if certType is a GSI-3 proxy, false |
| 228 |
* otherwise. |
| 229 |
*/ |
| 230 |
public static boolean isGsi3Proxy(int certType) { |
| 231 |
return ProxyCertificateUtil.isGsi3Proxy(GSIConstants.CertificateType.get(certType)); |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* Determines if a specified certificate type indicates a |
| 236 |
* GSI-2 proxy certificate. |
| 237 |
* |
| 238 |
* @param certType the certificate type to check. |
| 239 |
* @return true if certType is a GSI-2 proxy, false |
| 240 |
* otherwise. |
| 241 |
*/ |
| 242 |
public static boolean isGsi2Proxy(int certType) { |
| 243 |
return ProxyCertificateUtil.isGsi2Proxy(GSIConstants.CertificateType.get(certType)); |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
* Determines if a specified certificate type indicates a |
| 248 |
* GSI-2 or GSI-3 or GSI=4 limited proxy certificate. |
| 249 |
* |
| 250 |
* @param certType the certificate type to check. |
| 251 |
* @return true if certType is a GSI-2 or GSI-3 or GSI-4 limited proxy, |
| 252 |
* false otherwise. |
| 253 |
*/ |
| 254 |
public static boolean isLimitedProxy(int certType) { |
| 255 |
return ProxyCertificateUtil.isLimitedProxy(GSIConstants.CertificateType.get(certType)); |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* Determines if a specified certificate type indicates a |
| 260 |
* GSI-3 or GS-4 limited proxy certificate. |
| 261 |
* |
| 262 |
* @param certType the certificate type to check. |
| 263 |
* @return true if certType is a GSI-3 or GSI-4 independent proxy, |
| 264 |
* false otherwise. |
| 265 |
*/ |
| 266 |
public static boolean isIndependentProxy(int certType) { |
| 267 |
return ProxyCertificateUtil.isIndependentProxy(GSIConstants.CertificateType.get(certType)); |
| 268 |
} |
| 269 |
|
| 270 |
/** |
| 271 |
* Determines if a specified certificate type indicates a |
| 272 |
* GSI-2 or GSI-3 or GSI-4 impersonation proxy certificate. |
| 273 |
* |
| 274 |
* @param certType the certificate type to check. |
| 275 |
* @return true if certType is a GSI-2 or GSI-3 or GSI-4 |
| 276 |
* impersonation proxy, false otherwise. |
| 277 |
*/ |
| 278 |
public static boolean isImpersonationProxy(int certType) { |
| 279 |
return ProxyCertificateUtil.isImpersonationProxy(GSIConstants.CertificateType.get(certType)); |
| 280 |
} |
| 281 |
|
| 282 |
/** |
| 283 |
* Returns a string description of a specified proxy |
| 284 |
* type. |
| 285 |
* |
| 286 |
* @param proxyType the proxy type to get the string |
| 287 |
* description of. |
| 288 |
* @return the string description of the proxy type. |
| 289 |
*/ |
| 290 |
public static String getProxyTypeAsString(int proxyType) { |
| 291 |
return ProxyCertificateUtil.getProxyTypeAsString(GSIConstants.CertificateType.get(proxyType)); |
| 292 |
} |
| 293 |
|
| 294 |
/** |
| 295 |
* Checks if GSI-3 mode is enabled. |
| 296 |
* |
| 297 |
* @return true if <I>"org.globus.gsi.version"</I> system property |
| 298 |
* is set to "3". Otherwise, false. |
| 299 |
*/ |
| 300 |
public static boolean isGsi3Enabled() { |
| 301 |
return VersionUtil.isGsi3Enabled(); |
| 302 |
} |
| 303 |
|
| 304 |
/** |
| 305 |
* Checks if GSI-2 mode is enabled. |
| 306 |
* |
| 307 |
* @return true if <I>"org.globus.gsi.version"</I> system property |
| 308 |
* is set to "2". Otherwise, false. |
| 309 |
*/ |
| 310 |
public static boolean isGsi2Enabled() { |
| 311 |
return VersionUtil.isGsi2Enabled(); |
| 312 |
} |
| 313 |
|
| 314 |
// CRL Utilities JGLOBUS-91 |
| 315 |
public static X509CRL loadCrl(String file) |
| 316 |
throws IOException, GeneralSecurityException { |
| 317 |
return CertificateLoadUtil.loadCrl(file); |
| 318 |
} |
| 319 |
|
| 320 |
public static X509CRL loadCrl(InputStream in) |
| 321 |
throws GeneralSecurityException { |
| 322 |
return CertificateLoadUtil.loadCrl(in); |
| 323 |
} |
| 324 |
|
| 325 |
} |