001/* 002 * ==================================================================== 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, 014 * software distributed under the License is distributed on an 015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 016 * KIND, either express or implied. See the License for the 017 * specific language governing permissions and limitations 018 * under the License. 019 * ==================================================================== 020 * 021 * This software consists of voluntary contributions made by many 022 * individuals on behalf of the Apache Software Foundation. For more 023 * information on the Apache Software Foundation, please see 024 * <http://www.apache.org/>. 025 * 026 */ 027 028package org.apache.http.ssl; 029 030import java.security.KeyManagementException; 031import java.security.NoSuchAlgorithmException; 032 033import javax.net.ssl.SSLContext; 034 035/** 036 * {@link javax.net.ssl.SSLContext} factory methods. 037 * 038 * @since 4.4 039 */ 040public class SSLContexts { 041 042 /** 043 * Creates default factory based on the standard JSSE trust material 044 * ({@code cacerts} file in the security properties directory). System properties 045 * are not taken into consideration. 046 * 047 * @return the default SSL socket factory 048 */ 049 public static SSLContext createDefault() throws SSLInitializationException { 050 try { 051 final SSLContext sslContext = SSLContext.getInstance(SSLContextBuilder.TLS); 052 sslContext.init(null, null, null); 053 return sslContext; 054 } catch (final NoSuchAlgorithmException ex) { 055 throw new SSLInitializationException(ex.getMessage(), ex); 056 } catch (final KeyManagementException ex) { 057 throw new SSLInitializationException(ex.getMessage(), ex); 058 } 059 } 060 061 /** 062 * Creates default SSL context based on system properties. This method obtains 063 * default SSL context by calling {@code SSLContext.getInstance("Default")}. 064 * Please note that {@code Default} algorithm is supported as of Java 6. 065 * This method will fall back onto {@link #createDefault()} when 066 * {@code Default} algorithm is not available. 067 * 068 * @return default system SSL context 069 */ 070 public static SSLContext createSystemDefault() throws SSLInitializationException { 071 try { 072 return SSLContext.getDefault(); 073 } catch (final NoSuchAlgorithmException ex) { 074 return createDefault(); 075 } 076 } 077 078 /** 079 * Creates custom SSL context. 080 * 081 * @return default system SSL context 082 */ 083 public static SSLContextBuilder custom() { 084 return SSLContextBuilder.create(); 085 } 086 087}